base-4.12.0.0: Basic libraries

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

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  # Equivalence relations are Contravariant, because you can apply the contramapped function to each input to the equivalence relation. Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Equivalence b -> Equivalence a Source #(>$) :: b -> Equivalence b -> Equivalence a Source # # A Comparison is a Contravariant Functor, because contramap can apply its function argument to each input of the comparison function. Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Comparison b -> Comparison a Source #(>$) :: b -> Comparison b -> Comparison a Source # # A Predicate is a Contravariant Functor, because contramap can apply its function argument to the input of the predicate. Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Predicate b -> Predicate a Source #(>$) :: b -> Predicate b -> Predicate a Source # Contravariant (V1 :: Type -> Type) # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> V1 b -> V1 a Source #(>$) :: b -> V1 b -> V1 a Source # Contravariant (U1 :: Type -> Type) # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> U1 b -> U1 a Source #(>$) :: b -> U1 b -> U1 a Source # # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Proxy b -> Proxy a Source #(>$) :: b -> Proxy b -> Proxy a Source # # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a0 -> b) -> Op a b -> Op a a0 Source #(>$) :: b -> Op a b -> Op a a0 Source # # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Rec1 f b -> Rec1 f a Source #(>$) :: b -> Rec1 f b -> Rec1 f a Source # # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Alt f b -> Alt f a Source #(>$) :: b -> Alt f b -> Alt f a Source # Contravariant (Const a :: Type -> Type) # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a0 -> b) -> Const a b -> Const a a0 Source #(>$) :: b -> Const a b -> Const a a0 Source # Contravariant (K1 i c :: Type -> Type) # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> K1 i c b -> K1 i c a Source #(>$) :: b -> K1 i c b -> K1 i c a Source # (Contravariant f, Contravariant g) => Contravariant (f :+: g) # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> (f :+: g) b -> (f :+: g) a Source #(>$) :: b -> (f :+: g) b -> (f :+: g) a Source # (Contravariant f, Contravariant g) => Contravariant (f :*: g) # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> (f :*: g) b -> (f :*: g) a Source #(>$) :: b -> (f :*: g) b -> (f :*: g) a Source # (Contravariant f, Contravariant g) => Contravariant (Sum f g) # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Sum f g b -> Sum f g a Source #(>$) :: b -> Sum f g b -> Sum f g a Source # (Contravariant f, Contravariant g) => Contravariant (Product f g) # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Product f g b -> Product f g a Source #(>$) :: b -> Product f g b -> Product f g a Source # Contravariant f => Contravariant (M1 i c f) # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> M1 i c f b -> M1 i c f a Source #(>$) :: b -> M1 i c f b -> M1 i c f a Source # (Functor f, Contravariant g) => Contravariant (f :.: g) # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> (f :.: g) b -> (f :.: g) a Source #(>$) :: b -> (f :.: g) b -> (f :.: g) a Source # (Functor f, Contravariant g) => Contravariant (Compose f g) # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Compose f g b -> Compose f g a Source #(>$) :: b -> Compose f g b -> Compose f g a 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 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 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 FieldsgetPredicate :: a -> Bool
Instances
 # A Predicate is a Contravariant Functor, because contramap can apply its function argument to the input of the predicate. Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Predicate b -> Predicate a Source #(>$) :: b -> Predicate b -> Predicate a Source # # Instance detailsDefined in Data.Functor.Contravariant Methods(<>) :: Predicate a -> Predicate a -> Predicate a Source #stimes :: Integral b => b -> Predicate a -> Predicate a Source # # Instance detailsDefined in Data.Functor.Contravariant Methodsmappend :: Predicate a -> Predicate a -> Predicate a Source #mconcat :: [Predicate a] -> Predicate a 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 FieldsgetComparison :: a -> a -> Ordering Instances  # A Comparison is a Contravariant Functor, because contramap can apply its function argument to each input of the comparison function. Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Comparison b -> Comparison a Source #(>$) :: b -> Comparison b -> Comparison a Source # # Instance detailsDefined in Data.Functor.Contravariant Methods(<>) :: Comparison a -> Comparison a -> Comparison a Source #stimes :: Integral b => b -> Comparison a -> Comparison a Source # # Instance detailsDefined in Data.Functor.Contravariant Methodsmappend :: Comparison a -> Comparison a -> Comparison a Source #mconcat :: [Comparison a] -> Comparison a Source #

Compare using compare.

# 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 FieldsgetEquivalence :: a -> a -> Bool
Instances