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 ?
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 givenAwithout 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 sameorElsesemantics as your example.So, putting this all together: