contravariant-1.4: Contravariant functors

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.

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

 Source Methodscontramap :: (a -> b) -> V1 b -> V1 a Source(>\$) :: b -> V1 b -> V1 a Source Source Methodscontramap :: (a -> b) -> U1 b -> U1 a Source(>\$) :: b -> U1 b -> U1 a Source Source Methodscontramap :: (a -> b) -> SettableStateVar b -> SettableStateVar a Source(>\$) :: b -> SettableStateVar b -> SettableStateVar a Source Source Equivalence relations are `Contravariant`, because you can apply the contramapped function to each input to the equivalence relation. Methodscontramap :: (a -> b) -> Equivalence b -> Equivalence a Source(>\$) :: b -> Equivalence b -> Equivalence a Source Source A `Comparison` is a `Contravariant` `Functor`, because `contramap` can apply its function argument to each input of the comparison function. Methodscontramap :: (a -> b) -> Comparison b -> Comparison a Source(>\$) :: b -> Comparison b -> Comparison a Source Source A `Predicate` is a `Contravariant` `Functor`, because `contramap` can apply its function argument to the input of the predicate. Methodscontramap :: (a -> b) -> Predicate b -> Predicate a Source(>\$) :: b -> Predicate b -> Predicate a Source Source Methodscontramap :: (a -> b) -> Rec1 f b -> Rec1 f a Source(>\$) :: b -> Rec1 f b -> Rec1 f a Source Source Methodscontramap :: (b -> c) -> Const a c -> Const a b Source(>\$) :: b -> Const a b -> Const a c Source Source Methodscontramap :: (a -> b) -> Proxy * b -> Proxy * a Source(>\$) :: b -> Proxy * b -> Proxy * a Source Source Methodscontramap :: (a -> b) -> Reverse f b -> Reverse f a Source(>\$) :: b -> Reverse f b -> Reverse f a Source Source Methodscontramap :: (a -> b) -> Backwards f b -> Backwards f a Source(>\$) :: b -> Backwards f b -> Backwards f a Source Source Methodscontramap :: (a -> b) -> MaybeT m b -> MaybeT m a Source(>\$) :: b -> MaybeT m b -> MaybeT m a Source Source Methodscontramap :: (a -> b) -> ListT m b -> ListT m a Source(>\$) :: b -> ListT m b -> ListT m a Source Source Methodscontramap :: (a -> b) -> IdentityT f b -> IdentityT f a Source(>\$) :: b -> IdentityT f b -> IdentityT f a Source Source Methodscontramap :: (b -> c) -> Constant a c -> Constant a b Source(>\$) :: b -> Constant a b -> Constant a c Source Source Methodscontramap :: (b -> c) -> Op a c -> Op a b Source(>\$) :: b -> Op a b -> Op a c Source Contravariant (K1 i c) Source 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) Source 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) Source Methodscontramap :: (a -> b) -> (f :*: g) b -> (f :*: g) a Source(>\$) :: b -> (f :*: g) b -> (f :*: g) a Source (Functor f, Contravariant g) => Contravariant ((:.:) f g) Source Methodscontramap :: (a -> b) -> (f :.: g) b -> (f :.: g) a Source(>\$) :: b -> (f :.: g) b -> (f :.: g) a Source Contravariant f => Contravariant (Alt * f) Source Methodscontramap :: (a -> b) -> Alt * f b -> Alt * f a Source(>\$) :: b -> Alt * f b -> Alt * f a Source (Contravariant f, Contravariant g) => Contravariant (Sum f g) Source 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) Source Methodscontramap :: (a -> b) -> Product f g b -> Product f g a Source(>\$) :: b -> Product f g b -> Product f g a Source (Functor f, Contravariant g) => Contravariant (Compose f g) Source Methodscontramap :: (a -> b) -> Compose f g b -> Compose f g a Source(>\$) :: b -> Compose f g b -> Compose f g a Source Contravariant m => Contravariant (WriterT w m) Source Methodscontramap :: (a -> b) -> WriterT w m b -> WriterT w m a Source(>\$) :: b -> WriterT w m b -> WriterT w m a Source Contravariant m => Contravariant (WriterT w m) Source Methodscontramap :: (a -> b) -> WriterT w m b -> WriterT w m a Source(>\$) :: b -> WriterT w m b -> WriterT w m a Source Contravariant m => Contravariant (ErrorT e m) Source Methodscontramap :: (a -> b) -> ErrorT e m b -> ErrorT e m a Source(>\$) :: b -> ErrorT e m b -> ErrorT e m a Source Contravariant m => Contravariant (ExceptT e m) Source Methodscontramap :: (a -> b) -> ExceptT e m b -> ExceptT e m a Source(>\$) :: b -> ExceptT e m b -> ExceptT e m a Source Contravariant m => Contravariant (StateT s m) Source Methodscontramap :: (a -> b) -> StateT s m b -> StateT s m a Source(>\$) :: b -> StateT s m b -> StateT s m a Source Contravariant m => Contravariant (StateT s m) Source Methodscontramap :: (a -> b) -> StateT s m b -> StateT s m a Source(>\$) :: b -> StateT s m b -> StateT s m a Source Contravariant m => Contravariant (ReaderT r m) Source Methodscontramap :: (a -> b) -> ReaderT r m b -> ReaderT r m a Source(>\$) :: b -> ReaderT r m b -> ReaderT r m a Source (Contravariant f, Functor g) => Contravariant (ComposeCF f g) Source Methodscontramap :: (a -> b) -> ComposeCF f g b -> ComposeCF f g a Source(>\$) :: b -> ComposeCF f g b -> ComposeCF f g a Source (Functor f, Contravariant g) => Contravariant (ComposeFC f g) Source Methodscontramap :: (a -> b) -> ComposeFC f g b -> ComposeFC f g a Source(>\$) :: b -> ComposeFC f g b -> ComposeFC f g a Source Contravariant f => Contravariant (M1 i c f) Source 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 Contravariant m => Contravariant (RWST r w s m) Source Methodscontramap :: (a -> b) -> RWST r w s m b -> RWST r w s m a Source(>\$) :: b -> RWST r w s m b -> RWST r w s m a Source Contravariant m => Contravariant (RWST r w s m) Source Methodscontramap :: (a -> b) -> RWST r w s m b -> RWST r w s m a Source(>\$) :: b -> RWST r w s m b -> RWST r w s m 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 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 FieldsgetPredicate :: a -> Bool

