Copyright | (C) 2007-2015 Edward Kmett |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | libraries@haskell.org |
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: base-4.12.0.0
Synopsis
- class Contravariant f where
- phantom :: (Functor f, Contravariant f) => f a -> f b
- (>$<) :: Contravariant f => (a -> b) -> f b -> f a
- (>$$<) :: Contravariant f => f b -> (a -> b) -> f a
- ($<) :: Contravariant f => f b -> b -> f a
- newtype Predicate a = Predicate {
- getPredicate :: a -> Bool
- newtype Comparison a = Comparison {
- getComparison :: a -> a -> Ordering
- defaultComparison :: Ord a => Comparison a
- newtype Equivalence a = Equivalence {
- getEquivalence :: a -> a -> Bool
- defaultEquivalence :: Eq a => Equivalence a
- comparisonEquivalence :: Comparison a -> Equivalence a
- newtype Op a b = Op {
- getOp :: b -> a
Contravariant Functors
class Contravariant f 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:
contramap id = id contramap f . contramap g = contramap (g . f)
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.
Instances
phantom :: (Functor f, Contravariant f) => f a -> f b #
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
Operators
(>$<) :: Contravariant f => (a -> b) -> f b -> f a infixl 4 #
This is an infix alias for contramap
.
(>$$<) :: Contravariant f => f b -> (a -> b) -> f a infixl 4 #
This is an infix version of contramap
with the arguments flipped.
($<) :: Contravariant f => f b -> b -> f a infixl 4 #
This is >$
with its arguments flipped.
Predicates
Predicate | |
|
Comparisons
newtype 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.
Comparison | |
|
Instances
Contravariant Comparison # | A |
Defined in Data.Functor.Contravariant contramap :: (a -> b) -> Comparison b -> Comparison a # (>$) :: b -> Comparison b -> Comparison a # | |
Semigroup (Comparison a) # | |
Defined in Data.Functor.Contravariant (<>) :: Comparison a -> Comparison a -> Comparison a # sconcat :: NonEmpty (Comparison a) -> Comparison a # stimes :: Integral b => b -> Comparison a -> Comparison a # | |
Monoid (Comparison a) # | |
Defined in Data.Functor.Contravariant mempty :: Comparison a # mappend :: Comparison a -> Comparison a -> Comparison a # mconcat :: [Comparison a] -> Comparison a # |
defaultComparison :: Ord a => Comparison a #
Compare using compare
.
Equivalence Relations
newtype 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
and getEquivalence
f a b
are both getEquivalence
f b cTrue
then so is
.getEquivalence
f a c
The types alone do not enforce these laws, so you'll have to check them yourself.
Equivalence | |
|
Instances
Contravariant Equivalence # | Equivalence relations are |
Defined in Data.Functor.Contravariant contramap :: (a -> b) -> Equivalence b -> Equivalence a # (>$) :: b -> Equivalence b -> Equivalence a # | |
Semigroup (Equivalence a) # | |
Defined in Data.Functor.Contravariant (<>) :: Equivalence a -> Equivalence a -> Equivalence a # sconcat :: NonEmpty (Equivalence a) -> Equivalence a # stimes :: Integral b => b -> Equivalence a -> Equivalence a # | |
Monoid (Equivalence a) # | |
Defined in Data.Functor.Contravariant mempty :: Equivalence a # mappend :: Equivalence a -> Equivalence a -> Equivalence a # mconcat :: [Equivalence a] -> Equivalence a # |
defaultEquivalence :: Eq a => Equivalence a #
comparisonEquivalence :: Comparison a -> Equivalence a #
Dual arrows
Dual function arrows.
Instances
Contravariant (Op a) # | |
Category Op # | |
Floating a => Floating (Op a b) # | |
Fractional a => Fractional (Op a b) # | |
Num a => Num (Op a b) # | |
Semigroup a => Semigroup (Op a b) # | |
Monoid a => Monoid (Op a b) # | |