@@ -8,21 +8,70 @@ 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)
12+
1113-- define monoid as a record
1214record Monoid : Set₁ where
15+ -- Type and operations
16+ field
17+ Carrier : Set
18+ _<>_ : Carrier → Carrier → Carrier
19+ mempty : Carrier
20+
21+ -- and their properties
22+ field
23+ <>-assoc : ∀ x y z → ((x <> y) <> z) ≡ (x <> (y <> z))
24+ <>-mempty : ∀ x → (x <> mempty) ≡ x
25+ mempty-<> : ∀ x → (mempty <> x) ≡ x
1326
27+ -- <>-comm : ∀ x y → (x <> y) ≡ (y <> x)
1428
1529-- Your favourite example of a monoid
1630
1731
32+ data List (X : Set ) : Set where
33+ [] : List X
34+ _,-_ : X -> List X -> List X
1835
36+ _++_ : forall {X} -> List X -> List X -> List X
37+ [] ++ ys = ys
38+ (x ,- xs) ++ ys = x ,- (xs ++ ys)
1939
40+ open import Function.Base using (id; _∘′_)
2041
42+ foldr : ∀ {X Y : Set } → (X → Y → Y) → List X → Y → Y
43+ foldr c [] = id
44+ foldr c (x ,- xs) = c x ∘′ foldr c xs
2145
46+ foldl : ∀ {X Y : Set } → Y → (Y → X → Y) → List X → Y
47+ foldl acc c [] = acc
48+ foldl acc c (x ,- xs) = foldl (c acc x) c xs
2249
50+ module _ where
2351
52+ open Monoid
2453
54+ List-Monoid : Set -> Monoid
55+ List-Monoid X .Carrier = List X
56+ List-Monoid X ._<>_ xs ys = xs ++ ys
57+ List-Monoid X .mempty = []
58+ List-Monoid X .<>-assoc [] ys zs = refl
59+ List-Monoid X .<>-assoc (x ,- xs) ys zs =
60+ cong (x ,-_) (List-Monoid X .<>-assoc xs ys zs)
61+ List-Monoid X .<>-mempty [] = refl
62+ List-Monoid X .<>-mempty (x ,- xs) =
63+ cong (x ,-_) (List-Monoid X .<>-mempty xs)
64+ List-Monoid X .mempty-<> xs = refl
2565
66+ Endo-Monoid : Set → Monoid
67+ Endo-Monoid X = record
68+ { Carrier = X → X
69+ ; _<>_ = _∘′_
70+ ; mempty = id
71+ ; <>-assoc = λ _ _ _ → refl
72+ ; <>-mempty = λ _ → refl
73+ ; mempty-<> = λ _ → refl
74+ }
2675
2776
2877---------------------------------------------------------------------------
@@ -33,31 +82,32 @@ record Monoid : Set₁ where
3382module MonProofs (Mon : Monoid) where
3483
3584 -- puts the operations and axioms in scope
36- -- open Monoid Mon renaming (Carrier to M)
85+ open Monoid Mon renaming (Carrier to M)
3786
3887
3988 -- We can "squish together" all the elements in a list
40- open import Data.List.Base using (List; []; _∷_; _++_)
41- -- squish : List M → M
42- -- squish = {!!}
43-
44-
45-
46-
47-
48-
49-
50-
51-
89+ squish : List M → M
90+ squish xs = foldr _<>_ xs mempty
5291
5392 -- And squishing works in any left-right respecting order
5493 -- For instance:
5594
56- -- squish-++ : ∀ {xs ys} → squish (xs ++ ys) ≡ ?
57- -- squish-++ = ?
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}
98+ = sym (<>-assoc x (squish xs) (squish ys))
99+
100+ -- 1 ,- (2 ,- (3 ,- (4 ,- [])))
101+ -- 1 <> (2 <> (3 <> (4 <> mempty))) -- foldr
58102
103+ -- 1 ,- (2 ,- (3 ,- (4 ,- [])))
104+ -- ((((mempty <> 1) <> 2) <> 3) <> 4)
59105
106+ squish' : List M → M
107+ squish' = foldl mempty _<>_
60108
109+ squish'-correct : ∀ xs → squish xs ≡ squish' xs
110+ squish'-correct = {!!}
61111
62112
63113---------------------------------------------------------------------------
0 commit comments