author | haftmann |
Sun, 15 Nov 2020 10:13:03 +0000 | |
changeset 72611 | c7bc3e70a8c7 |
parent 72515 | c7038c397ae3 |
child 72735 | bbe5d3ef2052 |
permissions | -rw-r--r-- |
72515
c7038c397ae3
moved most material from session HOL-Word to Word_Lib in the AFP
haftmann
parents:
72512
diff
changeset
|
1 |
(* Title: HOL/Library/Word.thy |
c7038c397ae3
moved most material from session HOL-Word to Word_Lib in the AFP
haftmann
parents:
72512
diff
changeset
|
2 |
Author: Jeremy Dawson and Gerwin Klein, NICTA, et. al. |
24333 | 3 |
*) |
4 |
||
61799 | 5 |
section \<open>A type of finite bit strings\<close> |
24350 | 6 |
|
29628 | 7 |
theory Word |
41413
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
wenzelm
parents:
41060
diff
changeset
|
8 |
imports |
66453
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents:
65363
diff
changeset
|
9 |
"HOL-Library.Type_Length" |
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents:
65363
diff
changeset
|
10 |
"HOL-Library.Boolean_Algebra" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
11 |
"HOL-Library.Bit_Operations" |
37660 | 12 |
begin |
13 |
||
72243 | 14 |
subsection \<open>Preliminaries\<close> |
15 |
||
16 |
lemma signed_take_bit_decr_length_iff: |
|
17 |
\<open>signed_take_bit (LENGTH('a::len) - Suc 0) k = signed_take_bit (LENGTH('a) - Suc 0) l |
|
18 |
\<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> |
|
19 |
by (cases \<open>LENGTH('a)\<close>) |
|
20 |
(simp_all add: signed_take_bit_eq_iff_take_bit_eq) |
|
21 |
||
22 |
||
72244 | 23 |
subsection \<open>Fundamentals\<close> |
24 |
||
25 |
subsubsection \<open>Type definition\<close> |
|
37660 | 26 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
27 |
quotient_type (overloaded) 'a word = int / \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len) l\<close> |
72243 | 28 |
morphisms rep Word by (auto intro!: equivpI reflpI sympI transpI) |
29 |
||
30 |
hide_const (open) rep \<comment> \<open>only for foundational purpose\<close> |
|
72130
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
31 |
hide_const (open) Word \<comment> \<open>only for code generation\<close> |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
32 |
|
72244 | 33 |
|
34 |
subsubsection \<open>Basic arithmetic\<close> |
|
35 |
||
72243 | 36 |
instantiation word :: (len) comm_ring_1 |
37 |
begin |
|
38 |
||
39 |
lift_definition zero_word :: \<open>'a word\<close> |
|
40 |
is 0 . |
|
41 |
||
42 |
lift_definition one_word :: \<open>'a word\<close> |
|
43 |
is 1 . |
|
44 |
||
45 |
lift_definition plus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
46 |
is \<open>(+)\<close> |
|
47 |
by (auto simp add: take_bit_eq_mod intro: mod_add_cong) |
|
48 |
||
49 |
lift_definition minus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
50 |
is \<open>(-)\<close> |
|
51 |
by (auto simp add: take_bit_eq_mod intro: mod_diff_cong) |
|
52 |
||
53 |
lift_definition uminus_word :: \<open>'a word \<Rightarrow> 'a word\<close> |
|
54 |
is uminus |
|
55 |
by (auto simp add: take_bit_eq_mod intro: mod_minus_cong) |
|
56 |
||
57 |
lift_definition times_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
58 |
is \<open>(*)\<close> |
|
59 |
by (auto simp add: take_bit_eq_mod intro: mod_mult_cong) |
|
60 |
||
61 |
instance |
|
62 |
by (standard; transfer) (simp_all add: algebra_simps) |
|
63 |
||
64 |
end |
|
65 |
||
66 |
context |
|
67 |
includes lifting_syntax |
|
72244 | 68 |
notes |
69 |
power_transfer [transfer_rule] |
|
70 |
transfer_rule_of_bool [transfer_rule] |
|
71 |
transfer_rule_numeral [transfer_rule] |
|
72 |
transfer_rule_of_nat [transfer_rule] |
|
73 |
transfer_rule_of_int [transfer_rule] |
|
72243 | 74 |
begin |
75 |
||
76 |
lemma power_transfer_word [transfer_rule]: |
|
77 |
\<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close> |
|
78 |
by transfer_prover |
|
79 |
||
72244 | 80 |
lemma [transfer_rule]: |
81 |
\<open>((=) ===> pcr_word) of_bool of_bool\<close> |
|
82 |
by transfer_prover |
|
83 |
||
84 |
lemma [transfer_rule]: |
|
85 |
\<open>((=) ===> pcr_word) numeral numeral\<close> |
|
86 |
by transfer_prover |
|
87 |
||
88 |
lemma [transfer_rule]: |
|
89 |
\<open>((=) ===> pcr_word) int of_nat\<close> |
|
90 |
by transfer_prover |
|
91 |
||
92 |
lemma [transfer_rule]: |
|
93 |
\<open>((=) ===> pcr_word) (\<lambda>k. k) of_int\<close> |
|
94 |
proof - |
|
95 |
have \<open>((=) ===> pcr_word) of_int of_int\<close> |
|
96 |
by transfer_prover |
|
97 |
then show ?thesis by (simp add: id_def) |
|
98 |
qed |
|
99 |
||
100 |
lemma [transfer_rule]: |
|
101 |
\<open>(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)\<close> |
|
102 |
proof - |
|
103 |
have even_word_unfold: "even k \<longleftrightarrow> (\<exists>l. take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l))" (is "?P \<longleftrightarrow> ?Q") |
|
104 |
for k :: int |
|
105 |
proof |
|
106 |
assume ?P |
|
107 |
then show ?Q |
|
108 |
by auto |
|
109 |
next |
|
110 |
assume ?Q |
|
111 |
then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" .. |
|
112 |
then have "even (take_bit LENGTH('a) k)" |
|
113 |
by simp |
|
114 |
then show ?P |
|
115 |
by simp |
|
116 |
qed |
|
117 |
show ?thesis by (simp only: even_word_unfold [abs_def] dvd_def [where ?'a = "'a word", abs_def]) |
|
118 |
transfer_prover |
|
119 |
qed |
|
120 |
||
72243 | 121 |
end |
122 |
||
72512 | 123 |
lemma exp_eq_zero_iff [simp]: |
124 |
\<open>2 ^ n = (0 :: 'a::len word) \<longleftrightarrow> n \<ge> LENGTH('a)\<close> |
|
125 |
by transfer simp |
|
126 |
||
72244 | 127 |
lemma word_exp_length_eq_0 [simp]: |
128 |
\<open>(2 :: 'a::len word) ^ LENGTH('a) = 0\<close> |
|
72512 | 129 |
by simp |
72262 | 130 |
|
72244 | 131 |
|
72489 | 132 |
subsubsection \<open>Basic tool setup\<close> |
133 |
||
134 |
ML_file \<open>Tools/word_lib.ML\<close> |
|
135 |
||
136 |
||
72244 | 137 |
subsubsection \<open>Basic code generation setup\<close> |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
138 |
|
72262 | 139 |
context |
140 |
begin |
|
141 |
||
142 |
qualified lift_definition the_int :: \<open>'a::len word \<Rightarrow> int\<close> |
|
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
143 |
is \<open>take_bit LENGTH('a)\<close> . |
37660 | 144 |
|
72262 | 145 |
end |
146 |
||
72243 | 147 |
lemma [code abstype]: |
72262 | 148 |
\<open>Word.Word (Word.the_int w) = w\<close> |
72243 | 149 |
by transfer simp |
150 |
||
72262 | 151 |
lemma Word_eq_word_of_int [code_post, simp]: |
152 |
\<open>Word.Word = of_int\<close> |
|
153 |
by (rule; transfer) simp |
|
154 |
||
72243 | 155 |
quickcheck_generator word |
156 |
constructors: |
|
157 |
\<open>0 :: 'a::len word\<close>, |
|
158 |
\<open>numeral :: num \<Rightarrow> 'a::len word\<close> |
|
159 |
||
160 |
instantiation word :: (len) equal |
|
161 |
begin |
|
162 |
||
163 |
lift_definition equal_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> bool\<close> |
|
164 |
is \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> |
|
165 |
by simp |
|
166 |
||
167 |
instance |
|
168 |
by (standard; transfer) rule |
|
169 |
||
170 |
end |
|
171 |
||
172 |
lemma [code]: |
|
72262 | 173 |
\<open>HOL.equal v w \<longleftrightarrow> HOL.equal (Word.the_int v) (Word.the_int w)\<close> |
72243 | 174 |
by transfer (simp add: equal) |
175 |
||
176 |
lemma [code]: |
|
72262 | 177 |
\<open>Word.the_int 0 = 0\<close> |
72243 | 178 |
by transfer simp |
179 |
||
180 |
lemma [code]: |
|
72262 | 181 |
\<open>Word.the_int 1 = 1\<close> |
72243 | 182 |
by transfer simp |
183 |
||
184 |
lemma [code]: |
|
72262 | 185 |
\<open>Word.the_int (v + w) = take_bit LENGTH('a) (Word.the_int v + Word.the_int w)\<close> |
72243 | 186 |
for v w :: \<open>'a::len word\<close> |
187 |
by transfer (simp add: take_bit_add) |
|
188 |
||
189 |
lemma [code]: |
|
72262 | 190 |
\<open>Word.the_int (- w) = (let k = Word.the_int w in if w = 0 then 0 else 2 ^ LENGTH('a) - k)\<close> |
72243 | 191 |
for w :: \<open>'a::len word\<close> |
192 |
by transfer (auto simp add: take_bit_eq_mod zmod_zminus1_eq_if) |
|
193 |
||
194 |
lemma [code]: |
|
72262 | 195 |
\<open>Word.the_int (v - w) = take_bit LENGTH('a) (Word.the_int v - Word.the_int w)\<close> |
72243 | 196 |
for v w :: \<open>'a::len word\<close> |
197 |
by transfer (simp add: take_bit_diff) |
|
198 |
||
199 |
lemma [code]: |
|
72262 | 200 |
\<open>Word.the_int (v * w) = take_bit LENGTH('a) (Word.the_int v * Word.the_int w)\<close> |
72243 | 201 |
for v w :: \<open>'a::len word\<close> |
202 |
by transfer (simp add: take_bit_mult) |
|
203 |
||
204 |
||
72244 | 205 |
subsubsection \<open>Basic conversions\<close> |
70185 | 206 |
|
72262 | 207 |
abbreviation word_of_nat :: \<open>nat \<Rightarrow> 'a::len word\<close> |
208 |
where \<open>word_of_nat \<equiv> of_nat\<close> |
|
209 |
||
210 |
abbreviation word_of_int :: \<open>int \<Rightarrow> 'a::len word\<close> |
|
211 |
where \<open>word_of_int \<equiv> of_int\<close> |
|
212 |
||
213 |
lemma word_of_nat_eq_iff: |
|
214 |
\<open>word_of_nat m = (word_of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m = take_bit LENGTH('a) n\<close> |
|
215 |
by transfer (simp add: take_bit_of_nat) |
|
216 |
||
217 |
lemma word_of_int_eq_iff: |
|
218 |
\<open>word_of_int k = (word_of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> |
|
219 |
by transfer rule |
|
220 |
||
221 |
lemma word_of_nat_eq_0_iff [simp]: |
|
222 |
\<open>word_of_nat n = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd n\<close> |
|
223 |
using word_of_nat_eq_iff [where ?'a = 'a, of n 0] by (simp add: take_bit_eq_0_iff) |
|
224 |
||
225 |
lemma word_of_int_eq_0_iff [simp]: |
|
226 |
\<open>word_of_int k = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd k\<close> |
|
227 |
using word_of_int_eq_iff [where ?'a = 'a, of k 0] by (simp add: take_bit_eq_0_iff) |
|
228 |
||
229 |
context semiring_1 |
|
230 |
begin |
|
231 |
||
232 |
lift_definition unsigned :: \<open>'b::len word \<Rightarrow> 'a\<close> |
|
233 |
is \<open>of_nat \<circ> nat \<circ> take_bit LENGTH('b)\<close> |
|
72244 | 234 |
by simp |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
235 |
|
72262 | 236 |
lemma unsigned_0 [simp]: |
237 |
\<open>unsigned 0 = 0\<close> |
|
238 |
by transfer simp |
|
239 |
||
240 |
lemma unsigned_1 [simp]: |
|
241 |
\<open>unsigned 1 = 1\<close> |
|
242 |
by transfer simp |
|
243 |
||
244 |
lemma unsigned_numeral [simp]: |
|
245 |
\<open>unsigned (numeral n :: 'b::len word) = of_nat (take_bit LENGTH('b) (numeral n))\<close> |
|
246 |
by transfer (simp add: nat_take_bit_eq) |
|
247 |
||
248 |
lemma unsigned_neg_numeral [simp]: |
|
249 |
\<open>unsigned (- numeral n :: 'b::len word) = of_nat (nat (take_bit LENGTH('b) (- numeral n)))\<close> |
|
250 |
by transfer simp |
|
251 |
||
252 |
end |
|
253 |
||
254 |
context semiring_1 |
|
255 |
begin |
|
256 |
||
257 |
lemma unsigned_of_nat [simp]: |
|
258 |
\<open>unsigned (word_of_nat n :: 'b::len word) = of_nat (take_bit LENGTH('b) n)\<close> |
|
259 |
by transfer (simp add: nat_eq_iff take_bit_of_nat) |
|
260 |
||
261 |
lemma unsigned_of_int [simp]: |
|
262 |
\<open>unsigned (word_of_int k :: 'b::len word) = of_nat (nat (take_bit LENGTH('b) k))\<close> |
|
263 |
by transfer simp |
|
264 |
||
265 |
end |
|
266 |
||
267 |
context semiring_char_0 |
|
268 |
begin |
|
269 |
||
270 |
lemma unsigned_word_eqI: |
|
271 |
\<open>v = w\<close> if \<open>unsigned v = unsigned w\<close> |
|
272 |
using that by transfer (simp add: eq_nat_nat_iff) |
|
273 |
||
274 |
lemma word_eq_iff_unsigned: |
|
275 |
\<open>v = w \<longleftrightarrow> unsigned v = unsigned w\<close> |
|
276 |
by (auto intro: unsigned_word_eqI) |
|
277 |
||
72292 | 278 |
lemma inj_unsigned [simp]: |
279 |
\<open>inj unsigned\<close> |
|
280 |
by (rule injI) (simp add: unsigned_word_eqI) |
|
281 |
||
282 |
lemma unsigned_eq_0_iff: |
|
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
283 |
\<open>unsigned w = 0 \<longleftrightarrow> w = 0\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
284 |
using word_eq_iff_unsigned [of w 0] by simp |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
285 |
|
72262 | 286 |
end |
287 |
||
288 |
context ring_1 |
|
289 |
begin |
|
290 |
||
291 |
lift_definition signed :: \<open>'b::len word \<Rightarrow> 'a\<close> |
|
292 |
is \<open>of_int \<circ> signed_take_bit (LENGTH('b) - Suc 0)\<close> |
|
293 |
by (simp flip: signed_take_bit_decr_length_iff) |
|
294 |
||
295 |
lemma signed_0 [simp]: |
|
296 |
\<open>signed 0 = 0\<close> |
|
297 |
by transfer simp |
|
298 |
||
299 |
lemma signed_1 [simp]: |
|
300 |
\<open>signed (1 :: 'b::len word) = (if LENGTH('b) = 1 then - 1 else 1)\<close> |
|
72488 | 301 |
by (transfer fixing: uminus; cases \<open>LENGTH('b)\<close>) (auto dest: gr0_implies_Suc) |
72262 | 302 |
|
303 |
lemma signed_minus_1 [simp]: |
|
304 |
\<open>signed (- 1 :: 'b::len word) = - 1\<close> |
|
305 |
by (transfer fixing: uminus) simp |
|
306 |
||
307 |
lemma signed_numeral [simp]: |
|
308 |
\<open>signed (numeral n :: 'b::len word) = of_int (signed_take_bit (LENGTH('b) - 1) (numeral n))\<close> |
|
309 |
by transfer simp |
|
310 |
||
311 |
lemma signed_neg_numeral [simp]: |
|
312 |
\<open>signed (- numeral n :: 'b::len word) = of_int (signed_take_bit (LENGTH('b) - 1) (- numeral n))\<close> |
|
313 |
by transfer simp |
|
314 |
||
315 |
lemma signed_of_nat [simp]: |
|
316 |
\<open>signed (word_of_nat n :: 'b::len word) = of_int (signed_take_bit (LENGTH('b) - Suc 0) (int n))\<close> |
|
317 |
by transfer simp |
|
318 |
||
319 |
lemma signed_of_int [simp]: |
|
320 |
\<open>signed (word_of_int n :: 'b::len word) = of_int (signed_take_bit (LENGTH('b) - Suc 0) n)\<close> |
|
321 |
by transfer simp |
|
322 |
||
323 |
end |
|
324 |
||
325 |
context ring_char_0 |
|
326 |
begin |
|
327 |
||
328 |
lemma signed_word_eqI: |
|
329 |
\<open>v = w\<close> if \<open>signed v = signed w\<close> |
|
330 |
using that by transfer (simp flip: signed_take_bit_decr_length_iff) |
|
331 |
||
332 |
lemma word_eq_iff_signed: |
|
333 |
\<open>v = w \<longleftrightarrow> signed v = signed w\<close> |
|
334 |
by (auto intro: signed_word_eqI) |
|
335 |
||
72292 | 336 |
lemma inj_signed [simp]: |
337 |
\<open>inj signed\<close> |
|
338 |
by (rule injI) (simp add: signed_word_eqI) |
|
339 |
||
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
340 |
lemma signed_eq_0_iff: |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
341 |
\<open>signed w = 0 \<longleftrightarrow> w = 0\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
342 |
using word_eq_iff_signed [of w 0] by simp |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
343 |
|
72262 | 344 |
end |
345 |
||
346 |
abbreviation unat :: \<open>'a::len word \<Rightarrow> nat\<close> |
|
347 |
where \<open>unat \<equiv> unsigned\<close> |
|
348 |
||
349 |
abbreviation uint :: \<open>'a::len word \<Rightarrow> int\<close> |
|
350 |
where \<open>uint \<equiv> unsigned\<close> |
|
351 |
||
352 |
abbreviation sint :: \<open>'a::len word \<Rightarrow> int\<close> |
|
353 |
where \<open>sint \<equiv> signed\<close> |
|
354 |
||
355 |
abbreviation ucast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
356 |
where \<open>ucast \<equiv> unsigned\<close> |
|
357 |
||
358 |
abbreviation scast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
359 |
where \<open>scast \<equiv> signed\<close> |
|
360 |
||
361 |
context |
|
362 |
includes lifting_syntax |
|
363 |
begin |
|
364 |
||
365 |
lemma [transfer_rule]: |
|
366 |
\<open>(pcr_word ===> (=)) (nat \<circ> take_bit LENGTH('a)) (unat :: 'a::len word \<Rightarrow> nat)\<close> |
|
367 |
using unsigned.transfer [where ?'a = nat] by simp |
|
368 |
||
369 |
lemma [transfer_rule]: |
|
370 |
\<open>(pcr_word ===> (=)) (take_bit LENGTH('a)) (uint :: 'a::len word \<Rightarrow> int)\<close> |
|
371 |
using unsigned.transfer [where ?'a = int] by (simp add: comp_def) |
|
372 |
||
373 |
lemma [transfer_rule]: |
|
374 |
\<open>(pcr_word ===> (=)) (signed_take_bit (LENGTH('a) - Suc 0)) (sint :: 'a::len word \<Rightarrow> int)\<close> |
|
375 |
using signed.transfer [where ?'a = int] by simp |
|
376 |
||
377 |
lemma [transfer_rule]: |
|
378 |
\<open>(pcr_word ===> pcr_word) (take_bit LENGTH('a)) (ucast :: 'a::len word \<Rightarrow> 'b::len word)\<close> |
|
379 |
proof (rule rel_funI) |
|
380 |
fix k :: int and w :: \<open>'a word\<close> |
|
381 |
assume \<open>pcr_word k w\<close> |
|
382 |
then have \<open>w = word_of_int k\<close> |
|
383 |
by (simp add: pcr_word_def cr_word_def relcompp_apply) |
|
384 |
moreover have \<open>pcr_word (take_bit LENGTH('a) k) (ucast (word_of_int k :: 'a word))\<close> |
|
385 |
by transfer (simp add: pcr_word_def cr_word_def relcompp_apply) |
|
386 |
ultimately show \<open>pcr_word (take_bit LENGTH('a) k) (ucast w)\<close> |
|
387 |
by simp |
|
388 |
qed |
|
389 |
||
390 |
lemma [transfer_rule]: |
|
391 |
\<open>(pcr_word ===> pcr_word) (signed_take_bit (LENGTH('a) - Suc 0)) (scast :: 'a::len word \<Rightarrow> 'b::len word)\<close> |
|
392 |
proof (rule rel_funI) |
|
393 |
fix k :: int and w :: \<open>'a word\<close> |
|
394 |
assume \<open>pcr_word k w\<close> |
|
395 |
then have \<open>w = word_of_int k\<close> |
|
396 |
by (simp add: pcr_word_def cr_word_def relcompp_apply) |
|
397 |
moreover have \<open>pcr_word (signed_take_bit (LENGTH('a) - Suc 0) k) (scast (word_of_int k :: 'a word))\<close> |
|
398 |
by transfer (simp add: pcr_word_def cr_word_def relcompp_apply) |
|
399 |
ultimately show \<open>pcr_word (signed_take_bit (LENGTH('a) - Suc 0) k) (scast w)\<close> |
|
400 |
by simp |
|
401 |
qed |
|
402 |
||
403 |
end |
|
404 |
||
405 |
lemma of_nat_unat [simp]: |
|
406 |
\<open>of_nat (unat w) = unsigned w\<close> |
|
407 |
by transfer simp |
|
408 |
||
409 |
lemma of_int_uint [simp]: |
|
410 |
\<open>of_int (uint w) = unsigned w\<close> |
|
411 |
by transfer simp |
|
412 |
||
413 |
lemma of_int_sint [simp]: |
|
414 |
\<open>of_int (sint a) = signed a\<close> |
|
415 |
by transfer (simp_all add: take_bit_signed_take_bit) |
|
72079 | 416 |
|
417 |
lemma nat_uint_eq [simp]: |
|
418 |
\<open>nat (uint w) = unat w\<close> |
|
419 |
by transfer simp |
|
420 |
||
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
421 |
lemma sgn_uint_eq [simp]: |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
422 |
\<open>sgn (uint w) = of_bool (w \<noteq> 0)\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
423 |
by transfer (simp add: less_le) |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
424 |
|
72262 | 425 |
text \<open>Aliasses only for code generation\<close> |
426 |
||
427 |
context |
|
428 |
begin |
|
429 |
||
430 |
qualified lift_definition of_int :: \<open>int \<Rightarrow> 'a::len word\<close> |
|
431 |
is \<open>take_bit LENGTH('a)\<close> . |
|
432 |
||
433 |
qualified lift_definition of_nat :: \<open>nat \<Rightarrow> 'a::len word\<close> |
|
434 |
is \<open>int \<circ> take_bit LENGTH('a)\<close> . |
|
435 |
||
436 |
qualified lift_definition the_nat :: \<open>'a::len word \<Rightarrow> nat\<close> |
|
437 |
is \<open>nat \<circ> take_bit LENGTH('a)\<close> by simp |
|
438 |
||
439 |
qualified lift_definition the_signed_int :: \<open>'a::len word \<Rightarrow> int\<close> |
|
440 |
is \<open>signed_take_bit (LENGTH('a) - Suc 0)\<close> by (simp add: signed_take_bit_decr_length_iff) |
|
441 |
||
442 |
qualified lift_definition cast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
443 |
is \<open>take_bit LENGTH('a)\<close> by simp |
|
444 |
||
445 |
qualified lift_definition signed_cast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
446 |
is \<open>signed_take_bit (LENGTH('a) - Suc 0)\<close> by (metis signed_take_bit_decr_length_iff) |
|
447 |
||
448 |
end |
|
449 |
||
450 |
lemma [code_abbrev, simp]: |
|
451 |
\<open>Word.the_int = uint\<close> |
|
452 |
by transfer rule |
|
453 |
||
454 |
lemma [code]: |
|
455 |
\<open>Word.the_int (Word.of_int k :: 'a::len word) = take_bit LENGTH('a) k\<close> |
|
456 |
by transfer simp |
|
457 |
||
458 |
lemma [code_abbrev, simp]: |
|
459 |
\<open>Word.of_int = word_of_int\<close> |
|
460 |
by (rule; transfer) simp |
|
461 |
||
462 |
lemma [code]: |
|
463 |
\<open>Word.the_int (Word.of_nat n :: 'a::len word) = take_bit LENGTH('a) (int n)\<close> |
|
72244 | 464 |
by transfer (simp add: take_bit_of_nat) |
465 |
||
72262 | 466 |
lemma [code_abbrev, simp]: |
467 |
\<open>Word.of_nat = word_of_nat\<close> |
|
468 |
by (rule; transfer) (simp add: take_bit_of_nat) |
|
469 |
||
470 |
lemma [code]: |
|
471 |
\<open>Word.the_nat w = nat (Word.the_int w)\<close> |
|
472 |
by transfer simp |
|
473 |
||
474 |
lemma [code_abbrev, simp]: |
|
475 |
\<open>Word.the_nat = unat\<close> |
|
476 |
by (rule; transfer) simp |
|
477 |
||
478 |
lemma [code]: |
|
479 |
\<open>Word.the_signed_int w = signed_take_bit (LENGTH('a) - Suc 0) (Word.the_int w)\<close> |
|
480 |
for w :: \<open>'a::len word\<close> |
|
72488 | 481 |
by transfer (simp add: signed_take_bit_take_bit) |
72262 | 482 |
|
483 |
lemma [code_abbrev, simp]: |
|
484 |
\<open>Word.the_signed_int = sint\<close> |
|
485 |
by (rule; transfer) simp |
|
486 |
||
487 |
lemma [code]: |
|
488 |
\<open>Word.the_int (Word.cast w :: 'b::len word) = take_bit LENGTH('b) (Word.the_int w)\<close> |
|
489 |
for w :: \<open>'a::len word\<close> |
|
490 |
by transfer simp |
|
491 |
||
492 |
lemma [code_abbrev, simp]: |
|
493 |
\<open>Word.cast = ucast\<close> |
|
494 |
by (rule; transfer) simp |
|
495 |
||
496 |
lemma [code]: |
|
497 |
\<open>Word.the_int (Word.signed_cast w :: 'b::len word) = take_bit LENGTH('b) (Word.the_signed_int w)\<close> |
|
498 |
for w :: \<open>'a::len word\<close> |
|
499 |
by transfer simp |
|
500 |
||
501 |
lemma [code_abbrev, simp]: |
|
502 |
\<open>Word.signed_cast = scast\<close> |
|
503 |
by (rule; transfer) simp |
|
504 |
||
505 |
lemma [code]: |
|
506 |
\<open>unsigned w = of_nat (nat (Word.the_int w))\<close> |
|
507 |
by transfer simp |
|
508 |
||
509 |
lemma [code]: |
|
510 |
\<open>signed w = of_int (Word.the_signed_int w)\<close> |
|
511 |
by transfer simp |
|
72244 | 512 |
|
513 |
||
514 |
subsubsection \<open>Basic ordering\<close> |
|
45547 | 515 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
516 |
instantiation word :: (len) linorder |
45547 | 517 |
begin |
518 |
||
71950 | 519 |
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" |
520 |
is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b" |
|
521 |
by simp |
|
522 |
||
523 |
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" |
|
524 |
is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b" |
|
525 |
by simp |
|
37660 | 526 |
|
45547 | 527 |
instance |
71950 | 528 |
by (standard; transfer) auto |
45547 | 529 |
|
530 |
end |
|
531 |
||
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
532 |
interpretation word_order: ordering_top \<open>(\<le>)\<close> \<open>(<)\<close> \<open>- 1 :: 'a::len word\<close> |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
533 |
by (standard; transfer) (simp add: take_bit_eq_mod zmod_minus1) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
534 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
535 |
interpretation word_coorder: ordering_top \<open>(\<ge>)\<close> \<open>(>)\<close> \<open>0 :: 'a::len word\<close> |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
536 |
by (standard; transfer) simp |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
537 |
|
72262 | 538 |
lemma word_of_nat_less_eq_iff: |
539 |
\<open>word_of_nat m \<le> (word_of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m \<le> take_bit LENGTH('a) n\<close> |
|
540 |
by transfer (simp add: take_bit_of_nat) |
|
541 |
||
542 |
lemma word_of_int_less_eq_iff: |
|
543 |
\<open>word_of_int k \<le> (word_of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k \<le> take_bit LENGTH('a) l\<close> |
|
544 |
by transfer rule |
|
545 |
||
546 |
lemma word_of_nat_less_iff: |
|
547 |
\<open>word_of_nat m < (word_of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m < take_bit LENGTH('a) n\<close> |
|
548 |
by transfer (simp add: take_bit_of_nat) |
|
549 |
||
550 |
lemma word_of_int_less_iff: |
|
551 |
\<open>word_of_int k < (word_of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k < take_bit LENGTH('a) l\<close> |
|
552 |
by transfer rule |
|
553 |
||
71950 | 554 |
lemma word_le_def [code]: |
555 |
"a \<le> b \<longleftrightarrow> uint a \<le> uint b" |
|
556 |
by transfer rule |
|
557 |
||
558 |
lemma word_less_def [code]: |
|
559 |
"a < b \<longleftrightarrow> uint a < uint b" |
|
560 |
by transfer rule |
|
561 |
||
71951 | 562 |
lemma word_greater_zero_iff: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
563 |
\<open>a > 0 \<longleftrightarrow> a \<noteq> 0\<close> for a :: \<open>'a::len word\<close> |
71951 | 564 |
by transfer (simp add: less_le) |
565 |
||
566 |
lemma of_nat_word_less_eq_iff: |
|
567 |
\<open>of_nat m \<le> (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m \<le> take_bit LENGTH('a) n\<close> |
|
568 |
by transfer (simp add: take_bit_of_nat) |
|
569 |
||
570 |
lemma of_nat_word_less_iff: |
|
571 |
\<open>of_nat m < (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m < take_bit LENGTH('a) n\<close> |
|
572 |
by transfer (simp add: take_bit_of_nat) |
|
573 |
||
574 |
lemma of_int_word_less_eq_iff: |
|
575 |
\<open>of_int k \<le> (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k \<le> take_bit LENGTH('a) l\<close> |
|
576 |
by transfer rule |
|
577 |
||
578 |
lemma of_int_word_less_iff: |
|
579 |
\<open>of_int k < (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k < take_bit LENGTH('a) l\<close> |
|
580 |
by transfer rule |
|
581 |
||
37660 | 582 |
|
72280 | 583 |
|
584 |
subsection \<open>Enumeration\<close> |
|
585 |
||
586 |
lemma inj_on_word_of_nat: |
|
587 |
\<open>inj_on (word_of_nat :: nat \<Rightarrow> 'a::len word) {0..<2 ^ LENGTH('a)}\<close> |
|
588 |
by (rule inj_onI; transfer) (simp_all add: take_bit_int_eq_self) |
|
589 |
||
590 |
lemma UNIV_word_eq_word_of_nat: |
|
591 |
\<open>(UNIV :: 'a::len word set) = word_of_nat ` {0..<2 ^ LENGTH('a)}\<close> (is \<open>_ = ?A\<close>) |
|
592 |
proof |
|
593 |
show \<open>word_of_nat ` {0..<2 ^ LENGTH('a)} \<subseteq> UNIV\<close> |
|
594 |
by simp |
|
595 |
show \<open>UNIV \<subseteq> ?A\<close> |
|
596 |
proof |
|
597 |
fix w :: \<open>'a word\<close> |
|
598 |
show \<open>w \<in> (word_of_nat ` {0..<2 ^ LENGTH('a)} :: 'a word set)\<close> |
|
599 |
by (rule image_eqI [of _ _ \<open>unat w\<close>]; transfer) simp_all |
|
600 |
qed |
|
601 |
qed |
|
602 |
||
603 |
instantiation word :: (len) enum |
|
604 |
begin |
|
605 |
||
606 |
definition enum_word :: \<open>'a word list\<close> |
|
607 |
where \<open>enum_word = map word_of_nat [0..<2 ^ LENGTH('a)]\<close> |
|
608 |
||
609 |
definition enum_all_word :: \<open>('a word \<Rightarrow> bool) \<Rightarrow> bool\<close> |
|
610 |
where \<open>enum_all_word = Ball UNIV\<close> |
|
611 |
||
612 |
definition enum_ex_word :: \<open>('a word \<Rightarrow> bool) \<Rightarrow> bool\<close> |
|
613 |
where \<open>enum_ex_word = Bex UNIV\<close> |
|
614 |
||
615 |
lemma [code]: |
|
616 |
\<open>Enum.enum_all P \<longleftrightarrow> Ball UNIV P\<close> |
|
617 |
\<open>Enum.enum_ex P \<longleftrightarrow> Bex UNIV P\<close> for P :: \<open>'a word \<Rightarrow> bool\<close> |
|
618 |
by (simp_all add: enum_all_word_def enum_ex_word_def) |
|
619 |
||
620 |
instance |
|
621 |
by standard (simp_all add: UNIV_word_eq_word_of_nat inj_on_word_of_nat enum_word_def enum_all_word_def enum_ex_word_def distinct_map) |
|
622 |
||
623 |
end |
|
624 |
||
625 |
||
61799 | 626 |
subsection \<open>Bit-wise operations\<close> |
37660 | 627 |
|
72244 | 628 |
instantiation word :: (len) semiring_modulo |
629 |
begin |
|
630 |
||
631 |
lift_definition divide_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
632 |
is \<open>\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b\<close> |
|
633 |
by simp |
|
634 |
||
635 |
lift_definition modulo_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
636 |
is \<open>\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b\<close> |
|
637 |
by simp |
|
638 |
||
639 |
instance proof |
|
640 |
show "a div b * b + a mod b = a" for a b :: "'a word" |
|
641 |
proof transfer |
|
642 |
fix k l :: int |
|
643 |
define r :: int where "r = 2 ^ LENGTH('a)" |
|
644 |
then have r: "take_bit LENGTH('a) k = k mod r" for k |
|
645 |
by (simp add: take_bit_eq_mod) |
|
646 |
have "k mod r = ((k mod r) div (l mod r) * (l mod r) |
|
647 |
+ (k mod r) mod (l mod r)) mod r" |
|
648 |
by (simp add: div_mult_mod_eq) |
|
649 |
also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r |
|
650 |
+ (k mod r) mod (l mod r)) mod r" |
|
651 |
by (simp add: mod_add_left_eq) |
|
652 |
also have "... = (((k mod r) div (l mod r) * l) mod r |
|
653 |
+ (k mod r) mod (l mod r)) mod r" |
|
654 |
by (simp add: mod_mult_right_eq) |
|
655 |
finally have "k mod r = ((k mod r) div (l mod r) * l |
|
656 |
+ (k mod r) mod (l mod r)) mod r" |
|
657 |
by (simp add: mod_simps) |
|
658 |
with r show "take_bit LENGTH('a) (take_bit LENGTH('a) k div take_bit LENGTH('a) l * l |
|
659 |
+ take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k" |
|
660 |
by simp |
|
661 |
qed |
|
662 |
qed |
|
663 |
||
664 |
end |
|
665 |
||
666 |
instance word :: (len) semiring_parity |
|
667 |
proof |
|
668 |
show "\<not> 2 dvd (1::'a word)" |
|
669 |
by transfer simp |
|
670 |
show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0" |
|
671 |
for a :: "'a word" |
|
672 |
by transfer (simp_all add: mod_2_eq_odd take_bit_Suc) |
|
673 |
show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1" |
|
674 |
for a :: "'a word" |
|
675 |
by transfer (simp_all add: mod_2_eq_odd take_bit_Suc) |
|
676 |
qed |
|
677 |
||
71951 | 678 |
lemma word_bit_induct [case_names zero even odd]: |
679 |
\<open>P a\<close> if word_zero: \<open>P 0\<close> |
|
72262 | 680 |
and word_even: \<open>\<And>a. P a \<Longrightarrow> 0 < a \<Longrightarrow> a < 2 ^ (LENGTH('a) - Suc 0) \<Longrightarrow> P (2 * a)\<close> |
681 |
and word_odd: \<open>\<And>a. P a \<Longrightarrow> a < 2 ^ (LENGTH('a) - Suc 0) \<Longrightarrow> P (1 + 2 * a)\<close> |
|
71951 | 682 |
for P and a :: \<open>'a::len word\<close> |
683 |
proof - |
|
72262 | 684 |
define m :: nat where \<open>m = LENGTH('a) - Suc 0\<close> |
71951 | 685 |
then have l: \<open>LENGTH('a) = Suc m\<close> |
686 |
by simp |
|
687 |
define n :: nat where \<open>n = unat a\<close> |
|
688 |
then have \<open>n < 2 ^ LENGTH('a)\<close> |
|
72262 | 689 |
by transfer (simp add: take_bit_eq_mod) |
71951 | 690 |
then have \<open>n < 2 * 2 ^ m\<close> |
691 |
by (simp add: l) |
|
692 |
then have \<open>P (of_nat n)\<close> |
|
693 |
proof (induction n rule: nat_bit_induct) |
|
694 |
case zero |
|
695 |
show ?case |
|
696 |
by simp (rule word_zero) |
|
697 |
next |
|
698 |
case (even n) |
|
699 |
then have \<open>n < 2 ^ m\<close> |
|
700 |
by simp |
|
701 |
with even.IH have \<open>P (of_nat n)\<close> |
|
702 |
by simp |
|
703 |
moreover from \<open>n < 2 ^ m\<close> even.hyps have \<open>0 < (of_nat n :: 'a word)\<close> |
|
72262 | 704 |
by (auto simp add: word_greater_zero_iff l) |
705 |
moreover from \<open>n < 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - Suc 0)\<close> |
|
71951 | 706 |
using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>] |
72261 | 707 |
by (simp add: l take_bit_eq_mod) |
71951 | 708 |
ultimately have \<open>P (2 * of_nat n)\<close> |
709 |
by (rule word_even) |
|
710 |
then show ?case |
|
711 |
by simp |
|
712 |
next |
|
713 |
case (odd n) |
|
714 |
then have \<open>Suc n \<le> 2 ^ m\<close> |
|
715 |
by simp |
|
716 |
with odd.IH have \<open>P (of_nat n)\<close> |
|
717 |
by simp |
|
72262 | 718 |
moreover from \<open>Suc n \<le> 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - Suc 0)\<close> |
71951 | 719 |
using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>] |
72261 | 720 |
by (simp add: l take_bit_eq_mod) |
71951 | 721 |
ultimately have \<open>P (1 + 2 * of_nat n)\<close> |
722 |
by (rule word_odd) |
|
723 |
then show ?case |
|
724 |
by simp |
|
725 |
qed |
|
726 |
moreover have \<open>of_nat (nat (uint a)) = a\<close> |
|
727 |
by transfer simp |
|
728 |
ultimately show ?thesis |
|
72079 | 729 |
by (simp add: n_def) |
71951 | 730 |
qed |
731 |
||
732 |
lemma bit_word_half_eq: |
|
733 |
\<open>(of_bool b + a * 2) div 2 = a\<close> |
|
734 |
if \<open>a < 2 ^ (LENGTH('a) - Suc 0)\<close> |
|
735 |
for a :: \<open>'a::len word\<close> |
|
736 |
proof (cases \<open>2 \<le> LENGTH('a::len)\<close>) |
|
737 |
case False |
|
738 |
have \<open>of_bool (odd k) < (1 :: int) \<longleftrightarrow> even k\<close> for k :: int |
|
739 |
by auto |
|
740 |
with False that show ?thesis |
|
741 |
by transfer (simp add: eq_iff) |
|
742 |
next |
|
743 |
case True |
|
744 |
obtain n where length: \<open>LENGTH('a) = Suc n\<close> |
|
745 |
by (cases \<open>LENGTH('a)\<close>) simp_all |
|
746 |
show ?thesis proof (cases b) |
|
747 |
case False |
|
748 |
moreover have \<open>a * 2 div 2 = a\<close> |
|
749 |
using that proof transfer |
|
750 |
fix k :: int |
|
751 |
from length have \<open>k * 2 mod 2 ^ LENGTH('a) = (k mod 2 ^ n) * 2\<close> |
|
752 |
by simp |
|
753 |
moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close> |
|
754 |
with \<open>LENGTH('a) = Suc n\<close> |
|
755 |
have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close> |
|
756 |
by (simp add: take_bit_eq_mod divmod_digit_0) |
|
757 |
ultimately have \<open>take_bit LENGTH('a) (k * 2) = take_bit LENGTH('a) k * 2\<close> |
|
758 |
by (simp add: take_bit_eq_mod) |
|
759 |
with True show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (k * 2) div take_bit LENGTH('a) 2) |
|
760 |
= take_bit LENGTH('a) k\<close> |
|
761 |
by simp |
|
762 |
qed |
|
763 |
ultimately show ?thesis |
|
764 |
by simp |
|
765 |
next |
|
766 |
case True |
|
767 |
moreover have \<open>(1 + a * 2) div 2 = a\<close> |
|
768 |
using that proof transfer |
|
769 |
fix k :: int |
|
770 |
from length have \<open>(1 + k * 2) mod 2 ^ LENGTH('a) = 1 + (k mod 2 ^ n) * 2\<close> |
|
771 |
using pos_zmod_mult_2 [of \<open>2 ^ n\<close> k] by (simp add: ac_simps) |
|
772 |
moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close> |
|
773 |
with \<open>LENGTH('a) = Suc n\<close> |
|
774 |
have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close> |
|
775 |
by (simp add: take_bit_eq_mod divmod_digit_0) |
|
776 |
ultimately have \<open>take_bit LENGTH('a) (1 + k * 2) = 1 + take_bit LENGTH('a) k * 2\<close> |
|
777 |
by (simp add: take_bit_eq_mod) |
|
778 |
with True show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (1 + k * 2) div take_bit LENGTH('a) 2) |
|
779 |
= take_bit LENGTH('a) k\<close> |
|
780 |
by (auto simp add: take_bit_Suc) |
|
781 |
qed |
|
782 |
ultimately show ?thesis |
|
783 |
by simp |
|
784 |
qed |
|
785 |
qed |
|
786 |
||
787 |
lemma even_mult_exp_div_word_iff: |
|
788 |
\<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> \<not> ( |
|
789 |
m \<le> n \<and> |
|
790 |
n < LENGTH('a) \<and> odd (a div 2 ^ (n - m)))\<close> for a :: \<open>'a::len word\<close> |
|
791 |
by transfer |
|
792 |
(auto simp flip: drop_bit_eq_div simp add: even_drop_bit_iff_not_bit bit_take_bit_iff, |
|
793 |
simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int) |
|
794 |
||
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
795 |
instantiation word :: (len) semiring_bits |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
796 |
begin |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
797 |
|
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
798 |
lift_definition bit_word :: \<open>'a word \<Rightarrow> nat \<Rightarrow> bool\<close> |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
799 |
is \<open>\<lambda>k n. n < LENGTH('a) \<and> bit k n\<close> |
71951 | 800 |
proof |
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
801 |
fix k l :: int and n :: nat |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
802 |
assume *: \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
803 |
show \<open>n < LENGTH('a) \<and> bit k n \<longleftrightarrow> n < LENGTH('a) \<and> bit l n\<close> |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
804 |
proof (cases \<open>n < LENGTH('a)\<close>) |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
805 |
case True |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
806 |
from * have \<open>bit (take_bit LENGTH('a) k) n \<longleftrightarrow> bit (take_bit LENGTH('a) l) n\<close> |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
807 |
by simp |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
808 |
then show ?thesis |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
809 |
by (simp add: bit_take_bit_iff) |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
810 |
next |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
811 |
case False |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
812 |
then show ?thesis |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
813 |
by simp |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
814 |
qed |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
815 |
qed |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
816 |
|
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
817 |
instance proof |
71951 | 818 |
show \<open>P a\<close> if stable: \<open>\<And>a. a div 2 = a \<Longrightarrow> P a\<close> |
819 |
and rec: \<open>\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a)\<close> |
|
820 |
for P and a :: \<open>'a word\<close> |
|
821 |
proof (induction a rule: word_bit_induct) |
|
822 |
case zero |
|
823 |
have \<open>0 div 2 = (0::'a word)\<close> |
|
824 |
by transfer simp |
|
825 |
with stable [of 0] show ?case |
|
826 |
by simp |
|
827 |
next |
|
828 |
case (even a) |
|
829 |
with rec [of a False] show ?case |
|
830 |
using bit_word_half_eq [of a False] by (simp add: ac_simps) |
|
831 |
next |
|
832 |
case (odd a) |
|
833 |
with rec [of a True] show ?case |
|
834 |
using bit_word_half_eq [of a True] by (simp add: ac_simps) |
|
835 |
qed |
|
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
836 |
show \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close> for a :: \<open>'a word\<close> and n |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
837 |
by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit bit_iff_odd_drop_bit) |
71951 | 838 |
show \<open>0 div a = 0\<close> |
839 |
for a :: \<open>'a word\<close> |
|
840 |
by transfer simp |
|
841 |
show \<open>a div 1 = a\<close> |
|
842 |
for a :: \<open>'a word\<close> |
|
843 |
by transfer simp |
|
844 |
show \<open>a mod b div b = 0\<close> |
|
845 |
for a b :: \<open>'a word\<close> |
|
846 |
apply transfer |
|
847 |
apply (simp add: take_bit_eq_mod) |
|
848 |
apply (subst (3) mod_pos_pos_trivial [of _ \<open>2 ^ LENGTH('a)\<close>]) |
|
849 |
apply simp_all |
|
850 |
apply (metis le_less mod_by_0 pos_mod_conj zero_less_numeral zero_less_power) |
|
851 |
using pos_mod_bound [of \<open>2 ^ LENGTH('a)\<close>] apply simp |
|
852 |
proof - |
|
853 |
fix aa :: int and ba :: int |
|
854 |
have f1: "\<And>i n. (i::int) mod 2 ^ n = 0 \<or> 0 < i mod 2 ^ n" |
|
855 |
by (metis le_less take_bit_eq_mod take_bit_nonnegative) |
|
856 |
have "(0::int) < 2 ^ len_of (TYPE('a)::'a itself) \<and> ba mod 2 ^ len_of (TYPE('a)::'a itself) \<noteq> 0 \<or> aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)" |
|
857 |
by (metis (no_types) mod_by_0 unique_euclidean_semiring_numeral_class.pos_mod_bound zero_less_numeral zero_less_power) |
|
858 |
then show "aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)" |
|
859 |
using f1 by (meson le_less less_le_trans unique_euclidean_semiring_numeral_class.pos_mod_bound) |
|
860 |
qed |
|
861 |
show \<open>(1 + a) div 2 = a div 2\<close> |
|
862 |
if \<open>even a\<close> |
|
863 |
for a :: \<open>'a word\<close> |
|
71953 | 864 |
using that by transfer |
865 |
(auto dest: le_Suc_ex simp add: mod_2_eq_odd take_bit_Suc elim!: evenE) |
|
71951 | 866 |
show \<open>(2 :: 'a word) ^ m div 2 ^ n = of_bool ((2 :: 'a word) ^ m \<noteq> 0 \<and> n \<le> m) * 2 ^ (m - n)\<close> |
867 |
for m n :: nat |
|
868 |
by transfer (simp, simp add: exp_div_exp_eq) |
|
869 |
show "a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)" |
|
870 |
for a :: "'a word" and m n :: nat |
|
871 |
apply transfer |
|
872 |
apply (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: drop_bit_eq_div) |
|
873 |
apply (simp add: drop_bit_take_bit) |
|
874 |
done |
|
875 |
show "a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n" |
|
876 |
for a :: "'a word" and m n :: nat |
|
877 |
by transfer (auto simp flip: take_bit_eq_mod simp add: ac_simps) |
|
878 |
show \<open>a * 2 ^ m mod 2 ^ n = a mod 2 ^ (n - m) * 2 ^ m\<close> |
|
879 |
if \<open>m \<le> n\<close> for a :: "'a word" and m n :: nat |
|
880 |
using that apply transfer |
|
881 |
apply (auto simp flip: take_bit_eq_mod) |
|
882 |
apply (auto simp flip: push_bit_eq_mult simp add: push_bit_take_bit split: split_min_lin) |
|
883 |
done |
|
884 |
show \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close> |
|
885 |
for a :: "'a word" and m n :: nat |
|
886 |
by transfer (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: take_bit_eq_mod drop_bit_eq_div split: split_min_lin) |
|
887 |
show \<open>even ((2 ^ m - 1) div (2::'a word) ^ n) \<longleftrightarrow> 2 ^ n = (0::'a word) \<or> m \<le> n\<close> |
|
888 |
for m n :: nat |
|
889 |
by transfer (auto simp add: take_bit_of_mask even_mask_div_iff) |
|
890 |
show \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::'a word) ^ n = 0 \<or> m \<le> n \<and> even (a div 2 ^ (n - m))\<close> |
|
891 |
for a :: \<open>'a word\<close> and m n :: nat |
|
892 |
proof transfer |
|
893 |
show \<open>even (take_bit LENGTH('a) (k * 2 ^ m) div take_bit LENGTH('a) (2 ^ n)) \<longleftrightarrow> |
|
894 |
n < m |
|
895 |
\<or> take_bit LENGTH('a) ((2::int) ^ n) = take_bit LENGTH('a) 0 |
|
896 |
\<or> (m \<le> n \<and> even (take_bit LENGTH('a) k div take_bit LENGTH('a) (2 ^ (n - m))))\<close> |
|
897 |
for m n :: nat and k l :: int |
|
898 |
by (auto simp flip: take_bit_eq_mod drop_bit_eq_div push_bit_eq_mult |
|
899 |
simp add: div_push_bit_of_1_eq_drop_bit drop_bit_take_bit drop_bit_push_bit_int [of n m]) |
|
900 |
qed |
|
901 |
qed |
|
902 |
||
903 |
end |
|
904 |
||
72262 | 905 |
lemma bit_word_eqI: |
906 |
\<open>a = b\<close> if \<open>\<And>n. n < LENGTH('a) \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close> |
|
907 |
for a b :: \<open>'a::len word\<close> |
|
908 |
using that by transfer (auto simp add: nat_less_le bit_eq_iff bit_take_bit_iff) |
|
909 |
||
910 |
lemma bit_imp_le_length: |
|
911 |
\<open>n < LENGTH('a)\<close> if \<open>bit w n\<close> |
|
912 |
for w :: \<open>'a::len word\<close> |
|
913 |
using that by transfer simp |
|
914 |
||
915 |
lemma not_bit_length [simp]: |
|
916 |
\<open>\<not> bit w LENGTH('a)\<close> for w :: \<open>'a::len word\<close> |
|
917 |
by transfer simp |
|
918 |
||
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
919 |
instantiation word :: (len) semiring_bit_shifts |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
920 |
begin |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
921 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
922 |
lift_definition push_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
923 |
is push_bit |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
924 |
proof - |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
925 |
show \<open>take_bit LENGTH('a) (push_bit n k) = take_bit LENGTH('a) (push_bit n l)\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
926 |
if \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> for k l :: int and n :: nat |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
927 |
proof - |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
928 |
from that |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
929 |
have \<open>take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
930 |
= take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
931 |
by simp |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
932 |
moreover have \<open>min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
933 |
by simp |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
934 |
ultimately show ?thesis |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
935 |
by (simp add: take_bit_push_bit) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
936 |
qed |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
937 |
qed |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
938 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
939 |
lift_definition drop_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
940 |
is \<open>\<lambda>n. drop_bit n \<circ> take_bit LENGTH('a)\<close> |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
941 |
by (simp add: take_bit_eq_mod) |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
942 |
|
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
943 |
lift_definition take_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
944 |
is \<open>\<lambda>n. take_bit (min LENGTH('a) n)\<close> |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
945 |
by (simp add: ac_simps) (simp only: flip: take_bit_take_bit) |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
946 |
|
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
947 |
instance proof |
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
948 |
show \<open>push_bit n a = a * 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close> |
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
949 |
by transfer (simp add: push_bit_eq_mult) |
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
950 |
show \<open>drop_bit n a = a div 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close> |
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
951 |
by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit) |
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
952 |
show \<open>take_bit n a = a mod 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close> |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
953 |
by transfer (auto simp flip: take_bit_eq_mod) |
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
954 |
qed |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
955 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
956 |
end |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
957 |
|
72262 | 958 |
lemma [code]: |
72508 | 959 |
\<open>push_bit n w = w * 2 ^ n\<close> for w :: \<open>'a::len word\<close> |
960 |
by (fact push_bit_eq_mult) |
|
961 |
||
962 |
lemma [code]: |
|
963 |
\<open>Word.the_int (drop_bit n w) = drop_bit n (Word.the_int w)\<close> |
|
964 |
by transfer (simp add: drop_bit_take_bit min_def le_less less_diff_conv) |
|
965 |
||
966 |
lemma [code]: |
|
72262 | 967 |
\<open>Word.the_int (take_bit n w) = (if n < LENGTH('a::len) then take_bit n (Word.the_int w) else Word.the_int w)\<close> |
968 |
for w :: \<open>'a::len word\<close> |
|
969 |
by transfer (simp add: not_le not_less ac_simps min_absorb2) |
|
970 |
||
971 |
||
972 |
instantiation word :: (len) ring_bit_operations |
|
973 |
begin |
|
974 |
||
975 |
lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close> |
|
976 |
is not |
|
977 |
by (simp add: take_bit_not_iff) |
|
978 |
||
979 |
lift_definition and_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
980 |
is \<open>and\<close> |
|
981 |
by simp |
|
982 |
||
983 |
lift_definition or_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
984 |
is or |
|
985 |
by simp |
|
986 |
||
987 |
lift_definition xor_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
988 |
is xor |
|
989 |
by simp |
|
990 |
||
991 |
lift_definition mask_word :: \<open>nat \<Rightarrow> 'a word\<close> |
|
992 |
is mask |
|
993 |
. |
|
994 |
||
995 |
instance by (standard; transfer) |
|
996 |
(auto simp add: minus_eq_not_minus_1 mask_eq_exp_minus_1 |
|
997 |
bit_not_iff bit_and_iff bit_or_iff bit_xor_iff) |
|
998 |
||
999 |
end |
|
1000 |
||
1001 |
lemma [code_abbrev]: |
|
1002 |
\<open>push_bit n 1 = (2 :: 'a::len word) ^ n\<close> |
|
1003 |
by (fact push_bit_of_1) |
|
1004 |
||
1005 |
lemma [code]: |
|
1006 |
\<open>NOT w = Word.of_int (NOT (Word.the_int w))\<close> |
|
1007 |
for w :: \<open>'a::len word\<close> |
|
1008 |
by transfer (simp add: take_bit_not_take_bit) |
|
1009 |
||
1010 |
lemma [code]: |
|
1011 |
\<open>Word.the_int (v AND w) = Word.the_int v AND Word.the_int w\<close> |
|
71990 | 1012 |
by transfer simp |
1013 |
||
72262 | 1014 |
lemma [code]: |
1015 |
\<open>Word.the_int (v OR w) = Word.the_int v OR Word.the_int w\<close> |
|
1016 |
by transfer simp |
|
1017 |
||
1018 |
lemma [code]: |
|
1019 |
\<open>Word.the_int (v XOR w) = Word.the_int v XOR Word.the_int w\<close> |
|
1020 |
by transfer simp |
|
1021 |
||
1022 |
lemma [code]: |
|
1023 |
\<open>Word.the_int (mask n :: 'a::len word) = mask (min LENGTH('a) n)\<close> |
|
1024 |
by transfer simp |
|
1025 |
||
1026 |
context |
|
1027 |
includes lifting_syntax |
|
1028 |
begin |
|
1029 |
||
1030 |
lemma set_bit_word_transfer [transfer_rule]: |
|
1031 |
\<open>((=) ===> pcr_word ===> pcr_word) set_bit set_bit\<close> |
|
1032 |
by (unfold set_bit_def) transfer_prover |
|
1033 |
||
1034 |
lemma unset_bit_word_transfer [transfer_rule]: |
|
1035 |
\<open>((=) ===> pcr_word ===> pcr_word) unset_bit unset_bit\<close> |
|
1036 |
by (unfold unset_bit_def) transfer_prover |
|
1037 |
||
1038 |
lemma flip_bit_word_transfer [transfer_rule]: |
|
1039 |
\<open>((=) ===> pcr_word ===> pcr_word) flip_bit flip_bit\<close> |
|
1040 |
by (unfold flip_bit_def) transfer_prover |
|
1041 |
||
1042 |
lemma signed_take_bit_word_transfer [transfer_rule]: |
|
1043 |
\<open>((=) ===> pcr_word ===> pcr_word) |
|
1044 |
(\<lambda>n k. signed_take_bit n (take_bit LENGTH('a::len) k)) |
|
1045 |
(signed_take_bit :: nat \<Rightarrow> 'a word \<Rightarrow> 'a word)\<close> |
|
1046 |
proof - |
|
1047 |
let ?K = \<open>\<lambda>n (k :: int). take_bit (min LENGTH('a) n) k OR of_bool (n < LENGTH('a) \<and> bit k n) * NOT (mask n)\<close> |
|
1048 |
let ?W = \<open>\<lambda>n (w :: 'a word). take_bit n w OR of_bool (bit w n) * NOT (mask n)\<close> |
|
1049 |
have \<open>((=) ===> pcr_word ===> pcr_word) ?K ?W\<close> |
|
1050 |
by transfer_prover |
|
1051 |
also have \<open>?K = (\<lambda>n k. signed_take_bit n (take_bit LENGTH('a::len) k))\<close> |
|
1052 |
by (simp add: fun_eq_iff signed_take_bit_def bit_take_bit_iff ac_simps) |
|
1053 |
also have \<open>?W = signed_take_bit\<close> |
|
1054 |
by (simp add: fun_eq_iff signed_take_bit_def) |
|
1055 |
finally show ?thesis . |
|
1056 |
qed |
|
1057 |
||
1058 |
end |
|
1059 |
||
72244 | 1060 |
|
1061 |
subsection \<open>Conversions including casts\<close> |
|
1062 |
||
72262 | 1063 |
subsubsection \<open>Generic unsigned conversion\<close> |
1064 |
||
1065 |
context semiring_bits |
|
1066 |
begin |
|
1067 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
1068 |
lemma bit_unsigned_iff [bit_simps]: |
72262 | 1069 |
\<open>bit (unsigned w) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> bit w n\<close> |
1070 |
for w :: \<open>'b::len word\<close> |
|
1071 |
by (transfer fixing: bit) (simp add: bit_of_nat_iff bit_nat_iff bit_take_bit_iff) |
|
1072 |
||
1073 |
end |
|
1074 |
||
1075 |
context semiring_bit_shifts |
|
1076 |
begin |
|
1077 |
||
1078 |
lemma unsigned_push_bit_eq: |
|
1079 |
\<open>unsigned (push_bit n w) = take_bit LENGTH('b) (push_bit n (unsigned w))\<close> |
|
1080 |
for w :: \<open>'b::len word\<close> |
|
1081 |
proof (rule bit_eqI) |
|
1082 |
fix m |
|
1083 |
assume \<open>2 ^ m \<noteq> 0\<close> |
|
1084 |
show \<open>bit (unsigned (push_bit n w)) m = bit (take_bit LENGTH('b) (push_bit n (unsigned w))) m\<close> |
|
1085 |
proof (cases \<open>n \<le> m\<close>) |
|
1086 |
case True |
|
1087 |
with \<open>2 ^ m \<noteq> 0\<close> have \<open>2 ^ (m - n) \<noteq> 0\<close> |
|
1088 |
by (metis (full_types) diff_add exp_add_not_zero_imp) |
|
1089 |
with True show ?thesis |
|
1090 |
by (simp add: bit_unsigned_iff bit_push_bit_iff Parity.bit_push_bit_iff bit_take_bit_iff not_le exp_eq_zero_iff ac_simps) |
|
1091 |
next |
|
1092 |
case False |
|
1093 |
then show ?thesis |
|
1094 |
by (simp add: not_le bit_unsigned_iff bit_push_bit_iff Parity.bit_push_bit_iff bit_take_bit_iff) |
|
1095 |
qed |
|
1096 |
qed |
|
1097 |
||
1098 |
lemma unsigned_take_bit_eq: |
|
1099 |
\<open>unsigned (take_bit n w) = take_bit n (unsigned w)\<close> |
|
1100 |
for w :: \<open>'b::len word\<close> |
|
1101 |
by (rule bit_eqI) (simp add: bit_unsigned_iff bit_take_bit_iff Parity.bit_take_bit_iff) |
|
1102 |
||
1103 |
end |
|
1104 |
||
72512 | 1105 |
context unique_euclidean_semiring_with_bit_shifts |
1106 |
begin |
|
1107 |
||
1108 |
lemma unsigned_drop_bit_eq: |
|
1109 |
\<open>unsigned (drop_bit n w) = drop_bit n (take_bit LENGTH('b) (unsigned w))\<close> |
|
1110 |
for w :: \<open>'b::len word\<close> |
|
1111 |
by (rule bit_eqI) (auto simp add: bit_unsigned_iff bit_take_bit_iff bit_drop_bit_eq Parity.bit_drop_bit_eq dest: bit_imp_le_length) |
|
1112 |
||
1113 |
end |
|
1114 |
||
72262 | 1115 |
context semiring_bit_operations |
1116 |
begin |
|
1117 |
||
1118 |
lemma unsigned_and_eq: |
|
1119 |
\<open>unsigned (v AND w) = unsigned v AND unsigned w\<close> |
|
1120 |
for v w :: \<open>'b::len word\<close> |
|
1121 |
by (rule bit_eqI) (simp add: bit_unsigned_iff bit_and_iff Bit_Operations.bit_and_iff) |
|
1122 |
||
1123 |
lemma unsigned_or_eq: |
|
1124 |
\<open>unsigned (v OR w) = unsigned v OR unsigned w\<close> |
|
1125 |
for v w :: \<open>'b::len word\<close> |
|
1126 |
by (rule bit_eqI) (simp add: bit_unsigned_iff bit_or_iff Bit_Operations.bit_or_iff) |
|
1127 |
||
1128 |
lemma unsigned_xor_eq: |
|
1129 |
\<open>unsigned (v XOR w) = unsigned v XOR unsigned w\<close> |
|
1130 |
for v w :: \<open>'b::len word\<close> |
|
1131 |
by (rule bit_eqI) (simp add: bit_unsigned_iff bit_xor_iff Bit_Operations.bit_xor_iff) |
|
1132 |
||
1133 |
end |
|
1134 |
||
1135 |
context ring_bit_operations |
|
1136 |
begin |
|
1137 |
||
1138 |
lemma unsigned_not_eq: |
|
1139 |
\<open>unsigned (NOT w) = take_bit LENGTH('b) (NOT (unsigned w))\<close> |
|
1140 |
for w :: \<open>'b::len word\<close> |
|
1141 |
by (rule bit_eqI) |
|
1142 |
(simp add: bit_unsigned_iff bit_take_bit_iff bit_not_iff Bit_Operations.bit_not_iff exp_eq_zero_iff not_le) |
|
1143 |
||
1144 |
end |
|
1145 |
||
1146 |
context unique_euclidean_semiring_numeral |
|
1147 |
begin |
|
1148 |
||
72292 | 1149 |
lemma unsigned_greater_eq [simp]: |
72262 | 1150 |
\<open>0 \<le> unsigned w\<close> for w :: \<open>'b::len word\<close> |
1151 |
by (transfer fixing: less_eq) simp |
|
1152 |
||
72292 | 1153 |
lemma unsigned_less [simp]: |
72262 | 1154 |
\<open>unsigned w < 2 ^ LENGTH('b)\<close> for w :: \<open>'b::len word\<close> |
1155 |
by (transfer fixing: less) simp |
|
1156 |
||
1157 |
end |
|
1158 |
||
1159 |
context linordered_semidom |
|
1160 |
begin |
|
1161 |
||
1162 |
lemma word_less_eq_iff_unsigned: |
|
1163 |
"a \<le> b \<longleftrightarrow> unsigned a \<le> unsigned b" |
|
1164 |
by (transfer fixing: less_eq) (simp add: nat_le_eq_zle) |
|
1165 |
||
1166 |
lemma word_less_iff_unsigned: |
|
1167 |
"a < b \<longleftrightarrow> unsigned a < unsigned b" |
|
1168 |
by (transfer fixing: less) (auto dest: preorder_class.le_less_trans [OF take_bit_nonnegative]) |
|
1169 |
||
1170 |
end |
|
1171 |
||
1172 |
||
1173 |
subsubsection \<open>Generic signed conversion\<close> |
|
1174 |
||
1175 |
context ring_bit_operations |
|
1176 |
begin |
|
1177 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
1178 |
lemma bit_signed_iff [bit_simps]: |
72262 | 1179 |
\<open>bit (signed w) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> bit w (min (LENGTH('b) - Suc 0) n)\<close> |
1180 |
for w :: \<open>'b::len word\<close> |
|
1181 |
by (transfer fixing: bit) |
|
1182 |
(auto simp add: bit_of_int_iff Bit_Operations.bit_signed_take_bit_iff min_def) |
|
1183 |
||
1184 |
lemma signed_push_bit_eq: |
|
1185 |
\<open>signed (push_bit n w) = signed_take_bit (LENGTH('b) - Suc 0) (push_bit n (signed w :: 'a))\<close> |
|
1186 |
for w :: \<open>'b::len word\<close> |
|
1187 |
proof (rule bit_eqI) |
|
1188 |
fix m |
|
1189 |
assume \<open>2 ^ m \<noteq> 0\<close> |
|
1190 |
define q where \<open>q = LENGTH('b) - Suc 0\<close> |
|
1191 |
then have *: \<open>LENGTH('b) = Suc q\<close> |
|
1192 |
by simp |
|
1193 |
show \<open>bit (signed (push_bit n w)) m \<longleftrightarrow> |
|
1194 |
bit (signed_take_bit (LENGTH('b) - Suc 0) (push_bit n (signed w :: 'a))) m\<close> |
|
1195 |
proof (cases \<open>q \<le> m\<close>) |
|
1196 |
case True |
|
1197 |
moreover define r where \<open>r = m - q\<close> |
|
1198 |
ultimately have \<open>m = q + r\<close> |
|
1199 |
by simp |
|
1200 |
moreover from \<open>m = q + r\<close> \<open>2 ^ m \<noteq> 0\<close> have \<open>2 ^ q \<noteq> 0\<close> \<open>2 ^ r \<noteq> 0\<close> |
|
1201 |
using exp_add_not_zero_imp_left [of q r] exp_add_not_zero_imp_right [of q r] |
|
1202 |
by simp_all |
|
1203 |
moreover from \<open>2 ^ q \<noteq> 0\<close> have \<open>2 ^ (q - n) \<noteq> 0\<close> |
|
1204 |
by (rule exp_not_zero_imp_exp_diff_not_zero) |
|
1205 |
ultimately show ?thesis |
|
1206 |
by (auto simp add: bit_signed_iff bit_signed_take_bit_iff bit_push_bit_iff Parity.bit_push_bit_iff |
|
1207 |
min_def * exp_eq_zero_iff le_diff_conv2) |
|
1208 |
next |
|
1209 |
case False |
|
1210 |
then show ?thesis |
|
1211 |
using exp_not_zero_imp_exp_diff_not_zero [of m n] |
|
1212 |
by (auto simp add: bit_signed_iff bit_signed_take_bit_iff bit_push_bit_iff Parity.bit_push_bit_iff |
|
1213 |
min_def not_le not_less * le_diff_conv2 less_diff_conv2 Parity.exp_eq_0_imp_not_bit exp_eq_0_imp_not_bit |
|
1214 |
exp_eq_zero_iff) |
|
1215 |
qed |
|
1216 |
qed |
|
1217 |
||
1218 |
lemma signed_take_bit_eq: |
|
1219 |
\<open>signed (take_bit n w) = (if n < LENGTH('b) then take_bit n (signed w) else signed w)\<close> |
|
1220 |
for w :: \<open>'b::len word\<close> |
|
1221 |
by (transfer fixing: take_bit; cases \<open>LENGTH('b)\<close>) |
|
1222 |
(auto simp add: Bit_Operations.signed_take_bit_take_bit Bit_Operations.take_bit_signed_take_bit take_bit_of_int min_def less_Suc_eq) |
|
1223 |
||
1224 |
lemma signed_not_eq: |
|
1225 |
\<open>signed (NOT w) = signed_take_bit LENGTH('b) (NOT (signed w))\<close> |
|
1226 |
for w :: \<open>'b::len word\<close> |
|
1227 |
proof (rule bit_eqI) |
|
1228 |
fix n |
|
1229 |
assume \<open>2 ^ n \<noteq> 0\<close> |
|
1230 |
define q where \<open>q = LENGTH('b) - Suc 0\<close> |
|
1231 |
then have *: \<open>LENGTH('b) = Suc q\<close> |
|
1232 |
by simp |
|
1233 |
show \<open>bit (signed (NOT w)) n \<longleftrightarrow> |
|
1234 |
bit (signed_take_bit LENGTH('b) (NOT (signed w))) n\<close> |
|
1235 |
proof (cases \<open>q < n\<close>) |
|
1236 |
case True |
|
1237 |
moreover define r where \<open>r = n - Suc q\<close> |
|
1238 |
ultimately have \<open>n = r + Suc q\<close> |
|
1239 |
by simp |
|
1240 |
moreover from \<open>2 ^ n \<noteq> 0\<close> \<open>n = r + Suc q\<close> |
|
1241 |
have \<open>2 ^ Suc q \<noteq> 0\<close> |
|
1242 |
using exp_add_not_zero_imp_right by blast |
|
1243 |
ultimately show ?thesis |
|
1244 |
by (simp add: * bit_signed_iff bit_not_iff bit_signed_take_bit_iff Bit_Operations.bit_not_iff min_def |
|
1245 |
exp_eq_zero_iff) |
|
1246 |
next |
|
1247 |
case False |
|
1248 |
then show ?thesis |
|
1249 |
by (auto simp add: * bit_signed_iff bit_not_iff bit_signed_take_bit_iff Bit_Operations.bit_not_iff min_def |
|
1250 |
exp_eq_zero_iff) |
|
1251 |
qed |
|
1252 |
qed |
|
1253 |
||
1254 |
lemma signed_and_eq: |
|
1255 |
\<open>signed (v AND w) = signed v AND signed w\<close> |
|
1256 |
for v w :: \<open>'b::len word\<close> |
|
1257 |
by (rule bit_eqI) (simp add: bit_signed_iff bit_and_iff Bit_Operations.bit_and_iff) |
|
1258 |
||
1259 |
lemma signed_or_eq: |
|
1260 |
\<open>signed (v OR w) = signed v OR signed w\<close> |
|
1261 |
for v w :: \<open>'b::len word\<close> |
|
1262 |
by (rule bit_eqI) (simp add: bit_signed_iff bit_or_iff Bit_Operations.bit_or_iff) |
|
1263 |
||
1264 |
lemma signed_xor_eq: |
|
1265 |
\<open>signed (v XOR w) = signed v XOR signed w\<close> |
|
1266 |
for v w :: \<open>'b::len word\<close> |
|
1267 |
by (rule bit_eqI) (simp add: bit_signed_iff bit_xor_iff Bit_Operations.bit_xor_iff) |
|
1268 |
||
1269 |
end |
|
1270 |
||
1271 |
||
1272 |
subsubsection \<open>More\<close> |
|
1273 |
||
1274 |
lemma sint_greater_eq: |
|
1275 |
\<open>- (2 ^ (LENGTH('a) - Suc 0)) \<le> sint w\<close> for w :: \<open>'a::len word\<close> |
|
1276 |
proof (cases \<open>bit w (LENGTH('a) - Suc 0)\<close>) |
|
1277 |
case True |
|
1278 |
then show ?thesis |
|
1279 |
by transfer (simp add: signed_take_bit_eq_if_negative minus_exp_eq_not_mask or_greater_eq ac_simps) |
|
1280 |
next |
|
1281 |
have *: \<open>- (2 ^ (LENGTH('a) - Suc 0)) \<le> (0::int)\<close> |
|
1282 |
by simp |
|
1283 |
case False |
|
1284 |
then show ?thesis |
|
1285 |
by transfer (auto simp add: signed_take_bit_eq intro: order_trans *) |
|
1286 |
qed |
|
1287 |
||
1288 |
lemma sint_less: |
|
1289 |
\<open>sint w < 2 ^ (LENGTH('a) - Suc 0)\<close> for w :: \<open>'a::len word\<close> |
|
1290 |
by (cases \<open>bit w (LENGTH('a) - Suc 0)\<close>; transfer) |
|
1291 |
(simp_all add: signed_take_bit_eq signed_take_bit_def not_eq_complement mask_eq_exp_minus_1 OR_upper) |
|
1292 |
||
1293 |
lemma unat_div_distrib: |
|
1294 |
\<open>unat (v div w) = unat v div unat w\<close> |
|
1295 |
proof transfer |
|
1296 |
fix k l |
|
1297 |
have \<open>nat (take_bit LENGTH('a) k) div nat (take_bit LENGTH('a) l) \<le> nat (take_bit LENGTH('a) k)\<close> |
|
1298 |
by (rule div_le_dividend) |
|
1299 |
also have \<open>nat (take_bit LENGTH('a) k) < 2 ^ LENGTH('a)\<close> |
|
1300 |
by (simp add: nat_less_iff) |
|
1301 |
finally show \<open>(nat \<circ> take_bit LENGTH('a)) (take_bit LENGTH('a) k div take_bit LENGTH('a) l) = |
|
1302 |
(nat \<circ> take_bit LENGTH('a)) k div (nat \<circ> take_bit LENGTH('a)) l\<close> |
|
1303 |
by (simp add: nat_take_bit_eq div_int_pos_iff nat_div_distrib take_bit_nat_eq_self_iff) |
|
1304 |
qed |
|
1305 |
||
1306 |
lemma unat_mod_distrib: |
|
1307 |
\<open>unat (v mod w) = unat v mod unat w\<close> |
|
1308 |
proof transfer |
|
1309 |
fix k l |
|
1310 |
have \<open>nat (take_bit LENGTH('a) k) mod nat (take_bit LENGTH('a) l) \<le> nat (take_bit LENGTH('a) k)\<close> |
|
1311 |
by (rule mod_less_eq_dividend) |
|
1312 |
also have \<open>nat (take_bit LENGTH('a) k) < 2 ^ LENGTH('a)\<close> |
|
1313 |
by (simp add: nat_less_iff) |
|
1314 |
finally show \<open>(nat \<circ> take_bit LENGTH('a)) (take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = |
|
1315 |
(nat \<circ> take_bit LENGTH('a)) k mod (nat \<circ> take_bit LENGTH('a)) l\<close> |
|
1316 |
by (simp add: nat_take_bit_eq mod_int_pos_iff less_le nat_mod_distrib take_bit_nat_eq_self_iff) |
|
1317 |
qed |
|
1318 |
||
1319 |
lemma uint_div_distrib: |
|
1320 |
\<open>uint (v div w) = uint v div uint w\<close> |
|
1321 |
proof - |
|
1322 |
have \<open>int (unat (v div w)) = int (unat v div unat w)\<close> |
|
1323 |
by (simp add: unat_div_distrib) |
|
1324 |
then show ?thesis |
|
1325 |
by (simp add: of_nat_div) |
|
1326 |
qed |
|
1327 |
||
72388 | 1328 |
lemma unat_drop_bit_eq: |
1329 |
\<open>unat (drop_bit n w) = drop_bit n (unat w)\<close> |
|
1330 |
by (rule bit_eqI) (simp add: bit_unsigned_iff bit_drop_bit_eq) |
|
1331 |
||
72262 | 1332 |
lemma uint_mod_distrib: |
1333 |
\<open>uint (v mod w) = uint v mod uint w\<close> |
|
1334 |
proof - |
|
1335 |
have \<open>int (unat (v mod w)) = int (unat v mod unat w)\<close> |
|
1336 |
by (simp add: unat_mod_distrib) |
|
1337 |
then show ?thesis |
|
1338 |
by (simp add: of_nat_mod) |
|
1339 |
qed |
|
1340 |
||
1341 |
context semiring_bit_shifts |
|
1342 |
begin |
|
1343 |
||
1344 |
lemma unsigned_ucast_eq: |
|
1345 |
\<open>unsigned (ucast w :: 'c::len word) = take_bit LENGTH('c) (unsigned w)\<close> |
|
1346 |
for w :: \<open>'b::len word\<close> |
|
1347 |
by (rule bit_eqI) (simp add: bit_unsigned_iff Word.bit_unsigned_iff bit_take_bit_iff exp_eq_zero_iff not_le) |
|
1348 |
||
1349 |
end |
|
1350 |
||
1351 |
context ring_bit_operations |
|
1352 |
begin |
|
1353 |
||
1354 |
lemma signed_ucast_eq: |
|
1355 |
\<open>signed (ucast w :: 'c::len word) = signed_take_bit (LENGTH('c) - Suc 0) (unsigned w)\<close> |
|
1356 |
for w :: \<open>'b::len word\<close> |
|
1357 |
proof (rule bit_eqI) |
|
1358 |
fix n |
|
1359 |
assume \<open>2 ^ n \<noteq> 0\<close> |
|
1360 |
then have \<open>2 ^ (min (LENGTH('c) - Suc 0) n) \<noteq> 0\<close> |
|
1361 |
by (simp add: min_def) |
|
1362 |
(metis (mono_tags) diff_diff_cancel exp_not_zero_imp_exp_diff_not_zero) |
|
1363 |
then show \<open>bit (signed (ucast w :: 'c::len word)) n \<longleftrightarrow> bit (signed_take_bit (LENGTH('c) - Suc 0) (unsigned w)) n\<close> |
|
1364 |
by (simp add: bit_signed_iff bit_unsigned_iff Word.bit_unsigned_iff bit_signed_take_bit_iff exp_eq_zero_iff not_le) |
|
1365 |
qed |
|
1366 |
||
1367 |
lemma signed_scast_eq: |
|
1368 |
\<open>signed (scast w :: 'c::len word) = signed_take_bit (LENGTH('c) - Suc 0) (signed w)\<close> |
|
1369 |
for w :: \<open>'b::len word\<close> |
|
1370 |
proof (rule bit_eqI) |
|
1371 |
fix n |
|
1372 |
assume \<open>2 ^ n \<noteq> 0\<close> |
|
1373 |
then have \<open>2 ^ (min (LENGTH('c) - Suc 0) n) \<noteq> 0\<close> |
|
1374 |
by (simp add: min_def) |
|
1375 |
(metis (mono_tags) diff_diff_cancel exp_not_zero_imp_exp_diff_not_zero) |
|
1376 |
then show \<open>bit (signed (scast w :: 'c::len word)) n \<longleftrightarrow> bit (signed_take_bit (LENGTH('c) - Suc 0) (signed w)) n\<close> |
|
1377 |
by (simp add: bit_signed_iff bit_unsigned_iff Word.bit_signed_iff bit_signed_take_bit_iff exp_eq_zero_iff not_le) |
|
1378 |
qed |
|
1379 |
||
1380 |
end |
|
1381 |
||
72244 | 1382 |
lemma uint_nonnegative: "0 \<le> uint w" |
72262 | 1383 |
by (fact unsigned_greater_eq) |
72244 | 1384 |
|
1385 |
lemma uint_bounded: "uint w < 2 ^ LENGTH('a)" |
|
1386 |
for w :: "'a::len word" |
|
72262 | 1387 |
by (fact unsigned_less) |
72244 | 1388 |
|
1389 |
lemma uint_idem: "uint w mod 2 ^ LENGTH('a) = uint w" |
|
1390 |
for w :: "'a::len word" |
|
72262 | 1391 |
by transfer (simp add: take_bit_eq_mod) |
72244 | 1392 |
|
1393 |
lemma word_uint_eqI: "uint a = uint b \<Longrightarrow> a = b" |
|
72262 | 1394 |
by (fact unsigned_word_eqI) |
72244 | 1395 |
|
1396 |
lemma word_uint_eq_iff: "a = b \<longleftrightarrow> uint a = uint b" |
|
72262 | 1397 |
by (fact word_eq_iff_unsigned) |
1398 |
||
1399 |
lemma uint_word_of_int_eq: |
|
72244 | 1400 |
\<open>uint (word_of_int k :: 'a::len word) = take_bit LENGTH('a) k\<close> |
1401 |
by transfer rule |
|
1402 |
||
1403 |
lemma uint_word_of_int: "uint (word_of_int k :: 'a::len word) = k mod 2 ^ LENGTH('a)" |
|
1404 |
by (simp add: uint_word_of_int_eq take_bit_eq_mod) |
|
1405 |
||
1406 |
lemma word_of_int_uint: "word_of_int (uint w) = w" |
|
1407 |
by transfer simp |
|
1408 |
||
1409 |
lemma word_div_def [code]: |
|
1410 |
"a div b = word_of_int (uint a div uint b)" |
|
1411 |
by transfer rule |
|
1412 |
||
1413 |
lemma word_mod_def [code]: |
|
1414 |
"a mod b = word_of_int (uint a mod uint b)" |
|
1415 |
by transfer rule |
|
1416 |
||
1417 |
lemma split_word_all: "(\<And>x::'a::len word. PROP P x) \<equiv> (\<And>x. PROP P (word_of_int x))" |
|
1418 |
proof |
|
1419 |
fix x :: "'a word" |
|
1420 |
assume "\<And>x. PROP P (word_of_int x)" |
|
1421 |
then have "PROP P (word_of_int (uint x))" . |
|
72262 | 1422 |
then show "PROP P x" |
1423 |
by (simp only: word_of_int_uint) |
|
72244 | 1424 |
qed |
1425 |
||
72262 | 1426 |
lemma sint_uint: |
1427 |
\<open>sint w = signed_take_bit (LENGTH('a) - Suc 0) (uint w)\<close> |
|
72244 | 1428 |
for w :: \<open>'a::len word\<close> |
1429 |
by (cases \<open>LENGTH('a)\<close>; transfer) (simp_all add: signed_take_bit_take_bit) |
|
1430 |
||
72262 | 1431 |
lemma unat_eq_nat_uint: |
72244 | 1432 |
\<open>unat w = nat (uint w)\<close> |
1433 |
by simp |
|
1434 |
||
72262 | 1435 |
lemma ucast_eq: |
72244 | 1436 |
\<open>ucast w = word_of_int (uint w)\<close> |
1437 |
by transfer simp |
|
1438 |
||
72262 | 1439 |
lemma scast_eq: |
72244 | 1440 |
\<open>scast w = word_of_int (sint w)\<close> |
1441 |
by transfer simp |
|
1442 |
||
72262 | 1443 |
lemma uint_0_eq: |
72244 | 1444 |
\<open>uint 0 = 0\<close> |
72262 | 1445 |
by (fact unsigned_0) |
1446 |
||
1447 |
lemma uint_1_eq: |
|
72244 | 1448 |
\<open>uint 1 = 1\<close> |
72262 | 1449 |
by (fact unsigned_1) |
72244 | 1450 |
|
1451 |
lemma word_m1_wi: "- 1 = word_of_int (- 1)" |
|
72262 | 1452 |
by simp |
72244 | 1453 |
|
1454 |
lemma uint_0_iff: "uint x = 0 \<longleftrightarrow> x = 0" |
|
72262 | 1455 |
by (auto simp add: unsigned_word_eqI) |
72244 | 1456 |
|
1457 |
lemma unat_0_iff: "unat x = 0 \<longleftrightarrow> x = 0" |
|
72262 | 1458 |
by (auto simp add: unsigned_word_eqI) |
1459 |
||
1460 |
lemma unat_0: "unat 0 = 0" |
|
1461 |
by (fact unsigned_0) |
|
72244 | 1462 |
|
1463 |
lemma unat_gt_0: "0 < unat x \<longleftrightarrow> x \<noteq> 0" |
|
1464 |
by (auto simp: unat_0_iff [symmetric]) |
|
1465 |
||
72262 | 1466 |
lemma ucast_0: "ucast 0 = 0" |
1467 |
by (fact unsigned_0) |
|
1468 |
||
1469 |
lemma sint_0: "sint 0 = 0" |
|
1470 |
by (fact signed_0) |
|
1471 |
||
1472 |
lemma scast_0: "scast 0 = 0" |
|
1473 |
by (fact signed_0) |
|
1474 |
||
1475 |
lemma sint_n1: "sint (- 1) = - 1" |
|
1476 |
by (fact signed_minus_1) |
|
1477 |
||
1478 |
lemma scast_n1: "scast (- 1) = - 1" |
|
1479 |
by (fact signed_minus_1) |
|
72244 | 1480 |
|
1481 |
lemma uint_1: "uint (1::'a::len word) = 1" |
|
1482 |
by (fact uint_1_eq) |
|
1483 |
||
72262 | 1484 |
lemma unat_1: "unat (1::'a::len word) = 1" |
1485 |
by (fact unsigned_1) |
|
1486 |
||
1487 |
lemma ucast_1: "ucast (1::'a::len word) = 1" |
|
1488 |
by (fact unsigned_1) |
|
72244 | 1489 |
|
1490 |
instantiation word :: (len) size |
|
1491 |
begin |
|
1492 |
||
1493 |
lift_definition size_word :: \<open>'a word \<Rightarrow> nat\<close> |
|
1494 |
is \<open>\<lambda>_. LENGTH('a)\<close> .. |
|
1495 |
||
1496 |
instance .. |
|
1497 |
||
1498 |
end |
|
1499 |
||
1500 |
lemma word_size [code]: |
|
1501 |
\<open>size w = LENGTH('a)\<close> for w :: \<open>'a::len word\<close> |
|
1502 |
by (fact size_word.rep_eq) |
|
1503 |
||
1504 |
lemma word_size_gt_0 [iff]: "0 < size w" |
|
1505 |
for w :: "'a::len word" |
|
1506 |
by (simp add: word_size) |
|
1507 |
||
1508 |
lemmas lens_gt_0 = word_size_gt_0 len_gt_0 |
|
1509 |
||
1510 |
lemma lens_not_0 [iff]: |
|
1511 |
\<open>size w \<noteq> 0\<close> for w :: \<open>'a::len word\<close> |
|
1512 |
by auto |
|
1513 |
||
1514 |
lift_definition source_size :: \<open>('a::len word \<Rightarrow> 'b) \<Rightarrow> nat\<close> |
|
1515 |
is \<open>\<lambda>_. LENGTH('a)\<close> . |
|
1516 |
||
1517 |
lift_definition target_size :: \<open>('a \<Rightarrow> 'b::len word) \<Rightarrow> nat\<close> |
|
1518 |
is \<open>\<lambda>_. LENGTH('b)\<close> .. |
|
1519 |
||
1520 |
lift_definition is_up :: \<open>('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool\<close> |
|
1521 |
is \<open>\<lambda>_. LENGTH('a) \<le> LENGTH('b)\<close> .. |
|
1522 |
||
1523 |
lift_definition is_down :: \<open>('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool\<close> |
|
1524 |
is \<open>\<lambda>_. LENGTH('a) \<ge> LENGTH('b)\<close> .. |
|
1525 |
||
1526 |
lemma is_up_eq: |
|
1527 |
\<open>is_up f \<longleftrightarrow> source_size f \<le> target_size f\<close> |
|
1528 |
for f :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
1529 |
by (simp add: source_size.rep_eq target_size.rep_eq is_up.rep_eq) |
|
1530 |
||
1531 |
lemma is_down_eq: |
|
1532 |
\<open>is_down f \<longleftrightarrow> target_size f \<le> source_size f\<close> |
|
1533 |
for f :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
1534 |
by (simp add: source_size.rep_eq target_size.rep_eq is_down.rep_eq) |
|
1535 |
||
1536 |
lift_definition word_int_case :: \<open>(int \<Rightarrow> 'b) \<Rightarrow> 'a::len word \<Rightarrow> 'b\<close> |
|
1537 |
is \<open>\<lambda>f. f \<circ> take_bit LENGTH('a)\<close> by simp |
|
1538 |
||
1539 |
lemma word_int_case_eq_uint [code]: |
|
1540 |
\<open>word_int_case f w = f (uint w)\<close> |
|
1541 |
by transfer simp |
|
1542 |
||
1543 |
translations |
|
1544 |
"case x of XCONST of_int y \<Rightarrow> b" \<rightleftharpoons> "CONST word_int_case (\<lambda>y. b) x" |
|
1545 |
"case x of (XCONST of_int :: 'a) y \<Rightarrow> b" \<rightharpoonup> "CONST word_int_case (\<lambda>y. b) x" |
|
1546 |
||
1547 |
||
1548 |
subsection \<open>Arithmetic operations\<close> |
|
1549 |
||
1550 |
text \<open>Legacy theorems:\<close> |
|
1551 |
||
1552 |
lemma word_add_def [code]: |
|
1553 |
"a + b = word_of_int (uint a + uint b)" |
|
1554 |
by transfer (simp add: take_bit_add) |
|
1555 |
||
1556 |
lemma word_sub_wi [code]: |
|
1557 |
"a - b = word_of_int (uint a - uint b)" |
|
1558 |
by transfer (simp add: take_bit_diff) |
|
1559 |
||
1560 |
lemma word_mult_def [code]: |
|
1561 |
"a * b = word_of_int (uint a * uint b)" |
|
1562 |
by transfer (simp add: take_bit_eq_mod mod_simps) |
|
1563 |
||
1564 |
lemma word_minus_def [code]: |
|
1565 |
"- a = word_of_int (- uint a)" |
|
1566 |
by transfer (simp add: take_bit_minus) |
|
1567 |
||
1568 |
lemma word_0_wi: |
|
1569 |
"0 = word_of_int 0" |
|
1570 |
by transfer simp |
|
1571 |
||
1572 |
lemma word_1_wi: |
|
1573 |
"1 = word_of_int 1" |
|
1574 |
by transfer simp |
|
1575 |
||
1576 |
lift_definition word_succ :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x + 1" |
|
1577 |
by (auto simp add: take_bit_eq_mod intro: mod_add_cong) |
|
1578 |
||
1579 |
lift_definition word_pred :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x - 1" |
|
1580 |
by (auto simp add: take_bit_eq_mod intro: mod_diff_cong) |
|
1581 |
||
1582 |
lemma word_succ_alt [code]: |
|
1583 |
"word_succ a = word_of_int (uint a + 1)" |
|
1584 |
by transfer (simp add: take_bit_eq_mod mod_simps) |
|
1585 |
||
1586 |
lemma word_pred_alt [code]: |
|
1587 |
"word_pred a = word_of_int (uint a - 1)" |
|
1588 |
by transfer (simp add: take_bit_eq_mod mod_simps) |
|
1589 |
||
1590 |
lemmas word_arith_wis = |
|
1591 |
word_add_def word_sub_wi word_mult_def |
|
1592 |
word_minus_def word_succ_alt word_pred_alt |
|
1593 |
word_0_wi word_1_wi |
|
1594 |
||
1595 |
lemma wi_homs: |
|
1596 |
shows wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" |
|
1597 |
and wi_hom_sub: "word_of_int a - word_of_int b = word_of_int (a - b)" |
|
1598 |
and wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" |
|
1599 |
and wi_hom_neg: "- word_of_int a = word_of_int (- a)" |
|
1600 |
and wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)" |
|
1601 |
and wi_hom_pred: "word_pred (word_of_int a) = word_of_int (a - 1)" |
|
1602 |
by (transfer, simp)+ |
|
1603 |
||
1604 |
lemmas wi_hom_syms = wi_homs [symmetric] |
|
1605 |
||
1606 |
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi |
|
1607 |
||
1608 |
lemmas word_of_int_hom_syms = word_of_int_homs [symmetric] |
|
1609 |
||
1610 |
lemma double_eq_zero_iff: |
|
1611 |
\<open>2 * a = 0 \<longleftrightarrow> a = 0 \<or> a = 2 ^ (LENGTH('a) - Suc 0)\<close> |
|
1612 |
for a :: \<open>'a::len word\<close> |
|
1613 |
proof - |
|
1614 |
define n where \<open>n = LENGTH('a) - Suc 0\<close> |
|
1615 |
then have *: \<open>LENGTH('a) = Suc n\<close> |
|
1616 |
by simp |
|
1617 |
have \<open>a = 0\<close> if \<open>2 * a = 0\<close> and \<open>a \<noteq> 2 ^ (LENGTH('a) - Suc 0)\<close> |
|
1618 |
using that by transfer |
|
1619 |
(auto simp add: take_bit_eq_0_iff take_bit_eq_mod *) |
|
1620 |
moreover have \<open>2 ^ LENGTH('a) = (0 :: 'a word)\<close> |
|
1621 |
by transfer simp |
|
1622 |
then have \<open>2 * 2 ^ (LENGTH('a) - Suc 0) = (0 :: 'a word)\<close> |
|
1623 |
by (simp add: *) |
|
1624 |
ultimately show ?thesis |
|
1625 |
by auto |
|
1626 |
qed |
|
1627 |
||
1628 |
||
1629 |
subsection \<open>Ordering\<close> |
|
1630 |
||
72388 | 1631 |
lift_definition word_sle :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> bool\<close> |
1632 |
is \<open>\<lambda>k l. signed_take_bit (LENGTH('a) - Suc 0) k \<le> signed_take_bit (LENGTH('a) - Suc 0) l\<close> |
|
1633 |
by (simp flip: signed_take_bit_decr_length_iff) |
|
1634 |
||
1635 |
lift_definition word_sless :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> bool\<close> |
|
1636 |
is \<open>\<lambda>k l. signed_take_bit (LENGTH('a) - Suc 0) k < signed_take_bit (LENGTH('a) - Suc 0) l\<close> |
|
72244 | 1637 |
by (simp flip: signed_take_bit_decr_length_iff) |
1638 |
||
72388 | 1639 |
notation |
1640 |
word_sle ("'(\<le>s')") and |
|
1641 |
word_sle ("(_/ \<le>s _)" [51, 51] 50) and |
|
1642 |
word_sless ("'(<s')") and |
|
1643 |
word_sless ("(_/ <s _)" [51, 51] 50) |
|
1644 |
||
1645 |
notation (input) |
|
1646 |
word_sle ("(_/ <=s _)" [51, 51] 50) |
|
1647 |
||
72244 | 1648 |
lemma word_sle_eq [code]: |
1649 |
\<open>a <=s b \<longleftrightarrow> sint a \<le> sint b\<close> |
|
1650 |
by transfer simp |
|
1651 |
||
1652 |
lemma [code]: |
|
1653 |
\<open>a <s b \<longleftrightarrow> sint a < sint b\<close> |
|
1654 |
by transfer simp |
|
1655 |
||
72388 | 1656 |
lemma signed_ordering: \<open>ordering word_sle word_sless\<close> |
1657 |
apply (standard; transfer) |
|
1658 |
apply simp_all |
|
1659 |
using signed_take_bit_decr_length_iff apply force |
|
1660 |
using signed_take_bit_decr_length_iff apply force |
|
1661 |
done |
|
1662 |
||
1663 |
lemma signed_linorder: \<open>class.linorder word_sle word_sless\<close> |
|
1664 |
by (standard; transfer) (auto simp add: signed_take_bit_decr_length_iff) |
|
1665 |
||
1666 |
interpretation signed: linorder word_sle word_sless |
|
1667 |
by (fact signed_linorder) |
|
1668 |
||
1669 |
lemma word_sless_eq: |
|
1670 |
\<open>x <s y \<longleftrightarrow> x <=s y \<and> x \<noteq> y\<close> |
|
1671 |
by (fact signed.less_le) |
|
1672 |
||
72244 | 1673 |
lemma word_less_alt: "a < b \<longleftrightarrow> uint a < uint b" |
1674 |
by (fact word_less_def) |
|
1675 |
||
1676 |
lemma word_zero_le [simp]: "0 \<le> y" |
|
1677 |
for y :: "'a::len word" |
|
72388 | 1678 |
by (fact word_coorder.extremum) |
72244 | 1679 |
|
1680 |
lemma word_m1_ge [simp] : "word_pred 0 \<ge> y" (* FIXME: delete *) |
|
72488 | 1681 |
by transfer (simp add: take_bit_minus_one_eq_mask mask_eq_exp_minus_1 ) |
72244 | 1682 |
|
1683 |
lemma word_n1_ge [simp]: "y \<le> -1" |
|
1684 |
for y :: "'a::len word" |
|
1685 |
by (fact word_order.extremum) |
|
1686 |
||
1687 |
lemmas word_not_simps [simp] = |
|
1688 |
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] |
|
1689 |
||
1690 |
lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> y" |
|
1691 |
for y :: "'a::len word" |
|
1692 |
by (simp add: less_le) |
|
1693 |
||
1694 |
lemmas word_gt_0_no [simp] = word_gt_0 [of "numeral y"] for y |
|
1695 |
||
1696 |
lemma word_sless_alt: "a <s b \<longleftrightarrow> sint a < sint b" |
|
1697 |
by transfer simp |
|
1698 |
||
1699 |
lemma word_le_nat_alt: "a \<le> b \<longleftrightarrow> unat a \<le> unat b" |
|
1700 |
by transfer (simp add: nat_le_eq_zle) |
|
1701 |
||
1702 |
lemma word_less_nat_alt: "a < b \<longleftrightarrow> unat a < unat b" |
|
1703 |
by transfer (auto simp add: less_le [of 0]) |
|
1704 |
||
1705 |
lemmas unat_mono = word_less_nat_alt [THEN iffD1] |
|
1706 |
||
1707 |
instance word :: (len) wellorder |
|
1708 |
proof |
|
1709 |
fix P :: "'a word \<Rightarrow> bool" and a |
|
1710 |
assume *: "(\<And>b. (\<And>a. a < b \<Longrightarrow> P a) \<Longrightarrow> P b)" |
|
1711 |
have "wf (measure unat)" .. |
|
1712 |
moreover have "{(a, b :: ('a::len) word). a < b} \<subseteq> measure unat" |
|
1713 |
by (auto simp add: word_less_nat_alt) |
|
1714 |
ultimately have "wf {(a, b :: ('a::len) word). a < b}" |
|
1715 |
by (rule wf_subset) |
|
1716 |
then show "P a" using * |
|
1717 |
by induction blast |
|
1718 |
qed |
|
1719 |
||
1720 |
lemma wi_less: |
|
1721 |
"(word_of_int n < (word_of_int m :: 'a::len word)) = |
|
1722 |
(n mod 2 ^ LENGTH('a) < m mod 2 ^ LENGTH('a))" |
|
1723 |
by transfer (simp add: take_bit_eq_mod) |
|
1724 |
||
1725 |
lemma wi_le: |
|
1726 |
"(word_of_int n \<le> (word_of_int m :: 'a::len word)) = |
|
1727 |
(n mod 2 ^ LENGTH('a) \<le> m mod 2 ^ LENGTH('a))" |
|
1728 |
by transfer (simp add: take_bit_eq_mod) |
|
1729 |
||
1730 |
||
1731 |
subsection \<open>Bit-wise operations\<close> |
|
1732 |
||
72262 | 1733 |
lemma uint_take_bit_eq: |
72079 | 1734 |
\<open>uint (take_bit n w) = take_bit n (uint w)\<close> |
1735 |
by transfer (simp add: ac_simps) |
|
1736 |
||
72227 | 1737 |
lemma take_bit_word_eq_self: |
1738 |
\<open>take_bit n w = w\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close> |
|
1739 |
using that by transfer simp |
|
1740 |
||
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
1741 |
lemma take_bit_length_eq [simp]: |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
1742 |
\<open>take_bit LENGTH('a) w = w\<close> for w :: \<open>'a::len word\<close> |
72227 | 1743 |
by (rule take_bit_word_eq_self) simp |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
1744 |
|
71990 | 1745 |
lemma bit_word_of_int_iff: |
1746 |
\<open>bit (word_of_int k :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> bit k n\<close> |
|
1747 |
by transfer rule |
|
1748 |
||
1749 |
lemma bit_uint_iff: |
|
1750 |
\<open>bit (uint w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w n\<close> |
|
1751 |
for w :: \<open>'a::len word\<close> |
|
1752 |
by transfer (simp add: bit_take_bit_iff) |
|
1753 |
||
1754 |
lemma bit_sint_iff: |
|
1755 |
\<open>bit (sint w) n \<longleftrightarrow> n \<ge> LENGTH('a) \<and> bit w (LENGTH('a) - 1) \<or> bit w n\<close> |
|
1756 |
for w :: \<open>'a::len word\<close> |
|
72079 | 1757 |
by transfer (auto simp add: bit_signed_take_bit_iff min_def le_less not_less) |
71990 | 1758 |
|
1759 |
lemma bit_word_ucast_iff: |
|
1760 |
\<open>bit (ucast w :: 'b::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> n < LENGTH('b) \<and> bit w n\<close> |
|
1761 |
for w :: \<open>'a::len word\<close> |
|
72079 | 1762 |
by transfer (simp add: bit_take_bit_iff ac_simps) |
71990 | 1763 |
|
1764 |
lemma bit_word_scast_iff: |
|
1765 |
\<open>bit (scast w :: 'b::len word) n \<longleftrightarrow> |
|
1766 |
n < LENGTH('b) \<and> (bit w n \<or> LENGTH('a) \<le> n \<and> bit w (LENGTH('a) - Suc 0))\<close> |
|
1767 |
for w :: \<open>'a::len word\<close> |
|
72079 | 1768 |
by transfer (auto simp add: bit_signed_take_bit_iff le_less min_def) |
1769 |
||
1770 |
lift_definition shiftl1 :: \<open>'a::len word \<Rightarrow> 'a word\<close> |
|
1771 |
is \<open>(*) 2\<close> |
|
1772 |
by (auto simp add: take_bit_eq_mod intro: mod_mult_cong) |
|
1773 |
||
1774 |
lemma shiftl1_eq: |
|
1775 |
\<open>shiftl1 w = word_of_int (2 * uint w)\<close> |
|
1776 |
by transfer (simp add: take_bit_eq_mod mod_simps) |
|
70191 | 1777 |
|
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
1778 |
lemma shiftl1_eq_mult_2: |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
1779 |
\<open>shiftl1 = (*) 2\<close> |
72079 | 1780 |
by (rule ext, transfer) simp |
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
1781 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
1782 |
lemma bit_shiftl1_iff [bit_simps]: |
71990 | 1783 |
\<open>bit (shiftl1 w) n \<longleftrightarrow> 0 < n \<and> n < LENGTH('a) \<and> bit w (n - 1)\<close> |
1784 |
for w :: \<open>'a::len word\<close> |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
1785 |
by (simp add: shiftl1_eq_mult_2 bit_double_iff not_le) (simp add: ac_simps) |
71990 | 1786 |
|
72079 | 1787 |
lift_definition shiftr1 :: \<open>'a::len word \<Rightarrow> 'a word\<close> |
70191 | 1788 |
\<comment> \<open>shift right as unsigned or as signed, ie logical or arithmetic\<close> |
72488 | 1789 |
is \<open>\<lambda>k. take_bit LENGTH('a) k div 2\<close> |
1790 |
by simp |
|
70191 | 1791 |
|
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
1792 |
lemma shiftr1_eq_div_2: |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
1793 |
\<open>shiftr1 w = w div 2\<close> |
72079 | 1794 |
by transfer simp |
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
1795 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
1796 |
lemma bit_shiftr1_iff [bit_simps]: |
71990 | 1797 |
\<open>bit (shiftr1 w) n \<longleftrightarrow> bit w (Suc n)\<close> |
72079 | 1798 |
by transfer (auto simp flip: bit_Suc simp add: bit_take_bit_iff) |
1799 |
||
1800 |
lemma shiftr1_eq: |
|
72128 | 1801 |
\<open>shiftr1 w = word_of_int (uint w div 2)\<close> |
72079 | 1802 |
by transfer simp |
71990 | 1803 |
|
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
1804 |
lemma bit_word_iff_drop_bit_and [code]: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
1805 |
\<open>bit a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close> for a :: \<open>'a::len word\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
1806 |
by (simp add: bit_iff_odd_drop_bit odd_iff_mod_2_eq_one and_one_eq) |
72079 | 1807 |
|
1808 |
lemma |
|
72262 | 1809 |
word_not_def: "NOT (a::'a::len word) = word_of_int (NOT (uint a))" |
65268 | 1810 |
and word_and_def: "(a::'a word) AND b = word_of_int (uint a AND uint b)" |
1811 |
and word_or_def: "(a::'a word) OR b = word_of_int (uint a OR uint b)" |
|
1812 |
and word_xor_def: "(a::'a word) XOR b = word_of_int (uint a XOR uint b)" |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
1813 |
by (transfer, simp add: take_bit_not_take_bit)+ |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
1814 |
|
72079 | 1815 |
lift_definition setBit :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> 'a word\<close> |
1816 |
is \<open>\<lambda>k n. set_bit n k\<close> |
|
1817 |
by (simp add: take_bit_set_bit_eq) |
|
1818 |
||
1819 |
lemma set_Bit_eq: |
|
1820 |
\<open>setBit w n = set_bit n w\<close> |
|
1821 |
by transfer simp |
|
71990 | 1822 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
1823 |
lemma bit_setBit_iff [bit_simps]: |
71990 | 1824 |
\<open>bit (setBit w m) n \<longleftrightarrow> (m = n \<and> n < LENGTH('a) \<or> bit w n)\<close> |
1825 |
for w :: \<open>'a::len word\<close> |
|
72079 | 1826 |
by transfer (auto simp add: bit_set_bit_iff) |
1827 |
||
1828 |
lift_definition clearBit :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> 'a word\<close> |
|
1829 |
is \<open>\<lambda>k n. unset_bit n k\<close> |
|
1830 |
by (simp add: take_bit_unset_bit_eq) |
|
1831 |
||
1832 |
lemma clear_Bit_eq: |
|
1833 |
\<open>clearBit w n = unset_bit n w\<close> |
|
1834 |
by transfer simp |
|
71990 | 1835 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
1836 |
lemma bit_clearBit_iff [bit_simps]: |
71990 | 1837 |
\<open>bit (clearBit w m) n \<longleftrightarrow> m \<noteq> n \<and> bit w n\<close> |
1838 |
for w :: \<open>'a::len word\<close> |
|
72079 | 1839 |
by transfer (auto simp add: bit_unset_bit_iff) |
71990 | 1840 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
1841 |
definition even_word :: \<open>'a::len word \<Rightarrow> bool\<close> |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
1842 |
where [code_abbrev]: \<open>even_word = even\<close> |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
1843 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
1844 |
lemma even_word_iff [code]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
1845 |
\<open>even_word a \<longleftrightarrow> a AND 1 = 0\<close> |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
1846 |
by (simp add: and_one_eq even_iff_mod_2_eq_zero even_word_def) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
1847 |
|
72079 | 1848 |
lemma map_bit_range_eq_if_take_bit_eq: |
1849 |
\<open>map (bit k) [0..<n] = map (bit l) [0..<n]\<close> |
|
1850 |
if \<open>take_bit n k = take_bit n l\<close> for k l :: int |
|
1851 |
using that proof (induction n arbitrary: k l) |
|
1852 |
case 0 |
|
1853 |
then show ?case |
|
1854 |
by simp |
|
1855 |
next |
|
1856 |
case (Suc n) |
|
1857 |
from Suc.prems have \<open>take_bit n (k div 2) = take_bit n (l div 2)\<close> |
|
1858 |
by (simp add: take_bit_Suc) |
|
1859 |
then have \<open>map (bit (k div 2)) [0..<n] = map (bit (l div 2)) [0..<n]\<close> |
|
1860 |
by (rule Suc.IH) |
|
1861 |
moreover have \<open>bit (r div 2) = bit r \<circ> Suc\<close> for r :: int |
|
1862 |
by (simp add: fun_eq_iff bit_Suc) |
|
1863 |
moreover from Suc.prems have \<open>even k \<longleftrightarrow> even l\<close> |
|
1864 |
by (auto simp add: take_bit_Suc elim!: evenE oddE) arith+ |
|
1865 |
ultimately show ?case |
|
1866 |
by (simp only: map_Suc_upt upt_conv_Cons flip: list.map_comp) simp |
|
1867 |
qed |
|
1868 |
||
72262 | 1869 |
lemma |
1870 |
take_bit_word_Bit0_eq [simp]: \<open>take_bit (numeral n) (numeral (num.Bit0 m) :: 'a::len word) |
|
1871 |
= 2 * take_bit (pred_numeral n) (numeral m)\<close> (is ?P) |
|
1872 |
and take_bit_word_Bit1_eq [simp]: \<open>take_bit (numeral n) (numeral (num.Bit1 m) :: 'a::len word) |
|
1873 |
= 1 + 2 * take_bit (pred_numeral n) (numeral m)\<close> (is ?Q) |
|
1874 |
and take_bit_word_minus_Bit0_eq [simp]: \<open>take_bit (numeral n) (- numeral (num.Bit0 m) :: 'a::len word) |
|
1875 |
= 2 * take_bit (pred_numeral n) (- numeral m)\<close> (is ?R) |
|
1876 |
and take_bit_word_minus_Bit1_eq [simp]: \<open>take_bit (numeral n) (- numeral (num.Bit1 m) :: 'a::len word) |
|
1877 |
= 1 + 2 * take_bit (pred_numeral n) (- numeral (Num.inc m))\<close> (is ?S) |
|
1878 |
proof - |
|
1879 |
define w :: \<open>'a::len word\<close> |
|
1880 |
where \<open>w = numeral m\<close> |
|
1881 |
moreover define q :: nat |
|
1882 |
where \<open>q = pred_numeral n\<close> |
|
1883 |
ultimately have num: |
|
1884 |
\<open>numeral m = w\<close> |
|
1885 |
\<open>numeral (num.Bit0 m) = 2 * w\<close> |
|
1886 |
\<open>numeral (num.Bit1 m) = 1 + 2 * w\<close> |
|
1887 |
\<open>numeral (Num.inc m) = 1 + w\<close> |
|
1888 |
\<open>pred_numeral n = q\<close> |
|
1889 |
\<open>numeral n = Suc q\<close> |
|
1890 |
by (simp_all only: w_def q_def numeral_Bit0 [of m] numeral_Bit1 [of m] ac_simps |
|
1891 |
numeral_inc numeral_eq_Suc flip: mult_2) |
|
1892 |
have even: \<open>take_bit (Suc q) (2 * w) = 2 * take_bit q w\<close> for w :: \<open>'a::len word\<close> |
|
1893 |
by (rule bit_word_eqI) |
|
1894 |
(auto simp add: bit_take_bit_iff bit_double_iff) |
|
1895 |
have odd: \<open>take_bit (Suc q) (1 + 2 * w) = 1 + 2 * take_bit q w\<close> for w :: \<open>'a::len word\<close> |
|
1896 |
by (rule bit_eqI) |
|
1897 |
(auto simp add: bit_take_bit_iff bit_double_iff even_bit_succ_iff) |
|
1898 |
show ?P |
|
1899 |
using even [of w] by (simp add: num) |
|
1900 |
show ?Q |
|
1901 |
using odd [of w] by (simp add: num) |
|
1902 |
show ?R |
|
1903 |
using even [of \<open>- w\<close>] by (simp add: num) |
|
1904 |
show ?S |
|
1905 |
using odd [of \<open>- (1 + w)\<close>] by (simp add: num) |
|
1906 |
qed |
|
1907 |
||
72079 | 1908 |
|
1909 |
subsection \<open>More shift operations\<close> |
|
1910 |
||
72388 | 1911 |
lift_definition signed_drop_bit :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a::len word\<close> |
1912 |
is \<open>\<lambda>n. drop_bit n \<circ> signed_take_bit (LENGTH('a) - Suc 0)\<close> |
|
1913 |
using signed_take_bit_decr_length_iff |
|
1914 |
by (simp add: take_bit_drop_bit) force |
|
1915 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
1916 |
lemma bit_signed_drop_bit_iff [bit_simps]: |
72388 | 1917 |
\<open>bit (signed_drop_bit m w) n \<longleftrightarrow> bit w (if LENGTH('a) - m \<le> n \<and> n < LENGTH('a) then LENGTH('a) - 1 else m + n)\<close> |
1918 |
for w :: \<open>'a::len word\<close> |
|
1919 |
apply transfer |
|
1920 |
apply (auto simp add: bit_drop_bit_eq bit_signed_take_bit_iff not_le min_def) |
|
1921 |
apply (metis add.commute le_antisym less_diff_conv less_eq_decr_length_iff) |
|
1922 |
apply (metis le_antisym less_eq_decr_length_iff) |
|
1923 |
done |
|
1924 |
||
72508 | 1925 |
lemma [code]: |
1926 |
\<open>Word.the_int (signed_drop_bit n w) = take_bit LENGTH('a) (drop_bit n (Word.the_signed_int w))\<close> |
|
1927 |
for w :: \<open>'a::len word\<close> |
|
1928 |
by transfer simp |
|
1929 |
||
72488 | 1930 |
lemma signed_drop_bit_signed_drop_bit [simp]: |
1931 |
\<open>signed_drop_bit m (signed_drop_bit n w) = signed_drop_bit (m + n) w\<close> |
|
1932 |
for w :: \<open>'a::len word\<close> |
|
1933 |
apply (cases \<open>LENGTH('a)\<close>) |
|
1934 |
apply simp_all |
|
1935 |
apply (rule bit_word_eqI) |
|
1936 |
apply (auto simp add: bit_signed_drop_bit_iff not_le less_diff_conv ac_simps) |
|
1937 |
done |
|
1938 |
||
72388 | 1939 |
lemma signed_drop_bit_0 [simp]: |
1940 |
\<open>signed_drop_bit 0 w = w\<close> |
|
72488 | 1941 |
by transfer (simp add: take_bit_signed_take_bit) |
72388 | 1942 |
|
1943 |
lemma sint_signed_drop_bit_eq: |
|
1944 |
\<open>sint (signed_drop_bit n w) = drop_bit n (sint w)\<close> |
|
1945 |
apply (cases \<open>LENGTH('a)\<close>; cases n) |
|
1946 |
apply simp_all |
|
1947 |
apply (rule bit_eqI) |
|
1948 |
apply (auto simp add: bit_sint_iff bit_drop_bit_eq bit_signed_drop_bit_iff dest: bit_imp_le_length) |
|
1949 |
done |
|
1950 |
||
72488 | 1951 |
lift_definition sshiftr1 :: \<open>'a::len word \<Rightarrow> 'a word\<close> |
1952 |
is \<open>\<lambda>k. take_bit LENGTH('a) (signed_take_bit (LENGTH('a) - Suc 0) k div 2)\<close> |
|
72079 | 1953 |
by (simp flip: signed_take_bit_decr_length_iff) |
1954 |
||
1955 |
lift_definition bshiftr1 :: \<open>bool \<Rightarrow> 'a::len word \<Rightarrow> 'a word\<close> |
|
1956 |
is \<open>\<lambda>b k. take_bit LENGTH('a) k div 2 + of_bool b * 2 ^ (LENGTH('a) - Suc 0)\<close> |
|
1957 |
by (fact arg_cong) |
|
1958 |
||
72488 | 1959 |
lemma sshiftr1_eq_signed_drop_bit_Suc_0: |
1960 |
\<open>sshiftr1 = signed_drop_bit (Suc 0)\<close> |
|
1961 |
by (rule ext) (transfer, simp add: drop_bit_Suc) |
|
1962 |
||
72079 | 1963 |
lemma sshiftr1_eq: |
72128 | 1964 |
\<open>sshiftr1 w = word_of_int (sint w div 2)\<close> |
72079 | 1965 |
by transfer simp |
1966 |
||
37660 | 1967 |
|
61799 | 1968 |
subsection \<open>Rotation\<close> |
37660 | 1969 |
|
72079 | 1970 |
lift_definition word_rotr :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close> |
1971 |
is \<open>\<lambda>n k. concat_bit (LENGTH('a) - n mod LENGTH('a)) |
|
1972 |
(drop_bit (n mod LENGTH('a)) (take_bit LENGTH('a) k)) |
|
1973 |
(take_bit (n mod LENGTH('a)) k)\<close> |
|
1974 |
subgoal for n k l |
|
1975 |
apply (simp add: concat_bit_def nat_le_iff less_imp_le |
|
1976 |
take_bit_tightened [of \<open>LENGTH('a)\<close> k l \<open>n mod LENGTH('a::len)\<close>]) |
|
1977 |
done |
|
1978 |
done |
|
1979 |
||
1980 |
lift_definition word_rotl :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close> |
|
1981 |
is \<open>\<lambda>n k. concat_bit (n mod LENGTH('a)) |
|
1982 |
(drop_bit (LENGTH('a) - n mod LENGTH('a)) (take_bit LENGTH('a) k)) |
|
1983 |
(take_bit (LENGTH('a) - n mod LENGTH('a)) k)\<close> |
|
1984 |
subgoal for n k l |
|
1985 |
apply (simp add: concat_bit_def nat_le_iff less_imp_le |
|
1986 |
take_bit_tightened [of \<open>LENGTH('a)\<close> k l \<open>LENGTH('a) - n mod LENGTH('a::len)\<close>]) |
|
1987 |
done |
|
1988 |
done |
|
1989 |
||
1990 |
lift_definition word_roti :: \<open>int \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close> |
|
1991 |
is \<open>\<lambda>r k. concat_bit (LENGTH('a) - nat (r mod int LENGTH('a))) |
|
1992 |
(drop_bit (nat (r mod int LENGTH('a))) (take_bit LENGTH('a) k)) |
|
1993 |
(take_bit (nat (r mod int LENGTH('a))) k)\<close> |
|
1994 |
subgoal for r k l |
|
1995 |
apply (simp add: concat_bit_def nat_le_iff less_imp_le |
|
1996 |
take_bit_tightened [of \<open>LENGTH('a)\<close> k l \<open>nat (r mod int LENGTH('a::len))\<close>]) |
|
1997 |
done |
|
1998 |
done |
|
1999 |
||
2000 |
lemma word_rotl_eq_word_rotr [code]: |
|
2001 |
\<open>word_rotl n = (word_rotr (LENGTH('a) - n mod LENGTH('a)) :: 'a::len word \<Rightarrow> 'a word)\<close> |
|
2002 |
by (rule ext, cases \<open>n mod LENGTH('a) = 0\<close>; transfer) simp_all |
|
2003 |
||
2004 |
lemma word_roti_eq_word_rotr_word_rotl [code]: |
|
2005 |
\<open>word_roti i w = |
|
2006 |
(if i \<ge> 0 then word_rotr (nat i) w else word_rotl (nat (- i)) w)\<close> |
|
2007 |
proof (cases \<open>i \<ge> 0\<close>) |
|
2008 |
case True |
|
2009 |
moreover define n where \<open>n = nat i\<close> |
|
2010 |
ultimately have \<open>i = int n\<close> |
|
2011 |
by simp |
|
2012 |
moreover have \<open>word_roti (int n) = (word_rotr n :: _ \<Rightarrow> 'a word)\<close> |
|
2013 |
by (rule ext, transfer) (simp add: nat_mod_distrib) |
|
2014 |
ultimately show ?thesis |
|
2015 |
by simp |
|
2016 |
next |
|
2017 |
case False |
|
2018 |
moreover define n where \<open>n = nat (- i)\<close> |
|
2019 |
ultimately have \<open>i = - int n\<close> \<open>n > 0\<close> |
|
2020 |
by simp_all |
|
2021 |
moreover have \<open>word_roti (- int n) = (word_rotl n :: _ \<Rightarrow> 'a word)\<close> |
|
2022 |
by (rule ext, transfer) |
|
2023 |
(simp add: zmod_zminus1_eq_if flip: of_nat_mod of_nat_diff) |
|
2024 |
ultimately show ?thesis |
|
2025 |
by simp |
|
2026 |
qed |
|
2027 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
2028 |
lemma bit_word_rotr_iff [bit_simps]: |
72079 | 2029 |
\<open>bit (word_rotr m w) n \<longleftrightarrow> |
2030 |
n < LENGTH('a) \<and> bit w ((n + m) mod LENGTH('a))\<close> |
|
2031 |
for w :: \<open>'a::len word\<close> |
|
2032 |
proof transfer |
|
2033 |
fix k :: int and m n :: nat |
|
2034 |
define q where \<open>q = m mod LENGTH('a)\<close> |
|
2035 |
have \<open>q < LENGTH('a)\<close> |
|
2036 |
by (simp add: q_def) |
|
2037 |
then have \<open>q \<le> LENGTH('a)\<close> |
|
2038 |
by simp |
|
2039 |
have \<open>m mod LENGTH('a) = q\<close> |
|
2040 |
by (simp add: q_def) |
|
2041 |
moreover have \<open>(n + m) mod LENGTH('a) = (n + q) mod LENGTH('a)\<close> |
|
2042 |
by (subst mod_add_right_eq [symmetric]) (simp add: \<open>m mod LENGTH('a) = q\<close>) |
|
2043 |
moreover have \<open>n < LENGTH('a) \<and> |
|
2044 |
bit (concat_bit (LENGTH('a) - q) (drop_bit q (take_bit LENGTH('a) k)) (take_bit q k)) n \<longleftrightarrow> |
|
2045 |
n < LENGTH('a) \<and> bit k ((n + q) mod LENGTH('a))\<close> |
|
2046 |
using \<open>q < LENGTH('a)\<close> |
|
2047 |
by (cases \<open>q + n \<ge> LENGTH('a)\<close>) |
|
2048 |
(auto simp add: bit_concat_bit_iff bit_drop_bit_eq |
|
2049 |
bit_take_bit_iff le_mod_geq ac_simps) |
|
2050 |
ultimately show \<open>n < LENGTH('a) \<and> |
|
2051 |
bit (concat_bit (LENGTH('a) - m mod LENGTH('a)) |
|
2052 |
(drop_bit (m mod LENGTH('a)) (take_bit LENGTH('a) k)) |
|
2053 |
(take_bit (m mod LENGTH('a)) k)) n |
|
2054 |
\<longleftrightarrow> n < LENGTH('a) \<and> |
|
2055 |
(n + m) mod LENGTH('a) < LENGTH('a) \<and> |
|
2056 |
bit k ((n + m) mod LENGTH('a))\<close> |
|
2057 |
by simp |
|
2058 |
qed |
|
2059 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
2060 |
lemma bit_word_rotl_iff [bit_simps]: |
72079 | 2061 |
\<open>bit (word_rotl m w) n \<longleftrightarrow> |
2062 |
n < LENGTH('a) \<and> bit w ((n + (LENGTH('a) - m mod LENGTH('a))) mod LENGTH('a))\<close> |
|
2063 |
for w :: \<open>'a::len word\<close> |
|
2064 |
by (simp add: word_rotl_eq_word_rotr bit_word_rotr_iff) |
|
2065 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
2066 |
lemma bit_word_roti_iff [bit_simps]: |
72079 | 2067 |
\<open>bit (word_roti k w) n \<longleftrightarrow> |
2068 |
n < LENGTH('a) \<and> bit w (nat ((int n + k) mod int LENGTH('a)))\<close> |
|
2069 |
for w :: \<open>'a::len word\<close> |
|
2070 |
proof transfer |
|
2071 |
fix k l :: int and n :: nat |
|
2072 |
define m where \<open>m = nat (k mod int LENGTH('a))\<close> |
|
2073 |
have \<open>m < LENGTH('a)\<close> |
|
2074 |
by (simp add: nat_less_iff m_def) |
|
2075 |
then have \<open>m \<le> LENGTH('a)\<close> |
|
2076 |
by simp |
|
2077 |
have \<open>k mod int LENGTH('a) = int m\<close> |
|
2078 |
by (simp add: nat_less_iff m_def) |
|
2079 |
moreover have \<open>(int n + k) mod int LENGTH('a) = int ((n + m) mod LENGTH('a))\<close> |
|
2080 |
by (subst mod_add_right_eq [symmetric]) (simp add: of_nat_mod \<open>k mod int LENGTH('a) = int m\<close>) |
|
2081 |
moreover have \<open>n < LENGTH('a) \<and> |
|
2082 |
bit (concat_bit (LENGTH('a) - m) (drop_bit m (take_bit LENGTH('a) l)) (take_bit m l)) n \<longleftrightarrow> |
|
2083 |
n < LENGTH('a) \<and> bit l ((n + m) mod LENGTH('a))\<close> |
|
2084 |
using \<open>m < LENGTH('a)\<close> |
|
2085 |
by (cases \<open>m + n \<ge> LENGTH('a)\<close>) |
|
2086 |
(auto simp add: bit_concat_bit_iff bit_drop_bit_eq |
|
2087 |
bit_take_bit_iff nat_less_iff not_le not_less ac_simps |
|
2088 |
le_diff_conv le_mod_geq) |
|
2089 |
ultimately show \<open>n < LENGTH('a) |
|
2090 |
\<and> bit (concat_bit (LENGTH('a) - nat (k mod int LENGTH('a))) |
|
2091 |
(drop_bit (nat (k mod int LENGTH('a))) (take_bit LENGTH('a) l)) |
|
2092 |
(take_bit (nat (k mod int LENGTH('a))) l)) n \<longleftrightarrow> |
|
2093 |
n < LENGTH('a) |
|
2094 |
\<and> nat ((int n + k) mod int LENGTH('a)) < LENGTH('a) |
|
2095 |
\<and> bit l (nat ((int n + k) mod int LENGTH('a)))\<close> |
|
2096 |
by simp |
|
2097 |
qed |
|
2098 |
||
72262 | 2099 |
lemma uint_word_rotr_eq: |
72079 | 2100 |
\<open>uint (word_rotr n w) = concat_bit (LENGTH('a) - n mod LENGTH('a)) |
2101 |
(drop_bit (n mod LENGTH('a)) (uint w)) |
|
2102 |
(uint (take_bit (n mod LENGTH('a)) w))\<close> |
|
2103 |
for w :: \<open>'a::len word\<close> |
|
2104 |
apply transfer |
|
2105 |
apply (simp add: concat_bit_def take_bit_drop_bit push_bit_take_bit min_def) |
|
2106 |
using mod_less_divisor not_less apply blast |
|
2107 |
done |
|
2108 |
||
72262 | 2109 |
lemma [code]: |
2110 |
\<open>Word.the_int (word_rotr n w) = concat_bit (LENGTH('a) - n mod LENGTH('a)) |
|
2111 |
(drop_bit (n mod LENGTH('a)) (Word.the_int w)) |
|
2112 |
(Word.the_int (take_bit (n mod LENGTH('a)) w))\<close> |
|
2113 |
for w :: \<open>'a::len word\<close> |
|
2114 |
using uint_word_rotr_eq [of n w] by simp |
|
2115 |
||
72079 | 2116 |
|
61799 | 2117 |
subsection \<open>Split and cat operations\<close> |
37660 | 2118 |
|
72079 | 2119 |
lift_definition word_cat :: \<open>'a::len word \<Rightarrow> 'b::len word \<Rightarrow> 'c::len word\<close> |
2120 |
is \<open>\<lambda>k l. concat_bit LENGTH('b) l (take_bit LENGTH('a) k)\<close> |
|
2121 |
by (simp add: bit_eq_iff bit_concat_bit_iff bit_take_bit_iff) |
|
65268 | 2122 |
|
71990 | 2123 |
lemma word_cat_eq: |
2124 |
\<open>(word_cat v w :: 'c::len word) = push_bit LENGTH('b) (ucast v) + ucast w\<close> |
|
2125 |
for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close> |
|
72128 | 2126 |
by transfer (simp add: concat_bit_eq ac_simps) |
72079 | 2127 |
|
2128 |
lemma word_cat_eq' [code]: |
|
2129 |
\<open>word_cat a b = word_of_int (concat_bit LENGTH('b) (uint b) (uint a))\<close> |
|
2130 |
for a :: \<open>'a::len word\<close> and b :: \<open>'b::len word\<close> |
|
72488 | 2131 |
by transfer (simp add: concat_bit_take_bit_eq) |
71990 | 2132 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
2133 |
lemma bit_word_cat_iff [bit_simps]: |
71990 | 2134 |
\<open>bit (word_cat v w :: 'c::len word) n \<longleftrightarrow> n < LENGTH('c) \<and> (if n < LENGTH('b) then bit w n else bit v (n - LENGTH('b)))\<close> |
2135 |
for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close> |
|
72079 | 2136 |
by transfer (simp add: bit_concat_bit_iff bit_take_bit_iff) |
71990 | 2137 |
|
72488 | 2138 |
definition word_split :: \<open>'a::len word \<Rightarrow> 'b::len word \<times> 'c::len word\<close> |
2139 |
where \<open>word_split w = |
|
2140 |
(ucast (drop_bit LENGTH('c) w) :: 'b::len word, ucast w :: 'c::len word)\<close> |
|
65268 | 2141 |
|
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
2142 |
definition word_rcat :: \<open>'a::len word list \<Rightarrow> 'b::len word\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
2143 |
where \<open>word_rcat = word_of_int \<circ> horner_sum uint (2 ^ LENGTH('a)) \<circ> rev\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
2144 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2145 |
abbreviation (input) max_word :: \<open>'a::len word\<close> |
67443
3abf6a722518
standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents:
67408
diff
changeset
|
2146 |
\<comment> \<open>Largest representable machine integer.\<close> |
71946 | 2147 |
where "max_word \<equiv> - 1" |
37660 | 2148 |
|
2149 |
||
72292 | 2150 |
subsection \<open>More on conversions\<close> |
2151 |
||
2152 |
lemma int_word_sint: |
|
2153 |
\<open>sint (word_of_int x :: 'a::len word) = (x + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) - 2 ^ (LENGTH('a) - 1)\<close> |
|
72488 | 2154 |
by transfer (simp flip: take_bit_eq_mod add: signed_take_bit_eq_take_bit_shift) |
46010 | 2155 |
|
72128 | 2156 |
lemma sint_sbintrunc': "sint (word_of_int bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) bin" |
72292 | 2157 |
by simp |
65268 | 2158 |
|
72488 | 2159 |
lemma uint_sint: "uint w = take_bit LENGTH('a) (sint w)" |
65328 | 2160 |
for w :: "'a::len word" |
72488 | 2161 |
by transfer (simp add: take_bit_signed_take_bit) |
65268 | 2162 |
|
72128 | 2163 |
lemma bintr_uint: "LENGTH('a) \<le> n \<Longrightarrow> take_bit n (uint w) = uint w" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2164 |
for w :: "'a::len word" |
72292 | 2165 |
by transfer (simp add: min_def) |
37660 | 2166 |
|
46057 | 2167 |
lemma wi_bintr: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2168 |
"LENGTH('a::len) \<le> n \<Longrightarrow> |
72128 | 2169 |
word_of_int (take_bit n w) = (word_of_int w :: 'a word)" |
72292 | 2170 |
by transfer simp |
45805 | 2171 |
|
65268 | 2172 |
lemma word_numeral_alt: "numeral b = word_of_int (numeral b)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2173 |
by (induct b, simp_all only: numeral.simps word_of_int_homs) |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2174 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2175 |
declare word_numeral_alt [symmetric, code_abbrev] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2176 |
|
65268 | 2177 |
lemma word_neg_numeral_alt: "- numeral b = word_of_int (- numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2178 |
by (simp only: word_numeral_alt wi_hom_neg) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2179 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2180 |
declare word_neg_numeral_alt [symmetric, code_abbrev] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2181 |
|
45805 | 2182 |
lemma uint_bintrunc [simp]: |
65268 | 2183 |
"uint (numeral bin :: 'a word) = |
72128 | 2184 |
take_bit (LENGTH('a::len)) (numeral bin)" |
72292 | 2185 |
by transfer rule |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2186 |
|
65268 | 2187 |
lemma uint_bintrunc_neg [simp]: |
72128 | 2188 |
"uint (- numeral bin :: 'a word) = take_bit (LENGTH('a::len)) (- numeral bin)" |
72292 | 2189 |
by transfer rule |
37660 | 2190 |
|
45805 | 2191 |
lemma sint_sbintrunc [simp]: |
72128 | 2192 |
"sint (numeral bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) (numeral bin)" |
72292 | 2193 |
by transfer simp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2194 |
|
65268 | 2195 |
lemma sint_sbintrunc_neg [simp]: |
72128 | 2196 |
"sint (- numeral bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) (- numeral bin)" |
72292 | 2197 |
by transfer simp |
37660 | 2198 |
|
45805 | 2199 |
lemma unat_bintrunc [simp]: |
72128 | 2200 |
"unat (numeral bin :: 'a::len word) = nat (take_bit (LENGTH('a)) (numeral bin))" |
72079 | 2201 |
by transfer simp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2202 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2203 |
lemma unat_bintrunc_neg [simp]: |
72128 | 2204 |
"unat (- numeral bin :: 'a::len word) = nat (take_bit (LENGTH('a)) (- numeral bin))" |
72079 | 2205 |
by transfer simp |
37660 | 2206 |
|
65328 | 2207 |
lemma size_0_eq: "size w = 0 \<Longrightarrow> v = w" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2208 |
for v w :: "'a::len word" |
72292 | 2209 |
by transfer simp |
37660 | 2210 |
|
65268 | 2211 |
lemma uint_ge_0 [iff]: "0 \<le> uint x" |
72292 | 2212 |
by (fact unsigned_greater_eq) |
45805 | 2213 |
|
70185 | 2214 |
lemma uint_lt2p [iff]: "uint x < 2 ^ LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2215 |
for x :: "'a::len word" |
72292 | 2216 |
by (fact unsigned_less) |
45805 | 2217 |
|
70185 | 2218 |
lemma sint_ge: "- (2 ^ (LENGTH('a) - 1)) \<le> sint x" |
65268 | 2219 |
for x :: "'a::len word" |
72292 | 2220 |
using sint_greater_eq [of x] by simp |
45805 | 2221 |
|
70185 | 2222 |
lemma sint_lt: "sint x < 2 ^ (LENGTH('a) - 1)" |
65268 | 2223 |
for x :: "'a::len word" |
72292 | 2224 |
using sint_less [of x] by simp |
37660 | 2225 |
|
70185 | 2226 |
lemma uint_m2p_neg: "uint x - 2 ^ LENGTH('a) < 0" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2227 |
for x :: "'a::len word" |
45805 | 2228 |
by (simp only: diff_less_0_iff_less uint_lt2p) |
2229 |
||
70185 | 2230 |
lemma uint_m2p_not_non_neg: "\<not> 0 \<le> uint x - 2 ^ LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2231 |
for x :: "'a::len word" |
45805 | 2232 |
by (simp only: not_le uint_m2p_neg) |
37660 | 2233 |
|
70185 | 2234 |
lemma lt2p_lem: "LENGTH('a) \<le> n \<Longrightarrow> uint w < 2 ^ n" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2235 |
for w :: "'a::len word" |
72488 | 2236 |
using uint_bounded [of w] by (rule less_le_trans) simp |
37660 | 2237 |
|
45805 | 2238 |
lemma uint_le_0_iff [simp]: "uint x \<le> 0 \<longleftrightarrow> uint x = 0" |
70749
5d06b7bb9d22
More type class generalisations. Note that linorder_antisym_conv1 and linorder_antisym_conv2 no longer exist.
paulson <lp15@cam.ac.uk>
parents:
70342
diff
changeset
|
2239 |
by (fact uint_ge_0 [THEN leD, THEN antisym_conv1]) |
37660 | 2240 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2241 |
lemma uint_nat: "uint w = int (unat w)" |
72079 | 2242 |
by transfer simp |
65268 | 2243 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2244 |
lemma uint_numeral: "uint (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)" |
72292 | 2245 |
by (simp flip: take_bit_eq_mod add: of_nat_take_bit) |
65268 | 2246 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2247 |
lemma uint_neg_numeral: "uint (- numeral b :: 'a::len word) = - numeral b mod 2 ^ LENGTH('a)" |
72292 | 2248 |
by (simp flip: take_bit_eq_mod add: of_nat_take_bit) |
65268 | 2249 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2250 |
lemma unat_numeral: "unat (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)" |
72079 | 2251 |
by transfer (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq) |
37660 | 2252 |
|
65268 | 2253 |
lemma sint_numeral: |
2254 |
"sint (numeral b :: 'a::len word) = |
|
72292 | 2255 |
(numeral b + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) - 2 ^ (LENGTH('a) - 1)" |
2256 |
apply (transfer fixing: b) |
|
2257 |
using int_word_sint [of \<open>numeral b\<close>] |
|
2258 |
apply simp |
|
2259 |
done |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2260 |
|
65268 | 2261 |
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0" |
72292 | 2262 |
by (fact of_int_0) |
45958 | 2263 |
|
65268 | 2264 |
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1" |
72292 | 2265 |
by (fact of_int_1) |
45958 | 2266 |
|
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2267 |
lemma word_of_int_neg_1 [simp]: "word_of_int (- 1) = - 1" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2268 |
by (simp add: wi_hom_syms) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2269 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2270 |
lemma word_of_int_numeral [simp] : "(word_of_int (numeral bin) :: 'a::len word) = numeral bin" |
72292 | 2271 |
by (fact of_int_numeral) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2272 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2273 |
lemma word_of_int_neg_numeral [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2274 |
"(word_of_int (- numeral bin) :: 'a::len word) = - numeral bin" |
72292 | 2275 |
by (fact of_int_neg_numeral) |
65268 | 2276 |
|
2277 |
lemma word_int_case_wi: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2278 |
"word_int_case f (word_of_int i :: 'b word) = f (i mod 2 ^ LENGTH('b::len))" |
72079 | 2279 |
by transfer (simp add: take_bit_eq_mod) |
65268 | 2280 |
|
2281 |
lemma word_int_split: |
|
2282 |
"P (word_int_case f x) = |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2283 |
(\<forall>i. x = (word_of_int i :: 'b::len word) \<and> 0 \<le> i \<and> i < 2 ^ LENGTH('b) \<longrightarrow> P (f i))" |
72079 | 2284 |
by transfer (auto simp add: take_bit_eq_mod) |
65268 | 2285 |
|
2286 |
lemma word_int_split_asm: |
|
2287 |
"P (word_int_case f x) = |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2288 |
(\<nexists>n. x = (word_of_int n :: 'b::len word) \<and> 0 \<le> n \<and> n < 2 ^ LENGTH('b::len) \<and> \<not> P (f n))" |
72079 | 2289 |
by transfer (auto simp add: take_bit_eq_mod) |
45805 | 2290 |
|
65268 | 2291 |
lemma uint_range_size: "0 \<le> uint w \<and> uint w < 2 ^ size w" |
72292 | 2292 |
by transfer simp |
37660 | 2293 |
|
65268 | 2294 |
lemma sint_range_size: "- (2 ^ (size w - Suc 0)) \<le> sint w \<and> sint w < 2 ^ (size w - Suc 0)" |
72488 | 2295 |
by (simp add: word_size sint_greater_eq sint_less) |
37660 | 2296 |
|
65268 | 2297 |
lemma sint_above_size: "2 ^ (size w - 1) \<le> x \<Longrightarrow> sint w < x" |
2298 |
for w :: "'a::len word" |
|
45805 | 2299 |
unfolding word_size by (rule less_le_trans [OF sint_lt]) |
2300 |
||
65268 | 2301 |
lemma sint_below_size: "x \<le> - (2 ^ (size w - 1)) \<Longrightarrow> x \<le> sint w" |
2302 |
for w :: "'a::len word" |
|
45805 | 2303 |
unfolding word_size by (rule order_trans [OF _ sint_ge]) |
37660 | 2304 |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2305 |
|
61799 | 2306 |
subsection \<open>Testing bits\<close> |
46010 | 2307 |
|
72488 | 2308 |
lemma bin_nth_uint_imp: "bit (uint w) n \<Longrightarrow> n < LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2309 |
for w :: "'a::len word" |
72292 | 2310 |
by transfer (simp add: bit_take_bit_iff) |
37660 | 2311 |
|
46057 | 2312 |
lemma bin_nth_sint: |
70185 | 2313 |
"LENGTH('a) \<le> n \<Longrightarrow> |
72488 | 2314 |
bit (sint w) n = bit (sint w) (LENGTH('a) - 1)" |
65268 | 2315 |
for w :: "'a::len word" |
72292 | 2316 |
by (transfer fixing: n) (simp add: bit_signed_take_bit_iff le_diff_conv min_def) |
37660 | 2317 |
|
2318 |
lemma num_of_bintr': |
|
72128 | 2319 |
"take_bit (LENGTH('a::len)) (numeral a :: int) = (numeral b) \<Longrightarrow> |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2320 |
numeral a = (numeral b :: 'a word)" |
72292 | 2321 |
proof (transfer fixing: a b) |
2322 |
assume \<open>take_bit LENGTH('a) (numeral a :: int) = numeral b\<close> |
|
2323 |
then have \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (numeral a :: int)) = take_bit LENGTH('a) (numeral b)\<close> |
|
2324 |
by simp |
|
2325 |
then show \<open>take_bit LENGTH('a) (numeral a :: int) = take_bit LENGTH('a) (numeral b)\<close> |
|
2326 |
by simp |
|
2327 |
qed |
|
37660 | 2328 |
|
2329 |
lemma num_of_sbintr': |
|
72241 | 2330 |
"signed_take_bit (LENGTH('a::len) - 1) (numeral a :: int) = (numeral b) \<Longrightarrow> |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2331 |
numeral a = (numeral b :: 'a word)" |
72292 | 2332 |
proof (transfer fixing: a b) |
2333 |
assume \<open>signed_take_bit (LENGTH('a) - 1) (numeral a :: int) = numeral b\<close> |
|
2334 |
then have \<open>take_bit LENGTH('a) (signed_take_bit (LENGTH('a) - 1) (numeral a :: int)) = take_bit LENGTH('a) (numeral b)\<close> |
|
2335 |
by simp |
|
2336 |
then show \<open>take_bit LENGTH('a) (numeral a :: int) = take_bit LENGTH('a) (numeral b)\<close> |
|
72488 | 2337 |
by (simp add: take_bit_signed_take_bit) |
72292 | 2338 |
qed |
2339 |
||
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
2340 |
lemma num_abs_bintr: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2341 |
"(numeral x :: 'a word) = |
72128 | 2342 |
word_of_int (take_bit (LENGTH('a::len)) (numeral x))" |
72292 | 2343 |
by transfer simp |
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
2344 |
|
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
2345 |
lemma num_abs_sbintr: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2346 |
"(numeral x :: 'a word) = |
72128 | 2347 |
word_of_int (signed_take_bit (LENGTH('a::len) - 1) (numeral x))" |
72488 | 2348 |
by transfer (simp add: take_bit_signed_take_bit) |
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
2349 |
|
67408 | 2350 |
text \<open> |
2351 |
\<open>cast\<close> -- note, no arg for new length, as it's determined by type of result, |
|
2352 |
thus in \<open>cast w = w\<close>, the type means cast to length of \<open>w\<close>! |
|
2353 |
\<close> |
|
37660 | 2354 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
2355 |
lemma bit_ucast_iff: |
72079 | 2356 |
\<open>bit (ucast a :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a::len) \<and> Parity.bit a n\<close> |
2357 |
by transfer (simp add: bit_take_bit_iff) |
|
2358 |
||
2359 |
lemma ucast_id [simp]: "ucast w = w" |
|
2360 |
by transfer simp |
|
2361 |
||
2362 |
lemma scast_id [simp]: "scast w = w" |
|
72488 | 2363 |
by transfer (simp add: take_bit_signed_take_bit) |
37660 | 2364 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
2365 |
lemma ucast_mask_eq: |
72082 | 2366 |
\<open>ucast (mask n :: 'b word) = mask (min LENGTH('b::len) n)\<close> |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
2367 |
by (simp add: bit_eq_iff) (auto simp add: bit_mask_iff bit_ucast_iff exp_eq_zero_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
2368 |
|
67408 | 2369 |
\<comment> \<open>literal u(s)cast\<close> |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
2370 |
lemma ucast_bintr [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2371 |
"ucast (numeral w :: 'a::len word) = |
72128 | 2372 |
word_of_int (take_bit (LENGTH('a)) (numeral w))" |
72079 | 2373 |
by transfer simp |
65268 | 2374 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2375 |
(* TODO: neg_numeral *) |
37660 | 2376 |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
2377 |
lemma scast_sbintr [simp]: |
65268 | 2378 |
"scast (numeral w ::'a::len word) = |
72128 | 2379 |
word_of_int (signed_take_bit (LENGTH('a) - Suc 0) (numeral w))" |
72079 | 2380 |
by transfer simp |
37660 | 2381 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2382 |
lemma source_size: "source_size (c::'a::len word \<Rightarrow> _) = LENGTH('a)" |
72079 | 2383 |
by transfer simp |
46011 | 2384 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2385 |
lemma target_size: "target_size (c::_ \<Rightarrow> 'b::len word) = LENGTH('b)" |
72079 | 2386 |
by transfer simp |
46011 | 2387 |
|
70185 | 2388 |
lemma is_down: "is_down c \<longleftrightarrow> LENGTH('b) \<le> LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2389 |
for c :: "'a::len word \<Rightarrow> 'b::len word" |
72079 | 2390 |
by transfer simp |
65268 | 2391 |
|
70185 | 2392 |
lemma is_up: "is_up c \<longleftrightarrow> LENGTH('a) \<le> LENGTH('b)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2393 |
for c :: "'a::len word \<Rightarrow> 'b::len word" |
72079 | 2394 |
by transfer simp |
2395 |
||
2396 |
lemma is_up_down: |
|
2397 |
\<open>is_up c \<longleftrightarrow> is_down d\<close> |
|
2398 |
for c :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
2399 |
and d :: \<open>'b::len word \<Rightarrow> 'a::len word\<close> |
|
2400 |
by transfer simp |
|
2401 |
||
2402 |
context |
|
2403 |
fixes dummy_types :: \<open>'a::len \<times> 'b::len\<close> |
|
2404 |
begin |
|
2405 |
||
2406 |
private abbreviation (input) UCAST :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
2407 |
where \<open>UCAST == ucast\<close> |
|
2408 |
||
2409 |
private abbreviation (input) SCAST :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
2410 |
where \<open>SCAST == scast\<close> |
|
2411 |
||
2412 |
lemma down_cast_same: |
|
2413 |
\<open>UCAST = scast\<close> if \<open>is_down UCAST\<close> |
|
2414 |
by (rule ext, use that in transfer) (simp add: take_bit_signed_take_bit) |
|
2415 |
||
2416 |
lemma sint_up_scast: |
|
2417 |
\<open>sint (SCAST w) = sint w\<close> if \<open>is_up SCAST\<close> |
|
2418 |
using that by transfer (simp add: min_def Suc_leI le_diff_iff) |
|
2419 |
||
2420 |
lemma uint_up_ucast: |
|
2421 |
\<open>uint (UCAST w) = uint w\<close> if \<open>is_up UCAST\<close> |
|
2422 |
using that by transfer (simp add: min_def) |
|
2423 |
||
2424 |
lemma ucast_up_ucast: |
|
2425 |
\<open>ucast (UCAST w) = ucast w\<close> if \<open>is_up UCAST\<close> |
|
2426 |
using that by transfer (simp add: ac_simps) |
|
2427 |
||
2428 |
lemma ucast_up_ucast_id: |
|
2429 |
\<open>ucast (UCAST w) = w\<close> if \<open>is_up UCAST\<close> |
|
2430 |
using that by (simp add: ucast_up_ucast) |
|
2431 |
||
2432 |
lemma scast_up_scast: |
|
2433 |
\<open>scast (SCAST w) = scast w\<close> if \<open>is_up SCAST\<close> |
|
2434 |
using that by transfer (simp add: ac_simps) |
|
2435 |
||
2436 |
lemma scast_up_scast_id: |
|
2437 |
\<open>scast (SCAST w) = w\<close> if \<open>is_up SCAST\<close> |
|
2438 |
using that by (simp add: scast_up_scast) |
|
2439 |
||
2440 |
lemma isduu: |
|
2441 |
\<open>is_up UCAST\<close> if \<open>is_down d\<close> |
|
2442 |
for d :: \<open>'b word \<Rightarrow> 'a word\<close> |
|
2443 |
using that is_up_down [of UCAST d] by simp |
|
2444 |
||
2445 |
lemma isdus: |
|
2446 |
\<open>is_up SCAST\<close> if \<open>is_down d\<close> |
|
2447 |
for d :: \<open>'b word \<Rightarrow> 'a word\<close> |
|
2448 |
using that is_up_down [of SCAST d] by simp |
|
2449 |
||
37660 | 2450 |
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] |
72079 | 2451 |
lemmas scast_down_scast_id = isdus [THEN scast_up_scast_id] |
37660 | 2452 |
|
2453 |
lemma up_ucast_surj: |
|
72079 | 2454 |
\<open>surj (ucast :: 'b word \<Rightarrow> 'a word)\<close> if \<open>is_up UCAST\<close> |
2455 |
by (rule surjI) (use that in \<open>rule ucast_up_ucast_id\<close>) |
|
37660 | 2456 |
|
2457 |
lemma up_scast_surj: |
|
72079 | 2458 |
\<open>surj (scast :: 'b word \<Rightarrow> 'a word)\<close> if \<open>is_up SCAST\<close> |
2459 |
by (rule surjI) (use that in \<open>rule scast_up_scast_id\<close>) |
|
37660 | 2460 |
|
2461 |
lemma down_ucast_inj: |
|
72079 | 2462 |
\<open>inj_on UCAST A\<close> if \<open>is_down (ucast :: 'b word \<Rightarrow> 'a word)\<close> |
2463 |
by (rule inj_on_inverseI) (use that in \<open>rule ucast_down_ucast_id\<close>) |
|
2464 |
||
2465 |
lemma down_scast_inj: |
|
2466 |
\<open>inj_on SCAST A\<close> if \<open>is_down (scast :: 'b word \<Rightarrow> 'a word)\<close> |
|
2467 |
by (rule inj_on_inverseI) (use that in \<open>rule scast_down_scast_id\<close>) |
|
2468 |
||
2469 |
lemma ucast_down_wi: |
|
2470 |
\<open>UCAST (word_of_int x) = word_of_int x\<close> if \<open>is_down UCAST\<close> |
|
2471 |
using that by transfer simp |
|
2472 |
||
2473 |
lemma ucast_down_no: |
|
2474 |
\<open>UCAST (numeral bin) = numeral bin\<close> if \<open>is_down UCAST\<close> |
|
2475 |
using that by transfer simp |
|
2476 |
||
2477 |
end |
|
37660 | 2478 |
|
2479 |
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def |
|
2480 |
||
72000 | 2481 |
lemma bit_last_iff: |
2482 |
\<open>bit w (LENGTH('a) - Suc 0) \<longleftrightarrow> sint w < 0\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
2483 |
for w :: \<open>'a::len word\<close> |
|
2484 |
proof - |
|
2485 |
have \<open>?P \<longleftrightarrow> bit (uint w) (LENGTH('a) - Suc 0)\<close> |
|
2486 |
by (simp add: bit_uint_iff) |
|
2487 |
also have \<open>\<dots> \<longleftrightarrow> ?Q\<close> |
|
72010 | 2488 |
by (simp add: sint_uint) |
72000 | 2489 |
finally show ?thesis . |
2490 |
qed |
|
2491 |
||
2492 |
lemma drop_bit_eq_zero_iff_not_bit_last: |
|
2493 |
\<open>drop_bit (LENGTH('a) - Suc 0) w = 0 \<longleftrightarrow> \<not> bit w (LENGTH('a) - Suc 0)\<close> |
|
2494 |
for w :: "'a::len word" |
|
2495 |
apply (cases \<open>LENGTH('a)\<close>) |
|
2496 |
apply simp_all |
|
2497 |
apply (simp add: bit_iff_odd_drop_bit) |
|
2498 |
apply transfer |
|
2499 |
apply (simp add: take_bit_drop_bit) |
|
2500 |
apply (auto simp add: drop_bit_eq_div take_bit_eq_mod min_def) |
|
2501 |
apply (auto elim!: evenE) |
|
2502 |
apply (metis div_exp_eq mod_div_trivial mult.commute nonzero_mult_div_cancel_left power_Suc0_right power_add zero_neq_numeral) |
|
2503 |
done |
|
2504 |
||
37660 | 2505 |
|
61799 | 2506 |
subsection \<open>Word Arithmetic\<close> |
37660 | 2507 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2508 |
lemmas word_div_no [simp] = word_div_def [of "numeral a" "numeral b"] for a b |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2509 |
lemmas word_mod_no [simp] = word_mod_def [of "numeral a" "numeral b"] for a b |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2510 |
lemmas word_less_no [simp] = word_less_def [of "numeral a" "numeral b"] for a b |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2511 |
lemmas word_le_no [simp] = word_le_def [of "numeral a" "numeral b"] for a b |
72079 | 2512 |
lemmas word_sless_no [simp] = word_sless_eq [of "numeral a" "numeral b"] for a b |
2513 |
lemmas word_sle_no [simp] = word_sle_eq [of "numeral a" "numeral b"] for a b |
|
37660 | 2514 |
|
65268 | 2515 |
lemma size_0_same': "size w = 0 \<Longrightarrow> w = v" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2516 |
for v w :: "'a::len word" |
72079 | 2517 |
by (unfold word_size) simp |
37660 | 2518 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2519 |
lemmas size_0_same = size_0_same' [unfolded word_size] |
37660 | 2520 |
|
2521 |
lemmas unat_eq_0 = unat_0_iff |
|
2522 |
lemmas unat_eq_zero = unat_0_iff |
|
2523 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2524 |
|
61799 | 2525 |
subsection \<open>Transferring goals from words to ints\<close> |
37660 | 2526 |
|
65268 | 2527 |
lemma word_ths: |
2528 |
shows word_succ_p1: "word_succ a = a + 1" |
|
2529 |
and word_pred_m1: "word_pred a = a - 1" |
|
2530 |
and word_pred_succ: "word_pred (word_succ a) = a" |
|
2531 |
and word_succ_pred: "word_succ (word_pred a) = a" |
|
2532 |
and word_mult_succ: "word_succ a * b = b + a * b" |
|
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
2533 |
by (transfer, simp add: algebra_simps)+ |
37660 | 2534 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2535 |
lemma uint_cong: "x = y \<Longrightarrow> uint x = uint y" |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2536 |
by simp |
37660 | 2537 |
|
55818 | 2538 |
lemma uint_word_ariths: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2539 |
fixes a b :: "'a::len word" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2540 |
shows "uint (a + b) = (uint a + uint b) mod 2 ^ LENGTH('a::len)" |
70185 | 2541 |
and "uint (a - b) = (uint a - uint b) mod 2 ^ LENGTH('a)" |
2542 |
and "uint (a * b) = uint a * uint b mod 2 ^ LENGTH('a)" |
|
2543 |
and "uint (- a) = - uint a mod 2 ^ LENGTH('a)" |
|
2544 |
and "uint (word_succ a) = (uint a + 1) mod 2 ^ LENGTH('a)" |
|
2545 |
and "uint (word_pred a) = (uint a - 1) mod 2 ^ LENGTH('a)" |
|
2546 |
and "uint (0 :: 'a word) = 0 mod 2 ^ LENGTH('a)" |
|
2547 |
and "uint (1 :: 'a word) = 1 mod 2 ^ LENGTH('a)" |
|
72262 | 2548 |
by (simp_all only: word_arith_wis uint_word_of_int_eq flip: take_bit_eq_mod) |
55818 | 2549 |
|
2550 |
lemma uint_word_arith_bintrs: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2551 |
fixes a b :: "'a::len word" |
72128 | 2552 |
shows "uint (a + b) = take_bit (LENGTH('a)) (uint a + uint b)" |
2553 |
and "uint (a - b) = take_bit (LENGTH('a)) (uint a - uint b)" |
|
2554 |
and "uint (a * b) = take_bit (LENGTH('a)) (uint a * uint b)" |
|
2555 |
and "uint (- a) = take_bit (LENGTH('a)) (- uint a)" |
|
2556 |
and "uint (word_succ a) = take_bit (LENGTH('a)) (uint a + 1)" |
|
2557 |
and "uint (word_pred a) = take_bit (LENGTH('a)) (uint a - 1)" |
|
2558 |
and "uint (0 :: 'a word) = take_bit (LENGTH('a)) 0" |
|
2559 |
and "uint (1 :: 'a word) = take_bit (LENGTH('a)) 1" |
|
2560 |
by (simp_all add: uint_word_ariths take_bit_eq_mod) |
|
55818 | 2561 |
|
2562 |
lemma sint_word_ariths: |
|
2563 |
fixes a b :: "'a::len word" |
|
72128 | 2564 |
shows "sint (a + b) = signed_take_bit (LENGTH('a) - 1) (sint a + sint b)" |
2565 |
and "sint (a - b) = signed_take_bit (LENGTH('a) - 1) (sint a - sint b)" |
|
2566 |
and "sint (a * b) = signed_take_bit (LENGTH('a) - 1) (sint a * sint b)" |
|
2567 |
and "sint (- a) = signed_take_bit (LENGTH('a) - 1) (- sint a)" |
|
2568 |
and "sint (word_succ a) = signed_take_bit (LENGTH('a) - 1) (sint a + 1)" |
|
2569 |
and "sint (word_pred a) = signed_take_bit (LENGTH('a) - 1) (sint a - 1)" |
|
2570 |
and "sint (0 :: 'a word) = signed_take_bit (LENGTH('a) - 1) 0" |
|
2571 |
and "sint (1 :: 'a word) = signed_take_bit (LENGTH('a) - 1) 1" |
|
72488 | 2572 |
apply transfer apply (simp add: signed_take_bit_add) |
2573 |
apply transfer apply (simp add: signed_take_bit_diff) |
|
2574 |
apply transfer apply (simp add: signed_take_bit_mult) |
|
2575 |
apply transfer apply (simp add: signed_take_bit_minus) |
|
2576 |
apply (metis of_int_sint scast_id sint_sbintrunc' wi_hom_succ) |
|
2577 |
apply (metis of_int_sint scast_id sint_sbintrunc' wi_hom_pred) |
|
2578 |
apply (simp_all add: sint_uint) |
|
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
2579 |
done |
45604 | 2580 |
|
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
58061
diff
changeset
|
2581 |
lemma word_pred_0_n1: "word_pred 0 = word_of_int (- 1)" |
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
2582 |
unfolding word_pred_m1 by simp |
37660 | 2583 |
|
2584 |
lemma succ_pred_no [simp]: |
|
65268 | 2585 |
"word_succ (numeral w) = numeral w + 1" |
2586 |
"word_pred (numeral w) = numeral w - 1" |
|
2587 |
"word_succ (- numeral w) = - numeral w + 1" |
|
2588 |
"word_pred (- numeral w) = - numeral w - 1" |
|
2589 |
by (simp_all add: word_succ_p1 word_pred_m1) |
|
2590 |
||
2591 |
lemma word_sp_01 [simp]: |
|
2592 |
"word_succ (- 1) = 0 \<and> word_succ 0 = 1 \<and> word_pred 0 = - 1 \<and> word_pred 1 = 0" |
|
2593 |
by (simp_all add: word_succ_p1 word_pred_m1) |
|
37660 | 2594 |
|
67408 | 2595 |
\<comment> \<open>alternative approach to lifting arithmetic equalities\<close> |
65268 | 2596 |
lemma word_of_int_Ex: "\<exists>y. x = word_of_int y" |
37660 | 2597 |
by (rule_tac x="uint x" in exI) simp |
2598 |
||
2599 |
||
61799 | 2600 |
subsection \<open>Order on fixed-length words\<close> |
37660 | 2601 |
|
72262 | 2602 |
lift_definition udvd :: \<open>'a::len word \<Rightarrow> 'a::len word \<Rightarrow> bool\<close> (infixl \<open>udvd\<close> 50) |
2603 |
is \<open>\<lambda>k l. take_bit LENGTH('a) k dvd take_bit LENGTH('a) l\<close> by simp |
|
2604 |
||
2605 |
lemma udvd_iff_dvd: |
|
2606 |
\<open>x udvd y \<longleftrightarrow> unat x dvd unat y\<close> |
|
2607 |
by transfer (simp add: nat_dvd_iff) |
|
2608 |
||
2609 |
lemma udvd_iff_dvd_int: |
|
2610 |
\<open>v udvd w \<longleftrightarrow> uint v dvd uint w\<close> |
|
2611 |
by transfer rule |
|
2612 |
||
2613 |
lemma udvdI [intro]: |
|
2614 |
\<open>v udvd w\<close> if \<open>unat w = unat v * unat u\<close> |
|
2615 |
proof - |
|
2616 |
from that have \<open>unat v dvd unat w\<close> .. |
|
2617 |
then show ?thesis |
|
2618 |
by (simp add: udvd_iff_dvd) |
|
2619 |
qed |
|
2620 |
||
2621 |
lemma udvdE [elim]: |
|
2622 |
fixes v w :: \<open>'a::len word\<close> |
|
2623 |
assumes \<open>v udvd w\<close> |
|
2624 |
obtains u :: \<open>'a word\<close> where \<open>unat w = unat v * unat u\<close> |
|
2625 |
proof (cases \<open>v = 0\<close>) |
|
2626 |
case True |
|
2627 |
moreover from True \<open>v udvd w\<close> have \<open>w = 0\<close> |
|
2628 |
by transfer simp |
|
2629 |
ultimately show thesis |
|
2630 |
using that by simp |
|
2631 |
next |
|
2632 |
case False |
|
2633 |
then have \<open>unat v > 0\<close> |
|
2634 |
by (simp add: unat_gt_0) |
|
2635 |
from \<open>v udvd w\<close> have \<open>unat v dvd unat w\<close> |
|
2636 |
by (simp add: udvd_iff_dvd) |
|
2637 |
then obtain n where \<open>unat w = unat v * n\<close> .. |
|
2638 |
moreover have \<open>n < 2 ^ LENGTH('a)\<close> |
|
2639 |
proof (rule ccontr) |
|
2640 |
assume \<open>\<not> n < 2 ^ LENGTH('a)\<close> |
|
2641 |
then have \<open>n \<ge> 2 ^ LENGTH('a)\<close> |
|
2642 |
by (simp add: not_le) |
|
2643 |
then have \<open>unat v * n \<ge> 2 ^ LENGTH('a)\<close> |
|
2644 |
using \<open>unat v > 0\<close> mult_le_mono [of 1 \<open>unat v\<close> \<open>2 ^ LENGTH('a)\<close> n] |
|
2645 |
by simp |
|
72292 | 2646 |
with \<open>unat w = unat v * n\<close> |
2647 |
have \<open>unat w \<ge> 2 ^ LENGTH('a)\<close> |
|
72262 | 2648 |
by simp |
72292 | 2649 |
with unsigned_less [of w, where ?'a = nat] show False |
2650 |
by linarith |
|
72262 | 2651 |
qed |
2652 |
ultimately have \<open>unat w = unat v * unat (word_of_nat n :: 'a word)\<close> |
|
2653 |
by (auto simp add: take_bit_nat_eq_self_iff intro: sym) |
|
2654 |
with that show thesis . |
|
2655 |
qed |
|
2656 |
||
2657 |
lemma udvd_imp_mod_eq_0: |
|
2658 |
\<open>w mod v = 0\<close> if \<open>v udvd w\<close> |
|
2659 |
using that by transfer simp |
|
2660 |
||
2661 |
lemma mod_eq_0_imp_udvd [intro?]: |
|
2662 |
\<open>v udvd w\<close> if \<open>w mod v = 0\<close> |
|
2663 |
proof - |
|
2664 |
from that have \<open>unat (w mod v) = unat 0\<close> |
|
2665 |
by simp |
|
2666 |
then have \<open>unat w mod unat v = 0\<close> |
|
2667 |
by (simp add: unat_mod_distrib) |
|
2668 |
then have \<open>unat v dvd unat w\<close> .. |
|
2669 |
then show ?thesis |
|
2670 |
by (simp add: udvd_iff_dvd) |
|
2671 |
qed |
|
2672 |
||
72280 | 2673 |
lemma udvd_imp_dvd: |
2674 |
\<open>v dvd w\<close> if \<open>v udvd w\<close> for v w :: \<open>'a::len word\<close> |
|
2675 |
proof - |
|
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2676 |
from that obtain u :: \<open>'a word\<close> where \<open>unat w = unat v * unat u\<close> .. |
72280 | 2677 |
then have \<open>(word_of_nat (unat w) :: 'a word) = word_of_nat (unat v * unat u)\<close> |
2678 |
by simp |
|
2679 |
then have \<open>w = v * u\<close> |
|
2680 |
by simp |
|
2681 |
then show \<open>v dvd w\<close> .. |
|
2682 |
qed |
|
2683 |
||
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2684 |
lemma exp_dvd_iff_exp_udvd: |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2685 |
\<open>2 ^ n dvd w \<longleftrightarrow> 2 ^ n udvd w\<close> for v w :: \<open>'a::len word\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2686 |
proof |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2687 |
assume \<open>2 ^ n udvd w\<close> then show \<open>2 ^ n dvd w\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2688 |
by (rule udvd_imp_dvd) |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2689 |
next |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2690 |
assume \<open>2 ^ n dvd w\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2691 |
then obtain u :: \<open>'a word\<close> where \<open>w = 2 ^ n * u\<close> .. |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2692 |
then have \<open>w = push_bit n u\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2693 |
by (simp add: push_bit_eq_mult) |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2694 |
then show \<open>2 ^ n udvd w\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2695 |
by transfer (simp add: take_bit_push_bit dvd_eq_mod_eq_0 flip: take_bit_eq_mod) |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2696 |
qed |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2697 |
|
72262 | 2698 |
lemma udvd_nat_alt: |
2699 |
\<open>a udvd b \<longleftrightarrow> (\<exists>n. unat b = n * unat a)\<close> |
|
2700 |
by (auto simp add: udvd_iff_dvd) |
|
2701 |
||
2702 |
lemma udvd_unfold_int: |
|
2703 |
\<open>a udvd b \<longleftrightarrow> (\<exists>n\<ge>0. uint b = n * uint a)\<close> |
|
2704 |
apply (auto elim!: dvdE simp add: udvd_iff_dvd) |
|
2705 |
apply (simp only: uint_nat) |
|
2706 |
apply auto |
|
2707 |
using of_nat_0_le_iff apply blast |
|
2708 |
apply (simp only: unat_eq_nat_uint) |
|
2709 |
apply (simp add: nat_mult_distrib) |
|
37660 | 2710 |
done |
2711 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2712 |
lemma unat_minus_one: |
72079 | 2713 |
\<open>unat (w - 1) = unat w - 1\<close> if \<open>w \<noteq> 0\<close> |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2714 |
proof - |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2715 |
have "0 \<le> uint w" by (fact uint_nonnegative) |
72079 | 2716 |
moreover from that have "0 \<noteq> uint w" |
65328 | 2717 |
by (simp add: uint_0_iff) |
2718 |
ultimately have "1 \<le> uint w" |
|
2719 |
by arith |
|
70185 | 2720 |
from uint_lt2p [of w] have "uint w - 1 < 2 ^ LENGTH('a)" |
65328 | 2721 |
by arith |
70185 | 2722 |
with \<open>1 \<le> uint w\<close> have "(uint w - 1) mod 2 ^ LENGTH('a) = uint w - 1" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2723 |
by (auto intro: mod_pos_pos_trivial) |
70185 | 2724 |
with \<open>1 \<le> uint w\<close> have "nat ((uint w - 1) mod 2 ^ LENGTH('a)) = nat (uint w) - 1" |
72079 | 2725 |
by (auto simp del: nat_uint_eq) |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2726 |
then show ?thesis |
72292 | 2727 |
by (simp only: unat_eq_nat_uint word_arith_wis mod_diff_right_eq) |
2728 |
(metis of_int_1 uint_word_of_int unsigned_1) |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2729 |
qed |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2730 |
|
65328 | 2731 |
lemma measure_unat: "p \<noteq> 0 \<Longrightarrow> unat (p - 1) < unat p" |
37660 | 2732 |
by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric]) |
65268 | 2733 |
|
45604 | 2734 |
lemmas uint_add_ge0 [simp] = add_nonneg_nonneg [OF uint_ge_0 uint_ge_0] |
2735 |
lemmas uint_mult_ge0 [simp] = mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0] |
|
37660 | 2736 |
|
70185 | 2737 |
lemma uint_sub_lt2p [simp]: "uint x - uint y < 2 ^ LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2738 |
for x :: "'a::len word" and y :: "'b::len word" |
37660 | 2739 |
using uint_ge_0 [of y] uint_lt2p [of x] by arith |
2740 |
||
2741 |
||
61799 | 2742 |
subsection \<open>Conditions for the addition (etc) of two words to overflow\<close> |
37660 | 2743 |
|
65268 | 2744 |
lemma uint_add_lem: |
70185 | 2745 |
"(uint x + uint y < 2 ^ LENGTH('a)) = |
65328 | 2746 |
(uint (x + y) = uint x + uint y)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2747 |
for x y :: "'a::len word" |
71997 | 2748 |
by (metis add.right_neutral add_mono_thms_linordered_semiring(1) mod_pos_pos_trivial of_nat_0_le_iff uint_lt2p uint_nat uint_word_ariths(1)) |
37660 | 2749 |
|
65268 | 2750 |
lemma uint_mult_lem: |
70185 | 2751 |
"(uint x * uint y < 2 ^ LENGTH('a)) = |
65328 | 2752 |
(uint (x * y) = uint x * uint y)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2753 |
for x y :: "'a::len word" |
71997 | 2754 |
by (metis mod_pos_pos_trivial uint_lt2p uint_mult_ge0 uint_word_ariths(3)) |
37660 | 2755 |
|
65328 | 2756 |
lemma uint_sub_lem: "uint x \<ge> uint y \<longleftrightarrow> uint (x - y) = uint x - uint y" |
72292 | 2757 |
by (metis diff_ge_0_iff_ge of_nat_0_le_iff uint_nat uint_sub_lt2p uint_word_of_int unique_euclidean_semiring_numeral_class.mod_less word_sub_wi) |
65328 | 2758 |
|
2759 |
lemma uint_add_le: "uint (x + y) \<le> uint x + uint y" |
|
71997 | 2760 |
unfolding uint_word_ariths by (simp add: zmod_le_nonneg_dividend) |
37660 | 2761 |
|
65328 | 2762 |
lemma uint_sub_ge: "uint (x - y) \<ge> uint x - uint y" |
72488 | 2763 |
unfolding uint_word_ariths |
2764 |
by (simp flip: take_bit_eq_mod add: take_bit_int_greater_eq_self_iff) |
|
2765 |
||
2766 |
lemma int_mod_ge: \<open>a \<le> a mod n\<close> if \<open>a < n\<close> \<open>0 < n\<close> |
|
2767 |
for a n :: int |
|
2768 |
proof (cases \<open>a < 0\<close>) |
|
2769 |
case True |
|
2770 |
with \<open>0 < n\<close> show ?thesis |
|
2771 |
by (metis less_trans not_less pos_mod_conj) |
|
2772 |
||
2773 |
next |
|
2774 |
case False |
|
2775 |
with \<open>a < n\<close> show ?thesis |
|
2776 |
by simp |
|
2777 |
qed |
|
2778 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2779 |
lemma mod_add_if_z: |
65328 | 2780 |
"x < z \<Longrightarrow> y < z \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> z \<Longrightarrow> |
2781 |
(x + y) mod z = (if x + y < z then x + y else x + y - z)" |
|
2782 |
for x y z :: int |
|
71997 | 2783 |
apply (auto simp add: not_less) |
2784 |
apply (rule antisym) |
|
72488 | 2785 |
apply (metis diff_ge_0_iff_ge minus_mod_self2 zmod_le_nonneg_dividend) |
2786 |
apply (simp only: flip: minus_mod_self2 [of \<open>x + y\<close> z]) |
|
2787 |
apply (metis add.commute add_less_cancel_left add_mono_thms_linordered_field(5) diff_add_cancel diff_ge_0_iff_ge mod_pos_pos_trivial order_refl) |
|
71997 | 2788 |
done |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2789 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2790 |
lemma uint_plus_if': |
65328 | 2791 |
"uint (a + b) = |
70185 | 2792 |
(if uint a + uint b < 2 ^ LENGTH('a) then uint a + uint b |
2793 |
else uint a + uint b - 2 ^ LENGTH('a))" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2794 |
for a b :: "'a::len word" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2795 |
using mod_add_if_z [of "uint a" _ "uint b"] by (simp add: uint_word_ariths) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2796 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2797 |
lemma mod_sub_if_z: |
65328 | 2798 |
"x < z \<Longrightarrow> y < z \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> z \<Longrightarrow> |
2799 |
(x - y) mod z = (if y \<le> x then x - y else x - y + z)" |
|
2800 |
for x y z :: int |
|
71997 | 2801 |
apply (auto simp add: not_le) |
2802 |
apply (rule antisym) |
|
2803 |
apply (simp only: flip: mod_add_self2 [of \<open>x - y\<close> z]) |
|
2804 |
apply (rule zmod_le_nonneg_dividend) |
|
2805 |
apply simp |
|
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
2806 |
apply (metis add.commute add.right_neutral add_le_cancel_left diff_ge_0_iff_ge int_mod_ge le_less le_less_trans mod_add_self1 not_less) |
71997 | 2807 |
done |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2808 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2809 |
lemma uint_sub_if': |
65328 | 2810 |
"uint (a - b) = |
2811 |
(if uint b \<le> uint a then uint a - uint b |
|
70185 | 2812 |
else uint a - uint b + 2 ^ LENGTH('a))" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2813 |
for a b :: "'a::len word" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2814 |
using mod_sub_if_z [of "uint a" _ "uint b"] by (simp add: uint_word_ariths) |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2815 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2816 |
|
61799 | 2817 |
subsection \<open>Definition of \<open>uint_arith\<close>\<close> |
37660 | 2818 |
|
2819 |
lemma word_of_int_inverse: |
|
70185 | 2820 |
"word_of_int r = a \<Longrightarrow> 0 \<le> r \<Longrightarrow> r < 2 ^ LENGTH('a) \<Longrightarrow> uint a = r" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2821 |
for a :: "'a::len word" |
72292 | 2822 |
apply transfer |
2823 |
apply (drule sym) |
|
2824 |
apply (simp add: take_bit_int_eq_self) |
|
37660 | 2825 |
done |
2826 |
||
2827 |
lemma uint_split: |
|
70185 | 2828 |
"P (uint x) = (\<forall>i. word_of_int i = x \<and> 0 \<le> i \<and> i < 2^LENGTH('a) \<longrightarrow> P i)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2829 |
for x :: "'a::len word" |
72262 | 2830 |
by transfer (auto simp add: take_bit_eq_mod) |
37660 | 2831 |
|
2832 |
lemma uint_split_asm: |
|
70185 | 2833 |
"P (uint x) = (\<nexists>i. word_of_int i = x \<and> 0 \<le> i \<and> i < 2^LENGTH('a) \<and> \<not> P i)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2834 |
for x :: "'a::len word" |
72262 | 2835 |
by auto (metis take_bit_int_eq_self_iff) |
37660 | 2836 |
|
2837 |
lemmas uint_splits = uint_split uint_split_asm |
|
2838 |
||
65268 | 2839 |
lemmas uint_arith_simps = |
37660 | 2840 |
word_le_def word_less_alt |
72292 | 2841 |
word_uint_eq_iff |
37660 | 2842 |
uint_sub_if' uint_plus_if' |
2843 |
||
70185 | 2844 |
\<comment> \<open>use this to stop, eg. \<open>2 ^ LENGTH(32)\<close> being simplified\<close> |
65268 | 2845 |
lemma power_False_cong: "False \<Longrightarrow> a ^ b = c ^ d" |
37660 | 2846 |
by auto |
2847 |
||
67408 | 2848 |
\<comment> \<open>\<open>uint_arith_tac\<close>: reduce to arithmetic on int, try to solve by arith\<close> |
61799 | 2849 |
ML \<open> |
72292 | 2850 |
val uint_arith_simpset = |
2851 |
@{context} |
|
2852 |
|> fold Simplifier.add_simp @{thms uint_arith_simps} |
|
2853 |
|> fold Splitter.add_split @{thms if_split_asm} |
|
2854 |
|> fold Simplifier.add_cong @{thms power_False_cong} |
|
2855 |
|> simpset_of; |
|
2856 |
||
65268 | 2857 |
fun uint_arith_tacs ctxt = |
37660 | 2858 |
let |
2859 |
fun arith_tac' n t = |
|
59657
2441a80fb6c1
eliminated unused arith "verbose" flag -- tools that need options can use the context;
wenzelm
parents:
59498
diff
changeset
|
2860 |
Arith_Data.arith_tac ctxt n t |
37660 | 2861 |
handle Cooper.COOPER _ => Seq.empty; |
65268 | 2862 |
in |
42793 | 2863 |
[ clarify_tac ctxt 1, |
72292 | 2864 |
full_simp_tac (put_simpset uint_arith_simpset ctxt) 1, |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2865 |
ALLGOALS (full_simp_tac |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2866 |
(put_simpset HOL_ss ctxt |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2867 |
|> fold Splitter.add_split @{thms uint_splits} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2868 |
|> fold Simplifier.add_cong @{thms power_False_cong})), |
65268 | 2869 |
rewrite_goals_tac ctxt @{thms word_size}, |
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59487
diff
changeset
|
2870 |
ALLGOALS (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN |
60754 | 2871 |
REPEAT (eresolve_tac ctxt [conjE] n) THEN |
65268 | 2872 |
REPEAT (dresolve_tac ctxt @{thms word_of_int_inverse} n |
2873 |
THEN assume_tac ctxt n |
|
58963
26bf09b95dda
proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents:
58874
diff
changeset
|
2874 |
THEN assume_tac ctxt n)), |
37660 | 2875 |
TRYALL arith_tac' ] |
2876 |
end |
|
2877 |
||
2878 |
fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt)) |
|
61799 | 2879 |
\<close> |
37660 | 2880 |
|
65268 | 2881 |
method_setup uint_arith = |
61799 | 2882 |
\<open>Scan.succeed (SIMPLE_METHOD' o uint_arith_tac)\<close> |
37660 | 2883 |
"solving word arithmetic via integers and arith" |
2884 |
||
2885 |
||
61799 | 2886 |
subsection \<open>More on overflows and monotonicity\<close> |
37660 | 2887 |
|
65328 | 2888 |
lemma no_plus_overflow_uint_size: "x \<le> x + y \<longleftrightarrow> uint x + uint y < 2 ^ size x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2889 |
for x y :: "'a::len word" |
37660 | 2890 |
unfolding word_size by uint_arith |
2891 |
||
2892 |
lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size] |
|
2893 |
||
65328 | 2894 |
lemma no_ulen_sub: "x \<ge> x - y \<longleftrightarrow> uint y \<le> uint x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2895 |
for x y :: "'a::len word" |
37660 | 2896 |
by uint_arith |
2897 |
||
70185 | 2898 |
lemma no_olen_add': "x \<le> y + x \<longleftrightarrow> uint y + uint x < 2 ^ LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2899 |
for x y :: "'a::len word" |
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57512
diff
changeset
|
2900 |
by (simp add: ac_simps no_olen_add) |
37660 | 2901 |
|
45604 | 2902 |
lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric]] |
2903 |
||
2904 |
lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem] |
|
2905 |
lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1] |
|
2906 |
lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem] |
|
37660 | 2907 |
lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def] |
2908 |
lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def] |
|
45604 | 2909 |
lemmas word_sub_le = word_sub_le_iff [THEN iffD2] |
37660 | 2910 |
|
65328 | 2911 |
lemma word_less_sub1: "x \<noteq> 0 \<Longrightarrow> 1 < x \<longleftrightarrow> 0 < x - 1" |
2912 |
for x :: "'a::len word" |
|
37660 | 2913 |
by uint_arith |
2914 |
||
65328 | 2915 |
lemma word_le_sub1: "x \<noteq> 0 \<Longrightarrow> 1 \<le> x \<longleftrightarrow> 0 \<le> x - 1" |
2916 |
for x :: "'a::len word" |
|
37660 | 2917 |
by uint_arith |
2918 |
||
65328 | 2919 |
lemma sub_wrap_lt: "x < x - z \<longleftrightarrow> x < z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2920 |
for x z :: "'a::len word" |
37660 | 2921 |
by uint_arith |
2922 |
||
65328 | 2923 |
lemma sub_wrap: "x \<le> x - z \<longleftrightarrow> z = 0 \<or> x < z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2924 |
for x z :: "'a::len word" |
37660 | 2925 |
by uint_arith |
2926 |
||
65328 | 2927 |
lemma plus_minus_not_NULL_ab: "x \<le> ab - c \<Longrightarrow> c \<le> ab \<Longrightarrow> c \<noteq> 0 \<Longrightarrow> x + c \<noteq> 0" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2928 |
for x ab c :: "'a::len word" |
37660 | 2929 |
by uint_arith |
2930 |
||
65328 | 2931 |
lemma plus_minus_no_overflow_ab: "x \<le> ab - c \<Longrightarrow> c \<le> ab \<Longrightarrow> x \<le> x + c" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2932 |
for x ab c :: "'a::len word" |
37660 | 2933 |
by uint_arith |
2934 |
||
65328 | 2935 |
lemma le_minus': "a + c \<le> b \<Longrightarrow> a \<le> a + c \<Longrightarrow> c \<le> b - a" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2936 |
for a b c :: "'a::len word" |
37660 | 2937 |
by uint_arith |
2938 |
||
65328 | 2939 |
lemma le_plus': "a \<le> b \<Longrightarrow> c \<le> b - a \<Longrightarrow> a + c \<le> b" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2940 |
for a b c :: "'a::len word" |
37660 | 2941 |
by uint_arith |
2942 |
||
2943 |
lemmas le_plus = le_plus' [rotated] |
|
2944 |
||
46011 | 2945 |
lemmas le_minus = leD [THEN thin_rl, THEN le_minus'] (* FIXME *) |
37660 | 2946 |
|
65328 | 2947 |
lemma word_plus_mono_right: "y \<le> z \<Longrightarrow> x \<le> x + z \<Longrightarrow> x + y \<le> x + z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2948 |
for x y z :: "'a::len word" |
37660 | 2949 |
by uint_arith |
2950 |
||
65328 | 2951 |
lemma word_less_minus_cancel: "y - x < z - x \<Longrightarrow> x \<le> z \<Longrightarrow> y < z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2952 |
for x y z :: "'a::len word" |
37660 | 2953 |
by uint_arith |
2954 |
||
65328 | 2955 |
lemma word_less_minus_mono_left: "y < z \<Longrightarrow> x \<le> y \<Longrightarrow> y - x < z - x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2956 |
for x y z :: "'a::len word" |
37660 | 2957 |
by uint_arith |
2958 |
||
65328 | 2959 |
lemma word_less_minus_mono: "a < c \<Longrightarrow> d < b \<Longrightarrow> a - b < a \<Longrightarrow> c - d < c \<Longrightarrow> a - b < c - d" |
2960 |
for a b c d :: "'a::len word" |
|
37660 | 2961 |
by uint_arith |
2962 |
||
65328 | 2963 |
lemma word_le_minus_cancel: "y - x \<le> z - x \<Longrightarrow> x \<le> z \<Longrightarrow> y \<le> z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2964 |
for x y z :: "'a::len word" |
37660 | 2965 |
by uint_arith |
2966 |
||
65328 | 2967 |
lemma word_le_minus_mono_left: "y \<le> z \<Longrightarrow> x \<le> y \<Longrightarrow> y - x \<le> z - x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2968 |
for x y z :: "'a::len word" |
37660 | 2969 |
by uint_arith |
2970 |
||
65268 | 2971 |
lemma word_le_minus_mono: |
65328 | 2972 |
"a \<le> c \<Longrightarrow> d \<le> b \<Longrightarrow> a - b \<le> a \<Longrightarrow> c - d \<le> c \<Longrightarrow> a - b \<le> c - d" |
2973 |
for a b c d :: "'a::len word" |
|
37660 | 2974 |
by uint_arith |
2975 |
||
65328 | 2976 |
lemma plus_le_left_cancel_wrap: "x + y' < x \<Longrightarrow> x + y < x \<Longrightarrow> x + y' < x + y \<longleftrightarrow> y' < y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2977 |
for x y y' :: "'a::len word" |
37660 | 2978 |
by uint_arith |
2979 |
||
65328 | 2980 |
lemma plus_le_left_cancel_nowrap: "x \<le> x + y' \<Longrightarrow> x \<le> x + y \<Longrightarrow> x + y' < x + y \<longleftrightarrow> y' < y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2981 |
for x y y' :: "'a::len word" |
37660 | 2982 |
by uint_arith |
2983 |
||
65328 | 2984 |
lemma word_plus_mono_right2: "a \<le> a + b \<Longrightarrow> c \<le> b \<Longrightarrow> a \<le> a + c" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2985 |
for a b c :: "'a::len word" |
65328 | 2986 |
by uint_arith |
2987 |
||
2988 |
lemma word_less_add_right: "x < y - z \<Longrightarrow> z \<le> y \<Longrightarrow> x + z < y" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2989 |
for x y z :: "'a::len word" |
37660 | 2990 |
by uint_arith |
2991 |
||
65328 | 2992 |
lemma word_less_sub_right: "x < y + z \<Longrightarrow> y \<le> x \<Longrightarrow> x - y < z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2993 |
for x y z :: "'a::len word" |
37660 | 2994 |
by uint_arith |
2995 |
||
65328 | 2996 |
lemma word_le_plus_either: "x \<le> y \<or> x \<le> z \<Longrightarrow> y \<le> y + z \<Longrightarrow> x \<le> y + z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
2997 |
for x y z :: "'a::len word" |
37660 | 2998 |
by uint_arith |
2999 |
||
65328 | 3000 |
lemma word_less_nowrapI: "x < z - k \<Longrightarrow> k \<le> z \<Longrightarrow> 0 < k \<Longrightarrow> x < x + k" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3001 |
for x z k :: "'a::len word" |
37660 | 3002 |
by uint_arith |
3003 |
||
65328 | 3004 |
lemma inc_le: "i < m \<Longrightarrow> i + 1 \<le> m" |
3005 |
for i m :: "'a::len word" |
|
37660 | 3006 |
by uint_arith |
3007 |
||
65328 | 3008 |
lemma inc_i: "1 \<le> i \<Longrightarrow> i < m \<Longrightarrow> 1 \<le> i + 1 \<and> i + 1 \<le> m" |
3009 |
for i m :: "'a::len word" |
|
37660 | 3010 |
by uint_arith |
3011 |
||
3012 |
lemma udvd_incr_lem: |
|
65268 | 3013 |
"up < uq \<Longrightarrow> up = ua + n * uint K \<Longrightarrow> |
65328 | 3014 |
uq = ua + n' * uint K \<Longrightarrow> up + uint K \<le> uq" |
71997 | 3015 |
by auto (metis int_distrib(1) linorder_not_less mult.left_neutral mult_right_mono uint_nonnegative zless_imp_add1_zle) |
37660 | 3016 |
|
65268 | 3017 |
lemma udvd_incr': |
3018 |
"p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow> |
|
65328 | 3019 |
uint q = ua + n' * uint K \<Longrightarrow> p + K \<le> q" |
37660 | 3020 |
apply (unfold word_less_alt word_le_def) |
3021 |
apply (drule (2) udvd_incr_lem) |
|
3022 |
apply (erule uint_add_le [THEN order_trans]) |
|
3023 |
done |
|
3024 |
||
65268 | 3025 |
lemma udvd_decr': |
3026 |
"p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow> |
|
65328 | 3027 |
uint q = ua + n' * uint K \<Longrightarrow> p \<le> q - K" |
37660 | 3028 |
apply (unfold word_less_alt word_le_def) |
3029 |
apply (drule (2) udvd_incr_lem) |
|
3030 |
apply (drule le_diff_eq [THEN iffD2]) |
|
3031 |
apply (erule order_trans) |
|
3032 |
apply (rule uint_sub_ge) |
|
3033 |
done |
|
3034 |
||
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3035 |
lemmas udvd_incr_lem0 = udvd_incr_lem [where ua=0, unfolded add_0_left] |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3036 |
lemmas udvd_incr0 = udvd_incr' [where ua=0, unfolded add_0_left] |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3037 |
lemmas udvd_decr0 = udvd_decr' [where ua=0, unfolded add_0_left] |
37660 | 3038 |
|
65328 | 3039 |
lemma udvd_minus_le': "xy < k \<Longrightarrow> z udvd xy \<Longrightarrow> z udvd k \<Longrightarrow> xy \<le> k - z" |
72262 | 3040 |
apply (unfold udvd_unfold_int) |
37660 | 3041 |
apply clarify |
3042 |
apply (erule (2) udvd_decr0) |
|
3043 |
done |
|
3044 |
||
65268 | 3045 |
lemma udvd_incr2_K: |
65328 | 3046 |
"p < a + s \<Longrightarrow> a \<le> a + s \<Longrightarrow> K udvd s \<Longrightarrow> K udvd p - a \<Longrightarrow> a \<le> p \<Longrightarrow> |
3047 |
0 < K \<Longrightarrow> p \<le> p + K \<and> p + K \<le> a + s" |
|
3048 |
supply [[simproc del: linordered_ring_less_cancel_factor]] |
|
72262 | 3049 |
apply (unfold udvd_unfold_int) |
37660 | 3050 |
apply clarify |
62390 | 3051 |
apply (simp add: uint_arith_simps split: if_split_asm) |
65268 | 3052 |
prefer 2 |
72292 | 3053 |
using uint_lt2p [of s] apply simp |
71997 | 3054 |
apply (drule add.commute [THEN xtrans(1)]) |
3055 |
apply (simp flip: diff_less_eq) |
|
3056 |
apply (subst (asm) mult_less_cancel_right) |
|
37660 | 3057 |
apply simp |
71997 | 3058 |
apply (simp add: diff_eq_eq not_less) |
3059 |
apply (subst (asm) (3) zless_iff_Suc_zadd) |
|
3060 |
apply auto |
|
3061 |
apply (auto simp add: algebra_simps) |
|
3062 |
apply (drule less_le_trans [of _ \<open>2 ^ LENGTH('a)\<close>]) apply assumption |
|
3063 |
apply (simp add: mult_less_0_iff) |
|
37660 | 3064 |
done |
3065 |
||
3066 |
||
61799 | 3067 |
subsection \<open>Arithmetic type class instantiations\<close> |
37660 | 3068 |
|
3069 |
lemmas word_le_0_iff [simp] = |
|
70749
5d06b7bb9d22
More type class generalisations. Note that linorder_antisym_conv1 and linorder_antisym_conv2 no longer exist.
paulson <lp15@cam.ac.uk>
parents:
70342
diff
changeset
|
3070 |
word_zero_le [THEN leD, THEN antisym_conv1] |
37660 | 3071 |
|
65328 | 3072 |
lemma word_of_int_nat: "0 \<le> x \<Longrightarrow> word_of_int x = of_nat (nat x)" |
72262 | 3073 |
by simp |
37660 | 3074 |
|
67408 | 3075 |
text \<open> |
3076 |
note that \<open>iszero_def\<close> is only for class \<open>comm_semiring_1_cancel\<close>, |
|
3077 |
which requires word length \<open>\<ge> 1\<close>, ie \<open>'a::len word\<close> |
|
3078 |
\<close> |
|
46603 | 3079 |
lemma iszero_word_no [simp]: |
65268 | 3080 |
"iszero (numeral bin :: 'a::len word) = |
72128 | 3081 |
iszero (take_bit LENGTH('a) (numeral bin :: int))" |
72292 | 3082 |
apply (simp add: iszero_def) |
3083 |
apply transfer |
|
3084 |
apply simp |
|
3085 |
done |
|
65268 | 3086 |
|
61799 | 3087 |
text \<open>Use \<open>iszero\<close> to simplify equalities between word numerals.\<close> |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3088 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3089 |
lemmas word_eq_numeral_iff_iszero [simp] = |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3090 |
eq_numeral_iff_iszero [where 'a="'a::len word"] |
46603 | 3091 |
|
37660 | 3092 |
|
61799 | 3093 |
subsection \<open>Word and nat\<close> |
37660 | 3094 |
|
70185 | 3095 |
lemma word_nchotomy: "\<forall>w :: 'a::len word. \<exists>n. w = of_nat n \<and> n < 2 ^ LENGTH('a)" |
37660 | 3096 |
apply (rule allI) |
72292 | 3097 |
apply (rule exI [of _ \<open>unat w\<close> for w :: \<open>'a word\<close>]) |
3098 |
apply simp |
|
37660 | 3099 |
done |
3100 |
||
70185 | 3101 |
lemma of_nat_eq: "of_nat n = w \<longleftrightarrow> (\<exists>q. n = unat w + q * 2 ^ LENGTH('a))" |
65328 | 3102 |
for w :: "'a::len word" |
68157 | 3103 |
using mod_div_mult_eq [of n "2 ^ LENGTH('a)", symmetric] |
72292 | 3104 |
by (auto simp flip: take_bit_eq_mod) |
37660 | 3105 |
|
65328 | 3106 |
lemma of_nat_eq_size: "of_nat n = w \<longleftrightarrow> (\<exists>q. n = unat w + q * 2 ^ size w)" |
37660 | 3107 |
unfolding word_size by (rule of_nat_eq) |
3108 |
||
70185 | 3109 |
lemma of_nat_0: "of_nat m = (0::'a::len word) \<longleftrightarrow> (\<exists>q. m = q * 2 ^ LENGTH('a))" |
37660 | 3110 |
by (simp add: of_nat_eq) |
3111 |
||
70185 | 3112 |
lemma of_nat_2p [simp]: "of_nat (2 ^ LENGTH('a)) = (0::'a::len word)" |
45805 | 3113 |
by (fact mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]]) |
37660 | 3114 |
|
65328 | 3115 |
lemma of_nat_gt_0: "of_nat k \<noteq> 0 \<Longrightarrow> 0 < k" |
37660 | 3116 |
by (cases k) auto |
3117 |
||
70185 | 3118 |
lemma of_nat_neq_0: "0 < k \<Longrightarrow> k < 2 ^ LENGTH('a::len) \<Longrightarrow> of_nat k \<noteq> (0 :: 'a word)" |
65328 | 3119 |
by (auto simp add : of_nat_0) |
3120 |
||
3121 |
lemma Abs_fnat_hom_add: "of_nat a + of_nat b = of_nat (a + b)" |
|
37660 | 3122 |
by simp |
3123 |
||
65328 | 3124 |
lemma Abs_fnat_hom_mult: "of_nat a * of_nat b = (of_nat (a * b) :: 'a::len word)" |
72262 | 3125 |
by (simp add: wi_hom_mult) |
37660 | 3126 |
|
65328 | 3127 |
lemma Abs_fnat_hom_Suc: "word_succ (of_nat a) = of_nat (Suc a)" |
72262 | 3128 |
by transfer (simp add: ac_simps) |
37660 | 3129 |
|
3130 |
lemma Abs_fnat_hom_0: "(0::'a::len word) = of_nat 0" |
|
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
3131 |
by simp |
37660 | 3132 |
|
3133 |
lemma Abs_fnat_hom_1: "(1::'a::len word) = of_nat (Suc 0)" |
|
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
3134 |
by simp |
37660 | 3135 |
|
65268 | 3136 |
lemmas Abs_fnat_homs = |
3137 |
Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc |
|
37660 | 3138 |
Abs_fnat_hom_0 Abs_fnat_hom_1 |
3139 |
||
65328 | 3140 |
lemma word_arith_nat_add: "a + b = of_nat (unat a + unat b)" |
3141 |
by simp |
|
3142 |
||
3143 |
lemma word_arith_nat_mult: "a * b = of_nat (unat a * unat b)" |
|
37660 | 3144 |
by simp |
3145 |
||
65328 | 3146 |
lemma word_arith_nat_Suc: "word_succ a = of_nat (Suc (unat a))" |
37660 | 3147 |
by (subst Abs_fnat_hom_Suc [symmetric]) simp |
3148 |
||
65328 | 3149 |
lemma word_arith_nat_div: "a div b = of_nat (unat a div unat b)" |
72262 | 3150 |
by (metis of_int_of_nat_eq of_nat_unat of_nat_div word_div_def) |
3151 |
||
65328 | 3152 |
lemma word_arith_nat_mod: "a mod b = of_nat (unat a mod unat b)" |
72262 | 3153 |
by (metis of_int_of_nat_eq of_nat_mod of_nat_unat word_mod_def) |
37660 | 3154 |
|
3155 |
lemmas word_arith_nat_defs = |
|
3156 |
word_arith_nat_add word_arith_nat_mult |
|
3157 |
word_arith_nat_Suc Abs_fnat_hom_0 |
|
3158 |
Abs_fnat_hom_1 word_arith_nat_div |
|
65268 | 3159 |
word_arith_nat_mod |
37660 | 3160 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3161 |
lemma unat_cong: "x = y \<Longrightarrow> unat x = unat y" |
72292 | 3162 |
by (fact arg_cong) |
3163 |
||
3164 |
lemma unat_of_nat: |
|
3165 |
\<open>unat (word_of_nat x :: 'a::len word) = x mod 2 ^ LENGTH('a)\<close> |
|
3166 |
by transfer (simp flip: take_bit_eq_mod add: nat_take_bit_eq) |
|
65268 | 3167 |
|
37660 | 3168 |
lemmas unat_word_ariths = word_arith_nat_defs |
45604 | 3169 |
[THEN trans [OF unat_cong unat_of_nat]] |
37660 | 3170 |
|
3171 |
lemmas word_sub_less_iff = word_sub_le_iff |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3172 |
[unfolded linorder_not_less [symmetric] Not_eq_iff] |
37660 | 3173 |
|
65268 | 3174 |
lemma unat_add_lem: |
70185 | 3175 |
"unat x + unat y < 2 ^ LENGTH('a) \<longleftrightarrow> unat (x + y) = unat x + unat y" |
65328 | 3176 |
for x y :: "'a::len word" |
71997 | 3177 |
apply (auto simp: unat_word_ariths) |
72292 | 3178 |
apply (drule sym) |
3179 |
apply (metis unat_of_nat unsigned_less) |
|
71997 | 3180 |
done |
37660 | 3181 |
|
65268 | 3182 |
lemma unat_mult_lem: |
70185 | 3183 |
"unat x * unat y < 2 ^ LENGTH('a) \<longleftrightarrow> unat (x * y) = unat x * unat y" |
65363 | 3184 |
for x y :: "'a::len word" |
71997 | 3185 |
apply (auto simp: unat_word_ariths) |
72292 | 3186 |
apply (drule sym) |
3187 |
apply (metis unat_of_nat unsigned_less) |
|
71997 | 3188 |
done |
3189 |
||
3190 |
lemma unat_plus_if': |
|
3191 |
\<open>unat (a + b) = |
|
3192 |
(if unat a + unat b < 2 ^ LENGTH('a) |
|
3193 |
then unat a + unat b |
|
3194 |
else unat a + unat b - 2 ^ LENGTH('a))\<close> for a b :: \<open>'a::len word\<close> |
|
3195 |
apply (auto simp: unat_word_ariths not_less) |
|
3196 |
apply (subst (asm) le_iff_add) |
|
3197 |
apply auto |
|
72292 | 3198 |
apply (simp flip: take_bit_eq_mod add: take_bit_nat_eq_self_iff) |
3199 |
apply (metis add.commute add_less_cancel_right le_less_trans less_imp_le unsigned_less) |
|
71997 | 3200 |
done |
65328 | 3201 |
|
3202 |
lemma le_no_overflow: "x \<le> b \<Longrightarrow> a \<le> a + b \<Longrightarrow> x \<le> a + b" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3203 |
for a b x :: "'a::len word" |
37660 | 3204 |
apply (erule order_trans) |
3205 |
apply (erule olen_add_eqv [THEN iffD1]) |
|
3206 |
done |
|
3207 |
||
65328 | 3208 |
lemmas un_ui_le = |
3209 |
trans [OF word_le_nat_alt [symmetric] word_le_def] |
|
37660 | 3210 |
|
3211 |
lemma unat_sub_if_size: |
|
65328 | 3212 |
"unat (x - y) = |
3213 |
(if unat y \<le> unat x |
|
3214 |
then unat x - unat y |
|
3215 |
else unat x + 2 ^ size x - unat y)" |
|
72079 | 3216 |
supply nat_uint_eq [simp del] |
37660 | 3217 |
apply (unfold word_size) |
3218 |
apply (simp add: un_ui_le) |
|
72079 | 3219 |
apply (auto simp add: unat_eq_nat_uint uint_sub_if') |
37660 | 3220 |
apply (rule nat_diff_distrib) |
3221 |
prefer 3 |
|
3222 |
apply (simp add: algebra_simps) |
|
3223 |
apply (rule nat_diff_distrib [THEN trans]) |
|
3224 |
prefer 3 |
|
3225 |
apply (subst nat_add_distrib) |
|
3226 |
prefer 3 |
|
3227 |
apply (simp add: nat_power_eq) |
|
3228 |
apply auto |
|
3229 |
apply uint_arith |
|
3230 |
done |
|
3231 |
||
3232 |
lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size] |
|
3233 |
||
71997 | 3234 |
lemma uint_div: |
3235 |
\<open>uint (x div y) = uint x div uint y\<close> |
|
72262 | 3236 |
by (fact uint_div_distrib) |
71997 | 3237 |
|
3238 |
lemma unat_div: |
|
3239 |
\<open>unat (x div y) = unat x div unat y\<close> |
|
72262 | 3240 |
by (fact unat_div_distrib) |
71997 | 3241 |
|
3242 |
lemma uint_mod: |
|
3243 |
\<open>uint (x mod y) = uint x mod uint y\<close> |
|
72262 | 3244 |
by (fact uint_mod_distrib) |
71997 | 3245 |
|
72079 | 3246 |
lemma unat_mod: |
3247 |
\<open>unat (x mod y) = unat x mod unat y\<close> |
|
72262 | 3248 |
by (fact unat_mod_distrib) |
71997 | 3249 |
|
37660 | 3250 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3251 |
text \<open>Definition of \<open>unat_arith\<close> tactic\<close> |
37660 | 3252 |
|
70185 | 3253 |
lemma unat_split: "P (unat x) \<longleftrightarrow> (\<forall>n. of_nat n = x \<and> n < 2^LENGTH('a) \<longrightarrow> P n)" |
65328 | 3254 |
for x :: "'a::len word" |
72262 | 3255 |
by auto (metis take_bit_nat_eq_self_iff) |
37660 | 3256 |
|
70185 | 3257 |
lemma unat_split_asm: "P (unat x) \<longleftrightarrow> (\<nexists>n. of_nat n = x \<and> n < 2^LENGTH('a) \<and> \<not> P n)" |
65328 | 3258 |
for x :: "'a::len word" |
72262 | 3259 |
by auto (metis take_bit_nat_eq_self_iff) |
37660 | 3260 |
|
72292 | 3261 |
lemma of_nat_inverse: |
3262 |
\<open>word_of_nat r = a \<Longrightarrow> r < 2 ^ LENGTH('a) \<Longrightarrow> unat a = r\<close> |
|
3263 |
for a :: \<open>'a::len word\<close> |
|
3264 |
apply (drule sym) |
|
3265 |
apply transfer |
|
3266 |
apply (simp add: take_bit_int_eq_self) |
|
3267 |
done |
|
3268 |
||
3269 |
lemma word_unat_eq_iff: |
|
3270 |
\<open>v = w \<longleftrightarrow> unat v = unat w\<close> |
|
3271 |
for v w :: \<open>'a::len word\<close> |
|
3272 |
by (fact word_eq_iff_unsigned) |
|
37660 | 3273 |
|
3274 |
lemmas unat_splits = unat_split unat_split_asm |
|
3275 |
||
3276 |
lemmas unat_arith_simps = |
|
3277 |
word_le_nat_alt word_less_nat_alt |
|
72292 | 3278 |
word_unat_eq_iff |
37660 | 3279 |
unat_sub_if' unat_plus_if' unat_div unat_mod |
3280 |
||
67408 | 3281 |
\<comment> \<open>\<open>unat_arith_tac\<close>: tactic to reduce word arithmetic to \<open>nat\<close>, try to solve via \<open>arith\<close>\<close> |
61799 | 3282 |
ML \<open> |
72292 | 3283 |
val unat_arith_simpset = |
3284 |
@{context} |
|
3285 |
|> fold Simplifier.add_simp @{thms unat_arith_simps} |
|
3286 |
|> fold Splitter.add_split @{thms if_split_asm} |
|
3287 |
|> fold Simplifier.add_cong @{thms power_False_cong} |
|
3288 |
|> simpset_of |
|
37660 | 3289 |
|
65268 | 3290 |
fun unat_arith_tacs ctxt = |
37660 | 3291 |
let |
3292 |
fun arith_tac' n t = |
|
59657
2441a80fb6c1
eliminated unused arith "verbose" flag -- tools that need options can use the context;
wenzelm
parents:
59498
diff
changeset
|
3293 |
Arith_Data.arith_tac ctxt n t |
37660 | 3294 |
handle Cooper.COOPER _ => Seq.empty; |
65268 | 3295 |
in |
42793 | 3296 |
[ clarify_tac ctxt 1, |
72292 | 3297 |
full_simp_tac (put_simpset unat_arith_simpset ctxt) 1, |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
3298 |
ALLGOALS (full_simp_tac |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
3299 |
(put_simpset HOL_ss ctxt |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
3300 |
|> fold Splitter.add_split @{thms unat_splits} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
3301 |
|> fold Simplifier.add_cong @{thms power_False_cong})), |
65268 | 3302 |
rewrite_goals_tac ctxt @{thms word_size}, |
60754 | 3303 |
ALLGOALS (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN |
3304 |
REPEAT (eresolve_tac ctxt [conjE] n) THEN |
|
3305 |
REPEAT (dresolve_tac ctxt @{thms of_nat_inverse} n THEN assume_tac ctxt n)), |
|
65268 | 3306 |
TRYALL arith_tac' ] |
37660 | 3307 |
end |
3308 |
||
3309 |
fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt)) |
|
61799 | 3310 |
\<close> |
37660 | 3311 |
|
65268 | 3312 |
method_setup unat_arith = |
61799 | 3313 |
\<open>Scan.succeed (SIMPLE_METHOD' o unat_arith_tac)\<close> |
37660 | 3314 |
"solving word arithmetic via natural numbers and arith" |
3315 |
||
65328 | 3316 |
lemma no_plus_overflow_unat_size: "x \<le> x + y \<longleftrightarrow> unat x + unat y < 2 ^ size x" |
3317 |
for x y :: "'a::len word" |
|
37660 | 3318 |
unfolding word_size by unat_arith |
3319 |
||
65328 | 3320 |
lemmas no_olen_add_nat = |
3321 |
no_plus_overflow_unat_size [unfolded word_size] |
|
3322 |
||
3323 |
lemmas unat_plus_simple = |
|
3324 |
trans [OF no_olen_add_nat unat_add_lem] |
|
3325 |
||
70185 | 3326 |
lemma word_div_mult: "0 < y \<Longrightarrow> unat x * unat y < 2 ^ LENGTH('a) \<Longrightarrow> x * y div y = x" |
65328 | 3327 |
for x y :: "'a::len word" |
37660 | 3328 |
apply unat_arith |
3329 |
apply clarsimp |
|
3330 |
apply (subst unat_mult_lem [THEN iffD1]) |
|
65328 | 3331 |
apply auto |
37660 | 3332 |
done |
3333 |
||
70185 | 3334 |
lemma div_lt': "i \<le> k div x \<Longrightarrow> unat i * unat x < 2 ^ LENGTH('a)" |
65328 | 3335 |
for i k x :: "'a::len word" |
37660 | 3336 |
apply unat_arith |
3337 |
apply clarsimp |
|
3338 |
apply (drule mult_le_mono1) |
|
3339 |
apply (erule order_le_less_trans) |
|
72292 | 3340 |
apply (metis add_lessD1 div_mult_mod_eq unsigned_less) |
37660 | 3341 |
done |
3342 |
||
3343 |
lemmas div_lt'' = order_less_imp_le [THEN div_lt'] |
|
3344 |
||
65328 | 3345 |
lemma div_lt_mult: "i < k div x \<Longrightarrow> 0 < x \<Longrightarrow> i * x < k" |
3346 |
for i k x :: "'a::len word" |
|
37660 | 3347 |
apply (frule div_lt'' [THEN unat_mult_lem [THEN iffD1]]) |
3348 |
apply (simp add: unat_arith_simps) |
|
3349 |
apply (drule (1) mult_less_mono1) |
|
3350 |
apply (erule order_less_le_trans) |
|
71997 | 3351 |
apply auto |
37660 | 3352 |
done |
3353 |
||
65328 | 3354 |
lemma div_le_mult: "i \<le> k div x \<Longrightarrow> 0 < x \<Longrightarrow> i * x \<le> k" |
3355 |
for i k x :: "'a::len word" |
|
37660 | 3356 |
apply (frule div_lt' [THEN unat_mult_lem [THEN iffD1]]) |
3357 |
apply (simp add: unat_arith_simps) |
|
3358 |
apply (drule mult_le_mono1) |
|
3359 |
apply (erule order_trans) |
|
71997 | 3360 |
apply auto |
37660 | 3361 |
done |
3362 |
||
70185 | 3363 |
lemma div_lt_uint': "i \<le> k div x \<Longrightarrow> uint i * uint x < 2 ^ LENGTH('a)" |
65328 | 3364 |
for i k x :: "'a::len word" |
37660 | 3365 |
apply (unfold uint_nat) |
3366 |
apply (drule div_lt') |
|
65328 | 3367 |
apply (metis of_nat_less_iff of_nat_mult of_nat_numeral of_nat_power) |
3368 |
done |
|
37660 | 3369 |
|
3370 |
lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint'] |
|
3371 |
||
70185 | 3372 |
lemma word_le_exists': "x \<le> y \<Longrightarrow> \<exists>z. y = x + z \<and> uint x + uint z < 2 ^ LENGTH('a)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3373 |
for x y z :: "'a::len word" |
71997 | 3374 |
by (metis add_diff_cancel_left' add_diff_eq uint_add_lem uint_plus_simple) |
3375 |
||
37660 | 3376 |
lemmas plus_minus_not_NULL = order_less_imp_le [THEN plus_minus_not_NULL_ab] |
3377 |
||
3378 |
lemmas plus_minus_no_overflow = |
|
3379 |
order_less_imp_le [THEN plus_minus_no_overflow_ab] |
|
65268 | 3380 |
|
37660 | 3381 |
lemmas mcs = word_less_minus_cancel word_less_minus_mono_left |
3382 |
word_le_minus_cancel word_le_minus_mono_left |
|
3383 |
||
45604 | 3384 |
lemmas word_l_diffs = mcs [where y = "w + x", unfolded add_diff_cancel] for w x |
3385 |
lemmas word_diff_ls = mcs [where z = "w + x", unfolded add_diff_cancel] for w x |
|
3386 |
lemmas word_plus_mcs = word_diff_ls [where y = "v + x", unfolded add_diff_cancel] for v x |
|
37660 | 3387 |
|
72292 | 3388 |
lemma le_unat_uoi: |
3389 |
\<open>y \<le> unat z \<Longrightarrow> unat (word_of_nat y :: 'a word) = y\<close> |
|
3390 |
for z :: \<open>'a::len word\<close> |
|
3391 |
by transfer (simp add: nat_take_bit_eq take_bit_nat_eq_self_iff le_less_trans) |
|
37660 | 3392 |
|
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66453
diff
changeset
|
3393 |
lemmas thd = times_div_less_eq_dividend |
37660 | 3394 |
|
71997 | 3395 |
lemmas uno_simps [THEN le_unat_uoi] = mod_le_divisor div_le_dividend |
37660 | 3396 |
|
65328 | 3397 |
lemma word_mod_div_equality: "(n div b) * b + (n mod b) = n" |
3398 |
for n b :: "'a::len word" |
|
71997 | 3399 |
by (fact div_mult_mod_eq) |
37660 | 3400 |
|
65328 | 3401 |
lemma word_div_mult_le: "a div b * b \<le> a" |
3402 |
for a b :: "'a::len word" |
|
71997 | 3403 |
by (metis div_le_mult mult_not_zero order.not_eq_order_implies_strict order_refl word_zero_le) |
37660 | 3404 |
|
65328 | 3405 |
lemma word_mod_less_divisor: "0 < n \<Longrightarrow> m mod n < n" |
3406 |
for m n :: "'a::len word" |
|
71997 | 3407 |
by (simp add: unat_arith_simps) |
3408 |
||
65328 | 3409 |
lemma word_of_int_power_hom: "word_of_int a ^ n = (word_of_int (a ^ n) :: 'a::len word)" |
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
3410 |
by (induct n) (simp_all add: wi_hom_mult [symmetric]) |
37660 | 3411 |
|
65328 | 3412 |
lemma word_arith_power_alt: "a ^ n = (word_of_int (uint a ^ n) :: 'a::len word)" |
37660 | 3413 |
by (simp add : word_of_int_power_hom [symmetric]) |
3414 |
||
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3415 |
lemma unatSuc: "1 + n \<noteq> 0 \<Longrightarrow> unat (1 + n) = Suc (unat n)" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3416 |
for n :: "'a::len word" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3417 |
by unat_arith |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3418 |
|
37660 | 3419 |
|
61799 | 3420 |
subsection \<open>Cardinality, finiteness of set of words\<close> |
37660 | 3421 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3422 |
lemma inj_on_word_of_int: \<open>inj_on (word_of_int :: int \<Rightarrow> 'a word) {0..<2 ^ LENGTH('a::len)}\<close> |
72130
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
3423 |
apply (rule inj_onI) |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
3424 |
apply transfer |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
3425 |
apply (simp add: take_bit_eq_mod) |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
3426 |
done |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
3427 |
|
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
3428 |
lemma inj_uint: \<open>inj uint\<close> |
72292 | 3429 |
by (fact inj_unsigned) |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
3430 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3431 |
lemma range_uint: \<open>range (uint :: 'a word \<Rightarrow> int) = {0..<2 ^ LENGTH('a::len)}\<close> |
72488 | 3432 |
apply transfer |
3433 |
apply (auto simp add: image_iff) |
|
3434 |
apply (metis take_bit_int_eq_self_iff) |
|
3435 |
done |
|
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
3436 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3437 |
lemma UNIV_eq: \<open>(UNIV :: 'a word set) = word_of_int ` {0..<2 ^ LENGTH('a::len)}\<close> |
72488 | 3438 |
by (auto simp add: image_iff) (metis atLeastLessThan_iff linorder_not_le uint_split) |
45809
2bee94cbae72
finite class instance for word type; remove unused lemmas
huffman
parents:
45808
diff
changeset
|
3439 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3440 |
lemma card_word: "CARD('a word) = 2 ^ LENGTH('a::len)" |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
3441 |
by (simp add: UNIV_eq card_image inj_on_word_of_int) |
37660 | 3442 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3443 |
lemma card_word_size: "CARD('a word) = 2 ^ size x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3444 |
for x :: "'a::len word" |
65328 | 3445 |
unfolding word_size by (rule card_word) |
37660 | 3446 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3447 |
instance word :: (len) finite |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
3448 |
by standard (simp add: UNIV_eq) |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
3449 |
|
37660 | 3450 |
|
61799 | 3451 |
subsection \<open>Bitwise Operations on Words\<close> |
37660 | 3452 |
|
46011 | 3453 |
lemma word_wi_log_defs: |
71149 | 3454 |
"NOT (word_of_int a) = word_of_int (NOT a)" |
46011 | 3455 |
"word_of_int a AND word_of_int b = word_of_int (a AND b)" |
3456 |
"word_of_int a OR word_of_int b = word_of_int (a OR b)" |
|
3457 |
"word_of_int a XOR word_of_int b = word_of_int (a XOR b)" |
|
47374
9475d524bafb
set up and use lift_definition for word operations
huffman
parents:
47372
diff
changeset
|
3458 |
by (transfer, rule refl)+ |
47372 | 3459 |
|
46011 | 3460 |
lemma word_no_log_defs [simp]: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3461 |
"NOT (numeral a) = word_of_int (NOT (numeral a))" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3462 |
"NOT (- numeral a) = word_of_int (NOT (- numeral a))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3463 |
"numeral a AND numeral b = word_of_int (numeral a AND numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3464 |
"numeral a AND - numeral b = word_of_int (numeral a AND - numeral b)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3465 |
"- numeral a AND numeral b = word_of_int (- numeral a AND numeral b)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3466 |
"- numeral a AND - numeral b = word_of_int (- numeral a AND - numeral b)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3467 |
"numeral a OR numeral b = word_of_int (numeral a OR numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3468 |
"numeral a OR - numeral b = word_of_int (numeral a OR - numeral b)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3469 |
"- numeral a OR numeral b = word_of_int (- numeral a OR numeral b)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3470 |
"- numeral a OR - numeral b = word_of_int (- numeral a OR - numeral b)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3471 |
"numeral a XOR numeral b = word_of_int (numeral a XOR numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3472 |
"numeral a XOR - numeral b = word_of_int (numeral a XOR - numeral b)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3473 |
"- numeral a XOR numeral b = word_of_int (- numeral a XOR numeral b)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3474 |
"- numeral a XOR - numeral b = word_of_int (- numeral a XOR - numeral b)" |
47372 | 3475 |
by (transfer, rule refl)+ |
37660 | 3476 |
|
61799 | 3477 |
text \<open>Special cases for when one of the arguments equals 1.\<close> |
46064
88ef116e0522
add simp rules for bitwise word operations with 1
huffman
parents:
46057
diff
changeset
|
3478 |
|
88ef116e0522
add simp rules for bitwise word operations with 1
huffman
parents:
46057
diff
changeset
|
3479 |
lemma word_bitwise_1_simps [simp]: |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3480 |
"NOT (1::'a::len word) = -2" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3481 |
"1 AND numeral b = word_of_int (1 AND numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3482 |
"1 AND - numeral b = word_of_int (1 AND - numeral b)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3483 |
"numeral a AND 1 = word_of_int (numeral a AND 1)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3484 |
"- numeral a AND 1 = word_of_int (- numeral a AND 1)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3485 |
"1 OR numeral b = word_of_int (1 OR numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3486 |
"1 OR - numeral b = word_of_int (1 OR - numeral b)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3487 |
"numeral a OR 1 = word_of_int (numeral a OR 1)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3488 |
"- numeral a OR 1 = word_of_int (- numeral a OR 1)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3489 |
"1 XOR numeral b = word_of_int (1 XOR numeral b)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3490 |
"1 XOR - numeral b = word_of_int (1 XOR - numeral b)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3491 |
"numeral a XOR 1 = word_of_int (numeral a XOR 1)" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3492 |
"- numeral a XOR 1 = word_of_int (- numeral a XOR 1)" |
47372 | 3493 |
by (transfer, simp)+ |
46064
88ef116e0522
add simp rules for bitwise word operations with 1
huffman
parents:
46057
diff
changeset
|
3494 |
|
61799 | 3495 |
text \<open>Special cases for when one of the arguments equals -1.\<close> |
56979 | 3496 |
|
3497 |
lemma word_bitwise_m1_simps [simp]: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3498 |
"NOT (-1::'a::len word) = 0" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3499 |
"(-1::'a::len word) AND x = x" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3500 |
"x AND (-1::'a::len word) = x" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3501 |
"(-1::'a::len word) OR x = -1" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3502 |
"x OR (-1::'a::len word) = -1" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3503 |
" (-1::'a::len word) XOR x = NOT x" |
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3504 |
"x XOR (-1::'a::len word) = NOT x" |
56979 | 3505 |
by (transfer, simp)+ |
3506 |
||
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3507 |
lemma uint_and: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3508 |
\<open>uint (x AND y) = uint x AND uint y\<close> |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3509 |
by transfer simp |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3510 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3511 |
lemma uint_or: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3512 |
\<open>uint (x OR y) = uint x OR uint y\<close> |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3513 |
by transfer simp |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3514 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3515 |
lemma uint_xor: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3516 |
\<open>uint (x XOR y) = uint x XOR uint y\<close> |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3517 |
by transfer simp |
47372 | 3518 |
|
67408 | 3519 |
\<comment> \<open>get from commutativity, associativity etc of \<open>int_and\<close> etc to same for \<open>word_and etc\<close>\<close> |
65268 | 3520 |
lemmas bwsimps = |
46013 | 3521 |
wi_hom_add |
37660 | 3522 |
word_wi_log_defs |
3523 |
||
3524 |
lemma word_bw_assocs: |
|
3525 |
"(x AND y) AND z = x AND y AND z" |
|
3526 |
"(x OR y) OR z = x OR y OR z" |
|
3527 |
"(x XOR y) XOR z = x XOR y XOR z" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3528 |
for x :: "'a::len word" |
72508 | 3529 |
by (fact ac_simps)+ |
65268 | 3530 |
|
37660 | 3531 |
lemma word_bw_comms: |
3532 |
"x AND y = y AND x" |
|
3533 |
"x OR y = y OR x" |
|
3534 |
"x XOR y = y XOR x" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3535 |
for x :: "'a::len word" |
72508 | 3536 |
by (fact ac_simps)+ |
65268 | 3537 |
|
37660 | 3538 |
lemma word_bw_lcs: |
3539 |
"y AND x AND z = x AND y AND z" |
|
3540 |
"y OR x OR z = x OR y OR z" |
|
3541 |
"y XOR x XOR z = x XOR y XOR z" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3542 |
for x :: "'a::len word" |
72508 | 3543 |
by (fact ac_simps)+ |
37660 | 3544 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3545 |
lemma word_log_esimps: |
37660 | 3546 |
"x AND 0 = 0" |
3547 |
"x AND -1 = x" |
|
3548 |
"x OR 0 = x" |
|
3549 |
"x OR -1 = -1" |
|
3550 |
"x XOR 0 = x" |
|
3551 |
"x XOR -1 = NOT x" |
|
3552 |
"0 AND x = 0" |
|
3553 |
"-1 AND x = x" |
|
3554 |
"0 OR x = x" |
|
3555 |
"-1 OR x = -1" |
|
3556 |
"0 XOR x = x" |
|
3557 |
"-1 XOR x = NOT x" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3558 |
for x :: "'a::len word" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3559 |
by simp_all |
37660 | 3560 |
|
3561 |
lemma word_not_dist: |
|
3562 |
"NOT (x OR y) = NOT x AND NOT y" |
|
3563 |
"NOT (x AND y) = NOT x OR NOT y" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3564 |
for x :: "'a::len word" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3565 |
by simp_all |
37660 | 3566 |
|
3567 |
lemma word_bw_same: |
|
3568 |
"x AND x = x" |
|
3569 |
"x OR x = x" |
|
3570 |
"x XOR x = 0" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3571 |
for x :: "'a::len word" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3572 |
by simp_all |
37660 | 3573 |
|
3574 |
lemma word_ao_absorbs [simp]: |
|
3575 |
"x AND (y OR x) = x" |
|
3576 |
"x OR y AND x = x" |
|
3577 |
"x AND (x OR y) = x" |
|
3578 |
"y AND x OR x = x" |
|
3579 |
"(y OR x) AND x = x" |
|
3580 |
"x OR x AND y = x" |
|
3581 |
"(x OR y) AND x = x" |
|
3582 |
"x AND y OR x = x" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3583 |
for x :: "'a::len word" |
72508 | 3584 |
by (auto intro: bit_eqI simp add: bit_and_iff bit_or_iff) |
37660 | 3585 |
|
71149 | 3586 |
lemma word_not_not [simp]: "NOT (NOT x) = x" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3587 |
for x :: "'a::len word" |
72508 | 3588 |
by (fact bit.double_compl) |
37660 | 3589 |
|
65328 | 3590 |
lemma word_ao_dist: "(x OR y) AND z = x AND z OR y AND z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3591 |
for x :: "'a::len word" |
72508 | 3592 |
by (fact bit.conj_disj_distrib2) |
37660 | 3593 |
|
65328 | 3594 |
lemma word_oa_dist: "x AND y OR z = (x OR z) AND (y OR z)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3595 |
for x :: "'a::len word" |
72508 | 3596 |
by (fact bit.disj_conj_distrib2) |
3597 |
||
65328 | 3598 |
lemma word_add_not [simp]: "x + NOT x = -1" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3599 |
for x :: "'a::len word" |
72508 | 3600 |
by (simp add: not_eq_complement) |
3601 |
||
65328 | 3602 |
lemma word_plus_and_or [simp]: "(x AND y) + (x OR y) = x + y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3603 |
for x :: "'a::len word" |
47372 | 3604 |
by transfer (simp add: plus_and_or) |
37660 | 3605 |
|
65328 | 3606 |
lemma leoa: "w = x OR y \<Longrightarrow> y = w AND y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3607 |
for x :: "'a::len word" |
65328 | 3608 |
by auto |
3609 |
||
3610 |
lemma leao: "w' = x' AND y' \<Longrightarrow> x' = x' OR w'" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3611 |
for x' :: "'a::len word" |
65328 | 3612 |
by auto |
3613 |
||
3614 |
lemma word_ao_equiv: "w = w OR w' \<longleftrightarrow> w' = w AND w'" |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3615 |
for w w' :: "'a::len word" |
48196 | 3616 |
by (auto intro: leoa leao) |
37660 | 3617 |
|
65328 | 3618 |
lemma le_word_or2: "x \<le> x OR y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3619 |
for x y :: "'a::len word" |
72488 | 3620 |
by (simp add: or_greater_eq uint_or word_le_def) |
37660 | 3621 |
|
71997 | 3622 |
lemmas le_word_or1 = xtrans(3) [OF word_bw_comms (2) le_word_or2] |
3623 |
lemmas word_and_le1 = xtrans(3) [OF word_ao_absorbs (4) [symmetric] le_word_or2] |
|
3624 |
lemmas word_and_le2 = xtrans(3) [OF word_ao_absorbs (8) [symmetric] le_word_or2] |
|
37660 | 3625 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3626 |
lemma bit_horner_sum_bit_word_iff [bit_simps]: |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3627 |
\<open>bit (horner_sum of_bool (2 :: 'a::len word) bs) n |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3628 |
\<longleftrightarrow> n < min LENGTH('a) (length bs) \<and> bs ! n\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3629 |
by transfer (simp add: bit_horner_sum_bit_iff) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3630 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3631 |
definition word_reverse :: \<open>'a::len word \<Rightarrow> 'a word\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3632 |
where \<open>word_reverse w = horner_sum of_bool 2 (rev (map (bit w) [0..<LENGTH('a)]))\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3633 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3634 |
lemma bit_word_reverse_iff [bit_simps]: |
71990 | 3635 |
\<open>bit (word_reverse w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w (LENGTH('a) - Suc n)\<close> |
3636 |
for w :: \<open>'a::len word\<close> |
|
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3637 |
by (cases \<open>n < LENGTH('a)\<close>) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3638 |
(simp_all add: word_reverse_def bit_horner_sum_bit_word_iff rev_nth) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3639 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3640 |
lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w" |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3641 |
by (rule bit_word_eqI) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3642 |
(auto simp add: bit_word_reverse_iff bit_imp_le_length Suc_diff_Suc) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3643 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3644 |
lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w" |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3645 |
by (metis word_rev_rev) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3646 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3647 |
lemma word_rev_gal': "u = word_reverse w \<Longrightarrow> w = word_reverse u" |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3648 |
by simp |
37660 | 3649 |
|
65328 | 3650 |
lemma uint_2p: "(0::'a::len word) < 2 ^ n \<Longrightarrow> uint (2 ^ n::'a::len word) = 2 ^ n" |
72262 | 3651 |
apply (cases \<open>n < LENGTH('a)\<close>; transfer) |
3652 |
apply auto |
|
37660 | 3653 |
done |
3654 |
||
65268 | 3655 |
lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a::len word) = 2 ^ n" |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
3656 |
by (induct n) (simp_all add: wi_hom_syms) |
37660 | 3657 |
|
3658 |
||
61799 | 3659 |
subsection \<open>Shifting, Rotating, and Splitting Words\<close> |
37660 | 3660 |
|
71986 | 3661 |
lemma shiftl1_wi [simp]: "shiftl1 (word_of_int w) = word_of_int (2 * w)" |
72130
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
3662 |
by transfer simp |
37660 | 3663 |
|
65328 | 3664 |
lemma shiftl1_numeral [simp]: "shiftl1 (numeral w) = numeral (Num.Bit0 w)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3665 |
unfolding word_numeral_alt shiftl1_wi by simp |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3666 |
|
65328 | 3667 |
lemma shiftl1_neg_numeral [simp]: "shiftl1 (- numeral w) = - numeral (Num.Bit0 w)" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3668 |
unfolding word_neg_numeral_alt shiftl1_wi by simp |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3669 |
|
37660 | 3670 |
lemma shiftl1_0 [simp] : "shiftl1 0 = 0" |
72079 | 3671 |
by transfer simp |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3672 |
|
71986 | 3673 |
lemma shiftl1_def_u: "shiftl1 w = word_of_int (2 * uint w)" |
72079 | 3674 |
by (fact shiftl1_eq) |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3675 |
|
71986 | 3676 |
lemma shiftl1_def_s: "shiftl1 w = word_of_int (2 * sint w)" |
72079 | 3677 |
by (simp add: shiftl1_def_u wi_hom_syms) |
37660 | 3678 |
|
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
3679 |
lemma shiftr1_0 [simp]: "shiftr1 0 = 0" |
72079 | 3680 |
by transfer simp |
45995
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
3681 |
|
b16070689726
declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents:
45958
diff
changeset
|
3682 |
lemma sshiftr1_0 [simp]: "sshiftr1 0 = 0" |
72079 | 3683 |
by transfer simp |
65328 | 3684 |
|
3685 |
lemma sshiftr1_n1 [simp]: "sshiftr1 (- 1) = - 1" |
|
72079 | 3686 |
by transfer simp |
65328 | 3687 |
|
67408 | 3688 |
text \<open> |
3689 |
see paper page 10, (1), (2), \<open>shiftr1_def\<close> is of the form of (1), |
|
72488 | 3690 |
where \<open>f\<close> (ie \<open>_ div 2\<close>) takes normal arguments to normal results, |
67408 | 3691 |
thus we get (2) from (1) |
3692 |
\<close> |
|
37660 | 3693 |
|
72488 | 3694 |
lemma uint_shiftr1: "uint (shiftr1 w) = uint w div 2" |
72508 | 3695 |
using drop_bit_eq_div [of 1 \<open>uint w\<close>, symmetric] |
3696 |
apply simp |
|
3697 |
apply transfer |
|
3698 |
apply (simp add: drop_bit_take_bit min_def) |
|
3699 |
done |
|
37660 | 3700 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3701 |
lemma bit_sshiftr1_iff [bit_simps]: |
71990 | 3702 |
\<open>bit (sshiftr1 w) n \<longleftrightarrow> bit w (if n = LENGTH('a) - 1 then LENGTH('a) - 1 else Suc n)\<close> |
3703 |
for w :: \<open>'a::len word\<close> |
|
72079 | 3704 |
apply transfer |
3705 |
apply (auto simp add: bit_take_bit_iff bit_signed_take_bit_iff min_def simp flip: bit_Suc) |
|
3706 |
using le_less_Suc_eq apply fastforce |
|
3707 |
using le_less_Suc_eq apply fastforce |
|
71990 | 3708 |
done |
3709 |
||
37660 | 3710 |
lemma shiftr1_div_2: "uint (shiftr1 w) = uint w div 2" |
72079 | 3711 |
by (fact uint_shiftr1) |
37660 | 3712 |
|
3713 |
lemma sshiftr1_div_2: "sint (sshiftr1 w) = sint w div 2" |
|
72488 | 3714 |
using sint_signed_drop_bit_eq [of 1 w] |
3715 |
by (simp add: drop_bit_Suc sshiftr1_eq_signed_drop_bit_Suc_0) |
|
37660 | 3716 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3717 |
lemma bit_bshiftr1_iff [bit_simps]: |
71990 | 3718 |
\<open>bit (bshiftr1 b w) n \<longleftrightarrow> b \<and> n = LENGTH('a) - 1 \<or> bit w (Suc n)\<close> |
3719 |
for w :: \<open>'a::len word\<close> |
|
72079 | 3720 |
apply transfer |
3721 |
apply (simp add: bit_take_bit_iff flip: bit_Suc) |
|
3722 |
apply (subst disjunctive_add) |
|
3723 |
apply (auto simp add: bit_take_bit_iff bit_or_iff bit_exp_iff simp flip: bit_Suc) |
|
71990 | 3724 |
done |
3725 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
3726 |
|
61799 | 3727 |
subsubsection \<open>shift functions in terms of lists of bools\<close> |
37660 | 3728 |
|
65328 | 3729 |
lemma shiftl1_rev: "shiftl1 w = word_reverse (shiftr1 (word_reverse w))" |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3730 |
apply (rule bit_word_eqI) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3731 |
apply (auto simp add: bit_shiftl1_iff bit_word_reverse_iff bit_shiftr1_iff Suc_diff_Suc) |
37660 | 3732 |
done |
3733 |
||
67408 | 3734 |
\<comment> \<open>note -- the following results use \<open>'a::len word < number_ring\<close>\<close> |
65268 | 3735 |
|
65328 | 3736 |
lemma shiftl1_2t: "shiftl1 w = 2 * w" |
3737 |
for w :: "'a::len word" |
|
72079 | 3738 |
by (simp add: shiftl1_eq wi_hom_mult [symmetric]) |
37660 | 3739 |
|
65328 | 3740 |
lemma shiftl1_p: "shiftl1 w = w + w" |
3741 |
for w :: "'a::len word" |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
3742 |
by (simp add: shiftl1_2t) |
37660 | 3743 |
|
3744 |
lemma shiftr1_bintr [simp]: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
3745 |
"(shiftr1 (numeral w) :: 'a::len word) = |
72488 | 3746 |
word_of_int (take_bit LENGTH('a) (numeral w) div 2)" |
72262 | 3747 |
by transfer simp |
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
3748 |
|
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
3749 |
lemma sshiftr1_sbintr [simp]: |
65268 | 3750 |
"(sshiftr1 (numeral w) :: 'a::len word) = |
72488 | 3751 |
word_of_int (signed_take_bit (LENGTH('a) - 1) (numeral w) div 2)" |
72292 | 3752 |
by transfer simp |
37660 | 3753 |
|
71997 | 3754 |
text \<open>TODO: rules for \<^term>\<open>- (numeral n)\<close>\<close> |
3755 |
||
3756 |
lemma drop_bit_word_numeral [simp]: |
|
3757 |
\<open>drop_bit (numeral n) (numeral k) = |
|
3758 |
(word_of_int (drop_bit (numeral n) (take_bit LENGTH('a) (numeral k))) :: 'a::len word)\<close> |
|
3759 |
by transfer simp |
|
3760 |
||
65328 | 3761 |
lemma zip_replicate: "n \<ge> length ys \<Longrightarrow> zip (replicate n x) ys = map (\<lambda>y. (x, y)) ys" |
3762 |
apply (induct ys arbitrary: n) |
|
3763 |
apply simp_all |
|
3764 |
apply (case_tac n) |
|
3765 |
apply simp_all |
|
57492
74bf65a1910a
Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
56979
diff
changeset
|
3766 |
done |
74bf65a1910a
Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents:
56979
diff
changeset
|
3767 |
|
37660 | 3768 |
lemma align_lem_or [rule_format] : |
65328 | 3769 |
"\<forall>x m. length x = n + m \<longrightarrow> length y = n + m \<longrightarrow> |
3770 |
drop m x = replicate n False \<longrightarrow> take m y = replicate m False \<longrightarrow> |
|
67399 | 3771 |
map2 (|) x y = take m x @ drop m y" |
65328 | 3772 |
apply (induct y) |
37660 | 3773 |
apply force |
3774 |
apply clarsimp |
|
65328 | 3775 |
apply (case_tac x) |
3776 |
apply force |
|
3777 |
apply (case_tac m) |
|
3778 |
apply auto |
|
59807 | 3779 |
apply (drule_tac t="length xs" for xs in sym) |
70193 | 3780 |
apply (auto simp: zip_replicate o_def) |
37660 | 3781 |
done |
3782 |
||
3783 |
lemma align_lem_and [rule_format] : |
|
65328 | 3784 |
"\<forall>x m. length x = n + m \<longrightarrow> length y = n + m \<longrightarrow> |
3785 |
drop m x = replicate n False \<longrightarrow> take m y = replicate m False \<longrightarrow> |
|
67399 | 3786 |
map2 (\<and>) x y = replicate (n + m) False" |
65328 | 3787 |
apply (induct y) |
37660 | 3788 |
apply force |
3789 |
apply clarsimp |
|
65328 | 3790 |
apply (case_tac x) |
3791 |
apply force |
|
3792 |
apply (case_tac m) |
|
3793 |
apply auto |
|
59807 | 3794 |
apply (drule_tac t="length xs" for xs in sym) |
70193 | 3795 |
apply (auto simp: zip_replicate o_def map_replicate_const) |
37660 | 3796 |
done |
3797 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
3798 |
|
61799 | 3799 |
subsubsection \<open>Mask\<close> |
37660 | 3800 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3801 |
lemma minus_1_eq_mask: |
72082 | 3802 |
\<open>- 1 = (mask LENGTH('a) :: 'a::len word)\<close> |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3803 |
by (rule bit_eqI) (simp add: bit_exp_iff bit_mask_iff exp_eq_zero_iff) |
72079 | 3804 |
|
3805 |
lemma mask_eq_decr_exp: |
|
72082 | 3806 |
\<open>mask n = 2 ^ n - (1 :: 'a::len word)\<close> |
3807 |
by (fact mask_eq_exp_minus_1) |
|
71953 | 3808 |
|
3809 |
lemma mask_Suc_rec: |
|
72082 | 3810 |
\<open>mask (Suc n) = 2 * mask n + (1 :: 'a::len word)\<close> |
3811 |
by (simp add: mask_eq_exp_minus_1) |
|
71953 | 3812 |
|
3813 |
context |
|
3814 |
begin |
|
3815 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3816 |
qualified lemma bit_mask_iff [bit_simps]: |
71990 | 3817 |
\<open>bit (mask m :: 'a::len word) n \<longleftrightarrow> n < min LENGTH('a) m\<close> |
72082 | 3818 |
by (simp add: bit_mask_iff exp_eq_zero_iff not_le) |
71953 | 3819 |
|
3820 |
end |
|
3821 |
||
72128 | 3822 |
lemma mask_bin: "mask n = word_of_int (take_bit n (- 1))" |
72488 | 3823 |
by transfer (simp add: take_bit_minus_one_eq_mask) |
37660 | 3824 |
|
72128 | 3825 |
lemma and_mask_bintr: "w AND mask n = word_of_int (take_bit n (uint w))" |
72488 | 3826 |
by transfer (simp add: ac_simps take_bit_eq_mask) |
37660 | 3827 |
|
72128 | 3828 |
lemma and_mask_wi: "word_of_int i AND mask n = word_of_int (take_bit n i)" |
72488 | 3829 |
by (auto simp add: and_mask_bintr min_def not_le wi_bintr) |
46023
fad87bb608fc
restate some lemmas to respect int/bin distinction
huffman
parents:
46022
diff
changeset
|
3830 |
|
65328 | 3831 |
lemma and_mask_wi': |
72128 | 3832 |
"word_of_int i AND mask n = (word_of_int (take_bit (min LENGTH('a) n) i) :: 'a::len word)" |
72488 | 3833 |
by (auto simp add: and_mask_wi min_def wi_bintr) |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
3834 |
|
72128 | 3835 |
lemma and_mask_no: "numeral i AND mask n = word_of_int (take_bit n (numeral i))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3836 |
unfolding word_numeral_alt by (rule and_mask_wi) |
37660 | 3837 |
|
45811 | 3838 |
lemma and_mask_mod_2p: "w AND mask n = word_of_int (uint w mod 2 ^ n)" |
72128 | 3839 |
by (simp only: and_mask_bintr take_bit_eq_mod) |
37660 | 3840 |
|
72130
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
3841 |
lemma uint_mask_eq: |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
3842 |
\<open>uint (mask n :: 'a::len word) = mask (min LENGTH('a) n)\<close> |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
3843 |
by transfer simp |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
3844 |
|
37660 | 3845 |
lemma and_mask_lt_2p: "uint (w AND mask n) < 2 ^ n" |
72488 | 3846 |
apply (simp flip: take_bit_eq_mask) |
3847 |
apply transfer |
|
3848 |
apply (auto simp add: min_def) |
|
3849 |
using antisym_conv take_bit_int_eq_self_iff by fastforce |
|
37660 | 3850 |
|
65363 | 3851 |
lemma mask_eq_iff: "w AND mask n = w \<longleftrightarrow> uint w < 2 ^ n" |
72292 | 3852 |
apply (auto simp flip: take_bit_eq_mask) |
3853 |
apply (metis take_bit_int_eq_self_iff uint_take_bit_eq) |
|
3854 |
apply (simp add: take_bit_int_eq_self unsigned_take_bit_eq word_uint_eqI) |
|
37660 | 3855 |
done |
3856 |
||
65328 | 3857 |
lemma and_mask_dvd: "2 ^ n dvd uint w \<longleftrightarrow> w AND mask n = 0" |
72262 | 3858 |
by (simp flip: take_bit_eq_mask take_bit_eq_mod unsigned_take_bit_eq add: dvd_eq_mod_eq_0 uint_0_iff) |
37660 | 3859 |
|
65328 | 3860 |
lemma and_mask_dvd_nat: "2 ^ n dvd unat w \<longleftrightarrow> w AND mask n = 0" |
72262 | 3861 |
by (simp flip: take_bit_eq_mask take_bit_eq_mod unsigned_take_bit_eq add: dvd_eq_mod_eq_0 unat_0_iff uint_0_iff) |
37660 | 3862 |
|
65328 | 3863 |
lemma word_2p_lem: "n < size w \<Longrightarrow> w < 2 ^ n = (uint w < 2 ^ n)" |
3864 |
for w :: "'a::len word" |
|
72262 | 3865 |
by transfer simp |
37660 | 3866 |
|
65328 | 3867 |
lemma less_mask_eq: "x < 2 ^ n \<Longrightarrow> x AND mask n = x" |
3868 |
for x :: "'a::len word" |
|
72488 | 3869 |
apply (cases \<open>n < LENGTH('a)\<close>) |
3870 |
apply (simp_all add: not_less flip: take_bit_eq_mask exp_eq_zero_iff) |
|
71997 | 3871 |
apply transfer |
72488 | 3872 |
apply (simp add: min_def) |
3873 |
apply (metis min_def nat_less_le take_bit_int_eq_self_iff take_bit_take_bit) |
|
37660 | 3874 |
done |
3875 |
||
45604 | 3876 |
lemmas mask_eq_iff_w2p = trans [OF mask_eq_iff word_2p_lem [symmetric]] |
3877 |
||
3878 |
lemmas and_mask_less' = iffD2 [OF word_2p_lem and_mask_lt_2p, simplified word_size] |
|
37660 | 3879 |
|
72082 | 3880 |
lemma and_mask_less_size: "n < size x \<Longrightarrow> x AND mask n < 2 ^ n" |
3881 |
for x :: \<open>'a::len word\<close> |
|
37660 | 3882 |
unfolding word_size by (erule and_mask_less') |
3883 |
||
65328 | 3884 |
lemma word_mod_2p_is_mask [OF refl]: "c = 2 ^ n \<Longrightarrow> c > 0 \<Longrightarrow> x mod c = x AND mask n" |
3885 |
for c x :: "'a::len word" |
|
3886 |
by (auto simp: word_mod_def uint_2p and_mask_mod_2p) |
|
37660 | 3887 |
|
3888 |
lemma mask_eqs: |
|
3889 |
"(a AND mask n) + b AND mask n = a + b AND mask n" |
|
3890 |
"a + (b AND mask n) AND mask n = a + b AND mask n" |
|
3891 |
"(a AND mask n) - b AND mask n = a - b AND mask n" |
|
3892 |
"a - (b AND mask n) AND mask n = a - b AND mask n" |
|
3893 |
"a * (b AND mask n) AND mask n = a * b AND mask n" |
|
3894 |
"(b AND mask n) * a AND mask n = b * a AND mask n" |
|
3895 |
"(a AND mask n) + (b AND mask n) AND mask n = a + b AND mask n" |
|
3896 |
"(a AND mask n) - (b AND mask n) AND mask n = a - b AND mask n" |
|
3897 |
"(a AND mask n) * (b AND mask n) AND mask n = a * b AND mask n" |
|
3898 |
"- (a AND mask n) AND mask n = - a AND mask n" |
|
3899 |
"word_succ (a AND mask n) AND mask n = word_succ a AND mask n" |
|
3900 |
"word_pred (a AND mask n) AND mask n = word_pred a AND mask n" |
|
3901 |
using word_of_int_Ex [where x=a] word_of_int_Ex [where x=b] |
|
72262 | 3902 |
apply (auto simp flip: take_bit_eq_mask) |
3903 |
apply transfer |
|
3904 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3905 |
apply transfer |
|
3906 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3907 |
apply transfer |
|
3908 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3909 |
apply transfer |
|
3910 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3911 |
apply transfer |
|
3912 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3913 |
apply transfer |
|
3914 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3915 |
apply transfer |
|
3916 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3917 |
apply transfer |
|
3918 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3919 |
apply transfer |
|
3920 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3921 |
apply transfer |
|
3922 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3923 |
apply transfer |
|
3924 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3925 |
apply transfer |
|
3926 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3927 |
done |
|
65328 | 3928 |
|
3929 |
lemma mask_power_eq: "(x AND mask n) ^ k AND mask n = x ^ k AND mask n" |
|
72082 | 3930 |
for x :: \<open>'a::len word\<close> |
37660 | 3931 |
using word_of_int_Ex [where x=x] |
72262 | 3932 |
apply (auto simp flip: take_bit_eq_mask) |
3933 |
apply transfer |
|
3934 |
apply (simp add: take_bit_eq_mod mod_simps) |
|
3935 |
done |
|
37660 | 3936 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3937 |
lemma mask_full [simp]: "mask LENGTH('a) = (- 1 :: 'a::len word)" |
72079 | 3938 |
by transfer (simp add: take_bit_minus_one_eq_mask) |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3939 |
|
37660 | 3940 |
|
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3941 |
subsubsection \<open>Slices\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3942 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3943 |
definition slice1 :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3944 |
where \<open>slice1 n w = (if n < LENGTH('a) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3945 |
then ucast (drop_bit (LENGTH('a) - n) w) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3946 |
else push_bit (n - LENGTH('a)) (ucast w))\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3947 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3948 |
lemma bit_slice1_iff [bit_simps]: |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3949 |
\<open>bit (slice1 m w :: 'b::len word) n \<longleftrightarrow> m - LENGTH('a) \<le> n \<and> n < min LENGTH('b) m |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3950 |
\<and> bit w (n + (LENGTH('a) - m) - (m - LENGTH('a)))\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3951 |
for w :: \<open>'a::len word\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3952 |
by (auto simp add: slice1_def bit_ucast_iff bit_drop_bit_eq bit_push_bit_iff exp_eq_zero_iff not_less not_le ac_simps |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3953 |
dest: bit_imp_le_length) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3954 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3955 |
definition slice :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3956 |
where \<open>slice n = slice1 (LENGTH('a) - n)\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3957 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3958 |
lemma bit_slice_iff [bit_simps]: |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3959 |
\<open>bit (slice m w :: 'b::len word) n \<longleftrightarrow> n < min LENGTH('b) (LENGTH('a) - m) \<and> bit w (n + LENGTH('a) - (LENGTH('a) - m))\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3960 |
for w :: \<open>'a::len word\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3961 |
by (simp add: slice_def word_size bit_slice1_iff) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3962 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3963 |
lemma slice1_0 [simp] : "slice1 n 0 = 0" |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3964 |
unfolding slice1_def by simp |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3965 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3966 |
lemma slice_0 [simp] : "slice n 0 = 0" |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3967 |
unfolding slice_def by auto |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3968 |
|
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3969 |
lemma ucast_slice1: "ucast w = slice1 (size w) w" |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3970 |
apply (simp add: slice1_def) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3971 |
apply transfer |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3972 |
apply simp |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3973 |
done |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3974 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3975 |
lemma ucast_slice: "ucast w = slice 0 w" |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3976 |
by (simp add: slice_def slice1_def) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3977 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3978 |
lemma slice_id: "slice 0 t = t" |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3979 |
by (simp only: ucast_slice [symmetric] ucast_id) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3980 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3981 |
lemma rev_slice1: |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3982 |
\<open>slice1 n (word_reverse w :: 'b::len word) = word_reverse (slice1 k w :: 'a::len word)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3983 |
if \<open>n + k = LENGTH('a) + LENGTH('b)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3984 |
proof (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3985 |
fix m |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3986 |
assume *: \<open>m < LENGTH('a)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3987 |
from that have **: \<open>LENGTH('b) = n + k - LENGTH('a)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3988 |
by simp |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3989 |
show \<open>bit (slice1 n (word_reverse w :: 'b word) :: 'a word) m \<longleftrightarrow> bit (word_reverse (slice1 k w :: 'a word)) m\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3990 |
apply (simp add: bit_slice1_iff bit_word_reverse_iff) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3991 |
using * ** |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3992 |
apply (cases \<open>n \<le> LENGTH('a)\<close>; cases \<open>k \<le> LENGTH('a)\<close>) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3993 |
apply auto |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3994 |
done |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3995 |
qed |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3996 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3997 |
lemma rev_slice: |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3998 |
"n + k + LENGTH('a::len) = LENGTH('b::len) \<Longrightarrow> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3999 |
slice n (word_reverse (w::'b word)) = word_reverse (slice k w :: 'a word)" |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4000 |
apply (unfold slice_def word_size) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4001 |
apply (rule rev_slice1) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4002 |
apply arith |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4003 |
done |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4004 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4005 |
|
61799 | 4006 |
subsubsection \<open>Revcast\<close> |
37660 | 4007 |
|
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4008 |
definition revcast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4009 |
where \<open>revcast = slice1 LENGTH('b)\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4010 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
4011 |
lemma bit_revcast_iff [bit_simps]: |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4012 |
\<open>bit (revcast w :: 'b::len word) n \<longleftrightarrow> LENGTH('b) - LENGTH('a) \<le> n \<and> n < LENGTH('b) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4013 |
\<and> bit w (n + (LENGTH('a) - LENGTH('b)) - (LENGTH('b) - LENGTH('a)))\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4014 |
for w :: \<open>'a::len word\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4015 |
by (simp add: revcast_def bit_slice1_iff) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4016 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4017 |
lemma revcast_slice1 [OF refl]: "rc = revcast w \<Longrightarrow> slice1 (size rc) w = rc" |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4018 |
by (simp add: revcast_def word_size) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4019 |
|
65268 | 4020 |
lemma revcast_rev_ucast [OF refl refl refl]: |
4021 |
"cs = [rc, uc] \<Longrightarrow> rc = revcast (word_reverse w) \<Longrightarrow> uc = ucast w \<Longrightarrow> |
|
37660 | 4022 |
rc = word_reverse uc" |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4023 |
apply auto |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4024 |
apply (rule bit_word_eqI) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4025 |
apply (cases \<open>LENGTH('a) \<le> LENGTH('b)\<close>) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4026 |
apply (simp_all add: bit_revcast_iff bit_word_reverse_iff bit_ucast_iff not_le |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4027 |
bit_imp_le_length) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4028 |
using bit_imp_le_length apply fastforce |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
4029 |
using bit_imp_le_length apply fastforce |
37660 | 4030 |
done |
4031 |
||
45811 | 4032 |
lemma revcast_ucast: "revcast w = word_reverse (ucast (word_reverse w))" |
4033 |
using revcast_rev_ucast [of "word_reverse w"] by simp |
|
4034 |
||
4035 |
lemma ucast_revcast: "ucast w = word_reverse (revcast (word_reverse w))" |
|
4036 |
by (fact revcast_rev_ucast [THEN word_rev_gal']) |
|
4037 |
||
4038 |
lemma ucast_rev_revcast: "ucast (word_reverse w) = word_reverse (revcast w)" |
|
4039 |
by (fact revcast_ucast [THEN word_rev_gal']) |
|
37660 | 4040 |
|
4041 |
||
65328 | 4042 |
text "linking revcast and cast via shift" |
37660 | 4043 |
|
4044 |
lemmas wsst_TYs = source_size target_size word_size |
|
4045 |
||
65268 | 4046 |
lemmas sym_notr = |
37660 | 4047 |
not_iff [THEN iffD2, THEN not_sym, THEN not_iff [THEN iffD1]] |
4048 |
||
4049 |
||
61799 | 4050 |
subsection \<open>Split and cat\<close> |
37660 | 4051 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4052 |
lemmas word_split_bin' = word_split_def |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4053 |
lemmas word_cat_bin' = word_cat_eq |
37660 | 4054 |
|
65268 | 4055 |
\<comment> \<open>this odd result is analogous to \<open>ucast_id\<close>, |
61799 | 4056 |
result to the length given by the result type\<close> |
37660 | 4057 |
|
4058 |
lemma word_cat_id: "word_cat a b = b" |
|
72488 | 4059 |
by transfer (simp add: take_bit_concat_bit_eq) |
65336 | 4060 |
|
4061 |
lemma word_cat_split_alt: "size w \<le> size u + size v \<Longrightarrow> word_split w = (u, v) \<Longrightarrow> word_cat u v = w" |
|
72508 | 4062 |
apply (rule bit_word_eqI) |
4063 |
apply (auto simp add: bit_word_cat_iff not_less word_size word_split_def bit_ucast_iff bit_drop_bit_eq) |
|
37660 | 4064 |
done |
4065 |
||
45604 | 4066 |
lemmas word_cat_split_size = sym [THEN [2] word_cat_split_alt [symmetric]] |
37660 | 4067 |
|
4068 |
||
61799 | 4069 |
subsubsection \<open>Split and slice\<close> |
37660 | 4070 |
|
65336 | 4071 |
lemma split_slices: "word_split w = (u, v) \<Longrightarrow> u = slice (size v) w \<and> v = slice 0 w" |
72508 | 4072 |
apply (auto simp add: word_split_def word_size) |
4073 |
apply (rule bit_word_eqI) |
|
4074 |
apply (simp add: bit_slice_iff bit_ucast_iff bit_drop_bit_eq) |
|
4075 |
apply (cases \<open>LENGTH('c) \<ge> LENGTH('b)\<close>) |
|
4076 |
apply (auto simp add: ac_simps dest: bit_imp_le_length) |
|
4077 |
apply (rule bit_word_eqI) |
|
4078 |
apply (auto simp add: bit_slice_iff bit_ucast_iff dest: bit_imp_le_length) |
|
37660 | 4079 |
done |
4080 |
||
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
4081 |
lemma slice_cat1 [OF refl]: |
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4082 |
"wc = word_cat a b \<Longrightarrow> size wc >= size a + size b \<Longrightarrow> slice (size b) wc = a" |
72508 | 4083 |
apply (rule bit_word_eqI) |
4084 |
apply (auto simp add: bit_slice_iff bit_word_cat_iff word_size) |
|
37660 | 4085 |
done |
4086 |
||
4087 |
lemmas slice_cat2 = trans [OF slice_id word_cat_id] |
|
4088 |
||
4089 |
lemma cat_slices: |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
4090 |
"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
|
4091 |
size a + size b >= size c \<Longrightarrow> word_cat a b = c" |
72508 | 4092 |
apply (rule bit_word_eqI) |
4093 |
apply (auto simp add: bit_slice_iff bit_word_cat_iff word_size) |
|
37660 | 4094 |
done |
4095 |
||
4096 |
lemma word_split_cat_alt: |
|
65336 | 4097 |
"w = word_cat u v \<Longrightarrow> size u + size v \<le> size w \<Longrightarrow> word_split w = (u, v)" |
72508 | 4098 |
apply (auto simp add: word_split_def word_size) |
4099 |
apply (rule bit_eqI) |
|
4100 |
apply (auto simp add: bit_ucast_iff bit_drop_bit_eq bit_word_cat_iff dest: bit_imp_le_length) |
|
4101 |
apply (rule bit_eqI) |
|
4102 |
apply (auto simp add: bit_ucast_iff bit_drop_bit_eq bit_word_cat_iff dest: bit_imp_le_length) |
|
37660 | 4103 |
done |
4104 |
||
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4105 |
lemma horner_sum_uint_exp_Cons_eq: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4106 |
\<open>horner_sum uint (2 ^ LENGTH('a)) (w # ws) = |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4107 |
concat_bit LENGTH('a) (uint w) (horner_sum uint (2 ^ LENGTH('a)) ws)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4108 |
for ws :: \<open>'a::len word list\<close> |
72292 | 4109 |
apply (simp add: concat_bit_eq push_bit_eq_mult) |
4110 |
apply transfer |
|
4111 |
apply simp |
|
4112 |
done |
|
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4113 |
|
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4114 |
lemma bit_horner_sum_uint_exp_iff: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4115 |
\<open>bit (horner_sum uint (2 ^ LENGTH('a)) ws) n \<longleftrightarrow> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4116 |
n div LENGTH('a) < length ws \<and> bit (ws ! (n div LENGTH('a))) (n mod LENGTH('a))\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4117 |
for ws :: \<open>'a::len word list\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4118 |
proof (induction ws arbitrary: n) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4119 |
case Nil |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4120 |
then show ?case |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4121 |
by simp |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4122 |
next |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4123 |
case (Cons w ws) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4124 |
then show ?case |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4125 |
by (cases \<open>n \<ge> LENGTH('a)\<close>) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4126 |
(simp_all only: horner_sum_uint_exp_Cons_eq, simp_all add: bit_concat_bit_iff le_div_geq le_mod_geq bit_uint_iff Cons) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4127 |
qed |
37660 | 4128 |
|
4129 |
||
61799 | 4130 |
subsection \<open>Rotation\<close> |
37660 | 4131 |
|
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4132 |
lemma word_rotr_word_rotr_eq: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4133 |
\<open>word_rotr m (word_rotr n w) = word_rotr (m + n) w\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4134 |
by (rule bit_word_eqI) (simp add: bit_word_rotr_iff ac_simps mod_add_right_eq) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4135 |
|
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4136 |
lemma word_rot_rl [simp]: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4137 |
\<open>word_rotl k (word_rotr k v) = v\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4138 |
apply (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4139 |
apply (simp add: word_rotl_eq_word_rotr word_rotr_word_rotr_eq bit_word_rotr_iff algebra_simps) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4140 |
apply (auto dest: bit_imp_le_length) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4141 |
apply (metis (no_types, lifting) add.right_neutral add_diff_cancel_right' div_mult_mod_eq mod_add_right_eq mod_add_self2 mod_if mod_mult_self2_is_0) |
72239 | 4142 |
apply (metis (no_types, lifting) add.right_neutral add_diff_cancel_right' div_mult_mod_eq mod_add_right_eq mod_add_self2 mod_less mod_mult_self2_is_0) |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4143 |
done |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4144 |
|
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4145 |
lemma word_rot_lr [simp]: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4146 |
\<open>word_rotr k (word_rotl k v) = v\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4147 |
apply (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4148 |
apply (simp add: word_rotl_eq_word_rotr word_rotr_word_rotr_eq bit_word_rotr_iff algebra_simps) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4149 |
apply (auto dest: bit_imp_le_length) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4150 |
apply (metis (no_types, lifting) add.right_neutral add_diff_cancel_right' div_mult_mod_eq mod_add_right_eq mod_add_self2 mod_if mod_mult_self2_is_0) |
72239 | 4151 |
apply (metis (no_types, lifting) add.right_neutral add_diff_cancel_right' div_mult_mod_eq mod_add_right_eq mod_add_self2 mod_less mod_mult_self2_is_0) |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4152 |
done |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4153 |
|
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4154 |
lemma word_rot_gal: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4155 |
\<open>word_rotr n v = w \<longleftrightarrow> word_rotl n w = v\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4156 |
by auto |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4157 |
|
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4158 |
lemma word_rot_gal': |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4159 |
\<open>w = word_rotr n v \<longleftrightarrow> v = word_rotl n w\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4160 |
by auto |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4161 |
|
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4162 |
lemma word_rotr_rev: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4163 |
\<open>word_rotr n w = word_reverse (word_rotl n (word_reverse w))\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4164 |
proof (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4165 |
fix m |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4166 |
assume \<open>m < LENGTH('a)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4167 |
moreover have \<open>1 + |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4168 |
((int m + int n mod int LENGTH('a)) mod int LENGTH('a) + |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4169 |
((int LENGTH('a) * 2) mod int LENGTH('a) - (1 + (int m + int n mod int LENGTH('a)))) mod int LENGTH('a)) = |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4170 |
int LENGTH('a)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4171 |
apply (cases \<open>(1 + (int m + int n mod int LENGTH('a))) mod |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4172 |
int LENGTH('a) = 0\<close>) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4173 |
using zmod_zminus1_eq_if [of \<open>1 + (int m + int n mod int LENGTH('a))\<close> \<open>int LENGTH('a)\<close>] |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4174 |
apply simp_all |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4175 |
apply (auto simp add: algebra_simps) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4176 |
apply (simp add: minus_equation_iff [of \<open>int m\<close>]) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4177 |
apply (drule sym [of _ \<open>int m\<close>]) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4178 |
apply simp |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4179 |
apply (metis add.commute add_minus_cancel diff_minus_eq_add len_gt_0 less_imp_of_nat_less less_nat_zero_code mod_mult_self3 of_nat_gt_0 zmod_minus1) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4180 |
apply (metis (no_types, hide_lams) Abs_fnat_hom_add less_not_refl mod_Suc of_nat_Suc of_nat_gt_0 of_nat_mod) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4181 |
done |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4182 |
then have \<open>int ((m + n) mod LENGTH('a)) = |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4183 |
int (LENGTH('a) - Suc ((LENGTH('a) - Suc m + LENGTH('a) - n mod LENGTH('a)) mod LENGTH('a)))\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4184 |
using \<open>m < LENGTH('a)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4185 |
by (simp only: of_nat_mod mod_simps) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4186 |
(simp add: of_nat_diff of_nat_mod Suc_le_eq add_less_mono algebra_simps mod_simps) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4187 |
then have \<open>(m + n) mod LENGTH('a) = |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4188 |
LENGTH('a) - Suc ((LENGTH('a) - Suc m + LENGTH('a) - n mod LENGTH('a)) mod LENGTH('a))\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4189 |
by simp |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4190 |
ultimately show \<open>bit (word_rotr n w) m \<longleftrightarrow> bit (word_reverse (word_rotl n (word_reverse w))) m\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4191 |
by (simp add: word_rotl_eq_word_rotr bit_word_rotr_iff bit_word_reverse_iff) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4192 |
qed |
65268 | 4193 |
|
37660 | 4194 |
lemma word_roti_0 [simp]: "word_roti 0 w = w" |
72079 | 4195 |
by transfer simp |
37660 | 4196 |
|
65336 | 4197 |
lemma word_roti_add: "word_roti (m + n) w = word_roti m (word_roti n w)" |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4198 |
by (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4199 |
(simp add: bit_word_roti_iff nat_less_iff mod_simps ac_simps) |
65268 | 4200 |
|
67118 | 4201 |
lemma word_roti_conv_mod': |
4202 |
"word_roti n w = word_roti (n mod int (size w)) w" |
|
72079 | 4203 |
by transfer simp |
37660 | 4204 |
|
4205 |
lemmas word_roti_conv_mod = word_roti_conv_mod' [unfolded word_size] |
|
4206 |
||
4207 |
||
61799 | 4208 |
subsubsection \<open>"Word rotation commutes with bit-wise operations\<close> |
37660 | 4209 |
|
67408 | 4210 |
\<comment> \<open>using locale to not pollute lemma namespace\<close> |
65268 | 4211 |
locale word_rotate |
37660 | 4212 |
begin |
4213 |
||
4214 |
lemma word_rot_logs: |
|
71149 | 4215 |
"word_rotl n (NOT v) = NOT (word_rotl n v)" |
4216 |
"word_rotr n (NOT v) = NOT (word_rotr n v)" |
|
37660 | 4217 |
"word_rotl n (x AND y) = word_rotl n x AND word_rotl n y" |
4218 |
"word_rotr n (x AND y) = word_rotr n x AND word_rotr n y" |
|
4219 |
"word_rotl n (x OR y) = word_rotl n x OR word_rotl n y" |
|
4220 |
"word_rotr n (x OR y) = word_rotr n x OR word_rotr n y" |
|
4221 |
"word_rotl n (x XOR y) = word_rotl n x XOR word_rotl n y" |
|
65268 | 4222 |
"word_rotr n (x XOR y) = word_rotr n x XOR word_rotr n y" |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4223 |
apply (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4224 |
apply (auto simp add: bit_word_rotl_iff bit_not_iff algebra_simps exp_eq_zero_iff not_le) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4225 |
apply (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4226 |
apply (auto simp add: bit_word_rotr_iff bit_not_iff algebra_simps exp_eq_zero_iff not_le) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4227 |
apply (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4228 |
apply (auto simp add: bit_word_rotl_iff bit_and_iff algebra_simps exp_eq_zero_iff not_le) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4229 |
apply (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4230 |
apply (auto simp add: bit_word_rotr_iff bit_and_iff algebra_simps exp_eq_zero_iff not_le) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4231 |
apply (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4232 |
apply (auto simp add: bit_word_rotl_iff bit_or_iff algebra_simps exp_eq_zero_iff not_le) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4233 |
apply (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4234 |
apply (auto simp add: bit_word_rotr_iff bit_or_iff algebra_simps exp_eq_zero_iff not_le) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4235 |
apply (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4236 |
apply (auto simp add: bit_word_rotl_iff bit_xor_iff algebra_simps exp_eq_zero_iff not_le) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4237 |
apply (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4238 |
apply (auto simp add: bit_word_rotr_iff bit_xor_iff algebra_simps exp_eq_zero_iff not_le) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4239 |
done |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4240 |
|
37660 | 4241 |
end |
4242 |
||
4243 |
lemmas word_rot_logs = word_rotate.word_rot_logs |
|
4244 |
||
65336 | 4245 |
lemma word_rotx_0 [simp] : "word_rotr i 0 = 0 \<and> word_rotl i 0 = 0" |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
4246 |
by transfer simp_all |
37660 | 4247 |
|
4248 |
lemma word_roti_0' [simp] : "word_roti n 0 = 0" |
|
72079 | 4249 |
by transfer simp |
37660 | 4250 |
|
72079 | 4251 |
declare word_roti_eq_word_rotr_word_rotl [simp] |
37660 | 4252 |
|
4253 |
||
61799 | 4254 |
subsection \<open>Maximum machine word\<close> |
37660 | 4255 |
|
4256 |
lemma word_int_cases: |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4257 |
fixes x :: "'a::len word" |
70185 | 4258 |
obtains n where "x = word_of_int n" and "0 \<le> n" and "n < 2^LENGTH('a)" |
72292 | 4259 |
by (rule that [of \<open>uint x\<close>]) simp_all |
37660 | 4260 |
|
4261 |
lemma word_nat_cases [cases type: word]: |
|
65336 | 4262 |
fixes x :: "'a::len word" |
70185 | 4263 |
obtains n where "x = of_nat n" and "n < 2^LENGTH('a)" |
72292 | 4264 |
by (rule that [of \<open>unat x\<close>]) simp_all |
37660 | 4265 |
|
71946 | 4266 |
lemma max_word_max [intro!]: "n \<le> max_word" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
4267 |
by (fact word_order.extremum) |
65268 | 4268 |
|
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4269 |
lemma word_of_int_2p_len: "word_of_int (2 ^ LENGTH('a)) = (0::'a::len word)" |
72292 | 4270 |
by simp |
37660 | 4271 |
|
70185 | 4272 |
lemma word_pow_0: "(2::'a::len word) ^ LENGTH('a) = 0" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
4273 |
by (fact word_exp_length_eq_0) |
37660 | 4274 |
|
4275 |
lemma max_word_wrap: "x + 1 = 0 \<Longrightarrow> x = max_word" |
|
71946 | 4276 |
by (simp add: eq_neg_iff_add_eq_0) |
4277 |
||
4278 |
lemma word_and_max: "x AND max_word = x" |
|
4279 |
by (fact word_log_esimps) |
|
4280 |
||
4281 |
lemma word_or_max: "x OR max_word = max_word" |
|
4282 |
by (fact word_log_esimps) |
|
37660 | 4283 |
|
65336 | 4284 |
lemma word_ao_dist2: "x AND (y OR z) = x AND y OR x AND z" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4285 |
for x y z :: "'a::len word" |
72508 | 4286 |
by (fact bit.conj_disj_distrib) |
37660 | 4287 |
|
65336 | 4288 |
lemma word_oa_dist2: "x OR y AND z = (x OR y) AND (x OR z)" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4289 |
for x y z :: "'a::len word" |
72508 | 4290 |
by (fact bit.disj_conj_distrib) |
37660 | 4291 |
|
65336 | 4292 |
lemma word_and_not [simp]: "x AND NOT x = 0" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4293 |
for x :: "'a::len word" |
72508 | 4294 |
by (fact bit.conj_cancel_right) |
37660 | 4295 |
|
65336 | 4296 |
lemma word_or_not [simp]: "x OR NOT x = max_word" |
72508 | 4297 |
by (fact bit.disj_cancel_right) |
37660 | 4298 |
|
65336 | 4299 |
lemma word_xor_and_or: "x XOR y = x AND NOT y OR NOT x AND y" |
71954
13bb3f5cdc5b
pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents:
71953
diff
changeset
|
4300 |
for x y :: "'a::len word" |
72508 | 4301 |
by (fact bit.xor_def) |
37660 | 4302 |
|
65336 | 4303 |
lemma uint_lt_0 [simp]: "uint x < 0 = False" |
37660 | 4304 |
by (simp add: linorder_not_less) |
4305 |
||
65336 | 4306 |
lemma shiftr1_1 [simp]: "shiftr1 (1::'a::len word) = 0" |
72079 | 4307 |
by transfer simp |
37660 | 4308 |
|
65336 | 4309 |
lemma word_less_1 [simp]: "x < 1 \<longleftrightarrow> x = 0" |
4310 |
for x :: "'a::len word" |
|
37660 | 4311 |
by (simp add: word_less_nat_alt unat_0_iff) |
4312 |
||
4313 |
lemma uint_plus_if_size: |
|
65268 | 4314 |
"uint (x + y) = |
65336 | 4315 |
(if uint x + uint y < 2^size x |
4316 |
then uint x + uint y |
|
4317 |
else uint x + uint y - 2^size x)" |
|
72262 | 4318 |
apply (simp only: word_arith_wis word_size uint_word_of_int_eq) |
4319 |
apply (auto simp add: not_less take_bit_int_eq_self_iff) |
|
72292 | 4320 |
apply (metis not_less take_bit_eq_mod uint_plus_if' uint_word_ariths(1)) |
72262 | 4321 |
done |
37660 | 4322 |
|
4323 |
lemma unat_plus_if_size: |
|
65363 | 4324 |
"unat (x + y) = |
65336 | 4325 |
(if unat x + unat y < 2^size x |
4326 |
then unat x + unat y |
|
4327 |
else unat x + unat y - 2^size x)" |
|
65363 | 4328 |
for x y :: "'a::len word" |
37660 | 4329 |
apply (subst word_arith_nat_defs) |
4330 |
apply (subst unat_of_nat) |
|
71997 | 4331 |
apply (auto simp add: not_less word_size) |
4332 |
apply (metis not_le unat_plus_if' unat_word_ariths(1)) |
|
37660 | 4333 |
done |
4334 |
||
65336 | 4335 |
lemma word_neq_0_conv: "w \<noteq> 0 \<longleftrightarrow> 0 < w" |
4336 |
for w :: "'a::len word" |
|
72262 | 4337 |
by (fact word_coorder.not_eq_extremum) |
65336 | 4338 |
|
4339 |
lemma max_lt: "unat (max a b div c) = unat (max a b) div unat c" |
|
4340 |
for c :: "'a::len word" |
|
55818 | 4341 |
by (fact unat_div) |
37660 | 4342 |
|
4343 |
lemma uint_sub_if_size: |
|
65268 | 4344 |
"uint (x - y) = |
65336 | 4345 |
(if uint y \<le> uint x |
4346 |
then uint x - uint y |
|
4347 |
else uint x - uint y + 2^size x)" |
|
72262 | 4348 |
apply (simp only: word_arith_wis word_size uint_word_of_int_eq) |
4349 |
apply (auto simp add: take_bit_int_eq_self_iff not_le) |
|
72292 | 4350 |
apply (metis not_less uint_sub_if' uint_word_arith_bintrs(2)) |
72262 | 4351 |
done |
65336 | 4352 |
|
72130
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4353 |
lemma unat_sub: |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4354 |
\<open>unat (a - b) = unat a - unat b\<close> |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4355 |
if \<open>b \<le> a\<close> |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4356 |
proof - |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4357 |
from that have \<open>unat b \<le> unat a\<close> |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4358 |
by transfer simp |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4359 |
with that show ?thesis |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4360 |
apply transfer |
72488 | 4361 |
apply simp |
72130
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4362 |
apply (subst take_bit_diff [symmetric]) |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4363 |
apply (subst nat_take_bit_eq) |
72488 | 4364 |
apply (simp add: nat_le_eq_zle) |
4365 |
apply (simp add: nat_diff_distrib take_bit_nat_eq_self_iff less_imp_diff_less) |
|
72130
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4366 |
done |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4367 |
qed |
37660 | 4368 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4369 |
lemmas word_less_sub1_numberof [simp] = word_less_sub1 [of "numeral w"] for w |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4370 |
lemmas word_le_sub1_numberof [simp] = word_le_sub1 [of "numeral w"] for w |
65268 | 4371 |
|
70185 | 4372 |
lemma word_of_int_minus: "word_of_int (2^LENGTH('a) - i) = (word_of_int (-i)::'a::len word)" |
72292 | 4373 |
apply transfer |
4374 |
apply (subst take_bit_diff [symmetric]) |
|
4375 |
apply (simp add: take_bit_minus) |
|
4376 |
done |
|
4377 |
||
4378 |
lemma word_of_int_inj: |
|
4379 |
\<open>(word_of_int x :: 'a::len word) = word_of_int y \<longleftrightarrow> x = y\<close> |
|
4380 |
if \<open>0 \<le> x \<and> x < 2 ^ LENGTH('a)\<close> \<open>0 \<le> y \<and> y < 2 ^ LENGTH('a)\<close> |
|
4381 |
using that by (transfer fixing: x y) (simp add: take_bit_int_eq_self) |
|
37660 | 4382 |
|
65336 | 4383 |
lemma word_le_less_eq: "x \<le> y \<longleftrightarrow> x = y \<or> x < y" |
4384 |
for x y :: "'z::len word" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4385 |
by (auto simp add: order_class.le_less) |
37660 | 4386 |
|
4387 |
lemma mod_plus_cong: |
|
65336 | 4388 |
fixes b b' :: int |
4389 |
assumes 1: "b = b'" |
|
4390 |
and 2: "x mod b' = x' mod b'" |
|
4391 |
and 3: "y mod b' = y' mod b'" |
|
4392 |
and 4: "x' + y' = z'" |
|
37660 | 4393 |
shows "(x + y) mod b = z' mod b'" |
4394 |
proof - |
|
4395 |
from 1 2[symmetric] 3[symmetric] have "(x + y) mod b = (x' mod b' + y' mod b') mod b'" |
|
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
4396 |
by (simp add: mod_add_eq) |
37660 | 4397 |
also have "\<dots> = (x' + y') mod b'" |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
4398 |
by (simp add: mod_add_eq) |
65336 | 4399 |
finally show ?thesis |
4400 |
by (simp add: 4) |
|
37660 | 4401 |
qed |
4402 |
||
4403 |
lemma mod_minus_cong: |
|
65336 | 4404 |
fixes b b' :: int |
4405 |
assumes "b = b'" |
|
4406 |
and "x mod b' = x' mod b'" |
|
4407 |
and "y mod b' = y' mod b'" |
|
4408 |
and "x' - y' = z'" |
|
37660 | 4409 |
shows "(x - y) mod b = z' mod b'" |
65336 | 4410 |
using assms [symmetric] by (auto intro: mod_diff_cong) |
4411 |
||
72262 | 4412 |
lemma word_induct_less: |
4413 |
\<open>P m\<close> if zero: \<open>P 0\<close> and less: \<open>\<And>n. n < m \<Longrightarrow> P n \<Longrightarrow> P (1 + n)\<close> |
|
4414 |
for m :: \<open>'a::len word\<close> |
|
4415 |
proof - |
|
4416 |
define q where \<open>q = unat m\<close> |
|
4417 |
with less have \<open>\<And>n. n < word_of_nat q \<Longrightarrow> P n \<Longrightarrow> P (1 + n)\<close> |
|
4418 |
by simp |
|
4419 |
then have \<open>P (word_of_nat q :: 'a word)\<close> |
|
4420 |
proof (induction q) |
|
4421 |
case 0 |
|
4422 |
show ?case |
|
4423 |
by (simp add: zero) |
|
4424 |
next |
|
4425 |
case (Suc q) |
|
4426 |
show ?case |
|
4427 |
proof (cases \<open>1 + word_of_nat q = (0 :: 'a word)\<close>) |
|
4428 |
case True |
|
4429 |
then show ?thesis |
|
4430 |
by (simp add: zero) |
|
4431 |
next |
|
4432 |
case False |
|
4433 |
then have *: \<open>word_of_nat q < (word_of_nat (Suc q) :: 'a word)\<close> |
|
4434 |
by (simp add: unatSuc word_less_nat_alt) |
|
4435 |
then have **: \<open>n < (1 + word_of_nat q :: 'a word) \<longleftrightarrow> n \<le> (word_of_nat q :: 'a word)\<close> for n |
|
4436 |
by (metis (no_types, lifting) add.commute inc_le le_less_trans not_less of_nat_Suc) |
|
4437 |
have \<open>P (word_of_nat q)\<close> |
|
4438 |
apply (rule Suc.IH) |
|
4439 |
apply (rule Suc.prems) |
|
4440 |
apply (erule less_trans) |
|
4441 |
apply (rule *) |
|
4442 |
apply assumption |
|
4443 |
done |
|
4444 |
with * have \<open>P (1 + word_of_nat q)\<close> |
|
4445 |
by (rule Suc.prems) |
|
4446 |
then show ?thesis |
|
4447 |
by simp |
|
4448 |
qed |
|
4449 |
qed |
|
4450 |
with \<open>q = unat m\<close> show ?thesis |
|
4451 |
by simp |
|
4452 |
qed |
|
65268 | 4453 |
|
65363 | 4454 |
lemma word_induct: "P 0 \<Longrightarrow> (\<And>n. P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P m" |
65336 | 4455 |
for P :: "'a::len word \<Rightarrow> bool" |
72262 | 4456 |
by (rule word_induct_less) |
65336 | 4457 |
|
65363 | 4458 |
lemma word_induct2 [induct type]: "P 0 \<Longrightarrow> (\<And>n. 1 + n \<noteq> 0 \<Longrightarrow> P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P n" |
65336 | 4459 |
for P :: "'b::len word \<Rightarrow> bool" |
72262 | 4460 |
apply (rule word_induct_less) |
4461 |
apply simp_all |
|
4462 |
apply (case_tac "1 + na = 0") |
|
65336 | 4463 |
apply auto |
37660 | 4464 |
done |
4465 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
4466 |
|
61799 | 4467 |
subsection \<open>Recursion combinator for words\<close> |
46010 | 4468 |
|
54848 | 4469 |
definition word_rec :: "'a \<Rightarrow> ('b::len word \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'b word \<Rightarrow> 'a" |
65336 | 4470 |
where "word_rec forZero forSuc n = rec_nat forZero (forSuc \<circ> of_nat) (unat n)" |
37660 | 4471 |
|
4472 |
lemma word_rec_0: "word_rec z s 0 = z" |
|
4473 |
by (simp add: word_rec_def) |
|
4474 |
||
65363 | 4475 |
lemma word_rec_Suc: "1 + n \<noteq> 0 \<Longrightarrow> word_rec z s (1 + n) = s n (word_rec z s n)" |
4476 |
for n :: "'a::len word" |
|
71997 | 4477 |
apply (auto simp add: word_rec_def unat_word_ariths) |
72292 | 4478 |
apply (metis (mono_tags, lifting) Abs_fnat_hom_add add_diff_cancel_left' o_def of_nat_1 old.nat.simps(7) plus_1_eq_Suc unatSuc unat_word_ariths(1) unsigned_1 word_arith_nat_add) |
37660 | 4479 |
done |
4480 |
||
65363 | 4481 |
lemma word_rec_Pred: "n \<noteq> 0 \<Longrightarrow> word_rec z s n = s (n - 1) (word_rec z s (n - 1))" |
37660 | 4482 |
apply (rule subst[where t="n" and s="1 + (n - 1)"]) |
4483 |
apply simp |
|
4484 |
apply (subst word_rec_Suc) |
|
4485 |
apply simp |
|
4486 |
apply simp |
|
4487 |
done |
|
4488 |
||
65336 | 4489 |
lemma word_rec_in: "f (word_rec z (\<lambda>_. f) n) = word_rec (f z) (\<lambda>_. f) n" |
37660 | 4490 |
by (induct n) (simp_all add: word_rec_0 word_rec_Suc) |
4491 |
||
67399 | 4492 |
lemma word_rec_in2: "f n (word_rec z f n) = word_rec (f 0 z) (f \<circ> (+) 1) n" |
37660 | 4493 |
by (induct n) (simp_all add: word_rec_0 word_rec_Suc) |
4494 |
||
65268 | 4495 |
lemma word_rec_twice: |
67399 | 4496 |
"m \<le> n \<Longrightarrow> word_rec z f n = word_rec (word_rec z f (n - m)) (f \<circ> (+) (n - m)) m" |
65336 | 4497 |
apply (erule rev_mp) |
4498 |
apply (rule_tac x=z in spec) |
|
4499 |
apply (rule_tac x=f in spec) |
|
4500 |
apply (induct n) |
|
4501 |
apply (simp add: word_rec_0) |
|
4502 |
apply clarsimp |
|
4503 |
apply (rule_tac t="1 + n - m" and s="1 + (n - m)" in subst) |
|
4504 |
apply simp |
|
4505 |
apply (case_tac "1 + (n - m) = 0") |
|
4506 |
apply (simp add: word_rec_0) |
|
4507 |
apply (rule_tac f = "word_rec a b" for a b in arg_cong) |
|
4508 |
apply (rule_tac t="m" and s="m + (1 + (n - m))" in subst) |
|
4509 |
apply simp |
|
4510 |
apply (simp (no_asm_use)) |
|
4511 |
apply (simp add: word_rec_Suc word_rec_in2) |
|
4512 |
apply (erule impE) |
|
4513 |
apply uint_arith |
|
67399 | 4514 |
apply (drule_tac x="x \<circ> (+) 1" in spec) |
65336 | 4515 |
apply (drule_tac x="x 0 xa" in spec) |
37660 | 4516 |
apply simp |
65336 | 4517 |
apply (rule_tac t="\<lambda>a. x (1 + (n - m + a))" and s="\<lambda>a. x (1 + (n - m) + a)" in subst) |
4518 |
apply (clarsimp simp add: fun_eq_iff) |
|
4519 |
apply (rule_tac t="(1 + (n - m + xb))" and s="1 + (n - m) + xb" in subst) |
|
4520 |
apply simp |
|
4521 |
apply (rule refl) |
|
4522 |
apply (rule refl) |
|
4523 |
done |
|
37660 | 4524 |
|
4525 |
lemma word_rec_id: "word_rec z (\<lambda>_. id) n = z" |
|
4526 |
by (induct n) (auto simp add: word_rec_0 word_rec_Suc) |
|
4527 |
||
4528 |
lemma word_rec_id_eq: "\<forall>m < n. f m = id \<Longrightarrow> word_rec z f n = z" |
|
65336 | 4529 |
apply (erule rev_mp) |
4530 |
apply (induct n) |
|
4531 |
apply (auto simp add: word_rec_0 word_rec_Suc) |
|
4532 |
apply (drule spec, erule mp) |
|
4533 |
apply uint_arith |
|
4534 |
apply (drule_tac x=n in spec, erule impE) |
|
4535 |
apply uint_arith |
|
4536 |
apply simp |
|
4537 |
done |
|
37660 | 4538 |
|
65268 | 4539 |
lemma word_rec_max: |
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
58061
diff
changeset
|
4540 |
"\<forall>m\<ge>n. m \<noteq> - 1 \<longrightarrow> f m = id \<Longrightarrow> word_rec z f (- 1) = word_rec z f n" |
65336 | 4541 |
apply (subst word_rec_twice[where n="-1" and m="-1 - n"]) |
4542 |
apply simp |
|
4543 |
apply simp |
|
4544 |
apply (rule word_rec_id_eq) |
|
4545 |
apply clarsimp |
|
4546 |
apply (drule spec, rule mp, erule mp) |
|
4547 |
apply (rule word_plus_mono_right2[OF _ order_less_imp_le]) |
|
4548 |
prefer 2 |
|
4549 |
apply assumption |
|
4550 |
apply simp |
|
4551 |
apply (erule contrapos_pn) |
|
4552 |
apply simp |
|
4553 |
apply (drule arg_cong[where f="\<lambda>x. x - n"]) |
|
4554 |
apply simp |
|
4555 |
done |
|
4556 |
||
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4557 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4558 |
subsection \<open>More\<close> |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4559 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4560 |
lemma mask_1: "mask 1 = 1" |
72512 | 4561 |
by simp |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4562 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4563 |
lemma mask_Suc_0: "mask (Suc 0) = 1" |
72512 | 4564 |
by simp |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4565 |
|
72488 | 4566 |
lemma bin_last_bintrunc: "odd (take_bit l n) \<longleftrightarrow> l > 0 \<and> odd n" |
72079 | 4567 |
by simp |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4568 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4569 |
|
72512 | 4570 |
lemma push_bit_word_beyond [simp]: |
4571 |
\<open>push_bit n w = 0\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close> |
|
4572 |
using that by (transfer fixing: n) (simp add: take_bit_push_bit) |
|
4573 |
||
4574 |
lemma drop_bit_word_beyond [simp]: |
|
4575 |
\<open>drop_bit n w = 0\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close> |
|
4576 |
using that by (transfer fixing: n) (simp add: drop_bit_take_bit) |
|
4577 |
||
4578 |
lemma signed_drop_bit_beyond: |
|
4579 |
\<open>signed_drop_bit n w = (if bit w (LENGTH('a) - Suc 0) then - 1 else 0)\<close> |
|
4580 |
if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close> |
|
4581 |
by (rule bit_word_eqI) (simp add: bit_signed_drop_bit_iff that) |
|
4582 |
||
4583 |
||
72489 | 4584 |
subsection \<open>SMT support\<close> |
4585 |
||
69605 | 4586 |
ML_file \<open>Tools/smt_word.ML\<close> |
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
35049
diff
changeset
|
4587 |
|
41060
4199fdcfa3c0
moved smt_word.ML into the directory of the Word library
boehmes
parents:
40827
diff
changeset
|
4588 |
end |