Algebras and Coalgebras¶
An algebra of a monad evaluates its structured values into a carrier, compatibly with the monad's unit and multiplication (Mac Lane, 1998). For the list monad, an associative operation with a unit folds a list into one value, and associativity makes the fold agree with flattening nested lists. A coalgebra of a comonad dually equips a value with structured context. The module gives monad algebras and comonad coalgebras with their compatibility equations.
algebras
¶
Algebras and coalgebras for monads and comonads.
An algebra (A, α) for a monad (T, η, μ) consists of an object A and a morphism α: T(A) → A satisfying:
α ∘ η_A = id_A (unit law)
α ∘ μ_A = α ∘ T(α) (associativity)
Dually, a coalgebra (A, γ) for a comonad (W, ε, δ) consists of an object A and a morphism γ: A → W(A) satisfying:
ε_A ∘ γ = id_A (counit law)
δ_A ∘ γ = W(γ) ∘ γ (coassociativity)
The Eilenberg-Moore category of a monad T has T-algebras as objects and algebra homomorphisms as morphisms [Mac Lane (1998), §VI.2].
This module provides:
Algebra (abstract)
├── FreeAlgebra — free T-algebra μ_A: T(T(A)) → T(A)
└── ObservedAlgebra — algebra from a concrete structure map
Coalgebra (abstract)
├── CofreeCoalgebra — cofree W-coalgebra δ_A: W(A) → W(W(A))
└── ObservedCoalgebra — coalgebra from a concrete structure map
EilenbergMooreCategory — the category of algebras for a monad
Monad algebras use the typeclass interface of
quivers.monadic.typeclasses.Monad
(fmap_obj, fmap, pure, join), so they apply to every
monad instance. ObservedAlgebra and ObservedCoalgebra check their
laws when constructed and refuse a structure map that fails them.
References
Mac Lane, S. (1998). Categories for the Working Mathematician (2nd ed.). Springer. doi:10.1007/978-1-4757-4721-8
Algebra
¶
Bases: ABC
Abstract algebra for a monad.
An algebra (A, α) for monad T consists of a carrier object A and a structure map α: T(A) → A.
| PARAMETER | DESCRIPTION |
|---|---|
monad
|
The monad T.
TYPE:
|
carrier
|
The carrier object A.
TYPE:
|
Source code in src/quivers/monadic/algebras.py
structure_map
abstractmethod
¶
structure_map() -> Morphism
The structure map α: T(A) → A.
| RETURNS | DESCRIPTION |
|---|---|
Morphism
|
The algebra structure map. |
verify_unit_law
¶
Verify α ∘ η_A = id_A.
| PARAMETER | DESCRIPTION |
|---|---|
atol
|
Absolute tolerance for comparison.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the unit law holds within tolerance. |
Source code in src/quivers/monadic/algebras.py
verify_associativity
¶
Verify α ∘ μ_A = α ∘ T(α).
| PARAMETER | DESCRIPTION |
|---|---|
atol
|
Absolute tolerance for comparison.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if associativity holds within tolerance. |
Source code in src/quivers/monadic/algebras.py
FreeAlgebra
¶
Bases: Algebra
The free T-algebra on an object A.
The carrier is T(A) and the structure map is μ_A: T(T(A)) → T(A). Its laws are the monad's associativity and left unit law, so it is an algebra whenever the monad is lawful.
| PARAMETER | DESCRIPTION |
|---|---|
monad
|
The monad T.
TYPE:
|
obj
|
The base object A (the carrier will be T(A)).
TYPE:
|
Source code in src/quivers/monadic/algebras.py
ObservedAlgebra
¶
ObservedAlgebra(monad: Monad, carrier: SetObject, structure_tensor: Tensor, algebra: Algebra, atol: float = 1e-05)
Bases: Algebra
An algebra defined by an explicit structure map tensor.
| PARAMETER | DESCRIPTION |
|---|---|
monad
|
The monad T.
TYPE:
|
carrier
|
The carrier object A.
TYPE:
|
structure_tensor
|
The tensor for α: T(A) → A.
TYPE:
|
algebra
|
The enrichment algebra of the structure map, the one the monad's morphisms are over.
TYPE:
|
atol
|
Absolute tolerance of the law checks.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the structure map fails the unit law or associativity. |
Source code in src/quivers/monadic/algebras.py
Coalgebra
¶
Bases: ABC
Abstract coalgebra for a comonad.
A coalgebra (A, γ) for comonad W consists of a carrier object A and a structure map γ: A → W(A).
| PARAMETER | DESCRIPTION |
|---|---|
comonad
|
The comonad W.
TYPE:
|
carrier
|
The carrier object A.
TYPE:
|
Source code in src/quivers/monadic/algebras.py
structure_map
abstractmethod
¶
structure_map() -> Morphism
The structure map γ: A → W(A).
| RETURNS | DESCRIPTION |
|---|---|
Morphism
|
The coalgebra structure map. |
verify_counit_law
¶
Verify ε_A ∘ γ = id_A.
| PARAMETER | DESCRIPTION |
|---|---|
atol
|
Absolute tolerance for comparison.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the counit law holds within tolerance. |
Source code in src/quivers/monadic/algebras.py
verify_coassociativity
¶
Verify δ_A ∘ γ = W(γ) ∘ γ.
| PARAMETER | DESCRIPTION |
|---|---|
atol
|
Absolute tolerance for comparison.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if coassociativity holds within tolerance. |
Source code in src/quivers/monadic/algebras.py
CofreeCoalgebra
¶
Bases: Coalgebra
The cofree W-coalgebra on an object A.
The carrier is W(A) and the structure map is δ_A: W(A) → W(W(A)). Its laws are the comonad's coassociativity and left counit law.
| PARAMETER | DESCRIPTION |
|---|---|
comonad
|
The comonad W.
TYPE:
|
obj
|
The base object A (the carrier will be W(A)).
TYPE:
|
Source code in src/quivers/monadic/algebras.py
ObservedCoalgebra
¶
ObservedCoalgebra(comonad: Comonad, carrier: SetObject, structure_tensor: Tensor, algebra: Algebra, atol: float = 1e-05)
Bases: Coalgebra
A coalgebra defined by an explicit structure map tensor.
| PARAMETER | DESCRIPTION |
|---|---|
comonad
|
The comonad W.
TYPE:
|
carrier
|
The carrier object A.
TYPE:
|
structure_tensor
|
The tensor for γ: A → W(A).
TYPE:
|
algebra
|
The enrichment algebra of the structure map, the one the comonad's morphisms are over.
TYPE:
|
atol
|
Absolute tolerance of the law checks.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the structure map fails the counit law or coassociativity. |
Source code in src/quivers/monadic/algebras.py
EilenbergMooreCategory
¶
EilenbergMooreCategory(monad: Monad)
The Eilenberg-Moore category of a monad.
Objects are T-algebras. Morphisms (A, α) → (B, β) are base morphisms f: A → B satisfying β ∘ T(f) = f ∘ α, i.e., algebra homomorphisms.
| PARAMETER | DESCRIPTION |
|---|---|
monad
|
The underlying monad T.
TYPE:
|
Source code in src/quivers/monadic/algebras.py
free_algebra
¶
free_algebra(obj: SetObject) -> FreeAlgebra
Construct the free T-algebra on an object.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
The base object A.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FreeAlgebra
|
The free algebra with carrier T(A). |
Source code in src/quivers/monadic/algebras.py
is_homomorphism
¶
Check if f: A → B is an algebra homomorphism.
Verifies β ∘ T(f) = f ∘ α.
| PARAMETER | DESCRIPTION |
|---|---|
f
|
A morphism A → B between the carriers.
TYPE:
|
source_alg
|
The source algebra (A, α).
TYPE:
|
target_alg
|
The target algebra (B, β).
TYPE:
|
atol
|
Absolute tolerance for comparison.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if f is a homomorphism within tolerance. |
Source code in src/quivers/monadic/algebras.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.