src/ZF/Integ/Int.ML
author paulson
Tue Aug 01 15:28:21 2000 +0200 (2000-08-01)
changeset 9491 1a36151ee2fc
parent 9333 5cacc383157a
child 9496 07e33cac5d9c
permissions -rw-r--r--
natify, a coercion to reduce the number of type constraints in arithmetic
     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 (** Natural deduction for intrel **)
    20 
    21 Goalw [intrel_def]
    22     "<<x1,y1>,<x2,y2>>: intrel <-> \
    23 \    x1: nat & y1: nat & x2: nat & y2: nat & x1#+y2 = x2#+y1";
    24 by (Fast_tac 1);
    25 qed "intrel_iff";
    26 
    27 Goalw [intrel_def]
    28     "[| x1#+y2 = x2#+y1; x1: nat; y1: nat; x2: nat; y2: nat |] ==> \
    29 \             <<x1,y1>,<x2,y2>>: intrel";
    30 by (fast_tac (claset() addIs prems) 1);
    31 qed "intrelI";
    32 
    33 (*intrelE is hard to derive because fast_tac tries hyp_subst_tac so soon*)
    34 Goalw [intrel_def]
    35   "p: intrel --> (EX x1 y1 x2 y2. \
    36 \                  p = <<x1,y1>,<x2,y2>> & x1#+y2 = x2#+y1 & \
    37 \                  x1: nat & y1: nat & x2: nat & y2: nat)";
    38 by (Fast_tac 1);
    39 qed "intrelE_lemma";
    40 
    41 val [major,minor] = goal thy
    42   "[| p: intrel;  \
    43 \     !!x1 y1 x2 y2. [| p = <<x1,y1>,<x2,y2>>;  x1#+y2 = x2#+y1; \
    44 \                       x1: nat; y1: nat; x2: nat; y2: nat |] ==> Q |] \
    45 \  ==> Q";
    46 by (cut_facts_tac [major RS (intrelE_lemma RS mp)] 1);
    47 by (REPEAT (eresolve_tac [asm_rl,exE,conjE,minor] 1));
    48 qed "intrelE";
    49 
    50 AddSIs [intrelI];
    51 AddSEs [intrelE];
    52 
    53 val eqa::eqb::prems = goal Arith.thy 
    54     "[| x1 #+ y2 = x2 #+ y1; x2 #+ y3 = x3 #+ y2 |] ==> x1 #+ y3 = x3 #+ y1";
    55 by (res_inst_tac [("k","x2")] add_left_cancel 1);
    56 by (rtac (add_left_commute RS trans) 1);
    57 by Auto_tac;
    58 by (stac eqb 1);
    59 by (rtac (add_left_commute RS trans) 1);
    60 by (ALLGOALS (asm_simp_tac (simpset() addsimps [eqa, add_left_commute])));
    61 qed "int_trans_lemma";
    62 
    63 Goalw [equiv_def, refl_def, sym_def, trans_def]
    64     "equiv(nat*nat, intrel)";
    65 by (fast_tac (claset() addSEs [sym, int_trans_lemma]) 1);
    66 qed "equiv_intrel";
    67 
    68 
    69 Addsimps [equiv_intrel RS eq_equiv_class_iff, intrel_iff,
    70 	  add_0_right, add_succ_right];
    71 Addcongs [conj_cong];
    72 
    73 val eq_intrelD = equiv_intrel RSN (2,eq_equiv_class);
    74 
    75 (** int_of: the injection from nat to int **)
    76 
    77 Goalw [int_def,quotient_def,int_of_def]
    78     "m : nat ==> $#m : int";
    79 by Auto_tac;
    80 qed "int_of_type";
    81 
    82 Addsimps [int_of_type];
    83 AddTCs   [int_of_type];
    84 
    85 Goalw [int_of_def] "[| $#m = $#n;  m: nat |] ==> m=n";
    86 by (dtac (sym RS eq_intrelD) 1);
    87 by Auto_tac;
    88 qed "int_of_inject";
    89 
    90 AddSDs [int_of_inject];
    91 
    92 Goal "m: nat ==> ($# m = $# n) <-> (m = n)"; 
    93 by (Blast_tac 1); 
    94 qed "int_of_eq"; 
    95 Addsimps [int_of_eq]; 
    96 
    97 (**** zminus: unary negation on int ****)
    98 
    99 Goalw [congruent_def] "congruent(intrel, %<x,y>. intrel``{<y,x>})";
   100 by Safe_tac;
   101 by (asm_full_simp_tac (simpset() addsimps add_ac) 1);
   102 qed "zminus_congruent";
   103 
   104 val RSLIST = curry (op MRS);
   105 
   106 (*Resolve th against the corresponding facts for zminus*)
   107 val zminus_ize = RSLIST [equiv_intrel, zminus_congruent];
   108 
   109 Goalw [int_def,zminus_def] "z : int ==> $~z : int";
   110 by (typecheck_tac (tcset() addTCs [zminus_ize UN_equiv_class_type]));
   111 qed "zminus_type";
   112 AddTCs [zminus_type];
   113 
   114 Goalw [int_def,zminus_def] "[| $~z = $~w;  z: int;  w: int |] ==> z=w";
   115 by (etac (zminus_ize UN_equiv_class_inject) 1);
   116 by Safe_tac;
   117 (*The setloop is only needed because assumptions are in the wrong order!*)
   118 by (asm_full_simp_tac (simpset() addsimps add_ac
   119                        setloop dtac eq_intrelD) 1);
   120 qed "zminus_inject";
   121 
   122 Goalw [zminus_def]
   123     "[| x: nat;  y: nat |] ==> $~ (intrel``{<x,y>}) = intrel `` {<y,x>}";
   124 by (asm_simp_tac (simpset() addsimps [zminus_ize UN_equiv_class, SigmaI]) 1);
   125 qed "zminus";
   126 
   127 Goalw [int_def] "z : int ==> $~ ($~ z) = z";
   128 by (REPEAT (eresolve_tac [quotientE,SigmaE,ssubst] 1));
   129 by (asm_simp_tac (simpset() addsimps [zminus]) 1);
   130 qed "zminus_zminus";
   131 
   132 Goalw [int_def, int_of_def] "$~ ($#0) = $#0";
   133 by (simp_tac (simpset() addsimps [zminus]) 1);
   134 qed "zminus_0";
   135 
   136 Addsimps [zminus_zminus, zminus_0];
   137 
   138 
   139 (**** znegative: the test for negative integers ****)
   140 
   141 (*No natural number is negative!*)
   142 Goalw [znegative_def, int_of_def]  "~ znegative($# n)";
   143 by Safe_tac;
   144 by (dres_inst_tac [("psi", "?lhs=?rhs")] asm_rl 1);
   145 by (dres_inst_tac [("psi", "?lhs<?rhs")] asm_rl 1);
   146 by (force_tac (claset(),
   147 	       simpset() addsimps [add_le_self2 RS le_imp_not_lt]) 1);
   148 qed "not_znegative_int_of";
   149 
   150 Addsimps [not_znegative_int_of];
   151 AddSEs   [not_znegative_int_of RS notE];
   152 
   153 Goalw [znegative_def, int_of_def] "n: nat ==> znegative($~ $# succ(n))";
   154 by (asm_simp_tac (simpset() addsimps [zminus]) 1);
   155 by (blast_tac (claset() addIs [nat_0_le]) 1);
   156 qed "znegative_zminus_int_of";
   157 
   158 Addsimps [znegative_zminus_int_of];
   159 
   160 Goalw [znegative_def, int_of_def] "[| n: nat; ~ znegative($~ $# n) |] ==> n=0";
   161 by (asm_full_simp_tac (simpset() addsimps [zminus, image_singleton_iff]) 1);
   162 by (etac natE 1);
   163 by (dres_inst_tac [("x","0")] spec 2);
   164 by Auto_tac;
   165 qed "not_znegative_imp_zero";
   166 
   167 (**** zmagnitude: magnitide of an integer, as a natural number ****)
   168 
   169 Goalw [zmagnitude_def] "n: nat ==> zmagnitude($# n) = n";
   170 by Auto_tac;
   171 qed "zmagnitude_int_of";
   172 
   173 Goalw [zmagnitude_def] "n: nat ==> zmagnitude($~ $# n) = n";
   174 by (force_tac(claset() addDs [not_znegative_imp_zero], simpset())1);
   175 qed "zmagnitude_zminus_int_of";
   176 
   177 Addsimps [zmagnitude_int_of, zmagnitude_zminus_int_of];
   178 
   179 Goalw [zmagnitude_def] "zmagnitude(z) : nat";
   180 by (rtac theI2 1);
   181 by Auto_tac;
   182 qed "zmagnitude_type";
   183 AddTCs [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 by (rtac bexI 1);
   192 by (rtac (add_diff_inverse2 RS sym) 1);
   193 by Auto_tac;
   194 by (asm_full_simp_tac (simpset() addsimps [not_lt_iff_le]) 1);
   195 qed "not_zneg_int_of";
   196 
   197 Goal "[| z: int; ~ znegative(z) |] ==> $# (zmagnitude(z)) = z"; 
   198 by (dtac 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 by (dtac 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) 1);
   239 by (asm_simp_tac (simpset() addsimps [add_assoc RS sym]) 1);
   240 qed "zadd_congruent2";
   241 
   242 (*Resolve th against the corresponding facts for zadd*)
   243 val zadd_ize = RSLIST [equiv_intrel, zadd_congruent2];
   244 
   245 Goalw [int_def,zadd_def] "[| z: int;  w: int |] ==> z $+ w : int";
   246 by (rtac (zadd_ize UN_equiv_class_type2) 1);
   247 by (simp_tac (simpset() addsimps [Let_def]) 3);
   248 by (REPEAT (ares_tac [split_type, add_type, quotientI, SigmaI] 1));
   249 qed "zadd_type";
   250 AddTCs [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]) 1);
   286 by (asm_simp_tac (simpset() addsimps [zadd_commute]) 1);
   287 qed "zadd_left_commute";
   288 
   289 (*Integer addition is an AC operator*)
   290 val zadd_ac = [zadd_assoc, zadd_commute, zadd_left_commute];
   291 
   292 Goalw [int_of_def]
   293     "[| m: nat;  n: nat |] ==> $# (m #+ n) = ($#m) $+ ($#n)";
   294 by (asm_simp_tac (simpset() addsimps [zadd]) 1);
   295 qed "int_of_add";
   296 
   297 Goalw [int_def,int_of_def] "z : int ==> z $+ ($~ z) = $#0";
   298 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   299 by (asm_simp_tac (simpset() addsimps [zminus, zadd, add_commute]) 1);
   300 qed "zadd_zminus_inverse";
   301 
   302 Goal "z : int ==> ($~ z) $+ z = $#0";
   303 by (asm_simp_tac
   304     (simpset() addsimps [zadd_commute, zminus_type, zadd_zminus_inverse]) 1);
   305 qed "zadd_zminus_inverse2";
   306 
   307 Goal "z:int ==> z $+ $#0 = z";
   308 by (rtac (zadd_commute RS trans) 1);
   309 by (REPEAT (ares_tac [int_of_type, nat_0I, zadd_0] 1));
   310 qed "zadd_0_right";
   311 
   312 Addsimps [zadd_0, zadd_0_right, zadd_zminus_inverse, zadd_zminus_inverse2];
   313 
   314 
   315 (*Need properties of $- ???  Or use $- just as an abbreviation?
   316      [| m: nat;  n: nat;  m>=n |] ==> $# (m #- n) = ($#m) $- ($#n)
   317 *)
   318 
   319 (**** zmult: multiplication on int ****)
   320 
   321 (** Congruence property for multiplication **)
   322 
   323 Goal "congruent2(intrel, %p1 p2.                 \
   324 \               split(%x1 y1. split(%x2 y2.     \
   325 \                   intrel``{<x1#*x2 #+ y1#*y2, x1#*y2 #+ y1#*x2>}, p2), p1))";
   326 by (rtac (equiv_intrel RS congruent2_commuteI) 1);
   327 by Safe_tac;
   328 by (ALLGOALS Asm_simp_tac);
   329 (*Proof that zmult is congruent in one argument*)
   330 by (asm_simp_tac 
   331     (simpset() addsimps add_ac @ [add_mult_distrib_left RS sym]) 2);
   332 by (asm_simp_tac
   333     (simpset() addsimps [add_assoc RS sym, add_mult_distrib_left RS sym]) 2);
   334 (*Proof that zmult is commutative on representatives*)
   335 by (asm_simp_tac (simpset() addsimps mult_ac@add_ac) 1);
   336 qed "zmult_congruent2";
   337 
   338 
   339 (*Resolve th against the corresponding facts for zmult*)
   340 val zmult_ize = RSLIST [equiv_intrel, zmult_congruent2];
   341 
   342 Goalw [int_def,zmult_def] "[| z: int;  w: int |] ==> z $* w : int";
   343 by (REPEAT (ares_tac [zmult_ize UN_equiv_class_type2,
   344                       split_type, add_type, mult_type, 
   345                       quotientI, SigmaI] 1));
   346 qed "zmult_type";
   347 AddTCs [zmult_type];
   348 
   349 Goalw [zmult_def]
   350      "[| x1: nat; y1: nat;  x2: nat; y2: nat |] ==>    \
   351 \              (intrel``{<x1,y1>}) $* (intrel``{<x2,y2>}) =     \
   352 \              intrel `` {<x1#*x2 #+ y1#*y2, x1#*y2 #+ y1#*x2>}";
   353 by (asm_simp_tac (simpset() addsimps [zmult_ize UN_equiv_class2, SigmaI]) 1);
   354 qed "zmult";
   355 
   356 Goalw [int_def,int_of_def] "z : int ==> $#0 $* z = $#0";
   357 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   358 by (asm_simp_tac (simpset() addsimps [zmult]) 1);
   359 qed "zmult_0";
   360 
   361 Goalw [int_def,int_of_def] "z : int ==> $#1 $* z = z";
   362 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   363 by (asm_simp_tac (simpset() addsimps [zmult, add_0_right]) 1);
   364 qed "zmult_1";
   365 
   366 Goalw [int_def] "[| z: int;  w: int |] ==> ($~ z) $* w = $~ (z $* w)";
   367 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   368 by (asm_simp_tac (simpset() addsimps [zminus, zmult] @ add_ac) 1);
   369 qed "zmult_zminus";
   370 
   371 Addsimps [zmult_0, zmult_1, zmult_zminus];
   372 
   373 Goalw [int_def] "[| z: int;  w: int |] ==> ($~ z) $* ($~ w) = (z $* w)";
   374 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   375 by (asm_simp_tac (simpset() addsimps [zminus, zmult] @ add_ac) 1);
   376 qed "zmult_zminus_zminus";
   377 
   378 Goalw [int_def] "[| z: int;  w: int |] ==> z $* w = w $* z";
   379 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   380 by (asm_simp_tac (simpset() addsimps [zmult] @ add_ac @ mult_ac) 1);
   381 qed "zmult_commute";
   382 
   383 Goalw [int_def]
   384     "[| z1: int;  z2: int;  z3: int |]     \
   385 \    ==> (z1 $* z2) $* z3 = z1 $* (z2 $* z3)";
   386 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   387 by (asm_simp_tac 
   388     (simpset() addsimps [zmult, add_mult_distrib_left, 
   389                          add_mult_distrib] @ add_ac @ mult_ac) 1);
   390 qed "zmult_assoc";
   391 
   392 (*For AC rewriting*)
   393 Goal "[| z1:int;  z2:int;  z3: int |] ==> z1$*(z2$*z3) = z2$*(z1$*z3)";
   394 by (asm_simp_tac (simpset() addsimps [zmult_assoc RS sym]) 1);
   395 by (asm_simp_tac (simpset() addsimps [zmult_commute]) 1);
   396 qed "zmult_left_commute";
   397 
   398 (*Integer multiplication is an AC operator*)
   399 val zmult_ac = [zmult_assoc, zmult_commute, zmult_left_commute];
   400 
   401 Goalw [int_def]
   402     "[| z1: int;  z2: int;  w: int |] ==> \
   403 \                (z1 $+ z2) $* w = (z1 $* w) $+ (z2 $* w)";
   404 by (REPEAT (eresolve_tac [quotientE, SigmaE, ssubst] 1));
   405 by (asm_simp_tac (simpset() addsimps [zadd, zmult, add_mult_distrib]) 1);
   406 by (asm_simp_tac (simpset() addsimps add_ac @ mult_ac) 1);
   407 qed "zadd_zmult_distrib";
   408 
   409 val int_typechecks =
   410     [int_of_type, zminus_type, zmagnitude_type, zadd_type, zmult_type];
   411 
   412 Addsimps int_typechecks;
   413 
   414 
   415