[Haskell-cafe] More Flexible Monad Transformer OR Bad Coding Style

Gábor Lehel illissius at gmail.com
Mon Aug 9 15:39:49 EDT 2010


Actually, while I haven't even used monad transformers before (just
read about them a lot), I was thinking that something like this might
be the way to solve the lift . lift . lift . lift . foo problem on the
one hand, and by wrapping the 'contents' (e.g. the environment of a
reader monad) of every level of the stack in a unique newtype (if the
type isn't otherwise unique), the problem of "what if I want to use
the same transformer more than once, how do I disambiguate them". (Do
I have roughly the right idea?)

On Mon, Aug 9, 2010 at 9:05 PM, aditya siram <aditya.siram at gmail.com> wrote:
> 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
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>



-- 
Work is punishment for failing to procrastinate effectively.


More information about the Haskell-Cafe mailing list