source: src/utilities/extranat.ma @ 2670

Last change on this file since 2670 was 2670, checked in by campbell, 7 years ago

Clean up from recent commits.

File size: 4.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 max_O_n : ∀n. max O n = n.
66* //
67qed.
68
69lemma max_n_O : ∀n. max n O = n.
70* //
71qed.
72
73lemma associative_max : associative nat max.
74#n #m #o normalize
75@(leb_elim n m)
76[ normalize @(leb_elim m o) normalize #H1 #H2
77  [ >(le_to_leb_true n o) /2/
78  | >(le_to_leb_true n m) //
79  ]
80| normalize @(leb_elim m o) normalize #H1 #H2
81  [ %
82  | >(not_le_to_leb_false … H2)
83    >(not_le_to_leb_false n o) // @lt_to_not_le @(transitive_lt … m) /2/
84  ]
85] qed.
86
87lemma le_S_to_le: ∀n,m:ℕ.S n ≤ m → n ≤ m.
88 /2/ qed.
89
90lemma le_plus_k:
91  ∀n,m:ℕ.n ≤ m → ∃k:ℕ.m = n + k.
92 #n #m elim m -m;
93 [ #Hn % [ @O | <(le_n_O_to_eq n Hn) // ]
94 | #m #Hind #Hn cases (le_to_or_lt_eq … Hn) -Hn; #Hn
95   [ elim (Hind (le_S_S_to_le … Hn)) #k #Hk % [ @(S k) | >Hk // ]
96   | % [ @O | <Hn // ]
97   ]
98 ]
99qed.
100
101lemma eq_plus_S_to_lt:
102  ∀n,m,p:ℕ.n = m + (S p) → m < n.
103 #n #m #p /2 by lt_plus_to_lt_l/
104qed.
105
106(* "Fast" proofs:  some proofs get reduced during normalization (in particular,
107   some functions which use a proof for rewriting are applied to constants and
108   get reduced during a proof or while matita is searching for a term;
109   they may also be normalized during testing), and so here are some more
110   efficient versions.  Perhaps they could be replaced using some kind of proof
111   irrelevance? *)
112
113let rec plus_n_Sm_fast (n:nat) on n : ∀m:nat. S (n+m) = n+S m ≝
114match n return λn'.∀m.S(n'+m) = n'+S m with
115[ O ⇒ λm.refl ??
116| S n' ⇒ λm. ?
117]. normalize @(match plus_n_Sm_fast n' m with [ refl ⇒ ? ]) @refl qed.
118
119let rec plus_n_O_faster (n:nat) : n = n + O ≝
120match n return λn.n=n+O with
121[ O ⇒ refl ??
122| S n' ⇒ match plus_n_O_faster n' return λx.λ_.S n'=S x with [ refl ⇒ refl ?? ]
123].
124
125let rec commutative_plus_faster (n,m:nat) : n+m = m+n ≝
126match n return λn.n+m = m+n with
127[ O ⇒ plus_n_O_faster ?
128| S n' ⇒ ?
129]. @(match plus_n_Sm_fast m n' return λx.λ_. ? = x with [ refl ⇒ ? ])
130@(match commutative_plus_faster n' m return λx.λ_.? = S x with [refl ⇒ ?]) @refl qed.
131
132lemma distributive_times_plus_fast : distributive ? times plus.
133#n elim n [ #m #p % ]
134#n' #IH #m #p normalize
135>IH
136>associative_plus in ⊢ (???%);
137<(associative_plus ? p) in ⊢ (???%);
138>(commutative_plus_faster ? p) in ⊢ (???%);
139>(associative_plus p)
140@associative_plus
141qed.
142
143lemma times_n_Sm_fast : ∀n,m.n + n * m = n * S m.
144#n elim n -n
145[ #m % ]
146#n #IH #m normalize <IH
147<associative_plus >(commutative_plus_faster n)
148>associative_plus >IH %
149qed.
150
151lemma commutative_times_fast : commutative ? times.
152#n elim n -n
153[ #m <times_n_O % ]
154#n #IH #m normalize <times_n_Sm_fast >IH %
155qed.
156
Note: See TracBrowser for help on using the repository browser.