Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
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
- contramapped :: Contravariant f => Setter (f b) (f a) a b
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.
Instances
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]