contravariant-1.4: Contravariant functors

Data.Functor.Contravariant.Divisible

Description

This module supplies contravariant analogues to the `Applicative` and `Alternative` classes.

Synopsis

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)
```

`divide delta` should be associative with `conquer` as a unit

````divide` `delta` m `conquer` = m
`divide` `delta` `conquer` m = m
`divide` `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 m `conquer` = `contramap` (`fst` . f) m
`divide` f `conquer` m = `contramap` (`snd` . f) m
`divide` 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))
```

Methods

divide :: (a -> (b, c)) -> f b -> f c -> f a Source

conquer :: f a Source

The underlying theory would suggest that this should be:

```conquer :: (a -> ()) -> f a
```

However, as we are working over a Cartesian category (Hask) and the Cartesian product, such an input morphism is uniquely determined to be `const mempty`, so we elide it.

Instances

 Source Methodsdivide :: (a -> (b, c)) -> U1 b -> U1 c -> U1 a Source Source Methodsdivide :: (a -> (b, c)) -> Equivalence b -> Equivalence c -> Equivalence a Source Source Methodsdivide :: (a -> (b, c)) -> Comparison b -> Comparison c -> Comparison a Source Source Methodsdivide :: (a -> (b, c)) -> Predicate b -> Predicate c -> Predicate a Source Divisible f => Divisible (Rec1 f) Source Methodsdivide :: (a -> (b, c)) -> Rec1 f b -> Rec1 f c -> Rec1 f a Sourceconquer :: Rec1 f a Source Monoid m => Divisible (Const m) Source Methodsdivide :: (a -> (b, c)) -> Const m b -> Const m c -> Const m a Sourceconquer :: Const m a Source Source Methodsdivide :: (a -> (b, c)) -> Proxy * b -> Proxy * c -> Proxy * a Sourceconquer :: Proxy * a Source Divisible f => Divisible (Reverse f) Source Methodsdivide :: (a -> (b, c)) -> Reverse f b -> Reverse f c -> Reverse f a Sourceconquer :: Reverse f a Source Divisible f => Divisible (Backwards f) Source Methodsdivide :: (a -> (b, c)) -> Backwards f b -> Backwards f c -> Backwards f a Source Divisible m => Divisible (MaybeT m) Source Methodsdivide :: (a -> (b, c)) -> MaybeT m b -> MaybeT m c -> MaybeT m a Sourceconquer :: MaybeT m a Source Divisible m => Divisible (ListT m) Source Methodsdivide :: (a -> (b, c)) -> ListT m b -> ListT m c -> ListT m a Sourceconquer :: ListT m a Source Divisible f => Divisible (IdentityT f) Source Methodsdivide :: (a -> (b, c)) -> IdentityT f b -> IdentityT f c -> IdentityT f a Source Monoid m => Divisible (Constant m) Source Methodsdivide :: (a -> (b, c)) -> Constant m b -> Constant m c -> Constant m 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 (Divisible f, Divisible g) => Divisible ((:*:) f g) Source Methodsdivide :: (a -> (b, c)) -> (f :*: g) b -> (f :*: g) c -> (f :*: g) a Sourceconquer :: (f :*: g) a Source (Applicative f, Divisible g) => Divisible ((:.:) f g) Source Methodsdivide :: (a -> (b, c)) -> (f :.: g) b -> (f :.: g) c -> (f :.: g) a Sourceconquer :: (f :.: g) a Source Divisible f => Divisible (Alt * f) Source Methodsdivide :: (a -> (b, c)) -> Alt * f b -> Alt * f c -> Alt * f a Sourceconquer :: Alt * f a Source (Divisible f, Divisible g) => Divisible (Product f g) Source Methodsdivide :: (a -> (b, c)) -> Product f g b -> Product f g c -> Product f g a Sourceconquer :: Product f g a Source (Applicative f, Divisible g) => Divisible (Compose f g) Source Methodsdivide :: (a -> (b, c)) -> Compose f g b -> Compose f g c -> Compose f g a Sourceconquer :: Compose f g a Source Divisible m => Divisible (WriterT w m) Source Methodsdivide :: (a -> (b, c)) -> WriterT w m b -> WriterT w m c -> WriterT w m a Sourceconquer :: WriterT w m a Source Divisible m => Divisible (WriterT w m) Source Methodsdivide :: (a -> (b, c)) -> WriterT w m b -> WriterT w m c -> WriterT w m a Sourceconquer :: WriterT w m a Source Divisible m => Divisible (ErrorT e m) Source Methodsdivide :: (a -> (b, c)) -> ErrorT e m b -> ErrorT e m c -> ErrorT e m a Sourceconquer :: ErrorT e m a Source Divisible m => Divisible (ExceptT e m) Source Methodsdivide :: (a -> (b, c)) -> ExceptT e m b -> ExceptT e m c -> ExceptT e m a Sourceconquer :: ExceptT e m a Source Divisible m => Divisible (StateT s m) Source Methodsdivide :: (a -> (b, c)) -> StateT s m b -> StateT s m c -> StateT s m a Sourceconquer :: StateT s m a Source Divisible m => Divisible (StateT s m) Source Methodsdivide :: (a -> (b, c)) -> StateT s m b -> StateT s m c -> StateT s m a Sourceconquer :: StateT s m a Source Divisible m => Divisible (ReaderT r m) Source Methodsdivide :: (a -> (b, c)) -> ReaderT r m b -> ReaderT r m c -> ReaderT r m a Sourceconquer :: ReaderT r m a Source (Divisible f, Applicative g) => Divisible (ComposeCF f g) Source Methodsdivide :: (a -> (b, c)) -> ComposeCF f g b -> ComposeCF f g c -> ComposeCF f g a Sourceconquer :: ComposeCF f g a Source (Applicative f, Divisible g) => Divisible (ComposeFC f g) Source Methodsdivide :: (a -> (b, c)) -> ComposeFC f g b -> ComposeFC f g c -> ComposeFC f g a Sourceconquer :: ComposeFC f g a Source Divisible f => Divisible (M1 i c f) Source Methodsdivide :: (a -> (b, d)) -> M1 i c f b -> M1 i c f d -> M1 i c f a Sourceconquer :: M1 i c f a Source Divisible m => Divisible (RWST r w s m) Source Methodsdivide :: (a -> (b, c)) -> RWST r w s m b -> RWST r w s m c -> RWST r w s m a Sourceconquer :: RWST r w s m a Source Divisible m => Divisible (RWST r w s m) Source Methodsdivide :: (a -> (b, c)) -> RWST r w s m b -> RWST r w s m c -> RWST r w s m a Sourceconquer :: RWST r w s m a Source

