source: src/ASM/Util.ma @ 2037

Last change on this file since 2037 was 2037, checked in by sacerdot, 8 years ago

flatten is now part of stdlib

File size: 33.0 KB
Line 
1include "basics/lists/list.ma".
2include "basics/types.ma".
3include "arithmetics/nat.ma".
4include "basics/russell.ma".
5
6(* let's implement a daemon not used by automation *)
7inductive DAEMONXXX: Type[0] ≝ K1DAEMONXXX: DAEMONXXX | K2DAEMONXXX: DAEMONXXX.
8axiom IMPOSSIBLE: K1DAEMONXXX = K2DAEMONXXX.
9example daemon: False. generalize in match IMPOSSIBLE; #IMPOSSIBLE destruct(IMPOSSIBLE) qed.
10example not_implemented: False. cases daemon qed.
11
12notation "⊥" with precedence 90
13  for @{ match ? in False with [ ] }.
14notation "Ⓧ" with precedence 90
15  for @{ λabs.match abs in False with [ ] }.
16
17
18definition ltb ≝
19  λm, n: nat.
20    leb (S m) n.
21   
22definition geb ≝
23  λm, n: nat.
24    leb n m.
25
26definition gtb ≝
27  λm, n: nat.
28    ltb n m.
29
30(* dpm: unless I'm being stupid, this isn't defined in the stdlib? *)
31let rec eq_nat (n: nat) (m: nat) on n: bool ≝
32  match n with
33  [ O ⇒ match m with [ O ⇒ true | _ ⇒ false ]
34  | S n' ⇒ match m with [ S m' ⇒ eq_nat n' m' | _ ⇒ false ]
35  ].
36
37let rec forall
38  (A: Type[0]) (f: A → bool) (l: list A)
39    on l ≝
40  match l with
41  [ nil        ⇒ true
42  | cons hd tl ⇒ f hd ∧ forall A f tl
43  ].
44
45let rec prefix
46  (A: Type[0]) (k: nat) (l: list A)
47    on l ≝
48  match l with
49  [ nil ⇒ [ ]
50  | cons hd tl ⇒
51    match k with
52    [ O ⇒ [ ]
53    | S k' ⇒ hd :: prefix A k' tl
54    ]
55  ].
56 
57let rec fold_left2
58  (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C → A) (accu: A)
59  (left: list B) (right: list C) (proof: |left| = |right|)
60    on left: A ≝
61  match left return λx. |x| = |right| → A with
62  [ nil ⇒ λnil_prf.
63    match right return λx. |[ ]| = |x| → A with
64    [ nil ⇒ λnil_nil_prf. accu
65    | cons hd tl ⇒ λcons_nil_absrd. ?
66    ] nil_prf
67  | cons hd tl ⇒ λcons_prf.
68    match right return λx. |hd::tl| = |x| → A with
69    [ nil ⇒ λcons_nil_absrd. ?
70    | cons hd' tl' ⇒ λcons_cons_prf.
71        fold_left2 …  f (f accu hd hd') tl tl' ?
72    ] cons_prf
73  ] proof.
74  [ 1: normalize in cons_nil_absrd;
75       destruct(cons_nil_absrd)
76  | 2: normalize in cons_nil_absrd;
77       destruct(cons_nil_absrd)
78  | 3: normalize in cons_cons_prf;
79       @injective_S
80       assumption
81  ]
82qed.
83
84let rec remove_n_first_internal
85  (i: nat) (A: Type[0]) (l: list A) (n: nat)
86    on l ≝
87  match l with
88  [ nil ⇒ [ ]
89  | cons hd tl ⇒
90    match eq_nat i n with
91    [ true ⇒ l
92    | _ ⇒ remove_n_first_internal (S i) A tl n
93    ]
94  ].
95
96definition remove_n_first ≝
97  λA: Type[0].
98  λn: nat.
99  λl: list A.
100    remove_n_first_internal 0 A l n.
101   
102let rec foldi_from_until_internal
103  (A: Type[0]) (i: nat) (res: ?) (rem: list A) (m: nat) (f: nat → list A → A → list A)
104    on rem ≝
105  match rem with
106  [ nil ⇒ res
107  | cons e tl ⇒
108    match geb i m with
109    [ true ⇒ res
110    | _ ⇒ foldi_from_until_internal A (S i) (f i res e) tl m f
111    ]
112  ].
113
114definition foldi_from_until ≝
115  λA: Type[0].
116  λn: nat.
117  λm: nat.
118  λf: ?.
119  λa: ?.
120  λl: ?.
121    foldi_from_until_internal A 0 a (remove_n_first A n l) m f.
122
123definition foldi_from ≝
124  λA: Type[0].
125  λn.
126  λf.
127  λa.
128  λl.
129    foldi_from_until A n (|l|) f a l.
130
131definition foldi_until ≝
132  λA: Type[0].
133  λm.
134  λf.
135  λa.
136  λl.
137    foldi_from_until A 0 m f a l.
138
139definition foldi ≝
140  λA: Type[0].
141  λf.
142  λa.
143  λl.
144    foldi_from_until A 0 (|l|) f a l.
145
146definition hd_safe ≝
147  λA: Type[0].
148  λl: list A.
149  λproof: 0 < |l|.
150  match l return λx. 0 < |x| → A with
151  [ nil ⇒ λnil_absrd. ?
152  | cons hd tl ⇒ λcons_prf. hd
153  ] proof.
154  normalize in nil_absrd;
155  cases(not_le_Sn_O 0)
156  #HYP
157  cases(HYP nil_absrd)
158qed.
159
160definition tail_safe ≝
161  λA: Type[0].
162  λl: list A.
163  λproof: 0 < |l|.
164  match l return λx. 0 < |x| → list A with
165  [ nil ⇒ λnil_absrd. ?
166  | cons hd tl ⇒ λcons_prf. tl
167  ] proof.
168  normalize in nil_absrd;
169  cases(not_le_Sn_O 0)
170  #HYP
171  cases(HYP nil_absrd)
172qed.
173
174let rec safe_split
175  (A: Type[0]) (l: list A) (index: nat) (proof: index ≤ |l|)
176    on index ≝
177  match index return λx. x ≤ |l| → (list A) × (list A) with
178  [ O ⇒ λzero_prf. 〈[], l〉
179  | S index' ⇒ λsucc_prf.
180    match l return λx. S index' ≤ |x| → (list A) × (list A) with
181    [ nil ⇒ λnil_absrd. ?
182    | cons hd tl ⇒ λcons_prf.
183      let 〈l1, l2〉 ≝ safe_split A tl index' ? in
184        〈hd :: l1, l2〉
185    ] succ_prf
186  ] proof.
187  [1: normalize in nil_absrd;
188      cases(not_le_Sn_O index')
189      #HYP
190      cases(HYP nil_absrd)
191  |2: normalize in cons_prf;
192      @le_S_S_to_le
193      assumption
194  ]
195qed.
196
197let rec nth_safe
198  (elt_type: Type[0]) (index: nat) (the_list: list elt_type)
199  (proof: index < | the_list |)
200    on index ≝
201  match index return λs. s < | the_list | → elt_type with
202  [ O ⇒
203    match the_list return λt. 0 < | t | → elt_type with
204    [ nil        ⇒ λnil_absurd. ?
205    | cons hd tl ⇒ λcons_proof. hd
206    ]
207  | S index' ⇒
208    match the_list return λt. S index' < | t | → elt_type with
209    [ nil ⇒ λnil_absurd. ?
210    | cons hd tl ⇒
211      λcons_proof. nth_safe elt_type index' tl ?
212    ]
213  ] proof.
214  [ normalize in nil_absurd;
215    cases (not_le_Sn_O 0)
216    #ABSURD
217    elim (ABSURD nil_absurd)
218  | normalize in nil_absurd;
219    cases (not_le_Sn_O (S index'))
220    #ABSURD
221    elim (ABSURD nil_absurd)
222  | normalize in cons_proof;
223    @le_S_S_to_le
224    assumption
225  ]
226qed.
227
228definition last_safe ≝
229  λelt_type: Type[0].
230  λthe_list: list elt_type.
231  λproof   : 0 < | the_list |.
232    nth_safe elt_type (|the_list| - 1) the_list ?.
233  normalize /2 by lt_plus_to_minus/
234qed.
235
236let rec reduce
237  (A: Type[0]) (B: Type[0]) (left: list A) (right: list B) on left ≝
238  match left with
239  [ nil ⇒ 〈〈[ ], [ ]〉, 〈[ ], right〉〉
240  | cons hd tl ⇒
241    match right with
242    [ nil ⇒ 〈〈[ ], left〉, 〈[ ], [ ]〉〉
243    | cons hd' tl' ⇒
244      let 〈cleft, cright〉 ≝ reduce A B tl tl' in
245      let 〈commonl, restl〉 ≝ cleft in
246      let 〈commonr, restr〉 ≝ cright in
247        〈〈hd :: commonl, restl〉, 〈hd' :: commonr, restr〉〉
248    ]
249  ].
250
251(*
252axiom reduce_strong:
253  ∀A: Type[0].
254  ∀left: list A.
255  ∀right: list A.
256    Σret: ((list A) × (list A)) × ((list A) × (list A)). | \fst (\fst ret) | = | \fst (\snd ret) |.
257*)
258
259let rec reduce_strong
260  (A: Type[0]) (B: Type[0]) (left: list A) (right: list B)
261    on left : Σret: ((list A) × (list A)) × ((list B) × (list B)). |\fst (\fst ret)| = |\fst (\snd ret)|  ≝
262  match left with
263  [ nil ⇒ 〈〈[ ], [ ]〉, 〈[ ], right〉〉
264  | cons hd tl ⇒
265    match right with
266    [ nil ⇒ 〈〈[ ], left〉, 〈[ ], [ ]〉〉
267    | cons hd' tl' ⇒
268      let 〈cleft, cright〉 ≝ pi1 ?? (reduce_strong A B tl tl') in
269      let 〈commonl, restl〉 ≝ cleft in
270      let 〈commonr, restr〉 ≝ cright in
271        〈〈hd :: commonl, restl〉, 〈hd' :: commonr, restr〉〉
272    ]
273  ].
274  [ 1: normalize %
275  | 2: normalize %
276  | 3: normalize >p3 in p2; >p4 cases (reduce_strong … tl tl1) normalize
277       #X #H #EQ destruct // ]
278qed.
279   
280let rec map2_opt
281  (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C)
282  (left: list A) (right: list B) on left ≝
283  match left with
284  [ nil ⇒
285    match right with
286    [ nil ⇒ Some ? (nil C)
287    | _ ⇒ None ?
288    ]
289  | cons hd tl ⇒
290    match right with
291    [ nil ⇒ None ?
292    | cons hd' tl' ⇒
293      match map2_opt A B C f tl tl' with
294      [ None ⇒ None ?
295      | Some tail ⇒ Some ? (f hd hd' :: tail)
296      ]
297    ]
298  ].
299
300let rec map2
301  (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C)
302  (left: list A) (right: list B) (proof: | left | = | right |) on left ≝
303  match left return λx. | x | = | right | → list C with
304  [ nil ⇒
305    match right return λy. | [] | = | y | → list C with
306    [ nil ⇒ λnil_prf. nil C
307    | _ ⇒ λcons_absrd. ?
308    ]
309  | cons hd tl ⇒
310    match right return λy. | hd::tl | = | y | → list C with
311    [ nil ⇒ λnil_absrd. ?
312    | cons hd' tl' ⇒ λcons_prf. (f hd hd') :: map2 A B C f tl tl' ?
313    ]
314  ] proof.
315  [1: normalize in cons_absrd;
316      destruct(cons_absrd)
317  |2: normalize in nil_absrd;
318      destruct(nil_absrd)
319  |3: normalize in cons_prf;
320      destruct(cons_prf)
321      assumption
322  ]
323qed.
324
325let rec map3
326  (A: Type[0]) (B: Type[0]) (C: Type[0]) (D: Type[0]) (f: A → B → C → D)
327  (left: list A) (centre: list B) (right: list C)
328  (prflc: |left| = |centre|) (prfcr: |centre| = |right|) on left ≝
329  match left return λx. |x| = |centre| → list D with
330  [ nil ⇒ λnil_prf.
331    match centre return λx. |x| = |right| → list D with
332    [ nil ⇒ λnil_nil_prf.
333      match right return λx. |nil ?| = |x| → list D with
334      [ nil        ⇒ λnil_nil_nil_prf. nil D
335      | cons hd tl ⇒ λcons_nil_nil_absrd. ?
336      ] nil_nil_prf
337    | cons hd tl ⇒ λnil_cons_absrd. ?
338    ] prfcr
339  | cons hd tl ⇒ λcons_prf.
340    match centre return λx. |x| = |right| → list D with
341    [ nil ⇒ λcons_nil_absrd. ?
342    | cons hd' tl' ⇒ λcons_cons_prf.
343      match right return λx. |right| = |x| → |cons ? hd' tl'| = |x| → list D with
344      [ nil ⇒ λrefl_prf. λcons_cons_nil_absrd. ?
345      | cons hd'' tl'' ⇒ λrefl_prf. λcons_cons_cons_prf.
346        (f hd hd' hd'') :: (map3 A B C D f tl tl' tl'' ? ?)
347      ] (refl ? (|right|)) cons_cons_prf
348    ] prfcr
349  ] prflc.
350  [ 1: normalize in cons_nil_nil_absrd;
351       destruct(cons_nil_nil_absrd)
352  | 2: generalize in match nil_cons_absrd;
353       <prfcr <nil_prf #HYP
354       normalize in HYP;
355       destruct(HYP)
356  | 3: generalize in match cons_nil_absrd;
357       <prfcr <cons_prf #HYP
358       normalize in HYP;
359       destruct(HYP)
360  | 4: normalize in cons_cons_nil_absrd;
361       destruct(cons_cons_nil_absrd)
362  | 5: normalize in cons_cons_cons_prf;
363       destruct(cons_cons_cons_prf)
364       assumption
365  | 6: generalize in match cons_cons_cons_prf;
366       <refl_prf <prfcr <cons_prf #HYP
367       normalize in HYP;
368       destruct(HYP)
369       @sym_eq assumption
370  ]
371qed.
372 
373lemma eq_rect_Type0_r :
374  ∀A: Type[0].
375  ∀a:A.
376  ∀P: ∀x:A. eq ? x a → Type[0]. P a (refl A a) → ∀x: A.∀p:eq ? x a. P x p.
377  #A #a #P #H #x #p lapply H lapply P cases p //
378qed.
379 
380let rec safe_nth (A: Type[0]) (n: nat) (l: list A) (p: n < length A l) on n: A ≝
381  match n return λo. o < length A l → A with
382  [ O ⇒
383    match l return λm. 0 < length A m → A with
384    [ nil ⇒ λabsd1. ?
385    | cons hd tl ⇒ λprf1. hd
386    ]
387  | S n' ⇒
388    match l return λm. S n' < length A m → A with
389    [ nil ⇒ λabsd2. ?
390    | cons hd tl ⇒ λprf2. safe_nth A n' tl ?
391    ]
392  ] ?.
393  [ 1:
394    @ p
395  | 4:
396    normalize in prf2;
397    normalize
398    @ le_S_S_to_le
399    assumption
400  | 2:
401    normalize in absd1;
402    cases (not_le_Sn_O O)
403    # H
404    elim (H absd1)
405  | 3:
406    normalize in absd2;
407    cases (not_le_Sn_O (S n'))
408    # H
409    elim (H absd2)
410  ]
411qed.
412 
413let rec nub_by_internal (A: Type[0]) (f: A → A → bool) (l: list A) (n: nat) on n ≝
414  match n with
415  [ O ⇒
416    match l with
417    [ nil ⇒ [ ]
418    | cons hd tl ⇒ l
419    ]
420  | S n ⇒
421    match l with
422    [ nil ⇒ [ ]
423    | cons hd tl ⇒
424      hd :: nub_by_internal A f (filter ? (λy. notb (f y hd)) tl) n
425    ]
426  ].
427 
428definition nub_by ≝
429  λA: Type[0].
430  λf: A → A → bool.
431  λl: list A.
432    nub_by_internal A f l (length ? l).
433 
434let rec member (A: Type[0]) (eq: A → A → bool) (a: A) (l: list A) on l ≝
435  match l with
436  [ nil ⇒ false
437  | cons hd tl ⇒ orb (eq a hd) (member A eq a tl)
438  ].
439 
440let rec take (A: Type[0]) (n: nat) (l: list A) on n: list A ≝
441  match n with
442  [ O ⇒ [ ]
443  | S n ⇒
444    match l with
445    [ nil ⇒ [ ]
446    | cons hd tl ⇒ hd :: take A n tl
447    ]
448  ].
449 
450let rec drop (A: Type[0]) (n: nat) (l: list A) on n ≝
451  match n with
452  [ O ⇒ l
453  | S n ⇒
454    match l with
455    [ nil ⇒ [ ]
456    | cons hd tl ⇒ drop A n tl
457    ]
458  ].
459 
460definition list_split ≝
461  λA: Type[0].
462  λn: nat.
463  λl: list A.
464    〈take A n l, drop A n l〉.
465 
466let rec mapi_internal (A: Type[0]) (B: Type[0]) (n: nat) (f: nat → A → B)
467                      (l: list A) on l: list B ≝
468  match l with
469  [ nil ⇒ nil ?
470  | cons hd tl ⇒ (f n hd) :: (mapi_internal A B (n + 1) f tl)
471  ]. 
472
473definition mapi ≝
474  λA, B: Type[0].
475  λf: nat → A → B.
476  λl: list A.
477    mapi_internal A B 0 f l.
478
479let rec zip_pottier
480  (A: Type[0]) (B: Type[0]) (left: list A) (right: list B)
481    on left ≝
482  match left with
483  [ nil ⇒ [ ]
484  | cons hd tl ⇒
485    match right with
486    [ nil ⇒ [ ]
487    | cons hd' tl' ⇒ 〈hd, hd'〉 :: zip_pottier A B tl tl'
488    ]
489  ].
490
491let rec zip_safe
492  (A: Type[0]) (B: Type[0]) (left: list A) (right: list B) (prf: |left| = |right|)
493    on left ≝
494  match left return λx. |x| = |right| → list (A × B) with
495  [ nil ⇒ λnil_prf.
496    match right return λx. |[ ]| = |x| → list (A × B) with
497    [ nil ⇒ λnil_nil_prf. [ ]
498    | cons hd tl ⇒ λnil_cons_absrd. ?
499    ] nil_prf
500  | cons hd tl ⇒ λcons_prf.
501    match right return λx. |hd::tl| = |x| → list (A × B) with
502    [ nil ⇒ λcons_nil_absrd. ?
503    | cons hd' tl' ⇒ λcons_cons_prf. 〈hd, hd'〉 :: zip_safe A B tl tl' ?
504    ] cons_prf
505  ] prf.
506  [ 1: normalize in nil_cons_absrd;
507       destruct(nil_cons_absrd)
508  | 2: normalize in cons_nil_absrd;
509       destruct(cons_nil_absrd)
510  | 3: normalize in cons_cons_prf;
511       @injective_S
512       assumption
513  ]
514qed.
515
516let rec zip (A: Type[0]) (B: Type[0]) (l: list A) (r: list B) on l: option (list (A × B)) ≝
517  match l with
518  [ nil ⇒ Some ? (nil (A × B))
519  | cons hd tl ⇒
520    match r with
521    [ nil ⇒ None ?
522    | cons hd' tl' ⇒
523      match zip ? ? tl tl' with
524      [ None ⇒ None ?
525      | Some tail ⇒ Some ? (〈hd, hd'〉 :: tail)
526      ]
527    ]
528  ].
529
530let rec foldl (A: Type[0]) (B: Type[0]) (f: A → B → A) (a: A) (l: list B) on l ≝
531  match l with
532  [ nil ⇒ a
533  | cons hd tl ⇒ foldl A B f (f a hd) tl
534  ].
535
536lemma foldl_step:
537 ∀A:Type[0].
538  ∀B: Type[0].
539   ∀H: A → B → A.
540    ∀acc: A.
541     ∀pre: list B.
542      ∀hd:B.
543       foldl A B H acc (pre@[hd]) = (H (foldl A B H acc pre) hd).
544 #A #B #H #acc #pre generalize in match acc; -acc; elim pre
545  [ normalize; //
546  | #hd #tl #IH #acc #X normalize; @IH ]
547qed.
548
549lemma foldl_append:
550 ∀A:Type[0].
551  ∀B: Type[0].
552   ∀H: A → B → A.
553    ∀acc: A.
554     ∀suff,pre: list B.
555      foldl A B H acc (pre@suff) = (foldl A B H (foldl A B H acc pre) suff).
556 #A #B #H #acc #suff elim suff
557  [ #pre >append_nil %
558  | #hd #tl #IH #pre whd in ⊢ (???%); <(foldl_step … H ??) applyS (IH (pre@[hd])) ]
559qed.
560
561(* redirecting to library reverse *)
562definition rev ≝ reverse.
563
564lemma append_length:
565  ∀A: Type[0].
566  ∀l, r: list A.
567    |(l @ r)| = |l| + |r|.
568  #A #L #R
569  elim L
570  [ %
571  | #HD #TL #IH
572    normalize >IH %
573  ]
574qed.
575
576lemma append_nil:
577  ∀A: Type[0].
578  ∀l: list A.
579    l @ [ ] = l.
580  #A #L
581  elim L //
582qed.
583
584lemma rev_append:
585  ∀A: Type[0].
586  ∀l, r: list A.
587    rev A (l @ r) = rev A r @ rev A l.
588  #A #L #R
589  elim L
590  [ normalize >append_nil %
591  | #HD #TL normalize #IH
592    >rev_append_def
593    >rev_append_def
594    >rev_append_def
595    >append_nil
596    normalize
597    >IH
598    @associative_append
599  ]
600qed.
601
602lemma rev_length:
603  ∀A: Type[0].
604  ∀l: list A.
605    |rev A l| = |l|.
606  #A #L
607  elim L
608  [ %
609  | #HD #TL normalize #IH
610    >rev_append_def
611    >(append_length A (rev A TL) [HD])
612    normalize /2 by /
613  ]
614qed.
615
616lemma nth_append_first:
617 ∀A:Type[0].
618 ∀n:nat.∀l1,l2:list A.∀d:A.
619   n < |l1| → nth n A (l1@l2) d = nth n A l1 d.
620 #A #n #l1 #l2 #d
621 generalize in match n; -n; elim l1
622 [ normalize #k #Hk @⊥ @(absurd … Hk) @not_le_Sn_O
623 | #h #t #Hind #k normalize
624   cases k -k
625   [ #Hk normalize @refl
626   | #k #Hk normalize @(Hind k) @le_S_S_to_le @Hk
627   ] 
628 ]
629qed.
630
631lemma nth_append_second:
632 ∀A: Type[0].∀n.∀l1,l2:list A.∀d.n ≥ length A l1 ->
633  nth n A (l1@l2) d = nth (n - length A l1) A l2 d.
634 #A #n #l1 #l2 #d
635 generalize in match n; -n; elim l1
636 [ normalize #k #Hk <(minus_n_O) @refl
637 | #h #t #Hind #k normalize
638   cases k -k;
639   [ #Hk @⊥ @(absurd (S (|t|) ≤ 0)) [ @Hk | @not_le_Sn_O ]
640   | #k #Hk normalize @(Hind k) @le_S_S_to_le @Hk
641   ]
642 ]
643qed.
644
645   
646let rec fold_left_i_aux (A: Type[0]) (B: Type[0])
647                        (f: nat → A → B → A) (x: A) (i: nat) (l: list B) on l ≝
648  match l with
649    [ nil ⇒ x
650    | cons hd tl ⇒ fold_left_i_aux A B f (f i x hd) (S i) tl
651    ].
652
653definition fold_left_i ≝ λA,B,f,x. fold_left_i_aux A B f x O.
654
655notation "hvbox(t⌈o ↦ h⌉)"
656  with precedence 45
657  for @{ match (? : $o=$h) with [ refl ⇒ $t ] }.
658
659definition function_apply ≝
660  λA, B: Type[0].
661  λf: A → B.
662  λa: A.
663    f a.
664   
665notation "f break $ x"
666  left associative with precedence 99
667  for @{ 'function_apply $f $x }.
668 
669interpretation "Function application" 'function_apply f x = (function_apply ? ? f x).
670
671let rec iterate (A: Type[0]) (f: A → A) (a: A) (n: nat) on n ≝
672  match n with
673    [ O ⇒ a
674    | S o ⇒ f (iterate A f a o)
675    ].
676
677let rec division_aux (m: nat) (n : nat) (p: nat) ≝
678  match ltb n (S p) with
679    [ true ⇒ O
680    | false ⇒
681      match m with
682        [ O ⇒ O
683        | (S q) ⇒ S (division_aux q (n - (S p)) p)
684        ]
685    ].
686   
687definition division ≝
688  λm, n: nat.
689    match n with
690      [ O ⇒ S m
691      | S o ⇒ division_aux m m o
692      ].
693     
694notation "hvbox(n break ÷ m)"
695  right associative with precedence 47
696  for @{ 'division $n $m }.
697 
698interpretation "Nat division" 'division n m = (division n m).
699
700let rec modulus_aux (m: nat) (n: nat) (p: nat) ≝
701  match leb n p with
702    [ true ⇒ n
703    | false ⇒
704      match m with
705        [ O ⇒ n
706        | S o ⇒ modulus_aux o (n - (S p)) p
707        ]
708    ].
709   
710definition modulus ≝
711  λm, n: nat.
712    match n with
713      [ O ⇒ m
714      | S o ⇒ modulus_aux m m o
715      ].
716   
717notation "hvbox(n break 'mod' m)"
718  right associative with precedence 47
719  for @{ 'modulus $n $m }.
720 
721interpretation "Nat modulus" 'modulus m n = (modulus m n).
722
723definition divide_with_remainder ≝
724  λm, n: nat.
725    mk_Prod … (m ÷ n) (modulus m n).
726   
727let rec exponential (m: nat) (n: nat) on n ≝
728  match n with
729    [ O ⇒ S O
730    | S o ⇒ m * exponential m o
731    ].
732
733interpretation "Nat exponential" 'exp n m = (exponential n m).
734   
735notation "hvbox(a break ⊎ b)"
736 left associative with precedence 55
737for @{ 'disjoint_union $a $b }.
738interpretation "sum" 'disjoint_union A B = (Sum A B).
739
740theorem less_than_or_equal_monotone:
741  ∀m, n: nat.
742    m ≤ n → (S m) ≤ (S n).
743 #m #n #H
744 elim H
745 /2 by le_n, le_S/
746qed.
747
748theorem less_than_or_equal_b_complete:
749  ∀m, n: nat.
750    leb m n = false → ¬(m ≤ n).
751 #m;
752 elim m;
753 normalize
754 [ #n #H
755   destruct
756 | #y #H1 #z
757   cases z
758   normalize
759   [ #H
760     /2 by /
761   | /3 by not_le_to_not_le_S_S/
762   ]
763 ]
764qed.
765
766theorem less_than_or_equal_b_correct:
767  ∀m, n: nat.
768    leb m n = true → m ≤ n.
769 #m
770 elim m
771 //
772 #y #H1 #z
773 cases z
774 normalize
775 [ #H
776   destruct
777 | #n #H lapply (H1 … H) /2 by le_S_S/
778 ]
779qed.
780
781definition less_than_or_equal_b_elim:
782 ∀m, n: nat.
783 ∀P: bool → Type[0].
784   (m ≤ n → P true) → (¬(m ≤ n) → P false) → P (leb m n).
785 #m #n #P #H1 #H2;
786 lapply (less_than_or_equal_b_correct m n)
787 lapply (less_than_or_equal_b_complete m n)
788 cases (leb m n)
789 /3 by /
790qed.
791
792lemma lt_m_n_to_exists_o_plus_m_n:
793  ∀m, n: nat.
794    m < n → ∃o: nat. m + o = n.
795  #m #n
796  cases m
797  [1:
798    #irrelevant
799    %{n} %
800  |2:
801    #m' #lt_hyp
802    %{(n - S m')}
803    >commutative_plus in ⊢ (??%?);
804    <plus_minus_m_m
805    [1:
806      %
807    |2:
808      @lt_S_to_lt
809      assumption
810    ]
811  ]
812qed.
813
814lemma lt_O_n_to_S_pred_n_n:
815  ∀n: nat.
816    0 < n → S (pred n) = n.
817  #n
818  cases n
819  [1:
820    #absurd
821    cases(lt_to_not_zero 0 0 absurd)
822  |2:
823    #n' #lt_hyp %
824  ]
825qed.
826
827lemma exists_plus_m_n_to_exists_Sn:
828  ∀m, n: nat.
829    m < n → ∃p: nat. S p = n.
830  #m #n
831  cases m
832  [1:
833    #lt_hyp %{(pred n)}
834    @(lt_O_n_to_S_pred_n_n … lt_hyp)
835  |2:
836    #m' #lt_hyp %{(pred n)}
837    @(lt_O_n_to_S_pred_n_n)
838    @(transitive_le … (S m') …)
839    [1:
840      //
841    |2:
842      @lt_S_to_lt
843      assumption
844    ]
845  ]
846qed.
847
848lemma plus_right_monotone:
849  ∀m, n, o: nat.
850    m = n → m + o = n + o.
851  #m #n #o #refl >refl %
852qed.
853
854lemma plus_left_monotone:
855  ∀m, n, o: nat.
856    m = n → o + m = o + n.
857  #m #n #o #refl destruct %
858qed.
859
860lemma minus_plus_cancel:
861  ∀m, n : nat.
862  ∀proof: n ≤ m.
863    (m - n) + n = m.
864  #m #n #proof /2 by plus_minus/
865qed.
866
867lemma lt_to_le_to_le:
868  ∀n, m, p: nat.
869    n < m → m ≤ p → n ≤ p.
870  #n #m #p #H #H1
871  elim H
872  [1:
873    @(transitive_le n m p) /2/
874  |2:
875    /2/
876  ]
877qed.
878
879lemma eqb_decidable:
880  ∀l, r: nat.
881    (eqb l r = true) ∨ (eqb l r = false).
882  #l #r //
883qed.
884
885lemma r_Sr_and_l_r_to_Sl_r:
886  ∀r, l: nat.
887    (∃r': nat. r = S r' ∧ l = r') → S l = r.
888  #r #l #exists_hyp
889  cases exists_hyp #r'
890  #and_hyp cases and_hyp
891  #left_hyp #right_hyp
892  destruct %
893qed.
894
895lemma eqb_Sn_to_exists_n':
896  ∀m, n: nat.
897    eqb (S m) n = true → ∃n': nat. n = S n'.
898  #m #n
899  cases n
900  [1:
901    normalize #absurd
902    destruct(absurd)
903  |2:
904    #n' #_ %{n'} %
905  ]
906qed.
907
908lemma eqb_true_to_eqb_S_S_true:
909  ∀m, n: nat.
910    eqb m n = true → eqb (S m) (S n) = true.
911  #m #n normalize #assm assumption
912qed.
913
914lemma eqb_S_S_true_to_eqb_true:
915  ∀m, n: nat.
916    eqb (S m) (S n) = true → eqb m n = true.
917  #m #n normalize #assm assumption
918qed.
919
920lemma eqb_true_to_refl:
921  ∀l, r: nat.
922    eqb l r = true → l = r.
923  #l
924  elim l
925  [1:
926    #r cases r
927    [1:
928      #_ %
929    |2:
930      #l' normalize
931      #absurd destruct(absurd)
932    ]
933  |2:
934    #l' #inductive_hypothesis #r
935    #eqb_refl @r_Sr_and_l_r_to_Sl_r
936    %{(pred r)} @conj
937    [1:
938      cases (eqb_Sn_to_exists_n' … eqb_refl)
939      #r' #S_assm >S_assm %
940    |2:
941      cases (eqb_Sn_to_exists_n' … eqb_refl)
942      #r' #refl_assm destruct normalize
943      @inductive_hypothesis
944      normalize in eqb_refl; assumption
945    ]
946  ]
947qed.
948
949lemma r_O_or_exists_r_r_Sr_and_l_neq_r_to_Sl_neq_r:
950  ∀r, l: nat.
951    r = O ∨ (∃r': nat. r = S r' ∧ l ≠ r') → S l ≠ r.
952  #r #l #disj_hyp
953  cases disj_hyp
954  [1:
955    #r_O_refl destruct @nmk
956    #absurd destruct(absurd)
957  |2:
958    #exists_hyp cases exists_hyp #r'
959    #conj_hyp cases conj_hyp #left_conj #right_conj
960    destruct @nmk #S_S_refl_hyp
961    elim right_conj #hyp @hyp //
962  ]
963qed.
964
965lemma neq_l_r_to_neq_Sl_Sr:
966  ∀l, r: nat.
967    l ≠ r → S l ≠ S r.
968  #l #r #l_neq_r_assm
969  @nmk #Sl_Sr_assm cases l_neq_r_assm
970  #assm @assm //
971qed.
972
973lemma eqb_false_to_not_refl:
974  ∀l, r: nat.
975    eqb l r = false → l ≠ r.
976  #l
977  elim l
978  [1:
979    #r cases r
980    [1:
981      normalize #absurd destruct(absurd)
982    |2:
983      #r' #_ @nmk
984      #absurd destruct(absurd)
985    ]
986  |2:
987    #l' #inductive_hypothesis #r
988    cases r
989    [1:
990      #eqb_false_assm
991      @r_O_or_exists_r_r_Sr_and_l_neq_r_to_Sl_neq_r
992      @or_introl %
993    |2:
994      #r' #eqb_false_assm
995      @neq_l_r_to_neq_Sl_Sr
996      @inductive_hypothesis
997      assumption
998    ]
999  ]
1000qed.
1001
1002lemma le_to_lt_or_eq:
1003  ∀m, n: nat.
1004    m ≤ n → m = n ∨ m < n.
1005  #m #n #le_hyp
1006  cases le_hyp
1007  [1:
1008    @or_introl %
1009  |2:
1010    #m' #le_hyp'
1011    @or_intror
1012    normalize
1013    @le_S_S assumption
1014  ]
1015qed.
1016
1017lemma le_neq_to_lt:
1018  ∀m, n: nat.
1019    m ≤ n → m ≠ n → m < n.
1020  #m #n #le_hyp #neq_hyp
1021  cases neq_hyp
1022  #eq_absurd_hyp
1023  generalize in match (le_to_lt_or_eq m n le_hyp);
1024  #disj_assm cases disj_assm
1025  [1:
1026    #absurd cases (eq_absurd_hyp absurd)
1027  |2:
1028    #assm assumption
1029  ]
1030qed.
1031
1032inverter nat_jmdiscr for nat.
1033
1034lemma plus_lt_to_lt:
1035  ∀m, n, o: nat.
1036    m + n < o → m < o.
1037  #m #n #o
1038  elim n
1039  [1:
1040    <(plus_n_O m) in ⊢ (% → ?);
1041    #assumption assumption
1042  |2:
1043    #n' #inductive_hypothesis
1044    <(plus_n_Sm m n') in ⊢ (% → ?);
1045    #assm @inductive_hypothesis
1046    normalize in assm; normalize
1047    /2 by lt_S_to_lt/
1048  ]
1049qed.
1050
1051include "arithmetics/div_and_mod.ma".
1052
1053lemma n_plus_1_n_to_False:
1054  ∀n: nat.
1055    n + 1 = n → False.
1056  #n elim n
1057  [1:
1058    normalize #absurd destruct(absurd)
1059  |2:
1060    #n' #inductive_hypothesis normalize
1061    #absurd @inductive_hypothesis /2 by injective_S/
1062  ]
1063qed.
1064
1065lemma one_two_times_n_to_False:
1066  ∀n: nat.
1067    1=2*n→False.
1068  #n cases n
1069  [1:
1070    normalize #absurd destruct(absurd)
1071  |2:
1072    #n' normalize #absurd
1073    lapply (injective_S … absurd) -absurd #absurd
1074    /2/
1075  ]
1076qed.
1077
1078let rec odd_p
1079  (n: nat)
1080    on n ≝
1081  match n with
1082  [ O ⇒ False
1083  | S n' ⇒ even_p n'
1084  ]
1085and even_p
1086  (n: nat)
1087    on n ≝
1088  match n with
1089  [ O ⇒ True
1090  | S n' ⇒ odd_p n'
1091  ].
1092
1093let rec n_even_p_to_n_plus_2_even_p
1094  (n: nat)
1095    on n: even_p n → even_p (n + 2) ≝
1096  match n with
1097  [ O ⇒ ?
1098  | S n' ⇒ ?
1099  ]
1100and n_odd_p_to_n_plus_2_odd_p
1101  (n: nat)
1102    on n: odd_p n → odd_p (n + 2) ≝
1103  match n with
1104  [ O ⇒ ?
1105  | S n' ⇒ ?
1106  ].
1107  [1,3:
1108    normalize #assm assumption
1109  |2:
1110    normalize @n_odd_p_to_n_plus_2_odd_p
1111  |4:
1112    normalize @n_even_p_to_n_plus_2_even_p
1113  ]
1114qed.
1115
1116let rec two_times_n_even_p
1117  (n: nat)
1118    on n: even_p (2 * n) ≝
1119  match n with
1120  [ O ⇒ ?
1121  | S n' ⇒ ?
1122  ]
1123and two_times_n_plus_one_odd_p
1124  (n: nat)
1125    on n: odd_p ((2 * n) + 1) ≝
1126  match n with
1127  [ O ⇒ ?
1128  | S n' ⇒ ?
1129  ].
1130  [1,3:
1131    normalize @I
1132  |2:
1133    normalize
1134    >plus_n_Sm
1135    <(associative_plus n' n' 1)
1136    >(plus_n_O (n' + n'))
1137    cut(n' + n' + 0 + 1 = 2 * n' + 1)
1138    [1:
1139      //
1140    |2:
1141      #refl_assm >refl_assm
1142      @two_times_n_plus_one_odd_p     
1143    ]
1144  |4:
1145    normalize
1146    >plus_n_Sm
1147    cut(n' + (n' + 1) + 1 = (2 * n') + 2)
1148    [1:
1149      normalize /2/
1150    |2:
1151      #refl_assm >refl_assm
1152      @n_even_p_to_n_plus_2_even_p
1153      @two_times_n_even_p
1154    ]
1155  ]
1156qed.
1157
1158include alias "basics/logic.ma".
1159
1160let rec even_p_to_not_odd_p
1161  (n: nat)
1162    on n: even_p n → ¬ odd_p n ≝
1163  match n with
1164  [ O ⇒ ?
1165  | S n' ⇒ ?
1166  ]
1167and odd_p_to_not_even_p
1168  (n: nat)
1169    on n: odd_p n → ¬ even_p n ≝
1170  match n with
1171  [ O ⇒ ?
1172  | S n' ⇒ ?
1173  ].
1174  [1:
1175    normalize #_
1176    @nmk #assm assumption
1177  |3:
1178    normalize #absurd
1179    cases absurd
1180  |2:
1181    normalize
1182    @odd_p_to_not_even_p
1183  |4:
1184    normalize
1185    @even_p_to_not_odd_p
1186  ]
1187qed.
1188
1189lemma even_p_odd_p_cases:
1190  ∀n: nat.
1191    even_p n ∨ odd_p n.
1192  #n elim n
1193  [1:
1194    normalize @or_introl @I
1195  |2:
1196    #n' #inductive_hypothesis
1197    normalize
1198    cases inductive_hypothesis
1199    #assm
1200    try (@or_introl assumption)
1201    try (@or_intror assumption)
1202  ]
1203qed.
1204
1205lemma two_times_n_plus_one_refl_two_times_n_to_False:
1206  ∀m, n: nat.
1207    2 * m + 1 = 2 * n → False.
1208  #m #n
1209  #assm
1210  cut (even_p (2 * n) ∧ even_p ((2 * m) + 1))
1211  [1:
1212    >assm
1213    @conj
1214    @two_times_n_even_p
1215  |2:
1216    * #_ #absurd
1217    cases (even_p_to_not_odd_p … absurd)
1218    #assm @assm
1219    @two_times_n_plus_one_odd_p
1220  ]
1221qed.
1222
1223lemma not_Some_neq_None_to_False:
1224  ∀a: Type[0].
1225  ∀e: a.
1226    ¬ (Some a e ≠ None a) → False.
1227  #a #e #absurd cases absurd -absurd
1228  #absurd @absurd @nmk -absurd
1229  #absurd destruct(absurd)
1230qed.
1231
1232lemma not_None_to_Some:
1233  ∀A: Type[0].
1234  ∀o: option A.
1235    o ≠ None A → ∃v: A. o = Some A v.
1236  #A #o cases o
1237  [1:
1238    #absurd cases absurd #absurd' cases (absurd' (refl …))
1239  |2:
1240    #v' #ignore /2/
1241  ]
1242qed.
1243
1244lemma inclusive_disjunction_true:
1245  ∀b, c: bool.
1246    (orb b c) = true → b = true ∨ c = true.
1247  # b
1248  # c
1249  elim b
1250  [ normalize
1251    # H
1252    @ or_introl
1253    %
1254  | normalize
1255    /3 by trans_eq, orb_true_l/
1256  ]
1257qed.
1258
1259lemma conjunction_true:
1260  ∀b, c: bool.
1261    andb b c = true → b = true ∧ c = true.
1262  # b
1263  # c
1264  elim b
1265  normalize
1266  [ /2 by conj/
1267  | # K
1268    destruct
1269  ]
1270qed.
1271
1272lemma eq_true_false: false=true → False.
1273 # K
1274 destruct
1275qed.
1276
1277lemma inclusive_disjunction_b_true: ∀b. orb b true = true.
1278 # b
1279 cases b
1280 %
1281qed.
1282
1283(* XXX: to be moved into logic/basics.ma *)
1284lemma and_intro_right:
1285  ∀a, b: Prop.
1286    a → (a → b) → a ∧ b.
1287  #a #b /3/
1288qed.
1289
1290definition bool_to_Prop ≝
1291 λb. match b with [ true ⇒ True | false ⇒ False ].
1292
1293coercion bool_to_Prop: ∀b:bool. Prop ≝ bool_to_Prop on _b:bool to Type[0].
1294
1295lemma bool_as_Prop_to_eq : ∀b : bool. b → b = true.
1296**%
1297qed.
1298
1299(* with this you can use prf : b with b : bool with rewriting
1300   >prf rewrites b as true *)
1301coercion bool_to_Prop_to_eq : ∀b : bool.∀prf : b.b = true
1302 ≝ bool_as_Prop_to_eq on _prf : bool_to_Prop ? to (? = true).
1303
1304lemma andb_Prop : ∀b,d : bool.b → d → b∧d.
1305#b #d #btrue #dtrue >btrue >dtrue %
1306qed.
1307
1308lemma andb_Prop_true : ∀b,d : bool. (b∧d) → And (bool_to_Prop b) (bool_to_Prop d).
1309#b #d #bdtrue elim (andb_true … bdtrue) #btrue #dtrue >btrue >dtrue % %
1310qed.
1311
1312lemma orb_Prop_l : ∀b,d : bool.b → b∨d.
1313#b #d #btrue >btrue %
1314qed.
1315
1316lemma orb_Prop_r : ∀b,d : bool.d → b∨d.
1317#b #d #dtrue >dtrue elim b %
1318qed.
1319
1320lemma orb_Prop_true : ∀b,d : bool. (b∨d) → Or (bool_to_Prop b) (bool_to_Prop d).
1321#b #d #bdtrue elim (orb_true_l … bdtrue) #xtrue >xtrue [%1 | %2] %
1322qed.
1323
1324lemma notb_Prop : ∀b : bool. Not (bool_to_Prop b) → notb b.
1325* * #H [@H % | %]
1326qed.
1327
1328lemma eq_false_to_notb: ∀b. b = false → ¬ b.
1329 *; /2 by eq_true_false, I/
1330qed.
1331
1332lemma not_b_to_eq_false : ∀b : bool. Not (bool_to_Prop b) → b = false.
1333** #H [elim (H ?) % | %]
1334qed.
1335
1336(* with this you can use prf : ¬b with b : bool with rewriting
1337   >prf rewrites b as false *)
1338coercion not_bool_to_Prop_to_eq : ∀b : bool.∀prf : Not (bool_to_Prop b).b = false
1339 ≝ not_b_to_eq_false on _prf : Not (bool_to_Prop ?) to (? = false).
1340
1341
1342lemma true_or_false_Prop : ∀b : bool.Or (bool_to_Prop b) (¬(bool_to_Prop b)).
1343* [%1 % | %2 % *]
1344qed.
1345
1346lemma eq_true_to_b : ∀b. b = true → b.
1347#b #btrue >btrue %
1348qed.
1349
1350definition if_then_else_safe : ∀A : Type[0].∀b : bool.(b → A) → (¬(bool_to_Prop b) → A) → A ≝
1351  λA,b,f,g.
1352  match b return λx.match x with [true ⇒ bool_to_Prop b | false ⇒ ¬bool_to_Prop b] → A with
1353  [ true ⇒ f
1354  | false ⇒ g
1355  ] ?. elim b % *
1356qed.
1357
1358notation > "'If' b 'then' 'with' ident prf1 'do' f 'else' 'with' ident prf2 'do' g" with precedence 46 for
1359  @{'if_then_else_safe $b (λ${ident prf1}.$f) (λ${ident prf2}.$g)}.
1360notation > "'If' b 'then' 'with' ident prf1 : ty1 'do' f 'else' 'with' ident prf2 : ty2 'do' g" with precedence 46 for
1361  @{'if_then_else_safe $b (λ${ident prf1}:$ty1.$f) (λ${ident prf2}:$ty2.$g)}.
1362notation > "'If' b 'then' f 'else' 'with' ident prf2 'do' g" with precedence 46 for
1363  @{'if_then_else_safe $b (λ_.$f) (λ${ident prf2}.$g)}.
1364notation > "'If' b 'then' f 'else' 'with' ident prf2 : ty2 'do' g" with precedence 46 for
1365  @{'if_then_else_safe $b (λ_.$f) (λ${ident prf2}:$ty2.$g)}.
1366notation > "'If' b 'then' 'with' ident prf1 'do' f 'else' g" with precedence 46 for
1367  @{'if_then_else_safe $b (λ${ident prf1}.$f) (λ_.$g)}.
1368notation > "'If' b 'then' 'with' ident prf1 : ty1 'do' f 'else' g" with precedence 46 for
1369  @{'if_then_else_safe $b (λ${ident prf1}:$ty1.$f) (λ_.$g)}.
1370
1371notation < "hvbox('If' \nbsp b \nbsp 'then' \nbsp break 'with' \nbsp ident prf1 : ty1 \nbsp 'do' \nbsp break f \nbsp break 'else' \nbsp break 'with' \nbsp ident prf2 : ty2 \nbsp 'do' \nbsp break g)" with precedence 46 for
1372  @{'if_then_else_safe $b (λ${ident prf1}:$ty1.$f) (λ${ident prf2}:$ty2.$g)}.
1373notation < "hvbox('If' \nbsp b \nbsp 'then' \nbsp break f \nbsp break 'else' \nbsp break 'with' \nbsp ident prf2 : ty2 \nbsp 'do' \nbsp break g)" with precedence 46 for
1374  @{'if_then_else_safe $b (λ_:$ty1.$f) (λ${ident prf2}:$ty2.$g)}.
1375notation < "hvbox('If' \nbsp b \nbsp 'then' \nbsp break 'with' \nbsp ident prf1 : ty1 \nbsp 'do' \nbsp break f \nbsp break 'else' \nbsp break g)" with precedence 46 for
1376  @{'if_then_else_safe $b (λ${ident prf1}:$ty1.$f) (λ_:$ty2.$g)}.
1377 
1378interpretation "dependent if then else" 'if_then_else_safe b f g = (if_then_else_safe ? b f g).
1379
1380(* Also extracts an equality proof (useful when not using Russell). *)
1381notation > "hvbox('let' 〈ident x,ident y〉 'as' ident E 'return' ty ≝ t 'in' s)"
1382 with precedence 10
1383for @{ match $t return λx.∀${ident E}: x = $t. $ty with [ mk_Prod ${ident x} ${ident y} ⇒
1384        λ${ident E}.$s ] (refl ? $t) }.
1385
1386notation > "hvbox('deplet' 〈ident x,ident y〉 'as' ident E  ≝ t 'in' s)"
1387 with precedence 10
1388for @{ match $t return λx.∀${ident E}: x = $t. Σz: ?. ? with [ mk_Prod ${ident x} ${ident y} ⇒
1389        λ${ident E}.$s ] (refl ? $t) }.
1390
1391notation > "hvbox('deplet' 〈ident x,ident y,ident z〉 'as' ident E ≝ t 'in' s)"
1392 with precedence 10
1393for @{ match $t return λx.∀${fresh w}:x = $t. Σq: ?. ? with [ mk_Prod ${fresh xy} ${ident z} ⇒
1394       match ${fresh xy} return λx.∀${ident E}:? = $t. Σu: ?. ? with [ mk_Prod ${ident x} ${ident y} ⇒
1395        λ${ident E}.$s ] ] (refl ? $t) }.
1396
1397notation > "hvbox('let' 〈ident x,ident y,ident z〉 'as' ident E 'return' ty ≝ t 'in' s)"
1398 with precedence 10
1399for @{ match $t return λx.∀${fresh w}:x = $t. Σq: ?. ? with [ mk_Prod ${fresh xy} ${ident z} ⇒
1400       match ${fresh xy} return λx.∀${ident E}:? = $t. $ty with [ mk_Prod ${ident x} ${ident y} ⇒
1401        λ${ident E}.$s ] ] (refl ? $t) }.
1402
1403lemma length_append:
1404 ∀A.∀l1,l2:list A.
1405  |l1 @ l2| = |l1| + |l2|.
1406 #A #l1 elim l1
1407  [ //
1408  | #hd #tl #IH #l2 normalize <IH //]
1409qed.
1410
1411lemma nth_cons:
1412  ∀n,A,h,t,y.
1413  nth (S n) A (h::t) y = nth n A t y.
1414 #n #A #h #t #y /2 by refl/
1415qed.
1416
1417lemma option_destruct_Some: ∀A,a,b. Some A a = Some A b → a=b.
1418 #A #a #b #EQ destruct //
1419qed.
1420
1421lemma pi1_eq: ∀A:Type[0].∀P:A->Prop.∀s1:Σa1:A.P a1.∀s2:Σa2:A.P a2.
1422  s1 = s2 → (pi1 ?? s1) = (pi1 ?? s2).
1423 #A #P #s1 #s2 / by /
1424qed.
1425
1426lemma Some_eq:
1427 ∀T:Type[0].∀x,y:T. Some T x = Some T y → x = y.
1428 #T #x #y #H @option_destruct_Some @H
1429qed.
1430
1431lemma not_neq_None_to_eq : ∀A.∀a : option A.¬a≠None ? → a = None ?.
1432#A * [2: #a] * // #ABS elim (ABS ?) % #ABS' destruct(ABS')
1433qed.
1434
1435coercion not_neq_None : ∀A.∀a : option A.∀prf : ¬a≠None ?.a = None ? ≝
1436  not_neq_None_to_eq on _prf : ¬?≠None ? to ? = None ?.
Note: See TracBrowser for help on using the repository browser.