-- | 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 :: a -> a
debug = String -> a -> a
forall a. Show a => String -> a -> a
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 :: String -> a -> a
debugMsg msg :: String
msg = String -> a -> a
forall a. Show a => String -> a -> a
ltrace ("DEBUG: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
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 :: String -> (a -> Bool) -> a -> a
debugMsgIf msg :: String
msg cond :: a -> Bool
cond x :: a
x = if a -> Bool
cond a
x then String -> a -> a
forall a. Show a => String -> a -> a
ltrace ("DEBUG: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
msg) a
x else a
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 -> m a
debugM a :: a
a = a -> a
forall a. Show a => a -> a
debug a
a a -> m a -> m a
forall a b. a -> b -> b
`seq` a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
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 -> a
strace a :: a
a = String -> a -> a
forall a. String -> a -> a
trace (a -> String
forall a. Show a => a -> String
show a
a) a
a

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

-- | Labelled trace - like 'strace', but with a label prepended.
ltrace :: Show a => String -> a -> a
ltrace :: String -> a -> a
ltrace l :: String
l a :: a
a = String -> a -> a
forall a. String -> a -> a
trace (String
l String -> String -> String
forall a. [a] -> [a] -> [a]
++ ": " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
a) 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 :: String -> a -> m a
ltraceM str :: String
str a :: a
a = String -> a -> a
forall a. Show a => String -> a -> a
ltrace String
str a
a a -> m a -> m a
forall a b. a -> b -> b
`seq` a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a