]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/nlibrary/arithmetics/nat.ma
Moved compare in a different file.
[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 ≝
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 /3/; 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; napply nmk; #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; /2/;
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 (* deleterio?
116 ntheorem plus_n_1 : ∀n:nat. S n = n+1.
117 //; nqed.
118 *)
119
120 ntheorem symmetric_plus: symmetric ? plus.
121 #n; nelim n; nnormalize; //; nqed. 
122
123 ntheorem associative_plus : associative nat plus.
124 #n; nelim n; nnormalize; //; nqed.
125
126 ntheorem assoc_plus1: ∀a,b,c. c + (b + a) = b + c + a.
127 //; nqed.
128
129 ntheorem injective_plus_r: ∀n:nat.injective nat nat (λm.n+m).
130 #n; nelim n; nnormalize; /3/; nqed.
131
132 (* ntheorem inj_plus_r: \forall p,n,m:nat. p+n = p+m \to n=m
133 \def injective_plus_r. 
134
135 ntheorem injective_plus_l: ∀m:nat.injective nat nat (λn.n+m).
136 /2/; nqed. *)
137
138 (* ntheorem inj_plus_l: \forall p,n,m:nat. n+p = m+p \to n=m
139 \def injective_plus_l. *)
140
141 (*************************** times *****************************)
142
143 nlet rec times n m ≝ 
144  match n with 
145  [ O ⇒ O
146  | S p ⇒ m+(times p m) ].
147
148 interpretation "natural times" 'times x y = (times x y).
149
150 ntheorem times_Sn_m: ∀n,m:nat. m+n*m = S n*m.
151 //; nqed.
152
153 ntheorem times_O_n: ∀n:nat. O = O*n.
154 //; nqed.
155
156 ntheorem times_n_O: ∀n:nat. O = n*O.
157 #n; nelim n; //; nqed.
158
159 ntheorem times_n_Sm : ∀n,m:nat. n+(n*m) = n*(S m).
160 #n; nelim n; nnormalize; //; nqed.
161
162 ntheorem symmetric_times : symmetric nat times. 
163 #n; nelim n; nnormalize; //; nqed. 
164
165 (* variant sym_times : \forall n,m:nat. n*m = m*n \def
166 symmetric_times. *)
167
168 ntheorem distributive_times_plus : distributive nat times plus.
169 #n; nelim n; nnormalize; //; nqed.
170
171 ntheorem distributive_times_plus_r :
172   ∀a,b,c:nat. (b+c)*a = b*a + c*a.
173 //; nqed. 
174
175 ntheorem associative_times: associative nat times.
176 #n; nelim n; nnormalize; //; nqed.
177
178 nlemma times_times: ∀x,y,z. x*(y*z) = y*(x*z).
179 //; nqed. 
180
181 (* ci servono questi risultati? 
182 ntheorem times_O_to_O: ∀n,m:nat.n*m=O → n=O ∨ m=O.
183 napply nat_elim2; /2/; 
184 #n; #m; #H; nnormalize; #H1; napply False_ind;napply not_eq_O_S;
185 //; nqed.
186   
187 ntheorem times_n_SO : ∀n:nat. n = n * S O.
188 #n; //; nqed.
189
190 ntheorem times_SSO_n : ∀n:nat. n + n = (S(S O)) * n.
191 nnormalize; //; nqed.
192
193 nlemma times_SSO: \forall n.(S(S O))*(S n) = S(S((S(S O))*n)).
194 //; nqed.
195
196 ntheorem or_eq_eq_S: \forall n.\exists m. 
197 n = (S(S O))*m \lor n = S ((S(S O))*m).
198 #n; nelim n;
199   ##[@; /2/;
200   ##|#a; #H; nelim H; #b;#or;nelim or;#aeq;
201     ##[@ b; @ 2; //;
202     ##|@ (S b); @ 1; /2/;
203     ##]
204 nqed.
205 *)
206
207 (******************** ordering relations ************************)
208
209 ninductive le (n:nat) : nat → Prop ≝
210   | le_n : le n n
211   | le_S : ∀ m:nat. le n m → le n (S m).
212
213 interpretation "natural 'less or equal to'" 'leq x y = (le x y).
214
215 interpretation "natural 'neither less nor equal to'" 'nleq x y = (Not (le x y)).
216
217 ndefinition lt: nat → nat → Prop ≝
218 λn,m:nat. S n ≤ m.
219
220 interpretation "natural 'less than'" 'lt x y = (lt x y).
221
222 interpretation "natural 'not less than'" 'nless x y = (Not (lt x y)).
223
224 (* nlemma eq_lt: ∀n,m. (n < m) = (S n ≤ m).
225 //; nqed. *)
226
227 ndefinition ge: nat → nat → Prop ≝
228 λn,m:nat.m ≤ n.
229
230 interpretation "natural 'greater or equal to'" 'geq x y = (ge x y).
231
232 ndefinition gt: nat → nat → Prop ≝
233 λn,m:nat.m<n.
234
235 interpretation "natural 'greater than'" 'gt x y = (gt x y).
236
237 interpretation "natural 'not greater than'" 'ngtr x y = (Not (gt x y)).
238
239 ntheorem transitive_le : transitive nat le.
240 #a; #b; #c; #leab; #lebc;nelim lebc;/2/;
241 nqed.
242
243 (*
244 ntheorem trans_le: \forall n,m,p:nat. n \leq m \to m \leq p \to n \leq p
245 \def transitive_le. *)
246
247
248 ntheorem transitive_lt: transitive nat lt.
249 #a; #b; #c; #ltab; #ltbc;nelim ltbc;/2/;nqed.
250
251 (*
252 theorem trans_lt: \forall n,m,p:nat. lt n m \to lt m p \to lt n p
253 \def transitive_lt. *)
254
255 ntheorem le_S_S: ∀n,m:nat. n ≤ m → S n ≤ S m.
256 #n; #m; #lenm; nelim lenm; /2/; nqed.
257
258 ntheorem le_O_n : ∀n:nat. O ≤ n.
259 #n; nelim n; /2/; nqed.
260
261 ntheorem le_n_Sn : ∀n:nat. n ≤ S n.
262 /2/; nqed.
263
264 ntheorem le_pred_n : ∀n:nat. pred n ≤ n.
265 #n; nelim n; //; nqed.
266
267 (* XXX global problem 
268 nlemma my_trans_le : ∀x,y,z:nat.x ≤ y → y ≤ z → x ≤ z. 
269 napply transitive_le.
270 nqed. *)
271
272 ntheorem monotonic_pred: monotonic ? le pred.
273 #n; #m; #lenm; nelim lenm; /2/;nqed.
274
275 ntheorem le_S_S_to_le: ∀n,m:nat. S n ≤ S m → n ≤ m.
276 (* XXX *) nletin hint ≝ monotonic. 
277 #a; #b; #H; napplyS monotonic_pred;
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/; nqed. 
454
455 ntheorem lt_O_S : ∀n:nat. O < S n.
456 /2/; nqed.
457
458 (*
459 (* other abstract properties *)
460 theorem antisymmetric_le : antisymmetric nat le.
461 unfold antisymmetric.intros 2.
462 apply (nat_elim2 (\lambda n,m.(n \leq m \to m \leq n \to n=m))).
463 intros.apply le_n_O_to_eq.assumption.
464 intros.apply False_ind.apply (not_le_Sn_O ? H).
465 intros.apply eq_f.apply H.
466 apply le_S_S_to_le.assumption.
467 apply le_S_S_to_le.assumption.
468 qed.
469
470 theorem antisym_le: \forall n,m:nat. n \leq m \to m \leq n \to n=m
471 \def antisymmetric_le.
472
473 theorem le_n_m_to_lt_m_Sn_to_eq_n_m: ∀n,m. n ≤ m → m < S n → n=m.
474 intros;
475 unfold lt in H1;
476 generalize in match (le_S_S_to_le ? ? H1);
477 intro;
478 apply antisym_le;
479 assumption.
480 qed.
481 *)
482
483 (* well founded induction principles *)
484
485 ntheorem nat_elim1 : ∀n:nat.∀P:nat → Prop. 
486 (∀m.(∀p. p < m → P p) → P m) → P n.
487 #n; #P; #H; 
488 ncut (∀q:nat. q ≤ n → P q);/2/;
489 nelim n; 
490  ##[#q; #HleO; (* applica male *) 
491     napply (le_n_O_elim ? HleO);
492     napply H; #p; #ltpO;
493     napply False_ind; /2/; (* 3 *)
494  ##|#p; #Hind; #q; #HleS; 
495     napply H; #a; #lta; napply Hind;
496     napply le_S_S_to_le;/2/;
497  ##]
498 nqed.
499
500 (* some properties of functions *)
501 (*
502 definition increasing \def \lambda f:nat \to nat. 
503 \forall n:nat. f n < f (S n).
504
505 theorem increasing_to_monotonic: \forall f:nat \to nat.
506 increasing f \to monotonic nat lt f.
507 unfold monotonic.unfold lt.unfold increasing.unfold lt.intros.elim H1.apply H.
508 apply (trans_le ? (f n1)).
509 assumption.apply (trans_le ? (S (f n1))).
510 apply le_n_Sn.
511 apply H.
512 qed.
513
514 theorem le_n_fn: \forall f:nat \to nat. (increasing f) 
515 \to \forall n:nat. n \le (f n).
516 intros.elim n.
517 apply le_O_n.
518 apply (trans_le ? (S (f n1))).
519 apply le_S_S.apply H1.
520 simplify in H. unfold increasing in H.unfold lt in H.apply H.
521 qed.
522
523 theorem increasing_to_le: \forall f:nat \to nat. (increasing f) 
524 \to \forall m:nat. \exists i. m \le (f i).
525 intros.elim m.
526 apply (ex_intro ? ? O).apply le_O_n.
527 elim H1.
528 apply (ex_intro ? ? (S a)).
529 apply (trans_le ? (S (f a))).
530 apply le_S_S.assumption.
531 simplify in H.unfold increasing in H.unfold lt in H.
532 apply H.
533 qed.
534
535 theorem increasing_to_le2: \forall f:nat \to nat. (increasing f) 
536 \to \forall m:nat. (f O) \le m \to 
537 \exists i. (f i) \le m \land m <(f (S i)).
538 intros.elim H1.
539 apply (ex_intro ? ? O).
540 split.apply le_n.apply H.
541 elim H3.elim H4.
542 cut ((S n1) < (f (S a)) \lor (S n1) = (f (S a))).
543 elim Hcut.
544 apply (ex_intro ? ? a).
545 split.apply le_S. assumption.assumption.
546 apply (ex_intro ? ? (S a)).
547 split.rewrite < H7.apply le_n.
548 rewrite > H7.
549 apply H.
550 apply le_to_or_lt_eq.apply H6.
551 qed.
552 *)
553
554 (*********************** monotonicity ***************************)
555 ntheorem monotonic_le_plus_r: 
556 ∀n:nat.monotonic nat le (λm.n + m).
557 #n; #a; #b; nelim n; nnormalize; //;
558 #m; #H; #leab;napply le_S_S; /2/; nqed.
559
560 (*
561 ntheorem le_plus_r: ∀p,n,m:nat. n ≤ m → p + n ≤ p + m
562 ≝ monotonic_le_plus_r. *)
563
564 ntheorem monotonic_le_plus_l: 
565 ∀m:nat.monotonic nat le (λn.n + m).
566 /2/; nqed.
567
568 (*
569 ntheorem le_plus_l: \forall p,n,m:nat. n \le m \to n + p \le m + p
570 \def monotonic_le_plus_l. *)
571
572 ntheorem le_plus: ∀n1,n2,m1,m2:nat. n1 ≤ n2  → m1 ≤ m2 
573 → n1 + m1 ≤ n2 + m2.
574 #n1; #n2; #m1; #m2; #len; #lem; napply (transitive_le ? (n1+m2));
575 /2/; nqed.
576
577 ntheorem le_plus_n :∀n,m:nat. m ≤ n + m.
578 /2/; nqed. 
579
580 nlemma le_plus_a: ∀a,n,m. n ≤ m → n ≤ a + m.
581 /2/; nqed.
582
583 nlemma le_plus_b: ∀b,n,m. n + b ≤ m → n ≤ m.
584 /2/; nqed.
585
586 ntheorem le_plus_n_r :∀n,m:nat. m ≤ m + n.
587 /2/; nqed.
588
589 ntheorem eq_plus_to_le: ∀n,m,p:nat.n=m+p → m ≤ n.
590 //; nqed.
591
592 ntheorem le_plus_to_le: ∀a,n,m. a + n ≤ a + m → n ≤ m.
593 #a; nelim a; nnormalize; /3/; nqed. 
594
595 ntheorem le_plus_to_le_r: ∀a,n,m. n + a ≤ m +a → n ≤ m.
596 /2/; nqed. 
597
598 (* plus & lt *)
599
600 ntheorem monotonic_lt_plus_r: 
601 ∀n:nat.monotonic nat lt (λm.n+m).
602 /2/; nqed.
603
604 (*
605 variant lt_plus_r: \forall n,p,q:nat. p < q \to n + p < n + q \def
606 monotonic_lt_plus_r. *)
607
608 ntheorem monotonic_lt_plus_l: 
609 ∀n:nat.monotonic nat lt (λm.m+n).
610 /2/; nqed.
611
612 (*
613 variant lt_plus_l: \forall n,p,q:nat. p < q \to p + n < q + n \def
614 monotonic_lt_plus_l. *)
615
616 ntheorem lt_plus: ∀n,m,p,q:nat. n < m → p < q → n + p < m + q.
617 #n; #m; #p; #q; #ltnm; #ltpq;
618 napply (transitive_lt ? (n+q));/2/; nqed.
619
620 ntheorem lt_plus_to_lt_l :∀n,p,q:nat. p+n < q+n → p<q.
621 /2/; nqed.
622
623 ntheorem lt_plus_to_lt_r :∀n,p,q:nat. n+p < n+q → p<q.
624 /2/; nqed.
625
626 (*
627 ntheorem le_to_lt_to_lt_plus: ∀a,b,c,d:nat.
628 a ≤ c → b < d → a + b < c+d.
629 (* bello /2/ un po' lento *)
630 #a; #b; #c; #d; #leac; #lebd; 
631 nnormalize; napplyS le_plus; //; nqed.
632 *)
633
634 (* times *)
635 ntheorem monotonic_le_times_r: 
636 ∀n:nat.monotonic nat le (λm. n * m).
637 #n; #x; #y; #lexy; nelim n; nnormalize;//;(* lento /2/;*)
638 #a; #lea; napply le_plus; //;
639 nqed.
640
641 (*
642 ntheorem le_times_r: \forall p,n,m:nat. n \le m \to p*n \le p*m
643 \def monotonic_le_times_r. *)
644
645 (*
646 ntheorem monotonic_le_times_l: 
647 ∀m:nat.monotonic nat le (λn.n*m).
648 /2/; nqed.
649 *)
650
651 (*
652 theorem le_times_l: \forall p,n,m:nat. n \le m \to n*p \le m*p
653 \def monotonic_le_times_l. *)
654
655 ntheorem le_times: ∀n1,n2,m1,m2:nat. 
656 n1 ≤ n2  → m1 ≤ m2 → n1*m1 ≤ n2*m2.
657 #n1; #n2; #m1; #m2; #len; #lem; 
658 napply (transitive_le ? (n1*m2)); (* /2/ slow *)
659  ##[ napply monotonic_le_times_r;//; 
660  ##| napplyS monotonic_le_times_r;//;
661  ##]
662 nqed.
663
664 (* interesssante *)
665 ntheorem lt_times_n: ∀n,m:nat. O < n → m ≤ n*m.
666 #n; #m; #H; /2/; nqed.
667
668 ntheorem le_times_to_le: 
669 ∀a,n,m. O < a → a * n ≤ a * m → n ≤ m.
670 #a; napply nat_elim2; nnormalize;
671   ##[//;
672   ##|#n; #H1; #H2; 
673      napply (transitive_le ? (a*S n));/2/;
674   ##|#n; #m; #H; #lta; #le;
675      napply le_S_S; napply H; /2/;
676   ##]
677 nqed.
678
679 ntheorem le_S_times_2: ∀n,m.O < m → n ≤ m → S n ≤ 2*m.
680 #n; #m; #posm; #lenm;  (* interessante *)
681 napplyS (le_plus n m); //; nqed.
682
683 (* times & lt *)
684 (*
685 ntheorem lt_O_times_S_S: ∀n,m:nat.O < (S n)*(S m).
686 intros.simplify.unfold lt.apply le_S_S.apply le_O_n.
687 qed. *)
688
689 (*
690 ntheorem lt_times_eq_O: \forall a,b:nat.
691 O < a → a * b = O → b = O.
692 intros.
693 apply (nat_case1 b)
694 [ intros.
695   reflexivity
696 | intros.
697   rewrite > H2 in H1.
698   rewrite > (S_pred a) in H1
699   [ apply False_ind.
700     apply (eq_to_not_lt O ((S (pred a))*(S m)))
701     [ apply sym_eq.
702       assumption
703     | apply lt_O_times_S_S
704     ]
705   | assumption
706   ]
707 ]
708 qed. 
709
710 theorem O_lt_times_to_O_lt: \forall a,c:nat.
711 O \lt (a * c) \to O \lt a.
712 intros.
713 apply (nat_case1 a)
714 [ intros.
715   rewrite > H1 in H.
716   simplify in H.
717   assumption
718 | intros.
719   apply lt_O_S
720 ]
721 qed.
722
723 lemma lt_times_to_lt_O: \forall i,n,m:nat. i < n*m \to O < m.
724 intros.
725 elim (le_to_or_lt_eq O ? (le_O_n m))
726   [assumption
727   |apply False_ind.
728    rewrite < H1 in H.
729    rewrite < times_n_O in H.
730    apply (not_le_Sn_O ? H)
731   ]
732 qed. *)
733
734 (*
735 ntheorem monotonic_lt_times_r: 
736 ∀n:nat.monotonic nat lt (λm.(S n)*m).
737 /2/; 
738 simplify.
739 intros.elim n.
740 simplify.rewrite < plus_n_O.rewrite < plus_n_O.assumption.
741 apply lt_plus.assumption.assumption.
742 qed. *)
743
744 ntheorem monotonic_lt_times_l: 
745   ∀c:nat. O < c → monotonic nat lt (λt.(t*c)).
746 #c; #posc; #n; #m; #ltnm;
747 nelim ltnm; nnormalize;
748   ##[/2/; 
749   ##|#a; #_; #lt1; napply (transitive_le ??? lt1);//;
750   ##]
751 nqed.
752
753 ntheorem monotonic_lt_times_r: 
754   ∀c:nat. O < c → monotonic nat lt (λt.(c*t)).
755 /2/; nqed.
756
757 ntheorem lt_to_le_to_lt_times: 
758 ∀n,m,p,q:nat. n < m → p ≤ q → O < q → n*p < m*q.
759 #n; #m; #p; #q; #ltnm; #lepq; #posq;
760 napply (le_to_lt_to_lt ? (n*q));
761   ##[napply monotonic_le_times_r;//;
762   ##|napply monotonic_lt_times_l;//;
763   ##]
764 nqed.
765
766 ntheorem lt_times:∀n,m,p,q:nat. n<m → p<q → n*p < m*q.
767 #n; #m; #p; #q; #ltnm; #ltpq;
768 napply lt_to_le_to_lt_times;/2/;
769 nqed.
770
771 ntheorem lt_times_n_to_lt_l: 
772 ∀n,p,q:nat. p*n < q*n → p < q.
773 #n; #p; #q; #Hlt;
774 nelim (decidable_lt p q);//;
775 #nltpq; napply False_ind; 
776 napply (absurd ? ? (lt_to_not_le ? ? Hlt));
777 napplyS monotonic_le_times_r;/2/;
778 nqed.
779
780 ntheorem lt_times_n_to_lt_r: 
781 ∀n,p,q:nat. n*p < n*q → p < q.
782 /2/; nqed.
783
784 (*
785 theorem nat_compare_times_l : \forall n,p,q:nat. 
786 nat_compare p q = nat_compare ((S n) * p) ((S n) * q).
787 intros.apply nat_compare_elim.intro.
788 apply nat_compare_elim.
789 intro.reflexivity.
790 intro.absurd (p=q).
791 apply (inj_times_r n).assumption.
792 apply lt_to_not_eq. assumption.
793 intro.absurd (q<p).
794 apply (lt_times_to_lt_r n).assumption.
795 apply le_to_not_lt.apply lt_to_le.assumption.
796 intro.rewrite < H.rewrite > nat_compare_n_n.reflexivity.
797 intro.apply nat_compare_elim.intro.
798 absurd (p<q).
799 apply (lt_times_to_lt_r n).assumption.
800 apply le_to_not_lt.apply lt_to_le.assumption.
801 intro.absurd (q=p).
802 symmetry.
803 apply (inj_times_r n).assumption.
804 apply lt_to_not_eq.assumption.
805 intro.reflexivity.
806 qed. *)
807
808 (* times and plus 
809 theorem lt_times_plus_times: \forall a,b,n,m:nat. 
810 a < n \to b < m \to a*m + b < n*m.
811 intros 3.
812 apply (nat_case n)
813   [intros.apply False_ind.apply (not_le_Sn_O ? H)
814   |intros.simplify.
815    rewrite < sym_plus.
816    unfold.
817    change with (S b+a*m1 \leq m1+m*m1).
818    apply le_plus
819     [assumption
820     |apply le_times
821       [apply le_S_S_to_le.assumption
822       |apply le_n
823       ]
824     ]
825   ]
826 qed. *)
827
828 (************************** minus ******************************)
829
830 nlet rec minus n m ≝ 
831  match n with 
832  [ O ⇒ O
833  | S p ⇒ 
834         match m with
835           [ O ⇒ S p
836     | S q ⇒ minus p q ]].
837         
838 interpretation "natural minus" 'minus x y = (minus x y).
839
840 ntheorem minus_S_S: ∀n,m:nat.S n - S m = n -m.
841 //; nqed.
842
843 ntheorem minus_O_n: ∀n:nat.O=O-n.
844 #n; ncases n; //; nqed.
845
846 ntheorem minus_n_O: ∀n:nat.n=n-O.
847 #n; ncases n; //; nqed.
848
849 ntheorem minus_n_n: ∀n:nat.O=n-n.
850 #n; nelim n; //; nqed.
851
852 ntheorem minus_Sn_n: ∀n:nat. S O = (S n)-n.
853 #n; nelim n; nnormalize; //; nqed.
854
855 ntheorem minus_Sn_m: ∀m,n:nat. m ≤ n → S n -m = S (n-m).
856 (* qualcosa da capire qui 
857 #n; #m; #lenm; nelim lenm; napplyS refl_eq. *)
858 napply nat_elim2; 
859   ##[//
860   ##|#n; #abs; napply False_ind; /2/ 
861   ##|#n; #m; #Hind; #c; napplyS Hind; /2/;
862   ##]
863 nqed.
864
865 ntheorem not_eq_to_le_to_le_minus: 
866   ∀n,m.n ≠ m → n ≤ m → n ≤ m - 1.
867 #n; #m; ncases m;//; #m; nnormalize;
868 #H; #H1; napply le_S_S_to_le;
869 napplyS (not_eq_to_le_to_lt n (S m) H H1);
870 nqed.
871
872 ntheorem eq_minus_S_pred: ∀n,m. n - (S m) = pred(n -m).
873 napply nat_elim2; nnormalize; //; nqed.
874
875 ntheorem plus_minus:
876 ∀m,n,p:nat. m ≤ n → (n-m)+p = (n+p)-m.
877 napply nat_elim2; 
878   ##[//
879   ##|#n; #p; #abs; napply False_ind; /2/;
880   ##|nnormalize;/3/;
881   ##]
882 nqed.
883
884 ntheorem minus_plus_m_m: ∀n,m:nat.n = (n+m)-m.
885 #n; #m; napplyS (plus_minus m m n); //; nqed.
886
887 ntheorem plus_minus_m_m: ∀n,m:nat.
888   m ≤ n → n = (n-m)+m.
889 #n; #m; #lemn; napplyS symmetric_eq; 
890 napplyS (plus_minus m n m); //; nqed.
891
892 ntheorem le_plus_minus_m_m: ∀n,m:nat. n ≤ (n-m)+m.
893 #n; nelim n;
894   ##[//
895   ##|#a; #Hind; #m; ncases m;//;  
896      nnormalize; #n;/2/;  
897   ##]
898 nqed.
899
900 ntheorem minus_to_plus :∀n,m,p:nat.
901   m ≤ n → n-m = p → n = m+p.
902 #n; #m; #p; #lemn; #eqp; napplyS plus_minus_m_m; //;
903 nqed.
904
905 ntheorem plus_to_minus :∀n,m,p:nat.n = m+p → n-m = p.
906 (* /4/ done in 43.5 *)
907 #n; #m; #p; #eqp; 
908 napply symmetric_eq;
909 napplyS (minus_plus_m_m p m);
910 nqed.
911
912 ntheorem minus_pred_pred : ∀n,m:nat. O < n → O < m → 
913 pred n - pred m = n - m.
914 #n; #m; #posn; #posm;
915 napply (lt_O_n_elim n posn); 
916 napply (lt_O_n_elim m posm);//.
917 nqed.
918
919 (*
920 theorem eq_minus_n_m_O: \forall n,m:nat.
921 n \leq m \to n-m = O.
922 intros 2.
923 apply (nat_elim2 (\lambda n,m.n \leq m \to n-m = O)).
924 intros.simplify.reflexivity.
925 intros.apply False_ind.
926 apply not_le_Sn_O;
927 [2: apply H | skip].
928 intros.
929 simplify.apply H.apply le_S_S_to_le. apply H1.
930 qed.
931
932 theorem le_SO_minus: \forall n,m:nat.S n \leq m \to S O \leq m-n.
933 intros.elim H.elim (minus_Sn_n n).apply le_n.
934 rewrite > minus_Sn_m.
935 apply le_S.assumption.
936 apply lt_to_le.assumption.
937 qed.
938
939 theorem minus_le_S_minus_S: \forall n,m:nat. m-n \leq S (m-(S n)).
940 intros.
941 apply (nat_elim2 (\lambda n,m.m-n \leq S (m-(S n)))).
942 intro.elim n1.simplify.apply le_n_Sn.
943 simplify.rewrite < minus_n_O.apply le_n.
944 intros.simplify.apply le_n_Sn.
945 intros.simplify.apply H.
946 qed.
947
948 theorem lt_minus_S_n_to_le_minus_n : \forall n,m,p:nat. m-(S n) < p \to m-n \leq p. 
949 intros 3.intro.
950 (* autobatch *)
951 (* end auto($Revision: 9739 $) proof: TIME=1.33 SIZE=100 DEPTH=100 *)
952 apply (trans_le (m-n) (S (m-(S n))) p).
953 apply minus_le_S_minus_S.
954 assumption.
955 qed.
956
957 theorem le_minus_m: \forall n,m:nat. n-m \leq n.
958 intros.apply (nat_elim2 (\lambda m,n. n-m \leq n)).
959 intros.rewrite < minus_n_O.apply le_n.
960 intros.simplify.apply le_n.
961 intros.simplify.apply le_S.assumption.
962 qed.
963
964 theorem lt_minus_m: \forall n,m:nat. O < n \to O < m \to n-m \lt n.
965 intros.apply (lt_O_n_elim n H).intro.
966 apply (lt_O_n_elim m H1).intro.
967 simplify.unfold lt.apply le_S_S.apply le_minus_m.
968 qed.
969
970 theorem minus_le_O_to_le: \forall n,m:nat. n-m \leq O \to n \leq m.
971 intros 2.
972 apply (nat_elim2 (\lambda n,m:nat.n-m \leq O \to n \leq m)).
973 intros.apply le_O_n.
974 simplify.intros. assumption.
975 simplify.intros.apply le_S_S.apply H.assumption.
976 qed.
977 *)
978
979 (* monotonicity and galois *)
980
981 ntheorem monotonic_le_minus_l: 
982 ∀p,q,n:nat. q ≤ p → q-n ≤ p-n.
983 napply nat_elim2; #p; #q;
984   ##[#lePO; napply (le_n_O_elim ? lePO);//;
985   ##|//;
986   ##|#Hind; #n; ncases n;
987     ##[//;
988     ##|#a; #leSS; napply Hind; /2/;
989     ##]
990   ##]
991 nqed.
992
993 ntheorem le_minus_to_plus: ∀n,m,p. n-m ≤ p → n≤ p+m.
994 #n; #m; #p; #lep;
995 napply transitive_le;
996   ##[##|napply le_plus_minus_m_m
997   ##|napply monotonic_le_plus_l;//;
998   ##]
999 nqed.
1000
1001 ntheorem le_plus_to_minus: ∀n,m,p. n ≤ p+m → n-m ≤ p.
1002 #n; #m; #p; #lep;
1003 (* bello *)
1004 napplyS monotonic_le_minus_l;//;
1005 (* /2/; *)
1006 nqed.
1007
1008 ntheorem monotonic_le_minus_r: 
1009 ∀p,q,n:nat. q ≤ p → n-p ≤ n-q.
1010 #p; #q; #n; #lepq;
1011 napply le_plus_to_minus;
1012 napply (transitive_le ??? (le_plus_minus_m_m ? q));/2/;
1013 nqed.
1014
1015 (*********************** boolean arithmetics ********************) 
1016 include "basics/bool.ma".
1017
1018 nlet rec eqb n m ≝ 
1019 match n with 
1020   [ O ⇒ match m with [ O ⇒ true | S q ⇒ false] 
1021   | S p ⇒ match m with [ O ⇒ false | S q ⇒ eqb p q]
1022   ].
1023            
1024 (*
1025 ntheorem eqb_to_Prop: ∀n,m:nat. 
1026 match (eqb n m) with
1027 [ true  \Rightarrow n = m 
1028 | false \Rightarrow n \neq m].
1029 intros.
1030 apply (nat_elim2
1031 (\lambda n,m:nat.match (eqb n m) with
1032 [ true  \Rightarrow n = m 
1033 | false \Rightarrow n \neq m])).
1034 intro.elim n1.
1035 simplify.reflexivity.
1036 simplify.apply not_eq_O_S.
1037 intro.
1038 simplify.unfold Not.
1039 intro. apply (not_eq_O_S n1).apply sym_eq.assumption.
1040 intros.simplify.
1041 generalize in match H.
1042 elim ((eqb n1 m1)).
1043 simplify.apply eq_f.apply H1.
1044 simplify.unfold Not.intro.apply H1.apply inj_S.assumption.
1045 qed.
1046 *)
1047
1048 naxiom eqb_elim : ∀ n,m:nat.∀ P:bool → Prop.
1049 (n=m → (P true)) → (n ≠ m → (P false)) → (P (eqb n m)). 
1050 (*
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