-
Notifications
You must be signed in to change notification settings - Fork 0
/
lecture1-notes.agda
171 lines (126 loc) · 3.64 KB
/
lecture1-notes.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
{-# OPTIONS --without-K --safe #-}
module lecture1-notes where
open import general-notation
data Bool : Type where
true false : Bool
not : Bool → Bool
not true = false
not false = true
not' : Bool → Bool
not' true = false
not' false = true
idBool' : Bool → Bool
idBool' x = x
idBool : Bool → Bool
idBool = λ (x : Bool) → x
-- π type
id' : (X : Type) → X → X
id' X x = x
id : {X : Type} → X → X
id x = x
idBool'' : Bool → Bool
idBool'' = id' _
example : {P Q : Type} → P → (Q → P)
example p = λ q → p
impl-elim : {P Q : Type} → (P → Q) → P → Q
impl-elim h p = h p
data ℕ : Type where
zero : ℕ
suc : ℕ → ℕ
{-# BUILTIN NATURAL ℕ #-}
three : ℕ
three = 3
D : Bool → Type
D true = ℕ
D false = Bool
-- non dependent function
if_then_else1_ : {A : Type} → Bool → A → A → A
if true then x else1 y = x
if false then x else1 y = y
-- whenever I have a function that can be pattern matched
-- I can already type the right arguments to be returned (if simple enough)
if[_]_then_else_ : (X : Bool → Type)
→ (b : Bool)
→ X true
→ X false
→ X b
if[ X ] true then x else y = x
if[ X ] false then x else y = y
-- but i need to provide a Bool → Type
ex : (b : Bool) → D b
ex b = if[ D ] b then 3 else false
data List (A : Type) : Type where
[] : List A --- empty list
_∷_ : A → List A → List A
infixr 10 _∷_
sample-list₀ = 0 ∷ 1 ∷ 2 ∷ []
length : {X : Type} → List X → ℕ
length [] = 0
length (h ∷ t) = suc (length t)
List-elim : {X : Type} (A : List X → Type)
→ A []
→ ((x : X) → (xs : List X) → A xs → A (x ∷ xs))
→ (xs : List X)
→ A xs
List-elim A a₀ f [] = a₀
List-elim A a₀ f (x ∷ xs) = f x xs (List-elim A a₀ f xs)
-- defining false, a type with no elements
data 𝟘 : Type where
-- defining true, a type with one element
data 𝟙 : Type where
⋆ : 𝟙
_≣_ : ℕ → ℕ → Type
zero ≣ zero = 𝟙
zero ≣ suc y = 𝟘
suc x ≣ zero = 𝟘
suc x ≣ suc y = x ≣ y
infix 0 _≣_
ℕ-refl : (x : ℕ) → x ≣ x
ℕ-refl zero = ⋆
ℕ-refl (suc x) = ℕ-refl x
ℕ-elim : {A : ℕ → Type}
→ A 0
→ ((k : ℕ) → A k → A (suc k))
→ (n : ℕ)
→ A n
ℕ-elim {A} a₀ f zero = a₀
ℕ-elim {A} a₀ f (suc n) = f n IH
where
IH : A n
IH = ℕ-elim a₀ f n
-- prove that lists under ++ is a group or a monoid
_++_ : {A : Type} → List A → List A → List A
[] ++ ys = ys
(x ∷ xs) ++ ys = x ∷ (xs ++ ys)
-- prove that ℕ under + is a group or a monoid
_+_ : ℕ → ℕ → ℕ
zero + y = y
suc x + y = suc (x + y)
_*_ : ℕ → ℕ → ℕ
zero * y = zero
suc x * y = y + (x * y)
lh : {X : Type} (xs ys : List X)
→ length (xs ++ ys) ≣ length xs + length ys
lh [] ys = ℕ-refl (length ys)
lh (x ∷ xs) ys = IH
where
IH : length (xs ++ ys) ≣ (length xs + length ys)
IH = lh xs ys
data _≡_ {A : Type} : A → A → Type where
refl : (x : A) → x ≡ x
trans : {A : Type} {x y z : A} → x ≡ y → y ≡ z → x ≡ z
trans (refl x) (refl x) = refl x
sym : {A : Type} {x y : A} → x ≡ y → y ≡ x
sym (refl x) = refl x
ap : {A B : Type} (f : A → B) {x y : A} → x ≡ y → f x ≡ f y
ap f (refl x) = refl (f x)
back : (x y : ℕ) → x ≡ y → x ≣ y
back x x (refl x) = ℕ-refl x
suc_eq :(x y : ℕ) → x ≡ y → suc x ≡ suc y
suc_eq x x (refl x) = refl (suc x)
forth : (x y : ℕ) → x ≣ y → x ≡ y
forth zero zero h = refl zero
forth (suc x) (suc y) h = suc_eq x y IH
where
IH : x ≡ y
IH = forth x y h