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