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:
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.
Instances18Contravariant, …
Contravariant ComparisonDefined in base-4.20.2.0 · Data.Functor.ContravariantA Comparison is a Contravariant Functor, because contramap can apply its function argument to each input of the comparison function.
Contravariant EquivalenceDefined in base-4.20.2.0 · Data.Functor.ContravariantEquivalence relations are Contravariant, because you can apply the contramapped function to each input to the equivalence relation.
Contravariant PredicateDefined in base-4.20.2.0 · Data.Functor.ContravariantA Predicate is a Contravariant Functor, because contramap can apply its function argument to the input of the predicate.
Without newtypes
contramap fequals precomposing withf(=(. f)).contramap :: (a' -> a) -> (Predicate a -> Predicate a') contramap f (Predicate g) = Predicate (g . f)Contravariant ProxyDefined in base-4.20.2.0 · Data.Functor.ContravariantContravariant U1Defined in base-4.20.2.0 · Data.Functor.ContravariantContravariant V1Defined in base-4.20.2.0 · Data.Functor.ContravariantContravariant (Op a)Defined in base-4.20.2.0 · Data.Functor.ContravariantContravariant (Const a)Defined in base-4.20.2.0 · Data.Functor.ContravariantContravariant f => Contravariant (Alt f)Defined in base-4.20.2.0 · Data.Functor.ContravariantContravariant f => Contravariant (Rec1 f)Defined in base-4.20.2.0 · Data.Functor.ContravariantContravariant (K1 i c)Defined in base-4.20.2.0 · Data.Functor.Contravariant(Contravariant f, Contravariant g) => Contravariant (Product f g)Defined in base-4.20.2.0 · Data.Functor.Contravariant(Contravariant f, Contravariant g) => Contravariant (Sum f g)Defined in base-4.20.2.0 · Data.Functor.Contravariant(Contravariant f, Contravariant g) => Contravariant (f :*: g)Defined in base-4.20.2.0 · Data.Functor.Contravariant(Contravariant f, Contravariant g) => Contravariant (f :+: g)Defined in base-4.20.2.0 · Data.Functor.ContravariantContravariant f => Contravariant (M1 i c f)Defined in base-4.20.2.0 · Data.Functor.Contravariant(Functor f, Contravariant g) => Contravariant (Compose f g)Defined in base-4.20.2.0 · Data.Functor.Contravariant(Functor f, Contravariant g) => Contravariant (f :.: g)Defined in base-4.20.2.0 · Data.Functor.Contravariant