[Haskell-cafe] More Flexible Monad Transformer OR Bad Coding Style
aditya siram
aditya.siram at gmail.com
Mon Aug 9 15:05:14 EDT 2010
Hi all,
I was experimenting with monad transformers and realized that the stacking
order of the monads can remain unknown until it is used. Take for example
the following code:
import "mtl" Control.Monad.State
import "mtl" Control.Monad.Writer
import "mtl" Control.Monad.Identity
test :: (MonadWriter [Char] m, Num t, MonadState t m) => m ()
test = do
put 1
tell "hello"
main = do
x <- return $ runIdentity $ runStateT (runWriterT test) 1 -- test ::
WriterT String (StateT Int Identity)
y <- return $ runIdentity $ runWriterT $ runStateT test 1 -- test ::
StateT Int (WriterT String Identity)
z <- runWriterT $ runStateT test 1 -- test ::
StateT Int (WriterT String IO) (((), Int), String)
print x
print y
print z
*Main> main
(((),"hello"),1)
(((),1),"hello")
(((),1),"hello")
Until test is called in 'main' we don't know the order of monads. In fact
even the base monad is not know. All we know is that it uses the State and
Writer monad. In each call to 'test' in main we can determine the stacking
order and the base monad yielding different results. This seems to be a more
flexible way of using monad transformers but I haven't seen this in code
before so is there anything wrong with this style?
-deech
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100809/9954e7ee/attachment.html
More information about the Haskell-Cafe
mailing list