1 | include "basics/list.ma". |
---|
2 | include "basics/types.ma". |
---|
3 | include "arithmetics/nat.ma". |
---|
4 | |
---|
5 | include "ASM/JMCoercions.ma". |
---|
6 | |
---|
7 | (* let's implement a daemon not used by automation *) |
---|
8 | inductive DAEMONXXX: Type[0] ≝ K1DAEMONXXX: DAEMONXXX | K2DAEMONXXX: DAEMONXXX. |
---|
9 | axiom IMPOSSIBLE: K1DAEMONXXX = K2DAEMONXXX. |
---|
10 | example daemon: False. generalize in match IMPOSSIBLE; #IMPOSSIBLE destruct(IMPOSSIBLE) qed. |
---|
11 | example not_implemented: False. cases daemon qed. |
---|
12 | |
---|
13 | notation "⊥" with precedence 90 |
---|
14 | for @{ match ? in False with [ ] }. |
---|
15 | |
---|
16 | notation > "hvbox('let' 〈ident x,ident y〉 ≝ t 'in' s)" |
---|
17 | with precedence 10 |
---|
18 | for @{ match $t with [ pair ${ident x} ${ident y} ⇒ $s ] }. |
---|
19 | |
---|
20 | definition ltb ≝ |
---|
21 | λm, n: nat. |
---|
22 | leb (S m) n. |
---|
23 | |
---|
24 | definition geb ≝ |
---|
25 | λm, n: nat. |
---|
26 | ltb n m. |
---|
27 | |
---|
28 | definition gtb ≝ |
---|
29 | λm, n: nat. |
---|
30 | leb n m. |
---|
31 | |
---|
32 | (* dpm: unless I'm being stupid, this isn't defined in the stdlib? *) |
---|
33 | let rec eq_nat (n: nat) (m: nat) on n: bool ≝ |
---|
34 | match n with |
---|
35 | [ O ⇒ match m with [ O ⇒ true | _ ⇒ false ] |
---|
36 | | S n' ⇒ match m with [ S m' ⇒ eq_nat n' m' | _ ⇒ false ] |
---|
37 | ]. |
---|
38 | |
---|
39 | let rec prefix |
---|
40 | (A: Type[0]) (k: nat) (l: list A) |
---|
41 | on l ≝ |
---|
42 | match l with |
---|
43 | [ nil ⇒ [ ] |
---|
44 | | cons hd tl ⇒ |
---|
45 | match k with |
---|
46 | [ O ⇒ [ ] |
---|
47 | | S k' ⇒ hd :: prefix A k' tl |
---|
48 | ] |
---|
49 | ]. |
---|
50 | |
---|
51 | let rec fold_left2 |
---|
52 | (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C → A) (accu: A) |
---|
53 | (left: list B) (right: list C) (proof: |left| = |right|) |
---|
54 | on left: A ≝ |
---|
55 | match left return λx. |x| = |right| → A with |
---|
56 | [ nil ⇒ λnil_prf. |
---|
57 | match right return λx. |[ ]| = |x| → A with |
---|
58 | [ nil ⇒ λnil_nil_prf. accu |
---|
59 | | cons hd tl ⇒ λcons_nil_absrd. ? |
---|
60 | ] nil_prf |
---|
61 | | cons hd tl ⇒ λcons_prf. |
---|
62 | match right return λx. |hd::tl| = |x| → A with |
---|
63 | [ nil ⇒ λcons_nil_absrd. ? |
---|
64 | | cons hd' tl' ⇒ λcons_cons_prf. |
---|
65 | fold_left2 … f (f accu hd hd') tl tl' ? |
---|
66 | ] cons_prf |
---|
67 | ] proof. |
---|
68 | [ 1: normalize in cons_nil_absrd; |
---|
69 | destruct(cons_nil_absrd) |
---|
70 | | 2: normalize in cons_nil_absrd; |
---|
71 | destruct(cons_nil_absrd) |
---|
72 | | 3: normalize in cons_cons_prf; |
---|
73 | @injective_S |
---|
74 | assumption |
---|
75 | ] |
---|
76 | qed. |
---|
77 | |
---|
78 | let rec remove_n_first_internal |
---|
79 | (i: nat) (A: Type[0]) (l: list A) (n: nat) |
---|
80 | on l ≝ |
---|
81 | match l with |
---|
82 | [ nil ⇒ [ ] |
---|
83 | | cons hd tl ⇒ |
---|
84 | match eq_nat i n with |
---|
85 | [ true ⇒ l |
---|
86 | | _ ⇒ remove_n_first_internal (S i) A tl n |
---|
87 | ] |
---|
88 | ]. |
---|
89 | |
---|
90 | definition remove_n_first ≝ |
---|
91 | λA: Type[0]. |
---|
92 | λn: nat. |
---|
93 | λl: list A. |
---|
94 | remove_n_first_internal 0 A l n. |
---|
95 | |
---|
96 | let rec foldi_from_until_internal |
---|
97 | (A: Type[0]) (i: nat) (res: ?) (rem: list A) (m: nat) (f: nat → list A → A → list A) |
---|
98 | on rem ≝ |
---|
99 | match rem with |
---|
100 | [ nil ⇒ res |
---|
101 | | cons e tl ⇒ |
---|
102 | match geb i m with |
---|
103 | [ true ⇒ res |
---|
104 | | _ ⇒ foldi_from_until_internal A (S i) (f i res e) tl m f |
---|
105 | ] |
---|
106 | ]. |
---|
107 | |
---|
108 | definition foldi_from_until ≝ |
---|
109 | λA: Type[0]. |
---|
110 | λn: nat. |
---|
111 | λm: nat. |
---|
112 | λf: ?. |
---|
113 | λa: ?. |
---|
114 | λl: ?. |
---|
115 | foldi_from_until_internal A 0 a (remove_n_first A n l) m f. |
---|
116 | |
---|
117 | definition foldi_from ≝ |
---|
118 | λA: Type[0]. |
---|
119 | λn. |
---|
120 | λf. |
---|
121 | λa. |
---|
122 | λl. |
---|
123 | foldi_from_until A n (|l|) f a l. |
---|
124 | |
---|
125 | definition foldi_until ≝ |
---|
126 | λA: Type[0]. |
---|
127 | λm. |
---|
128 | λf. |
---|
129 | λa. |
---|
130 | λl. |
---|
131 | foldi_from_until A 0 m f a l. |
---|
132 | |
---|
133 | definition foldi ≝ |
---|
134 | λA: Type[0]. |
---|
135 | λf. |
---|
136 | λa. |
---|
137 | λl. |
---|
138 | foldi_from_until A 0 (|l|) f a l. |
---|
139 | |
---|
140 | definition hd_safe ≝ |
---|
141 | λA: Type[0]. |
---|
142 | λl: list A. |
---|
143 | λproof: 0 < |l|. |
---|
144 | match l return λx. 0 < |x| → A with |
---|
145 | [ nil ⇒ λnil_absrd. ? |
---|
146 | | cons hd tl ⇒ λcons_prf. hd |
---|
147 | ] proof. |
---|
148 | normalize in nil_absrd; |
---|
149 | cases(not_le_Sn_O 0) |
---|
150 | #HYP |
---|
151 | cases(HYP nil_absrd) |
---|
152 | qed. |
---|
153 | |
---|
154 | definition tail_safe ≝ |
---|
155 | λA: Type[0]. |
---|
156 | λl: list A. |
---|
157 | λproof: 0 < |l|. |
---|
158 | match l return λx. 0 < |x| → list A with |
---|
159 | [ nil ⇒ λnil_absrd. ? |
---|
160 | | cons hd tl ⇒ λcons_prf. tl |
---|
161 | ] proof. |
---|
162 | normalize in nil_absrd; |
---|
163 | cases(not_le_Sn_O 0) |
---|
164 | #HYP |
---|
165 | cases(HYP nil_absrd) |
---|
166 | qed. |
---|
167 | |
---|
168 | let rec split |
---|
169 | (A: Type[0]) (l: list A) (index: nat) (proof: index ≤ |l|) |
---|
170 | on index ≝ |
---|
171 | match index return λx. x ≤ |l| → (list A) × (list A) with |
---|
172 | [ O ⇒ λzero_prf. 〈[], l〉 |
---|
173 | | S index' ⇒ λsucc_prf. |
---|
174 | match l return λx. S index' ≤ |x| → (list A) × (list A) with |
---|
175 | [ nil ⇒ λnil_absrd. ? |
---|
176 | | cons hd tl ⇒ λcons_prf. |
---|
177 | let 〈l1, l2〉 ≝ split A tl index' ? in |
---|
178 | 〈hd :: l1, l2〉 |
---|
179 | ] succ_prf |
---|
180 | ] proof. |
---|
181 | [1: normalize in nil_absrd; |
---|
182 | cases(not_le_Sn_O index') |
---|
183 | #HYP |
---|
184 | cases(HYP nil_absrd) |
---|
185 | |2: normalize in cons_prf; |
---|
186 | @le_S_S_to_le |
---|
187 | assumption |
---|
188 | ] |
---|
189 | qed. |
---|
190 | |
---|
191 | let rec nth_safe |
---|
192 | (elt_type: Type[0]) (index: nat) (the_list: list elt_type) |
---|
193 | (proof: index < | the_list |) |
---|
194 | on index ≝ |
---|
195 | match index return λs. s < | the_list | → elt_type with |
---|
196 | [ O ⇒ |
---|
197 | match the_list return λt. 0 < | t | → elt_type with |
---|
198 | [ nil ⇒ λnil_absurd. ? |
---|
199 | | cons hd tl ⇒ λcons_proof. hd |
---|
200 | ] |
---|
201 | | S index' ⇒ |
---|
202 | match the_list return λt. S index' < | t | → elt_type with |
---|
203 | [ nil ⇒ λnil_absurd. ? |
---|
204 | | cons hd tl ⇒ |
---|
205 | λcons_proof. nth_safe elt_type index' tl ? |
---|
206 | ] |
---|
207 | ] proof. |
---|
208 | [ normalize in nil_absurd; |
---|
209 | cases (not_le_Sn_O 0) |
---|
210 | #ABSURD |
---|
211 | elim (ABSURD nil_absurd) |
---|
212 | | normalize in nil_absurd; |
---|
213 | cases (not_le_Sn_O (S index')) |
---|
214 | #ABSURD |
---|
215 | elim (ABSURD nil_absurd) |
---|
216 | | normalize in cons_proof |
---|
217 | @le_S_S_to_le |
---|
218 | assumption |
---|
219 | ] |
---|
220 | qed. |
---|
221 | |
---|
222 | definition last_safe ≝ |
---|
223 | λelt_type: Type[0]. |
---|
224 | λthe_list: list elt_type. |
---|
225 | λproof : 0 < | the_list |. |
---|
226 | nth_safe elt_type (|the_list| - 1) the_list ?. |
---|
227 | normalize /2/ |
---|
228 | qed. |
---|
229 | |
---|
230 | let rec reduce |
---|
231 | (A: Type[0]) (B: Type[0]) (left: list A) (right: list B) on left ≝ |
---|
232 | match left with |
---|
233 | [ nil ⇒ 〈〈[ ], [ ]〉, 〈[ ], right〉〉 |
---|
234 | | cons hd tl ⇒ |
---|
235 | match right with |
---|
236 | [ nil ⇒ 〈〈[ ], left〉, 〈[ ], [ ]〉〉 |
---|
237 | | cons hd' tl' ⇒ |
---|
238 | let 〈cleft, cright〉 ≝ reduce A B tl tl' in |
---|
239 | let 〈commonl, restl〉 ≝ cleft in |
---|
240 | let 〈commonr, restr〉 ≝ cright in |
---|
241 | 〈〈hd :: commonl, restl〉, 〈hd' :: commonr, restr〉〉 |
---|
242 | ] |
---|
243 | ]. |
---|
244 | |
---|
245 | (* |
---|
246 | axiom reduce_strong: |
---|
247 | ∀A: Type[0]. |
---|
248 | ∀left: list A. |
---|
249 | ∀right: list A. |
---|
250 | Σret: ((list A) × (list A)) × ((list A) × (list A)). | \fst (\fst ret) | = | \fst (\snd ret) |. |
---|
251 | *) |
---|
252 | |
---|
253 | let rec reduce_strong |
---|
254 | (A: Type[0]) (B: Type[0]) (left: list A) (right: list B) |
---|
255 | on left : Σret: ((list A) × (list A)) × ((list B) × (list B)). |\fst (\fst ret)| = |\fst (\snd ret)| ≝ |
---|
256 | match left with |
---|
257 | [ nil ⇒ 〈〈[ ], [ ]〉, 〈[ ], right〉〉 |
---|
258 | | cons hd tl ⇒ |
---|
259 | match right with |
---|
260 | [ nil ⇒ 〈〈[ ], left〉, 〈[ ], [ ]〉〉 |
---|
261 | | cons hd' tl' ⇒ |
---|
262 | let 〈cleft, cright〉 ≝ reduce_strong A B tl tl' in |
---|
263 | let 〈commonl, restl〉 ≝ cleft in |
---|
264 | let 〈commonr, restr〉 ≝ cright in |
---|
265 | 〈〈hd :: commonl, restl〉, 〈hd' :: commonr, restr〉〉 |
---|
266 | ] |
---|
267 | ]. |
---|
268 | [ 1: normalize % |
---|
269 | | 2: normalize % |
---|
270 | | 3: normalize |
---|
271 | generalize in match (sig2 … (reduce_strong A B tl tl1)); |
---|
272 | >p2 >p3 >p4 normalize in ⊢ (% → ?) |
---|
273 | #HYP // |
---|
274 | ] |
---|
275 | qed. |
---|
276 | |
---|
277 | let rec map2_opt |
---|
278 | (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C) |
---|
279 | (left: list A) (right: list B) on left ≝ |
---|
280 | match left with |
---|
281 | [ nil ⇒ |
---|
282 | match right with |
---|
283 | [ nil ⇒ Some ? (nil C) |
---|
284 | | _ ⇒ None ? |
---|
285 | ] |
---|
286 | | cons hd tl ⇒ |
---|
287 | match right with |
---|
288 | [ nil ⇒ None ? |
---|
289 | | cons hd' tl' ⇒ |
---|
290 | match map2_opt A B C f tl tl' with |
---|
291 | [ None ⇒ None ? |
---|
292 | | Some tail ⇒ Some ? (f hd hd' :: tail) |
---|
293 | ] |
---|
294 | ] |
---|
295 | ]. |
---|
296 | |
---|
297 | let rec map2 |
---|
298 | (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C) |
---|
299 | (left: list A) (right: list B) (proof: | left | = | right |) on left ≝ |
---|
300 | match left return λx. | x | = | right | → list C with |
---|
301 | [ nil ⇒ |
---|
302 | match right return λy. | [] | = | y | → list C with |
---|
303 | [ nil ⇒ λnil_prf. nil C |
---|
304 | | _ ⇒ λcons_absrd. ? |
---|
305 | ] |
---|
306 | | cons hd tl ⇒ |
---|
307 | match right return λy. | hd::tl | = | y | → list C with |
---|
308 | [ nil ⇒ λnil_absrd. ? |
---|
309 | | cons hd' tl' ⇒ λcons_prf. (f hd hd') :: map2 A B C f tl tl' ? |
---|
310 | ] |
---|
311 | ] proof. |
---|
312 | [1: normalize in cons_absrd; |
---|
313 | destruct(cons_absrd) |
---|
314 | |2: normalize in nil_absrd; |
---|
315 | destruct(nil_absrd) |
---|
316 | |3: normalize in cons_prf; |
---|
317 | destruct(cons_prf) |
---|
318 | assumption |
---|
319 | ] |
---|
320 | qed. |
---|
321 | |
---|
322 | let rec map3 |
---|
323 | (A: Type[0]) (B: Type[0]) (C: Type[0]) (D: Type[0]) (f: A → B → C → D) |
---|
324 | (left: list A) (centre: list B) (right: list C) |
---|
325 | (prflc: |left| = |centre|) (prfcr: |centre| = |right|) on left ≝ |
---|
326 | match left return λx. |x| = |centre| → list D with |
---|
327 | [ nil ⇒ λnil_prf. |
---|
328 | match centre return λx. |x| = |right| → list D with |
---|
329 | [ nil ⇒ λnil_nil_prf. |
---|
330 | match right return λx. |nil ?| = |x| → list D with |
---|
331 | [ nil ⇒ λnil_nil_nil_prf. nil D |
---|
332 | | cons hd tl ⇒ λcons_nil_nil_absrd. ? |
---|
333 | ] nil_nil_prf |
---|
334 | | cons hd tl ⇒ λnil_cons_absrd. ? |
---|
335 | ] prfcr |
---|
336 | | cons hd tl ⇒ λcons_prf. |
---|
337 | match centre return λx. |x| = |right| → list D with |
---|
338 | [ nil ⇒ λcons_nil_absrd. ? |
---|
339 | | cons hd' tl' ⇒ λcons_cons_prf. |
---|
340 | match right return λx. |right| = |x| → |cons ? hd' tl'| = |x| → list D with |
---|
341 | [ nil ⇒ λrefl_prf. λcons_cons_nil_absrd. ? |
---|
342 | | cons hd'' tl'' ⇒ λrefl_prf. λcons_cons_cons_prf. |
---|
343 | (f hd hd' hd'') :: (map3 A B C D f tl tl' tl'' ? ?) |
---|
344 | ] (refl ? (|right|)) cons_cons_prf |
---|
345 | ] prfcr |
---|
346 | ] prflc. |
---|
347 | [ 1: normalize in cons_nil_nil_absrd; |
---|
348 | destruct(cons_nil_nil_absrd) |
---|
349 | | 2: generalize in match nil_cons_absrd; |
---|
350 | <prfcr <nil_prf #HYP |
---|
351 | normalize in HYP; |
---|
352 | destruct(HYP) |
---|
353 | | 3: generalize in match cons_nil_absrd; |
---|
354 | <prfcr <cons_prf #HYP |
---|
355 | normalize in HYP; |
---|
356 | destruct(HYP) |
---|
357 | | 4: normalize in cons_cons_nil_absrd; |
---|
358 | destruct(cons_cons_nil_absrd) |
---|
359 | | 5: normalize in cons_cons_cons_prf; |
---|
360 | destruct(cons_cons_cons_prf) |
---|
361 | assumption |
---|
362 | | 6: generalize in match cons_cons_cons_prf; |
---|
363 | <refl_prf <prfcr <cons_prf #HYP |
---|
364 | normalize in HYP; |
---|
365 | destruct(HYP) |
---|
366 | @sym_eq assumption |
---|
367 | ] |
---|
368 | qed. |
---|
369 | |
---|
370 | lemma eq_rect_Type0_r : |
---|
371 | ∀A: Type[0]. |
---|
372 | ∀a:A. |
---|
373 | ∀P: ∀x:A. eq ? x a → Type[0]. P a (refl A a) → ∀x: A.∀p:eq ? x a. P x p. |
---|
374 | #A #a #P #H #x #p |
---|
375 | generalize in match H |
---|
376 | generalize in match P |
---|
377 | cases p |
---|
378 | // |
---|
379 | qed. |
---|
380 | |
---|
381 | let rec safe_nth (A: Type[0]) (n: nat) (l: list A) (p: n < length A l) on n: A ≝ |
---|
382 | match n return λo. o < length A l → A with |
---|
383 | [ O ⇒ |
---|
384 | match l return λm. 0 < length A m → A with |
---|
385 | [ nil ⇒ λabsd1. ? |
---|
386 | | cons hd tl ⇒ λprf1. hd |
---|
387 | ] |
---|
388 | | S n' ⇒ |
---|
389 | match l return λm. S n' < length A m → A with |
---|
390 | [ nil ⇒ λabsd2. ? |
---|
391 | | cons hd tl ⇒ λprf2. safe_nth A n' tl ? |
---|
392 | ] |
---|
393 | ] ?. |
---|
394 | [ 1: |
---|
395 | @ p |
---|
396 | | 4: |
---|
397 | normalize in prf2 |
---|
398 | normalize |
---|
399 | @ le_S_S_to_le |
---|
400 | assumption |
---|
401 | | 2: |
---|
402 | normalize in absd1; |
---|
403 | cases (not_le_Sn_O O) |
---|
404 | # H |
---|
405 | elim (H absd1) |
---|
406 | | 3: |
---|
407 | normalize in absd2; |
---|
408 | cases (not_le_Sn_O (S n')) |
---|
409 | # H |
---|
410 | elim (H absd2) |
---|
411 | ] |
---|
412 | qed. |
---|
413 | |
---|
414 | let rec nub_by_internal (A: Type[0]) (f: A → A → bool) (l: list A) (n: nat) on n ≝ |
---|
415 | match n with |
---|
416 | [ O ⇒ |
---|
417 | match l with |
---|
418 | [ nil ⇒ [ ] |
---|
419 | | cons hd tl ⇒ l |
---|
420 | ] |
---|
421 | | S n ⇒ |
---|
422 | match l with |
---|
423 | [ nil ⇒ [ ] |
---|
424 | | cons hd tl ⇒ |
---|
425 | hd :: nub_by_internal A f (filter ? (λy. notb (f y hd)) tl) n |
---|
426 | ] |
---|
427 | ]. |
---|
428 | |
---|
429 | definition nub_by ≝ |
---|
430 | λA: Type[0]. |
---|
431 | λf: A → A → bool. |
---|
432 | λl: list A. |
---|
433 | nub_by_internal A f l (length ? l). |
---|
434 | |
---|
435 | let rec member (A: Type[0]) (eq: A → A → bool) (a: A) (l: list A) on l ≝ |
---|
436 | match l with |
---|
437 | [ nil ⇒ false |
---|
438 | | cons hd tl ⇒ orb (eq a hd) (member A eq a tl) |
---|
439 | ]. |
---|
440 | |
---|
441 | let rec take (A: Type[0]) (n: nat) (l: list A) on n: list A ≝ |
---|
442 | match n with |
---|
443 | [ O ⇒ [ ] |
---|
444 | | S n ⇒ |
---|
445 | match l with |
---|
446 | [ nil ⇒ [ ] |
---|
447 | | cons hd tl ⇒ hd :: take A n tl |
---|
448 | ] |
---|
449 | ]. |
---|
450 | |
---|
451 | let rec drop (A: Type[0]) (n: nat) (l: list A) on n ≝ |
---|
452 | match n with |
---|
453 | [ O ⇒ l |
---|
454 | | S n ⇒ |
---|
455 | match l with |
---|
456 | [ nil ⇒ [ ] |
---|
457 | | cons hd tl ⇒ drop A n tl |
---|
458 | ] |
---|
459 | ]. |
---|
460 | |
---|
461 | definition list_split ≝ |
---|
462 | λA: Type[0]. |
---|
463 | λn: nat. |
---|
464 | λl: list A. |
---|
465 | 〈take A n l, drop A n l〉. |
---|
466 | |
---|
467 | let rec mapi_internal (A: Type[0]) (B: Type[0]) (n: nat) (f: nat → A → B) |
---|
468 | (l: list A) on l: list B ≝ |
---|
469 | match l with |
---|
470 | [ nil ⇒ nil ? |
---|
471 | | cons hd tl ⇒ (f n hd) :: (mapi_internal A B (n + 1) f tl) |
---|
472 | ]. |
---|
473 | |
---|
474 | definition mapi ≝ |
---|
475 | λA, B: Type[0]. |
---|
476 | λf: nat → A → B. |
---|
477 | λl: list A. |
---|
478 | mapi_internal A B 0 f l. |
---|
479 | |
---|
480 | let rec zip_pottier |
---|
481 | (A: Type[0]) (B: Type[0]) (left: list A) (right: list B) |
---|
482 | on left ≝ |
---|
483 | match left with |
---|
484 | [ nil ⇒ [ ] |
---|
485 | | cons hd tl ⇒ |
---|
486 | match right with |
---|
487 | [ nil ⇒ [ ] |
---|
488 | | cons hd' tl' ⇒ 〈hd, hd'〉 :: zip_pottier A B tl tl' |
---|
489 | ] |
---|
490 | ]. |
---|
491 | |
---|
492 | let rec zip_safe |
---|
493 | (A: Type[0]) (B: Type[0]) (left: list A) (right: list B) (prf: |left| = |right|) |
---|
494 | on left ≝ |
---|
495 | match left return λx. |x| = |right| → list (A × B) with |
---|
496 | [ nil ⇒ λnil_prf. |
---|
497 | match right return λx. |[ ]| = |x| → list (A × B) with |
---|
498 | [ nil ⇒ λnil_nil_prf. [ ] |
---|
499 | | cons hd tl ⇒ λnil_cons_absrd. ? |
---|
500 | ] nil_prf |
---|
501 | | cons hd tl ⇒ λcons_prf. |
---|
502 | match right return λx. |hd::tl| = |x| → list (A × B) with |
---|
503 | [ nil ⇒ λcons_nil_absrd. ? |
---|
504 | | cons hd' tl' ⇒ λcons_cons_prf. 〈hd, hd'〉 :: zip_safe A B tl tl' ? |
---|
505 | ] cons_prf |
---|
506 | ] prf. |
---|
507 | [ 1: normalize in nil_cons_absrd; |
---|
508 | destruct(nil_cons_absrd) |
---|
509 | | 2: normalize in cons_nil_absrd; |
---|
510 | destruct(cons_nil_absrd) |
---|
511 | | 3: normalize in cons_cons_prf; |
---|
512 | @injective_S |
---|
513 | assumption |
---|
514 | ] |
---|
515 | qed. |
---|
516 | |
---|
517 | let rec zip (A: Type[0]) (B: Type[0]) (l: list A) (r: list B) on l: option (list (A × B)) ≝ |
---|
518 | match l with |
---|
519 | [ nil ⇒ Some ? (nil (A × B)) |
---|
520 | | cons hd tl ⇒ |
---|
521 | match r with |
---|
522 | [ nil ⇒ None ? |
---|
523 | | cons hd' tl' ⇒ |
---|
524 | match zip ? ? tl tl' with |
---|
525 | [ None ⇒ None ? |
---|
526 | | Some tail ⇒ Some ? (〈hd, hd'〉 :: tail) |
---|
527 | ] |
---|
528 | ] |
---|
529 | ]. |
---|
530 | |
---|
531 | let rec foldl (A: Type[0]) (B: Type[0]) (f: A → B → A) (a: A) (l: list B) on l ≝ |
---|
532 | match l with |
---|
533 | [ nil ⇒ a |
---|
534 | | cons hd tl ⇒ foldl A B f (f a hd) tl |
---|
535 | ]. |
---|
536 | |
---|
537 | lemma foldl_step: |
---|
538 | ∀A:Type[0]. |
---|
539 | ∀B: Type[0]. |
---|
540 | ∀H: A → B → A. |
---|
541 | ∀acc: A. |
---|
542 | ∀pre: list B. |
---|
543 | ∀hd:B. |
---|
544 | foldl A B H acc (pre@[hd]) = (H (foldl A B H acc pre) hd). |
---|
545 | #A #B #H #acc #pre generalize in match acc; -acc; elim pre |
---|
546 | [ normalize; // |
---|
547 | | #hd #tl #IH #acc #X normalize; @IH ] |
---|
548 | qed. |
---|
549 | |
---|
550 | lemma foldl_append: |
---|
551 | ∀A:Type[0]. |
---|
552 | ∀B: Type[0]. |
---|
553 | ∀H: A → B → A. |
---|
554 | ∀acc: A. |
---|
555 | ∀suff,pre: list B. |
---|
556 | foldl A B H acc (pre@suff) = (foldl A B H (foldl A B H acc pre) suff). |
---|
557 | #A #B #H #acc #suff elim suff |
---|
558 | [ #pre >append_nil % |
---|
559 | | #hd #tl #IH #pre whd in ⊢ (???%) <(foldl_step … H ??) applyS (IH (pre@[hd])) ] |
---|
560 | qed. |
---|
561 | |
---|
562 | definition flatten ≝ |
---|
563 | λA: Type[0]. |
---|
564 | λl: list (list A). |
---|
565 | foldr ? ? (append ?) [ ] l. |
---|
566 | |
---|
567 | let rec rev (A: Type[0]) (l: list A) on l ≝ |
---|
568 | match l with |
---|
569 | [ nil ⇒ nil A |
---|
570 | | cons hd tl ⇒ (rev A tl) @ [ hd ] |
---|
571 | ]. |
---|
572 | |
---|
573 | lemma append_length: |
---|
574 | ∀A: Type[0]. |
---|
575 | ∀l, r: list A. |
---|
576 | |(l @ r)| = |l| + |r|. |
---|
577 | #A #L #R |
---|
578 | elim L |
---|
579 | [ % |
---|
580 | | #HD #TL #IH |
---|
581 | normalize >IH % |
---|
582 | ] |
---|
583 | qed. |
---|
584 | |
---|
585 | lemma append_nil: |
---|
586 | ∀A: Type[0]. |
---|
587 | ∀l: list A. |
---|
588 | l @ [ ] = l. |
---|
589 | #A #L |
---|
590 | elim L // |
---|
591 | qed. |
---|
592 | |
---|
593 | lemma rev_append: |
---|
594 | ∀A: Type[0]. |
---|
595 | ∀l, r: list A. |
---|
596 | rev A (l @ r) = rev A r @ rev A l. |
---|
597 | #A #L #R |
---|
598 | elim L |
---|
599 | [ normalize >append_nil % |
---|
600 | | #HD #TL #IH |
---|
601 | normalize >IH |
---|
602 | @associative_append |
---|
603 | ] |
---|
604 | qed. |
---|
605 | |
---|
606 | lemma rev_length: |
---|
607 | ∀A: Type[0]. |
---|
608 | ∀l: list A. |
---|
609 | |rev A l| = |l|. |
---|
610 | #A #L |
---|
611 | elim L |
---|
612 | [ % |
---|
613 | | #HD #TL #IH |
---|
614 | normalize |
---|
615 | >(append_length A (rev A TL) [HD]) |
---|
616 | normalize /2/ |
---|
617 | ] |
---|
618 | qed. |
---|
619 | |
---|
620 | lemma nth_append_first: |
---|
621 | ∀A:Type[0]. |
---|
622 | ∀n:nat.∀l1,l2:list A.∀d:A. |
---|
623 | n < |l1| → nth n A (l1@l2) d = nth n A l1 d. |
---|
624 | #A #n #l1 #l2 #d |
---|
625 | generalize in match n; -n; elim l1 |
---|
626 | [ normalize #k #Hk @⊥ @(absurd … Hk) @not_le_Sn_O |
---|
627 | | #h #t #Hind #k normalize |
---|
628 | cases k -k |
---|
629 | [ #Hk normalize @refl |
---|
630 | | #k #Hk normalize @(Hind k) @le_S_S_to_le @Hk |
---|
631 | ] |
---|
632 | ] |
---|
633 | qed. |
---|
634 | |
---|
635 | lemma nth_append_second: |
---|
636 | ∀A: Type[0].∀n.∀l1,l2:list A.∀d.n ≥ length A l1 -> |
---|
637 | nth n A (l1@l2) d = nth (n - length A l1) A l2 d. |
---|
638 | #A #n #l1 #l2 #d |
---|
639 | generalize in match n; -n; elim l1 |
---|
640 | [ normalize #k #Hk <(minus_n_O) @refl |
---|
641 | | #h #t #Hind #k normalize |
---|
642 | cases k -k; |
---|
643 | [ #Hk @⊥ @(absurd (S (|t|) ≤ 0)) [ @Hk | @not_le_Sn_O ] |
---|
644 | | #k #Hk normalize @(Hind k) @le_S_S_to_le @Hk |
---|
645 | ] |
---|
646 | ] |
---|
647 | qed. |
---|
648 | |
---|
649 | |
---|
650 | notation > "'if' term 19 e 'then' term 19 t 'else' term 48 f" non associative with precedence 19 |
---|
651 | for @{ match $e in bool with [ true ⇒ $t | false ⇒ $f] }. |
---|
652 | 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 |
---|
653 | for @{ match $e with [ true ⇒ $t | false ⇒ $f] }. |
---|
654 | |
---|
655 | let rec fold_left_i_aux (A: Type[0]) (B: Type[0]) |
---|
656 | (f: nat → A → B → A) (x: A) (i: nat) (l: list B) on l ≝ |
---|
657 | match l with |
---|
658 | [ nil ⇒ x |
---|
659 | | cons hd tl ⇒ fold_left_i_aux A B f (f i x hd) (S i) tl |
---|
660 | ]. |
---|
661 | |
---|
662 | definition fold_left_i ≝ λA,B,f,x. fold_left_i_aux A B f x O. |
---|
663 | |
---|
664 | notation "hvbox(t⌈o ↦ h⌉)" |
---|
665 | with precedence 45 |
---|
666 | for @{ match (? : $o=$h) with [ refl ⇒ $t ] }. |
---|
667 | |
---|
668 | definition function_apply ≝ |
---|
669 | λA, B: Type[0]. |
---|
670 | λf: A → B. |
---|
671 | λa: A. |
---|
672 | f a. |
---|
673 | |
---|
674 | notation "f break $ x" |
---|
675 | left associative with precedence 99 |
---|
676 | for @{ 'function_apply $f $x }. |
---|
677 | |
---|
678 | interpretation "Function application" 'function_apply f x = (function_apply ? ? f x). |
---|
679 | |
---|
680 | let rec iterate (A: Type[0]) (f: A → A) (a: A) (n: nat) on n ≝ |
---|
681 | match n with |
---|
682 | [ O ⇒ a |
---|
683 | | S o ⇒ f (iterate A f a o) |
---|
684 | ]. |
---|
685 | |
---|
686 | (* Yeah, I probably ought to do something more general... *) |
---|
687 | notation "hvbox(\langle term 19 a, break term 19 b, break term 19 c\rangle)" |
---|
688 | with precedence 90 for @{ 'triple $a $b $c}. |
---|
689 | interpretation "Triple construction" 'triple x y z = (pair ? ? (pair ? ? x y) z). |
---|
690 | |
---|
691 | notation "hvbox(\langle term 19 a, break term 19 b, break term 19 c, break term 19 d\rangle)" |
---|
692 | with precedence 90 for @{ 'quadruple $a $b $c $d}. |
---|
693 | interpretation "Quadruple construction" 'quadruple w x y z = (pair ? ? (pair ? ? w x) (pair ? ? y z)). |
---|
694 | |
---|
695 | notation > "hvbox('let' 〈ident w,ident x,ident y,ident z〉 ≝ t 'in' s)" |
---|
696 | with precedence 10 |
---|
697 | for @{ match $t with [ pair ${fresh wx} ${fresh yz} ⇒ match ${fresh wx} with [ pair ${ident w} ${ident x} ⇒ match ${fresh yz} with [ pair ${ident y} ${ident z} ⇒ $s ] ] ] }. |
---|
698 | |
---|
699 | notation > "hvbox('let' 〈ident x,ident y,ident z〉 ≝ t 'in' s)" |
---|
700 | with precedence 10 |
---|
701 | for @{ match $t with [ pair ${fresh xy} ${ident z} ⇒ match ${fresh xy} with [ pair ${ident x} ${ident y} ⇒ $s ] ] }. |
---|
702 | |
---|
703 | notation < "hvbox('let' \nbsp hvbox(〈ident x,ident y〉\nbsp ≝ break t \nbsp 'in' \nbsp) break s)" |
---|
704 | with precedence 10 |
---|
705 | for @{ match $t with [ pair (${ident x}:$ignore) (${ident y}:$ignora) ⇒ $s ] }. |
---|
706 | |
---|
707 | axiom pair_elim': |
---|
708 | ∀A,B,C: Type[0]. |
---|
709 | ∀T: A → B → C. |
---|
710 | ∀p. |
---|
711 | ∀P: A×B → C → Prop. |
---|
712 | (∀lft, rgt. p = 〈lft,rgt〉 → P 〈lft,rgt〉 (T lft rgt)) → |
---|
713 | P p (let 〈lft, rgt〉 ≝ p in T lft rgt). |
---|
714 | |
---|
715 | axiom pair_elim'': |
---|
716 | ∀A,B,C,C': Type[0]. |
---|
717 | ∀T: A → B → C. |
---|
718 | ∀T': A → B → C'. |
---|
719 | ∀p. |
---|
720 | ∀P: A×B → C → C' → Prop. |
---|
721 | (∀lft, rgt. p = 〈lft,rgt〉 → P 〈lft,rgt〉 (T lft rgt) (T' lft rgt)) → |
---|
722 | P p (let 〈lft, rgt〉 ≝ p in T lft rgt) (let 〈lft, rgt〉 ≝ p in T' lft rgt). |
---|
723 | |
---|
724 | lemma pair_destruct_1: |
---|
725 | ∀A,B.∀a:A.∀b:B.∀c. 〈a,b〉 = c → a = \fst c. |
---|
726 | #A #B #a #b *; /2/ |
---|
727 | qed. |
---|
728 | |
---|
729 | lemma pair_destruct_2: |
---|
730 | ∀A,B.∀a:A.∀b:B.∀c. 〈a,b〉 = c → b = \snd c. |
---|
731 | #A #B #a #b *; /2/ |
---|
732 | qed. |
---|
733 | |
---|
734 | |
---|
735 | let rec exclusive_disjunction (b: bool) (c: bool) on b ≝ |
---|
736 | match b with |
---|
737 | [ true ⇒ |
---|
738 | match c with |
---|
739 | [ false ⇒ true |
---|
740 | | true ⇒ false |
---|
741 | ] |
---|
742 | | false ⇒ |
---|
743 | match c with |
---|
744 | [ false ⇒ false |
---|
745 | | true ⇒ true |
---|
746 | ] |
---|
747 | ]. |
---|
748 | |
---|
749 | (* dpm: conflicts with library definitions |
---|
750 | interpretation "Nat less than" 'lt m n = (ltb m n). |
---|
751 | interpretation "Nat greater than" 'gt m n = (gtb m n). |
---|
752 | interpretation "Nat greater than eq" 'geq m n = (geb m n). |
---|
753 | *) |
---|
754 | |
---|
755 | let rec division_aux (m: nat) (n : nat) (p: nat) ≝ |
---|
756 | match ltb n (S p) with |
---|
757 | [ true ⇒ O |
---|
758 | | false ⇒ |
---|
759 | match m with |
---|
760 | [ O ⇒ O |
---|
761 | | (S q) ⇒ S (division_aux q (n - (S p)) p) |
---|
762 | ] |
---|
763 | ]. |
---|
764 | |
---|
765 | definition division ≝ |
---|
766 | λm, n: nat. |
---|
767 | match n with |
---|
768 | [ O ⇒ S m |
---|
769 | | S o ⇒ division_aux m m o |
---|
770 | ]. |
---|
771 | |
---|
772 | notation "hvbox(n break ÷ m)" |
---|
773 | right associative with precedence 47 |
---|
774 | for @{ 'division $n $m }. |
---|
775 | |
---|
776 | interpretation "Nat division" 'division n m = (division n m). |
---|
777 | |
---|
778 | let rec modulus_aux (m: nat) (n: nat) (p: nat) ≝ |
---|
779 | match leb n p with |
---|
780 | [ true ⇒ n |
---|
781 | | false ⇒ |
---|
782 | match m with |
---|
783 | [ O ⇒ n |
---|
784 | | S o ⇒ modulus_aux o (n - (S p)) p |
---|
785 | ] |
---|
786 | ]. |
---|
787 | |
---|
788 | definition modulus ≝ |
---|
789 | λm, n: nat. |
---|
790 | match n with |
---|
791 | [ O ⇒ m |
---|
792 | | S o ⇒ modulus_aux m m o |
---|
793 | ]. |
---|
794 | |
---|
795 | notation "hvbox(n break 'mod' m)" |
---|
796 | right associative with precedence 47 |
---|
797 | for @{ 'modulus $n $m }. |
---|
798 | |
---|
799 | interpretation "Nat modulus" 'modulus m n = (modulus m n). |
---|
800 | |
---|
801 | definition divide_with_remainder ≝ |
---|
802 | λm, n: nat. |
---|
803 | pair ? ? (m ÷ n) (modulus m n). |
---|
804 | |
---|
805 | let rec exponential (m: nat) (n: nat) on n ≝ |
---|
806 | match n with |
---|
807 | [ O ⇒ S O |
---|
808 | | S o ⇒ m * exponential m o |
---|
809 | ]. |
---|
810 | |
---|
811 | interpretation "Nat exponential" 'exp n m = (exponential n m). |
---|
812 | |
---|
813 | notation "hvbox(a break ⊎ b)" |
---|
814 | left associative with precedence 50 |
---|
815 | for @{ 'disjoint_union $a $b }. |
---|
816 | interpretation "sum" 'disjoint_union A B = (Sum A B). |
---|
817 | |
---|
818 | theorem less_than_or_equal_monotone: |
---|
819 | ∀m, n: nat. |
---|
820 | m ≤ n → (S m) ≤ (S n). |
---|
821 | #m #n #H |
---|
822 | elim H |
---|
823 | /2/ |
---|
824 | qed. |
---|
825 | |
---|
826 | theorem less_than_or_equal_b_complete: |
---|
827 | ∀m, n: nat. |
---|
828 | leb m n = false → ¬(m ≤ n). |
---|
829 | #m; |
---|
830 | elim m; |
---|
831 | normalize |
---|
832 | [ #n #H |
---|
833 | destruct |
---|
834 | | #y #H1 #z |
---|
835 | cases z |
---|
836 | normalize |
---|
837 | [ #H |
---|
838 | /2/ |
---|
839 | | /3/ |
---|
840 | ] |
---|
841 | ] |
---|
842 | qed. |
---|
843 | |
---|
844 | theorem less_than_or_equal_b_correct: |
---|
845 | ∀m, n: nat. |
---|
846 | leb m n = true → m ≤ n. |
---|
847 | #m |
---|
848 | elim m |
---|
849 | // |
---|
850 | #y #H1 #z |
---|
851 | cases z |
---|
852 | normalize |
---|
853 | [ #H |
---|
854 | destruct |
---|
855 | | #n #H lapply (H1 … H) /2/ |
---|
856 | ] |
---|
857 | qed. |
---|
858 | |
---|
859 | definition less_than_or_equal_b_elim: |
---|
860 | ∀m, n: nat. |
---|
861 | ∀P: bool → Type[0]. |
---|
862 | (m ≤ n → P true) → (¬(m ≤ n) → P false) → P (leb m n). |
---|
863 | #m #n #P #H1 #H2; |
---|
864 | lapply (less_than_or_equal_b_correct m n) |
---|
865 | lapply (less_than_or_equal_b_complete m n) |
---|
866 | cases (leb m n) |
---|
867 | /3/ |
---|
868 | qed. |
---|
869 | |
---|
870 | lemma inclusive_disjunction_true: |
---|
871 | ∀b, c: bool. |
---|
872 | (orb b c) = true → b = true ∨ c = true. |
---|
873 | # b |
---|
874 | # c |
---|
875 | elim b |
---|
876 | [ normalize |
---|
877 | # H |
---|
878 | @ or_introl |
---|
879 | % |
---|
880 | | normalize |
---|
881 | /2/ |
---|
882 | ] |
---|
883 | qed. |
---|
884 | |
---|
885 | lemma conjunction_true: |
---|
886 | ∀b, c: bool. |
---|
887 | andb b c = true → b = true ∧ c = true. |
---|
888 | # b |
---|
889 | # c |
---|
890 | elim b |
---|
891 | normalize |
---|
892 | [ /2/ |
---|
893 | | # K |
---|
894 | destruct |
---|
895 | ] |
---|
896 | qed. |
---|
897 | |
---|
898 | lemma eq_true_false: false=true → False. |
---|
899 | # K |
---|
900 | destruct |
---|
901 | qed. |
---|
902 | |
---|
903 | lemma inclusive_disjunction_b_true: ∀b. orb b true = true. |
---|
904 | # b |
---|
905 | cases b |
---|
906 | % |
---|
907 | qed. |
---|
908 | |
---|
909 | definition bool_to_Prop ≝ |
---|
910 | λb. match b with [ true ⇒ True | false ⇒ False ]. |
---|
911 | |
---|
912 | coercion bool_to_Prop: ∀b:bool. Prop ≝ bool_to_Prop on _b:bool to Type[0]. |
---|
913 | |
---|
914 | lemma eq_false_to_notb: ∀b. b = false → ¬ b. |
---|
915 | *; /2/ |
---|
916 | qed. |
---|
917 | |
---|
918 | lemma length_append: |
---|
919 | ∀A.∀l1,l2:list A. |
---|
920 | |l1 @ l2| = |l1| + |l2|. |
---|
921 | #A #l1 elim l1 |
---|
922 | [ // |
---|
923 | | #hd #tl #IH #l2 normalize <IH //] |
---|
924 | qed. |
---|