1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| A.Asperti, C.Sacerdoti Coen, *)
8 (* ||A|| E.Tassi, S.Zacchiroli *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU Lesser General Public License Version 2.1 *)
13 (**************************************************************************)
15 include "arithmetics/nat.ma".
22 interpretation "Integers" 'Z = Z.
24 ndefinition Z_of_nat ≝
29 ncoercion Z_of_nat : ∀n:nat.Z ≝ Z_of_nat on _n:nat to Z.
31 ndefinition neg_Z_of_nat \def
36 nlemma pos_n_eq_S_n : ∀n : nat.pos n = S n.
52 ntheorem OZ_test_to_Prop : ∀z:Z.
58 ##|##*:#z1;napply nmk;#H;ndestruct]
62 ntheorem injective_pos: injective nat Z pos.
63 #n;#m;#H;ndestruct;//;
66 (* variant inj_pos : \forall n,m:nat. pos n = pos m \to n = m
67 \def injective_pos. *)
69 ntheorem injective_neg: injective nat Z neg.
70 #n;#m;#H;ndestruct;//;
73 (* variant inj_neg : \forall n,m:nat. neg n = neg m \to n = m
74 \def injective_neg. *)
76 ntheorem not_eq_OZ_pos: ∀n:nat. OZ ≠ pos n.
77 #n;napply nmk;#H;ndestruct;
80 ntheorem not_eq_OZ_neg :∀n:nat. OZ ≠ neg n.
81 #n;napply nmk;#H;ndestruct;
84 ntheorem not_eq_pos_neg : ∀n,m:nat. pos n ≠ neg m.
85 #n;#m;napply nmk;#H;ndestruct;
88 ntheorem decidable_eq_Z : ∀x,y:Z. decidable (x=y).
92 ##|##*:#n;@2;napply nmk;#H;ndestruct]
94 ##[@2;napply nmk;#H;ndestruct;
95 ##|#m;ncases (decidable_eq_nat n m);#H;
97 ##|@2;napply nmk;#H2;nelim H;
98 (* bug if you don't introduce H3 *)#H3;ndestruct;
100 ##|#m;@2;napply nmk;#H;ndestruct]
102 ##[@2;napply nmk;#H;ndestruct;
103 ##|#m;@2;napply nmk;#H;ndestruct
104 ##|#m;ncases (decidable_eq_nat n m);#H;
106 ##|@2;napply nmk;#H2;nelim H;#H3;ndestruct;
126 | neg n ⇒ neg (S n)].
128 ntheorem Zpred_Zsucc: ∀z:Z. Zpred (Zsucc z) = z.
134 ntheorem Zsucc_Zpred: ∀z:Z. Zsucc (Zpred z) = z.
136 ##[##2:#n;ncases n;//
140 ndefinition Zle : Z → Z → Prop ≝
159 interpretation "integer 'less or equal to'" 'leq x y = (Zle x y).
160 interpretation "integer 'neither less nor equal to'" 'nleq x y = (Not (Zle x y)).
162 ndefinition Zlt : Z → Z → Prop ≝
181 interpretation "integer 'less than'" 'lt x y = (Zlt x y).
182 interpretation "integer 'not less than'" 'nless x y = (Not (Zlt x y)).
184 ntheorem irreflexive_Zlt: irreflexive Z Zlt.
187 ##|##*:#n;napply (not_le_Sn_n n) (* XXX: auto?? *)]
191 ntheorem transitive_Zle : transitive Z Zle.
195 ##|##*:#n;ncases z;//]
198 ##|(*##*:#m;#Hl;ncases z;//;*)
199 #m;#Hl;ncases z;//;#p;#Hr;
200 napply (transitive_le n m p);//; (* XXX: auto??? *)
209 ##|#m;#Hl;ncases z;//;#p;#Hr;
210 napply (transitive_le p m n);//; (* XXX: auto??? *) ##]
213 (* variant trans_Zle: transitive Z Zle
214 \def transitive_Zle.*)
216 ntheorem transitive_Zlt: transitive Z Zlt.
223 ##|#m;#_;#Hr;ncases Hr]
229 ##|#p;napply transitive_lt (* XXX: auto??? *)
235 ##|#m;#_;#Hr;ncases Hr]
238 ##|#p;#_;#Hr;ncases Hr]
241 ##|#p;#Hl;#Hr;napply (transitive_lt … Hr Hl)]
246 (* variant trans_Zlt: transitive Z Zlt
248 theorem irrefl_Zlt: irreflexive Z Zlt
249 \def irreflexive_Zlt.*)
251 ntheorem Zlt_neg_neg_to_lt:
252 ∀n,m:nat. neg n < neg m → m < n.
256 ntheorem lt_to_Zlt_neg_neg: ∀n,m:nat.m < n → neg n < neg m.
260 ntheorem Zlt_pos_pos_to_lt:
261 ∀n,m:nat. pos n < pos m → n < m.
265 ntheorem lt_to_Zlt_pos_pos: ∀n,m:nat.n < m → pos n < pos m.
269 ntheorem Zlt_to_Zle: ∀x,y:Z. x < y → Zsucc x ≤ y.
281 ##|#p;ncases n;nnormalize;
282 ##[#H;nelim (not_le_Sn_O p);#H2;napply H2;//; (* XXX: auto? *)
283 ##|#m;napply le_S_S_to_le (* XXX: auto? *)]
290 (* boolean equality *)
291 ndefinition eqZb : Z → Z → bool ≝
310 ntheorem eqZb_to_Prop:
318 ##|napply not_eq_OZ_pos (* XXX: auto? *)
319 ##|napply not_eq_OZ_neg (* XXX: auto? *)]
321 ##[napply nmk;#H;nelim (not_eq_OZ_pos n);#H2;/2/;
322 ##|#m;napply eqb_elim;
324 ##|#H;napply nmk;#H2;nelim H;#H3;ndestruct;/2/]
325 ##|#m;napply not_eq_pos_neg]
327 ##[napply nmk;#H;nelim (not_eq_OZ_neg n);#H;/2/;
328 ##|#m;napply nmk;#H;ndestruct
329 ##|#m;napply eqb_elim;
331 ##|#H;napply nmk;#H2;ndestruct;nelim H;/2/]
336 ntheorem eqZb_elim: ∀x,y:Z.∀P:bool → Prop.
337 (x=y → P true) → (x ≠ y → P false) → P (eqZb x y).
340 (match (eqZb x y) with
342 | false ⇒ x ≠ y] → P (eqZb x y))
343 ##[ncases (eqZb x y);nnormalize; (* XXX: auto?? *)
346 ##|#Hcut;napply Hcut;napply eqZb_to_Prop]
349 ndefinition Z_compare : Z → Z → compare ≝
360 | pos m ⇒ nat_compare n m
366 | neg m ⇒ nat_compare m n ]].
368 ntheorem Z_compare_to_Prop :
369 ∀x,y:Z. match (Z_compare x y) with
378 ncut (match (nat_compare m n) with
382 match (nat_compare m n) with
386 ##[ncases (nat_compare m n);//
387 ##|#Hcut;napply Hcut;napply nat_compare_to_Prop]
393 ncut (match (nat_compare n m) with
397 match (nat_compare n m) with
401 ##[ncases (nat_compare n m);//
402 ##|#Hcut;napply Hcut;napply nat_compare_to_Prop]
407 ndefinition Zplus : Z → Z → Z ≝
413 | pos n ⇒ (pos (pred ((S m)+(S n))))
415 match nat_compare m n with
416 [ LT ⇒ (neg (pred (n-m)))
418 | GT ⇒ (pos (pred (m-n)))] ]
423 match nat_compare m n with
424 [ LT ⇒ (pos (pred (n-m)))
426 | GT ⇒ (neg (pred (m-n)))]
427 | neg n ⇒ (neg (pred ((S m)+(S n))))] ].
429 interpretation "integer plus" 'plus x y = (Zplus x y).
431 ntheorem eq_plus_Zplus: ∀n,m:nat. Z_of_nat (n+m) = Z_of_nat n + Z_of_nat m.
439 ntheorem Zplus_z_OZ: ∀z:Z. z+OZ = z.
443 (* theorem symmetric_Zplus: symmetric Z Zplus. *)
445 ntheorem sym_Zplus : ∀x,y:Z. x+y = y+x.
447 ##[nrewrite > (Zplus_z_OZ ?) (*XXX*);//
451 ##|#q;nnormalize;nrewrite > (nat_compare_n_m_m_n ??) (*XXX*);
452 ncases (nat_compare q p);//]
455 ##|#q;nnormalize;nrewrite > (nat_compare_n_m_m_n ??) (*XXX*);
456 ncases (nat_compare q p);//
461 ntheorem Zpred_Zplus_neg_O : ∀z. Zpred z = (neg O)+z.
464 ##|##*:#n;ncases n;//]
467 ntheorem Zsucc_Zplus_pos_O : ∀z. Zsucc z = (pos O)+z.
470 ##|##*:#n;ncases n;//]
473 ntheorem Zplus_pos_pos:
474 ∀n,m. (pos n)+(pos m) = (Zsucc (pos n))+(Zpred (pos m)).
479 nrewrite < (plus_n_Sm ??);nrewrite < (plus_n_O ?); (* XXX yet again *)
481 ##|#q;nnormalize;nrewrite < (plus_n_Sm ??);nrewrite < (plus_n_Sm ??);//]
485 ntheorem Zplus_pos_neg:
486 ∀n,m. (pos n)+(neg m) = (Zsucc (pos n))+(Zpred (neg m)).
490 ntheorem Zplus_neg_pos :
491 ∀n,m. (neg n)+(pos m) = (Zsucc (neg n))+(Zpred (pos m)).
492 #n;#m;ncases n;ncases m;//;
495 ntheorem Zplus_neg_neg:
496 ∀n,m. (neg n)+(neg m) = (Zsucc (neg n))+(Zpred (neg m)).
499 ##|#p;ncases m;nnormalize;
500 ##[nrewrite > (plus_n_Sm ??);//
501 ##|#q;nrewrite > (plus_n_Sm ??);//]
505 ntheorem Zplus_Zsucc_Zpred: ∀x,y. x+y = (Zsucc x)+(Zpred y).
509 ##|#n;nrewrite < (Zsucc_Zplus_pos_O ?);nrewrite > (Zsucc_Zpred ?);//
513 ##[#n;nrewrite < (sym_Zplus ??);nrewrite < (sym_Zplus (Zpred OZ) ?);
514 nrewrite < (Zpred_Zplus_neg_O ?);nrewrite > (Zpred_Zsucc ?);//
518 ntheorem Zplus_Zsucc_pos_pos :
519 ∀n,m. (Zsucc (pos n))+(pos m) = Zsucc ((pos n)+(pos m)).
523 ntheorem Zplus_Zsucc_pos_neg:
524 ∀n,m. (Zsucc (pos n))+(neg m) = (Zsucc ((pos n)+(neg m))).
526 napply (nat_elim2 (λn,m. (Zsucc (pos n))+(neg m) = (Zsucc ((pos n)+(neg m)))))
529 ##|#n2;#IH;nelim n2;//]
531 ##|#n1;#m1;#IH;nrewrite < (Zplus_pos_neg ? m1);nelim IH;//]
534 ntheorem Zplus_Zsucc_neg_neg :
535 ∀n,m. Zsucc (neg n) + neg m = Zsucc (neg n + neg m).
537 napply (nat_elim2 (λ n,m. Zsucc (neg n) + neg m = Zsucc (neg n + neg m)))
540 ##|#n2;#IH;nelim n2;//]
544 ntheorem Zplus_Zsucc_neg_pos:
545 ∀n,m. Zsucc (neg n)+(pos m) = Zsucc ((neg n)+(pos m)).
547 napply (nat_elim2 (λn,m. Zsucc (neg n) + (pos m) = Zsucc (neg n + pos m)))
550 ##|#n2;#IH;nelim n2;//]
552 ##|#n1;#m1;#IH;nrewrite < IH;nrewrite < (Zplus_neg_pos ? (S m1));//]
555 ntheorem Zplus_Zsucc : ∀x,y:Z. (Zsucc x)+y = Zsucc (x+y).
557 ##[ncases y;//;#n;nnormalize;ncases n;//;
558 ##|##*:#n;ncases y;//]
561 ntheorem Zplus_Zpred: ∀x,y:Z. (Zpred x)+y = Zpred (x+y).
562 #x;#y;ncut (Zpred (x+y) = Zpred ((Zsucc (Zpred x))+y))
563 ##[nrewrite > (Zsucc_Zpred ?);//
564 ##|#Hcut;nrewrite > Hcut;nrewrite > (Zplus_Zsucc ??);//]
567 ntheorem associative_Zplus: associative Z Zplus.
568 (* nchange with (\forall x,y,z:Z. (x + y) + z = x + (y + z));*)
572 ##[nrewrite < (Zsucc_Zplus_pos_O ?);nrewrite < (Zsucc_Zplus_pos_O ?);//;
574 nrewrite > (Zplus_Zsucc (pos n1) ?);nrewrite > (Zplus_Zsucc (pos n1) ?);
575 nrewrite > (Zplus_Zsucc ((pos n1)+y) ?);//]
577 ##[nrewrite < (Zpred_Zplus_neg_O (y+z));nrewrite < (Zpred_Zplus_neg_O y);//;
579 nrewrite > (Zplus_Zpred (neg n1) ?);nrewrite > (Zplus_Zpred (neg n1) ?);
580 nrewrite > (Zplus_Zpred ((neg n1)+y) ?);//]
584 (* variant assoc_Zplus : \forall x,y,z:Z. (x+y)+z = x+(y+z)
585 \def associative_Zplus. *)
588 ndefinition Zopp : Z → Z ≝
594 interpretation "integer unary minus" 'uminus x = (Zopp x).
596 ntheorem eq_OZ_Zopp_OZ : OZ = (- OZ).
600 ntheorem Zopp_Zplus: ∀x,y:Z. -(x+y) = -x + -y.
603 ##|##*:#n;ncases y;//;#m;nnormalize;napply nat_compare_elim;nnormalize;//]
606 ntheorem Zopp_Zopp: ∀x:Z. --x = x.
610 ntheorem Zplus_Zopp: ∀ x:Z. x+ -x = OZ.
613 ##|##*:#n;nnormalize;nrewrite > (nat_compare_n_n ?);//]
616 ntheorem injective_Zplus_l: ∀x:Z.injective Z Z (λy.y+x).
618 nrewrite < (Zplus_z_OZ z);nrewrite < (Zplus_z_OZ y);
619 nrewrite < (Zplus_Zopp x);
620 nrewrite < (associative_Zplus ???);nrewrite < (associative_Zplus ???);
624 ntheorem injective_Zplus_r: ∀x:Z.injective Z Z (λy.x+y).
626 napply (injective_Zplus_l x);
627 nrewrite < (sym_Zplus ??);
632 ndefinition Zminus : Z → Z → Z ≝ λx,y:Z. x + (-y).
634 interpretation "integer minus" 'minus x y = (Zminus x y).