source: src/ASM/Util.ma @ 1599

Last change on this file since 1599 was 1599, checked in by sacerdot, 9 years ago

Start of merging of stuff into the standard library of Matita.

File size: 19.6 KB
Line 
1include "basics/lists/list.ma".
2include "basics/types.ma".
3include "arithmetics/nat.ma".
4include "ASM/JMCoercions.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 [ ] }.
14
15definition ltb ≝
16  λm, n: nat.
17    leb (S m) n.
18   
19definition geb ≝
20  λm, n: nat.
21    ltb n m.
22
23definition gtb ≝
24  λm, n: nat.
25    ltb n m.
26
27(* dpm: unless I'm being stupid, this isn't defined in the stdlib? *)
28let rec eq_nat (n: nat) (m: nat) on n: bool ≝
29  match n with
30  [ O ⇒ match m with [ O ⇒ true | _ ⇒ false ]
31  | S n' ⇒ match m with [ S m' ⇒ eq_nat n' m' | _ ⇒ false ]
32  ].
33
34let rec forall
35  (A: Type[0]) (f: A → bool) (l: list A)
36    on l ≝
37  match l with
38  [ nil        ⇒ true
39  | cons hd tl ⇒ f hd ∧ forall A f tl
40  ].
41
42let rec prefix
43  (A: Type[0]) (k: nat) (l: list A)
44    on l ≝
45  match l with
46  [ nil ⇒ [ ]
47  | cons hd tl ⇒
48    match k with
49    [ O ⇒ [ ]
50    | S k' ⇒ hd :: prefix A k' tl
51    ]
52  ].
53 
54let rec fold_left2
55  (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C → A) (accu: A)
56  (left: list B) (right: list C) (proof: |left| = |right|)
57    on left: A ≝
58  match left return λx. |x| = |right| → A with
59  [ nil ⇒ λnil_prf.
60    match right return λx. |[ ]| = |x| → A with
61    [ nil ⇒ λnil_nil_prf. accu
62    | cons hd tl ⇒ λcons_nil_absrd. ?
63    ] nil_prf
64  | cons hd tl ⇒ λcons_prf.
65    match right return λx. |hd::tl| = |x| → A with
66    [ nil ⇒ λcons_nil_absrd. ?
67    | cons hd' tl' ⇒ λcons_cons_prf.
68        fold_left2 …  f (f accu hd hd') tl tl' ?
69    ] cons_prf
70  ] proof.
71  [ 1: normalize in cons_nil_absrd;
72       destruct(cons_nil_absrd)
73  | 2: normalize in cons_nil_absrd;
74       destruct(cons_nil_absrd)
75  | 3: normalize in cons_cons_prf;
76       @injective_S
77       assumption
78  ]
79qed.
80
81let rec remove_n_first_internal
82  (i: nat) (A: Type[0]) (l: list A) (n: nat)
83    on l ≝
84  match l with
85  [ nil ⇒ [ ]
86  | cons hd tl ⇒
87    match eq_nat i n with
88    [ true ⇒ l
89    | _ ⇒ remove_n_first_internal (S i) A tl n
90    ]
91  ].
92
93definition remove_n_first ≝
94  λA: Type[0].
95  λn: nat.
96  λl: list A.
97    remove_n_first_internal 0 A l n.
98   
99let rec foldi_from_until_internal
100  (A: Type[0]) (i: nat) (res: ?) (rem: list A) (m: nat) (f: nat → list A → A → list A)
101    on rem ≝
102  match rem with
103  [ nil ⇒ res
104  | cons e tl ⇒
105    match geb i m with
106    [ true ⇒ res
107    | _ ⇒ foldi_from_until_internal A (S i) (f i res e) tl m f
108    ]
109  ].
110
111definition foldi_from_until ≝
112  λA: Type[0].
113  λn: nat.
114  λm: nat.
115  λf: ?.
116  λa: ?.
117  λl: ?.
118    foldi_from_until_internal A 0 a (remove_n_first A n l) m f.
119
120definition foldi_from ≝
121  λA: Type[0].
122  λn.
123  λf.
124  λa.
125  λl.
126    foldi_from_until A n (|l|) f a l.
127
128definition foldi_until ≝
129  λA: Type[0].
130  λm.
131  λf.
132  λa.
133  λl.
134    foldi_from_until A 0 m f a l.
135
136definition foldi ≝
137  λA: Type[0].
138  λf.
139  λa.
140  λl.
141    foldi_from_until A 0 (|l|) f a l.
142
143definition hd_safe ≝
144  λA: Type[0].
145  λl: list A.
146  λproof: 0 < |l|.
147  match l return λx. 0 < |x| → A with
148  [ nil ⇒ λnil_absrd. ?
149  | cons hd tl ⇒ λcons_prf. hd
150  ] proof.
151  normalize in nil_absrd;
152  cases(not_le_Sn_O 0)
153  #HYP
154  cases(HYP nil_absrd)
155qed.
156
157definition tail_safe ≝
158  λA: Type[0].
159  λl: list A.
160  λproof: 0 < |l|.
161  match l return λx. 0 < |x| → list A with
162  [ nil ⇒ λnil_absrd. ?
163  | cons hd tl ⇒ λcons_prf. tl
164  ] proof.
165  normalize in nil_absrd;
166  cases(not_le_Sn_O 0)
167  #HYP
168  cases(HYP nil_absrd)
169qed.
170
171let rec split
172  (A: Type[0]) (l: list A) (index: nat) (proof: index ≤ |l|)
173    on index ≝
174  match index return λx. x ≤ |l| → (list A) × (list A) with
175  [ O ⇒ λzero_prf. 〈[], l〉
176  | S index' ⇒ λsucc_prf.
177    match l return λx. S index' ≤ |x| → (list A) × (list A) with
178    [ nil ⇒ λnil_absrd. ?
179    | cons hd tl ⇒ λcons_prf.
180      let 〈l1, l2〉 ≝ split A tl index' ? in
181        〈hd :: l1, l2〉
182    ] succ_prf
183  ] proof.
184  [1: normalize in nil_absrd;
185      cases(not_le_Sn_O index')
186      #HYP
187      cases(HYP nil_absrd)
188  |2: normalize in cons_prf;
189      @le_S_S_to_le
190      assumption
191  ]
192qed.
193
194let rec nth_safe
195  (elt_type: Type[0]) (index: nat) (the_list: list elt_type)
196  (proof: index < | the_list |)
197    on index ≝
198  match index return λs. s < | the_list | → elt_type with
199  [ O ⇒
200    match the_list return λt. 0 < | t | → elt_type with
201    [ nil        ⇒ λnil_absurd. ?
202    | cons hd tl ⇒ λcons_proof. hd
203    ]
204  | S index' ⇒
205    match the_list return λt. S index' < | t | → elt_type with
206    [ nil ⇒ λnil_absurd. ?
207    | cons hd tl ⇒
208      λcons_proof. nth_safe elt_type index' tl ?
209    ]
210  ] proof.
211  [ normalize in nil_absurd;
212    cases (not_le_Sn_O 0)
213    #ABSURD
214    elim (ABSURD nil_absurd)
215  | normalize in nil_absurd;
216    cases (not_le_Sn_O (S index'))
217    #ABSURD
218    elim (ABSURD nil_absurd)
219  | normalize in cons_proof;
220    @le_S_S_to_le
221    assumption
222  ]
223qed.
224
225definition last_safe ≝
226  λelt_type: Type[0].
227  λthe_list: list elt_type.
228  λproof   : 0 < | the_list |.
229    nth_safe elt_type (|the_list| - 1) the_list ?.
230  normalize /2/
231qed.
232
233let rec reduce
234  (A: Type[0]) (B: Type[0]) (left: list A) (right: list B) on left ≝
235  match left with
236  [ nil ⇒ 〈〈[ ], [ ]〉, 〈[ ], right〉〉
237  | cons hd tl ⇒
238    match right with
239    [ nil ⇒ 〈〈[ ], left〉, 〈[ ], [ ]〉〉
240    | cons hd' tl' ⇒
241      let 〈cleft, cright〉 ≝ reduce A B tl tl' in
242      let 〈commonl, restl〉 ≝ cleft in
243      let 〈commonr, restr〉 ≝ cright in
244        〈〈hd :: commonl, restl〉, 〈hd' :: commonr, restr〉〉
245    ]
246  ].
247
248(*
249axiom reduce_strong:
250  ∀A: Type[0].
251  ∀left: list A.
252  ∀right: list A.
253    Σret: ((list A) × (list A)) × ((list A) × (list A)). | \fst (\fst ret) | = | \fst (\snd ret) |.
254*)
255
256let rec reduce_strong
257  (A: Type[0]) (B: Type[0]) (left: list A) (right: list B)
258    on left : Σret: ((list A) × (list A)) × ((list B) × (list B)). |\fst (\fst ret)| = |\fst (\snd ret)|  ≝
259  match left with
260  [ nil ⇒ 〈〈[ ], [ ]〉, 〈[ ], right〉〉
261  | cons hd tl ⇒
262    match right with
263    [ nil ⇒ 〈〈[ ], left〉, 〈[ ], [ ]〉〉
264    | cons hd' tl' ⇒
265      let 〈cleft, cright〉 ≝ reduce_strong A B tl tl' in
266      let 〈commonl, restl〉 ≝ cleft in
267      let 〈commonr, restr〉 ≝ cright in
268        〈〈hd :: commonl, restl〉, 〈hd' :: commonr, restr〉〉
269    ]
270  ].
271  [ 1: normalize %
272  | 2: normalize %
273  | 3: normalize
274       generalize in match (sig2 … (reduce_strong A B tl tl1));
275       >p2 >p3 >p4 normalize in ⊢ (% → ?);
276       #HYP //
277  ]
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
561definition flatten ≝
562  λA: Type[0].
563  λl: list (list A).
564    foldr ? ? (append ?) [ ] l.
565
566let rec rev (A: Type[0]) (l: list A) on l ≝
567  match l with
568  [ nil ⇒ nil A
569  | cons hd tl ⇒ (rev A tl) @ [ hd ]
570  ].
571
572lemma append_length:
573  ∀A: Type[0].
574  ∀l, r: list A.
575    |(l @ r)| = |l| + |r|.
576  #A #L #R
577  elim L
578  [ %
579  | #HD #TL #IH
580    normalize >IH %
581  ]
582qed.
583
584lemma append_nil:
585  ∀A: Type[0].
586  ∀l: list A.
587    l @ [ ] = l.
588  #A #L
589  elim L //
590qed.
591
592lemma rev_append:
593  ∀A: Type[0].
594  ∀l, r: list A.
595    rev A (l @ r) = rev A r @ rev A l.
596  #A #L #R
597  elim L
598  [ normalize >append_nil %
599  | #HD #TL #IH
600    normalize >IH
601    @associative_append
602  ]
603qed.
604
605lemma rev_length:
606  ∀A: Type[0].
607  ∀l: list A.
608    |rev A l| = |l|.
609  #A #L
610  elim L
611  [ %
612  | #HD #TL #IH
613    normalize
614    >(append_length A (rev A TL) [HD])
615    normalize /2/
616  ]
617qed.
618
619lemma nth_append_first:
620 ∀A:Type[0].
621 ∀n:nat.∀l1,l2:list A.∀d:A.
622   n < |l1| → nth n A (l1@l2) d = nth n A l1 d.
623 #A #n #l1 #l2 #d
624 generalize in match n; -n; elim l1
625 [ normalize #k #Hk @⊥ @(absurd … Hk) @not_le_Sn_O
626 | #h #t #Hind #k normalize
627   cases k -k
628   [ #Hk normalize @refl
629   | #k #Hk normalize @(Hind k) @le_S_S_to_le @Hk
630   ] 
631 ]
632qed.
633
634lemma nth_append_second:
635 ∀A: Type[0].∀n.∀l1,l2:list A.∀d.n ≥ length A l1 ->
636  nth n A (l1@l2) d = nth (n - length A l1) A l2 d.
637 #A #n #l1 #l2 #d
638 generalize in match n; -n; elim l1
639 [ normalize #k #Hk <(minus_n_O) @refl
640 | #h #t #Hind #k normalize
641   cases k -k;
642   [ #Hk @⊥ @(absurd (S (|t|) ≤ 0)) [ @Hk | @not_le_Sn_O ]
643   | #k #Hk normalize @(Hind k) @le_S_S_to_le @Hk
644   ]
645 ]
646qed.
647
648   
649let rec fold_left_i_aux (A: Type[0]) (B: Type[0])
650                        (f: nat → A → B → A) (x: A) (i: nat) (l: list B) on l ≝
651  match l with
652    [ nil ⇒ x
653    | cons hd tl ⇒ fold_left_i_aux A B f (f i x hd) (S i) tl
654    ].
655
656definition fold_left_i ≝ λA,B,f,x. fold_left_i_aux A B f x O.
657
658notation "hvbox(t⌈o ↦ h⌉)"
659  with precedence 45
660  for @{ match (? : $o=$h) with [ refl ⇒ $t ] }.
661
662definition function_apply ≝
663  λA, B: Type[0].
664  λf: A → B.
665  λa: A.
666    f a.
667   
668notation "f break $ x"
669  left associative with precedence 99
670  for @{ 'function_apply $f $x }.
671 
672interpretation "Function application" 'function_apply f x = (function_apply ? ? f x).
673
674let rec iterate (A: Type[0]) (f: A → A) (a: A) (n: nat) on n ≝
675  match n with
676    [ O ⇒ a
677    | S o ⇒ f (iterate A f a o)
678    ].
679
680let rec division_aux (m: nat) (n : nat) (p: nat) ≝
681  match ltb n (S p) with
682    [ true ⇒ O
683    | false ⇒
684      match m with
685        [ O ⇒ O
686        | (S q) ⇒ S (division_aux q (n - (S p)) p)
687        ]
688    ].
689   
690definition division ≝
691  λm, n: nat.
692    match n with
693      [ O ⇒ S m
694      | S o ⇒ division_aux m m o
695      ].
696     
697notation "hvbox(n break ÷ m)"
698  right associative with precedence 47
699  for @{ 'division $n $m }.
700 
701interpretation "Nat division" 'division n m = (division n m).
702
703let rec modulus_aux (m: nat) (n: nat) (p: nat) ≝
704  match leb n p with
705    [ true ⇒ n
706    | false ⇒
707      match m with
708        [ O ⇒ n
709        | S o ⇒ modulus_aux o (n - (S p)) p
710        ]
711    ].
712   
713definition modulus ≝
714  λm, n: nat.
715    match n with
716      [ O ⇒ m
717      | S o ⇒ modulus_aux m m o
718      ].
719   
720notation "hvbox(n break 'mod' m)"
721  right associative with precedence 47
722  for @{ 'modulus $n $m }.
723 
724interpretation "Nat modulus" 'modulus m n = (modulus m n).
725
726definition divide_with_remainder ≝
727  λm, n: nat.
728    mk_Prod … (m ÷ n) (modulus m n).
729   
730let rec exponential (m: nat) (n: nat) on n ≝
731  match n with
732    [ O ⇒ S O
733    | S o ⇒ m * exponential m o
734    ].
735
736interpretation "Nat exponential" 'exp n m = (exponential n m).
737   
738notation "hvbox(a break ⊎ b)"
739 left associative with precedence 50
740for @{ 'disjoint_union $a $b }.
741interpretation "sum" 'disjoint_union A B = (Sum A B).
742
743theorem less_than_or_equal_monotone:
744  ∀m, n: nat.
745    m ≤ n → (S m) ≤ (S n).
746 #m #n #H
747 elim H
748 /2/
749qed.
750
751theorem less_than_or_equal_b_complete:
752  ∀m, n: nat.
753    leb m n = false → ¬(m ≤ n).
754 #m;
755 elim m;
756 normalize
757 [ #n #H
758   destruct
759 | #y #H1 #z
760   cases z
761   normalize
762   [ #H
763     /2/
764   | /3/
765   ]
766 ]
767qed.
768
769theorem less_than_or_equal_b_correct:
770  ∀m, n: nat.
771    leb m n = true → m ≤ n.
772 #m
773 elim m
774 //
775 #y #H1 #z
776 cases z
777 normalize
778 [ #H
779   destruct
780 | #n #H lapply (H1 … H) /2/
781 ]
782qed.
783
784definition less_than_or_equal_b_elim:
785 ∀m, n: nat.
786 ∀P: bool → Type[0].
787   (m ≤ n → P true) → (¬(m ≤ n) → P false) → P (leb m n).
788 #m #n #P #H1 #H2;
789 lapply (less_than_or_equal_b_correct m n)
790 lapply (less_than_or_equal_b_complete m n)
791 cases (leb m n)
792 /3/
793qed.
794
795lemma inclusive_disjunction_true:
796  ∀b, c: bool.
797    (orb b c) = true → b = true ∨ c = true.
798  # b
799  # c
800  elim b
801  [ normalize
802    # H
803    @ or_introl
804    %
805  | normalize
806    /2/
807  ]
808qed.
809
810lemma conjunction_true:
811  ∀b, c: bool.
812    andb b c = true → b = true ∧ c = true.
813  # b
814  # c
815  elim b
816  normalize
817  [ /2 by conj/
818  | # K
819    destruct
820  ]
821qed.
822
823lemma eq_true_false: false=true → False.
824 # K
825 destruct
826qed.
827
828lemma inclusive_disjunction_b_true: ∀b. orb b true = true.
829 # b
830 cases b
831 %
832qed.
833
834definition bool_to_Prop ≝
835 λb. match b with [ true ⇒ True | false ⇒ False ].
836
837coercion bool_to_Prop: ∀b:bool. Prop ≝ bool_to_Prop on _b:bool to Type[0].
838
839lemma eq_false_to_notb: ∀b. b = false → ¬ b.
840 *; /2/
841qed.
842
843lemma length_append:
844 ∀A.∀l1,l2:list A.
845  |l1 @ l2| = |l1| + |l2|.
846 #A #l1 elim l1
847  [ //
848  | #hd #tl #IH #l2 normalize <IH //]
849qed.
Note: See TracBrowser for help on using the repository browser.