annotate maybe-monad.agda @ 839:111ee96c09ab

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 02 Apr 2020 09:15:52 +0900
parents 04ffb37df2af
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
660
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 open import Level
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 open import Category
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 open import Category.Sets
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 module maybe-monad {c : Level} where
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 open import HomReasoning
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 open import cat-utility
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 open import Relation.Binary.Core
661
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
9 open import Category.Cat
660
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 import Relation.Binary.PropositionalEquality
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 -- Extensionality a b = {A : Set a} {B : A → Set b} {f g : (x : A) → B x} → (∀ x → f x ≡ g x) → f ≡ g → ( λ x → f x ≡ λ x → g x )
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 postulate extensionality : { c₁ c₂ ℓ : Level} ( A : Category c₁ c₂ ℓ ) → Relation.Binary.PropositionalEquality.Extensionality c₂ c₂
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 data maybe (A : Set c) : Set c where
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 just : A → maybe A
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 nothing : maybe A
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 -- Maybe : A → ⊥ + A
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 A = Sets {c}
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 Maybe : Functor A A
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 Maybe = record {
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 FObj = λ a → maybe a
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 ; FMap = λ {a} {b} f → fmap f
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 ; isFunctor = record {
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 identity = λ {x} → extensionality A ( λ y → identity1 x y )
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 ; distr = λ {a} {b} {c} {f} {g} → extensionality A ( λ w → distr2 a b c f g w )
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 ; ≈-cong = λ {a} {b} {c} {f} f≡g → extensionality A ( λ z → ≈-cong1 a b c f z f≡g )
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 }
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 } where
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 fmap : {a b : Obj A} → (f : Hom A a b ) → Hom A (maybe a) (maybe b)
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 fmap f nothing = nothing
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 fmap f (just x) = just (f x)
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 identity1 : (x : Obj A) → (y : maybe x ) → fmap (id1 A x) y ≡ id1 A (maybe x) y
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 identity1 x nothing = refl
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 identity1 x (just y) = refl
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 distr2 : (x y z : Obj A) (f : Hom A x y ) ( g : Hom A y z ) → (w : maybe x) → fmap (λ w → g (f w)) w ≡ fmap g ( fmap f w)
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 distr2 x y z f g nothing = refl
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 distr2 x y z f g (just w) = refl
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 ≈-cong1 : (x y : Obj A) ( f g : Hom A x y ) → ( z : maybe x ) → f ≡ g → fmap f z ≡ fmap g z
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 ≈-cong1 x y f g nothing refl = refl
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 ≈-cong1 x y f g (just z) refl = refl
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
661
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
46 -- Maybe-η : 1 → M
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
47
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
48 open Functor
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
49 open NTrans
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
50
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
51 Maybe-η : NTrans A A identityFunctor Maybe
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
52 Maybe-η = record {
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
53 TMap = λ a → λ(x : a) → just x ;
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
54 isNTrans = record {
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
55 commute = λ {a b : Obj A} {f : Hom A a b} → comm a b f
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
56 }
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
57 } where
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
58 comm1 : (a b : Obj A) (f : Hom A a b) → (x : a) →
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
59 (A [ FMap Maybe f o just ]) x ≡ (A [ just o FMap (identityFunctor {_} {_} {_} {A}) f ]) x
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
60 comm1 a b f x = refl
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
61 comm : (a b : Obj A) (f : Hom A a b) →
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
62 A [ A [ Functor.FMap Maybe f o (λ x → just x) ] ≈
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
63 A [ (λ x → just x) o FMap (identityFunctor {_} {_} {_} {A} ) f ] ]
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
64 comm a b f = extensionality A ( λ x → comm1 a b f x )
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
65
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
66
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
67
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
68 -- Maybe-μ : MM → M
660
b9358172faf2 add maybe-monad
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69
661
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
70 Maybe-μ : NTrans A A ( Maybe ○ Maybe ) Maybe
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
71 Maybe-μ = record {
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
72 TMap = λ a → tmap a
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
73 ; isNTrans = record {
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
74 commute = comm
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
75 }
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
76 } where
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
77 tmap : (a : Obj A) → Hom A (FObj (Maybe ○ Maybe) a) (FObj Maybe a )
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
78 tmap a nothing = nothing
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
79 tmap a (just nothing ) = nothing
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
80 tmap a (just (just x) ) = just x
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
81 comm1 : (a b : Obj A) (f : Hom A a b) → ( x : maybe ( maybe a) ) →
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
82 ( A [ FMap Maybe f o tmap a ] ) x ≡ ( A [ tmap b o FMap (Maybe ○ Maybe) f ] ) x
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
83 comm1 a b f nothing = refl
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
84 comm1 a b f (just nothing ) = refl
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
85 comm1 a b f (just (just x)) = refl
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
86 comm : {a b : Obj A} {f : Hom A a b} →
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
87 A [ A [ FMap Maybe f o tmap a ] ≈ A [ tmap b o FMap (Maybe ○ Maybe) f ] ]
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
88 comm {a} {b} {f} = extensionality A ( λ x → comm1 a b f x )
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
89
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
90 MaybeMonad : Monad A Maybe Maybe-η Maybe-μ
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
91 MaybeMonad = record {
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
92 isMonad = record {
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
93 unity1 = unity1
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
94 ; unity2 = unity2
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
95 ; assoc = assoc
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
96 }
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
97 } where
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
98 unity1-1 : (a : Obj A ) → (x : maybe a) → (A [ TMap Maybe-μ a o TMap Maybe-η (FObj Maybe a) ]) x ≡ id1 A (FObj Maybe a) x
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
99 unity1-1 a nothing = refl
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
100 unity1-1 a (just x) = refl
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
101 unity2-1 : (a : Obj A ) → (x : maybe a) → (A [ TMap Maybe-μ a o FMap Maybe (TMap Maybe-η a) ]) x ≡ id1 A (FObj Maybe a) x
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
102 unity2-1 a nothing = refl
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
103 unity2-1 a (just x) = refl
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
104 assoc-1 : (a : Obj A ) → (x : maybe (maybe (maybe a))) → (A [ TMap Maybe-μ a o TMap Maybe-μ (FObj Maybe a) ]) x ≡
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
105 (A [ TMap Maybe-μ a o FMap Maybe (TMap Maybe-μ a) ]) x
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
106 assoc-1 a nothing = refl
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
107 assoc-1 a (just nothing) = refl
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
108 assoc-1 a (just (just nothing )) = refl
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
109 assoc-1 a (just (just (just x) )) = refl
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
110 unity1 : {a : Obj A} →
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
111 A [ A [ TMap Maybe-μ a o TMap Maybe-η (FObj Maybe a) ] ≈ id1 A (FObj Maybe a) ]
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
112 unity1 {a} = extensionality A ( λ x → unity1-1 a x )
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
113 unity2 : {a : Obj A} →
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
114 A [ A [ TMap Maybe-μ a o FMap Maybe (TMap Maybe-η a) ] ≈ id1 A (FObj Maybe a) ]
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
115 unity2 {a} = extensionality A ( λ x → unity2-1 a x )
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
116 assoc : {a : Obj A} → A [ A [ TMap Maybe-μ a o TMap Maybe-μ (FObj Maybe a) ] ≈
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
117 A [ TMap Maybe-μ a o FMap Maybe (TMap Maybe-μ a) ] ]
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
118 assoc {a} = extensionality A ( λ x → assoc-1 a x )
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
119
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
120
04ffb37df2af maybe monad done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 660
diff changeset
121