1 | include "basics/list.ma". |
---|
2 | include "basics/types.ma". |
---|
3 | include "arithmetics/nat.ma". |
---|
4 | |
---|
5 | lemma eq_rect_Type0_r : |
---|
6 | ∀A: Type[0]. |
---|
7 | ∀a:A. |
---|
8 | ∀P: ∀x:A. eq ? x a → Type[0]. P a (refl A a) → ∀x: A.∀p:eq ? x a. P x p. |
---|
9 | #A #a #P #H #x #p |
---|
10 | generalize in match H |
---|
11 | generalize in match P |
---|
12 | cases p |
---|
13 | // |
---|
14 | qed. |
---|
15 | |
---|
16 | let rec safe_nth (A: Type[0]) (n: nat) (l: list A) (p: n < length A l) on n: A ≝ |
---|
17 | match n return λo. o < length A l → A with |
---|
18 | [ O ⇒ |
---|
19 | match l return λm. 0 < length A m → A with |
---|
20 | [ nil ⇒ λabsd1. ? |
---|
21 | | cons hd tl ⇒ λprf1. hd |
---|
22 | ] |
---|
23 | | S n' ⇒ |
---|
24 | match l return λm. S n' < length A m → A with |
---|
25 | [ nil ⇒ λabsd2. ? |
---|
26 | | cons hd tl ⇒ λprf2. safe_nth A n' tl ? |
---|
27 | ] |
---|
28 | ] ?. |
---|
29 | [ 1: |
---|
30 | @ p |
---|
31 | | 4: |
---|
32 | normalize in prf2 |
---|
33 | normalize |
---|
34 | @ le_S_S_to_le |
---|
35 | assumption |
---|
36 | | 2: |
---|
37 | normalize in absd1; |
---|
38 | cases (not_le_Sn_O O) |
---|
39 | # H |
---|
40 | elim (H absd1) |
---|
41 | | 3: |
---|
42 | normalize in absd2; |
---|
43 | cases (not_le_Sn_O (S n')) |
---|
44 | # H |
---|
45 | elim (H absd2) |
---|
46 | ] |
---|
47 | qed. |
---|
48 | |
---|
49 | let rec nub_by_internal (A: Type[0]) (f: A → A → bool) (l: list A) (n: nat) on n ≝ |
---|
50 | match n with |
---|
51 | [ O ⇒ |
---|
52 | match l with |
---|
53 | [ nil ⇒ [ ] |
---|
54 | | cons hd tl ⇒ l |
---|
55 | ] |
---|
56 | | S n ⇒ |
---|
57 | match l with |
---|
58 | [ nil ⇒ [ ] |
---|
59 | | cons hd tl ⇒ |
---|
60 | hd :: nub_by_internal A f (filter ? (λy. notb (f y hd)) tl) n |
---|
61 | ] |
---|
62 | ]. |
---|
63 | |
---|
64 | definition nub_by ≝ |
---|
65 | λA: Type[0]. |
---|
66 | λf: A → A → bool. |
---|
67 | λl: list A. |
---|
68 | nub_by_internal A f l (length ? l). |
---|
69 | |
---|
70 | let rec member (A: Type[0]) (eq: A → A → bool) (a: A) (l: list A) on l ≝ |
---|
71 | match l with |
---|
72 | [ nil ⇒ false |
---|
73 | | cons hd tl ⇒ orb (eq a hd) (member A eq a tl) |
---|
74 | ]. |
---|
75 | |
---|
76 | let rec take (A: Type[0]) (n: nat) (l: list A) on n: list A ≝ |
---|
77 | match n with |
---|
78 | [ O ⇒ [ ] |
---|
79 | | S n ⇒ |
---|
80 | match l with |
---|
81 | [ nil ⇒ [ ] |
---|
82 | | cons hd tl ⇒ hd :: take A n tl |
---|
83 | ] |
---|
84 | ]. |
---|
85 | |
---|
86 | let rec drop (A: Type[0]) (n: nat) (l: list A) on n ≝ |
---|
87 | match n with |
---|
88 | [ O ⇒ l |
---|
89 | | S n ⇒ |
---|
90 | match l with |
---|
91 | [ nil ⇒ [ ] |
---|
92 | | cons hd tl ⇒ drop A n tl |
---|
93 | ] |
---|
94 | ]. |
---|
95 | |
---|
96 | definition list_split ≝ |
---|
97 | λA: Type[0]. |
---|
98 | λn: nat. |
---|
99 | λl: list A. |
---|
100 | 〈take A n l, drop A n l〉. |
---|
101 | |
---|
102 | let rec mapi_internal (A: Type[0]) (B: Type[0]) (n: nat) (f: nat → A → B) |
---|
103 | (l: list A) on l: list B ≝ |
---|
104 | match l with |
---|
105 | [ nil ⇒ nil ? |
---|
106 | | cons hd tl ⇒ (f n hd) :: (mapi_internal A B (n + 1) f tl) |
---|
107 | ]. |
---|
108 | |
---|
109 | definition mapi ≝ |
---|
110 | λA, B: Type[0]. |
---|
111 | λf: nat → A → B. |
---|
112 | λl: list A. |
---|
113 | mapi_internal A B 0 f l. |
---|
114 | |
---|
115 | let rec zip (A: Type[0]) (B: Type[0]) (l: list A) (r: list B) on l: option (list (A × B)) ≝ |
---|
116 | match l with |
---|
117 | [ nil ⇒ Some ? (nil (A × B)) |
---|
118 | | cons hd tl ⇒ |
---|
119 | match r with |
---|
120 | [ nil ⇒ None ? |
---|
121 | | cons hd' tl' ⇒ |
---|
122 | match zip ? ? tl tl' with |
---|
123 | [ None ⇒ None ? |
---|
124 | | Some tail ⇒ Some ? (〈hd, hd'〉 :: tail) |
---|
125 | ] |
---|
126 | ] |
---|
127 | ]. |
---|
128 | |
---|
129 | let rec foldl (A: Type[0]) (B: Type[0]) (f: A → B → A) (a: A) (l: list B) on l ≝ |
---|
130 | match l with |
---|
131 | [ nil ⇒ a |
---|
132 | | cons hd tl ⇒ foldl A B f (f a hd) tl |
---|
133 | ]. |
---|
134 | |
---|
135 | definition flatten ≝ |
---|
136 | λA: Type[0]. |
---|
137 | λl: list (list A). |
---|
138 | foldr ? ? (λx. λy. append ? y x) [ ] l. |
---|
139 | |
---|
140 | let rec rev (A: Type[0]) (l: list A) on l ≝ |
---|
141 | match l with |
---|
142 | [ nil ⇒ nil A |
---|
143 | | cons hd tl ⇒ (rev A tl) @ [ hd ] |
---|
144 | ]. |
---|
145 | |
---|
146 | notation > "'if' term 19 e 'then' term 19 t 'else' term 48 f" non associative with precedence 19 |
---|
147 | for @{ match $e in bool with [ true ⇒ $t | false ⇒ $f] }. |
---|
148 | notation < "hvbox('if' \nbsp term 19 e \nbsp break 'then' \nbsp term 19 t \nbsp break 'else' \nbsp term 48 f \nbsp)" non associative with precedence 19 |
---|
149 | for @{ match $e with [ true ⇒ $t | false ⇒ $f] }. |
---|
150 | |
---|
151 | let rec fold_left_i_aux (A: Type[0]) (B: Type[0]) |
---|
152 | (f: nat → A → B → A) (x: A) (i: nat) (l: list B) on l ≝ |
---|
153 | match l with |
---|
154 | [ nil ⇒ x |
---|
155 | | cons hd tl ⇒ fold_left_i_aux A B f (f i x hd) (S i) tl |
---|
156 | ]. |
---|
157 | |
---|
158 | definition fold_left_i ≝ λA,B,f,x. fold_left_i_aux A B f x O. |
---|
159 | |
---|
160 | notation "hvbox(t⌈o ↦ h⌉)" |
---|
161 | with precedence 45 |
---|
162 | for @{ match (? : $o=$h) with [ refl ⇒ $t ] }. |
---|
163 | |
---|
164 | definition function_apply ≝ |
---|
165 | λA, B: Type[0]. |
---|
166 | λf: A → B. |
---|
167 | λa: A. |
---|
168 | f a. |
---|
169 | |
---|
170 | notation "f break $ x" |
---|
171 | left associative with precedence 99 |
---|
172 | for @{ 'function_apply $f $x }. |
---|
173 | |
---|
174 | interpretation "Function application" 'function_apply f x = (function_apply ? ? f x). |
---|
175 | |
---|
176 | let rec iterate (A: Type[0]) (f: A → A) (a: A) (n: nat) on n ≝ |
---|
177 | match n with |
---|
178 | [ O ⇒ a |
---|
179 | | S o ⇒ f (iterate A f a o) |
---|
180 | ]. |
---|
181 | |
---|
182 | notation > "hvbox('let' 〈ident x,ident y〉 ≝ t 'in' s)" |
---|
183 | with precedence 10 |
---|
184 | for @{ match $t with [ pair ${ident x} ${ident y} ⇒ $s ] }. |
---|
185 | |
---|
186 | (* Yeah, I probably ought to do something more general... *) |
---|
187 | notation "hvbox(\langle term 19 a, break term 19 b, break term 19 c\rangle)" |
---|
188 | with precedence 90 for @{ 'triple $a $b $c}. |
---|
189 | interpretation "Triple construction" 'triple x y z = (pair ? ? (pair ? ? x y) z). |
---|
190 | |
---|
191 | notation > "hvbox('let' 〈ident x,ident y,ident z〉 ≝ t 'in' s)" |
---|
192 | with precedence 10 |
---|
193 | for @{ match $t with [ pair ${fresh xy} ${ident z} ⇒ match ${fresh xy} with [ pair ${ident x} ${ident y} ⇒ $s ] ] }. |
---|
194 | |
---|
195 | notation < "hvbox('let' \nbsp hvbox(〈ident x,ident y〉\nbsp ≝ break t \nbsp 'in' \nbsp) break s)" |
---|
196 | with precedence 10 |
---|
197 | for @{ match $t with [ pair (${ident x}:$ignore) (${ident y}:$ignora) ⇒ $s ] }. |
---|
198 | |
---|
199 | notation "⊥" with precedence 90 |
---|
200 | for @{ match ? in False with [ ] }. |
---|
201 | |
---|
202 | let rec exclusive_disjunction (b: bool) (c: bool) on b ≝ |
---|
203 | match b with |
---|
204 | [ true ⇒ |
---|
205 | match c with |
---|
206 | [ false ⇒ true |
---|
207 | | true ⇒ false |
---|
208 | ] |
---|
209 | | false ⇒ |
---|
210 | match c with |
---|
211 | [ false ⇒ false |
---|
212 | | true ⇒ true |
---|
213 | ] |
---|
214 | ]. |
---|
215 | |
---|
216 | definition ltb ≝ |
---|
217 | λm, n: nat. |
---|
218 | leb (S m) n. |
---|
219 | |
---|
220 | definition geb ≝ |
---|
221 | λm, n: nat. |
---|
222 | ltb n m. |
---|
223 | |
---|
224 | definition gtb ≝ |
---|
225 | λm, n: nat. |
---|
226 | leb n m. |
---|
227 | |
---|
228 | (* dpm: unless I'm being stupid, this isn't defined in the stdlib? *) |
---|
229 | let rec eq_nat (n: nat) (m: nat) on n: bool ≝ |
---|
230 | match n with |
---|
231 | [ O ⇒ match m with [ O ⇒ true | _ ⇒ false ] |
---|
232 | | S n' ⇒ match m with [ S m' ⇒ eq_nat n' m' | _ ⇒ false ] |
---|
233 | ]. |
---|
234 | |
---|
235 | (* dpm: conflicts with library definitions |
---|
236 | interpretation "Nat less than" 'lt m n = (ltb m n). |
---|
237 | interpretation "Nat greater than" 'gt m n = (gtb m n). |
---|
238 | interpretation "Nat greater than eq" 'geq m n = (geb m n). |
---|
239 | *) |
---|
240 | |
---|
241 | let rec division_aux (m: nat) (n : nat) (p: nat) ≝ |
---|
242 | match ltb n (S p) with |
---|
243 | [ true ⇒ O |
---|
244 | | false ⇒ |
---|
245 | match m with |
---|
246 | [ O ⇒ O |
---|
247 | | (S q) ⇒ S (division_aux q (n - (S p)) p) |
---|
248 | ] |
---|
249 | ]. |
---|
250 | |
---|
251 | definition division ≝ |
---|
252 | λm, n: nat. |
---|
253 | match n with |
---|
254 | [ O ⇒ S m |
---|
255 | | S o ⇒ division_aux m m o |
---|
256 | ]. |
---|
257 | |
---|
258 | notation "hvbox(n break ÷ m)" |
---|
259 | right associative with precedence 47 |
---|
260 | for @{ 'division $n $m }. |
---|
261 | |
---|
262 | interpretation "Nat division" 'division n m = (division n m). |
---|
263 | |
---|
264 | let rec modulus_aux (m: nat) (n: nat) (p: nat) ≝ |
---|
265 | match leb n p with |
---|
266 | [ true ⇒ n |
---|
267 | | false ⇒ |
---|
268 | match m with |
---|
269 | [ O ⇒ n |
---|
270 | | S o ⇒ modulus_aux o (n - (S p)) p |
---|
271 | ] |
---|
272 | ]. |
---|
273 | |
---|
274 | definition modulus ≝ |
---|
275 | λm, n: nat. |
---|
276 | match n with |
---|
277 | [ O ⇒ m |
---|
278 | | S o ⇒ modulus_aux m m o |
---|
279 | ]. |
---|
280 | |
---|
281 | notation "hvbox(n break 'mod' m)" |
---|
282 | right associative with precedence 47 |
---|
283 | for @{ 'modulus $n $m }. |
---|
284 | |
---|
285 | interpretation "Nat modulus" 'modulus m n = (modulus m n). |
---|
286 | |
---|
287 | definition divide_with_remainder ≝ |
---|
288 | λm, n: nat. |
---|
289 | pair ? ? (m ÷ n) (modulus m n). |
---|
290 | |
---|
291 | let rec exponential (m: nat) (n: nat) on n ≝ |
---|
292 | match n with |
---|
293 | [ O ⇒ S O |
---|
294 | | S o ⇒ m * exponential m o |
---|
295 | ]. |
---|
296 | |
---|
297 | interpretation "Nat exponential" 'exp n m = (exponential n m). |
---|
298 | |
---|
299 | notation "hvbox(a break ⊎ b)" |
---|
300 | left associative with precedence 50 |
---|
301 | for @{ 'disjoint_union $a $b }. |
---|
302 | interpretation "sum" 'disjoint_union A B = (Sum A B). |
---|
303 | |
---|
304 | theorem less_than_or_equal_monotone: |
---|
305 | ∀m, n: nat. |
---|
306 | m ≤ n → (S m) ≤ (S n). |
---|
307 | #m #n #H |
---|
308 | elim H |
---|
309 | /2/ |
---|
310 | qed. |
---|
311 | |
---|
312 | theorem less_than_or_equal_b_complete: |
---|
313 | ∀m, n: nat. |
---|
314 | leb m n = false → ¬(m ≤ n). |
---|
315 | #m; |
---|
316 | elim m; |
---|
317 | normalize |
---|
318 | [ #n #H |
---|
319 | destruct |
---|
320 | | #y #H1 #z |
---|
321 | cases z |
---|
322 | normalize |
---|
323 | [ #H |
---|
324 | /2/ |
---|
325 | | /3/ |
---|
326 | ] |
---|
327 | ] |
---|
328 | qed. |
---|
329 | |
---|
330 | theorem less_than_or_equal_b_correct: |
---|
331 | ∀m, n: nat. |
---|
332 | leb m n = true → m ≤ n. |
---|
333 | #m |
---|
334 | elim m |
---|
335 | // |
---|
336 | #y #H1 #z |
---|
337 | cases z |
---|
338 | normalize |
---|
339 | [ #H |
---|
340 | destruct |
---|
341 | | #n #H lapply (H1 … H) /2/ |
---|
342 | ] |
---|
343 | qed. |
---|
344 | |
---|
345 | definition less_than_or_equal_b_elim: |
---|
346 | ∀m, n: nat. |
---|
347 | ∀P: bool → Type[0]. |
---|
348 | (m ≤ n → P true) → (¬(m ≤ n) → P false) → P (leb m n). |
---|
349 | #m #n #P #H1 #H2; |
---|
350 | lapply (less_than_or_equal_b_correct m n) |
---|
351 | lapply (less_than_or_equal_b_complete m n) |
---|
352 | cases (leb m n) |
---|
353 | /3/ |
---|
354 | qed. |
---|