src/ZF/Integ/Int.ML
author paulson
Fri Sep 25 13:18:07 1998 +0200 (1998-09-25)
changeset 5561 426c1e330903
child 5758 27a2b36efd95
permissions -rw-r--r--
Renaming of Integ/Integ.* to Integ/Int.*, and renaming of related constants
     1 (*  Title:      ZF/Integ/Int.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1993  University of Cambridge
     5 
     6 The integers as equivalence classes over nat*nat.
     7 
     8 Could also prove...
     9 "znegative(z) ==> $# zmagnitude(z) = $~ z"
    10 "~ znegative(z) ==> $# zmagnitude(z) = z"
    11 $< is a linear ordering
    12 $+ and $* are monotonic wrt $<
    13 *)
    14 
    15 AddSEs [quotientE];
    16 
    17 (*** Proving that intrel is an equivalence relation ***)
    18 
    19 (*By luck, requires no typing premises for y1, y2,y3*)
    20 val eqa::eqb::prems = goal Arith.thy 
    21     "[| x1 #+ y2 = x2 #+ y1; x2 #+ y3 = x3 #+ y2;  \
    22 \       x1: nat; x2: nat; x3: nat |]    ==>    x1 #+ y3 = x3 #+ y1";
    23 by (res_inst_tac [("k","x2")] add_left_cancel 1);
    24 by (resolve_tac prems 2);
    25 by (rtac (add_left_commute RS trans) 1 THEN typechk_tac prems);
    26 by (stac eqb 1);
    27 by (rtac (add_left_commute RS trans) 1 THEN typechk_tac prems);
    28 by (stac eqa 1);
    29 by (rtac (add_left_commute) 1 THEN typechk_tac prems);
    30 qed "int_trans_lemma";
    31 
    32 (** Natural deduction for intrel **)
    33 
    34 Goalw [intrel_def]
    35     "<<x1,y1>,<x2,y2>>: intrel <-> \
    36 \    x1: nat & y1: nat & x2: nat & y2: nat & x1#+y2 = x2#+y1";
    37 by (Fast_tac 1);
    38 qed "intrel_iff";
    39 
    40 Goalw [intrel_def]
    41     "[| x1#+y2 = x2#+y1; x1: nat; y1: nat; x2: nat; y2: nat |] ==> \
    42 \             <<x1,y1>,<x2,y2>>: intrel";
    43 by (fast_tac (claset() addIs prems) 1);
    44 qed "intrelI";
    45 
    46 (*intrelE is hard to derive because fast_tac tries hyp_subst_tac so soon*)
    47 Goalw [intrel_def]
    48   "p: intrel --> (EX x1 y1 x2 y2. \
    49 \                  p = <<x1,y1>,<x2,y2>> & x1#+y2 = x2#+y1 & \
    50 \                  x1: nat & y1: nat & x2: nat & y2: nat)";
    51 by (Fast_tac 1);
    52 qed "intrelE_lemma";
    53 
    54 val [major,minor] = goal thy
    55   "[| p: intrel;  \
    56 \     !!x1 y1 x2 y2. [| p = <<x1,y1>,<x2,y2>>;  x1#+y2 = x2#+y1; \
    57 \                       x1: nat; y1: nat; x2: nat; y2: nat |] ==> Q |] \
    58 \  ==> Q";
    59 by (cut_facts_tac [major RS (intrelE_lemma RS mp)] 1);
    60 by (REPEAT (eresolve_tac [asm_rl,exE,conjE,minor] 1));
    61 qed "intrelE";
    62 
    63 AddSIs [intrelI];
    64 AddSEs [intrelE];
    65 
    66 Goalw [equiv_def, refl_def, sym_def, trans_def]
    67     "equiv(nat*nat, intrel)";
    68 by (fast_tac (claset() addSEs [sym, int_trans_lemma]) 1);
    69 qed "equiv_intrel";
    70 
    71 
    72 Addsimps [equiv_intrel RS eq_equiv_class_iff, intrel_iff,
    73 	  add_0_right, add_succ_right];
    74 Addcongs [conj_cong];
    75 
    76 val eq_intrelD = equiv_intrel RSN (2,eq_equiv_class);
    77 
    78 (** int_of: the injection from nat to int **)
    79 
    80 Goalw [int_def,quotient_def,int_of_def]
    81     "m : nat ==> $#m : int";
    82 by (fast_tac (claset() addSIs [nat_0I]) 1);
    83 qed "int_of_type";
    84 
    85 Addsimps [int_of_type];
    86 
    87 Goalw [int_of_def] "[| $#m = $#n;  m: nat |] ==> m=n";
    88 by (dtac (sym RS eq_intrelD) 1);
    89 by (typechk_tac [nat_0I, SigmaI]);
    90 by (Asm_full_simp_tac 1);
    91 qed "int_of_inject";
    92 
    93 AddSDs [int_of_inject];
    94 
    95 Goal "m: nat ==> ($# m = $# n) <-> (m = n)"; 
    96 by (Blast_tac 1); 
    97 qed "int_of_eq"; 
    98 Addsimps [int_of_eq]; 
    99 
   100 (**** zminus: unary negation on int ****)
   101 
   102 Goalw [congruent_def] "congruent(intrel, %<x,y>. intrel``{<y,x>})";
   103 by Safe_tac;
   104 by (asm_full_simp_tac (simpset() addsimps add_ac) 1);
   105 qed "zminus_congruent";
   106 
   107 (*Resolve th against the corresponding facts for zminus*)
   108 val zminus_ize = RSLIST [equiv_intrel, zminus_congruent];
   109 
   110 Goalw [int_def,zminus_def] "z : int ==> $~z : int";
   111 by (typechk_tac [split_type, SigmaI, zminus_ize UN_equiv_class_type,
   112                  quotientI]);
   113 qed "zminus_type";
   114 
   115 Goalw [int_def,zminus_def] "[| $~z = $~w;  z: int;  w: int |] ==> z=w";
   116 by (etac (zminus_ize UN_equiv_class_inject) 1);
   117 by Safe_tac;
   118 (*The setloop is only needed because assumptions are in the wrong order!*)
   119 by (asm_full_simp_tac (simpset() addsimps add_ac
   120                        setloop dtac eq_intrelD) 1);
   121 qed "zminus_inject";
   122 
   123 Goalw [zminus_def]
   124     "[| x: nat;  y: nat |] ==> $~ (intrel``{<x,y>}) = intrel `` {<y,x>}";
   125 by (asm_simp_tac (simpset() addsimps [zminus_ize UN_equiv_class, SigmaI]) 1);
   126 qed "zminus";
   127 
   128 Goalw [int_def] "z : int ==> $~ ($~ z) = z";
   129 by (REPEAT (eresolve_tac [quotientE,SigmaE,ssubst] 1));
   130 by (asm_simp_tac (simpset() addsimps [zminus]) 1);
   131 qed "zminus_zminus";
   132 
   133 Goalw [int_def, int_of_def] "$~ ($#0) = $#0";
   134 by (simp_tac (simpset() addsimps [zminus]) 1);
   135 qed "zminus_0";
   136 
   137 Addsimps [zminus_zminus, zminus_0];
   138 
   139 
   140 (**** znegative: the test for negative integers ****)
   141 
   142 (*No natural number is negative!*)
   143 Goalw [znegative_def, int_of_def]  "~ znegative($# n)";
   144 by Safe_tac;
   145 by (dres_inst_tac [("psi", "?lhs=?rhs")] asm_rl 1);
   146 by (dres_inst_tac [("psi", "?lhs<?rhs")] asm_rl 1);
   147 by (force_tac (claset(),
   148 	       simpset() addsimps [add_le_self2 RS le_imp_not_lt]) 1);
   149 qed "not_znegative_int_of";
   150 
   151 Addsimps [not_znegative_int_of];
   152 AddSEs   [not_znegative_int_of RS notE];
   153 
   154 Goalw [znegative_def, int_of_def] "n: nat ==> znegative($~ $# succ(n))";
   155 by (asm_simp_tac (simpset() addsimps [zminus]) 1);
   156 by (blast_tac (claset() addIs [nat_0_le]) 1);
   157 qed "znegative_zminus_int_of";
   158 
   159 Addsimps [znegative_zminus_int_of];
   160 
   161 Goalw [znegative_def, int_of_def] "[| n: nat; ~ znegative($~ $# n) |] ==> n=0";
   162 by (asm_full_simp_tac (simpset() addsimps [zminus, image_singleton_iff]) 1);
   163 be natE 1;
   164 by (dres_inst_tac [("x","0")] spec 2);
   165 by Auto_tac;
   166 qed "not_znegative_imp_zero";
   167 
   168 (**** zmagnitude: magnitide of an integer, as a natural number ****)
   169 
   170 Goalw [zmagnitude_def] "n: nat ==> zmagnitude($# n) = n";
   171 by Auto_tac;
   172 qed "zmagnitude_int_of";
   173 
   174 Goalw [zmagnitude_def] "n: nat ==> zmagnitude($~ $# n) = n";
   175 by (auto_tac(claset() addDs [not_znegative_imp_zero], simpset()));
   176 qed "zmagnitude_zminus_int_of";
   177 
   178 Addsimps [zmagnitude_int_of, zmagnitude_zminus_int_of];
   179 
   180 Goalw [zmagnitude_def] "zmagnitude(z) : nat";
   181 br theI2 1;
   182 by Auto_tac;
   183 qed "zmagnitude_type";
   184 
   185 Goalw [int_def, znegative_def, int_of_def]
   186      "[| z: int; ~ znegative(z) |] ==> EX n:nat. z = $# n"; 
   187 by (auto_tac(claset() , simpset() addsimps [image_singleton_iff]));
   188 by (rename_tac "i j" 1);
   189 by (dres_inst_tac [("x", "i")] spec 1);
   190 by (dres_inst_tac [("x", "j")] spec 1);
   191 br bexI 1;
   192 br (add_diff_inverse2 RS sym) 1;
   193 by Auto_tac;
   194 by (asm_full_simp_tac (simpset() addsimps [nat_into_Ord, not_lt_iff_le]) 1);
   195 qed "not_zneg_int_of";
   196 
   197 Goal "[| z: int; ~ znegative(z) |] ==> $# (zmagnitude(z)) = z"; 
   198 bd not_zneg_int_of 1;
   199 by Auto_tac;
   200 qed "not_zneg_mag"; 
   201 
   202 Addsimps [not_zneg_mag];
   203 
   204 
   205 Goalw [int_def, znegative_def, int_of_def]
   206      "[| z: int; znegative(z) |] ==> EX n:nat. z = $~ ($# succ(n))"; 
   207 by (auto_tac(claset() addSDs [less_imp_Suc_add], 
   208 	     simpset() addsimps [zminus, image_singleton_iff]));
   209 by (rename_tac "m n j k" 1);
   210 by (subgoal_tac "j #+ succ(m #+ k) = j #+ n" 1);
   211 by (rotate_tac ~2 2);
   212 by (asm_full_simp_tac (simpset() addsimps add_ac) 2);
   213 by (blast_tac (claset() addSDs [add_left_cancel]) 1);
   214 qed "zneg_int_of";
   215 
   216 Goal "[| z: int; znegative(z) |] ==> $# (zmagnitude(z)) = $~ z"; 
   217 bd zneg_int_of 1;
   218 by Auto_tac;
   219 qed "zneg_mag"; 
   220 
   221 Addsimps [zneg_mag];
   222 
   223 
   224 (**** zadd: addition on int ****)
   225 
   226 (** Congruence property for addition **)
   227 
   228 Goalw [congruent2_def]
   229     "congruent2(intrel, %z1 z2.                      \
   230 \         let <x1,y1>=z1; <x2,y2>=z2                 \
   231 \                           in intrel``{<x1#+x2, y1#+y2>})";
   232 (*Proof via congruent2_commuteI seems longer*)
   233 by Safe_tac;
   234 by (asm_simp_tac (simpset() addsimps [add_assoc, Let_def]) 1);
   235 (*The rest should be trivial, but rearranging terms is hard;
   236   add_ac does not help rewriting with the assumptions.*)
   237 by (res_inst_tac [("m1","x1a")] (add_left_commute RS ssubst) 1);
   238 by (res_inst_tac [("m1","x2a")] (add_left_commute RS ssubst) 3);
   239 by (typechk_tac [add_type]);
   240 by (asm_simp_tac (simpset() addsimps [add_assoc RS sym]) 1);
   241 qed "zadd_congruent2";
   242 
   243 (*Resolve th against the corresponding facts for zadd*)
   244 val zadd_ize = RSLIST [equiv_intrel, zadd_congruent2];
   245 
   246 Goalw [int_def,zadd_def] "[| z: int;  w: int |] ==> z $+ w : int";
   247 by (rtac (zadd_ize UN_equiv_class_type2) 1);
   248 by (simp_tac (simpset() addsimps [Let_def]) 3);
   249 by (REPEAT (ares_tac [split_type, add_type, quotientI, SigmaI] 1));
   250 qed "zadd_type";
   251 
   252 Goalw [zadd_def]
   253   "[| x1: nat; y1: nat;  x2: nat; y2: nat |] ==>       \
   254 \           (intrel``{<x1,y1>}) $+ (intrel``{<x2,y2>}) =        \
   255 \           intrel `` {<x1#+x2, y1#+y2>}";
   256 by (asm_simp_tac (simpset() addsimps [zadd_ize UN_equiv_class2, SigmaI]) 1);
   257 by (simp_tac (simpset() addsimps [Let_def]) 1);
   258 qed "zadd";
   259 
   260 Goalw [int_def,int_of_def] "z : int ==> $#0 $+ z = z";
   261 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   262 by (asm_simp_tac (simpset() addsimps [zadd]) 1);
   263 qed "zadd_0";
   264 
   265 Goalw [int_def] "[| z: int;  w: int |] ==> $~ (z $+ w) = $~ z $+ $~ w";
   266 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   267 by (asm_simp_tac (simpset() addsimps [zminus,zadd]) 1);
   268 qed "zminus_zadd_distrib";
   269 
   270 Goalw [int_def] "[| z: int;  w: int |] ==> z $+ w = w $+ z";
   271 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   272 by (asm_simp_tac (simpset() addsimps add_ac @ [zadd]) 1);
   273 qed "zadd_commute";
   274 
   275 Goalw [int_def]
   276     "[| z1: int;  z2: int;  z3: int |]   \
   277 \    ==> (z1 $+ z2) $+ z3 = z1 $+ (z2 $+ z3)";
   278 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   279 (*rewriting is much faster without intrel_iff, etc.*)
   280 by (asm_simp_tac (simpset() addsimps [zadd, add_assoc]) 1);
   281 qed "zadd_assoc";
   282 
   283 (*For AC rewriting*)
   284 Goal "[| z1:int;  z2:int;  z3: int |] ==> z1$+(z2$+z3) = z2$+(z1$+z3)";
   285 by (asm_simp_tac (simpset() addsimps [zadd_assoc RS sym, zadd_commute]) 1);
   286 qed "zadd_left_commute";
   287 
   288 (*Integer addition is an AC operator*)
   289 val zadd_ac = [zadd_assoc, zadd_commute, zadd_left_commute];
   290 
   291 Goalw [int_of_def]
   292     "[| m: nat;  n: nat |] ==> $# (m #+ n) = ($#m) $+ ($#n)";
   293 by (asm_simp_tac (simpset() addsimps [zadd]) 1);
   294 qed "int_of_add";
   295 
   296 Goalw [int_def,int_of_def] "z : int ==> z $+ ($~ z) = $#0";
   297 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   298 by (asm_simp_tac (simpset() addsimps [zminus, zadd, add_commute]) 1);
   299 qed "zadd_zminus_inverse";
   300 
   301 Goal "z : int ==> ($~ z) $+ z = $#0";
   302 by (asm_simp_tac
   303     (simpset() addsimps [zadd_commute, zminus_type, zadd_zminus_inverse]) 1);
   304 qed "zadd_zminus_inverse2";
   305 
   306 Goal "z:int ==> z $+ $#0 = z";
   307 by (rtac (zadd_commute RS trans) 1);
   308 by (REPEAT (ares_tac [int_of_type, nat_0I, zadd_0] 1));
   309 qed "zadd_0_right";
   310 
   311 Addsimps [zadd_0, zadd_0_right, zadd_zminus_inverse, zadd_zminus_inverse2];
   312 
   313 
   314 (*Need properties of $- ???  Or use $- just as an abbreviation?
   315      [| m: nat;  n: nat;  m>=n |] ==> $# (m #- n) = ($#m) $- ($#n)
   316 *)
   317 
   318 (**** zmult: multiplication on int ****)
   319 
   320 (** Congruence property for multiplication **)
   321 
   322 Goal "congruent2(intrel, %p1 p2.                 \
   323 \               split(%x1 y1. split(%x2 y2.     \
   324 \                   intrel``{<x1#*x2 #+ y1#*y2, x1#*y2 #+ y1#*x2>}, p2), p1))";
   325 by (rtac (equiv_intrel RS congruent2_commuteI) 1);
   326 by Safe_tac;
   327 by (ALLGOALS Asm_simp_tac);
   328 (*Proof that zmult is congruent in one argument*)
   329 by (asm_simp_tac 
   330     (simpset() addsimps add_ac @ [add_mult_distrib_left RS sym]) 2);
   331 by (asm_simp_tac
   332     (simpset() addsimps [add_assoc RS sym, add_mult_distrib_left RS sym]) 2);
   333 (*Proof that zmult is commutative on representatives*)
   334 by (asm_simp_tac (simpset() addsimps mult_ac@add_ac) 1);
   335 qed "zmult_congruent2";
   336 
   337 
   338 (*Resolve th against the corresponding facts for zmult*)
   339 val zmult_ize = RSLIST [equiv_intrel, zmult_congruent2];
   340 
   341 Goalw [int_def,zmult_def] "[| z: int;  w: int |] ==> z $* w : int";
   342 by (REPEAT (ares_tac [zmult_ize UN_equiv_class_type2,
   343                       split_type, add_type, mult_type, 
   344                       quotientI, SigmaI] 1));
   345 qed "zmult_type";
   346 
   347 Goalw [zmult_def]
   348      "[| x1: nat; y1: nat;  x2: nat; y2: nat |] ==>    \
   349 \              (intrel``{<x1,y1>}) $* (intrel``{<x2,y2>}) =     \
   350 \              intrel `` {<x1#*x2 #+ y1#*y2, x1#*y2 #+ y1#*x2>}";
   351 by (asm_simp_tac (simpset() addsimps [zmult_ize UN_equiv_class2, SigmaI]) 1);
   352 qed "zmult";
   353 
   354 Goalw [int_def,int_of_def] "z : int ==> $#0 $* z = $#0";
   355 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   356 by (asm_simp_tac (simpset() addsimps [zmult]) 1);
   357 qed "zmult_0";
   358 
   359 Goalw [int_def,int_of_def] "z : int ==> $#1 $* z = z";
   360 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   361 by (asm_simp_tac (simpset() addsimps [zmult, add_0_right]) 1);
   362 qed "zmult_1";
   363 
   364 Goalw [int_def] "[| z: int;  w: int |] ==> ($~ z) $* w = $~ (z $* w)";
   365 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   366 by (asm_simp_tac (simpset() addsimps [zminus, zmult] @ add_ac) 1);
   367 qed "zmult_zminus";
   368 
   369 Addsimps [zmult_0, zmult_1, zmult_zminus];
   370 
   371 Goalw [int_def] "[| z: int;  w: int |] ==> ($~ z) $* ($~ w) = (z $* w)";
   372 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   373 by (asm_simp_tac (simpset() addsimps [zminus, zmult] @ add_ac) 1);
   374 qed "zmult_zminus_zminus";
   375 
   376 Goalw [int_def] "[| z: int;  w: int |] ==> z $* w = w $* z";
   377 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   378 by (asm_simp_tac (simpset() addsimps [zmult] @ add_ac @ mult_ac) 1);
   379 qed "zmult_commute";
   380 
   381 Goalw [int_def]
   382     "[| z1: int;  z2: int;  z3: int |]     \
   383 \    ==> (z1 $* z2) $* z3 = z1 $* (z2 $* z3)";
   384 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   385 by (asm_simp_tac 
   386     (simpset() addsimps [zmult, add_mult_distrib_left, 
   387                          add_mult_distrib] @ add_ac @ mult_ac) 1);
   388 qed "zmult_assoc";
   389 
   390 (*For AC rewriting*)
   391 Goal "[| z1:int;  z2:int;  z3: int |] ==> z1$*(z2$*z3) = z2$*(z1$*z3)";
   392 by (asm_simp_tac (simpset() addsimps [zmult_assoc RS sym, zmult_commute]) 1);
   393 qed "zmult_left_commute";
   394 
   395 (*Integer multiplication is an AC operator*)
   396 val zmult_ac = [zmult_assoc, zmult_commute, zmult_left_commute];
   397 
   398 Goalw [int_def]
   399     "[| z1: int;  z2: int;  w: int |] ==> \
   400 \                (z1 $+ z2) $* w = (z1 $* w) $+ (z2 $* w)";
   401 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   402 by (asm_simp_tac (simpset() addsimps [zadd, zmult, add_mult_distrib]) 1);
   403 by (asm_simp_tac (simpset() addsimps add_ac @ mult_ac) 1);
   404 qed "zadd_zmult_distrib";
   405 
   406 val int_typechecks =
   407     [int_of_type, zminus_type, zmagnitude_type, zadd_type, zmult_type];
   408 
   409 Addsimps int_typechecks;
   410 
   411 
   412