Skip to content

Commit e644597

Browse files
committed
[ week7 ] done: categories as fancy monoids
1 parent f403451 commit e644597

File tree

1 file changed

+69
-9
lines changed

1 file changed

+69
-9
lines changed

lectures/Week7.agda

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module Week7 where
88
-- operation on it, which has a unit, and which satisfies the axiom of
99
-- associativity -- that is, "brackets are not needed".
1010

11-
open import Relation.Binary.PropositionalEquality using (_≡_; refl; cong; sym)
11+
open import Relation.Binary.PropositionalEquality
12+
using (_≡_; refl; cong; sym; module ≡-Reasoning)
1213

1314
-- define monoid as a record
1415
record Monoid : Set₁ where
@@ -68,7 +69,7 @@ module _ where
6869
{ Carrier = X X
6970
; _<>_ = _∘′_
7071
; mempty = id
71-
; <>-assoc = λ _ _ _ refl
72+
; <>-assoc = λ _ _ _ refl -- f ≡ (λ x → f x)
7273
; <>-mempty = λ _ refl
7374
; mempty-<> = λ _ refl
7475
}
@@ -84,17 +85,19 @@ module MonProofs (Mon : Monoid) where
8485
-- puts the operations and axioms in scope
8586
open Monoid Mon renaming (Carrier to M)
8687

87-
8888
-- We can "squish together" all the elements in a list
8989
squish : List M M
9090
squish xs = foldr _<>_ xs mempty
9191

9292
-- And squishing works in any left-right respecting order
9393
-- For instance:
9494

95-
squish-++ : {xs ys} squish (xs ++ ys) ≡ (squish xs <> squish ys)
96-
squish-++ {[]} {ys} = sym (mempty-<> (squish ys))
97-
squish-++ {x ,- xs} {ys} rewrite squish-++ {xs} {ys}
95+
squish-mempty : squish [] ≡ mempty
96+
squish-mempty = refl
97+
98+
squish-++ : xs ys squish (xs ++ ys) ≡ (squish xs <> squish ys)
99+
squish-++ [] ys = sym (mempty-<> (squish ys))
100+
squish-++ (x ,- xs) ys rewrite squish-++ xs ys
98101
= sym (<>-assoc x (squish xs) (squish ys))
99102

100103
-- 1 ,- (2 ,- (3 ,- (4 ,- [])))
@@ -103,17 +106,74 @@ module MonProofs (Mon : Monoid) where
103106
-- 1 ,- (2 ,- (3 ,- (4 ,- [])))
104107
-- ((((mempty <> 1) <> 2) <> 3) <> 4)
105108

109+
squishAcc' : M List M M
110+
squishAcc' acc = foldl acc _<>_
111+
106112
squish' : List M M
107-
squish' = foldl mempty _<>_
113+
squish' = squishAcc' mempty
114+
115+
open ≡-Reasoning
116+
117+
squishAcc'-correct : acc xs acc <> squish xs ≡ squishAcc' acc xs
118+
squishAcc'-correct acc [] = <>-mempty acc
119+
squishAcc'-correct acc (x ,- xs) =
120+
begin
121+
(acc <> squish (x ,- xs)) ≡⟨⟩
122+
(acc <> (x <> squish xs)) ≡⟨ <>-assoc acc x (squish xs) ⟨
123+
((acc <> x) <> squish xs) ≡⟨ squishAcc'-correct (acc <> x) xs ⟩
124+
squishAcc' (acc <> x) xs ≡⟨⟩
125+
squishAcc' acc (x ,- xs)
126+
108127

109128
squish'-correct : xs squish xs ≡ squish' xs
110-
squish'-correct = {!!}
129+
squish'-correct xs = begin
130+
squish xs ≡⟨ mempty-<> (squish xs) ⟨
131+
mempty <> squish xs ≡⟨ squishAcc'-correct mempty xs ⟩
132+
squishAcc' mempty xs ≡⟨⟩
133+
squish' xs ∎
111134

112135

113136
---------------------------------------------------------------------------
114137
-- Homomorphisms
115138
---------------------------------------------------------------------------
116139

117-
118140
-- define Monoid Homomorphisms
119141
-- prove they commute with squishing
142+
143+
144+
record HomoMorphism (S T : Monoid) : Set where
145+
module S = Monoid S
146+
module T = Monoid T
147+
field
148+
function : S.Carrier T.Carrier
149+
mempty-resp : function S.mempty ≡ T.mempty
150+
<>-resp : x y function (x S.<> y) ≡ function x T.<> function y
151+
152+
HomoMorphism-List : (M : Monoid) HomoMorphism (List-Monoid (Monoid.Carrier M)) M
153+
HomoMorphism-List M = record
154+
{ function = MonProofs.squish M
155+
; mempty-resp = MonProofs.squish-mempty M
156+
; <>-resp = MonProofs.squish-++ M
157+
}
158+
159+
---------------------------------------------------------------------------
160+
-- Category
161+
---------------------------------------------------------------------------
162+
163+
-- _≤_
164+
-- source ≤ middle → middle ≤ target → source ≤ target
165+
166+
record Category : Set₁ where
167+
field
168+
-- Type and operations
169+
Object : Set
170+
Morphism : (source target : Object) Set
171+
_then_ : {s m t} Morphism s m Morphism m t Morphism s t
172+
identity : {s} Morphism s s
173+
174+
field
175+
then-assoc : {s m₁ m₂ t} (x : Morphism s m₁) (y : Morphism m₁ m₂) (z : Morphism m₂ t)
176+
((x then y) then z) ≡ (x then (y then z))
177+
178+
then-identity : {s t} (x : Morphism s t) (x then identity) ≡ x
179+
identity-then : {s t} (x : Morphism s t) (identity then x) ≡ x

0 commit comments

Comments
 (0)