[Haskell-cafe] monadic plumbing

Brandon Moore brandon_m_moore at yahoo.com
Tue Feb 22 22:29:27 CET 2011


>From: Alberto G. Corona  <agocorona at gmail.com>
>Sent: Tue, February 22, 2011 3:03:56 PM
>
>Recently I had to navigatate trough data structures chained with mutable 
>referenes in th STM monad. The problem is that their values are enveloped in 
> Either or Maybe results.
...
>to summarize, given:
>
> foo, :  a -> m (Maybe b)
> bar :   b -> m (Maybe c)
> baz :  c -> m (Maybe d)
>
>how to compose foo bar and baz? Or, at least,  Are something out there to handle 
>
>it in the less painful way?.

Control.Monad.Trans.Maybe.MaybeT
from mtl is defined as
newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) }
with (among others), instance Monad m => Monad (MaybeT m)

>I solved the generalized problem  (chaining  any double monadic combination) 
>with a sort of monadic connector that acts as a " double monadic" operator   
>>>>>==  so that
>
>return. return (x :: a) >>>>== foo >>>== bar >>>== baz  
>
>can be possible. Although I don't know if  it is the best solution. I wonder why 
>
>nobody has written about it before:
>
>class (Monad m, Monad n) => Bimonad m n where
> (>>>=)   ::  n a -> (a -> m(n b)) -> m(n b)

The standard construction uses a distributive law - if
you have an operation
distribute :: n (m a) -> m (n a),
then you can make m around n into a monad by

m (n (m (n a))) 
 == by fmap distribute =>
m (m (n (n a)))
 == by join (for m) =>
m (n (n a))
 == by fmap join (n's join) =>
m (n a)

Here, that's a function Maybe (m a) -> m (Maybe a), which can be

distribute (Just ma) = fmap Just ma
distribute Nothing = return Nothing

Brandon


      



More information about the Haskell-Cafe mailing list