24333
|
1 |
(*
|
|
2 |
ID: $Id$
|
|
3 |
Author: Jeremy Dawson and Gerwin Klein, NICTA
|
|
4 |
|
|
5 |
contains theorems to do with bit-wise (logical) operations on words
|
|
6 |
*)
|
|
7 |
theory WordBitwise imports WordArith begin
|
|
8 |
|
|
9 |
lemmas bin_log_bintrs = bin_trunc_not bin_trunc_xor bin_trunc_and bin_trunc_or
|
|
10 |
|
|
11 |
(* following definitions require both arithmetic and bit-wise word operations *)
|
|
12 |
|
|
13 |
(* to get word_no_log_defs from word_log_defs, using bin_log_bintrs *)
|
|
14 |
lemmas wils1 = bin_log_bintrs [THEN word_ubin.norm_eq_iff [THEN iffD1],
|
|
15 |
folded word_ubin.eq_norm, THEN eq_reflection, standard]
|
|
16 |
|
|
17 |
(* the binary operations only *)
|
|
18 |
lemmas word_log_binary_defs =
|
|
19 |
word_and_def word_or_def word_xor_def
|
|
20 |
|
|
21 |
lemmas word_no_log_defs [simp] =
|
|
22 |
word_not_def [where a="number_of ?a",
|
|
23 |
unfolded word_no_wi wils1, folded word_no_wi]
|
|
24 |
word_log_binary_defs [where a="number_of ?a" and b="number_of ?b",
|
|
25 |
unfolded word_no_wi wils1, folded word_no_wi]
|
|
26 |
|
|
27 |
lemmas word_wi_log_defs = word_no_log_defs [unfolded word_no_wi]
|
|
28 |
|
|
29 |
lemma uint_or: "uint (x OR y) = int_or (uint x) (uint y)"
|
|
30 |
by (simp add: word_or_def word_no_wi [symmetric] int_number_of
|
|
31 |
bin_trunc_ao(2) [symmetric])
|
|
32 |
|
|
33 |
lemma uint_and: "uint (x AND y) = int_and (uint x) (uint y)"
|
|
34 |
by (simp add: word_and_def int_number_of word_no_wi [symmetric]
|
|
35 |
bin_trunc_ao(1) [symmetric])
|
|
36 |
|
|
37 |
lemma word_ops_nth_size:
|
|
38 |
"n < size (x::'a::len0 word) ==>
|
|
39 |
(x OR y) !! n = (x !! n | y !! n) &
|
|
40 |
(x AND y) !! n = (x !! n & y !! n) &
|
|
41 |
(x XOR y) !! n = (x !! n ~= y !! n) &
|
|
42 |
(NOT x) !! n = (~ x !! n)"
|
|
43 |
unfolding word_size word_no_wi word_test_bit_def word_log_defs
|
|
44 |
by (clarsimp simp add : word_ubin.eq_norm nth_bintr bin_nth_ops)
|
|
45 |
|
|
46 |
lemma word_ao_nth:
|
|
47 |
fixes x :: "'a::len0 word"
|
|
48 |
shows "(x OR y) !! n = (x !! n | y !! n) &
|
|
49 |
(x AND y) !! n = (x !! n & y !! n)"
|
|
50 |
apply (cases "n < size x")
|
|
51 |
apply (drule_tac y = "y" in word_ops_nth_size)
|
|
52 |
apply simp
|
|
53 |
apply (simp add : test_bit_bin word_size)
|
|
54 |
done
|
|
55 |
|
|
56 |
(* get from commutativity, associativity etc of int_and etc
|
|
57 |
to same for word_and etc *)
|
|
58 |
|
|
59 |
lemmas bwsimps =
|
|
60 |
word_of_int_homs(2)
|
|
61 |
word_0_wi_Pls
|
|
62 |
word_m1_wi_Min
|
|
63 |
word_wi_log_defs
|
|
64 |
|
|
65 |
lemma word_bw_assocs:
|
|
66 |
fixes x :: "'a::len0 word"
|
|
67 |
shows
|
|
68 |
"(x AND y) AND z = x AND y AND z"
|
|
69 |
"(x OR y) OR z = x OR y OR z"
|
|
70 |
"(x XOR y) XOR z = x XOR y XOR z"
|
|
71 |
using word_of_int_Ex [where x=x]
|
|
72 |
word_of_int_Ex [where x=y]
|
|
73 |
word_of_int_Ex [where x=z]
|
|
74 |
by (auto simp: bwsimps)
|
|
75 |
|
|
76 |
lemma word_bw_comms:
|
|
77 |
fixes x :: "'a::len0 word"
|
|
78 |
shows
|
|
79 |
"x AND y = y AND x"
|
|
80 |
"x OR y = y OR x"
|
|
81 |
"x XOR y = y XOR x"
|
|
82 |
using word_of_int_Ex [where x=x]
|
|
83 |
word_of_int_Ex [where x=y]
|
|
84 |
by (auto simp: bwsimps)
|
|
85 |
|
|
86 |
lemma word_bw_lcs:
|
|
87 |
fixes x :: "'a::len0 word"
|
|
88 |
shows
|
|
89 |
"y AND x AND z = x AND y AND z"
|
|
90 |
"y OR x OR z = x OR y OR z"
|
|
91 |
"y XOR x XOR z = x XOR y XOR z"
|
|
92 |
using word_of_int_Ex [where x=x]
|
|
93 |
word_of_int_Ex [where x=y]
|
|
94 |
word_of_int_Ex [where x=z]
|
|
95 |
by (auto simp: bwsimps)
|
|
96 |
|
|
97 |
lemma word_log_esimps [simp]:
|
|
98 |
fixes x :: "'a::len0 word"
|
|
99 |
shows
|
|
100 |
"x AND 0 = 0"
|
|
101 |
"x AND -1 = x"
|
|
102 |
"x OR 0 = x"
|
|
103 |
"x OR -1 = -1"
|
|
104 |
"x XOR 0 = x"
|
|
105 |
"x XOR -1 = NOT x"
|
|
106 |
"0 AND x = 0"
|
|
107 |
"-1 AND x = x"
|
|
108 |
"0 OR x = x"
|
|
109 |
"-1 OR x = -1"
|
|
110 |
"0 XOR x = x"
|
|
111 |
"-1 XOR x = NOT x"
|
|
112 |
using word_of_int_Ex [where x=x]
|
|
113 |
by (auto simp: bwsimps)
|
|
114 |
|
|
115 |
lemma word_not_dist:
|
|
116 |
fixes x :: "'a::len0 word"
|
|
117 |
shows
|
|
118 |
"NOT (x OR y) = NOT x AND NOT y"
|
|
119 |
"NOT (x AND y) = NOT x OR NOT y"
|
|
120 |
using word_of_int_Ex [where x=x]
|
|
121 |
word_of_int_Ex [where x=y]
|
|
122 |
by (auto simp: bwsimps bbw_not_dist)
|
|
123 |
|
|
124 |
lemma word_bw_same:
|
|
125 |
fixes x :: "'a::len0 word"
|
|
126 |
shows
|
|
127 |
"x AND x = x"
|
|
128 |
"x OR x = x"
|
|
129 |
"x XOR x = 0"
|
|
130 |
using word_of_int_Ex [where x=x]
|
|
131 |
by (auto simp: bwsimps)
|
|
132 |
|
|
133 |
lemma word_ao_absorbs [simp]:
|
|
134 |
fixes x :: "'a::len0 word"
|
|
135 |
shows
|
|
136 |
"x AND (y OR x) = x"
|
|
137 |
"x OR y AND x = x"
|
|
138 |
"x AND (x OR y) = x"
|
|
139 |
"y AND x OR x = x"
|
|
140 |
"(y OR x) AND x = x"
|
|
141 |
"x OR x AND y = x"
|
|
142 |
"(x OR y) AND x = x"
|
|
143 |
"x AND y OR x = x"
|
|
144 |
using word_of_int_Ex [where x=x]
|
|
145 |
word_of_int_Ex [where x=y]
|
|
146 |
by (auto simp: bwsimps)
|
|
147 |
|
|
148 |
lemma word_not_not [simp]:
|
|
149 |
"NOT NOT (x::'a::len0 word) = x"
|
|
150 |
using word_of_int_Ex [where x=x]
|
|
151 |
by (auto simp: bwsimps)
|
|
152 |
|
|
153 |
lemma word_ao_dist:
|
|
154 |
fixes x :: "'a::len0 word"
|
|
155 |
shows "(x OR y) AND z = x AND z OR y AND z"
|
|
156 |
using word_of_int_Ex [where x=x]
|
|
157 |
word_of_int_Ex [where x=y]
|
|
158 |
word_of_int_Ex [where x=z]
|
|
159 |
by (auto simp: bwsimps bbw_ao_dist simp del: bin_ops_comm)
|
|
160 |
|
|
161 |
lemma word_oa_dist:
|
|
162 |
fixes x :: "'a::len0 word"
|
|
163 |
shows "x AND y OR z = (x OR z) AND (y OR z)"
|
|
164 |
using word_of_int_Ex [where x=x]
|
|
165 |
word_of_int_Ex [where x=y]
|
|
166 |
word_of_int_Ex [where x=z]
|
|
167 |
by (auto simp: bwsimps bbw_oa_dist simp del: bin_ops_comm)
|
|
168 |
|
|
169 |
lemma word_add_not [simp]:
|
|
170 |
fixes x :: "'a::len0 word"
|
|
171 |
shows "x + NOT x = -1"
|
|
172 |
using word_of_int_Ex [where x=x]
|
|
173 |
by (auto simp: bwsimps bin_add_not)
|
|
174 |
|
|
175 |
lemma word_plus_and_or [simp]:
|
|
176 |
fixes x :: "'a::len0 word"
|
|
177 |
shows "(x AND y) + (x OR y) = x + y"
|
|
178 |
using word_of_int_Ex [where x=x]
|
|
179 |
word_of_int_Ex [where x=y]
|
|
180 |
by (auto simp: bwsimps plus_and_or)
|
|
181 |
|
|
182 |
lemma leoa:
|
|
183 |
fixes x :: "'a::len0 word"
|
|
184 |
shows "(w = (x OR y)) ==> (y = (w AND y))" by auto
|
|
185 |
lemma leao:
|
|
186 |
fixes x' :: "'a::len0 word"
|
|
187 |
shows "(w' = (x' AND y')) ==> (x' = (x' OR w'))" by auto
|
|
188 |
|
|
189 |
lemmas word_ao_equiv = leao [COMP leoa [COMP iffI]]
|
|
190 |
|
|
191 |
lemma le_word_or2: "x <= x OR (y::'a::len0 word)"
|
|
192 |
unfolding word_le_def uint_or
|
|
193 |
by (auto intro: le_int_or)
|
|
194 |
|
|
195 |
lemmas le_word_or1 = xtr3 [OF word_bw_comms (2) le_word_or2, standard]
|
|
196 |
lemmas word_and_le1 =
|
|
197 |
xtr3 [OF word_ao_absorbs (4) [symmetric] le_word_or2, standard]
|
|
198 |
lemmas word_and_le2 =
|
|
199 |
xtr3 [OF word_ao_absorbs (8) [symmetric] le_word_or2, standard]
|
|
200 |
|
|
201 |
lemma bl_word_not: "to_bl (NOT w) = map Not (to_bl w)"
|
|
202 |
unfolding to_bl_def word_log_defs
|
|
203 |
by (simp add: bl_not_bin int_number_of word_no_wi [symmetric])
|
|
204 |
|
|
205 |
lemma bl_word_xor: "to_bl (v XOR w) = app2 op ~= (to_bl v) (to_bl w)"
|
|
206 |
unfolding to_bl_def word_log_defs bl_xor_bin
|
|
207 |
by (simp add: int_number_of word_no_wi [symmetric])
|
|
208 |
|
|
209 |
lemma bl_word_or: "to_bl (v OR w) = app2 op | (to_bl v) (to_bl w)"
|
|
210 |
unfolding to_bl_def word_log_defs
|
|
211 |
by (simp add: bl_or_bin int_number_of word_no_wi [symmetric])
|
|
212 |
|
|
213 |
lemma bl_word_and: "to_bl (v AND w) = app2 op & (to_bl v) (to_bl w)"
|
|
214 |
unfolding to_bl_def word_log_defs
|
|
215 |
by (simp add: bl_and_bin int_number_of word_no_wi [symmetric])
|
|
216 |
|
|
217 |
lemma word_lsb_alt: "lsb (w::'a::len0 word) = test_bit w 0"
|
|
218 |
by (auto simp: word_test_bit_def word_lsb_def)
|
|
219 |
|
|
220 |
lemma word_lsb_1_0: "lsb (1::'a::len word) & ~ lsb (0::'b::len0 word)"
|
|
221 |
unfolding word_lsb_def word_1_no word_0_no by auto
|
|
222 |
|
|
223 |
lemma word_lsb_last: "lsb (w::'a::len word) = last (to_bl w)"
|
|
224 |
apply (unfold word_lsb_def uint_bl bin_to_bl_def)
|
|
225 |
apply (rule_tac bin="uint w" in bin_exhaust)
|
|
226 |
apply (cases "size w")
|
|
227 |
apply auto
|
|
228 |
apply (auto simp add: bin_to_bl_aux_alt)
|
|
229 |
done
|
|
230 |
|
|
231 |
lemma word_lsb_int: "lsb w = (uint w mod 2 = 1)"
|
|
232 |
unfolding word_lsb_def bin_last_mod by auto
|
|
233 |
|
|
234 |
lemma word_msb_sint: "msb w = (sint w < 0)"
|
|
235 |
unfolding word_msb_def
|
|
236 |
by (simp add : sign_Min_lt_0 int_number_of)
|
|
237 |
|
|
238 |
lemma word_msb_no':
|
|
239 |
"w = number_of bin ==> msb (w::'a::len word) = bin_nth bin (size w - 1)"
|
|
240 |
unfolding word_msb_def word_number_of_def
|
|
241 |
by (clarsimp simp add: word_sbin.eq_norm word_size bin_sign_lem)
|
|
242 |
|
|
243 |
lemmas word_msb_no = refl [THEN word_msb_no', unfolded word_size]
|
|
244 |
|
|
245 |
lemma word_msb_nth': "msb (w::'a::len word) = bin_nth (uint w) (size w - 1)"
|
|
246 |
apply (unfold word_size)
|
|
247 |
apply (rule trans [OF _ word_msb_no])
|
|
248 |
apply (simp add : word_number_of_def)
|
|
249 |
done
|
|
250 |
|
|
251 |
lemmas word_msb_nth = word_msb_nth' [unfolded word_size]
|
|
252 |
|
|
253 |
lemma word_msb_alt: "msb (w::'a::len word) = hd (to_bl w)"
|
|
254 |
apply (unfold word_msb_nth uint_bl)
|
|
255 |
apply (subst hd_conv_nth)
|
|
256 |
apply (rule length_greater_0_conv [THEN iffD1])
|
|
257 |
apply simp
|
|
258 |
apply (simp add : nth_bin_to_bl word_size)
|
|
259 |
done
|
|
260 |
|
|
261 |
lemma word_set_nth:
|
|
262 |
"set_bit w n (test_bit w n) = (w::'a::len0 word)"
|
|
263 |
unfolding word_test_bit_def word_set_bit_def by auto
|
|
264 |
|
|
265 |
lemma bin_nth_uint':
|
|
266 |
"bin_nth (uint w) n = (rev (bin_to_bl (size w) (uint w)) ! n & n < size w)"
|
|
267 |
apply (unfold word_size)
|
|
268 |
apply (safe elim!: bin_nth_uint_imp)
|
|
269 |
apply (frule bin_nth_uint_imp)
|
|
270 |
apply (fast dest!: bin_nth_bl)+
|
|
271 |
done
|
|
272 |
|
|
273 |
lemmas bin_nth_uint = bin_nth_uint' [unfolded word_size]
|
|
274 |
|
|
275 |
lemma test_bit_bl: "w !! n = (rev (to_bl w) ! n & n < size w)"
|
|
276 |
unfolding to_bl_def word_test_bit_def word_size
|
|
277 |
by (rule bin_nth_uint)
|
|
278 |
|
|
279 |
lemma to_bl_nth: "n < size w ==> to_bl w ! n = w !! (size w - Suc n)"
|
|
280 |
apply (unfold test_bit_bl)
|
|
281 |
apply clarsimp
|
|
282 |
apply (rule trans)
|
|
283 |
apply (rule nth_rev_alt)
|
|
284 |
apply (auto simp add: word_size)
|
|
285 |
done
|
|
286 |
|
|
287 |
lemma test_bit_set:
|
|
288 |
fixes w :: "'a::len0 word"
|
|
289 |
shows "(set_bit w n x) !! n = (n < size w & x)"
|
|
290 |
unfolding word_size word_test_bit_def word_set_bit_def
|
|
291 |
by (clarsimp simp add : word_ubin.eq_norm nth_bintr)
|
|
292 |
|
|
293 |
lemma test_bit_set_gen:
|
|
294 |
fixes w :: "'a::len0 word"
|
|
295 |
shows "test_bit (set_bit w n x) m =
|
|
296 |
(if m = n then n < size w & x else test_bit w m)"
|
|
297 |
apply (unfold word_size word_test_bit_def word_set_bit_def)
|
|
298 |
apply (clarsimp simp add: word_ubin.eq_norm nth_bintr bin_nth_sc_gen)
|
|
299 |
apply (auto elim!: test_bit_size [unfolded word_size]
|
|
300 |
simp add: word_test_bit_def [symmetric])
|
|
301 |
done
|
|
302 |
|
|
303 |
lemma of_bl_rep_False: "of_bl (replicate n False @ bs) = of_bl bs"
|
|
304 |
unfolding of_bl_def bl_to_bin_rep_F by auto
|
|
305 |
|
|
306 |
lemma msb_nth':
|
|
307 |
fixes w :: "'a::len word"
|
|
308 |
shows "msb w = w !! (size w - 1)"
|
|
309 |
unfolding word_msb_nth' word_test_bit_def by simp
|
|
310 |
|
|
311 |
lemmas msb_nth = msb_nth' [unfolded word_size]
|
|
312 |
|
|
313 |
lemmas msb0 = len_gt_0 [THEN diff_Suc_less, THEN
|
|
314 |
word_ops_nth_size [unfolded word_size], standard]
|
|
315 |
lemmas msb1 = msb0 [where i = 0]
|
|
316 |
lemmas word_ops_msb = msb1 [unfolded msb_nth [symmetric, unfolded One_nat_def]]
|
|
317 |
|
|
318 |
lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size], standard]
|
|
319 |
lemmas word_ops_lsb = lsb0 [unfolded word_lsb_alt]
|
|
320 |
|
|
321 |
lemma td_ext_nth':
|
|
322 |
"n = size (w::'a::len0 word) ==> ofn = set_bits ==> [w, ofn g] = l ==>
|
|
323 |
td_ext test_bit ofn {f. ALL i. f i --> i < n} (%h i. h i & i < n)"
|
|
324 |
apply (unfold word_size td_ext_def')
|
|
325 |
apply safe
|
|
326 |
apply (rule_tac [3] ext)
|
|
327 |
apply (rule_tac [4] ext)
|
|
328 |
apply (unfold word_size of_nth_def test_bit_bl)
|
|
329 |
apply safe
|
|
330 |
defer
|
|
331 |
apply (clarsimp simp: word_bl.Abs_inverse)+
|
|
332 |
apply (rule word_bl.Rep_inverse')
|
|
333 |
apply (rule sym [THEN trans])
|
|
334 |
apply (rule bl_of_nth_nth)
|
|
335 |
apply simp
|
|
336 |
apply (rule bl_of_nth_inj)
|
|
337 |
apply (clarsimp simp add : test_bit_bl word_size)
|
|
338 |
done
|
|
339 |
|
|
340 |
lemmas td_ext_nth = td_ext_nth' [OF refl refl refl, unfolded word_size]
|
|
341 |
|
|
342 |
interpretation test_bit:
|
|
343 |
td_ext ["op !! :: 'a::len0 word => nat => bool"
|
|
344 |
set_bits
|
|
345 |
"{f. \<forall>i. f i \<longrightarrow> i < len_of TYPE('a::len0)}"
|
|
346 |
"(\<lambda>h i. h i \<and> i < len_of TYPE('a::len0))"]
|
|
347 |
by (rule td_ext_nth)
|
|
348 |
|
|
349 |
declare test_bit.Rep' [simp del]
|
|
350 |
declare test_bit.Rep' [rule del]
|
|
351 |
|
|
352 |
lemmas td_nth = test_bit.td_thm
|
|
353 |
|
|
354 |
lemma word_set_set_same:
|
|
355 |
fixes w :: "'a::len0 word"
|
|
356 |
shows "set_bit (set_bit w n x) n y = set_bit w n y"
|
|
357 |
by (rule word_eqI) (simp add : test_bit_set_gen word_size)
|
|
358 |
|
|
359 |
lemma word_set_set_diff:
|
|
360 |
fixes w :: "'a::len0 word"
|
|
361 |
assumes "m ~= n"
|
|
362 |
shows "set_bit (set_bit w m x) n y = set_bit (set_bit w n y) m x"
|
|
363 |
by (rule word_eqI) (clarsimp simp add : test_bit_set_gen word_size prems)
|
|
364 |
|
|
365 |
lemma test_bit_no':
|
|
366 |
fixes w :: "'a::len0 word"
|
|
367 |
shows "w = number_of bin ==> test_bit w n = (n < size w & bin_nth bin n)"
|
|
368 |
unfolding word_test_bit_def word_number_of_def word_size
|
|
369 |
by (simp add : nth_bintr [symmetric] word_ubin.eq_norm)
|
|
370 |
|
|
371 |
lemmas test_bit_no =
|
|
372 |
refl [THEN test_bit_no', unfolded word_size, THEN eq_reflection, standard]
|
|
373 |
|
|
374 |
lemma nth_0: "~ (0::'a::len0 word) !! n"
|
|
375 |
unfolding test_bit_no word_0_no by auto
|
|
376 |
|
|
377 |
lemma nth_sint:
|
|
378 |
fixes w :: "'a::len word"
|
|
379 |
defines "l \<equiv> len_of TYPE ('a)"
|
|
380 |
shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))"
|
|
381 |
unfolding sint_uint l_def
|
|
382 |
by (clarsimp simp add: nth_sbintr word_test_bit_def [symmetric])
|
|
383 |
|
|
384 |
lemma word_lsb_no:
|
|
385 |
"lsb (number_of bin :: 'a :: len word) = (bin_last bin = bit.B1)"
|
|
386 |
unfolding word_lsb_alt test_bit_no by auto
|
|
387 |
|
|
388 |
lemma word_set_no:
|
|
389 |
"set_bit (number_of bin::'a::len0 word) n b =
|
|
390 |
number_of (bin_sc n (if b then bit.B1 else bit.B0) bin)"
|
|
391 |
apply (unfold word_set_bit_def word_number_of_def [symmetric])
|
|
392 |
apply (rule word_eqI)
|
|
393 |
apply (clarsimp simp: word_size bin_nth_sc_gen int_number_of
|
|
394 |
test_bit_no nth_bintr)
|
|
395 |
done
|
|
396 |
|
|
397 |
lemmas setBit_no = setBit_def [THEN trans [OF meta_eq_to_obj_eq word_set_no],
|
|
398 |
simplified if_simps, THEN eq_reflection, standard]
|
|
399 |
lemmas clearBit_no = clearBit_def [THEN trans [OF meta_eq_to_obj_eq word_set_no],
|
|
400 |
simplified if_simps, THEN eq_reflection, standard]
|
|
401 |
|
|
402 |
lemma to_bl_n1:
|
|
403 |
"to_bl (-1::'a::len0 word) = replicate (len_of TYPE ('a)) True"
|
|
404 |
apply (rule word_bl.Abs_inverse')
|
|
405 |
apply simp
|
|
406 |
apply (rule word_eqI)
|
|
407 |
apply (clarsimp simp add: word_size test_bit_no)
|
|
408 |
apply (auto simp add: word_bl.Abs_inverse test_bit_bl word_size)
|
|
409 |
done
|
|
410 |
|
|
411 |
lemma word_msb_n1: "msb (-1::'a::len word)"
|
|
412 |
unfolding word_msb_alt word_msb_alt to_bl_n1 by simp
|
|
413 |
|
|
414 |
declare word_set_set_same [simp] word_set_nth [simp]
|
|
415 |
test_bit_no [simp] word_set_no [simp] nth_0 [simp]
|
|
416 |
setBit_no [simp] clearBit_no [simp]
|
|
417 |
word_lsb_no [simp] word_msb_no [simp] word_msb_n1 [simp] word_lsb_1_0 [simp]
|
|
418 |
|
|
419 |
lemma word_set_nth_iff:
|
|
420 |
"(set_bit w n b = w) = (w !! n = b | n >= size (w::'a::len0 word))"
|
|
421 |
apply (rule iffI)
|
|
422 |
apply (rule disjCI)
|
|
423 |
apply (drule word_eqD)
|
|
424 |
apply (erule sym [THEN trans])
|
|
425 |
apply (simp add: test_bit_set)
|
|
426 |
apply (erule disjE)
|
|
427 |
apply clarsimp
|
|
428 |
apply (rule word_eqI)
|
|
429 |
apply (clarsimp simp add : test_bit_set_gen)
|
|
430 |
apply (drule test_bit_size)
|
|
431 |
apply force
|
|
432 |
done
|
|
433 |
|
|
434 |
lemma test_bit_2p':
|
|
435 |
"w = word_of_int (2 ^ n) ==>
|
|
436 |
w !! m = (m = n & m < size (w :: 'a :: len word))"
|
|
437 |
unfolding word_test_bit_def word_size
|
|
438 |
by (auto simp add: word_ubin.eq_norm nth_bintr nth_2p_bin)
|
|
439 |
|
|
440 |
lemmas test_bit_2p = refl [THEN test_bit_2p', unfolded word_size]
|
|
441 |
|
|
442 |
lemmas nth_w2p = test_bit_2p [unfolded of_int_number_of_eq
|
|
443 |
word_of_int [symmetric] of_int_power]
|
|
444 |
|
|
445 |
lemma uint_2p:
|
|
446 |
"(0::'a::len word) < 2 ^ n ==> uint (2 ^ n::'a::len word) = 2 ^ n"
|
|
447 |
apply (unfold word_arith_power_alt)
|
|
448 |
apply (case_tac "len_of TYPE ('a)")
|
|
449 |
apply clarsimp
|
|
450 |
apply (case_tac "nat")
|
|
451 |
apply clarsimp
|
|
452 |
apply (case_tac "n")
|
|
453 |
apply (clarsimp simp add : word_1_wi [symmetric])
|
|
454 |
apply (clarsimp simp add : word_0_wi [symmetric])
|
|
455 |
apply (drule word_gt_0 [THEN iffD1])
|
|
456 |
apply (safe intro!: word_eqI bin_nth_lem ext)
|
|
457 |
apply (auto simp add: test_bit_2p nth_2p_bin word_test_bit_def [symmetric])
|
|
458 |
done
|
|
459 |
|
|
460 |
lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a :: len word) = 2 ^ n"
|
|
461 |
apply (unfold word_arith_power_alt)
|
|
462 |
apply (case_tac "len_of TYPE ('a)")
|
|
463 |
apply clarsimp
|
|
464 |
apply (case_tac "nat")
|
|
465 |
apply (rule word_ubin.norm_eq_iff [THEN iffD1])
|
|
466 |
apply (rule box_equals)
|
|
467 |
apply (rule_tac [2] bintr_ariths (1))+
|
|
468 |
apply (clarsimp simp add : int_number_of)
|
|
469 |
apply simp
|
|
470 |
done
|
|
471 |
|
|
472 |
lemma bang_is_le: "x !! m ==> 2 ^ m <= (x :: 'a :: len word)"
|
|
473 |
apply (rule xtr3)
|
|
474 |
apply (rule_tac [2] y = "x" in le_word_or2)
|
|
475 |
apply (rule word_eqI)
|
|
476 |
apply (auto simp add: word_ao_nth nth_w2p word_size)
|
|
477 |
done
|
|
478 |
|
|
479 |
lemma word_clr_le:
|
|
480 |
fixes w :: "'a::len0 word"
|
|
481 |
shows "w >= set_bit w n False"
|
|
482 |
apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
|
|
483 |
apply simp
|
|
484 |
apply (rule order_trans)
|
|
485 |
apply (rule bintr_bin_clr_le)
|
|
486 |
apply simp
|
|
487 |
done
|
|
488 |
|
|
489 |
lemma word_set_ge:
|
|
490 |
fixes w :: "'a::len word"
|
|
491 |
shows "w <= set_bit w n True"
|
|
492 |
apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm)
|
|
493 |
apply simp
|
|
494 |
apply (rule order_trans [OF _ bintr_bin_set_ge])
|
|
495 |
apply simp
|
|
496 |
done
|
|
497 |
|
|
498 |
end
|
|
499 |
|