]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/nat/minus.ma
New version of the library. nth_prime, gcd, log.
[helm.git] / helm / matita / library / nat / minus.ma
1 (**************************************************************************)
2 (*       ___                                                                *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||       A.Asperti, C.Sacerdoti Coen,                          *)
8 (*      ||A||       E.Tassi, S.Zacchiroli                                 *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU Lesser General Public License Version 2.1         *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15
16 set "baseuri" "cic:/matita/nat/minus".
17
18 include "nat/le_arith.ma".
19 include "nat/compare.ma".
20
21 let rec minus n m \def 
22  match n with 
23  [ O \Rightarrow O
24  | (S p) \Rightarrow 
25         match m with
26         [O \Rightarrow (S p)
27         | (S q) \Rightarrow minus p q ]].
28
29 (*CSC: the URI must disappear: there is a bug now *)
30 interpretation "natural minus" 'minus x y = (cic:/matita/nat/minus/minus.con x y).
31
32 theorem minus_n_O: \forall n:nat.n=n-O.
33 intros.elim n.simplify.reflexivity.
34 simplify.reflexivity.
35 qed.
36
37 theorem minus_n_n: \forall n:nat.O=n-n.
38 intros.elim n.simplify.
39 reflexivity.
40 simplify.apply H.
41 qed.
42
43 theorem minus_Sn_n: \forall n:nat. S O = (S n)-n.
44 intro.elim n.
45 simplify.reflexivity.
46 elim H.reflexivity.
47 qed.
48
49 theorem minus_Sn_m: \forall n,m:nat. m \leq n \to (S n)-m = S (n-m).
50 intros 2.
51 apply nat_elim2
52 (\lambda n,m.m \leq n \to (S n)-m = S (n-m)).
53 intros.apply le_n_O_elim n1 H.
54 simplify.reflexivity.
55 intros.simplify.reflexivity.
56 intros.rewrite < H.reflexivity.
57 apply le_S_S_to_le. assumption.
58 qed.
59
60 theorem plus_minus:
61 \forall n,m,p:nat. m \leq n \to (n-m)+p = (n+p)-m.
62 intros 2.
63 apply nat_elim2
64 (\lambda n,m.\forall p:nat.m \leq n \to (n-m)+p = (n+p)-m).
65 intros.apply le_n_O_elim ? H.
66 simplify.rewrite < minus_n_O.reflexivity.
67 intros.simplify.reflexivity.
68 intros.simplify.apply H.apply le_S_S_to_le.assumption.
69 qed.
70
71 theorem plus_minus_m_m: \forall n,m:nat.
72 m \leq n \to n = (n-m)+m.
73 intros 2.
74 apply nat_elim2 (\lambda n,m.m \leq n \to n = (n-m)+m).
75 intros.apply le_n_O_elim n1 H.
76 reflexivity.
77 intros.simplify.rewrite < plus_n_O.reflexivity.
78 intros.simplify.rewrite < sym_plus.simplify.
79 apply eq_f.rewrite < sym_plus.apply H.
80 apply le_S_S_to_le.assumption.
81 qed.
82
83 theorem minus_to_plus :\forall n,m,p:nat.m \leq n \to n-m = p \to 
84 n = m+p.
85 intros.apply trans_eq ? ? ((n-m)+m) ?.
86 apply plus_minus_m_m.
87 apply H.elim H1.
88 apply sym_plus.
89 qed.
90
91 theorem plus_to_minus :\forall n,m,p:nat.m \leq n \to
92 n = m+p \to n-m = p.
93 intros.
94 apply inj_plus_r m.
95 rewrite < H1.
96 rewrite < sym_plus.
97 symmetry.
98 apply plus_minus_m_m.assumption.
99 qed.
100
101 theorem minus_S_S : \forall n,m:nat.
102 eq nat (minus (S n) (S m)) (minus n m).
103 intros.
104 reflexivity.
105 qed.
106
107 theorem minus_pred_pred : \forall n,m:nat. lt O n \to lt O m \to 
108 eq nat (minus (pred n) (pred m)) (minus n m).
109 intros.
110 apply lt_O_n_elim n H.intro.
111 apply lt_O_n_elim m H1.intro.
112 simplify.reflexivity.
113 qed.
114
115 theorem eq_minus_n_m_O: \forall n,m:nat.
116 n \leq m \to n-m = O.
117 intros 2.
118 apply nat_elim2 (\lambda n,m.n \leq m \to n-m = O).
119 intros.simplify.reflexivity.
120 intros.apply False_ind.
121 (* ancora problemi con il not *)
122 apply not_le_Sn_O n1 H.
123 intros.
124 simplify.apply H.apply le_S_S_to_le. apply H1.
125 qed.
126
127 theorem le_SO_minus: \forall n,m:nat.S n \leq m \to S O \leq m-n.
128 intros.elim H.elim minus_Sn_n n.apply le_n.
129 rewrite > minus_Sn_m.
130 apply le_S.assumption.
131 apply lt_to_le.assumption.
132 qed.
133
134 theorem minus_le_S_minus_S: \forall n,m:nat. m-n \leq S (m-(S n)).
135 intros.apply nat_elim2 (\lambda n,m.m-n \leq S (m-(S n))).
136 intro.elim n1.simplify.apply le_n_Sn.
137 simplify.rewrite < minus_n_O.apply le_n.
138 intros.simplify.apply le_n_Sn.
139 intros.simplify.apply H.
140 qed.
141
142 theorem lt_minus_S_n_to_le_minus_n : \forall n,m,p:nat. m-(S n) < p \to m-n \leq p. 
143 intros 3.simplify.intro.
144 apply trans_le (m-n) (S (m-(S n))) p.
145 apply minus_le_S_minus_S.
146 assumption.
147 qed.
148
149 theorem le_minus_m: \forall n,m:nat. n-m \leq n.
150 intros.apply nat_elim2 (\lambda m,n. n-m \leq n).
151 intros.rewrite < minus_n_O.apply le_n.
152 intros.simplify.apply le_n.
153 intros.simplify.apply le_S.assumption.
154 qed.
155
156 theorem lt_minus_m: \forall n,m:nat. O < n \to O < m \to n-m \lt n.
157 intros.apply lt_O_n_elim n H.intro.
158 apply lt_O_n_elim m H1.intro.
159 simplify.apply le_S_S.apply le_minus_m.
160 qed.
161
162 theorem minus_le_O_to_le: \forall n,m:nat. n-m \leq O \to n \leq m.
163 intros 2.
164 apply nat_elim2 (\lambda n,m:nat.n-m \leq O \to n \leq m).
165 intros.apply le_O_n.
166 simplify.intros. assumption.
167 simplify.intros.apply le_S_S.apply H.assumption.
168 qed.
169
170 (* galois *)
171 theorem monotonic_le_minus_r: 
172 \forall p,q,n:nat. q \leq p \to n-p \le n-q.
173 simplify.intros 2.apply nat_elim2 
174 (\lambda p,q.\forall a.q \leq p \to a-p \leq a-q).
175 intros.apply le_n_O_elim n H.apply le_n.
176 intros.rewrite < minus_n_O.
177 apply le_minus_m.
178 intros.elim a.simplify.apply le_n.
179 simplify.apply H.apply le_S_S_to_le.assumption.
180 qed.
181
182 theorem le_minus_to_plus: \forall n,m,p. (le (n-m) p) \to (le n (p+m)).
183 intros 2.apply nat_elim2 (\lambda n,m.\forall p.(le (n-m) p) \to (le n (p+m))).
184 intros.apply le_O_n.
185 simplify.intros.rewrite < plus_n_O.assumption.
186 intros.
187 rewrite < plus_n_Sm.
188 apply le_S_S.apply H.
189 exact H1.
190 qed.
191
192 theorem le_plus_to_minus: \forall n,m,p. (le n (p+m)) \to (le (n-m) p).
193 intros 2.apply nat_elim2 (\lambda n,m.\forall p.(le n (p+m)) \to (le (n-m) p)).
194 intros.simplify.apply le_O_n.
195 intros 2.rewrite < plus_n_O.intro.simplify.assumption.
196 intros.simplify.apply H.
197 apply le_S_S_to_le.rewrite > plus_n_Sm.assumption.
198 qed.
199
200 (* the converse of le_plus_to_minus does not hold *)
201 theorem le_plus_to_minus_r: \forall n,m,p. (le (n+m) p) \to (le n (p-m)).
202 intros 3.apply nat_elim2 (\lambda m,p.(le (n+m) p) \to (le n (p-m))).
203 intro.rewrite < plus_n_O.rewrite < minus_n_O.intro.assumption.
204 intro.intro.cut n=O.rewrite > Hcut.apply le_O_n.
205 apply sym_eq. apply le_n_O_to_eq.
206 apply trans_le ? (n+(S n1)).
207 rewrite < sym_plus.
208 apply le_plus_n.assumption.
209 intros.simplify.
210 apply H.apply le_S_S_to_le.
211 rewrite > plus_n_Sm.assumption.
212 qed.
213
214
215 theorem distributive_times_minus: distributive nat times minus.
216 simplify.
217 intros.
218 apply (leb_elim z y).intro.
219 cut x*(y-z)+x*z = (x*y-x*z)+x*z.
220 apply inj_plus_l (x*z).
221 assumption.
222 apply trans_eq nat ? (x*y).
223 rewrite < distr_times_plus. 
224 rewrite < plus_minus_m_m ? ? H.reflexivity.
225 rewrite < plus_minus_m_m ? ? ?.reflexivity.
226 apply le_times_r.
227 assumption.
228 intro.
229 rewrite > eq_minus_n_m_O.
230 rewrite > eq_minus_n_m_O (x*y).
231 rewrite < sym_times.simplify.reflexivity.
232 apply lt_to_le.
233 apply not_le_to_lt.assumption.
234 apply le_times_r.apply lt_to_le.
235 apply not_le_to_lt.assumption.
236 qed.
237
238 theorem distr_times_minus: \forall n,m,p:nat. n*(m-p) = n*m-n*p
239 \def distributive_times_minus.
240
241 theorem eq_minus_minus_minus_plus: \forall n,m,p:nat. (n-m)-p = n-(m+p).
242 intros.
243 cut m+p \le n \or \not m+p \le n.
244 elim Hcut.
245 apply sym_eq.
246 apply plus_to_minus.assumption.
247 rewrite > assoc_plus.
248 rewrite > sym_plus p.
249 rewrite < plus_minus_m_m.
250 rewrite > sym_plus.
251 rewrite < plus_minus_m_m.reflexivity.
252 rewrite > eq_minus_n_m_O n (m+p).
253 rewrite > eq_minus_n_m_O (n-m) p.reflexivity.
254 apply decidable_le (m+p) n.
255 apply le_plus_to_minus_r.
256 rewrite > sym_plus.assumption.
257 apply trans_le ? (m+p).
258 rewrite < sym_plus.
259 apply le_plus_n.assumption.
260 apply lt_to_le.apply not_le_to_lt.assumption.
261 apply le_plus_to_minus.
262 apply lt_to_le.apply not_le_to_lt.
263 rewrite < sym_plus.assumption.
264 qed.
265
266 theorem eq_plus_minus_minus_minus: \forall n,m,p:nat. p \le m \to m \le n \to
267 p+(n-m) = n-(m-p).
268 intros.
269 apply sym_eq.
270 apply plus_to_minus.
271 apply le_plus_to_minus.
272 apply trans_le ? n.assumption.rewrite < sym_plus.apply le_plus_n.
273 rewrite < assoc_plus.
274 rewrite < plus_minus_m_m.
275 rewrite < sym_plus.
276 rewrite < plus_minus_m_m.reflexivity.
277 assumption.assumption.
278 qed.