Skip to content

Instantly share code, notes, and snippets.

@echatav
Last active April 21, 2024 15:41
Show Gist options
  • Save echatav/25815ea86a4f3f0c7861e289703b9043 to your computer and use it in GitHub Desktop.
Save echatav/25815ea86a4f3f0c7861e289703b9043 to your computer and use it in GitHub Desktop.
alternative and distributive functors and profunctors
Given distributive category
* -C>, >C<, 1C, +C+, 0C
And monoidal category
* -D>, >D<, 1D
A lax alternative functor consists of
* A functor
- F : C -> D
* morphisms
- eps : 1D -D> F(1C)
- mu : F(a) >D< F(b) -D> F(a >C< b)
- eta : 1D -D> F(0C)
- nu : F(a) >D< F(b) -D> F(a +C+ b)
* laws
- (F, eps, mu) is a lax monoidal functor
- (F, eta, nu) is a lax monoidal functor
Given distributive categories
* -C>, >C<, 1C, +C+, 0C
* -D>, >D<, 1D, +D+, 0D
A lax distributive functor consists of
* A functor
- F : C -> D
* morphisms
- eps : 1D -D> F(1C)
- mu : F(a) >D< F(b) -D> F(a >C< b)
- eta : F(0C) -D> 0D
- nu : F(a +C+ b) -D> F(a) +D+ F(b)
* laws
- (F, eps, mu) is a lax monoidal functor
- (F, eta, nu) is an oplax monoidal functor
Given distributive categories
* -C>, >C<, 1C, +C+, 0C
* -D>, >D<, 1D, +D+, 0D
Then
* C^op x D is a distributive category
- (a >C< c, b >D< d)
- (1C, 1D)
- (a +C+ c, b +D+ d)
- (0C, 0D)
* Set is a monoidal category
- Functions ->
- Cartesian product ><
- One point set 1
A lax distributive profunctor consists of
* A profunctor
- P : C^op x D -> Set
* functions
- eps : 1 -> P(1C, 1D)
- mu : P(a,b) >< P(c,d) -> P(a >C< c, b >D< d)
- eta : 1 -> P(0C, 0D)
- nu : P(a,b) >< P(c,d) -> P(a +C+ c, b +D+ d)
* law
- (P, eps, mu, eta, nu) is a lax alternative functor
Proposition:
Given a lax distributive functor F,
both F^* and F_* are lax distributive profunctors.
Example:
In Haskell, the `Alternative` typeclass is equivalent to
a lax alternative functor from ((->), (,), (), Either, Void) to ((->), (,), ()).
```
eps :: Applicative f => () -> f ()
eps _ = pure ()
mu :: Applicative f => (f a, f b) -> f (a,b)
mu (fa, fb) = (,) <$> fa <*> fb
eta :: Alternative f => () -> f Void
eta _ = empty
nu :: Alternative f => (f a, f b) -> f (Either a b)
nu (fa, fb) = Left <$> fa <|> Right <$> fb
```
Examples of lax distributive functors can be found too,
using the `Adjunction` typeclass in Haskell.
```
eta :: Adjunction f u => f Void -> Void
eta = unabsurdL
nu :: Adjunction f u => f (Either a b) -> Either (f a) (f b)
nu = cozipL
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment