[Haskell-cafe] not possible with monad transformers ?
Pavel Zolnikov
pavelzolnikov at yahoo.com
Mon Nov 29 15:10:10 EST 2004
Hi, I have been playing with Maybe and Output monad transformers
and realized that I cannot make the following work nicely:
> foo a b = do
> output "before"
> r <- (liftM2(+)) a b
> when r == Nothing $ output "error"
> return r
As soon as computation produces Nothing, I am loosing ability to do any
output. I think what I need is not wrapping one monad into another with
transformer; I need some sort of monad combiner:
> newtype MC m1 m2 = MC (m1 a, m2 b)
So I could execute both of monads ‘in parallel’. But I have no idea
how to express this or even if this is doable at all. Any comments?
Thanks,
Pavel.
P.S. Here is summary of what I tried with transformers so far:
Let’s say I have following monad transformers:
> newtype MaybeMonadT m a = MMT (m a)
> newtype OuptutMonadT m o a = OMT (m a, o)
And I am trying to use following monads:
> type M1 a = MaybeMonadT OutputM a
> type M2 a = OuptutMonadT Maybe String a
Now, they both won’t quite work in the following example:
> foo a b = do
> output "before"
> r <- (liftM2(+)) a b
> when r == Nothing $ output "error"
> return r
In case of M1, as soon as I get Nothing in r, computation will stop
and return without any output gathered.
In case of M2, as soon as I get Nothing in r, computation will stop
and return only with output gathered so far. That is
> output "error"
will never be called.
More information about the Haskell-Cafe
mailing list