Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Kleisli composition with functions returning Validations?

How do I compose two functions returning Validations? Following are my attempts that didn't work:

scala> def f: Int => Validation[String, Int] = i => if(i % 2 == 0) Success(i * 2) else Failure("Odd!")
f: Int => scalaz.Validation[String,Int]

scala> def g: Int => Validation[String, Int] = i => if(i > 0) Success(i + 1) else Failure("Not positive!")
g: Int => scalaz.Validation[String,Int]

scala> kleisli(f) >=> kleisli(g)
<console>:16: error: no type parameters for method kleisli: (f: A => M[B])scalaz.Kleisli[M,A,B] exist so that it can be applied to arguments (Int => scalaz.Validation[String,Int])
 --- because ---
argument expression's type is not compatible with formal parameter type;
 found   : Int => scalaz.Validation[String,Int]
 required: ?A => ?M[?B]

              kleisli(f) >=> kleisli(g)
              ^

scala> type Va[+A] = Validation[String, A]
defined type alias Va

scala> kleisli[Va, Int, Int](f) >=> kleisli[Va, Int, Int](g)
<console>:17: error: could not find implicit value for parameter b: scalaz.Bind[Va]
              kleisli[Va, Int, Int](f) >=> kleisli[Va, Int, Int](g)
                                       ^

This works in Haskell, so I am expecting there should be a way of doing this with Scalaz as well.

λ> let f i = if i `mod` 2 == 0 then Right $ i * 2 else Left "Odd!"
λ> let g i = if i > 0 then Right $ i + 1 else Left "Not positive!"
λ> let h = f >=> g
λ> h 11
Left "Odd!"
λ> h (-4)
Left "Not positive!"
λ> h 4
Right 9
like image 348
missingfaktor Avatar asked Dec 13 '11 13:12

missingfaktor


People also ask

What is Kleisli composition?

Kleisli composition lets you compose two (or more) compatible monadic functions into a single monadic function.

Why use Kleisli?

Kleisli enables composition of functions that return a monadic value, for instance an Option[Int] or a Either[String, List[Double]] , without having functions take an Option or Either as a parameter, which can be strange and unwieldy.

Is Kleisli a Monad?

In category theory, a Kleisli category is a category naturally associated to any monad T. It is equivalent to the category of free T-algebras.


1 Answers

you need to import Validation.Monad._ since >=> requires a Bind[M]

scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._

scala> def f: Int => Validation[String, Int] = i => if(i % 2 == 0) Success(i * 2) else    Failure("Odd!")
f: Int => scalaz.Validation[String,Int]

scala> def g: Int => Validation[String, Int] = i => if(i > 0) Success(i + 1) else Failure("Not positive!")
g: Int => scalaz.Validation[String,Int]

scala> type Va[+A] = Validation[String, A]
defined type alias Va

scala> import Validation.Monad._
import Validation.Monad._

scala> kleisli[Va, Int, Int](f) >=> kleisli[Va, Int, Int](g)
res0: scalaz.Kleisli[Va,Int,Int] = scalaz.Kleislis$$anon$1@4fae3fa6

scala> res0(11)
res1: Va[Int] = Failure(Odd!)

scala> res0(-4)
res2: Va[Int] = Failure(Not positive!)

scala> res0(4)
res3: Va[Int] = Success(9)
like image 127
Jon-Anders Teigen Avatar answered Oct 23 '22 05:10

Jon-Anders Teigen