| Copyright | (C) 2025 Alexey Tochin |
|---|---|
| License | BSD3 (see the file LICENSE) |
| Maintainer | Alexey Tochin <Alexey.Tochin@gmail.com> |
| Safe Haskell | None |
| Language | Haskell2010 |
Debug.SimpleExpr.Utils.Traced
Description
This module provides a Traced type that wraps values with and adds automatic
tracing functionality from
Trace
for debugging purposes. When operations are performed
on Traced values, they output trace messages showing what computations
are being performed.
Overview
The Traced type is particularly useful for:
- Debugging complex mathematical computations
- Understanding the order of operations in lazy evaluation
- Tracking intermediate values in symbolic computations
- Educational purposes to visualize computation flow
Basic Usage
>>>import Debug.SimpleExpr.Utils.Traced (traced)
- Create traced values
>>>x = traced 3>>>y = traced 4
- Operations automatically trace
>>>x + y<<< TRACING: Calculating (+) of 3 and 4 >>> 7
Integration with NumHask
This module integrates with the numhask hierarchy, providing instances for:
>>>import NumHask (Additive, Subtractive, Multiplicative, Divisive, ExpField, TrigField)
Additive- addition and zeroSubtractive- subtraction and negationMultiplicative- multiplication and oneDivisive- divisionExpField- exponential, logarithm, and powerTrigField- trigonometric functions
Synopsis
- newtype Traced a = MkTraced {
- getTraced :: a
- traced :: a -> Traced a
- untraced :: Traced a -> a
- addTraceUnary :: Show a => String -> (a -> b) -> Traced a -> Traced b
- addTraceBinary :: (Show a, Show b) => String -> (a -> b -> c) -> Traced a -> Traced b -> Traced c
- addTraceTernary :: (Show a, Show b, Show c) => String -> (a -> b -> c -> d) -> Traced a -> Traced b -> Traced c -> Traced d
- withTrace :: String -> Traced a -> Traced a
- traceShow :: Show a => String -> Traced a -> Traced a
The Traced Type
A wrapper type that adds tracing to any value.
When operations are performed on Traced values, they output
trace messages to stderr showing what computations are happening.
Examples
Basic arithmetic with tracing:
>>>x = traced 5>>>y = traced 3>>>x * y<<< TRACING: Calculating (*) of 5 and 3 >>> 15
Tracing can be nested:
>>>(x + y) * (x - y)<<< TRACING: Calculating (+) of 5 and 3 >>> <<< TRACING: Calculating (-) of 5 and 3 >>> <<< TRACING: Calculating (*) of 8 and 2 >>> 16
Instances
| Functor Traced Source # | |
| ExtandableMap a b c d => ExtandableMap a b (Traced c) (Traced d) Source # |
|
| (Show b, AlgebraicPower a b) => AlgebraicPower a (Traced b) Source # | |
| (Show b, MultiplicativeAction a b) => MultiplicativeAction a (Traced b) Source # | |
| (Num a, Show a) => Num (Traced a) Source # | Standard |
Defined in Debug.SimpleExpr.Utils.Traced | |
| Show a => Show (Traced a) Source # | Show instance that displays the wrapped value. Note that this shows the wrapped value, not the
|
| Eq a => Eq (Traced a) Source # | |
| Hashable a => Hashable (Traced a) Source # | |
Defined in Debug.SimpleExpr.Utils.Traced | |
| (Additive a, Show a) => Additive (Traced a) Source # | NumHask |
| (Subtractive a, Show a) => Subtractive (Traced a) Source # | NumHask |
| (ExpField a, Show a) => ExpField (Traced a) Source # | NumHask
|
| (TrigField a, Show a) => TrigField (Traced a) Source # | NumHask
|
Defined in Debug.SimpleExpr.Utils.Traced | |
| (Divisive a, Show a) => Divisive (Traced a) Source # | NumHask |
| (Multiplicative a, Show a) => Multiplicative (Traced a) Source # | NumHask |
| FromInteger a => FromInteger (Traced a) Source # | |
Defined in Debug.SimpleExpr.Utils.Traced Methods fromInteger :: Integer -> Traced a # | |
Creating Traced Values
traced :: a -> Traced a Source #
Smart constructor for creating traced values.
This is equivalent to using the MkTraced constructor directly,
but provides a more descriptive name.
Examples
>>>traced 4242
Tracing Combinators
addTraceUnary :: Show a => String -> (a -> b) -> Traced a -> Traced b Source #
Apply a unary function with tracing.
This is the core building block for traced unary operations. It outputs a trace message before applying the function.
Examples
>>>import GHC.Num (abs)
>>>absoluteTraced = addTraceUnary "abs" abs>>>absoluteTraced (traced (-5))<<< TRACING: Calculating abs of -5 >>> 5
Custom unary operations:
>>>double = addTraceUnary "double" (\x -> x * 2)>>>double (traced 7)<<< TRACING: Calculating double of 7 >>> 14
addTraceBinary :: (Show a, Show b) => String -> (a -> b -> c) -> Traced a -> Traced b -> Traced c Source #
Apply a binary function with tracing.
This is the core building block for traced binary operations. It outputs a trace message before applying the function.
Examples
Basic binary operation:
>>>two = traced 2>>>three = traced 3>>>addTraced = addTraceBinary "(+)" (+)>>>addTraced two three<<< TRACING: Calculating (+) of 2 and 3 >>> 5
With symbolic expressions (assuming SimpleExpr is imported):
>>>import Debug.SimpleExpr (variable)>>>x = traced $ variable "x">>>y = traced $ variable "y">>>z = x + y>>>z ** 2<<< TRACING: Calculating (+) of x and y >>> <<< TRACING: Calculating (**) of x+y and 2 >>> (x+y)^2
addTraceTernary :: (Show a, Show b, Show c) => String -> (a -> b -> c -> d) -> Traced a -> Traced b -> Traced c -> Traced d Source #
Apply a ternary function with tracing.
Useful for functions that take three arguments.
Examples
>>>import Data.Ord (Ord(min, max))
>>>clamp = addTraceTernary "clamp" (\low high x -> max low (min high x))>>>clamp (traced 0) (traced 10) (traced 15)<<< TRACING: Calculating clamp of 0, 10, and 15 >>> 10
Utility Functions
withTrace :: String -> Traced a -> Traced a Source #
Execute a computation with a custom trace message.
This allows you to add custom trace points in your code.
Examples
>>>withTrace "Starting computation" $ (traced 3) + (traced 4)<<< TRACING: Starting computation >>> <<< TRACING: Calculating (+) of 3 and 4 >>> 7