[Haskell-cafe] New to Monads

Christian Maeder maeder at tzi.de
Fri Oct 7 06:26:09 EDT 2005


Tom Hawkins wrote:
> I'm just starting to feel comfortable working inside IO; now I am trying 
> to wrap my head around Control.Monad.State.  Looking at the 
> implementation, I came across some unfamiliar syntax...
> 
> class (Monad m) => MonadState s m | m -> s where

For a simple state monad the class MonadState is only important to 
supply the functions set, get and (indirectly) modify and gets.

More important to note (and independent of MonadState) is:

   instance Monad (State s)

This instance is not directly displayed by the haddock documentation, 
only (indirectly):

   instance MonadState s (State s)

> With IO, I understand one your in it, you can't get out.  I.e., any 
> function that conducts IO must have IO _ in the type signature.

"IO" is kind of a hidden type constructor whereas

   newtype State s a = State { runState :: s -> (a, s) }

is fully known.

Maybe it helps to view the "type IO a" as "State RealWorld a" where 
"Realworld" is never exposed (and of course without "instance MonadState 
RealWorld IO").

Christian


More information about the Haskell-Cafe mailing list