After reading this article I understand that >=>
(Kleisli arrow) is just a higher order function to compose functions, that return "monadic values". For example:
val f: A => M[B] = ... val g: B => M[C] = ... val h: A => M[C] = f >=> g // compose f and g with Kleisli arrow
It looks like a simple composition of "simple" functions (i.e. pure functions that return simple values):
val f: A => B = ... val g: B => C = ... val h = f andThen g; // compose f and g
Now I guess this "simple" composition andThen
conforms to certain laws
f andThen g == g
and g andThen f == g
for identity function: f[A](a:A):A = a
(f1 andThen f2) andThen f3
== f1 andThen (f2 andThen f3)
And now my questions:
>=>
conform to those laws, where identity is f(a:A) = M[a].unit(a)
?Kleisli composition lets you compose two (or more) compatible monadic functions into a single monadic function.
Kleisli is a type of Arrow for a Monadic context. It is defined as: final case class Kleisli[F[_], A, B](run: A => F[B]) Kleisli Type Signature. The Kleisli type is a wrapper around A => F[B] , where F is some context that is a Monad.
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.
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.
What you have here is the immediate consequence of this construction being a category.
unit
is called like that: it is the unit under composition of Kleisli arrows.(f <=< g) x = f =<< (g x)
(where <=<
is andThen
, and =<<
is probably something like flip(bind)
in Scala). The exact steps of the derivation can be found here.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With