Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- class Contravariant k => ContravariantCPS r k | k -> r where
- class Contravariant (f :: Type -> Type) where
Contravariant continuation-passing style
class Contravariant k => ContravariantCPS r k | k -> r where Source #
Instances
ContravariantCPS Bool Predicate Source # | |
ContravariantCPS Bool Equivalence Source # | |
Defined in Data.Functor.Contravariant.CPS (<#>) :: ((a' ~~ Bool) ~> a) -> Equivalence a -> Equivalence a' Source # | |
ContravariantCPS Ordering Comparison Source # | |
Defined in Data.Functor.Contravariant.CPS (<#>) :: ((a' ~~ Ordering) ~> a) -> Comparison a -> Comparison a' Source # | |
ContravariantCPS r (Op r) Source # | |
ContravariantCPS r ((!) r) Source # | |
class 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 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.