[Haskell-cafe] Prelude.head: empty list

Bryan Richter b at chreekat.net
Fri Mar 11 18:17:01 UTC 2016

On Fri, Mar 11, 2016 at 05:13:15PM +0100, Jonas Scholl wrote:
> 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.

I'd like to add a link to
https://hackage.haskell.org/package/located-base, which adds
callstacks to a lot of partial functions.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Digital signature
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160311/fd08e442/attachment.sig>

More information about the Haskell-Cafe mailing list