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