Copyright  (C) 20072015 Edward Kmett 

License  BSDstyle (see the file LICENSE) 
Maintainer  [email protected] 
Stability  provisional 
Portability  portable 
Safe Haskell  Trustworthy 
Language  Haskell2010 
Contravariant
functors, sometimes referred to colloquially as Cofunctor
, even though the dual of a Functor
is just a Functor
. As with Functor
the definition of Contravariant
for a given ADT is unambiguous.
Since: base4.12.0.0
class Contravariant f 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 reuse 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.
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.
phantom :: (Functor f, Contravariant f) => f a > f b Source
If f
is both Functor
and Contravariant
then by the time you factor in the laws of each of those classes, it can't actually use its argument in any meaningful capacity.
This method is surprisingly useful. Where both instances exist and are lawful we have the following laws:
fmap f ≡ phantom contramap f ≡ phantom
(>$<) :: Contravariant f => (a > b) > f b > f a infixl 4 Source
This is an infix alias for contramap
.
(>$$<) :: Contravariant f => f b > (a > b) > f a infixl 4 Source
This is an infix version of contramap
with the arguments flipped.
($<) :: Contravariant f => f b > b > f a infixl 4 Source
This is >$
with its arguments flipped.
Predicate  
Fields

Contravariant Predicate  A 
Semigroup (Predicate a)  
Monoid (Predicate a)  
newtype Comparison a Source
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.
Comparison  
Fields

Contravariant Comparison  A 
Defined in Data.Functor.Contravariant Methodscontramap :: (a > b) > Comparison b > Comparison a Source (>$) :: b > Comparison b > Comparison a Source  
Semigroup (Comparison a)  
Defined in Data.Functor.Contravariant Methods(<>) :: Comparison a > Comparison a > Comparison a Source sconcat :: NonEmpty (Comparison a) > Comparison a Source stimes :: Integral b => b > Comparison a > Comparison a Source  
Monoid (Comparison a)  
Defined in Data.Functor.Contravariant Methodsmempty :: Comparison a Source mappend :: Comparison a > Comparison a > Comparison a Source mconcat :: [Comparison a] > Comparison a Source 
defaultComparison :: Ord a => Comparison a Source
Compare using compare
.
newtype Equivalence a Source
This data type represents an equivalence relation.
Equivalence relations are expected to satisfy three laws:
getEquivalence f a a = True
getEquivalence f a b = getEquivalence f b a
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.
Equivalence  
Fields

Contravariant Equivalence  Equivalence relations are 
Defined in Data.Functor.Contravariant Methodscontramap :: (a > b) > Equivalence b > Equivalence a Source (>$) :: b > Equivalence b > Equivalence a Source  
Semigroup (Equivalence a)  
Defined in Data.Functor.Contravariant Methods(<>) :: Equivalence a > Equivalence a > Equivalence a Source sconcat :: NonEmpty (Equivalence a) > Equivalence a Source stimes :: Integral b => b > Equivalence a > Equivalence a Source  
Monoid (Equivalence a)  
Defined in Data.Functor.Contravariant Methodsmempty :: Equivalence a Source mappend :: Equivalence a > Equivalence a > Equivalence a Source mconcat :: [Equivalence a] > Equivalence a Source 
defaultEquivalence :: Eq a => Equivalence a Source
Check for equivalence with ==
.
Note: The instances for Double
and Float
violate reflexivity for NaN
.
comparisonEquivalence :: Comparison a > Equivalence a Source
Dual function arrows.
Contravariant (Op a)  
Category Op  
Floating a => Floating (Op a b)  
Defined in Data.Functor.Contravariant Methodsexp :: Op a b > Op a b Source log :: Op a b > Op a b Source sqrt :: Op a b > Op a b Source (**) :: Op a b > Op a b > Op a b Source logBase :: Op a b > Op a b > Op a b Source sin :: Op a b > Op a b Source cos :: Op a b > Op a b Source tan :: Op a b > Op a b Source asin :: Op a b > Op a b Source acos :: Op a b > Op a b Source atan :: Op a b > Op a b Source sinh :: Op a b > Op a b Source cosh :: Op a b > Op a b Source tanh :: Op a b > Op a b Source asinh :: Op a b > Op a b Source acosh :: Op a b > Op a b Source atanh :: Op a b > Op a b Source log1p :: Op a b > Op a b Source expm1 :: Op a b > Op a b Source  
Fractional a => Fractional (Op a b)  
Num a => Num (Op a b)  
Defined in Data.Functor.Contravariant  
Semigroup a => Semigroup (Op a b)  
Monoid a => Monoid (Op a b)  
© The University of Glasgow and others
Licensed under a BSDstyle license (see top of the page).
https://downloads.haskell.org/~ghc/8.8.3/docs/html/libraries/base4.13.0.0/DataFunctorContravariant.html