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 l
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.
A quick-n-dirty workaround is the assert function from Control.Exception. However, it's a little clunkier than error
.
Warning: All asserts will be silently ignored if you compile with optimizations (ghc -O1
, -O2
, etc.).
Example:
import Control.Exception
main = do
print (42 + (assert True 17)) -- adds 42 and 17
print (42 + (assert False 21)) -- crashes
Output:
59
test.hs: /tmp/test.hs:5:18-23: Assertion failed
Note the line number "5" in the output.
You could use trace
from Debug.Trace to add an error message:
print (42 + (trace "omg error" $ assert False 21))
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.
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.