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