[commit: base] master: Add Debug.Trace.{traceId,traceShowId,traceM,traceShowM}; fixes #7626 (ef47691)
Ian Lynagh
igloo at earth.li
Mon Apr 22 00:23:50 CEST 2013
Repository : ssh://darcs.haskell.org//srv/darcs/packages/base
On branch : master
https://github.com/ghc/packages-base/commit/ef47691fe215422061752411bca922e4f846e7a3
>---------------------------------------------------------------
commit ef47691fe215422061752411bca922e4f846e7a3
Author: Ian Lynagh <ian at well-typed.com>
Date: Sun Apr 21 22:35:50 2013 +0100
Add Debug.Trace.{traceId,traceShowId,traceM,traceShowM}; fixes #7626
>---------------------------------------------------------------
Debug/Trace.hs | 42 ++++++++++++++++++++++++++++++++++++++++++
1 files changed, 42 insertions(+), 0 deletions(-)
diff --git a/Debug/Trace.hs b/Debug/Trace.hs
index 94568d2..d4df4d9 100644
--- a/Debug/Trace.hs
+++ b/Debug/Trace.hs
@@ -22,9 +22,13 @@ module Debug.Trace (
-- * Tracing
-- $tracing
trace,
+ traceId,
traceShow,
+ traceShowId,
traceStack,
traceIO,
+ traceM,
+ traceShowM,
putTraceMsg,
-- * Eventlog tracing
@@ -109,6 +113,12 @@ trace string expr = unsafePerformIO $ do
return expr
{-|
+Like 'trace' but returns the message instead of a third value.
+-}
+traceId :: String -> String
+traceId a = trace a a
+
+{-|
Like 'trace', but uses 'show' on the argument to convert it to a 'String'.
This makes it convenient for printing the values of interesting variables or
@@ -124,6 +134,38 @@ variables @x@ and @z@:
traceShow :: (Show a) => a -> b -> b
traceShow = trace . show
+{-|
+Like 'traceShow' but returns the shown value instead of a third value.
+-}
+traceShowId :: (Show a) => a -> a
+traceShowId a = trace (show a) a
+
+{-|
+Like 'trace' but returning unit in an arbitrary monad. Allows for convenient
+use in do-notation. Note that the application of 'trace' is not an action in the
+monad, as 'traceIO' is in the 'IO' monad.
+
+> ... = do
+> x <- ...
+> traceM $ "x: " ++ show x
+> y <- ...
+> traceM $ "y: " ++ show y
+-}
+traceM :: (Monad m) => String -> m ()
+traceM string = trace string $ return ()
+
+{-|
+Like 'traceM', but uses 'show' on the argument to convert it to a 'String'.
+
+> ... = do
+> x <- ...
+> traceMShow $ x
+> y <- ...
+> traceMShow $ x + y
+-}
+traceShowM :: (Show a, Monad m) => a -> m ()
+traceShowM = traceM . show
+
-- | like 'trace', but additionally prints a call stack if one is
-- available.
--
More information about the ghc-commits
mailing list