Application letters at the Haskell workshop: suggestion
Sat, 15 Sep 2001 15:44:52 -0500
> On Fri, 14 Sep 2001, Mark Carroll wrote: (snip)
> > simplistic (but adequate for my immediate needs, which are currently
> > being served with lots of ifs and Maybes!).
> Oh - and I should add, lots of two-tuple return values which are
> basically of the form (Maybe a, error details). ):
> -- Mark
Sounds like you need....
the Error Monad!
I've been writing a little compiler in Haskell and found that a variety of
error monads have been very helpful for structuring code that needs to be
able to return errors.
I've been using a few variants: single error, multiple error and multiple
error/warning types. I'm also particularly pleased with one that has an
extra combinator which allows seperate 'branches' of an expression to
combine all the errors (if there are any). That allows multiple
*independant* errors to be returned to the user rather than just the first one.
I'm sure it been done before, buit it looks like this:
BinOp op <$&> CheckExpr e1 <&> CheckExpr e2
and have overall type
:: MaybeErrors Expr
(where, BinOp op e e2 :: Expr)
This would check each sub expression and return the overall expression
(wrapped in the monad), unless either branch 'raises' an error. Also, if
both branches raise an error, both errors are returned.
It's suprising how little an algorithm needs to be changed when using
monads like this. If found that I can start with a non-monadic version
that just uses 'error' and then convert it to a monadic version quite