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 : \forall 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 ntheorem monotonic_pred: monotonic ? le pred.
263 #n; #m; #lenm; nelim lenm; /2/; nqed.
265 ntheorem le_S_S_to_le: ∀n,m:nat. S n ≤ S m → n ≤ m.
268 ntheorem lt_S_S_to_lt: ∀n,m. S n < S m \to n < m.
271 ntheorem lt_to_lt_S_S: ∀n,m. n < m → S n < S m.
274 ntheorem lt_to_not_zero : ∀n,m:nat. n < m → not_zero m.
275 #n; #m; #Hlt; nelim Hlt;//; nqed.
278 ntheorem not_le_Sn_O: ∀ n:nat. S n ≰ O.
279 #n; #Hlen0; napply (lt_to_not_zero ?? Hlen0); nqed.
281 ntheorem not_le_to_not_le_S_S: ∀ n,m:nat. n ≰ m → S n ≰ S m.
284 ntheorem not_le_S_S_to_not_le: ∀ n,m:nat. S n ≰ S m → n ≰ m.
287 ntheorem decidable_le: ∀n,m. decidable (n≤m).
288 napply nat_elim2; #n; /2/;
289 #m; #dec; ncases dec;/3/; nqed.
291 ntheorem decidable_lt: ∀n,m. decidable (n < m).
292 #n; #m; napply decidable_le ; nqed.
294 ntheorem not_le_Sn_n: ∀n:nat. S n ≰ n.
295 #n; nelim n; /2/; nqed.
297 ntheorem lt_S_to_le: ∀n,m:nat. n < S m → n ≤ m.
300 ntheorem not_le_to_lt: ∀n,m. n ≰ m → m < n.
301 napply nat_elim2; #n;
302 ##[#abs; napply False_ind;/2/;
304 ##|#m;#Hind;#HnotleSS; napply lt_to_lt_S_S;/3/;
308 ntheorem lt_to_not_le: ∀n,m. n < m → m ≰ n.
309 #n; #m; #Hltnm; nelim Hltnm;/3/; nqed.
311 ntheorem not_lt_to_le: ∀n,m:nat. n ≮ m → m ≤ n.
312 #n; #m; #Hnlt; napply lt_S_to_le;
313 (* something strange here: /2/ fails *)
314 napply not_le_to_lt; napply Hnlt; nqed.
316 ntheorem le_to_not_lt: ∀n,m:nat. n ≤ m → m ≮ n.
319 (* lt and le trans *)
321 ntheorem lt_to_le_to_lt: ∀n,m,p:nat. n < m → m ≤ p → n < p.
322 #n; #m; #p; #H; #H1; nelim H1; /2/; nqed.
324 ntheorem le_to_lt_to_lt: ∀n,m,p:nat. n ≤ m → m < p → n < p.
325 #n; #m; #p; #H; nelim H; /3/; nqed.
327 ntheorem lt_S_to_lt: ∀n,m. S n < m → n < m.
330 ntheorem ltn_to_ltO: ∀n,m:nat. n < m → O < m.
334 theorem lt_SO_n_to_lt_O_pred_n: \forall n:nat.
335 (S O) \lt n \to O \lt (pred n).
337 apply (ltn_to_ltO (pred (S O)) (pred n) ?).
338 apply (lt_pred (S O) n);
344 ntheorem lt_O_n_elim: ∀n:nat. O < n →
345 ∀P:nat → Prop.(∀m:nat.P (S m)) → P n.
346 #n; nelim n; //; #abs; napply False_ind; /2/; nqed.
349 theorem lt_pred: \forall n,m.
350 O < n \to n < m \to pred n < pred m.
352 [intros.apply False_ind.apply (not_le_Sn_O ? H)
353 |intros.apply False_ind.apply (not_le_Sn_O ? H1)
354 |intros.simplify.unfold.apply le_S_S_to_le.assumption
358 theorem S_pred: \forall n:nat.lt O n \to eq nat n (S (pred n)).
359 intro.elim n.apply False_ind.exact (not_le_Sn_O O H).
360 apply eq_f.apply pred_Sn.
363 theorem le_pred_to_le:
364 ∀n,m. O < m → pred n ≤ pred m → n ≤ m.
369 rewrite > (S_pred m);
380 ntheorem le_to_or_lt_eq: ∀n,m:nat. n ≤ m → n < m ∨ n = m.
381 #n; #m; #lenm; nelim lenm; /3/; nqed.
384 ntheorem lt_to_not_eq : ∀n,m:nat. n < m → n ≠ m.
388 ntheorem eq_to_not_lt: ∀a,b:nat. a = b → a ≮ b.
393 apply (lt_to_not_eq b b)
399 theorem lt_n_m_to_not_lt_m_Sn: ∀n,m. n < m → m ≮ S n.
405 generalize in match (le_S_S ? ? H);
407 generalize in match (transitive_le ? ? ? H2 H1);
409 apply (not_le_Sn_n ? H3).
412 ntheorem not_eq_to_le_to_lt: ∀n,m. n≠m → n≤m → n<m.
413 #n; #m; #Hneq; #Hle; ncases (le_to_or_lt_eq ?? Hle); //;
414 #Heq; nelim (Hneq Heq); nqed.
417 ntheorem le_n_O_to_eq : ∀n:nat. n ≤ O → O=n.
418 #n; ncases n; //; #a ; #abs; nelim (not_le_Sn_O ? abs); nqed.
420 ntheorem le_n_O_elim: ∀n:nat. n ≤ O → ∀P: nat →Prop. P O → P n.
421 #n; ncases n; //; #a; #abs; nelim (not_le_Sn_O ? abs); nqed.
423 ntheorem le_n_Sm_elim : ∀n,m:nat.n ≤ S m →
424 ∀P:Prop. (S n ≤ S m → P) → (n=S m → P) → P.
425 #n; #m; #Hle; #P; nelim Hle; /3/; nqed.
429 ntheorem le_to_le_to_eq: ∀n,m. n ≤ m → m ≤ n → n = m.
430 napply nat_elim2; /3/; nqed.
432 ntheorem lt_O_S : \forall n:nat. O < S n.
436 (* other abstract properties *)
437 theorem antisymmetric_le : antisymmetric nat le.
438 unfold antisymmetric.intros 2.
439 apply (nat_elim2 (\lambda n,m.(n \leq m \to m \leq n \to n=m))).
440 intros.apply le_n_O_to_eq.assumption.
441 intros.apply False_ind.apply (not_le_Sn_O ? H).
442 intros.apply eq_f.apply H.
443 apply le_S_S_to_le.assumption.
444 apply le_S_S_to_le.assumption.
447 theorem antisym_le: \forall n,m:nat. n \leq m \to m \leq n \to n=m
448 \def antisymmetric_le.
450 theorem le_n_m_to_lt_m_Sn_to_eq_n_m: ∀n,m. n ≤ m → m < S n → n=m.
453 generalize in match (le_S_S_to_le ? ? H1);
460 (* well founded induction principles *)
462 ntheorem nat_elim1 : ∀n:nat.∀P:nat → Prop.
463 (∀m.(∀p. p < m → P p) → P m) → P n.
465 ncut (∀q:nat. q ≤ n → P q);/2/;
467 ##[#q; #HleO; (* applica male *)
468 napply (le_n_O_elim ? HleO);
470 napply False_ind; /2/;
471 ##|#p; #Hind; #q; #HleS;
472 napply H; #a; #lta; napply Hind;
473 napply le_S_S_to_le;/2/;
477 (* some properties of functions *)
479 definition increasing \def \lambda f:nat \to nat.
480 \forall n:nat. f n < f (S n).
482 theorem increasing_to_monotonic: \forall f:nat \to nat.
483 increasing f \to monotonic nat lt f.
484 unfold monotonic.unfold lt.unfold increasing.unfold lt.intros.elim H1.apply H.
485 apply (trans_le ? (f n1)).
486 assumption.apply (trans_le ? (S (f n1))).
491 theorem le_n_fn: \forall f:nat \to nat. (increasing f)
492 \to \forall n:nat. n \le (f n).
495 apply (trans_le ? (S (f n1))).
496 apply le_S_S.apply H1.
497 simplify in H. unfold increasing in H.unfold lt in H.apply H.
500 theorem increasing_to_le: \forall f:nat \to nat. (increasing f)
501 \to \forall m:nat. \exists i. m \le (f i).
503 apply (ex_intro ? ? O).apply le_O_n.
505 apply (ex_intro ? ? (S a)).
506 apply (trans_le ? (S (f a))).
507 apply le_S_S.assumption.
508 simplify in H.unfold increasing in H.unfold lt in H.
512 theorem increasing_to_le2: \forall f:nat \to nat. (increasing f)
513 \to \forall m:nat. (f O) \le m \to
514 \exists i. (f i) \le m \land m <(f (S i)).
516 apply (ex_intro ? ? O).
517 split.apply le_n.apply H.
519 cut ((S n1) < (f (S a)) \lor (S n1) = (f (S a))).
521 apply (ex_intro ? ? a).
522 split.apply le_S. assumption.assumption.
523 apply (ex_intro ? ? (S a)).
524 split.rewrite < H7.apply le_n.
527 apply le_to_or_lt_eq.apply H6.
531 (******************* monotonicity ******************************)
532 ntheorem monotonic_le_plus_r:
533 ∀n:nat.monotonic nat le (λm.n + m).
534 #n; #a; #b; nelim n; nnormalize; //;
535 #m; #H; #leab;napply le_S_S; /2/; nqed.
537 ntheorem le_plus_r: ∀p,n,m:nat. n ≤ m → p + n ≤ p + m
538 ≝ monotonic_le_plus_r.
540 ntheorem monotonic_le_plus_l:
541 ∀m:nat.monotonic nat le (λn.n + m).
544 ntheorem le_plus_l: \forall p,n,m:nat. n \le m \to n + p \le m + p
545 \def monotonic_le_plus_l.
547 ntheorem le_plus: ∀n1,n2,m1,m2:nat. n1 ≤ n2 \to m1 ≤ m2
549 #n1; #n2; #m1; #m2; #len; #lem; napply transitive_le;
552 ntheorem le_plus_n :∀n,m:nat. m ≤ n + m.
555 ntheorem le_plus_n_r :∀n,m:nat. m ≤ m + n.
558 ntheorem eq_plus_to_le: ∀n,m,p:nat.n=m+p → m ≤ n.
561 ntheorem le_plus_to_le: ∀a,n,m. a + n ≤ a + m → n ≤ m.
562 #a; nelim a; /3/; nqed.
565 theorem monotonic_le_times_r:
566 \forall n:nat.monotonic nat le (\lambda m. n * m).
567 simplify.intros.elim n.
568 simplify.apply le_O_n.
569 simplify.apply le_plus.
574 theorem le_times_r: \forall p,n,m:nat. n \le m \to p*n \le p*m
575 \def monotonic_le_times_r.
577 theorem monotonic_le_times_l:
578 \forall m:nat.monotonic nat le (\lambda n.n*m).
580 rewrite < sym_times.rewrite < (sym_times m).
581 apply le_times_r.assumption.
584 theorem le_times_l: \forall p,n,m:nat. n \le m \to n*p \le m*p
585 \def monotonic_le_times_l.
587 theorem le_times: \forall n1,n2,m1,m2:nat. n1 \le n2 \to m1 \le m2
590 apply (trans_le ? (n2*m1)).
591 apply le_times_l.assumption.
592 apply le_times_r.assumption.
595 theorem le_times_n: \forall n,m:nat.(S O) \le n \to m \le n*m.
596 intros.elim H.simplify.
597 elim (plus_n_O ?).apply le_n.
598 simplify.rewrite < sym_plus.apply le_plus_n.
601 theorem le_times_to_le:
602 \forall a,n,m. S O \le a \to a * n \le a * m \to n \le m.
604 apply nat_elim2;intros
607 rewrite < times_n_O in H1.
608 generalize in match H1.
609 apply (lt_O_n_elim ? H).
612 apply (le_to_not_lt ? ? H2).
617 |rewrite < times_n_Sm in H2.
618 rewrite < times_n_Sm in H2.
619 apply (le_plus_to_le a).
625 theorem le_S_times_SSO: \forall n,m.O < m \to
626 n \le m \to S n \le (S(S O))*m.
630 simplify.rewrite > plus_n_Sm.
638 theorem O_lt_const_to_le_times_const: \forall a,c:nat.
639 O \lt c \to a \le a*c.
641 rewrite > (times_n_SO a) in \vdash (? % ?).