1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| A.Asperti, C.Sacerdoti Coen, *)
8 (* ||A|| E.Tassi, S.Zacchiroli *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU Lesser General Public License Version 2.1 *)
13 (**************************************************************************)
16 set "baseuri" "cic:/matita/nat/minus".
18 include "nat/le_arith.ma".
19 include "nat/compare.ma".
21 let rec minus n m \def
27 | (S q) \Rightarrow minus p q ]].
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).
32 theorem minus_n_O: \forall n:nat.n=n-O.
33 intros.elim n.simplify.reflexivity.
37 theorem minus_n_n: \forall n:nat.O=n-n.
38 intros.elim n.simplify.
43 theorem minus_Sn_n: \forall n:nat. S O = (S n)-n.
49 theorem minus_Sn_m: \forall n,m:nat. m \leq n \to (S n)-m = S (n-m).
52 (\lambda n,m.m \leq n \to (S n)-m = S (n-m))).
53 intros.apply (le_n_O_elim n1 H).
55 intros.simplify.reflexivity.
56 intros.rewrite < H.reflexivity.
57 apply le_S_S_to_le. assumption.
60 theorem eq_minus_S_pred: \forall n,m. n - (S m) = pred(n -m).
63 |intro.simplify.autobatch
64 |intros.simplify.assumption
69 \forall n,m,p:nat. m \leq n \to (n-m)+p = (n+p)-m.
72 (\lambda n,m.\forall p:nat.m \leq n \to (n-m)+p = (n+p)-m)).
73 intros.apply (le_n_O_elim ? H).
74 simplify.rewrite < minus_n_O.reflexivity.
75 intros.simplify.reflexivity.
76 intros.simplify.apply H.apply le_S_S_to_le.assumption.
79 theorem minus_plus_m_m: \forall n,m:nat.n = (n+m)-m.
81 generalize in match n.
83 rewrite < minus_n_O.apply plus_n_O.
87 change with (S n3 = (S n3 + n1)-n1).
91 theorem plus_minus_m_m: \forall n,m:nat.
92 m \leq n \to n = (n-m)+m.
94 apply (nat_elim2 (\lambda n,m.m \leq n \to n = (n-m)+m)).
95 intros.apply (le_n_O_elim n1 H).
97 intros.simplify.rewrite < plus_n_O.reflexivity.
98 intros.simplify.rewrite < sym_plus.simplify.
99 apply eq_f.rewrite < sym_plus.apply H.
100 apply le_S_S_to_le.assumption.
103 theorem minus_to_plus :\forall n,m,p:nat.m \leq n \to n-m = p \to
105 intros.apply (trans_eq ? ? ((n-m)+m)).
106 apply plus_minus_m_m.
111 theorem plus_to_minus :\forall n,m,p:nat.
114 apply (inj_plus_r m).
118 apply plus_minus_m_m.rewrite > H.
123 theorem minus_S_S : \forall n,m:nat.
124 eq nat (minus (S n) (S m)) (minus n m).
129 theorem minus_pred_pred : \forall n,m:nat. lt O n \to lt O m \to
130 eq nat (minus (pred n) (pred m)) (minus n m).
132 apply (lt_O_n_elim n H).intro.
133 apply (lt_O_n_elim m H1).intro.
134 simplify.reflexivity.
137 theorem eq_minus_n_m_O: \forall n,m:nat.
138 n \leq m \to n-m = O.
140 apply (nat_elim2 (\lambda n,m.n \leq m \to n-m = O)).
141 intros.simplify.reflexivity.
142 intros.apply False_ind.
146 simplify.apply H.apply le_S_S_to_le. apply H1.
149 theorem le_SO_minus: \forall n,m:nat.S n \leq m \to S O \leq m-n.
150 intros.elim H.elim (minus_Sn_n n).apply le_n.
151 rewrite > minus_Sn_m.
152 apply le_S.assumption.
153 apply lt_to_le.assumption.
156 theorem minus_le_S_minus_S: \forall n,m:nat. m-n \leq S (m-(S n)).
157 intros.apply (nat_elim2 (\lambda n,m.m-n \leq S (m-(S n)))).
158 intro.elim n1.simplify.apply le_n_Sn.
159 simplify.rewrite < minus_n_O.apply le_n.
160 intros.simplify.apply le_n_Sn.
161 intros.simplify.apply H.
164 theorem lt_minus_S_n_to_le_minus_n : \forall n,m,p:nat. m-(S n) < p \to m-n \leq p.
165 intros 3.simplify.intro.
166 apply (trans_le (m-n) (S (m-(S n))) p).
167 apply minus_le_S_minus_S.
171 theorem le_minus_m: \forall n,m:nat. n-m \leq n.
172 intros.apply (nat_elim2 (\lambda m,n. n-m \leq n)).
173 intros.rewrite < minus_n_O.apply le_n.
174 intros.simplify.apply le_n.
175 intros.simplify.apply le_S.assumption.
178 theorem lt_minus_m: \forall n,m:nat. O < n \to O < m \to n-m \lt n.
179 intros.apply (lt_O_n_elim n H).intro.
180 apply (lt_O_n_elim m H1).intro.
181 simplify.unfold lt.apply le_S_S.apply le_minus_m.
184 theorem minus_le_O_to_le: \forall n,m:nat. n-m \leq O \to n \leq m.
186 apply (nat_elim2 (\lambda n,m:nat.n-m \leq O \to n \leq m)).
188 simplify.intros. assumption.
189 simplify.intros.apply le_S_S.apply H.assumption.
193 theorem monotonic_le_minus_r:
194 \forall p,q,n:nat. q \leq p \to n-p \le n-q.
195 simplify.intros 2.apply (nat_elim2
196 (\lambda p,q.\forall a.q \leq p \to a-p \leq a-q)).
197 intros.apply (le_n_O_elim n H).apply le_n.
198 intros.rewrite < minus_n_O.
200 intros.elim a.simplify.apply le_n.
201 simplify.apply H.apply le_S_S_to_le.assumption.
204 theorem le_minus_to_plus: \forall n,m,p. (le (n-m) p) \to (le n (p+m)).
205 intros 2.apply (nat_elim2 (\lambda n,m.\forall p.(le (n-m) p) \to (le n (p+m)))).
207 simplify.intros.rewrite < plus_n_O.assumption.
210 apply le_S_S.apply H.
214 theorem le_plus_to_minus: \forall n,m,p. (le n (p+m)) \to (le (n-m) p).
215 intros 2.apply (nat_elim2 (\lambda n,m.\forall p.(le n (p+m)) \to (le (n-m) p))).
216 intros.simplify.apply le_O_n.
217 intros 2.rewrite < plus_n_O.intro.simplify.assumption.
218 intros.simplify.apply H.
219 apply le_S_S_to_le.rewrite > plus_n_Sm.assumption.
222 (* the converse of le_plus_to_minus does not hold *)
223 theorem le_plus_to_minus_r: \forall n,m,p. (le (n+m) p) \to (le n (p-m)).
224 intros 3.apply (nat_elim2 (\lambda m,p.(le (n+m) p) \to (le n (p-m)))).
225 intro.rewrite < plus_n_O.rewrite < minus_n_O.intro.assumption.
226 intro.intro.cut (n=O).rewrite > Hcut.apply le_O_n.
227 apply sym_eq. apply le_n_O_to_eq.
228 apply (trans_le ? (n+(S n1))).
230 apply le_plus_n.assumption.
232 apply H.apply le_S_S_to_le.
233 rewrite > plus_n_Sm.assumption.
236 (* minus and lt - to be completed *)
237 theorem lt_minus_l: \forall m,l,n:nat.
238 l < m \to m \le n \to n - m < n - l.
240 [intros.apply False_ind.apply (not_le_Sn_O ? H)
241 |intros.rewrite < minus_n_O.
244 generalize in match H2.
246 [intros.apply False_ind.apply (not_le_Sn_O ? H3)
252 |apply le_S_S_to_le.assumption
258 theorem lt_minus_r: \forall n,m,l:nat.
259 n \le l \to l < m \to l -n < m -n.
262 |rewrite > eq_minus_S_pred.
263 rewrite > eq_minus_S_pred.
265 [unfold lt.apply le_plus_to_minus_r.applyS H1
266 |apply H[autobatch|assumption]
271 lemma lt_to_lt_O_minus : \forall m,n.
274 unfold. apply le_plus_to_minus_r. unfold in H. rewrite > sym_plus.
280 theorem lt_minus_to_plus: \forall n,m,p. (lt n (p-m)) \to (lt (n+m) p).
281 intros 3.apply (nat_elim2 (\lambda m,p.(lt n (p-m)) \to (lt (n+m) p))).
282 intro.rewrite < plus_n_O.rewrite < minus_n_O.intro.assumption.
283 simplify.intros.apply False_ind.apply (not_le_Sn_O n H).
284 simplify.intros.unfold lt.
290 theorem lt_O_minus_to_lt: \forall a,b:nat.
291 O \lt b-a \to a \lt b.
293 rewrite > (plus_n_O a).
294 rewrite > (sym_plus a O).
295 apply (lt_minus_to_plus O a b).
299 theorem lt_minus_to_lt_plus:
300 \forall n,m,p. n - m < p \to n < m + p.
302 apply (nat_elim2 ? ? ? ? n m)
303 [simplify.intros.autobatch.
304 |intros 2.rewrite < minus_n_O.
316 theorem lt_plus_to_lt_minus:
317 \forall n,m,p. m \le n \to n < m + p \to n - m < p.
319 apply (nat_elim2 ? ? ? ? n m)
321 apply (le_n_O_elim ? H).
322 simplify.intros.assumption
323 |simplify.intros.assumption.
327 [apply le_S_S_to_le.assumption
328 |apply le_S_S_to_le.apply H2
333 theorem minus_m_minus_mn: \forall n,m. n\le m \to n=m-(m-n).
340 theorem distributive_times_minus: distributive nat times minus.
343 apply ((leb_elim z y)).
344 intro.cut (x*(y-z)+x*z = (x*y-x*z)+x*z).
345 apply (inj_plus_l (x*z)).assumption.
346 apply (trans_eq nat ? (x*y)).
347 rewrite < distr_times_plus.rewrite < (plus_minus_m_m ? ? H).reflexivity.
348 rewrite < plus_minus_m_m.
350 apply le_times_r.assumption.
351 intro.rewrite > eq_minus_n_m_O.
352 rewrite > (eq_minus_n_m_O (x*y)).
353 rewrite < sym_times.simplify.reflexivity.
354 apply le_times_r.apply lt_to_le.apply not_le_to_lt.assumption.
355 apply lt_to_le.apply not_le_to_lt.assumption.
358 theorem distr_times_minus: \forall n,m,p:nat. n*(m-p) = n*m-n*p
359 \def distributive_times_minus.
361 theorem eq_minus_plus_plus_minus: \forall n,m,p:nat. p \le m \to (n+m)-p = n+(m-p).
364 rewrite > sym_plus in \vdash (? ? ? %).
365 rewrite > assoc_plus.
366 rewrite < plus_minus_m_m.
367 reflexivity.assumption.
370 theorem eq_minus_minus_minus_plus: \forall n,m,p:nat. (n-m)-p = n-(m+p).
372 cut (m+p \le n \or m+p \nleq n).
374 symmetry.apply plus_to_minus.
375 rewrite > assoc_plus.rewrite > (sym_plus p).rewrite < plus_minus_m_m.
376 rewrite > sym_plus.rewrite < plus_minus_m_m.
378 apply (trans_le ? (m+p)).
379 rewrite < sym_plus.apply le_plus_n.
381 apply le_plus_to_minus_r.rewrite > sym_plus.assumption.
382 rewrite > (eq_minus_n_m_O n (m+p)).
383 rewrite > (eq_minus_n_m_O (n-m) p).
385 apply le_plus_to_minus.apply lt_to_le. rewrite < sym_plus.
386 apply not_le_to_lt. assumption.
387 apply lt_to_le.apply not_le_to_lt.assumption.
388 apply (decidable_le (m+p) n).
391 theorem eq_plus_minus_minus_minus: \forall n,m,p:nat. p \le m \to m \le n \to
396 rewrite < assoc_plus.
397 rewrite < plus_minus_m_m.
399 rewrite < plus_minus_m_m.reflexivity.
400 assumption.assumption.