Instances

 Source A `Predicate` is a `Contravariant` `Functor`, because `contramap` can apply its function argument to the input of the predicate. Methodscontramap :: (a -> b) -> Predicate b -> Predicate a Source(>\$) :: b -> Predicate b -> Predicate a Source Source Methodslose :: (a -> Void) -> Predicate a Sourcechoose :: (a -> Either b c) -> Predicate b -> Predicate c -> Predicate a Source Source Methodsdivide :: (a -> (b, c)) -> Predicate b -> Predicate c -> 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

 Source A `Comparison` is a `Contravariant` `Functor`, because `contramap` can apply its function argument to each input of the comparison function. Methodscontramap :: (a -> b) -> Comparison b -> Comparison a Source(>\$) :: b -> Comparison b -> Comparison a Source Source Methodslose :: (a -> Void) -> Comparison a Sourcechoose :: (a -> Either b c) -> Comparison b -> Comparison c -> Comparison a Source Source Methodsdivide :: (a -> (b, c)) -> Comparison b -> Comparison c -> Comparison a Source Source Methodsmappend :: Comparison a -> Comparison a -> Comparison amconcat :: [Comparison a] -> Comparison a Source Methods(<>) :: Comparison a -> Comparison a -> Comparison asconcat :: NonEmpty (Comparison a) -> Comparison astimes :: Integral b => b -> Comparison a -> Comparison a

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

 Source Equivalence relations are `Contravariant`, because you can apply the contramapped function to each input to the equivalence relation. Methodscontramap :: (a -> b) -> Equivalence b -> Equivalence a Source(>\$) :: b -> Equivalence b -> Equivalence a Source Source Methodslose :: (a -> Void) -> Equivalence a Sourcechoose :: (a -> Either b c) -> Equivalence b -> Equivalence c -> Equivalence a Source Source Methodsdivide :: (a -> (b, c)) -> Equivalence b -> Equivalence c -> Equivalence a Source Source Methodsmappend :: Equivalence a -> Equivalence a -> Equivalence amconcat :: [Equivalence a] -> Equivalence a Source Methods(<>) :: Equivalence a -> Equivalence a -> Equivalence astimes :: Integral b => b -> Equivalence a -> Equivalence a

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 FieldsgetOp :: b -> a

Instances

 Source Methodsid :: Op a a(.) :: Op b c -> Op a b -> Op a c Source Methodscontramap :: (b -> c) -> Op a c -> Op a b Source(>\$) :: b -> Op a b -> Op a c Source Monoid r => Decidable (Op r) Source Methodslose :: (a -> Void) -> Op r a Sourcechoose :: (a -> Either b c) -> Op r b -> Op r c -> Op r a Source Monoid r => Divisible (Op r) Source Methodsdivide :: (a -> (b, c)) -> Op r b -> Op r c -> Op r a Sourceconquer :: Op r a Source Floating a => Floating (Op a b) Source Methodspi :: Op a bexp :: Op a b -> Op a blog :: Op a b -> Op a bsqrt :: Op a b -> Op a b(**) :: Op a b -> Op a b -> Op a blogBase :: Op a b -> Op a b -> Op a bsin :: Op a b -> Op a bcos :: Op a b -> Op a btan :: Op a b -> Op a basin :: Op a b -> Op a bacos :: Op a b -> Op a batan :: Op a b -> Op a bsinh :: Op a b -> Op a bcosh :: Op a b -> Op a btanh :: Op a b -> Op a basinh :: Op a b -> Op a bacosh :: Op a b -> Op a batanh :: Op a b -> Op a b Fractional a => Fractional (Op a b) Source Methods(/) :: Op a b -> Op a b -> Op a brecip :: Op a b -> Op a bfromRational :: Rational -> Op a b Num a => Num (Op a b) Source 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 bnegate :: Op a b -> Op a babs :: Op a b -> Op a bsignum :: Op a b -> Op a bfromInteger :: Integer -> Op a b Monoid a => Monoid (Op a b) Source Methodsmempty :: Op a bmappend :: Op a b -> Op a b -> Op a bmconcat :: [Op a b] -> Op a b Semigroup a => Semigroup (Op a b) Source Methods(<>) :: Op a b -> Op a b -> Op a bsconcat :: NonEmpty (Op a b) -> Op a bstimes :: Integral c => c -> Op a b -> Op a b