contravariant-1.4: Contravariant functors

Copyright(C) 2007-2015 Edward Kmett
LicenseBSD-style (see the file LICENSE)
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityprovisional
Portabilityportable
Safe HaskellSafe
LanguageHaskell98

Data.Functor.Contravariant

Contents

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.

Synopsis

Contravariant Functors

class Contravariant f where Source

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.

Minimal complete definition

contramap

Methods

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.

Instances

Contravariant V1 Source 
Contravariant U1 Source 
Contravariant SettableStateVar Source 
Contravariant Equivalence Source

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

Contravariant Comparison Source

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

Contravariant Predicate Source

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

Contravariant f => Contravariant (Rec1 f) Source 
Contravariant (Const a) Source 
Contravariant (Proxy *) Source 
Contravariant f => Contravariant (Reverse f) Source 
Contravariant f => Contravariant (Backwards f) Source 
Contravariant m => Contravariant (MaybeT m) Source 
Contravariant m => Contravariant (ListT m) Source 
Contravariant f => Contravariant (IdentityT f) Source 
Contravariant (Constant a) Source 
Contravariant (Op a) Source 
Contravariant (K1 i c) Source 
(Contravariant f, Contravariant g) => Contravariant ((:+:) f g) Source 
(Contravariant f, Contravariant g) => Contravariant ((:*:) f g) Source 
(Functor f, Contravariant g) => Contravariant ((:.:) f g) Source 
Contravariant f => Contravariant (Alt * f) Source 
(Contravariant f, Contravariant g) => Contravariant (Sum f g) Source 
(Contravariant f, Contravariant g) => Contravariant (Product f g) Source 
(Functor f, Contravariant g) => Contravariant (Compose f g) Source 
Contravariant m => Contravariant (WriterT w m) Source 
Contravariant m => Contravariant (WriterT w m) Source 
Contravariant m => Contravariant (ErrorT e m) Source 
Contravariant m => Contravariant (ExceptT e m) Source 
Contravariant m => Contravariant (StateT s m) Source 
Contravariant m => Contravariant (StateT s m) Source 
Contravariant m => Contravariant (ReaderT r m) Source 
(Contravariant f, Functor g) => Contravariant (ComposeCF f g) Source 
(Functor f, Contravariant g) => Contravariant (ComposeFC f g) Source 
Contravariant f => Contravariant (M1 i c f) Source 
Contravariant m => Contravariant (RWST r w s m) Source 
Contravariant m => Contravariant (RWST r w s m) Source 

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 it's 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 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.

Predicates

newtype Predicate a Source

Constructors

Predicate 

Fields

getPredicate :: a -> Bool
 

Instances

Contravariant Predicate Source

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

Decidable Predicate Source 
Divisible Predicate Source 

Comparisons

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.

Constructors

Comparison 

Fields

getComparison :: a -> a -> Ordering
 

Instances

Contravariant Comparison Source

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

Decidable Comparison Source 
Divisible Comparison Source 
Monoid (Comparison a) Source 
Semigroup (Comparison a) Source 

Equivalence Relations

newtype Equivalence a Source

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

getEquivalence :: a -> a -> Bool
 

Instances

Contravariant Equivalence Source

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

Decidable Equivalence Source 
Divisible Equivalence Source 
Monoid (Equivalence a) Source 
Semigroup (Equivalence a) Source 

defaultEquivalence :: Eq a => Equivalence a Source

Check for equivalence with ==

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

Dual arrows

newtype Op a b Source

Dual function arrows.

Constructors

Op 

Fields

getOp :: b -> a
 

Instances

Category * Op Source 
Contravariant (Op a) Source 
Monoid r => Decidable (Op r) Source 
Monoid r => Divisible (Op r) Source 
Floating a => Floating (Op a b) Source 
Fractional a => Fractional (Op a b) Source 
Num a => Num (Op a b) Source 
Monoid a => Monoid (Op a b) Source 
Semigroup a => Semigroup (Op a b) Source