[Haskell-cafe] exceptions vs. Either

Ketil Malde ketil+haskell at ii.uib.no
Wed Aug 4 08:35:13 EDT 2004


David Roundy <droundy at abridgegame.org> writes:

> Here "bug" is a function that just calls "error" with a little prefix
> explaining that there is a bug in darcs, and would the user please report
> it.  Obviously, defining a head here would be just as easy,

Cool!  The basic trick is just to inline the actual function
defintions using CPP macros.  I've made macros for most of the
troublesome functions, but I can't get it to work for operators
(something like `(!!)` doesn't parse, it seems)  Any tricks?

Unless I'm overlooking something, I could have a file prelude.h
containing something like:

----8<------
import Prelude hiding (head,(!!),read)

#define head (\xs -> case xs of { (x:_) -> x ; _ -> bug "head" __FILE__ __LINE__})
#define at (let {at (y:_) 0  = y; at (y:ys) n = at ys (n-1); at _ _ = bug "at" __FILE__ __LINE__} in \a x -> at a x)
#define read (\s -> case [ x | (x,t) <- reads s, ("","") <- lex t] of { [x] -> x ; _ -> bug "read" __FILE__ __LINE__})
#define fromJust (\x -> case x of Just a -> a; Nothing -> bug "fromJust" __FILE__ __LINE__)

bug c f l = error ("Program error - illegal parameters to '"++c++"', file '"++f++"', line "++show l)
----8<------

and just #include "prelude.h" if/when I want better debugging.  No
expensive stack frames, no unwanted strictness, and no clutter.

Any comments?

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants


More information about the Haskell-Cafe mailing list