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 |
|
|
102 |
Goalw [integ_of_def] "w: bin ==> integ_of(w) : integ";
|
|
103 |
by (typechk_tac (bin_typechecks0@integ_typechecks@
|
|
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 |
|
|
146 |
val typechecks = bin_typechecks @ integ_typechecks @ nat_typechecks @
|
|
147 |
[bool_subset_nat RS subsetD];
|
|
148 |
|
|
149 |
(**** The carry/borrow functions, bin_succ and bin_pred ****)
|
|
150 |
|
|
151 |
(** Lemmas **)
|
|
152 |
|
|
153 |
goal Integ.thy
|
|
154 |
"!!z v. [| z $+ v = z' $+ v'; \
|
|
155 |
\ z: integ; z': integ; v: integ; v': integ; w: integ |] \
|
|
156 |
\ ==> z $+ (v $+ w) = z' $+ (v' $+ w)";
|
|
157 |
by (asm_simp_tac (simpset() addsimps [zadd_assoc RS sym]) 1);
|
|
158 |
qed "zadd_assoc_cong";
|
|
159 |
|
|
160 |
goal Integ.thy
|
|
161 |
"!!z v w. [| z: integ; v: integ; w: integ |] \
|
|
162 |
\ ==> z $+ (v $+ w) = v $+ (z $+ w)";
|
|
163 |
by (REPEAT (ares_tac [zadd_commute RS zadd_assoc_cong] 1));
|
|
164 |
qed "zadd_assoc_swap";
|
|
165 |
|
|
166 |
(*Pushes 'constants' of the form $#m to the right -- LOOPS if two!*)
|
|
167 |
bind_thm ("zadd_assoc_znat", (znat_type RS zadd_assoc_swap));
|
|
168 |
|
|
169 |
|
|
170 |
Addsimps (bin_recs bin_succ_def @ bin_recs bin_pred_def);
|
|
171 |
|
|
172 |
|
|
173 |
(*NCons preserves the integer value of its argument*)
|
|
174 |
Goal "[| w: bin; b: bool |] ==> \
|
|
175 |
\ integ_of(NCons(w,b)) = integ_of(Cons(w,b))";
|
|
176 |
by (etac bin.elim 1);
|
|
177 |
by (asm_simp_tac (simpset() addsimps NCons_simps) 3);
|
|
178 |
by (ALLGOALS (etac boolE));
|
|
179 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps NCons_simps)));
|
|
180 |
qed "integ_of_NCons";
|
|
181 |
|
|
182 |
Addsimps [integ_of_NCons];
|
|
183 |
|
|
184 |
Goal "w: bin ==> integ_of(bin_succ(w)) = $#1 $+ integ_of(w)";
|
|
185 |
by (etac bin.induct 1);
|
|
186 |
by (Simp_tac 1);
|
|
187 |
by (Simp_tac 1);
|
|
188 |
by (etac boolE 1);
|
|
189 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps zadd_ac)));
|
|
190 |
qed "integ_of_succ";
|
|
191 |
|
|
192 |
Goal "w: bin ==> integ_of(bin_pred(w)) = $~ ($#1) $+ integ_of(w)";
|
|
193 |
by (etac bin.induct 1);
|
|
194 |
by (Simp_tac 1);
|
|
195 |
by (Simp_tac 1);
|
|
196 |
by (etac boolE 1);
|
|
197 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps zadd_ac)));
|
|
198 |
qed "integ_of_pred";
|
|
199 |
|
|
200 |
(*These two results replace the definitions of bin_succ and bin_pred*)
|
|
201 |
|
|
202 |
|
|
203 |
(*** bin_minus: (unary!) negation of binary integers ***)
|
|
204 |
|
|
205 |
Addsimps (bin_recs bin_minus_def @ [integ_of_succ, integ_of_pred]);
|
|
206 |
|
|
207 |
Goal "w: bin ==> integ_of(bin_minus(w)) = $~ integ_of(w)";
|
|
208 |
by (etac bin.induct 1);
|
|
209 |
by (Simp_tac 1);
|
|
210 |
by (Simp_tac 1);
|
|
211 |
by (etac boolE 1);
|
|
212 |
by (ALLGOALS
|
|
213 |
(asm_simp_tac (simpset() addsimps zadd_ac@[zminus_zadd_distrib])));
|
|
214 |
qed "integ_of_minus";
|
|
215 |
|
|
216 |
|
|
217 |
(*** bin_add: binary addition ***)
|
|
218 |
|
|
219 |
Goalw [bin_add_def] "w: bin ==> bin_add(Pls,w) = w";
|
|
220 |
by (Asm_simp_tac 1);
|
|
221 |
qed "bin_add_Pls";
|
|
222 |
|
|
223 |
Goalw [bin_add_def] "w: bin ==> bin_add(Min,w) = bin_pred(w)";
|
|
224 |
by (Asm_simp_tac 1);
|
|
225 |
qed "bin_add_Min";
|
|
226 |
|
|
227 |
Goalw [bin_add_def] "bin_add(Cons(v,x),Pls) = Cons(v,x)";
|
|
228 |
by (Simp_tac 1);
|
|
229 |
qed "bin_add_Cons_Pls";
|
|
230 |
|
|
231 |
Goalw [bin_add_def] "bin_add(Cons(v,x),Min) = bin_pred(Cons(v,x))";
|
|
232 |
by (Simp_tac 1);
|
|
233 |
qed "bin_add_Cons_Min";
|
|
234 |
|
|
235 |
Goalw [bin_add_def]
|
|
236 |
"[| w: bin; y: bool |] ==> \
|
|
237 |
\ bin_add(Cons(v,x), Cons(w,y)) = \
|
|
238 |
\ NCons(bin_add(v, cond(x and y, bin_succ(w), w)), x xor y)";
|
|
239 |
by (Asm_simp_tac 1);
|
|
240 |
qed "bin_add_Cons_Cons";
|
|
241 |
|
|
242 |
Addsimps [bin_add_Pls, bin_add_Min, bin_add_Cons_Pls,
|
|
243 |
bin_add_Cons_Min, bin_add_Cons_Cons,
|
|
244 |
integ_of_succ, integ_of_pred];
|
|
245 |
|
|
246 |
Addsimps [bool_subset_nat RS subsetD];
|
|
247 |
|
|
248 |
Goal "v: bin ==> \
|
|
249 |
\ ALL w: bin. integ_of(bin_add(v,w)) = \
|
|
250 |
\ integ_of(v) $+ integ_of(w)";
|
|
251 |
by (etac bin.induct 1);
|
|
252 |
by (Simp_tac 1);
|
|
253 |
by (Simp_tac 1);
|
|
254 |
by (rtac ballI 1);
|
|
255 |
by (bin_ind_tac "wa" [] 1);
|
|
256 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps zadd_ac setloop (etac boolE))));
|
|
257 |
val integ_of_add_lemma = result();
|
|
258 |
|
|
259 |
bind_thm("integ_of_add", integ_of_add_lemma RS bspec);
|
|
260 |
|
|
261 |
|
|
262 |
(*** bin_add: binary multiplication ***)
|
|
263 |
|
|
264 |
Addsimps (bin_recs bin_mult_def @
|
|
265 |
[integ_of_minus, integ_of_add]);
|
|
266 |
|
|
267 |
val major::prems = goal Bin.thy
|
|
268 |
"[| v: bin; w: bin |] ==> \
|
|
269 |
\ integ_of(bin_mult(v,w)) = \
|
|
270 |
\ integ_of(v) $* integ_of(w)";
|
|
271 |
by (cut_facts_tac prems 1);
|
|
272 |
by (bin_ind_tac "v" [major] 1);
|
|
273 |
by (Asm_simp_tac 1);
|
|
274 |
by (Asm_simp_tac 1);
|
|
275 |
by (etac boolE 1);
|
|
276 |
by (asm_simp_tac (simpset() addsimps [zadd_zmult_distrib]) 2);
|
|
277 |
by (asm_simp_tac
|
|
278 |
(simpset() addsimps [zadd_zmult_distrib, zmult_1] @ zadd_ac) 1);
|
|
279 |
qed "integ_of_mult";
|
|
280 |
|
|
281 |
(**** Computations ****)
|
|
282 |
|
|
283 |
(** extra rules for bin_succ, bin_pred **)
|
|
284 |
|
|
285 |
val [bin_succ_Pls, bin_succ_Min, _] = bin_recs bin_succ_def;
|
|
286 |
val [bin_pred_Pls, bin_pred_Min, _] = bin_recs bin_pred_def;
|
|
287 |
|
|
288 |
Goal "bin_succ(Cons(w,1)) = Cons(bin_succ(w), 0)";
|
|
289 |
by (Simp_tac 1);
|
|
290 |
qed "bin_succ_Cons1";
|
|
291 |
|
|
292 |
Goal "bin_succ(Cons(w,0)) = NCons(w,1)";
|
|
293 |
by (Simp_tac 1);
|
|
294 |
qed "bin_succ_Cons0";
|
|
295 |
|
|
296 |
Goal "bin_pred(Cons(w,1)) = NCons(w,0)";
|
|
297 |
by (Simp_tac 1);
|
|
298 |
qed "bin_pred_Cons1";
|
|
299 |
|
|
300 |
Goal "bin_pred(Cons(w,0)) = Cons(bin_pred(w), 1)";
|
|
301 |
by (Simp_tac 1);
|
|
302 |
qed "bin_pred_Cons0";
|
|
303 |
|
|
304 |
(** extra rules for bin_minus **)
|
|
305 |
|
|
306 |
val [bin_minus_Pls, bin_minus_Min, _] = bin_recs bin_minus_def;
|
|
307 |
|
|
308 |
Goal "bin_minus(Cons(w,1)) = bin_pred(NCons(bin_minus(w), 0))";
|
|
309 |
by (Simp_tac 1);
|
|
310 |
qed "bin_minus_Cons1";
|
|
311 |
|
|
312 |
Goal "bin_minus(Cons(w,0)) = Cons(bin_minus(w), 0)";
|
|
313 |
by (Simp_tac 1);
|
|
314 |
qed "bin_minus_Cons0";
|
|
315 |
|
|
316 |
(** extra rules for bin_add **)
|
|
317 |
|
|
318 |
Goal "w: bin ==> bin_add(Cons(v,1), Cons(w,1)) = \
|
|
319 |
\ NCons(bin_add(v, bin_succ(w)), 0)";
|
|
320 |
by (Asm_simp_tac 1);
|
|
321 |
qed "bin_add_Cons_Cons11";
|
|
322 |
|
|
323 |
Goal "w: bin ==> bin_add(Cons(v,1), Cons(w,0)) = \
|
|
324 |
\ NCons(bin_add(v,w), 1)";
|
|
325 |
by (Asm_simp_tac 1);
|
|
326 |
qed "bin_add_Cons_Cons10";
|
|
327 |
|
|
328 |
Goal "[| w: bin; y: bool |] ==> \
|
|
329 |
\ bin_add(Cons(v,0), Cons(w,y)) = NCons(bin_add(v,w), y)";
|
|
330 |
by (Asm_simp_tac 1);
|
|
331 |
qed "bin_add_Cons_Cons0";
|
|
332 |
|
|
333 |
(** extra rules for bin_mult **)
|
|
334 |
|
|
335 |
val [bin_mult_Pls, bin_mult_Min, _] = bin_recs bin_mult_def;
|
|
336 |
|
|
337 |
Goal "bin_mult(Cons(v,1), w) = bin_add(NCons(bin_mult(v,w),0), w)";
|
|
338 |
by (Simp_tac 1);
|
|
339 |
qed "bin_mult_Cons1";
|
|
340 |
|
|
341 |
Goal "bin_mult(Cons(v,0), w) = NCons(bin_mult(v,w),0)";
|
|
342 |
by (Simp_tac 1);
|
|
343 |
qed "bin_mult_Cons0";
|
|
344 |
|
|
345 |
|
|
346 |
(*** The computation simpset ***)
|
|
347 |
|
|
348 |
(*Adding the typechecking rules as rewrites is much slower, unfortunately...*)
|
|
349 |
val bin_comp_ss = simpset_of Integ.thy
|
|
350 |
addsimps [integ_of_add RS sym, (*invoke bin_add*)
|
|
351 |
integ_of_minus RS sym, (*invoke bin_minus*)
|
|
352 |
integ_of_mult RS sym, (*invoke bin_mult*)
|
|
353 |
bin_succ_Pls, bin_succ_Min,
|
|
354 |
bin_succ_Cons1, bin_succ_Cons0,
|
|
355 |
bin_pred_Pls, bin_pred_Min,
|
|
356 |
bin_pred_Cons1, bin_pred_Cons0,
|
|
357 |
bin_minus_Pls, bin_minus_Min,
|
|
358 |
bin_minus_Cons1, bin_minus_Cons0,
|
|
359 |
bin_add_Pls, bin_add_Min, bin_add_Cons_Pls,
|
|
360 |
bin_add_Cons_Min, bin_add_Cons_Cons0,
|
|
361 |
bin_add_Cons_Cons10, bin_add_Cons_Cons11,
|
|
362 |
bin_mult_Pls, bin_mult_Min,
|
|
363 |
bin_mult_Cons1, bin_mult_Cons0]
|
|
364 |
@ NCons_simps
|
|
365 |
setSolver (type_auto_tac ([bool_1I, bool_0I] @ bin_typechecks0));
|