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

Gregory Crosswhite gcross at phys.washington.edu
Mon Aug 9 16:37:16 EDT 2010


 I've never used this myself, but the package mtlx seems to offer one
possible solution to this problem by tagging the monad transformers with
index types:

    http://hackage.haskell.org/package/mtlx

Cheers,
Greg

On 08/09/10 12:39, Gábor Lehel wrote:
> 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
>>
>>
>
>



More information about the Haskell-Cafe mailing list