[Haskell-cafe] trying to understand monad transformers....

wren ng thornton wren at freegeek.org
Wed Sep 10 02:40:17 EDT 2008

wren ng thornton wrote:
> Daryoush Mehrtash wrote:
>> The MaybeT transformer is defined as:
>> newtype MaybeT m a = MaybeT {runMaybeT :: m (Maybe a)}
>> ....
>> Question:  What does "runMaybeT x" mean?
> As for "what does it do", I think everyone else has handled that pretty 
> well. As far as "what does it mean", it may help to think categorically.
> For every monad |m| we have another monad |MaybeT m|. If we ignore some 
> details, we can think of the transformed monad as |Maybe| composed with 
> |m|, sic: |Maybe . m|. With this perspective, runMaybeT is inverting 
> |MaybeT m| into |m . Maybe| by pushing the Maybe down over/through the 
> monad m. Hence we can envision the function as:
>  | runMaybeT           :: (MaybeT m) a -> (m . Maybe) a
>  | runMaybeT NothingT   = return Nothing
>  | runMaybeT (JustT ma) = fmap Just ma

Erh, whoops, I said that backwards. |MaybeT m| is like |m . Maybe| and 
runMaybeT breaks apart the implicit composition into an explicit one. 
The MaybeT monad just gives the additional possibility of a Nothing 
value at *each* object in |m a|.

To see why this is different than the above, consider where |m| is a 
list or Logic. |MaybeT []| says that each element of the list 
independently could be Nothing, whereas |ListT Maybe| says that we have 
either Nothing or Just xs. The pseudocode above gives the latter case 
since it assumes a top level Nothing means Nothing everywhere, which is 
wrong for |MaybeT m|.

Live well,

More information about the Haskell-Cafe mailing list