Comonads¶
A comonad is the dual of a monad: a counit extracts a
value from its context, and a comultiplication duplicates the context so that computations extend
over it (Mac Lane, 1998). The counit laws constrain how extraction and duplication
interact (comonads). The module gives the Comonad interface,
its co-Kleisli category, and CofreeComonad, the product (environment) comonad \(A \times S\).
comonads
¶
Comonads and coKleisli categories on V-enriched FinSet.
A comonad (W, ε, δ) consists of an endofunctor W, a natural counit ε: W ⇒ Id, and a natural comultiplication δ: W ⇒ W² satisfying:
W(δ) ∘ δ = δ_W ∘ δ (coassociativity)
ε_W ∘ δ = id (left counit)
W(ε) ∘ δ = id (right counit)
The coKleisli category of a comonad has the same objects but morphisms A → B are morphisms W(A) → B in the base category, composed via:
f =>> g = g ∘ W(f) ∘ δ_A
The base category here is the category of V-valued matrices, which
is a category when V is a Semiring, so the comonads below require
a semiring algebra.
This module provides the abstract Comonad, the CoKleisliCategory
adapter, and CofreeComonad, the product (environment) comonad
W(A) = A × S.
References
Uustalu, T., and Vene, V. (2008). Comonadic notions of computation. Electronic Notes in Theoretical Computer Science, 203(5), 263-284. doi:10.1016/j.entcs.2008.05.029
Comonad
¶
Bases: ABC
Abstract comonad (W, ε, δ) on V-enriched FinSet.
Subclasses must implement endofunctor, counit, and comultiply. CoKleisli composition is derived.
counit
abstractmethod
¶
comultiply
abstractmethod
¶
cokleisli_compose
¶
CoKleisli composition: f =>> g = g ∘ W(f) ∘ δ_A.
For f: W(A) → B and g: W(B) → C, produces W(A) → C.
| PARAMETER | DESCRIPTION |
|---|---|
f
|
Left coKleisli morphism W(A) → B.
TYPE:
|
g
|
Right coKleisli morphism W(B) → C.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Morphism
|
Composed coKleisli morphism W(A) → C. |
Source code in src/quivers/monadic/comonads.py
CoKleisliCategory
¶
CoKleisliCategory(comonad: Comonad)
The coKleisli category of a comonad.
Objects are the same as the base category. Morphisms A → B in the coKleisli category are morphisms W(A) → B in the base category.
| PARAMETER | DESCRIPTION |
|---|---|
comonad
|
The underlying comonad.
TYPE:
|
Source code in src/quivers/monadic/comonads.py
identity
¶
CofreeComonad
¶
Bases: Comonad
The product (environment) comonad W(A) = A × S.
This is the comonad of values in an environment S
[Uustalu and Vene (2008)], and it is the cofree comonad on the
constant functor at S. It is not the store (costate) comonad,
which is S × (S → A).
ε_A: A × S → A is the first projection (extract the value). δ_A: A × S → (A × S) × S duplicates the environment component. W(f) = f × id_S.
Only the environment is copied, and morphisms act on it as the
identity, so ε and δ are natural for every V-valued morphism,
not only for functions.
| PARAMETER | DESCRIPTION |
|---|---|
store
|
The environment object S.
TYPE:
|
algebra
|
The enrichment algebra; a semiring, so that the base category and the coKleisli category are categories.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
Source code in src/quivers/monadic/comonads.py
counit
¶
ε_A: A × S → A is the first projection.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
The object A.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ObservedMorphism
|
The extraction morphism. |
Source code in src/quivers/monadic/comonads.py
comultiply
¶
δ_A: A × S → (A × S) × S duplicates the store.
Maps (a, s) to ((a, s), s).
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
The object A.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ObservedMorphism
|
The comultiplication morphism. |
Source code in src/quivers/monadic/comonads.py
base_of
¶
Recover A from W(A) = A × S.
| PARAMETER | DESCRIPTION |
|---|---|
wa
|
An object
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SetObject
|
The object |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/monadic/comonads.py
Cited sources¶
Saunders Mac Lane. Categories for the Working Mathematician. Volume 5 of Graduate Texts in Mathematics. Springer, 2 edition, 1998. URL: https://link.springer.com/book/10.1007/978-1-4757-4721-8, doi:10.1007/978-1-4757-4721-8.