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