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