Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't Validation a Monad?

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

like image 619
Chris Avatar asked Aug 31 '12 08:08

Chris


People also ask

Why validated is not a monad?

Validation is not quite a monad as it doesn't quite follow the monad rules, even though it has the monad methods. Validation is similar to a Monad, and the difference is academic. Since we're only interested in practice, not theory, let's make Validation the next step in our monadic adventures in JavaScript.

Are lists monads?

List as a data structure is not a Monad, but the fact that Scala's List implements flatMap is what gives it its monadic super-powers. It also needs to fulfil associativity, left unit and right unit laws to qualify as a Monad.

Is monad a Typeclass?

The Monad class As of GHC 7.10, the Applicative typeclass is a superclass of Monad , and the Functor typeclass is a superclass of Applicative . This means that all monads are applicatives, all applicatives are functors, and therefore all monads are also functors.

Is either a monad?

Either is a monad, which has a map and flatMap functionality. We don't notice it now how handy Either becomes after it becomes a Monad. Either is right-biased, meaning the map and flatMap method can execute if the value is a “right” or “happy scenario”.


2 Answers

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.

like image 74
Debilski Avatar answered Oct 08 '22 02:10

Debilski


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

like image 34
oxbow_lakes Avatar answered Oct 08 '22 01:10

oxbow_lakes