1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
------------------------------------------------------------------------------
--- This library contains some useful operation for debugging programs.
---
--- @author Bjoern Peemoeller
--- @version September 2014
--- @category general
------------------------------------------------------------------------------

module Debug.Trace
  ( trace, traceId, traceShow, traceShowId, traceIO
  , assert, assertIO
  ) where

import Control.Monad    (unless)
import System.IO        (hPutStrLn, stderr)
import System.IO.Unsafe (unsafePerformIO)

--- Prints the first argument as a side effect and behaves as identity on the
--- second argument.
trace :: String -> a -> a
trace s x = unsafePerformIO (traceIO s >> return x)

--- Prints the first argument as a side effect and returns it afterwards.
traceId :: String -> String
traceId a = trace a a

--- Prints the first argument using `show` and returns the second argument
--- afterwards.
traceShow :: Show a => a -> b -> b
traceShow a b = trace (show a) b

--- Prints the first argument using `show` and returns it afterwards.
traceShowId :: Show a => a -> a
traceShowId a = trace (show a) a

--- Output a trace message from the `IO` monad.
traceIO :: String -> IO ()
traceIO m = hPutStrLn stderr m

--- Assert a condition w.r.t. an error message.
--- If the condition is not met it fails with the given error message,
--- otherwise the third argument is returned.
assert :: Bool -> String -> a -> a
assert cond s x = if cond then x else error s

--- Assert a condition w.r.t. an error message from the `IO` monad.
--- If the condition is not met it fails with the given error message.
assertIO :: Bool -> String -> IO ()
assertIO cond s = unless cond $ error s