HORIZON HASKELLDocslts/ghc-10.0.x2b276322026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 2b27632 · 2026-10-31

Modulebase-compat-0.15.0Haskell2010

Data.Functor.Contravariant.Compat

Synopsis
newtypenewtype Comparison a

Defines a total ordering on a type as per compare.

This condition is not checked by the types. You must ensure that the supplied values are valid total orderings yourself.

Constructors

Instances3Contravariant, Semigroup, Monoid
  • Contravariant ComparisonDefined in base-4.23.0.0 · Data.Functor.Contravariant

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

  • Semigroup (Comparison a)Defined in base-4.23.0.0 · Data.Functor.Contravariant

    (<>) on comparisons combines results with (<>) @Ordering. Without newtypes this equals liftA2 (liftA2 (<>)).

    (<>) :: Comparison a -> Comparison a -> Comparison a
    Comparison cmp <> Comparison cmp' = Comparison a a' ->
      cmp a a' <> cmp a a'
    
  • Monoid (Comparison a)Defined in base-4.23.0.0 · Data.Functor.Contravariant

    mempty on comparisons always returns EQ. Without newtypes this equals pure (pure EQ).

    mempty :: Comparison a
    mempty = Comparison _ _ -> EQ
    
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.

Instances18Contravariant, …
newtypenewtype Equivalence a

This data type represents an equivalence relation.

Equivalence relations are expected to satisfy three laws:

Reflexivity

getEquivalence f a a = True

Symmetry

getEquivalence f a b = getEquivalence f b a

Transitivity

If

getEquivalence f a b

and

getEquivalence f b c

are both

True

then so is

getEquivalence f a c

.

The types alone do not enforce these laws, so you'll have to check them yourself.

Constructors

Instances3Contravariant, Semigroup, Monoid
  • Contravariant EquivalenceDefined in base-4.23.0.0 · Data.Functor.Contravariant

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

  • Semigroup (Equivalence a)Defined in base-4.23.0.0 · Data.Functor.Contravariant

    (<>) on equivalences uses logical conjunction (&&) on the results. Without newtypes this equals liftA2 (liftA2 (&&)).

    (<>) :: Equivalence a -> Equivalence a -> Equivalence a
    Equivalence equiv <> Equivalence equiv' = Equivalence a b ->
      equiv a b && equiv' a b
    
  • Monoid (Equivalence a)Defined in base-4.23.0.0 · Data.Functor.Contravariant

    mempty on equivalences always returns True. Without newtypes this equals pure (pure True).

    mempty :: Equivalence a
    mempty = Equivalence _ _ -> True
    
newtypenewtype Op a b

Dual function arrows.

Constructors

Instances7Category, Contravariant, Floating, Fractional, Num, Semigroup, …
  • Category OpDefined in base-4.23.0.0 · Data.Functor.Contravariant
  • Contravariant (Op a)Defined in base-4.23.0.0 · Data.Functor.Contravariant
  • Floating a => Floating (Op a b)Defined in base-4.23.0.0 · Data.Functor.Contravariant
  • Fractional a => Fractional (Op a b)Defined in base-4.23.0.0 · Data.Functor.Contravariant
  • Num a => Num (Op a b)Defined in base-4.23.0.0 · Data.Functor.Contravariant
  • Semigroup a => Semigroup (Op a b)Defined in base-4.23.0.0 · Data.Functor.Contravariant

    (<>) @(Op a b) without newtypes is (<>) @(b->a) = liftA2 (<>). This lifts the Semigroup operation (<>) over the output of a.

    (<>) :: Op a b -> Op a b -> Op a b
    Op f <> Op g = Op a -> f a <> g a
    
  • Monoid a => Monoid (Op a b)Defined in base-4.23.0.0 · Data.Functor.Contravariant

    mempty @(Op a b) without newtypes is mempty @(b->a) = _ -> mempty.

    mempty :: Op a b
    mempty = Op _ -> mempty
    
newtypenewtype Predicate a
#

Constructors

Instances3Contravariant, Semigroup, Monoid
  • Contravariant PredicateDefined in base-4.23.0.0 · Data.Functor.Contravariant

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

    Without newtypes contramap f equals precomposing with f (= (. f)).

    contramap :: (a' -> a) -> (Predicate a -> Predicate a')
    contramap f (Predicate g) = Predicate (g . f)
    
  • Semigroup (Predicate a)Defined in base-4.23.0.0 · Data.Functor.Contravariant

    (<>) on predicates uses logical conjunction (&&) on the results. Without newtypes this equals liftA2 (&&).

    (<>) :: Predicate a -> Predicate a -> Predicate a
    Predicate pred <> Predicate pred' = Predicate a ->
      pred a && pred' a
    
  • Monoid (Predicate a)Defined in base-4.23.0.0 · Data.Functor.Contravariant

    mempty on predicates always returns True. Without newtypes this equals pure True.

    mempty :: Predicate a
    mempty = _ -> True