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

Jules Bean jules at jellybean.co.uk
Wed Dec 1 06:42:17 EST 2004


On 1 Dec 2004, at 00:37, Marcin 'Qrczak' Kowalczyk wrote:

> Ben Rudiak-Gould <Benjamin.Rudiak-Gould at cl.cam.ac.uk> writes:
>
>> 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]
>
> When writing a compiler, it makes sense to collect errors as by the
> writer monad, and not abort anything - producing dummy values instead
> (except perhaps some fatal errors when it's inconvenient).
>

Or you could use the monad:

data Perhaps a = Success a | Failure a [Error]

instance Monad Perhaps where
     Success a >>= k     = k a
     Failure a es >>= k  = Failure (value (k a)) (errors (k a) ++ es)
         where value Success b = b
               value Failure b _ = b
               errors Success _ = []
               errors Failure _ fs = fs

...such that failures must also produce a value (which could be some 
approximating 'best guess' based on incorrect input, or some simple 
default value).

This is just a special case of the writer monad, I think.

Jules



More information about the Haskell-Cafe mailing list