Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a backtrace for an Error thrown in Haskell

Tags:

haskell

I'm currently debugging an algorithm I implemented in Haskell for my Diploma thesis. It seems to work correctly for most inputs, yet I found one input which makes GHC throw the error

*** Exception: Map.find: element not in the map

Since I have many Map lookups in my code, I need to find the line throwing this error to make any sense of it. I read through this guide, but although I set the flag fbreak-on-exception (and -error), all GHCi gives me after tracing the function I'm testing is:

[...]> :trace test
[...]
Stopped at <exception thrown>
_exception ::
  e = GHC.Exception.SomeException (GHC.Exception.D:Exception _
                                                         (GHC.Show.D:Show ...) ....)
                              (GHC.Exception.ErrorCall ['M',....])
Unable to list source for <exception thrown>
Try rerunning with :trace, :back then :list
[<exception thrown>] [...]> :history
Empty history. Perhaps you forgot to use :trace?

Trying :trace again doesn't seem to help either.

So, can someone tell me what is going wrong or offer another way of finding the offending line? Thanks in advance!

PS: I'm using GHC version 7.0.3, so the linked guide should apply.

like image 536
Joe Eastwood Avatar asked Jul 05 '11 14:07

Joe Eastwood


1 Answers

Maybe this will help you

http://www.haskell.org/haskellwiki/Debugging

LocH provides wrappers over assert for generating source-located exceptions and errors.
...
adding: import Debug.Trace.Location and then recompiling with the preprocessor on:

  $ ghc A.hs --make -pgmF loch -F -no-recomp
  [1 of 1] Compiling Main             ( A.hs, A.o )
  Linking A ...
  $ ./A
  A: A.hs:14:14-19: Maybe.fromJust: Nothing

There are also other tips on the wiki, like e.g. using the Safe-Library.

like image 148
hal Avatar answered Oct 10 '22 13:10

hal