[Haskell-beginners] order of monad transformers

Brent Yorgey byorgey at seas.upenn.edu
Wed Nov 4 07:42:02 EST 2009


On Tue, Nov 03, 2009 at 06:29:46PM -0800, Michael Mossey wrote:
> How does one consider the best ordering of monad transformers? For example, 
> if I'm combining ErrorT, StateT (or State), and WriterT (or Writer)? But 
> not just this specific example---what principles can one consult to 
> determine ordering?

As Stephen has illustrated, the principle is that the effects of
_inner_ transformers can "override" the effects of _outer_
transformers.  (This has often seemed unintuitive and "backwards" to
me, but that's the way it is.)  For example, consider StateT s (FooT
...): if FooT has a failure mode, when a computation fails you don't
even get a state anymore.  Or if FooT has some sort of backtracking
effect, the state will get rewound along with the rest of the
computation.  On the other hand, FooT (StateT s ....) will still
compute a state even when the FooT fails, and the state will be
preserved even when the computation backtracks.

-Brent


More information about the Beginners mailing list