Prelude.head: empty list

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!

No. See a discussion and some explanations inside. https://www.reddit.com/r/haskell/comments/441e3s/how_to_use_the_implicit_err... Unfortunately, you can only look at the diff with the version before the error occured and perform impact analysis. Michał

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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

+RTS -xc has worked for me,
https://wiki.haskell.org/Debugging
On Fri, Mar 11, 2016 at 8:13 AM, Jonas Scholl
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

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.

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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

On Fri, Mar 11, 2016 at 7:56 AM, Han Joosten
Of course I know that head is partial, and should be used with care.
How about never? I do fine with either case and a Maybe-returning variant. So if I see that error it's definitely library code... which unfortunately has happened a few times. I wish library authors went with "never" as well!

Could you perhaps change every `head` to a pattern match with `[] -> error "impossible, because..."` to see what impossible happened? It could help when some other impossible happens later. Other than that, located-base that Bryan Richter mentioned looks useful too. Best regards, Marcin Mrotek

On Fri, Mar 11, 2016 at 04:56:57PM +0100, 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!
I scanned previous replies and didn't see it mentioned, maybe you can abuse C preprocessor to shadow `head` and insert a __LINE__ on pattern matching []?

You mean like this? default-extensions: CPP cpp-options: -Dhead=(foldr(const)(error("head:\x20\x65mpty\x20list\x20in\x20"++__FILE__++":"++show(__LINE__))).(take$1)) Yes, it is a little bit ugly as I am unable to convince cabal to skip the spaces, so I just avoided them. And build an ugly inline-definition of head. Maybe having a head' function taking a line number and the file would help :) But it should work, if it is not too much broken my my mail-client. The define should be on one long line. On 03/11/2016 07:26 PM, Francesco Ariis wrote:
On Fri, Mar 11, 2016 at 04:56:57PM +0100, 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!
I scanned previous replies and didn't see it mentioned, maybe you can abuse C preprocessor to shadow `head` and insert a __LINE__ on pattern matching []? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

On Sat, Mar 12, 2016 at 8:07 AM, Jonas Scholl
You mean like this?
default-extensions: CPP cpp-options: -Dhead=(foldr(const)(error("head:\x20\x65mpty\x20list\x20in\x20"++__FILE__++":"++show(__LINE__))).(take$1))
That's pretty cool! By the way: can the (take$1) be removed? foldr should be lazy enough already. -- Chris Wong (https://lambda.xyz) "I had not the vaguest idea what this meant and when I could not remember the words, my tutor threw the book at my head, which did not stimulate my intellect in any way." -- Bertrand Russell

Thanks all! Plenty good options, fortunately I found my bug pretty fast by
simple code inspection :))
2016-03-12 4:28 GMT+01:00 Chris Wong
On Sat, Mar 12, 2016 at 8:07 AM, Jonas Scholl
wrote: You mean like this?
default-extensions: CPP cpp-options:
-Dhead=(foldr(const)(error("head:\x20\x65mpty\x20list\x20in\x20"++__FILE__++":"++show(__LINE__))).(take$1))
That's pretty cool!
By the way: can the (take$1) be removed? foldr should be lazy enough already.
-- Chris Wong (https://lambda.xyz)
"I had not the vaguest idea what this meant and when I could not remember the words, my tutor threw the book at my head, which did not stimulate my intellect in any way." -- Bertrand Russell _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Just thinking out loud here, but could one write their own Prelude which
just imports hiding (head), adds a new definition of head, and reexports
the lot. Then add this new Prelude higher in the search path perhaps?
On Monday, 14 March 2016, Han Joosten
Thanks all! Plenty good options, fortunately I found my bug pretty fast by simple code inspection :))
2016-03-12 4:28 GMT+01:00 Chris Wong
javascript:_e(%7B%7D,'cvml','lambda.fairy@gmail.com');>: On Sat, Mar 12, 2016 at 8:07 AM, Jonas Scholl
javascript:_e(%7B%7D,'cvml','anselm.scholl@tu-harburg.de');> wrote: You mean like this?
default-extensions: CPP cpp-options:
-Dhead=(foldr(const)(error("head:\x20\x65mpty\x20list\x20in\x20"++__FILE__++":"++show(__LINE__))).(take$1))
That's pretty cool!
By the way: can the (take$1) be removed? foldr should be lazy enough already.
-- Chris Wong (https://lambda.xyz)
"I had not the vaguest idea what this meant and when I could not remember the words, my tutor threw the book at my head, which did not stimulate my intellect in any way." -- Bertrand Russell _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org javascript:_e(%7B%7D,'cvml','Haskell-Cafe@haskell.org'); http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Yes you can define your own Prelude. You can’t shadow modules but you can depend on a package providing a custom prelude while not explicitly depending on base.
On Mon 14 Mar 2016 at 14:01 Clinton Mead
<
mailto:Clinton Mead
wrote:
Just thinking out loud here, but could one write their own Prelude which just imports hiding (head), adds a new definition of head, and reexports the lot. Then add this new Prelude higher in the search path perhaps? On Monday, 14 March 2016, Han Joosten < mailto:han.joosten.han@gmail.com
wrote:
Thanks all! Plenty good options, fortunately I found my bug pretty fast by simple code inspection :)) _______________________________________________ Haskell-Cafe mailing list mailto:Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe 2016-03-12 4:28 GMT+01:00 Chris Wong < javascript:_e(%7B%7D,'cvml','lambda.fairy@gmail.com');
: On Sat, Mar 12, 2016 at 8:07 AM, Jonas Scholl < javascript:_e(%7B%7D,'cvml','anselm.scholl@tu-harburg.de');
wrote:
You mean like this?
default-extensions: CPP
cpp-options:
-Dhead=(foldr(const)(error("head:\x20\x65mpty\x20list\x20in\x20"++__FILE__++":"++show(__LINE__))).(take$1))
That's pretty cool! By the way: can the (take$1) be removed? foldr should be lazy enough already. -- Chris Wong ( https://lambda.xyz ) "I had not the vaguest idea what this meant and when I could not remember the words, my tutor threw the book at my head, which did not stimulate my intellect in any way." -- Bertrand Russell _______________________________________________ Haskell-Cafe mailing list javascript:_e(%7B%7D,'cvml','Haskell-Cafe@haskell.org'); http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Always using `headNote` from `safe` package works good enough usually.
On Fri, Mar 11, 2016 at 5:56 PM, Han Joosten
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (14)
-
Adam Bergmark
-
Anatoly Yakovenko
-
Bryan Richter
-
Chris Wong
-
Clinton Mead
-
Evan Laforge
-
Francesco Ariis
-
Han Joosten
-
Jonas Scholl
-
Jonne Ransijn
-
Kostiantyn Rybnikov
-
lennart spitzner
-
Marcin Mrotek
-
Michał Antkiewicz