[Haskell-cafe] How to debug GHC

Claus Reinke claus.reinke at talk21.com
Tue Apr 26 06:40:50 EDT 2005

>   I'm developing a back end for GHC and I have the following problem:
> my program is throwing an "empty list exception" due to head [] and I
> need to compile GHC with -prof -auto-all in order to see the stack
> trace when running it with +RTS -xc -RTS.  I changed the makefile but
> the option +RTS -xc -RTS was not recognized as an available RTS option
> Does anyone have any idea about how I can do that ?


no direct answer to your question, but a general comment on the original
problem (speaking from bad experience;-): things like "head" have no place 
in a Haskell program of any non-trivial size, because of their useless error 

Whenever you read some code that includes an unguarded call to 
head (where the non-emptyness of the list is not immediately obvious), 
you are looking at a nasty trouble spot - get rid of it now!

At the very least, use something like Control.Exception.assert to guard
uses of partial functions like head, or define your own "safeHead":

safeHead msg [] = error msg
safeHead msg (h:_) = h

and replace unguarded uses of head with calls to safeHead "<what am
I trying to do here, or what function am I in>" (such replacements can 
be automated, at least partially, so you can follow this approach to
debug your problem and avoid similar problems in future).

However, that is only when you've already messed up your code and
"only want to get rid of this kind of problem quickly". Otherwise, I
often prefer explicit use of pattern matching/case for self-documentation:

case l of { (h:_) -> h; _ -> error "<this> should never happen!"}

if you're in a branch of a case, you can not only be sure that l is 
non-empty, you have already named its head.

Better to improve the code than to use bigger debuggers?-)


More information about the Haskell-Cafe mailing list