diff options
author | Kavon Farvardin <kavon@farvard.in> | 2018-09-23 15:29:37 -0500 |
---|---|---|
committer | Kavon Farvardin <kavon@farvard.in> | 2018-09-23 15:29:37 -0500 |
commit | 84c2ad99582391005b5e873198b15e9e9eb4f78d (patch) | |
tree | caa8c2f2ec7e97fbb4977263c6817c9af5025cf4 /libraries/base/Debug | |
parent | 8ddb47cfcf5776e9a3c55fd37947c8a95e00fa12 (diff) | |
parent | e68b439fe5de61b9a2ca51af472185c62ccb8b46 (diff) | |
download | haskell-wip/T13904.tar.gz |
update to current master againwip/T13904
Diffstat (limited to 'libraries/base/Debug')
-rw-r--r-- | libraries/base/Debug/Trace.hs | 72 | ||||
-rw-r--r-- | libraries/base/Debug/Trace.hs-boot | 76 |
2 files changed, 125 insertions, 23 deletions
diff --git a/libraries/base/Debug/Trace.hs b/libraries/base/Debug/Trace.hs index 40475d32f9..7f40b10156 100644 --- a/libraries/base/Debug/Trace.hs +++ b/libraries/base/Debug/Trace.hs @@ -55,6 +55,9 @@ import GHC.Show import GHC.Stack import Data.List +-- $setup +-- >>> import Prelude + -- $tracing -- -- The 'trace', 'traceShow' and 'traceIO' functions print messages to an output @@ -104,7 +107,10 @@ before returning the second argument as its result. For example, this returns the value of @f x@ but first outputs the message. -> trace ("calling f with x = " ++ show x) (f x) +>>> let x = 123; f = show +>>> trace ("calling f with x = " ++ show x) (f x) +"calling f with x = 123 +123" The 'trace' function should /only/ be used for debugging, or for monitoring execution. The function is not referentially transparent: its type indicates @@ -119,6 +125,10 @@ trace string expr = unsafePerformIO $ do {-| Like 'trace' but returns the message instead of a third value. +>>> traceId "hello" +"hello +hello" + @since 4.7.0.0 -} traceId :: String -> String @@ -129,23 +139,26 @@ 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 expressions inside a function. For example here we print the value of the -variables @x@ and @z@: +variables @x@ and @y@: + +>>> let f x y = traceShow (x,y) (x + y) in f (1+2) 5 +(3,5) +8 -> f x y = -> traceShow (x, z) $ result -> where -> z = ... -> ... -} -traceShow :: (Show a) => a -> b -> b +traceShow :: Show a => a -> b -> b traceShow = trace . show {-| Like 'traceShow' but returns the shown value instead of a third value. +>>> traceShowId (1+2+3, "hello" ++ "world") +(6,"helloworld") +(6,"helloworld") + @since 4.7.0.0 -} -traceShowId :: (Show a) => a -> a +traceShowId :: Show a => a -> a traceShowId a = trace (show a) a {-| @@ -156,28 +169,41 @@ Note that the application of 'traceM' is not an action in the 'Applicative' context, as 'traceIO' is in the 'IO' type. While the fresh bindings in the following example will force the 'traceM' expressions to be reduced every time the @do@-block is executed, @traceM "not crashed"@ would only be reduced once, -and the message would only be printed once. If your monad is in 'MonadIO', -@liftIO . traceIO@ may be a better option. - -> ... = do -> x <- ... -> traceM $ "x: " ++ show x -> y <- ... -> traceM $ "y: " ++ show y +and the message would only be printed once. If your monad is in +'Control.Monad.IO.Class.MonadIO', @'Control.Monad.IO.Class.liftIO' . 'traceIO'@ +may be a better option. + +>>> :{ +do + x <- Just 3 + traceM ("x: " ++ show x) + y <- pure 12 + traceM ("y: " ++ show y) + pure (x*2 + y) +:} +x: 3 +y: 12 +Just 18 @since 4.7.0.0 -} -traceM :: (Applicative f) => String -> f () +traceM :: Applicative f => String -> f () traceM string = trace string $ pure () {-| Like 'traceM', but uses 'show' on the argument to convert it to a 'String'. -> ... = do -> x <- ... -> traceShowM $ x -> y <- ... -> traceShowM $ x + y +>>> :{ +do + x <- Just 3 + traceShowM x + y <- pure 12 + traceShowM y + pure (x*2 + y) +:} +3 +12 +Just 18 @since 4.7.0.0 -} diff --git a/libraries/base/Debug/Trace.hs-boot b/libraries/base/Debug/Trace.hs-boot new file mode 100644 index 0000000000..9dbbe2dd37 --- /dev/null +++ b/libraries/base/Debug/Trace.hs-boot @@ -0,0 +1,76 @@ +{-# LANGUAGE Unsafe #-} +{-# LANGUAGE MagicHash #-} +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE UnboxedTuples #-} + +-- This boot file is necessary to allow GHC developers to +-- use trace facilities in those (relatively few) modules that Debug.Trace +-- itself depends on. It is also necessary to make DsMonad.pprRuntimeTrace +-- trace injections work in those modules. + +----------------------------------------------------------------------------- +-- | +-- Module : Debug.Trace +-- Copyright : (c) The University of Glasgow 2001 +-- License : BSD-style (see the file libraries/base/LICENSE) +-- +-- Maintainer : libraries@haskell.org +-- Stability : provisional +-- Portability : portable +-- +-- Functions for tracing and monitoring execution. +-- +-- These can be useful for investigating bugs or performance problems. +-- They should /not/ be used in production code. +-- +----------------------------------------------------------------------------- + +module Debug.Trace ( + -- * Tracing + -- $tracing + trace, + traceId, + traceShow, + traceShowId, + traceStack, + traceIO, + traceM, + traceShowM, + + -- * Eventlog tracing + -- $eventlog_tracing + traceEvent, + traceEventIO, + + -- * Execution phase markers + -- $markers + traceMarker, + traceMarkerIO, + ) where + +import GHC.Base +import GHC.Show + +traceIO :: String -> IO () + +trace :: String -> a -> a + +traceId :: String -> String + +traceShow :: Show a => a -> b -> b + +traceShowId :: Show a => a -> a + +traceM :: Applicative f => String -> f () + +traceShowM :: (Show a, Applicative f) => a -> f () + +traceStack :: String -> a -> a + +traceEvent :: String -> a -> a + +traceEventIO :: String -> IO () + +traceMarker :: String -> a -> a + +traceMarkerIO :: String -> IO () |