[Haskell-cafe] Re: not possible with monad transformers ?

Ben Rudiak-Gould Benjamin.Rudiak-Gould at cl.cam.ac.uk
Tue Nov 30 18:29:10 EST 2004

Jules Bean wrote:

> However, your problem *does* have a natural underlying monad, if you 
> care to use it.

I may be confused, but I don't think it does. It seems like the OP wants 
a type like

    data Perhaps a = Success a | Failure [Error]

and semantics like

    liftM2 (+) (Failure [error1]) (Failure [error2]) === Failure 


    liftM2 f a1 a2 = a1 >>= p
      where p v1 = a2 >>= q
        where q v2 = return (f v1 v2)

I don't see how to define (>>=) such that this will return the 
appropriate value. If a1 fails you must call p in order to collect 
additional errors, but there's no appropriate value of v1 that you can pass.

But it's easy with a custom lifting function:

    liftPerhaps2 f (Success x) (Success y) = Success (f x y)
    liftPerhaps2 f p q = Failure (errors p ++ errors q)
      where errors (Success _) = []
            errors (Failure es) = es

-- Ben

More information about the Haskell-Cafe mailing list