Copyright | (C) 2014-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 |
This module supplies contravariant analogues to the Applicative
and Alternative
classes.
Contravariant Applicative
class Contravariant f => Divisible f where Source
A Divisible
contravariant functor is the contravariant analogue of Applicative
.
In denser jargon, a Divisible
contravariant functor is a monoid object in the category
of presheaves from Hask to Hask, equipped with Day convolution mapping the Cartesian
product of the source to the Cartesian product of the target.
By way of contrast, an Applicative
functor can be viewed as a monoid object in the
category of copresheaves from Hask to Hask, equipped with Day convolution mapping the
Cartesian product of the source to the Cartesian product of the target.
Given the canonical diagonal morphism:
delta a = (a,a)
should be associative with divide
delta
conquer
as a unit
divide
delta
mconquer
= mdivide
delta
conquer
m = mdivide
delta
(divide
delta
m n) o =divide
delta
m (divide
delta
n o)
With more general arguments you'll need to reassociate and project using the monoidal structure of the source category. (Here fst and snd are used in lieu of the more restricted lambda and rho, but this construction works with just a monoidal category.)
divide
f mconquer
=contramap
(fst
. f) mdivide
fconquer
m =contramap
(snd
. f) mdivide
f (divide
g m n) o =divide
f' m (divide
id
n o) where f' a = case f a of (bc,d) -> case g bc of (b,c) -> (a,(b,c))
Contravariant Alternative
class Divisible f => Decidable f where Source
A Divisible
contravariant functor is a monoid object in the category of presheaves
from Hask to Hask, equipped with Day convolution mapping the cartesian product of the
source to the Cartesian product of the target.
choose
Left
m (lose
f) = mchoose
Right
(lose
f) m = mchoose
f (choose
g m n) o =divide
f' m (divide
id
n o) where f' bcd =either
(either
id
(Right
.Left
) . g) (Right
.Right
) . f
In addition, we expect the same kind of distributive law as is satisfied by the usual
covariant Alternative
, w.r.t Applicative
, which should be fully formulated and
added here at some point!