]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/arithmetics/nat.ma
Some integrations from CerCo. In particular:
[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 (* Negated equalities *******************************************************)
188
189 theorem not_eq_O_S : ∀n:nat. 0 ≠ S n.
190 #n @nmk #eqOS (change with (not_zero O)) >eqOS // qed.
191
192 theorem not_eq_n_Sn: ∀n:nat. n ≠ S n.
193 #n (elim n) /2/ qed.
194
195 (* Atomic conclusion *******************************************************)
196
197 (* not_zero *)
198
199 theorem lt_to_not_zero : ∀n,m:nat. n < m → not_zero m.
200 #n #m #Hlt (elim Hlt) // qed.
201
202 (* le *)
203
204 theorem le_S_S: ∀n,m:nat. n ≤ m → S n ≤ S m.
205 #n #m #lenm (elim lenm) /2/ qed.
206
207 theorem le_O_n : ∀n:nat. 0 ≤ n.
208 #n (elim n) /2/ qed.
209
210 theorem le_n_Sn : ∀n:nat. n ≤ S n.
211 /2/ qed.
212
213 theorem transitive_le : transitive nat le.
214 #a #b #c #leab #lebc (elim lebc) /2/
215 qed.
216
217 theorem le_pred_n : ∀n:nat. pred n ≤ n.
218 #n (elim n) // qed.
219
220 theorem monotonic_pred: monotonic ? le pred.
221 #n #m #lenm (elim lenm) /2/ qed.
222
223 theorem le_S_S_to_le: ∀n,m:nat. S n ≤ S m → n ≤ m.
224 (* demo *)
225 /2/ qed-.
226
227 theorem monotonic_le_plus_r: 
228 ∀n:nat.monotonic nat le (λm.n + m).
229 #n #a #b (elim n) normalize //
230 #m #H #leab @le_S_S /2/ qed.
231
232 theorem monotonic_le_plus_l: 
233 ∀m:nat.monotonic nat le (λn.n + m).
234 /2/ qed.
235
236 theorem le_plus: ∀n1,n2,m1,m2:nat. n1 ≤ n2  → m1 ≤ m2 
237 → n1 + m1 ≤ n2 + m2.
238 #n1 #n2 #m1 #m2 #len #lem @(transitive_le ? (n1+m2))
239 /2/ qed-.
240
241 theorem le_plus_n :∀n,m:nat. m ≤ n + m.
242 /2/ qed. 
243
244 lemma le_plus_a: ∀a,n,m. n ≤ m → n ≤ a + m.
245 /2/ qed.
246
247 lemma le_plus_b: ∀b,n,m. n + b ≤ m → n ≤ m.
248 /2/ qed.
249
250 theorem le_plus_n_r :∀n,m:nat. m ≤ m + n.
251 /2/ qed.
252
253 theorem eq_plus_to_le: ∀n,m,p:nat.n=m+p → m ≤ n.
254 // qed-.
255
256 theorem le_plus_to_le: ∀a,n,m. a + n ≤ a + m → n ≤ m.
257 #a (elim a) normalize /3/ qed. 
258
259 theorem le_plus_to_le_r: ∀a,n,m. n + a ≤ m +a → n ≤ m.
260 /2/ qed-. 
261
262 theorem monotonic_le_times_r: 
263 ∀n:nat.monotonic nat le (λm. n * m).
264 #n #x #y #lexy (elim n) normalize//(* lento /2/*)
265 #a #lea @le_plus //
266 qed.
267
268 theorem le_times: ∀n1,n2,m1,m2:nat. 
269 n1 ≤ n2  → m1 ≤ m2 → n1*m1 ≤ n2*m2.
270 #n1 #n2 #m1 #m2 #len #lem @(transitive_le ? (n1*m2)) /2/
271 qed.
272
273 (* interessante *)
274 theorem lt_times_n: ∀n,m:nat. O < n → m ≤ n*m.
275 #n #m #H /2/ qed.
276
277 theorem le_times_to_le: 
278 ∀a,n,m. O < a → a * n ≤ a * m → n ≤ m.
279 #a @nat_elim2 normalize
280   [//
281   |#n #H1 #H2 
282      @(transitive_le ? (a*S n)) /2/
283   |#n #m #H #lta #le
284      @le_S_S @H /2/
285   ]
286 qed-.
287
288 theorem le_plus_minus_m_m: ∀n,m:nat. n ≤ (n-m)+m.
289 #n (elim n) // #a #Hind #m (cases m) // normalize #n/2/  
290 qed.
291
292 theorem le_plus_to_minus_r: ∀a,b,c. a + b ≤ c → a ≤ c -b.
293 #a #b #c #H @(le_plus_to_le_r … b) /2/
294 qed.
295
296 (* lt *)
297
298 theorem transitive_lt: transitive nat lt.
299 #a #b #c #ltab #ltbc (elim ltbc) /2/
300 qed.
301
302 theorem lt_to_le_to_lt: ∀n,m,p:nat. n < m → m ≤ p → n < p.
303 #n #m #p #H #H1 (elim H1) /2/ qed.
304
305 theorem le_to_lt_to_lt: ∀n,m,p:nat. n ≤ m → m < p → n < p.
306 #n #m #p #H (elim H) /3/ qed.
307
308 theorem lt_S_to_lt: ∀n,m. S n < m → n < m.
309 /2/ qed.
310
311 theorem ltn_to_ltO: ∀n,m:nat. n < m → 0 < m.
312 /2/ qed.
313
314 theorem lt_O_S : ∀n:nat. O < S n.
315 /2/ qed.
316
317 theorem monotonic_lt_plus_r: 
318 ∀n:nat.monotonic nat lt (λm.n+m).
319 /2/ qed.
320
321 theorem monotonic_lt_plus_l: 
322 ∀n:nat.monotonic nat lt (λm.m+n).
323 /2/ qed.
324
325 theorem lt_plus: ∀n,m,p,q:nat. n < m → p < q → n + p < m + q.
326 #n #m #p #q #ltnm #ltpq
327 @(transitive_lt ? (n+q))/2/ qed.
328
329 theorem lt_plus_to_lt_l :∀n,p,q:nat. p+n < q+n → p<q.
330 /2/ qed.
331
332 theorem lt_plus_to_lt_r :∀n,p,q:nat. n+p < n+q → p<q.
333 /2/ qed-.
334
335 theorem increasing_to_monotonic: ∀f:nat → nat.
336   increasing f → monotonic nat lt f.
337 #f #incr #n #m #ltnm (elim ltnm) /2/
338 qed.
339
340 theorem monotonic_lt_times_r: 
341   ∀c:nat. O < c → monotonic nat lt (λt.(c*t)).
342 #c #posc #n #m #ltnm
343 (elim ltnm) normalize
344   [/2/ 
345   |#a #_ #lt1 @(transitive_le … lt1) //
346   ]
347 qed.
348
349 theorem monotonic_lt_times_l: 
350   ∀c:nat. 0 < c → monotonic nat lt (λt.(t*c)).
351 /2/
352 qed.
353
354 theorem lt_to_le_to_lt_times: 
355 ∀n,m,p,q:nat. n < m → p ≤ q → O < q → n*p < m*q.
356 #n #m #p #q #ltnm #lepq #posq
357 @(le_to_lt_to_lt ? (n*q))
358   [@monotonic_le_times_r //
359   |@monotonic_lt_times_l //
360   ]
361 qed.
362
363 theorem lt_times:∀n,m,p,q:nat. n<m → p<q → n*p < m*q.
364 #n #m #p #q #ltnm #ltpq @lt_to_le_to_lt_times/2/
365 qed.
366
367 theorem lt_plus_to_minus_r: ∀a,b,c. a + b < c → a < c - b.
368 #a #b #c #H @le_plus_to_minus_r //
369 qed.
370
371 (* not le, lt *)
372
373 theorem not_le_Sn_O: ∀ n:nat. S n ≰ 0.
374 #n @nmk #Hlen0 @(lt_to_not_zero ?? Hlen0) qed.
375
376 theorem not_le_to_not_le_S_S: ∀ n,m:nat. n ≰ m → S n ≰ S m.
377 /3/ qed.
378
379 theorem not_le_S_S_to_not_le: ∀ n,m:nat. S n ≰ S m → n ≰ m.
380 /3/ qed.
381
382 theorem not_le_Sn_n: ∀n:nat. S n ≰ n.
383 #n (elim n) /2/ qed.
384
385 theorem lt_to_not_le: ∀n,m. n < m → m ≰ n.
386 #n #m #Hltnm (elim Hltnm) /3/ qed.
387
388 theorem not_le_to_lt: ∀n,m. n ≰ m → m < n.
389 @nat_elim2 #n
390  [#abs @False_ind /2/
391  |/2/
392  |#m #Hind #HnotleSS @le_S_S /3/
393  ]
394 qed.
395
396 (* not lt, le *)
397
398 theorem not_lt_to_le: ∀n,m:nat. n ≮ m → m ≤ n.
399 /4/ qed.
400
401 theorem le_to_not_lt: ∀n,m:nat. n ≤ m → m ≮ n.
402 #n #m #H @lt_to_not_le /2/ (* /3/ *) qed.
403
404 (* Compound conclusion ******************************************************)
405
406 theorem decidable_eq_nat : ∀n,m:nat.decidable (n=m).
407 @nat_elim2 #n [ (cases n) /2/ | /3/ | #m #Hind (cases Hind) /3/]
408 qed. 
409
410 theorem decidable_le: ∀n,m. decidable (n≤m).
411 @nat_elim2 #n /2/ #m * /3/ qed.
412
413 theorem decidable_lt: ∀n,m. decidable (n < m).
414 #n #m @decidable_le  qed.
415
416 theorem le_to_or_lt_eq: ∀n,m:nat. n ≤ m → n < m ∨ n = m.
417 #n #m #lenm (elim lenm) /3/ qed.
418
419 theorem increasing_to_le2: ∀f:nat → nat. increasing f → 
420   ∀m:nat. f 0 ≤ m → ∃i. f i ≤ m ∧ m < f (S i).
421 #f #incr #m #lem (elim lem)
422   [@(ex_intro ? ? O) /2/
423   |#n #len * #a * #len #ltnr (cases(le_to_or_lt_eq … ltnr)) #H
424     [@(ex_intro ? ? a) % /2/ 
425     |@(ex_intro ? ? (S a)) % //
426     ]
427   ]
428 qed.
429
430 (* More general conclusion **************************************************)
431
432 theorem lt_O_n_elim: ∀n:nat. 0 < n → 
433   ∀P:nat → Prop.(∀m:nat.P (S m)) → P n.
434 #n (elim n) // #abs @False_ind /2/ @absurd
435 qed.
436
437 theorem le_n_O_elim: ∀n:nat. n ≤ O → ∀P: nat →Prop. P O → P n.
438 #n (cases n) // #a #abs @False_ind /2/ qed. 
439
440 theorem le_n_Sm_elim : ∀n,m:nat.n ≤ S m → 
441 ∀P:Prop. (S n ≤ S m → P) → (n=S m → P) → P.
442 #n #m #Hle #P (elim Hle) /3/ qed.
443
444 theorem nat_elim1 : ∀n:nat.∀P:nat → Prop. 
445 (∀m.(∀p. p < m → P p) → P m) → P n.
446 #n #P #H 
447 cut (∀q:nat. q ≤ n → P q) /2/
448 (elim n) 
449  [#q #HleO (* applica male *) 
450     @(le_n_O_elim ? HleO)
451     @H #p #ltpO @False_ind /2/ (* 3 *)
452  |#p #Hind #q #HleS 
453     @H #a #lta @Hind @le_S_S_to_le /2/
454  ]
455 qed.
456
457 (* More negated equalities **************************************************)
458
459 theorem lt_to_not_eq : ∀n,m:nat. n < m → n ≠ m.
460 #n #m #H @not_to_not /2/ qed.
461
462 (* More equalities **********************************************************)
463
464 theorem le_n_O_to_eq : ∀n:nat. n ≤ 0 → 0=n.
465 #n (cases n) // #a  #abs @False_ind /2/ qed.
466
467 theorem le_to_le_to_eq: ∀n,m. n ≤ m → m ≤ n → n = m.
468 @nat_elim2 /4/
469 qed. 
470
471 theorem increasing_to_injective: ∀f:nat → nat.
472   increasing f → injective nat nat f.
473 #f #incr #n #m cases(decidable_le n m)
474   [#lenm cases(le_to_or_lt_eq … lenm) //
475    #lenm #eqf @False_ind @(absurd … eqf) @lt_to_not_eq 
476    @increasing_to_monotonic //
477   |#nlenm #eqf @False_ind @(absurd … eqf) @sym_not_eq 
478    @lt_to_not_eq @increasing_to_monotonic /2/
479   ]
480 qed.
481
482 theorem minus_Sn_m: ∀m,n:nat. m ≤ n → S n -m = S (n-m).
483 (* qualcosa da capire qui 
484 #n #m #lenm nelim lenm napplyS refl_eq. *)
485 @nat_elim2 
486   [//
487   |#n #abs @False_ind /2/ 
488   |#n #m #Hind #c applyS Hind /2/
489   ]
490 qed.
491
492 theorem plus_minus:
493 ∀m,n,p:nat. m ≤ n → (n-m)+p = (n+p)-m.
494 @nat_elim2 
495   [//
496   |#n #p #abs @False_ind /2/
497   |normalize/3/
498   ]
499 qed.
500
501 theorem minus_plus_m_m: ∀n,m:nat.n = (n+m)-m.
502 /2/ qed.
503
504 theorem plus_minus_m_m: ∀n,m:nat.
505   m ≤ n → n = (n-m)+m.
506 #n #m #lemn @sym_eq /2/ qed.
507
508 theorem minus_to_plus :∀n,m,p:nat.
509   m ≤ n → n-m = p → n = m+p.
510 #n #m #p #lemn #eqp (applyS plus_minus_m_m) //
511 qed.
512
513 theorem plus_to_minus :∀n,m,p:nat.n = m+p → n-m = p.
514 #n #m #p #eqp @sym_eq (applyS (minus_plus_m_m p m))
515 qed.
516
517 theorem minus_pred_pred : ∀n,m:nat. O < n → O < m → 
518 pred n - pred m = n - m.
519 #n #m #posn #posm @(lt_O_n_elim n posn) @(lt_O_n_elim m posm) //.
520 qed.
521
522 (* More atomic conclusion ***************************************************)
523
524 (* le *)
525
526 theorem le_n_fn: ∀f:nat → nat. 
527   increasing f → ∀n:nat. n ≤ f n.
528 #f #incr #n (elim n) /2/
529 qed-.
530
531 theorem monotonic_le_minus_l: 
532 ∀p,q,n:nat. q ≤ p → q-n ≤ p-n.
533 @nat_elim2 #p #q
534   [#lePO @(le_n_O_elim ? lePO) //
535   |//
536   |#Hind #n (cases n) // #a #leSS @Hind /2/
537   ]
538 qed.
539
540 theorem le_minus_to_plus: ∀n,m,p. n-m ≤ p → n≤ p+m.
541 #n #m #p #lep @transitive_le
542   [|@le_plus_minus_m_m | @monotonic_le_plus_l // ]
543 qed.
544
545 theorem le_minus_to_plus_r: ∀a,b,c. c ≤ b → a ≤ b - c → a + c ≤ b.
546 #a #b #c #Hlecb #H >(plus_minus_m_m … Hlecb) /2/
547 qed.
548
549 theorem le_plus_to_minus: ∀n,m,p. n ≤ p+m → n-m ≤ p.
550 #n #m #p #lep /2/ qed.
551
552 theorem monotonic_le_minus_r: 
553 ∀p,q,n:nat. q ≤ p → n-p ≤ n-q.
554 #p #q #n #lepq @le_plus_to_minus
555 @(transitive_le … (le_plus_minus_m_m ? q)) /2/
556 qed.
557
558 theorem increasing_to_le: ∀f:nat → nat. 
559   increasing f → ∀m:nat.∃i.m ≤ f i.
560 #f #incr #m (elim m) /2/#n * #a #lenfa
561 @(ex_intro ?? (S a)) /2/
562 qed.
563
564 (* lt *)
565
566 theorem not_eq_to_le_to_lt: ∀n,m. n≠m → n≤m → n<m.
567 #n #m #Hneq #Hle cases (le_to_or_lt_eq ?? Hle) //
568 #Heq /3/ qed-.
569
570 theorem lt_times_n_to_lt_l: 
571 ∀n,p,q:nat. p*n < q*n → p < q.
572 #n #p #q #Hlt (elim (decidable_lt p q)) //
573 #nltpq @False_ind @(absurd ? ? (lt_to_not_le ? ? Hlt))
574 applyS monotonic_le_times_r /2/
575 qed.
576
577 theorem lt_times_n_to_lt_r: 
578 ∀n,p,q:nat. n*p < n*q → p < q.
579 /2/ qed-.
580
581 theorem lt_minus_to_plus: ∀a,b,c. a - b < c → a < c + b.
582 #a #b #c #H @not_le_to_lt 
583 @(not_to_not … (lt_to_not_le …H)) /2/
584 qed.
585
586 theorem lt_minus_to_plus_r: ∀a,b,c. a < b - c → a + c < b.
587 #a #b #c #H @not_le_to_lt @(not_to_not … (le_plus_to_minus …))
588 @lt_to_not_le //
589 qed.
590
591 theorem lt_plus_to_minus: ∀n,m,p. m ≤ n → n < p+m → n-m < p.
592 #n #m #p #lenm #H normalize <minus_Sn_m // @le_plus_to_minus //
593 qed.
594
595 theorem monotonic_lt_minus_l: ∀p,q,n. n ≤ q → q < p → q - n < p - n.
596 #p #q #n #H1 #H2
597 @lt_plus_to_minus_r <plus_minus_m_m //
598 qed.
599
600 (* Still more equalities ****************************************************)
601
602 theorem eq_minus_O: ∀n,m:nat.
603   n ≤ m → n-m = O.
604 #n #m #lenm @(le_n_O_elim (n-m)) /2/
605 qed.
606
607 theorem distributive_times_minus: distributive ? times minus.
608 #a #b #c
609 (cases (decidable_lt b c)) #Hbc
610  [> eq_minus_O /2/ >eq_minus_O // 
611   @monotonic_le_times_r /2/
612  |@sym_eq (applyS plus_to_minus) <distributive_times_plus 
613   @eq_f (applyS plus_minus_m_m) /2/
614 qed.
615
616 theorem minus_plus: ∀n,m,p. n-m-p = n -(m+p).
617 #n #m #p 
618 cases (decidable_le (m+p) n) #Hlt
619   [@plus_to_minus @plus_to_minus <associative_plus
620    @minus_to_plus //
621   |cut (n ≤ m+p) [@(transitive_le … (le_n_Sn …)) @not_le_to_lt //]
622    #H >eq_minus_O /2/ (* >eq_minus_O // *) 
623   ]
624 qed.
625
626 theorem minus_minus: ∀n,m,p:nat. p ≤ m → m ≤ n →
627   p+(n-m) = n-(m-p).
628 #n #m #p #lepm #lemn
629 @sym_eq @plus_to_minus <associative_plus <plus_minus_m_m //
630 <commutative_plus <plus_minus_m_m //
631 qed.
632
633 (*********************** boolean arithmetics ********************) 
634 include "basics/bool.ma".
635
636 let rec eqb n m ≝ 
637 match n with 
638   [ O ⇒ match m with [ O ⇒ true | S q ⇒ false] 
639   | S p ⇒ match m with [ O ⇒ false | S q ⇒ eqb p q]
640   ].
641
642 theorem eqb_elim : ∀ n,m:nat.∀ P:bool → Prop.
643 (n=m → (P true)) → (n ≠ m → (P false)) → (P (eqb n m)). 
644 @nat_elim2 
645   [#n (cases n) normalize /3/ 
646   |normalize /3/
647   |normalize /4/ 
648   ] 
649 qed.
650
651 theorem eqb_n_n: ∀n. eqb n n = true.
652 #n (elim n) normalize // qed. 
653
654 theorem eqb_true_to_eq: ∀n,m:nat. eqb n m = true → n = m.
655 #n #m @(eqb_elim n m) // #_ #abs @False_ind /2/ qed.
656
657 theorem eqb_false_to_not_eq: ∀n,m:nat. eqb n m = false → n ≠ m.
658 #n #m @(eqb_elim n m) /2/ qed.
659
660 theorem eq_to_eqb_true: ∀n,m:nat.n = m → eqb n m = true.
661 // qed.
662
663 theorem not_eq_to_eqb_false: ∀n,m:nat.
664   n ≠  m → eqb n m = false.
665 #n #m #noteq @eqb_elim// #Heq @False_ind /2/ qed.
666
667 let rec leb n m ≝ 
668 match n with 
669     [ O ⇒ true
670     | (S p) ⇒
671         match m with 
672         [ O ⇒ false
673               | (S q) ⇒ leb p q]].
674
675 theorem leb_elim: ∀n,m:nat. ∀P:bool → Prop. 
676 (n ≤ m → P true) → (n ≰ m → P false) → P (leb n m).
677 @nat_elim2 normalize
678   [/2/
679   |/3/
680   |#n #m #Hind #P #Pt #Pf @Hind
681     [#lenm @Pt @le_S_S // |#nlenm @Pf /2/ ]
682   ]
683 qed.
684
685 theorem leb_true_to_le:∀n,m.leb n m = true → n ≤ m.
686 #n #m @leb_elim // #_ #abs @False_ind /2/ qed.
687
688 theorem leb_false_to_not_le:∀n,m.
689   leb n m = false → n ≰ m.
690 #n #m @leb_elim // #_ #abs @False_ind /2/ qed.
691
692 theorem le_to_leb_true: ∀n,m. n ≤ m → leb n m = true.
693 #n #m @leb_elim // #H #H1 @False_ind /2/ qed.
694
695 theorem not_le_to_leb_false: ∀n,m. n ≰ m → leb n m = false.
696 #n #m @leb_elim // #H #H1 @False_ind /2/ qed.
697
698 theorem lt_to_leb_false: ∀n,m. m < n → leb n m = false.
699 /3/ qed.
700
701 (* min e max *)
702 definition min: nat →nat →nat ≝
703 λn.λm. if leb n m then n else m.
704
705 definition max: nat →nat →nat ≝
706 λn.λm. if leb n m then m else n.
707
708 lemma commutative_min: commutative ? min.
709 #n #m normalize @leb_elim 
710   [@leb_elim normalize /2/
711   |#notle >(le_to_leb_true …) // @(transitive_le ? (S m)) /2/
712   ] qed.
713
714 lemma le_minr: ∀i,n,m. i ≤ min n m → i ≤ m.
715 #i #n #m normalize @leb_elim normalize /2/ qed. 
716
717 lemma le_minl: ∀i,n,m. i ≤ min n m → i ≤ n.
718 /2/ qed-.
719
720 lemma to_min: ∀i,n,m. i ≤ n → i ≤ m → i ≤ min n m.
721 #i #n #m #lein #leim normalize (cases (leb n m)) 
722 normalize // qed.
723
724 lemma commutative_max: commutative ? max.
725 #n #m normalize @leb_elim 
726   [@leb_elim normalize /2/
727   |#notle >(le_to_leb_true …) // @(transitive_le ? (S m)) /2/
728   ] qed.
729
730 lemma le_maxl: ∀i,n,m. max n m ≤ i → n ≤ i.
731 #i #n #m normalize @leb_elim normalize /2/ qed. 
732
733 lemma le_maxr: ∀i,n,m. max n m ≤ i → m ≤ i.
734 /2/ qed-.
735
736 lemma to_max: ∀i,n,m. n ≤ i → m ≤ i → max n m ≤ i.
737 #i #n #m #leni #lemi normalize (cases (leb n m)) 
738 normalize // qed.
739