[Haskell-cafe] trying to understand monad transformers....
Ryan Ingram
ryani.spam at gmail.com
Tue Sep 9 04:51:45 EDT 2008
2008/9/8 Daryoush Mehrtash <dmehrtash at gmail.com>:
> The MaybeT transformer is defined as:
>
> > newtype MaybeT m a = MaybeT {runMaybeT :: m (Maybe a)}
>
> Question: What does "runMaybeT x" mean?
This is just shorthand for the following:
> newtype MaybeT m a = MaybeT (m (Maybe a))
> runMaybeT :: MaybeT m a -> m (Maybe a)
> runMaybeT (MaybeT x) = x
(with some minor differences to automated deriving of Show instances)
At runtime, runMaybeT and MaybeT are just really fast identity
functions (they should get optimized out of existence, even!)
So, if you have "x :: MaybeT m a" at runtime, you really just have
"runMaybeT x :: m (Maybe a)"
> instance Functor m => Functor (MaybeT m) where
> fmap f x = MaybeT $ fmap (fmap f) $ runMaybeT x
This code is a bit confusing at first because each fmap is at a different type.
The first (in the function declaration) is
> fmap :: Functor m => (a -> b) -> MaybeT m a -> MaybeT m b
The second (*fmap* (fmap f)) is
> fmap :: Functor m => (Maybe a -> Maybe b) -> m (Maybe a) -> m (Maybe b)
The third (fmap (*fmap* f)) is
> fmap :: (a -> b) -> Maybe a -> Maybe b
When you work with functors a lot you start to be able to read this
stuff more easily.
-- ryan
More information about the Haskell-Cafe
mailing list