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 "higher_order_defs/functions.ma". *)
16 include "hints_declaration.ma".
17 include "basics/functions.ma".
18 include "basics/eq.ma".
20 ninductive nat : Type[0] ≝
24 interpretation "Natural numbers" 'N = nat.
26 alias num (instance 0) = "nnatural number".
30 {n:>nat; is_pos: n ≠ 0}.
32 ncoercion nat_to_pos: ∀n:nat. n ≠0 →pos ≝ mk_pos on
35 (* default "natural numbers" cic:/matita/ng/arithmetics/nat/nat.ind.
39 λn. match n with [ O ⇒ O | (S p) ⇒ p].
41 ntheorem pred_Sn : ∀n. n = pred (S n).
44 ntheorem injective_S : injective nat nat S.
48 ntheorem inj_S : \forall n,m:nat.(S n)=(S m) \to n=m.
51 ntheorem not_eq_S: ∀n,m:nat. n ≠ m → S n ≠ S m.
54 ndefinition not_zero: nat → Prop ≝
56 [ O ⇒ False | (S p) ⇒ True ].
58 ntheorem not_eq_O_S : ∀n:nat. O ≠ S n.
59 #n; #eqOS; nchange with (not_zero O); nrewrite > eqOS; //.
62 ntheorem not_eq_n_Sn : ∀n:nat. n ≠ S n.
63 #n; nelim n; /2/; nqed.
67 (n=O → P O) → (∀m:nat. (n=(S m) → P (S m))) → P n.
68 #n; #P; nelim n; /2/; nqed.
74 → (∀n,m:nat. R n m → R (S n) (S m))
76 #R; #ROn; #RSO; #RSS; #n; nelim n;//;
77 #n0; #Rn0m; #m; ncases m;/2/; nqed.
79 ntheorem decidable_eq_nat : ∀n,m:nat.decidable (n=m).
83 ##| #m; #Hind; ncases Hind; /3/;
87 (*************************** plus ******************************)
92 | S p ⇒ S (plus p m) ].
94 interpretation "natural plus" 'plus x y = (plus x y).
96 ntheorem plus_O_n: ∀n:nat. n = 0+n.
100 ntheorem plus_Sn_m: ∀n,m:nat. S (n + m) = S n + m.
104 ntheorem plus_n_O: ∀n:nat. n = n+0.
105 #n; nelim n; nnormalize; //; nqed.
107 ntheorem plus_n_Sm : ∀n,m:nat. S (n+m) = n + S m.
108 #n; nelim n; nnormalize; //; nqed.
111 ntheorem plus_Sn_m1: ∀n,m:nat. S m + n = n + S m.
112 #n; nelim n; nnormalize; //; nqed.
116 ntheorem plus_n_SO : ∀n:nat. S n = n+S O.
119 ntheorem symmetric_plus: symmetric ? plus.
120 #n; nelim n; nnormalize; //; nqed.
122 ntheorem associative_plus : associative nat plus.
123 #n; nelim n; nnormalize; //; nqed.
125 ntheorem assoc_plus1: ∀a,b,c. c + (b + a) = b + c + a.
128 ntheorem injective_plus_r: ∀n:nat.injective nat nat (λm.n+m).
129 #n; nelim n; nnormalize; /3/; nqed.
131 (* ntheorem inj_plus_r: \forall p,n,m:nat. p+n = p+m \to n=m
132 \def injective_plus_r.
134 ntheorem injective_plus_l: ∀m:nat.injective nat nat (λn.n+m).
137 (* ntheorem inj_plus_l: \forall p,n,m:nat. n+p = m+p \to n=m
138 \def injective_plus_l. *)
140 (*************************** times *****************************)
145 | S p ⇒ m+(times p m) ].
147 interpretation "natural times" 'times x y = (times x y).
149 ntheorem times_Sn_m: ∀n,m:nat. m+n*m = S n*m.
152 ntheorem times_O_n: ∀n:nat. O = O*n.
155 ntheorem times_n_O: ∀n:nat. O = n*O.
156 #n; nelim n; //; nqed.
158 ntheorem times_n_Sm : ∀n,m:nat. n+(n*m) = n*(S m).
159 #n; nelim n; nnormalize; //; nqed.
161 ntheorem symmetric_times : symmetric nat times.
162 #n; nelim n; nnormalize; //; nqed.
164 (* variant sym_times : \forall n,m:nat. n*m = m*n \def
167 ntheorem distributive_times_plus : distributive nat times plus.
168 #n; nelim n; nnormalize; //; nqed.
170 ntheorem distributive_times_plus_r:
171 \forall a,b,c:nat. (b+c)*a = b*a + c*a.
174 ntheorem associative_times: associative nat times.
175 #n; nelim n; nnormalize; //; nqed.
177 nlemma times_times: ∀x,y,z. x*(y*z) = y*(x*z).
180 (* ci servono questi risultati?
181 ntheorem times_O_to_O: ∀n,m:nat.n*m=O → n=O ∨ m=O.
182 napply nat_elim2; /2/;
183 #n; #m; #H; nnormalize; #H1; napply False_ind;napply not_eq_O_S;
186 ntheorem times_n_SO : ∀n:nat. n = n * S O.
189 ntheorem times_SSO_n : ∀n:nat. n + n = (S(S O)) * n.
190 nnormalize; //; nqed.
192 nlemma times_SSO: \forall n.(S(S O))*(S n) = S(S((S(S O))*n)).
195 ntheorem or_eq_eq_S: \forall n.\exists m.
196 n = (S(S O))*m \lor n = S ((S(S O))*m).
199 ##|#a; #H; nelim H; #b;#or;nelim or;#aeq;
201 ##|@ (S b); @ 1; /2/;
206 (******************** ordering relations ************************)
208 ninductive le (n:nat) : nat → Prop ≝
210 | le_S : ∀ m:nat. le n m → le n (S m).
212 interpretation "natural 'less or equal to'" 'leq x y = (le x y).
214 interpretation "natural 'neither less nor equal to'" 'nleq x y = (Not (le x y)).
216 ndefinition lt: nat → nat → Prop ≝
219 interpretation "natural 'less than'" 'lt x y = (lt x y).
221 interpretation "natural 'not less than'" 'nless x y = (Not (lt x y)).
223 ndefinition ge: nat \to nat \to Prop \def
224 \lambda n,m:nat.m \leq n.
226 interpretation "natural 'greater or equal to'" 'geq x y = (ge x y).
228 ndefinition gt: nat \to nat \to Prop \def
231 interpretation "natural 'greater than'" 'gt x y = (gt x y).
233 interpretation "natural 'not greater than'" 'ngtr x y = (Not (gt x y)).
235 ntheorem transitive_le : transitive nat le.
236 #a; #b; #c; #leab; #lebc;nelim lebc;/2/;
240 ntheorem trans_le: \forall n,m,p:nat. n \leq m \to m \leq p \to n \leq p
241 \def transitive_le. *)
243 ntheorem transitive_lt: transitive nat lt.
244 #a; #b; #c; #ltab; #ltbc;nelim ltbc;/2/;nqed.
247 theorem trans_lt: \forall n,m,p:nat. lt n m \to lt m p \to lt n p
248 \def transitive_lt. *)
250 ntheorem le_S_S: ∀n,m:nat. n ≤ m → S n ≤ S m.
251 #n; #m; #lenm; nelim lenm; /2/; nqed.
253 ntheorem le_O_n : ∀n:nat. O ≤ n.
254 #n; nelim n; /2/; nqed.
256 ntheorem le_n_Sn : ∀n:nat. n ≤ S n.
259 ntheorem le_pred_n : ∀n:nat. pred n ≤ n.
260 #n; nelim n; //; nqed.
262 (* XXX global problem *)
263 nlemma my_trans_le : ∀x,y,z:nat.x ≤ y → y ≤ z → x ≤ z.
264 napply transitive_le.
267 ntheorem monotonic_pred: monotonic ? le pred.
268 #n; #m; #lenm; nelim lenm; /2/; nqed.
270 ntheorem le_S_S_to_le: ∀n,m:nat. S n ≤ S m → n ≤ m.
271 (* XXX *) nletin hint ≝ monotonic. /2/; nqed.
273 ntheorem lt_S_S_to_lt: ∀n,m. S n < S m → n < m.
276 ntheorem lt_to_lt_S_S: ∀n,m. n < m → S n < S m.
279 ntheorem lt_to_not_zero : ∀n,m:nat. n < m → not_zero m.
280 #n; #m; #Hlt; nelim Hlt;//; nqed.
283 ntheorem not_le_Sn_O: ∀ n:nat. S n ≰ O.
284 #n; #Hlen0; napply (lt_to_not_zero ?? Hlen0); nqed.
286 ntheorem not_le_to_not_le_S_S: ∀ n,m:nat. n ≰ m → S n ≰ S m.
289 ntheorem not_le_S_S_to_not_le: ∀ n,m:nat. S n ≰ S m → n ≰ m.
292 ntheorem decidable_le: ∀n,m. decidable (n≤m).
293 napply nat_elim2; #n; /3/;
294 #m; #dec; ncases dec;/4/; nqed.
296 ntheorem decidable_lt: ∀n,m. decidable (n < m).
297 #n; #m; napply decidable_le ; nqed.
299 ntheorem not_le_Sn_n: ∀n:nat. S n ≰ n.
300 #n; nelim n; /3/; nqed.
302 ntheorem lt_S_to_le: ∀n,m:nat. n < S m → n ≤ m.
305 ntheorem not_le_to_lt: ∀n,m. n ≰ m → m < n.
306 napply nat_elim2; #n;
307 ##[#abs; napply False_ind;/2/;
309 ##|#m;#Hind;#HnotleSS; napply lt_to_lt_S_S;/4/;
313 ntheorem lt_to_not_le: ∀n,m. n < m → m ≰ n.
314 #n; #m; #Hltnm; nelim Hltnm;/3/; nqed.
316 ntheorem not_lt_to_le: ∀n,m:nat. n ≮ m → m ≤ n.
317 #n; #m; #Hnlt; napply lt_S_to_le;
318 (* something strange here: /2/ fails:
319 we need an extra depths for unfolding not *)
320 napply not_le_to_lt; napply Hnlt; nqed.
322 ntheorem le_to_not_lt: ∀n,m:nat. n ≤ m → m ≮ n.
325 (* lt and le trans *)
327 ntheorem lt_to_le_to_lt: ∀n,m,p:nat. n < m → m ≤ p → n < p.
328 #n; #m; #p; #H; #H1; nelim H1; /2/; nqed.
330 ntheorem le_to_lt_to_lt: ∀n,m,p:nat. n ≤ m → m < p → n < p.
331 #n; #m; #p; #H; nelim H; /3/; nqed.
333 ntheorem lt_S_to_lt: ∀n,m. S n < m → n < m.
336 ntheorem ltn_to_ltO: ∀n,m:nat. n < m → O < m.
340 theorem lt_SO_n_to_lt_O_pred_n: \forall n:nat.
341 (S O) \lt n \to O \lt (pred n).
343 apply (ltn_to_ltO (pred (S O)) (pred n) ?).
344 apply (lt_pred (S O) n);
350 ntheorem lt_O_n_elim: ∀n:nat. O < n →
351 ∀P:nat → Prop.(∀m:nat.P (S m)) → P n.
352 #n; nelim n; //; #abs; napply False_ind; /2/; nqed.
355 theorem lt_pred: \forall n,m.
356 O < n \to n < m \to pred n < pred m.
358 [intros.apply False_ind.apply (not_le_Sn_O ? H)
359 |intros.apply False_ind.apply (not_le_Sn_O ? H1)
360 |intros.simplify.unfold.apply le_S_S_to_le.assumption
364 theorem S_pred: \forall n:nat.lt O n \to eq nat n (S (pred n)).
365 intro.elim n.apply False_ind.exact (not_le_Sn_O O H).
366 apply eq_f.apply pred_Sn.
369 theorem le_pred_to_le:
370 ∀n,m. O < m → pred n ≤ pred m → n ≤ m.
375 rewrite > (S_pred m);
386 ntheorem le_to_or_lt_eq: ∀n,m:nat. n ≤ m → n < m ∨ n = m.
387 #n; #m; #lenm; nelim lenm; /3/; nqed.
390 ntheorem lt_to_not_eq : ∀n,m:nat. n < m → n ≠ m.
394 ntheorem eq_to_not_lt: ∀a,b:nat. a = b → a ≮ b.
399 apply (lt_to_not_eq b b)
405 theorem lt_n_m_to_not_lt_m_Sn: ∀n,m. n < m → m ≮ S n.
411 generalize in match (le_S_S ? ? H);
413 generalize in match (transitive_le ? ? ? H2 H1);
415 apply (not_le_Sn_n ? H3).
418 ntheorem not_eq_to_le_to_lt: ∀n,m. n≠m → n≤m → n<m.
419 #n; #m; #Hneq; #Hle; ncases (le_to_or_lt_eq ?? Hle); //;
420 #Heq; nelim (Hneq Heq); nqed.
423 ntheorem le_n_O_to_eq : ∀n:nat. n ≤ O → O=n.
424 #n; ncases n; //; #a ; #abs; nelim (not_le_Sn_O ? abs); nqed.
426 ntheorem le_n_O_elim: ∀n:nat. n ≤ O → ∀P: nat →Prop. P O → P n.
427 #n; ncases n; //; #a; #abs; nelim (not_le_Sn_O ? abs); nqed.
429 ntheorem le_n_Sm_elim : ∀n,m:nat.n ≤ S m →
430 ∀P:Prop. (S n ≤ S m → P) → (n=S m → P) → P.
431 #n; #m; #Hle; #P; nelim Hle; /3/; nqed.
435 ntheorem le_to_le_to_eq: ∀n,m. n ≤ m → m ≤ n → n = m.
436 napply nat_elim2; /4/; nqed.
438 ntheorem lt_O_S : ∀n:nat. O < S n.
442 (* other abstract properties *)
443 theorem antisymmetric_le : antisymmetric nat le.
444 unfold antisymmetric.intros 2.
445 apply (nat_elim2 (\lambda n,m.(n \leq m \to m \leq n \to n=m))).
446 intros.apply le_n_O_to_eq.assumption.
447 intros.apply False_ind.apply (not_le_Sn_O ? H).
448 intros.apply eq_f.apply H.
449 apply le_S_S_to_le.assumption.
450 apply le_S_S_to_le.assumption.
453 theorem antisym_le: \forall n,m:nat. n \leq m \to m \leq n \to n=m
454 \def antisymmetric_le.
456 theorem le_n_m_to_lt_m_Sn_to_eq_n_m: ∀n,m. n ≤ m → m < S n → n=m.
459 generalize in match (le_S_S_to_le ? ? H1);
466 (* well founded induction principles *)
468 ntheorem nat_elim1 : ∀n:nat.∀P:nat → Prop.
469 (∀m.(∀p. p < m → P p) → P m) → P n.
471 ncut (∀q:nat. q ≤ n → P q);/2/;
473 ##[#q; #HleO; (* applica male *)
474 napply (le_n_O_elim ? HleO);
476 napply False_ind; /2/;
477 ##|#p; #Hind; #q; #HleS;
478 napply H; #a; #lta; napply Hind;
479 napply le_S_S_to_le;/2/;
483 (* some properties of functions *)
485 definition increasing \def \lambda f:nat \to nat.
486 \forall n:nat. f n < f (S n).
488 theorem increasing_to_monotonic: \forall f:nat \to nat.
489 increasing f \to monotonic nat lt f.
490 unfold monotonic.unfold lt.unfold increasing.unfold lt.intros.elim H1.apply H.
491 apply (trans_le ? (f n1)).
492 assumption.apply (trans_le ? (S (f n1))).
497 theorem le_n_fn: \forall f:nat \to nat. (increasing f)
498 \to \forall n:nat. n \le (f n).
501 apply (trans_le ? (S (f n1))).
502 apply le_S_S.apply H1.
503 simplify in H. unfold increasing in H.unfold lt in H.apply H.
506 theorem increasing_to_le: \forall f:nat \to nat. (increasing f)
507 \to \forall m:nat. \exists i. m \le (f i).
509 apply (ex_intro ? ? O).apply le_O_n.
511 apply (ex_intro ? ? (S a)).
512 apply (trans_le ? (S (f a))).
513 apply le_S_S.assumption.
514 simplify in H.unfold increasing in H.unfold lt in H.
518 theorem increasing_to_le2: \forall f:nat \to nat. (increasing f)
519 \to \forall m:nat. (f O) \le m \to
520 \exists i. (f i) \le m \land m <(f (S i)).
522 apply (ex_intro ? ? O).
523 split.apply le_n.apply H.
525 cut ((S n1) < (f (S a)) \lor (S n1) = (f (S a))).
527 apply (ex_intro ? ? a).
528 split.apply le_S. assumption.assumption.
529 apply (ex_intro ? ? (S a)).
530 split.rewrite < H7.apply le_n.
533 apply le_to_or_lt_eq.apply H6.
537 (*********************** monotonicity ***************************)
538 ntheorem monotonic_le_plus_r:
539 ∀n:nat.monotonic nat le (λm.n + m).
540 #n; #a; #b; nelim n; nnormalize; //;
541 #m; #H; #leab;napply le_S_S; /2/; nqed.
544 ntheorem le_plus_r: ∀p,n,m:nat. n ≤ m → p + n ≤ p + m
545 ≝ monotonic_le_plus_r. *)
547 ntheorem monotonic_le_plus_l:
548 ∀m:nat.monotonic nat le (λn.n + m).
552 ntheorem le_plus_l: \forall p,n,m:nat. n \le m \to n + p \le m + p
553 \def monotonic_le_plus_l. *)
555 ntheorem le_plus: ∀n1,n2,m1,m2:nat. n1 ≤ n2 \to m1 ≤ m2
557 #n1; #n2; #m1; #m2; #len; #lem; napply transitive_le;
560 ntheorem le_plus_n :∀n,m:nat. m ≤ n + m.
563 ntheorem le_plus_n_r :∀n,m:nat. m ≤ m + n.
566 ntheorem eq_plus_to_le: ∀n,m,p:nat.n=m+p → m ≤ n.
569 ntheorem le_plus_to_le: ∀a,n,m. a + n ≤ a + m → n ≤ m.
570 #a; nelim a; /3/; nqed.
572 ntheorem le_plus_to_le_r: ∀a,n,m. n + a ≤ m +a → n ≤ m.
577 ntheorem monotonic_lt_plus_r:
578 ∀n:nat.monotonic nat lt (λm.n+m).
582 variant lt_plus_r: \forall n,p,q:nat. p < q \to n + p < n + q \def
583 monotonic_lt_plus_r. *)
585 ntheorem monotonic_lt_plus_l:
586 ∀n:nat.monotonic nat lt (λm.m+n).
590 variant lt_plus_l: \forall n,p,q:nat. p < q \to p + n < q + n \def
591 monotonic_lt_plus_l. *)
593 ntheorem lt_plus: ∀n,m,p,q:nat. n < m → p < q → n + p < m + q.
594 #n; #m; #p; #q; #ltnm; #ltpq;
595 napply (transitive_lt ? (n+q));/2/; nqed.
597 ntheorem lt_plus_to_lt_l :∀n,p,q:nat. p+n < q+n → p<q.
600 ntheorem lt_plus_to_lt_r :∀n,p,q:nat. n+p < n+q → p<q.
603 ntheorem le_to_lt_to_plus_lt: ∀a,b,c,d:nat.
604 a ≤ c → b < d → a + b < c+d.
605 (* bello /2/ un po' lento *)
606 #a; #b; #c; #d; #leac; #lebd;
607 nnormalize; napplyS le_plus; //; nqed.
610 ntheorem monotonic_le_times_r:
611 ∀n:nat.monotonic nat le (λm. n * m).
612 #n; #x; #y; #lexy; nelim n; nnormalize;//;(* lento /2/;*)
613 #a; #lea; napply le_plus; //;
617 ntheorem le_times_r: \forall p,n,m:nat. n \le m \to p*n \le p*m
618 \def monotonic_le_times_r. *)
620 ntheorem monotonic_le_times_l:
621 ∀m:nat.monotonic nat le (λn.n*m).
625 theorem le_times_l: \forall p,n,m:nat. n \le m \to n*p \le m*p
626 \def monotonic_le_times_l. *)
628 ntheorem le_times: ∀n1,n2,m1,m2:nat.
629 n1 ≤ n2 → m1 ≤ m2 → n1*m1 ≤ n2*m2.
630 #n1; #n2; #m1; #m2; #len; #lem;
631 napply transitive_le; (* /2/ slow *)
632 ##[ ##| napply monotonic_le_times_l;//;
633 ##| napply monotonic_le_times_r;//;
637 ntheorem lt_times_n: ∀n,m:nat. O < n → m ≤ n*m.
641 ntheorem le_times_to_le:
642 ∀a,n,m. O < a → a * n ≤ a * m → n ≤ m.
643 #a; napply nat_elim2; nnormalize;
645 ##|#n; #H1; #H2; napply False_ind;
646 ngeneralize in match H2;
648 napply (transitive_le ? (S n));/2/;
649 ##|#n; #m; #H; #lta; #le;
650 napply le_S_S; napply H; /2/;
654 ntheorem le_S_times_2: ∀n,m.O < m → n ≤ m → n < 2*m.
655 #n; #m; #posm; #lenm; (* interessante *)
656 nnormalize; napplyS (le_plus n); //; nqed.
660 ntheorem lt_O_times_S_S: ∀n,m:nat.O < (S n)*(S m).
661 intros.simplify.unfold lt.apply le_S_S.apply le_O_n.
665 ntheorem lt_times_eq_O: \forall a,b:nat.
666 O < a → a * b = O → b = O.
673 rewrite > (S_pred a) in H1
675 apply (eq_to_not_lt O ((S (pred a))*(S m)))
678 | apply lt_O_times_S_S
685 theorem O_lt_times_to_O_lt: \forall a,c:nat.
686 O \lt (a * c) \to O \lt a.
698 lemma lt_times_to_lt_O: \forall i,n,m:nat. i < n*m \to O < m.
700 elim (le_to_or_lt_eq O ? (le_O_n m))
704 rewrite < times_n_O in H.
705 apply (not_le_Sn_O ? H)
710 ntheorem monotonic_lt_times_r:
711 ∀n:nat.monotonic nat lt (λm.(S n)*m).
715 simplify.rewrite < plus_n_O.rewrite < plus_n_O.assumption.
716 apply lt_plus.assumption.assumption.
719 ntheorem monotonic_lt_times_l:
720 ∀c:nat. O < c → monotonic nat lt (λt.(t*c)).
721 #c; #posc; #n; #m; #ltnm;
722 nelim ltnm; nnormalize;
723 ##[napplyS monotonic_lt_plus_l;//;
724 ##|#a; #_; #lt1; napply (transitive_le ??? lt1);//;
728 ntheorem monotonic_lt_times_r:
729 ∀c:nat. O < c → monotonic nat lt (λt.(c*t)).
731 #c; #posc; #n; #m; #ltnm;
732 (* why?? napplyS (monotonic_lt_times_l c posc n m ltnm); *)
733 nrewrite > (symmetric_times c n);
734 nrewrite > (symmetric_times c m);
735 napply monotonic_lt_times_l;//;
738 ntheorem lt_to_le_to_lt_times:
739 ∀n,m,p,q:nat. n < m → p ≤ q → O < q → n*p < m*q.
740 #n; #m; #p; #q; #ltnm; #lepq; #posq;
741 napply (le_to_lt_to_lt ? (n*q));
742 ##[napply monotonic_le_times_r;//;
743 ##|napply monotonic_lt_times_l;//;
747 ntheorem lt_times:∀n,m,p,q:nat. n<m → p<q → n*p < m*q.
748 #n; #m; #p; #q; #ltnm; #ltpq;
749 napply lt_to_le_to_lt_times;/2/;
752 ntheorem lt_times_n_to_lt_l:
753 ∀n,p,q:nat. O < n → p*n < q*n → p < q.
754 #n; #p; #q; #posn; #Hlt;
755 nelim (decidable_lt p q);//;
756 #nltpq;napply False_ind;
757 napply (lt_to_not_le ? ? Hlt);
758 napply monotonic_le_times_l;/3/;
761 ntheorem lt_times_n_to_lt_r:
762 ∀n,p,q:nat. O < n → n*p < n*q → p < q.
763 #n; #p; #q; #posn; #Hlt;
764 napply (lt_times_n_to_lt_l ??? posn);//;
768 theorem nat_compare_times_l : \forall n,p,q:nat.
769 nat_compare p q = nat_compare ((S n) * p) ((S n) * q).
770 intros.apply nat_compare_elim.intro.
771 apply nat_compare_elim.
774 apply (inj_times_r n).assumption.
775 apply lt_to_not_eq. assumption.
777 apply (lt_times_to_lt_r n).assumption.
778 apply le_to_not_lt.apply lt_to_le.assumption.
779 intro.rewrite < H.rewrite > nat_compare_n_n.reflexivity.
780 intro.apply nat_compare_elim.intro.
782 apply (lt_times_to_lt_r n).assumption.
783 apply le_to_not_lt.apply lt_to_le.assumption.
786 apply (inj_times_r n).assumption.
787 apply lt_to_not_eq.assumption.
792 theorem lt_times_plus_times: \forall a,b,n,m:nat.
793 a < n \to b < m \to a*m + b < n*m.
796 [intros.apply False_ind.apply (not_le_Sn_O ? H)
800 change with (S b+a*m1 \leq m1+m*m1).
804 [apply le_S_S_to_le.assumption
811 (************************** minus ******************************)
819 | S q ⇒ minus p q ]].
821 interpretation "natural minus" 'minus x y = (minus x y).
823 ntheorem minus_S_S: ∀n,m:nat.S n - S m = n -m.
826 ntheorem minus_O_n: ∀n:nat.O=O-n.
827 #n; ncases n; //; nqed.
829 ntheorem minus_n_O: ∀n:nat.n=n-O.
830 #n; ncases n; //; nqed.
832 ntheorem minus_n_n: ∀n:nat.O=n-n.
833 #n; nelim n; //; nqed.
835 ntheorem minus_Sn_n: ∀n:nat. S O = (S n)-n.
836 #n; nelim n; //; nqed.
838 ntheorem minus_Sn_m: ∀m,n:nat. m ≤ n → S n -m = S (n-m).
839 (* qualcosa da capire qui
840 #n; #m; #lenm; nelim lenm; napplyS refl_eq. *)
843 ##|#n; #abs; napply False_ind; (* XXX *) napply not_le_Sn_O; /2/.
844 ##|#n; #m; #Hind; #c; napplyS Hind; /2/;
848 ntheorem not_eq_to_le_to_le_minus:
849 ∀n,m.n ≠ m → n ≤ m → n ≤ m - 1.
850 #n; #m; ncases m;//; #m; nnormalize;
851 #H; #H1; napply le_S_S_to_le;
852 napplyS (not_eq_to_le_to_lt n (S m) H H1);
855 ntheorem eq_minus_S_pred: ∀n,m. n - (S m) = pred(n -m).
856 napply nat_elim2; //; nqed.
859 ∀m,n,p:nat. m ≤ n → (n-m)+p = (n+p)-m.
862 ##|#n; #p; #abs; napply False_ind; (* XXX *) napply not_le_Sn_O; /2/;
867 ntheorem minus_plus_m_m: ∀n,m:nat.n = (n+m)-m.
868 #n; #m; napplyS (plus_minus m m n); //; nqed.
870 ntheorem plus_minus_m_m: ∀n,m:nat.
871 m \leq n \to n = (n-m)+m.
872 #n; #m; #lemn; napplyS symmetric_eq;
873 napplyS (plus_minus m n m); //; nqed.
875 ntheorem le_plus_minus_m_m: ∀n,m:nat. n ≤ (n-m)+m.
878 ##|#a; #Hind; #m; ncases m;//;
879 nnormalize; #n;napplyS le_S_S;//
883 ntheorem minus_to_plus :∀n,m,p:nat.
884 m ≤ n → n-m = p → n = m+p.
885 #n; #m; #p; #lemn; #eqp; napplyS plus_minus_m_m; //;
888 ntheorem plus_to_minus :∀n,m,p:nat.n = m+p → n-m = p.
889 (* /4/ done in 43.5 *)
892 napplyS (minus_plus_m_m p m);
895 ntheorem minus_pred_pred : ∀n,m:nat. O < n → O < m →
896 pred n - pred m = n - m.
897 #n; #m; #posn; #posm;
898 napply (lt_O_n_elim n posn);
899 napply (lt_O_n_elim m posm);//.
903 theorem eq_minus_n_m_O: \forall n,m:nat.
904 n \leq m \to n-m = O.
906 apply (nat_elim2 (\lambda n,m.n \leq m \to n-m = O)).
907 intros.simplify.reflexivity.
908 intros.apply False_ind.
912 simplify.apply H.apply le_S_S_to_le. apply H1.
915 theorem le_SO_minus: \forall n,m:nat.S n \leq m \to S O \leq m-n.
916 intros.elim H.elim (minus_Sn_n n).apply le_n.
917 rewrite > minus_Sn_m.
918 apply le_S.assumption.
919 apply lt_to_le.assumption.
922 theorem minus_le_S_minus_S: \forall n,m:nat. m-n \leq S (m-(S n)).
924 apply (nat_elim2 (\lambda n,m.m-n \leq S (m-(S n)))).
925 intro.elim n1.simplify.apply le_n_Sn.
926 simplify.rewrite < minus_n_O.apply le_n.
927 intros.simplify.apply le_n_Sn.
928 intros.simplify.apply H.
931 theorem lt_minus_S_n_to_le_minus_n : \forall n,m,p:nat. m-(S n) < p \to m-n \leq p.
934 (* end auto($Revision: 9739 $) proof: TIME=1.33 SIZE=100 DEPTH=100 *)
935 apply (trans_le (m-n) (S (m-(S n))) p).
936 apply minus_le_S_minus_S.
940 theorem le_minus_m: \forall n,m:nat. n-m \leq n.
941 intros.apply (nat_elim2 (\lambda m,n. n-m \leq n)).
942 intros.rewrite < minus_n_O.apply le_n.
943 intros.simplify.apply le_n.
944 intros.simplify.apply le_S.assumption.
947 theorem lt_minus_m: \forall n,m:nat. O < n \to O < m \to n-m \lt n.
948 intros.apply (lt_O_n_elim n H).intro.
949 apply (lt_O_n_elim m H1).intro.
950 simplify.unfold lt.apply le_S_S.apply le_minus_m.
953 theorem minus_le_O_to_le: \forall n,m:nat. n-m \leq O \to n \leq m.
955 apply (nat_elim2 (\lambda n,m:nat.n-m \leq O \to n \leq m)).
957 simplify.intros. assumption.
958 simplify.intros.apply le_S_S.apply H.assumption.
962 (* monotonicity and galois *)
964 ntheorem monotonic_le_minus_l:
965 ∀p,q,n:nat. q ≤ p → q-n ≤ p-n.
966 napply nat_elim2; #p; #q;
967 ##[#lePO; napply (le_n_O_elim ? lePO);//;
969 ##|#Hind; #n; ncases n;
971 ##|#a; #leSS; napply Hind; /2/;
976 ntheorem le_minus_to_plus: ∀n,m,p. n-m ≤ p → n≤ p+m.
978 napply transitive_le;
979 ##[##|napply le_plus_minus_m_m
980 ##|napply monotonic_le_plus_l;//;
984 ntheorem le_plus_to_minus: ∀n,m,p. n ≤ p+m → n-m ≤ p.
987 napplyS monotonic_le_minus_l;//;
990 ntheorem monotonic_le_minus_r:
991 ∀p,q,n:nat. q ≤ p → n-p ≤ n-q.
993 napply le_plus_to_minus;
994 napply (transitive_le ??? (le_plus_minus_m_m ? q));/2/;
997 (*********************** boolean arithmetics ********************)
998 include "basics/bool.ma".
1002 [ O ⇒ match m with [ O ⇒ true | S q ⇒ false]
1003 | S p ⇒ match m with [ O ⇒ false | S q ⇒ eqb p q]
1007 ntheorem eqb_to_Prop: ∀n,m:nat.
1008 match (eqb n m) with
1009 [ true \Rightarrow n = m
1010 | false \Rightarrow n \neq m].
1013 (\lambda n,m:nat.match (eqb n m) with
1014 [ true \Rightarrow n = m
1015 | false \Rightarrow n \neq m])).
1017 simplify.reflexivity.
1018 simplify.apply not_eq_O_S.
1020 simplify.unfold Not.
1021 intro. apply (not_eq_O_S n1).apply sym_eq.assumption.
1023 generalize in match H.
1025 simplify.apply eq_f.apply H1.
1026 simplify.unfold Not.intro.apply H1.apply inj_S.assumption.
1030 ntheorem eqb_elim : ∀ n,m:nat.∀ P:bool → Prop.
1031 (n=m → (P true)) → (n ≠ m → (P false)) → (P (eqb n m)).
1033 ##[#n; ncases n; nnormalize; /3/;
1034 ##|nnormalize; (* XXX *) nletin hint ≝ not_eq_O_S; /3/;
1036 ##] (* XXX rimane aperto *) #m; #P; #_; #H; napply H; napply not_eq_O_S.
1039 ntheorem eqb_n_n: ∀n. eqb n n = true.
1040 #n; nelim n; nnormalize; //.
1043 ntheorem eqb_true_to_eq: ∀n,m:nat. eqb n m = true → n = m.
1044 #n; #m; napply (eqb_elim n m);//;
1045 #_; #abs; napply False_ind; (* XXX *) nletin hint ≝ not_eq_true_false; /2/;
1048 ntheorem eqb_false_to_not_eq: ∀n,m:nat. eqb n m = false → n ≠ m.
1049 #n; #m; napply (eqb_elim n m);/2/;
1052 ntheorem eq_to_eqb_true: ∀n,m:nat.
1053 n = m → eqb n m = true.
1056 ntheorem not_eq_to_eqb_false: ∀n,m:nat.
1057 n ≠ m → eqb n m = false.
1059 nelim (true_or_false (eqb n m)); //;
1060 #Heq; napply False_ind; napply noteq;/2/;
1069 | (S q) ⇒ leb p q]].
1071 ntheorem leb_elim: ∀n,m:nat. ∀P:bool → Prop.
1072 (n ≤ m → P true) → (n ≰ m → P false) → P (leb n m).
1073 napply nat_elim2; nnormalize;
1075 ##| (* XXX *) nletin hint ≝ not_le_Sn_O; /3/;
1076 ##|#n; #m; #Hind; #P; #Pt; #Pf; napply Hind;
1077 ##[#lenm; napply Pt; napply le_S_S;//;
1078 ##|#nlenm; napply Pf; #leSS; /3/;
1083 ntheorem leb_true_to_le:∀n,m.leb n m = true → n ≤ m.
1084 #n; #m; napply leb_elim;
1086 ##|#_; #abs; napply False_ind; (* XXX *) nletin hint ≝ not_eq_true_false; /2/;
1090 ntheorem leb_false_to_not_le:∀n,m.
1091 leb n m = false → n ≰ m.
1092 #n; #m; napply leb_elim;
1093 ##[#_; #abs; napply False_ind; (* XXX *) nletin hint ≝ not_eq_true_false; /2/;
1098 ntheorem le_to_leb_true: ∀n,m. n ≤ m → leb n m = true.
1099 #n; #m; napply leb_elim; //;
1100 #H; #H1; napply False_ind; /2/;
1103 ntheorem lt_to_leb_false: ∀n,m. m < n → leb n m = false.
1104 #n; #m; napply leb_elim; //;
1105 #H; #H1; napply False_ind; /2/;
1109 ndefinition ltb ≝λn,m. leb (S n) m.
1111 ntheorem ltb_elim: ∀n,m:nat. ∀P:bool → Prop.
1112 (n < m → P true) → (n ≮ m → P false) → P (ltb n m).
1113 #n; #m; #P; #Hlt; #Hnlt;
1114 napply leb_elim; /3/; nqed.
1116 ntheorem ltb_true_to_lt:∀n,m.ltb n m = true → n < m.
1117 #n; #m; #Hltb; napply leb_true_to_le; nassumption;
1120 ntheorem ltb_false_to_not_lt:∀n,m.
1121 ltb n m = false → n ≮ m.
1122 #n; #m; #Hltb; napply leb_false_to_not_le; nassumption;
1125 ntheorem lt_to_ltb_true: ∀n,m. n < m → ltb n m = true.
1126 #n; #m; #Hltb; napply le_to_leb_true; nassumption;
1129 ntheorem le_to_ltb_false: ∀n,m. m \le n → ltb n m = false.
1130 #n; #m; #Hltb; napply lt_to_leb_false; /2/;