author | haftmann |
Wed, 02 Jan 2008 15:14:02 +0100 | |
changeset 25762 | c03e9d04b3e4 |
parent 25112 | 98824cc791c0 |
child 25919 | 8b1c0d434824 |
permissions | -rw-r--r-- |
24333 | 1 |
(* |
2 |
ID: $Id$ |
|
3 |
Author: Jeremy Dawson and Gerwin Klein, NICTA |
|
4 |
||
5 |
definition and basic theorems for bit-wise logical operations |
|
6 |
for integers expressed using Pls, Min, BIT, |
|
7 |
and converting them to and from lists of bools |
|
8 |
*) |
|
9 |
||
24350 | 10 |
header {* Bitwise Operations on Binary Integers *} |
11 |
||
24353 | 12 |
theory BinOperations imports BinGeneral BitSyntax |
24333 | 13 |
|
14 |
begin |
|
15 |
||
24364 | 16 |
subsection {* Logical operations *} |
24353 | 17 |
|
18 |
text "bit-wise logical operations on the int type" |
|
19 |
||
25762 | 20 |
instantiation int :: bit |
21 |
begin |
|
22 |
||
23 |
definition |
|
24 |
int_not_def: "bitNOT = bin_rec Numeral.Min Numeral.Pls |
|
24353 | 25 |
(\<lambda>w b s. s BIT (NOT b))" |
25762 | 26 |
|
27 |
definition |
|
28 |
int_and_def: "bitAND = bin_rec (\<lambda>x. Numeral.Pls) (\<lambda>y. y) |
|
24353 | 29 |
(\<lambda>w b s y. s (bin_rest y) BIT (b AND bin_last y))" |
25762 | 30 |
|
31 |
definition |
|
32 |
int_or_def: "bitOR = bin_rec (\<lambda>x. x) (\<lambda>y. Numeral.Min) |
|
24353 | 33 |
(\<lambda>w b s y. s (bin_rest y) BIT (b OR bin_last y))" |
25762 | 34 |
|
35 |
definition |
|
36 |
int_xor_def: "bitXOR = bin_rec (\<lambda>x. x) bitNOT |
|
24353 | 37 |
(\<lambda>w b s y. s (bin_rest y) BIT (b XOR bin_last y))" |
25762 | 38 |
|
39 |
instance .. |
|
40 |
||
41 |
end |
|
24353 | 42 |
|
24333 | 43 |
lemma int_not_simps [simp]: |
24353 | 44 |
"NOT Numeral.Pls = Numeral.Min" |
45 |
"NOT Numeral.Min = Numeral.Pls" |
|
46 |
"NOT (w BIT b) = (NOT w) BIT (NOT b)" |
|
24333 | 47 |
by (unfold int_not_def) (auto intro: bin_rec_simps) |
48 |
||
49 |
lemma int_xor_Pls [simp]: |
|
24353 | 50 |
"Numeral.Pls XOR x = x" |
24465 | 51 |
unfolding int_xor_def by (simp add: bin_rec_PM) |
24333 | 52 |
|
53 |
lemma int_xor_Min [simp]: |
|
24353 | 54 |
"Numeral.Min XOR x = NOT x" |
24465 | 55 |
unfolding int_xor_def by (simp add: bin_rec_PM) |
24333 | 56 |
|
57 |
lemma int_xor_Bits [simp]: |
|
24353 | 58 |
"(x BIT b) XOR (y BIT c) = (x XOR y) BIT (b XOR c)" |
24333 | 59 |
apply (unfold int_xor_def) |
60 |
apply (rule bin_rec_simps (1) [THEN fun_cong, THEN trans]) |
|
61 |
apply (rule ext, simp) |
|
62 |
prefer 2 |
|
63 |
apply simp |
|
64 |
apply (rule ext) |
|
65 |
apply (simp add: int_not_simps [symmetric]) |
|
66 |
done |
|
67 |
||
68 |
lemma int_xor_x_simps': |
|
24353 | 69 |
"w XOR (Numeral.Pls BIT bit.B0) = w" |
70 |
"w XOR (Numeral.Min BIT bit.B1) = NOT w" |
|
24333 | 71 |
apply (induct w rule: bin_induct) |
72 |
apply simp_all[4] |
|
73 |
apply (unfold int_xor_Bits) |
|
74 |
apply clarsimp+ |
|
75 |
done |
|
76 |
||
77 |
lemmas int_xor_extra_simps [simp] = int_xor_x_simps' [simplified arith_simps] |
|
78 |
||
79 |
lemma int_or_Pls [simp]: |
|
24353 | 80 |
"Numeral.Pls OR x = x" |
24465 | 81 |
by (unfold int_or_def) (simp add: bin_rec_PM) |
24333 | 82 |
|
83 |
lemma int_or_Min [simp]: |
|
24353 | 84 |
"Numeral.Min OR x = Numeral.Min" |
24465 | 85 |
by (unfold int_or_def) (simp add: bin_rec_PM) |
24333 | 86 |
|
87 |
lemma int_or_Bits [simp]: |
|
24353 | 88 |
"(x BIT b) OR (y BIT c) = (x OR y) BIT (b OR c)" |
24333 | 89 |
unfolding int_or_def by (simp add: bin_rec_simps) |
90 |
||
91 |
lemma int_or_x_simps': |
|
24353 | 92 |
"w OR (Numeral.Pls BIT bit.B0) = w" |
93 |
"w OR (Numeral.Min BIT bit.B1) = Numeral.Min" |
|
24333 | 94 |
apply (induct w rule: bin_induct) |
95 |
apply simp_all[4] |
|
96 |
apply (unfold int_or_Bits) |
|
97 |
apply clarsimp+ |
|
98 |
done |
|
99 |
||
100 |
lemmas int_or_extra_simps [simp] = int_or_x_simps' [simplified arith_simps] |
|
101 |
||
102 |
||
103 |
lemma int_and_Pls [simp]: |
|
24353 | 104 |
"Numeral.Pls AND x = Numeral.Pls" |
24465 | 105 |
unfolding int_and_def by (simp add: bin_rec_PM) |
24333 | 106 |
|
24353 | 107 |
lemma int_and_Min [simp]: |
108 |
"Numeral.Min AND x = x" |
|
24465 | 109 |
unfolding int_and_def by (simp add: bin_rec_PM) |
24333 | 110 |
|
111 |
lemma int_and_Bits [simp]: |
|
24353 | 112 |
"(x BIT b) AND (y BIT c) = (x AND y) BIT (b AND c)" |
24333 | 113 |
unfolding int_and_def by (simp add: bin_rec_simps) |
114 |
||
115 |
lemma int_and_x_simps': |
|
24353 | 116 |
"w AND (Numeral.Pls BIT bit.B0) = Numeral.Pls" |
117 |
"w AND (Numeral.Min BIT bit.B1) = w" |
|
24333 | 118 |
apply (induct w rule: bin_induct) |
119 |
apply simp_all[4] |
|
120 |
apply (unfold int_and_Bits) |
|
121 |
apply clarsimp+ |
|
122 |
done |
|
123 |
||
124 |
lemmas int_and_extra_simps [simp] = int_and_x_simps' [simplified arith_simps] |
|
125 |
||
126 |
(* commutativity of the above *) |
|
127 |
lemma bin_ops_comm: |
|
128 |
shows |
|
24353 | 129 |
int_and_comm: "!!y::int. x AND y = y AND x" and |
130 |
int_or_comm: "!!y::int. x OR y = y OR x" and |
|
131 |
int_xor_comm: "!!y::int. x XOR y = y XOR x" |
|
24333 | 132 |
apply (induct x rule: bin_induct) |
133 |
apply simp_all[6] |
|
134 |
apply (case_tac y rule: bin_exhaust, simp add: bit_ops_comm)+ |
|
135 |
done |
|
136 |
||
137 |
lemma bin_ops_same [simp]: |
|
24353 | 138 |
"(x::int) AND x = x" |
139 |
"(x::int) OR x = x" |
|
140 |
"(x::int) XOR x = Numeral.Pls" |
|
24333 | 141 |
by (induct x rule: bin_induct) auto |
142 |
||
24353 | 143 |
lemma int_not_not [simp]: "NOT (NOT (x::int)) = x" |
24333 | 144 |
by (induct x rule: bin_induct) auto |
145 |
||
146 |
lemmas bin_log_esimps = |
|
147 |
int_and_extra_simps int_or_extra_simps int_xor_extra_simps |
|
148 |
int_and_Pls int_and_Min int_or_Pls int_or_Min int_xor_Pls int_xor_Min |
|
149 |
||
150 |
(* basic properties of logical (bit-wise) operations *) |
|
151 |
||
152 |
lemma bbw_ao_absorb: |
|
24353 | 153 |
"!!y::int. x AND (y OR x) = x & x OR (y AND x) = x" |
24333 | 154 |
apply (induct x rule: bin_induct) |
155 |
apply auto |
|
156 |
apply (case_tac [!] y rule: bin_exhaust) |
|
157 |
apply auto |
|
158 |
apply (case_tac [!] bit) |
|
159 |
apply auto |
|
160 |
done |
|
161 |
||
162 |
lemma bbw_ao_absorbs_other: |
|
24353 | 163 |
"x AND (x OR y) = x \<and> (y AND x) OR x = (x::int)" |
164 |
"(y OR x) AND x = x \<and> x OR (x AND y) = (x::int)" |
|
165 |
"(x OR y) AND x = x \<and> (x AND y) OR x = (x::int)" |
|
24333 | 166 |
apply (auto simp: bbw_ao_absorb int_or_comm) |
167 |
apply (subst int_or_comm) |
|
168 |
apply (simp add: bbw_ao_absorb) |
|
169 |
apply (subst int_and_comm) |
|
170 |
apply (subst int_or_comm) |
|
171 |
apply (simp add: bbw_ao_absorb) |
|
172 |
apply (subst int_and_comm) |
|
173 |
apply (simp add: bbw_ao_absorb) |
|
174 |
done |
|
24353 | 175 |
|
24333 | 176 |
lemmas bbw_ao_absorbs [simp] = bbw_ao_absorb bbw_ao_absorbs_other |
177 |
||
178 |
lemma int_xor_not: |
|
24353 | 179 |
"!!y::int. (NOT x) XOR y = NOT (x XOR y) & |
180 |
x XOR (NOT y) = NOT (x XOR y)" |
|
24333 | 181 |
apply (induct x rule: bin_induct) |
182 |
apply auto |
|
183 |
apply (case_tac y rule: bin_exhaust, auto, |
|
184 |
case_tac b, auto)+ |
|
185 |
done |
|
186 |
||
187 |
lemma bbw_assocs': |
|
24353 | 188 |
"!!y z::int. (x AND y) AND z = x AND (y AND z) & |
189 |
(x OR y) OR z = x OR (y OR z) & |
|
190 |
(x XOR y) XOR z = x XOR (y XOR z)" |
|
24333 | 191 |
apply (induct x rule: bin_induct) |
192 |
apply (auto simp: int_xor_not) |
|
193 |
apply (case_tac [!] y rule: bin_exhaust) |
|
194 |
apply (case_tac [!] z rule: bin_exhaust) |
|
195 |
apply (case_tac [!] bit) |
|
196 |
apply (case_tac [!] b) |
|
197 |
apply auto |
|
198 |
done |
|
199 |
||
200 |
lemma int_and_assoc: |
|
24353 | 201 |
"(x AND y) AND (z::int) = x AND (y AND z)" |
24333 | 202 |
by (simp add: bbw_assocs') |
203 |
||
204 |
lemma int_or_assoc: |
|
24353 | 205 |
"(x OR y) OR (z::int) = x OR (y OR z)" |
24333 | 206 |
by (simp add: bbw_assocs') |
207 |
||
208 |
lemma int_xor_assoc: |
|
24353 | 209 |
"(x XOR y) XOR (z::int) = x XOR (y XOR z)" |
24333 | 210 |
by (simp add: bbw_assocs') |
211 |
||
212 |
lemmas bbw_assocs = int_and_assoc int_or_assoc int_xor_assoc |
|
213 |
||
214 |
lemma bbw_lcs [simp]: |
|
24353 | 215 |
"(y::int) AND (x AND z) = x AND (y AND z)" |
216 |
"(y::int) OR (x OR z) = x OR (y OR z)" |
|
217 |
"(y::int) XOR (x XOR z) = x XOR (y XOR z)" |
|
24333 | 218 |
apply (auto simp: bbw_assocs [symmetric]) |
219 |
apply (auto simp: bin_ops_comm) |
|
220 |
done |
|
221 |
||
222 |
lemma bbw_not_dist: |
|
24353 | 223 |
"!!y::int. NOT (x OR y) = (NOT x) AND (NOT y)" |
224 |
"!!y::int. NOT (x AND y) = (NOT x) OR (NOT y)" |
|
24333 | 225 |
apply (induct x rule: bin_induct) |
226 |
apply auto |
|
227 |
apply (case_tac [!] y rule: bin_exhaust) |
|
228 |
apply (case_tac [!] bit, auto) |
|
229 |
done |
|
230 |
||
231 |
lemma bbw_oa_dist: |
|
24353 | 232 |
"!!y z::int. (x AND y) OR z = |
233 |
(x OR z) AND (y OR z)" |
|
24333 | 234 |
apply (induct x rule: bin_induct) |
235 |
apply auto |
|
236 |
apply (case_tac y rule: bin_exhaust) |
|
237 |
apply (case_tac z rule: bin_exhaust) |
|
238 |
apply (case_tac ba, auto) |
|
239 |
done |
|
240 |
||
241 |
lemma bbw_ao_dist: |
|
24353 | 242 |
"!!y z::int. (x OR y) AND z = |
243 |
(x AND z) OR (y AND z)" |
|
24333 | 244 |
apply (induct x rule: bin_induct) |
245 |
apply auto |
|
246 |
apply (case_tac y rule: bin_exhaust) |
|
247 |
apply (case_tac z rule: bin_exhaust) |
|
248 |
apply (case_tac ba, auto) |
|
249 |
done |
|
250 |
||
24367
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
huffman
parents:
24366
diff
changeset
|
251 |
(* |
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
huffman
parents:
24366
diff
changeset
|
252 |
Why were these declared simp??? |
24333 | 253 |
declare bin_ops_comm [simp] bbw_assocs [simp] |
24367
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
huffman
parents:
24366
diff
changeset
|
254 |
*) |
24333 | 255 |
|
256 |
lemma plus_and_or [rule_format]: |
|
24353 | 257 |
"ALL y::int. (x AND y) + (x OR y) = x + y" |
24333 | 258 |
apply (induct x rule: bin_induct) |
259 |
apply clarsimp |
|
260 |
apply clarsimp |
|
261 |
apply clarsimp |
|
262 |
apply (case_tac y rule: bin_exhaust) |
|
263 |
apply clarsimp |
|
264 |
apply (unfold Bit_def) |
|
265 |
apply clarsimp |
|
266 |
apply (erule_tac x = "x" in allE) |
|
267 |
apply (simp split: bit.split) |
|
268 |
done |
|
269 |
||
270 |
lemma le_int_or: |
|
24353 | 271 |
"!!x. bin_sign y = Numeral.Pls ==> x <= x OR y" |
24333 | 272 |
apply (induct y rule: bin_induct) |
273 |
apply clarsimp |
|
274 |
apply clarsimp |
|
275 |
apply (case_tac x rule: bin_exhaust) |
|
276 |
apply (case_tac b) |
|
277 |
apply (case_tac [!] bit) |
|
278 |
apply (auto simp: less_eq_numeral_code) |
|
279 |
done |
|
280 |
||
281 |
lemmas int_and_le = |
|
282 |
xtr3 [OF bbw_ao_absorbs (2) [THEN conjunct2, symmetric] le_int_or] ; |
|
283 |
||
24364 | 284 |
lemma bin_nth_ops: |
285 |
"!!x y. bin_nth (x AND y) n = (bin_nth x n & bin_nth y n)" |
|
286 |
"!!x y. bin_nth (x OR y) n = (bin_nth x n | bin_nth y n)" |
|
287 |
"!!x y. bin_nth (x XOR y) n = (bin_nth x n ~= bin_nth y n)" |
|
288 |
"!!x. bin_nth (NOT x) n = (~ bin_nth x n)" |
|
289 |
apply (induct n) |
|
290 |
apply safe |
|
291 |
apply (case_tac [!] x rule: bin_exhaust) |
|
292 |
apply simp_all |
|
293 |
apply (case_tac [!] y rule: bin_exhaust) |
|
294 |
apply simp_all |
|
295 |
apply (auto dest: not_B1_is_B0 intro: B1_ass_B0) |
|
296 |
done |
|
297 |
||
298 |
(* interaction between bit-wise and arithmetic *) |
|
299 |
(* good example of bin_induction *) |
|
300 |
lemma bin_add_not: "x + NOT x = Numeral.Min" |
|
301 |
apply (induct x rule: bin_induct) |
|
302 |
apply clarsimp |
|
303 |
apply clarsimp |
|
304 |
apply (case_tac bit, auto) |
|
305 |
done |
|
306 |
||
307 |
(* truncating results of bit-wise operations *) |
|
308 |
lemma bin_trunc_ao: |
|
309 |
"!!x y. (bintrunc n x) AND (bintrunc n y) = bintrunc n (x AND y)" |
|
310 |
"!!x y. (bintrunc n x) OR (bintrunc n y) = bintrunc n (x OR y)" |
|
311 |
apply (induct n) |
|
312 |
apply auto |
|
313 |
apply (case_tac [!] x rule: bin_exhaust) |
|
314 |
apply (case_tac [!] y rule: bin_exhaust) |
|
315 |
apply auto |
|
316 |
done |
|
317 |
||
318 |
lemma bin_trunc_xor: |
|
319 |
"!!x y. bintrunc n (bintrunc n x XOR bintrunc n y) = |
|
320 |
bintrunc n (x XOR y)" |
|
321 |
apply (induct n) |
|
322 |
apply auto |
|
323 |
apply (case_tac [!] x rule: bin_exhaust) |
|
324 |
apply (case_tac [!] y rule: bin_exhaust) |
|
325 |
apply auto |
|
326 |
done |
|
327 |
||
328 |
lemma bin_trunc_not: |
|
329 |
"!!x. bintrunc n (NOT (bintrunc n x)) = bintrunc n (NOT x)" |
|
330 |
apply (induct n) |
|
331 |
apply auto |
|
332 |
apply (case_tac [!] x rule: bin_exhaust) |
|
333 |
apply auto |
|
334 |
done |
|
335 |
||
336 |
(* want theorems of the form of bin_trunc_xor *) |
|
337 |
lemma bintr_bintr_i: |
|
338 |
"x = bintrunc n y ==> bintrunc n x = bintrunc n y" |
|
339 |
by auto |
|
340 |
||
341 |
lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i] |
|
342 |
lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i] |
|
343 |
||
344 |
subsection {* Setting and clearing bits *} |
|
345 |
||
346 |
consts |
|
347 |
bin_sc :: "nat => bit => int => int" |
|
348 |
||
349 |
primrec |
|
350 |
Z : "bin_sc 0 b w = bin_rest w BIT b" |
|
351 |
Suc : |
|
352 |
"bin_sc (Suc n) b w = bin_sc n b (bin_rest w) BIT bin_last w" |
|
353 |
||
24333 | 354 |
(** nth bit, set/clear **) |
355 |
||
356 |
lemma bin_nth_sc [simp]: |
|
357 |
"!!w. bin_nth (bin_sc n b w) n = (b = bit.B1)" |
|
358 |
by (induct n) auto |
|
359 |
||
360 |
lemma bin_sc_sc_same [simp]: |
|
361 |
"!!w. bin_sc n c (bin_sc n b w) = bin_sc n c w" |
|
362 |
by (induct n) auto |
|
363 |
||
364 |
lemma bin_sc_sc_diff: |
|
365 |
"!!w m. m ~= n ==> |
|
366 |
bin_sc m c (bin_sc n b w) = bin_sc n b (bin_sc m c w)" |
|
367 |
apply (induct n) |
|
368 |
apply (case_tac [!] m) |
|
369 |
apply auto |
|
370 |
done |
|
371 |
||
372 |
lemma bin_nth_sc_gen: |
|
373 |
"!!w m. bin_nth (bin_sc n b w) m = (if m = n then b = bit.B1 else bin_nth w m)" |
|
374 |
by (induct n) (case_tac [!] m, auto) |
|
375 |
||
376 |
lemma bin_sc_nth [simp]: |
|
377 |
"!!w. (bin_sc n (If (bin_nth w n) bit.B1 bit.B0) w) = w" |
|
24465 | 378 |
by (induct n) auto |
24333 | 379 |
|
380 |
lemma bin_sign_sc [simp]: |
|
381 |
"!!w. bin_sign (bin_sc n b w) = bin_sign w" |
|
382 |
by (induct n) auto |
|
383 |
||
384 |
lemma bin_sc_bintr [simp]: |
|
385 |
"!!w m. bintrunc m (bin_sc n x (bintrunc m (w))) = bintrunc m (bin_sc n x w)" |
|
386 |
apply (induct n) |
|
387 |
apply (case_tac [!] w rule: bin_exhaust) |
|
388 |
apply (case_tac [!] m, auto) |
|
389 |
done |
|
390 |
||
391 |
lemma bin_clr_le: |
|
392 |
"!!w. bin_sc n bit.B0 w <= w" |
|
393 |
apply (induct n) |
|
394 |
apply (case_tac [!] w rule: bin_exhaust) |
|
395 |
apply auto |
|
396 |
apply (unfold Bit_def) |
|
397 |
apply (simp_all split: bit.split) |
|
398 |
done |
|
399 |
||
400 |
lemma bin_set_ge: |
|
401 |
"!!w. bin_sc n bit.B1 w >= w" |
|
402 |
apply (induct n) |
|
403 |
apply (case_tac [!] w rule: bin_exhaust) |
|
404 |
apply auto |
|
405 |
apply (unfold Bit_def) |
|
406 |
apply (simp_all split: bit.split) |
|
407 |
done |
|
408 |
||
409 |
lemma bintr_bin_clr_le: |
|
410 |
"!!w m. bintrunc n (bin_sc m bit.B0 w) <= bintrunc n w" |
|
411 |
apply (induct n) |
|
412 |
apply simp |
|
413 |
apply (case_tac w rule: bin_exhaust) |
|
414 |
apply (case_tac m) |
|
415 |
apply auto |
|
416 |
apply (unfold Bit_def) |
|
417 |
apply (simp_all split: bit.split) |
|
418 |
done |
|
419 |
||
420 |
lemma bintr_bin_set_ge: |
|
421 |
"!!w m. bintrunc n (bin_sc m bit.B1 w) >= bintrunc n w" |
|
422 |
apply (induct n) |
|
423 |
apply simp |
|
424 |
apply (case_tac w rule: bin_exhaust) |
|
425 |
apply (case_tac m) |
|
426 |
apply auto |
|
427 |
apply (unfold Bit_def) |
|
428 |
apply (simp_all split: bit.split) |
|
429 |
done |
|
430 |
||
431 |
lemma bin_sc_FP [simp]: "bin_sc n bit.B0 Numeral.Pls = Numeral.Pls" |
|
432 |
by (induct n) auto |
|
433 |
||
434 |
lemma bin_sc_TM [simp]: "bin_sc n bit.B1 Numeral.Min = Numeral.Min" |
|
435 |
by (induct n) auto |
|
436 |
||
437 |
lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP |
|
438 |
||
439 |
lemma bin_sc_minus: |
|
440 |
"0 < n ==> bin_sc (Suc (n - 1)) b w = bin_sc n b w" |
|
441 |
by auto |
|
442 |
||
443 |
lemmas bin_sc_Suc_minus = |
|
444 |
trans [OF bin_sc_minus [symmetric] bin_sc.Suc, standard] |
|
445 |
||
446 |
lemmas bin_sc_Suc_pred [simp] = |
|
447 |
bin_sc_Suc_minus [of "number_of bin", simplified nobm1, standard] |
|
448 |
||
24465 | 449 |
subsection {* Operations on lists of booleans *} |
450 |
||
451 |
consts |
|
452 |
bin_to_bl :: "nat => int => bool list" |
|
453 |
bin_to_bl_aux :: "nat => int => bool list => bool list" |
|
454 |
bl_to_bin :: "bool list => int" |
|
455 |
bl_to_bin_aux :: "int => bool list => int" |
|
456 |
||
457 |
bl_of_nth :: "nat => (nat => bool) => bool list" |
|
458 |
||
459 |
primrec |
|
460 |
Nil : "bl_to_bin_aux w [] = w" |
|
461 |
Cons : "bl_to_bin_aux w (b # bs) = |
|
462 |
bl_to_bin_aux (w BIT (if b then bit.B1 else bit.B0)) bs" |
|
463 |
||
464 |
primrec |
|
465 |
Z : "bin_to_bl_aux 0 w bl = bl" |
|
466 |
Suc : "bin_to_bl_aux (Suc n) w bl = |
|
467 |
bin_to_bl_aux n (bin_rest w) ((bin_last w = bit.B1) # bl)" |
|
468 |
||
469 |
defs |
|
470 |
bin_to_bl_def : "bin_to_bl n w == bin_to_bl_aux n w []" |
|
471 |
bl_to_bin_def : "bl_to_bin bs == bl_to_bin_aux Numeral.Pls bs" |
|
472 |
||
473 |
primrec |
|
474 |
Suc : "bl_of_nth (Suc n) f = f n # bl_of_nth n f" |
|
475 |
Z : "bl_of_nth 0 f = []" |
|
476 |
||
477 |
consts |
|
478 |
takefill :: "'a => nat => 'a list => 'a list" |
|
479 |
app2 :: "('a => 'b => 'c) => 'a list => 'b list => 'c list" |
|
480 |
||
481 |
-- "takefill - like take but if argument list too short," |
|
482 |
-- "extends result to get requested length" |
|
483 |
primrec |
|
484 |
Z : "takefill fill 0 xs = []" |
|
485 |
Suc : "takefill fill (Suc n) xs = ( |
|
486 |
case xs of [] => fill # takefill fill n xs |
|
487 |
| y # ys => y # takefill fill n ys)" |
|
488 |
||
489 |
defs |
|
490 |
app2_def : "app2 f as bs == map (split f) (zip as bs)" |
|
491 |
||
24364 | 492 |
subsection {* Splitting and concatenation *} |
24333 | 493 |
|
24364 | 494 |
-- "rcat and rsplit" |
495 |
consts |
|
496 |
bin_rcat :: "nat => int list => int" |
|
497 |
bin_rsplit_aux :: "nat * int list * nat * int => int list" |
|
498 |
bin_rsplit :: "nat => (nat * int) => int list" |
|
499 |
bin_rsplitl_aux :: "nat * int list * nat * int => int list" |
|
500 |
bin_rsplitl :: "nat => (nat * int) => int list" |
|
501 |
||
502 |
recdef bin_rsplit_aux "measure (fst o snd o snd)" |
|
503 |
"bin_rsplit_aux (n, bs, (m, c)) = |
|
504 |
(if m = 0 | n = 0 then bs else |
|
505 |
let (a, b) = bin_split n c |
|
506 |
in bin_rsplit_aux (n, b # bs, (m - n, a)))" |
|
507 |
||
508 |
recdef bin_rsplitl_aux "measure (fst o snd o snd)" |
|
509 |
"bin_rsplitl_aux (n, bs, (m, c)) = |
|
510 |
(if m = 0 | n = 0 then bs else |
|
511 |
let (a, b) = bin_split (min m n) c |
|
512 |
in bin_rsplitl_aux (n, b # bs, (m - n, a)))" |
|
513 |
||
514 |
defs |
|
515 |
bin_rcat_def : "bin_rcat n bs == foldl (%u v. bin_cat u n v) Numeral.Pls bs" |
|
516 |
bin_rsplit_def : "bin_rsplit n w == bin_rsplit_aux (n, [], w)" |
|
517 |
bin_rsplitl_def : "bin_rsplitl n w == bin_rsplitl_aux (n, [], w)" |
|
518 |
||
519 |
||
520 |
(* potential for looping *) |
|
521 |
declare bin_rsplit_aux.simps [simp del] |
|
522 |
declare bin_rsplitl_aux.simps [simp del] |
|
523 |
||
524 |
lemma bin_sign_cat: |
|
525 |
"!!y. bin_sign (bin_cat x n y) = bin_sign x" |
|
526 |
by (induct n) auto |
|
527 |
||
528 |
lemma bin_cat_Suc_Bit: |
|
529 |
"bin_cat w (Suc n) (v BIT b) = bin_cat w n v BIT b" |
|
530 |
by auto |
|
531 |
||
532 |
lemma bin_nth_cat: |
|
533 |
"!!n y. bin_nth (bin_cat x k y) n = |
|
534 |
(if n < k then bin_nth y n else bin_nth x (n - k))" |
|
535 |
apply (induct k) |
|
536 |
apply clarsimp |
|
537 |
apply (case_tac n, auto) |
|
24333 | 538 |
done |
539 |
||
24364 | 540 |
lemma bin_nth_split: |
541 |
"!!b c. bin_split n c = (a, b) ==> |
|
542 |
(ALL k. bin_nth a k = bin_nth c (n + k)) & |
|
543 |
(ALL k. bin_nth b k = (k < n & bin_nth c k))" |
|
24333 | 544 |
apply (induct n) |
24364 | 545 |
apply clarsimp |
546 |
apply (clarsimp simp: Let_def split: ls_splits) |
|
547 |
apply (case_tac k) |
|
548 |
apply auto |
|
549 |
done |
|
550 |
||
551 |
lemma bin_cat_assoc: |
|
552 |
"!!z. bin_cat (bin_cat x m y) n z = bin_cat x (m + n) (bin_cat y n z)" |
|
553 |
by (induct n) auto |
|
554 |
||
555 |
lemma bin_cat_assoc_sym: "!!z m. |
|
556 |
bin_cat x m (bin_cat y n z) = bin_cat (bin_cat x (m - n) y) (min m n) z" |
|
557 |
apply (induct n, clarsimp) |
|
558 |
apply (case_tac m, auto) |
|
24333 | 559 |
done |
560 |
||
24364 | 561 |
lemma bin_cat_Pls [simp]: |
562 |
"!!w. bin_cat Numeral.Pls n w = bintrunc n w" |
|
563 |
by (induct n) auto |
|
564 |
||
565 |
lemma bintr_cat1: |
|
566 |
"!!b. bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b" |
|
567 |
by (induct n) auto |
|
568 |
||
569 |
lemma bintr_cat: "bintrunc m (bin_cat a n b) = |
|
570 |
bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)" |
|
571 |
by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr) |
|
572 |
||
573 |
lemma bintr_cat_same [simp]: |
|
574 |
"bintrunc n (bin_cat a n b) = bintrunc n b" |
|
575 |
by (auto simp add : bintr_cat) |
|
576 |
||
577 |
lemma cat_bintr [simp]: |
|
578 |
"!!b. bin_cat a n (bintrunc n b) = bin_cat a n b" |
|
579 |
by (induct n) auto |
|
580 |
||
581 |
lemma split_bintrunc: |
|
582 |
"!!b c. bin_split n c = (a, b) ==> b = bintrunc n c" |
|
583 |
by (induct n) (auto simp: Let_def split: ls_splits) |
|
584 |
||
585 |
lemma bin_cat_split: |
|
586 |
"!!v w. bin_split n w = (u, v) ==> w = bin_cat u n v" |
|
587 |
by (induct n) (auto simp: Let_def split: ls_splits) |
|
588 |
||
589 |
lemma bin_split_cat: |
|
590 |
"!!w. bin_split n (bin_cat v n w) = (v, bintrunc n w)" |
|
591 |
by (induct n) auto |
|
592 |
||
593 |
lemma bin_split_Pls [simp]: |
|
594 |
"bin_split n Numeral.Pls = (Numeral.Pls, Numeral.Pls)" |
|
595 |
by (induct n) (auto simp: Let_def split: ls_splits) |
|
596 |
||
597 |
lemma bin_split_Min [simp]: |
|
598 |
"bin_split n Numeral.Min = (Numeral.Min, bintrunc n Numeral.Min)" |
|
599 |
by (induct n) (auto simp: Let_def split: ls_splits) |
|
600 |
||
601 |
lemma bin_split_trunc: |
|
602 |
"!!m b c. bin_split (min m n) c = (a, b) ==> |
|
603 |
bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)" |
|
604 |
apply (induct n, clarsimp) |
|
605 |
apply (simp add: bin_rest_trunc Let_def split: ls_splits) |
|
606 |
apply (case_tac m) |
|
607 |
apply (auto simp: Let_def split: ls_splits) |
|
24333 | 608 |
done |
609 |
||
24364 | 610 |
lemma bin_split_trunc1: |
611 |
"!!m b c. bin_split n c = (a, b) ==> |
|
612 |
bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)" |
|
613 |
apply (induct n, clarsimp) |
|
614 |
apply (simp add: bin_rest_trunc Let_def split: ls_splits) |
|
615 |
apply (case_tac m) |
|
616 |
apply (auto simp: Let_def split: ls_splits) |
|
617 |
done |
|
24333 | 618 |
|
24364 | 619 |
lemma bin_cat_num: |
620 |
"!!b. bin_cat a n b = a * 2 ^ n + bintrunc n b" |
|
621 |
apply (induct n, clarsimp) |
|
622 |
apply (simp add: Bit_def cong: number_of_False_cong) |
|
623 |
done |
|
624 |
||
625 |
lemma bin_split_num: |
|
626 |
"!!b. bin_split n b = (b div 2 ^ n, b mod 2 ^ n)" |
|
627 |
apply (induct n, clarsimp) |
|
628 |
apply (simp add: bin_rest_div zdiv_zmult2_eq) |
|
629 |
apply (case_tac b rule: bin_exhaust) |
|
630 |
apply simp |
|
631 |
apply (simp add: Bit_def zmod_zmult_zmult1 p1mod22k |
|
632 |
split: bit.split |
|
633 |
cong: number_of_False_cong) |
|
634 |
done |
|
635 |
||
636 |
subsection {* Miscellaneous lemmas *} |
|
24333 | 637 |
|
638 |
lemma nth_2p_bin: |
|
639 |
"!!m. bin_nth (2 ^ n) m = (m = n)" |
|
640 |
apply (induct n) |
|
641 |
apply clarsimp |
|
642 |
apply safe |
|
643 |
apply (case_tac m) |
|
644 |
apply (auto simp: trans [OF numeral_1_eq_1 [symmetric] number_of_eq]) |
|
645 |
apply (case_tac m) |
|
646 |
apply (auto simp: Bit_B0_2t [symmetric]) |
|
647 |
done |
|
648 |
||
649 |
(* for use when simplifying with bin_nth_Bit *) |
|
650 |
||
651 |
lemma ex_eq_or: |
|
652 |
"(EX m. n = Suc m & (m = k | P m)) = (n = Suc k | (EX m. n = Suc m & P m))" |
|
653 |
by auto |
|
654 |
||
655 |
end |
|
656 |