[Haskell-cafe] Prelude.head: empty list

lennart spitzner lsp at informatik.uni-kiel.de
Sat Mar 12 00:16:43 UTC 2016


As it has not been mentioned yet, i may also point out
http://hackage.haskell.org/package/safe-0.3.9/docs/Safe.html#v:headNote

never use head, sparingly use headNote with unique notes.

On 11/03/16 19:17, Bryan Richter wrote:
> 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.
> 
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> 



More information about the Haskell-Cafe mailing list