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 include "nat/le_arith.ma".
17 include "nat/compare.ma".
19 let rec minus n m \def
25 | (S q) \Rightarrow minus p q ]].
27 interpretation "natural minus" 'minus x y = (minus x y).
29 theorem minus_S_S: ∀n,m:nat.S n - S m = n -m.
33 theorem minus_n_O: \forall n:nat.n=n-O.
34 intros.elim n.simplify.reflexivity.
38 theorem minus_n_n: \forall n:nat.O=n-n.
39 intros.elim n.simplify.
44 theorem minus_Sn_n: \forall n:nat. S O = (S n)-n.
50 theorem minus_Sn_m: \forall n,m:nat. m \leq n \to (S n)-m = S (n-m).
53 (\lambda n,m.m \leq n \to (S n)-m = S (n-m))).
54 intros.apply (le_n_O_elim n1 H).
56 intros.simplify.reflexivity.
57 intros.rewrite < H.reflexivity.
58 apply le_S_S_to_le. assumption.
61 theorem eq_minus_S_pred: \forall n,m. n - (S m) = pred(n -m).
64 |intro.simplify.autobatch
65 |intros.simplify.assumption
70 \forall n,m,p:nat. m \leq n \to (n-m)+p = (n+p)-m.
73 (\lambda n,m.\forall p:nat.m \leq n \to (n-m)+p = (n+p)-m)).
74 intros.apply (le_n_O_elim ? H).
75 simplify.rewrite < minus_n_O.reflexivity.
76 intros.simplify.reflexivity.
77 intros.simplify.apply H.apply le_S_S_to_le.assumption.
80 theorem minus_plus_m_m: \forall n,m:nat.n = (n+m)-m.
81 intros 2 .elim m in n ⊢ %.
82 rewrite < minus_n_O.apply plus_n_O.
83 elim n1.simplify. apply minus_n_n.
85 (* rewrite < plus_n_Sm.
86 change with (S n2 = (S n2 + n)-n).
90 theorem plus_minus_m_m: \forall n,m:nat.
91 m \leq n \to n = (n-m)+m.
93 apply (nat_elim2 (\lambda n,m.m \leq n \to n = (n-m)+m)).
94 intros.apply (le_n_O_elim n1 H).
96 intros.simplify.rewrite < plus_n_O.reflexivity.
98 rewrite < sym_plus.simplify. apply eq_f.
99 rewrite < sym_plus.apply H.
100 apply le_S_S_to_le.assumption.
103 theorem le_plus_minus_m_m: \forall n,m:nat. n \le (n-m)+m.
106 |cases m; simplify[autobatch|autobatch by H, le_S_S]
110 theorem minus_to_plus :\forall n,m,p:nat.m \leq n \to n-m = p \to
112 intros.apply (trans_eq ? ? ((n-m)+m)).
113 apply plus_minus_m_m.
118 theorem plus_to_minus :\forall n,m,p:nat.
121 (* autobatch by transitive_eq, eq_f2, H. *)
122 autobatch depth=4 by inj_plus_r, symmetric_eq, minus_to_plus, le_plus_n_r.
127 apply plus_minus_m_m.rewrite > H.
132 theorem minus_pred_pred : \forall n,m:nat. lt O n \to lt O m \to
133 eq nat (minus (pred n) (pred m)) (minus n m).
135 apply (lt_O_n_elim n H).intro.
136 apply (lt_O_n_elim m H1).intro.
137 simplify.reflexivity.
140 theorem eq_minus_n_m_O: \forall n,m:nat.
141 n \leq m \to n-m = O.
143 apply (nat_elim2 (\lambda n,m.n \leq m \to n-m = O)).
144 intros.simplify.reflexivity.
145 intros.apply False_ind.
149 simplify.apply H.apply le_S_S_to_le. apply H1.
152 theorem le_SO_minus: \forall n,m:nat.S n \leq m \to S O \leq m-n.
153 intros.elim H.elim (minus_Sn_n n).apply le_n.
154 rewrite > minus_Sn_m.
155 apply le_S.assumption.
156 apply lt_to_le.assumption.
159 theorem minus_le_S_minus_S: \forall n,m:nat. m-n \leq S (m-(S n)).
161 apply (nat_elim2 (\lambda n,m.m-n \leq S (m-(S n)))).
162 intro.elim n1.simplify.apply le_n_Sn.
163 simplify.rewrite < minus_n_O.apply le_n.
164 intros.simplify.apply le_n_Sn.
165 intros.simplify.apply H.
168 theorem lt_minus_S_n_to_le_minus_n : \forall n,m,p:nat. m-(S n) < p \to m-n \leq p.
171 (* end auto($Revision$) proof: TIME=1.33 SIZE=100 DEPTH=100 *)
172 apply (trans_le (m-n) (S (m-(S n))) p).
173 apply minus_le_S_minus_S.
177 theorem le_minus_m: \forall n,m:nat. n-m \leq n.
178 intros.apply (nat_elim2 (\lambda m,n. n-m \leq n)).
179 intros.rewrite < minus_n_O.apply le_n.
180 intros.simplify.apply le_n.
181 intros.simplify.apply le_S.assumption.
184 theorem lt_minus_m: \forall n,m:nat. O < n \to O < m \to n-m \lt n.
185 intros.apply (lt_O_n_elim n H).intro.
186 apply (lt_O_n_elim m H1).intro.
187 simplify.unfold lt.apply le_S_S.apply le_minus_m.
190 theorem minus_le_O_to_le: \forall n,m:nat. n-m \leq O \to n \leq m.
192 apply (nat_elim2 (\lambda n,m:nat.n-m \leq O \to n \leq m)).
194 simplify.intros. assumption.
195 simplify.intros.apply le_S_S.apply H.assumption.
199 theorem monotonic_le_minus_r:
200 \forall p,q,n:nat. q \leq p \to n-p \le n-q.
201 intros 2.apply (nat_elim2 ???? p q); intros;
202 [apply (le_n_O_elim n H).apply le_n.
204 |elim n1;intros;[apply le_n|simplify.apply H.apply le_S_S_to_le.assumption]
208 theorem monotonic_le_minus_l:
209 ∀p,q,n:nat. q \leq p \to q-n \le p-n.
210 intros 2.apply (nat_elim2 ???? p q); intros;
211 [apply (le_n_O_elim n H).apply le_n.
213 |cases n1;[apply H1|simplify. apply H.apply le_S_S_to_le.assumption]
217 theorem le_minus_to_plus: \forall n,m,p. (le (n-m) p) \to (le n (p+m)).
219 (* autobatch by transitive_le, le_plus_minus_m_m, le_plus_l, H.*)
221 [2:apply le_plus_minus_m_m.
222 |3:apply le_plus_l.apply H.
227 theorem le_plus_to_minus: \forall n,m,p. (le n (p+m)) \to (le (n-m) p).
229 autobatch by (monotonic_le_minus_l (p+m) n m), H.
231 intros 2.apply (nat_elim2 (\lambda n,m.\forall p.(le n (p+m)) \to (le (n-m) p))).
232 intros.simplify.apply le_O_n.
233 intros 2.rewrite < plus_n_O.intro.simplify.assumption.
234 intros.simplify.apply H.
235 apply le_S_S_to_le.rewrite > plus_n_Sm.assumption. *)
238 (* the converse of le_plus_to_minus does not hold *)
239 theorem le_plus_to_minus_r: \forall n,m,p. (le (n+m) p) \to (le n (p-m)).
241 autobatch by trans_le, le_plus_to_le, H, le_plus_minus_m_m.
243 apply (nat_elim2 (\lambda m,p.(le (n+m) p) \to (le n (p-m)))).
244 intro.rewrite < plus_n_O.rewrite < minus_n_O.intro.assumption.
245 intro.intro.cut (n=O).rewrite > Hcut.apply le_O_n.
246 apply sym_eq. apply le_n_O_to_eq.
247 apply (trans_le ? (n+(S n1))).
249 apply le_plus_n.assumption.
251 apply H.apply le_S_S_to_le.
252 rewrite > plus_n_Sm.assumption. *)
255 (* minus and lt - to be completed *)
256 theorem lt_minus_l: \forall m,l,n:nat.
257 l < m \to m \le n \to n - m < n - l.
259 [intros.apply False_ind.apply (not_le_Sn_O ? H)
261 (* rewrite < minus_n_O.
262 change in H1 with (n<n1);
263 apply lt_minus_m; autobatch depth=2; *)
265 generalize in match H2.
267 [intros.apply False_ind.apply (not_le_Sn_O ? H3)
273 |apply le_S_S_to_le.assumption
279 theorem lt_minus_r: \forall n,m,l:nat.
280 n \le l \to l < m \to l -n < m -n.
285 letin x ≝ (lt_pred (l-n1) (m-n1)).
288 autobatch depth=4 width=5 size=10 by
289 x, H, H1, H2, trans_le, le_n_Sn, le_n.
291 rewrite > eq_minus_S_pred.
292 rewrite > eq_minus_S_pred.
294 [unfold lt.apply le_plus_to_minus_r.applyS H1
295 |apply H[autobatch depth=2|assumption]
300 lemma lt_to_lt_O_minus : \forall m,n.
303 unfold. apply le_plus_to_minus_r. unfold in H.
304 cut ((S n ≤ m) = (1 + n ≤ m)) as applyS;
305 [ rewrite < applyS; assumption;
306 | demodulate; reflexivity. ]
315 theorem lt_minus_to_plus: \forall n,m,p. (lt n (p-m)) \to (lt (n+m) p).
316 intros 3.apply (nat_elim2 (\lambda m,p.(lt n (p-m)) \to (lt (n+m) p))).
317 intro.rewrite < plus_n_O.rewrite < minus_n_O.intro.assumption.
318 simplify.intros.apply False_ind.apply (not_le_Sn_O n H).
319 simplify.intros.unfold lt.
325 theorem lt_O_minus_to_lt: \forall a,b:nat.
326 O \lt b-a \to a \lt b.
327 intros. applyS (lt_minus_to_plus O a b). assumption;
329 rewrite > (plus_n_O a).
330 rewrite > (sym_plus a O).
331 apply (lt_minus_to_plus O a b).
336 theorem lt_minus_to_lt_plus:
337 \forall n,m,p. n - m < p \to n < m + p.
339 apply (nat_elim2 ? ? ? ? n m)
341 lapply depth=0 le_n; autobatch;
342 |intros 2.rewrite < minus_n_O.
354 theorem lt_plus_to_lt_minus:
355 \forall n,m,p. m \le n \to n < m + p \to n - m < p.
357 apply (nat_elim2 ? ? ? ? n m)
359 apply (le_n_O_elim ? H).
360 simplify.intros.assumption
361 |simplify.intros.assumption.
365 [apply le_S_S_to_le.assumption
366 |apply le_S_S_to_le.apply H2
371 theorem minus_m_minus_mn: \forall n,m. n\le m \to n=m-(m-n).
378 theorem distributive_times_minus: distributive nat times minus.
381 apply ((leb_elim z y)).
382 intro.cut (x*(y-z)+x*z = (x*y-x*z)+x*z).
383 apply (inj_plus_l (x*z)).assumption.
384 apply (trans_eq nat ? (x*y)).
385 rewrite < distr_times_plus.rewrite < (plus_minus_m_m ? ? H).reflexivity.
386 rewrite < plus_minus_m_m.
388 apply le_times_r.assumption.
389 intro.rewrite > eq_minus_n_m_O.
390 rewrite > (eq_minus_n_m_O (x*y)).
391 rewrite < sym_times.simplify.reflexivity.
392 apply le_times_r.apply lt_to_le.apply not_le_to_lt.assumption.
393 apply lt_to_le.apply not_le_to_lt.assumption.
396 theorem distr_times_minus: \forall n,m,p:nat. n*(m-p) = n*m-n*p
397 \def distributive_times_minus.
399 theorem eq_minus_plus_plus_minus: \forall n,m,p:nat. p \le m \to (n+m)-p = n+(m-p).
402 lapply (plus_minus_m_m ?? H); demodulate. reflexivity.
404 rewrite > sym_plus in \vdash (? ? ? %).
405 rewrite > assoc_plus.
406 rewrite < plus_minus_m_m.
407 reflexivity.assumption.
411 theorem eq_minus_minus_minus_plus: \forall n,m,p:nat. (n-m)-p = n-(m+p).
413 cut (m+p \le n \or m+p \nleq n).
415 symmetry.apply plus_to_minus.
416 rewrite > assoc_plus.rewrite > (sym_plus p).rewrite < plus_minus_m_m.
417 rewrite > sym_plus.rewrite < plus_minus_m_m.
419 apply (trans_le ? (m+p)).
420 rewrite < sym_plus.apply le_plus_n.
422 apply le_plus_to_minus_r.rewrite > sym_plus.assumption.
423 rewrite > (eq_minus_n_m_O n (m+p)).
424 rewrite > (eq_minus_n_m_O (n-m) p).
426 apply le_plus_to_minus.apply lt_to_le. rewrite < sym_plus.
427 apply not_le_to_lt. assumption.
428 apply lt_to_le.apply not_le_to_lt.assumption.
429 apply (decidable_le (m+p) n).
432 theorem eq_plus_minus_minus_minus: \forall n,m,p:nat. p \le m \to m \le n \to
437 rewrite < assoc_plus.
438 rewrite < plus_minus_m_m.
440 rewrite < plus_minus_m_m.reflexivity.
441 assumption.assumption.