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 (**************************************************************************)
15 include "nat/div_and_mod.ma".
18 theorem monotonic_lt_plus_r:
19 \forall n:nat.monotonic nat lt (\lambda m.n+m).
21 elim n.simplify.assumption.
23 apply le_S_S.assumption.
26 variant lt_plus_r: \forall n,p,q:nat. p < q \to n + p < n + q \def
29 theorem monotonic_lt_plus_l:
30 \forall n:nat.monotonic nat lt (\lambda m.m+n).
33 rewrite < sym_plus. rewrite < (sym_plus n).
34 apply lt_plus_r.assumption.
37 variant lt_plus_l: \forall n,p,q:nat. p < q \to p + n < q + n \def
40 theorem lt_plus: \forall n,m,p,q:nat. n < m \to p < q \to n + p < m + q.
42 apply (trans_lt ? (n + q)).
43 apply lt_plus_r.assumption.
44 apply lt_plus_l.assumption.
47 theorem lt_plus_to_lt_l :\forall n,p,q:nat. p+n < q+n \to p<q.
50 rewrite > (plus_n_O q).assumption.
52 unfold lt.apply le_S_S_to_le.
54 rewrite > (plus_n_Sm q).
58 theorem lt_plus_to_lt_r :\forall n,p,q:nat. n+p < n+q \to p<q.
59 intros.apply (lt_plus_to_lt_l n).
61 rewrite > (sym_plus q).assumption.
64 theorem le_to_lt_to_plus_lt: \forall a,b,c,d:nat.
65 a \le c \to b \lt d \to (a + b) \lt (c+d).
67 cut (a \lt c \lor a = c)
72 apply (lt_plus_r c b d).
75 | apply le_to_or_lt_eq.
82 theorem lt_O_times_S_S: \forall n,m:nat.O < (S n)*(S m).
83 intros.simplify.unfold lt.apply le_S_S.apply le_O_n.
86 theorem lt_times_eq_O: \forall a,b:nat.
87 O \lt a \to (a * b) = O \to b = O.
94 rewrite > (S_pred a) in H1
96 apply (eq_to_not_lt O ((S (pred a))*(S m)))
99 | apply lt_O_times_S_S
106 theorem O_lt_times_to_O_lt: \forall a,c:nat.
107 O \lt (a * c) \to O \lt a.
119 lemma lt_times_to_lt_O: \forall i,n,m:nat. i < n*m \to O < m.
121 elim (le_to_or_lt_eq O ? (le_O_n m))
125 rewrite < times_n_O in H.
126 apply (not_le_Sn_O ? H)
131 theorem monotonic_lt_times_r:
132 \forall n:nat.monotonic nat lt (\lambda m.(S n)*m).
135 simplify.rewrite < plus_n_O.rewrite < plus_n_O.assumption.
136 apply lt_plus.assumption.assumption.
139 (* a simple variant of the previus monotionic_lt_times *)
140 theorem monotonic_lt_times_variant: \forall c:nat.
141 O \lt c \to monotonic nat lt (\lambda t.(t*c)).
143 apply (increasing_to_monotonic).
148 rewrite > plus_n_O in \vdash (? % ?).
153 theorem lt_times_r: \forall n,p,q:nat. p < q \to (S n) * p < (S n) * q
154 \def monotonic_lt_times_r.
156 theorem monotonic_lt_times_l:
157 \forall m:nat.monotonic nat lt (\lambda n.n * (S m)).
160 rewrite < sym_times.rewrite < (sym_times (S m)).
161 apply lt_times_r.assumption.
164 variant lt_times_l: \forall n,p,q:nat. p<q \to p*(S n) < q*(S n)
165 \def monotonic_lt_times_l.
167 theorem lt_times:\forall n,m,p,q:nat. n<m \to p<q \to n*p < m*q.
170 apply (lt_O_n_elim m H).
173 apply (lt_O_n_elim q Hcut).
174 intro.change with (O < (S m1)*(S m2)).
175 apply lt_O_times_S_S.
176 apply (ltn_to_ltO p q H1).
177 apply (trans_lt ? ((S n1)*q)).
178 apply lt_times_r.assumption.
180 apply (lt_O_n_elim q Hcut).
184 apply (ltn_to_ltO p q H2).
188 \forall n,m,p. O < n \to m < p \to n*m < n*p.
190 elim H;apply lt_times_r;assumption.
194 \forall n,m,p. O < n \to m < p \to m*n < p*n.
196 elim H;apply lt_times_l;assumption.
199 theorem lt_to_le_to_lt_times :
200 \forall n,n1,m,m1. n < n1 \to m \le m1 \to O < m1 \to n*m < n1*m1.
202 apply (le_to_lt_to_lt ? (n*m1))
203 [apply le_times_r.assumption
205 [assumption|assumption]
209 theorem lt_times_to_lt_l:
210 \forall n,p,q:nat. p*(S n) < q*(S n) \to p < q.
212 cut (p < q \lor p \nlt q).
215 absurd (p * (S n) < q * (S n)).
221 exact (decidable_lt p q).
224 theorem lt_times_n_to_lt:
225 \forall n,p,q:nat. O < n \to p*n < q*n \to p < q.
228 [intros.apply False_ind.apply (not_le_Sn_n ? H)
229 |intros 4.apply lt_times_to_lt_l
233 theorem lt_times_to_lt_r:
234 \forall n,p,q:nat. (S n)*p < (S n)*q \to lt p q.
236 apply (lt_times_to_lt_l n).
238 rewrite < (sym_times (S n)).
242 theorem lt_times_n_to_lt_r:
243 \forall n,p,q:nat. O < n \to n*p < n*q \to lt p q.
246 [intros.apply False_ind.apply (not_le_Sn_n ? H)
247 |intros 4.apply lt_times_to_lt_r
253 theorem nat_compare_times_l : \forall n,p,q:nat.
254 nat_compare p q = nat_compare ((S n) * p) ((S n) * q).
255 intros.apply nat_compare_elim.intro.
256 apply nat_compare_elim.
259 apply (inj_times_r n).assumption.
260 apply lt_to_not_eq. assumption.
262 apply (lt_times_to_lt_r n).assumption.
263 apply le_to_not_lt.apply lt_to_le.assumption.
264 intro.rewrite < H.rewrite > nat_compare_n_n.reflexivity.
265 intro.apply nat_compare_elim.intro.
267 apply (lt_times_to_lt_r n).assumption.
268 apply le_to_not_lt.apply lt_to_le.assumption.
271 apply (inj_times_r n).assumption.
272 apply lt_to_not_eq.assumption.
277 theorem lt_times_plus_times: \forall a,b,n,m:nat.
278 a < n \to b < m \to a*m + b < n*m.
281 [intros.apply False_ind.apply (not_le_Sn_O ? H)
285 change with (S b+a*m1 \leq m1+m*m1).
289 [apply le_S_S_to_le.assumption
298 theorem eq_mod_O_to_lt_O_div: \forall n,m:nat. O < m \to O < n\to n \mod m = O \to O < n / m.
299 intros 4.apply (lt_O_n_elim m H).intros.
300 apply (lt_times_to_lt_r m1).
302 rewrite > (plus_n_O ((S m1)*(n / (S m1)))).
308 unfold lt.apply le_S_S.apply le_O_n.
311 theorem lt_div_n_m_n: \forall n,m:nat. (S O) < m \to O < n \to n / m \lt n.
313 apply (nat_case1 (n / m)).intro.
314 assumption.intros.rewrite < H2.
315 rewrite > (div_mod n m) in \vdash (? ? %).
316 apply (lt_to_le_to_lt ? ((n / m)*m)).
317 apply (lt_to_le_to_lt ? ((n / m)*(S (S O)))).
330 apply (trans_lt ? (S O)).
331 unfold lt. apply le_n.assumption.
334 theorem eq_div_div_div_times: \forall n,m,q. O < n \to O < m \to
337 apply (div_mod_spec_to_eq q (n*m) ? (q\mod n+n*(q/n\mod m)) ? (mod q (n*m)))
338 [apply div_mod_spec_intro
339 [apply (lt_to_le_to_lt ? (n*(S (q/n\mod m))))
340 [rewrite < times_n_Sm.
348 |rewrite > sym_times in ⊢ (? ? ? (? (? ? %) ?)).
349 rewrite < assoc_times.
350 rewrite > (eq_times_div_minus_mod ? ? H1).
352 rewrite > distributive_times_minus.
354 rewrite > (eq_times_div_minus_mod ? ? H).
355 rewrite < sym_plus in ⊢ (? ? ? (? ? %)).
356 rewrite < assoc_plus.
357 rewrite < plus_minus_m_m
358 [rewrite < plus_minus_m_m
360 |apply (eq_plus_to_le ? ? ((q/n)*n)).
365 |apply (trans_le ? (n*(q/n)))
367 apply (eq_plus_to_le ? ? ((q/n)/m*m)).
371 |rewrite > sym_times.
372 rewrite > eq_times_div_minus_mod
379 |apply div_mod_spec_div_mod.
380 rewrite > (times_n_O O).
381 apply lt_times;assumption
385 theorem eq_div_div_div_div: \forall n,m,q. O < n \to O < m \to
388 apply (trans_eq ? ? (q/(n*m)))
389 [apply eq_div_div_div_times;assumption
390 |rewrite > sym_times.
392 apply eq_div_div_div_times;assumption
396 theorem SSO_mod: \forall n,m. O < m \to (S(S O))*n/m = (n/m)*(S(S O)) + mod ((S(S O))*n/m) (S(S O)).
398 rewrite < (lt_O_to_div_times n (S(S O))) in ⊢ (? ? ? (? (? (? % ?) ?) ?))
399 [rewrite > eq_div_div_div_div
400 [rewrite > sym_times in ⊢ (? ? ? (? (? (? (? % ?) ?) ?) ?)).
409 (* Forall a,b : N. 0 < b \to b * (a/b) <= a < b * (a/b +1) *)
410 (* The theorem is shown in two different parts: *)
412 theorem lt_to_div_to_and_le_times_lt_S: \forall a,b,c:nat.
413 O \lt b \to a/b = c \to (b*c \le a \land a \lt b*(S c)).
418 rewrite > eq_times_div_minus_mod
419 [ apply (le_minus_m a (a \mod b))
422 | rewrite < (times_n_Sm b c).
425 rewrite > (div_mod a b) in \vdash (? % ?)
426 [ rewrite > (sym_plus b ((a/b)*b)).
435 theorem lt_to_le_times_to_lt_S_to_div: \forall a,c,b:nat.
436 O \lt b \to (b*c) \le a \to a \lt (b*(S c)) \to a/b = c.
438 apply (le_to_le_to_eq)
439 [ apply (leb_elim (a/b) c);intros
443 apply (lt_to_not_le (a \mod b) O)
444 [ apply (lt_plus_to_lt_l ((a/b)*b)).
448 [ apply (lt_to_le_to_lt ? (b*(S c)) ?)
450 | rewrite > (sym_times (a/b) b).
458 | apply not_le_to_lt.
462 | apply (leb_elim c (a/b));intros
466 apply (lt_to_not_le (a \mod b) b)
467 [ apply (lt_mod_m_m).
469 | apply (le_plus_to_le ((a/b)*b)).
470 rewrite < (div_mod a b)
471 [ apply (trans_le ? (b*c) ?)
472 [ rewrite > (sym_times (a/b) b).
473 rewrite > (times_n_SO b) in \vdash (? (? ? %) ?).
474 rewrite < distr_times_plus.
476 simplify in \vdash (? (? ? %) ?).
484 | apply not_le_to_lt.
492 theorem lt_to_lt_to_eq_div_div_times_times: \forall a,b,c:nat.
493 O \lt c \to O \lt b \to (a/b) = (a*c)/(b*c).
496 cut (b*(a/b) \le a \land a \lt b*(S (a/b)))
498 apply lt_to_le_times_to_lt_S_to_div
499 [ rewrite > (S_pred b)
500 [ rewrite > (S_pred c)
501 [ apply (lt_O_times_S_S)
506 | rewrite > assoc_times.
507 rewrite > (sym_times c (a/b)).
508 rewrite < assoc_times.
509 rewrite > (sym_times (b*(a/b)) c).
510 rewrite > (sym_times a c).
511 apply (le_times_r c (b*(a/b)) a).
513 | rewrite > (sym_times a c).
514 rewrite > (assoc_times ).
515 rewrite > (sym_times c (S (a/b))).
516 rewrite < (assoc_times).
517 rewrite > (sym_times (b*(S (a/b))) c).
518 apply (lt_times_r1 c a (b*(S (a/b))));
521 | apply (lt_to_div_to_and_le_times_lt_S)
528 theorem times_mod: \forall a,b,c:nat.
529 O \lt c \to O \lt b \to ((a*c) \mod (b*c)) = c*(a\mod b).
531 apply (div_mod_spec_to_eq2 (a*c) (b*c) (a/b) ((a*c) \mod (b*c)) (a/b) (c*(a \mod b)))
532 [ rewrite > (lt_to_lt_to_eq_div_div_times_times a b c)
533 [ apply div_mod_spec_div_mod.
535 [ rewrite > (S_pred c)
536 [ apply lt_O_times_S_S
544 | apply div_mod_spec_intro
545 [ rewrite > (sym_times b c).
546 apply (lt_times_r1 c)
548 | apply (lt_mod_m_m).
551 | rewrite < (assoc_times (a/b) b c).
552 rewrite > (sym_times a c).
553 rewrite > (sym_times ((a/b)*b) c).
554 rewrite < (distr_times_plus c ? ?).
565 (* general properties of functions *)
566 theorem monotonic_to_injective: \forall f:nat\to nat.
567 monotonic nat lt f \to injective nat nat f.
568 unfold injective.intros.
569 apply (nat_compare_elim x y).
570 intro.apply False_ind.apply (not_le_Sn_n (f x)).
571 rewrite > H1 in \vdash (? ? %).
572 change with (f x < f y).
575 intro.apply False_ind.apply (not_le_Sn_n (f y)).
576 rewrite < H1 in \vdash (? ? %).
577 change with (f y < f x).
581 theorem increasing_to_injective: \forall f:nat\to nat.
582 increasing f \to injective nat nat f.
583 intros.apply monotonic_to_injective.
584 apply increasing_to_monotonic.assumption.