16

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.

Joe Eastwood
  • 163
  • 1
  • 5
  • see [here](http://stackoverflow.com/questions/8595077/how-can-i-get-the-position-where-error-was-called) for a better solution – Simon Bergot Dec 29 '14 at 17:34

1 Answers1

9

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.

hal
  • 305
  • 2
  • 6
  • Thank you Hal! Unfortunately, Loch no longer seems to be supported, at least it doesn't compile on modern versions of GHC. But I will try the other methods in your Wiki link. – Joe Eastwood Jul 05 '11 at 15:34
  • Okay, after looking at the Safe-Library, I replaced all instances of `Data.Map.(!)` with `findWithDefault (error "line nr")` and found the bug. Thanks again! – Joe Eastwood Jul 05 '11 at 15:49
  • 1
    Great! :) I just found http://hackage.haskell.org/package/loch-th, which seems to build on GHC7. – hal Jul 05 '11 at 15:54
  • Cool, I'll keep this in mind for those other bugs that will turn up. :) – Joe Eastwood Jul 05 '11 at 16:04
  • Maybe Don Stewart can say some words about LocH, whether it will be maintained again or if he is looking for someone to maintain it for him? Seems like a great extension for Haskell programms. – hal Jul 05 '11 at 18:24