Skip to content

Commit e05ccd6

Browse files
committed
feat: 1-category Monad(C)
1 parent 68ba831 commit e05ccd6

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed

src/Cat/Instances/Monads.lagda.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<!--
2+
```agda
3+
open import Cat.Instances.Product
4+
open import Cat.Functor.Compose
5+
open import Cat.Diagram.Monad
6+
open import Cat.Functor.Base
7+
open import Cat.Prelude
8+
9+
import Cat.Reasoning
10+
```
11+
-->
12+
13+
```agda
14+
module Cat.Instances.Monads where
15+
```
16+
17+
<!--
18+
```agda
19+
private variable
20+
o h : Level
21+
open Precategory
22+
open Functor
23+
```
24+
-->
25+
26+
# Categories of Monads {defines="category-of-monads"}
27+
28+
The **category of monads** of a category $\cC$ consists of [[monads]]
29+
on $\cC$ and natural transformations preserving the monadic structure.
30+
In terms of the bicategory of [monads in a bicategory], a morphism
31+
in the 1-categorical version always has the identity functor as the
32+
underlying 1-cell.
33+
34+
[monads in a bicategory]: Cat.Bi.Diagram.Monad.html
35+
36+
<!--
37+
```agda
38+
module _ {C : Precategory o h} where
39+
private
40+
module C = Cat.Reasoning C
41+
42+
Endo : Precategory (o ⊔ h) (o ⊔ h)
43+
Endo = Cat[ C , C ]
44+
module Endo = Cat.Reasoning Endo
45+
46+
Endo-∘-functor : Functor (Endo ×ᶜ Endo) Endo
47+
Endo-∘-functor = F∘-functor
48+
module Endo-∘ = Functor Endo-∘-functor
49+
```
50+
-->
51+
52+
A monad homomorphism is a natural transformation $\nu$ preserving
53+
the unit $\eta$ and the multiplication $\mu$. In other words, the
54+
following two diagrams commute where $-\blacklozenge-$ is the
55+
[horizontal composition of natural transformations]:
56+
57+
[horizontal composition of natural transformations]: Cat.Functor.Compose.html
58+
59+
~~~{.quiver}
60+
\[\begin{tikzcd}
61+
{\id} && {\id} && {MM} && {NN} \\
62+
\\
63+
{M} && {N} && {M} && {N}
64+
\arrow["{\id}", from=1-1, to=1-3]
65+
\arrow["{\nu}"', from=3-1, to=3-3]
66+
\arrow["{\eta_M}"', from=1-1, to=3-1]
67+
\arrow["{\eta_N}", from=1-3, to=3-3]
68+
\arrow["{\nu\blacklozenge\nu}", from=1-5, to=1-7]
69+
\arrow["{\nu}"', from=3-5, to=3-7]
70+
\arrow["{\mu_M}"', from=1-5, to=3-5]
71+
\arrow["{\mu_N}", from=1-7, to=3-7]
72+
\end{tikzcd}\]
73+
~~~
74+
75+
```agda
76+
record Monad-hom (M N : Monad C) : Type (o ⊔ h) where
77+
no-eta-equality
78+
module M = Monad M
79+
module N = Monad N
80+
field
81+
nat : M.M => N.M
82+
open _=>_ nat public
83+
field
84+
pres-unit : nat ∘nt M.unit ≡ N.unit
85+
pres-mult : nat ∘nt M.mult ≡ N.mult ∘nt (nat ◆ nat)
86+
```
87+
88+
<!--
89+
```agda
90+
module _ {M N : Monad C} where
91+
private
92+
module M = Monad M
93+
module N = Monad N
94+
95+
Monad-hom-path
96+
: (ν ξ : Monad-hom M N)
97+
→ ν .Monad-hom.nat ≡ ξ .Monad-hom.nat
98+
→ ν ≡ ξ
99+
Monad-hom-path ν ξ p i .Monad-hom.nat = p i
100+
Monad-hom-path ν ξ p i .Monad-hom.pres-unit =
101+
is-prop→pathp
102+
(λ i → Nat-is-set (p i ∘nt M.unit) N.unit)
103+
(ν .Monad-hom.pres-unit)
104+
(ξ .Monad-hom.pres-unit)
105+
i
106+
Monad-hom-path ν ξ p i .Monad-hom.pres-mult =
107+
is-prop→pathp
108+
(λ i → Nat-is-set (p i ∘nt M.mult) (N.mult ∘nt (p i ◆ p i)))
109+
(ν .Monad-hom.pres-mult)
110+
(ξ .Monad-hom.pres-mult)
111+
i
112+
113+
abstract instance
114+
H-Level-Monad-hom : ∀ {n} → H-Level (Monad-hom M N) (2 + n)
115+
H-Level-Monad-hom = basic-instance 2 $ Iso→is-hlevel 2 eqv (hlevel 2)
116+
where unquoteDecl eqv = declare-record-iso eqv (quote Monad-hom)
117+
118+
instance
119+
Extensional-Monad-hom
120+
: ∀ {ℓ} ⦃ sa : Extensional (M.M => N.M) ℓ ⦄
121+
→ Extensional (Monad-hom M N) ℓ
122+
Extensional-Monad-hom ⦃ sa ⦄ =
123+
injection→extensional!
124+
{f = Monad-hom.nat}
125+
(Monad-hom-path _ _) sa
126+
127+
Funlike-Monad-hom
128+
: Funlike (Monad-hom M N) ⌞ C ⌟ (λ x → C .Hom (M.M # x) (N.M # x))
129+
Funlike-Monad-hom ._#_ = Monad-hom.η
130+
```
131+
-->
132+
133+
We have the identity and composition as expected.
134+
135+
```agda
136+
monad-hom-id : ∀ {M : Monad C} → Monad-hom M M
137+
monad-hom-id {M = _} .Monad-hom.nat = idnt
138+
monad-hom-id {M = _} .Monad-hom.pres-unit = Endo.idl _
139+
monad-hom-id {M = M} .Monad-hom.pres-mult =
140+
let module M = Monad M in
141+
idnt ∘nt M.mult ≡⟨ Endo.idl _ ⟩
142+
M.mult ≡˘⟨ Endo.idr _ ⟩
143+
M.mult ∘nt idnt ≡˘⟨ ap (M.mult ∘nt_) Endo-∘.F-id ⟩
144+
M.mult ∘nt (idnt ◆ idnt) ∎
145+
146+
monad-hom-∘
147+
: ∀ {M N O : Monad C}
148+
→ Monad-hom N O
149+
→ Monad-hom M N
150+
→ Monad-hom M O
151+
monad-hom-∘ {M = M} {N} {O} ν ξ = mh where
152+
module M = Monad M
153+
module N = Monad N
154+
module O = Monad O
155+
module ν = Monad-hom ν
156+
module ξ = Monad-hom ξ
157+
158+
mh : Monad-hom M O
159+
mh .Monad-hom.nat = ν.nat ∘nt ξ.nat
160+
mh .Monad-hom.pres-unit =
161+
(ν.nat ∘nt ξ.nat) ∘nt M.unit ≡˘⟨ Endo.assoc ν.nat ξ.nat M.unit ⟩
162+
ν.nat ∘nt (ξ.nat ∘nt M.unit) ≡⟨ ap (ν.nat ∘nt_) ξ.pres-unit ⟩
163+
ν.nat ∘nt N.unit ≡⟨ ν.pres-unit ⟩
164+
O.unit ∎
165+
mh .Monad-hom.pres-mult =
166+
(ν.nat ∘nt ξ.nat) ∘nt M.mult ≡˘⟨ Endo.assoc ν.nat ξ.nat M.mult ⟩
167+
ν.nat ∘nt (ξ.nat ∘nt M.mult) ≡⟨ ap (ν.nat ∘nt_) ξ.pres-mult ⟩
168+
ν.nat ∘nt (N.mult ∘nt (ξ.nat ◆ ξ.nat)) ≡⟨ Endo.assoc ν.nat N.mult (ξ.nat ◆ ξ.nat) ⟩
169+
(ν.nat ∘nt N.mult) ∘nt (ξ.nat ◆ ξ.nat) ≡⟨ ap (_∘nt (ξ.nat ◆ ξ.nat)) ν.pres-mult ⟩
170+
(O.mult ∘nt (ν.nat ◆ ν.nat)) ∘nt (ξ.nat ◆ ξ.nat) ≡˘⟨ Endo.assoc O.mult (ν.nat ◆ ν.nat) (ξ.nat ◆ ξ.nat) ⟩
171+
O.mult ∘nt ((ν.nat ◆ ν.nat) ∘nt (ξ.nat ◆ ξ.nat)) ≡˘⟨ ap (O.mult ∘nt_) $ Endo-∘.F-∘ (ν.nat , ν.nat) (ξ.nat , ξ.nat) ⟩
172+
O.mult ∘nt ((ν.nat ∘nt ξ.nat) ◆ (ν.nat ∘nt ξ.nat)) ∎
173+
```
174+
175+
Putting these together, we have the 1-category of monads.
176+
177+
```agda
178+
Monads : ∀ (C : Precategory o h) → Precategory (o ⊔ h) (o ⊔ h)
179+
Monads C .Ob = Monad C
180+
Monads C .Hom = Monad-hom
181+
Monads C .Hom-set _ _ = hlevel 2
182+
Monads C .id = monad-hom-id
183+
Monads C ._∘_ = monad-hom-∘
184+
Monads C .idr _ = ext λ _ → C .idr _
185+
Monads C .idl _ = ext λ _ → C .idl _
186+
Monads C .assoc _ _ _ = ext λ _ → C. assoc _ _ _
187+
```

0 commit comments

Comments
 (0)