[Haskell-cafe] Closure trace?

Neil Mitchell ndmitchell at gmail.com
Wed Jun 14 05:30:22 EDT 2006

Hi Michael,

I have defined fromJustNote and headNote, which take an extra
parameter, for example:

fromJustNote msg (Just x) = x
fromJustNote msg Nothing = error $ "fromJustNote failed, " ++ msg

I also have lookupJust which does a lookup and a fromJust, since this
is a common pattern in my program, and if the lookup fails I can show
the key, which usually gives me a clue as to what went wrong - rather
than fromJust Nothing which has no useful information.

I found that this is a really useful thing to do as programs get
bigger - when I get a fromJust error I just run around replacing
fromJust's to fromJustNote's and wait til I catch the error.

The other thing to do is use Hat (http://www.haskell.org/hat) to
generate a hat-trace then use hat-stack, which does exactly what you
ask for and more.



On 6/14/06, Michael Marte <marte at pms.informatik.uni-muenchen.de> wrote:
> Hello *,
> frequently we are seeing messages like "List.head: empty list" or
> "Maybe.fromJust: Nothing". It is clear what happened and where the
> messages were triggered yet the real cause is usually VERY hard to find
> unless the program is small and simple. I came to the conclusion that
> functions like head and fromJust are best to be avoided because their
> use may render large programs unmaintainable. Instead I use irrefutable
> pattern matching like (x : _) = l and Just bla = maybeBla whenever
> possible because when a pattern match fails the ghc runtime system gives
> me a nice error message naming the module, line and column. However,
> this procedure may become tedious when the error occurs in a call to a
> third-party library function.
> I wonder whether it is possible to print a "closure trace" similar to a
> stack trace in procedural programming. Say we have two modules A and B:
>         module A where
>         import B
>         a = b
>         module B
>         b = c where c = error "go to hell"
> I would like to see something like this:
>         sh$ ghci A
>         <ghci startup messages>
>         > a
>         Runtime error "go to hell" in module B at line 2
>         Trace:
>         B.b.c
>         B.b
>         A.a
> This way it would be easy to find the reason that actually caused, say,
> head to fail.
> I guess it would be sufficient to complement closures with information
> on the precise place of their definition.
> Michael
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list