src/ZF/ex/Bin.ML
changeset 906 6cd9c397f36a
parent 782 200a16083201
child 1461 6bcb44e4d6e5
equal deleted inserted replaced
905:80b581036036 906:6cd9c397f36a
    27 by (rtac (bin_rec_def RS def_Vrec RS trans) 1);
    27 by (rtac (bin_rec_def RS def_Vrec RS trans) 1);
    28 by (rewrite_goals_tac bin.con_defs);
    28 by (rewrite_goals_tac bin.con_defs);
    29 by (simp_tac rank_ss 1);
    29 by (simp_tac rank_ss 1);
    30 qed "bin_rec_Minus";
    30 qed "bin_rec_Minus";
    31 
    31 
    32 goal Bin.thy "bin_rec(w$$x,a,b,h) = h(w, x, bin_rec(w,a,b,h))";
    32 goal Bin.thy "bin_rec(Bcons(w,x),a,b,h) = h(w, x, bin_rec(w,a,b,h))";
    33 by (rtac (bin_rec_def RS def_Vrec RS trans) 1);
    33 by (rtac (bin_rec_def RS def_Vrec RS trans) 1);
    34 by (rewrite_goals_tac bin.con_defs);
    34 by (rewrite_goals_tac bin.con_defs);
    35 by (simp_tac rank_ss 1);
    35 by (simp_tac rank_ss 1);
    36 qed "bin_rec_Bcons";
    36 qed "bin_rec_Bcons";
    37 
    37 
    38 (*Type checking*)
    38 (*Type checking*)
    39 val prems = goal Bin.thy
    39 val prems = goal Bin.thy
    40     "[| w: bin;    \
    40     "[| w: bin;    \
    41 \       a: C(Plus);   b: C(Minus);       \
    41 \       a: C(Plus);   b: C(Minus);       \
    42 \       !!w x r. [| w: bin;  x: bool;  r: C(w) |] ==> h(w,x,r): C(w$$x)  \
    42 \       !!w x r. [| w: bin; x: bool; r: C(w) |] ==> h(w,x,r): C(Bcons(w,x))  \
    43 \    |] ==> bin_rec(w,a,b,h) : C(w)";
    43 \    |] ==> bin_rec(w,a,b,h) : C(w)";
    44 by (bin_ind_tac "w" prems 1);
    44 by (bin_ind_tac "w" prems 1);
    45 by (ALLGOALS 
    45 by (ALLGOALS 
    46     (asm_simp_tac (ZF_ss addsimps (prems@[bin_rec_Plus,bin_rec_Minus,
    46     (asm_simp_tac (ZF_ss addsimps (prems@[bin_rec_Plus, bin_rec_Minus,
    47 					 bin_rec_Bcons]))));
    47 					  bin_rec_Bcons]))));
    48 qed "bin_rec_type";
    48 qed "bin_rec_type";
    49 
    49 
    50 (** Versions for use with definitions **)
    50 (** Versions for use with definitions **)
    51 
    51 
    52 val [rew] = goal Bin.thy
    52 val [rew] = goal Bin.thy
    60 by (rewtac rew);
    60 by (rewtac rew);
    61 by (rtac bin_rec_Minus 1);
    61 by (rtac bin_rec_Minus 1);
    62 qed "def_bin_rec_Minus";
    62 qed "def_bin_rec_Minus";
    63 
    63 
    64 val [rew] = goal Bin.thy
    64 val [rew] = goal Bin.thy
    65     "[| !!w. j(w)==bin_rec(w,a,b,h) |] ==> j(w$$x) = h(w,x,j(w))";
    65     "[| !!w. j(w)==bin_rec(w,a,b,h) |] ==> j(Bcons(w,x)) = h(w,x,j(w))";
    66 by (rewtac rew);
    66 by (rewtac rew);
    67 by (rtac bin_rec_Bcons 1);
    67 by (rtac bin_rec_Bcons 1);
    68 qed "def_bin_rec_Bcons";
    68 qed "def_bin_rec_Bcons";
    69 
    69 
    70 fun bin_recs def = map standard
    70 fun bin_recs def = map standard
    71 	([def] RL [def_bin_rec_Plus, def_bin_rec_Minus, def_bin_rec_Bcons]);
    71 	([def] RL [def_bin_rec_Plus, def_bin_rec_Minus, def_bin_rec_Bcons]);
       
    72 
       
    73 goalw Bin.thy [norm_Bcons_def] "norm_Bcons(Plus,0) = Plus";
       
    74 by (asm_simp_tac (ZF_ss addsimps (bin.case_eqns @ bool_simps)) 1);
       
    75 qed "norm_Bcons_Plus_0";
       
    76 
       
    77 goalw Bin.thy [norm_Bcons_def] "norm_Bcons(Plus,1) = Bcons(Plus,1)";
       
    78 by (asm_simp_tac (ZF_ss addsimps (bin.case_eqns @ bool_simps)) 1);
       
    79 qed "norm_Bcons_Plus_1";
       
    80 
       
    81 goalw Bin.thy [norm_Bcons_def] "norm_Bcons(Minus,0) = Bcons(Minus,0)";
       
    82 by (asm_simp_tac (ZF_ss addsimps (bin.case_eqns @ bool_simps)) 1);
       
    83 qed "norm_Bcons_Minus_0";
       
    84 
       
    85 goalw Bin.thy [norm_Bcons_def] "norm_Bcons(Minus,1) = Minus";
       
    86 by (asm_simp_tac (ZF_ss addsimps (bin.case_eqns @ bool_simps)) 1);
       
    87 qed "norm_Bcons_Minus_1";
       
    88 
       
    89 goalw Bin.thy [norm_Bcons_def]
       
    90     "norm_Bcons(Bcons(w,x),b) = Bcons(Bcons(w,x),b)";
       
    91 by (asm_simp_tac (ZF_ss addsimps bin.case_eqns) 1);
       
    92 qed "norm_Bcons_Bcons";
       
    93 
       
    94 val norm_Bcons_simps = [norm_Bcons_Plus_0, norm_Bcons_Plus_1, 
       
    95 			norm_Bcons_Minus_0, norm_Bcons_Minus_1,
       
    96 			norm_Bcons_Bcons];
    72 
    97 
    73 (** Type checking **)
    98 (** Type checking **)
    74 
    99 
    75 val bin_typechecks0 = bin_rec_type :: bin.intrs;
   100 val bin_typechecks0 = bin_rec_type :: bin.intrs;
    76 
   101 
    78     "!!w. w: bin ==> integ_of_bin(w) : integ";
   103     "!!w. w: bin ==> integ_of_bin(w) : integ";
    79 by (typechk_tac (bin_typechecks0@integ_typechecks@
   104 by (typechk_tac (bin_typechecks0@integ_typechecks@
    80 		 nat_typechecks@[bool_into_nat]));
   105 		 nat_typechecks@[bool_into_nat]));
    81 qed "integ_of_bin_type";
   106 qed "integ_of_bin_type";
    82 
   107 
       
   108 goalw Bin.thy [norm_Bcons_def]
       
   109     "!!w. [| w: bin; b: bool |] ==> norm_Bcons(w,b) : bin";
       
   110 by (etac bin.elim 1);
       
   111 by (ALLGOALS (asm_simp_tac (ZF_ss addsimps bin.case_eqns)));
       
   112 by (typechk_tac (bin_typechecks0@bool_typechecks));
       
   113 qed "norm_Bcons_type";
       
   114 
    83 goalw Bin.thy [bin_succ_def]
   115 goalw Bin.thy [bin_succ_def]
    84     "!!w. w: bin ==> bin_succ(w) : bin";
   116     "!!w. w: bin ==> bin_succ(w) : bin";
    85 by (typechk_tac (bin_typechecks0@bool_typechecks));
   117 by (typechk_tac ([norm_Bcons_type]@bin_typechecks0@bool_typechecks));
    86 qed "bin_succ_type";
   118 qed "bin_succ_type";
    87 
   119 
    88 goalw Bin.thy [bin_pred_def]
   120 goalw Bin.thy [bin_pred_def]
    89     "!!w. w: bin ==> bin_pred(w) : bin";
   121     "!!w. w: bin ==> bin_pred(w) : bin";
    90 by (typechk_tac (bin_typechecks0@bool_typechecks));
   122 by (typechk_tac ([norm_Bcons_type]@bin_typechecks0@bool_typechecks));
    91 qed "bin_pred_type";
   123 qed "bin_pred_type";
    92 
   124 
    93 goalw Bin.thy [bin_minus_def]
   125 goalw Bin.thy [bin_minus_def]
    94     "!!w. w: bin ==> bin_minus(w) : bin";
   126     "!!w. w: bin ==> bin_minus(w) : bin";
    95 by (typechk_tac ([bin_pred_type]@bin_typechecks0@bool_typechecks));
   127 by (typechk_tac ([bin_pred_type]@bin_typechecks0@bool_typechecks));
    96 qed "bin_minus_type";
   128 qed "bin_minus_type";
    97 
   129 
    98 goalw Bin.thy [bin_add_def]
   130 goalw Bin.thy [bin_add_def]
    99     "!!v w. [| v: bin; w: bin |] ==> bin_add(v,w) : bin";
   131     "!!v w. [| v: bin; w: bin |] ==> bin_add(v,w) : bin";
   100 by (typechk_tac ([bin_succ_type,bin_pred_type]@bin_typechecks0@
   132 by (typechk_tac ([norm_Bcons_type, bin_succ_type, bin_pred_type]@
   101 		 bool_typechecks@ZF_typechecks));
   133 		 bin_typechecks0@ bool_typechecks@ZF_typechecks));
   102 qed "bin_add_type";
   134 qed "bin_add_type";
   103 
   135 
   104 goalw Bin.thy [bin_mult_def]
   136 goalw Bin.thy [bin_mult_def]
   105     "!!v w. [| v: bin; w: bin |] ==> bin_mult(v,w) : bin";
   137     "!!v w. [| v: bin; w: bin |] ==> bin_mult(v,w) : bin";
   106 by (typechk_tac ([bin_minus_type,bin_add_type]@bin_typechecks0@
   138 by (typechk_tac ([norm_Bcons_type, bin_minus_type, bin_add_type]@
   107 		 bool_typechecks));
   139 		 bin_typechecks0@ bool_typechecks));
   108 qed "bin_mult_type";
   140 qed "bin_mult_type";
   109 
   141 
   110 val bin_typechecks = bin_typechecks0 @
   142 val bin_typechecks = bin_typechecks0 @
   111     [integ_of_bin_type, bin_succ_type, bin_pred_type, 
   143     [integ_of_bin_type, norm_Bcons_type, bin_succ_type, bin_pred_type, 
   112      bin_minus_type, bin_add_type, bin_mult_type];
   144      bin_minus_type, bin_add_type, bin_mult_type];
   113 
   145 
   114 val bin_ss = integ_ss 
   146 val bin_ss = integ_ss 
   115     addsimps([bool_1I, bool_0I,
   147     addsimps([bool_1I, bool_0I,
   116 	     bin_rec_Plus, bin_rec_Minus, bin_rec_Bcons] @ 
   148 	     bin_rec_Plus, bin_rec_Minus, bin_rec_Bcons] @ 
   134     "!!z v w. [| z: integ;  v: integ;  w: integ |]   \
   166     "!!z v w. [| z: integ;  v: integ;  w: integ |]   \
   135 \    ==> z $+ (v $+ w) = v $+ (z $+ w)";
   167 \    ==> z $+ (v $+ w) = v $+ (z $+ w)";
   136 by (REPEAT (ares_tac [zadd_commute RS zadd_assoc_cong] 1));
   168 by (REPEAT (ares_tac [zadd_commute RS zadd_assoc_cong] 1));
   137 qed "zadd_assoc_swap";
   169 qed "zadd_assoc_swap";
   138 
   170 
   139 val zadd_cong = 
       
   140     read_instantiate_sg (sign_of Integ.thy) [("t","op $+")] subst_context2;
       
   141 
       
   142 val zadd_kill = (refl RS zadd_cong);
       
   143 val zadd_assoc_swap_kill = zadd_kill RSN (4, zadd_assoc_swap RS trans);
       
   144 
       
   145 (*Pushes 'constants' of the form $#m to the right -- LOOPS if two!*)
   171 (*Pushes 'constants' of the form $#m to the right -- LOOPS if two!*)
   146 bind_thm ("zadd_assoc_znat", (znat_type RS zadd_assoc_swap));
   172 bind_thm ("zadd_assoc_znat", (znat_type RS zadd_assoc_swap));
   147 
   173 
   148 goal Integ.thy 
       
   149     "!!z w. [| z: integ;  w: integ |]   \
       
   150 \    ==> w $+ (z $+ (w $+ z)) = w $+ (w $+ (z $+ z))";
       
   151 by (REPEAT (ares_tac [zadd_kill, zadd_assoc_swap] 1));
       
   152 qed "zadd_swap_pairs";
       
   153 
       
   154 
   174 
   155 val carry_ss = bin_ss addsimps 
   175 val carry_ss = bin_ss addsimps 
   156                (bin_recs bin_succ_def @ bin_recs bin_pred_def);
   176                (bin_recs bin_succ_def @ bin_recs bin_pred_def);
   157 
   177 
       
   178 
       
   179 (*norm_Bcons preserves the integer value of its argument*)
       
   180 goal Bin.thy
       
   181     "!!w. [| w: bin; b: bool |] ==>	\
       
   182 \         integ_of_bin(norm_Bcons(w,b)) = integ_of_bin(Bcons(w,b))";
       
   183 by (etac bin.elim 1);
       
   184 by (asm_simp_tac (ZF_ss addsimps norm_Bcons_simps) 3);
       
   185 by (ALLGOALS (etac boolE));
       
   186 by (ALLGOALS (asm_simp_tac (bin_ss addsimps (norm_Bcons_simps))));
       
   187 qed "integ_of_bin_norm_Bcons";
       
   188 
   158 goal Bin.thy
   189 goal Bin.thy
   159     "!!w. w: bin ==> integ_of_bin(bin_succ(w)) = $#1 $+ integ_of_bin(w)";
   190     "!!w. w: bin ==> integ_of_bin(bin_succ(w)) = $#1 $+ integ_of_bin(w)";
   160 by (etac bin.induct 1);
   191 by (etac bin.induct 1);
   161 by (simp_tac (carry_ss addsimps [zadd_0_right]) 1);
   192 by (simp_tac carry_ss 1);
   162 by (simp_tac (carry_ss addsimps [zadd_zminus_inverse]) 1);
   193 by (simp_tac carry_ss 1);
   163 by (etac boolE 1);
   194 by (etac boolE 1);
   164 by (ALLGOALS (asm_simp_tac (carry_ss addsimps [zadd_assoc])));
   195 by (ALLGOALS
   165 by (REPEAT (ares_tac (zadd_swap_pairs::typechecks) 1));
   196     (asm_simp_tac (carry_ss addsimps integ_of_bin_norm_Bcons::zadd_ac)));
   166 qed "integ_of_bin_succ";
   197 qed "integ_of_bin_succ";
   167 
   198 
   168 goal Bin.thy
   199 goal Bin.thy
   169     "!!w. w: bin ==> integ_of_bin(bin_pred(w)) = $~ ($#1) $+ integ_of_bin(w)";
   200     "!!w. w: bin ==> integ_of_bin(bin_pred(w)) = $~ ($#1) $+ integ_of_bin(w)";
   170 by (etac bin.induct 1);
   201 by (etac bin.induct 1);
   171 by (simp_tac (carry_ss addsimps [zadd_0_right]) 1);
   202 by (simp_tac carry_ss 1);
   172 by (simp_tac (carry_ss addsimps [zadd_zminus_inverse]) 1);
   203 by (simp_tac carry_ss 1);
   173 by (etac boolE 1);
   204 by (etac boolE 1);
   174 by (ALLGOALS 
   205 by (ALLGOALS
   175     (asm_simp_tac 
   206     (asm_simp_tac (carry_ss addsimps integ_of_bin_norm_Bcons::zadd_ac)));
   176      (carry_ss addsimps [zadd_assoc RS sym,
       
   177 			zadd_zminus_inverse, zadd_zminus_inverse2])));
       
   178 by (REPEAT (ares_tac ([zadd_commute, zadd_cong, refl]@typechecks) 1));
       
   179 qed "integ_of_bin_pred";
   207 qed "integ_of_bin_pred";
   180 
   208 
   181 (*These two results replace the definitions of bin_succ and bin_pred*)
   209 (*These two results replace the definitions of bin_succ and bin_pred*)
   182 
   210 
   183 
   211 
   188 		    [integ_of_bin_succ, integ_of_bin_pred]);
   216 		    [integ_of_bin_succ, integ_of_bin_pred]);
   189 
   217 
   190 goal Bin.thy
   218 goal Bin.thy
   191     "!!w. w: bin ==> integ_of_bin(bin_minus(w)) = $~ integ_of_bin(w)";
   219     "!!w. w: bin ==> integ_of_bin(bin_minus(w)) = $~ integ_of_bin(w)";
   192 by (etac bin.induct 1);
   220 by (etac bin.induct 1);
   193 by (simp_tac (bin_minus_ss addsimps [zminus_0]) 1);
   221 by (simp_tac bin_minus_ss 1);
   194 by (simp_tac (bin_minus_ss addsimps [zadd_0_right]) 1);
   222 by (simp_tac bin_minus_ss 1);
   195 by (etac boolE 1);
   223 by (etac boolE 1);
   196 by (ALLGOALS 
   224 by (ALLGOALS 
   197     (asm_simp_tac (bin_minus_ss addsimps [zminus_zadd_distrib, zadd_assoc])));
   225     (asm_simp_tac (bin_minus_ss addsimps [zminus_zadd_distrib, zadd_assoc])));
   198 qed "integ_of_bin_minus";
   226 qed "integ_of_bin_minus";
   199 
   227 
   206 
   234 
   207 goalw Bin.thy [bin_add_def] "!!w. w: bin ==> bin_add(Minus,w) = bin_pred(w)";
   235 goalw Bin.thy [bin_add_def] "!!w. w: bin ==> bin_add(Minus,w) = bin_pred(w)";
   208 by (asm_simp_tac bin_ss 1);
   236 by (asm_simp_tac bin_ss 1);
   209 qed "bin_add_Minus";
   237 qed "bin_add_Minus";
   210 
   238 
   211 goalw Bin.thy [bin_add_def] "bin_add(v$$x,Plus) = v$$x";
   239 goalw Bin.thy [bin_add_def] "bin_add(Bcons(v,x),Plus) = Bcons(v,x)";
   212 by (simp_tac bin_ss 1);
   240 by (simp_tac bin_ss 1);
   213 qed "bin_add_Bcons_Plus";
   241 qed "bin_add_Bcons_Plus";
   214 
   242 
   215 goalw Bin.thy [bin_add_def] "bin_add(v$$x,Minus) = bin_pred(v$$x)";
   243 goalw Bin.thy [bin_add_def] "bin_add(Bcons(v,x),Minus) = bin_pred(Bcons(v,x))";
   216 by (simp_tac bin_ss 1);
   244 by (simp_tac bin_ss 1);
   217 qed "bin_add_Bcons_Minus";
   245 qed "bin_add_Bcons_Minus";
   218 
   246 
   219 goalw Bin.thy [bin_add_def]
   247 goalw Bin.thy [bin_add_def]
   220     "!!w y. [| w: bin;  y: bool |] ==> \
   248     "!!w y. [| w: bin;  y: bool |] ==> \
   221 \           bin_add(v$$x, w$$y) = \
   249 \           bin_add(Bcons(v,x), Bcons(w,y)) = \
   222 \           bin_add(v, cond(x and y, bin_succ(w), w)) $$ (x xor y)";
   250 \           norm_Bcons(bin_add(v, cond(x and y, bin_succ(w), w)), x xor y)";
   223 by (asm_simp_tac bin_ss 1);
   251 by (asm_simp_tac bin_ss 1);
   224 qed "bin_add_Bcons_Bcons";
   252 qed "bin_add_Bcons_Bcons";
   225 
   253 
   226 val bin_add_rews = [bin_add_Plus, bin_add_Minus, bin_add_Bcons_Plus,
   254 val bin_add_simps = [bin_add_Plus, bin_add_Minus, bin_add_Bcons_Plus,
   227 		    bin_add_Bcons_Minus, bin_add_Bcons_Bcons,
   255 		     bin_add_Bcons_Minus, bin_add_Bcons_Bcons,
   228 		    integ_of_bin_succ, integ_of_bin_pred];
   256 		     integ_of_bin_succ, integ_of_bin_pred,
   229 
   257 		     integ_of_bin_norm_Bcons];
   230 val bin_add_ss = bin_ss addsimps ([bool_subset_nat RS subsetD] @ bin_add_rews);
   258 
       
   259 val bin_add_ss = 
       
   260     bin_ss addsimps ([bool_subset_nat RS subsetD] @ bin_add_simps);
   231 
   261 
   232 goal Bin.thy
   262 goal Bin.thy
   233     "!!v. v: bin ==> \
   263     "!!v. v: bin ==> \
   234 \         ALL w: bin. integ_of_bin(bin_add(v,w)) = \
   264 \         ALL w: bin. integ_of_bin(bin_add(v,w)) = \
   235 \                     integ_of_bin(v) $+ integ_of_bin(w)";
   265 \                     integ_of_bin(v) $+ integ_of_bin(w)";
   236 by (etac bin.induct 1);
   266 by (etac bin.induct 1);
   237 by (simp_tac bin_add_ss 1);
   267 by (simp_tac bin_add_ss 1);
   238 by (simp_tac bin_add_ss 1);
   268 by (simp_tac bin_add_ss 1);
   239 by (rtac ballI 1);
   269 by (rtac ballI 1);
   240 by (bin_ind_tac "wa" [] 1);
   270 by (bin_ind_tac "wa" [] 1);
   241 by (asm_simp_tac (bin_add_ss addsimps [zadd_0_right]) 1);
       
   242 by (asm_simp_tac bin_add_ss 1);
   271 by (asm_simp_tac bin_add_ss 1);
   243 by (REPEAT (ares_tac (zadd_commute::typechecks) 1));
   272 by (asm_simp_tac (bin_add_ss addsimps zadd_ac) 1);
   244 by (etac boolE 1);
   273 by (etac boolE 1);
   245 by (asm_simp_tac (bin_add_ss addsimps [zadd_assoc, zadd_swap_pairs]) 2);
   274 by (asm_simp_tac (bin_add_ss addsimps zadd_ac) 2);
   246 by (REPEAT (ares_tac ([refl, zadd_kill, zadd_assoc_swap_kill]@typechecks) 2));
   275 by (etac boolE 1);
   247 by (etac boolE 1);
   276 by (ALLGOALS (asm_simp_tac (bin_add_ss addsimps zadd_ac)));
   248 by (ALLGOALS (asm_simp_tac (bin_add_ss addsimps [zadd_assoc,zadd_swap_pairs])));
   277 val integ_of_bin_add_lemma = result();
   249 by (REPEAT (ares_tac ([refl, zadd_kill, zadd_assoc_swap_kill RS sym]@
   278 
   250 		      typechecks) 1));
   279 bind_thm("integ_of_bin_add", integ_of_bin_add_lemma RS bspec);
   251 qed "integ_of_bin_add_lemma";
       
   252 
       
   253 val integ_of_bin_add = integ_of_bin_add_lemma RS bspec;
       
   254 
   280 
   255 
   281 
   256 (*** bin_add: binary multiplication ***)
   282 (*** bin_add: binary multiplication ***)
   257 
   283 
   258 val bin_mult_ss =
   284 val bin_mult_ss =
   259     bin_ss addsimps (bin_recs bin_mult_def @ 
   285     bin_ss addsimps (bin_recs bin_mult_def @ 
   260 		       [integ_of_bin_minus, integ_of_bin_add]);
   286 		       [integ_of_bin_minus, integ_of_bin_add,
   261 
   287 			integ_of_bin_norm_Bcons]);
   262 
   288 
   263 val major::prems = goal Bin.thy
   289 val major::prems = goal Bin.thy
   264     "[| v: bin; w: bin |] ==>	\
   290     "[| v: bin; w: bin |] ==>	\
   265 \    integ_of_bin(bin_mult(v,w)) = \
   291 \    integ_of_bin(bin_mult(v,w)) = \
   266 \    integ_of_bin(v) $* integ_of_bin(w)";
   292 \    integ_of_bin(v) $* integ_of_bin(w)";
   267 by (cut_facts_tac prems 1);
   293 by (cut_facts_tac prems 1);
   268 by (bin_ind_tac "v" [major] 1);
   294 by (bin_ind_tac "v" [major] 1);
   269 by (asm_simp_tac (bin_mult_ss addsimps [zmult_0]) 1);
   295 by (asm_simp_tac bin_mult_ss 1);
   270 by (asm_simp_tac (bin_mult_ss addsimps [zmult_1,zmult_zminus]) 1);
   296 by (asm_simp_tac bin_mult_ss 1);
   271 by (etac boolE 1);
   297 by (etac boolE 1);
   272 by (asm_simp_tac (bin_mult_ss addsimps [zadd_zmult_distrib]) 2);
   298 by (asm_simp_tac (bin_mult_ss addsimps [zadd_zmult_distrib]) 2);
   273 by (asm_simp_tac 
   299 by (asm_simp_tac 
   274     (bin_mult_ss addsimps [zadd_zmult_distrib, zmult_1, zadd_assoc]) 1);
   300     (bin_mult_ss addsimps ([zadd_zmult_distrib, zmult_1] @ zadd_ac)) 1);
   275 by (REPEAT (ares_tac ([zadd_commute, zadd_assoc_swap_kill RS sym]@
       
   276 		      typechecks) 1));
       
   277 qed "integ_of_bin_mult";
   301 qed "integ_of_bin_mult";
   278 
   302 
   279 (**** Computations ****)
   303 (**** Computations ****)
   280 
   304 
   281 (** extra rules for bin_succ, bin_pred **)
   305 (** extra rules for bin_succ, bin_pred **)
   282 
   306 
   283 val [bin_succ_Plus, bin_succ_Minus, _] = bin_recs bin_succ_def;
   307 val [bin_succ_Plus, bin_succ_Minus, _] = bin_recs bin_succ_def;
   284 val [bin_pred_Plus, bin_pred_Minus, _] = bin_recs bin_pred_def;
   308 val [bin_pred_Plus, bin_pred_Minus, _] = bin_recs bin_pred_def;
   285 
   309 
   286 goal Bin.thy "bin_succ(w$$1) = bin_succ(w) $$ 0";
   310 goal Bin.thy "bin_succ(Bcons(w,1)) = Bcons(bin_succ(w), 0)";
   287 by (simp_tac carry_ss 1);
   311 by (simp_tac carry_ss 1);
   288 qed "bin_succ_Bcons1";
   312 qed "bin_succ_Bcons1";
   289 
   313 
   290 goal Bin.thy "bin_succ(w$$0) = w$$1";
   314 goal Bin.thy "bin_succ(Bcons(w,0)) = norm_Bcons(w,1)";
   291 by (simp_tac carry_ss 1);
   315 by (simp_tac carry_ss 1);
   292 qed "bin_succ_Bcons0";
   316 qed "bin_succ_Bcons0";
   293 
   317 
   294 goal Bin.thy "bin_pred(w$$1) = w$$0";
   318 goal Bin.thy "bin_pred(Bcons(w,1)) = norm_Bcons(w,0)";
   295 by (simp_tac carry_ss 1);
   319 by (simp_tac carry_ss 1);
   296 qed "bin_pred_Bcons1";
   320 qed "bin_pred_Bcons1";
   297 
   321 
   298 goal Bin.thy "bin_pred(w$$0) = bin_pred(w) $$ 1";
   322 goal Bin.thy "bin_pred(Bcons(w,0)) = Bcons(bin_pred(w), 1)";
   299 by (simp_tac carry_ss 1);
   323 by (simp_tac carry_ss 1);
   300 qed "bin_pred_Bcons0";
   324 qed "bin_pred_Bcons0";
   301 
   325 
   302 (** extra rules for bin_minus **)
   326 (** extra rules for bin_minus **)
   303 
   327 
   304 val [bin_minus_Plus, bin_minus_Minus, _] = bin_recs bin_minus_def;
   328 val [bin_minus_Plus, bin_minus_Minus, _] = bin_recs bin_minus_def;
   305 
   329 
   306 goal Bin.thy "bin_minus(w$$1) = bin_pred(bin_minus(w) $$ 0)";
   330 goal Bin.thy "bin_minus(Bcons(w,1)) = bin_pred(Bcons(bin_minus(w), 0))";
   307 by (simp_tac bin_minus_ss 1);
   331 by (simp_tac bin_minus_ss 1);
   308 qed "bin_minus_Bcons1";
   332 qed "bin_minus_Bcons1";
   309 
   333 
   310 goal Bin.thy "bin_minus(w$$0) = bin_minus(w) $$ 0";
   334 goal Bin.thy "bin_minus(Bcons(w,0)) = Bcons(bin_minus(w), 0)";
   311 by (simp_tac bin_minus_ss 1);
   335 by (simp_tac bin_minus_ss 1);
   312 qed "bin_minus_Bcons0";
   336 qed "bin_minus_Bcons0";
   313 
   337 
   314 (** extra rules for bin_add **)
   338 (** extra rules for bin_add **)
   315 
   339 
   316 goal Bin.thy 
   340 goal Bin.thy 
   317     "!!w. w: bin ==> bin_add(v$$1, w$$1) = bin_add(v, bin_succ(w)) $$ 0";
   341     "!!w. w: bin ==> bin_add(Bcons(v,1), Bcons(w,1)) = \
       
   342 \                    norm_Bcons(bin_add(v, bin_succ(w)), 0)";
   318 by (asm_simp_tac bin_add_ss 1);
   343 by (asm_simp_tac bin_add_ss 1);
   319 qed "bin_add_Bcons_Bcons11";
   344 qed "bin_add_Bcons_Bcons11";
   320 
   345 
   321 goal Bin.thy 
   346 goal Bin.thy 
   322     "!!w. w: bin ==> bin_add(v$$1, w$$0) = bin_add(v,w) $$ 1";
   347     "!!w. w: bin ==> bin_add(Bcons(v,1), Bcons(w,0)) =  \
       
   348 \                    norm_Bcons(bin_add(v,w), 1)";
   323 by (asm_simp_tac bin_add_ss 1);
   349 by (asm_simp_tac bin_add_ss 1);
   324 qed "bin_add_Bcons_Bcons10";
   350 qed "bin_add_Bcons_Bcons10";
   325 
   351 
   326 goal Bin.thy 
   352 goal Bin.thy 
   327     "!!w y.[| w: bin;  y: bool |] ==> bin_add(v$$0, w$$y) = bin_add(v,w) $$ y";
   353     "!!w y. [| w: bin;  y: bool |] ==> \
       
   354 \           bin_add(Bcons(v,0), Bcons(w,y)) = norm_Bcons(bin_add(v,w), y)";
   328 by (asm_simp_tac bin_add_ss 1);
   355 by (asm_simp_tac bin_add_ss 1);
   329 qed "bin_add_Bcons_Bcons0";
   356 qed "bin_add_Bcons_Bcons0";
   330 
   357 
   331 (** extra rules for bin_mult **)
   358 (** extra rules for bin_mult **)
   332 
   359 
   333 val [bin_mult_Plus, bin_mult_Minus, _] = bin_recs bin_mult_def;
   360 val [bin_mult_Plus, bin_mult_Minus, _] = bin_recs bin_mult_def;
   334 
   361 
   335 goal Bin.thy "bin_mult(v$$1, w) = bin_add(bin_mult(v,w)$$0, w)";
   362 goal Bin.thy
       
   363     "bin_mult(Bcons(v,1), w) = bin_add(norm_Bcons(bin_mult(v,w),0), w)";
   336 by (simp_tac bin_mult_ss 1);
   364 by (simp_tac bin_mult_ss 1);
   337 qed "bin_mult_Bcons1";
   365 qed "bin_mult_Bcons1";
   338 
   366 
   339 goal Bin.thy "bin_mult(v$$0, w) = bin_mult(v,w)$$0";
   367 goal Bin.thy "bin_mult(Bcons(v,0), w) = norm_Bcons(bin_mult(v,w),0)";
   340 by (simp_tac bin_mult_ss 1);
   368 by (simp_tac bin_mult_ss 1);
   341 qed "bin_mult_Bcons0";
   369 qed "bin_mult_Bcons0";
   342 
   370 
   343 
   371 
   344 (*** The computation simpset ***)
   372 (*** The computation simpset ***)
   345 
   373 
   346 val bin_comp_ss = integ_ss 
   374 val bin_comp_ss = integ_ss 
   347     addsimps [bin_succ_Plus, bin_succ_Minus,
   375     addsimps [integ_of_bin_add RS sym,   (*invoke bin_add*)
   348 	     bin_succ_Bcons1, bin_succ_Bcons0,
   376 	      integ_of_bin_minus RS sym, (*invoke bin_minus*)
   349 	     bin_pred_Plus, bin_pred_Minus,
   377 	      integ_of_bin_mult RS sym,	 (*invoke bin_mult*)
   350 	     bin_pred_Bcons1, bin_pred_Bcons0,
   378 	      bin_succ_Plus, bin_succ_Minus,
   351 	     bin_minus_Plus, bin_minus_Minus,
   379 	      bin_succ_Bcons1, bin_succ_Bcons0,
   352 	     bin_minus_Bcons1, bin_minus_Bcons0,
   380 	      bin_pred_Plus, bin_pred_Minus,
   353 	     bin_add_Plus, bin_add_Minus, bin_add_Bcons_Plus, 
   381 	      bin_pred_Bcons1, bin_pred_Bcons0,
   354 	     bin_add_Bcons_Minus, bin_add_Bcons_Bcons0, 
   382 	      bin_minus_Plus, bin_minus_Minus,
   355 	     bin_add_Bcons_Bcons10, bin_add_Bcons_Bcons11,
   383 	      bin_minus_Bcons1, bin_minus_Bcons0,
   356 	     bin_mult_Plus, bin_mult_Minus,
   384 	      bin_add_Plus, bin_add_Minus, bin_add_Bcons_Plus, 
   357 	     bin_mult_Bcons1, bin_mult_Bcons0]
   385 	      bin_add_Bcons_Minus, bin_add_Bcons_Bcons0, 
       
   386 	      bin_add_Bcons_Bcons10, bin_add_Bcons_Bcons11,
       
   387 	      bin_mult_Plus, bin_mult_Minus,
       
   388 	      bin_mult_Bcons1, bin_mult_Bcons0] @
       
   389              norm_Bcons_simps
   358     setsolver (type_auto_tac ([bool_1I, bool_0I] @ bin_typechecks0));
   390     setsolver (type_auto_tac ([bool_1I, bool_0I] @ bin_typechecks0));
   359 
   391 
   360 (*** Examples of performing binary arithmetic by simplification ***)
   392 (*** Examples of performing binary arithmetic by simplification ***)
   361 
   393 
   362 proof_timing := true;
   394 proof_timing := true;
   363 (*All runtimes below are on a SPARCserver 10*)
   395 (*All runtimes below are on a SPARCserver 10*)
   364 
   396 
   365 (* 13+19 = 32 *)
   397 goal Bin.thy "#13  $+  #19 = #32";
   366 goal Bin.thy
   398 by (simp_tac bin_comp_ss 1);	(*0.4 secs*)
   367     "bin_add(Plus$$1$$1$$0$$1, Plus$$1$$0$$0$$1$$1) = Plus$$1$$0$$0$$0$$0$$0";
       
   368 by (simp_tac bin_comp_ss 1);	(*0.6 secs*)
       
   369 result();
   399 result();
   370 
   400 
   371 bin_add(binary_of_int 13, binary_of_int 19);
   401 bin_add(binary_of_int 13, binary_of_int 19);
   372 
   402 
   373 (* 1234+5678 = 6912 *)
   403 goal Bin.thy "#1234  $+  #5678 = #6912";
   374 goal Bin.thy
   404 by (simp_tac bin_comp_ss 1);	(*1.3 secs*)
   375     "bin_add(Plus$$1$$0$$0$$1$$1$$0$$1$$0$$0$$1$$0, \
       
   376 \	     Plus$$1$$0$$1$$1$$0$$0$$0$$1$$0$$1$$1$$1$$0) = \
       
   377 \    Plus$$1$$1$$0$$1$$1$$0$$0$$0$$0$$0$$0$$0$$0";
       
   378 by (simp_tac bin_comp_ss 1);	(*2.6 secs*)
       
   379 result();
   405 result();
   380 
   406 
   381 bin_add(binary_of_int 1234, binary_of_int 5678);
   407 bin_add(binary_of_int 1234, binary_of_int 5678);
   382 
   408 
   383 (* 1359-2468 = ~1109 *)
   409 goal Bin.thy "#1359  $+  #~2468 = #~1109";
   384 goal Bin.thy
   410 by (simp_tac bin_comp_ss 1);	(*1.2 secs*)
   385     "bin_add(Plus$$1$$0$$1$$0$$1$$0$$0$$1$$1$$1$$1,		\
       
   386 \	     Minus$$0$$1$$1$$0$$0$$1$$0$$1$$1$$1$$0$$0) = 	\
       
   387 \    Minus$$1$$0$$1$$1$$1$$0$$1$$0$$1$$0$$1$$1";
       
   388 by (simp_tac bin_comp_ss 1);	(*2.3 secs*)
       
   389 result();
   411 result();
   390 
   412 
   391 bin_add(binary_of_int 1359, binary_of_int ~2468);
   413 bin_add(binary_of_int 1359, binary_of_int ~2468);
   392 
   414 
   393 (* 93746-46375 = 47371 *)
   415 goal Bin.thy "#93746  $+  #~46375 = #47371";
   394 goal Bin.thy
   416 by (simp_tac bin_comp_ss 1);	(*1.9 secs*)
   395     "bin_add(Plus$$1$$0$$1$$1$$0$$1$$1$$1$$0$$0$$0$$1$$1$$0$$0$$1$$0, \
       
   396 \	     Minus$$0$$1$$0$$0$$1$$0$$1$$0$$1$$1$$0$$1$$1$$0$$0$$1) = \
       
   397 \    Plus$$0$$1$$0$$1$$1$$1$$0$$0$$1$$0$$0$$0$$0$$1$$0$$1$$1";
       
   398 by (simp_tac bin_comp_ss 1);	(*3.9 secs*)
       
   399 result();
   417 result();
   400 
   418 
   401 bin_add(binary_of_int 93746, binary_of_int ~46375);
   419 bin_add(binary_of_int 93746, binary_of_int ~46375);
   402 
   420 
   403 (* negation of 65745 *)
   421 goal Bin.thy "$~ #65745 = #~65745";
   404 goal Bin.thy
   422 by (simp_tac bin_comp_ss 1);	(*0.4 secs*)
   405     "bin_minus(Plus$$1$$0$$0$$0$$0$$0$$0$$0$$0$$1$$1$$0$$1$$0$$0$$0$$1) = \
       
   406 \    Minus$$0$$1$$1$$1$$1$$1$$1$$1$$1$$0$$0$$1$$0$$1$$1$$1$$1";
       
   407 by (simp_tac bin_comp_ss 1);	(*0.6 secs*)
       
   408 result();
   423 result();
   409 
   424 
   410 bin_minus(binary_of_int 65745);
   425 bin_minus(binary_of_int 65745);
   411 
   426 
   412 (* negation of ~54321 *)
   427 (* negation of ~54321 *)
   413 goal Bin.thy
   428 goal Bin.thy "$~ #~54321 = #54321";
   414     "bin_minus(Minus$$0$$0$$1$$0$$1$$0$$1$$1$$1$$1$$0$$0$$1$$1$$1$$1) = \
   429 by (simp_tac bin_comp_ss 1);	(*0.5 secs*)
   415 \    Plus$$0$$1$$1$$0$$1$$0$$1$$0$$0$$0$$0$$1$$1$$0$$0$$0$$1";
   430 result();
       
   431 
       
   432 bin_minus(binary_of_int ~54321);
       
   433 
       
   434 goal Bin.thy "#13  $*  #19 = #247";
   416 by (simp_tac bin_comp_ss 1);	(*0.7 secs*)
   435 by (simp_tac bin_comp_ss 1);	(*0.7 secs*)
   417 result();
   436 result();
   418 
   437 
   419 bin_minus(binary_of_int ~54321);
       
   420 
       
   421 (* 13*19 = 247 *)
       
   422 goal Bin.thy "bin_mult(Plus$$1$$1$$0$$1, Plus$$1$$0$$0$$1$$1) = \
       
   423 \               Plus$$1$$1$$1$$1$$0$$1$$1$$1";
       
   424 by (simp_tac bin_comp_ss 1);	(*1.5 secs*)
       
   425 result();
       
   426 
       
   427 bin_mult(binary_of_int 13, binary_of_int 19);
   438 bin_mult(binary_of_int 13, binary_of_int 19);
   428 
   439 
   429 (* ~84 * 51 = ~4284 *)
   440 goal Bin.thy "#~84  $*  #51 = #~4284";
   430 goal Bin.thy
   441 by (simp_tac bin_comp_ss 1);	(*1.3 secs*)
   431     "bin_mult(Minus$$0$$1$$0$$1$$1$$0$$0, Plus$$1$$1$$0$$0$$1$$1) = \
       
   432 \    Minus$$0$$1$$1$$1$$1$$0$$1$$0$$0$$0$$1$$0$$0";
       
   433 by (simp_tac bin_comp_ss 1);	(*2.6 secs*)
       
   434 result();
   442 result();
   435 
   443 
   436 bin_mult(binary_of_int ~84, binary_of_int 51);
   444 bin_mult(binary_of_int ~84, binary_of_int 51);
   437 
   445 
   438 (* 255*255 = 65025;  the worst case for 8-bit operands *)
   446 (*The worst case for 8-bit operands *)
   439 goal Bin.thy
   447 goal Bin.thy "#255  $*  #255 = #65025";
   440     "bin_mult(Plus$$1$$1$$1$$1$$1$$1$$1$$1, \
   448 by (simp_tac bin_comp_ss 1);	(*4.3 secs*)
   441 \             Plus$$1$$1$$1$$1$$1$$1$$1$$1) = \
       
   442 \        Plus$$1$$1$$1$$1$$1$$1$$1$$0$$0$$0$$0$$0$$0$$0$$0$$1";
       
   443 by (simp_tac bin_comp_ss 1);	(*9.8 secs*)
       
   444 result();
   449 result();
   445 
   450 
   446 bin_mult(binary_of_int 255, binary_of_int 255);
   451 bin_mult(binary_of_int 255, binary_of_int 255);
   447 
   452 
   448 (* 1359 * ~2468 = ~3354012 *)
   453 goal Bin.thy "#1359  $*  #~2468 = #~3354012";
   449 goal Bin.thy
   454 by (simp_tac bin_comp_ss 1);	(*6.1 secs*)
   450     "bin_mult(Plus$$1$$0$$1$$0$$1$$0$$0$$1$$1$$1$$1, 		\
       
   451 \	      Minus$$0$$1$$1$$0$$0$$1$$0$$1$$1$$1$$0$$0) = 	\
       
   452 \    Minus$$0$$0$$1$$1$$0$$0$$1$$1$$0$$1$$0$$0$$1$$0$$0$$1$$1$$0$$0$$1$$0$$0";
       
   453 by (simp_tac bin_comp_ss 1);	(*13.7 secs*)
       
   454 result();
   455 result();
   455 
   456 
   456 bin_mult(binary_of_int 1359, binary_of_int ~2468);
   457 bin_mult(binary_of_int 1359, binary_of_int ~2468);