HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Moduletrace-dispatcher-2.13.1Haskell2010

Cardano.Logging

  • 1 class
  • 18 values
valuenatTracer :: (forall x. m x -> n x) -> Tracer m s -> Tracer n s
#

Use a natural transformation to change the m type. This is useful, for instance, to use concrete IO tracers in monad transformer stacks that have IO as their base.

valuetraceMaybe :: Monad m => (a -> Maybe b) -> Tracer m b -> Tracer m a
#

Run a tracer only for the Just variant of a Maybe. If it's Nothing, the nullTracer is used (no output).

The arrow representation allows for proper laziness: if the tracer parameter does not produce any tracing effects, then the predicate won't even be evaluated. Contrast with the simple contravariant representation as a -> m (), in which the predicate _must_ be forced no matter what, because it's impossible to know a priori whether that function will not produce any tracing effects.

It's written out explicitly for demonstration. Could also use arrow notation:

traceMaybe p tr = Tracer $ proc a -> do
  case k a of
    Just b  -> use tr        -< b
    Nothing -> Arrow.squelch -< ()
valueuse :: Tracer m a -> TracerA m a ()
#

Inverse of arrow. Useful when writing arrow tracers which use a contravariant tracer (the newtype in this module).

valueeffect :: (a -> m b) -> TracerA m a b
#

Do a non-emitting effect. This effect will only be run if some part of the tracer downstream emits (see emit).

valueemit :: Applicative m => (a -> m ()) -> TracerA m a ()
#

Do an emitting effect. Contrast with effect which does not make the tracer an emitting tracer.

valuenat :: (forall x. m x -> n x) -> TracerA m a b -> TracerA n a b
#

Use a natural transformation to change the underlying monad.

valuesquelch :: Applicative m => TracerA m a ()
#

Ignore the input and do not emit. The name is intended to lead to clear and suggestive arrow expressions.

classclass Contravariant (f :: Type -> Type) where
#

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 :: (a' -> a) -> (Predicate a -> Predicate a')
  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.

Methods

  • contramap :: (a' -> a) -> f a -> f a'
  • (>$) :: b -> f b -> f ainfixl 4

    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.

Instances38Contravariant, …