base-4.13.0.0: Basic libraries
Copyright(C) 2007-2015 Edward Kmett
LicenseBSD-style (see the file LICENSE)
Maintainerlibraries@haskell.org
Stabilityprovisional
Portabilityportable
Safe HaskellTrustworthy
LanguageHaskell2010

Data.Functor.Contravariant

Description

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

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:

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.

Minimal complete definition

contramap

Methods

contramap :: (a -> b) -> f b -> f a #

(>$) :: b -> f b -> f a infixl 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.

Instances

Instances details
Contravariant Equivalence #

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

Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Equivalence b -> Equivalence a #

(>$) :: b -> Equivalence b -> Equivalence a #

Contravariant Comparison #

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

Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Comparison b -> Comparison a #

(>$) :: b -> Comparison b -> Comparison a #

Contravariant Predicate #

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

Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Predicate b -> Predicate a #

(>$) :: b -> Predicate b -> Predicate a #

Contravariant (V1 :: Type -> Type) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> V1 b -> V1 a #

(>$) :: b -> V1 b -> V1 a #

Contravariant (U1 :: Type -> Type) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> U1 b -> U1 a #

(>$) :: b -> U1 b -> U1 a #

Contravariant (Proxy :: Type -> Type) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Proxy b -> Proxy a #

(>$) :: b -> Proxy b -> Proxy a #

Contravariant (Op a) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a0 -> b) -> Op a b -> Op a a0 #

(>$) :: b -> Op a b -> Op a a0 #

Contravariant f => Contravariant (Rec1 f) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Rec1 f b -> Rec1 f a #

(>$) :: b -> Rec1 f b -> Rec1 f a #

Contravariant f => Contravariant (Alt f) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Alt f b -> Alt f a #

(>$) :: b -> Alt f b -> Alt f a #

Contravariant (Const a :: Type -> Type) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a0 -> b) -> Const a b -> Const a a0 #

(>$) :: b -> Const a b -> Const a a0 #

Contravariant (K1 i c :: Type -> Type) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> K1 i c b -> K1 i c a #

(>$) :: b -> K1 i c b -> K1 i c a #

(Contravariant f, Contravariant g) => Contravariant (f :+: g) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> (f :+: g) b -> (f :+: g) a #

(>$) :: b -> (f :+: g) b -> (f :+: g) a #

(Contravariant f, Contravariant g) => Contravariant (f :*: g) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> (f :*: g) b -> (f :*: g) a #

(>$) :: b -> (f :*: g) b -> (f :*: g) a #

(Contravariant f, Contravariant g) => Contravariant (Sum f g) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Sum f g b -> Sum f g a #

(>$) :: b -> Sum f g b -> Sum f g a #

(Contravariant f, Contravariant g) => Contravariant (Product f g) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Product f g b -> Product f g a #

(>$) :: b -> Product f g b -> Product f g a #

Contravariant f => Contravariant (M1 i c f) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> M1 i c f b -> M1 i c f a #

(>$) :: b -> M1 i c f b -> M1 i c f a #

(Functor f, Contravariant g) => Contravariant (f :.: g) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> (f :.: g) b -> (f :.: g) a #

(>$) :: b -> (f :.: g) b -> (f :.: g) a #

(Functor f, Contravariant g) => Contravariant (Compose f g) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Compose f g b -> Compose f g a #

(>$) :: b -> Compose f g b -> Compose f g a #

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

newtype Predicate a #

Constructors

Predicate 

Fields

Instances

Instances details
Contravariant Predicate #

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

Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Predicate b -> Predicate a #

(>$) :: b -> Predicate b -> Predicate a #

Semigroup (Predicate a) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

(<>) :: Predicate a -> Predicate a -> Predicate a #

sconcat :: NonEmpty (Predicate a) -> Predicate a #

stimes :: Integral b => b -> Predicate a -> Predicate a #

Monoid (Predicate a) # 
Instance details

Defined in Data.Functor.Contravariant

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.

Constructors

Comparison 

Fields

Instances

Instances details
Contravariant Comparison #

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

Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Comparison b -> Comparison a #

(>$) :: b -> Comparison b -> Comparison a #

Semigroup (Comparison a) # 
Instance details

Defined in Data.Functor.Contravariant

Monoid (Comparison a) # 
Instance details

Defined in Data.Functor.Contravariant

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 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

Equivalence 

Fields

Instances

Instances details
Contravariant Equivalence #

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

Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a -> b) -> Equivalence b -> Equivalence a #

(>$) :: b -> Equivalence b -> Equivalence a #

Semigroup (Equivalence a) # 
Instance details

Defined in Data.Functor.Contravariant

Monoid (Equivalence a) # 
Instance details

Defined in Data.Functor.Contravariant

defaultEquivalence :: Eq a => Equivalence a #

Check for equivalence with ==.

Note: The instances for Double and Float violate reflexivity for NaN.

Dual arrows

newtype Op a b #

Dual function arrows.

Constructors

Op 

Fields

Instances

Instances details
Contravariant (Op a) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

contramap :: (a0 -> b) -> Op a b -> Op a a0 #

(>$) :: b -> Op a b -> Op a a0 #

Category Op # 
Instance details

Defined in Data.Functor.Contravariant

Methods

id :: forall (a :: k). Op a a #

(.) :: forall (b :: k) (c :: k) (a :: k). Op b c -> Op a b -> Op a c #

Floating a => Floating (Op a b) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

pi :: Op a b #

exp :: Op a b -> Op a b #

log :: Op a b -> Op a b #

sqrt :: Op a b -> Op a b #

(**) :: Op a b -> Op a b -> Op a b #

logBase :: Op a b -> Op a b -> Op a b #

sin :: Op a b -> Op a b #

cos :: Op a b -> Op a b #

tan :: Op a b -> Op a b #

asin :: Op a b -> Op a b #

acos :: Op a b -> Op a b #

atan :: Op a b -> Op a b #

sinh :: Op a b -> Op a b #

cosh :: Op a b -> Op a b #

tanh :: Op a b -> Op a b #

asinh :: Op a b -> Op a b #

acosh :: Op a b -> Op a b #

atanh :: Op a b -> Op a b #

log1p :: Op a b -> Op a b #

expm1 :: Op a b -> Op a b #

log1pexp :: Op a b -> Op a b #

log1mexp :: Op a b -> Op a b #

Fractional a => Fractional (Op a b) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

(/) :: Op a b -> Op a b -> Op a b #

recip :: Op a b -> Op a b #

fromRational :: Rational -> Op a b #

Num a => Num (Op a b) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

(+) :: Op a b -> Op a b -> Op a b #

(-) :: Op a b -> Op a b -> Op a b #

(*) :: Op a b -> Op a b -> Op a b #

negate :: Op a b -> Op a b #

abs :: Op a b -> Op a b #

signum :: Op a b -> Op a b #

fromInteger :: Integer -> Op a b #

Semigroup a => Semigroup (Op a b) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

(<>) :: Op a b -> Op a b -> Op a b #

sconcat :: NonEmpty (Op a b) -> Op a b #

stimes :: Integral b0 => b0 -> Op a b -> Op a b #

Monoid a => Monoid (Op a b) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

mempty :: Op a b #

mappend :: Op a b -> Op a b -> Op a b #

mconcat :: [Op a b] -> Op a b #