contra-tracer-0.1.0.0: A simple interface for logging, tracing or monitoring.
Safe Haskell None
Language Haskell2010

Control.Tracer

Description

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

Documentation

newtype Tracer m a Source #

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

runTracer evaluates a Tracer (i.e. consumes its argument)

Constructors

Tracer

Fields

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:

Identity
contramap id = id
Composition
contramap (g . f) = contramap f . contramap g

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.

Minimal complete definition

contramap

Methods

contramap :: (a -> b) -> f b -> f a Source #

(>$) :: b -> f b -> f a infixl 4 Source #

Replace all locations in the output with the same value. The default definition is contramap . const , but this may be overridden with a more efficient version.

Instances

Instances details
Contravariant Predicate

A Predicate is a Contravariant Functor , because contramap can apply its function argument to the input of the predicate.

Instance details

Defined in Data.Functor.Contravariant

Contravariant Comparison

A Comparison is a Contravariant Functor , because contramap can apply its function argument to each input of the comparison function.

Instance details

Defined in Data.Functor.Contravariant

Contravariant Equivalence

Equivalence relations are Contravariant , because you can apply the contramapped function to each input to the equivalence relation.

Instance details

Defined in Data.Functor.Contravariant

Contravariant ( V1 :: Type -> Type )
Instance details

Defined in Data.Functor.Contravariant

Contravariant ( U1 :: Type -> Type )
Instance details

Defined in Data.Functor.Contravariant

Contravariant ( Op a)
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a0 -> b) -> Op a b -> Op a a0 Source #

(>$) :: b -> Op a b -> Op a a0 Source #

Contravariant ( Proxy :: Type -> Type )
Instance details

Defined in Data.Functor.Contravariant

Contravariant ( Tracer m) Source #
Instance details

Defined in Control.Tracer

Contravariant f => Contravariant ( Rec1 f)
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Rec1 f b -> Rec1 f a Source #

(>$) :: b -> Rec1 f b -> Rec1 f a Source #

Contravariant ( Const a :: Type -> Type )
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a0 -> b) -> Const a b -> Const a a0 Source #

(>$) :: b -> Const a b -> Const a a0 Source #

Contravariant f => Contravariant ( Alt f)
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Alt f b -> Alt f a Source #

(>$) :: b -> Alt f b -> Alt f a Source #

Contravariant ( K1 i c :: Type -> Type )
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> K1 i c b -> K1 i c a Source #

(>$) :: b -> K1 i c b -> K1 i c a Source #

( Contravariant f, Contravariant g) => Contravariant (f :+: g)
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> (f :+: g) b -> (f :+: g) a Source #

(>$) :: b -> (f :+: g) b -> (f :+: g) a Source #

( Contravariant f, Contravariant g) => Contravariant (f :*: g)
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> (f :*: g) b -> (f :*: g) a Source #

(>$) :: b -> (f :*: g) b -> (f :*: g) a Source #

( Contravariant f, Contravariant g) => Contravariant ( Product f g)
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Product f g b -> Product f g a Source #

(>$) :: b -> Product f g b -> Product f g a Source #

( Contravariant f, Contravariant g) => Contravariant ( Sum f g)
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Sum f g b -> Sum f g a Source #

(>$) :: b -> Sum f g b -> Sum f g a Source #

Contravariant f => Contravariant ( M1 i c f)
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> M1 i c f b -> M1 i c f a Source #

(>$) :: b -> M1 i c f b -> M1 i c f a Source #

( Functor f, Contravariant g) => Contravariant (f :.: g)
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> (f :.: g) b -> (f :.: g) a Source #

(>$) :: b -> (f :.: g) b -> (f :.: g) a Source #

( Functor f, Contravariant g) => Contravariant ( Compose f g)
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Compose f g b -> Compose f g a Source #

(>$) :: b -> Compose f g b -> Compose f g a Source #

tracing

traceWith :: Tracer m a -> a -> m () Source #

trace an observable value with a Tracer

tracers

nullTracer :: Applicative m => Tracer m a Source #

this Tracer forgets about all arguments

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.

condTracingM :: Monad m => m (a -> Bool ) -> Tracer m a -> Tracer m a Source #

conditionally trace an observable given the evaluation of a predicate in a monadic context.

natTracer :: ( forall x. m x -> n x) -> Tracer m s -> Tracer n s Source #

natural transformation from monad m to monad n .