why isn't Validation a Monad? (scalaz7)

蹲街弑〆低调 提交于 2019-11-26 22:45:35

问题


an example use case:

def div2(i: Int): Validation[String, Int] = 
    if (i%2 == 0) Validation.success(i/2)
    else Validation.failure("odd")

def div4(i: Int) = for {
    a <- div2(i)
    b <- div2(a)
} yield b

error: Unable to unapply type scalaz.Validation[String,Int] into a type constructor of kind M[_] that is classified by the type class scalaz.Bind

I guess the error is caused by the compiler can't find a Monad instance for Validation[String, Int]

I can make one for myself, like:

object Instances {
implicit def validationMonad[E] = new Monad[({type L[A] = Validation[E, A]})#L] {
    override def point[A](a: => A) =
        Validation.success(a)
    override def bind[A, B](fa: Validation[E, A])(f: A => Validation[E, B]) =
        fa bind f
}
}

but why doesn't Validation have it already? after all, Validation already has the bind method defined.

moreover, I can't have import Validation._ and import Instances._ together anymore (this took me looong to figure out...), because of another complicated error...
ambiguous implicit values: something like both validationMonad (my instance), and method ValidationInstances1 in trait ValidationInstances2... both match some Functor of Validation...

should I modify the source of scalaz? or I'm completely missing something~?
please help~

I'm using scalaz 7.0.0-M2


回答1:


As discussed in the Scalaz group, the problem seems to be that ap would accumulate errors whereas (pseudo-)monadic composition would only operate on the value part of Validation.

Therefore, one cannot be expressed in terms of the other and thus no monad instance exists for Validation.




回答2:


The issue is that the applicative functor as implied by the monad does not equal the actual applicative functor



来源:https://stackoverflow.com/questions/12211776/why-isnt-validation-a-monad-scalaz7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!