Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the position where error was called?

Tags:

haskell

I am looking for something to replace loch (and its preprocessor) since it doesn't compile with ghc 7.

Specifically, if error is called then I would like to figure out, as conveniently as possible, where it was called from (line number and stack trace would be nice).

like image 364
HaskellElephant Avatar asked Dec 21 '11 19:12

HaskellElephant


3 Answers

You can use the -xc RTS option, as described on this page; you need to compile your program with profiling support, and the output is pretty ugly, but it works.

This should do it:

$ ghc --make -prof -auto-all myprog.hs
$ ./myprog +RTS -xc

Technically this only gives a cost centre stack, not a true stack trace. Improved stack trace support is coming in GHC 7.4.

like image 112
ehird Avatar answered Oct 23 '22 04:10

ehird


If this is for use in code you're working on, and you can tolerate using Template Haskell, the placeholders package is a cute and simple way to do something like this. It won't help you find the location of actual error expressions, though, only uses of its own error-like functions.

like image 20
C. A. McCann Avatar answered Oct 23 '22 04:10

C. A. McCann


It's pretty trivial to get it to build with GHC-7. It's just the Control.Exception change that came with 6.12, the simple fix is to change the Exception type to SomeException in Debug.Trace.Location, line 70 and add an expression type signature in line 144. Restrict the base dependency to >= 4.2 && < 4.6 in the .cabal file (bump the version) and you're good to go.

like image 28
Daniel Fischer Avatar answered Oct 23 '22 04:10

Daniel Fischer