[Haskell-cafe] Prelude.head: empty list

Jonas Scholl anselm.scholl at tu-harburg.de
Fri Mar 11 16:13:15 UTC 2016

Which version of GHC are you using? If you are using 7.10.3, you can use
implicit parameters to get the caller of head (this should be enough to
narrow down the problem to one module and then add more debugging
information there). Or you could compile a profiling version of your
executable and use +RTS -xc (I think), which should print a stack trace
on any thrown exception (also for exceptions which are later caught).
Then the last trace should be for head.

To use implicit parameters, you need:

{-# LANGUAGE ImplicitParams    #-}
import GHC.Stack (CallStack, showCallStack)

head'                    :: (?callStack :: CallStack) => [a] -> a
head' (x:_)              =  x
head' []                 =  error $ "head': empty list" ++ "\nCallStack:
" ++ showCallStack ?callStack

Then replace every use of head by head'. If you need more than one stack
frame, just add (?callStack :: CallStack) to the constraints of the
calling function and GHC should add another stack frame.

On 03/11/2016 04:56 PM, Han Joosten wrote:
> In a rather large program I made some changes, and now I get the runtime
> error:
> ampersand.exe: Prelude.head: empty list
> Of course I know that head is partial, and should be used with care. It
> is used many times (we have 100+ modules in the program). Is there an
> elegant way to get some information about where the specific call to
> head is being made?
> Thanks!
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160311/7c0c348e/attachment.sig>

More information about the Haskell-Cafe mailing list