[Haskell-cafe] left to right pipeline of functions

Csongor Kiss kiss.csongor.kiss at gmail.com
Sun Jun 12 06:07:08 UTC 2016


(>>=) :: Monad m => m a -> (a -> m b) -> m b, and if I understand correctly, what you need is a ‘generic’ version, that works without the returns?

'return :: a -> m a' for some monad m. This m comes from the monad instance you use, but if we want a generic one, it’s only possible by fixing a specific m. The obvious choice is to use the Identity monad and have a specialised (>>=‘) :: Identity a -> (a -> Identity b) -> Identity b.

For all `a`, `Identity a` is isomorphic to `a`, so with a bit of cheating, you could rewrite the above to (>>=‘’) :: a -> (a -> b) -> b,
which starts to look really familiar, in fact, it’s just the function composition (.) with its arguments flipped, and is actually defined
as (&) in Data.Function.

g' = 2 & \n -> (n + 1) & \n -> (n + 3)

This was obviously a really roundabout way of arriving at the result, but I think the analogy with function composition is really nice, as we can think of (a -> b) as a special case of (a -> m b), the so-called Kleisli arrow. What monads do is they compose these Kleisli arrows.

On 12 June 2016 at 06:47:13, Christopher Howard (ch.howard at zoho.com) wrote:

Hi, I am learning about monads, and this question came to mind: is there  
a way to have a sequence of functions left to right like so:  

g = return 2 >>= \n -> return (n + 1) >>= \n -> return (n + 3)  

Either: some kind of generic monad that makes this legal, or some way to  
do this without monad (i.e., without the "returns").  

To protect my privacy, please use PGP encryption. It's free and easy  
to use! My public key ID is 0x340EA95A (pgp.mit.edu).  

Haskell-Cafe mailing list  
Haskell-Cafe at haskell.org  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160612/03ede6f5/attachment.html>

More information about the Haskell-Cafe mailing list