]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/nlibrary/arithmetics/nat.ma
d8ac55049f19e5c3d16008f4f4387f1b28442131
[helm.git] / helm / software / matita / nlibrary / arithmetics / nat.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||       A.Asperti, C.Sacerdoti Coen,                          *)
8 (*      ||A||       E.Tassi, S.Zacchiroli                                 *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU Lesser General Public License Version 2.1         *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 (* include "higher_order_defs/functions.ma". *)
16 include "hints_declaration.ma".
17 include "basics/functions.ma".
18 include "basics/eq.ma". 
19
20 ninductive nat : Type[0] ≝
21   | O : nat
22   | S : nat → nat.
23   
24 interpretation "Natural numbers" 'N = nat.
25
26 alias num (instance 0) = "nnatural number".
27
28 (*
29 nrecord pos : Type ≝
30  {n:>nat; is_pos: n ≠ 0}.
31
32 ncoercion nat_to_pos: ∀n:nat. n ≠0 →pos ≝ mk_pos on 
33 *)
34
35 (* default "natural numbers" cic:/matita/ng/arithmetics/nat/nat.ind.
36 *)
37
38 ndefinition pred ≝
39  λn. match n with [ O ⇒  O | (S p) ⇒ p].
40
41 ntheorem pred_Sn : ∀n. n = pred (S n).
42 //; nqed.
43
44 ntheorem injective_S : injective nat nat S.
45 //; nqed.
46
47 (*
48 ntheorem inj_S : \forall n,m:nat.(S n)=(S m) \to n=m.
49 //. nqed. *)
50
51 ntheorem not_eq_S: ∀n,m:nat. n ≠ m → S n ≠ S m.
52 /2/; nqed.
53
54 ndefinition not_zero: nat → Prop ≝
55  λn: nat. match n with
56   [ O ⇒ False | (S p) ⇒ True ].
57
58 ntheorem not_eq_O_S : ∀n:nat. O ≠ S n.
59 #n; #eqOS; nchange with (not_zero O); nrewrite > eqOS; //.
60 nqed.
61
62 ntheorem not_eq_n_Sn : ∀n:nat. n ≠ S n.
63 #n; nelim n; /2/; nqed.
64
65 ntheorem nat_case:
66  ∀n:nat.∀P:nat → Prop. 
67   (n=O → P O) → (∀m:nat. (n=(S m) → P (S m))) → P n.
68 #n; #P; nelim n; /2/; nqed.
69
70 ntheorem nat_elim2 :
71  ∀R:nat → nat → Prop.
72   (∀n:nat. R O n) 
73   → (∀n:nat. R (S n) O)
74   → (∀n,m:nat. R n m → R (S n) (S m))
75   → ∀n,m:nat. R n m.
76 #R; #ROn; #RSO; #RSS; #n; nelim n;//;
77 #n0; #Rn0m; #m; ncases m;/2/; nqed.
78
79 ntheorem decidable_eq_nat : ∀n,m:nat.decidable (n=m).
80 napply nat_elim2; #n;
81  ##[ ncases n; /3/;
82  ##| /3/;
83  ##| #m; #Hind; ncases Hind; /3/;
84  ##]
85 nqed. 
86
87 (*************************** plus ******************************)
88
89 nlet rec plus n m ≝ 
90  match n with 
91  [ O ⇒ m
92  | S p ⇒ S (plus p m) ].
93
94 interpretation "natural plus" 'plus x y = (plus x y).
95
96 ntheorem plus_O_n: ∀n:nat. n = 0+n.
97 //; nqed.
98
99 (*
100 ntheorem plus_Sn_m: ∀n,m:nat. S (n + m) = S n + m.
101 //; nqed.
102 *)
103
104 ntheorem plus_n_O: ∀n:nat. n = n+0.
105 #n; nelim n; nnormalize; //; nqed.
106
107 ntheorem plus_n_Sm : ∀n,m:nat. S (n+m) = n + S m.
108 #n; nelim n; nnormalize; //; nqed.
109
110 (*
111 ntheorem plus_Sn_m1: ∀n,m:nat. S m + n = n + S m.
112 #n; nelim n; nnormalize; //; nqed.
113 *)
114
115 (*
116 ntheorem plus_n_SO : ∀n:nat. S n = n+S O.
117 //; nqed. *)
118
119 ntheorem symmetric_plus: symmetric ? plus.
120 #n; nelim n; nnormalize; //; nqed. 
121
122 ntheorem associative_plus : associative nat plus.
123 #n; nelim n; nnormalize; //; nqed.
124
125 ntheorem assoc_plus1: ∀a,b,c. c + (b + a) = b + c + a.
126 //; nqed.
127
128 ntheorem injective_plus_r: ∀n:nat.injective nat nat (λm.n+m).
129 #n; nelim n; nnormalize; /3/; nqed.
130
131 (* ntheorem inj_plus_r: \forall p,n,m:nat. p+n = p+m \to n=m
132 \def injective_plus_r. 
133
134 ntheorem injective_plus_l: ∀m:nat.injective nat nat (λn.n+m).
135 /2/; nqed. *)
136
137 (* ntheorem inj_plus_l: \forall p,n,m:nat. n+p = m+p \to n=m
138 \def injective_plus_l. *)
139
140 (*************************** times *****************************)
141
142 nlet rec times n m ≝ 
143  match n with 
144  [ O ⇒ O
145  | S p ⇒ m+(times p m) ].
146
147 interpretation "natural times" 'times x y = (times x y).
148
149 ntheorem times_Sn_m: ∀n,m:nat. m+n*m = S n*m.
150 //; nqed.
151
152 ntheorem times_O_n: ∀n:nat. O = O*n.
153 //; nqed.
154
155 ntheorem times_n_O: ∀n:nat. O = n*O.
156 #n; nelim n; //; nqed.
157
158 ntheorem times_n_Sm : ∀n,m:nat. n+(n*m) = n*(S m).
159 #n; nelim n; nnormalize; //; nqed.
160
161 ntheorem symmetric_times : symmetric nat times. 
162 #n; nelim n; nnormalize; //; nqed.
163
164 (* variant sym_times : \forall n,m:nat. n*m = m*n \def
165 symmetric_times. *)
166
167 ntheorem distributive_times_plus : distributive nat times plus.
168 #n; nelim n; nnormalize; //; nqed.
169
170 ntheorem distributive_times_plus_r:  
171 \forall a,b,c:nat. (b+c)*a = b*a + c*a.
172 //; nqed.
173
174 ntheorem associative_times: associative nat times.
175 #n; nelim n; nnormalize; //; nqed.
176
177 nlemma times_times: ∀x,y,z. x*(y*z) = y*(x*z).
178 //; nqed. 
179
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;
184 //; nqed.
185   
186 ntheorem times_n_SO : ∀n:nat. n = n * S O.
187 #n; //; nqed.
188
189 ntheorem times_SSO_n : ∀n:nat. n + n = (S(S O)) * n.
190 nnormalize; //; nqed.
191
192 nlemma times_SSO: \forall n.(S(S O))*(S n) = S(S((S(S O))*n)).
193 //; nqed.
194
195 ntheorem or_eq_eq_S: \forall n.\exists m. 
196 n = (S(S O))*m \lor n = S ((S(S O))*m).
197 #n; nelim n;
198   ##[@; /2/;
199   ##|#a; #H; nelim H; #b;#or;nelim or;#aeq;
200     ##[@ b; @ 2; //;
201     ##|@ (S b); @ 1; /2/;
202     ##]
203 nqed.
204 *)
205
206 (******************** ordering relations ************************)
207
208 ninductive le (n:nat) : nat → Prop ≝
209   | le_n : le n n
210   | le_S : ∀ m:nat. le n m → le n (S m).
211
212 interpretation "natural 'less or equal to'" 'leq x y = (le x y).
213
214 interpretation "natural 'neither less nor equal to'" 'nleq x y = (Not (le x y)).
215
216 ndefinition lt: nat → nat → Prop ≝
217 λn,m:nat. S n ≤ m.
218
219 interpretation "natural 'less than'" 'lt x y = (lt x y).
220
221 interpretation "natural 'not less than'" 'nless x y = (Not (lt x y)).
222
223 ndefinition ge: nat \to nat \to Prop \def
224 \lambda n,m:nat.m \leq n.
225
226 interpretation "natural 'greater or equal to'" 'geq x y = (ge x y).
227
228 ndefinition gt: nat \to nat \to Prop \def
229 \lambda n,m:nat.m<n.
230
231 interpretation "natural 'greater than'" 'gt x y = (gt x y).
232
233 interpretation "natural 'not greater than'" 'ngtr x y = (Not (gt x y)).
234
235 ntheorem transitive_le : transitive nat le.
236 #a; #b; #c; #leab; #lebc;nelim lebc;/2/;
237 nqed.
238
239 (*
240 ntheorem trans_le: \forall n,m,p:nat. n \leq m \to m \leq p \to n \leq p
241 \def transitive_le. *)
242
243 ntheorem transitive_lt: transitive nat lt.
244 #a; #b; #c; #ltab; #ltbc;nelim ltbc;/2/;nqed.
245
246 (*
247 theorem trans_lt: \forall n,m,p:nat. lt n m \to lt m p \to lt n p
248 \def transitive_lt. *)
249
250 ntheorem le_S_S: ∀n,m:nat. n ≤ m → S n ≤ S m.
251 #n; #m; #lenm; nelim lenm; /2/; nqed.
252
253 ntheorem le_O_n : ∀n:nat. O ≤ n.
254 #n; nelim n; /2/; nqed.
255
256 ntheorem le_n_Sn : ∀n:nat. n ≤ S n.
257 /2/; nqed.
258
259 ntheorem le_pred_n : ∀n:nat. pred n ≤ n.
260 #n; nelim n; //; nqed.
261
262 ntheorem monotonic_pred: monotonic ? le pred.
263 #n; #m; #lenm; nelim lenm; //; /2/; nqed.
264
265 ntheorem le_S_S_to_le: ∀n,m:nat. S n ≤ S m → n ≤ m.
266 /2/; nqed.
267
268 ntheorem lt_S_S_to_lt: ∀n,m. S n < S m \to n < m.
269 /2/; nqed. 
270
271 ntheorem lt_to_lt_S_S: ∀n,m. n < m → S n < S m.
272 /2/; nqed.
273
274 ntheorem lt_to_not_zero : ∀n,m:nat. n < m → not_zero m.
275 #n; #m; #Hlt; nelim Hlt;//; nqed.
276
277 (* lt vs. le *)
278 ntheorem not_le_Sn_O: ∀ n:nat. S n ≰ O.
279 #n; #Hlen0; napply (lt_to_not_zero ?? Hlen0); nqed.
280
281 ntheorem not_le_to_not_le_S_S: ∀ n,m:nat. n ≰ m → S n ≰ S m.
282 /3/; nqed.
283
284 ntheorem not_le_S_S_to_not_le: ∀ n,m:nat. S n ≰ S m → n ≰ m.
285 /3/; nqed.
286
287 ntheorem decidable_le: ∀n,m. decidable (n≤m).
288 napply nat_elim2; #n; /3/;
289 #m; #dec; ncases dec;/4/; nqed.
290
291 ntheorem decidable_lt: ∀n,m. decidable (n < m).
292 #n; #m; napply decidable_le ; nqed.
293
294 ntheorem not_le_Sn_n: ∀n:nat. S n ≰ n.
295 #n; nelim n; /3/; nqed.
296
297 ntheorem lt_S_to_le: ∀n,m:nat. n < S m → n ≤ m.
298 /2/; nqed.
299
300 ntheorem not_le_to_lt: ∀n,m. n ≰ m → m < n.
301 napply nat_elim2; #n;
302  ##[#abs; napply False_ind;/2/;
303  ##|/2/;
304  ##|#m;#Hind;#HnotleSS; napply lt_to_lt_S_S;/4/;
305  ##]
306 nqed.
307
308 ntheorem lt_to_not_le: ∀n,m. n < m → m ≰ n.
309 #n; #m; #Hltnm; nelim Hltnm;/3/; nqed.
310
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    we need an extra depths for unfolding not *)
315 napply not_le_to_lt; napply Hnlt; nqed. 
316
317 ntheorem le_to_not_lt: ∀n,m:nat. n ≤ m → m ≮ n.
318 /2/; nqed.
319
320 (* lt and le trans *)
321
322 ntheorem lt_to_le_to_lt: ∀n,m,p:nat. n < m → m ≤ p → n < p.
323 #n; #m; #p; #H; #H1; nelim H1; /2/; nqed.
324
325 ntheorem le_to_lt_to_lt: ∀n,m,p:nat. n ≤ m → m < p → n < p.
326 #n; #m; #p; #H; nelim H; /3/; nqed.
327
328 ntheorem lt_S_to_lt: ∀n,m. S n < m → n < m.
329 /2/; nqed.
330
331 ntheorem ltn_to_ltO: ∀n,m:nat. n < m → O < m.
332 /2/; nqed.
333
334 (*
335 theorem lt_SO_n_to_lt_O_pred_n: \forall n:nat.
336 (S O) \lt n \to O \lt (pred n).
337 intros.
338 apply (ltn_to_ltO (pred (S O)) (pred n) ?).
339  apply (lt_pred (S O) n);
340  [ apply (lt_O_S O) 
341  | assumption
342  ]
343 qed. *)
344
345 ntheorem lt_O_n_elim: ∀n:nat. O < n → 
346   ∀P:nat → Prop.(∀m:nat.P (S m)) → P n.
347 #n; nelim n; //; #abs; napply False_ind; /2/; nqed.
348
349 (*
350 theorem lt_pred: \forall n,m. 
351   O < n \to n < m \to pred n < pred m. 
352 apply nat_elim2
353   [intros.apply False_ind.apply (not_le_Sn_O ? H)
354   |intros.apply False_ind.apply (not_le_Sn_O ? H1)
355   |intros.simplify.unfold.apply le_S_S_to_le.assumption
356   ]
357 qed.
358
359 theorem S_pred: \forall n:nat.lt O n \to eq nat n (S (pred n)).
360 intro.elim n.apply False_ind.exact (not_le_Sn_O O H).
361 apply eq_f.apply pred_Sn.
362 qed.
363
364 theorem le_pred_to_le:
365  ∀n,m. O < m → pred n ≤ pred m → n ≤ m.
366 intros 2;
367 elim n;
368 [ apply le_O_n
369 | simplify in H2;
370   rewrite > (S_pred m);
371   [ apply le_S_S;
372     assumption
373   | assumption
374   ]
375 ].
376 qed.
377
378 *)
379
380 (* le to lt or eq *)
381 ntheorem le_to_or_lt_eq: ∀n,m:nat. n ≤ m → n < m ∨ n = m.
382 #n; #m; #lenm; nelim lenm; /3/; nqed.
383
384 (* not eq *)
385 ntheorem lt_to_not_eq : ∀n,m:nat. n < m → n ≠ m.
386 /2/; nqed.
387
388 (*not lt 
389 ntheorem eq_to_not_lt: ∀a,b:nat. a = b → a ≮ b.
390 intros.
391 unfold Not.
392 intros.
393 rewrite > H in H1.
394 apply (lt_to_not_eq b b)
395 [ assumption
396 | reflexivity
397 ]
398 qed. 
399
400 theorem lt_n_m_to_not_lt_m_Sn: ∀n,m. n < m → m ≮ S n.
401 intros;
402 unfold Not;
403 intro;
404 unfold lt in H;
405 unfold lt in H1;
406 generalize in match (le_S_S ? ? H);
407 intro;
408 generalize in match (transitive_le ? ? ? H2 H1);
409 intro;
410 apply (not_le_Sn_n ? H3).
411 qed. *)
412
413 ntheorem not_eq_to_le_to_lt: ∀n,m. n≠m → n≤m → n<m.
414 #n; #m; #Hneq; #Hle; ncases (le_to_or_lt_eq ?? Hle); //;
415 #Heq; nelim (Hneq Heq); nqed.
416
417 (* le elimination *)
418 ntheorem le_n_O_to_eq : ∀n:nat. n ≤ O → O=n.
419 #n; ncases n; //; #a ; #abs; nelim (not_le_Sn_O ? abs); nqed.
420
421 ntheorem le_n_O_elim: ∀n:nat. n ≤ O → ∀P: nat →Prop. P O → P n.
422 #n; ncases n; //; #a; #abs; nelim (not_le_Sn_O ? abs); nqed. 
423
424 ntheorem le_n_Sm_elim : ∀n,m:nat.n ≤ S m → 
425 ∀P:Prop. (S n ≤ S m → P) → (n=S m → P) → P.
426 #n; #m; #Hle; #P; nelim Hle; /3/; nqed.
427
428 (* le and eq *)
429
430 ntheorem le_to_le_to_eq: ∀n,m. n ≤ m → m ≤ n → n = m.
431 napply nat_elim2; /4/; nqed.
432
433 ntheorem lt_O_S : ∀n:nat. O < S n.
434 /2/; nqed.
435
436 (*
437 (* other abstract properties *)
438 theorem antisymmetric_le : antisymmetric nat le.
439 unfold antisymmetric.intros 2.
440 apply (nat_elim2 (\lambda n,m.(n \leq m \to m \leq n \to n=m))).
441 intros.apply le_n_O_to_eq.assumption.
442 intros.apply False_ind.apply (not_le_Sn_O ? H).
443 intros.apply eq_f.apply H.
444 apply le_S_S_to_le.assumption.
445 apply le_S_S_to_le.assumption.
446 qed.
447
448 theorem antisym_le: \forall n,m:nat. n \leq m \to m \leq n \to n=m
449 \def antisymmetric_le.
450
451 theorem le_n_m_to_lt_m_Sn_to_eq_n_m: ∀n,m. n ≤ m → m < S n → n=m.
452 intros;
453 unfold lt in H1;
454 generalize in match (le_S_S_to_le ? ? H1);
455 intro;
456 apply antisym_le;
457 assumption.
458 qed.
459 *)
460
461 (* well founded induction principles *)
462
463 ntheorem nat_elim1 : ∀n:nat.∀P:nat → Prop. 
464 (∀m.(∀p. p < m → P p) → P m) → P n.
465 #n; #P; #H; 
466 ncut (∀q:nat. q ≤ n → P q);/2/;
467 nelim n; 
468  ##[#q; #HleO; (* applica male *) 
469     napply (le_n_O_elim ? HleO);
470     napply H; #p; #ltpO;
471     napply False_ind; /2/; 
472  ##|#p; #Hind; #q; #HleS; 
473     napply H; #a; #lta; napply Hind;
474     napply le_S_S_to_le;/2/;
475  ##]
476 nqed.
477
478 (* some properties of functions *)
479 (*
480 definition increasing \def \lambda f:nat \to nat. 
481 \forall n:nat. f n < f (S n).
482
483 theorem increasing_to_monotonic: \forall f:nat \to nat.
484 increasing f \to monotonic nat lt f.
485 unfold monotonic.unfold lt.unfold increasing.unfold lt.intros.elim H1.apply H.
486 apply (trans_le ? (f n1)).
487 assumption.apply (trans_le ? (S (f n1))).
488 apply le_n_Sn.
489 apply H.
490 qed.
491
492 theorem le_n_fn: \forall f:nat \to nat. (increasing f) 
493 \to \forall n:nat. n \le (f n).
494 intros.elim n.
495 apply le_O_n.
496 apply (trans_le ? (S (f n1))).
497 apply le_S_S.apply H1.
498 simplify in H. unfold increasing in H.unfold lt in H.apply H.
499 qed.
500
501 theorem increasing_to_le: \forall f:nat \to nat. (increasing f) 
502 \to \forall m:nat. \exists i. m \le (f i).
503 intros.elim m.
504 apply (ex_intro ? ? O).apply le_O_n.
505 elim H1.
506 apply (ex_intro ? ? (S a)).
507 apply (trans_le ? (S (f a))).
508 apply le_S_S.assumption.
509 simplify in H.unfold increasing in H.unfold lt in H.
510 apply H.
511 qed.
512
513 theorem increasing_to_le2: \forall f:nat \to nat. (increasing f) 
514 \to \forall m:nat. (f O) \le m \to 
515 \exists i. (f i) \le m \land m <(f (S i)).
516 intros.elim H1.
517 apply (ex_intro ? ? O).
518 split.apply le_n.apply H.
519 elim H3.elim H4.
520 cut ((S n1) < (f (S a)) \lor (S n1) = (f (S a))).
521 elim Hcut.
522 apply (ex_intro ? ? a).
523 split.apply le_S. assumption.assumption.
524 apply (ex_intro ? ? (S a)).
525 split.rewrite < H7.apply le_n.
526 rewrite > H7.
527 apply H.
528 apply le_to_or_lt_eq.apply H6.
529 qed.
530 *)
531
532 (*********************** monotonicity ***************************)
533 ntheorem monotonic_le_plus_r: 
534 ∀n:nat.monotonic nat le (λm.n + m).
535 #n; #a; #b; nelim n; nnormalize; //;
536 #m; #H; #leab;napply le_S_S; /2/; nqed.
537
538 (*
539 ntheorem le_plus_r: ∀p,n,m:nat. n ≤ m → p + n ≤ p + m
540 ≝ monotonic_le_plus_r. *)
541
542 ntheorem monotonic_le_plus_l: 
543 ∀m:nat.monotonic nat le (λn.n + m).
544 /2/; nqed.
545
546 (*
547 ntheorem le_plus_l: \forall p,n,m:nat. n \le m \to n + p \le m + p
548 \def monotonic_le_plus_l. *)
549
550 ntheorem le_plus: ∀n1,n2,m1,m2:nat. n1 ≤ n2  \to m1 ≤ m2 
551 → n1 + m1 ≤ n2 + m2.
552 #n1; #n2; #m1; #m2; #len; #lem; napply transitive_le;
553 /2/; nqed.
554
555 ntheorem le_plus_n :∀n,m:nat. m ≤ n + m.
556 /2/; nqed. 
557
558 ntheorem le_plus_n_r :∀n,m:nat. m ≤ m + n.
559 /2/; nqed.
560
561 ntheorem eq_plus_to_le: ∀n,m,p:nat.n=m+p → m ≤ n.
562 //; nqed.
563
564 ntheorem le_plus_to_le: ∀a,n,m. a + n ≤ a + m → n ≤ m.
565 #a; nelim a; /3/; nqed. 
566
567 ntheorem le_plus_to_le_r: ∀a,n,m. n + a ≤ m +a → n ≤ m.
568 /2/; nqed. 
569
570 (* plus & lt *)
571
572 ntheorem monotonic_lt_plus_r: 
573 ∀n:nat.monotonic nat lt (λm.n+m).
574 /2/; nqed.
575
576 (*
577 variant lt_plus_r: \forall n,p,q:nat. p < q \to n + p < n + q \def
578 monotonic_lt_plus_r. *)
579
580 ntheorem monotonic_lt_plus_l: 
581 ∀n:nat.monotonic nat lt (λm.m+n).
582 /2/;nqed.
583
584 (*
585 variant lt_plus_l: \forall n,p,q:nat. p < q \to p + n < q + n \def
586 monotonic_lt_plus_l. *)
587
588 ntheorem lt_plus: ∀n,m,p,q:nat. n < m → p < q → n + p < m + q.
589 #n; #m; #p; #q; #ltnm; #ltpq;
590 napply (transitive_lt ? (n+q));/2/; nqed.
591
592 ntheorem lt_plus_to_lt_l :∀n,p,q:nat. p+n < q+n → p<q.
593 /2/; nqed.
594
595 ntheorem lt_plus_to_lt_r :∀n,p,q:nat. n+p < n+q → p<q.
596 /2/; nqed.
597
598 ntheorem le_to_lt_to_plus_lt: ∀a,b,c,d:nat.
599 a ≤ c → b < d → a + b < c+d.
600 (* bello /2/ un po' lento *)
601 #a; #b; #c; #d; #leac; #lebd; 
602 nnormalize; napplyS le_plus; //; nqed.
603
604 (* times *)
605 ntheorem monotonic_le_times_r: 
606 ∀n:nat.monotonic nat le (λm. n * m).
607 #n; #x; #y; #lexy; nelim n; nnormalize;//;(* lento /2/;*)
608 #a; #lea; napply le_plus; //;
609 nqed.
610
611 (*
612 ntheorem le_times_r: \forall p,n,m:nat. n \le m \to p*n \le p*m
613 \def monotonic_le_times_r. *)
614
615 ntheorem monotonic_le_times_l: 
616 ∀m:nat.monotonic nat le (λn.n*m).
617 /2/; nqed.
618
619 (*
620 theorem le_times_l: \forall p,n,m:nat. n \le m \to n*p \le m*p
621 \def monotonic_le_times_l. *)
622
623 ntheorem le_times: ∀n1,n2,m1,m2:nat. 
624 n1 ≤ n2  → m1 ≤ m2 → n1*m1 ≤ n2*m2.
625 #n1; #n2; #m1; #m2; #len; #lem; 
626 napply transitive_le; (* /2/ slow *)
627  ##[ ##| napply monotonic_le_times_l;//; 
628      ##| napply monotonic_le_times_r;//;
629  ##]
630 nqed.
631
632 ntheorem lt_times_n: ∀n,m:nat. O < n → m ≤ n*m.
633 (* bello *)
634 /2/; nqed.
635
636 ntheorem le_times_to_le: 
637 ∀a,n,m. O < a → a * n ≤ a * m → n ≤ m.
638 #a; napply nat_elim2; nnormalize;
639   ##[//;
640   ##|#n; #H1; #H2; napply False_ind;
641      ngeneralize in match H2;
642      napply lt_to_not_le;
643      napply (transitive_le ? (S n));/2/;
644   ##|#n; #m; #H; #lta; #le;
645      napply le_S_S; napply H; /2/;
646   ##]
647 nqed.
648
649 ntheorem le_S_times_2: ∀n,m.O < m → n ≤ m → n < 2*m.
650 #n; #m; #posm; #lenm; (* interessante *)
651 nnormalize; napplyS (le_plus n); //; nqed.
652
653 (* times & lt *)
654 (*
655 ntheorem lt_O_times_S_S: ∀n,m:nat.O < (S n)*(S m).
656 intros.simplify.unfold lt.apply le_S_S.apply le_O_n.
657 qed. *)
658
659 (*
660 ntheorem lt_times_eq_O: \forall a,b:nat.
661 O < a → a * b = O → b = O.
662 intros.
663 apply (nat_case1 b)
664 [ intros.
665   reflexivity
666 | intros.
667   rewrite > H2 in H1.
668   rewrite > (S_pred a) in H1
669   [ apply False_ind.
670     apply (eq_to_not_lt O ((S (pred a))*(S m)))
671     [ apply sym_eq.
672       assumption
673     | apply lt_O_times_S_S
674     ]
675   | assumption
676   ]
677 ]
678 qed. 
679
680 theorem O_lt_times_to_O_lt: \forall a,c:nat.
681 O \lt (a * c) \to O \lt a.
682 intros.
683 apply (nat_case1 a)
684 [ intros.
685   rewrite > H1 in H.
686   simplify in H.
687   assumption
688 | intros.
689   apply lt_O_S
690 ]
691 qed.
692
693 lemma lt_times_to_lt_O: \forall i,n,m:nat. i < n*m \to O < m.
694 intros.
695 elim (le_to_or_lt_eq O ? (le_O_n m))
696   [assumption
697   |apply False_ind.
698    rewrite < H1 in H.
699    rewrite < times_n_O in H.
700    apply (not_le_Sn_O ? H)
701   ]
702 qed. *)
703
704 (*
705 ntheorem monotonic_lt_times_r: 
706 ∀n:nat.monotonic nat lt (λm.(S n)*m).
707 /2/; 
708 simplify.
709 intros.elim n.
710 simplify.rewrite < plus_n_O.rewrite < plus_n_O.assumption.
711 apply lt_plus.assumption.assumption.
712 qed. *)
713
714 ntheorem monotonic_lt_times_l: 
715   ∀c:nat. O < c → monotonic nat lt (λt.(t*c)).
716 #c; #posc; #n; #m; #ltnm;
717 nelim ltnm; nnormalize;
718   ##[napplyS monotonic_lt_plus_l;//;
719   ##|#a; #_; #lt1; napply (transitive_le ??? lt1);//;
720   ##]
721 nqed.
722
723 ntheorem monotonic_lt_times_r: 
724   ∀c:nat. O < c → monotonic nat lt (λt.(c*t)).
725 (* /2/ lentissimo *)
726 #c; #posc; #n; #m; #ltnm;
727 (* why?? napplyS (monotonic_lt_times_l c posc n m ltnm); *)
728 nrewrite > (symmetric_times c n);
729 nrewrite > (symmetric_times c m);
730 napply monotonic_lt_times_l;//;
731 nqed.
732
733 ntheorem lt_to_le_to_lt_times: 
734 ∀n,m,p,q:nat. n < m → p ≤ q → O < q → n*p < m*q.
735 #n; #m; #p; #q; #ltnm; #lepq; #posq;
736 napply (le_to_lt_to_lt ? (n*q));
737   ##[napply monotonic_le_times_r;//;
738   ##|napply monotonic_lt_times_l;//;
739   ##]
740 nqed.
741
742 ntheorem lt_times:∀n,m,p,q:nat. n<m → p<q → n*p < m*q.
743 #n; #m; #p; #q; #ltnm; #ltpq;
744 napply lt_to_le_to_lt_times;/2/;
745 nqed.
746
747 ntheorem lt_times_n_to_lt_l: 
748 ∀n,p,q:nat. O < n → p*n < q*n → p < q.
749 #n; #p; #q; #posn; #Hlt;
750 nelim (decidable_lt p q);//;
751 #nltpq;napply False_ind; 
752 napply (lt_to_not_le ? ? Hlt);
753 napply monotonic_le_times_l;/3/;
754 nqed.
755
756 ntheorem lt_times_n_to_lt_r: 
757 ∀n,p,q:nat. O < n → n*p < n*q → p < q.
758 #n; #p; #q; #posn; #Hlt;
759 napply (lt_times_n_to_lt_l ??? posn);//;
760 nqed.
761
762 (*
763 theorem nat_compare_times_l : \forall n,p,q:nat. 
764 nat_compare p q = nat_compare ((S n) * p) ((S n) * q).
765 intros.apply nat_compare_elim.intro.
766 apply nat_compare_elim.
767 intro.reflexivity.
768 intro.absurd (p=q).
769 apply (inj_times_r n).assumption.
770 apply lt_to_not_eq. assumption.
771 intro.absurd (q<p).
772 apply (lt_times_to_lt_r n).assumption.
773 apply le_to_not_lt.apply lt_to_le.assumption.
774 intro.rewrite < H.rewrite > nat_compare_n_n.reflexivity.
775 intro.apply nat_compare_elim.intro.
776 absurd (p<q).
777 apply (lt_times_to_lt_r n).assumption.
778 apply le_to_not_lt.apply lt_to_le.assumption.
779 intro.absurd (q=p).
780 symmetry.
781 apply (inj_times_r n).assumption.
782 apply lt_to_not_eq.assumption.
783 intro.reflexivity.
784 qed. *)
785
786 (* times and plus 
787 theorem lt_times_plus_times: \forall a,b,n,m:nat. 
788 a < n \to b < m \to a*m + b < n*m.
789 intros 3.
790 apply (nat_case n)
791   [intros.apply False_ind.apply (not_le_Sn_O ? H)
792   |intros.simplify.
793    rewrite < sym_plus.
794    unfold.
795    change with (S b+a*m1 \leq m1+m*m1).
796    apply le_plus
797     [assumption
798     |apply le_times
799       [apply le_S_S_to_le.assumption
800       |apply le_n
801       ]
802     ]
803   ]
804 qed. *)
805
806 (************************** minus ******************************)
807
808 nlet rec minus n m ≝ 
809  match n with 
810  [ O ⇒ O
811  | S p ⇒ 
812         match m with
813           [ O ⇒ S p
814     | S q ⇒ minus p q ]].
815         
816 interpretation "natural minus" 'minus x y = (minus x y).
817
818 ntheorem minus_S_S: ∀n,m:nat.S n - S m = n -m.
819 //; nqed.
820
821 ntheorem minus_O_n: ∀n:nat.O=O-n.
822 #n; ncases n; //; nqed.
823
824 ntheorem minus_n_O: ∀n:nat.n=n-O.
825 #n; ncases n; //; nqed.
826
827 ntheorem minus_n_n: ∀n:nat.O=n-n.
828 #n; nelim n; //; nqed.
829
830 ntheorem minus_Sn_n: ∀n:nat. S O = (S n)-n.
831 #n; nelim n; //; nqed.
832
833 ntheorem minus_Sn_m: ∀m,n:nat. m ≤ n → S n -m = S (n-m).
834 (* qualcosa da capire qui 
835 #n; #m; #lenm; nelim lenm; napplyS refl_eq. *)
836 napply nat_elim2; 
837   ##[//
838   ##|#n; #abs; napply False_ind;/2/;
839   ##|#n; #m; #Hind; #c; napplyS Hind; /2/;
840   ##]
841 nqed.
842
843 ntheorem not_eq_to_le_to_le_minus: 
844   ∀n,m.n ≠ m → n ≤ m → n ≤ m - 1.
845 #n; #m; ncases m;//; #m; nnormalize;
846 #H; #H1; napply le_S_S_to_le;
847 napplyS (not_eq_to_le_to_lt n (S m) H H1);
848 nqed.
849
850 ntheorem eq_minus_S_pred: ∀n,m. n - (S m) = pred(n -m).
851 napply nat_elim2; //; nqed.
852
853 ntheorem plus_minus:
854 ∀m,n,p:nat. m ≤ n → (n-m)+p = (n+p)-m.
855 napply nat_elim2; 
856   ##[//
857   ##|#n; #p; #abs; napply False_ind;/2/;
858   ##|nnormalize;/3/;
859   ##]
860 nqed.
861
862 ntheorem minus_plus_m_m: ∀n,m:nat.n = (n+m)-m.
863 #n; #m; napplyS (plus_minus m m n); //; nqed.
864
865 ntheorem plus_minus_m_m: ∀n,m:nat.
866 m \leq n \to n = (n-m)+m.
867 #n; #m; #lemn; napplyS symmetric_eq; 
868 napplyS (plus_minus m n m); //; nqed.
869
870 ntheorem le_plus_minus_m_m: ∀n,m:nat. n ≤ (n-m)+m.
871 #n; nelim n;
872   ##[//
873   ##|#a; #Hind; #m; ncases m;//;  
874      nnormalize; #n;napplyS le_S_S;//  
875   ##]
876 nqed.
877
878 ntheorem minus_to_plus :∀n,m,p:nat.
879   m ≤ n → n-m = p → n = m+p.
880 #n; #m; #p; #lemn; #eqp; napplyS plus_minus_m_m; //;
881 nqed.
882
883 ntheorem plus_to_minus :∀n,m,p:nat.n = m+p → n-m = p.
884 (* /4/ done in 43.5 *)
885 #n; #m; #p; #eqp; 
886 napply symmetric_eq;
887 napplyS (minus_plus_m_m p m);
888 nqed.
889
890 ntheorem minus_pred_pred : ∀n,m:nat. O < n → O < m → 
891 pred n - pred m = n - m.
892 #n; #m; #posn; #posm;
893 napply (lt_O_n_elim n posn);
894 napply (lt_O_n_elim m posm);//.
895 nqed.
896
897 (*
898 theorem eq_minus_n_m_O: \forall n,m:nat.
899 n \leq m \to n-m = O.
900 intros 2.
901 apply (nat_elim2 (\lambda n,m.n \leq m \to n-m = O)).
902 intros.simplify.reflexivity.
903 intros.apply False_ind.
904 apply not_le_Sn_O;
905 [2: apply H | skip].
906 intros.
907 simplify.apply H.apply le_S_S_to_le. apply H1.
908 qed.
909
910 theorem le_SO_minus: \forall n,m:nat.S n \leq m \to S O \leq m-n.
911 intros.elim H.elim (minus_Sn_n n).apply le_n.
912 rewrite > minus_Sn_m.
913 apply le_S.assumption.
914 apply lt_to_le.assumption.
915 qed.
916
917 theorem minus_le_S_minus_S: \forall n,m:nat. m-n \leq S (m-(S n)).
918 intros.
919 apply (nat_elim2 (\lambda n,m.m-n \leq S (m-(S n)))).
920 intro.elim n1.simplify.apply le_n_Sn.
921 simplify.rewrite < minus_n_O.apply le_n.
922 intros.simplify.apply le_n_Sn.
923 intros.simplify.apply H.
924 qed.
925
926 theorem lt_minus_S_n_to_le_minus_n : \forall n,m,p:nat. m-(S n) < p \to m-n \leq p. 
927 intros 3.intro.
928 (* autobatch *)
929 (* end auto($Revision: 9739 $) proof: TIME=1.33 SIZE=100 DEPTH=100 *)
930 apply (trans_le (m-n) (S (m-(S n))) p).
931 apply minus_le_S_minus_S.
932 assumption.
933 qed.
934
935 theorem le_minus_m: \forall n,m:nat. n-m \leq n.
936 intros.apply (nat_elim2 (\lambda m,n. n-m \leq n)).
937 intros.rewrite < minus_n_O.apply le_n.
938 intros.simplify.apply le_n.
939 intros.simplify.apply le_S.assumption.
940 qed.
941
942 theorem lt_minus_m: \forall n,m:nat. O < n \to O < m \to n-m \lt n.
943 intros.apply (lt_O_n_elim n H).intro.
944 apply (lt_O_n_elim m H1).intro.
945 simplify.unfold lt.apply le_S_S.apply le_minus_m.
946 qed.
947
948 theorem minus_le_O_to_le: \forall n,m:nat. n-m \leq O \to n \leq m.
949 intros 2.
950 apply (nat_elim2 (\lambda n,m:nat.n-m \leq O \to n \leq m)).
951 intros.apply le_O_n.
952 simplify.intros. assumption.
953 simplify.intros.apply le_S_S.apply H.assumption.
954 qed.
955 *)
956
957 (* monotonicity and galois *)
958
959 ntheorem monotonic_le_minus_l: 
960 ∀p,q,n:nat. q ≤ p → q-n ≤ p-n.
961 napply nat_elim2; #p; #q;
962   ##[#lePO; napply (le_n_O_elim ? lePO);//;
963   ##|//;
964   ##|#Hind; #n; ncases n;
965     ##[//;
966     ##|#a; #leSS; napply Hind; /2/;
967     ##]
968   ##]
969 nqed.
970
971 ntheorem le_minus_to_plus: ∀n,m,p. n-m ≤ p → n≤ p+m.
972 #n; #m; #p; #lep;
973 napply transitive_le;
974   ##[##|napply le_plus_minus_m_m
975   ##|napply monotonic_le_plus_l;//;
976   ##]
977 nqed.
978
979 ntheorem le_plus_to_minus: ∀n,m,p. n ≤ p+m → n-m ≤ p.
980 #n; #m; #p; #lep;
981 (* bello *)
982 napplyS monotonic_le_minus_l;//;
983 nqed.
984
985 ntheorem monotonic_le_minus_r: 
986 ∀p,q,n:nat. q ≤ p → n-p ≤ n-q.
987 #p; #q; #n; #lepq;
988 napply le_plus_to_minus;
989 napply (transitive_le ??? (le_plus_minus_m_m ? q));/2/;
990 nqed.
991
992 (*********************** boolean arithmetics ********************) 
993 include "basics/bool.ma".
994
995 nlet rec eqb n m ≝ 
996 match n with 
997   [ O ⇒ match m with [ O ⇒ true | S q ⇒ false] 
998   | S p ⇒ match m with [ O ⇒ false | S q ⇒ eqb p q]
999   ].
1000            
1001 (*
1002 ntheorem eqb_to_Prop: ∀n,m:nat. 
1003 match (eqb n m) with
1004 [ true  \Rightarrow n = m 
1005 | false \Rightarrow n \neq m].
1006 intros.
1007 apply (nat_elim2
1008 (\lambda n,m:nat.match (eqb n m) with
1009 [ true  \Rightarrow n = m 
1010 | false \Rightarrow n \neq m])).
1011 intro.elim n1.
1012 simplify.reflexivity.
1013 simplify.apply not_eq_O_S.
1014 intro.
1015 simplify.unfold Not.
1016 intro. apply (not_eq_O_S n1).apply sym_eq.assumption.
1017 intros.simplify.
1018 generalize in match H.
1019 elim ((eqb n1 m1)).
1020 simplify.apply eq_f.apply H1.
1021 simplify.unfold Not.intro.apply H1.apply inj_S.assumption.
1022 qed.
1023 *)
1024
1025 ntheorem eqb_elim : ∀ n,m:nat.∀ P:bool → Prop.
1026 (n=m → (P true)) → (n ≠ m → (P false)) → (P (eqb n m)). 
1027 napply nat_elim2; 
1028   ##[#n; ncases n; nnormalize; /3/; 
1029   ##|nnormalize; /3/;
1030   ##|nnormalize; /4/;
1031   ##]
1032 nqed.
1033
1034 ntheorem eqb_n_n: ∀n. eqb n n = true.
1035 #n; nelim n; nnormalize; //.
1036 nqed. 
1037
1038 ntheorem eqb_true_to_eq: ∀n,m:nat. eqb n m = true → n = m.
1039 #n; #m; napply (eqb_elim n m);//;
1040 #_; #abs; napply False_ind;/2/;
1041 nqed.
1042
1043 ntheorem eqb_false_to_not_eq: ∀n,m:nat. eqb n m = false → n ≠ m.
1044 #n; #m; napply (eqb_elim n m);/2/;
1045 nqed.
1046
1047 ntheorem eq_to_eqb_true: ∀n,m:nat.
1048   n = m → eqb n m = true.
1049 //; nqed.
1050
1051 ntheorem not_eq_to_eqb_false: ∀n,m:nat.
1052   n ≠  m → eqb n m = false.
1053 #n; #m; #noteq; 
1054 nelim (true_or_false (eqb n m)); //;
1055 #Heq; napply False_ind; napply noteq;/2/;
1056 nqed.
1057
1058 nlet rec leb n m ≝ 
1059 match n with 
1060     [ O ⇒ true
1061     | (S p) ⇒
1062         match m with 
1063         [ O ⇒ false
1064               | (S q) ⇒ leb p q]].
1065
1066 ntheorem leb_elim: ∀n,m:nat. ∀P:bool → Prop. 
1067 (n ≤ m → P true) → (n ≰ m → P false) → P (leb n m).
1068 napply nat_elim2; nnormalize;
1069   ##[/2/
1070   ##|/3/;
1071   ##|#n; #m; #Hind; #P; #Pt; #Pf; napply Hind;
1072     ##[#lenm; napply Pt; napply le_S_S;//;
1073     ##|#nlenm; napply Pf; #leSS; /3/;
1074     ##]
1075   ##]
1076 nqed.
1077
1078 ntheorem leb_true_to_le:∀n,m.leb n m = true → n ≤ m.
1079 #n; #m; napply leb_elim;
1080   ##[//;
1081   ##|#_; #abs; napply False_ind; /2/;
1082   ##]
1083 nqed.
1084
1085 ntheorem leb_false_to_not_le:∀n,m.
1086   leb n m = false → n ≰ m.
1087 #n; #m; napply leb_elim;
1088   ##[#_; #abs; napply False_ind; /2/;
1089   ##|/2/;
1090   ##]
1091 nqed.
1092
1093 ntheorem le_to_leb_true: ∀n,m. n ≤ m → leb n m = true.
1094 #n; #m; napply leb_elim; //;
1095 #H; #H1; napply False_ind; /2/;
1096 nqed.
1097
1098 ntheorem lt_to_leb_false: ∀n,m. m < n → leb n m = false.
1099 #n; #m; napply leb_elim; //;
1100 #H; #H1; napply False_ind; /2/;
1101 nqed.
1102
1103 (* serve anche ltb? 
1104 ndefinition ltb ≝λn,m. leb (S n) m.
1105
1106 ntheorem ltb_elim: ∀n,m:nat. ∀P:bool → Prop. 
1107 (n < m → P true) → (n ≮ m → P false) → P (ltb n m).
1108 #n; #m; #P; #Hlt; #Hnlt;
1109 napply leb_elim; /3/; nqed.
1110
1111 ntheorem ltb_true_to_lt:∀n,m.ltb n m = true → n < m.
1112 #n; #m; #Hltb; napply leb_true_to_le; nassumption;
1113 nqed.
1114
1115 ntheorem ltb_false_to_not_lt:∀n,m.
1116   ltb n m = false → n ≮ m.
1117 #n; #m; #Hltb; napply leb_false_to_not_le; nassumption;
1118 nqed.
1119
1120 ntheorem lt_to_ltb_true: ∀n,m. n < m → ltb n m = true.
1121 #n; #m; #Hltb; napply le_to_leb_true; nassumption;
1122 nqed.
1123
1124 ntheorem le_to_ltb_false: ∀n,m. m \le n → ltb n m = false.
1125 #n; #m; #Hltb; napply lt_to_leb_false; /2/;
1126 nqed. *)