[Haskell-cafe] Monad transformers [Stacking monads]

Dougal Stanton dougal at dougalstanton.net
Tue Oct 7 08:58:54 EDT 2008

On Mon, Oct 6, 2008 at 9:48 PM, Andrew Coppin
<andrewcoppin at btinternet.com> wrote:
> Andrew Coppin wrote:
>> I have some longwinded code that works, but I'm still thinking about how
>> to do this more elegantly. It looks like what I really need is something
>> like
>>  type M = StateT State (ResultSetT (ErrorT ErrorType Identity))
>> Is that the correct ordering?
>> If so, I guess that means I have to somehow construct ResultSetT. Is there
>> an easy way to do that, given that I already have ResultSet? For example, if
>> I put ResultSet into Traversable, would that let me do it?
> ...and again I'm talking to myself... :-/
> So after much experimentation, I have managed to piece together the
> following facts:
> - It appears that the outer-most monad transformer represents the inner-most
> monad. So "StateT Foo ListT" means a list of stateful computations, while
> "ListT (StateT Foo)" means a stateful list of computations.

Have you read "Monad Transformers Step by Step" [1] by Martin
Grabmueller? It's a fantastic introduction to these beasties, leading
the reader through a series of transformations from pure code to using
about 4 different monads/transformers for all sorts of extra features.

Seriously recommend it.

[]: <http://uebb.cs.tu-berlin.de/~magr/pub/Transformers.pdf>



More information about the Haskell-Cafe mailing list