[Haskell-cafe] Monad transformers

Andrew Coppin andrewcoppin at btinternet.com
Sat Jul 3 17:20:30 EDT 2010


Roman Cheplyaka wrote:
> * Andrew Coppin <andrewcoppin at btinternet.com> [2010-07-03 15:07:17+0100]
>   
>> In my experience, defining a type representing several stacked monad
>> transformers is the easy part.
>>     
>
> Of course it is. It wasn't my intention just to show you how easy it is
> to define a newtype in Haskell :)
>
> I just showed you a monad stack which is successfully used in xmonad --
> and you really need to read the code a bit to get the taste of it.
>   

OK, fair enough then.

>> The hard part is figuring out how in
>> the name of God to run the resulting computation
>>     
>
> It is run just in the one place, so you don't need to think about it each
> time you do some changes.
>   

As I say, every time I've tried to do this, I end up writing a function 
to "run this stuff", and it typically takes a few hours to reach the 
point where it type-checks.

>> or how to access functions burried at various levels of the stack.
>>     
>
> See above:
> -- Dynamic components may be retrieved with 'get', static components
> -- with 'ask'.
>
> So you use ask to get some configuration variable (reader monad is used
> for configuration in xmonad) and get/put/modify to deal with dynamic
> state of application. You use liftIO (abbreviated to 'io') to run IO
> computations.
>   

In other words, somebody has written a combinatorial explosion of class 
instances to automate some of the lifting.

>> From what I've seen, it usually ends up being faster and easier to
>> just define a custom monad that does exactly what you want, and then
>> use that.
>>     
>
> In which way is it faster and easier?

It's faster and easier to write the code because I don't have to spend 
multiple hours trying to work out how to make it type-check. Whether 
it's any faster at run-time, I have no idea...



More information about the Haskell-Cafe mailing list