Monad Transformer question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Monad Transformer question

Fan Wu
Hi Haskell gurus,

I'm learning Haskell now and here I'm quite puzzled over some code
about Monad Transformers. The code is like:


   type NDS a = StateT ProblemState [] a

   getVar :: Var -> NDS (Maybe Value)
   getVar v = do vs <- gets vars
                 return $ lookup v vs


What puzzles me is that, I think the Monad of the do block shall be
the "NDS (Maybe Value)" in declaration, but the type of gets is

    gets :: (MonadState s m) => (s -> a) -> m a

So "gets" returns a Monad of type m ([] is this case), which seems to
be different from "NDS (Maybe Value)", but GHC does not complain about
it.


If I comment out the type declaration of "getVar :: Var -> NDS (Maybe
Value)" and let GHC interpret the type, then the type of getVar is
like:

    getVar :: (MonadState ProblemState m) => Var -> m (Maybe Value)

So does it mean "StateT ProblemState m" and the "m" as in "MonadState
ProblemState m" is the same thing?

I guess I must missed something when trying to understand the Monad
Transformers. Please give me some insights.

Thanks,
Fan
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Monad Transformer question

Andrew Pimlott-3
On Tue, Nov 22, 2005 at 05:26:00PM -0700, Fan Wu wrote:

>    type NDS a = StateT ProblemState [] a
>
>    getVar :: Var -> NDS (Maybe Value)
>    getVar v = do vs <- gets vars
>                  return $ lookup v vs
>
>
> What puzzles me is that, I think the Monad of the do block shall be
> the "NDS (Maybe Value)" in declaration, but the type of gets is
>
>     gets :: (MonadState s m) => (s -> a) -> m a
>
> So "gets" returns a Monad of type m ([] is this case), which seems to
> be different from "NDS (Maybe Value)", but GHC does not complain about
> it.
>
>
> If I comment out the type declaration of "getVar :: Var -> NDS (Maybe
> Value)" and let GHC interpret the type, then the type of getVar is
> like:
>
>     getVar :: (MonadState ProblemState m) => Var -> m (Maybe Value)
>
> So does it mean "StateT ProblemState m" and the "m" as in "MonadState
> ProblemState m" is the same thing?

No!  Check out the instance:

    instance (Monad m) => MonadState s (StateT s m) where ...

So you can see that the "m" in "MonadState ProblemState m" is actually
"StateT ProblemState m'" where m' is the inner monad, [] in your case.
So a correct type for gets as you use it is

    gets :: (ProblemState -> a) -> MonadState ProblemState [] a

Andrew
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe