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