[Haskell-cafe] Monad Imparative Usage Example
Brian Hulley
brianh at metamilk.com
Sat Aug 5 13:04:16 EDT 2006
Ooops - more bugs in my explanation...
Brian Hulley wrote:
> -- from State.hs
> newtype State s a = S (s -> (a,s))
I used the source given in ghc-6.4.2\libraries\monads\Monad\State.hs but the
version of state monad that comes with the hierarchical libs is in
ghc-6.4.2\libraries\mtl\Control\Monad\State.hs - the bits related to the
explanation behave in the same way but you might find it interesting to
decide which implementation is more readable since mtl uses record syntax
and the other version doesn't.
> q >>= (\x -> p)
>
> means that both q and p are expressions that evaluate to monadic
> values ie values whose type is of the form
>
> S (s -> (a, s))
> So we have:
>
> q :: S (s -> (a, s))
> (\x -> p) :: a -> S (s -> (b, s))
Ooops! I meant:
q :: State s a
(\x -> p) :: a -> State s b
therefore the *value* of q is of the form S (s -> (a,s)) and the value of
(\x -> p) is of the form (a -> S(s -> (b, s)))
>
> To make the explanation simpler, we can rename the variables in the
> definition of >>= to reflect their types:
^^^^^^^^
"to reflect the structure of their values"
Apologies for the millions of corrections and re-posts for this
explanation - no matter how hard I try to proof read my posts something
always slips through... ;-)
Regards, Brian.
More information about the Haskell-Cafe
mailing list