Data.Functor.Contravariant

contravariant-1.4: Contravariant functors

Copyright | (C) 2007-2015 Edward Kmett
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | provisional
Portability | portable
Safe Haskell | Safe
Language | Haskell98

Data.Functor.Contravariant

Contents

- Contravariant Functors
- Operators
- Predicates
- Comparisons
- Equivalence Relations
- Dual arrows

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

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

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 #

(>$) :: 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

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

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 getPredicate :: a -> Bool

Instances

Contravariant Predicate # | A `Predicate` is a `Contravariant` `Functor`, because `contramap` can apply its function argument to the input of the predicate.
Methods contramap :: (a -> b) -> Predicate b -> Predicate a # (>$) :: b -> Predicate b -> Predicate a #
Decidable Predicate # |
Methods lose :: (a -> Void) -> Predicate a # choose :: (a -> Either b c) -> Predicate b -> Predicate c -> Predicate a #
Divisible Predicate # |
Methods divide :: (a -> (b, c)) -> Predicate b -> Predicate c -> Predicate a # conquer :: Predicate a #

# 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 getComparison :: a -> a -> Ordering

Instances

Contravariant Comparison # | A `Comparison` is a `Contravariant` `Functor`, because `contramap` can apply its function argument to each input of the comparison function.
Methods contramap :: (a -> b) -> Comparison b -> Comparison a # (>$) :: b -> Comparison b -> Comparison a #
Decidable Comparison # |
Methods lose :: (a -> Void) -> Comparison a # choose :: (a -> Either b c) -> Comparison b -> Comparison c -> Comparison a #
Divisible Comparison # |
Methods divide :: (a -> (b, c)) -> Comparison b -> Comparison c -> Comparison a # conquer :: Comparison a #
Semigroup (Comparison a) # |
Methods (<>) :: Comparison a -> Comparison a -> Comparison a # sconcat :: NonEmpty (Comparison a) -> Comparison a # stimes :: Integral b => b -> Comparison a -> Comparison a #
Monoid (Comparison a) # |
Methods 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 ``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 # | Equivalence relations are `Contravariant`, because you can apply the contramapped function to each input to the equivalence relation.
Methods contramap :: (a -> b) -> Equivalence b -> Equivalence a # (>$) :: b -> Equivalence b -> Equivalence a #
Decidable Equivalence # |
Methods lose :: (a -> Void) -> Equivalence a # choose :: (a -> Either b c) -> Equivalence b -> Equivalence c -> Equivalence a #
Divisible Equivalence # |
Methods divide :: (a -> (b, c)) -> Equivalence b -> Equivalence c -> Equivalence a # conquer :: Equivalence a #
Semigroup (Equivalence a) # |
Methods (<>) :: Equivalence a -> Equivalence a -> Equivalence a # sconcat :: NonEmpty (Equivalence a) -> Equivalence a # stimes :: Integral b => b -> Equivalence a -> Equivalence a #
Monoid (Equivalence a) # |
Methods mempty :: Equivalence a # mappend :: Equivalence a -> Equivalence a -> Equivalence a # mconcat :: [Equivalence a] -> Equivalence a #

defaultEquivalence :: Eq a => Equivalence a #

Check for equivalence with `==`

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

comparisonEquivalence :: Comparison a -> Equivalence a #

# Dual arrows

newtype Op a b #

Dual function arrows.

Constructors

Op |
Fields getOp :: b -> a

Instances

Contravariant (Op a) # |
Methods contramap :: (a -> b) -> Op a b -> Op a a # (>$) :: b -> Op a b -> Op a a #
Monoid r => Decidable (Op r) # |
Methods lose :: (a -> Void) -> Op r a # choose :: (a -> Either b c) -> Op r b -> Op r c -> Op r a #
Monoid r => Divisible (Op r) # |
Methods divide :: (a -> (b, c)) -> Op r b -> Op r c -> Op r a # conquer :: Op r a #
Category * Op # |
Methods id :: cat a a # (.) :: cat b c -> cat a b -> cat a c #
Floating a => Floating (Op a b) # |
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) # |
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) # |
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) # |
Methods (<>) :: Op a b -> Op a b -> Op a b # sconcat :: NonEmpty (Op a b) -> Op a b # stimes :: Integral b => b -> Op a b -> Op a b #
Monoid a => Monoid (Op a b) # |
Methods mempty :: Op a b # mappend :: Op a b -> Op a b -> Op a b # mconcat :: [Op a b] -> Op a b #