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
: Monad → Type (o ⊔ h)
Algebra = Σ _ (Algebra-on M) Algebra M
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”.
record Algebra-hom (M : Monad) (X Y : Algebra M) : Type (o ⊔ h) where
no-eta-equality
constructor algebra-hom
private
module X = Algebra-on (X .snd)
module Y = Algebra-on (Y .snd)
open Monad M
field
: C.Hom (X .fst) (Y .fst)
morphism : morphism C.∘ X.ν ≡ Y.ν C.∘ M₁ morphism
commutes
open Algebra-hom
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.
Since commutes
is an
identification between morphisms, it inhabits a proposition (because
Hom-sets are sets
), equality of algebra
homomorphisms only depends on an equality of their underlying
morphisms.
Algebra-hom-path: {M : Monad} {X Y : Algebra M} {F G : Algebra-hom M X Y}
→ morphism F ≡ morphism G
→ F ≡ G
.morphism = x i
Algebra-hom-path x i {M = M} {X} {Y} {F} {G} x i .commutes =
Algebra-hom-path (λ i → C.Hom-set _ _ (x i C.∘ X .snd .Algebra-on.ν)
is-prop→pathp (Y .snd .Algebra-on.ν C.∘ Monad.M₁ M (x i)))
(F .commutes) (G .commutes) i
Since the square we drew above commutes for the identity morphism, and we can show that the composite of two algebra homomorphisms is an algebra homomorphism, they assemble into a category: The Eilenberg-Moore category of
module _ (M : Monad C) where
private
module M = Monad M
open M hiding (M)
open Precategory
open Algebra-on
: Precategory _ _
Eilenberg-Moore .Ob = Algebra C M
Eilenberg-Moore .Hom X Y = Algebra-hom C M X Y Eilenberg-Moore
Defining the identity and composition maps is mostly an exercise in categorical yoga:
.id {o , x} .morphism = C.id
Eilenberg-Moore .id {o , x} .commutes =
Eilenberg-Moore .id C.∘ ν x ≡⟨ C.id-comm-sym ⟩
C.∘ C.id ≡⟨ ap (C._∘_ _) (sym M-id) ⟩
ν x C.∘ M₁ C.id ∎
ν x C
._∘_ {_ , x} {_ , y} {_ , z} F G .morphism =
Eilenberg-Moore .∘ morphism G
morphism F C._∘_ {_ , x} {_ , y} {_ , z} F G .commutes =
Eilenberg-Moore (morphism F C.∘ morphism G) C.∘ ν x ≡⟨ C.extendr (commutes G) ⟩
.∘ ν y ⌝ C.∘ M₁ (morphism G) ≡⟨ ap! (commutes F) ⟩
⌜ morphism F C(ν z C.∘ M₁ (morphism F)) C.∘ M₁ (morphism G) ≡⟨ C.pullr (sym (M-∘ _ _)) ⟩
.∘ M₁ (morphism F C.∘ morphism G) ∎ ν z C
Because we have characterised equality of algebra homomorphisms as equality of their underlying maps, the Eilenberg-Moore category inherits the identity and associativity laws from its underlying category.
.idr f = ext (C.idr _)
Eilenberg-Moore .idl f = ext (C.idl _)
Eilenberg-Moore .assoc f g h = ext (C.assoc _ _ _)
Eilenberg-Moore .Hom-set X Y = hlevel 2 Eilenberg-Moore
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 C
Forget .F₀ = fst
Forget .F₁ = Algebra-hom.morphism
Forget .F-id = refl
Forget .F-∘ f g = refl Forget
The lemma Algebra-hom-path
says
exactly that this functor is faithful.
: is-faithful Forget
Forget-is-faithful = ext Forget-is-faithful
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
Free .F₀ A .fst = M₀ A
Free .F₀ A .snd .ν = mult .η A
Free .F₀ A .snd .ν-mult = mult-assoc
Free .F₀ A .snd .ν-unit = right-ident Free
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 .morphism = M₁ f
Free .F₁ f .commutes = sym $ mult.is-natural _ _ _
Free .F-id = ext M-id
Free .F-∘ f g = ext (M-∘ f g) Free
This is a free construction in the precise sense of the word: it’s
the left adjoint to the functor
Forget
, so in particular it
provides a systematic, universal way of
mapping from
to
open _⊣_
: Free ⊣ Forget
Free⊣Forget .unit = NT M.η M.unit.is-natural
Free⊣Forget .counit .η x =
Free⊣Forget record { morphism = x .snd .ν
; commutes = sym (x .snd .ν-mult)
}
.counit .is-natural x y f =
Free⊣Forget (sym (commutes f))
ext .zig = ext left-ident
Free⊣Forget .zag {x} = x .snd .ν-unit Free⊣Forget