I recently stumbled on the concept of a Kleisli and every tutorial/link/reference that I read motivates the use of Kleisli via the following constructs:
- Composing functions that return monads:
f: a -> m[b]
withg: b -> m[c]
- I think the very definition of a monad already captures this case -do/bind/for/flatMap
do that. One needn't lean on the Kleisli construct to achieve this. So this cannot be the "primary" use case of a Kleisli IMO. - Inserting configuration: This one states that if multiple objects (types, case/data classes etc.,) need to have a
Config
injected then a Kleisli construct can be used to abstract away the repeatable injection. There are numerous ways of achieving this (for example withimplicit
s in Scala) that invoking a Kleisli may not be necessary. Again, IMO this doesn't stand out as a "primary" use case. - Monad Transformers: I don't have a solid understanding of this but here's my interpretation: If you have the need of "composing monads" you need a construct that allows you to parameterize the monads themselves. For example
M1[M2[M1[M2[a]]]]
could be transformed to[M1[M2[a]]]
which could (I may be wrong) be flattened across monadic boundaries to be composable with ana -> M3[b]
(say). For this one could us a Kleisli triple and invoke the construct since if you were to do it from scratch you may just reinvent the Kleisli. This seems to be a good candidate for justifying the use of a Kleisli. Is this correct?
I believe #1-#2
above are "secondary uses". That is, if you do happen to use the Kleisli construct, you can also get patterns for composing functions that return monads as well as config injection. However, they cannot be motivating problems advocating the power of Kleislis.
Under the assumption of using the least powerful abstraction to solve the problem at hand, what motivating problems can be used to showcase their use?
Alternate Thesis: It's entirely possible that I am totally wrong and my understanding of Kleislis is incorrect. I lack the necessary category theory background, but it could be that a Kleisli is an orthogonal construct that can be used in place of monads and they (Kleisli) are a category theoretic lens through which we view the problems of the functional world (i.e., a Klesli simply wraps a monadic function a -> M[b]
and now we can work at a higher level of abstraction where the function is the object of manipulation vs an object of usage). Thus, the use of Kleisli can be simply understood to be "Functional Programming with Kleisli". If this is true, then there ought to be a situation where a Kleisli can solve a problem better than existing constructs and we circle back to the issue of a motivating problem. It's equally likely, that there isn't such a motivating problem per se, if it's simply a lens which offers different solutions to the same problem. Which is it?
It'd be really helpful to get some input be able to reconstruct the need for Kleislis.