[Haskell-cafe] Is there an escape from MonadState+MonadIO+MonadError monad stack?

Tillmann Rendel rendel at informatik.uni-marburg.de
Sun Apr 7 00:34:00 CEST 2013


Ömer Sinan Ağacan wrote:
> One thing I'm not happy about my Haskell programs is, almost all of my
> programs have a monad transformer stack consisting MonadError, MonadIO
> and MonadState.

You can try to write most of your program in pure functions that are 
called from a few "main" functions in the monad. Or, if you need some 
but not all monadic actions in each function, you can use the following 

   -- This helper function cannot cause monadic effects other than
   -- throwing errors. But it can be used in arbitrary monads that
   -- support throwing errors.
   helper :: MonadError MyError m => ... -> m ...
   helper = do ...

   -- Same but with only allowing IO, but other monadic actions
   other :: MonadIO m => ... -> m ...
   other = do ...

   -- we can use both functions in the same monad
   main = runMyStack $ do

This way, you have some control over what effects are allowed where.


More information about the Haskell-Cafe mailing list