views:

136

answers:

1

Is there a way to get line-number/traceback information in Haskell?

(like C's __LINE__ macro or Python's traceback.extract_stack())

That would be of use for me for writing Haskell program that generates C++ code, which would be notated with comments telling which Haskell line is responsible for which C++ line.

Haskell example:

LINE "#include <foo.h>" -- this is line 12
: INDENT "void Foo::bar() {" "}"
    [ LINE $ "blah(m_" ++ x ++ ", \"" ++ x ++ "\");"
    | x <- ["Potato", "Avocado"]
    ]

will generate this C++ code:

#include <foo.h>                  // gen.hs:12
void Foo::bar() {                 // gen.hs:13
  blah(m_Potato, "Potato");       // gen.hs:14
  blah(m_Avocado, "Avocado");     // gen.hs:14
}                                 // gen.hs:13
+11  A: 

You can actually use the CPP LINE pragma in Haskell.

{-# LANGUAGE CPP #-}

main = do
    print "one"
    print __LINE__


$ runhaskell A.hs
"one"
5

Also, the assert function, from GHC.Base will emit a line number when evaluated.

import GHC.Base

main = do
    print "one"
    assert False $
        print "two"


$ runhaskell A.hs
"one"
A.hs: A.hs:5:5-10: Assertion failed
Don Stewart
@dons: Awesome! I'm now using it and also I changed the code snippet above to reflect how this solution looks. It looks good!
yairchu