I have a problem. I wrote a big Haskell program, and it always works with small input. Now, when I want to test it and generate a bigger input, I always get the message:
HsProg: Prelude.head: empty list
I use Prelude.head many times. What can I do to find out more or get a better error output to get the code line in which it happens?
The GHCi option
-fbreak-on-exceptioncan be useful. Here’s an example debugging session. First we load our file into GHCi.Now, we turn on
-fbreak-on-exceptionsand trace our expression (mainin this case for the whole program).We’ve stopped at an exception. Let’s try to look at the code with
:list.Because the exception happened in
Prelude.head, we can’t look at the source directly. But as GHCi informs us, we can go:backand try to list what happened before in the trace.In the terminal, the offending expression
filter odd [2, 4, 6]is highlighted in bold font. So this is the expression that evaluated to the empty list in this case.For more information on how to use the GHCi debugger, see the GHC User’s Guide.