Scala PartialFunction 可以是 Monoid 吗?
我认为 PartialFunction 可以是 Monoid。我的思维过程正确吗? 例如,
import scalaz._
import scala.{PartialFunction => -->}
implicit def partialFunctionSemigroup[A,B]:Semigroup[A-->B] = new Semigroup[A-->B]{
def append(s1: A-->B, s2: => A-->B): A-->B = s1.orElse(s2)
}
implicit def partialFunctionZero[A,B]:Zero[A-->B] = new Zero[A-->B]{
val zero = new (A-->B){
def isDefinedAt(a:A) = false
def apply(a:A) = sys.error("error")
}
}
但当前版本 Scalaz(6.0.4) 不包括在内。没有包含某些内容是否有原因?
I thought PartialFunction can be Monoid. Is my thought process correct ?
For example,
import scalaz._
import scala.{PartialFunction => -->}
implicit def partialFunctionSemigroup[A,B]:Semigroup[A-->B] = new Semigroup[A-->B]{
def append(s1: A-->B, s2: => A-->B): A-->B = s1.orElse(s2)
}
implicit def partialFunctionZero[A,B]:Zero[A-->B] = new Zero[A-->B]{
val zero = new (A-->B){
def isDefinedAt(a:A) = false
def apply(a:A) = sys.error("error")
}
}
But current version Scalaz(6.0.4) is not included it. Is there a reason for something not included ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们从不同的角度来看待这一点。
PartialFunction[A, B]
与A => 同构选项[B]
。 (实际上,为了能够检查它是否是为给定的A
定义的而不触发B
的计算,您需要A => LazyOption[B ]
)因此,如果我们能找到一个
Monoid[A =>;选项[B]]
我们已经证明了你的断言。给定Monoid[Z],我们可以形成Monoid[A => Z] 如下:
那么,如果我们使用
Option[B]
作为我们的Z
,我们会得到什么幺半群呢? Scalaz 提供了三个。主实例需要一个Semigroup[B]
。使用这个:
但这并不是组合两个选项的唯一方法。如果两个选项都是
Some
,我们可以简单地选择两个选项中的第一个或最后一个,而不是附加两个选项的内容。两个触发这个,我们用称为标记类型的技巧创建一个独特的类型。这在本质上与 Haskell 的newtype
类似。通过其
Monoid
附加的Option[A] @@ First
使用与示例相同的orElse
语义。因此,将所有这些放在一起:
Let's shine a different light on this.
PartialFunction[A, B]
is isomorphic toA => Option[B]
. (Actually, to be able to check if it is defined for a givenA
without triggering evaluation of theB
, you would needA => LazyOption[B]
)So if we can find a
Monoid[A => Option[B]]
we've proved your assertion.Given
Monoid[Z]
, we can formMonoid[A => Z]
as follows:So, what Monoid(s) do we have if we use
Option[B]
as ourZ
? Scalaz provides three. The primary instance requires aSemigroup[B]
.Using this:
But that's not the only way to combine two Options. Rather than appending the contents of the two options in the case they are both
Some
, we could simply pick the first or the last of the two. Two trigger this, we create a distinct type with trick called Tagged Types. This is similar in spirit to Haskell'snewtype
.Option[A] @@ First
, appended through it'sMonoid
, uses the sameorElse
semantics as your example.So, putting this all together:
不,这看起来不错,满足(非交换)幺半群的两个要求。有趣的想法。您想支持什么用例?
No, this looks good, satisfying both the requirements for (non-commutative) Monoid. Interesting idea. What use case are you trying to support?
你的零肯定违反了恒等元素的公理,但我认为恒等(部分)函数就可以了。
您的追加也不满足 Monoid 定律,但您可以调用 andThen (组合)来代替 orElse。但这仅适用于 A == B:
Your zero certainly violates the axiom for the identity element, but I think the identity (partial) function would be OK.
Your append also doesn't fulfill the Monoid laws, but instead of orElse you could call andThen (composition). But this would only work for A == B: