author | wenzelm |
Thu, 17 Nov 2011 21:58:10 +0100 | |
changeset 45538 | 1fffa81b9b83 |
parent 45529 | 0e1037d4e049 |
child 45549 | 3eb6319febfe |
permissions | -rw-r--r-- |
29628 | 1 |
(* Title: HOL/Word/Word.thy |
37660 | 2 |
Author: Jeremy Dawson and Gerwin Klein, NICTA |
24333 | 3 |
*) |
4 |
||
37660 | 5 |
header {* A type of finite bit strings *} |
24350 | 6 |
|
29628 | 7 |
theory Word |
41413
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
wenzelm
parents:
41060
diff
changeset
|
8 |
imports |
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
wenzelm
parents:
41060
diff
changeset
|
9 |
Type_Length |
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
wenzelm
parents:
41060
diff
changeset
|
10 |
Misc_Typedef |
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
wenzelm
parents:
41060
diff
changeset
|
11 |
"~~/src/HOL/Library/Boolean_Algebra" |
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
wenzelm
parents:
41060
diff
changeset
|
12 |
Bool_List_Representation |
41060
4199fdcfa3c0
moved smt_word.ML into the directory of the Word library
boehmes
parents:
40827
diff
changeset
|
13 |
uses ("~~/src/HOL/Word/Tools/smt_word.ML") |
37660 | 14 |
begin |
15 |
||
16 |
text {* see @{text "Examples/WordExamples.thy"} for examples *} |
|
17 |
||
18 |
subsection {* Type definition *} |
|
19 |
||
20 |
typedef (open word) 'a word = "{(0::int) ..< 2^len_of TYPE('a::len0)}" |
|
21 |
morphisms uint Abs_word by auto |
|
22 |
||
23 |
definition word_of_int :: "int \<Rightarrow> 'a\<Colon>len0 word" where |
|
24 |
-- {* representation of words using unsigned or signed bins, |
|
25 |
only difference in these is the type class *} |
|
26 |
"word_of_int w = Abs_word (bintrunc (len_of TYPE ('a)) w)" |
|
27 |
||
28 |
lemma uint_word_of_int [code]: "uint (word_of_int w \<Colon> 'a\<Colon>len0 word) = w mod 2 ^ len_of TYPE('a)" |
|
29 |
by (auto simp add: word_of_int_def bintrunc_mod2p intro: Abs_word_inverse) |
|
30 |
||
31 |
code_datatype word_of_int |
|
32 |
||
37751 | 33 |
notation fcomp (infixl "\<circ>>" 60) |
34 |
notation scomp (infixl "\<circ>\<rightarrow>" 60) |
|
37660 | 35 |
|
36 |
instantiation word :: ("{len0, typerep}") random |
|
37 |
begin |
|
38 |
||
39 |
definition |
|
37751 | 40 |
"random_word i = Random.range (max i (2 ^ len_of TYPE('a))) \<circ>\<rightarrow> (\<lambda>k. Pair ( |
37660 | 41 |
let j = word_of_int (Code_Numeral.int_of k) :: 'a word |
42 |
in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))" |
|
43 |
||
44 |
instance .. |
|
45 |
||
46 |
end |
|
47 |
||
37751 | 48 |
no_notation fcomp (infixl "\<circ>>" 60) |
49 |
no_notation scomp (infixl "\<circ>\<rightarrow>" 60) |
|
37660 | 50 |
|
51 |
||
52 |
subsection {* Type conversions and casting *} |
|
53 |
||
54 |
definition sint :: "'a :: len word => int" where |
|
55 |
-- {* treats the most-significant-bit as a sign bit *} |
|
56 |
sint_uint: "sint w = sbintrunc (len_of TYPE ('a) - 1) (uint w)" |
|
57 |
||
58 |
definition unat :: "'a :: len0 word => nat" where |
|
59 |
"unat w = nat (uint w)" |
|
60 |
||
61 |
definition uints :: "nat => int set" where |
|
62 |
-- "the sets of integers representing the words" |
|
63 |
"uints n = range (bintrunc n)" |
|
64 |
||
65 |
definition sints :: "nat => int set" where |
|
66 |
"sints n = range (sbintrunc (n - 1))" |
|
67 |
||
68 |
definition unats :: "nat => nat set" where |
|
69 |
"unats n = {i. i < 2 ^ n}" |
|
70 |
||
71 |
definition norm_sint :: "nat => int => int" where |
|
72 |
"norm_sint n w = (w + 2 ^ (n - 1)) mod 2 ^ n - 2 ^ (n - 1)" |
|
73 |
||
74 |
definition scast :: "'a :: len word => 'b :: len word" where |
|
75 |
-- "cast a word to a different length" |
|
76 |
"scast w = word_of_int (sint w)" |
|
77 |
||
78 |
definition ucast :: "'a :: len0 word => 'b :: len0 word" where |
|
79 |
"ucast w = word_of_int (uint w)" |
|
80 |
||
81 |
instantiation word :: (len0) size |
|
82 |
begin |
|
83 |
||
84 |
definition |
|
85 |
word_size: "size (w :: 'a word) = len_of TYPE('a)" |
|
86 |
||
87 |
instance .. |
|
88 |
||
89 |
end |
|
90 |
||
91 |
definition source_size :: "('a :: len0 word => 'b) => nat" where |
|
92 |
-- "whether a cast (or other) function is to a longer or shorter length" |
|
93 |
"source_size c = (let arb = undefined ; x = c arb in size arb)" |
|
94 |
||
95 |
definition target_size :: "('a => 'b :: len0 word) => nat" where |
|
96 |
"target_size c = size (c undefined)" |
|
97 |
||
98 |
definition is_up :: "('a :: len0 word => 'b :: len0 word) => bool" where |
|
99 |
"is_up c \<longleftrightarrow> source_size c <= target_size c" |
|
100 |
||
101 |
definition is_down :: "('a :: len0 word => 'b :: len0 word) => bool" where |
|
102 |
"is_down c \<longleftrightarrow> target_size c <= source_size c" |
|
103 |
||
104 |
definition of_bl :: "bool list => 'a :: len0 word" where |
|
105 |
"of_bl bl = word_of_int (bl_to_bin bl)" |
|
106 |
||
107 |
definition to_bl :: "'a :: len0 word => bool list" where |
|
108 |
"to_bl w = bin_to_bl (len_of TYPE ('a)) (uint w)" |
|
109 |
||
110 |
definition word_reverse :: "'a :: len0 word => 'a word" where |
|
111 |
"word_reverse w = of_bl (rev (to_bl w))" |
|
112 |
||
113 |
definition word_int_case :: "(int => 'b) => ('a :: len0 word) => 'b" where |
|
114 |
"word_int_case f w = f (uint w)" |
|
115 |
||
116 |
syntax |
|
117 |
of_int :: "int => 'a" |
|
118 |
translations |
|
119 |
"case x of CONST of_int y => b" == "CONST word_int_case (%y. b) x" |
|
120 |
||
121 |
||
122 |
subsection "Arithmetic operations" |
|
123 |
||
124 |
instantiation word :: (len0) "{number, uminus, minus, plus, one, zero, times, Divides.div, ord, bit}" |
|
125 |
begin |
|
126 |
||
127 |
definition |
|
128 |
word_0_wi: "0 = word_of_int 0" |
|
129 |
||
130 |
definition |
|
131 |
word_1_wi: "1 = word_of_int 1" |
|
132 |
||
133 |
definition |
|
134 |
word_add_def: "a + b = word_of_int (uint a + uint b)" |
|
135 |
||
136 |
definition |
|
137 |
word_sub_wi: "a - b = word_of_int (uint a - uint b)" |
|
138 |
||
139 |
definition |
|
140 |
word_minus_def: "- a = word_of_int (- uint a)" |
|
141 |
||
142 |
definition |
|
143 |
word_mult_def: "a * b = word_of_int (uint a * uint b)" |
|
144 |
||
145 |
definition |
|
146 |
word_div_def: "a div b = word_of_int (uint a div uint b)" |
|
147 |
||
148 |
definition |
|
149 |
word_mod_def: "a mod b = word_of_int (uint a mod uint b)" |
|
150 |
||
151 |
definition |
|
152 |
word_number_of_def: "number_of w = word_of_int w" |
|
153 |
||
154 |
definition |
|
155 |
word_le_def: "a \<le> b \<longleftrightarrow> uint a \<le> uint b" |
|
156 |
||
157 |
definition |
|
158 |
word_less_def: "x < y \<longleftrightarrow> x \<le> y \<and> x \<noteq> (y \<Colon> 'a word)" |
|
159 |
||
160 |
definition |
|
161 |
word_and_def: |
|
162 |
"(a::'a word) AND b = word_of_int (uint a AND uint b)" |
|
163 |
||
164 |
definition |
|
165 |
word_or_def: |
|
166 |
"(a::'a word) OR b = word_of_int (uint a OR uint b)" |
|
167 |
||
168 |
definition |
|
169 |
word_xor_def: |
|
170 |
"(a::'a word) XOR b = word_of_int (uint a XOR uint b)" |
|
171 |
||
172 |
definition |
|
173 |
word_not_def: |
|
174 |
"NOT (a::'a word) = word_of_int (NOT (uint a))" |
|
175 |
||
176 |
instance .. |
|
177 |
||
178 |
end |
|
179 |
||
180 |
definition |
|
181 |
word_succ :: "'a :: len0 word => 'a word" |
|
182 |
where |
|
183 |
"word_succ a = word_of_int (Int.succ (uint a))" |
|
184 |
||
185 |
definition |
|
186 |
word_pred :: "'a :: len0 word => 'a word" |
|
187 |
where |
|
188 |
"word_pred a = word_of_int (Int.pred (uint a))" |
|
189 |
||
190 |
definition udvd :: "'a::len word => 'a::len word => bool" (infixl "udvd" 50) where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
191 |
"a udvd b = (EX n>=0. uint b = n * uint a)" |
37660 | 192 |
|
193 |
definition word_sle :: "'a :: len word => 'a word => bool" ("(_/ <=s _)" [50, 51] 50) where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
194 |
"a <=s b = (sint a <= sint b)" |
37660 | 195 |
|
196 |
definition word_sless :: "'a :: len word => 'a word => bool" ("(_/ <s _)" [50, 51] 50) where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
197 |
"(x <s y) = (x <=s y & x ~= y)" |
37660 | 198 |
|
199 |
||
200 |
||
201 |
subsection "Bit-wise operations" |
|
202 |
||
203 |
instantiation word :: (len0) bits |
|
204 |
begin |
|
205 |
||
206 |
definition |
|
207 |
word_test_bit_def: "test_bit a = bin_nth (uint a)" |
|
208 |
||
209 |
definition |
|
210 |
word_set_bit_def: "set_bit a n x = |
|
211 |
word_of_int (bin_sc n (If x 1 0) (uint a))" |
|
212 |
||
213 |
definition |
|
214 |
word_set_bits_def: "(BITS n. f n) = of_bl (bl_of_nth (len_of TYPE ('a)) f)" |
|
215 |
||
216 |
definition |
|
217 |
word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a) = 1" |
|
218 |
||
219 |
definition shiftl1 :: "'a word \<Rightarrow> 'a word" where |
|
220 |
"shiftl1 w = word_of_int (uint w BIT 0)" |
|
221 |
||
222 |
definition shiftr1 :: "'a word \<Rightarrow> 'a word" where |
|
223 |
-- "shift right as unsigned or as signed, ie logical or arithmetic" |
|
224 |
"shiftr1 w = word_of_int (bin_rest (uint w))" |
|
225 |
||
226 |
definition |
|
227 |
shiftl_def: "w << n = (shiftl1 ^^ n) w" |
|
228 |
||
229 |
definition |
|
230 |
shiftr_def: "w >> n = (shiftr1 ^^ n) w" |
|
231 |
||
232 |
instance .. |
|
233 |
||
234 |
end |
|
235 |
||
236 |
instantiation word :: (len) bitss |
|
237 |
begin |
|
238 |
||
239 |
definition |
|
240 |
word_msb_def: |
|
241 |
"msb a \<longleftrightarrow> bin_sign (sint a) = Int.Min" |
|
242 |
||
243 |
instance .. |
|
244 |
||
245 |
end |
|
246 |
||
37667 | 247 |
lemma [code]: |
248 |
"msb a \<longleftrightarrow> bin_sign (sint a) = (- 1 :: int)" |
|
249 |
by (simp only: word_msb_def Min_def) |
|
250 |
||
37660 | 251 |
definition setBit :: "'a :: len0 word => nat => 'a word" where |
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
252 |
"setBit w n = set_bit w n True" |
37660 | 253 |
|
254 |
definition clearBit :: "'a :: len0 word => nat => 'a word" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
255 |
"clearBit w n = set_bit w n False" |
37660 | 256 |
|
257 |
||
258 |
subsection "Shift operations" |
|
259 |
||
260 |
definition sshiftr1 :: "'a :: len word => 'a word" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
261 |
"sshiftr1 w = word_of_int (bin_rest (sint w))" |
37660 | 262 |
|
263 |
definition bshiftr1 :: "bool => 'a :: len word => 'a word" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
264 |
"bshiftr1 b w = of_bl (b # butlast (to_bl w))" |
37660 | 265 |
|
266 |
definition sshiftr :: "'a :: len word => nat => 'a word" (infixl ">>>" 55) where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
267 |
"w >>> n = (sshiftr1 ^^ n) w" |
37660 | 268 |
|
269 |
definition mask :: "nat => 'a::len word" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
270 |
"mask n = (1 << n) - 1" |
37660 | 271 |
|
272 |
definition revcast :: "'a :: len0 word => 'b :: len0 word" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
273 |
"revcast w = of_bl (takefill False (len_of TYPE('b)) (to_bl w))" |
37660 | 274 |
|
275 |
definition slice1 :: "nat => 'a :: len0 word => 'b :: len0 word" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
276 |
"slice1 n w = of_bl (takefill False n (to_bl w))" |
37660 | 277 |
|
278 |
definition slice :: "nat => 'a :: len0 word => 'b :: len0 word" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
279 |
"slice n w = slice1 (size w - n) w" |
37660 | 280 |
|
281 |
||
282 |
subsection "Rotation" |
|
283 |
||
284 |
definition rotater1 :: "'a list => 'a list" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
285 |
"rotater1 ys = |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
286 |
(case ys of [] => [] | x # xs => last ys # butlast ys)" |
37660 | 287 |
|
288 |
definition rotater :: "nat => 'a list => 'a list" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
289 |
"rotater n = rotater1 ^^ n" |
37660 | 290 |
|
291 |
definition word_rotr :: "nat => 'a :: len0 word => 'a :: len0 word" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
292 |
"word_rotr n w = of_bl (rotater n (to_bl w))" |
37660 | 293 |
|
294 |
definition word_rotl :: "nat => 'a :: len0 word => 'a :: len0 word" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
295 |
"word_rotl n w = of_bl (rotate n (to_bl w))" |
37660 | 296 |
|
297 |
definition word_roti :: "int => 'a :: len0 word => 'a :: len0 word" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
298 |
"word_roti i w = (if i >= 0 then word_rotr (nat i) w |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
299 |
else word_rotl (nat (- i)) w)" |
37660 | 300 |
|
301 |
||
302 |
subsection "Split and cat operations" |
|
303 |
||
304 |
definition word_cat :: "'a :: len0 word => 'b :: len0 word => 'c :: len0 word" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
305 |
"word_cat a b = word_of_int (bin_cat (uint a) (len_of TYPE ('b)) (uint b))" |
37660 | 306 |
|
307 |
definition word_split :: "'a :: len0 word => ('b :: len0 word) * ('c :: len0 word)" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
308 |
"word_split a = |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
309 |
(case bin_split (len_of TYPE ('c)) (uint a) of |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
310 |
(u, v) => (word_of_int u, word_of_int v))" |
37660 | 311 |
|
312 |
definition word_rcat :: "'a :: len0 word list => 'b :: len0 word" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
313 |
"word_rcat ws = |
37660 | 314 |
word_of_int (bin_rcat (len_of TYPE ('a)) (map uint ws))" |
315 |
||
316 |
definition word_rsplit :: "'a :: len0 word => 'b :: len word list" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
317 |
"word_rsplit w = |
37660 | 318 |
map word_of_int (bin_rsplit (len_of TYPE ('b)) (len_of TYPE ('a), uint w))" |
319 |
||
320 |
definition max_word :: "'a::len word" -- "Largest representable machine integer." where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
321 |
"max_word = word_of_int (2 ^ len_of TYPE('a) - 1)" |
37660 | 322 |
|
323 |
primrec of_bool :: "bool \<Rightarrow> 'a::len word" where |
|
324 |
"of_bool False = 0" |
|
325 |
| "of_bool True = 1" |
|
326 |
||
327 |
||
328 |
lemmas of_nth_def = word_set_bits_def |
|
329 |
||
330 |
lemmas word_size_gt_0 [iff] = |
|
331 |
xtr1 [OF word_size len_gt_0, standard] |
|
332 |
lemmas lens_gt_0 = word_size_gt_0 len_gt_0 |
|
333 |
lemmas lens_not_0 [iff] = lens_gt_0 [THEN gr_implies_not0, standard] |
|
334 |
||
335 |
lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}" |
|
336 |
by (simp add: uints_def range_bintrunc) |
|
337 |
||
338 |
lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}" |
|
339 |
by (simp add: sints_def range_sbintrunc) |
|
340 |
||
341 |
lemmas atLeastLessThan_alt = atLeastLessThan_def [unfolded |
|
342 |
atLeast_def lessThan_def Collect_conj_eq [symmetric]] |
|
343 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
344 |
lemma mod_in_reps: "m > 0 \<Longrightarrow> y mod m : {0::int ..< m}" |
37660 | 345 |
unfolding atLeastLessThan_alt by auto |
346 |
||
347 |
lemma |
|
348 |
uint_0:"0 <= uint x" and |
|
349 |
uint_lt: "uint (x::'a::len0 word) < 2 ^ len_of TYPE('a)" |
|
350 |
by (auto simp: uint [simplified]) |
|
351 |
||
352 |
lemma uint_mod_same: |
|
353 |
"uint x mod 2 ^ len_of TYPE('a) = uint (x::'a::len0 word)" |
|
354 |
by (simp add: int_mod_eq uint_lt uint_0) |
|
355 |
||
356 |
lemma td_ext_uint: |
|
357 |
"td_ext (uint :: 'a word => int) word_of_int (uints (len_of TYPE('a::len0))) |
|
358 |
(%w::int. w mod 2 ^ len_of TYPE('a))" |
|
359 |
apply (unfold td_ext_def') |
|
360 |
apply (simp add: uints_num word_of_int_def bintrunc_mod2p) |
|
361 |
apply (simp add: uint_mod_same uint_0 uint_lt |
|
362 |
word.uint_inverse word.Abs_word_inverse int_mod_lem) |
|
363 |
done |
|
364 |
||
365 |
lemmas int_word_uint = td_ext_uint [THEN td_ext.eq_norm, standard] |
|
366 |
||
367 |
interpretation word_uint: |
|
368 |
td_ext "uint::'a::len0 word \<Rightarrow> int" |
|
369 |
word_of_int |
|
370 |
"uints (len_of TYPE('a::len0))" |
|
371 |
"\<lambda>w. w mod 2 ^ len_of TYPE('a::len0)" |
|
372 |
by (rule td_ext_uint) |
|
373 |
||
374 |
lemmas td_uint = word_uint.td_thm |
|
375 |
||
376 |
lemmas td_ext_ubin = td_ext_uint |
|
377 |
[simplified len_gt_0 no_bintr_alt1 [symmetric]] |
|
378 |
||
379 |
interpretation word_ubin: |
|
380 |
td_ext "uint::'a::len0 word \<Rightarrow> int" |
|
381 |
word_of_int |
|
382 |
"uints (len_of TYPE('a::len0))" |
|
383 |
"bintrunc (len_of TYPE('a::len0))" |
|
384 |
by (rule td_ext_ubin) |
|
385 |
||
386 |
lemma sint_sbintrunc': |
|
387 |
"sint (word_of_int bin :: 'a word) = |
|
388 |
(sbintrunc (len_of TYPE ('a :: len) - 1) bin)" |
|
389 |
unfolding sint_uint |
|
390 |
by (auto simp: word_ubin.eq_norm sbintrunc_bintrunc_lt) |
|
391 |
||
392 |
lemma uint_sint: |
|
393 |
"uint w = bintrunc (len_of TYPE('a)) (sint (w :: 'a :: len word))" |
|
394 |
unfolding sint_uint by (auto simp: bintrunc_sbintrunc_le) |
|
395 |
||
396 |
lemma bintr_uint': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
397 |
"n >= size w \<Longrightarrow> bintrunc n (uint w) = uint w" |
37660 | 398 |
apply (unfold word_size) |
399 |
apply (subst word_ubin.norm_Rep [symmetric]) |
|
400 |
apply (simp only: bintrunc_bintrunc_min word_size) |
|
401 |
apply (simp add: min_max.inf_absorb2) |
|
402 |
done |
|
403 |
||
404 |
lemma wi_bintr': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
405 |
"wb = word_of_int bin \<Longrightarrow> n >= size wb \<Longrightarrow> |
37660 | 406 |
word_of_int (bintrunc n bin) = wb" |
407 |
unfolding word_size |
|
408 |
by (clarsimp simp add: word_ubin.norm_eq_iff [symmetric] min_max.inf_absorb1) |
|
409 |
||
410 |
lemmas bintr_uint = bintr_uint' [unfolded word_size] |
|
411 |
lemmas wi_bintr = wi_bintr' [unfolded word_size] |
|
412 |
||
413 |
lemma td_ext_sbin: |
|
414 |
"td_ext (sint :: 'a word => int) word_of_int (sints (len_of TYPE('a::len))) |
|
415 |
(sbintrunc (len_of TYPE('a) - 1))" |
|
416 |
apply (unfold td_ext_def' sint_uint) |
|
417 |
apply (simp add : word_ubin.eq_norm) |
|
418 |
apply (cases "len_of TYPE('a)") |
|
419 |
apply (auto simp add : sints_def) |
|
420 |
apply (rule sym [THEN trans]) |
|
421 |
apply (rule word_ubin.Abs_norm) |
|
422 |
apply (simp only: bintrunc_sbintrunc) |
|
423 |
apply (drule sym) |
|
424 |
apply simp |
|
425 |
done |
|
426 |
||
427 |
lemmas td_ext_sint = td_ext_sbin |
|
428 |
[simplified len_gt_0 no_sbintr_alt2 Suc_pred' [symmetric]] |
|
429 |
||
430 |
(* We do sint before sbin, before sint is the user version |
|
431 |
and interpretations do not produce thm duplicates. I.e. |
|
432 |
we get the name word_sint.Rep_eqD, but not word_sbin.Req_eqD, |
|
433 |
because the latter is the same thm as the former *) |
|
434 |
interpretation word_sint: |
|
435 |
td_ext "sint ::'a::len word => int" |
|
436 |
word_of_int |
|
437 |
"sints (len_of TYPE('a::len))" |
|
438 |
"%w. (w + 2^(len_of TYPE('a::len) - 1)) mod 2^len_of TYPE('a::len) - |
|
439 |
2 ^ (len_of TYPE('a::len) - 1)" |
|
440 |
by (rule td_ext_sint) |
|
441 |
||
442 |
interpretation word_sbin: |
|
443 |
td_ext "sint ::'a::len word => int" |
|
444 |
word_of_int |
|
445 |
"sints (len_of TYPE('a::len))" |
|
446 |
"sbintrunc (len_of TYPE('a::len) - 1)" |
|
447 |
by (rule td_ext_sbin) |
|
448 |
||
449 |
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm, standard] |
|
450 |
||
451 |
lemmas td_sint = word_sint.td |
|
452 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
453 |
lemma word_number_of_alt [code_unfold_post]: |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
454 |
"number_of b = word_of_int (number_of b)" |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
455 |
by (simp add: number_of_eq word_number_of_def) |
37660 | 456 |
|
457 |
lemma word_no_wi: "number_of = word_of_int" |
|
44762 | 458 |
by (auto simp: word_number_of_def) |
37660 | 459 |
|
460 |
lemma to_bl_def': |
|
461 |
"(to_bl :: 'a :: len0 word => bool list) = |
|
462 |
bin_to_bl (len_of TYPE('a)) o uint" |
|
44762 | 463 |
by (auto simp: to_bl_def) |
37660 | 464 |
|
465 |
lemmas word_reverse_no_def [simp] = word_reverse_def [of "number_of w", standard] |
|
466 |
||
467 |
lemmas uints_mod = uints_def [unfolded no_bintr_alt1] |
|
468 |
||
469 |
lemma uint_bintrunc: "uint (number_of bin :: 'a word) = |
|
470 |
number_of (bintrunc (len_of TYPE ('a :: len0)) bin)" |
|
471 |
unfolding word_number_of_def number_of_eq |
|
472 |
by (auto intro: word_ubin.eq_norm) |
|
473 |
||
474 |
lemma sint_sbintrunc: "sint (number_of bin :: 'a word) = |
|
475 |
number_of (sbintrunc (len_of TYPE ('a :: len) - 1) bin)" |
|
476 |
unfolding word_number_of_def number_of_eq |
|
477 |
by (subst word_sbin.eq_norm) simp |
|
478 |
||
479 |
lemma unat_bintrunc: |
|
480 |
"unat (number_of bin :: 'a :: len0 word) = |
|
481 |
number_of (bintrunc (len_of TYPE('a)) bin)" |
|
482 |
unfolding unat_def nat_number_of_def |
|
483 |
by (simp only: uint_bintrunc) |
|
484 |
||
485 |
(* WARNING - these may not always be helpful *) |
|
486 |
declare |
|
487 |
uint_bintrunc [simp] |
|
488 |
sint_sbintrunc [simp] |
|
489 |
unat_bintrunc [simp] |
|
490 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
491 |
lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 \<Longrightarrow> v = w" |
37660 | 492 |
apply (unfold word_size) |
493 |
apply (rule word_uint.Rep_eqD) |
|
494 |
apply (rule box_equals) |
|
495 |
defer |
|
496 |
apply (rule word_ubin.norm_Rep)+ |
|
497 |
apply simp |
|
498 |
done |
|
499 |
||
500 |
lemmas uint_lem = word_uint.Rep [unfolded uints_num mem_Collect_eq] |
|
501 |
lemmas sint_lem = word_sint.Rep [unfolded sints_num mem_Collect_eq] |
|
502 |
lemmas uint_ge_0 [iff] = uint_lem [THEN conjunct1, standard] |
|
503 |
lemmas uint_lt2p [iff] = uint_lem [THEN conjunct2, standard] |
|
504 |
lemmas sint_ge = sint_lem [THEN conjunct1, standard] |
|
505 |
lemmas sint_lt = sint_lem [THEN conjunct2, standard] |
|
506 |
||
507 |
lemma sign_uint_Pls [simp]: |
|
508 |
"bin_sign (uint x) = Int.Pls" |
|
509 |
by (simp add: sign_Pls_ge_0 number_of_eq) |
|
510 |
||
511 |
lemmas uint_m2p_neg = iffD2 [OF diff_less_0_iff_less uint_lt2p, standard] |
|
512 |
lemmas uint_m2p_not_non_neg = |
|
513 |
iffD2 [OF linorder_not_le uint_m2p_neg, standard] |
|
514 |
||
515 |
lemma lt2p_lem: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
516 |
"len_of TYPE('a) <= n \<Longrightarrow> uint (w :: 'a :: len0 word) < 2 ^ n" |
37660 | 517 |
by (rule xtr8 [OF _ uint_lt2p]) simp |
518 |
||
519 |
lemmas uint_le_0_iff [simp] = |
|
520 |
uint_ge_0 [THEN leD, THEN linorder_antisym_conv1, standard] |
|
521 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
522 |
lemma uint_nat: "uint w = int (unat w)" |
37660 | 523 |
unfolding unat_def by auto |
524 |
||
525 |
lemma uint_number_of: |
|
526 |
"uint (number_of b :: 'a :: len0 word) = number_of b mod 2 ^ len_of TYPE('a)" |
|
527 |
unfolding word_number_of_alt |
|
528 |
by (simp only: int_word_uint) |
|
529 |
||
530 |
lemma unat_number_of: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
531 |
"bin_sign b = Int.Pls \<Longrightarrow> |
37660 | 532 |
unat (number_of b::'a::len0 word) = number_of b mod 2 ^ len_of TYPE ('a)" |
533 |
apply (unfold unat_def) |
|
534 |
apply (clarsimp simp only: uint_number_of) |
|
535 |
apply (rule nat_mod_distrib [THEN trans]) |
|
536 |
apply (erule sign_Pls_ge_0 [THEN iffD1]) |
|
537 |
apply (simp_all add: nat_power_eq) |
|
538 |
done |
|
539 |
||
540 |
lemma sint_number_of: "sint (number_of b :: 'a :: len word) = (number_of b + |
|
541 |
2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) - |
|
542 |
2 ^ (len_of TYPE('a) - 1)" |
|
543 |
unfolding word_number_of_alt by (rule int_word_sint) |
|
544 |
||
545 |
lemma word_of_int_bin [simp] : |
|
546 |
"(word_of_int (number_of bin) :: 'a :: len0 word) = (number_of bin)" |
|
547 |
unfolding word_number_of_alt by auto |
|
548 |
||
549 |
lemma word_int_case_wi: |
|
550 |
"word_int_case f (word_of_int i :: 'b word) = |
|
551 |
f (i mod 2 ^ len_of TYPE('b::len0))" |
|
552 |
unfolding word_int_case_def by (simp add: word_uint.eq_norm) |
|
553 |
||
554 |
lemma word_int_split: |
|
555 |
"P (word_int_case f x) = |
|
556 |
(ALL i. x = (word_of_int i :: 'b :: len0 word) & |
|
557 |
0 <= i & i < 2 ^ len_of TYPE('b) --> P (f i))" |
|
558 |
unfolding word_int_case_def |
|
559 |
by (auto simp: word_uint.eq_norm int_mod_eq') |
|
560 |
||
561 |
lemma word_int_split_asm: |
|
562 |
"P (word_int_case f x) = |
|
563 |
(~ (EX n. x = (word_of_int n :: 'b::len0 word) & |
|
564 |
0 <= n & n < 2 ^ len_of TYPE('b::len0) & ~ P (f n)))" |
|
565 |
unfolding word_int_case_def |
|
566 |
by (auto simp: word_uint.eq_norm int_mod_eq') |
|
567 |
||
568 |
lemmas uint_range' = |
|
569 |
word_uint.Rep [unfolded uints_num mem_Collect_eq, standard] |
|
570 |
lemmas sint_range' = word_sint.Rep [unfolded One_nat_def |
|
571 |
sints_num mem_Collect_eq, standard] |
|
572 |
||
573 |
lemma uint_range_size: "0 <= uint w & uint w < 2 ^ size w" |
|
574 |
unfolding word_size by (rule uint_range') |
|
575 |
||
576 |
lemma sint_range_size: |
|
577 |
"- (2 ^ (size w - Suc 0)) <= sint w & sint w < 2 ^ (size w - Suc 0)" |
|
578 |
unfolding word_size by (rule sint_range') |
|
579 |
||
580 |
lemmas sint_above_size = sint_range_size |
|
581 |
[THEN conjunct2, THEN [2] xtr8, folded One_nat_def, standard] |
|
582 |
||
583 |
lemmas sint_below_size = sint_range_size |
|
584 |
[THEN conjunct1, THEN [2] order_trans, folded One_nat_def, standard] |
|
585 |
||
586 |
lemma test_bit_eq_iff: "(test_bit (u::'a::len0 word) = test_bit v) = (u = v)" |
|
587 |
unfolding word_test_bit_def by (simp add: bin_nth_eq_iff) |
|
588 |
||
589 |
lemma test_bit_size [rule_format] : "(w::'a::len0 word) !! n --> n < size w" |
|
590 |
apply (unfold word_test_bit_def) |
|
591 |
apply (subst word_ubin.norm_Rep [symmetric]) |
|
592 |
apply (simp only: nth_bintr word_size) |
|
593 |
apply fast |
|
594 |
done |
|
595 |
||
596 |
lemma word_eqI [rule_format] : |
|
597 |
fixes u :: "'a::len0 word" |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
598 |
shows "(ALL n. n < size u --> u !! n = v !! n) \<Longrightarrow> u = v" |
37660 | 599 |
apply (rule test_bit_eq_iff [THEN iffD1]) |
600 |
apply (rule ext) |
|
601 |
apply (erule allE) |
|
602 |
apply (erule impCE) |
|
603 |
prefer 2 |
|
604 |
apply assumption |
|
605 |
apply (auto dest!: test_bit_size simp add: word_size) |
|
606 |
done |
|
607 |
||
608 |
lemmas word_eqD = test_bit_eq_iff [THEN iffD2, THEN fun_cong, standard] |
|
609 |
||
610 |
lemma test_bit_bin': "w !! n = (n < size w & bin_nth (uint w) n)" |
|
611 |
unfolding word_test_bit_def word_size |
|
612 |
by (simp add: nth_bintr [symmetric]) |
|
613 |
||
614 |
lemmas test_bit_bin = test_bit_bin' [unfolded word_size] |
|
615 |
||
616 |
lemma bin_nth_uint_imp': "bin_nth (uint w) n --> n < size w" |
|
617 |
apply (unfold word_size) |
|
618 |
apply (rule impI) |
|
619 |
apply (rule nth_bintr [THEN iffD1, THEN conjunct1]) |
|
620 |
apply (subst word_ubin.norm_Rep) |
|
621 |
apply assumption |
|
622 |
done |
|
623 |
||
624 |
lemma bin_nth_sint': |
|
625 |
"n >= size w --> bin_nth (sint w) n = bin_nth (sint w) (size w - 1)" |
|
626 |
apply (rule impI) |
|
627 |
apply (subst word_sbin.norm_Rep [symmetric]) |
|
628 |
apply (simp add : nth_sbintr word_size) |
|
629 |
apply auto |
|
630 |
done |
|
631 |
||
632 |
lemmas bin_nth_uint_imp = bin_nth_uint_imp' [rule_format, unfolded word_size] |
|
633 |
lemmas bin_nth_sint = bin_nth_sint' [rule_format, unfolded word_size] |
|
634 |
||
635 |
(* type definitions theorem for in terms of equivalent bool list *) |
|
636 |
lemma td_bl: |
|
637 |
"type_definition (to_bl :: 'a::len0 word => bool list) |
|
638 |
of_bl |
|
639 |
{bl. length bl = len_of TYPE('a)}" |
|
640 |
apply (unfold type_definition_def of_bl_def to_bl_def) |
|
641 |
apply (simp add: word_ubin.eq_norm) |
|
642 |
apply safe |
|
643 |
apply (drule sym) |
|
644 |
apply simp |
|
645 |
done |
|
646 |
||
647 |
interpretation word_bl: |
|
648 |
type_definition "to_bl :: 'a::len0 word => bool list" |
|
649 |
of_bl |
|
650 |
"{bl. length bl = len_of TYPE('a::len0)}" |
|
651 |
by (rule td_bl) |
|
652 |
||
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
653 |
lemmas word_bl_Rep' = word_bl.Rep [simplified, iff] |
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
654 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
655 |
lemma word_size_bl: "size w = size (to_bl w)" |
37660 | 656 |
unfolding word_size by auto |
657 |
||
658 |
lemma to_bl_use_of_bl: |
|
659 |
"(to_bl w = bl) = (w = of_bl bl \<and> length bl = length (to_bl w))" |
|
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44821
diff
changeset
|
660 |
by (fastforce elim!: word_bl.Abs_inverse [simplified]) |
37660 | 661 |
|
662 |
lemma to_bl_word_rev: "to_bl (word_reverse w) = rev (to_bl w)" |
|
663 |
unfolding word_reverse_def by (simp add: word_bl.Abs_inverse) |
|
664 |
||
665 |
lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w" |
|
666 |
unfolding word_reverse_def by (simp add : word_bl.Abs_inverse) |
|
667 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
668 |
lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w" |
37660 | 669 |
by auto |
670 |
||
671 |
lemmas word_rev_gal' = sym [THEN word_rev_gal, symmetric, standard] |
|
672 |
||
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
673 |
lemmas length_bl_gt_0 [iff] = xtr1 [OF word_bl_Rep' len_gt_0, standard] |
37660 | 674 |
lemmas bl_not_Nil [iff] = |
675 |
length_bl_gt_0 [THEN length_greater_0_conv [THEN iffD1], standard] |
|
676 |
lemmas length_bl_neq_0 [iff] = length_bl_gt_0 [THEN gr_implies_not0] |
|
677 |
||
678 |
lemma hd_bl_sign_sint: "hd (to_bl w) = (bin_sign (sint w) = Int.Min)" |
|
679 |
apply (unfold to_bl_def sint_uint) |
|
680 |
apply (rule trans [OF _ bl_sbin_sign]) |
|
681 |
apply simp |
|
682 |
done |
|
683 |
||
684 |
lemma of_bl_drop': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
685 |
"lend = length bl - len_of TYPE ('a :: len0) \<Longrightarrow> |
37660 | 686 |
of_bl (drop lend bl) = (of_bl bl :: 'a word)" |
687 |
apply (unfold of_bl_def) |
|
688 |
apply (clarsimp simp add : trunc_bl2bin [symmetric]) |
|
689 |
done |
|
690 |
||
691 |
lemmas of_bl_no = of_bl_def [folded word_number_of_def] |
|
692 |
||
693 |
lemma test_bit_of_bl: |
|
694 |
"(of_bl bl::'a::len0 word) !! n = (rev bl ! n \<and> n < len_of TYPE('a) \<and> n < length bl)" |
|
695 |
apply (unfold of_bl_def word_test_bit_def) |
|
696 |
apply (auto simp add: word_size word_ubin.eq_norm nth_bintr bin_nth_of_bl) |
|
697 |
done |
|
698 |
||
699 |
lemma no_of_bl: |
|
700 |
"(number_of bin ::'a::len0 word) = of_bl (bin_to_bl (len_of TYPE ('a)) bin)" |
|
701 |
unfolding word_size of_bl_no by (simp add : word_number_of_def) |
|
702 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
703 |
lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)" |
37660 | 704 |
unfolding word_size to_bl_def by auto |
705 |
||
706 |
lemma to_bl_bin: "bl_to_bin (to_bl w) = uint w" |
|
707 |
unfolding uint_bl by (simp add : word_size) |
|
708 |
||
709 |
lemma to_bl_of_bin: |
|
710 |
"to_bl (word_of_int bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin" |
|
711 |
unfolding uint_bl by (clarsimp simp add: word_ubin.eq_norm word_size) |
|
712 |
||
713 |
lemmas to_bl_no_bin [simp] = to_bl_of_bin [folded word_number_of_def] |
|
714 |
||
715 |
lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w" |
|
716 |
unfolding uint_bl by (simp add : word_size) |
|
717 |
||
718 |
lemmas uint_bl_bin [simp] = trans [OF bin_bl_bin word_ubin.norm_Rep, standard] |
|
719 |
||
720 |
lemmas num_AB_u [simp] = word_uint.Rep_inverse |
|
721 |
[unfolded o_def word_number_of_def [symmetric], standard] |
|
722 |
lemmas num_AB_s [simp] = word_sint.Rep_inverse |
|
723 |
[unfolded o_def word_number_of_def [symmetric], standard] |
|
724 |
||
725 |
(* naturals *) |
|
726 |
lemma uints_unats: "uints n = int ` unats n" |
|
727 |
apply (unfold unats_def uints_num) |
|
728 |
apply safe |
|
729 |
apply (rule_tac image_eqI) |
|
730 |
apply (erule_tac nat_0_le [symmetric]) |
|
731 |
apply auto |
|
732 |
apply (erule_tac nat_less_iff [THEN iffD2]) |
|
733 |
apply (rule_tac [2] zless_nat_eq_int_zless [THEN iffD1]) |
|
734 |
apply (auto simp add : nat_power_eq int_power) |
|
735 |
done |
|
736 |
||
737 |
lemma unats_uints: "unats n = nat ` uints n" |
|
738 |
by (auto simp add : uints_unats image_iff) |
|
739 |
||
740 |
lemmas bintr_num = word_ubin.norm_eq_iff |
|
741 |
[symmetric, folded word_number_of_def, standard] |
|
742 |
lemmas sbintr_num = word_sbin.norm_eq_iff |
|
743 |
[symmetric, folded word_number_of_def, standard] |
|
744 |
||
745 |
lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def, standard] |
|
45284
ae78a4ffa81d
use simproc_setup for cancellation simprocs, to get proper name bindings
huffman
parents:
44938
diff
changeset
|
746 |
lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def, standard] |
37660 | 747 |
|
748 |
(* don't add these to simpset, since may want bintrunc n w to be simplified; |
|
749 |
may want these in reverse, but loop as simp rules, so use following *) |
|
750 |
||
751 |
lemma num_of_bintr': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
752 |
"bintrunc (len_of TYPE('a :: len0)) a = b \<Longrightarrow> |
37660 | 753 |
number_of a = (number_of b :: 'a word)" |
754 |
apply safe |
|
755 |
apply (rule_tac num_of_bintr [symmetric]) |
|
756 |
done |
|
757 |
||
758 |
lemma num_of_sbintr': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
759 |
"sbintrunc (len_of TYPE('a :: len) - 1) a = b \<Longrightarrow> |
37660 | 760 |
number_of a = (number_of b :: 'a word)" |
761 |
apply safe |
|
762 |
apply (rule_tac num_of_sbintr [symmetric]) |
|
763 |
done |
|
764 |
||
765 |
lemmas num_abs_bintr = sym [THEN trans, |
|
766 |
OF num_of_bintr word_number_of_def, standard] |
|
767 |
lemmas num_abs_sbintr = sym [THEN trans, |
|
768 |
OF num_of_sbintr word_number_of_def, standard] |
|
769 |
||
770 |
(** cast - note, no arg for new length, as it's determined by type of result, |
|
771 |
thus in "cast w = w, the type means cast to length of w! **) |
|
772 |
||
773 |
lemma ucast_id: "ucast w = w" |
|
774 |
unfolding ucast_def by auto |
|
775 |
||
776 |
lemma scast_id: "scast w = w" |
|
777 |
unfolding scast_def by auto |
|
778 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
779 |
lemma ucast_bl: "ucast w = of_bl (to_bl w)" |
37660 | 780 |
unfolding ucast_def of_bl_def uint_bl |
781 |
by (auto simp add : word_size) |
|
782 |
||
783 |
lemma nth_ucast: |
|
784 |
"(ucast w::'a::len0 word) !! n = (w !! n & n < len_of TYPE('a))" |
|
785 |
apply (unfold ucast_def test_bit_bin) |
|
786 |
apply (simp add: word_ubin.eq_norm nth_bintr word_size) |
|
787 |
apply (fast elim!: bin_nth_uint_imp) |
|
788 |
done |
|
789 |
||
790 |
(* for literal u(s)cast *) |
|
791 |
||
792 |
lemma ucast_bintr [simp]: |
|
793 |
"ucast (number_of w ::'a::len0 word) = |
|
794 |
number_of (bintrunc (len_of TYPE('a)) w)" |
|
795 |
unfolding ucast_def by simp |
|
796 |
||
797 |
lemma scast_sbintr [simp]: |
|
798 |
"scast (number_of w ::'a::len word) = |
|
799 |
number_of (sbintrunc (len_of TYPE('a) - Suc 0) w)" |
|
800 |
unfolding scast_def by simp |
|
801 |
||
802 |
lemmas source_size = source_size_def [unfolded Let_def word_size] |
|
803 |
lemmas target_size = target_size_def [unfolded Let_def word_size] |
|
804 |
lemmas is_down = is_down_def [unfolded source_size target_size] |
|
805 |
lemmas is_up = is_up_def [unfolded source_size target_size] |
|
806 |
||
807 |
lemmas is_up_down = trans [OF is_up is_down [symmetric], standard] |
|
808 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
809 |
lemma down_cast_same': "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc = scast" |
37660 | 810 |
apply (unfold is_down) |
811 |
apply safe |
|
812 |
apply (rule ext) |
|
813 |
apply (unfold ucast_def scast_def uint_sint) |
|
814 |
apply (rule word_ubin.norm_eq_iff [THEN iffD1]) |
|
815 |
apply simp |
|
816 |
done |
|
817 |
||
818 |
lemma word_rev_tf': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
819 |
"r = to_bl (of_bl bl) \<Longrightarrow> r = rev (takefill False (length r) (rev bl))" |
37660 | 820 |
unfolding of_bl_def uint_bl |
821 |
by (clarsimp simp add: bl_bin_bl_rtf word_ubin.eq_norm word_size) |
|
822 |
||
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
823 |
lemmas word_rev_tf = refl [THEN word_rev_tf', unfolded word_bl_Rep', standard] |
37660 | 824 |
|
825 |
lemmas word_rep_drop = word_rev_tf [simplified takefill_alt, |
|
826 |
simplified, simplified rev_take, simplified] |
|
827 |
||
828 |
lemma to_bl_ucast: |
|
829 |
"to_bl (ucast (w::'b::len0 word) ::'a::len0 word) = |
|
830 |
replicate (len_of TYPE('a) - len_of TYPE('b)) False @ |
|
831 |
drop (len_of TYPE('b) - len_of TYPE('a)) (to_bl w)" |
|
832 |
apply (unfold ucast_bl) |
|
833 |
apply (rule trans) |
|
834 |
apply (rule word_rep_drop) |
|
835 |
apply simp |
|
836 |
done |
|
837 |
||
838 |
lemma ucast_up_app': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
839 |
"uc = ucast \<Longrightarrow> source_size uc + n = target_size uc \<Longrightarrow> |
37660 | 840 |
to_bl (uc w) = replicate n False @ (to_bl w)" |
841 |
by (auto simp add : source_size target_size to_bl_ucast) |
|
842 |
||
843 |
lemma ucast_down_drop': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
844 |
"uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow> |
37660 | 845 |
to_bl (uc w) = drop n (to_bl w)" |
846 |
by (auto simp add : source_size target_size to_bl_ucast) |
|
847 |
||
848 |
lemma scast_down_drop': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
849 |
"sc = scast \<Longrightarrow> source_size sc = target_size sc + n \<Longrightarrow> |
37660 | 850 |
to_bl (sc w) = drop n (to_bl w)" |
851 |
apply (subgoal_tac "sc = ucast") |
|
852 |
apply safe |
|
853 |
apply simp |
|
854 |
apply (erule refl [THEN ucast_down_drop']) |
|
855 |
apply (rule refl [THEN down_cast_same', symmetric]) |
|
856 |
apply (simp add : source_size target_size is_down) |
|
857 |
done |
|
858 |
||
859 |
lemma sint_up_scast': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
860 |
"sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> sint (sc w) = sint w" |
37660 | 861 |
apply (unfold is_up) |
862 |
apply safe |
|
863 |
apply (simp add: scast_def word_sbin.eq_norm) |
|
864 |
apply (rule box_equals) |
|
865 |
prefer 3 |
|
866 |
apply (rule word_sbin.norm_Rep) |
|
867 |
apply (rule sbintrunc_sbintrunc_l) |
|
868 |
defer |
|
869 |
apply (subst word_sbin.norm_Rep) |
|
870 |
apply (rule refl) |
|
871 |
apply simp |
|
872 |
done |
|
873 |
||
874 |
lemma uint_up_ucast': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
875 |
"uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> uint (uc w) = uint w" |
37660 | 876 |
apply (unfold is_up) |
877 |
apply safe |
|
878 |
apply (rule bin_eqI) |
|
879 |
apply (fold word_test_bit_def) |
|
880 |
apply (auto simp add: nth_ucast) |
|
881 |
apply (auto simp add: test_bit_bin) |
|
882 |
done |
|
883 |
||
884 |
lemmas down_cast_same = refl [THEN down_cast_same'] |
|
885 |
lemmas ucast_up_app = refl [THEN ucast_up_app'] |
|
886 |
lemmas ucast_down_drop = refl [THEN ucast_down_drop'] |
|
887 |
lemmas scast_down_drop = refl [THEN scast_down_drop'] |
|
888 |
lemmas uint_up_ucast = refl [THEN uint_up_ucast'] |
|
889 |
lemmas sint_up_scast = refl [THEN sint_up_scast'] |
|
890 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
891 |
lemma ucast_up_ucast': "uc = ucast \<Longrightarrow> is_up uc \<Longrightarrow> ucast (uc w) = ucast w" |
37660 | 892 |
apply (simp (no_asm) add: ucast_def) |
893 |
apply (clarsimp simp add: uint_up_ucast) |
|
894 |
done |
|
895 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
896 |
lemma scast_up_scast': "sc = scast \<Longrightarrow> is_up sc \<Longrightarrow> scast (sc w) = scast w" |
37660 | 897 |
apply (simp (no_asm) add: scast_def) |
898 |
apply (clarsimp simp add: sint_up_scast) |
|
899 |
done |
|
900 |
||
901 |
lemma ucast_of_bl_up': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
902 |
"w = of_bl bl \<Longrightarrow> size bl <= size w \<Longrightarrow> ucast w = of_bl bl" |
37660 | 903 |
by (auto simp add : nth_ucast word_size test_bit_of_bl intro!: word_eqI) |
904 |
||
905 |
lemmas ucast_up_ucast = refl [THEN ucast_up_ucast'] |
|
906 |
lemmas scast_up_scast = refl [THEN scast_up_scast'] |
|
907 |
lemmas ucast_of_bl_up = refl [THEN ucast_of_bl_up'] |
|
908 |
||
909 |
lemmas ucast_up_ucast_id = trans [OF ucast_up_ucast ucast_id] |
|
910 |
lemmas scast_up_scast_id = trans [OF scast_up_scast scast_id] |
|
911 |
||
912 |
lemmas isduu = is_up_down [where c = "ucast", THEN iffD2] |
|
913 |
lemmas isdus = is_up_down [where c = "scast", THEN iffD2] |
|
914 |
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] |
|
915 |
lemmas scast_down_scast_id = isdus [THEN ucast_up_ucast_id] |
|
916 |
||
917 |
lemma up_ucast_surj: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
918 |
"is_up (ucast :: 'b::len0 word => 'a::len0 word) \<Longrightarrow> |
37660 | 919 |
surj (ucast :: 'a word => 'b word)" |
920 |
by (rule surjI, erule ucast_up_ucast_id) |
|
921 |
||
922 |
lemma up_scast_surj: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
923 |
"is_up (scast :: 'b::len word => 'a::len word) \<Longrightarrow> |
37660 | 924 |
surj (scast :: 'a word => 'b word)" |
925 |
by (rule surjI, erule scast_up_scast_id) |
|
926 |
||
927 |
lemma down_scast_inj: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
928 |
"is_down (scast :: 'b::len word => 'a::len word) \<Longrightarrow> |
37660 | 929 |
inj_on (ucast :: 'a word => 'b word) A" |
930 |
by (rule inj_on_inverseI, erule scast_down_scast_id) |
|
931 |
||
932 |
lemma down_ucast_inj: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
933 |
"is_down (ucast :: 'b::len0 word => 'a::len0 word) \<Longrightarrow> |
37660 | 934 |
inj_on (ucast :: 'a word => 'b word) A" |
935 |
by (rule inj_on_inverseI, erule ucast_down_ucast_id) |
|
936 |
||
937 |
lemma of_bl_append_same: "of_bl (X @ to_bl w) = w" |
|
938 |
by (rule word_bl.Rep_eqD) (simp add: word_rep_drop) |
|
939 |
||
940 |
lemma ucast_down_no': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
941 |
"uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (number_of bin) = number_of bin" |
37660 | 942 |
apply (unfold word_number_of_def is_down) |
943 |
apply (clarsimp simp add: ucast_def word_ubin.eq_norm) |
|
944 |
apply (rule word_ubin.norm_eq_iff [THEN iffD1]) |
|
945 |
apply (erule bintrunc_bintrunc_ge) |
|
946 |
done |
|
947 |
||
948 |
lemmas ucast_down_no = ucast_down_no' [OF refl] |
|
949 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
950 |
lemma ucast_down_bl': "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (of_bl bl) = of_bl bl" |
37660 | 951 |
unfolding of_bl_no by clarify (erule ucast_down_no) |
952 |
||
953 |
lemmas ucast_down_bl = ucast_down_bl' [OF refl] |
|
954 |
||
955 |
lemmas slice_def' = slice_def [unfolded word_size] |
|
956 |
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] |
|
957 |
||
958 |
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def |
|
959 |
lemmas word_log_bin_defs = word_log_defs |
|
960 |
||
961 |
text {* Executable equality *} |
|
962 |
||
38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38527
diff
changeset
|
963 |
instantiation word :: (len0) equal |
24333 | 964 |
begin |
965 |
||
38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38527
diff
changeset
|
966 |
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" where |
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38527
diff
changeset
|
967 |
"equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)" |
37660 | 968 |
|
969 |
instance proof |
|
38857
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
haftmann
parents:
38527
diff
changeset
|
970 |
qed (simp add: equal equal_word_def) |
37660 | 971 |
|
972 |
end |
|
973 |
||
974 |
||
975 |
subsection {* Word Arithmetic *} |
|
976 |
||
977 |
lemma word_less_alt: "(a < b) = (uint a < uint b)" |
|
978 |
unfolding word_less_def word_le_def |
|
979 |
by (auto simp del: word_uint.Rep_inject |
|
980 |
simp: word_uint.Rep_inject [symmetric]) |
|
981 |
||
982 |
lemma signed_linorder: "class.linorder word_sle word_sless" |
|
983 |
proof |
|
984 |
qed (unfold word_sle_def word_sless_def, auto) |
|
985 |
||
986 |
interpretation signed: linorder "word_sle" "word_sless" |
|
987 |
by (rule signed_linorder) |
|
988 |
||
989 |
lemmas word_arith_wis = |
|
990 |
word_add_def word_mult_def word_minus_def |
|
991 |
word_succ_def word_pred_def word_0_wi word_1_wi |
|
992 |
||
993 |
lemma udvdI: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
994 |
"0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" |
37660 | 995 |
by (auto simp: udvd_def) |
996 |
||
997 |
lemmas word_div_no [simp] = |
|
998 |
word_div_def [of "number_of a" "number_of b", standard] |
|
999 |
||
1000 |
lemmas word_mod_no [simp] = |
|
1001 |
word_mod_def [of "number_of a" "number_of b", standard] |
|
1002 |
||
1003 |
lemmas word_less_no [simp] = |
|
1004 |
word_less_def [of "number_of a" "number_of b", standard] |
|
1005 |
||
1006 |
lemmas word_le_no [simp] = |
|
1007 |
word_le_def [of "number_of a" "number_of b", standard] |
|
1008 |
||
1009 |
lemmas word_sless_no [simp] = |
|
1010 |
word_sless_def [of "number_of a" "number_of b", standard] |
|
1011 |
||
1012 |
lemmas word_sle_no [simp] = |
|
1013 |
word_sle_def [of "number_of a" "number_of b", standard] |
|
1014 |
||
1015 |
(* following two are available in class number_ring, |
|
1016 |
but convenient to have them here here; |
|
1017 |
note - the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1 |
|
1018 |
are in the default simpset, so to use the automatic simplifications for |
|
1019 |
(eg) sint (number_of bin) on sint 1, must do |
|
1020 |
(simp add: word_1_no del: numeral_1_eq_1) |
|
1021 |
*) |
|
1022 |
lemmas word_0_wi_Pls = word_0_wi [folded Pls_def] |
|
1023 |
lemmas word_0_no = word_0_wi_Pls [folded word_no_wi] |
|
1024 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1025 |
lemma int_one_bin: "(1 :: int) = (Int.Pls BIT 1)" |
37660 | 1026 |
unfolding Pls_def Bit_def by auto |
1027 |
||
1028 |
lemma word_1_no: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1029 |
"(1 :: 'a :: len0 word) = number_of (Int.Pls BIT 1)" |
37660 | 1030 |
unfolding word_1_wi word_number_of_def int_one_bin by auto |
1031 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1032 |
lemma word_m1_wi: "-1 = word_of_int -1" |
37660 | 1033 |
by (rule word_number_of_alt) |
1034 |
||
1035 |
lemma word_m1_wi_Min: "-1 = word_of_int Int.Min" |
|
1036 |
by (simp add: word_m1_wi number_of_eq) |
|
1037 |
||
1038 |
lemma word_0_bl: "of_bl [] = 0" |
|
1039 |
unfolding word_0_wi of_bl_def by (simp add : Pls_def) |
|
1040 |
||
1041 |
lemma word_1_bl: "of_bl [True] = 1" |
|
1042 |
unfolding word_1_wi of_bl_def |
|
1043 |
by (simp add : bl_to_bin_def Bit_def Pls_def) |
|
1044 |
||
1045 |
lemma uint_eq_0 [simp] : "(uint 0 = 0)" |
|
1046 |
unfolding word_0_wi |
|
1047 |
by (simp add: word_ubin.eq_norm Pls_def [symmetric]) |
|
1048 |
||
1049 |
lemma of_bl_0 [simp] : "of_bl (replicate n False) = 0" |
|
1050 |
by (simp add : word_0_wi of_bl_def bl_to_bin_rep_False Pls_def) |
|
1051 |
||
1052 |
lemma to_bl_0: |
|
1053 |
"to_bl (0::'a::len0 word) = replicate (len_of TYPE('a)) False" |
|
1054 |
unfolding uint_bl |
|
1055 |
by (simp add : word_size bin_to_bl_Pls Pls_def [symmetric]) |
|
1056 |
||
1057 |
lemma uint_0_iff: "(uint x = 0) = (x = 0)" |
|
1058 |
by (auto intro!: word_uint.Rep_eqD) |
|
1059 |
||
1060 |
lemma unat_0_iff: "(unat x = 0) = (x = 0)" |
|
1061 |
unfolding unat_def by (auto simp add : nat_eq_iff uint_0_iff) |
|
1062 |
||
1063 |
lemma unat_0 [simp]: "unat 0 = 0" |
|
1064 |
unfolding unat_def by auto |
|
1065 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1066 |
lemma size_0_same': "size w = 0 \<Longrightarrow> w = (v :: 'a :: len0 word)" |
37660 | 1067 |
apply (unfold word_size) |
1068 |
apply (rule box_equals) |
|
1069 |
defer |
|
1070 |
apply (rule word_uint.Rep_inverse)+ |
|
1071 |
apply (rule word_ubin.norm_eq_iff [THEN iffD1]) |
|
1072 |
apply simp |
|
1073 |
done |
|
1074 |
||
1075 |
lemmas size_0_same = size_0_same' [folded word_size] |
|
1076 |
||
1077 |
lemmas unat_eq_0 = unat_0_iff |
|
1078 |
lemmas unat_eq_zero = unat_0_iff |
|
1079 |
||
1080 |
lemma unat_gt_0: "(0 < unat x) = (x ~= 0)" |
|
1081 |
by (auto simp: unat_0_iff [symmetric]) |
|
1082 |
||
1083 |
lemma ucast_0 [simp] : "ucast 0 = 0" |
|
1084 |
unfolding ucast_def |
|
1085 |
by simp (simp add: word_0_wi) |
|
1086 |
||
1087 |
lemma sint_0 [simp] : "sint 0 = 0" |
|
1088 |
unfolding sint_uint |
|
1089 |
by (simp add: Pls_def [symmetric]) |
|
1090 |
||
1091 |
lemma scast_0 [simp] : "scast 0 = 0" |
|
1092 |
apply (unfold scast_def) |
|
1093 |
apply simp |
|
1094 |
apply (simp add: word_0_wi) |
|
1095 |
done |
|
1096 |
||
1097 |
lemma sint_n1 [simp] : "sint -1 = -1" |
|
1098 |
apply (unfold word_m1_wi_Min) |
|
1099 |
apply (simp add: word_sbin.eq_norm) |
|
1100 |
apply (unfold Min_def number_of_eq) |
|
1101 |
apply simp |
|
1102 |
done |
|
1103 |
||
1104 |
lemma scast_n1 [simp] : "scast -1 = -1" |
|
1105 |
apply (unfold scast_def sint_n1) |
|
1106 |
apply (unfold word_number_of_alt) |
|
1107 |
apply (rule refl) |
|
1108 |
done |
|
1109 |
||
1110 |
lemma uint_1 [simp] : "uint (1 :: 'a :: len word) = 1" |
|
1111 |
unfolding word_1_wi |
|
1112 |
by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps) |
|
1113 |
||
1114 |
lemma unat_1 [simp] : "unat (1 :: 'a :: len word) = 1" |
|
1115 |
by (unfold unat_def uint_1) auto |
|
1116 |
||
1117 |
lemma ucast_1 [simp] : "ucast (1 :: 'a :: len word) = 1" |
|
1118 |
unfolding ucast_def word_1_wi |
|
1119 |
by (simp add: word_ubin.eq_norm int_one_bin bintrunc_minus_simps) |
|
1120 |
||
1121 |
(* abstraction preserves the operations |
|
1122 |
(the definitions tell this for bins in range uint) *) |
|
1123 |
||
1124 |
lemmas arths = |
|
1125 |
bintr_ariths [THEN word_ubin.norm_eq_iff [THEN iffD1], |
|
1126 |
folded word_ubin.eq_norm, standard] |
|
1127 |
||
1128 |
lemma wi_homs: |
|
1129 |
shows |
|
1130 |
wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" and |
|
1131 |
wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" and |
|
1132 |
wi_hom_neg: "- word_of_int a = word_of_int (- a)" and |
|
1133 |
wi_hom_succ: "word_succ (word_of_int a) = word_of_int (Int.succ a)" and |
|
1134 |
wi_hom_pred: "word_pred (word_of_int a) = word_of_int (Int.pred a)" |
|
1135 |
by (auto simp: word_arith_wis arths) |
|
1136 |
||
1137 |
lemmas wi_hom_syms = wi_homs [symmetric] |
|
1138 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1139 |
lemma word_sub_def: "a - b = a + - (b :: 'a :: len0 word)" |
37887 | 1140 |
unfolding word_sub_wi diff_minus |
37660 | 1141 |
by (simp only : word_uint.Rep_inverse wi_hom_syms) |
1142 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1143 |
lemmas word_diff_minus = word_sub_def [standard] |
37660 | 1144 |
|
1145 |
lemma word_of_int_sub_hom: |
|
1146 |
"(word_of_int a) - word_of_int b = word_of_int (a - b)" |
|
37887 | 1147 |
unfolding word_sub_def diff_minus by (simp only : wi_homs) |
37660 | 1148 |
|
1149 |
lemmas new_word_of_int_homs = |
|
1150 |
word_of_int_sub_hom wi_homs word_0_wi word_1_wi |
|
1151 |
||
1152 |
lemmas new_word_of_int_hom_syms = new_word_of_int_homs [symmetric, standard] |
|
1153 |
||
1154 |
lemmas word_of_int_hom_syms = |
|
1155 |
new_word_of_int_hom_syms [unfolded succ_def pred_def] |
|
1156 |
||
1157 |
lemmas word_of_int_homs = |
|
1158 |
new_word_of_int_homs [unfolded succ_def pred_def] |
|
1159 |
||
1160 |
lemmas word_of_int_add_hom = word_of_int_homs (2) |
|
1161 |
lemmas word_of_int_mult_hom = word_of_int_homs (3) |
|
1162 |
lemmas word_of_int_minus_hom = word_of_int_homs (4) |
|
1163 |
lemmas word_of_int_succ_hom = word_of_int_homs (5) |
|
1164 |
lemmas word_of_int_pred_hom = word_of_int_homs (6) |
|
1165 |
lemmas word_of_int_0_hom = word_of_int_homs (7) |
|
1166 |
lemmas word_of_int_1_hom = word_of_int_homs (8) |
|
1167 |
||
1168 |
(* now, to get the weaker results analogous to word_div/mod_def *) |
|
1169 |
||
1170 |
lemmas word_arith_alts = |
|
1171 |
word_sub_wi [unfolded succ_def pred_def, standard] |
|
1172 |
word_arith_wis [unfolded succ_def pred_def, standard] |
|
1173 |
||
1174 |
lemmas word_sub_alt = word_arith_alts (1) |
|
1175 |
lemmas word_add_alt = word_arith_alts (2) |
|
1176 |
lemmas word_mult_alt = word_arith_alts (3) |
|
1177 |
lemmas word_minus_alt = word_arith_alts (4) |
|
1178 |
lemmas word_succ_alt = word_arith_alts (5) |
|
1179 |
lemmas word_pred_alt = word_arith_alts (6) |
|
1180 |
lemmas word_0_alt = word_arith_alts (7) |
|
1181 |
lemmas word_1_alt = word_arith_alts (8) |
|
1182 |
||
1183 |
subsection "Transferring goals from words to ints" |
|
1184 |
||
1185 |
lemma word_ths: |
|
1186 |
shows |
|
1187 |
word_succ_p1: "word_succ a = a + 1" and |
|
1188 |
word_pred_m1: "word_pred a = a - 1" and |
|
1189 |
word_pred_succ: "word_pred (word_succ a) = a" and |
|
1190 |
word_succ_pred: "word_succ (word_pred a) = a" and |
|
1191 |
word_mult_succ: "word_succ a * b = b + a * b" |
|
1192 |
by (rule word_uint.Abs_cases [of b], |
|
1193 |
rule word_uint.Abs_cases [of a], |
|
1194 |
simp add: pred_def succ_def add_commute mult_commute |
|
1195 |
ring_distribs new_word_of_int_homs)+ |
|
1196 |
||
1197 |
lemmas uint_cong = arg_cong [where f = uint] |
|
1198 |
||
1199 |
lemmas uint_word_ariths = |
|
1200 |
word_arith_alts [THEN trans [OF uint_cong int_word_uint], standard] |
|
1201 |
||
1202 |
lemmas uint_word_arith_bintrs = uint_word_ariths [folded bintrunc_mod2p] |
|
1203 |
||
1204 |
(* similar expressions for sint (arith operations) *) |
|
1205 |
lemmas sint_word_ariths = uint_word_arith_bintrs |
|
1206 |
[THEN uint_sint [symmetric, THEN trans], |
|
1207 |
unfolded uint_sint bintr_arith1s bintr_ariths |
|
1208 |
len_gt_0 [THEN bin_sbin_eq_iff'] word_sbin.norm_Rep, standard] |
|
1209 |
||
1210 |
lemmas uint_div_alt = word_div_def |
|
1211 |
[THEN trans [OF uint_cong int_word_uint], standard] |
|
1212 |
lemmas uint_mod_alt = word_mod_def |
|
1213 |
[THEN trans [OF uint_cong int_word_uint], standard] |
|
1214 |
||
1215 |
lemma word_pred_0_n1: "word_pred 0 = word_of_int -1" |
|
1216 |
unfolding word_pred_def number_of_eq |
|
1217 |
by (simp add : pred_def word_no_wi) |
|
1218 |
||
1219 |
lemma word_pred_0_Min: "word_pred 0 = word_of_int Int.Min" |
|
1220 |
by (simp add: word_pred_0_n1 number_of_eq) |
|
1221 |
||
1222 |
lemma word_m1_Min: "- 1 = word_of_int Int.Min" |
|
1223 |
unfolding Min_def by (simp only: word_of_int_hom_syms) |
|
1224 |
||
1225 |
lemma succ_pred_no [simp]: |
|
1226 |
"word_succ (number_of bin) = number_of (Int.succ bin) & |
|
1227 |
word_pred (number_of bin) = number_of (Int.pred bin)" |
|
1228 |
unfolding word_number_of_def by (simp add : new_word_of_int_homs) |
|
1229 |
||
1230 |
lemma word_sp_01 [simp] : |
|
1231 |
"word_succ -1 = 0 & word_succ 0 = 1 & word_pred 0 = -1 & word_pred 1 = 0" |
|
1232 |
by (unfold word_0_no word_1_no) auto |
|
1233 |
||
1234 |
(* alternative approach to lifting arithmetic equalities *) |
|
1235 |
lemma word_of_int_Ex: |
|
1236 |
"\<exists>y. x = word_of_int y" |
|
1237 |
by (rule_tac x="uint x" in exI) simp |
|
1238 |
||
1239 |
lemma word_arith_eqs: |
|
1240 |
fixes a :: "'a::len0 word" |
|
1241 |
fixes b :: "'a::len0 word" |
|
1242 |
shows |
|
1243 |
word_add_0: "0 + a = a" and |
|
1244 |
word_add_0_right: "a + 0 = a" and |
|
1245 |
word_mult_1: "1 * a = a" and |
|
1246 |
word_mult_1_right: "a * 1 = a" and |
|
1247 |
word_add_commute: "a + b = b + a" and |
|
1248 |
word_add_assoc: "a + b + c = a + (b + c)" and |
|
1249 |
word_add_left_commute: "a + (b + c) = b + (a + c)" and |
|
1250 |
word_mult_commute: "a * b = b * a" and |
|
1251 |
word_mult_assoc: "a * b * c = a * (b * c)" and |
|
1252 |
word_mult_left_commute: "a * (b * c) = b * (a * c)" and |
|
1253 |
word_left_distrib: "(a + b) * c = a * c + b * c" and |
|
1254 |
word_right_distrib: "a * (b + c) = a * b + a * c" and |
|
1255 |
word_left_minus: "- a + a = 0" and |
|
1256 |
word_diff_0_right: "a - 0 = a" and |
|
1257 |
word_diff_self: "a - a = 0" |
|
1258 |
using word_of_int_Ex [of a] |
|
1259 |
word_of_int_Ex [of b] |
|
1260 |
word_of_int_Ex [of c] |
|
1261 |
by (auto simp: word_of_int_hom_syms [symmetric] |
|
44821 | 1262 |
add_0_right add_commute add_assoc add_left_commute |
37660 | 1263 |
mult_commute mult_assoc mult_left_commute |
1264 |
left_distrib right_distrib) |
|
1265 |
||
1266 |
lemmas word_add_ac = word_add_commute word_add_assoc word_add_left_commute |
|
1267 |
lemmas word_mult_ac = word_mult_commute word_mult_assoc word_mult_left_commute |
|
1268 |
||
1269 |
lemmas word_plus_ac0 = word_add_0 word_add_0_right word_add_ac |
|
1270 |
lemmas word_times_ac1 = word_mult_1 word_mult_1_right word_mult_ac |
|
1271 |
||
1272 |
||
1273 |
subsection "Order on fixed-length words" |
|
1274 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1275 |
lemma word_order_trans: "x <= y \<Longrightarrow> y <= z \<Longrightarrow> x <= (z :: 'a :: len0 word)" |
37660 | 1276 |
unfolding word_le_def by auto |
1277 |
||
1278 |
lemma word_order_refl: "z <= (z :: 'a :: len0 word)" |
|
1279 |
unfolding word_le_def by auto |
|
1280 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1281 |
lemma word_order_antisym: "x <= y \<Longrightarrow> y <= x \<Longrightarrow> x = (y :: 'a :: len0 word)" |
37660 | 1282 |
unfolding word_le_def by (auto intro!: word_uint.Rep_eqD) |
1283 |
||
1284 |
lemma word_order_linear: |
|
1285 |
"y <= x | x <= (y :: 'a :: len0 word)" |
|
1286 |
unfolding word_le_def by auto |
|
1287 |
||
1288 |
lemma word_zero_le [simp] : |
|
1289 |
"0 <= (y :: 'a :: len0 word)" |
|
1290 |
unfolding word_le_def by auto |
|
1291 |
||
1292 |
instance word :: (len0) semigroup_add |
|
1293 |
by intro_classes (simp add: word_add_assoc) |
|
1294 |
||
1295 |
instance word :: (len0) linorder |
|
1296 |
by intro_classes (auto simp: word_less_def word_le_def) |
|
1297 |
||
1298 |
instance word :: (len0) ring |
|
1299 |
by intro_classes |
|
1300 |
(auto simp: word_arith_eqs word_diff_minus |
|
1301 |
word_diff_self [unfolded word_diff_minus]) |
|
1302 |
||
1303 |
lemma word_m1_ge [simp] : "word_pred 0 >= y" |
|
1304 |
unfolding word_le_def |
|
1305 |
by (simp only : word_pred_0_n1 word_uint.eq_norm m1mod2k) auto |
|
1306 |
||
1307 |
lemmas word_n1_ge [simp] = word_m1_ge [simplified word_sp_01] |
|
1308 |
||
1309 |
lemmas word_not_simps [simp] = |
|
1310 |
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] |
|
1311 |
||
1312 |
lemma word_gt_0: "0 < y = (0 ~= (y :: 'a :: len0 word))" |
|
1313 |
unfolding word_less_def by auto |
|
1314 |
||
1315 |
lemmas word_gt_0_no [simp] = word_gt_0 [of "number_of y", standard] |
|
1316 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1317 |
lemma word_sless_alt: "(a <s b) = (sint a < sint b)" |
37660 | 1318 |
unfolding word_sle_def word_sless_def |
1319 |
by (auto simp add: less_le) |
|
1320 |
||
1321 |
lemma word_le_nat_alt: "(a <= b) = (unat a <= unat b)" |
|
1322 |
unfolding unat_def word_le_def |
|
1323 |
by (rule nat_le_eq_zle [symmetric]) simp |
|
1324 |
||
1325 |
lemma word_less_nat_alt: "(a < b) = (unat a < unat b)" |
|
1326 |
unfolding unat_def word_less_alt |
|
1327 |
by (rule nat_less_eq_zless [symmetric]) simp |
|
1328 |
||
1329 |
lemma wi_less: |
|
1330 |
"(word_of_int n < (word_of_int m :: 'a :: len0 word)) = |
|
1331 |
(n mod 2 ^ len_of TYPE('a) < m mod 2 ^ len_of TYPE('a))" |
|
1332 |
unfolding word_less_alt by (simp add: word_uint.eq_norm) |
|
1333 |
||
1334 |
lemma wi_le: |
|
1335 |
"(word_of_int n <= (word_of_int m :: 'a :: len0 word)) = |
|
1336 |
(n mod 2 ^ len_of TYPE('a) <= m mod 2 ^ len_of TYPE('a))" |
|
1337 |
unfolding word_le_def by (simp add: word_uint.eq_norm) |
|
1338 |
||
1339 |
lemma udvd_nat_alt: "a udvd b = (EX n>=0. unat b = n * unat a)" |
|
1340 |
apply (unfold udvd_def) |
|
1341 |
apply safe |
|
1342 |
apply (simp add: unat_def nat_mult_distrib) |
|
1343 |
apply (simp add: uint_nat int_mult) |
|
1344 |
apply (rule exI) |
|
1345 |
apply safe |
|
1346 |
prefer 2 |
|
1347 |
apply (erule notE) |
|
1348 |
apply (rule refl) |
|
1349 |
apply force |
|
1350 |
done |
|
1351 |
||
1352 |
lemma udvd_iff_dvd: "x udvd y <-> unat x dvd unat y" |
|
1353 |
unfolding dvd_def udvd_nat_alt by force |
|
1354 |
||
1355 |
lemmas unat_mono = word_less_nat_alt [THEN iffD1, standard] |
|
1356 |
||
45284
ae78a4ffa81d
use simproc_setup for cancellation simprocs, to get proper name bindings
huffman
parents:
44938
diff
changeset
|
1357 |
lemma word_zero_neq_one: "0 < len_of TYPE ('a :: len0) \<Longrightarrow> (0 :: 'a word) ~= 1" |
37660 | 1358 |
unfolding word_arith_wis |
1359 |
by (auto simp add: word_ubin.norm_eq_iff [symmetric] gr0_conv_Suc) |
|
1360 |
||
1361 |
lemmas lenw1_zero_neq_one = len_gt_0 [THEN word_zero_neq_one] |
|
1362 |
||
1363 |
lemma no_no [simp] : "number_of (number_of b) = number_of b" |
|
1364 |
by (simp add: number_of_eq) |
|
1365 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1366 |
lemma unat_minus_one: "x ~= 0 \<Longrightarrow> unat (x - 1) = unat x - 1" |
37660 | 1367 |
apply (unfold unat_def) |
1368 |
apply (simp only: int_word_uint word_arith_alts rdmods) |
|
1369 |
apply (subgoal_tac "uint x >= 1") |
|
1370 |
prefer 2 |
|
1371 |
apply (drule contrapos_nn) |
|
1372 |
apply (erule word_uint.Rep_inverse' [symmetric]) |
|
1373 |
apply (insert uint_ge_0 [of x])[1] |
|
1374 |
apply arith |
|
1375 |
apply (rule box_equals) |
|
1376 |
apply (rule nat_diff_distrib) |
|
1377 |
prefer 2 |
|
1378 |
apply assumption |
|
1379 |
apply simp |
|
1380 |
apply (subst mod_pos_pos_trivial) |
|
1381 |
apply arith |
|
1382 |
apply (insert uint_lt2p [of x])[1] |
|
1383 |
apply arith |
|
1384 |
apply (rule refl) |
|
1385 |
apply simp |
|
1386 |
done |
|
1387 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1388 |
lemma measure_unat: "p ~= 0 \<Longrightarrow> unat (p - 1) < unat p" |
37660 | 1389 |
by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric]) |
1390 |
||
1391 |
lemmas uint_add_ge0 [simp] = |
|
1392 |
add_nonneg_nonneg [OF uint_ge_0 uint_ge_0, standard] |
|
1393 |
lemmas uint_mult_ge0 [simp] = |
|
1394 |
mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0, standard] |
|
1395 |
||
1396 |
lemma uint_sub_lt2p [simp]: |
|
1397 |
"uint (x :: 'a :: len0 word) - uint (y :: 'b :: len0 word) < |
|
1398 |
2 ^ len_of TYPE('a)" |
|
1399 |
using uint_ge_0 [of y] uint_lt2p [of x] by arith |
|
1400 |
||
1401 |
||
1402 |
subsection "Conditions for the addition (etc) of two words to overflow" |
|
1403 |
||
1404 |
lemma uint_add_lem: |
|
1405 |
"(uint x + uint y < 2 ^ len_of TYPE('a)) = |
|
1406 |
(uint (x + y :: 'a :: len0 word) = uint x + uint y)" |
|
1407 |
by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem]) |
|
1408 |
||
1409 |
lemma uint_mult_lem: |
|
1410 |
"(uint x * uint y < 2 ^ len_of TYPE('a)) = |
|
1411 |
(uint (x * y :: 'a :: len0 word) = uint x * uint y)" |
|
1412 |
by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem]) |
|
1413 |
||
1414 |
lemma uint_sub_lem: |
|
1415 |
"(uint x >= uint y) = (uint (x - y) = uint x - uint y)" |
|
1416 |
by (unfold uint_word_ariths) (auto intro!: trans [OF _ int_mod_lem]) |
|
1417 |
||
1418 |
lemma uint_add_le: "uint (x + y) <= uint x + uint y" |
|
1419 |
unfolding uint_word_ariths by (auto simp: mod_add_if_z) |
|
1420 |
||
1421 |
lemma uint_sub_ge: "uint (x - y) >= uint x - uint y" |
|
1422 |
unfolding uint_word_ariths by (auto simp: mod_sub_if_z) |
|
1423 |
||
1424 |
lemmas uint_sub_if' = |
|
1425 |
trans [OF uint_word_ariths(1) mod_sub_if_z, simplified, standard] |
|
1426 |
lemmas uint_plus_if' = |
|
1427 |
trans [OF uint_word_ariths(2) mod_add_if_z, simplified, standard] |
|
1428 |
||
1429 |
||
1430 |
subsection {* Definition of uint\_arith *} |
|
1431 |
||
1432 |
lemma word_of_int_inverse: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1433 |
"word_of_int r = a \<Longrightarrow> 0 <= r \<Longrightarrow> r < 2 ^ len_of TYPE('a) \<Longrightarrow> |
37660 | 1434 |
uint (a::'a::len0 word) = r" |
1435 |
apply (erule word_uint.Abs_inverse' [rotated]) |
|
1436 |
apply (simp add: uints_num) |
|
1437 |
done |
|
1438 |
||
1439 |
lemma uint_split: |
|
1440 |
fixes x::"'a::len0 word" |
|
1441 |
shows "P (uint x) = |
|
1442 |
(ALL i. word_of_int i = x & 0 <= i & i < 2^len_of TYPE('a) --> P i)" |
|
1443 |
apply (fold word_int_case_def) |
|
1444 |
apply (auto dest!: word_of_int_inverse simp: int_word_uint int_mod_eq' |
|
1445 |
split: word_int_split) |
|
1446 |
done |
|
1447 |
||
1448 |
lemma uint_split_asm: |
|
1449 |
fixes x::"'a::len0 word" |
|
1450 |
shows "P (uint x) = |
|
1451 |
(~(EX i. word_of_int i = x & 0 <= i & i < 2^len_of TYPE('a) & ~ P i))" |
|
1452 |
by (auto dest!: word_of_int_inverse |
|
1453 |
simp: int_word_uint int_mod_eq' |
|
1454 |
split: uint_split) |
|
1455 |
||
1456 |
lemmas uint_splits = uint_split uint_split_asm |
|
1457 |
||
1458 |
lemmas uint_arith_simps = |
|
1459 |
word_le_def word_less_alt |
|
1460 |
word_uint.Rep_inject [symmetric] |
|
1461 |
uint_sub_if' uint_plus_if' |
|
1462 |
||
1463 |
(* use this to stop, eg, 2 ^ len_of TYPE (32) being simplified *) |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1464 |
lemma power_False_cong: "False \<Longrightarrow> a ^ b = c ^ d" |
37660 | 1465 |
by auto |
1466 |
||
1467 |
(* uint_arith_tac: reduce to arithmetic on int, try to solve by arith *) |
|
1468 |
ML {* |
|
1469 |
fun uint_arith_ss_of ss = |
|
1470 |
ss addsimps @{thms uint_arith_simps} |
|
1471 |
delsimps @{thms word_uint.Rep_inject} |
|
1472 |
addsplits @{thms split_if_asm} |
|
1473 |
addcongs @{thms power_False_cong} |
|
1474 |
||
1475 |
fun uint_arith_tacs ctxt = |
|
1476 |
let |
|
1477 |
fun arith_tac' n t = |
|
1478 |
Arith_Data.verbose_arith_tac ctxt n t |
|
1479 |
handle Cooper.COOPER _ => Seq.empty; |
|
1480 |
in |
|
42793 | 1481 |
[ clarify_tac ctxt 1, |
1482 |
full_simp_tac (uint_arith_ss_of (simpset_of ctxt)) 1, |
|
37660 | 1483 |
ALLGOALS (full_simp_tac (HOL_ss addsplits @{thms uint_splits} |
1484 |
addcongs @{thms power_False_cong})), |
|
1485 |
rewrite_goals_tac @{thms word_size}, |
|
1486 |
ALLGOALS (fn n => REPEAT (resolve_tac [allI, impI] n) THEN |
|
1487 |
REPEAT (etac conjE n) THEN |
|
1488 |
REPEAT (dtac @{thm word_of_int_inverse} n |
|
1489 |
THEN atac n |
|
1490 |
THEN atac n)), |
|
1491 |
TRYALL arith_tac' ] |
|
1492 |
end |
|
1493 |
||
1494 |
fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt)) |
|
1495 |
*} |
|
1496 |
||
1497 |
method_setup uint_arith = |
|
1498 |
{* Scan.succeed (SIMPLE_METHOD' o uint_arith_tac) *} |
|
1499 |
"solving word arithmetic via integers and arith" |
|
1500 |
||
1501 |
||
1502 |
subsection "More on overflows and monotonicity" |
|
1503 |
||
1504 |
lemma no_plus_overflow_uint_size: |
|
1505 |
"((x :: 'a :: len0 word) <= x + y) = (uint x + uint y < 2 ^ size x)" |
|
1506 |
unfolding word_size by uint_arith |
|
1507 |
||
1508 |
lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size] |
|
1509 |
||
1510 |
lemma no_ulen_sub: "((x :: 'a :: len0 word) >= x - y) = (uint y <= uint x)" |
|
1511 |
by uint_arith |
|
1512 |
||
1513 |
lemma no_olen_add': |
|
1514 |
fixes x :: "'a::len0 word" |
|
1515 |
shows "(x \<le> y + x) = (uint y + uint x < 2 ^ len_of TYPE('a))" |
|
1516 |
by (simp add: word_add_ac add_ac no_olen_add) |
|
1517 |
||
1518 |
lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric], standard] |
|
1519 |
||
1520 |
lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem, standard] |
|
1521 |
lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1, standard] |
|
1522 |
lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem, standard] |
|
1523 |
lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def] |
|
1524 |
lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def] |
|
1525 |
lemmas word_sub_le = word_sub_le_iff [THEN iffD2, standard] |
|
1526 |
||
1527 |
lemma word_less_sub1: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1528 |
"(x :: 'a :: len word) ~= 0 \<Longrightarrow> (1 < x) = (0 < x - 1)" |
37660 | 1529 |
by uint_arith |
1530 |
||
1531 |
lemma word_le_sub1: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1532 |
"(x :: 'a :: len word) ~= 0 \<Longrightarrow> (1 <= x) = (0 <= x - 1)" |
37660 | 1533 |
by uint_arith |
1534 |
||
1535 |
lemma sub_wrap_lt: |
|
1536 |
"((x :: 'a :: len0 word) < x - z) = (x < z)" |
|
1537 |
by uint_arith |
|
1538 |
||
1539 |
lemma sub_wrap: |
|
1540 |
"((x :: 'a :: len0 word) <= x - z) = (z = 0 | x < z)" |
|
1541 |
by uint_arith |
|
1542 |
||
1543 |
lemma plus_minus_not_NULL_ab: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1544 |
"(x :: 'a :: len0 word) <= ab - c \<Longrightarrow> c <= ab \<Longrightarrow> c ~= 0 \<Longrightarrow> x + c ~= 0" |
37660 | 1545 |
by uint_arith |
1546 |
||
1547 |
lemma plus_minus_no_overflow_ab: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1548 |
"(x :: 'a :: len0 word) <= ab - c \<Longrightarrow> c <= ab \<Longrightarrow> x <= x + c" |
37660 | 1549 |
by uint_arith |
1550 |
||
1551 |
lemma le_minus': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1552 |
"(a :: 'a :: len0 word) + c <= b \<Longrightarrow> a <= a + c \<Longrightarrow> c <= b - a" |
37660 | 1553 |
by uint_arith |
1554 |
||
1555 |
lemma le_plus': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1556 |
"(a :: 'a :: len0 word) <= b \<Longrightarrow> c <= b - a \<Longrightarrow> a + c <= b" |
37660 | 1557 |
by uint_arith |
1558 |
||
1559 |
lemmas le_plus = le_plus' [rotated] |
|
1560 |
||
1561 |
lemmas le_minus = leD [THEN thin_rl, THEN le_minus', standard] |
|
1562 |
||
1563 |
lemma word_plus_mono_right: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1564 |
"(y :: 'a :: len0 word) <= z \<Longrightarrow> x <= x + z \<Longrightarrow> x + y <= x + z" |
37660 | 1565 |
by uint_arith |
1566 |
||
1567 |
lemma word_less_minus_cancel: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1568 |
"y - x < z - x \<Longrightarrow> x <= z \<Longrightarrow> (y :: 'a :: len0 word) < z" |
37660 | 1569 |
by uint_arith |
1570 |
||
1571 |
lemma word_less_minus_mono_left: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1572 |
"(y :: 'a :: len0 word) < z \<Longrightarrow> x <= y \<Longrightarrow> y - x < z - x" |
37660 | 1573 |
by uint_arith |
1574 |
||
1575 |
lemma word_less_minus_mono: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1576 |
"a < c \<Longrightarrow> d < b \<Longrightarrow> a - b < a \<Longrightarrow> c - d < c |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1577 |
\<Longrightarrow> a - b < c - (d::'a::len word)" |
37660 | 1578 |
by uint_arith |
1579 |
||
1580 |
lemma word_le_minus_cancel: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1581 |
"y - x <= z - x \<Longrightarrow> x <= z \<Longrightarrow> (y :: 'a :: len0 word) <= z" |
37660 | 1582 |
by uint_arith |
1583 |
||
1584 |
lemma word_le_minus_mono_left: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1585 |
"(y :: 'a :: len0 word) <= z \<Longrightarrow> x <= y \<Longrightarrow> y - x <= z - x" |
37660 | 1586 |
by uint_arith |
1587 |
||
1588 |
lemma word_le_minus_mono: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1589 |
"a <= c \<Longrightarrow> d <= b \<Longrightarrow> a - b <= a \<Longrightarrow> c - d <= c |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1590 |
\<Longrightarrow> a - b <= c - (d::'a::len word)" |
37660 | 1591 |
by uint_arith |
1592 |
||
1593 |
lemma plus_le_left_cancel_wrap: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1594 |
"(x :: 'a :: len0 word) + y' < x \<Longrightarrow> x + y < x \<Longrightarrow> (x + y' < x + y) = (y' < y)" |
37660 | 1595 |
by uint_arith |
1596 |
||
1597 |
lemma plus_le_left_cancel_nowrap: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1598 |
"(x :: 'a :: len0 word) <= x + y' \<Longrightarrow> x <= x + y \<Longrightarrow> |
37660 | 1599 |
(x + y' < x + y) = (y' < y)" |
1600 |
by uint_arith |
|
1601 |
||
1602 |
lemma word_plus_mono_right2: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1603 |
"(a :: 'a :: len0 word) <= a + b \<Longrightarrow> c <= b \<Longrightarrow> a <= a + c" |
37660 | 1604 |
by uint_arith |
1605 |
||
1606 |
lemma word_less_add_right: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1607 |
"(x :: 'a :: len0 word) < y - z \<Longrightarrow> z <= y \<Longrightarrow> x + z < y" |
37660 | 1608 |
by uint_arith |
1609 |
||
1610 |
lemma word_less_sub_right: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1611 |
"(x :: 'a :: len0 word) < y + z \<Longrightarrow> y <= x \<Longrightarrow> x - y < z" |
37660 | 1612 |
by uint_arith |
1613 |
||
1614 |
lemma word_le_plus_either: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1615 |
"(x :: 'a :: len0 word) <= y | x <= z \<Longrightarrow> y <= y + z \<Longrightarrow> x <= y + z" |
37660 | 1616 |
by uint_arith |
1617 |
||
1618 |
lemma word_less_nowrapI: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1619 |
"(x :: 'a :: len0 word) < z - k \<Longrightarrow> k <= z \<Longrightarrow> 0 < k \<Longrightarrow> x < x + k" |
37660 | 1620 |
by uint_arith |
1621 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1622 |
lemma inc_le: "(i :: 'a :: len word) < m \<Longrightarrow> i + 1 <= m" |
37660 | 1623 |
by uint_arith |
1624 |
||
1625 |
lemma inc_i: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1626 |
"(1 :: 'a :: len word) <= i \<Longrightarrow> i < m \<Longrightarrow> 1 <= (i + 1) & i + 1 <= m" |
37660 | 1627 |
by uint_arith |
1628 |
||
1629 |
lemma udvd_incr_lem: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1630 |
"up < uq \<Longrightarrow> up = ua + n * uint K \<Longrightarrow> |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1631 |
uq = ua + n' * uint K \<Longrightarrow> up + uint K <= uq" |
37660 | 1632 |
apply clarsimp |
1633 |
apply (drule less_le_mult) |
|
1634 |
apply safe |
|
1635 |
done |
|
1636 |
||
1637 |
lemma udvd_incr': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1638 |
"p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow> |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1639 |
uint q = ua + n' * uint K \<Longrightarrow> p + K <= q" |
37660 | 1640 |
apply (unfold word_less_alt word_le_def) |
1641 |
apply (drule (2) udvd_incr_lem) |
|
1642 |
apply (erule uint_add_le [THEN order_trans]) |
|
1643 |
done |
|
1644 |
||
1645 |
lemma udvd_decr': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1646 |
"p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow> |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1647 |
uint q = ua + n' * uint K \<Longrightarrow> p <= q - K" |
37660 | 1648 |
apply (unfold word_less_alt word_le_def) |
1649 |
apply (drule (2) udvd_incr_lem) |
|
1650 |
apply (drule le_diff_eq [THEN iffD2]) |
|
1651 |
apply (erule order_trans) |
|
1652 |
apply (rule uint_sub_ge) |
|
1653 |
done |
|
1654 |
||
1655 |
lemmas udvd_incr_lem0 = udvd_incr_lem [where ua=0, simplified] |
|
1656 |
lemmas udvd_incr0 = udvd_incr' [where ua=0, simplified] |
|
1657 |
lemmas udvd_decr0 = udvd_decr' [where ua=0, simplified] |
|
1658 |
||
1659 |
lemma udvd_minus_le': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1660 |
"xy < k \<Longrightarrow> z udvd xy \<Longrightarrow> z udvd k \<Longrightarrow> xy <= k - z" |
37660 | 1661 |
apply (unfold udvd_def) |
1662 |
apply clarify |
|
1663 |
apply (erule (2) udvd_decr0) |
|
1664 |
done |
|
1665 |
||
45284
ae78a4ffa81d
use simproc_setup for cancellation simprocs, to get proper name bindings
huffman
parents:
44938
diff
changeset
|
1666 |
ML {* Delsimprocs [@{simproc linordered_ring_less_cancel_factor}] *} |
37660 | 1667 |
|
1668 |
lemma udvd_incr2_K: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1669 |
"p < a + s \<Longrightarrow> a <= a + s \<Longrightarrow> K udvd s \<Longrightarrow> K udvd p - a \<Longrightarrow> a <= p \<Longrightarrow> |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1670 |
0 < K \<Longrightarrow> p <= p + K & p + K <= a + s" |
37660 | 1671 |
apply (unfold udvd_def) |
1672 |
apply clarify |
|
1673 |
apply (simp add: uint_arith_simps split: split_if_asm) |
|
1674 |
prefer 2 |
|
1675 |
apply (insert uint_range' [of s])[1] |
|
1676 |
apply arith |
|
1677 |
apply (drule add_commute [THEN xtr1]) |
|
1678 |
apply (simp add: diff_less_eq [symmetric]) |
|
1679 |
apply (drule less_le_mult) |
|
1680 |
apply arith |
|
1681 |
apply simp |
|
1682 |
done |
|
1683 |
||
45284
ae78a4ffa81d
use simproc_setup for cancellation simprocs, to get proper name bindings
huffman
parents:
44938
diff
changeset
|
1684 |
ML {* Addsimprocs [@{simproc linordered_ring_less_cancel_factor}] *} |
37660 | 1685 |
|
1686 |
(* links with rbl operations *) |
|
1687 |
lemma word_succ_rbl: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1688 |
"to_bl w = bl \<Longrightarrow> to_bl (word_succ w) = (rev (rbl_succ (rev bl)))" |
37660 | 1689 |
apply (unfold word_succ_def) |
1690 |
apply clarify |
|
1691 |
apply (simp add: to_bl_of_bin) |
|
1692 |
apply (simp add: to_bl_def rbl_succ) |
|
1693 |
done |
|
1694 |
||
1695 |
lemma word_pred_rbl: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1696 |
"to_bl w = bl \<Longrightarrow> to_bl (word_pred w) = (rev (rbl_pred (rev bl)))" |
37660 | 1697 |
apply (unfold word_pred_def) |
1698 |
apply clarify |
|
1699 |
apply (simp add: to_bl_of_bin) |
|
1700 |
apply (simp add: to_bl_def rbl_pred) |
|
1701 |
done |
|
1702 |
||
1703 |
lemma word_add_rbl: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1704 |
"to_bl v = vbl \<Longrightarrow> to_bl w = wbl \<Longrightarrow> |
37660 | 1705 |
to_bl (v + w) = (rev (rbl_add (rev vbl) (rev wbl)))" |
1706 |
apply (unfold word_add_def) |
|
1707 |
apply clarify |
|
1708 |
apply (simp add: to_bl_of_bin) |
|
1709 |
apply (simp add: to_bl_def rbl_add) |
|
1710 |
done |
|
1711 |
||
1712 |
lemma word_mult_rbl: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1713 |
"to_bl v = vbl \<Longrightarrow> to_bl w = wbl \<Longrightarrow> |
37660 | 1714 |
to_bl (v * w) = (rev (rbl_mult (rev vbl) (rev wbl)))" |
1715 |
apply (unfold word_mult_def) |
|
1716 |
apply clarify |
|
1717 |
apply (simp add: to_bl_of_bin) |
|
1718 |
apply (simp add: to_bl_def rbl_mult) |
|
1719 |
done |
|
1720 |
||
1721 |
lemma rtb_rbl_ariths: |
|
1722 |
"rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_succ w)) = rbl_succ ys" |
|
1723 |
"rev (to_bl w) = ys \<Longrightarrow> rev (to_bl (word_pred w)) = rbl_pred ys" |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1724 |
"rev (to_bl v) = ys \<Longrightarrow> rev (to_bl w) = xs \<Longrightarrow> rev (to_bl (v * w)) = rbl_mult ys xs" |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1725 |
"rev (to_bl v) = ys \<Longrightarrow> rev (to_bl w) = xs \<Longrightarrow> rev (to_bl (v + w)) = rbl_add ys xs" |
37660 | 1726 |
by (auto simp: rev_swap [symmetric] word_succ_rbl |
1727 |
word_pred_rbl word_mult_rbl word_add_rbl) |
|
1728 |
||
1729 |
||
1730 |
subsection "Arithmetic type class instantiations" |
|
1731 |
||
1732 |
instance word :: (len0) comm_monoid_add .. |
|
1733 |
||
1734 |
instance word :: (len0) comm_monoid_mult |
|
1735 |
apply (intro_classes) |
|
1736 |
apply (simp add: word_mult_commute) |
|
1737 |
apply (simp add: word_mult_1) |
|
1738 |
done |
|
1739 |
||
1740 |
instance word :: (len0) comm_semiring |
|
1741 |
by (intro_classes) (simp add : word_left_distrib) |
|
1742 |
||
1743 |
instance word :: (len0) ab_group_add .. |
|
1744 |
||
1745 |
instance word :: (len0) comm_ring .. |
|
1746 |
||
1747 |
instance word :: (len) comm_semiring_1 |
|
1748 |
by (intro_classes) (simp add: lenw1_zero_neq_one) |
|
1749 |
||
1750 |
instance word :: (len) comm_ring_1 .. |
|
1751 |
||
1752 |
instance word :: (len0) comm_semiring_0 .. |
|
1753 |
||
1754 |
instance word :: (len0) order .. |
|
1755 |
||
1756 |
(* note that iszero_def is only for class comm_semiring_1_cancel, |
|
1757 |
which requires word length >= 1, ie 'a :: len word *) |
|
1758 |
lemma zero_bintrunc: |
|
1759 |
"iszero (number_of x :: 'a :: len word) = |
|
1760 |
(bintrunc (len_of TYPE('a)) x = Int.Pls)" |
|
1761 |
apply (unfold iszero_def word_0_wi word_no_wi) |
|
1762 |
apply (rule word_ubin.norm_eq_iff [symmetric, THEN trans]) |
|
1763 |
apply (simp add : Pls_def [symmetric]) |
|
1764 |
done |
|
1765 |
||
1766 |
lemmas word_le_0_iff [simp] = |
|
1767 |
word_zero_le [THEN leD, THEN linorder_antisym_conv1] |
|
1768 |
||
1769 |
lemma word_of_nat: "of_nat n = word_of_int (int n)" |
|
1770 |
by (induct n) (auto simp add : word_of_int_hom_syms) |
|
1771 |
||
1772 |
lemma word_of_int: "of_int = word_of_int" |
|
1773 |
apply (rule ext) |
|
45528
726b743855ea
simplify proof of word_of_int; remove several now-unused lemmas about Rep_Integ
huffman
parents:
45284
diff
changeset
|
1774 |
apply (case_tac x rule: int_diff_cases) |
726b743855ea
simplify proof of word_of_int; remove several now-unused lemmas about Rep_Integ
huffman
parents:
45284
diff
changeset
|
1775 |
apply (simp add: word_of_nat word_of_int_sub_hom) |
37660 | 1776 |
done |
1777 |
||
1778 |
lemma word_of_int_nat: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1779 |
"0 <= x \<Longrightarrow> word_of_int x = of_nat (nat x)" |
37660 | 1780 |
by (simp add: of_nat_nat word_of_int) |
1781 |
||
1782 |
lemma word_number_of_eq: |
|
1783 |
"number_of w = (of_int w :: 'a :: len word)" |
|
1784 |
unfolding word_number_of_def word_of_int by auto |
|
1785 |
||
1786 |
instance word :: (len) number_ring |
|
1787 |
by (intro_classes) (simp add : word_number_of_eq) |
|
1788 |
||
1789 |
lemma iszero_word_no [simp] : |
|
1790 |
"iszero (number_of bin :: 'a :: len word) = |
|
1791 |
iszero (number_of (bintrunc (len_of TYPE('a)) bin) :: int)" |
|
1792 |
apply (simp add: zero_bintrunc number_of_is_id) |
|
1793 |
apply (unfold iszero_def Pls_def) |
|
1794 |
apply (rule refl) |
|
1795 |
done |
|
1796 |
||
1797 |
||
1798 |
subsection "Word and nat" |
|
1799 |
||
1800 |
lemma td_ext_unat': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1801 |
"n = len_of TYPE ('a :: len) \<Longrightarrow> |
37660 | 1802 |
td_ext (unat :: 'a word => nat) of_nat |
1803 |
(unats n) (%i. i mod 2 ^ n)" |
|
1804 |
apply (unfold td_ext_def' unat_def word_of_nat unats_uints) |
|
1805 |
apply (auto intro!: imageI simp add : word_of_int_hom_syms) |
|
1806 |
apply (erule word_uint.Abs_inverse [THEN arg_cong]) |
|
1807 |
apply (simp add: int_word_uint nat_mod_distrib nat_power_eq) |
|
1808 |
done |
|
1809 |
||
1810 |
lemmas td_ext_unat = refl [THEN td_ext_unat'] |
|
1811 |
lemmas unat_of_nat = td_ext_unat [THEN td_ext.eq_norm, standard] |
|
1812 |
||
1813 |
interpretation word_unat: |
|
1814 |
td_ext "unat::'a::len word => nat" |
|
1815 |
of_nat |
|
1816 |
"unats (len_of TYPE('a::len))" |
|
1817 |
"%i. i mod 2 ^ len_of TYPE('a::len)" |
|
1818 |
by (rule td_ext_unat) |
|
1819 |
||
1820 |
lemmas td_unat = word_unat.td_thm |
|
1821 |
||
1822 |
lemmas unat_lt2p [iff] = word_unat.Rep [unfolded unats_def mem_Collect_eq] |
|
1823 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1824 |
lemma unat_le: "y <= unat (z :: 'a :: len word) \<Longrightarrow> y : unats (len_of TYPE ('a))" |
37660 | 1825 |
apply (unfold unats_def) |
1826 |
apply clarsimp |
|
1827 |
apply (rule xtrans, rule unat_lt2p, assumption) |
|
1828 |
done |
|
1829 |
||
1830 |
lemma word_nchotomy: |
|
1831 |
"ALL w. EX n. (w :: 'a :: len word) = of_nat n & n < 2 ^ len_of TYPE ('a)" |
|
1832 |
apply (rule allI) |
|
1833 |
apply (rule word_unat.Abs_cases) |
|
1834 |
apply (unfold unats_def) |
|
1835 |
apply auto |
|
1836 |
done |
|
1837 |
||
1838 |
lemma of_nat_eq: |
|
1839 |
fixes w :: "'a::len word" |
|
1840 |
shows "(of_nat n = w) = (\<exists>q. n = unat w + q * 2 ^ len_of TYPE('a))" |
|
1841 |
apply (rule trans) |
|
1842 |
apply (rule word_unat.inverse_norm) |
|
1843 |
apply (rule iffI) |
|
1844 |
apply (rule mod_eqD) |
|
1845 |
apply simp |
|
1846 |
apply clarsimp |
|
1847 |
done |
|
1848 |
||
1849 |
lemma of_nat_eq_size: |
|
1850 |
"(of_nat n = w) = (EX q. n = unat w + q * 2 ^ size w)" |
|
1851 |
unfolding word_size by (rule of_nat_eq) |
|
1852 |
||
1853 |
lemma of_nat_0: |
|
1854 |
"(of_nat m = (0::'a::len word)) = (\<exists>q. m = q * 2 ^ len_of TYPE('a))" |
|
1855 |
by (simp add: of_nat_eq) |
|
1856 |
||
1857 |
lemmas of_nat_2p = mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]] |
|
1858 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1859 |
lemma of_nat_gt_0: "of_nat k ~= 0 \<Longrightarrow> 0 < k" |
37660 | 1860 |
by (cases k) auto |
1861 |
||
1862 |
lemma of_nat_neq_0: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1863 |
"0 < k \<Longrightarrow> k < 2 ^ len_of TYPE ('a :: len) \<Longrightarrow> of_nat k ~= (0 :: 'a word)" |
37660 | 1864 |
by (clarsimp simp add : of_nat_0) |
1865 |
||
1866 |
lemma Abs_fnat_hom_add: |
|
1867 |
"of_nat a + of_nat b = of_nat (a + b)" |
|
1868 |
by simp |
|
1869 |
||
1870 |
lemma Abs_fnat_hom_mult: |
|
1871 |
"of_nat a * of_nat b = (of_nat (a * b) :: 'a :: len word)" |
|
1872 |
by (simp add: word_of_nat word_of_int_mult_hom zmult_int) |
|
1873 |
||
1874 |
lemma Abs_fnat_hom_Suc: |
|
1875 |
"word_succ (of_nat a) = of_nat (Suc a)" |
|
1876 |
by (simp add: word_of_nat word_of_int_succ_hom add_ac) |
|
1877 |
||
1878 |
lemma Abs_fnat_hom_0: "(0::'a::len word) = of_nat 0" |
|
1879 |
by (simp add: word_of_nat word_0_wi) |
|
1880 |
||
1881 |
lemma Abs_fnat_hom_1: "(1::'a::len word) = of_nat (Suc 0)" |
|
1882 |
by (simp add: word_of_nat word_1_wi) |
|
1883 |
||
1884 |
lemmas Abs_fnat_homs = |
|
1885 |
Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc |
|
1886 |
Abs_fnat_hom_0 Abs_fnat_hom_1 |
|
1887 |
||
1888 |
lemma word_arith_nat_add: |
|
1889 |
"a + b = of_nat (unat a + unat b)" |
|
1890 |
by simp |
|
1891 |
||
1892 |
lemma word_arith_nat_mult: |
|
1893 |
"a * b = of_nat (unat a * unat b)" |
|
1894 |
by (simp add: Abs_fnat_hom_mult [symmetric]) |
|
1895 |
||
1896 |
lemma word_arith_nat_Suc: |
|
1897 |
"word_succ a = of_nat (Suc (unat a))" |
|
1898 |
by (subst Abs_fnat_hom_Suc [symmetric]) simp |
|
1899 |
||
1900 |
lemma word_arith_nat_div: |
|
1901 |
"a div b = of_nat (unat a div unat b)" |
|
1902 |
by (simp add: word_div_def word_of_nat zdiv_int uint_nat) |
|
1903 |
||
1904 |
lemma word_arith_nat_mod: |
|
1905 |
"a mod b = of_nat (unat a mod unat b)" |
|
1906 |
by (simp add: word_mod_def word_of_nat zmod_int uint_nat) |
|
1907 |
||
1908 |
lemmas word_arith_nat_defs = |
|
1909 |
word_arith_nat_add word_arith_nat_mult |
|
1910 |
word_arith_nat_Suc Abs_fnat_hom_0 |
|
1911 |
Abs_fnat_hom_1 word_arith_nat_div |
|
1912 |
word_arith_nat_mod |
|
1913 |
||
1914 |
lemmas unat_cong = arg_cong [where f = "unat"] |
|
1915 |
||
1916 |
lemmas unat_word_ariths = word_arith_nat_defs |
|
1917 |
[THEN trans [OF unat_cong unat_of_nat], standard] |
|
1918 |
||
1919 |
lemmas word_sub_less_iff = word_sub_le_iff |
|
1920 |
[simplified linorder_not_less [symmetric], simplified] |
|
1921 |
||
1922 |
lemma unat_add_lem: |
|
1923 |
"(unat x + unat y < 2 ^ len_of TYPE('a)) = |
|
1924 |
(unat (x + y :: 'a :: len word) = unat x + unat y)" |
|
1925 |
unfolding unat_word_ariths |
|
1926 |
by (auto intro!: trans [OF _ nat_mod_lem]) |
|
1927 |
||
1928 |
lemma unat_mult_lem: |
|
1929 |
"(unat x * unat y < 2 ^ len_of TYPE('a)) = |
|
1930 |
(unat (x * y :: 'a :: len word) = unat x * unat y)" |
|
1931 |
unfolding unat_word_ariths |
|
1932 |
by (auto intro!: trans [OF _ nat_mod_lem]) |
|
1933 |
||
1934 |
lemmas unat_plus_if' = |
|
1935 |
trans [OF unat_word_ariths(1) mod_nat_add, simplified, standard] |
|
1936 |
||
1937 |
lemma le_no_overflow: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
1938 |
"x <= b \<Longrightarrow> a <= a + b \<Longrightarrow> x <= a + (b :: 'a :: len0 word)" |
37660 | 1939 |
apply (erule order_trans) |
1940 |
apply (erule olen_add_eqv [THEN iffD1]) |
|
1941 |
done |
|
1942 |
||
1943 |
lemmas un_ui_le = trans |
|
1944 |
[OF word_le_nat_alt [symmetric] |
|
1945 |
word_le_def, |
|
1946 |
standard] |
|
1947 |
||
1948 |
lemma unat_sub_if_size: |
|
1949 |
"unat (x - y) = (if unat y <= unat x |
|
1950 |
then unat x - unat y |
|
1951 |
else unat x + 2 ^ size x - unat y)" |
|
1952 |
apply (unfold word_size) |
|
1953 |
apply (simp add: un_ui_le) |
|
1954 |
apply (auto simp add: unat_def uint_sub_if') |
|
1955 |
apply (rule nat_diff_distrib) |
|
1956 |
prefer 3 |
|
1957 |
apply (simp add: algebra_simps) |
|
1958 |
apply (rule nat_diff_distrib [THEN trans]) |
|
1959 |
prefer 3 |
|
1960 |
apply (subst nat_add_distrib) |
|
1961 |
prefer 3 |
|
1962 |
apply (simp add: nat_power_eq) |
|
1963 |
apply auto |
|
1964 |
apply uint_arith |
|
1965 |
done |
|
1966 |
||
1967 |
lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size] |
|
1968 |
||
1969 |
lemma unat_div: "unat ((x :: 'a :: len word) div y) = unat x div unat y" |
|
1970 |
apply (simp add : unat_word_ariths) |
|
1971 |
apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq']) |
|
1972 |
apply (rule div_le_dividend) |
|
1973 |
done |
|
1974 |
||
1975 |
lemma unat_mod: "unat ((x :: 'a :: len word) mod y) = unat x mod unat y" |
|
1976 |
apply (clarsimp simp add : unat_word_ariths) |
|
1977 |
apply (cases "unat y") |
|
1978 |
prefer 2 |
|
1979 |
apply (rule unat_lt2p [THEN xtr7, THEN nat_mod_eq']) |
|
1980 |
apply (rule mod_le_divisor) |
|
1981 |
apply auto |
|
1982 |
done |
|
1983 |
||
1984 |
lemma uint_div: "uint ((x :: 'a :: len word) div y) = uint x div uint y" |
|
1985 |
unfolding uint_nat by (simp add : unat_div zdiv_int) |
|
1986 |
||
1987 |
lemma uint_mod: "uint ((x :: 'a :: len word) mod y) = uint x mod uint y" |
|
1988 |
unfolding uint_nat by (simp add : unat_mod zmod_int) |
|
1989 |
||
1990 |
||
1991 |
subsection {* Definition of unat\_arith tactic *} |
|
1992 |
||
1993 |
lemma unat_split: |
|
1994 |
fixes x::"'a::len word" |
|
1995 |
shows "P (unat x) = |
|
1996 |
(ALL n. of_nat n = x & n < 2^len_of TYPE('a) --> P n)" |
|
1997 |
by (auto simp: unat_of_nat) |
|
1998 |
||
1999 |
lemma unat_split_asm: |
|
2000 |
fixes x::"'a::len word" |
|
2001 |
shows "P (unat x) = |
|
2002 |
(~(EX n. of_nat n = x & n < 2^len_of TYPE('a) & ~ P n))" |
|
2003 |
by (auto simp: unat_of_nat) |
|
2004 |
||
2005 |
lemmas of_nat_inverse = |
|
2006 |
word_unat.Abs_inverse' [rotated, unfolded unats_def, simplified] |
|
2007 |
||
2008 |
lemmas unat_splits = unat_split unat_split_asm |
|
2009 |
||
2010 |
lemmas unat_arith_simps = |
|
2011 |
word_le_nat_alt word_less_nat_alt |
|
2012 |
word_unat.Rep_inject [symmetric] |
|
2013 |
unat_sub_if' unat_plus_if' unat_div unat_mod |
|
2014 |
||
2015 |
(* unat_arith_tac: tactic to reduce word arithmetic to nat, |
|
2016 |
try to solve via arith *) |
|
2017 |
ML {* |
|
2018 |
fun unat_arith_ss_of ss = |
|
2019 |
ss addsimps @{thms unat_arith_simps} |
|
2020 |
delsimps @{thms word_unat.Rep_inject} |
|
2021 |
addsplits @{thms split_if_asm} |
|
2022 |
addcongs @{thms power_False_cong} |
|
2023 |
||
2024 |
fun unat_arith_tacs ctxt = |
|
2025 |
let |
|
2026 |
fun arith_tac' n t = |
|
2027 |
Arith_Data.verbose_arith_tac ctxt n t |
|
2028 |
handle Cooper.COOPER _ => Seq.empty; |
|
2029 |
in |
|
42793 | 2030 |
[ clarify_tac ctxt 1, |
2031 |
full_simp_tac (unat_arith_ss_of (simpset_of ctxt)) 1, |
|
37660 | 2032 |
ALLGOALS (full_simp_tac (HOL_ss addsplits @{thms unat_splits} |
2033 |
addcongs @{thms power_False_cong})), |
|
2034 |
rewrite_goals_tac @{thms word_size}, |
|
2035 |
ALLGOALS (fn n => REPEAT (resolve_tac [allI, impI] n) THEN |
|
2036 |
REPEAT (etac conjE n) THEN |
|
2037 |
REPEAT (dtac @{thm of_nat_inverse} n THEN atac n)), |
|
2038 |
TRYALL arith_tac' ] |
|
2039 |
end |
|
2040 |
||
2041 |
fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt)) |
|
2042 |
*} |
|
2043 |
||
2044 |
method_setup unat_arith = |
|
2045 |
{* Scan.succeed (SIMPLE_METHOD' o unat_arith_tac) *} |
|
2046 |
"solving word arithmetic via natural numbers and arith" |
|
2047 |
||
2048 |
lemma no_plus_overflow_unat_size: |
|
2049 |
"((x :: 'a :: len word) <= x + y) = (unat x + unat y < 2 ^ size x)" |
|
2050 |
unfolding word_size by unat_arith |
|
2051 |
||
2052 |
lemmas no_olen_add_nat = no_plus_overflow_unat_size [unfolded word_size] |
|
2053 |
||
2054 |
lemmas unat_plus_simple = trans [OF no_olen_add_nat unat_add_lem, standard] |
|
2055 |
||
2056 |
lemma word_div_mult: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2057 |
"(0 :: 'a :: len word) < y \<Longrightarrow> unat x * unat y < 2 ^ len_of TYPE('a) \<Longrightarrow> |
37660 | 2058 |
x * y div y = x" |
2059 |
apply unat_arith |
|
2060 |
apply clarsimp |
|
2061 |
apply (subst unat_mult_lem [THEN iffD1]) |
|
2062 |
apply auto |
|
2063 |
done |
|
2064 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2065 |
lemma div_lt': "(i :: 'a :: len word) <= k div x \<Longrightarrow> |
37660 | 2066 |
unat i * unat x < 2 ^ len_of TYPE('a)" |
2067 |
apply unat_arith |
|
2068 |
apply clarsimp |
|
2069 |
apply (drule mult_le_mono1) |
|
2070 |
apply (erule order_le_less_trans) |
|
2071 |
apply (rule xtr7 [OF unat_lt2p div_mult_le]) |
|
2072 |
done |
|
2073 |
||
2074 |
lemmas div_lt'' = order_less_imp_le [THEN div_lt'] |
|
2075 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2076 |
lemma div_lt_mult: "(i :: 'a :: len word) < k div x \<Longrightarrow> 0 < x \<Longrightarrow> i * x < k" |
37660 | 2077 |
apply (frule div_lt'' [THEN unat_mult_lem [THEN iffD1]]) |
2078 |
apply (simp add: unat_arith_simps) |
|
2079 |
apply (drule (1) mult_less_mono1) |
|
2080 |
apply (erule order_less_le_trans) |
|
2081 |
apply (rule div_mult_le) |
|
2082 |
done |
|
2083 |
||
2084 |
lemma div_le_mult: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2085 |
"(i :: 'a :: len word) <= k div x \<Longrightarrow> 0 < x \<Longrightarrow> i * x <= k" |
37660 | 2086 |
apply (frule div_lt' [THEN unat_mult_lem [THEN iffD1]]) |
2087 |
apply (simp add: unat_arith_simps) |
|
2088 |
apply (drule mult_le_mono1) |
|
2089 |
apply (erule order_trans) |
|
2090 |
apply (rule div_mult_le) |
|
2091 |
done |
|
2092 |
||
2093 |
lemma div_lt_uint': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2094 |
"(i :: 'a :: len word) <= k div x \<Longrightarrow> uint i * uint x < 2 ^ len_of TYPE('a)" |
37660 | 2095 |
apply (unfold uint_nat) |
2096 |
apply (drule div_lt') |
|
2097 |
apply (simp add: zmult_int zless_nat_eq_int_zless [symmetric] |
|
2098 |
nat_power_eq) |
|
2099 |
done |
|
2100 |
||
2101 |
lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint'] |
|
2102 |
||
2103 |
lemma word_le_exists': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2104 |
"(x :: 'a :: len0 word) <= y \<Longrightarrow> |
37660 | 2105 |
(EX z. y = x + z & uint x + uint z < 2 ^ len_of TYPE('a))" |
2106 |
apply (rule exI) |
|
2107 |
apply (rule conjI) |
|
2108 |
apply (rule zadd_diff_inverse) |
|
2109 |
apply uint_arith |
|
2110 |
done |
|
2111 |
||
2112 |
lemmas plus_minus_not_NULL = order_less_imp_le [THEN plus_minus_not_NULL_ab] |
|
2113 |
||
2114 |
lemmas plus_minus_no_overflow = |
|
2115 |
order_less_imp_le [THEN plus_minus_no_overflow_ab] |
|
2116 |
||
2117 |
lemmas mcs = word_less_minus_cancel word_less_minus_mono_left |
|
2118 |
word_le_minus_cancel word_le_minus_mono_left |
|
2119 |
||
2120 |
lemmas word_l_diffs = mcs [where y = "w + x", unfolded add_diff_cancel, standard] |
|
2121 |
lemmas word_diff_ls = mcs [where z = "w + x", unfolded add_diff_cancel, standard] |
|
2122 |
lemmas word_plus_mcs = word_diff_ls |
|
2123 |
[where y = "v + x", unfolded add_diff_cancel, standard] |
|
2124 |
||
2125 |
lemmas le_unat_uoi = unat_le [THEN word_unat.Abs_inverse] |
|
2126 |
||
2127 |
lemmas thd = refl [THEN [2] split_div_lemma [THEN iffD2], THEN conjunct1] |
|
2128 |
||
2129 |
lemma thd1: |
|
2130 |
"a div b * b \<le> (a::nat)" |
|
2131 |
using gt_or_eq_0 [of b] |
|
2132 |
apply (rule disjE) |
|
2133 |
apply (erule xtr4 [OF thd mult_commute]) |
|
2134 |
apply clarsimp |
|
2135 |
done |
|
2136 |
||
2137 |
lemmas uno_simps [THEN le_unat_uoi, standard] = |
|
2138 |
mod_le_divisor div_le_dividend thd1 |
|
2139 |
||
2140 |
lemma word_mod_div_equality: |
|
2141 |
"(n div b) * b + (n mod b) = (n :: 'a :: len word)" |
|
2142 |
apply (unfold word_less_nat_alt word_arith_nat_defs) |
|
2143 |
apply (cut_tac y="unat b" in gt_or_eq_0) |
|
2144 |
apply (erule disjE) |
|
2145 |
apply (simp add: mod_div_equality uno_simps) |
|
2146 |
apply simp |
|
2147 |
done |
|
2148 |
||
2149 |
lemma word_div_mult_le: "a div b * b <= (a::'a::len word)" |
|
2150 |
apply (unfold word_le_nat_alt word_arith_nat_defs) |
|
2151 |
apply (cut_tac y="unat b" in gt_or_eq_0) |
|
2152 |
apply (erule disjE) |
|
2153 |
apply (simp add: div_mult_le uno_simps) |
|
2154 |
apply simp |
|
2155 |
done |
|
2156 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2157 |
lemma word_mod_less_divisor: "0 < n \<Longrightarrow> m mod n < (n :: 'a :: len word)" |
37660 | 2158 |
apply (simp only: word_less_nat_alt word_arith_nat_defs) |
2159 |
apply (clarsimp simp add : uno_simps) |
|
2160 |
done |
|
2161 |
||
2162 |
lemma word_of_int_power_hom: |
|
2163 |
"word_of_int a ^ n = (word_of_int (a ^ n) :: 'a :: len word)" |
|
41550 | 2164 |
by (induct n) (simp_all add: word_of_int_hom_syms) |
37660 | 2165 |
|
2166 |
lemma word_arith_power_alt: |
|
2167 |
"a ^ n = (word_of_int (uint a ^ n) :: 'a :: len word)" |
|
2168 |
by (simp add : word_of_int_power_hom [symmetric]) |
|
2169 |
||
2170 |
lemma of_bl_length_less: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2171 |
"length x = k \<Longrightarrow> k < len_of TYPE('a) \<Longrightarrow> (of_bl x :: 'a :: len word) < 2 ^ k" |
37660 | 2172 |
apply (unfold of_bl_no [unfolded word_number_of_def] |
2173 |
word_less_alt word_number_of_alt) |
|
2174 |
apply safe |
|
2175 |
apply (simp (no_asm) add: word_of_int_power_hom word_uint.eq_norm |
|
2176 |
del: word_of_int_bin) |
|
2177 |
apply (simp add: mod_pos_pos_trivial) |
|
2178 |
apply (subst mod_pos_pos_trivial) |
|
2179 |
apply (rule bl_to_bin_ge0) |
|
2180 |
apply (rule order_less_trans) |
|
2181 |
apply (rule bl_to_bin_lt2p) |
|
2182 |
apply simp |
|
2183 |
apply (rule bl_to_bin_lt2p) |
|
2184 |
done |
|
2185 |
||
2186 |
||
2187 |
subsection "Cardinality, finiteness of set of words" |
|
2188 |
||
2189 |
lemmas card_lessThan' = card_lessThan [unfolded lessThan_def] |
|
2190 |
||
2191 |
lemmas card_eq = word_unat.Abs_inj_on [THEN card_image, |
|
2192 |
unfolded word_unat.image, unfolded unats_def, standard] |
|
2193 |
||
2194 |
lemmas card_word = trans [OF card_eq card_lessThan', standard] |
|
2195 |
||
2196 |
lemma finite_word_UNIV: "finite (UNIV :: 'a :: len word set)" |
|
2197 |
apply (rule contrapos_np) |
|
2198 |
prefer 2 |
|
2199 |
apply (erule card_infinite) |
|
2200 |
apply (simp add: card_word) |
|
2201 |
done |
|
2202 |
||
2203 |
lemma card_word_size: |
|
2204 |
"card (UNIV :: 'a :: len word set) = (2 ^ size (x :: 'a word))" |
|
2205 |
unfolding word_size by (rule card_word) |
|
2206 |
||
2207 |
||
2208 |
subsection {* Bitwise Operations on Words *} |
|
2209 |
||
2210 |
lemmas bin_log_bintrs = bin_trunc_not bin_trunc_xor bin_trunc_and bin_trunc_or |
|
2211 |
||
2212 |
(* following definitions require both arithmetic and bit-wise word operations *) |
|
2213 |
||
2214 |
(* to get word_no_log_defs from word_log_defs, using bin_log_bintrs *) |
|
2215 |
lemmas wils1 = bin_log_bintrs [THEN word_ubin.norm_eq_iff [THEN iffD1], |
|
2216 |
folded word_ubin.eq_norm, THEN eq_reflection, standard] |
|
2217 |
||
2218 |
(* the binary operations only *) |
|
2219 |
lemmas word_log_binary_defs = |
|
2220 |
word_and_def word_or_def word_xor_def |
|
2221 |
||
2222 |
lemmas word_no_log_defs [simp] = |
|
2223 |
word_not_def [where a="number_of a", |
|
2224 |
unfolded word_no_wi wils1, folded word_no_wi, standard] |
|
2225 |
word_log_binary_defs [where a="number_of a" and b="number_of b", |
|
2226 |
unfolded word_no_wi wils1, folded word_no_wi, standard] |
|
2227 |
||
2228 |
lemmas word_wi_log_defs = word_no_log_defs [unfolded word_no_wi] |
|
2229 |
||
2230 |
lemma uint_or: "uint (x OR y) = (uint x) OR (uint y)" |
|
2231 |
by (simp add: word_or_def word_no_wi [symmetric] number_of_is_id |
|
2232 |
bin_trunc_ao(2) [symmetric]) |
|
2233 |
||
2234 |
lemma uint_and: "uint (x AND y) = (uint x) AND (uint y)" |
|
2235 |
by (simp add: word_and_def number_of_is_id word_no_wi [symmetric] |
|
2236 |
bin_trunc_ao(1) [symmetric]) |
|
2237 |
||
2238 |
lemma word_ops_nth_size: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2239 |
"n < size (x::'a::len0 word) \<Longrightarrow> |
37660 | 2240 |
(x OR y) !! n = (x !! n | y !! n) & |
2241 |
(x AND y) !! n = (x !! n & y !! n) & |
|
2242 |
(x XOR y) !! n = (x !! n ~= y !! n) & |
|
2243 |
(NOT x) !! n = (~ x !! n)" |
|
2244 |
unfolding word_size word_no_wi word_test_bit_def word_log_defs |
|
2245 |
by (clarsimp simp add : word_ubin.eq_norm nth_bintr bin_nth_ops) |
|
2246 |
||
2247 |
lemma word_ao_nth: |
|
2248 |
fixes x :: "'a::len0 word" |
|
2249 |
shows "(x OR y) !! n = (x !! n | y !! n) & |
|
2250 |
(x AND y) !! n = (x !! n & y !! n)" |
|
2251 |
apply (cases "n < size x") |
|
2252 |
apply (drule_tac y = "y" in word_ops_nth_size) |
|
2253 |
apply simp |
|
2254 |
apply (simp add : test_bit_bin word_size) |
|
2255 |
done |
|
2256 |
||
2257 |
(* get from commutativity, associativity etc of int_and etc |
|
2258 |
to same for word_and etc *) |
|
2259 |
||
2260 |
lemmas bwsimps = |
|
2261 |
word_of_int_homs(2) |
|
2262 |
word_0_wi_Pls |
|
2263 |
word_m1_wi_Min |
|
2264 |
word_wi_log_defs |
|
2265 |
||
2266 |
lemma word_bw_assocs: |
|
2267 |
fixes x :: "'a::len0 word" |
|
2268 |
shows |
|
2269 |
"(x AND y) AND z = x AND y AND z" |
|
2270 |
"(x OR y) OR z = x OR y OR z" |
|
2271 |
"(x XOR y) XOR z = x XOR y XOR z" |
|
2272 |
using word_of_int_Ex [where x=x] |
|
2273 |
word_of_int_Ex [where x=y] |
|
2274 |
word_of_int_Ex [where x=z] |
|
2275 |
by (auto simp: bwsimps bbw_assocs) |
|
2276 |
||
2277 |
lemma word_bw_comms: |
|
2278 |
fixes x :: "'a::len0 word" |
|
2279 |
shows |
|
2280 |
"x AND y = y AND x" |
|
2281 |
"x OR y = y OR x" |
|
2282 |
"x XOR y = y XOR x" |
|
2283 |
using word_of_int_Ex [where x=x] |
|
2284 |
word_of_int_Ex [where x=y] |
|
2285 |
by (auto simp: bwsimps bin_ops_comm) |
|
2286 |
||
2287 |
lemma word_bw_lcs: |
|
2288 |
fixes x :: "'a::len0 word" |
|
2289 |
shows |
|
2290 |
"y AND x AND z = x AND y AND z" |
|
2291 |
"y OR x OR z = x OR y OR z" |
|
2292 |
"y XOR x XOR z = x XOR y XOR z" |
|
2293 |
using word_of_int_Ex [where x=x] |
|
2294 |
word_of_int_Ex [where x=y] |
|
2295 |
word_of_int_Ex [where x=z] |
|
2296 |
by (auto simp: bwsimps) |
|
2297 |
||
2298 |
lemma word_log_esimps [simp]: |
|
2299 |
fixes x :: "'a::len0 word" |
|
2300 |
shows |
|
2301 |
"x AND 0 = 0" |
|
2302 |
"x AND -1 = x" |
|
2303 |
"x OR 0 = x" |
|
2304 |
"x OR -1 = -1" |
|
2305 |
"x XOR 0 = x" |
|
2306 |
"x XOR -1 = NOT x" |
|
2307 |
"0 AND x = 0" |
|
2308 |
"-1 AND x = x" |
|
2309 |
"0 OR x = x" |
|
2310 |
"-1 OR x = -1" |
|
2311 |
"0 XOR x = x" |
|
2312 |
"-1 XOR x = NOT x" |
|
2313 |
using word_of_int_Ex [where x=x] |
|
2314 |
by (auto simp: bwsimps) |
|
2315 |
||
2316 |
lemma word_not_dist: |
|
2317 |
fixes x :: "'a::len0 word" |
|
2318 |
shows |
|
2319 |
"NOT (x OR y) = NOT x AND NOT y" |
|
2320 |
"NOT (x AND y) = NOT x OR NOT y" |
|
2321 |
using word_of_int_Ex [where x=x] |
|
2322 |
word_of_int_Ex [where x=y] |
|
2323 |
by (auto simp: bwsimps bbw_not_dist) |
|
2324 |
||
2325 |
lemma word_bw_same: |
|
2326 |
fixes x :: "'a::len0 word" |
|
2327 |
shows |
|
2328 |
"x AND x = x" |
|
2329 |
"x OR x = x" |
|
2330 |
"x XOR x = 0" |
|
2331 |
using word_of_int_Ex [where x=x] |
|
2332 |
by (auto simp: bwsimps) |
|
2333 |
||
2334 |
lemma word_ao_absorbs [simp]: |
|
2335 |
fixes x :: "'a::len0 word" |
|
2336 |
shows |
|
2337 |
"x AND (y OR x) = x" |
|
2338 |
"x OR y AND x = x" |
|
2339 |
"x AND (x OR y) = x" |
|
2340 |
"y AND x OR x = x" |
|
2341 |
"(y OR x) AND x = x" |
|
2342 |
"x OR x AND y = x" |
|
2343 |
"(x OR y) AND x = x" |
|
2344 |
"x AND y OR x = x" |
|
2345 |
using word_of_int_Ex [where x=x] |
|
2346 |
word_of_int_Ex [where x=y] |
|
2347 |
by (auto simp: bwsimps) |
|
2348 |
||
2349 |
lemma word_not_not [simp]: |
|
2350 |
"NOT NOT (x::'a::len0 word) = x" |
|
2351 |
using word_of_int_Ex [where x=x] |
|
2352 |
by (auto simp: bwsimps) |
|
2353 |
||
2354 |
lemma word_ao_dist: |
|
2355 |
fixes x :: "'a::len0 word" |
|
2356 |
shows "(x OR y) AND z = x AND z OR y AND z" |
|
2357 |
using word_of_int_Ex [where x=x] |
|
2358 |
word_of_int_Ex [where x=y] |
|
2359 |
word_of_int_Ex [where x=z] |
|
41550 | 2360 |
by (auto simp: bwsimps bbw_ao_dist) |
37660 | 2361 |
|
2362 |
lemma word_oa_dist: |
|
2363 |
fixes x :: "'a::len0 word" |
|
2364 |
shows "x AND y OR z = (x OR z) AND (y OR z)" |
|
2365 |
using word_of_int_Ex [where x=x] |
|
2366 |
word_of_int_Ex [where x=y] |
|
2367 |
word_of_int_Ex [where x=z] |
|
41550 | 2368 |
by (auto simp: bwsimps bbw_oa_dist) |
37660 | 2369 |
|
2370 |
lemma word_add_not [simp]: |
|
2371 |
fixes x :: "'a::len0 word" |
|
2372 |
shows "x + NOT x = -1" |
|
2373 |
using word_of_int_Ex [where x=x] |
|
2374 |
by (auto simp: bwsimps bin_add_not) |
|
2375 |
||
2376 |
lemma word_plus_and_or [simp]: |
|
2377 |
fixes x :: "'a::len0 word" |
|
2378 |
shows "(x AND y) + (x OR y) = x + y" |
|
2379 |
using word_of_int_Ex [where x=x] |
|
2380 |
word_of_int_Ex [where x=y] |
|
2381 |
by (auto simp: bwsimps plus_and_or) |
|
2382 |
||
2383 |
lemma leoa: |
|
2384 |
fixes x :: "'a::len0 word" |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2385 |
shows "(w = (x OR y)) \<Longrightarrow> (y = (w AND y))" by auto |
37660 | 2386 |
lemma leao: |
2387 |
fixes x' :: "'a::len0 word" |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2388 |
shows "(w' = (x' AND y')) \<Longrightarrow> (x' = (x' OR w'))" by auto |
37660 | 2389 |
|
2390 |
lemmas word_ao_equiv = leao [COMP leoa [COMP iffI]] |
|
2391 |
||
2392 |
lemma le_word_or2: "x <= x OR (y::'a::len0 word)" |
|
2393 |
unfolding word_le_def uint_or |
|
2394 |
by (auto intro: le_int_or) |
|
2395 |
||
2396 |
lemmas le_word_or1 = xtr3 [OF word_bw_comms (2) le_word_or2, standard] |
|
2397 |
lemmas word_and_le1 = |
|
2398 |
xtr3 [OF word_ao_absorbs (4) [symmetric] le_word_or2, standard] |
|
2399 |
lemmas word_and_le2 = |
|
2400 |
xtr3 [OF word_ao_absorbs (8) [symmetric] le_word_or2, standard] |
|
2401 |
||
2402 |
lemma bl_word_not: "to_bl (NOT w) = map Not (to_bl w)" |
|
2403 |
unfolding to_bl_def word_log_defs |
|
2404 |
by (simp add: bl_not_bin number_of_is_id word_no_wi [symmetric] bin_to_bl_def[symmetric]) |
|
2405 |
||
2406 |
lemma bl_word_xor: "to_bl (v XOR w) = map2 op ~= (to_bl v) (to_bl w)" |
|
2407 |
unfolding to_bl_def word_log_defs bl_xor_bin |
|
2408 |
by (simp add: number_of_is_id word_no_wi [symmetric]) |
|
2409 |
||
2410 |
lemma bl_word_or: "to_bl (v OR w) = map2 op | (to_bl v) (to_bl w)" |
|
2411 |
unfolding to_bl_def word_log_defs |
|
2412 |
by (simp add: bl_or_bin number_of_is_id word_no_wi [symmetric]) |
|
2413 |
||
2414 |
lemma bl_word_and: "to_bl (v AND w) = map2 op & (to_bl v) (to_bl w)" |
|
2415 |
unfolding to_bl_def word_log_defs |
|
2416 |
by (simp add: bl_and_bin number_of_is_id word_no_wi [symmetric]) |
|
2417 |
||
2418 |
lemma word_lsb_alt: "lsb (w::'a::len0 word) = test_bit w 0" |
|
2419 |
by (auto simp: word_test_bit_def word_lsb_def) |
|
2420 |
||
2421 |
lemma word_lsb_1_0: "lsb (1::'a::len word) & ~ lsb (0::'b::len0 word)" |
|
2422 |
unfolding word_lsb_def word_1_no word_0_no by auto |
|
2423 |
||
2424 |
lemma word_lsb_last: "lsb (w::'a::len word) = last (to_bl w)" |
|
2425 |
apply (unfold word_lsb_def uint_bl bin_to_bl_def) |
|
2426 |
apply (rule_tac bin="uint w" in bin_exhaust) |
|
2427 |
apply (cases "size w") |
|
2428 |
apply auto |
|
2429 |
apply (auto simp add: bin_to_bl_aux_alt) |
|
2430 |
done |
|
2431 |
||
2432 |
lemma word_lsb_int: "lsb w = (uint w mod 2 = 1)" |
|
45529
0e1037d4e049
remove redundant lemmas bin_last_mod and bin_rest_div, use bin_last_def and bin_rest_def instead
huffman
parents:
45528
diff
changeset
|
2433 |
unfolding word_lsb_def bin_last_def by auto |
37660 | 2434 |
|
2435 |
lemma word_msb_sint: "msb w = (sint w < 0)" |
|
2436 |
unfolding word_msb_def |
|
2437 |
by (simp add : sign_Min_lt_0 number_of_is_id) |
|
2438 |
||
2439 |
lemma word_msb_no': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2440 |
"w = number_of bin \<Longrightarrow> msb (w::'a::len word) = bin_nth bin (size w - 1)" |
37660 | 2441 |
unfolding word_msb_def word_number_of_def |
2442 |
by (clarsimp simp add: word_sbin.eq_norm word_size bin_sign_lem) |
|
2443 |
||
2444 |
lemmas word_msb_no = refl [THEN word_msb_no', unfolded word_size] |
|
2445 |
||
2446 |
lemma word_msb_nth': "msb (w::'a::len word) = bin_nth (uint w) (size w - 1)" |
|
2447 |
apply (unfold word_size) |
|
2448 |
apply (rule trans [OF _ word_msb_no]) |
|
2449 |
apply (simp add : word_number_of_def) |
|
2450 |
done |
|
2451 |
||
2452 |
lemmas word_msb_nth = word_msb_nth' [unfolded word_size] |
|
2453 |
||
2454 |
lemma word_msb_alt: "msb (w::'a::len word) = hd (to_bl w)" |
|
2455 |
apply (unfold word_msb_nth uint_bl) |
|
2456 |
apply (subst hd_conv_nth) |
|
2457 |
apply (rule length_greater_0_conv [THEN iffD1]) |
|
2458 |
apply simp |
|
2459 |
apply (simp add : nth_bin_to_bl word_size) |
|
2460 |
done |
|
2461 |
||
2462 |
lemma word_set_nth: |
|
2463 |
"set_bit w n (test_bit w n) = (w::'a::len0 word)" |
|
2464 |
unfolding word_test_bit_def word_set_bit_def by auto |
|
2465 |
||
2466 |
lemma bin_nth_uint': |
|
2467 |
"bin_nth (uint w) n = (rev (bin_to_bl (size w) (uint w)) ! n & n < size w)" |
|
2468 |
apply (unfold word_size) |
|
2469 |
apply (safe elim!: bin_nth_uint_imp) |
|
2470 |
apply (frule bin_nth_uint_imp) |
|
2471 |
apply (fast dest!: bin_nth_bl)+ |
|
2472 |
done |
|
2473 |
||
2474 |
lemmas bin_nth_uint = bin_nth_uint' [unfolded word_size] |
|
2475 |
||
2476 |
lemma test_bit_bl: "w !! n = (rev (to_bl w) ! n & n < size w)" |
|
2477 |
unfolding to_bl_def word_test_bit_def word_size |
|
2478 |
by (rule bin_nth_uint) |
|
2479 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2480 |
lemma to_bl_nth: "n < size w \<Longrightarrow> to_bl w ! n = w !! (size w - Suc n)" |
37660 | 2481 |
apply (unfold test_bit_bl) |
2482 |
apply clarsimp |
|
2483 |
apply (rule trans) |
|
2484 |
apply (rule nth_rev_alt) |
|
2485 |
apply (auto simp add: word_size) |
|
2486 |
done |
|
2487 |
||
2488 |
lemma test_bit_set: |
|
2489 |
fixes w :: "'a::len0 word" |
|
2490 |
shows "(set_bit w n x) !! n = (n < size w & x)" |
|
2491 |
unfolding word_size word_test_bit_def word_set_bit_def |
|
2492 |
by (clarsimp simp add : word_ubin.eq_norm nth_bintr) |
|
2493 |
||
2494 |
lemma test_bit_set_gen: |
|
2495 |
fixes w :: "'a::len0 word" |
|
2496 |
shows "test_bit (set_bit w n x) m = |
|
2497 |
(if m = n then n < size w & x else test_bit w m)" |
|
2498 |
apply (unfold word_size word_test_bit_def word_set_bit_def) |
|
2499 |
apply (clarsimp simp add: word_ubin.eq_norm nth_bintr bin_nth_sc_gen) |
|
2500 |
apply (auto elim!: test_bit_size [unfolded word_size] |
|
2501 |
simp add: word_test_bit_def [symmetric]) |
|
2502 |
done |
|
2503 |
||
2504 |
lemma of_bl_rep_False: "of_bl (replicate n False @ bs) = of_bl bs" |
|
2505 |
unfolding of_bl_def bl_to_bin_rep_F by auto |
|
2506 |
||
2507 |
lemma msb_nth': |
|
2508 |
fixes w :: "'a::len word" |
|
2509 |
shows "msb w = w !! (size w - 1)" |
|
2510 |
unfolding word_msb_nth' word_test_bit_def by simp |
|
2511 |
||
2512 |
lemmas msb_nth = msb_nth' [unfolded word_size] |
|
2513 |
||
2514 |
lemmas msb0 = len_gt_0 [THEN diff_Suc_less, THEN |
|
2515 |
word_ops_nth_size [unfolded word_size], standard] |
|
2516 |
lemmas msb1 = msb0 [where i = 0] |
|
2517 |
lemmas word_ops_msb = msb1 [unfolded msb_nth [symmetric, unfolded One_nat_def]] |
|
2518 |
||
2519 |
lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size], standard] |
|
2520 |
lemmas word_ops_lsb = lsb0 [unfolded word_lsb_alt] |
|
2521 |
||
2522 |
lemma td_ext_nth': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2523 |
"n = size (w::'a::len0 word) \<Longrightarrow> ofn = set_bits \<Longrightarrow> [w, ofn g] = l \<Longrightarrow> |
37660 | 2524 |
td_ext test_bit ofn {f. ALL i. f i --> i < n} (%h i. h i & i < n)" |
2525 |
apply (unfold word_size td_ext_def') |
|
2526 |
apply (safe del: subset_antisym) |
|
2527 |
apply (rule_tac [3] ext) |
|
2528 |
apply (rule_tac [4] ext) |
|
2529 |
apply (unfold word_size of_nth_def test_bit_bl) |
|
2530 |
apply safe |
|
2531 |
defer |
|
2532 |
apply (clarsimp simp: word_bl.Abs_inverse)+ |
|
2533 |
apply (rule word_bl.Rep_inverse') |
|
2534 |
apply (rule sym [THEN trans]) |
|
2535 |
apply (rule bl_of_nth_nth) |
|
2536 |
apply simp |
|
2537 |
apply (rule bl_of_nth_inj) |
|
2538 |
apply (clarsimp simp add : test_bit_bl word_size) |
|
2539 |
done |
|
2540 |
||
2541 |
lemmas td_ext_nth = td_ext_nth' [OF refl refl refl, unfolded word_size] |
|
2542 |
||
2543 |
interpretation test_bit: |
|
2544 |
td_ext "op !! :: 'a::len0 word => nat => bool" |
|
2545 |
set_bits |
|
2546 |
"{f. \<forall>i. f i \<longrightarrow> i < len_of TYPE('a::len0)}" |
|
2547 |
"(\<lambda>h i. h i \<and> i < len_of TYPE('a::len0))" |
|
2548 |
by (rule td_ext_nth) |
|
2549 |
||
2550 |
lemmas td_nth = test_bit.td_thm |
|
2551 |
||
2552 |
lemma word_set_set_same: |
|
2553 |
fixes w :: "'a::len0 word" |
|
2554 |
shows "set_bit (set_bit w n x) n y = set_bit w n y" |
|
2555 |
by (rule word_eqI) (simp add : test_bit_set_gen word_size) |
|
2556 |
||
2557 |
lemma word_set_set_diff: |
|
2558 |
fixes w :: "'a::len0 word" |
|
2559 |
assumes "m ~= n" |
|
2560 |
shows "set_bit (set_bit w m x) n y = set_bit (set_bit w n y) m x" |
|
41550 | 2561 |
by (rule word_eqI) (clarsimp simp add: test_bit_set_gen word_size assms) |
37660 | 2562 |
|
2563 |
lemma test_bit_no': |
|
2564 |
fixes w :: "'a::len0 word" |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2565 |
shows "w = number_of bin \<Longrightarrow> test_bit w n = (n < size w & bin_nth bin n)" |
37660 | 2566 |
unfolding word_test_bit_def word_number_of_def word_size |
2567 |
by (simp add : nth_bintr [symmetric] word_ubin.eq_norm) |
|
2568 |
||
2569 |
lemmas test_bit_no = |
|
2570 |
refl [THEN test_bit_no', unfolded word_size, THEN eq_reflection, standard] |
|
2571 |
||
2572 |
lemma nth_0: "~ (0::'a::len0 word) !! n" |
|
2573 |
unfolding test_bit_no word_0_no by auto |
|
2574 |
||
2575 |
lemma nth_sint: |
|
2576 |
fixes w :: "'a::len word" |
|
2577 |
defines "l \<equiv> len_of TYPE ('a)" |
|
2578 |
shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))" |
|
2579 |
unfolding sint_uint l_def |
|
2580 |
by (clarsimp simp add: nth_sbintr word_test_bit_def [symmetric]) |
|
2581 |
||
2582 |
lemma word_lsb_no: |
|
2583 |
"lsb (number_of bin :: 'a :: len word) = (bin_last bin = 1)" |
|
2584 |
unfolding word_lsb_alt test_bit_no by auto |
|
2585 |
||
2586 |
lemma word_set_no: |
|
2587 |
"set_bit (number_of bin::'a::len0 word) n b = |
|
2588 |
number_of (bin_sc n (if b then 1 else 0) bin)" |
|
2589 |
apply (unfold word_set_bit_def word_number_of_def [symmetric]) |
|
2590 |
apply (rule word_eqI) |
|
2591 |
apply (clarsimp simp: word_size bin_nth_sc_gen number_of_is_id |
|
2592 |
test_bit_no nth_bintr) |
|
2593 |
done |
|
2594 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2595 |
lemma setBit_no: |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2596 |
"setBit (number_of bin) n = number_of (bin_sc n 1 bin) " |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2597 |
by (simp add: setBit_def word_set_no) |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2598 |
|
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2599 |
lemma clearBit_no: |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2600 |
"clearBit (number_of bin) n = number_of (bin_sc n 0 bin)" |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2601 |
by (simp add: clearBit_def word_set_no) |
37660 | 2602 |
|
2603 |
lemma to_bl_n1: |
|
2604 |
"to_bl (-1::'a::len0 word) = replicate (len_of TYPE ('a)) True" |
|
2605 |
apply (rule word_bl.Abs_inverse') |
|
2606 |
apply simp |
|
2607 |
apply (rule word_eqI) |
|
2608 |
apply (clarsimp simp add: word_size test_bit_no) |
|
2609 |
apply (auto simp add: word_bl.Abs_inverse test_bit_bl word_size) |
|
2610 |
done |
|
2611 |
||
2612 |
lemma word_msb_n1: "msb (-1::'a::len word)" |
|
41550 | 2613 |
unfolding word_msb_alt to_bl_n1 by simp |
37660 | 2614 |
|
2615 |
declare word_set_set_same [simp] word_set_nth [simp] |
|
2616 |
test_bit_no [simp] word_set_no [simp] nth_0 [simp] |
|
2617 |
setBit_no [simp] clearBit_no [simp] |
|
2618 |
word_lsb_no [simp] word_msb_no [simp] word_msb_n1 [simp] word_lsb_1_0 [simp] |
|
2619 |
||
2620 |
lemma word_set_nth_iff: |
|
2621 |
"(set_bit w n b = w) = (w !! n = b | n >= size (w::'a::len0 word))" |
|
2622 |
apply (rule iffI) |
|
2623 |
apply (rule disjCI) |
|
2624 |
apply (drule word_eqD) |
|
2625 |
apply (erule sym [THEN trans]) |
|
2626 |
apply (simp add: test_bit_set) |
|
2627 |
apply (erule disjE) |
|
2628 |
apply clarsimp |
|
2629 |
apply (rule word_eqI) |
|
2630 |
apply (clarsimp simp add : test_bit_set_gen) |
|
2631 |
apply (drule test_bit_size) |
|
2632 |
apply force |
|
2633 |
done |
|
2634 |
||
2635 |
lemma test_bit_2p': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2636 |
"w = word_of_int (2 ^ n) \<Longrightarrow> |
37660 | 2637 |
w !! m = (m = n & m < size (w :: 'a :: len word))" |
2638 |
unfolding word_test_bit_def word_size |
|
2639 |
by (auto simp add: word_ubin.eq_norm nth_bintr nth_2p_bin) |
|
2640 |
||
2641 |
lemmas test_bit_2p = refl [THEN test_bit_2p', unfolded word_size] |
|
2642 |
||
2643 |
lemma nth_w2p: |
|
2644 |
"((2\<Colon>'a\<Colon>len word) ^ n) !! m \<longleftrightarrow> m = n \<and> m < len_of TYPE('a\<Colon>len)" |
|
2645 |
unfolding test_bit_2p [symmetric] word_of_int [symmetric] |
|
2646 |
by (simp add: of_int_power) |
|
2647 |
||
2648 |
lemma uint_2p: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2649 |
"(0::'a::len word) < 2 ^ n \<Longrightarrow> uint (2 ^ n::'a::len word) = 2 ^ n" |
37660 | 2650 |
apply (unfold word_arith_power_alt) |
2651 |
apply (case_tac "len_of TYPE ('a)") |
|
2652 |
apply clarsimp |
|
2653 |
apply (case_tac "nat") |
|
2654 |
apply clarsimp |
|
2655 |
apply (case_tac "n") |
|
2656 |
apply (clarsimp simp add : word_1_wi [symmetric]) |
|
2657 |
apply (clarsimp simp add : word_0_wi [symmetric]) |
|
2658 |
apply (drule word_gt_0 [THEN iffD1]) |
|
2659 |
apply (safe intro!: word_eqI bin_nth_lem ext) |
|
2660 |
apply (auto simp add: test_bit_2p nth_2p_bin word_test_bit_def [symmetric]) |
|
2661 |
done |
|
2662 |
||
2663 |
lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a :: len word) = 2 ^ n" |
|
2664 |
apply (unfold word_arith_power_alt) |
|
2665 |
apply (case_tac "len_of TYPE ('a)") |
|
2666 |
apply clarsimp |
|
2667 |
apply (case_tac "nat") |
|
2668 |
apply (rule word_ubin.norm_eq_iff [THEN iffD1]) |
|
2669 |
apply (rule box_equals) |
|
2670 |
apply (rule_tac [2] bintr_ariths (1))+ |
|
2671 |
apply (clarsimp simp add : number_of_is_id) |
|
2672 |
apply simp |
|
2673 |
done |
|
2674 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2675 |
lemma bang_is_le: "x !! m \<Longrightarrow> 2 ^ m <= (x :: 'a :: len word)" |
37660 | 2676 |
apply (rule xtr3) |
2677 |
apply (rule_tac [2] y = "x" in le_word_or2) |
|
2678 |
apply (rule word_eqI) |
|
2679 |
apply (auto simp add: word_ao_nth nth_w2p word_size) |
|
2680 |
done |
|
2681 |
||
2682 |
lemma word_clr_le: |
|
2683 |
fixes w :: "'a::len0 word" |
|
2684 |
shows "w >= set_bit w n False" |
|
2685 |
apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm) |
|
2686 |
apply simp |
|
2687 |
apply (rule order_trans) |
|
2688 |
apply (rule bintr_bin_clr_le) |
|
2689 |
apply simp |
|
2690 |
done |
|
2691 |
||
2692 |
lemma word_set_ge: |
|
2693 |
fixes w :: "'a::len word" |
|
2694 |
shows "w <= set_bit w n True" |
|
2695 |
apply (unfold word_set_bit_def word_le_def word_ubin.eq_norm) |
|
2696 |
apply simp |
|
2697 |
apply (rule order_trans [OF _ bintr_bin_set_ge]) |
|
2698 |
apply simp |
|
2699 |
done |
|
2700 |
||
2701 |
||
2702 |
subsection {* Shifting, Rotating, and Splitting Words *} |
|
2703 |
||
2704 |
lemma shiftl1_number [simp] : |
|
2705 |
"shiftl1 (number_of w) = number_of (w BIT 0)" |
|
2706 |
apply (unfold shiftl1_def word_number_of_def) |
|
2707 |
apply (simp add: word_ubin.norm_eq_iff [symmetric] word_ubin.eq_norm |
|
2708 |
del: BIT_simps) |
|
2709 |
apply (subst refl [THEN bintrunc_BIT_I, symmetric]) |
|
2710 |
apply (subst bintrunc_bintrunc_min) |
|
2711 |
apply simp |
|
2712 |
done |
|
2713 |
||
2714 |
lemma shiftl1_0 [simp] : "shiftl1 0 = 0" |
|
2715 |
unfolding word_0_no shiftl1_number by auto |
|
2716 |
||
2717 |
lemmas shiftl1_def_u = shiftl1_def [folded word_number_of_def] |
|
2718 |
||
2719 |
lemma shiftl1_def_s: "shiftl1 w = number_of (sint w BIT 0)" |
|
2720 |
by (rule trans [OF _ shiftl1_number]) simp |
|
2721 |
||
2722 |
lemma shiftr1_0 [simp] : "shiftr1 0 = 0" |
|
2723 |
unfolding shiftr1_def |
|
2724 |
by simp (simp add: word_0_wi) |
|
2725 |
||
2726 |
lemma sshiftr1_0 [simp] : "sshiftr1 0 = 0" |
|
2727 |
apply (unfold sshiftr1_def) |
|
2728 |
apply simp |
|
2729 |
apply (simp add : word_0_wi) |
|
2730 |
done |
|
2731 |
||
2732 |
lemma sshiftr1_n1 [simp] : "sshiftr1 -1 = -1" |
|
2733 |
unfolding sshiftr1_def by auto |
|
2734 |
||
2735 |
lemma shiftl_0 [simp] : "(0::'a::len0 word) << n = 0" |
|
2736 |
unfolding shiftl_def by (induct n) auto |
|
2737 |
||
2738 |
lemma shiftr_0 [simp] : "(0::'a::len0 word) >> n = 0" |
|
2739 |
unfolding shiftr_def by (induct n) auto |
|
2740 |
||
2741 |
lemma sshiftr_0 [simp] : "0 >>> n = 0" |
|
2742 |
unfolding sshiftr_def by (induct n) auto |
|
2743 |
||
2744 |
lemma sshiftr_n1 [simp] : "-1 >>> n = -1" |
|
2745 |
unfolding sshiftr_def by (induct n) auto |
|
2746 |
||
2747 |
lemma nth_shiftl1: "shiftl1 w !! n = (n < size w & n > 0 & w !! (n - 1))" |
|
2748 |
apply (unfold shiftl1_def word_test_bit_def) |
|
2749 |
apply (simp add: nth_bintr word_ubin.eq_norm word_size) |
|
2750 |
apply (cases n) |
|
2751 |
apply auto |
|
2752 |
done |
|
2753 |
||
2754 |
lemma nth_shiftl' [rule_format]: |
|
2755 |
"ALL n. ((w::'a::len0 word) << m) !! n = (n < size w & n >= m & w !! (n - m))" |
|
2756 |
apply (unfold shiftl_def) |
|
2757 |
apply (induct "m") |
|
2758 |
apply (force elim!: test_bit_size) |
|
2759 |
apply (clarsimp simp add : nth_shiftl1 word_size) |
|
2760 |
apply arith |
|
2761 |
done |
|
2762 |
||
2763 |
lemmas nth_shiftl = nth_shiftl' [unfolded word_size] |
|
2764 |
||
2765 |
lemma nth_shiftr1: "shiftr1 w !! n = w !! Suc n" |
|
2766 |
apply (unfold shiftr1_def word_test_bit_def) |
|
2767 |
apply (simp add: nth_bintr word_ubin.eq_norm) |
|
2768 |
apply safe |
|
2769 |
apply (drule bin_nth.Suc [THEN iffD2, THEN bin_nth_uint_imp]) |
|
2770 |
apply simp |
|
2771 |
done |
|
2772 |
||
2773 |
lemma nth_shiftr: |
|
2774 |
"\<And>n. ((w::'a::len0 word) >> m) !! n = w !! (n + m)" |
|
2775 |
apply (unfold shiftr_def) |
|
2776 |
apply (induct "m") |
|
2777 |
apply (auto simp add : nth_shiftr1) |
|
2778 |
done |
|
2779 |
||
2780 |
(* see paper page 10, (1), (2), shiftr1_def is of the form of (1), |
|
2781 |
where f (ie bin_rest) takes normal arguments to normal results, |
|
2782 |
thus we get (2) from (1) *) |
|
2783 |
||
2784 |
lemma uint_shiftr1: "uint (shiftr1 w) = bin_rest (uint w)" |
|
2785 |
apply (unfold shiftr1_def word_ubin.eq_norm bin_rest_trunc_i) |
|
2786 |
apply (subst bintr_uint [symmetric, OF order_refl]) |
|
2787 |
apply (simp only : bintrunc_bintrunc_l) |
|
2788 |
apply simp |
|
2789 |
done |
|
2790 |
||
2791 |
lemma nth_sshiftr1: |
|
2792 |
"sshiftr1 w !! n = (if n = size w - 1 then w !! n else w !! Suc n)" |
|
2793 |
apply (unfold sshiftr1_def word_test_bit_def) |
|
2794 |
apply (simp add: nth_bintr word_ubin.eq_norm |
|
2795 |
bin_nth.Suc [symmetric] word_size |
|
2796 |
del: bin_nth.simps) |
|
2797 |
apply (simp add: nth_bintr uint_sint del : bin_nth.simps) |
|
2798 |
apply (auto simp add: bin_nth_sint) |
|
2799 |
done |
|
2800 |
||
2801 |
lemma nth_sshiftr [rule_format] : |
|
2802 |
"ALL n. sshiftr w m !! n = (n < size w & |
|
2803 |
(if n + m >= size w then w !! (size w - 1) else w !! (n + m)))" |
|
2804 |
apply (unfold sshiftr_def) |
|
2805 |
apply (induct_tac "m") |
|
2806 |
apply (simp add: test_bit_bl) |
|
2807 |
apply (clarsimp simp add: nth_sshiftr1 word_size) |
|
2808 |
apply safe |
|
2809 |
apply arith |
|
2810 |
apply arith |
|
2811 |
apply (erule thin_rl) |
|
2812 |
apply (case_tac n) |
|
2813 |
apply safe |
|
2814 |
apply simp |
|
2815 |
apply simp |
|
2816 |
apply (erule thin_rl) |
|
2817 |
apply (case_tac n) |
|
2818 |
apply safe |
|
2819 |
apply simp |
|
2820 |
apply simp |
|
2821 |
apply arith+ |
|
2822 |
done |
|
2823 |
||
2824 |
lemma shiftr1_div_2: "uint (shiftr1 w) = uint w div 2" |
|
45529
0e1037d4e049
remove redundant lemmas bin_last_mod and bin_rest_div, use bin_last_def and bin_rest_def instead
huffman
parents:
45528
diff
changeset
|
2825 |
apply (unfold shiftr1_def bin_rest_def) |
37660 | 2826 |
apply (rule word_uint.Abs_inverse) |
2827 |
apply (simp add: uints_num pos_imp_zdiv_nonneg_iff) |
|
2828 |
apply (rule xtr7) |
|
2829 |
prefer 2 |
|
2830 |
apply (rule zdiv_le_dividend) |
|
2831 |
apply auto |
|
2832 |
done |
|
2833 |
||
2834 |
lemma sshiftr1_div_2: "sint (sshiftr1 w) = sint w div 2" |
|
45529
0e1037d4e049
remove redundant lemmas bin_last_mod and bin_rest_div, use bin_last_def and bin_rest_def instead
huffman
parents:
45528
diff
changeset
|
2835 |
apply (unfold sshiftr1_def bin_rest_def [symmetric]) |
37660 | 2836 |
apply (simp add: word_sbin.eq_norm) |
2837 |
apply (rule trans) |
|
2838 |
defer |
|
2839 |
apply (subst word_sbin.norm_Rep [symmetric]) |
|
2840 |
apply (rule refl) |
|
2841 |
apply (subst word_sbin.norm_Rep [symmetric]) |
|
2842 |
apply (unfold One_nat_def) |
|
2843 |
apply (rule sbintrunc_rest) |
|
2844 |
done |
|
2845 |
||
2846 |
lemma shiftr_div_2n: "uint (shiftr w n) = uint w div 2 ^ n" |
|
2847 |
apply (unfold shiftr_def) |
|
2848 |
apply (induct "n") |
|
2849 |
apply simp |
|
2850 |
apply (simp add: shiftr1_div_2 mult_commute |
|
2851 |
zdiv_zmult2_eq [symmetric]) |
|
2852 |
done |
|
2853 |
||
2854 |
lemma sshiftr_div_2n: "sint (sshiftr w n) = sint w div 2 ^ n" |
|
2855 |
apply (unfold sshiftr_def) |
|
2856 |
apply (induct "n") |
|
2857 |
apply simp |
|
2858 |
apply (simp add: sshiftr1_div_2 mult_commute |
|
2859 |
zdiv_zmult2_eq [symmetric]) |
|
2860 |
done |
|
2861 |
||
2862 |
subsubsection "shift functions in terms of lists of bools" |
|
2863 |
||
2864 |
lemmas bshiftr1_no_bin [simp] = |
|
2865 |
bshiftr1_def [where w="number_of w", unfolded to_bl_no_bin, standard] |
|
2866 |
||
2867 |
lemma bshiftr1_bl: "to_bl (bshiftr1 b w) = b # butlast (to_bl w)" |
|
2868 |
unfolding bshiftr1_def by (rule word_bl.Abs_inverse) simp |
|
2869 |
||
2870 |
lemma shiftl1_of_bl: "shiftl1 (of_bl bl) = of_bl (bl @ [False])" |
|
2871 |
unfolding uint_bl of_bl_no |
|
2872 |
by (simp add: bl_to_bin_aux_append bl_to_bin_def) |
|
2873 |
||
2874 |
lemma shiftl1_bl: "shiftl1 (w::'a::len0 word) = of_bl (to_bl w @ [False])" |
|
2875 |
proof - |
|
2876 |
have "shiftl1 w = shiftl1 (of_bl (to_bl w))" by simp |
|
2877 |
also have "\<dots> = of_bl (to_bl w @ [False])" by (rule shiftl1_of_bl) |
|
2878 |
finally show ?thesis . |
|
2879 |
qed |
|
2880 |
||
2881 |
lemma bl_shiftl1: |
|
2882 |
"to_bl (shiftl1 (w :: 'a :: len word)) = tl (to_bl w) @ [False]" |
|
2883 |
apply (simp add: shiftl1_bl word_rep_drop drop_Suc drop_Cons') |
|
2884 |
apply (fast intro!: Suc_leI) |
|
2885 |
done |
|
2886 |
||
2887 |
lemma shiftr1_bl: "shiftr1 w = of_bl (butlast (to_bl w))" |
|
2888 |
apply (unfold shiftr1_def uint_bl of_bl_def) |
|
2889 |
apply (simp add: butlast_rest_bin word_size) |
|
2890 |
apply (simp add: bin_rest_trunc [symmetric, unfolded One_nat_def]) |
|
2891 |
done |
|
2892 |
||
2893 |
lemma bl_shiftr1: |
|
2894 |
"to_bl (shiftr1 (w :: 'a :: len word)) = False # butlast (to_bl w)" |
|
2895 |
unfolding shiftr1_bl |
|
2896 |
by (simp add : word_rep_drop len_gt_0 [THEN Suc_leI]) |
|
2897 |
||
2898 |
||
2899 |
(* relate the two above : TODO - remove the :: len restriction on |
|
2900 |
this theorem and others depending on it *) |
|
2901 |
lemma shiftl1_rev: |
|
2902 |
"shiftl1 (w :: 'a :: len word) = word_reverse (shiftr1 (word_reverse w))" |
|
2903 |
apply (unfold word_reverse_def) |
|
2904 |
apply (rule word_bl.Rep_inverse' [symmetric]) |
|
2905 |
apply (simp add: bl_shiftl1 bl_shiftr1 word_bl.Abs_inverse) |
|
2906 |
apply (cases "to_bl w") |
|
2907 |
apply auto |
|
2908 |
done |
|
2909 |
||
2910 |
lemma shiftl_rev: |
|
2911 |
"shiftl (w :: 'a :: len word) n = word_reverse (shiftr (word_reverse w) n)" |
|
2912 |
apply (unfold shiftl_def shiftr_def) |
|
2913 |
apply (induct "n") |
|
2914 |
apply (auto simp add : shiftl1_rev) |
|
2915 |
done |
|
2916 |
||
2917 |
lemmas rev_shiftl = |
|
2918 |
shiftl_rev [where w = "word_reverse w", simplified, standard] |
|
2919 |
||
2920 |
lemmas shiftr_rev = rev_shiftl [THEN word_rev_gal', standard] |
|
2921 |
lemmas rev_shiftr = shiftl_rev [THEN word_rev_gal', standard] |
|
2922 |
||
2923 |
lemma bl_sshiftr1: |
|
2924 |
"to_bl (sshiftr1 (w :: 'a :: len word)) = hd (to_bl w) # butlast (to_bl w)" |
|
2925 |
apply (unfold sshiftr1_def uint_bl word_size) |
|
2926 |
apply (simp add: butlast_rest_bin word_ubin.eq_norm) |
|
2927 |
apply (simp add: sint_uint) |
|
2928 |
apply (rule nth_equalityI) |
|
2929 |
apply clarsimp |
|
2930 |
apply clarsimp |
|
2931 |
apply (case_tac i) |
|
2932 |
apply (simp_all add: hd_conv_nth length_0_conv [symmetric] |
|
2933 |
nth_bin_to_bl bin_nth.Suc [symmetric] |
|
2934 |
nth_sbintr |
|
2935 |
del: bin_nth.Suc) |
|
2936 |
apply force |
|
2937 |
apply (rule impI) |
|
2938 |
apply (rule_tac f = "bin_nth (uint w)" in arg_cong) |
|
2939 |
apply simp |
|
2940 |
done |
|
2941 |
||
2942 |
lemma drop_shiftr: |
|
2943 |
"drop n (to_bl ((w :: 'a :: len word) >> n)) = take (size w - n) (to_bl w)" |
|
2944 |
apply (unfold shiftr_def) |
|
2945 |
apply (induct n) |
|
2946 |
prefer 2 |
|
2947 |
apply (simp add: drop_Suc bl_shiftr1 butlast_drop [symmetric]) |
|
2948 |
apply (rule butlast_take [THEN trans]) |
|
2949 |
apply (auto simp: word_size) |
|
2950 |
done |
|
2951 |
||
2952 |
lemma drop_sshiftr: |
|
2953 |
"drop n (to_bl ((w :: 'a :: len word) >>> n)) = take (size w - n) (to_bl w)" |
|
2954 |
apply (unfold sshiftr_def) |
|
2955 |
apply (induct n) |
|
2956 |
prefer 2 |
|
2957 |
apply (simp add: drop_Suc bl_sshiftr1 butlast_drop [symmetric]) |
|
2958 |
apply (rule butlast_take [THEN trans]) |
|
2959 |
apply (auto simp: word_size) |
|
2960 |
done |
|
2961 |
||
2962 |
lemma take_shiftr [rule_format] : |
|
2963 |
"n <= size (w :: 'a :: len word) --> take n (to_bl (w >> n)) = |
|
2964 |
replicate n False" |
|
2965 |
apply (unfold shiftr_def) |
|
2966 |
apply (induct n) |
|
2967 |
prefer 2 |
|
2968 |
apply (simp add: bl_shiftr1) |
|
2969 |
apply (rule impI) |
|
2970 |
apply (rule take_butlast [THEN trans]) |
|
2971 |
apply (auto simp: word_size) |
|
2972 |
done |
|
2973 |
||
2974 |
lemma take_sshiftr' [rule_format] : |
|
2975 |
"n <= size (w :: 'a :: len word) --> hd (to_bl (w >>> n)) = hd (to_bl w) & |
|
2976 |
take n (to_bl (w >>> n)) = replicate n (hd (to_bl w))" |
|
2977 |
apply (unfold sshiftr_def) |
|
2978 |
apply (induct n) |
|
2979 |
prefer 2 |
|
2980 |
apply (simp add: bl_sshiftr1) |
|
2981 |
apply (rule impI) |
|
2982 |
apply (rule take_butlast [THEN trans]) |
|
2983 |
apply (auto simp: word_size) |
|
2984 |
done |
|
2985 |
||
2986 |
lemmas hd_sshiftr = take_sshiftr' [THEN conjunct1, standard] |
|
2987 |
lemmas take_sshiftr = take_sshiftr' [THEN conjunct2, standard] |
|
2988 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2989 |
lemma atd_lem: "take n xs = t \<Longrightarrow> drop n xs = d \<Longrightarrow> xs = t @ d" |
37660 | 2990 |
by (auto intro: append_take_drop_id [symmetric]) |
2991 |
||
2992 |
lemmas bl_shiftr = atd_lem [OF take_shiftr drop_shiftr] |
|
2993 |
lemmas bl_sshiftr = atd_lem [OF take_sshiftr drop_sshiftr] |
|
2994 |
||
2995 |
lemma shiftl_of_bl: "of_bl bl << n = of_bl (bl @ replicate n False)" |
|
2996 |
unfolding shiftl_def |
|
2997 |
by (induct n) (auto simp: shiftl1_of_bl replicate_app_Cons_same) |
|
2998 |
||
2999 |
lemma shiftl_bl: |
|
3000 |
"(w::'a::len0 word) << (n::nat) = of_bl (to_bl w @ replicate n False)" |
|
3001 |
proof - |
|
3002 |
have "w << n = of_bl (to_bl w) << n" by simp |
|
3003 |
also have "\<dots> = of_bl (to_bl w @ replicate n False)" by (rule shiftl_of_bl) |
|
3004 |
finally show ?thesis . |
|
3005 |
qed |
|
3006 |
||
3007 |
lemmas shiftl_number [simp] = shiftl_def [where w="number_of w", standard] |
|
3008 |
||
3009 |
lemma bl_shiftl: |
|
3010 |
"to_bl (w << n) = drop n (to_bl w) @ replicate (min (size w) n) False" |
|
3011 |
by (simp add: shiftl_bl word_rep_drop word_size) |
|
3012 |
||
3013 |
lemma shiftl_zero_size: |
|
3014 |
fixes x :: "'a::len0 word" |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3015 |
shows "size x <= n \<Longrightarrow> x << n = 0" |
37660 | 3016 |
apply (unfold word_size) |
3017 |
apply (rule word_eqI) |
|
3018 |
apply (clarsimp simp add: shiftl_bl word_size test_bit_of_bl nth_append) |
|
3019 |
done |
|
3020 |
||
3021 |
(* note - the following results use 'a :: len word < number_ring *) |
|
3022 |
||
3023 |
lemma shiftl1_2t: "shiftl1 (w :: 'a :: len word) = 2 * w" |
|
3024 |
apply (simp add: shiftl1_def_u) |
|
3025 |
apply (simp only: double_number_of_Bit0 [symmetric]) |
|
3026 |
apply simp |
|
3027 |
done |
|
3028 |
||
3029 |
lemma shiftl1_p: "shiftl1 (w :: 'a :: len word) = w + w" |
|
3030 |
apply (simp add: shiftl1_def_u) |
|
3031 |
apply (simp only: double_number_of_Bit0 [symmetric]) |
|
3032 |
apply simp |
|
3033 |
done |
|
3034 |
||
3035 |
lemma shiftl_t2n: "shiftl (w :: 'a :: len word) n = 2 ^ n * w" |
|
3036 |
unfolding shiftl_def |
|
41550 | 3037 |
by (induct n) (auto simp: shiftl1_2t) |
37660 | 3038 |
|
3039 |
lemma shiftr1_bintr [simp]: |
|
3040 |
"(shiftr1 (number_of w) :: 'a :: len0 word) = |
|
3041 |
number_of (bin_rest (bintrunc (len_of TYPE ('a)) w))" |
|
3042 |
unfolding shiftr1_def word_number_of_def |
|
3043 |
by (simp add : word_ubin.eq_norm) |
|
3044 |
||
3045 |
lemma sshiftr1_sbintr [simp] : |
|
3046 |
"(sshiftr1 (number_of w) :: 'a :: len word) = |
|
3047 |
number_of (bin_rest (sbintrunc (len_of TYPE ('a) - 1) w))" |
|
3048 |
unfolding sshiftr1_def word_number_of_def |
|
3049 |
by (simp add : word_sbin.eq_norm) |
|
3050 |
||
3051 |
lemma shiftr_no': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3052 |
"w = number_of bin \<Longrightarrow> |
37660 | 3053 |
(w::'a::len0 word) >> n = number_of ((bin_rest ^^ n) (bintrunc (size w) bin))" |
3054 |
apply clarsimp |
|
3055 |
apply (rule word_eqI) |
|
3056 |
apply (auto simp: nth_shiftr nth_rest_power_bin nth_bintr word_size) |
|
3057 |
done |
|
3058 |
||
3059 |
lemma sshiftr_no': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3060 |
"w = number_of bin \<Longrightarrow> w >>> n = number_of ((bin_rest ^^ n) |
37660 | 3061 |
(sbintrunc (size w - 1) bin))" |
3062 |
apply clarsimp |
|
3063 |
apply (rule word_eqI) |
|
3064 |
apply (auto simp: nth_sshiftr nth_rest_power_bin nth_sbintr word_size) |
|
3065 |
apply (subgoal_tac "na + n = len_of TYPE('a) - Suc 0", simp, simp)+ |
|
3066 |
done |
|
3067 |
||
3068 |
lemmas sshiftr_no [simp] = |
|
3069 |
sshiftr_no' [where w = "number_of w", OF refl, unfolded word_size, standard] |
|
3070 |
||
3071 |
lemmas shiftr_no [simp] = |
|
3072 |
shiftr_no' [where w = "number_of w", OF refl, unfolded word_size, standard] |
|
3073 |
||
3074 |
lemma shiftr1_bl_of': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3075 |
"us = shiftr1 (of_bl bl) \<Longrightarrow> length bl <= size us \<Longrightarrow> |
37660 | 3076 |
us = of_bl (butlast bl)" |
3077 |
by (clarsimp simp: shiftr1_def of_bl_def word_size butlast_rest_bl2bin |
|
3078 |
word_ubin.eq_norm trunc_bl2bin) |
|
3079 |
||
3080 |
lemmas shiftr1_bl_of = refl [THEN shiftr1_bl_of', unfolded word_size] |
|
3081 |
||
3082 |
lemma shiftr_bl_of' [rule_format]: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3083 |
"us = of_bl bl >> n \<Longrightarrow> length bl <= size us --> |
37660 | 3084 |
us = of_bl (take (length bl - n) bl)" |
3085 |
apply (unfold shiftr_def) |
|
3086 |
apply hypsubst |
|
3087 |
apply (unfold word_size) |
|
3088 |
apply (induct n) |
|
3089 |
apply clarsimp |
|
3090 |
apply clarsimp |
|
3091 |
apply (subst shiftr1_bl_of) |
|
3092 |
apply simp |
|
3093 |
apply (simp add: butlast_take) |
|
3094 |
done |
|
3095 |
||
3096 |
lemmas shiftr_bl_of = refl [THEN shiftr_bl_of', unfolded word_size] |
|
3097 |
||
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
3098 |
lemmas shiftr_bl = word_bl_Rep' [THEN eq_imp_le, THEN shiftr_bl_of, |
37660 | 3099 |
simplified word_size, simplified, THEN eq_reflection, standard] |
3100 |
||
3101 |
lemma msb_shift': "msb (w::'a::len word) <-> (w >> (size w - 1)) ~= 0" |
|
3102 |
apply (unfold shiftr_bl word_msb_alt) |
|
3103 |
apply (simp add: word_size Suc_le_eq take_Suc) |
|
3104 |
apply (cases "hd (to_bl w)") |
|
3105 |
apply (auto simp: word_1_bl word_0_bl |
|
3106 |
of_bl_rep_False [where n=1 and bs="[]", simplified]) |
|
3107 |
done |
|
3108 |
||
3109 |
lemmas msb_shift = msb_shift' [unfolded word_size] |
|
3110 |
||
3111 |
lemma align_lem_or [rule_format] : |
|
3112 |
"ALL x m. length x = n + m --> length y = n + m --> |
|
3113 |
drop m x = replicate n False --> take m y = replicate m False --> |
|
3114 |
map2 op | x y = take m x @ drop m y" |
|
3115 |
apply (induct_tac y) |
|
3116 |
apply force |
|
3117 |
apply clarsimp |
|
3118 |
apply (case_tac x, force) |
|
3119 |
apply (case_tac m, auto) |
|
3120 |
apply (drule sym) |
|
3121 |
apply auto |
|
3122 |
apply (induct_tac list, auto) |
|
3123 |
done |
|
3124 |
||
3125 |
lemma align_lem_and [rule_format] : |
|
3126 |
"ALL x m. length x = n + m --> length y = n + m --> |
|
3127 |
drop m x = replicate n False --> take m y = replicate m False --> |
|
3128 |
map2 op & x y = replicate (n + m) False" |
|
3129 |
apply (induct_tac y) |
|
3130 |
apply force |
|
3131 |
apply clarsimp |
|
3132 |
apply (case_tac x, force) |
|
3133 |
apply (case_tac m, auto) |
|
3134 |
apply (drule sym) |
|
3135 |
apply auto |
|
3136 |
apply (induct_tac list, auto) |
|
3137 |
done |
|
3138 |
||
3139 |
lemma aligned_bl_add_size': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3140 |
"size x - n = m \<Longrightarrow> n <= size x \<Longrightarrow> drop m (to_bl x) = replicate n False \<Longrightarrow> |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3141 |
take m (to_bl y) = replicate m False \<Longrightarrow> |
37660 | 3142 |
to_bl (x + y) = take m (to_bl x) @ drop m (to_bl y)" |
3143 |
apply (subgoal_tac "x AND y = 0") |
|
3144 |
prefer 2 |
|
3145 |
apply (rule word_bl.Rep_eqD) |
|
3146 |
apply (simp add: bl_word_and to_bl_0) |
|
3147 |
apply (rule align_lem_and [THEN trans]) |
|
3148 |
apply (simp_all add: word_size)[5] |
|
3149 |
apply simp |
|
3150 |
apply (subst word_plus_and_or [symmetric]) |
|
3151 |
apply (simp add : bl_word_or) |
|
3152 |
apply (rule align_lem_or) |
|
3153 |
apply (simp_all add: word_size) |
|
3154 |
done |
|
3155 |
||
3156 |
lemmas aligned_bl_add_size = refl [THEN aligned_bl_add_size'] |
|
3157 |
||
3158 |
subsubsection "Mask" |
|
3159 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3160 |
lemma nth_mask': "m = mask n \<Longrightarrow> test_bit m i = (i < n & i < size m)" |
37660 | 3161 |
apply (unfold mask_def test_bit_bl) |
3162 |
apply (simp only: word_1_bl [symmetric] shiftl_of_bl) |
|
3163 |
apply (clarsimp simp add: word_size) |
|
3164 |
apply (simp only: of_bl_no mask_lem number_of_succ add_diff_cancel2) |
|
3165 |
apply (fold of_bl_no) |
|
3166 |
apply (simp add: word_1_bl) |
|
3167 |
apply (rule test_bit_of_bl [THEN trans, unfolded test_bit_bl word_size]) |
|
3168 |
apply auto |
|
3169 |
done |
|
3170 |
||
3171 |
lemmas nth_mask [simp] = refl [THEN nth_mask'] |
|
3172 |
||
3173 |
lemma mask_bl: "mask n = of_bl (replicate n True)" |
|
3174 |
by (auto simp add : test_bit_of_bl word_size intro: word_eqI) |
|
3175 |
||
3176 |
lemma mask_bin: "mask n = number_of (bintrunc n Int.Min)" |
|
3177 |
by (auto simp add: nth_bintr word_size intro: word_eqI) |
|
3178 |
||
3179 |
lemma and_mask_bintr: "w AND mask n = number_of (bintrunc n (uint w))" |
|
3180 |
apply (rule word_eqI) |
|
3181 |
apply (simp add: nth_bintr word_size word_ops_nth_size) |
|
3182 |
apply (auto simp add: test_bit_bin) |
|
3183 |
done |
|
3184 |
||
3185 |
lemma and_mask_no: "number_of i AND mask n = number_of (bintrunc n i)" |
|
3186 |
by (auto simp add : nth_bintr word_size word_ops_nth_size intro: word_eqI) |
|
3187 |
||
3188 |
lemmas and_mask_wi = and_mask_no [unfolded word_number_of_def] |
|
3189 |
||
3190 |
lemma bl_and_mask': |
|
3191 |
"to_bl (w AND mask n :: 'a :: len word) = |
|
3192 |
replicate (len_of TYPE('a) - n) False @ |
|
3193 |
drop (len_of TYPE('a) - n) (to_bl w)" |
|
3194 |
apply (rule nth_equalityI) |
|
3195 |
apply simp |
|
3196 |
apply (clarsimp simp add: to_bl_nth word_size) |
|
3197 |
apply (simp add: word_size word_ops_nth_size) |
|
3198 |
apply (auto simp add: word_size test_bit_bl nth_append nth_rev) |
|
3199 |
done |
|
3200 |
||
3201 |
lemmas and_mask_mod_2p = |
|
3202 |
and_mask_bintr [unfolded word_number_of_alt no_bintr_alt] |
|
3203 |
||
3204 |
lemma and_mask_lt_2p: "uint (w AND mask n) < 2 ^ n" |
|
3205 |
apply (simp add : and_mask_bintr no_bintr_alt) |
|
3206 |
apply (rule xtr8) |
|
3207 |
prefer 2 |
|
3208 |
apply (rule pos_mod_bound) |
|
3209 |
apply auto |
|
3210 |
done |
|
3211 |
||
3212 |
lemmas eq_mod_iff = trans [symmetric, OF int_mod_lem eq_sym_conv] |
|
3213 |
||
3214 |
lemma mask_eq_iff: "(w AND mask n) = w <-> uint w < 2 ^ n" |
|
3215 |
apply (simp add: and_mask_bintr word_number_of_def) |
|
3216 |
apply (simp add: word_ubin.inverse_norm) |
|
3217 |
apply (simp add: eq_mod_iff bintrunc_mod2p min_def) |
|
3218 |
apply (fast intro!: lt2p_lem) |
|
3219 |
done |
|
3220 |
||
3221 |
lemma and_mask_dvd: "2 ^ n dvd uint w = (w AND mask n = 0)" |
|
3222 |
apply (simp add: dvd_eq_mod_eq_0 and_mask_mod_2p) |
|
3223 |
apply (simp add: word_uint.norm_eq_iff [symmetric] word_of_int_homs) |
|
3224 |
apply (subst word_uint.norm_Rep [symmetric]) |
|
3225 |
apply (simp only: bintrunc_bintrunc_min bintrunc_mod2p [symmetric] min_def) |
|
3226 |
apply auto |
|
3227 |
done |
|
3228 |
||
3229 |
lemma and_mask_dvd_nat: "2 ^ n dvd unat w = (w AND mask n = 0)" |
|
3230 |
apply (unfold unat_def) |
|
3231 |
apply (rule trans [OF _ and_mask_dvd]) |
|
3232 |
apply (unfold dvd_def) |
|
3233 |
apply auto |
|
3234 |
apply (drule uint_ge_0 [THEN nat_int.Abs_inverse' [simplified], symmetric]) |
|
3235 |
apply (simp add : int_mult int_power) |
|
3236 |
apply (simp add : nat_mult_distrib nat_power_eq) |
|
3237 |
done |
|
3238 |
||
3239 |
lemma word_2p_lem: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3240 |
"n < size w \<Longrightarrow> w < 2 ^ n = (uint (w :: 'a :: len word) < 2 ^ n)" |
37660 | 3241 |
apply (unfold word_size word_less_alt word_number_of_alt) |
3242 |
apply (clarsimp simp add: word_of_int_power_hom word_uint.eq_norm |
|
3243 |
int_mod_eq' |
|
3244 |
simp del: word_of_int_bin) |
|
3245 |
done |
|
3246 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3247 |
lemma less_mask_eq: "x < 2 ^ n \<Longrightarrow> x AND mask n = (x :: 'a :: len word)" |
37660 | 3248 |
apply (unfold word_less_alt word_number_of_alt) |
3249 |
apply (clarsimp simp add: and_mask_mod_2p word_of_int_power_hom |
|
3250 |
word_uint.eq_norm |
|
3251 |
simp del: word_of_int_bin) |
|
3252 |
apply (drule xtr8 [rotated]) |
|
3253 |
apply (rule int_mod_le) |
|
3254 |
apply (auto simp add : mod_pos_pos_trivial) |
|
3255 |
done |
|
3256 |
||
3257 |
lemmas mask_eq_iff_w2p = |
|
3258 |
trans [OF mask_eq_iff word_2p_lem [symmetric], standard] |
|
3259 |
||
3260 |
lemmas and_mask_less' = |
|
3261 |
iffD2 [OF word_2p_lem and_mask_lt_2p, simplified word_size, standard] |
|
3262 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3263 |
lemma and_mask_less_size: "n < size x \<Longrightarrow> x AND mask n < 2^n" |
37660 | 3264 |
unfolding word_size by (erule and_mask_less') |
3265 |
||
3266 |
lemma word_mod_2p_is_mask': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3267 |
"c = 2 ^ n \<Longrightarrow> c > 0 \<Longrightarrow> x mod c = (x :: 'a :: len word) AND mask n" |
37660 | 3268 |
by (clarsimp simp add: word_mod_def uint_2p and_mask_mod_2p) |
3269 |
||
3270 |
lemmas word_mod_2p_is_mask = refl [THEN word_mod_2p_is_mask'] |
|
3271 |
||
3272 |
lemma mask_eqs: |
|
3273 |
"(a AND mask n) + b AND mask n = a + b AND mask n" |
|
3274 |
"a + (b AND mask n) AND mask n = a + b AND mask n" |
|
3275 |
"(a AND mask n) - b AND mask n = a - b AND mask n" |
|
3276 |
"a - (b AND mask n) AND mask n = a - b AND mask n" |
|
3277 |
"a * (b AND mask n) AND mask n = a * b AND mask n" |
|
3278 |
"(b AND mask n) * a AND mask n = b * a AND mask n" |
|
3279 |
"(a AND mask n) + (b AND mask n) AND mask n = a + b AND mask n" |
|
3280 |
"(a AND mask n) - (b AND mask n) AND mask n = a - b AND mask n" |
|
3281 |
"(a AND mask n) * (b AND mask n) AND mask n = a * b AND mask n" |
|
3282 |
"- (a AND mask n) AND mask n = - a AND mask n" |
|
3283 |
"word_succ (a AND mask n) AND mask n = word_succ a AND mask n" |
|
3284 |
"word_pred (a AND mask n) AND mask n = word_pred a AND mask n" |
|
3285 |
using word_of_int_Ex [where x=a] word_of_int_Ex [where x=b] |
|
3286 |
by (auto simp: and_mask_wi bintr_ariths bintr_arith1s new_word_of_int_homs) |
|
3287 |
||
3288 |
lemma mask_power_eq: |
|
3289 |
"(x AND mask n) ^ k AND mask n = x ^ k AND mask n" |
|
3290 |
using word_of_int_Ex [where x=x] |
|
3291 |
by (clarsimp simp: and_mask_wi word_of_int_power_hom bintr_ariths) |
|
3292 |
||
3293 |
||
3294 |
subsubsection "Revcast" |
|
3295 |
||
3296 |
lemmas revcast_def' = revcast_def [simplified] |
|
3297 |
lemmas revcast_def'' = revcast_def' [simplified word_size] |
|
3298 |
lemmas revcast_no_def [simp] = |
|
3299 |
revcast_def' [where w="number_of w", unfolded word_size, standard] |
|
3300 |
||
3301 |
lemma to_bl_revcast: |
|
3302 |
"to_bl (revcast w :: 'a :: len0 word) = |
|
3303 |
takefill False (len_of TYPE ('a)) (to_bl w)" |
|
3304 |
apply (unfold revcast_def' word_size) |
|
3305 |
apply (rule word_bl.Abs_inverse) |
|
3306 |
apply simp |
|
3307 |
done |
|
3308 |
||
3309 |
lemma revcast_rev_ucast': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3310 |
"cs = [rc, uc] \<Longrightarrow> rc = revcast (word_reverse w) \<Longrightarrow> uc = ucast w \<Longrightarrow> |
37660 | 3311 |
rc = word_reverse uc" |
3312 |
apply (unfold ucast_def revcast_def' Let_def word_reverse_def) |
|
3313 |
apply (clarsimp simp add : to_bl_of_bin takefill_bintrunc) |
|
3314 |
apply (simp add : word_bl.Abs_inverse word_size) |
|
3315 |
done |
|
3316 |
||
3317 |
lemmas revcast_rev_ucast = revcast_rev_ucast' [OF refl refl refl] |
|
3318 |
||
3319 |
lemmas revcast_ucast = revcast_rev_ucast |
|
3320 |
[where w = "word_reverse w", simplified word_rev_rev, standard] |
|
3321 |
||
3322 |
lemmas ucast_revcast = revcast_rev_ucast [THEN word_rev_gal', standard] |
|
3323 |
lemmas ucast_rev_revcast = revcast_ucast [THEN word_rev_gal', standard] |
|
3324 |
||
3325 |
||
3326 |
-- "linking revcast and cast via shift" |
|
3327 |
||
3328 |
lemmas wsst_TYs = source_size target_size word_size |
|
3329 |
||
3330 |
lemma revcast_down_uu': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3331 |
"rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> |
37660 | 3332 |
rc (w :: 'a :: len word) = ucast (w >> n)" |
3333 |
apply (simp add: revcast_def') |
|
3334 |
apply (rule word_bl.Rep_inverse') |
|
3335 |
apply (rule trans, rule ucast_down_drop) |
|
3336 |
prefer 2 |
|
3337 |
apply (rule trans, rule drop_shiftr) |
|
3338 |
apply (auto simp: takefill_alt wsst_TYs) |
|
3339 |
done |
|
3340 |
||
3341 |
lemma revcast_down_us': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3342 |
"rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> |
37660 | 3343 |
rc (w :: 'a :: len word) = ucast (w >>> n)" |
3344 |
apply (simp add: revcast_def') |
|
3345 |
apply (rule word_bl.Rep_inverse') |
|
3346 |
apply (rule trans, rule ucast_down_drop) |
|
3347 |
prefer 2 |
|
3348 |
apply (rule trans, rule drop_sshiftr) |
|
3349 |
apply (auto simp: takefill_alt wsst_TYs) |
|
3350 |
done |
|
3351 |
||
3352 |
lemma revcast_down_su': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3353 |
"rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> |
37660 | 3354 |
rc (w :: 'a :: len word) = scast (w >> n)" |
3355 |
apply (simp add: revcast_def') |
|
3356 |
apply (rule word_bl.Rep_inverse') |
|
3357 |
apply (rule trans, rule scast_down_drop) |
|
3358 |
prefer 2 |
|
3359 |
apply (rule trans, rule drop_shiftr) |
|
3360 |
apply (auto simp: takefill_alt wsst_TYs) |
|
3361 |
done |
|
3362 |
||
3363 |
lemma revcast_down_ss': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3364 |
"rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> |
37660 | 3365 |
rc (w :: 'a :: len word) = scast (w >>> n)" |
3366 |
apply (simp add: revcast_def') |
|
3367 |
apply (rule word_bl.Rep_inverse') |
|
3368 |
apply (rule trans, rule scast_down_drop) |
|
3369 |
prefer 2 |
|
3370 |
apply (rule trans, rule drop_sshiftr) |
|
3371 |
apply (auto simp: takefill_alt wsst_TYs) |
|
3372 |
done |
|
3373 |
||
3374 |
lemmas revcast_down_uu = refl [THEN revcast_down_uu'] |
|
3375 |
lemmas revcast_down_us = refl [THEN revcast_down_us'] |
|
3376 |
lemmas revcast_down_su = refl [THEN revcast_down_su'] |
|
3377 |
lemmas revcast_down_ss = refl [THEN revcast_down_ss'] |
|
3378 |
||
3379 |
lemma cast_down_rev: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3380 |
"uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow> |
37660 | 3381 |
uc w = revcast ((w :: 'a :: len word) << n)" |
3382 |
apply (unfold shiftl_rev) |
|
3383 |
apply clarify |
|
3384 |
apply (simp add: revcast_rev_ucast) |
|
3385 |
apply (rule word_rev_gal') |
|
3386 |
apply (rule trans [OF _ revcast_rev_ucast]) |
|
3387 |
apply (rule revcast_down_uu [symmetric]) |
|
3388 |
apply (auto simp add: wsst_TYs) |
|
3389 |
done |
|
3390 |
||
3391 |
lemma revcast_up': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3392 |
"rc = revcast \<Longrightarrow> source_size rc + n = target_size rc \<Longrightarrow> |
37660 | 3393 |
rc w = (ucast w :: 'a :: len word) << n" |
3394 |
apply (simp add: revcast_def') |
|
3395 |
apply (rule word_bl.Rep_inverse') |
|
3396 |
apply (simp add: takefill_alt) |
|
3397 |
apply (rule bl_shiftl [THEN trans]) |
|
3398 |
apply (subst ucast_up_app) |
|
3399 |
apply (auto simp add: wsst_TYs) |
|
3400 |
done |
|
3401 |
||
3402 |
lemmas revcast_up = refl [THEN revcast_up'] |
|
3403 |
||
3404 |
lemmas rc1 = revcast_up [THEN |
|
3405 |
revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]] |
|
3406 |
lemmas rc2 = revcast_down_uu [THEN |
|
3407 |
revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]] |
|
3408 |
||
3409 |
lemmas ucast_up = |
|
3410 |
rc1 [simplified rev_shiftr [symmetric] revcast_ucast [symmetric]] |
|
3411 |
lemmas ucast_down = |
|
3412 |
rc2 [simplified rev_shiftr revcast_ucast [symmetric]] |
|
3413 |
||
3414 |
||
3415 |
subsubsection "Slices" |
|
3416 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3417 |
lemma slice1_no_bin [simp]: |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3418 |
"slice1 n (number_of w :: 'b word) = of_bl (takefill False n (bin_to_bl (len_of TYPE('b :: len0)) w))" |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3419 |
by (simp add: slice1_def) |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3420 |
|
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3421 |
lemma slice_no_bin [simp]: |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3422 |
"slice n (number_of w :: 'b word) = of_bl (takefill False (len_of TYPE('b :: len0) - n) |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3423 |
(bin_to_bl (len_of TYPE('b :: len0)) w))" |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3424 |
by (simp add: slice_def word_size) |
37660 | 3425 |
|
3426 |
lemma slice1_0 [simp] : "slice1 n 0 = 0" |
|
3427 |
unfolding slice1_def by (simp add : to_bl_0) |
|
3428 |
||
3429 |
lemma slice_0 [simp] : "slice n 0 = 0" |
|
3430 |
unfolding slice_def by auto |
|
3431 |
||
3432 |
lemma slice_take': "slice n w = of_bl (take (size w - n) (to_bl w))" |
|
3433 |
unfolding slice_def' slice1_def |
|
3434 |
by (simp add : takefill_alt word_size) |
|
3435 |
||
3436 |
lemmas slice_take = slice_take' [unfolded word_size] |
|
3437 |
||
3438 |
-- "shiftr to a word of the same size is just slice, |
|
3439 |
slice is just shiftr then ucast" |
|
3440 |
lemmas shiftr_slice = trans |
|
3441 |
[OF shiftr_bl [THEN meta_eq_to_obj_eq] slice_take [symmetric], standard] |
|
3442 |
||
3443 |
lemma slice_shiftr: "slice n w = ucast (w >> n)" |
|
3444 |
apply (unfold slice_take shiftr_bl) |
|
3445 |
apply (rule ucast_of_bl_up [symmetric]) |
|
3446 |
apply (simp add: word_size) |
|
3447 |
done |
|
3448 |
||
3449 |
lemma nth_slice: |
|
3450 |
"(slice n w :: 'a :: len0 word) !! m = |
|
3451 |
(w !! (m + n) & m < len_of TYPE ('a))" |
|
3452 |
unfolding slice_shiftr |
|
3453 |
by (simp add : nth_ucast nth_shiftr) |
|
3454 |
||
3455 |
lemma slice1_down_alt': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3456 |
"sl = slice1 n w \<Longrightarrow> fs = size sl \<Longrightarrow> fs + k = n \<Longrightarrow> |
37660 | 3457 |
to_bl sl = takefill False fs (drop k (to_bl w))" |
3458 |
unfolding slice1_def word_size of_bl_def uint_bl |
|
3459 |
by (clarsimp simp: word_ubin.eq_norm bl_bin_bl_rep_drop drop_takefill) |
|
3460 |
||
3461 |
lemma slice1_up_alt': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3462 |
"sl = slice1 n w \<Longrightarrow> fs = size sl \<Longrightarrow> fs = n + k \<Longrightarrow> |
37660 | 3463 |
to_bl sl = takefill False fs (replicate k False @ (to_bl w))" |
3464 |
apply (unfold slice1_def word_size of_bl_def uint_bl) |
|
3465 |
apply (clarsimp simp: word_ubin.eq_norm bl_bin_bl_rep_drop |
|
3466 |
takefill_append [symmetric]) |
|
3467 |
apply (rule_tac f = "%k. takefill False (len_of TYPE('a)) |
|
3468 |
(replicate k False @ bin_to_bl (len_of TYPE('b)) (uint w))" in arg_cong) |
|
3469 |
apply arith |
|
3470 |
done |
|
3471 |
||
3472 |
lemmas sd1 = slice1_down_alt' [OF refl refl, unfolded word_size] |
|
3473 |
lemmas su1 = slice1_up_alt' [OF refl refl, unfolded word_size] |
|
3474 |
lemmas slice1_down_alt = le_add_diff_inverse [THEN sd1] |
|
3475 |
lemmas slice1_up_alts = |
|
3476 |
le_add_diff_inverse [symmetric, THEN su1] |
|
3477 |
le_add_diff_inverse2 [symmetric, THEN su1] |
|
3478 |
||
3479 |
lemma ucast_slice1: "ucast w = slice1 (size w) w" |
|
3480 |
unfolding slice1_def ucast_bl |
|
3481 |
by (simp add : takefill_same' word_size) |
|
3482 |
||
3483 |
lemma ucast_slice: "ucast w = slice 0 w" |
|
3484 |
unfolding slice_def by (simp add : ucast_slice1) |
|
3485 |
||
3486 |
lemmas slice_id = trans [OF ucast_slice [symmetric] ucast_id] |
|
3487 |
||
3488 |
lemma revcast_slice1': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3489 |
"rc = revcast w \<Longrightarrow> slice1 (size rc) w = rc" |
37660 | 3490 |
unfolding slice1_def revcast_def' by (simp add : word_size) |
3491 |
||
3492 |
lemmas revcast_slice1 = refl [THEN revcast_slice1'] |
|
3493 |
||
3494 |
lemma slice1_tf_tf': |
|
3495 |
"to_bl (slice1 n w :: 'a :: len0 word) = |
|
3496 |
rev (takefill False (len_of TYPE('a)) (rev (takefill False n (to_bl w))))" |
|
3497 |
unfolding slice1_def by (rule word_rev_tf) |
|
3498 |
||
3499 |
lemmas slice1_tf_tf = slice1_tf_tf' |
|
3500 |
[THEN word_bl.Rep_inverse', symmetric, standard] |
|
3501 |
||
3502 |
lemma rev_slice1: |
|
3503 |
"n + k = len_of TYPE('a) + len_of TYPE('b) \<Longrightarrow> |
|
3504 |
slice1 n (word_reverse w :: 'b :: len0 word) = |
|
3505 |
word_reverse (slice1 k w :: 'a :: len0 word)" |
|
3506 |
apply (unfold word_reverse_def slice1_tf_tf) |
|
3507 |
apply (rule word_bl.Rep_inverse') |
|
3508 |
apply (rule rev_swap [THEN iffD1]) |
|
3509 |
apply (rule trans [symmetric]) |
|
3510 |
apply (rule tf_rev) |
|
3511 |
apply (simp add: word_bl.Abs_inverse) |
|
3512 |
apply (simp add: word_bl.Abs_inverse) |
|
3513 |
done |
|
3514 |
||
3515 |
lemma rev_slice': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3516 |
"res = slice n (word_reverse w) \<Longrightarrow> n + k + size res = size w \<Longrightarrow> |
37660 | 3517 |
res = word_reverse (slice k w)" |
3518 |
apply (unfold slice_def word_size) |
|
3519 |
apply clarify |
|
3520 |
apply (rule rev_slice1) |
|
3521 |
apply arith |
|
3522 |
done |
|
3523 |
||
3524 |
lemmas rev_slice = refl [THEN rev_slice', unfolded word_size] |
|
3525 |
||
3526 |
lemmas sym_notr = |
|
3527 |
not_iff [THEN iffD2, THEN not_sym, THEN not_iff [THEN iffD1]] |
|
3528 |
||
3529 |
-- {* problem posed by TPHOLs referee: |
|
3530 |
criterion for overflow of addition of signed integers *} |
|
3531 |
||
3532 |
lemma sofl_test: |
|
3533 |
"(sint (x :: 'a :: len word) + sint y = sint (x + y)) = |
|
3534 |
((((x+y) XOR x) AND ((x+y) XOR y)) >> (size x - 1) = 0)" |
|
3535 |
apply (unfold word_size) |
|
3536 |
apply (cases "len_of TYPE('a)", simp) |
|
3537 |
apply (subst msb_shift [THEN sym_notr]) |
|
3538 |
apply (simp add: word_ops_msb) |
|
3539 |
apply (simp add: word_msb_sint) |
|
3540 |
apply safe |
|
3541 |
apply simp_all |
|
3542 |
apply (unfold sint_word_ariths) |
|
3543 |
apply (unfold word_sbin.set_iff_norm [symmetric] sints_num) |
|
3544 |
apply safe |
|
3545 |
apply (insert sint_range' [where x=x]) |
|
3546 |
apply (insert sint_range' [where x=y]) |
|
3547 |
defer |
|
3548 |
apply (simp (no_asm), arith) |
|
3549 |
apply (simp (no_asm), arith) |
|
3550 |
defer |
|
3551 |
defer |
|
3552 |
apply (simp (no_asm), arith) |
|
3553 |
apply (simp (no_asm), arith) |
|
3554 |
apply (rule notI [THEN notnotD], |
|
3555 |
drule leI not_leE, |
|
3556 |
drule sbintrunc_inc sbintrunc_dec, |
|
3557 |
simp)+ |
|
3558 |
done |
|
3559 |
||
3560 |
||
3561 |
subsection "Split and cat" |
|
3562 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3563 |
lemmas word_split_bin' = word_split_def |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3564 |
lemmas word_cat_bin' = word_cat_def |
37660 | 3565 |
|
3566 |
lemma word_rsplit_no: |
|
3567 |
"(word_rsplit (number_of bin :: 'b :: len0 word) :: 'a word list) = |
|
3568 |
map number_of (bin_rsplit (len_of TYPE('a :: len)) |
|
3569 |
(len_of TYPE('b), bintrunc (len_of TYPE('b)) bin))" |
|
3570 |
apply (unfold word_rsplit_def word_no_wi) |
|
3571 |
apply (simp add: word_ubin.eq_norm) |
|
3572 |
done |
|
3573 |
||
3574 |
lemmas word_rsplit_no_cl [simp] = word_rsplit_no |
|
3575 |
[unfolded bin_rsplitl_def bin_rsplit_l [symmetric]] |
|
3576 |
||
3577 |
lemma test_bit_cat: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3578 |
"wc = word_cat a b \<Longrightarrow> wc !! n = (n < size wc & |
37660 | 3579 |
(if n < size b then b !! n else a !! (n - size b)))" |
3580 |
apply (unfold word_cat_bin' test_bit_bin) |
|
3581 |
apply (auto simp add : word_ubin.eq_norm nth_bintr bin_nth_cat word_size) |
|
3582 |
apply (erule bin_nth_uint_imp) |
|
3583 |
done |
|
3584 |
||
3585 |
lemma word_cat_bl: "word_cat a b = of_bl (to_bl a @ to_bl b)" |
|
3586 |
apply (unfold of_bl_def to_bl_def word_cat_bin') |
|
3587 |
apply (simp add: bl_to_bin_app_cat) |
|
3588 |
done |
|
3589 |
||
3590 |
lemma of_bl_append: |
|
3591 |
"(of_bl (xs @ ys) :: 'a :: len word) = of_bl xs * 2^(length ys) + of_bl ys" |
|
3592 |
apply (unfold of_bl_def) |
|
3593 |
apply (simp add: bl_to_bin_app_cat bin_cat_num) |
|
3594 |
apply (simp add: word_of_int_power_hom [symmetric] new_word_of_int_hom_syms) |
|
3595 |
done |
|
3596 |
||
3597 |
lemma of_bl_False [simp]: |
|
3598 |
"of_bl (False#xs) = of_bl xs" |
|
3599 |
by (rule word_eqI) |
|
3600 |
(auto simp add: test_bit_of_bl nth_append) |
|
3601 |
||
3602 |
lemma of_bl_True: |
|
3603 |
"(of_bl (True#xs)::'a::len word) = 2^length xs + of_bl xs" |
|
3604 |
by (subst of_bl_append [where xs="[True]", simplified]) |
|
3605 |
(simp add: word_1_bl) |
|
3606 |
||
3607 |
lemma of_bl_Cons: |
|
3608 |
"of_bl (x#xs) = of_bool x * 2^length xs + of_bl xs" |
|
3609 |
by (cases x) (simp_all add: of_bl_True) |
|
3610 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3611 |
lemma split_uint_lem: "bin_split n (uint (w :: 'a :: len0 word)) = (a, b) \<Longrightarrow> |
37660 | 3612 |
a = bintrunc (len_of TYPE('a) - n) a & b = bintrunc (len_of TYPE('a)) b" |
3613 |
apply (frule word_ubin.norm_Rep [THEN ssubst]) |
|
3614 |
apply (drule bin_split_trunc1) |
|
3615 |
apply (drule sym [THEN trans]) |
|
3616 |
apply assumption |
|
3617 |
apply safe |
|
3618 |
done |
|
3619 |
||
3620 |
lemma word_split_bl': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3621 |
"std = size c - size b \<Longrightarrow> (word_split c = (a, b)) \<Longrightarrow> |
37660 | 3622 |
(a = of_bl (take std (to_bl c)) & b = of_bl (drop std (to_bl c)))" |
3623 |
apply (unfold word_split_bin') |
|
3624 |
apply safe |
|
3625 |
defer |
|
3626 |
apply (clarsimp split: prod.splits) |
|
3627 |
apply (drule word_ubin.norm_Rep [THEN ssubst]) |
|
3628 |
apply (drule split_bintrunc) |
|
3629 |
apply (simp add : of_bl_def bl2bin_drop word_size |
|
3630 |
word_ubin.norm_eq_iff [symmetric] min_def del : word_ubin.norm_Rep) |
|
3631 |
apply (clarsimp split: prod.splits) |
|
3632 |
apply (frule split_uint_lem [THEN conjunct1]) |
|
3633 |
apply (unfold word_size) |
|
3634 |
apply (cases "len_of TYPE('a) >= len_of TYPE('b)") |
|
3635 |
defer |
|
3636 |
apply (simp add: word_0_bl word_0_wi_Pls) |
|
3637 |
apply (simp add : of_bl_def to_bl_def) |
|
3638 |
apply (subst bin_split_take1 [symmetric]) |
|
3639 |
prefer 2 |
|
3640 |
apply assumption |
|
3641 |
apply simp |
|
3642 |
apply (erule thin_rl) |
|
3643 |
apply (erule arg_cong [THEN trans]) |
|
3644 |
apply (simp add : word_ubin.norm_eq_iff [symmetric]) |
|
3645 |
done |
|
3646 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3647 |
lemma word_split_bl: "std = size c - size b \<Longrightarrow> |
37660 | 3648 |
(a = of_bl (take std (to_bl c)) & b = of_bl (drop std (to_bl c))) <-> |
3649 |
word_split c = (a, b)" |
|
3650 |
apply (rule iffI) |
|
3651 |
defer |
|
3652 |
apply (erule (1) word_split_bl') |
|
3653 |
apply (case_tac "word_split c") |
|
3654 |
apply (auto simp add : word_size) |
|
3655 |
apply (frule word_split_bl' [rotated]) |
|
3656 |
apply (auto simp add : word_size) |
|
3657 |
done |
|
3658 |
||
3659 |
lemma word_split_bl_eq: |
|
3660 |
"(word_split (c::'a::len word) :: ('c :: len0 word * 'd :: len0 word)) = |
|
3661 |
(of_bl (take (len_of TYPE('a::len) - len_of TYPE('d::len0)) (to_bl c)), |
|
3662 |
of_bl (drop (len_of TYPE('a) - len_of TYPE('d)) (to_bl c)))" |
|
3663 |
apply (rule word_split_bl [THEN iffD1]) |
|
3664 |
apply (unfold word_size) |
|
3665 |
apply (rule refl conjI)+ |
|
3666 |
done |
|
3667 |
||
3668 |
-- "keep quantifiers for use in simplification" |
|
3669 |
lemma test_bit_split': |
|
3670 |
"word_split c = (a, b) --> (ALL n m. b !! n = (n < size b & c !! n) & |
|
3671 |
a !! m = (m < size a & c !! (m + size b)))" |
|
3672 |
apply (unfold word_split_bin' test_bit_bin) |
|
3673 |
apply (clarify) |
|
3674 |
apply (clarsimp simp: word_ubin.eq_norm nth_bintr word_size split: prod.splits) |
|
3675 |
apply (drule bin_nth_split) |
|
3676 |
apply safe |
|
3677 |
apply (simp_all add: add_commute) |
|
3678 |
apply (erule bin_nth_uint_imp)+ |
|
3679 |
done |
|
3680 |
||
3681 |
lemma test_bit_split: |
|
3682 |
"word_split c = (a, b) \<Longrightarrow> |
|
3683 |
(\<forall>n\<Colon>nat. b !! n \<longleftrightarrow> n < size b \<and> c !! n) \<and> (\<forall>m\<Colon>nat. a !! m \<longleftrightarrow> m < size a \<and> c !! (m + size b))" |
|
3684 |
by (simp add: test_bit_split') |
|
3685 |
||
3686 |
lemma test_bit_split_eq: "word_split c = (a, b) <-> |
|
3687 |
((ALL n::nat. b !! n = (n < size b & c !! n)) & |
|
3688 |
(ALL m::nat. a !! m = (m < size a & c !! (m + size b))))" |
|
3689 |
apply (rule_tac iffI) |
|
3690 |
apply (rule_tac conjI) |
|
3691 |
apply (erule test_bit_split [THEN conjunct1]) |
|
3692 |
apply (erule test_bit_split [THEN conjunct2]) |
|
3693 |
apply (case_tac "word_split c") |
|
3694 |
apply (frule test_bit_split) |
|
3695 |
apply (erule trans) |
|
44890
22f665a2e91c
new fastforce replacing fastsimp - less confusing name
nipkow
parents:
44821
diff
changeset
|
3696 |
apply (fastforce intro ! : word_eqI simp add : word_size) |
37660 | 3697 |
done |
3698 |
||
3699 |
-- {* this odd result is analogous to @{text "ucast_id"}, |
|
3700 |
result to the length given by the result type *} |
|
3701 |
||
3702 |
lemma word_cat_id: "word_cat a b = b" |
|
3703 |
unfolding word_cat_bin' by (simp add: word_ubin.inverse_norm) |
|
3704 |
||
3705 |
-- "limited hom result" |
|
3706 |
lemma word_cat_hom: |
|
3707 |
"len_of TYPE('a::len0) <= len_of TYPE('b::len0) + len_of TYPE ('c::len0) |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3708 |
\<Longrightarrow> |
37660 | 3709 |
(word_cat (word_of_int w :: 'b word) (b :: 'c word) :: 'a word) = |
3710 |
word_of_int (bin_cat w (size b) (uint b))" |
|
3711 |
apply (unfold word_cat_def word_size) |
|
3712 |
apply (clarsimp simp add: word_ubin.norm_eq_iff [symmetric] |
|
3713 |
word_ubin.eq_norm bintr_cat min_max.inf_absorb1) |
|
3714 |
done |
|
3715 |
||
3716 |
lemma word_cat_split_alt: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3717 |
"size w <= size u + size v \<Longrightarrow> word_split w = (u, v) \<Longrightarrow> word_cat u v = w" |
37660 | 3718 |
apply (rule word_eqI) |
3719 |
apply (drule test_bit_split) |
|
3720 |
apply (clarsimp simp add : test_bit_cat word_size) |
|
3721 |
apply safe |
|
3722 |
apply arith |
|
3723 |
done |
|
3724 |
||
3725 |
lemmas word_cat_split_size = |
|
3726 |
sym [THEN [2] word_cat_split_alt [symmetric], standard] |
|
3727 |
||
3728 |
||
3729 |
subsubsection "Split and slice" |
|
3730 |
||
3731 |
lemma split_slices: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3732 |
"word_split w = (u, v) \<Longrightarrow> u = slice (size v) w & v = slice 0 w" |
37660 | 3733 |
apply (drule test_bit_split) |
3734 |
apply (rule conjI) |
|
3735 |
apply (rule word_eqI, clarsimp simp: nth_slice word_size)+ |
|
3736 |
done |
|
3737 |
||
3738 |
lemma slice_cat1': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3739 |
"wc = word_cat a b \<Longrightarrow> size wc >= size a + size b \<Longrightarrow> slice (size b) wc = a" |
37660 | 3740 |
apply safe |
3741 |
apply (rule word_eqI) |
|
3742 |
apply (simp add: nth_slice test_bit_cat word_size) |
|
3743 |
done |
|
3744 |
||
3745 |
lemmas slice_cat1 = refl [THEN slice_cat1'] |
|
3746 |
lemmas slice_cat2 = trans [OF slice_id word_cat_id] |
|
3747 |
||
3748 |
lemma cat_slices: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3749 |
"a = slice n c \<Longrightarrow> b = slice 0 c \<Longrightarrow> n = size b \<Longrightarrow> |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3750 |
size a + size b >= size c \<Longrightarrow> word_cat a b = c" |
37660 | 3751 |
apply safe |
3752 |
apply (rule word_eqI) |
|
3753 |
apply (simp add: nth_slice test_bit_cat word_size) |
|
3754 |
apply safe |
|
3755 |
apply arith |
|
3756 |
done |
|
3757 |
||
3758 |
lemma word_split_cat_alt: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3759 |
"w = word_cat u v \<Longrightarrow> size u + size v <= size w \<Longrightarrow> word_split w = (u, v)" |
37660 | 3760 |
apply (case_tac "word_split ?w") |
3761 |
apply (rule trans, assumption) |
|
3762 |
apply (drule test_bit_split) |
|
3763 |
apply safe |
|
3764 |
apply (rule word_eqI, clarsimp simp: test_bit_cat word_size)+ |
|
3765 |
done |
|
3766 |
||
3767 |
lemmas word_cat_bl_no_bin [simp] = |
|
3768 |
word_cat_bl [where a="number_of a" |
|
3769 |
and b="number_of b", |
|
3770 |
unfolded to_bl_no_bin, standard] |
|
3771 |
||
3772 |
lemmas word_split_bl_no_bin [simp] = |
|
3773 |
word_split_bl_eq [where c="number_of c", unfolded to_bl_no_bin, standard] |
|
3774 |
||
3775 |
-- {* this odd result arises from the fact that the statement of the |
|
3776 |
result implies that the decoded words are of the same type, |
|
3777 |
and therefore of the same length, as the original word *} |
|
3778 |
||
3779 |
lemma word_rsplit_same: "word_rsplit w = [w]" |
|
3780 |
unfolding word_rsplit_def by (simp add : bin_rsplit_all) |
|
3781 |
||
3782 |
lemma word_rsplit_empty_iff_size: |
|
3783 |
"(word_rsplit w = []) = (size w = 0)" |
|
3784 |
unfolding word_rsplit_def bin_rsplit_def word_size |
|
3785 |
by (simp add: bin_rsplit_aux_simp_alt Let_def split: Product_Type.split_split) |
|
3786 |
||
3787 |
lemma test_bit_rsplit: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3788 |
"sw = word_rsplit w \<Longrightarrow> m < size (hd sw :: 'a :: len word) \<Longrightarrow> |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3789 |
k < length sw \<Longrightarrow> (rev sw ! k) !! m = (w !! (k * size (hd sw) + m))" |
37660 | 3790 |
apply (unfold word_rsplit_def word_test_bit_def) |
3791 |
apply (rule trans) |
|
3792 |
apply (rule_tac f = "%x. bin_nth x m" in arg_cong) |
|
3793 |
apply (rule nth_map [symmetric]) |
|
3794 |
apply simp |
|
3795 |
apply (rule bin_nth_rsplit) |
|
3796 |
apply simp_all |
|
3797 |
apply (simp add : word_size rev_map) |
|
3798 |
apply (rule trans) |
|
3799 |
defer |
|
3800 |
apply (rule map_ident [THEN fun_cong]) |
|
3801 |
apply (rule refl [THEN map_cong]) |
|
3802 |
apply (simp add : word_ubin.eq_norm) |
|
3803 |
apply (erule bin_rsplit_size_sign [OF len_gt_0 refl]) |
|
3804 |
done |
|
3805 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3806 |
lemma word_rcat_bl: "word_rcat wl = of_bl (concat (map to_bl wl))" |
37660 | 3807 |
unfolding word_rcat_def to_bl_def' of_bl_def |
3808 |
by (clarsimp simp add : bin_rcat_bl) |
|
3809 |
||
3810 |
lemma size_rcat_lem': |
|
3811 |
"size (concat (map to_bl wl)) = length wl * size (hd wl)" |
|
3812 |
unfolding word_size by (induct wl) auto |
|
3813 |
||
3814 |
lemmas size_rcat_lem = size_rcat_lem' [unfolded word_size] |
|
3815 |
||
3816 |
lemmas td_gal_lt_len = len_gt_0 [THEN td_gal_lt, standard] |
|
3817 |
||
3818 |
lemma nth_rcat_lem' [rule_format] : |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3819 |
"sw = size (hd wl :: 'a :: len word) \<Longrightarrow> (ALL n. n < size wl * sw --> |
37660 | 3820 |
rev (concat (map to_bl wl)) ! n = |
3821 |
rev (to_bl (rev wl ! (n div sw))) ! (n mod sw))" |
|
3822 |
apply (unfold word_size) |
|
3823 |
apply (induct "wl") |
|
3824 |
apply clarsimp |
|
3825 |
apply (clarsimp simp add : nth_append size_rcat_lem) |
|
3826 |
apply (simp (no_asm_use) only: mult_Suc [symmetric] |
|
3827 |
td_gal_lt_len less_Suc_eq_le mod_div_equality') |
|
3828 |
apply clarsimp |
|
3829 |
done |
|
3830 |
||
3831 |
lemmas nth_rcat_lem = refl [THEN nth_rcat_lem', unfolded word_size] |
|
3832 |
||
3833 |
lemma test_bit_rcat: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3834 |
"sw = size (hd wl :: 'a :: len word) \<Longrightarrow> rc = word_rcat wl \<Longrightarrow> rc !! n = |
37660 | 3835 |
(n < size rc & n div sw < size wl & (rev wl) ! (n div sw) !! (n mod sw))" |
3836 |
apply (unfold word_rcat_bl word_size) |
|
3837 |
apply (clarsimp simp add : |
|
3838 |
test_bit_of_bl size_rcat_lem word_size td_gal_lt_len) |
|
3839 |
apply safe |
|
3840 |
apply (auto simp add : |
|
3841 |
test_bit_bl word_size td_gal_lt_len [THEN iffD2, THEN nth_rcat_lem]) |
|
3842 |
done |
|
3843 |
||
3844 |
lemma foldl_eq_foldr [rule_format] : |
|
3845 |
"ALL x. foldl op + x xs = foldr op + (x # xs) (0 :: 'a :: comm_monoid_add)" |
|
3846 |
by (induct xs) (auto simp add : add_assoc) |
|
3847 |
||
3848 |
lemmas test_bit_cong = arg_cong [where f = "test_bit", THEN fun_cong] |
|
3849 |
||
3850 |
lemmas test_bit_rsplit_alt = |
|
3851 |
trans [OF nth_rev_alt [THEN test_bit_cong] |
|
3852 |
test_bit_rsplit [OF refl asm_rl diff_Suc_less]] |
|
3853 |
||
3854 |
-- "lazy way of expressing that u and v, and su and sv, have same types" |
|
3855 |
lemma word_rsplit_len_indep': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3856 |
"[u,v] = p \<Longrightarrow> [su,sv] = q \<Longrightarrow> word_rsplit u = su \<Longrightarrow> |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3857 |
word_rsplit v = sv \<Longrightarrow> length su = length sv" |
37660 | 3858 |
apply (unfold word_rsplit_def) |
3859 |
apply (auto simp add : bin_rsplit_len_indep) |
|
3860 |
done |
|
3861 |
||
3862 |
lemmas word_rsplit_len_indep = word_rsplit_len_indep' [OF refl refl refl refl] |
|
3863 |
||
3864 |
lemma length_word_rsplit_size: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3865 |
"n = len_of TYPE ('a :: len) \<Longrightarrow> |
37660 | 3866 |
(length (word_rsplit w :: 'a word list) <= m) = (size w <= m * n)" |
3867 |
apply (unfold word_rsplit_def word_size) |
|
3868 |
apply (clarsimp simp add : bin_rsplit_len_le) |
|
3869 |
done |
|
3870 |
||
3871 |
lemmas length_word_rsplit_lt_size = |
|
3872 |
length_word_rsplit_size [unfolded Not_eq_iff linorder_not_less [symmetric]] |
|
3873 |
||
3874 |
lemma length_word_rsplit_exp_size: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3875 |
"n = len_of TYPE ('a :: len) \<Longrightarrow> |
37660 | 3876 |
length (word_rsplit w :: 'a word list) = (size w + n - 1) div n" |
3877 |
unfolding word_rsplit_def by (clarsimp simp add : word_size bin_rsplit_len) |
|
3878 |
||
3879 |
lemma length_word_rsplit_even_size: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3880 |
"n = len_of TYPE ('a :: len) \<Longrightarrow> size w = m * n \<Longrightarrow> |
37660 | 3881 |
length (word_rsplit w :: 'a word list) = m" |
3882 |
by (clarsimp simp add : length_word_rsplit_exp_size given_quot_alt) |
|
3883 |
||
3884 |
lemmas length_word_rsplit_exp_size' = refl [THEN length_word_rsplit_exp_size] |
|
3885 |
||
3886 |
(* alternative proof of word_rcat_rsplit *) |
|
3887 |
lemmas tdle = iffD2 [OF split_div_lemma refl, THEN conjunct1] |
|
3888 |
lemmas dtle = xtr4 [OF tdle mult_commute] |
|
3889 |
||
3890 |
lemma word_rcat_rsplit: "word_rcat (word_rsplit w) = w" |
|
3891 |
apply (rule word_eqI) |
|
3892 |
apply (clarsimp simp add : test_bit_rcat word_size) |
|
3893 |
apply (subst refl [THEN test_bit_rsplit]) |
|
3894 |
apply (simp_all add: word_size |
|
3895 |
refl [THEN length_word_rsplit_size [simplified not_less [symmetric], simplified]]) |
|
3896 |
apply safe |
|
3897 |
apply (erule xtr7, rule len_gt_0 [THEN dtle])+ |
|
3898 |
done |
|
3899 |
||
3900 |
lemma size_word_rsplit_rcat_size': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3901 |
"word_rcat (ws :: 'a :: len word list) = frcw \<Longrightarrow> |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3902 |
size frcw = length ws * len_of TYPE ('a) \<Longrightarrow> |
37660 | 3903 |
size (hd [word_rsplit frcw, ws]) = size ws" |
3904 |
apply (clarsimp simp add : word_size length_word_rsplit_exp_size') |
|
3905 |
apply (fast intro: given_quot_alt) |
|
3906 |
done |
|
3907 |
||
3908 |
lemmas size_word_rsplit_rcat_size = |
|
3909 |
size_word_rsplit_rcat_size' [simplified] |
|
3910 |
||
3911 |
lemma msrevs: |
|
3912 |
fixes n::nat |
|
3913 |
shows "0 < n \<Longrightarrow> (k * n + m) div n = m div n + k" |
|
3914 |
and "(k * n + m) mod n = m mod n" |
|
3915 |
by (auto simp: add_commute) |
|
3916 |
||
3917 |
lemma word_rsplit_rcat_size': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3918 |
"word_rcat (ws :: 'a :: len word list) = frcw \<Longrightarrow> |
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3919 |
size frcw = length ws * len_of TYPE ('a) \<Longrightarrow> word_rsplit frcw = ws" |
37660 | 3920 |
apply (frule size_word_rsplit_rcat_size, assumption) |
3921 |
apply (clarsimp simp add : word_size) |
|
3922 |
apply (rule nth_equalityI, assumption) |
|
3923 |
apply clarsimp |
|
3924 |
apply (rule word_eqI) |
|
3925 |
apply (rule trans) |
|
3926 |
apply (rule test_bit_rsplit_alt) |
|
3927 |
apply (clarsimp simp: word_size)+ |
|
3928 |
apply (rule trans) |
|
3929 |
apply (rule test_bit_rcat [OF refl refl]) |
|
41550 | 3930 |
apply (simp add: word_size msrevs) |
37660 | 3931 |
apply (subst nth_rev) |
3932 |
apply arith |
|
41550 | 3933 |
apply (simp add: le0 [THEN [2] xtr7, THEN diff_Suc_less]) |
37660 | 3934 |
apply safe |
41550 | 3935 |
apply (simp add: diff_mult_distrib) |
37660 | 3936 |
apply (rule mpl_lem) |
3937 |
apply (cases "size ws") |
|
3938 |
apply simp_all |
|
3939 |
done |
|
3940 |
||
3941 |
lemmas word_rsplit_rcat_size = refl [THEN word_rsplit_rcat_size'] |
|
3942 |
||
3943 |
||
3944 |
subsection "Rotation" |
|
3945 |
||
3946 |
lemmas rotater_0' [simp] = rotater_def [where n = "0", simplified] |
|
3947 |
||
3948 |
lemmas word_rot_defs = word_roti_def word_rotr_def word_rotl_def |
|
3949 |
||
3950 |
lemma rotate_eq_mod: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3951 |
"m mod length xs = n mod length xs \<Longrightarrow> rotate m xs = rotate n xs" |
37660 | 3952 |
apply (rule box_equals) |
3953 |
defer |
|
3954 |
apply (rule rotate_conv_mod [symmetric])+ |
|
3955 |
apply simp |
|
3956 |
done |
|
3957 |
||
3958 |
lemmas rotate_eqs [standard] = |
|
3959 |
trans [OF rotate0 [THEN fun_cong] id_apply] |
|
3960 |
rotate_rotate [symmetric] |
|
3961 |
rotate_id |
|
3962 |
rotate_conv_mod |
|
3963 |
rotate_eq_mod |
|
3964 |
||
3965 |
||
3966 |
subsubsection "Rotation of list to right" |
|
3967 |
||
3968 |
lemma rotate1_rl': "rotater1 (l @ [a]) = a # l" |
|
3969 |
unfolding rotater1_def by (cases l) auto |
|
3970 |
||
3971 |
lemma rotate1_rl [simp] : "rotater1 (rotate1 l) = l" |
|
3972 |
apply (unfold rotater1_def) |
|
3973 |
apply (cases "l") |
|
3974 |
apply (case_tac [2] "list") |
|
3975 |
apply auto |
|
3976 |
done |
|
3977 |
||
3978 |
lemma rotate1_lr [simp] : "rotate1 (rotater1 l) = l" |
|
3979 |
unfolding rotater1_def by (cases l) auto |
|
3980 |
||
3981 |
lemma rotater1_rev': "rotater1 (rev xs) = rev (rotate1 xs)" |
|
3982 |
apply (cases "xs") |
|
3983 |
apply (simp add : rotater1_def) |
|
3984 |
apply (simp add : rotate1_rl') |
|
3985 |
done |
|
3986 |
||
3987 |
lemma rotater_rev': "rotater n (rev xs) = rev (rotate n xs)" |
|
3988 |
unfolding rotater_def by (induct n) (auto intro: rotater1_rev') |
|
3989 |
||
3990 |
lemmas rotater_rev = rotater_rev' [where xs = "rev ys", simplified, standard] |
|
3991 |
||
3992 |
lemma rotater_drop_take: |
|
3993 |
"rotater n xs = |
|
3994 |
drop (length xs - n mod length xs) xs @ |
|
3995 |
take (length xs - n mod length xs) xs" |
|
3996 |
by (clarsimp simp add : rotater_rev rotate_drop_take rev_take rev_drop) |
|
3997 |
||
3998 |
lemma rotater_Suc [simp] : |
|
3999 |
"rotater (Suc n) xs = rotater1 (rotater n xs)" |
|
4000 |
unfolding rotater_def by auto |
|
4001 |
||
4002 |
lemma rotate_inv_plus [rule_format] : |
|
4003 |
"ALL k. k = m + n --> rotater k (rotate n xs) = rotater m xs & |
|
4004 |
rotate k (rotater n xs) = rotate m xs & |
|
4005 |
rotater n (rotate k xs) = rotate m xs & |
|
4006 |
rotate n (rotater k xs) = rotater m xs" |
|
4007 |
unfolding rotater_def rotate_def |
|
4008 |
by (induct n) (auto intro: funpow_swap1 [THEN trans]) |
|
4009 |
||
4010 |
lemmas rotate_inv_rel = le_add_diff_inverse2 [symmetric, THEN rotate_inv_plus] |
|
4011 |
||
4012 |
lemmas rotate_inv_eq = order_refl [THEN rotate_inv_rel, simplified] |
|
4013 |
||
4014 |
lemmas rotate_lr [simp] = rotate_inv_eq [THEN conjunct1, standard] |
|
4015 |
lemmas rotate_rl [simp] = |
|
4016 |
rotate_inv_eq [THEN conjunct2, THEN conjunct1, standard] |
|
4017 |
||
4018 |
lemma rotate_gal: "(rotater n xs = ys) = (rotate n ys = xs)" |
|
4019 |
by auto |
|
4020 |
||
4021 |
lemma rotate_gal': "(ys = rotater n xs) = (xs = rotate n ys)" |
|
4022 |
by auto |
|
4023 |
||
4024 |
lemma length_rotater [simp]: |
|
4025 |
"length (rotater n xs) = length xs" |
|
4026 |
by (simp add : rotater_rev) |
|
4027 |
||
38527 | 4028 |
lemma restrict_to_left: |
4029 |
assumes "x = y" |
|
4030 |
shows "(x = z) = (y = z)" |
|
4031 |
using assms by simp |
|
4032 |
||
37660 | 4033 |
lemmas rrs0 = rotate_eqs [THEN restrict_to_left, |
4034 |
simplified rotate_gal [symmetric] rotate_gal' [symmetric], standard] |
|
4035 |
lemmas rrs1 = rrs0 [THEN refl [THEN rev_iffD1]] |
|
4036 |
lemmas rotater_eqs = rrs1 [simplified length_rotater, standard] |
|
4037 |
lemmas rotater_0 = rotater_eqs (1) |
|
4038 |
lemmas rotater_add = rotater_eqs (2) |
|
4039 |
||
4040 |
||
4041 |
subsubsection "map, map2, commuting with rotate(r)" |
|
4042 |
||
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4043 |
lemma last_map: "xs ~= [] \<Longrightarrow> last (map f xs) = f (last xs)" |
37660 | 4044 |
by (induct xs) auto |
4045 |
||
4046 |
lemma butlast_map: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4047 |
"xs ~= [] \<Longrightarrow> butlast (map f xs) = map f (butlast xs)" |
37660 | 4048 |
by (induct xs) auto |
4049 |
||
4050 |
lemma rotater1_map: "rotater1 (map f xs) = map f (rotater1 xs)" |
|
4051 |
unfolding rotater1_def |
|
4052 |
by (cases xs) (auto simp add: last_map butlast_map) |
|
4053 |
||
4054 |
lemma rotater_map: |
|
4055 |
"rotater n (map f xs) = map f (rotater n xs)" |
|
4056 |
unfolding rotater_def |
|
4057 |
by (induct n) (auto simp add : rotater1_map) |
|
4058 |
||
4059 |
lemma but_last_zip [rule_format] : |
|
4060 |
"ALL ys. length xs = length ys --> xs ~= [] --> |
|
4061 |
last (zip xs ys) = (last xs, last ys) & |
|
4062 |
butlast (zip xs ys) = zip (butlast xs) (butlast ys)" |
|
4063 |
apply (induct "xs") |
|
4064 |
apply auto |
|
4065 |
apply ((case_tac ys, auto simp: neq_Nil_conv)[1])+ |
|
4066 |
done |
|
4067 |
||
4068 |
lemma but_last_map2 [rule_format] : |
|
4069 |
"ALL ys. length xs = length ys --> xs ~= [] --> |
|
4070 |
last (map2 f xs ys) = f (last xs) (last ys) & |
|
4071 |
butlast (map2 f xs ys) = map2 f (butlast xs) (butlast ys)" |
|
4072 |
apply (induct "xs") |
|
4073 |
apply auto |
|
4074 |
apply (unfold map2_def) |
|
4075 |
apply ((case_tac ys, auto simp: neq_Nil_conv)[1])+ |
|
4076 |
done |
|
4077 |
||
4078 |
lemma rotater1_zip: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4079 |
"length xs = length ys \<Longrightarrow> |
37660 | 4080 |
rotater1 (zip xs ys) = zip (rotater1 xs) (rotater1 ys)" |
4081 |
apply (unfold rotater1_def) |
|
4082 |
apply (cases "xs") |
|
4083 |
apply auto |
|
4084 |
apply ((case_tac ys, auto simp: neq_Nil_conv but_last_zip)[1])+ |
|
4085 |
done |
|
4086 |
||
4087 |
lemma rotater1_map2: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4088 |
"length xs = length ys \<Longrightarrow> |
37660 | 4089 |
rotater1 (map2 f xs ys) = map2 f (rotater1 xs) (rotater1 ys)" |
4090 |
unfolding map2_def by (simp add: rotater1_map rotater1_zip) |
|
4091 |
||
4092 |
lemmas lrth = |
|
4093 |
box_equals [OF asm_rl length_rotater [symmetric] |
|
4094 |
length_rotater [symmetric], |
|
4095 |
THEN rotater1_map2] |
|
4096 |
||
4097 |
lemma rotater_map2: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4098 |
"length xs = length ys \<Longrightarrow> |
37660 | 4099 |
rotater n (map2 f xs ys) = map2 f (rotater n xs) (rotater n ys)" |
4100 |
by (induct n) (auto intro!: lrth) |
|
4101 |
||
4102 |
lemma rotate1_map2: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4103 |
"length xs = length ys \<Longrightarrow> |
37660 | 4104 |
rotate1 (map2 f xs ys) = map2 f (rotate1 xs) (rotate1 ys)" |
4105 |
apply (unfold map2_def) |
|
4106 |
apply (cases xs) |
|
4107 |
apply (cases ys, auto simp add : rotate1_def)+ |
|
4108 |
done |
|
4109 |
||
4110 |
lemmas lth = box_equals [OF asm_rl length_rotate [symmetric] |
|
4111 |
length_rotate [symmetric], THEN rotate1_map2] |
|
4112 |
||
4113 |
lemma rotate_map2: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4114 |
"length xs = length ys \<Longrightarrow> |
37660 | 4115 |
rotate n (map2 f xs ys) = map2 f (rotate n xs) (rotate n ys)" |
4116 |
by (induct n) (auto intro!: lth) |
|
4117 |
||
4118 |
||
4119 |
-- "corresponding equalities for word rotation" |
|
4120 |
||
4121 |
lemma to_bl_rotl: |
|
4122 |
"to_bl (word_rotl n w) = rotate n (to_bl w)" |
|
4123 |
by (simp add: word_bl.Abs_inverse' word_rotl_def) |
|
4124 |
||
4125 |
lemmas blrs0 = rotate_eqs [THEN to_bl_rotl [THEN trans]] |
|
4126 |
||
4127 |
lemmas word_rotl_eqs = |
|
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
4128 |
blrs0 [simplified word_bl_Rep' word_bl.Rep_inject to_bl_rotl [symmetric]] |
37660 | 4129 |
|
4130 |
lemma to_bl_rotr: |
|
4131 |
"to_bl (word_rotr n w) = rotater n (to_bl w)" |
|
4132 |
by (simp add: word_bl.Abs_inverse' word_rotr_def) |
|
4133 |
||
4134 |
lemmas brrs0 = rotater_eqs [THEN to_bl_rotr [THEN trans]] |
|
4135 |
||
4136 |
lemmas word_rotr_eqs = |
|
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
4137 |
brrs0 [simplified word_bl_Rep' word_bl.Rep_inject to_bl_rotr [symmetric]] |
37660 | 4138 |
|
4139 |
declare word_rotr_eqs (1) [simp] |
|
4140 |
declare word_rotl_eqs (1) [simp] |
|
4141 |
||
4142 |
lemma |
|
4143 |
word_rot_rl [simp]: |
|
4144 |
"word_rotl k (word_rotr k v) = v" and |
|
4145 |
word_rot_lr [simp]: |
|
4146 |
"word_rotr k (word_rotl k v) = v" |
|
4147 |
by (auto simp add: to_bl_rotr to_bl_rotl word_bl.Rep_inject [symmetric]) |
|
4148 |
||
4149 |
lemma |
|
4150 |
word_rot_gal: |
|
4151 |
"(word_rotr n v = w) = (word_rotl n w = v)" and |
|
4152 |
word_rot_gal': |
|
4153 |
"(w = word_rotr n v) = (v = word_rotl n w)" |
|
4154 |
by (auto simp: to_bl_rotr to_bl_rotl word_bl.Rep_inject [symmetric] |
|
4155 |
dest: sym) |
|
4156 |
||
4157 |
lemma word_rotr_rev: |
|
4158 |
"word_rotr n w = word_reverse (word_rotl n (word_reverse w))" |
|
4159 |
by (simp add: word_bl.Rep_inject [symmetric] to_bl_word_rev |
|
4160 |
to_bl_rotr to_bl_rotl rotater_rev) |
|
4161 |
||
4162 |
lemma word_roti_0 [simp]: "word_roti 0 w = w" |
|
4163 |
by (unfold word_rot_defs) auto |
|
4164 |
||
4165 |
lemmas abl_cong = arg_cong [where f = "of_bl"] |
|
4166 |
||
4167 |
lemma word_roti_add: |
|
4168 |
"word_roti (m + n) w = word_roti m (word_roti n w)" |
|
4169 |
proof - |
|
4170 |
have rotater_eq_lem: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4171 |
"\<And>m n xs. m = n \<Longrightarrow> rotater m xs = rotater n xs" |
37660 | 4172 |
by auto |
4173 |
||
4174 |
have rotate_eq_lem: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4175 |
"\<And>m n xs. m = n \<Longrightarrow> rotate m xs = rotate n xs" |
37660 | 4176 |
by auto |
4177 |
||
4178 |
note rpts [symmetric, standard] = |
|
4179 |
rotate_inv_plus [THEN conjunct1] |
|
4180 |
rotate_inv_plus [THEN conjunct2, THEN conjunct1] |
|
4181 |
rotate_inv_plus [THEN conjunct2, THEN conjunct2, THEN conjunct1] |
|
4182 |
rotate_inv_plus [THEN conjunct2, THEN conjunct2, THEN conjunct2] |
|
4183 |
||
4184 |
note rrp = trans [symmetric, OF rotate_rotate rotate_eq_lem] |
|
4185 |
note rrrp = trans [symmetric, OF rotater_add [symmetric] rotater_eq_lem] |
|
4186 |
||
4187 |
show ?thesis |
|
4188 |
apply (unfold word_rot_defs) |
|
4189 |
apply (simp only: split: split_if) |
|
4190 |
apply (safe intro!: abl_cong) |
|
4191 |
apply (simp_all only: to_bl_rotl [THEN word_bl.Rep_inverse'] |
|
4192 |
to_bl_rotl |
|
4193 |
to_bl_rotr [THEN word_bl.Rep_inverse'] |
|
4194 |
to_bl_rotr) |
|
4195 |
apply (rule rrp rrrp rpts, |
|
4196 |
simp add: nat_add_distrib [symmetric] |
|
4197 |
nat_diff_distrib [symmetric])+ |
|
4198 |
done |
|
4199 |
qed |
|
4200 |
||
4201 |
lemma word_roti_conv_mod': "word_roti n w = word_roti (n mod int (size w)) w" |
|
4202 |
apply (unfold word_rot_defs) |
|
4203 |
apply (cut_tac y="size w" in gt_or_eq_0) |
|
4204 |
apply (erule disjE) |
|
4205 |
apply simp_all |
|
4206 |
apply (safe intro!: abl_cong) |
|
4207 |
apply (rule rotater_eqs) |
|
4208 |
apply (simp add: word_size nat_mod_distrib) |
|
4209 |
apply (simp add: rotater_add [symmetric] rotate_gal [symmetric]) |
|
4210 |
apply (rule rotater_eqs) |
|
4211 |
apply (simp add: word_size nat_mod_distrib) |
|
4212 |
apply (rule int_eq_0_conv [THEN iffD1]) |
|
44821 | 4213 |
apply (simp only: zmod_int of_nat_add) |
37660 | 4214 |
apply (simp add: rdmods) |
4215 |
done |
|
4216 |
||
4217 |
lemmas word_roti_conv_mod = word_roti_conv_mod' [unfolded word_size] |
|
4218 |
||
4219 |
||
4220 |
subsubsection "Word rotation commutes with bit-wise operations" |
|
4221 |
||
4222 |
(* using locale to not pollute lemma namespace *) |
|
4223 |
locale word_rotate |
|
4224 |
begin |
|
4225 |
||
4226 |
lemmas word_rot_defs' = to_bl_rotl to_bl_rotr |
|
4227 |
||
4228 |
lemmas blwl_syms [symmetric] = bl_word_not bl_word_and bl_word_or bl_word_xor |
|
4229 |
||
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
4230 |
lemmas lbl_lbl = trans [OF word_bl_Rep' word_bl_Rep' [symmetric]] |
37660 | 4231 |
|
4232 |
lemmas ths_map2 [OF lbl_lbl] = rotate_map2 rotater_map2 |
|
4233 |
||
4234 |
lemmas ths_map [where xs = "to_bl v", standard] = rotate_map rotater_map |
|
4235 |
||
4236 |
lemmas th1s [simplified word_rot_defs' [symmetric]] = ths_map2 ths_map |
|
4237 |
||
4238 |
lemma word_rot_logs: |
|
4239 |
"word_rotl n (NOT v) = NOT word_rotl n v" |
|
4240 |
"word_rotr n (NOT v) = NOT word_rotr n v" |
|
4241 |
"word_rotl n (x AND y) = word_rotl n x AND word_rotl n y" |
|
4242 |
"word_rotr n (x AND y) = word_rotr n x AND word_rotr n y" |
|
4243 |
"word_rotl n (x OR y) = word_rotl n x OR word_rotl n y" |
|
4244 |
"word_rotr n (x OR y) = word_rotr n x OR word_rotr n y" |
|
4245 |
"word_rotl n (x XOR y) = word_rotl n x XOR word_rotl n y" |
|
4246 |
"word_rotr n (x XOR y) = word_rotr n x XOR word_rotr n y" |
|
4247 |
by (rule word_bl.Rep_eqD, |
|
4248 |
rule word_rot_defs' [THEN trans], |
|
4249 |
simp only: blwl_syms [symmetric], |
|
4250 |
rule th1s [THEN trans], |
|
4251 |
rule refl)+ |
|
4252 |
end |
|
4253 |
||
4254 |
lemmas word_rot_logs = word_rotate.word_rot_logs |
|
4255 |
||
4256 |
lemmas bl_word_rotl_dt = trans [OF to_bl_rotl rotate_drop_take, |
|
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
4257 |
simplified word_bl_Rep', standard] |
37660 | 4258 |
|
4259 |
lemmas bl_word_rotr_dt = trans [OF to_bl_rotr rotater_drop_take, |
|
45538
1fffa81b9b83
eliminated slightly odd Rep' with dynamically-scoped [simplified];
wenzelm
parents:
45529
diff
changeset
|
4260 |
simplified word_bl_Rep', standard] |
37660 | 4261 |
|
4262 |
lemma bl_word_roti_dt': |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4263 |
"n = nat ((- i) mod int (size (w :: 'a :: len word))) \<Longrightarrow> |
37660 | 4264 |
to_bl (word_roti i w) = drop n (to_bl w) @ take n (to_bl w)" |
4265 |
apply (unfold word_roti_def) |
|
4266 |
apply (simp add: bl_word_rotl_dt bl_word_rotr_dt word_size) |
|
4267 |
apply safe |
|
4268 |
apply (simp add: zmod_zminus1_eq_if) |
|
4269 |
apply safe |
|
4270 |
apply (simp add: nat_mult_distrib) |
|
4271 |
apply (simp add: nat_diff_distrib [OF pos_mod_sign pos_mod_conj |
|
4272 |
[THEN conjunct2, THEN order_less_imp_le]] |
|
4273 |
nat_mod_distrib) |
|
4274 |
apply (simp add: nat_mod_distrib) |
|
4275 |
done |
|
4276 |
||
4277 |
lemmas bl_word_roti_dt = bl_word_roti_dt' [unfolded word_size] |
|
4278 |
||
4279 |
lemmas word_rotl_dt = bl_word_rotl_dt |
|
4280 |
[THEN word_bl.Rep_inverse' [symmetric], standard] |
|
4281 |
lemmas word_rotr_dt = bl_word_rotr_dt |
|
4282 |
[THEN word_bl.Rep_inverse' [symmetric], standard] |
|
4283 |
lemmas word_roti_dt = bl_word_roti_dt |
|
4284 |
[THEN word_bl.Rep_inverse' [symmetric], standard] |
|
4285 |
||
4286 |
lemma word_rotx_0 [simp] : "word_rotr i 0 = 0 & word_rotl i 0 = 0" |
|
4287 |
by (simp add : word_rotr_dt word_rotl_dt to_bl_0 replicate_add [symmetric]) |
|
4288 |
||
4289 |
lemma word_roti_0' [simp] : "word_roti n 0 = 0" |
|
4290 |
unfolding word_roti_def by auto |
|
4291 |
||
4292 |
lemmas word_rotr_dt_no_bin' [simp] = |
|
4293 |
word_rotr_dt [where w="number_of w", unfolded to_bl_no_bin, standard] |
|
4294 |
||
4295 |
lemmas word_rotl_dt_no_bin' [simp] = |
|
4296 |
word_rotl_dt [where w="number_of w", unfolded to_bl_no_bin, standard] |
|
4297 |
||
4298 |
declare word_roti_def [simp] |
|
4299 |
||
4300 |
||
4301 |
subsection {* Miscellaneous *} |
|
4302 |
||
4303 |
declare of_nat_2p [simp] |
|
4304 |
||
4305 |
lemma word_int_cases: |
|
4306 |
"\<lbrakk>\<And>n. \<lbrakk>(x ::'a::len0 word) = word_of_int n; 0 \<le> n; n < 2^len_of TYPE('a)\<rbrakk> \<Longrightarrow> P\<rbrakk> |
|
4307 |
\<Longrightarrow> P" |
|
4308 |
by (cases x rule: word_uint.Abs_cases) (simp add: uints_num) |
|
4309 |
||
4310 |
lemma word_nat_cases [cases type: word]: |
|
4311 |
"\<lbrakk>\<And>n. \<lbrakk>(x ::'a::len word) = of_nat n; n < 2^len_of TYPE('a)\<rbrakk> \<Longrightarrow> P\<rbrakk> |
|
4312 |
\<Longrightarrow> P" |
|
4313 |
by (cases x rule: word_unat.Abs_cases) (simp add: unats_def) |
|
4314 |
||
4315 |
lemma max_word_eq: |
|
4316 |
"(max_word::'a::len word) = 2^len_of TYPE('a) - 1" |
|
4317 |
by (simp add: max_word_def word_of_int_hom_syms word_of_int_2p) |
|
4318 |
||
4319 |
lemma max_word_max [simp,intro!]: |
|
4320 |
"n \<le> max_word" |
|
4321 |
by (cases n rule: word_int_cases) |
|
4322 |
(simp add: max_word_def word_le_def int_word_uint int_mod_eq') |
|
4323 |
||
4324 |
lemma word_of_int_2p_len: |
|
4325 |
"word_of_int (2 ^ len_of TYPE('a)) = (0::'a::len0 word)" |
|
4326 |
by (subst word_uint.Abs_norm [symmetric]) |
|
4327 |
(simp add: word_of_int_hom_syms) |
|
4328 |
||
4329 |
lemma word_pow_0: |
|
4330 |
"(2::'a::len word) ^ len_of TYPE('a) = 0" |
|
4331 |
proof - |
|
4332 |
have "word_of_int (2 ^ len_of TYPE('a)) = (0::'a word)" |
|
4333 |
by (rule word_of_int_2p_len) |
|
4334 |
thus ?thesis by (simp add: word_of_int_2p) |
|
4335 |
qed |
|
4336 |
||
4337 |
lemma max_word_wrap: "x + 1 = 0 \<Longrightarrow> x = max_word" |
|
4338 |
apply (simp add: max_word_eq) |
|
4339 |
apply uint_arith |
|
4340 |
apply auto |
|
4341 |
apply (simp add: word_pow_0) |
|
4342 |
done |
|
4343 |
||
4344 |
lemma max_word_minus: |
|
4345 |
"max_word = (-1::'a::len word)" |
|
4346 |
proof - |
|
4347 |
have "-1 + 1 = (0::'a word)" by simp |
|
4348 |
thus ?thesis by (rule max_word_wrap [symmetric]) |
|
4349 |
qed |
|
4350 |
||
4351 |
lemma max_word_bl [simp]: |
|
4352 |
"to_bl (max_word::'a::len word) = replicate (len_of TYPE('a)) True" |
|
4353 |
by (subst max_word_minus to_bl_n1)+ simp |
|
4354 |
||
4355 |
lemma max_test_bit [simp]: |
|
4356 |
"(max_word::'a::len word) !! n = (n < len_of TYPE('a))" |
|
4357 |
by (auto simp add: test_bit_bl word_size) |
|
4358 |
||
4359 |
lemma word_and_max [simp]: |
|
4360 |
"x AND max_word = x" |
|
4361 |
by (rule word_eqI) (simp add: word_ops_nth_size word_size) |
|
4362 |
||
4363 |
lemma word_or_max [simp]: |
|
4364 |
"x OR max_word = max_word" |
|
4365 |
by (rule word_eqI) (simp add: word_ops_nth_size word_size) |
|
4366 |
||
4367 |
lemma word_ao_dist2: |
|
4368 |
"x AND (y OR z) = x AND y OR x AND (z::'a::len0 word)" |
|
4369 |
by (rule word_eqI) (auto simp add: word_ops_nth_size word_size) |
|
4370 |
||
4371 |
lemma word_oa_dist2: |
|
4372 |
"x OR y AND z = (x OR y) AND (x OR (z::'a::len0 word))" |
|
4373 |
by (rule word_eqI) (auto simp add: word_ops_nth_size word_size) |
|
4374 |
||
4375 |
lemma word_and_not [simp]: |
|
4376 |
"x AND NOT x = (0::'a::len0 word)" |
|
4377 |
by (rule word_eqI) (auto simp add: word_ops_nth_size word_size) |
|
4378 |
||
4379 |
lemma word_or_not [simp]: |
|
4380 |
"x OR NOT x = max_word" |
|
4381 |
by (rule word_eqI) (auto simp add: word_ops_nth_size word_size) |
|
4382 |
||
4383 |
lemma word_boolean: |
|
4384 |
"boolean (op AND) (op OR) bitNOT 0 max_word" |
|
4385 |
apply (rule boolean.intro) |
|
4386 |
apply (rule word_bw_assocs) |
|
4387 |
apply (rule word_bw_assocs) |
|
4388 |
apply (rule word_bw_comms) |
|
4389 |
apply (rule word_bw_comms) |
|
4390 |
apply (rule word_ao_dist2) |
|
4391 |
apply (rule word_oa_dist2) |
|
4392 |
apply (rule word_and_max) |
|
4393 |
apply (rule word_log_esimps) |
|
4394 |
apply (rule word_and_not) |
|
4395 |
apply (rule word_or_not) |
|
4396 |
done |
|
4397 |
||
4398 |
interpretation word_bool_alg: |
|
4399 |
boolean "op AND" "op OR" bitNOT 0 max_word |
|
4400 |
by (rule word_boolean) |
|
4401 |
||
4402 |
lemma word_xor_and_or: |
|
4403 |
"x XOR y = x AND NOT y OR NOT x AND (y::'a::len0 word)" |
|
4404 |
by (rule word_eqI) (auto simp add: word_ops_nth_size word_size) |
|
4405 |
||
4406 |
interpretation word_bool_alg: |
|
4407 |
boolean_xor "op AND" "op OR" bitNOT 0 max_word "op XOR" |
|
4408 |
apply (rule boolean_xor.intro) |
|
4409 |
apply (rule word_boolean) |
|
4410 |
apply (rule boolean_xor_axioms.intro) |
|
4411 |
apply (rule word_xor_and_or) |
|
4412 |
done |
|
4413 |
||
4414 |
lemma shiftr_x_0 [iff]: |
|
4415 |
"(x::'a::len0 word) >> 0 = x" |
|
4416 |
by (simp add: shiftr_bl) |
|
4417 |
||
4418 |
lemma shiftl_x_0 [simp]: |
|
4419 |
"(x :: 'a :: len word) << 0 = x" |
|
4420 |
by (simp add: shiftl_t2n) |
|
4421 |
||
4422 |
lemma shiftl_1 [simp]: |
|
4423 |
"(1::'a::len word) << n = 2^n" |
|
4424 |
by (simp add: shiftl_t2n) |
|
4425 |
||
4426 |
lemma uint_lt_0 [simp]: |
|
4427 |
"uint x < 0 = False" |
|
4428 |
by (simp add: linorder_not_less) |
|
4429 |
||
4430 |
lemma shiftr1_1 [simp]: |
|
4431 |
"shiftr1 (1::'a::len word) = 0" |
|
4432 |
by (simp add: shiftr1_def word_0_alt) |
|
4433 |
||
4434 |
lemma shiftr_1[simp]: |
|
4435 |
"(1::'a::len word) >> n = (if n = 0 then 1 else 0)" |
|
4436 |
by (induct n) (auto simp: shiftr_def) |
|
4437 |
||
4438 |
lemma word_less_1 [simp]: |
|
4439 |
"((x::'a::len word) < 1) = (x = 0)" |
|
4440 |
by (simp add: word_less_nat_alt unat_0_iff) |
|
4441 |
||
4442 |
lemma to_bl_mask: |
|
4443 |
"to_bl (mask n :: 'a::len word) = |
|
4444 |
replicate (len_of TYPE('a) - n) False @ |
|
4445 |
replicate (min (len_of TYPE('a)) n) True" |
|
4446 |
by (simp add: mask_bl word_rep_drop min_def) |
|
4447 |
||
4448 |
lemma map_replicate_True: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4449 |
"n = length xs \<Longrightarrow> |
37660 | 4450 |
map (\<lambda>(x,y). x & y) (zip xs (replicate n True)) = xs" |
4451 |
by (induct xs arbitrary: n) auto |
|
4452 |
||
4453 |
lemma map_replicate_False: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4454 |
"n = length xs \<Longrightarrow> map (\<lambda>(x,y). x & y) |
37660 | 4455 |
(zip xs (replicate n False)) = replicate n False" |
4456 |
by (induct xs arbitrary: n) auto |
|
4457 |
||
4458 |
lemma bl_and_mask: |
|
4459 |
fixes w :: "'a::len word" |
|
4460 |
fixes n |
|
4461 |
defines "n' \<equiv> len_of TYPE('a) - n" |
|
4462 |
shows "to_bl (w AND mask n) = replicate n' False @ drop n' (to_bl w)" |
|
4463 |
proof - |
|
4464 |
note [simp] = map_replicate_True map_replicate_False |
|
4465 |
have "to_bl (w AND mask n) = |
|
4466 |
map2 op & (to_bl w) (to_bl (mask n::'a::len word))" |
|
4467 |
by (simp add: bl_word_and) |
|
4468 |
also |
|
4469 |
have "to_bl w = take n' (to_bl w) @ drop n' (to_bl w)" by simp |
|
4470 |
also |
|
4471 |
have "map2 op & \<dots> (to_bl (mask n::'a::len word)) = |
|
4472 |
replicate n' False @ drop n' (to_bl w)" |
|
4473 |
unfolding to_bl_mask n'_def map2_def |
|
4474 |
by (subst zip_append) auto |
|
4475 |
finally |
|
4476 |
show ?thesis . |
|
4477 |
qed |
|
4478 |
||
4479 |
lemma drop_rev_takefill: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4480 |
"length xs \<le> n \<Longrightarrow> |
37660 | 4481 |
drop (n - length xs) (rev (takefill False n (rev xs))) = xs" |
4482 |
by (simp add: takefill_alt rev_take) |
|
4483 |
||
4484 |
lemma map_nth_0 [simp]: |
|
4485 |
"map (op !! (0::'a::len0 word)) xs = replicate (length xs) False" |
|
4486 |
by (induct xs) auto |
|
4487 |
||
4488 |
lemma uint_plus_if_size: |
|
4489 |
"uint (x + y) = |
|
4490 |
(if uint x + uint y < 2^size x then |
|
4491 |
uint x + uint y |
|
4492 |
else |
|
4493 |
uint x + uint y - 2^size x)" |
|
4494 |
by (simp add: word_arith_alts int_word_uint mod_add_if_z |
|
4495 |
word_size) |
|
4496 |
||
4497 |
lemma unat_plus_if_size: |
|
4498 |
"unat (x + (y::'a::len word)) = |
|
4499 |
(if unat x + unat y < 2^size x then |
|
4500 |
unat x + unat y |
|
4501 |
else |
|
4502 |
unat x + unat y - 2^size x)" |
|
4503 |
apply (subst word_arith_nat_defs) |
|
4504 |
apply (subst unat_of_nat) |
|
4505 |
apply (simp add: mod_nat_add word_size) |
|
4506 |
done |
|
4507 |
||
44938
98e05fc1ce7d
removed word_neq_0_conv from simpset, it's almost never wanted.
kleing
parents:
44890
diff
changeset
|
4508 |
lemma word_neq_0_conv: |
37660 | 4509 |
fixes w :: "'a :: len word" |
4510 |
shows "(w \<noteq> 0) = (0 < w)" |
|
4511 |
proof - |
|
4512 |
have "0 \<le> w" by (rule word_zero_le) |
|
4513 |
thus ?thesis by (auto simp add: word_less_def) |
|
4514 |
qed |
|
4515 |
||
4516 |
lemma max_lt: |
|
4517 |
"unat (max a b div c) = unat (max a b) div unat (c:: 'a :: len word)" |
|
4518 |
apply (subst word_arith_nat_defs) |
|
4519 |
apply (subst word_unat.eq_norm) |
|
4520 |
apply (subst mod_if) |
|
4521 |
apply clarsimp |
|
4522 |
apply (erule notE) |
|
4523 |
apply (insert div_le_dividend [of "unat (max a b)" "unat c"]) |
|
4524 |
apply (erule order_le_less_trans) |
|
4525 |
apply (insert unat_lt2p [of "max a b"]) |
|
4526 |
apply simp |
|
4527 |
done |
|
4528 |
||
4529 |
lemma uint_sub_if_size: |
|
4530 |
"uint (x - y) = |
|
4531 |
(if uint y \<le> uint x then |
|
4532 |
uint x - uint y |
|
4533 |
else |
|
4534 |
uint x - uint y + 2^size x)" |
|
4535 |
by (simp add: word_arith_alts int_word_uint mod_sub_if_z |
|
4536 |
word_size) |
|
4537 |
||
4538 |
lemma unat_sub: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4539 |
"b <= a \<Longrightarrow> unat (a - b) = unat a - unat b" |
37660 | 4540 |
by (simp add: unat_def uint_sub_if_size word_le_def nat_diff_distrib) |
4541 |
||
4542 |
lemmas word_less_sub1_numberof [simp] = |
|
4543 |
word_less_sub1 [of "number_of w", standard] |
|
4544 |
lemmas word_le_sub1_numberof [simp] = |
|
4545 |
word_le_sub1 [of "number_of w", standard] |
|
4546 |
||
4547 |
lemma word_of_int_minus: |
|
4548 |
"word_of_int (2^len_of TYPE('a) - i) = (word_of_int (-i)::'a::len word)" |
|
4549 |
proof - |
|
4550 |
have x: "2^len_of TYPE('a) - i = -i + 2^len_of TYPE('a)" by simp |
|
4551 |
show ?thesis |
|
4552 |
apply (subst x) |
|
4553 |
apply (subst word_uint.Abs_norm [symmetric], subst mod_add_self2) |
|
4554 |
apply simp |
|
4555 |
done |
|
4556 |
qed |
|
4557 |
||
4558 |
lemmas word_of_int_inj = |
|
4559 |
word_uint.Abs_inject [unfolded uints_num, simplified] |
|
4560 |
||
4561 |
lemma word_le_less_eq: |
|
4562 |
"(x ::'z::len word) \<le> y = (x = y \<or> x < y)" |
|
4563 |
by (auto simp add: word_less_def) |
|
4564 |
||
4565 |
lemma mod_plus_cong: |
|
4566 |
assumes 1: "(b::int) = b'" |
|
4567 |
and 2: "x mod b' = x' mod b'" |
|
4568 |
and 3: "y mod b' = y' mod b'" |
|
4569 |
and 4: "x' + y' = z'" |
|
4570 |
shows "(x + y) mod b = z' mod b'" |
|
4571 |
proof - |
|
4572 |
from 1 2[symmetric] 3[symmetric] have "(x + y) mod b = (x' mod b' + y' mod b') mod b'" |
|
4573 |
by (simp add: mod_add_eq[symmetric]) |
|
4574 |
also have "\<dots> = (x' + y') mod b'" |
|
4575 |
by (simp add: mod_add_eq[symmetric]) |
|
4576 |
finally show ?thesis by (simp add: 4) |
|
4577 |
qed |
|
4578 |
||
4579 |
lemma mod_minus_cong: |
|
4580 |
assumes 1: "(b::int) = b'" |
|
4581 |
and 2: "x mod b' = x' mod b'" |
|
4582 |
and 3: "y mod b' = y' mod b'" |
|
4583 |
and 4: "x' - y' = z'" |
|
4584 |
shows "(x - y) mod b = z' mod b'" |
|
4585 |
using assms |
|
4586 |
apply (subst zmod_zsub_left_eq) |
|
4587 |
apply (subst zmod_zsub_right_eq) |
|
4588 |
apply (simp add: zmod_zsub_left_eq [symmetric] zmod_zsub_right_eq [symmetric]) |
|
4589 |
done |
|
4590 |
||
4591 |
lemma word_induct_less: |
|
4592 |
"\<lbrakk>P (0::'a::len word); \<And>n. \<lbrakk>n < m; P n\<rbrakk> \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P m" |
|
4593 |
apply (cases m) |
|
4594 |
apply atomize |
|
4595 |
apply (erule rev_mp)+ |
|
4596 |
apply (rule_tac x=m in spec) |
|
4597 |
apply (induct_tac n) |
|
4598 |
apply simp |
|
4599 |
apply clarsimp |
|
4600 |
apply (erule impE) |
|
4601 |
apply clarsimp |
|
4602 |
apply (erule_tac x=n in allE) |
|
4603 |
apply (erule impE) |
|
4604 |
apply (simp add: unat_arith_simps) |
|
4605 |
apply (clarsimp simp: unat_of_nat) |
|
4606 |
apply simp |
|
4607 |
apply (erule_tac x="of_nat na" in allE) |
|
4608 |
apply (erule impE) |
|
4609 |
apply (simp add: unat_arith_simps) |
|
4610 |
apply (clarsimp simp: unat_of_nat) |
|
4611 |
apply simp |
|
4612 |
done |
|
4613 |
||
4614 |
lemma word_induct: |
|
4615 |
"\<lbrakk>P (0::'a::len word); \<And>n. P n \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P m" |
|
4616 |
by (erule word_induct_less, simp) |
|
4617 |
||
4618 |
lemma word_induct2 [induct type]: |
|
4619 |
"\<lbrakk>P 0; \<And>n. \<lbrakk>1 + n \<noteq> 0; P n\<rbrakk> \<Longrightarrow> P (1 + n)\<rbrakk> \<Longrightarrow> P (n::'b::len word)" |
|
4620 |
apply (rule word_induct, simp) |
|
4621 |
apply (case_tac "1+n = 0", auto) |
|
4622 |
done |
|
4623 |
||
4624 |
definition word_rec :: "'a \<Rightarrow> ('b::len word \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'b word \<Rightarrow> 'a" where |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4625 |
"word_rec forZero forSuc n = nat_rec forZero (forSuc \<circ> of_nat) (unat n)" |
37660 | 4626 |
|
4627 |
lemma word_rec_0: "word_rec z s 0 = z" |
|
4628 |
by (simp add: word_rec_def) |
|
4629 |
||
4630 |
lemma word_rec_Suc: |
|
4631 |
"1 + n \<noteq> (0::'a::len word) \<Longrightarrow> word_rec z s (1 + n) = s n (word_rec z s n)" |
|
4632 |
apply (simp add: word_rec_def unat_word_ariths) |
|
4633 |
apply (subst nat_mod_eq') |
|
4634 |
apply (cut_tac x=n in unat_lt2p) |
|
4635 |
apply (drule Suc_mono) |
|
4636 |
apply (simp add: less_Suc_eq_le) |
|
4637 |
apply (simp only: order_less_le, simp) |
|
4638 |
apply (erule contrapos_pn, simp) |
|
4639 |
apply (drule arg_cong[where f=of_nat]) |
|
4640 |
apply simp |
|
4641 |
apply (subst (asm) word_unat.Rep_inverse[of n]) |
|
4642 |
apply simp |
|
4643 |
apply simp |
|
4644 |
done |
|
4645 |
||
4646 |
lemma word_rec_Pred: |
|
4647 |
"n \<noteq> 0 \<Longrightarrow> word_rec z s n = s (n - 1) (word_rec z s (n - 1))" |
|
4648 |
apply (rule subst[where t="n" and s="1 + (n - 1)"]) |
|
4649 |
apply simp |
|
4650 |
apply (subst word_rec_Suc) |
|
4651 |
apply simp |
|
4652 |
apply simp |
|
4653 |
done |
|
4654 |
||
4655 |
lemma word_rec_in: |
|
4656 |
"f (word_rec z (\<lambda>_. f) n) = word_rec (f z) (\<lambda>_. f) n" |
|
4657 |
by (induct n) (simp_all add: word_rec_0 word_rec_Suc) |
|
4658 |
||
4659 |
lemma word_rec_in2: |
|
4660 |
"f n (word_rec z f n) = word_rec (f 0 z) (f \<circ> op + 1) n" |
|
4661 |
by (induct n) (simp_all add: word_rec_0 word_rec_Suc) |
|
4662 |
||
4663 |
lemma word_rec_twice: |
|
4664 |
"m \<le> n \<Longrightarrow> word_rec z f n = word_rec (word_rec z f (n - m)) (f \<circ> op + (n - m)) m" |
|
4665 |
apply (erule rev_mp) |
|
4666 |
apply (rule_tac x=z in spec) |
|
4667 |
apply (rule_tac x=f in spec) |
|
4668 |
apply (induct n) |
|
4669 |
apply (simp add: word_rec_0) |
|
4670 |
apply clarsimp |
|
4671 |
apply (rule_tac t="1 + n - m" and s="1 + (n - m)" in subst) |
|
4672 |
apply simp |
|
4673 |
apply (case_tac "1 + (n - m) = 0") |
|
4674 |
apply (simp add: word_rec_0) |
|
4675 |
apply (rule_tac f = "word_rec ?a ?b" in arg_cong) |
|
4676 |
apply (rule_tac t="m" and s="m + (1 + (n - m))" in subst) |
|
4677 |
apply simp |
|
4678 |
apply (simp (no_asm_use)) |
|
4679 |
apply (simp add: word_rec_Suc word_rec_in2) |
|
4680 |
apply (erule impE) |
|
4681 |
apply uint_arith |
|
4682 |
apply (drule_tac x="x \<circ> op + 1" in spec) |
|
4683 |
apply (drule_tac x="x 0 xa" in spec) |
|
4684 |
apply simp |
|
4685 |
apply (rule_tac t="\<lambda>a. x (1 + (n - m + a))" and s="\<lambda>a. x (1 + (n - m) + a)" |
|
4686 |
in subst) |
|
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39198
diff
changeset
|
4687 |
apply (clarsimp simp add: fun_eq_iff) |
37660 | 4688 |
apply (rule_tac t="(1 + (n - m + xb))" and s="1 + (n - m) + xb" in subst) |
4689 |
apply simp |
|
4690 |
apply (rule refl) |
|
4691 |
apply (rule refl) |
|
4692 |
done |
|
4693 |
||
4694 |
lemma word_rec_id: "word_rec z (\<lambda>_. id) n = z" |
|
4695 |
by (induct n) (auto simp add: word_rec_0 word_rec_Suc) |
|
4696 |
||
4697 |
lemma word_rec_id_eq: "\<forall>m < n. f m = id \<Longrightarrow> word_rec z f n = z" |
|
4698 |
apply (erule rev_mp) |
|
4699 |
apply (induct n) |
|
4700 |
apply (auto simp add: word_rec_0 word_rec_Suc) |
|
4701 |
apply (drule spec, erule mp) |
|
4702 |
apply uint_arith |
|
4703 |
apply (drule_tac x=n in spec, erule impE) |
|
4704 |
apply uint_arith |
|
4705 |
apply simp |
|
4706 |
done |
|
4707 |
||
4708 |
lemma word_rec_max: |
|
4709 |
"\<forall>m\<ge>n. m \<noteq> -1 \<longrightarrow> f m = id \<Longrightarrow> word_rec z f -1 = word_rec z f n" |
|
4710 |
apply (subst word_rec_twice[where n="-1" and m="-1 - n"]) |
|
4711 |
apply simp |
|
4712 |
apply simp |
|
4713 |
apply (rule word_rec_id_eq) |
|
4714 |
apply clarsimp |
|
4715 |
apply (drule spec, rule mp, erule mp) |
|
4716 |
apply (rule word_plus_mono_right2[OF _ order_less_imp_le]) |
|
4717 |
prefer 2 |
|
4718 |
apply assumption |
|
4719 |
apply simp |
|
4720 |
apply (erule contrapos_pn) |
|
4721 |
apply simp |
|
4722 |
apply (drule arg_cong[where f="\<lambda>x. x - n"]) |
|
4723 |
apply simp |
|
4724 |
done |
|
4725 |
||
4726 |
lemma unatSuc: |
|
4727 |
"1 + n \<noteq> (0::'a::len word) \<Longrightarrow> unat (1 + n) = Suc (unat n)" |
|
4728 |
by unat_arith |
|
4729 |
||
4730 |
||
4731 |
lemmas word_no_1 [simp] = word_1_no [symmetric, unfolded BIT_simps] |
|
4732 |
lemmas word_no_0 [simp] = word_0_no [symmetric] |
|
4733 |
||
4734 |
declare word_0_bl [simp] |
|
4735 |
declare bin_to_bl_def [simp] |
|
4736 |
declare to_bl_0 [simp] |
|
4737 |
declare of_bl_True [simp] |
|
4738 |
||
4739 |
||
41060
4199fdcfa3c0
moved smt_word.ML into the directory of the Word library
boehmes
parents:
40827
diff
changeset
|
4740 |
use "~~/src/HOL/Word/Tools/smt_word.ML" |
37660 | 4741 |
|
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
35049
diff
changeset
|
4742 |
setup {* SMT_Word.setup *} |
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
35049
diff
changeset
|
4743 |
|
41060
4199fdcfa3c0
moved smt_word.ML into the directory of the Word library
boehmes
parents:
40827
diff
changeset
|
4744 |
end |