[Haskell-beginners] Difference between Monad composition and transformation

Brent Yorgey byorgey at seas.upenn.edu
Sat Aug 25 14:34:10 CEST 2012


On Sat, Aug 25, 2012 at 03:14:02AM -0700, Dennis Raddle wrote:
> I'm not exactly a Haskell beginner but haven't grasped much of it yet. I
> have a feeling the answer to your question has something to do with the
> fact that the monad transformer library provides instances that allow the
> use of, for example, "get" and "put" in any transformed monad that has a
> StateT somewhere in it. Or throwError in anything that has an ErrorT in it.
> In other words, you don't need to lift everything into the right level.

Well, not really, that's a separate thing to do with type classes like
MonadState and MonadError.

To answer the OP's question, literally composing monadic types does
often give you a type similar to the transformer version.  However,
the point of composing monads is to give you a new, combined monad.
If you just use the type (State s (Writer w a)) this is just a State
computation which happens to return something of type Writer w a.
Combining such things with >>= does not take the Writer into account
at all.  On the other hand, StateT s (Writer w) a is a separate type
with a Monad instance that combines the effects of State and Writer.

-Brent

> 
> On Sat, Aug 25, 2012 at 1:15 AM, Song Zhang <vxanica at gmail.com> wrote:
> 
> >
> > When I use a State Monad transformer to combine with a Writer Monad
> > StateT s (Writer w) a. is it different from composition of State Monad and
> > Writer Monad. It is State s (Writer w a) ?
> > StateT is defined as (s -> m (a, s)), so StateT s (Writer w) a can be
> > regarded as (s -> Writer w a) , which is (s -> ((a,w),s)
> > and on the other hand State s (Writer w a) is (s -> ((a,w),s). I suppose
> > the are similar and if so, what is the point we still get Monad
> > transformers? Thanks
> >
> >
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> >

> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners




More information about the Beginners mailing list