planet-mitchell-0.1.0: Planet Mitchell

Contravariant

Contents

Synopsis

# Contravariant

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:

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  Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> SettableStateVar b -> SettableStateVar a #(>$) :: b -> SettableStateVar b -> SettableStateVar a # 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 #(>$) :: b -> Predicate b -> Predicate a # 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 #(>$) :: b -> Comparison b -> Comparison a # 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 #(>$) :: b -> Equivalence b -> Equivalence a # Contravariant (V1 :: * -> *) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> V1 b -> V1 a #(>$) :: b -> V1 b -> V1 a # Contravariant (U1 :: * -> *) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> U1 b -> U1 a #(>$) :: b -> U1 b -> U1 a # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a0 -> b) -> Op a b -> Op a a0 #(>$) :: b -> Op a b -> Op a a0 # Contravariant (Proxy :: * -> *) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Proxy b -> Proxy a #(>$) :: b -> Proxy b -> Proxy a # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> MaybeT m b -> MaybeT m a #(>$) :: b -> MaybeT m b -> MaybeT m a # Instance detailsDefined in Control.Lens.Internal.Indexed Methodscontramap :: (a -> b) -> Indexing f b -> Indexing f a #(>$) :: b -> Indexing f b -> Indexing f a # Instance detailsDefined in Control.Lens.Internal.Indexed Methodscontramap :: (a -> b) -> Indexing64 f b -> Indexing64 f a #(>$) :: b -> Indexing64 f b -> Indexing64 f a # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> ListT m b -> ListT m a #(>$) :: b -> ListT m b -> ListT m a # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Rec1 f b -> Rec1 f a #(>$) :: b -> Rec1 f b -> Rec1 f a # Contravariant (Const a :: * -> *) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a0 -> b) -> Const a b -> Const a a0 #(>$) :: b -> Const a b -> Const a a0 # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Alt f b -> Alt f a #(>$) :: b -> Alt f b -> Alt f a # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> IdentityT f b -> IdentityT f a #(>$) :: b -> IdentityT f b -> IdentityT f a # Contravariant m => Contravariant (ExceptT e m) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> ExceptT e m b -> ExceptT e m a #(>$) :: b -> ExceptT e m b -> ExceptT e m a # Contravariant m => Contravariant (ErrorT e m) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> ErrorT e m b -> ErrorT e m a #(>$) :: b -> ErrorT e m b -> ErrorT e m a # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Backwards f b -> Backwards f a #(>$) :: b -> Backwards f b -> Backwards f a # Contravariant m => Contravariant (WriterT w m) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (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) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (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) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> StateT s m b -> StateT s m a #(>$) :: b -> StateT s m b -> StateT s m a # Contravariant m => Contravariant (WriterT w m) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> WriterT w m b -> WriterT w m a #(>$) :: b -> WriterT w m b -> WriterT w m a # Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> Reverse f b -> Reverse f a #(>$) :: b -> Reverse f b -> Reverse f a # Contravariant (Constant a :: * -> *) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a0 -> b) -> Constant a b -> Constant a a0 #(>$) :: b -> Constant a b -> Constant a a0 # Contravariant (K1 i c :: * -> *) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (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 detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> (f :+: g) b -> (f :+: g) a #(>$) :: b -> (f :+: g) b -> (f :+: g) a # (Contravariant f, Contravariant g) => Contravariant (f :*: g) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> (f :*: g) b -> (f :*: g) a #(>$) :: b -> (f :*: g) b -> (f :*: g) a # (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 #(>$) :: b -> Product f g b -> Product f g a # (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 #(>$) :: b -> Sum f g b -> Sum f g a # Contravariant m => Contravariant (ReaderT r m) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> ReaderT r m b -> ReaderT r m a #(>$) :: b -> ReaderT r m b -> ReaderT r m a # 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 #(>$) :: b -> M1 i c f b -> M1 i c f a # (Functor f, Contravariant g) => Contravariant (f :.: g) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (a -> b) -> (f :.: g) b -> (f :.: g) a #(>$) :: b -> (f :.: g) b -> (f :.: g) a # (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 #(>$) :: b -> Compose f g b -> Compose f g a # Contravariant f => Contravariant (TakingWhile p f a b) Instance detailsDefined in Control.Lens.Internal.Magma Methodscontramap :: (a0 -> b0) -> TakingWhile p f a b b0 -> TakingWhile p f a b a0 #(>$) :: b0 -> TakingWhile p f a b b0 -> TakingWhile p f a b a0 # (Profunctor p, Contravariant g) => Contravariant (BazaarT p g a b) Instance detailsDefined in Control.Lens.Internal.Bazaar Methodscontramap :: (a0 -> b0) -> BazaarT p g a b b0 -> BazaarT p g a b a0 #(>$) :: b0 -> BazaarT p g a b b0 -> BazaarT p g a b a0 # (Profunctor p, Contravariant g) => Contravariant (BazaarT1 p g a b) Instance detailsDefined in Control.Lens.Internal.Bazaar Methodscontramap :: (a0 -> b0) -> BazaarT1 p g a b b0 -> BazaarT1 p g a b a0 #(>$) :: b0 -> BazaarT1 p g a b b0 -> BazaarT1 p g a b a0 # (Profunctor p, Contravariant g) => Contravariant (PretextT p g a b) Instance detailsDefined in Control.Lens.Internal.Context Methodscontramap :: (a0 -> b0) -> PretextT p g a b b0 -> PretextT p g a b a0 #(>$) :: b0 -> PretextT p g a b b0 -> PretextT p g a b a0 # Contravariant m => Contravariant (RWST r w s m) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (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) Instance detailsDefined in Data.Functor.Contravariant Methodscontramap :: (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 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  (>$<) :: 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.

## Optics

contramapped :: Contravariant f => Setter (f b) (f a) a b #

This Setter can be used to map over all of the inputs to a Contravariant.

contramap ≡ over contramapped

>>> getPredicate (over contramapped (*2) (Predicate even)) 5
True

>>> getOp (over contramapped (*5) (Op show)) 100
"500"

>>> Prelude.map ($1)$ over (mapped . _Unwrapping' Op . contramapped) (*12) [(*2),(+1),(^3)]
[24,13,1728]