Guest User

Untitled

a guest
Dec 15th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. trace2 :: Show a => [Char] -> a -> a
  2. trace2 name x = trace (name ++ ": " ++ show x) x
  3.  
  4. {-# LANGUAGE TemplateHaskell #-}
  5. import Language.Haskell.TH
  6. import Language.Haskell.TH.Syntax as TH
  7. import Debug.Trace
  8.  
  9. withLocation :: Q Exp -> Q Exp
  10. withLocation f = do
  11. let error = locationString =<< location
  12. appE f error
  13. where
  14. locationString :: Loc -> Q Exp
  15. locationString loc = do
  16. litE $ stringL $ formatLoc loc
  17.  
  18. formatLoc :: Loc -> String
  19. formatLoc loc = let file = loc_filename loc
  20. (line, col) = loc_start loc
  21. in concat [file, ":", show line, ":", show col]
  22.  
  23. trace3' (loc :: String) msg x =
  24. trace2 ('[' : loc ++ "] " ++ msg) x
  25. trace3 = withLocation [| trace3' |]
  26.  
  27. {-# LANGUAGE TemplateHaskell #-}
  28. tr3 x = $trace3 "hello" x
  29.  
  30. > tr3 4
  31. [MyFile.hs:2:9] hello: 4
  32.  
  33. $ ghci HsColour.hs
  34. *Main> :set -fbreak-on-exception
  35. *Main> :set args "source.hs"
  36.  
  37. *Main> :trace main
  38. Stopped at (exception thrown)
  39.  
  40. [(exception thrown)] *Main> :back
  41. Logged breakpoint at Language/Haskell/HsColour/Classify.hs:(19,0)-(31,46)
  42. _result :: [String]
  43.  
  44. [-1: Language/Haskell/HsColour/Classify.hs:(19,0)-(31,46)] *Main> :list
  45. 18 chunk :: String -> [String]
  46. vv
  47. 19 chunk [] = head []
  48. 20 chunk ('r':s) = chunk s -- get rid of DOS newline stuff
  49. 21 chunk ('n':s) = "n": chunk s
  50. ^^
Add Comment
Please, Sign In to add comment