author | wenzelm |
Sun, 25 Oct 1998 12:33:27 +0100 | |
changeset 5769 | 6a422b22ba02 |
parent 5561 | 426c1e330903 |
child 6046 | 2c8a8be36c94 |
permissions | -rw-r--r-- |
5528 | 1 |
(* Title: ZF/ex/Bin.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1994 University of Cambridge |
|
5 |
||
6 |
Arithmetic on binary integers. |
|
7 |
*) |
|
8 |
||
9 |
Addsimps bin.case_eqns; |
|
10 |
||
11 |
(*Perform induction on l, then prove the major premise using prems. *) |
|
12 |
fun bin_ind_tac a prems i = |
|
13 |
EVERY [res_inst_tac [("x",a)] bin.induct i, |
|
14 |
rename_last_tac a ["1"] (i+3), |
|
15 |
ares_tac prems i]; |
|
16 |
||
17 |
||
18 |
(** bin_rec -- by Vset recursion **) |
|
19 |
||
20 |
Goal "bin_rec(Pls,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); |
|
24 |
qed "bin_rec_Pls"; |
|
25 |
||
26 |
Goal "bin_rec(Min,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); |
|
30 |
qed "bin_rec_Min"; |
|
31 |
||
32 |
Goal "bin_rec(Cons(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); |
|
34 |
by (rewrite_goals_tac bin.con_defs); |
|
35 |
by (simp_tac rank_ss 1); |
|
36 |
qed "bin_rec_Cons"; |
|
37 |
||
38 |
(*Type checking*) |
|
39 |
val prems = goal Bin.thy |
|
40 |
"[| w: bin; \ |
|
41 |
\ a: C(Pls); b: C(Min); \ |
|
42 |
\ !!w x r. [| w: bin; x: bool; r: C(w) |] ==> h(w,x,r): C(Cons(w,x)) \ |
|
43 |
\ |] ==> bin_rec(w,a,b,h) : C(w)"; |
|
44 |
by (bin_ind_tac "w" prems 1); |
|
45 |
by (ALLGOALS |
|
46 |
(asm_simp_tac (simpset() addsimps prems @ [bin_rec_Pls, bin_rec_Min, |
|
47 |
bin_rec_Cons]))); |
|
48 |
qed "bin_rec_type"; |
|
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(Pls) = a"; |
|
54 |
by (rewtac rew); |
|
55 |
by (rtac bin_rec_Pls 1); |
|
56 |
qed "def_bin_rec_Pls"; |
|
57 |
||
58 |
val [rew] = goal Bin.thy |
|
59 |
"[| !!w. j(w)==bin_rec(w,a,b,h) |] ==> j(Min) = b"; |
|
60 |
by (rewtac rew); |
|
61 |
by (rtac bin_rec_Min 1); |
|
62 |
qed "def_bin_rec_Min"; |
|
63 |
||
64 |
val [rew] = goal Bin.thy |
|
65 |
"[| !!w. j(w)==bin_rec(w,a,b,h) |] ==> j(Cons(w,x)) = h(w,x,j(w))"; |
|
66 |
by (rewtac rew); |
|
67 |
by (rtac bin_rec_Cons 1); |
|
68 |
qed "def_bin_rec_Cons"; |
|
69 |
||
70 |
fun bin_recs def = map standard |
|
71 |
([def] RL [def_bin_rec_Pls, def_bin_rec_Min, def_bin_rec_Cons]); |
|
72 |
||
73 |
Goalw [NCons_def] "NCons(Pls,0) = Pls"; |
|
74 |
by (Asm_simp_tac 1); |
|
75 |
qed "NCons_Pls_0"; |
|
76 |
||
77 |
Goalw [NCons_def] "NCons(Pls,1) = Cons(Pls,1)"; |
|
78 |
by (Asm_simp_tac 1); |
|
79 |
qed "NCons_Pls_1"; |
|
80 |
||
81 |
Goalw [NCons_def] "NCons(Min,0) = Cons(Min,0)"; |
|
82 |
by (Asm_simp_tac 1); |
|
83 |
qed "NCons_Min_0"; |
|
84 |
||
85 |
Goalw [NCons_def] "NCons(Min,1) = Min"; |
|
86 |
by (Asm_simp_tac 1); |
|
87 |
qed "NCons_Min_1"; |
|
88 |
||
89 |
Goalw [NCons_def] |
|
90 |
"NCons(Cons(w,x),b) = Cons(Cons(w,x),b)"; |
|
91 |
by (asm_simp_tac (simpset() addsimps bin.case_eqns) 1); |
|
92 |
qed "NCons_Cons"; |
|
93 |
||
94 |
val NCons_simps = [NCons_Pls_0, NCons_Pls_1, |
|
95 |
NCons_Min_0, NCons_Min_1, |
|
96 |
NCons_Cons]; |
|
97 |
||
98 |
(** Type checking **) |
|
99 |
||
100 |
val bin_typechecks0 = bin_rec_type :: bin.intrs; |
|
101 |
||
5561
426c1e330903
Renaming of Integ/Integ.* to Integ/Int.*, and renaming of related constants
paulson
parents:
5528
diff
changeset
|
102 |
Goalw [integ_of_def] "w: bin ==> integ_of(w) : int"; |
426c1e330903
Renaming of Integ/Integ.* to Integ/Int.*, and renaming of related constants
paulson
parents:
5528
diff
changeset
|
103 |
by (typechk_tac (bin_typechecks0@int_typechecks@ |
5528 | 104 |
nat_typechecks@[bool_into_nat])); |
105 |
qed "integ_of_type"; |
|
106 |
||
107 |
Goalw [NCons_def] "[| w: bin; b: bool |] ==> NCons(w,b) : bin"; |
|
108 |
by (etac bin.elim 1); |
|
109 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps bin.case_eqns))); |
|
110 |
by (typechk_tac (bin_typechecks0@bool_typechecks)); |
|
111 |
qed "NCons_type"; |
|
112 |
||
113 |
Goalw [bin_succ_def] "w: bin ==> bin_succ(w) : bin"; |
|
114 |
by (typechk_tac ([NCons_type]@bin_typechecks0@bool_typechecks)); |
|
115 |
qed "bin_succ_type"; |
|
116 |
||
117 |
Goalw [bin_pred_def] "w: bin ==> bin_pred(w) : bin"; |
|
118 |
by (typechk_tac ([NCons_type]@bin_typechecks0@bool_typechecks)); |
|
119 |
qed "bin_pred_type"; |
|
120 |
||
121 |
Goalw [bin_minus_def] "w: bin ==> bin_minus(w) : bin"; |
|
122 |
by (typechk_tac ([NCons_type,bin_pred_type]@ |
|
123 |
bin_typechecks0@bool_typechecks)); |
|
124 |
qed "bin_minus_type"; |
|
125 |
||
126 |
Goalw [bin_add_def] |
|
127 |
"[| v: bin; w: bin |] ==> bin_add(v,w) : bin"; |
|
128 |
by (typechk_tac ([NCons_type, bin_succ_type, bin_pred_type]@ |
|
129 |
bin_typechecks0@ bool_typechecks@ZF_typechecks)); |
|
130 |
qed "bin_add_type"; |
|
131 |
||
132 |
Goalw [bin_mult_def] |
|
133 |
"[| v: bin; w: bin |] ==> bin_mult(v,w) : bin"; |
|
134 |
by (typechk_tac ([NCons_type, bin_minus_type, bin_add_type]@ |
|
135 |
bin_typechecks0@ bool_typechecks)); |
|
136 |
qed "bin_mult_type"; |
|
137 |
||
138 |
val bin_typechecks = bin_typechecks0 @ |
|
139 |
[integ_of_type, NCons_type, bin_succ_type, bin_pred_type, |
|
140 |
bin_minus_type, bin_add_type, bin_mult_type]; |
|
141 |
||
142 |
Addsimps ([bool_1I, bool_0I, |
|
143 |
bin_rec_Pls, bin_rec_Min, bin_rec_Cons] @ |
|
144 |
bin_recs integ_of_def @ bin_typechecks); |
|
145 |
||
5561
426c1e330903
Renaming of Integ/Integ.* to Integ/Int.*, and renaming of related constants
paulson
parents:
5528
diff
changeset
|
146 |
val typechecks = bin_typechecks @ int_typechecks @ nat_typechecks @ |
5528 | 147 |
[bool_subset_nat RS subsetD]; |
148 |
||
149 |
(**** The carry/borrow functions, bin_succ and bin_pred ****) |
|
150 |
||
151 |
Addsimps (bin_recs bin_succ_def @ bin_recs bin_pred_def); |
|
152 |
||
153 |
(*NCons preserves the integer value of its argument*) |
|
154 |
Goal "[| w: bin; b: bool |] ==> \ |
|
155 |
\ integ_of(NCons(w,b)) = integ_of(Cons(w,b))"; |
|
156 |
by (etac bin.elim 1); |
|
157 |
by (asm_simp_tac (simpset() addsimps NCons_simps) 3); |
|
158 |
by (ALLGOALS (etac boolE)); |
|
159 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps NCons_simps))); |
|
160 |
qed "integ_of_NCons"; |
|
161 |
||
162 |
Addsimps [integ_of_NCons]; |
|
163 |
||
164 |
Goal "w: bin ==> integ_of(bin_succ(w)) = $#1 $+ integ_of(w)"; |
|
165 |
by (etac bin.induct 1); |
|
166 |
by (Simp_tac 1); |
|
167 |
by (Simp_tac 1); |
|
168 |
by (etac boolE 1); |
|
169 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps zadd_ac))); |
|
170 |
qed "integ_of_succ"; |
|
171 |
||
172 |
Goal "w: bin ==> integ_of(bin_pred(w)) = $~ ($#1) $+ integ_of(w)"; |
|
173 |
by (etac bin.induct 1); |
|
174 |
by (Simp_tac 1); |
|
175 |
by (Simp_tac 1); |
|
176 |
by (etac boolE 1); |
|
177 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps zadd_ac))); |
|
178 |
qed "integ_of_pred"; |
|
179 |
||
180 |
(*These two results replace the definitions of bin_succ and bin_pred*) |
|
181 |
||
182 |
||
183 |
(*** bin_minus: (unary!) negation of binary integers ***) |
|
184 |
||
185 |
Addsimps (bin_recs bin_minus_def @ [integ_of_succ, integ_of_pred]); |
|
186 |
||
187 |
Goal "w: bin ==> integ_of(bin_minus(w)) = $~ integ_of(w)"; |
|
188 |
by (etac bin.induct 1); |
|
189 |
by (Simp_tac 1); |
|
190 |
by (Simp_tac 1); |
|
191 |
by (etac boolE 1); |
|
192 |
by (ALLGOALS |
|
193 |
(asm_simp_tac (simpset() addsimps zadd_ac@[zminus_zadd_distrib]))); |
|
194 |
qed "integ_of_minus"; |
|
195 |
||
196 |
||
197 |
(*** bin_add: binary addition ***) |
|
198 |
||
199 |
Goalw [bin_add_def] "w: bin ==> bin_add(Pls,w) = w"; |
|
200 |
by (Asm_simp_tac 1); |
|
201 |
qed "bin_add_Pls"; |
|
202 |
||
203 |
Goalw [bin_add_def] "w: bin ==> bin_add(Min,w) = bin_pred(w)"; |
|
204 |
by (Asm_simp_tac 1); |
|
205 |
qed "bin_add_Min"; |
|
206 |
||
207 |
Goalw [bin_add_def] "bin_add(Cons(v,x),Pls) = Cons(v,x)"; |
|
208 |
by (Simp_tac 1); |
|
209 |
qed "bin_add_Cons_Pls"; |
|
210 |
||
211 |
Goalw [bin_add_def] "bin_add(Cons(v,x),Min) = bin_pred(Cons(v,x))"; |
|
212 |
by (Simp_tac 1); |
|
213 |
qed "bin_add_Cons_Min"; |
|
214 |
||
215 |
Goalw [bin_add_def] |
|
216 |
"[| w: bin; y: bool |] ==> \ |
|
217 |
\ bin_add(Cons(v,x), Cons(w,y)) = \ |
|
218 |
\ NCons(bin_add(v, cond(x and y, bin_succ(w), w)), x xor y)"; |
|
219 |
by (Asm_simp_tac 1); |
|
220 |
qed "bin_add_Cons_Cons"; |
|
221 |
||
222 |
Addsimps [bin_add_Pls, bin_add_Min, bin_add_Cons_Pls, |
|
223 |
bin_add_Cons_Min, bin_add_Cons_Cons, |
|
224 |
integ_of_succ, integ_of_pred]; |
|
225 |
||
226 |
Addsimps [bool_subset_nat RS subsetD]; |
|
227 |
||
228 |
Goal "v: bin ==> \ |
|
229 |
\ ALL w: bin. integ_of(bin_add(v,w)) = \ |
|
230 |
\ integ_of(v) $+ integ_of(w)"; |
|
231 |
by (etac bin.induct 1); |
|
232 |
by (Simp_tac 1); |
|
233 |
by (Simp_tac 1); |
|
234 |
by (rtac ballI 1); |
|
235 |
by (bin_ind_tac "wa" [] 1); |
|
236 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps zadd_ac setloop (etac boolE)))); |
|
237 |
val integ_of_add_lemma = result(); |
|
238 |
||
239 |
bind_thm("integ_of_add", integ_of_add_lemma RS bspec); |
|
240 |
||
241 |
||
242 |
(*** bin_add: binary multiplication ***) |
|
243 |
||
244 |
Addsimps (bin_recs bin_mult_def @ |
|
245 |
[integ_of_minus, integ_of_add]); |
|
246 |
||
247 |
val major::prems = goal Bin.thy |
|
248 |
"[| v: bin; w: bin |] ==> \ |
|
249 |
\ integ_of(bin_mult(v,w)) = \ |
|
250 |
\ integ_of(v) $* integ_of(w)"; |
|
251 |
by (cut_facts_tac prems 1); |
|
252 |
by (bin_ind_tac "v" [major] 1); |
|
253 |
by (Asm_simp_tac 1); |
|
254 |
by (Asm_simp_tac 1); |
|
255 |
by (etac boolE 1); |
|
256 |
by (asm_simp_tac (simpset() addsimps [zadd_zmult_distrib]) 2); |
|
257 |
by (asm_simp_tac |
|
258 |
(simpset() addsimps [zadd_zmult_distrib, zmult_1] @ zadd_ac) 1); |
|
259 |
qed "integ_of_mult"; |
|
260 |
||
261 |
(**** Computations ****) |
|
262 |
||
263 |
(** extra rules for bin_succ, bin_pred **) |
|
264 |
||
265 |
val [bin_succ_Pls, bin_succ_Min, _] = bin_recs bin_succ_def; |
|
266 |
val [bin_pred_Pls, bin_pred_Min, _] = bin_recs bin_pred_def; |
|
267 |
||
268 |
Goal "bin_succ(Cons(w,1)) = Cons(bin_succ(w), 0)"; |
|
269 |
by (Simp_tac 1); |
|
270 |
qed "bin_succ_Cons1"; |
|
271 |
||
272 |
Goal "bin_succ(Cons(w,0)) = NCons(w,1)"; |
|
273 |
by (Simp_tac 1); |
|
274 |
qed "bin_succ_Cons0"; |
|
275 |
||
276 |
Goal "bin_pred(Cons(w,1)) = NCons(w,0)"; |
|
277 |
by (Simp_tac 1); |
|
278 |
qed "bin_pred_Cons1"; |
|
279 |
||
280 |
Goal "bin_pred(Cons(w,0)) = Cons(bin_pred(w), 1)"; |
|
281 |
by (Simp_tac 1); |
|
282 |
qed "bin_pred_Cons0"; |
|
283 |
||
284 |
(** extra rules for bin_minus **) |
|
285 |
||
286 |
val [bin_minus_Pls, bin_minus_Min, _] = bin_recs bin_minus_def; |
|
287 |
||
288 |
Goal "bin_minus(Cons(w,1)) = bin_pred(NCons(bin_minus(w), 0))"; |
|
289 |
by (Simp_tac 1); |
|
290 |
qed "bin_minus_Cons1"; |
|
291 |
||
292 |
Goal "bin_minus(Cons(w,0)) = Cons(bin_minus(w), 0)"; |
|
293 |
by (Simp_tac 1); |
|
294 |
qed "bin_minus_Cons0"; |
|
295 |
||
296 |
(** extra rules for bin_add **) |
|
297 |
||
298 |
Goal "w: bin ==> bin_add(Cons(v,1), Cons(w,1)) = \ |
|
299 |
\ NCons(bin_add(v, bin_succ(w)), 0)"; |
|
300 |
by (Asm_simp_tac 1); |
|
301 |
qed "bin_add_Cons_Cons11"; |
|
302 |
||
303 |
Goal "w: bin ==> bin_add(Cons(v,1), Cons(w,0)) = \ |
|
304 |
\ NCons(bin_add(v,w), 1)"; |
|
305 |
by (Asm_simp_tac 1); |
|
306 |
qed "bin_add_Cons_Cons10"; |
|
307 |
||
308 |
Goal "[| w: bin; y: bool |] ==> \ |
|
309 |
\ bin_add(Cons(v,0), Cons(w,y)) = NCons(bin_add(v,w), y)"; |
|
310 |
by (Asm_simp_tac 1); |
|
311 |
qed "bin_add_Cons_Cons0"; |
|
312 |
||
313 |
(** extra rules for bin_mult **) |
|
314 |
||
315 |
val [bin_mult_Pls, bin_mult_Min, _] = bin_recs bin_mult_def; |
|
316 |
||
317 |
Goal "bin_mult(Cons(v,1), w) = bin_add(NCons(bin_mult(v,w),0), w)"; |
|
318 |
by (Simp_tac 1); |
|
319 |
qed "bin_mult_Cons1"; |
|
320 |
||
321 |
Goal "bin_mult(Cons(v,0), w) = NCons(bin_mult(v,w),0)"; |
|
322 |
by (Simp_tac 1); |
|
323 |
qed "bin_mult_Cons0"; |
|
324 |
||
325 |
||
326 |
(*** The computation simpset ***) |
|
327 |
||
328 |
(*Adding the typechecking rules as rewrites is much slower, unfortunately...*) |
|
5561
426c1e330903
Renaming of Integ/Integ.* to Integ/Int.*, and renaming of related constants
paulson
parents:
5528
diff
changeset
|
329 |
val bin_comp_ss = simpset_of Int.thy |
5528 | 330 |
addsimps [integ_of_add RS sym, (*invoke bin_add*) |
331 |
integ_of_minus RS sym, (*invoke bin_minus*) |
|
332 |
integ_of_mult RS sym, (*invoke bin_mult*) |
|
333 |
bin_succ_Pls, bin_succ_Min, |
|
334 |
bin_succ_Cons1, bin_succ_Cons0, |
|
335 |
bin_pred_Pls, bin_pred_Min, |
|
336 |
bin_pred_Cons1, bin_pred_Cons0, |
|
337 |
bin_minus_Pls, bin_minus_Min, |
|
338 |
bin_minus_Cons1, bin_minus_Cons0, |
|
339 |
bin_add_Pls, bin_add_Min, bin_add_Cons_Pls, |
|
340 |
bin_add_Cons_Min, bin_add_Cons_Cons0, |
|
341 |
bin_add_Cons_Cons10, bin_add_Cons_Cons11, |
|
342 |
bin_mult_Pls, bin_mult_Min, |
|
343 |
bin_mult_Cons1, bin_mult_Cons0] |
|
344 |
@ NCons_simps |
|
345 |
setSolver (type_auto_tac ([bool_1I, bool_0I] @ bin_typechecks0)); |