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 |
|
6046
|
9 |
|
6153
|
10 |
AddTCs bin.intrs;
|
5528
|
11 |
|
6046
|
12 |
Goal "NCons(Pls,0) = Pls";
|
5528
|
13 |
by (Asm_simp_tac 1);
|
|
14 |
qed "NCons_Pls_0";
|
|
15 |
|
6153
|
16 |
Goal "NCons(Pls,1) = Pls BIT 1";
|
5528
|
17 |
by (Asm_simp_tac 1);
|
|
18 |
qed "NCons_Pls_1";
|
|
19 |
|
6153
|
20 |
Goal "NCons(Min,0) = Min BIT 0";
|
5528
|
21 |
by (Asm_simp_tac 1);
|
|
22 |
qed "NCons_Min_0";
|
|
23 |
|
6046
|
24 |
Goal "NCons(Min,1) = Min";
|
5528
|
25 |
by (Asm_simp_tac 1);
|
|
26 |
qed "NCons_Min_1";
|
|
27 |
|
6153
|
28 |
Goal "NCons(w BIT x,b) = w BIT x BIT b";
|
5528
|
29 |
by (asm_simp_tac (simpset() addsimps bin.case_eqns) 1);
|
6153
|
30 |
qed "NCons_BIT";
|
5528
|
31 |
|
|
32 |
val NCons_simps = [NCons_Pls_0, NCons_Pls_1,
|
|
33 |
NCons_Min_0, NCons_Min_1,
|
6153
|
34 |
NCons_BIT];
|
6046
|
35 |
Addsimps NCons_simps;
|
|
36 |
|
5528
|
37 |
|
|
38 |
(** Type checking **)
|
|
39 |
|
6046
|
40 |
Goal "w: bin ==> integ_of(w) : int";
|
6065
|
41 |
by (induct_tac "w" 1);
|
6153
|
42 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [bool_into_nat])));
|
5528
|
43 |
qed "integ_of_type";
|
6153
|
44 |
AddTCs [integ_of_type];
|
5528
|
45 |
|
6046
|
46 |
Goal "[| w: bin; b: bool |] ==> NCons(w,b) : bin";
|
6065
|
47 |
by (induct_tac "w" 1);
|
6046
|
48 |
by Auto_tac;
|
5528
|
49 |
qed "NCons_type";
|
6153
|
50 |
AddTCs [NCons_type];
|
5528
|
51 |
|
6046
|
52 |
Goal "w: bin ==> bin_succ(w) : bin";
|
6065
|
53 |
by (induct_tac "w" 1);
|
6046
|
54 |
by Auto_tac;
|
5528
|
55 |
qed "bin_succ_type";
|
6153
|
56 |
AddTCs [bin_succ_type];
|
5528
|
57 |
|
6046
|
58 |
Goal "w: bin ==> bin_pred(w) : bin";
|
6065
|
59 |
by (induct_tac "w" 1);
|
6046
|
60 |
by Auto_tac;
|
|
61 |
qed "bin_pred_type";
|
6153
|
62 |
AddTCs [bin_pred_type];
|
5528
|
63 |
|
6046
|
64 |
Goal "w: bin ==> bin_minus(w) : bin";
|
6065
|
65 |
by (induct_tac "w" 1);
|
6046
|
66 |
by Auto_tac;
|
|
67 |
qed "bin_minus_type";
|
6153
|
68 |
AddTCs [bin_minus_type];
|
5528
|
69 |
|
6046
|
70 |
(*This proof is complicated by the mutual recursion*)
|
|
71 |
Goal "v: bin ==> ALL w: bin. bin_add(v,w) : bin";
|
6065
|
72 |
by (induct_tac "v" 1);
|
6046
|
73 |
by (rtac ballI 3);
|
6065
|
74 |
by (rename_tac "w'" 3);
|
|
75 |
by (induct_tac "w'" 3);
|
6153
|
76 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps [NCons_type])));
|
6112
|
77 |
qed_spec_mp "bin_add_type";
|
6153
|
78 |
AddTCs [bin_add_type];
|
5528
|
79 |
|
6046
|
80 |
Goal "[| v: bin; w: bin |] ==> bin_mult(v,w) : bin";
|
6065
|
81 |
by (induct_tac "v" 1);
|
6046
|
82 |
by Auto_tac;
|
|
83 |
qed "bin_mult_type";
|
6153
|
84 |
AddTCs [bin_mult_type];
|
5528
|
85 |
|
|
86 |
|
|
87 |
(**** The carry/borrow functions, bin_succ and bin_pred ****)
|
|
88 |
|
|
89 |
(*NCons preserves the integer value of its argument*)
|
|
90 |
Goal "[| w: bin; b: bool |] ==> \
|
6153
|
91 |
\ integ_of(NCons(w,b)) = integ_of(w BIT b)";
|
5528
|
92 |
by (etac bin.elim 1);
|
6046
|
93 |
by (Asm_simp_tac 3);
|
5528
|
94 |
by (ALLGOALS (etac boolE));
|
6046
|
95 |
by (ALLGOALS Asm_simp_tac);
|
5528
|
96 |
qed "integ_of_NCons";
|
|
97 |
|
|
98 |
Addsimps [integ_of_NCons];
|
|
99 |
|
|
100 |
Goal "w: bin ==> integ_of(bin_succ(w)) = $#1 $+ integ_of(w)";
|
|
101 |
by (etac bin.induct 1);
|
|
102 |
by (Simp_tac 1);
|
|
103 |
by (Simp_tac 1);
|
|
104 |
by (etac boolE 1);
|
|
105 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps zadd_ac)));
|
|
106 |
qed "integ_of_succ";
|
|
107 |
|
|
108 |
Goal "w: bin ==> integ_of(bin_pred(w)) = $~ ($#1) $+ integ_of(w)";
|
|
109 |
by (etac bin.induct 1);
|
|
110 |
by (Simp_tac 1);
|
|
111 |
by (Simp_tac 1);
|
|
112 |
by (etac boolE 1);
|
|
113 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps zadd_ac)));
|
|
114 |
qed "integ_of_pred";
|
|
115 |
|
6046
|
116 |
Addsimps [integ_of_succ, integ_of_pred];
|
5528
|
117 |
|
|
118 |
|
|
119 |
(*** bin_minus: (unary!) negation of binary integers ***)
|
|
120 |
|
|
121 |
Goal "w: bin ==> integ_of(bin_minus(w)) = $~ integ_of(w)";
|
|
122 |
by (etac bin.induct 1);
|
|
123 |
by (Simp_tac 1);
|
|
124 |
by (Simp_tac 1);
|
|
125 |
by (etac boolE 1);
|
|
126 |
by (ALLGOALS
|
|
127 |
(asm_simp_tac (simpset() addsimps zadd_ac@[zminus_zadd_distrib])));
|
|
128 |
qed "integ_of_minus";
|
|
129 |
|
|
130 |
|
|
131 |
(*** bin_add: binary addition ***)
|
|
132 |
|
6046
|
133 |
Goal "w: bin ==> bin_add(Pls,w) = w";
|
5528
|
134 |
by (Asm_simp_tac 1);
|
|
135 |
qed "bin_add_Pls";
|
|
136 |
|
6046
|
137 |
Goal "w: bin ==> bin_add(Min,w) = bin_pred(w)";
|
5528
|
138 |
by (Asm_simp_tac 1);
|
|
139 |
qed "bin_add_Min";
|
|
140 |
|
6153
|
141 |
Goal "bin_add(v BIT x,Pls) = v BIT x";
|
5528
|
142 |
by (Simp_tac 1);
|
6153
|
143 |
qed "bin_add_BIT_Pls";
|
5528
|
144 |
|
6153
|
145 |
Goal "bin_add(v BIT x,Min) = bin_pred(v BIT x)";
|
5528
|
146 |
by (Simp_tac 1);
|
6153
|
147 |
qed "bin_add_BIT_Min";
|
5528
|
148 |
|
6046
|
149 |
Goal "[| w: bin; y: bool |] \
|
6153
|
150 |
\ ==> bin_add(v BIT x, w BIT y) = \
|
6046
|
151 |
\ NCons(bin_add(v, cond(x and y, bin_succ(w), w)), x xor y)";
|
5528
|
152 |
by (Asm_simp_tac 1);
|
6153
|
153 |
qed "bin_add_BIT_BIT";
|
5528
|
154 |
|
6153
|
155 |
Addsimps [bin_add_Pls, bin_add_Min, bin_add_BIT_Pls,
|
|
156 |
bin_add_BIT_Min, bin_add_BIT_BIT,
|
5528
|
157 |
integ_of_succ, integ_of_pred];
|
|
158 |
|
|
159 |
Goal "v: bin ==> \
|
6046
|
160 |
\ ALL w: bin. integ_of(bin_add(v,w)) = integ_of(v) $+ integ_of(w)";
|
5528
|
161 |
by (etac bin.induct 1);
|
|
162 |
by (Simp_tac 1);
|
|
163 |
by (Simp_tac 1);
|
|
164 |
by (rtac ballI 1);
|
6065
|
165 |
by (induct_tac "wa" 1);
|
5528
|
166 |
by (ALLGOALS (asm_simp_tac (simpset() addsimps zadd_ac setloop (etac boolE))));
|
6112
|
167 |
qed_spec_mp "integ_of_add";
|
5528
|
168 |
|
|
169 |
|
|
170 |
(*** bin_add: binary multiplication ***)
|
|
171 |
|
6046
|
172 |
Goal "[| v: bin; w: bin |] \
|
|
173 |
\ ==> integ_of(bin_mult(v,w)) = integ_of(v) $* integ_of(w)";
|
6065
|
174 |
by (induct_tac "v" 1);
|
5528
|
175 |
by (Asm_simp_tac 1);
|
6153
|
176 |
by (asm_simp_tac (simpset() addsimps [integ_of_minus]) 1);
|
5528
|
177 |
by (etac boolE 1);
|
|
178 |
by (asm_simp_tac (simpset() addsimps [zadd_zmult_distrib]) 2);
|
6153
|
179 |
by (asm_simp_tac (simpset() addsimps [integ_of_add,
|
|
180 |
zadd_zmult_distrib] @ zadd_ac) 1);
|
5528
|
181 |
qed "integ_of_mult";
|
|
182 |
|
|
183 |
(**** Computations ****)
|
|
184 |
|
|
185 |
(** extra rules for bin_succ, bin_pred **)
|
|
186 |
|
6153
|
187 |
Goal "bin_succ(w BIT 1) = bin_succ(w) BIT 0";
|
5528
|
188 |
by (Simp_tac 1);
|
6153
|
189 |
qed "bin_succ_BIT1";
|
5528
|
190 |
|
6153
|
191 |
Goal "bin_succ(w BIT 0) = NCons(w,1)";
|
5528
|
192 |
by (Simp_tac 1);
|
6153
|
193 |
qed "bin_succ_BIT0";
|
5528
|
194 |
|
6153
|
195 |
Goal "bin_pred(w BIT 1) = NCons(w,0)";
|
5528
|
196 |
by (Simp_tac 1);
|
6153
|
197 |
qed "bin_pred_BIT1";
|
5528
|
198 |
|
6153
|
199 |
Goal "bin_pred(w BIT 0) = bin_pred(w) BIT 1";
|
5528
|
200 |
by (Simp_tac 1);
|
6153
|
201 |
qed "bin_pred_BIT0";
|
5528
|
202 |
|
|
203 |
(** extra rules for bin_minus **)
|
|
204 |
|
6153
|
205 |
Goal "bin_minus(w BIT 1) = bin_pred(NCons(bin_minus(w), 0))";
|
5528
|
206 |
by (Simp_tac 1);
|
6153
|
207 |
qed "bin_minus_BIT1";
|
5528
|
208 |
|
6153
|
209 |
Goal "bin_minus(w BIT 0) = bin_minus(w) BIT 0";
|
5528
|
210 |
by (Simp_tac 1);
|
6153
|
211 |
qed "bin_minus_BIT0";
|
5528
|
212 |
|
|
213 |
(** extra rules for bin_add **)
|
|
214 |
|
6153
|
215 |
Goal "w: bin ==> bin_add(v BIT 1, w BIT 1) = \
|
5528
|
216 |
\ NCons(bin_add(v, bin_succ(w)), 0)";
|
|
217 |
by (Asm_simp_tac 1);
|
6153
|
218 |
qed "bin_add_BIT_BIT11";
|
5528
|
219 |
|
6153
|
220 |
Goal "w: bin ==> bin_add(v BIT 1, w BIT 0) = \
|
5528
|
221 |
\ NCons(bin_add(v,w), 1)";
|
|
222 |
by (Asm_simp_tac 1);
|
6153
|
223 |
qed "bin_add_BIT_BIT10";
|
5528
|
224 |
|
|
225 |
Goal "[| w: bin; y: bool |] ==> \
|
6153
|
226 |
\ bin_add(v BIT 0, w BIT y) = NCons(bin_add(v,w), y)";
|
5528
|
227 |
by (Asm_simp_tac 1);
|
6153
|
228 |
qed "bin_add_BIT_BIT0";
|
5528
|
229 |
|
|
230 |
(** extra rules for bin_mult **)
|
|
231 |
|
6153
|
232 |
Goal "bin_mult(v BIT 1, w) = bin_add(NCons(bin_mult(v,w),0), w)";
|
5528
|
233 |
by (Simp_tac 1);
|
6153
|
234 |
qed "bin_mult_BIT1";
|
5528
|
235 |
|
6153
|
236 |
Goal "bin_mult(v BIT 0, w) = NCons(bin_mult(v,w),0)";
|
5528
|
237 |
by (Simp_tac 1);
|
6153
|
238 |
qed "bin_mult_BIT0";
|
5528
|
239 |
|
|
240 |
|
6153
|
241 |
(*Delete the original rewrites, with their clumsy conditional expressions*)
|
|
242 |
Delsimps [bin_succ_BIT, bin_pred_BIT, bin_minus_BIT,
|
|
243 |
NCons_Pls, NCons_Min, bin_add_BIT, bin_mult_BIT];
|
|
244 |
|
|
245 |
(*Hide the binary representation of integer constants*)
|
|
246 |
Delsimps [integ_of_Pls, integ_of_Min, integ_of_BIT];
|
|
247 |
|
5528
|
248 |
|
6153
|
249 |
Addsimps [integ_of_add RS sym, (*invoke bin_add*)
|
|
250 |
integ_of_minus RS sym, (*invoke bin_minus*)
|
|
251 |
integ_of_mult RS sym, (*invoke bin_mult*)
|
|
252 |
bin_succ_Pls, bin_succ_Min,
|
|
253 |
bin_succ_BIT1, bin_succ_BIT0,
|
|
254 |
bin_pred_Pls, bin_pred_Min,
|
|
255 |
bin_pred_BIT1, bin_pred_BIT0,
|
|
256 |
bin_minus_Pls, bin_minus_Min,
|
|
257 |
bin_minus_BIT1, bin_minus_BIT0,
|
|
258 |
bin_add_Pls, bin_add_Min, bin_add_BIT_Pls,
|
|
259 |
bin_add_BIT_Min, bin_add_BIT_BIT0,
|
|
260 |
bin_add_BIT_BIT10, bin_add_BIT_BIT11,
|
|
261 |
bin_mult_Pls, bin_mult_Min,
|
|
262 |
bin_mult_BIT1, bin_mult_BIT0];
|