summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Hazelden <philip.hazelden@gmail.com>2022-03-07 21:40:24 +0000
committerMarge Bot <ben+marge-bot@smart-cactus.org>2022-04-09 13:02:59 -0400
commit6c6c537964c6dc9ed3e8c4a4be68b587a27a3f50 (patch)
treecd8c8a6ea7f49036ab7e4e9f8148e26a37c27925
parentdcf30da8ff3ddcea9db3369870039f1b2c5d7b2c (diff)
downloadhaskell-6c6c537964c6dc9ed3e8c4a4be68b587a27a3f50.tar.gz
Add functions traceWith, traceShowWith, traceEventWith.
As discussed at https://github.com/haskell/core-libraries-committee/issues/36
-rw-r--r--libraries/base/Debug/Trace.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/libraries/base/Debug/Trace.hs b/libraries/base/Debug/Trace.hs
index e85fb7590a..90aed3acb9 100644
--- a/libraries/base/Debug/Trace.hs
+++ b/libraries/base/Debug/Trace.hs
@@ -27,6 +27,8 @@ module Debug.Trace (
traceId,
traceShow,
traceShowId,
+ traceWith,
+ traceShowWith,
traceStack,
traceIO,
traceM,
@@ -36,6 +38,7 @@ module Debug.Trace (
-- * Eventlog tracing
-- $eventlog_tracing
traceEvent,
+ traceEventWith,
traceEventIO,
flushEventLog,
@@ -164,6 +167,31 @@ traceShowId :: Show a => a -> a
traceShowId a = trace (show a) a
{-|
+Like 'trace', but outputs the result of calling a function on the argument.
+
+>>> traceWith fst ("hello","world")
+hello
+("hello","world")
+
+@since 4.17.0.0
+-}
+traceWith :: (a -> String) -> a -> a
+traceWith f a = trace (f a) a
+
+{-|
+Like 'traceWith', but uses 'show' on the result of the function to convert it to
+a 'String'.
+
+>>> traceShowWith length [1,2,3]
+3
+[1,2,3]
+
+@since 4.17.0.0
+-}
+traceShowWith :: Show b => (a -> b) -> a -> a
+traceShowWith f = traceWith (show . f)
+
+{-|
Like 'trace' but returning unit in an arbitrary 'Applicative' context. Allows
for convenient use in do-notation.
@@ -272,6 +300,13 @@ traceEventIO msg =
GHC.Foreign.withCString utf8 msg $ \(Ptr p) -> IO $ \s ->
case traceEvent# p s of s' -> (# s', () #)
+-- | Like 'traceEvent', but emits the result of calling a function on its
+-- argument.
+--
+-- @since 4.17.0.0
+traceEventWith :: (a -> String) -> a -> a
+traceEventWith f a = traceEvent (f a) a
+
-- $markers
--
-- When looking at a profile for the execution of a program we often want to