[Haskell-cafe] How to debug GHC
Ben Lippmeier
Ben.Lippmeier at anu.edu.au
Fri Sep 2 03:10:35 EDT 2005
>> ... It's very hard to debug a large program when you
>> can randomly get messages like "*** Exception: Prelude.head: empty
>> list" and have no idea where they came from.
>
As a purely pragmatic suggestion: don't use head, fromJust, last, or any
other function that is likely to fail in impossible-to-find way, at
least not directly.
In GHC, you can wrap or replace them with irrefutable patterns which are
almost as easy to write, and will give you a sensible error message if
they fail.
Example:
replace x = head xx
with (x:_) = xx
replace x = fromJust mX
with (Just x) = mX
replace x = last xx
with
y@(_:_) = xx
x = last y
Ben.
More information about the Haskell-Cafe
mailing list