Kleisli is an operator to compose monadic functions. if you have one monadic function that takes in As and outputs Bs and another monadic function that takes in Bs and outputs Cs, and you want to chain them together, you use Kleisli composition.
Questions tagged [kleisli]
38 questions
21
votes
1 answer
Why isn't Kleisli an instance of Monoid?
If you wish to append two functions of type (a -> m b) so you get only one function of the same type appending both results, you could use Kleisli to do so:
instance (Monad m, Monoid b) => Monoid (Kleisli m a b) where
mempty = Kleisli (\_ ->…

leo
- 455
- 2
- 9
20
votes
1 answer
Function composition, Kleisli arrow, and Monadic laws
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…

Michael
- 41,026
- 70
- 193
- 341
19
votes
1 answer
Is it just a coincidence that Kleisli, ReaderT, and Reader are the same in Scalaz
In Scalaz
Kleisli[F, A, B] is a wrapper for A => F[B].
ReaderT[F, A, B] -- reader monad transformer -- is just an alias of Kleisli[F, A, B].
Reader[A, B] monad is a specialization of ReaderT with identity monad Id:
type Reader[A, B] =…

Michael
- 41,026
- 70
- 193
- 341
9
votes
1 answer
How to use >=> in Scala?
I am trying to use >=> (Kleisli arrow) in Scala. As I understand, it composes functions returning monads. Now I am trying it as follows:
scala> val f = {i:Int => Some(i + 1)}
f: Int => Some[Int] =
scala> val g = {i:Int =>…

Michael
- 41,026
- 70
- 193
- 341
9
votes
4 answers
How to use Kleisli arrows with monads?
In the Haskell Control.Arrow documentation it talks about Kleisli arrows' relationship to monads, but it is not obvious to me how to use this. I have a function which I think fits with arrows except for it involving the IO monad, so I think Kleisli…

Opa
- 335
- 2
- 7
7
votes
1 answer
Scalaz Kleisli usage benefits
In scalaz Kleisli[M[_], A, B] is a wrapper of A => M[B], which allows composition of such functions. For instance, if M[_] is monad I can compose Kleisli[M, A, B] and Kleisli[M, B, C] with >=> to get Kleisli[M, A, C].
In a nutshell, Kleisli provides…

Michael
- 41,026
- 70
- 193
- 341
6
votes
1 answer
Can I use monad transformers to simplify this composition?
suppose I have
type VS[A] = Validation[String, A]
val v: VS[Option[A]]
val f: A => VS[B]
I want to get a result of type VS[Option[B]] but if v is a Success(None), the result should also be a Success(None). Here's an example:
scala> val v:…

oxbow_lakes
- 133,303
- 56
- 317
- 449
5
votes
1 answer
f, g, h :: Kleisli ((->) e) a b <=> f >>> (g &&& h) = (f >>> g) &&& (f >>> h)?
Edit: We will call an arrow p pure if exists such function f that: p = arr f.
I'm trying to get a better grasp of Arrows in Haskell, and I want to figure out when
f >>> (g &&& h) = (f >>> g) &&& (f >>> h), where f, g, h are arrows.
Obviously, it…

Zhiltsoff Igor
- 1,812
- 8
- 24
5
votes
3 answers
List of Kleisli to Kleisli of list
I was wondering if there is a way to turn List[Kleisli[Option, Int, Int]] to Kleisli[Option, Int, List[Int]].
In particular I have the list of kleisli formed like this:
def k(a: String) = Kleisli[Option, Int, Int](m => Some(a.length * m))
val kList…

gurghet
- 7,591
- 4
- 36
- 63
5
votes
1 answer
How to implement caching with Kleisli
I've followed the design principle from the book Functional and Reactive Modeling.
So all the service methods return Kleisli.
The question is how can I add an updatable cache over these services.
Here is my current implementation, is there a better…

Yann Moisan
- 8,161
- 8
- 47
- 91
4
votes
2 answers
How to use Scala Cats' Kleisli with Either
I'm trying to use Kleisli to compose functions returning a monad. It works for an Option:
import cats.data.Kleisli
import cats.implicits._
object KleisliOptionEx extends App {
case class Failure(msg: String)
sealed trait Context
case class…

KaC
- 613
- 6
- 14
4
votes
1 answer
How to combine Kleisli[M, A, C] and Kleisli[M, B, C]
I follow the design of the excellent book Reactive Domain Modeling and I need to mix Kleisli with different types :
object CombinedKleisli {
type User = String
type Project = String
trait UserRepo
trait ProjectRepo
trait UserService {
…

Yann Moisan
- 8,161
- 8
- 47
- 91
4
votes
1 answer
Why is there no >=> semigroup for A => M[A] in Scalaz?
This is a followup to my previous question
Kleisli defines two operators <=< (compose) and >=> (andThen). The >=> looks very natural for me and I don't understand how <=< can be useful.
Moreover, it looks like there is no >=> semigroup for A => M[A]…

Michael
- 41,026
- 70
- 193
- 341
4
votes
0 answers
Lift Kleisli-like function to take monadic values as arguments
I'm writing ScalaCheck generators for my domain models. For added flexibility, my generator-returning functions take specific values for the associations. For example:
case class A(...)
case class B(...)
case class C(a: A, ...)
case class D(b: B, c:…

Tim Yates
- 5,151
- 2
- 29
- 29
3
votes
2 answers
Why does kleisli composition expect a pure value?
This is the common implementation for kleisli composition:
kleisli :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
kleisli = \f g x -> f x >>= g
Why doesn't it expect a value in a monadic context instead? I'm sure there is a good reason. I just…
user5536315