[Haskell-cafe] How to debug GHC

David Roundy droundy at abridgegame.org
Fri Sep 2 07:36:18 EDT 2005

On Fri, Sep 02, 2005 at 05:10:35PM +1000, Ben Lippmeier wrote:
> >>... 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.

That's a good suggestion.  One can also use the C preprocessor to get
decent error messages:

#define fromJust (\m_fromJust_funny_name -> case m_fromJust_funny_name of {Nothing -> bug ("fromJust error at "++__FILE__++":"++show (__LINE__ :: Int)++" compiled "++__TIME__++" "++__DATE__); Just x -> x})

Do to the usage of the C preprocessor, this is likely to fail if you've got
variables names something like x', but apart from that it works nicely, and
allows you to do stuff like

foo = head . tail . sort . head

which could be ugly when written in terms of irrefutable patterns.
David Roundy

More information about the Haskell-Cafe mailing list