module Cat.Diagram.Monad where
Monads🔗
A monad on a category
is one way of categorifying the concept of monoid. Specifically, rather than living
in a monoidal category, a monad lives in a bicategory. Here, we concern
ourselves with the case of monads in the bicategory of categories, so
that we may say: A monad is an endofunctor
equipped with a unit
natural
transformation
and a multiplication
record Monad : Type (o ⊔ h) where
no-eta-equality
field
: Functor C C
M : Id => M
unit : (M F∘ M) => M mult
Furthermore, these natural transformations must satisfy identity and associativity laws exactly analogous to those of a monoid.
field
: ∀ {x} → μ x C.∘ M₁ (η x) ≡ C.id
left-ident : ∀ {x} → μ x C.∘ η (M₀ x) ≡ C.id
right-ident : ∀ {x} → μ x C.∘ M₁ (μ x) ≡ μ x C.∘ μ (M₀ x) mult-assoc
Algebras over a monad🔗
One way of interpreting a monad is as giving a signature for an algebraic theory. For instance, the free monoid monad describes the signature for the theory of monoids, and the free group monad describes the theory of groups.
Under this light, an algebra over a monad is a way of evaluating the abstract operations given by a monadic expression to a concrete value. Formally, an algebra for is given by a choice of object and a morphism
record Algebra-on (M : Monad) (ob : C.Ob) : Type (o ⊔ h) where
no-eta-equality
open Monad M
field
: C.Hom (M₀ ob) ob ν
This morphism must satisfy equations categorifying those which define
a monoid action. If we think of
as specifying a signature of effects, then v-unit
says that the unit
has no effects, and v-mult
says that, given two layers
it doesn’t matter whether you first join then evaluate, or evaluate
twice.
: ν C.∘ η ob ≡ C.id
ν-unit : ν C.∘ M₁ ν ≡ ν C.∘ μ ob ν-mult
Eilenberg-Moore category🔗
If we take a monad
as the signature of an (algebraic) theory, and
as giving models of that theory, then we can ask (like with
everything in category theory): Are there maps between interpretations?
The answer (as always!) is yes: An algebra homomorphism
is a map of the
underlying objects which “commutes with the algebras”.
We can be more specific about “commuting with the algebras” by drawing a square: A map in the ambient category is a homomorphism of when the square below commutes.
We can assemble and their homomorphisms into a displayed category over the type of objects over some consists of all possible algebra structures on and the type of morphisms over are proofs that is an homomorphism.
: Displayed C (o ⊔ ℓ) ℓ
Monad-algebras .Ob[_] X = Algebra-on C M X
Monad-algebras .Hom[_] f α β = f C.∘ α .ν ≡ β .ν C.∘ M₁ f
Monad-algebras .Hom[_]-set _ _ _ = hlevel 2 Monad-algebras
Defining the identity and composition maps is mostly an exercise in categorical yoga:
.id' {X} {α} =
Monad-algebras .id C.∘ α .ν ≡⟨ C.idl _ ∙ C.intror M-id ⟩
C.ν C.∘ M₁ C.id ∎
α ._∘'_ {_} {_} {_} {α} {β} {γ} {f = f} {g = g} p q =
Monad-algebras (f C.∘ g) C.∘ α .ν ≡⟨ C.pullr q ⟩
.∘ β .ν C.∘ M₁ g ≡⟨ C.pulll p ⟩
f C(γ .ν C.∘ M₁ f) C.∘ M₁ g ≡⟨ C.pullr (sym (M-∘ _ _)) ⟩
.ν C.∘ M₁ (f C.∘ g) ∎ γ
The equations all hold trivially, as the type of displayed morphisms over is a proposition.
.idr' _ = prop!
Monad-algebras .idl' _ = prop!
Monad-algebras .assoc' _ _ _ = prop! Monad-algebras
The total category of this displayed category is referred to as the Eilenberg Moore category of
: Precategory (o ⊔ ℓ) ℓ
Eilenberg-Moore = ∫ Monad-algebras
Eilenberg-Moore
private
module EM = Cat.Reasoning Eilenberg-Moore
: Type _
Algebra = EM.Ob
Algebra
: (X Y : Algebra) → Type _
Algebra-hom = EM.Hom X Y Algebra-hom X Y
By projecting the underlying object of the algebras, and the
underlying morphisms of the homomorphisms between them, we can define a
functor from Eilenberg-Moore
back
to the underlying category:
: Functor (Eilenberg-Moore M) C
Forget-EM = πᶠ (Monad-algebras M) Forget-EM
This functor is faithful as the maps in the Eilenberg-Moore category are structured maps of
: is-faithful Forget-EM
Forget-EM-is-faithful = ext Forget-EM-is-faithful
Moreover, this functor is conservative. This follows from a bit of routine algebra.
: is-conservative Forget-EM
Forget-EM-is-conservative {X , α} {Y , β} {f = f} f-inv =
Forget-EM-is-conservative .make-invertible f-alg-inv (ext invl) (ext invr)
EMwhere
open C.is-invertible f-inv
: Algebra-hom M (Y , β) (X , α)
f-alg-inv .hom = inv
f-alg-inv .preserves =
f-alg-inv .∘ β .ν ≡⟨ ap₂ C._∘_ refl (C.intror (MR.annihilate invl)) ⟩
inv C.∘ β .ν C.∘ M₁ (f .hom) C.∘ M.M₁ inv ≡⟨ ap₂ C._∘_ refl (C.extendl (sym (f .preserves))) ⟩
inv C.∘ f .hom C.∘ α .ν C.∘ M.M₁ inv ≡⟨ C.cancell invr ⟩
inv C.ν C.∘ M₁ inv ∎ α
Univalence🔗
The displayed category of monad algebras is a displayed univalent category. This is relatively straightforward to show: first, note that the type of displayed isomorphisms must be a proposition. Next, we can perform a bit of simple algebra to show that the actions of two isomorphic are, in fact, equal.
: is-category-displayed (Monad-algebras M)
Monad-algebras-is-category (β , p) (γ , q) =
Monad-algebras-is-category f α (λ _ _ _ → ext prop!) $ ext $
Σ-prop-path .ν ≡⟨ C.introl invl ⟩
β (to C.∘ from) C.∘ β .ν ≡⟨ C.pullr (p .from') ⟩
to C.∘ α .ν C.∘ M₁ from ≡⟨ C.pulll (q .to') ⟩
(γ .ν C.∘ M₁ to) C.∘ M₁ from ≡⟨ MR.cancelr invl ⟩
.ν ∎
γ where
open C._≅_ f
open Cat.Displayed.Morphism (Monad-algebras M)
By univalence of total categories, we can immediately deduce that the Eilenberg-Moore category inherits univalence from the base category.
: is-category C → is-category (Eilenberg-Moore M)
EM-is-category =
EM-is-category cat (Monad-algebras M) cat Monad-algebras-is-category is-category-total
Free algebras🔗
In exactly the same way that we may construct a free group by taking the inhabitants of some set as generating the “words” of a group, we can, given an object of the underlying category, build a free on Keeping with our interpretation of monads as logical signatures, this is the syntactic model of with a set of “neutrals” chosen from the object
This construction is a lot simpler to do in generality than in any specific case: We can always turn into an by taking the underlying object to be and the algebra map to be the monadic multiplication; The associativity and unit laws of the monad itself become those of the
: Functor C (Eilenberg-Moore M)
Free-EM .F₀ A .fst = M₀ A
Free-EM .F₀ A .snd .ν = μ A
Free-EM .F₀ A .snd .ν-mult = mult-assoc
Free-EM .F₀ A .snd .ν-unit = right-ident Free-EM
The construction of free is furthermore functorial on the underlying objects; Since the monadic multiplication is a natural transformation the naturality condition (drawn below) doubles as showing that the functorial action of can be taken as an algebraic action:
.F₁ f .hom = M₁ f
Free-EM .F₁ f .preserves = sym $ mult.is-natural _ _ _
Free-EM .F-id = ext M-id
Free-EM .F-∘ f g = ext (M-∘ f g) Free-EM
This is a free construction in the precise sense of the word: it’s
the left adjoint to the functor
Forget-EM
, so in particular it
provides a systematic, universal way of
mapping from
to
open _⊣_
: Free-EM ⊣ Forget-EM
Free-EM⊣Forget-EM .unit =
Free-EM⊣Forget-EM .η M.unit.is-natural
NT M.counit =
Free-EM⊣Forget-EM (λ x → total-hom (x .snd .ν) (sym (x .snd .ν-mult)))
NT (λ x y f → ext (sym (f .preserves)))
.zig = ext left-ident
Free-EM⊣Forget-EM .zag {x} = x .snd .ν-unit Free-EM⊣Forget-EM
The full subcategory of free is often referred to as the Kleisli category of
: Precategory (o ⊔ ℓ) ℓ
Kleisli = Essential-image (Free-EM {M = M}) Kleisli
If is univalent then so is the Kleisli category as it is a full subcategory of a univalent category.
: is-category C → is-category (Kleisli M)
Kleisli-is-category = Essential-image-is-category Free-EM
Kleisli-is-category cat (EM-is-category cat)
As the Kleisli category is a full subcategory, there is a canonical full inclusion into the Eilenberg-Moore category.
: Functor (Kleisli M) (Eilenberg-Moore M)
Kleisli→EM = Forget-full-subcat
Kleisli→EM
: is-fully-faithful Kleisli→EM
Kleisli→EM-is-ff = id-equiv Kleisli→EM-is-ff
Additionally, the free/forgetful adjunction between and the Eilenberg-Moore category can be restricted to the Kleisli category.
: Functor (Kleisli M) C
Forget-Kleisli = Forget-EM F∘ Kleisli→EM
Forget-Kleisli
: Functor C (Kleisli M)
Free-Kleisli = Essential-inc Free-EM
Free-Kleisli
: Free-Kleisli ⊣ Forget-Kleisli
Free-Kleisli⊣Forget-Kleisli ._⊣_.unit ._=>_.η = η
Free-Kleisli⊣Forget-Kleisli ._⊣_.unit .is-natural = unit.is-natural
Free-Kleisli⊣Forget-Kleisli ._⊣_.counit ._=>_.η ((X , α) , free) =
Free-Kleisli⊣Forget-Kleisli (α .ν) (sym (α .ν-mult))
total-hom ._⊣_.counit .is-natural _ _ f =
Free-Kleisli⊣Forget-Kleisli (sym (f .preserves))
ext ._⊣_.zig =
Free-Kleisli⊣Forget-Kleisli
ext left-ident._⊣_.zag {(X , α) , free} =
Free-Kleisli⊣Forget-Kleisli . ν-unit α
Note that the forgetful functor from the Kleisli category of to is also faithful and conservative.
: is-faithful Forget-Kleisli
Forget-Kleisli-is-faithful = Forget-EM-is-faithful
Forget-Kleisli-is-faithful
: is-conservative Forget-Kleisli
Forget-Kleisli-is-conservative =
Forget-Kleisli-is-conservative f-inv _ $
super-inv→sub-inv Forget-EM-is-conservative f-inv