[Haskell-cafe] source line annotations

Max Bolingbroke batterseapower at hotmail.com
Thu Jan 20 00:32:05 CET 2011


On 19 January 2011 22:43, Evan Laforge <qdunkan at gmail.com> wrote:
> Reasons why I
> don't actually want this after all?

Are you aware of the magic assert function?

http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Exception-Base.html#v:assert

The compiler spots it and replaces it with a function that raises an
exception that contains the source location if the assert fails.
It is deeply evil, but you can write a library function like this (untested):

getSourceLoc :: (Bool -> a -> a) -> String
getSourceLoc my_assert = unsafePerformIO (evaluate (my_assert False
(error "Impossible")) `catch` (\(AssertionFailed s) -> return s))

Now your consumers can write:

getSourceLoc assert :: String

And you will have a value that describes where you are. You can then
use this to implement logging, throw errors etc. The annoying part is
that you have to explicitly pass in assert from the call site to make
sure that you get the right source location reported.

Cheers,
Max



More information about the Haskell-Cafe mailing list