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