source: src/utilities/extranat.ma @ 1681

Last change on this file since 1681 was 1593, checked in by boender, 9 years ago
  • cleaned up Assembly, moved some definitions elsewhere
File size: 3.4 KB
Line 
1include "basics/types.ma".
2include "arithmetics/nat.ma".
3
4inductive nat_compared : nat → nat → Type[0] ≝
5| nat_lt : ∀n,m:nat. nat_compared n (n+S m)
6| nat_eq : ∀n:nat.   nat_compared n n
7| nat_gt : ∀n,m:nat. nat_compared (m+S n) m.
8
9let rec nat_compare (n:nat) (m:nat) : nat_compared n m ≝
10match n return λx. nat_compared x m with
11[ O ⇒ match m return λy. nat_compared O y with [ O ⇒ nat_eq ? | S m' ⇒ nat_lt ?? ]
12| S n' ⇒
13    match m return λy. nat_compared (S n') y with
14    [ O ⇒ nat_gt n' O
15    | S m' ⇒ match nat_compare n' m' return λx,y.λ_. nat_compared (S x) (S y) with
16             [ nat_lt x y ⇒ nat_lt ??
17             | nat_eq x ⇒ nat_eq ?
18             | nat_gt x y ⇒ nat_gt ? (S y)
19             ]
20    ]
21].
22
23lemma nat_compare_eq : ∀n. nat_compare n n = nat_eq n.
24#n elim n
25[ @refl
26| #m #IH whd in ⊢ (??%?); >IH @refl
27] qed.
28
29lemma nat_compare_lt : ∀n,m. nat_compare n (n+S m) = nat_lt n m.
30#n #m elim n
31[ //
32| #n' #IH whd in ⊢ (??%?); >IH @refl
33] qed.
34
35lemma nat_compare_gt : ∀n,m. nat_compare (m+S n) m = nat_gt n m.
36#n #m elim m
37[ //
38| #m' #IH whd in ⊢ (??%?); >IH @refl
39] qed.
40
41
42let rec eq_nat_dec (n:nat) (m:nat) : Sum (n=m) (n≠m) ≝
43match n return λn.Sum (n=m) (n≠m) with
44[ O ⇒ match m return λm.Sum (O=m) (O≠m) with [O ⇒ inl ?? (refl ??) | S m' ⇒ inr ??? ]
45| S n' ⇒ match m return λm.Sum (S n'=m) (S n'≠m) with [O ⇒ inr ??? | S m' ⇒
46           match eq_nat_dec n' m' with [ inl E ⇒ inl ??? | inr NE ⇒ inr ??? ] ]
47].
48[ 1,2: % #E destruct
49| >E @refl
50| % #E destruct cases NE /2/
51] qed.
52
53lemma max_l : ∀m,n,o:nat. o ≤ m → o ≤ max m n.
54#m #n #o #H whd in ⊢ (??%); @leb_elim #H'
55[ @(transitive_le ? m ? H H')
56| @H
57] qed.
58
59lemma max_r : ∀m,n,o:nat. o ≤ n → o ≤ max m n.
60#m #n #o #H whd in ⊢ (??%); @leb_elim #H'
61[ @H
62| @(transitive_le … H) @(transitive_le … (not_le_to_lt … H')) //
63] qed.
64
65lemma le_S_to_le: ∀n,m:ℕ.S n ≤ m → n ≤ m.
66 /2/ qed.
67
68lemma le_plus_k:
69  ∀n,m:ℕ.n ≤ m → ∃k:ℕ.m = n + k.
70 #n #m elim m -m;
71 [ #Hn % [ @O | <(le_n_O_to_eq n Hn) // ]
72 | #m #Hind #Hn cases (le_to_or_lt_eq … Hn) -Hn; #Hn
73   [ elim (Hind (le_S_S_to_le … Hn)) #k #Hk % [ @(S k) | >Hk // ]
74   | % [ @O | <Hn // ]
75   ]
76 ]
77qed.
78
79lemma eq_plus_S_to_lt:
80  ∀n,m,p:ℕ.n = m + (S p) → m < n.
81 #n #m #p /2 by lt_plus_to_lt_l/
82qed.
83
84(* "Fast" proofs:  some proofs get reduced during normalization (in particular,
85   some functions which use a proof for rewriting are applied to constants and
86   get reduced during a proof or while matita is searching for a term;
87   they may also be normalized during testing), and so here are some more
88   efficient versions.  Perhaps they could be replaced using some kind of proof
89   irrelevance? *)
90
91let rec plus_n_Sm_fast (n:nat) on n : ∀m:nat. S (n+m) = n+S m ≝
92match n return λn'.∀m.S(n'+m) = n'+S m with
93[ O ⇒ λm.refl ??
94| S n' ⇒ λm. ?
95]. normalize @(match plus_n_Sm_fast n' m with [ refl ⇒ ? ]) @refl qed.
96
97let rec plus_n_O_faster (n:nat) : n = n + O ≝
98match n return λn.n=n+O with
99[ O ⇒ refl ??
100| S n' ⇒ match plus_n_O_faster n' return λx.λ_.S n'=S x with [ refl ⇒ refl ?? ]
101].
102
103let rec commutative_plus_faster (n,m:nat) : n+m = m+n ≝
104match n return λn.n+m = m+n with
105[ O ⇒ plus_n_O_faster ?
106| S n' ⇒ ?
107]. @(match plus_n_Sm_fast m n' return λx.λ_. ? = x with [ refl ⇒ ? ])
108@(match commutative_plus_faster n' m return λx.λ_.? = S x with [refl ⇒ ?]) @refl qed.
109
Note: See TracBrowser for help on using the repository browser.