divided :: Divisible f => f a -> f b -> f (a, b) Source

````divided` = `divide` `id`
```

conquered :: Divisible f => f () Source

Redundant, but provided for symmetry.

````conquered` = `conquer`
```

liftD :: Divisible f => (a -> b) -> f b -> f a Source

This is the divisible analogue of `liftA`. It gives a viable default definition for `contramap` in terms of the members of `Divisible`.

````liftD` f = `divide` ((,) () . f) `conquer`
```

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)  = m
`choose` `Right` (`lose` f) m = m
`choose` 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!

Methods

lose :: (a -> Void) -> f a Source

The only way to win is not to play.

choose :: (a -> Either b c) -> f b -> f c -> f a Source

Instances

 Source Methodslose :: (a -> Void) -> U1 a Sourcechoose :: (a -> Either b c) -> U1 b -> U1 c -> U1 a Source Source Methodslose :: (a -> Void) -> Equivalence a Sourcechoose :: (a -> Either b c) -> Equivalence b -> Equivalence c -> Equivalence a Source Source Methodslose :: (a -> Void) -> Comparison a Sourcechoose :: (a -> Either b c) -> Comparison b -> Comparison c -> Comparison a Source Source Methodslose :: (a -> Void) -> Predicate a Sourcechoose :: (a -> Either b c) -> Predicate b -> Predicate c -> Predicate a Source Decidable f => Decidable (Rec1 f) Source Methodslose :: (a -> Void) -> Rec1 f a Sourcechoose :: (a -> Either b c) -> Rec1 f b -> Rec1 f c -> Rec1 f a Source Source Methodslose :: (a -> Void) -> Proxy * a Sourcechoose :: (a -> Either b c) -> Proxy * b -> Proxy * c -> Proxy * a Source Decidable f => Decidable (Reverse f) Source Methodslose :: (a -> Void) -> Reverse f a Sourcechoose :: (a -> Either b c) -> Reverse f b -> Reverse f c -> Reverse f a Source Decidable f => Decidable (Backwards f) Source Methodslose :: (a -> Void) -> Backwards f a Sourcechoose :: (a -> Either b c) -> Backwards f b -> Backwards f c -> Backwards f a Source Divisible m => Decidable (MaybeT m) Source Methodslose :: (a -> Void) -> MaybeT m a Sourcechoose :: (a -> Either b c) -> MaybeT m b -> MaybeT m c -> MaybeT m a Source Divisible m => Decidable (ListT m) Source Methodslose :: (a -> Void) -> ListT m a Sourcechoose :: (a -> Either b c) -> ListT m b -> ListT m c -> ListT m a Source Decidable f => Decidable (IdentityT f) Source Methodslose :: (a -> Void) -> IdentityT f a Sourcechoose :: (a -> Either b c) -> IdentityT f b -> IdentityT f c -> IdentityT f a 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 (Decidable f, Decidable g) => Decidable ((:*:) f g) Source Methodslose :: (a -> Void) -> (f :*: g) a Sourcechoose :: (a -> Either b c) -> (f :*: g) b -> (f :*: g) c -> (f :*: g) a Source (Applicative f, Decidable g) => Decidable ((:.:) f g) Source Methodslose :: (a -> Void) -> (f :.: g) a Sourcechoose :: (a -> Either b c) -> (f :.: g) b -> (f :.: g) c -> (f :.: g) a Source Decidable f => Decidable (Alt * f) Source Methodslose :: (a -> Void) -> Alt * f a Sourcechoose :: (a -> Either b c) -> Alt * f b -> Alt * f c -> Alt * f a Source (Decidable f, Decidable g) => Decidable (Product f g) Source Methodslose :: (a -> Void) -> Product f g a Sourcechoose :: (a -> Either b c) -> Product f g b -> Product f g c -> Product f g a Source (Applicative f, Decidable g) => Decidable (Compose f g) Source Methodslose :: (a -> Void) -> Compose f g a Sourcechoose :: (a -> Either b c) -> Compose f g b -> Compose f g c -> Compose f g a Source Decidable m => Decidable (WriterT w m) Source Methodslose :: (a -> Void) -> WriterT w m a Sourcechoose :: (a -> Either b c) -> WriterT w m b -> WriterT w m c -> WriterT w m a Source Decidable m => Decidable (WriterT w m) Source Methodslose :: (a -> Void) -> WriterT w m a Sourcechoose :: (a -> Either b c) -> WriterT w m b -> WriterT w m c -> WriterT w m a Source Decidable m => Decidable (StateT s m) Source Methodslose :: (a -> Void) -> StateT s m a Sourcechoose :: (a -> Either b c) -> StateT s m b -> StateT s m c -> StateT s m a Source Decidable m => Decidable (StateT s m) Source Methodslose :: (a -> Void) -> StateT s m a Sourcechoose :: (a -> Either b c) -> StateT s m b -> StateT s m c -> StateT s m a Source Decidable m => Decidable (ReaderT r m) Source Methodslose :: (a -> Void) -> ReaderT r m a Sourcechoose :: (a -> Either b c) -> ReaderT r m b -> ReaderT r m c -> ReaderT r m a Source (Applicative f, Decidable g) => Decidable (ComposeFC f g) Source Methodslose :: (a -> Void) -> ComposeFC f g a Sourcechoose :: (a -> Either b c) -> ComposeFC f g b -> ComposeFC f g c -> ComposeFC f g a Source Decidable f => Decidable (M1 i c f) Source Methodslose :: (a -> Void) -> M1 i c f a Sourcechoose :: (a -> Either b d) -> M1 i c f b -> M1 i c f d -> M1 i c f a Source Decidable m => Decidable (RWST r w s m) Source Methodslose :: (a -> Void) -> RWST r w s m a Sourcechoose :: (a -> Either b c) -> RWST r w s m b -> RWST r w s m c -> RWST r w s m a Source Decidable m => Decidable (RWST r w s m) Source Methodslose :: (a -> Void) -> RWST r w s m a Sourcechoose :: (a -> Either b c) -> RWST r w s m b -> RWST r w s m c -> RWST r w s m a Source

chosen :: Decidable f => f b -> f c -> f (Either b c) Source

````chosen` = `choose` `id`
```

lost :: Decidable f => f Void Source

````lost` = `lose` `id`
```