[Haskell-cafe] source locations (was: best practices for monad transformers and exceptions?)

Evan Laforge qdunkan at gmail.com
Thu Sep 24 16:03:21 EDT 2009


I forgot to mention this, but control-monad-exception reminded me:

Is there any interest in a way for ghc to report source locations?  I
have a preprocessor that basically works and is really useful but also
awkward and not scalable.  control-monad-exception has TH which is
more scalable but requires manual annotation at every call site.  The
loch package also requires annotation at call sites.

I searched on trac, but it's hard to know what to search for and I
only found a feature request to make 'undefined' report line numbers.
Some way to have the callee get the caller's location would be quite
handy.  Perhaps something like:

-- | (calling_function, calling_filename, line_number)
type SrcPos = Maybe (String, String, Int)

{-# SRCPOS_ANNOTATE foo, foo_srcpos #-}
foo :: Arg -> Result
foo = foo_srcpos Nothing

foo_srcpos :: SrcPos -> Arg -> Result
foo_srcpos srcpos arg = do
  State.modify $ \st -> st { state_stack = srcpos : state_stack st }
  -- or whatever


Then ghc will rewrite calls to 'foo' as calls to 'foo_srcpos' and add
a SrcPos arg.  If the extension isn't present, plain 'foo' will be
called which will pass Nothing.  This is basically what my
preprocessor does, only it doesn't understand modules and importing,
so it relies on me having a consistent import convention.

I suppose the preprocessor could be extended with the ability to
understand import lines... but ghc could put the annotation in .hi and
wouldn't have to scan all the source files.  My preprocessor wouldn't
work with libraries.

Getting a complete call stack would be even nicer, and I remember
seeing something a while ago about how it would require keeping track
of the calls separately from the real (possibly heavily inlined or
tail recursive) call stack but would be technically possible
especially with some more detailed source code information which I
suppose may be in by now...


More information about the Haskell-Cafe mailing list