author | paulson |
Fri, 11 Sep 1998 18:09:54 +0200 | |
changeset 5484 | e9430ed7e8d6 |
parent 5147 | 825877190618 |
child 5512 | 4327eec06849 |
permissions | -rw-r--r-- |
1461 | 1 |
(* Title: ZF/ex/Bin.ML |
0 | 2 |
ID: $Id$ |
1461 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
515 | 4 |
Copyright 1994 University of Cambridge |
0 | 5 |
|
515 | 6 |
For Bin.thy. Arithmetic on binary integers. |
0 | 7 |
*) |
8 |
||
515 | 9 |
open Bin; |
0 | 10 |
|
2469 | 11 |
Addsimps bin.case_eqns; |
12 |
||
0 | 13 |
(*Perform induction on l, then prove the major premise using prems. *) |
14 |
fun bin_ind_tac a prems i = |
|
515 | 15 |
EVERY [res_inst_tac [("x",a)] bin.induct i, |
1461 | 16 |
rename_last_tac a ["1"] (i+3), |
17 |
ares_tac prems i]; |
|
0 | 18 |
|
515 | 19 |
|
20 |
(** bin_rec -- by Vset recursion **) |
|
21 |
||
5068 | 22 |
Goal "bin_rec(Plus,a,b,h) = a"; |
515 | 23 |
by (rtac (bin_rec_def RS def_Vrec RS trans) 1); |
24 |
by (rewrite_goals_tac bin.con_defs); |
|
25 |
by (simp_tac rank_ss 1); |
|
760 | 26 |
qed "bin_rec_Plus"; |
515 | 27 |
|
5068 | 28 |
Goal "bin_rec(Minus,a,b,h) = b"; |
515 | 29 |
by (rtac (bin_rec_def RS def_Vrec RS trans) 1); |
30 |
by (rewrite_goals_tac bin.con_defs); |
|
31 |
by (simp_tac rank_ss 1); |
|
760 | 32 |
qed "bin_rec_Minus"; |
515 | 33 |
|
5068 | 34 |
Goal "bin_rec(Bcons(w,x),a,b,h) = h(w, x, bin_rec(w,a,b,h))"; |
515 | 35 |
by (rtac (bin_rec_def RS def_Vrec RS trans) 1); |
36 |
by (rewrite_goals_tac bin.con_defs); |
|
37 |
by (simp_tac rank_ss 1); |
|
760 | 38 |
qed "bin_rec_Bcons"; |
515 | 39 |
|
40 |
(*Type checking*) |
|
41 |
val prems = goal Bin.thy |
|
42 |
"[| w: bin; \ |
|
43 |
\ a: C(Plus); b: C(Minus); \ |
|
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
44 |
\ !!w x r. [| w: bin; x: bool; r: C(w) |] ==> h(w,x,r): C(Bcons(w,x)) \ |
515 | 45 |
\ |] ==> bin_rec(w,a,b,h) : C(w)"; |
46 |
by (bin_ind_tac "w" prems 1); |
|
47 |
by (ALLGOALS |
|
4091 | 48 |
(asm_simp_tac (simpset() addsimps (prems@[bin_rec_Plus, bin_rec_Minus, |
1461 | 49 |
bin_rec_Bcons])))); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
50 |
qed "bin_rec_type"; |
515 | 51 |
|
52 |
(** Versions for use with definitions **) |
|
53 |
||
54 |
val [rew] = goal Bin.thy |
|
55 |
"[| !!w. j(w)==bin_rec(w,a,b,h) |] ==> j(Plus) = a"; |
|
56 |
by (rewtac rew); |
|
57 |
by (rtac bin_rec_Plus 1); |
|
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
58 |
qed "def_bin_rec_Plus"; |
515 | 59 |
|
60 |
val [rew] = goal Bin.thy |
|
61 |
"[| !!w. j(w)==bin_rec(w,a,b,h) |] ==> j(Minus) = b"; |
|
62 |
by (rewtac rew); |
|
63 |
by (rtac bin_rec_Minus 1); |
|
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
64 |
qed "def_bin_rec_Minus"; |
515 | 65 |
|
66 |
val [rew] = goal Bin.thy |
|
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
67 |
"[| !!w. j(w)==bin_rec(w,a,b,h) |] ==> j(Bcons(w,x)) = h(w,x,j(w))"; |
515 | 68 |
by (rewtac rew); |
69 |
by (rtac bin_rec_Bcons 1); |
|
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
70 |
qed "def_bin_rec_Bcons"; |
515 | 71 |
|
72 |
fun bin_recs def = map standard |
|
1461 | 73 |
([def] RL [def_bin_rec_Plus, def_bin_rec_Minus, def_bin_rec_Bcons]); |
515 | 74 |
|
5068 | 75 |
Goalw [norm_Bcons_def] "norm_Bcons(Plus,0) = Plus"; |
2469 | 76 |
by (Asm_simp_tac 1); |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
77 |
qed "norm_Bcons_Plus_0"; |
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
78 |
|
5068 | 79 |
Goalw [norm_Bcons_def] "norm_Bcons(Plus,1) = Bcons(Plus,1)"; |
2469 | 80 |
by (Asm_simp_tac 1); |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
81 |
qed "norm_Bcons_Plus_1"; |
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
82 |
|
5068 | 83 |
Goalw [norm_Bcons_def] "norm_Bcons(Minus,0) = Bcons(Minus,0)"; |
2469 | 84 |
by (Asm_simp_tac 1); |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
85 |
qed "norm_Bcons_Minus_0"; |
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
86 |
|
5068 | 87 |
Goalw [norm_Bcons_def] "norm_Bcons(Minus,1) = Minus"; |
2469 | 88 |
by (Asm_simp_tac 1); |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
89 |
qed "norm_Bcons_Minus_1"; |
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
90 |
|
5068 | 91 |
Goalw [norm_Bcons_def] |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
92 |
"norm_Bcons(Bcons(w,x),b) = Bcons(Bcons(w,x),b)"; |
4091 | 93 |
by (asm_simp_tac (simpset() addsimps bin.case_eqns) 1); |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
94 |
qed "norm_Bcons_Bcons"; |
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
95 |
|
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
96 |
val norm_Bcons_simps = [norm_Bcons_Plus_0, norm_Bcons_Plus_1, |
1461 | 97 |
norm_Bcons_Minus_0, norm_Bcons_Minus_1, |
98 |
norm_Bcons_Bcons]; |
|
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
99 |
|
515 | 100 |
(** Type checking **) |
101 |
||
102 |
val bin_typechecks0 = bin_rec_type :: bin.intrs; |
|
103 |
||
5068 | 104 |
Goalw [integ_of_bin_def] |
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
105 |
"w: bin ==> integ_of_bin(w) : integ"; |
515 | 106 |
by (typechk_tac (bin_typechecks0@integ_typechecks@ |
1461 | 107 |
nat_typechecks@[bool_into_nat])); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
108 |
qed "integ_of_bin_type"; |
515 | 109 |
|
5068 | 110 |
Goalw [norm_Bcons_def] |
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
111 |
"[| w: bin; b: bool |] ==> norm_Bcons(w,b) : bin"; |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
112 |
by (etac bin.elim 1); |
4091 | 113 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps bin.case_eqns))); |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
114 |
by (typechk_tac (bin_typechecks0@bool_typechecks)); |
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
115 |
qed "norm_Bcons_type"; |
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
116 |
|
5068 | 117 |
Goalw [bin_succ_def] |
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
118 |
"w: bin ==> bin_succ(w) : bin"; |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
119 |
by (typechk_tac ([norm_Bcons_type]@bin_typechecks0@bool_typechecks)); |
760 | 120 |
qed "bin_succ_type"; |
515 | 121 |
|
5068 | 122 |
Goalw [bin_pred_def] |
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
123 |
"w: bin ==> bin_pred(w) : bin"; |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
124 |
by (typechk_tac ([norm_Bcons_type]@bin_typechecks0@bool_typechecks)); |
760 | 125 |
qed "bin_pred_type"; |
515 | 126 |
|
5068 | 127 |
Goalw [bin_minus_def] |
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
128 |
"w: bin ==> bin_minus(w) : bin"; |
515 | 129 |
by (typechk_tac ([bin_pred_type]@bin_typechecks0@bool_typechecks)); |
760 | 130 |
qed "bin_minus_type"; |
515 | 131 |
|
5068 | 132 |
Goalw [bin_add_def] |
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
133 |
"[| v: bin; w: bin |] ==> bin_add(v,w) : bin"; |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
134 |
by (typechk_tac ([norm_Bcons_type, bin_succ_type, bin_pred_type]@ |
1461 | 135 |
bin_typechecks0@ bool_typechecks@ZF_typechecks)); |
760 | 136 |
qed "bin_add_type"; |
515 | 137 |
|
5068 | 138 |
Goalw [bin_mult_def] |
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
139 |
"[| v: bin; w: bin |] ==> bin_mult(v,w) : bin"; |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
140 |
by (typechk_tac ([norm_Bcons_type, bin_minus_type, bin_add_type]@ |
1461 | 141 |
bin_typechecks0@ bool_typechecks)); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
142 |
qed "bin_mult_type"; |
515 | 143 |
|
144 |
val bin_typechecks = bin_typechecks0 @ |
|
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
145 |
[integ_of_bin_type, norm_Bcons_type, bin_succ_type, bin_pred_type, |
515 | 146 |
bin_minus_type, bin_add_type, bin_mult_type]; |
147 |
||
2469 | 148 |
Addsimps ([bool_1I, bool_0I, |
149 |
bin_rec_Plus, bin_rec_Minus, bin_rec_Bcons] @ |
|
150 |
bin_recs integ_of_bin_def @ bin_typechecks); |
|
515 | 151 |
|
152 |
val typechecks = bin_typechecks @ integ_typechecks @ nat_typechecks @ |
|
153 |
[bool_subset_nat RS subsetD]; |
|
154 |
||
155 |
(**** The carry/borrow functions, bin_succ and bin_pred ****) |
|
156 |
||
157 |
(** Lemmas **) |
|
158 |
||
159 |
goal Integ.thy |
|
160 |
"!!z v. [| z $+ v = z' $+ v'; \ |
|
161 |
\ z: integ; z': integ; v: integ; v': integ; w: integ |] \ |
|
162 |
\ ==> z $+ (v $+ w) = z' $+ (v' $+ w)"; |
|
4091 | 163 |
by (asm_simp_tac (simpset() addsimps ([zadd_assoc RS sym])) 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
164 |
qed "zadd_assoc_cong"; |
515 | 165 |
|
166 |
goal Integ.thy |
|
167 |
"!!z v w. [| z: integ; v: integ; w: integ |] \ |
|
168 |
\ ==> z $+ (v $+ w) = v $+ (z $+ w)"; |
|
169 |
by (REPEAT (ares_tac [zadd_commute RS zadd_assoc_cong] 1)); |
|
760 | 170 |
qed "zadd_assoc_swap"; |
515 | 171 |
|
172 |
(*Pushes 'constants' of the form $#m to the right -- LOOPS if two!*) |
|
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
173 |
bind_thm ("zadd_assoc_znat", (znat_type RS zadd_assoc_swap)); |
515 | 174 |
|
175 |
||
2469 | 176 |
Addsimps (bin_recs bin_succ_def @ bin_recs bin_pred_def); |
515 | 177 |
|
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
178 |
|
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
179 |
(*norm_Bcons preserves the integer value of its argument*) |
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
180 |
Goal "[| w: bin; b: bool |] ==> \ |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
181 |
\ integ_of_bin(norm_Bcons(w,b)) = integ_of_bin(Bcons(w,b))"; |
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
182 |
by (etac bin.elim 1); |
4091 | 183 |
by (asm_simp_tac (simpset() addsimps norm_Bcons_simps) 3); |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
184 |
by (ALLGOALS (etac boolE)); |
4091 | 185 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps (norm_Bcons_simps)))); |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
186 |
qed "integ_of_bin_norm_Bcons"; |
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
187 |
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
188 |
Goal "w: bin ==> integ_of_bin(bin_succ(w)) = $#1 $+ integ_of_bin(w)"; |
515 | 189 |
by (etac bin.induct 1); |
2469 | 190 |
by (Simp_tac 1); |
191 |
by (Simp_tac 1); |
|
515 | 192 |
by (etac boolE 1); |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
193 |
by (ALLGOALS |
4091 | 194 |
(asm_simp_tac (simpset() addsimps integ_of_bin_norm_Bcons::zadd_ac))); |
760 | 195 |
qed "integ_of_bin_succ"; |
515 | 196 |
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
197 |
Goal "w: bin ==> integ_of_bin(bin_pred(w)) = $~ ($#1) $+ integ_of_bin(w)"; |
515 | 198 |
by (etac bin.induct 1); |
2469 | 199 |
by (Simp_tac 1); |
200 |
by (Simp_tac 1); |
|
515 | 201 |
by (etac boolE 1); |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
202 |
by (ALLGOALS |
4091 | 203 |
(asm_simp_tac (simpset() addsimps integ_of_bin_norm_Bcons::zadd_ac))); |
760 | 204 |
qed "integ_of_bin_pred"; |
515 | 205 |
|
206 |
(*These two results replace the definitions of bin_succ and bin_pred*) |
|
207 |
||
208 |
||
209 |
(*** bin_minus: (unary!) negation of binary integers ***) |
|
210 |
||
2469 | 211 |
Addsimps (bin_recs bin_minus_def @ |
212 |
[integ_of_bin_succ, integ_of_bin_pred]); |
|
515 | 213 |
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
214 |
Goal "w: bin ==> integ_of_bin(bin_minus(w)) = $~ integ_of_bin(w)"; |
515 | 215 |
by (etac bin.induct 1); |
2469 | 216 |
by (Simp_tac 1); |
217 |
by (Simp_tac 1); |
|
515 | 218 |
by (etac boolE 1); |
219 |
by (ALLGOALS |
|
4091 | 220 |
(asm_simp_tac (simpset() addsimps (zadd_ac@[zminus_zadd_distrib])))); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
221 |
qed "integ_of_bin_minus"; |
515 | 222 |
|
223 |
||
224 |
(*** bin_add: binary addition ***) |
|
225 |
||
5137 | 226 |
Goalw [bin_add_def] "w: bin ==> bin_add(Plus,w) = w"; |
2469 | 227 |
by (Asm_simp_tac 1); |
760 | 228 |
qed "bin_add_Plus"; |
515 | 229 |
|
5137 | 230 |
Goalw [bin_add_def] "w: bin ==> bin_add(Minus,w) = bin_pred(w)"; |
2469 | 231 |
by (Asm_simp_tac 1); |
760 | 232 |
qed "bin_add_Minus"; |
515 | 233 |
|
5068 | 234 |
Goalw [bin_add_def] "bin_add(Bcons(v,x),Plus) = Bcons(v,x)"; |
2469 | 235 |
by (Simp_tac 1); |
760 | 236 |
qed "bin_add_Bcons_Plus"; |
515 | 237 |
|
5068 | 238 |
Goalw [bin_add_def] "bin_add(Bcons(v,x),Minus) = bin_pred(Bcons(v,x))"; |
2469 | 239 |
by (Simp_tac 1); |
760 | 240 |
qed "bin_add_Bcons_Minus"; |
515 | 241 |
|
5068 | 242 |
Goalw [bin_add_def] |
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
243 |
"[| w: bin; y: bool |] ==> \ |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
244 |
\ bin_add(Bcons(v,x), Bcons(w,y)) = \ |
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
245 |
\ norm_Bcons(bin_add(v, cond(x and y, bin_succ(w), w)), x xor y)"; |
2469 | 246 |
by (Asm_simp_tac 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
247 |
qed "bin_add_Bcons_Bcons"; |
515 | 248 |
|
2469 | 249 |
Addsimps [bin_add_Plus, bin_add_Minus, bin_add_Bcons_Plus, |
250 |
bin_add_Bcons_Minus, bin_add_Bcons_Bcons, |
|
251 |
integ_of_bin_succ, integ_of_bin_pred, |
|
252 |
integ_of_bin_norm_Bcons]; |
|
515 | 253 |
|
2469 | 254 |
Addsimps [bool_subset_nat RS subsetD]; |
515 | 255 |
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
256 |
Goal "v: bin ==> \ |
515 | 257 |
\ ALL w: bin. integ_of_bin(bin_add(v,w)) = \ |
258 |
\ integ_of_bin(v) $+ integ_of_bin(w)"; |
|
259 |
by (etac bin.induct 1); |
|
2469 | 260 |
by (Simp_tac 1); |
261 |
by (Simp_tac 1); |
|
515 | 262 |
by (rtac ballI 1); |
263 |
by (bin_ind_tac "wa" [] 1); |
|
4091 | 264 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps zadd_ac setloop (etac boolE)))); |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
265 |
val integ_of_bin_add_lemma = result(); |
515 | 266 |
|
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
267 |
bind_thm("integ_of_bin_add", integ_of_bin_add_lemma RS bspec); |
515 | 268 |
|
269 |
||
270 |
(*** bin_add: binary multiplication ***) |
|
271 |
||
2469 | 272 |
Addsimps (bin_recs bin_mult_def @ |
273 |
[integ_of_bin_minus, integ_of_bin_add, |
|
274 |
integ_of_bin_norm_Bcons]); |
|
515 | 275 |
|
276 |
val major::prems = goal Bin.thy |
|
1461 | 277 |
"[| v: bin; w: bin |] ==> \ |
515 | 278 |
\ integ_of_bin(bin_mult(v,w)) = \ |
279 |
\ integ_of_bin(v) $* integ_of_bin(w)"; |
|
280 |
by (cut_facts_tac prems 1); |
|
281 |
by (bin_ind_tac "v" [major] 1); |
|
2469 | 282 |
by (Asm_simp_tac 1); |
283 |
by (Asm_simp_tac 1); |
|
515 | 284 |
by (etac boolE 1); |
4091 | 285 |
by (asm_simp_tac (simpset() addsimps [zadd_zmult_distrib]) 2); |
515 | 286 |
by (asm_simp_tac |
4091 | 287 |
(simpset() addsimps ([zadd_zmult_distrib, zmult_1] @ zadd_ac)) 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
288 |
qed "integ_of_bin_mult"; |
515 | 289 |
|
290 |
(**** Computations ****) |
|
291 |
||
292 |
(** extra rules for bin_succ, bin_pred **) |
|
293 |
||
294 |
val [bin_succ_Plus, bin_succ_Minus, _] = bin_recs bin_succ_def; |
|
295 |
val [bin_pred_Plus, bin_pred_Minus, _] = bin_recs bin_pred_def; |
|
296 |
||
5068 | 297 |
Goal "bin_succ(Bcons(w,1)) = Bcons(bin_succ(w), 0)"; |
2469 | 298 |
by (Simp_tac 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
299 |
qed "bin_succ_Bcons1"; |
515 | 300 |
|
5068 | 301 |
Goal "bin_succ(Bcons(w,0)) = norm_Bcons(w,1)"; |
2469 | 302 |
by (Simp_tac 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
303 |
qed "bin_succ_Bcons0"; |
515 | 304 |
|
5068 | 305 |
Goal "bin_pred(Bcons(w,1)) = norm_Bcons(w,0)"; |
2469 | 306 |
by (Simp_tac 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
307 |
qed "bin_pred_Bcons1"; |
515 | 308 |
|
5068 | 309 |
Goal "bin_pred(Bcons(w,0)) = Bcons(bin_pred(w), 1)"; |
2469 | 310 |
by (Simp_tac 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
311 |
qed "bin_pred_Bcons0"; |
515 | 312 |
|
313 |
(** extra rules for bin_minus **) |
|
314 |
||
315 |
val [bin_minus_Plus, bin_minus_Minus, _] = bin_recs bin_minus_def; |
|
316 |
||
5068 | 317 |
Goal "bin_minus(Bcons(w,1)) = bin_pred(Bcons(bin_minus(w), 0))"; |
2469 | 318 |
by (Simp_tac 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
319 |
qed "bin_minus_Bcons1"; |
515 | 320 |
|
5068 | 321 |
Goal "bin_minus(Bcons(w,0)) = Bcons(bin_minus(w), 0)"; |
2469 | 322 |
by (Simp_tac 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
323 |
qed "bin_minus_Bcons0"; |
515 | 324 |
|
325 |
(** extra rules for bin_add **) |
|
326 |
||
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
327 |
Goal "w: bin ==> bin_add(Bcons(v,1), Bcons(w,1)) = \ |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
328 |
\ norm_Bcons(bin_add(v, bin_succ(w)), 0)"; |
2469 | 329 |
by (Asm_simp_tac 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
330 |
qed "bin_add_Bcons_Bcons11"; |
515 | 331 |
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
332 |
Goal "w: bin ==> bin_add(Bcons(v,1), Bcons(w,0)) = \ |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
333 |
\ norm_Bcons(bin_add(v,w), 1)"; |
2469 | 334 |
by (Asm_simp_tac 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
335 |
qed "bin_add_Bcons_Bcons10"; |
515 | 336 |
|
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
337 |
Goal "[| w: bin; y: bool |] ==> \ |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
338 |
\ bin_add(Bcons(v,0), Bcons(w,y)) = norm_Bcons(bin_add(v,w), y)"; |
2469 | 339 |
by (Asm_simp_tac 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
340 |
qed "bin_add_Bcons_Bcons0"; |
515 | 341 |
|
342 |
(** extra rules for bin_mult **) |
|
343 |
||
344 |
val [bin_mult_Plus, bin_mult_Minus, _] = bin_recs bin_mult_def; |
|
345 |
||
5147
825877190618
More tidying and removal of "\!\!... from Goal commands
paulson
parents:
5137
diff
changeset
|
346 |
Goal "bin_mult(Bcons(v,1), w) = bin_add(norm_Bcons(bin_mult(v,w),0), w)"; |
2469 | 347 |
by (Simp_tac 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
348 |
qed "bin_mult_Bcons1"; |
515 | 349 |
|
5068 | 350 |
Goal "bin_mult(Bcons(v,0), w) = norm_Bcons(bin_mult(v,w),0)"; |
2469 | 351 |
by (Simp_tac 1); |
782
200a16083201
added bind_thm for theorems defined by "standard ..."
clasohm
parents:
760
diff
changeset
|
352 |
qed "bin_mult_Bcons0"; |
515 | 353 |
|
354 |
||
355 |
(*** The computation simpset ***) |
|
356 |
||
4091 | 357 |
val bin_comp_ss = simpset_of Integ.thy |
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
358 |
addsimps [integ_of_bin_add RS sym, (*invoke bin_add*) |
1461 | 359 |
integ_of_bin_minus RS sym, (*invoke bin_minus*) |
360 |
integ_of_bin_mult RS sym, (*invoke bin_mult*) |
|
361 |
bin_succ_Plus, bin_succ_Minus, |
|
362 |
bin_succ_Bcons1, bin_succ_Bcons0, |
|
363 |
bin_pred_Plus, bin_pred_Minus, |
|
364 |
bin_pred_Bcons1, bin_pred_Bcons0, |
|
365 |
bin_minus_Plus, bin_minus_Minus, |
|
366 |
bin_minus_Bcons1, bin_minus_Bcons0, |
|
367 |
bin_add_Plus, bin_add_Minus, bin_add_Bcons_Plus, |
|
368 |
bin_add_Bcons_Minus, bin_add_Bcons_Bcons0, |
|
369 |
bin_add_Bcons_Bcons10, bin_add_Bcons_Bcons11, |
|
370 |
bin_mult_Plus, bin_mult_Minus, |
|
371 |
bin_mult_Bcons1, bin_mult_Bcons0] @ |
|
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
372 |
norm_Bcons_simps |
2637
e9b203f854ae
reflecting my recent changes of the simplifier and classical reasoner
oheimb
parents:
2469
diff
changeset
|
373 |
setSolver (type_auto_tac ([bool_1I, bool_0I] @ bin_typechecks0)); |
515 | 374 |
|
2469 | 375 |
|
515 | 376 |
(*** Examples of performing binary arithmetic by simplification ***) |
377 |
||
4446 | 378 |
set proof_timing; |
515 | 379 |
(*All runtimes below are on a SPARCserver 10*) |
380 |
||
5068 | 381 |
Goal "#13 $+ #19 = #32"; |
1461 | 382 |
by (simp_tac bin_comp_ss 1); (*0.4 secs*) |
515 | 383 |
result(); |
384 |
||
385 |
bin_add(binary_of_int 13, binary_of_int 19); |
|
386 |
||
5068 | 387 |
Goal "#1234 $+ #5678 = #6912"; |
1461 | 388 |
by (simp_tac bin_comp_ss 1); (*1.3 secs*) |
515 | 389 |
result(); |
390 |
||
391 |
bin_add(binary_of_int 1234, binary_of_int 5678); |
|
392 |
||
5068 | 393 |
Goal "#1359 $+ #~2468 = #~1109"; |
1461 | 394 |
by (simp_tac bin_comp_ss 1); (*1.2 secs*) |
515 | 395 |
result(); |
396 |
||
397 |
bin_add(binary_of_int 1359, binary_of_int ~2468); |
|
398 |
||
5068 | 399 |
Goal "#93746 $+ #~46375 = #47371"; |
1461 | 400 |
by (simp_tac bin_comp_ss 1); (*1.9 secs*) |
515 | 401 |
result(); |
402 |
||
403 |
bin_add(binary_of_int 93746, binary_of_int ~46375); |
|
404 |
||
5068 | 405 |
Goal "$~ #65745 = #~65745"; |
1461 | 406 |
by (simp_tac bin_comp_ss 1); (*0.4 secs*) |
515 | 407 |
result(); |
408 |
||
409 |
bin_minus(binary_of_int 65745); |
|
410 |
||
411 |
(* negation of ~54321 *) |
|
5068 | 412 |
Goal "$~ #~54321 = #54321"; |
1461 | 413 |
by (simp_tac bin_comp_ss 1); (*0.5 secs*) |
515 | 414 |
result(); |
415 |
||
416 |
bin_minus(binary_of_int ~54321); |
|
417 |
||
5068 | 418 |
Goal "#13 $* #19 = #247"; |
1461 | 419 |
by (simp_tac bin_comp_ss 1); (*0.7 secs*) |
515 | 420 |
result(); |
421 |
||
422 |
bin_mult(binary_of_int 13, binary_of_int 19); |
|
423 |
||
5068 | 424 |
Goal "#~84 $* #51 = #~4284"; |
1461 | 425 |
by (simp_tac bin_comp_ss 1); (*1.3 secs*) |
515 | 426 |
result(); |
427 |
||
428 |
bin_mult(binary_of_int ~84, binary_of_int 51); |
|
429 |
||
906
6cd9c397f36a
Redefined arithmetic to suppress redundant leading 0s and 1s
lcp
parents:
782
diff
changeset
|
430 |
(*The worst case for 8-bit operands *) |
5068 | 431 |
Goal "#255 $* #255 = #65025"; |
1461 | 432 |
by (simp_tac bin_comp_ss 1); (*4.3 secs*) |
515 | 433 |
result(); |
434 |
||
435 |
bin_mult(binary_of_int 255, binary_of_int 255); |
|
436 |
||
5068 | 437 |
Goal "#1359 $* #~2468 = #~3354012"; |
1461 | 438 |
by (simp_tac bin_comp_ss 1); (*6.1 secs*) |
515 | 439 |
result(); |
440 |
||
441 |
bin_mult(binary_of_int 1359, binary_of_int ~2468); |