Safe Haskell | None |
---|---|
Language | Haskell2010 |
Tracer
is a contravariant functor to thread observable values through a
number of transformers, possibly annotating them with additional information,
or filtering them based on evaluating predicates.
Synopsis
-
newtype
Tracer
m a =
Tracer
{
- runTracer :: a -> m ()
- class Contravariant (f :: Type -> Type ) where
- traceWith :: Tracer m a -> a -> m ()
- nullTracer :: Applicative m => Tracer m a
- stdoutTracer :: MonadIO m => Tracer m String
- debugTracer :: Applicative m => Tracer m String
- contramapM :: Monad m => (a -> m b) -> Tracer m b -> Tracer m a
- showTracing :: Show a => Tracer m String -> Tracer m a
- condTracing :: Monad m => (a -> Bool ) -> Tracer m a -> Tracer m a
- condTracingM :: Monad m => m (a -> Bool ) -> Tracer m a -> Tracer m a
- natTracer :: ( forall x. m x -> n x) -> Tracer m s -> Tracer n s
Documentation
example: simply output a message on the console
let logTrace = traceWith $ showTracing $ stdoutTracer in logTrace "hello world"
example: calling a function and passing in a
Tracer
example1 :: IO () example1 = do let logTrace a = traceWith (showTracing (contramap ("Debug: " ++) stdoutTracer)) a void $ callFun1 logTrace
callFun1 :: (String -> IO ()) -> IO Int callFun1 logTrace = do logTrace "in function 1" return 42
Instances
Contravariant ( Tracer m) Source # | |
Applicative m => Semigroup ( Tracer m s) Source # | |
Applicative m => Monoid ( Tracer m s) Source # | |
class Contravariant (f :: Type -> Type ) where Source #
The class of contravariant functors.
Whereas in Haskell, one can think of a
Functor
as containing or producing
values, a contravariant functor is a functor that can be thought of as
consuming
values.
As an example, consider the type of predicate functions
a -> Bool
. One
such predicate might be
negative x = x < 0
, which
classifies integers as to whether they are negative. However, given this
predicate, we can re-use it in other situations, providing we have a way to
map values
to
integers. For instance, we can use the
negative
predicate
on a person's bank balance to work out if they are currently overdrawn:
newtype Predicate a = Predicate { getPredicate :: a -> Bool } instance Contravariant Predicate where contramap f (Predicate p) = Predicate (p . f) | `- First, map the input... `----- then apply the predicate. overdrawn :: Predicate Person overdrawn = contramap personBankBalance negative
Any instance should be subject to the following laws:
Note, that the second law follows from the free theorem of the type of
contramap
and the first law, so you need only check that the former
condition holds.
Instances
tracing
tracers
nullTracer :: Applicative m => Tracer m a Source #
this
Tracer
forgets about all arguments
debugTracer :: Applicative m => Tracer m String Source #
transformers
contramapM :: Monad m => (a -> m b) -> Tracer m b -> Tracer m a Source #
Transform a tracer using a Kleisli map.
showTracing :: Show a => Tracer m String -> Tracer m a Source #
transform a traced value to a showable instance.
condTracing :: Monad m => (a -> Bool ) -> Tracer m a -> Tracer m a Source #
conditionally trace an observable given the evaluation of a predicate.