[Haskell-cafe] Re: how do you debug programs?

Jason Dagit dagit at eecs.oregonstate.edu
Wed Sep 6 12:56:17 EDT 2006

On 9/6/06, Neil Mitchell <ndmitchell at gmail.com> wrote:
> Let take for example a bug I spent tracking down in Haskell this
> weekend. The bug can be summarized as "Program error: pattern match
> failure: head []". And indeed, thats all you get. A quick grep reveals
> there are about 60 calls to head in the program. In this instance I
> have the choice between 1) crying, 2) a debugger, 3) a lot of hard
> work. [Of course, knowing this situation arises, I had already
> prepared my getout plan, so it wasn't a massive problem]

I don't know what your getout plan was but when I'm in this situation
I do the following (hopefully listing this trick will help the OP):

head' :: String -> [a] -> a
head' s [] = error $ "head' empty list at " ++ s
head' _ xs = head xs

Then I do the tedious thing of replacing all the heads in my program
with head' and giving each a unique string to print out, such as head'
"line 5".

You could even take this a step further and let the type system help
you find the places to replace head by doing something like:

import Prelude hiding (head)
import qualified Prelude (head)

head :: String -> [a] -> a
head s [] = error $ "head empty list at " ++ s
head _ xs = Prelude.head

Now you should get type errors anywhere you're using head.

Or maybe even more extreme you could use template haskell or the c
preprocessor to fill in the line number + column.


More information about the Haskell-Cafe mailing list