]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/arithmetics/nat.ma
- the definition of the framework for strong normalization continues ...
[helm.git] / matita / matita / lib / arithmetics / nat.ma
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department of the University of Bologna, Italy.                     
5     ||I||                                                                 
6     ||T||  
7     ||A||  This file is distributed under the terms of the 
8     \   /  GNU General Public License Version 2        
9      \ /      
10       V_______________________________________________________________ *)
11
12 include "basics/relations.ma".
13
14 (* Definitions **************************************************************)
15
16 (* natural numbers *)
17
18 inductive nat : Type[0] ≝
19   | O : nat
20   | S : nat → nat.
21   
22 interpretation "Natural numbers" 'N = nat.
23
24 alias num (instance 0) = "natural number".
25
26 definition pred ≝
27  λn. match n with [ O ⇒ O | S p ⇒ p].
28
29 definition not_zero: nat → Prop ≝
30  λn: nat. match n with [ O ⇒ False | (S p) ⇒ True ].
31
32 (* order relations *)
33
34 inductive le (n:nat) : nat → Prop ≝
35   | le_n : le n n
36   | le_S : ∀ m:nat. le n m → le n (S m).
37
38 interpretation "natural 'less or equal to'" 'leq x y = (le x y).
39
40 interpretation "natural 'neither less nor equal to'" 'nleq x y = (Not (le x y)).
41
42 definition lt: nat → nat → Prop ≝ λn,m. S n ≤ m.
43
44 interpretation "natural 'less than'" 'lt x y = (lt x y).
45
46 interpretation "natural 'not less than'" 'nless x y = (Not (lt x y)).
47
48 definition ge: nat → nat → Prop ≝ λn,m.m ≤ n.
49
50 interpretation "natural 'greater or equal to'" 'geq x y = (ge x y).
51
52 definition gt: nat → nat → Prop ≝ λn,m.m<n.
53
54 interpretation "natural 'greater than'" 'gt x y = (gt x y).
55
56 interpretation "natural 'not greater than'" 'ngtr x y = (Not (gt x y)).
57
58 (* abstract properties *)
59
60 definition increasing ≝ λf:nat → nat. ∀n:nat. f n < f (S n).
61
62 (* arithmetic operations *)
63
64 let rec plus n m ≝ 
65  match n with [ O ⇒ m | S p ⇒ S (plus p m) ].
66
67 interpretation "natural plus" 'plus x y = (plus x y).
68
69 let rec times n m ≝ 
70  match n with [ O ⇒ 0 | S p ⇒ m + (times p m) ].
71
72 interpretation "natural times" 'times x y = (times x y).
73
74 let rec minus n m ≝ 
75  match n with 
76  [ O ⇒ O
77  | S p ⇒ 
78         match m with
79           [ O ⇒ S p
80     | S q ⇒ minus p q ]].
81         
82 interpretation "natural minus" 'minus x y = (minus x y).
83
84 (* Generic conclusion ******************************************************)
85
86 theorem nat_case:
87  ∀n:nat.∀P:nat → Prop. 
88   (n=O → P O) → (∀m:nat. n= S m → P (S m)) → P n.
89 #n #P (elim n) /2/ qed.
90
91 theorem nat_elim2 :
92  ∀R:nat → nat → Prop.
93   (∀n:nat. R O n) 
94   → (∀n:nat. R (S n) O)
95   → (∀n,m:nat. R n m → R (S n) (S m))
96   → ∀n,m:nat. R n m.
97 #R #ROn #RSO #RSS #n (elim n) // #n0 #Rn0m #m (cases m) /2/ qed.
98
99 lemma le_gen: ∀P:nat → Prop.∀n.(∀i. i ≤ n → P i) → P n.
100 /2/ qed.
101
102 (* Equalities ***************************************************************)
103
104 theorem pred_Sn : ∀n. n = pred (S n).
105 // qed.
106
107 theorem injective_S : injective nat nat S.
108 // qed.
109
110 theorem S_pred: ∀n. 0 < n → S(pred n) = n.
111 #n #posn (cases posn) //
112 qed.
113
114 theorem plus_O_n: ∀n:nat. n = 0 + n.
115 // qed.
116
117 theorem plus_n_O: ∀n:nat. n = n + 0.
118 #n (elim n) normalize // qed.
119
120 theorem plus_n_Sm : ∀n,m:nat. S (n+m) = n + S m.
121 #n (elim n) normalize // qed.
122
123 theorem commutative_plus: commutative ? plus.
124 #n (elim n) normalize // qed. 
125
126 theorem associative_plus : associative nat plus.
127 #n (elim n) normalize // qed.
128
129 theorem assoc_plus1: ∀a,b,c. c + (b + a) = b + c + a.
130 // qed. 
131
132 theorem injective_plus_r: ∀n:nat.injective nat nat (λm.n+m).
133 #n (elim n) normalize /3/ qed.
134
135 theorem not_eq_S: ∀n,m:nat. n ≠ m → S n ≠ S m.
136 /2/ qed.
137
138 theorem times_Sn_m: ∀n,m:nat. m+n*m = S n*m.
139 // qed.
140
141 theorem times_O_n: ∀n:nat. 0 = 0 * n.
142 // qed.
143
144 theorem times_n_O: ∀n:nat. 0 = n * 0.
145 #n (elim n) // qed.
146
147 theorem times_n_Sm : ∀n,m:nat. n+(n*m) = n*(S m).
148 #n (elim n) normalize // qed.
149
150 theorem commutative_times : commutative nat times. 
151 #n (elim n) normalize // qed. 
152
153 theorem distributive_times_plus : distributive nat times plus.
154 #n (elim n) normalize // qed.
155
156 theorem distributive_times_plus_r :
157   ∀a,b,c:nat. (b+c)*a = b*a + c*a.
158 // qed. 
159
160 theorem associative_times: associative nat times.
161 #n (elim n) normalize // qed.
162
163 lemma times_times: ∀x,y,z. x*(y*z) = y*(x*z).
164 // qed. 
165
166 theorem times_n_1 : ∀n:nat. n = n * 1.
167 #n // qed.
168
169 theorem minus_S_S: ∀n,m:nat.S n - S m = n -m.
170 // qed.
171
172 theorem minus_O_n: ∀n:nat.0=0-n.
173 #n (cases n) // qed.
174
175 theorem minus_n_O: ∀n:nat.n=n-0.
176 #n (cases n) // qed.
177
178 theorem minus_n_n: ∀n:nat.0=n-n.
179 #n (elim n) // qed.
180
181 theorem minus_Sn_n: ∀n:nat. S 0 = (S n)-n.
182 #n (elim n) normalize // qed.
183
184 theorem eq_minus_S_pred: ∀n,m. n - (S m) = pred(n -m).
185 @nat_elim2 normalize // qed.
186
187 lemma plus_plus_comm_23: ∀x,y,z. x + y + z = x + z + y.
188 // qed.
189
190 (* Negated equalities *******************************************************)
191
192 theorem not_eq_O_S : ∀n:nat. 0 ≠ S n.
193 #n @nmk #eqOS (change with (not_zero O)) >eqOS // qed.
194
195 theorem not_eq_n_Sn: ∀n:nat. n ≠ S n.
196 #n (elim n) /2/ qed.
197
198 (* Atomic conclusion *******************************************************)
199
200 (* not_zero *)
201
202 theorem lt_to_not_zero : ∀n,m:nat. n < m → not_zero m.
203 #n #m #Hlt (elim Hlt) // qed.
204
205 (* le *)
206
207 theorem le_S_S: ∀n,m:nat. n ≤ m → S n ≤ S m.
208 #n #m #lenm (elim lenm) /2/ qed.
209
210 theorem le_O_n : ∀n:nat. 0 ≤ n.
211 #n (elim n) /2/ qed.
212
213 theorem le_n_Sn : ∀n:nat. n ≤ S n.
214 /2/ qed.
215
216 theorem transitive_le : transitive nat le.
217 #a #b #c #leab #lebc (elim lebc) /2/
218 qed.
219
220 theorem le_pred_n : ∀n:nat. pred n ≤ n.
221 #n (elim n) // qed.
222
223 theorem monotonic_pred: monotonic ? le pred.
224 #n #m #lenm (elim lenm) /2/ qed.
225
226 theorem le_S_S_to_le: ∀n,m:nat. S n ≤ S m → n ≤ m.
227 (* demo *)
228 /2/ qed-.
229
230 theorem monotonic_le_plus_r: 
231 ∀n:nat.monotonic nat le (λm.n + m).
232 #n #a #b (elim n) normalize //
233 #m #H #leab @le_S_S /2/ qed.
234
235 theorem monotonic_le_plus_l: 
236 ∀m:nat.monotonic nat le (λn.n + m).
237 /2/ qed.
238
239 theorem le_plus: ∀n1,n2,m1,m2:nat. n1 ≤ n2  → m1 ≤ m2 
240 → n1 + m1 ≤ n2 + m2.
241 #n1 #n2 #m1 #m2 #len #lem @(transitive_le ? (n1+m2))
242 /2/ qed-.
243
244 theorem le_plus_n :∀n,m:nat. m ≤ n + m.
245 /2/ qed. 
246
247 lemma le_plus_a: ∀a,n,m. n ≤ m → n ≤ a + m.
248 /2/ qed.
249
250 lemma le_plus_b: ∀b,n,m. n + b ≤ m → n ≤ m.
251 /2/ qed.
252
253 theorem le_plus_n_r :∀n,m:nat. m ≤ m + n.
254 /2/ qed.
255
256 theorem eq_plus_to_le: ∀n,m,p:nat.n=m+p → m ≤ n.
257 // qed-.
258
259 theorem le_plus_to_le: ∀a,n,m. a + n ≤ a + m → n ≤ m.
260 #a (elim a) normalize /3/ qed. 
261
262 theorem le_plus_to_le_r: ∀a,n,m. n + a ≤ m +a → n ≤ m.
263 /2/ qed-. 
264
265 theorem monotonic_le_times_r: 
266 ∀n:nat.monotonic nat le (λm. n * m).
267 #n #x #y #lexy (elim n) normalize//(* lento /2/*)
268 #a #lea @le_plus //
269 qed.
270
271 theorem le_times: ∀n1,n2,m1,m2:nat. 
272 n1 ≤ n2  → m1 ≤ m2 → n1*m1 ≤ n2*m2.
273 #n1 #n2 #m1 #m2 #len #lem @(transitive_le ? (n1*m2)) /2/
274 qed.
275
276 (* interessante *)
277 theorem lt_times_n: ∀n,m:nat. O < n → m ≤ n*m.
278 #n #m #H /2/ qed.
279
280 theorem le_times_to_le: 
281 ∀a,n,m. O < a → a * n ≤ a * m → n ≤ m.
282 #a @nat_elim2 normalize
283   [//
284   |#n #H1 #H2 
285      @(transitive_le ? (a*S n)) /2/
286   |#n #m #H #lta #le
287      @le_S_S @H /2/
288   ]
289 qed-.
290
291 theorem le_plus_minus_m_m: ∀n,m:nat. n ≤ (n-m)+m.
292 #n (elim n) // #a #Hind #m (cases m) // normalize #n/2/  
293 qed.
294
295 theorem le_plus_to_minus_r: ∀a,b,c. a + b ≤ c → a ≤ c -b.
296 #a #b #c #H @(le_plus_to_le_r … b) /2/
297 qed.
298
299 (* lt *)
300
301 theorem transitive_lt: transitive nat lt.
302 #a #b #c #ltab #ltbc (elim ltbc) /2/
303 qed.
304
305 theorem lt_to_le_to_lt: ∀n,m,p:nat. n < m → m ≤ p → n < p.
306 #n #m #p #H #H1 (elim H1) /2/ qed.
307
308 theorem le_to_lt_to_lt: ∀n,m,p:nat. n ≤ m → m < p → n < p.
309 #n #m #p #H (elim H) /3/ qed.
310
311 theorem lt_S_to_lt: ∀n,m. S n < m → n < m.
312 /2/ qed.
313
314 theorem ltn_to_ltO: ∀n,m:nat. n < m → 0 < m.
315 /2/ qed.
316
317 theorem lt_O_S : ∀n:nat. O < S n.
318 /2/ qed.
319
320 theorem monotonic_lt_plus_r: 
321 ∀n:nat.monotonic nat lt (λm.n+m).
322 /2/ qed.
323
324 theorem monotonic_lt_plus_l: 
325 ∀n:nat.monotonic nat lt (λm.m+n).
326 /2/ qed.
327
328 theorem lt_plus: ∀n,m,p,q:nat. n < m → p < q → n + p < m + q.
329 #n #m #p #q #ltnm #ltpq
330 @(transitive_lt ? (n+q))/2/ qed.
331
332 theorem lt_plus_to_lt_l :∀n,p,q:nat. p+n < q+n → p<q.
333 /2/ qed.
334
335 theorem lt_plus_to_lt_r :∀n,p,q:nat. n+p < n+q → p<q.
336 /2/ qed-.
337
338 theorem increasing_to_monotonic: ∀f:nat → nat.
339   increasing f → monotonic nat lt f.
340 #f #incr #n #m #ltnm (elim ltnm) /2/
341 qed.
342
343 theorem monotonic_lt_times_r: 
344   ∀c:nat. O < c → monotonic nat lt (λt.(c*t)).
345 #c #posc #n #m #ltnm
346 (elim ltnm) normalize
347   [/2/ 
348   |#a #_ #lt1 @(transitive_le … lt1) //
349   ]
350 qed.
351
352 theorem monotonic_lt_times_l: 
353   ∀c:nat. 0 < c → monotonic nat lt (λt.(t*c)).
354 /2/
355 qed.
356
357 theorem lt_to_le_to_lt_times: 
358 ∀n,m,p,q:nat. n < m → p ≤ q → O < q → n*p < m*q.
359 #n #m #p #q #ltnm #lepq #posq
360 @(le_to_lt_to_lt ? (n*q))
361   [@monotonic_le_times_r //
362   |@monotonic_lt_times_l //
363   ]
364 qed.
365
366 theorem lt_times:∀n,m,p,q:nat. n<m → p<q → n*p < m*q.
367 #n #m #p #q #ltnm #ltpq @lt_to_le_to_lt_times/2/
368 qed.
369
370 theorem lt_plus_to_minus_r: ∀a,b,c. a + b < c → a < c - b.
371 #a #b #c #H @le_plus_to_minus_r //
372 qed.
373
374 theorem lt_S_S_to_lt: ∀n,m:nat. S n < S m → n < m.
375 (* demo *)
376 /2/ qed-.
377
378 (* not le, lt *)
379
380 theorem not_le_Sn_O: ∀ n:nat. S n ≰ 0.
381 #n @nmk #Hlen0 @(lt_to_not_zero ?? Hlen0) qed.
382
383 theorem not_le_to_not_le_S_S: ∀ n,m:nat. n ≰ m → S n ≰ S m.
384 /3/ qed.
385
386 theorem not_le_S_S_to_not_le: ∀ n,m:nat. S n ≰ S m → n ≰ m.
387 /3/ qed.
388
389 theorem not_le_Sn_n: ∀n:nat. S n ≰ n.
390 #n (elim n) /2/ qed.
391
392 theorem lt_to_not_le: ∀n,m. n < m → m ≰ n.
393 #n #m #Hltnm (elim Hltnm) /3/ qed.
394
395 theorem not_le_to_lt: ∀n,m. n ≰ m → m < n.
396 @nat_elim2 #n
397  [#abs @False_ind /2/
398  |/2/
399  |#m #Hind #HnotleSS @le_S_S /3/
400  ]
401 qed.
402
403 (* not lt, le *)
404
405 theorem not_lt_to_le: ∀n,m:nat. n ≮ m → m ≤ n.
406 /4/ qed.
407
408 theorem le_to_not_lt: ∀n,m:nat. n ≤ m → m ≮ n.
409 #n #m #H @lt_to_not_le /2/ (* /3/ *) qed.
410
411 (* Compound conclusion ******************************************************)
412
413 theorem decidable_eq_nat : ∀n,m:nat.decidable (n=m).
414 @nat_elim2 #n [ (cases n) /2/ | /3/ | #m #Hind (cases Hind) /3/]
415 qed. 
416
417 theorem decidable_le: ∀n,m. decidable (n≤m).
418 @nat_elim2 #n /2/ #m * /3/ qed.
419
420 theorem decidable_lt: ∀n,m. decidable (n < m).
421 #n #m @decidable_le  qed.
422
423 theorem le_to_or_lt_eq: ∀n,m:nat. n ≤ m → n < m ∨ n = m.
424 #n #m #lenm (elim lenm) /3/ qed.
425
426 theorem increasing_to_le2: ∀f:nat → nat. increasing f → 
427   ∀m:nat. f 0 ≤ m → ∃i. f i ≤ m ∧ m < f (S i).
428 #f #incr #m #lem (elim lem)
429   [@(ex_intro ? ? O) /2/
430   |#n #len * #a * #len #ltnr (cases(le_to_or_lt_eq … ltnr)) #H
431     [@(ex_intro ? ? a) % /2/ 
432     |@(ex_intro ? ? (S a)) % //
433     ]
434   ]
435 qed.
436
437 lemma le_inv_plus_l: ∀x,y,z. x + y ≤ z → x ≤ z - y ∧ y ≤ z.
438 /3 width=2/ qed-.
439
440 lemma lt_inv_plus_l: ∀x,y,z. x + y < z → x < z ∧ y < z - x.
441 /3 width=2/ qed-.
442
443 lemma lt_or_ge: ∀m,n. m < n ∨ n ≤ m.
444 #m #n elim (decidable_lt m n) /2 width=1/ /3 width=1/
445 qed-.
446
447 (* More general conclusion **************************************************)
448
449 theorem lt_O_n_elim: ∀n:nat. 0 < n → 
450   ∀P:nat → Prop.(∀m:nat.P (S m)) → P n.
451 #n (elim n) // #abs @False_ind /2/ @absurd
452 qed.
453
454 theorem le_n_O_elim: ∀n:nat. n ≤ O → ∀P: nat →Prop. P O → P n.
455 #n (cases n) // #a #abs @False_ind /2/ qed. 
456
457 theorem le_n_Sm_elim : ∀n,m:nat.n ≤ S m → 
458 ∀P:Prop. (S n ≤ S m → P) → (n=S m → P) → P.
459 #n #m #Hle #P (elim Hle) /3/ qed.
460
461 theorem nat_elim1 : ∀n:nat.∀P:nat → Prop. 
462 (∀m.(∀p. p < m → P p) → P m) → P n.
463 #n #P #H 
464 cut (∀q:nat. q ≤ n → P q) /2/
465 (elim n) 
466  [#q #HleO (* applica male *) 
467     @(le_n_O_elim ? HleO)
468     @H #p #ltpO @False_ind /2/ (* 3 *)
469  |#p #Hind #q #HleS 
470     @H #a #lta @Hind @le_S_S_to_le /2/
471  ]
472 qed.
473
474 (* More negated equalities **************************************************)
475
476 theorem lt_to_not_eq : ∀n,m:nat. n < m → n ≠ m.
477 #n #m #H @not_to_not /2/ qed.
478
479 (* More equalities **********************************************************)
480
481 theorem le_n_O_to_eq : ∀n:nat. n ≤ 0 → 0=n.
482 #n (cases n) // #a  #abs @False_ind /2/ qed.
483
484 theorem le_to_le_to_eq: ∀n,m. n ≤ m → m ≤ n → n = m.
485 @nat_elim2 /4/
486 qed. 
487
488 theorem increasing_to_injective: ∀f:nat → nat.
489   increasing f → injective nat nat f.
490 #f #incr #n #m cases(decidable_le n m)
491   [#lenm cases(le_to_or_lt_eq … lenm) //
492    #lenm #eqf @False_ind @(absurd … eqf) @lt_to_not_eq 
493    @increasing_to_monotonic //
494   |#nlenm #eqf @False_ind @(absurd … eqf) @sym_not_eq 
495    @lt_to_not_eq @increasing_to_monotonic /2/
496   ]
497 qed.
498
499 theorem minus_Sn_m: ∀m,n:nat. m ≤ n → S n -m = S (n-m).
500 (* qualcosa da capire qui 
501 #n #m #lenm nelim lenm napplyS refl_eq. *)
502 @nat_elim2 
503   [//
504   |#n #abs @False_ind /2/ 
505   |#n #m #Hind #c applyS Hind /2/
506   ]
507 qed.
508
509 theorem plus_minus:
510 ∀m,n,p:nat. m ≤ n → (n-m)+p = (n+p)-m.
511 @nat_elim2 
512   [//
513   |#n #p #abs @False_ind /2/
514   |normalize/3/
515   ]
516 qed.
517
518 theorem minus_plus_m_m: ∀n,m:nat.n = (n+m)-m.
519 /2/ qed.
520
521 theorem plus_minus_m_m: ∀n,m:nat.
522   m ≤ n → n = (n-m)+m.
523 #n #m #lemn @sym_eq /2/ qed.
524
525 theorem minus_to_plus :∀n,m,p:nat.
526   m ≤ n → n-m = p → n = m+p.
527 #n #m #p #lemn #eqp (applyS plus_minus_m_m) //
528 qed.
529
530 theorem plus_to_minus :∀n,m,p:nat.n = m+p → n-m = p.
531 #n #m #p #eqp @sym_eq (applyS (minus_plus_m_m p m))
532 qed.
533
534 theorem minus_pred_pred : ∀n,m:nat. O < n → O < m → 
535 pred n - pred m = n - m.
536 #n #m #posn #posm @(lt_O_n_elim n posn) @(lt_O_n_elim m posm) //.
537 qed.
538
539 theorem plus_minus_commutative: ∀x,y,z. z ≤ y → x + (y - z) = x + y - z.
540 /2 by plus_minus/ qed.
541
542 (* More atomic conclusion ***************************************************)
543
544 (* le *)
545
546 theorem le_n_fn: ∀f:nat → nat. 
547   increasing f → ∀n:nat. n ≤ f n.
548 #f #incr #n (elim n) /2/
549 qed-.
550
551 theorem monotonic_le_minus_l: 
552 ∀p,q,n:nat. q ≤ p → q-n ≤ p-n.
553 @nat_elim2 #p #q
554   [#lePO @(le_n_O_elim ? lePO) //
555   |//
556   |#Hind #n (cases n) // #a #leSS @Hind /2/
557   ]
558 qed.
559
560 theorem le_minus_to_plus: ∀n,m,p. n-m ≤ p → n≤ p+m.
561 #n #m #p #lep @transitive_le
562   [|@le_plus_minus_m_m | @monotonic_le_plus_l // ]
563 qed.
564
565 theorem le_minus_to_plus_r: ∀a,b,c. c ≤ b → a ≤ b - c → a + c ≤ b.
566 #a #b #c #Hlecb #H >(plus_minus_m_m … Hlecb) /2/
567 qed.
568
569 theorem le_plus_to_minus: ∀n,m,p. n ≤ p+m → n-m ≤ p.
570 #n #m #p #lep /2/ qed.
571
572 theorem monotonic_le_minus_r: 
573 ∀p,q,n:nat. q ≤ p → n-p ≤ n-q.
574 #p #q #n #lepq @le_plus_to_minus
575 @(transitive_le … (le_plus_minus_m_m ? q)) /2/
576 qed.
577
578 theorem increasing_to_le: ∀f:nat → nat. 
579   increasing f → ∀m:nat.∃i.m ≤ f i.
580 #f #incr #m (elim m) /2/#n * #a #lenfa
581 @(ex_intro ?? (S a)) /2/
582 qed.
583
584 lemma le_plus_compatible: ∀x1,x2,y1,y2. x1 ≤ y1 → x2 ≤ y2 → x1 + x2 ≤ y1 + y2.
585 /3 by le_minus_to_plus, monotonic_le_plus_r, transitive_le/ qed.
586
587 (* lt *)
588
589 theorem not_eq_to_le_to_lt: ∀n,m. n≠m → n≤m → n<m.
590 #n #m #Hneq #Hle cases (le_to_or_lt_eq ?? Hle) //
591 #Heq /3/ qed-.
592
593 theorem lt_times_n_to_lt_l: 
594 ∀n,p,q:nat. p*n < q*n → p < q.
595 #n #p #q #Hlt (elim (decidable_lt p q)) //
596 #nltpq @False_ind @(absurd ? ? (lt_to_not_le ? ? Hlt))
597 applyS monotonic_le_times_r /2/
598 qed.
599
600 theorem lt_times_n_to_lt_r: 
601 ∀n,p,q:nat. n*p < n*q → p < q.
602 /2/ qed-.
603
604 theorem lt_minus_to_plus: ∀a,b,c. a - b < c → a < c + b.
605 #a #b #c #H @not_le_to_lt 
606 @(not_to_not … (lt_to_not_le …H)) /2/
607 qed.
608
609 theorem lt_minus_to_plus_r: ∀a,b,c. a < b - c → a + c < b.
610 #a #b #c #H @not_le_to_lt @(not_to_not … (le_plus_to_minus …))
611 @lt_to_not_le //
612 qed.
613
614 theorem lt_plus_to_minus: ∀n,m,p. m ≤ n → n < p+m → n-m < p.
615 #n #m #p #lenm #H normalize <minus_Sn_m // @le_plus_to_minus //
616 qed.
617
618 theorem monotonic_lt_minus_l: ∀p,q,n. n ≤ q → q < p → q - n < p - n.
619 #p #q #n #H1 #H2
620 @lt_plus_to_minus_r <plus_minus_m_m //
621 qed.
622
623 (* Still more equalities ****************************************************)
624
625 theorem eq_minus_O: ∀n,m:nat.
626   n ≤ m → n-m = O.
627 #n #m #lenm @(le_n_O_elim (n-m)) /2/
628 qed.
629
630 theorem distributive_times_minus: distributive ? times minus.
631 #a #b #c
632 (cases (decidable_lt b c)) #Hbc
633  [> eq_minus_O /2/ >eq_minus_O // 
634   @monotonic_le_times_r /2/
635  |@sym_eq (applyS plus_to_minus) <distributive_times_plus 
636   @eq_f (applyS plus_minus_m_m) /2/
637 qed.
638
639 theorem minus_plus: ∀n,m,p. n-m-p = n -(m+p).
640 #n #m #p 
641 cases (decidable_le (m+p) n) #Hlt
642   [@plus_to_minus @plus_to_minus <associative_plus
643    @minus_to_plus //
644   |cut (n ≤ m+p) [@(transitive_le … (le_n_Sn …)) @not_le_to_lt //]
645    #H >eq_minus_O /2/ (* >eq_minus_O // *) 
646   ]
647 qed.
648
649 theorem minus_minus: ∀n,m,p:nat. p ≤ m → m ≤ n →
650   p+(n-m) = n-(m-p).
651 #n #m #p #lepm #lemn
652 @sym_eq @plus_to_minus <associative_plus <plus_minus_m_m //
653 <commutative_plus <plus_minus_m_m //
654 qed.
655
656 lemma minus_minus_comm: ∀a,b,c. a - b - c = a - c - b.
657 /3 by monotonic_le_minus_l, le_to_le_to_eq/ qed.
658
659 lemma minus_le_minus_minus_comm: ∀b,c,a. c ≤ b → a - (b - c) = a + c - b.
660 #b #c #a #H >(plus_minus_m_m b c) in ⊢ (? ? ?%); //
661 qed.
662
663 (* Stilll more atomic conclusion ********************************************)
664
665 (* le *)
666
667 lemma le_fwd_plus_plus_ge: ∀m1,m2. m2 ≤ m1 → ∀n1,n2. m1 + n1 ≤ m2 + n2 → n1 ≤ n2.
668 #m1 #m2 #H #n1 #n2 >commutative_plus
669 #H elim (le_inv_plus_l … H) -H >commutative_plus <minus_le_minus_minus_comm //
670 #H #_ @(transitive_le … H) /2 width=1/
671 qed-. 
672
673 (*********************** boolean arithmetics ********************) 
674
675 include "basics/bool.ma".
676
677 let rec eqb n m ≝ 
678 match n with 
679   [ O ⇒ match m with [ O ⇒ true | S q ⇒ false] 
680   | S p ⇒ match m with [ O ⇒ false | S q ⇒ eqb p q]
681   ].
682
683 theorem eqb_elim : ∀ n,m:nat.∀ P:bool → Prop.
684 (n=m → (P true)) → (n ≠ m → (P false)) → (P (eqb n m)). 
685 @nat_elim2 
686   [#n (cases n) normalize /3/ 
687   |normalize /3/
688   |normalize /4/ 
689   ] 
690 qed.
691
692 theorem eqb_n_n: ∀n. eqb n n = true.
693 #n (elim n) normalize // qed. 
694
695 theorem eqb_true_to_eq: ∀n,m:nat. eqb n m = true → n = m.
696 #n #m @(eqb_elim n m) // #_ #abs @False_ind /2/ qed.
697
698 theorem eqb_false_to_not_eq: ∀n,m:nat. eqb n m = false → n ≠ m.
699 #n #m @(eqb_elim n m) /2/ qed.
700
701 theorem eq_to_eqb_true: ∀n,m:nat.n = m → eqb n m = true.
702 // qed.
703
704 theorem not_eq_to_eqb_false: ∀n,m:nat.
705   n ≠  m → eqb n m = false.
706 #n #m #noteq @eqb_elim// #Heq @False_ind /2/ qed.
707
708 let rec leb n m ≝ 
709 match n with 
710     [ O ⇒ true
711     | (S p) ⇒
712         match m with 
713         [ O ⇒ false
714               | (S q) ⇒ leb p q]].
715
716 theorem leb_elim: ∀n,m:nat. ∀P:bool → Prop. 
717 (n ≤ m → P true) → (n ≰ m → P false) → P (leb n m).
718 @nat_elim2 normalize
719   [/2/
720   |/3/
721   |#n #m #Hind #P #Pt #Pf @Hind
722     [#lenm @Pt @le_S_S // |#nlenm @Pf /2/ ]
723   ]
724 qed.
725
726 theorem leb_true_to_le:∀n,m.leb n m = true → n ≤ m.
727 #n #m @leb_elim // #_ #abs @False_ind /2/ qed.
728
729 theorem leb_false_to_not_le:∀n,m.
730   leb n m = false → n ≰ m.
731 #n #m @leb_elim // #_ #abs @False_ind /2/ qed.
732
733 theorem le_to_leb_true: ∀n,m. n ≤ m → leb n m = true.
734 #n #m @leb_elim // #H #H1 @False_ind /2/ qed.
735
736 theorem not_le_to_leb_false: ∀n,m. n ≰ m → leb n m = false.
737 #n #m @leb_elim // #H #H1 @False_ind /2/ qed.
738
739 theorem lt_to_leb_false: ∀n,m. m < n → leb n m = false.
740 /3/ qed.
741
742 (* min e max *)
743 definition min: nat →nat →nat ≝
744 λn.λm. if leb n m then n else m.
745
746 definition max: nat →nat →nat ≝
747 λn.λm. if leb n m then m else n.
748
749 lemma commutative_min: commutative ? min.
750 #n #m normalize @leb_elim 
751   [@leb_elim normalize /2/
752   |#notle >(le_to_leb_true …) // @(transitive_le ? (S m)) /2/
753   ] qed.
754
755 lemma le_minr: ∀i,n,m. i ≤ min n m → i ≤ m.
756 #i #n #m normalize @leb_elim normalize /2/ qed. 
757
758 lemma le_minl: ∀i,n,m. i ≤ min n m → i ≤ n.
759 /2/ qed-.
760
761 lemma to_min: ∀i,n,m. i ≤ n → i ≤ m → i ≤ min n m.
762 #i #n #m #lein #leim normalize (cases (leb n m)) 
763 normalize // qed.
764
765 lemma commutative_max: commutative ? max.
766 #n #m normalize @leb_elim 
767   [@leb_elim normalize /2/
768   |#notle >(le_to_leb_true …) // @(transitive_le ? (S m)) /2/
769   ] qed.
770
771 lemma le_maxl: ∀i,n,m. max n m ≤ i → n ≤ i.
772 #i #n #m normalize @leb_elim normalize /2/ qed. 
773
774 lemma le_maxr: ∀i,n,m. max n m ≤ i → m ≤ i.
775 /2/ qed-.
776
777 lemma to_max: ∀i,n,m. n ≤ i → m ≤ i → max n m ≤ i.
778 #i #n #m #leni #lemi normalize (cases (leb n m)) 
779 normalize // qed.
780