-- -- Pure Haskell line/column numbers in a trace message. -- -- Copyright 2006 Don Stewart -- -- Free for any use. -- module Location (trace, assert) where import qualified Control.Exception as C (catch) import System.IO.Unsafe (unsafePerformIO) import GHC.Base (assert) import System.IO type Assert = Bool -> IO () -> IO () -- An identity function that also prints the current line and column number trace :: String -> Assert -> a -> a trace str assrt f = (unsafePerformIO $ C.catch (assrt False $ return ()) printIt) `seq` f where printIt e = let (x,_) = break (== ' ') $ show e in hPutStrLn stderr (x ++ " " ++ str) -- -- Examples: -- -- *Location> trace "trace me now!" assert $ map (+1) [1..10] -- :1:22-27: trace me now! -- [2,3,4,5,6,7,8,9,10,11] --