File: Util.hs

package info (click to toggle)
haskell-file-location 0.4.9.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 112 kB
  • sloc: haskell: 282; ansic: 13; makefile: 2; sh: 1
file content (51 lines) | stat: -rw-r--r-- 1,751 bytes parent folder | download | duplicates (5)
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
50
51
-- | Functions that help you with debugging.
-- Most would make sense in the Debug.Trace module.
module Debug.Util 
  (debug, debugM, debugMsg, debugMsgIf, ltrace, ltraceM, strace, traceId)
  where

import Debug.Trace (trace)

-- | A version of Debug.Trace.trace that just prints a value.
-- This should be included in Debug.Trace
debug :: Show a => a -> a
debug = ltrace "DEBUG"

-- | A version of Debug.Trace.trace that just prints a value and a message.
-- This should be included in Debug.Trace
debugMsg :: Show a => String -> a -> a
debugMsg msg = ltrace ("DEBUG: " ++ msg)

-- | A version of Debug.Trace.trace that just prints a value and a message.
-- This should be included in Debug.Trace
debugMsgIf :: Show a => String -> (a -> Bool) -> a -> a
debugMsgIf msg cond x = if cond x then ltrace ("DEBUG: " ++ msg) x else x

-- | Monadic debug - like debug, but works as a standalone line in a monad.
--
-- TODO: TH version with error loaction info
debugM :: (Monad m, Show a) => a -> m a
debugM a = debug a `seq` return a

-- | Trace (print on stderr at runtime) a showable expression
-- like 'debug', but do not print \"DEBUG: \".
--
-- \"strace\" stands for \"show trace\".
strace :: Show a => a -> a
strace a = trace (show a) a

-- Alias for 'strace'.
--
-- \"traceId\" means it returns itself after tracing like the 'id' function.
traceId :: Show a => a -> a
traceId = strace

-- | Labelled trace - like 'strace', but with a label prepended.
ltrace :: Show a => String -> a -> a
ltrace l a = trace (l ++ ": " ++ show a) a

-- | Monadic debug - like debug, but works as a standalone line in a monad.
--
-- TODO: TH version with error loaction info
ltraceM :: (Monad m, Show a) => String -> a -> m a
ltraceM str a = ltrace str a `seq` return a