[Haskell-cafe] questions on lazy pattern, StateT monad
Wolfgang Jeltsch
wolfgang at jeltsch.net
Thu Nov 24 14:28:54 EST 2005
Am Donnerstag, 24. November 2005 19:24 schrieb Fan Wu:
> Hi Wolfgang,
>
> I don't know the history so maybe this is a new implementation of
> State transformer. The Peek and poke functions are defined below
> (copied from StateT.hs):
>
> instance Monad m => StateM (StateT s m) s where
> peek = S (\s -> return (s,s))
> poke s = S (\s1 -> return (s1,s))
This is obviously a newer implementation. peek and poke are obviously what
get and put were in the old one.
> The question is why can't the mplus be written as simple as what
> Andrew suggested:
>
> mplus m1 m2 = S (\s -> let m1' = runState s m1
> m2' = runState s m2
> in ~(a, s') <- mplus m1' m2'
> return (a, s'))
>
> this is easier to understand. I don't see what's the purpose of the
> peek, lift, poke, return in the new implementattion.
[There is a "do" missing before the lazy pattern, isn't it?]
Maybe the point is that Andrew's implementation needs access to the internal
structure of a state transformer while the solution with peek, poke etc.
doesn't need this kind of access. So the non-Andrew solution can be
considered more elegant because it doesn't depend on the implementation of
state transformers, can be put into a different module and shows clearly that
mplus is not yet another primitive StateT operation but can be defined in
terms of primitives.
> Cheers,
> Fan
Best wishes,
Wolfgang
More information about the Haskell-Cafe
mailing list