Algebras and Coalgebras¶
Monad algebras, comonad coalgebras, and their structural properties.
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. The comparison functor K: Kl(T) → EM(T) relates the Kleisli and Eilenberg-Moore categories.
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
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
58 59 60 | |
structure_map
abstractmethod
¶
structure_map() -> Morphism
The structure map α: T(A) → A.
| RETURNS | DESCRIPTION |
|---|---|
Morphism
|
The algebra structure map. |
Source code in src/quivers/monadic/algebras.py
72 73 74 75 76 77 78 79 80 81 | |
verify_unit_law
¶
verify_unit_law(atol: float = 1e-05) -> bool
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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
verify_associativity
¶
verify_associativity(atol: float = 1e-05) -> bool
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
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
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). This is the algebra that the comparison functor assigns to each object in the Kleisli category.
| 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
147 148 149 150 | |
ObservedAlgebra
¶
ObservedAlgebra(monad: Monad, carrier: SetObject, structure_tensor: Tensor, quantale: Quantale | None = None)
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:
|
quantale
|
The enrichment algebra. Defaults to PRODUCT_FUZZY.
TYPE:
|
Source code in src/quivers/monadic/algebras.py
177 178 179 180 181 182 183 184 185 186 187 | |
Coalgebra
¶
Coalgebra(comonad: object, carrier: SetObject)
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 (a Comonad instance).
TYPE:
|
carrier
|
The carrier object A.
TYPE:
|
Source code in src/quivers/monadic/algebras.py
208 209 210 | |
structure_map
abstractmethod
¶
structure_map() -> Morphism
The structure map γ: A → W(A).
| RETURNS | DESCRIPTION |
|---|---|
Morphism
|
The coalgebra structure map. |
Source code in src/quivers/monadic/algebras.py
222 223 224 225 226 227 228 229 230 231 | |
verify_counit_law
¶
verify_counit_law(atol: float = 1e-05) -> bool
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
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
verify_coassociativity
¶
verify_coassociativity(atol: float = 1e-05) -> bool
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
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
CofreeCoalgebra
¶
CofreeCoalgebra(comonad: object, obj: SetObject)
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)).
| 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
297 298 299 300 | |
ObservedCoalgebra
¶
ObservedCoalgebra(comonad: object, carrier: SetObject, structure_tensor: Tensor, quantale: Quantale | None = None)
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:
|
quantale
|
The enrichment algebra. Defaults to PRODUCT_FUZZY.
TYPE:
|
Source code in src/quivers/monadic/algebras.py
327 328 329 330 331 332 333 334 335 336 337 | |
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
357 358 | |
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
365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
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
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |