Skip to content

Commit 0c9ff65

Browse files
authored
Merge pull request #3 from lemastero/traceAll
add traceAll
2 parents 071c658 + 3f13c5c commit 0c9ff65

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/Control/Tracer.hs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ module Control.Tracer
6464
, condTracing
6565
, condTracingM
6666
, showTracing
67+
, traceTraversable
68+
, traceAll
6769
-- * Re-export of Contravariant
6870
, Contravariant(..)
6971
) where
@@ -73,6 +75,7 @@ import Control.Monad.IO.Class (MonadIO (..))
7375
import Data.Functor.Contravariant (Contravariant (..))
7476
import Data.Semigroup (Semigroup (..))
7577
import Data.Monoid (Monoid (..))
78+
import Data.Foldable (traverse_)
7679
import Debug.Trace (traceM)
7780

7881
-- | This type describes some effect in @m@ which depends upon some value of
@@ -108,7 +111,7 @@ import Debug.Trace (traceM)
108111
newtype Tracer m a = Tracer { runTracer :: a -> m () }
109112

110113
instance Contravariant (Tracer m) where
111-
contramap f (Tracer g) = Tracer (g . f)
114+
contramap f = mapTracer (. f)
112115

113116
-- | @tr1 <> tr2@ will run @tr1@ and then @tr2@ with the same input.
114117
instance Applicative m => Semigroup (Tracer m s) where
@@ -137,7 +140,7 @@ contramapM :: Monad m
137140
=> (a -> m b)
138141
-> Tracer m b
139142
-> Tracer m a
140-
contramapM f (Tracer tr) = Tracer (f >=> tr)
143+
contramapM f = mapTracer (f >=>)
141144

142145
-- | Use a predicate to filter traced values: if it gives false then the
143146
-- tracer will not be run.
@@ -154,11 +157,25 @@ condTracingM activeP tr = Tracer $ \s -> do
154157
active <- activeP
155158
when (active s) (traceWith tr s)
156159

160+
traceTraversable :: (Applicative m, Foldable t)
161+
=> Tracer m a -> Tracer m (t a)
162+
traceTraversable = mapTracer traverse_
163+
164+
traceAll :: (Applicative m, Traversable t)
165+
=> (b -> t a) -> Tracer m a -> Tracer m b
166+
traceAll f = contramap f . traceTraversable
167+
168+
mapTracer :: ((a -> m ()) -> b -> n ())
169+
-> Tracer m a -> Tracer n b
170+
mapTracer f (Tracer tr) = Tracer (f tr)
171+
157172
-- | Use a natural transformation to change the monad. This is useful, for
158173
-- instance, to use concrete IO tracers in monad transformer stacks that have
159174
-- IO as their base.
160-
natTracer :: (forall x . m x -> n x) -> Tracer m s -> Tracer n s
161-
natTracer nat (Tracer tr) = Tracer (nat . tr)
175+
natTracer :: (forall x. m x -> n x)
176+
-> Tracer m a
177+
-> Tracer n a
178+
natTracer f = mapTracer (f .)
162179

163180
-- | Trace strings to stdout. Output could be jumbled when this is used from
164181
-- multiple threads. Consider 'debugTracer' instead.

0 commit comments

Comments
 (0)