|
1 (* |
|
2 ID: $Id$ |
|
3 Author: Jeremy Dawson and Gerwin Klein, NICTA |
|
4 |
|
5 Basic definition of word type and basic theorems following from |
|
6 the definition of the word type |
|
7 *) |
|
8 |
|
9 |
|
10 theory WordDefinition imports Size BitSyntax BinBoolList begin |
|
11 |
|
12 typedef (open word) 'a word |
|
13 = "{(0::int) ..< 2^len_of TYPE('a::len0)}" by auto |
|
14 |
|
15 instance word :: (len0) number .. |
|
16 instance word :: (type) minus .. |
|
17 instance word :: (type) plus .. |
|
18 instance word :: (type) one .. |
|
19 instance word :: (type) zero .. |
|
20 instance word :: (type) times .. |
|
21 instance word :: (type) Divides.div .. |
|
22 instance word :: (type) power .. |
|
23 instance word :: (type) ord .. |
|
24 instance word :: (type) size .. |
|
25 instance word :: (type) inverse .. |
|
26 instance word :: (type) bit .. |
|
27 |
|
28 |
|
29 section "Type conversions and casting" |
|
30 |
|
31 constdefs |
|
32 -- {* representation of words using unsigned or signed bins, |
|
33 only difference in these is the type class *} |
|
34 word_of_int :: "int => 'a :: len0 word" |
|
35 "word_of_int w == Abs_word (bintrunc (len_of TYPE ('a)) w)" |
|
36 |
|
37 -- {* uint and sint cast a word to an integer, |
|
38 uint treats the word as unsigned, |
|
39 sint treats the most-significant-bit as a sign bit *} |
|
40 uint :: "'a :: len0 word => int" |
|
41 "uint w == Rep_word w" |
|
42 sint :: "'a :: len word => int" |
|
43 sint_uint: "sint w == sbintrunc (len_of TYPE ('a) - 1) (uint w)" |
|
44 unat :: "'a :: len0 word => nat" |
|
45 "unat w == nat (uint w)" |
|
46 |
|
47 -- "the sets of integers representing the words" |
|
48 uints :: "nat => int set" |
|
49 "uints n == range (bintrunc n)" |
|
50 sints :: "nat => int set" |
|
51 "sints n == range (sbintrunc (n - 1))" |
|
52 unats :: "nat => nat set" |
|
53 "unats n == {i. i < 2 ^ n}" |
|
54 norm_sint :: "nat => int => int" |
|
55 "norm_sint n w == (w + 2 ^ (n - 1)) mod 2 ^ n - 2 ^ (n - 1)" |
|
56 |
|
57 -- "cast a word to a different length" |
|
58 scast :: "'a :: len word => 'b :: len word" |
|
59 "scast w == word_of_int (sint w)" |
|
60 ucast :: "'a :: len0 word => 'b :: len0 word" |
|
61 "ucast w == word_of_int (uint w)" |
|
62 |
|
63 -- "whether a cast (or other) function is to a longer or shorter length" |
|
64 source_size :: "('a :: len0 word => 'b) => nat" |
|
65 "source_size c == let arb = arbitrary ; x = c arb in size arb" |
|
66 target_size :: "('a => 'b :: len0 word) => nat" |
|
67 "target_size c == size (c arbitrary)" |
|
68 is_up :: "('a :: len0 word => 'b :: len0 word) => bool" |
|
69 "is_up c == source_size c <= target_size c" |
|
70 is_down :: "('a :: len0 word => 'b :: len0 word) => bool" |
|
71 "is_down c == target_size c <= source_size c" |
|
72 |
|
73 constdefs |
|
74 of_bl :: "bool list => 'a :: len0 word" |
|
75 "of_bl bl == word_of_int (bl_to_bin bl)" |
|
76 to_bl :: "'a :: len0 word => bool list" |
|
77 "to_bl w == |
|
78 bin_to_bl (len_of TYPE ('a)) (uint w)" |
|
79 |
|
80 word_reverse :: "'a :: len0 word => 'a word" |
|
81 "word_reverse w == of_bl (rev (to_bl w))" |
|
82 |
|
83 defs (overloaded) |
|
84 word_size: "size (w :: 'a :: len0 word) == len_of TYPE('a)" |
|
85 word_number_of_def: "number_of w == word_of_int w" |
|
86 |
|
87 constdefs |
|
88 word_int_case :: "(int => 'b) => ('a :: len0 word) => 'b" |
|
89 "word_int_case f w == f (uint w)" |
|
90 |
|
91 syntax |
|
92 of_int :: "int => 'a" |
|
93 translations |
|
94 "case x of of_int y => b" == "word_int_case (%y. b) x" |
|
95 |
|
96 |
|
97 section "Arithmetic operations" |
|
98 |
|
99 defs (overloaded) |
|
100 word_1_wi: "(1 :: ('a :: len0) word) == word_of_int 1" |
|
101 word_0_wi: "(0 :: ('a :: len0) word) == word_of_int 0" |
|
102 |
|
103 word_le_def: "a <= b == uint a <= uint b" |
|
104 word_less_def: "x < y == x <= y & x ~= (y :: 'a :: len0 word)" |
|
105 |
|
106 constdefs |
|
107 word_succ :: "'a :: len0 word => 'a word" |
|
108 "word_succ a == word_of_int (Numeral.succ (uint a))" |
|
109 |
|
110 word_pred :: "'a :: len0 word => 'a word" |
|
111 "word_pred a == word_of_int (Numeral.pred (uint a))" |
|
112 |
|
113 udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50) |
|
114 "a udvd b == EX n>=0. uint b = n * uint a" |
|
115 |
|
116 word_sle :: "'a :: len word => 'a word => bool" ("(_/ <=s _)" [50, 51] 50) |
|
117 "a <=s b == sint a <= sint b" |
|
118 |
|
119 word_sless :: "'a :: len word => 'a word => bool" ("(_/ <s _)" [50, 51] 50) |
|
120 "(x <s y) == (x <=s y & x ~= y)" |
|
121 |
|
122 consts |
|
123 word_power :: "'a :: len0 word => nat => 'a word" |
|
124 primrec |
|
125 "word_power a 0 = 1" |
|
126 "word_power a (Suc n) = a * word_power a n" |
|
127 |
|
128 defs (overloaded) |
|
129 word_pow: "power == word_power" |
|
130 word_add_def: "a + b == word_of_int (uint a + uint b)" |
|
131 word_sub_wi: "a - b == word_of_int (uint a - uint b)" |
|
132 word_minus_def: "- a == word_of_int (- uint a)" |
|
133 word_mult_def: "a * b == word_of_int (uint a * uint b)" |
|
134 word_div_def: "a div b == word_of_int (uint a div uint b)" |
|
135 word_mod_def: "a mod b == word_of_int (uint a mod uint b)" |
|
136 |
|
137 |
|
138 section "Bit-wise operations" |
|
139 |
|
140 defs (overloaded) |
|
141 word_and_def: |
|
142 "(a::'a::len0 word) AND b == word_of_int (int_and (uint a) (uint b))" |
|
143 |
|
144 word_or_def: |
|
145 "(a::'a::len0 word) OR b == word_of_int (int_or (uint a) (uint b))" |
|
146 |
|
147 word_xor_def: |
|
148 "(a::'a::len0 word) XOR b == word_of_int (int_xor (uint a) (uint b))" |
|
149 |
|
150 word_not_def: |
|
151 "NOT (a::'a::len0 word) == word_of_int (int_not (uint a))" |
|
152 |
|
153 word_test_bit_def: |
|
154 "test_bit (a::'a::len0 word) == bin_nth (uint a)" |
|
155 |
|
156 word_set_bit_def: |
|
157 "set_bit (a::'a::len0 word) n x == |
|
158 word_of_int (bin_sc n (If x bit.B1 bit.B0) (uint a))" |
|
159 |
|
160 word_set_bits_def: |
|
161 "(BITS n. f n)::'a::len0 word == of_bl (bl_of_nth (len_of TYPE ('a)) f)" |
|
162 |
|
163 word_lsb_def: |
|
164 "lsb (a::'a::len0 word) == bin_last (uint a) = bit.B1" |
|
165 |
|
166 word_msb_def: |
|
167 "msb (a::'a::len word) == bin_sign (sint a) = Numeral.Min" |
|
168 |
|
169 |
|
170 constdefs |
|
171 setBit :: "'a :: len0 word => nat => 'a word" |
|
172 "setBit w n == set_bit w n True" |
|
173 |
|
174 clearBit :: "'a :: len0 word => nat => 'a word" |
|
175 "clearBit w n == set_bit w n False" |
|
176 |
|
177 |
|
178 section "Shift operations" |
|
179 |
|
180 constdefs |
|
181 shiftl1 :: "'a :: len0 word => 'a word" |
|
182 "shiftl1 w == word_of_int (uint w BIT bit.B0)" |
|
183 |
|
184 -- "shift right as unsigned or as signed, ie logical or arithmetic" |
|
185 shiftr1 :: "'a :: len0 word => 'a word" |
|
186 "shiftr1 w == word_of_int (bin_rest (uint w))" |
|
187 |
|
188 sshiftr1 :: "'a :: len word => 'a word" |
|
189 "sshiftr1 w == word_of_int (bin_rest (sint w))" |
|
190 |
|
191 bshiftr1 :: "bool => 'a :: len word => 'a word" |
|
192 "bshiftr1 b w == of_bl (b # butlast (to_bl w))" |
|
193 |
|
194 sshiftr :: "'a :: len word => nat => 'a word" (infixl ">>>" 55) |
|
195 "w >>> n == (sshiftr1 ^ n) w" |
|
196 |
|
197 mask :: "nat => 'a::len word" |
|
198 "mask n == (1 << n) - 1" |
|
199 |
|
200 revcast :: "'a :: len0 word => 'b :: len0 word" |
|
201 "revcast w == of_bl (takefill False (len_of TYPE('b)) (to_bl w))" |
|
202 |
|
203 slice1 :: "nat => 'a :: len0 word => 'b :: len0 word" |
|
204 "slice1 n w == of_bl (takefill False n (to_bl w))" |
|
205 |
|
206 slice :: "nat => 'a :: len0 word => 'b :: len0 word" |
|
207 "slice n w == slice1 (size w - n) w" |
|
208 |
|
209 |
|
210 defs (overloaded) |
|
211 shiftl_def: "(w::'a::len0 word) << n == (shiftl1 ^ n) w" |
|
212 shiftr_def: "(w::'a::len0 word) >> n == (shiftr1 ^ n) w" |
|
213 |
|
214 |
|
215 section "Rotation" |
|
216 |
|
217 constdefs |
|
218 rotater1 :: "'a list => 'a list" |
|
219 "rotater1 ys == |
|
220 case ys of [] => [] | x # xs => last ys # butlast ys" |
|
221 |
|
222 rotater :: "nat => 'a list => 'a list" |
|
223 "rotater n == rotater1 ^ n" |
|
224 |
|
225 word_rotr :: "nat => 'a :: len0 word => 'a :: len0 word" |
|
226 "word_rotr n w == of_bl (rotater n (to_bl w))" |
|
227 |
|
228 word_rotl :: "nat => 'a :: len0 word => 'a :: len0 word" |
|
229 "word_rotl n w == of_bl (rotate n (to_bl w))" |
|
230 |
|
231 word_roti :: "int => 'a :: len0 word => 'a :: len0 word" |
|
232 "word_roti i w == if i >= 0 then word_rotr (nat i) w |
|
233 else word_rotl (nat (- i)) w" |
|
234 |
|
235 |
|
236 section "Split and cat operations" |
|
237 |
|
238 constdefs |
|
239 word_cat :: "'a :: len0 word => 'b :: len0 word => 'c :: len0 word" |
|
240 "word_cat a b == word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))" |
|
241 |
|
242 word_split :: "'a :: len0 word => ('b :: len0 word) * ('c :: len0 word)" |
|
243 "word_split a == |
|
244 case bin_split (len_of TYPE ('c)) (uint a) of |
|
245 (u, v) => (word_of_int u, word_of_int v)" |
|
246 |
|
247 word_rcat :: "'a :: len0 word list => 'b :: len0 word" |
|
248 "word_rcat ws == |
|
249 word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))" |
|
250 |
|
251 word_rsplit :: "'a :: len0 word => 'b :: len word list" |
|
252 "word_rsplit w == |
|
253 map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))" |
|
254 |
|
255 constdefs |
|
256 -- "Largest representable machine integer." |
|
257 max_word :: "'a::len word" |
|
258 "max_word \<equiv> word_of_int (2^len_of TYPE('a) - 1)" |
|
259 |
|
260 consts |
|
261 of_bool :: "bool \<Rightarrow> 'a::len word" |
|
262 primrec |
|
263 "of_bool False = 0" |
|
264 "of_bool True = 1" |
|
265 |
|
266 |
|
267 |
|
268 lemmas of_nth_def = word_set_bits_def |
|
269 |
|
270 lemmas word_size_gt_0 [iff] = |
|
271 xtr1 [OF word_size [THEN meta_eq_to_obj_eq] len_gt_0, standard] |
|
272 lemmas lens_gt_0 = word_size_gt_0 len_gt_0 |
|
273 lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0, standard] |
|
274 |
|
275 lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}" |
|
276 by (simp add: uints_def range_bintrunc) |
|
277 |
|
278 lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}" |
|
279 by (simp add: sints_def range_sbintrunc) |
|
280 |
|
281 lemmas atLeastLessThan_alt = atLeastLessThan_def [unfolded |
|
282 atLeast_def lessThan_def Collect_conj_eq [symmetric]] |
|
283 |
|
284 lemma mod_in_reps: "m > 0 ==> y mod m : {0::int ..< m}" |
|
285 unfolding atLeastLessThan_alt by auto |
|
286 |
|
287 lemma |
|
288 Rep_word_0:"0 <= Rep_word x" and |
|
289 Rep_word_lt: "Rep_word (x::'a::len0 word) < 2 ^ len_of TYPE('a)" |
|
290 by (auto simp: Rep_word [simplified]) |
|
291 |
|
292 lemma Rep_word_mod_same: |
|
293 "Rep_word x mod 2 ^ len_of TYPE('a) = Rep_word (x::'a::len0 word)" |
|
294 by (simp add: int_mod_eq Rep_word_lt Rep_word_0) |
|
295 |
|
296 lemma td_ext_uint: |
|
297 "td_ext (uint :: 'a word => int) word_of_int (uints (len_of TYPE('a::len0))) |
|
298 (%w::int. w mod 2 ^ len_of TYPE('a))" |
|
299 apply (unfold td_ext_def') |
|
300 apply (simp add: uints_num uint_def word_of_int_def bintrunc_mod2p) |
|
301 apply (simp add: Rep_word_mod_same Rep_word_0 Rep_word_lt |
|
302 word.Rep_word_inverse word.Abs_word_inverse int_mod_lem) |
|
303 done |
|
304 |
|
305 lemmas int_word_uint = td_ext_uint [THEN td_ext.eq_norm, standard] |
|
306 |
|
307 interpretation word_uint: |
|
308 td_ext ["uint::'a::len0 word \<Rightarrow> int" |
|
309 word_of_int |
|
310 "uints (len_of TYPE('a::len0))" |
|
311 "\<lambda>w. w mod 2 ^ len_of TYPE('a::len0)"] |
|
312 by (rule td_ext_uint) |
|
313 |
|
314 lemmas td_uint = word_uint.td_thm |
|
315 |
|
316 lemmas td_ext_ubin = td_ext_uint |
|
317 [simplified len_gt_0 no_bintr_alt1 [symmetric]] |
|
318 |
|
319 interpretation word_ubin: |
|
320 td_ext ["uint::'a::len0 word \<Rightarrow> int" |
|
321 word_of_int |
|
322 "uints (len_of TYPE('a::len0))" |
|
323 "bintrunc (len_of TYPE('a::len0))"] |
|
324 by (rule td_ext_ubin) |
|
325 |
|
326 lemma sint_sbintrunc': |
|
327 "sint (word_of_int bin :: 'a word) = |
|
328 (sbintrunc (len_of TYPE ('a :: len) - 1) bin)" |
|
329 unfolding sint_uint |
|
330 by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt) |
|
331 |
|
332 lemma uint_sint: |
|
333 "uint w = bintrunc (len_of TYPE('a)) (sint (w :: 'a :: len word))" |
|
334 unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le) |
|
335 |
|
336 lemma bintr_uint': |
|
337 "n >= size w ==> bintrunc n (uint w) = uint w" |
|
338 apply (unfold word_size) |
|
339 apply (subst word_ubin.norm_Rep [symmetric]) |
|
340 apply (simp only: bintrunc_bintrunc_min word_size min_def) |
|
341 apply simp |
|
342 done |
|
343 |
|
344 lemma wi_bintr': |
|
345 "wb = word_of_int bin ==> n >= size wb ==> |
|
346 word_of_int (bintrunc n bin) = wb" |
|
347 unfolding word_size |
|
348 by (clarsimp simp add : word_ubin.norm_eq_iff [symmetric] min_def) |
|
349 |
|
350 lemmas bintr_uint = bintr_uint' [unfolded word_size] |
|
351 lemmas wi_bintr = wi_bintr' [unfolded word_size] |
|
352 |
|
353 lemma td_ext_sbin: |
|
354 "td_ext (sint :: 'a word => int) word_of_int (sints (len_of TYPE('a::len))) |
|
355 (sbintrunc (len_of TYPE('a) - 1))" |
|
356 apply (unfold td_ext_def' sint_uint) |
|
357 apply (simp add : word_ubin.eq_norm) |
|
358 apply (cases "len_of TYPE('a)") |
|
359 apply (auto simp add : sints_def) |
|
360 apply (rule sym [THEN trans]) |
|
361 apply (rule word_ubin.Abs_norm) |
|
362 apply (simp only: bintrunc_sbintrunc) |
|
363 apply (drule sym) |
|
364 apply simp |
|
365 done |
|
366 |
|
367 lemmas td_ext_sint = td_ext_sbin |
|
368 [simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]] |
|
369 |
|
370 (* We do sint before sbin, before sint is the user version |
|
371 and interpretations do not produce thm duplicates. I.e. |
|
372 we get the name word_sint.Rep_eqD, but not word_sbin.Req_eqD, |
|
373 because the latter is the same thm as the former *) |
|
374 interpretation word_sint: |
|
375 td_ext ["sint ::'a::len word => int" |
|
376 word_of_int |
|
377 "sints (len_of TYPE('a::len))" |
|
378 "%w. (w + 2^(len_of TYPE('a::len) - 1)) mod 2^len_of TYPE('a::len) - |
|
379 2 ^ (len_of TYPE('a::len) - 1)"] |
|
380 by (rule td_ext_sint) |
|
381 |
|
382 interpretation word_sbin: |
|
383 td_ext ["sint ::'a::len word => int" |
|
384 word_of_int |
|
385 "sints (len_of TYPE('a::len))" |
|
386 "sbintrunc (len_of TYPE('a::len) - 1)"] |
|
387 by (rule td_ext_sbin) |
|
388 |
|
389 lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm, standard] |
|
390 |
|
391 lemmas td_sint = word_sint.td |
|
392 |
|
393 lemma word_number_of_alt: "number_of b == word_of_int (number_of b)" |
|
394 unfolding word_number_of_def by (simp add: number_of_eq) |
|
395 |
|
396 lemma word_no_wi: "number_of = word_of_int" |
|
397 by (auto simp: word_number_of_def intro: ext) |
|
398 |
|
399 lemma to_bl_def': |
|
400 "(to_bl :: 'a :: len0 word => bool list) = |
|
401 bin_to_bl (len_of TYPE('a)) o uint" |
|
402 by (auto simp: to_bl_def intro: ext) |
|
403 |
|
404 lemmas word_reverse_no_def [simp] = word_reverse_def [of "number_of ?w"] |
|
405 |
|
406 lemmas uints_mod = uints_def [unfolded no_bintr_alt1] |
|
407 |
|
408 lemma uint_bintrunc: "uint (number_of bin :: 'a word) = |
|
409 number_of (bintrunc (len_of TYPE ('a :: len0)) bin)" |
|
410 unfolding word_number_of_def number_of_eq |
|
411 by (auto intro: word_ubin.eq_norm) |
|
412 |
|
413 lemma sint_sbintrunc: "sint (number_of bin :: 'a word) = |
|
414 number_of (sbintrunc (len_of TYPE ('a :: len) - 1) bin)" |
|
415 unfolding word_number_of_def number_of_eq |
|
416 by (auto intro!: word_sbin.eq_norm simp del: one_is_Suc_zero) |
|
417 |
|
418 lemma unat_bintrunc: |
|
419 "unat (number_of bin :: 'a :: len0 word) = |
|
420 number_of (bintrunc (len_of TYPE('a)) bin)" |
|
421 unfolding unat_def nat_number_of_def |
|
422 by (simp only: uint_bintrunc) |
|
423 |
|
424 (* WARNING - these may not always be helpful *) |
|
425 declare |
|
426 uint_bintrunc [simp] |
|
427 sint_sbintrunc [simp] |
|
428 unat_bintrunc [simp] |
|
429 |
|
430 lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 ==> v = w" |
|
431 apply (unfold word_size) |
|
432 apply (rule word_uint.Rep_eqD) |
|
433 apply (rule box_equals) |
|
434 defer |
|
435 apply (rule word_ubin.norm_Rep)+ |
|
436 apply simp |
|
437 done |
|
438 |
|
439 lemmas uint_lem = word_uint.Rep [unfolded uints_num mem_Collect_eq] |
|
440 lemmas sint_lem = word_sint.Rep [unfolded sints_num mem_Collect_eq] |
|
441 lemmas uint_ge_0 [iff] = uint_lem [THEN conjunct1, standard] |
|
442 lemmas uint_lt2p [iff] = uint_lem [THEN conjunct2, standard] |
|
443 lemmas sint_ge = sint_lem [THEN conjunct1, standard] |
|
444 lemmas sint_lt = sint_lem [THEN conjunct2, standard] |
|
445 |
|
446 lemma sign_uint_Pls [simp]: |
|
447 "bin_sign (uint x) = Numeral.Pls" |
|
448 by (simp add: sign_Pls_ge_0 number_of_eq) |
|
449 |
|
450 lemmas uint_m2p_neg = iffD2 [OF diff_less_0_iff_less uint_lt2p, standard] |
|
451 lemmas uint_m2p_not_non_neg = |
|
452 iffD2 [OF linorder_not_le uint_m2p_neg, standard] |
|
453 |
|
454 lemma lt2p_lem: |
|
455 "len_of TYPE('a) <= n ==> uint (w :: 'a :: len0 word) < 2 ^ n" |
|
456 by (rule xtr8 [OF _ uint_lt2p]) simp |
|
457 |
|
458 lemmas uint_le_0_iff [simp] = |
|
459 uint_ge_0 [THEN leD, THEN linorder_antisym_conv1, standard] |
|
460 |
|
461 lemma uint_nat: "uint w == int (unat w)" |
|
462 unfolding unat_def by auto |
|
463 |
|
464 lemma uint_number_of: |
|
465 "uint (number_of b :: 'a :: len0 word) = number_of b mod 2 ^ len_of TYPE('a)" |
|
466 unfolding word_number_of_alt |
|
467 by (simp only: int_word_uint) |
|
468 |
|
469 lemma unat_number_of: |
|
470 "bin_sign b = Numeral.Pls ==> |
|
471 unat (number_of b::'a::len0 word) = number_of b mod 2 ^ len_of TYPE ('a)" |
|
472 apply (unfold unat_def) |
|
473 apply (clarsimp simp only: uint_number_of) |
|
474 apply (rule nat_mod_distrib [THEN trans]) |
|
475 apply (erule sign_Pls_ge_0 [THEN iffD1]) |
|
476 apply (simp_all add: nat_power_eq) |
|
477 done |
|
478 |
|
479 lemma sint_number_of: "sint (number_of b :: 'a :: len word) = (number_of b + |
|
480 2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) - |
|
481 2 ^ (len_of TYPE('a) - 1)" |
|
482 unfolding word_number_of_alt by (rule int_word_sint) |
|
483 |
|
484 lemma word_of_int_bin [simp] : |
|
485 "(word_of_int (number_of bin) :: 'a :: len0 word) = (number_of bin)" |
|
486 unfolding word_number_of_alt by auto |
|
487 |
|
488 lemma word_int_case_wi: |
|
489 "word_int_case f (word_of_int i :: 'b word) = |
|
490 f (i mod 2 ^ len_of TYPE('b::len0))" |
|
491 unfolding word_int_case_def by (simp add: word_uint.eq_norm) |
|
492 |
|
493 lemma word_int_split: |
|
494 "P (word_int_case f x) = |
|
495 (ALL i. x = (word_of_int i :: 'b :: len0 word) & |
|
496 0 <= i & i < 2 ^ len_of TYPE('b) --> P (f i))" |
|
497 unfolding word_int_case_def |
|
498 by (auto simp: word_uint.eq_norm int_mod_eq') |
|
499 |
|
500 lemma word_int_split_asm: |
|
501 "P (word_int_case f x) = |
|
502 (~ (EX n. x = (word_of_int n :: 'b::len0 word) & |
|
503 0 <= n & n < 2 ^ len_of TYPE('b::len0) & ~ P (f n)))" |
|
504 unfolding word_int_case_def |
|
505 by (auto simp: word_uint.eq_norm int_mod_eq') |
|
506 |
|
507 lemmas uint_range' = |
|
508 word_uint.Rep [unfolded uints_num mem_Collect_eq, standard] |
|
509 lemmas sint_range' = word_sint.Rep [unfolded One_nat_def |
|
510 sints_num mem_Collect_eq, standard] |
|
511 |
|
512 lemma uint_range_size: "0 <= uint w & uint w < 2 ^ size w" |
|
513 unfolding word_size by (rule uint_range') |
|
514 |
|
515 lemma sint_range_size: |
|
516 "- (2 ^ (size w - Suc 0)) <= sint w & sint w < 2 ^ (size w - Suc 0)" |
|
517 unfolding word_size by (rule sint_range') |
|
518 |
|
519 lemmas sint_above_size = sint_range_size |
|
520 [THEN conjunct2, THEN [2] xtr8, folded One_nat_def, standard] |
|
521 |
|
522 lemmas sint_below_size = sint_range_size |
|
523 [THEN conjunct1, THEN [2] order_trans, folded One_nat_def, standard] |
|
524 |
|
525 lemma test_bit_eq_iff: "(test_bit (u::'a::len0 word) = test_bit v) = (u = v)" |
|
526 unfolding word_test_bit_def by (simp add: bin_nth_eq_iff) |
|
527 |
|
528 lemma test_bit_size [rule_format] : "(w::'a::len0 word) !! n --> n < size w" |
|
529 apply (unfold word_test_bit_def) |
|
530 apply (subst word_ubin.norm_Rep [symmetric]) |
|
531 apply (simp only: nth_bintr word_size) |
|
532 apply fast |
|
533 done |
|
534 |
|
535 lemma word_eqI [rule_format] : |
|
536 fixes u :: "'a::len0 word" |
|
537 shows "(ALL n. n < size u --> u !! n = v !! n) ==> u = v" |
|
538 apply (rule test_bit_eq_iff [THEN iffD1]) |
|
539 apply (rule ext) |
|
540 apply (erule allE) |
|
541 apply (erule impCE) |
|
542 prefer 2 |
|
543 apply assumption |
|
544 apply (auto dest!: test_bit_size simp add: word_size) |
|
545 done |
|
546 |
|
547 lemmas word_eqD = test_bit_eq_iff [THEN iffD2, THEN fun_cong, standard] |
|
548 |
|
549 lemma test_bit_bin': "w !! n = (n < size w & bin_nth (uint w) n)" |
|
550 unfolding word_test_bit_def word_size |
|
551 by (simp add: nth_bintr [symmetric]) |
|
552 |
|
553 lemmas test_bit_bin = test_bit_bin' [unfolded word_size] |
|
554 |
|
555 lemma bin_nth_uint_imp': "bin_nth (uint w) n --> n < size w" |
|
556 apply (unfold word_size) |
|
557 apply (rule impI) |
|
558 apply (rule nth_bintr [THEN iffD1, THEN conjunct1]) |
|
559 apply (subst word_ubin.norm_Rep) |
|
560 apply assumption |
|
561 done |
|
562 |
|
563 lemma bin_nth_sint': |
|
564 "n >= size w --> bin_nth (sint w) n = bin_nth (sint w) (size w - 1)" |
|
565 apply (rule impI) |
|
566 apply (subst word_sbin.norm_Rep [symmetric]) |
|
567 apply (simp add : nth_sbintr word_size) |
|
568 apply auto |
|
569 done |
|
570 |
|
571 lemmas bin_nth_uint_imp = bin_nth_uint_imp' [rule_format, unfolded word_size] |
|
572 lemmas bin_nth_sint = bin_nth_sint' [rule_format, unfolded word_size] |
|
573 |
|
574 (* type definitions theorem for in terms of equivalent bool list *) |
|
575 lemma td_bl: |
|
576 "type_definition (to_bl :: 'a::len0 word => bool list) |
|
577 of_bl |
|
578 {bl. length bl = len_of TYPE('a)}" |
|
579 apply (unfold type_definition_def of_bl_def to_bl_def) |
|
580 apply (simp add: word_ubin.eq_norm) |
|
581 apply safe |
|
582 apply (drule sym) |
|
583 apply simp |
|
584 done |
|
585 |
|
586 interpretation word_bl: |
|
587 type_definition ["to_bl :: 'a::len0 word => bool list" |
|
588 of_bl |
|
589 "{bl. length bl = len_of TYPE('a::len0)}"] |
|
590 by (rule td_bl) |
|
591 |
|
592 lemma word_size_bl: "size w == size (to_bl w)" |
|
593 unfolding word_size by auto |
|
594 |
|
595 lemma to_bl_use_of_bl: |
|
596 "(to_bl w = bl) = (w = of_bl bl \<and> length bl = length (to_bl w))" |
|
597 by (fastsimp elim!: word_bl.Abs_inverse [simplified]) |
|
598 |
|
599 lemma to_bl_word_rev: "to_bl (word_reverse w) = rev (to_bl w)" |
|
600 unfolding word_reverse_def by (simp add: word_bl.Abs_inverse) |
|
601 |
|
602 lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w" |
|
603 unfolding word_reverse_def by (simp add : word_bl.Abs_inverse) |
|
604 |
|
605 lemma word_rev_gal: "word_reverse w = u ==> word_reverse u = w" |
|
606 by auto |
|
607 |
|
608 lemmas word_rev_gal' = sym [THEN word_rev_gal, symmetric, standard] |
|
609 |
|
610 lemmas length_bl_gt_0 [iff] = xtr1 [OF word_bl.Rep' len_gt_0, standard] |
|
611 lemmas bl_not_Nil [iff] = |
|
612 length_bl_gt_0 [THEN length_greater_0_conv [THEN iffD1], standard] |
|
613 lemmas length_bl_neq_0 [iff] = length_bl_gt_0 [THEN gr_implies_not0] |
|
614 |
|
615 lemma hd_bl_sign_sint: "hd (to_bl w) = (bin_sign (sint w) = Numeral.Min)" |
|
616 apply (unfold to_bl_def sint_uint) |
|
617 apply (rule trans [OF _ bl_sbin_sign]) |
|
618 apply simp |
|
619 done |
|
620 |
|
621 lemma of_bl_drop': |
|
622 "lend = length bl - len_of TYPE ('a :: len0) ==> |
|
623 of_bl (drop lend bl) = (of_bl bl :: 'a word)" |
|
624 apply (unfold of_bl_def) |
|
625 apply (clarsimp simp add : trunc_bl2bin [symmetric]) |
|
626 done |
|
627 |
|
628 lemmas of_bl_no = of_bl_def [folded word_number_of_def] |
|
629 |
|
630 lemma test_bit_of_bl: |
|
631 "(of_bl bl::'a::len0 word) !! n = (rev bl ! n \<and> n < len_of TYPE('a) \<and> n < length bl)" |
|
632 apply (unfold of_bl_def word_test_bit_def) |
|
633 apply (auto simp add: word_size word_ubin.eq_norm nth_bintr bin_nth_of_bl) |
|
634 done |
|
635 |
|
636 lemma no_of_bl: |
|
637 "(number_of bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE ('a)) bin)" |
|
638 unfolding word_size of_bl_no by (simp add : word_number_of_def) |
|
639 |
|
640 lemma uint_bl: "to_bl w == bin_to_bl (size w) (uint w)" |
|
641 unfolding word_size to_bl_def by auto |
|
642 |
|
643 lemma to_bl_bin: "bl_to_bin (to_bl w) = uint w" |
|
644 unfolding uint_bl by (simp add : word_size) |
|
645 |
|
646 lemma to_bl_of_bin: |
|
647 "to_bl (word_of_int bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin" |
|
648 unfolding uint_bl by (clarsimp simp add: word_ubin.eq_norm word_size) |
|
649 |
|
650 lemmas to_bl_no_bin [simp] = to_bl_of_bin [folded word_number_of_def] |
|
651 |
|
652 lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w" |
|
653 unfolding uint_bl by (simp add : word_size) |
|
654 |
|
655 lemmas uint_bl_bin [simp] = trans [OF bin_bl_bin word_ubin.norm_Rep, standard] |
|
656 |
|
657 lemmas num_AB_u [simp] = word_uint.Rep_inverse |
|
658 [unfolded o_def word_number_of_def [symmetric], standard] |
|
659 lemmas num_AB_s [simp] = word_sint.Rep_inverse |
|
660 [unfolded o_def word_number_of_def [symmetric], standard] |
|
661 |
|
662 (* naturals *) |
|
663 lemma uints_unats: "uints n = int ` unats n" |
|
664 apply (unfold unats_def uints_num) |
|
665 apply safe |
|
666 apply (rule_tac image_eqI) |
|
667 apply (erule_tac nat_0_le [symmetric]) |
|
668 apply auto |
|
669 apply (erule_tac nat_less_iff [THEN iffD2]) |
|
670 apply (rule_tac [2] zless_nat_eq_int_zless [THEN iffD1]) |
|
671 apply (auto simp add : nat_power_eq int_power) |
|
672 done |
|
673 |
|
674 lemma unats_uints: "unats n = nat ` uints n" |
|
675 apply (auto simp add : uints_unats image_iff) |
|
676 done |
|
677 |
|
678 lemmas bintr_num = word_ubin.norm_eq_iff |
|
679 [symmetric, folded word_number_of_def, standard] |
|
680 lemmas sbintr_num = word_sbin.norm_eq_iff |
|
681 [symmetric, folded word_number_of_def, standard] |
|
682 |
|
683 lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def, standard] |
|
684 lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def, standard]; |
|
685 |
|
686 (* don't add these to simpset, since may want bintrunc n w to be simplified; |
|
687 may want these in reverse, but loop as simp rules, so use following *) |
|
688 |
|
689 lemma num_of_bintr': |
|
690 "bintrunc (len_of TYPE('a :: len0)) a = b ==> |
|
691 number_of a = (number_of b :: 'a word)" |
|
692 apply safe |
|
693 apply (rule_tac num_of_bintr [symmetric]) |
|
694 done |
|
695 |
|
696 lemma num_of_sbintr': |
|
697 "sbintrunc (len_of TYPE('a :: len) - 1) a = b ==> |
|
698 number_of a = (number_of b :: 'a word)" |
|
699 apply safe |
|
700 apply (rule_tac num_of_sbintr [symmetric]) |
|
701 done |
|
702 |
|
703 lemmas num_abs_bintr = sym [THEN trans, |
|
704 OF num_of_bintr word_number_of_def [THEN meta_eq_to_obj_eq], standard] |
|
705 lemmas num_abs_sbintr = sym [THEN trans, |
|
706 OF num_of_sbintr word_number_of_def [THEN meta_eq_to_obj_eq], standard] |
|
707 |
|
708 (** cast - note, no arg for new length, as it's determined by type of result, |
|
709 thus in "cast w = w, the type means cast to length of w! **) |
|
710 |
|
711 lemma ucast_id: "ucast w = w" |
|
712 unfolding ucast_def by auto |
|
713 |
|
714 lemma scast_id: "scast w = w" |
|
715 unfolding scast_def by auto |
|
716 |
|
717 lemma ucast_bl: "ucast w == of_bl (to_bl w)" |
|
718 unfolding ucast_def of_bl_def uint_bl |
|
719 by (auto simp add : word_size) |
|
720 |
|
721 lemma nth_ucast: |
|
722 "(ucast w::'a::len0 word) !! n = (w !! n & n < len_of TYPE('a))" |
|
723 apply (unfold ucast_def test_bit_bin) |
|
724 apply (simp add: word_ubin.eq_norm nth_bintr word_size) |
|
725 apply (fast elim!: bin_nth_uint_imp) |
|
726 done |
|
727 |
|
728 (* for literal u(s)cast *) |
|
729 |
|
730 lemma ucast_bintr [simp]: |
|
731 "ucast (number_of w ::'a::len0 word) = |
|
732 number_of (bintrunc (len_of TYPE('a)) w)" |
|
733 unfolding ucast_def by simp |
|
734 |
|
735 lemma scast_sbintr [simp]: |
|
736 "scast (number_of w ::'a::len word) = |
|
737 number_of (sbintrunc (len_of TYPE('a) - Suc 0) w)" |
|
738 unfolding scast_def by simp |
|
739 |
|
740 lemmas source_size = source_size_def [unfolded Let_def word_size] |
|
741 lemmas target_size = target_size_def [unfolded Let_def word_size] |
|
742 lemmas is_down = is_down_def [unfolded source_size target_size] |
|
743 lemmas is_up = is_up_def [unfolded source_size target_size] |
|
744 |
|
745 lemmas is_up_down = |
|
746 trans [OF is_up [THEN meta_eq_to_obj_eq] |
|
747 is_down [THEN meta_eq_to_obj_eq, symmetric], |
|
748 standard] |
|
749 |
|
750 lemma down_cast_same': "uc = ucast ==> is_down uc ==> uc = scast" |
|
751 apply (unfold is_down) |
|
752 apply safe |
|
753 apply (rule ext) |
|
754 apply (unfold ucast_def scast_def uint_sint) |
|
755 apply (rule word_ubin.norm_eq_iff [THEN iffD1]) |
|
756 apply simp |
|
757 done |
|
758 |
|
759 lemma word_rev_tf': |
|
760 "r = to_bl (of_bl bl) ==> r = rev (takefill False (length r) (rev bl))" |
|
761 unfolding of_bl_def uint_bl |
|
762 by (clarsimp simp add: bl_bin_bl_rtf word_ubin.eq_norm word_size) |
|
763 |
|
764 lemmas word_rev_tf = refl [THEN word_rev_tf', unfolded word_bl.Rep', standard] |
|
765 |
|
766 lemmas word_rep_drop = word_rev_tf [simplified takefill_alt, |
|
767 simplified, simplified rev_take, simplified] |
|
768 |
|
769 lemma to_bl_ucast: |
|
770 "to_bl (ucast (w::'b::len0 word) ::'a::len0 word) = |
|
771 replicate (len_of TYPE('a) - len_of TYPE('b)) False @ |
|
772 drop (len_of TYPE('b) - len_of TYPE('a)) (to_bl w)" |
|
773 apply (unfold ucast_bl) |
|
774 apply (rule trans) |
|
775 apply (rule word_rep_drop) |
|
776 apply simp |
|
777 done |
|
778 |
|
779 lemma ucast_up_app': |
|
780 "uc = ucast ==> source_size uc + n = target_size uc ==> |
|
781 to_bl (uc w) = replicate n False @ (to_bl w)" |
|
782 apply (auto simp add : source_size target_size to_bl_ucast) |
|
783 apply (rule_tac f = "%n. replicate n False" in arg_cong) |
|
784 apply simp |
|
785 done |
|
786 |
|
787 lemma ucast_down_drop': |
|
788 "uc = ucast ==> source_size uc = target_size uc + n ==> |
|
789 to_bl (uc w) = drop n (to_bl w)" |
|
790 by (auto simp add : source_size target_size to_bl_ucast) |
|
791 |
|
792 lemma scast_down_drop': |
|
793 "sc = scast ==> source_size sc = target_size sc + n ==> |
|
794 to_bl (sc w) = drop n (to_bl w)" |
|
795 apply (subgoal_tac "sc = ucast") |
|
796 apply safe |
|
797 apply simp |
|
798 apply (erule refl [THEN ucast_down_drop']) |
|
799 apply (rule refl [THEN down_cast_same', symmetric]) |
|
800 apply (simp add : source_size target_size is_down) |
|
801 done |
|
802 |
|
803 lemma sint_up_scast': |
|
804 "sc = scast ==> is_up sc ==> sint (sc w) = sint w" |
|
805 apply (unfold is_up) |
|
806 apply safe |
|
807 apply (simp add: scast_def word_sbin.eq_norm) |
|
808 apply (rule box_equals) |
|
809 prefer 3 |
|
810 apply (rule word_sbin.norm_Rep) |
|
811 apply (rule sbintrunc_sbintrunc_l) |
|
812 defer |
|
813 apply (subst word_sbin.norm_Rep) |
|
814 apply (rule refl) |
|
815 apply simp |
|
816 done |
|
817 |
|
818 lemma uint_up_ucast': |
|
819 "uc = ucast ==> is_up uc ==> uint (uc w) = uint w" |
|
820 apply (unfold is_up) |
|
821 apply safe |
|
822 apply (rule bin_eqI) |
|
823 apply (fold word_test_bit_def) |
|
824 apply (auto simp add: nth_ucast) |
|
825 apply (auto simp add: test_bit_bin) |
|
826 done |
|
827 |
|
828 lemmas down_cast_same = refl [THEN down_cast_same'] |
|
829 lemmas ucast_up_app = refl [THEN ucast_up_app'] |
|
830 lemmas ucast_down_drop = refl [THEN ucast_down_drop'] |
|
831 lemmas scast_down_drop = refl [THEN scast_down_drop'] |
|
832 lemmas uint_up_ucast = refl [THEN uint_up_ucast'] |
|
833 lemmas sint_up_scast = refl [THEN sint_up_scast'] |
|
834 |
|
835 lemma ucast_up_ucast': "uc = ucast ==> is_up uc ==> ucast (uc w) = ucast w" |
|
836 apply (simp (no_asm) add: ucast_def) |
|
837 apply (clarsimp simp add: uint_up_ucast) |
|
838 done |
|
839 |
|
840 lemma scast_up_scast': "sc = scast ==> is_up sc ==> scast (sc w) = scast w" |
|
841 apply (simp (no_asm) add: scast_def) |
|
842 apply (clarsimp simp add: sint_up_scast) |
|
843 done |
|
844 |
|
845 lemma ucast_of_bl_up': |
|
846 "w = of_bl bl ==> size bl <= size w ==> ucast w = of_bl bl" |
|
847 by (auto simp add : nth_ucast word_size test_bit_of_bl intro!: word_eqI) |
|
848 |
|
849 lemmas ucast_up_ucast = refl [THEN ucast_up_ucast'] |
|
850 lemmas scast_up_scast = refl [THEN scast_up_scast'] |
|
851 lemmas ucast_of_bl_up = refl [THEN ucast_of_bl_up'] |
|
852 |
|
853 lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] |
|
854 lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] |
|
855 |
|
856 lemmas isduu = is_up_down [where c = "ucast", THEN iffD2] |
|
857 lemmas isdus = is_up_down [where c = "scast", THEN iffD2] |
|
858 lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] |
|
859 lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] |
|
860 |
|
861 lemma up_ucast_surj: |
|
862 "is_up (ucast :: 'b::len0 word => 'a::len0 word) ==> |
|
863 surj (ucast :: 'a word => 'b word)" |
|
864 by (rule surjI, erule ucast_up_ucast_id) |
|
865 |
|
866 lemma up_scast_surj: |
|
867 "is_up (scast :: 'b::len word => 'a::len word) ==> |
|
868 surj (scast :: 'a word => 'b word)" |
|
869 by (rule surjI, erule scast_up_scast_id) |
|
870 |
|
871 lemma down_scast_inj: |
|
872 "is_down (scast :: 'b::len word => 'a::len word) ==> |
|
873 inj_on (ucast :: 'a word => 'b word) A" |
|
874 by (rule inj_on_inverseI, erule scast_down_scast_id) |
|
875 |
|
876 lemma down_ucast_inj: |
|
877 "is_down (ucast :: 'b::len0 word => 'a::len0 word) ==> |
|
878 inj_on (ucast :: 'a word => 'b word) A" |
|
879 by (rule inj_on_inverseI, erule ucast_down_ucast_id) |
|
880 |
|
881 lemma of_bl_append_same: "of_bl (X @ to_bl w) = w" |
|
882 by (rule word_bl.Rep_eqD) (simp add: word_rep_drop) |
|
883 |
|
884 lemma ucast_down_no': |
|
885 "uc = ucast ==> is_down uc ==> uc (number_of bin) = number_of bin" |
|
886 apply (unfold word_number_of_def is_down) |
|
887 apply (clarsimp simp add: ucast_def word_ubin.eq_norm) |
|
888 apply (rule word_ubin.norm_eq_iff [THEN iffD1]) |
|
889 apply (erule bintrunc_bintrunc_ge) |
|
890 done |
|
891 |
|
892 lemmas ucast_down_no = ucast_down_no' [OF refl] |
|
893 |
|
894 lemma ucast_down_bl': "uc = ucast ==> is_down uc ==> uc (of_bl bl) = of_bl bl" |
|
895 unfolding of_bl_no by clarify (erule ucast_down_no) |
|
896 |
|
897 lemmas ucast_down_bl = ucast_down_bl' [OF refl] |
|
898 |
|
899 lemmas slice_def' = slice_def [unfolded word_size] |
|
900 lemmas test_bit_def' = word_test_bit_def [THEN meta_eq_to_obj_eq, THEN fun_cong] |
|
901 |
|
902 lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def |
|
903 lemmas word_log_bin_defs = word_log_defs |
|
904 |
|
905 end |