Data.Tracer.TraceWith
Pattern synonym for tracer deconstruction.
Pattern Synonym
TraceWith
A pattern synonym to deconstruct a Tracer into itself, its underlying emitter function, and a contravariant mapping function against itself.
Fields:
tracer- returns the tracer unchangedtrace- extracts thea -> m ()emitter functioncontra- applies contravariant mapping
Examples
Pattern Matching
{-# LANGUAGE PatternSynonyms #-}
import Data.Tracer.Contrib
example :: Tracer IO String -> IO ()
example (TraceWith t emit mapTrace) = do
emit "Direct emit" -- use emitter directly
traceWith t "Normal trace" -- use tracer normally
let intTracer = mapTrace show -- map to trace integers
traceWith intTracer 42
Using Fields as Functions
import Data.Tracer.Contrib
example :: Tracer IO String -> IO ()
example t = do
-- Extract and use the emitter
let emit = trace t
emit "Hello"
-- Create a derived tracer
let intTracer = contra t show
traceWith intTracer 123
Composing Transformations
import Data.Tracer.Contrib
pipeline :: Tracer IO String -> IO ()
pipeline t = do
let showTracer = contra t show :: Tracer IO Int
doubleTracer = contra showTracer (* 2) :: Tracer IO Int
traceWith doubleTracer 5
-- Output: "10"
Direct Emitter Access
Sometimes you need the raw a -> m () function:
import Data.Tracer.Contrib
import qualified Data.Map.Strict as Map
logMap :: Tracer IO String -> Map.Map String Int -> IO ()
logMap t m = do
let emit = trace t
Map.traverseWithKey (\k v -> emit $ k ++ ": " ++ show v) m
pure ()
Notes
- The pattern is
COMPLETE- you can use it in exhaustive pattern matches contra t idis equivalent tot(identity mapping)- The
contrafield provides the same functionality ascontramapbut with a more convenient syntax for partial application