[<<][compsci][>>][..]

Mon Feb 22 15:47:33 CET 2010

## How to explain Monads

Best one until now[1].
Given a couple of functions a -> m b where m is a type constructor,
how can one construct a composition of these functions given that the
wrapped types b correspond to the input type a of the next function?
Solution: Impose two requirements:
- m is a Functor, i.e. it has a map function that can lift a -> m b
to m a -> m (m b)
- there is a function join : m (m a) -> ma that can combine layers
The function that computes a Kleisli composition is:
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
In this picture a Comonad is also simple to understand.
Given a couple of functions w a -> b where w is a type constructor,
how can one construct a composition of these functions given that the
wrapped types a correspond to the output type b of the prvious
function?
Solution: Impose two requirements:
- w is a Functor, i.e. it has a map function that can lift w a -> b
to w (w a) -> w b
- there is a function duplicate : w a -> w (w b) that can combine layers
So why does haskell use bind (>>=) and not join? My guess is that
bind allows CPS-style code to look like assignments or list
comprehensions. So from a _usage_ point of view, bind seems more
natural than join.
[1] http://blog.sigfpe.com/2006/06/monads-kleisli-arrows-comonads-and.html

[Reply][About]

[<<][compsci][>>][..]