author | haftmann |
Sun, 06 Jun 2021 15:49:39 +0000 | |
changeset 73816 | 0510c7a4256a |
parent 73789 | aab7975fa070 |
child 73853 | 52b829b18066 |
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> |
|
73535 | 125 |
by transfer auto |
72512 | 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 |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
844 |
have \<section>: "\<And>i n. (i::int) mod 2 ^ n = 0 \<or> 0 < i mod 2 ^ n" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
845 |
by (metis le_less take_bit_eq_mod take_bit_nonnegative) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
846 |
have less_power: "\<And>n i p. (i::int) mod numeral p ^ n < numeral p ^ n" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
847 |
by simp |
71951 | 848 |
show \<open>a mod b div b = 0\<close> |
849 |
for a b :: \<open>'a word\<close> |
|
850 |
apply transfer |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
851 |
apply (simp add: take_bit_eq_mod mod_eq_0_iff_dvd dvd_def) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
852 |
by (metis (no_types, hide_lams) "\<section>" Euclidean_Division.pos_mod_bound Euclidean_Division.pos_mod_sign le_less_trans mult_eq_0_iff take_bit_eq_mod take_bit_nonnegative zdiv_eq_0_iff zmod_le_nonneg_dividend) |
71951 | 853 |
show \<open>(1 + a) div 2 = a div 2\<close> |
854 |
if \<open>even a\<close> |
|
855 |
for a :: \<open>'a word\<close> |
|
71953 | 856 |
using that by transfer |
73535 | 857 |
(auto dest: le_Suc_ex simp add: take_bit_Suc elim!: evenE) |
71951 | 858 |
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> |
859 |
for m n :: nat |
|
860 |
by transfer (simp, simp add: exp_div_exp_eq) |
|
861 |
show "a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)" |
|
862 |
for a :: "'a word" and m n :: nat |
|
863 |
apply transfer |
|
864 |
apply (auto simp add: not_less take_bit_drop_bit ac_simps simp flip: drop_bit_eq_div) |
|
865 |
apply (simp add: drop_bit_take_bit) |
|
866 |
done |
|
867 |
show "a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n" |
|
868 |
for a :: "'a word" and m n :: nat |
|
869 |
by transfer (auto simp flip: take_bit_eq_mod simp add: ac_simps) |
|
870 |
show \<open>a * 2 ^ m mod 2 ^ n = a mod 2 ^ (n - m) * 2 ^ m\<close> |
|
871 |
if \<open>m \<le> n\<close> for a :: "'a word" and m n :: nat |
|
872 |
using that apply transfer |
|
873 |
apply (auto simp flip: take_bit_eq_mod) |
|
874 |
apply (auto simp flip: push_bit_eq_mult simp add: push_bit_take_bit split: split_min_lin) |
|
875 |
done |
|
876 |
show \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close> |
|
877 |
for a :: "'a word" and m n :: nat |
|
878 |
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) |
|
879 |
show \<open>even ((2 ^ m - 1) div (2::'a word) ^ n) \<longleftrightarrow> 2 ^ n = (0::'a word) \<or> m \<le> n\<close> |
|
880 |
for m n :: nat |
|
881 |
by transfer (auto simp add: take_bit_of_mask even_mask_div_iff) |
|
882 |
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> |
|
883 |
for a :: \<open>'a word\<close> and m n :: nat |
|
884 |
proof transfer |
|
885 |
show \<open>even (take_bit LENGTH('a) (k * 2 ^ m) div take_bit LENGTH('a) (2 ^ n)) \<longleftrightarrow> |
|
886 |
n < m |
|
887 |
\<or> take_bit LENGTH('a) ((2::int) ^ n) = take_bit LENGTH('a) 0 |
|
888 |
\<or> (m \<le> n \<and> even (take_bit LENGTH('a) k div take_bit LENGTH('a) (2 ^ (n - m))))\<close> |
|
889 |
for m n :: nat and k l :: int |
|
890 |
by (auto simp flip: take_bit_eq_mod drop_bit_eq_div push_bit_eq_mult |
|
891 |
simp add: div_push_bit_of_1_eq_drop_bit drop_bit_take_bit drop_bit_push_bit_int [of n m]) |
|
892 |
qed |
|
893 |
qed |
|
894 |
||
895 |
end |
|
896 |
||
72262 | 897 |
lemma bit_word_eqI: |
898 |
\<open>a = b\<close> if \<open>\<And>n. n < LENGTH('a) \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close> |
|
899 |
for a b :: \<open>'a::len word\<close> |
|
900 |
using that by transfer (auto simp add: nat_less_le bit_eq_iff bit_take_bit_iff) |
|
901 |
||
902 |
lemma bit_imp_le_length: |
|
903 |
\<open>n < LENGTH('a)\<close> if \<open>bit w n\<close> |
|
904 |
for w :: \<open>'a::len word\<close> |
|
905 |
using that by transfer simp |
|
906 |
||
907 |
lemma not_bit_length [simp]: |
|
908 |
\<open>\<not> bit w LENGTH('a)\<close> for w :: \<open>'a::len word\<close> |
|
909 |
by transfer simp |
|
910 |
||
72830 | 911 |
lemma finite_bit_word [simp]: |
912 |
\<open>finite {n. bit w n}\<close> |
|
913 |
for w :: \<open>'a::len word\<close> |
|
914 |
proof - |
|
915 |
have \<open>{n. bit w n} \<subseteq> {0..LENGTH('a)}\<close> |
|
916 |
by (auto dest: bit_imp_le_length) |
|
917 |
moreover have \<open>finite {0..LENGTH('a)}\<close> |
|
918 |
by simp |
|
919 |
ultimately show ?thesis |
|
920 |
by (rule finite_subset) |
|
921 |
qed |
|
922 |
||
73789 | 923 |
lemma bit_numeral_word_iff [simp]: |
924 |
\<open>bit (numeral w :: 'a::len word) n |
|
925 |
\<longleftrightarrow> n < LENGTH('a) \<and> bit (numeral w :: int) n\<close> |
|
926 |
by transfer simp |
|
927 |
||
928 |
lemma bit_neg_numeral_word_iff [simp]: |
|
929 |
\<open>bit (- numeral w :: 'a::len word) n |
|
930 |
\<longleftrightarrow> n < LENGTH('a) \<and> bit (- numeral w :: int) n\<close> |
|
931 |
by transfer simp |
|
932 |
||
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
933 |
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
|
934 |
begin |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
935 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
936 |
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
|
937 |
is push_bit |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
938 |
proof - |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
939 |
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
|
940 |
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
|
941 |
proof - |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
942 |
from that |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
943 |
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
|
944 |
= 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
|
945 |
by simp |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
946 |
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
|
947 |
by simp |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
948 |
ultimately show ?thesis |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
949 |
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
|
950 |
qed |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
951 |
qed |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
952 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
953 |
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
|
954 |
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
|
955 |
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
|
956 |
|
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
957 |
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
|
958 |
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
|
959 |
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
|
960 |
|
71952
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
961 |
instance proof |
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71958
diff
changeset
|
962 |
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
|
963 |
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
|
964 |
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
|
965 |
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
|
966 |
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
|
967 |
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
|
968 |
qed |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
969 |
|
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
970 |
end |
2efc5b8c7456
canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents:
71951
diff
changeset
|
971 |
|
72262 | 972 |
lemma [code]: |
72508 | 973 |
\<open>push_bit n w = w * 2 ^ n\<close> for w :: \<open>'a::len word\<close> |
974 |
by (fact push_bit_eq_mult) |
|
975 |
||
976 |
lemma [code]: |
|
977 |
\<open>Word.the_int (drop_bit n w) = drop_bit n (Word.the_int w)\<close> |
|
978 |
by transfer (simp add: drop_bit_take_bit min_def le_less less_diff_conv) |
|
979 |
||
980 |
lemma [code]: |
|
72262 | 981 |
\<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> |
982 |
for w :: \<open>'a::len word\<close> |
|
983 |
by transfer (simp add: not_le not_less ac_simps min_absorb2) |
|
984 |
||
985 |
||
986 |
instantiation word :: (len) ring_bit_operations |
|
987 |
begin |
|
988 |
||
989 |
lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close> |
|
990 |
is not |
|
991 |
by (simp add: take_bit_not_iff) |
|
992 |
||
993 |
lift_definition and_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
994 |
is \<open>and\<close> |
|
995 |
by simp |
|
996 |
||
997 |
lift_definition or_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
998 |
is or |
|
999 |
by simp |
|
1000 |
||
1001 |
lift_definition xor_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
|
1002 |
is xor |
|
1003 |
by simp |
|
1004 |
||
1005 |
lift_definition mask_word :: \<open>nat \<Rightarrow> 'a word\<close> |
|
1006 |
is mask |
|
1007 |
. |
|
1008 |
||
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1009 |
lift_definition set_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1010 |
is set_bit |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1011 |
by (simp add: set_bit_def) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1012 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1013 |
lift_definition unset_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1014 |
is unset_bit |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1015 |
by (simp add: unset_bit_def) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1016 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1017 |
lift_definition flip_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1018 |
is flip_bit |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1019 |
by (simp add: flip_bit_def) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1020 |
|
72262 | 1021 |
instance by (standard; transfer) |
1022 |
(auto simp add: minus_eq_not_minus_1 mask_eq_exp_minus_1 |
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1023 |
bit_simps set_bit_def flip_bit_def) |
72262 | 1024 |
|
1025 |
end |
|
1026 |
||
1027 |
lemma [code_abbrev]: |
|
1028 |
\<open>push_bit n 1 = (2 :: 'a::len word) ^ n\<close> |
|
1029 |
by (fact push_bit_of_1) |
|
1030 |
||
1031 |
lemma [code]: |
|
1032 |
\<open>NOT w = Word.of_int (NOT (Word.the_int w))\<close> |
|
1033 |
for w :: \<open>'a::len word\<close> |
|
1034 |
by transfer (simp add: take_bit_not_take_bit) |
|
1035 |
||
1036 |
lemma [code]: |
|
1037 |
\<open>Word.the_int (v AND w) = Word.the_int v AND Word.the_int w\<close> |
|
71990 | 1038 |
by transfer simp |
1039 |
||
72262 | 1040 |
lemma [code]: |
1041 |
\<open>Word.the_int (v OR w) = Word.the_int v OR Word.the_int w\<close> |
|
1042 |
by transfer simp |
|
1043 |
||
1044 |
lemma [code]: |
|
1045 |
\<open>Word.the_int (v XOR w) = Word.the_int v XOR Word.the_int w\<close> |
|
1046 |
by transfer simp |
|
1047 |
||
1048 |
lemma [code]: |
|
1049 |
\<open>Word.the_int (mask n :: 'a::len word) = mask (min LENGTH('a) n)\<close> |
|
1050 |
by transfer simp |
|
1051 |
||
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1052 |
lemma [code]: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1053 |
\<open>set_bit n w = w OR push_bit n 1\<close> for w :: \<open>'a::len word\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1054 |
by (fact set_bit_eq_or) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1055 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1056 |
lemma [code]: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1057 |
\<open>unset_bit n w = w AND NOT (push_bit n 1)\<close> for w :: \<open>'a::len word\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1058 |
by (fact unset_bit_eq_and_not) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1059 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1060 |
lemma [code]: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1061 |
\<open>flip_bit n w = w XOR push_bit n 1\<close> for w :: \<open>'a::len word\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1062 |
by (fact flip_bit_eq_xor) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1063 |
|
72262 | 1064 |
context |
1065 |
includes lifting_syntax |
|
1066 |
begin |
|
1067 |
||
1068 |
lemma set_bit_word_transfer [transfer_rule]: |
|
1069 |
\<open>((=) ===> pcr_word ===> pcr_word) set_bit set_bit\<close> |
|
1070 |
by (unfold set_bit_def) transfer_prover |
|
1071 |
||
1072 |
lemma unset_bit_word_transfer [transfer_rule]: |
|
1073 |
\<open>((=) ===> pcr_word ===> pcr_word) unset_bit unset_bit\<close> |
|
1074 |
by (unfold unset_bit_def) transfer_prover |
|
1075 |
||
1076 |
lemma flip_bit_word_transfer [transfer_rule]: |
|
1077 |
\<open>((=) ===> pcr_word ===> pcr_word) flip_bit flip_bit\<close> |
|
1078 |
by (unfold flip_bit_def) transfer_prover |
|
1079 |
||
1080 |
lemma signed_take_bit_word_transfer [transfer_rule]: |
|
1081 |
\<open>((=) ===> pcr_word ===> pcr_word) |
|
1082 |
(\<lambda>n k. signed_take_bit n (take_bit LENGTH('a::len) k)) |
|
1083 |
(signed_take_bit :: nat \<Rightarrow> 'a word \<Rightarrow> 'a word)\<close> |
|
1084 |
proof - |
|
1085 |
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> |
|
1086 |
let ?W = \<open>\<lambda>n (w :: 'a word). take_bit n w OR of_bool (bit w n) * NOT (mask n)\<close> |
|
1087 |
have \<open>((=) ===> pcr_word ===> pcr_word) ?K ?W\<close> |
|
1088 |
by transfer_prover |
|
1089 |
also have \<open>?K = (\<lambda>n k. signed_take_bit n (take_bit LENGTH('a::len) k))\<close> |
|
1090 |
by (simp add: fun_eq_iff signed_take_bit_def bit_take_bit_iff ac_simps) |
|
1091 |
also have \<open>?W = signed_take_bit\<close> |
|
1092 |
by (simp add: fun_eq_iff signed_take_bit_def) |
|
1093 |
finally show ?thesis . |
|
1094 |
qed |
|
1095 |
||
1096 |
end |
|
1097 |
||
72244 | 1098 |
|
1099 |
subsection \<open>Conversions including casts\<close> |
|
1100 |
||
72262 | 1101 |
subsubsection \<open>Generic unsigned conversion\<close> |
1102 |
||
1103 |
context semiring_bits |
|
1104 |
begin |
|
1105 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
1106 |
lemma bit_unsigned_iff [bit_simps]: |
72262 | 1107 |
\<open>bit (unsigned w) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> bit w n\<close> |
1108 |
for w :: \<open>'b::len word\<close> |
|
1109 |
by (transfer fixing: bit) (simp add: bit_of_nat_iff bit_nat_iff bit_take_bit_iff) |
|
1110 |
||
1111 |
end |
|
1112 |
||
1113 |
context semiring_bit_shifts |
|
1114 |
begin |
|
1115 |
||
1116 |
lemma unsigned_push_bit_eq: |
|
1117 |
\<open>unsigned (push_bit n w) = take_bit LENGTH('b) (push_bit n (unsigned w))\<close> |
|
1118 |
for w :: \<open>'b::len word\<close> |
|
1119 |
proof (rule bit_eqI) |
|
1120 |
fix m |
|
1121 |
assume \<open>2 ^ m \<noteq> 0\<close> |
|
1122 |
show \<open>bit (unsigned (push_bit n w)) m = bit (take_bit LENGTH('b) (push_bit n (unsigned w))) m\<close> |
|
1123 |
proof (cases \<open>n \<le> m\<close>) |
|
1124 |
case True |
|
1125 |
with \<open>2 ^ m \<noteq> 0\<close> have \<open>2 ^ (m - n) \<noteq> 0\<close> |
|
1126 |
by (metis (full_types) diff_add exp_add_not_zero_imp) |
|
1127 |
with True show ?thesis |
|
1128 |
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) |
|
1129 |
next |
|
1130 |
case False |
|
1131 |
then show ?thesis |
|
1132 |
by (simp add: not_le bit_unsigned_iff bit_push_bit_iff Parity.bit_push_bit_iff bit_take_bit_iff) |
|
1133 |
qed |
|
1134 |
qed |
|
1135 |
||
1136 |
lemma unsigned_take_bit_eq: |
|
1137 |
\<open>unsigned (take_bit n w) = take_bit n (unsigned w)\<close> |
|
1138 |
for w :: \<open>'b::len word\<close> |
|
1139 |
by (rule bit_eqI) (simp add: bit_unsigned_iff bit_take_bit_iff Parity.bit_take_bit_iff) |
|
1140 |
||
1141 |
end |
|
1142 |
||
72512 | 1143 |
context unique_euclidean_semiring_with_bit_shifts |
1144 |
begin |
|
1145 |
||
1146 |
lemma unsigned_drop_bit_eq: |
|
1147 |
\<open>unsigned (drop_bit n w) = drop_bit n (take_bit LENGTH('b) (unsigned w))\<close> |
|
1148 |
for w :: \<open>'b::len word\<close> |
|
1149 |
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) |
|
1150 |
||
1151 |
end |
|
1152 |
||
72262 | 1153 |
context semiring_bit_operations |
1154 |
begin |
|
1155 |
||
1156 |
lemma unsigned_and_eq: |
|
1157 |
\<open>unsigned (v AND w) = unsigned v AND unsigned w\<close> |
|
1158 |
for v w :: \<open>'b::len word\<close> |
|
1159 |
by (rule bit_eqI) (simp add: bit_unsigned_iff bit_and_iff Bit_Operations.bit_and_iff) |
|
1160 |
||
1161 |
lemma unsigned_or_eq: |
|
1162 |
\<open>unsigned (v OR w) = unsigned v OR unsigned w\<close> |
|
1163 |
for v w :: \<open>'b::len word\<close> |
|
1164 |
by (rule bit_eqI) (simp add: bit_unsigned_iff bit_or_iff Bit_Operations.bit_or_iff) |
|
1165 |
||
1166 |
lemma unsigned_xor_eq: |
|
1167 |
\<open>unsigned (v XOR w) = unsigned v XOR unsigned w\<close> |
|
1168 |
for v w :: \<open>'b::len word\<close> |
|
1169 |
by (rule bit_eqI) (simp add: bit_unsigned_iff bit_xor_iff Bit_Operations.bit_xor_iff) |
|
1170 |
||
1171 |
end |
|
1172 |
||
1173 |
context ring_bit_operations |
|
1174 |
begin |
|
1175 |
||
1176 |
lemma unsigned_not_eq: |
|
1177 |
\<open>unsigned (NOT w) = take_bit LENGTH('b) (NOT (unsigned w))\<close> |
|
1178 |
for w :: \<open>'b::len word\<close> |
|
1179 |
by (rule bit_eqI) |
|
1180 |
(simp add: bit_unsigned_iff bit_take_bit_iff bit_not_iff Bit_Operations.bit_not_iff exp_eq_zero_iff not_le) |
|
1181 |
||
1182 |
end |
|
1183 |
||
1184 |
context unique_euclidean_semiring_numeral |
|
1185 |
begin |
|
1186 |
||
72292 | 1187 |
lemma unsigned_greater_eq [simp]: |
72262 | 1188 |
\<open>0 \<le> unsigned w\<close> for w :: \<open>'b::len word\<close> |
1189 |
by (transfer fixing: less_eq) simp |
|
1190 |
||
72292 | 1191 |
lemma unsigned_less [simp]: |
72262 | 1192 |
\<open>unsigned w < 2 ^ LENGTH('b)\<close> for w :: \<open>'b::len word\<close> |
1193 |
by (transfer fixing: less) simp |
|
1194 |
||
1195 |
end |
|
1196 |
||
1197 |
context linordered_semidom |
|
1198 |
begin |
|
1199 |
||
1200 |
lemma word_less_eq_iff_unsigned: |
|
1201 |
"a \<le> b \<longleftrightarrow> unsigned a \<le> unsigned b" |
|
1202 |
by (transfer fixing: less_eq) (simp add: nat_le_eq_zle) |
|
1203 |
||
1204 |
lemma word_less_iff_unsigned: |
|
1205 |
"a < b \<longleftrightarrow> unsigned a < unsigned b" |
|
1206 |
by (transfer fixing: less) (auto dest: preorder_class.le_less_trans [OF take_bit_nonnegative]) |
|
1207 |
||
1208 |
end |
|
1209 |
||
1210 |
||
1211 |
subsubsection \<open>Generic signed conversion\<close> |
|
1212 |
||
1213 |
context ring_bit_operations |
|
1214 |
begin |
|
1215 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
1216 |
lemma bit_signed_iff [bit_simps]: |
72262 | 1217 |
\<open>bit (signed w) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> bit w (min (LENGTH('b) - Suc 0) n)\<close> |
1218 |
for w :: \<open>'b::len word\<close> |
|
1219 |
by (transfer fixing: bit) |
|
1220 |
(auto simp add: bit_of_int_iff Bit_Operations.bit_signed_take_bit_iff min_def) |
|
1221 |
||
1222 |
lemma signed_push_bit_eq: |
|
1223 |
\<open>signed (push_bit n w) = signed_take_bit (LENGTH('b) - Suc 0) (push_bit n (signed w :: 'a))\<close> |
|
1224 |
for w :: \<open>'b::len word\<close> |
|
1225 |
proof (rule bit_eqI) |
|
1226 |
fix m |
|
1227 |
assume \<open>2 ^ m \<noteq> 0\<close> |
|
1228 |
define q where \<open>q = LENGTH('b) - Suc 0\<close> |
|
1229 |
then have *: \<open>LENGTH('b) = Suc q\<close> |
|
1230 |
by simp |
|
1231 |
show \<open>bit (signed (push_bit n w)) m \<longleftrightarrow> |
|
1232 |
bit (signed_take_bit (LENGTH('b) - Suc 0) (push_bit n (signed w :: 'a))) m\<close> |
|
1233 |
proof (cases \<open>q \<le> m\<close>) |
|
1234 |
case True |
|
1235 |
moreover define r where \<open>r = m - q\<close> |
|
1236 |
ultimately have \<open>m = q + r\<close> |
|
1237 |
by simp |
|
1238 |
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> |
|
1239 |
using exp_add_not_zero_imp_left [of q r] exp_add_not_zero_imp_right [of q r] |
|
1240 |
by simp_all |
|
1241 |
moreover from \<open>2 ^ q \<noteq> 0\<close> have \<open>2 ^ (q - n) \<noteq> 0\<close> |
|
1242 |
by (rule exp_not_zero_imp_exp_diff_not_zero) |
|
1243 |
ultimately show ?thesis |
|
1244 |
by (auto simp add: bit_signed_iff bit_signed_take_bit_iff bit_push_bit_iff Parity.bit_push_bit_iff |
|
1245 |
min_def * exp_eq_zero_iff le_diff_conv2) |
|
1246 |
next |
|
1247 |
case False |
|
1248 |
then show ?thesis |
|
1249 |
using exp_not_zero_imp_exp_diff_not_zero [of m n] |
|
1250 |
by (auto simp add: bit_signed_iff bit_signed_take_bit_iff bit_push_bit_iff Parity.bit_push_bit_iff |
|
1251 |
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 |
|
1252 |
exp_eq_zero_iff) |
|
1253 |
qed |
|
1254 |
qed |
|
1255 |
||
1256 |
lemma signed_take_bit_eq: |
|
1257 |
\<open>signed (take_bit n w) = (if n < LENGTH('b) then take_bit n (signed w) else signed w)\<close> |
|
1258 |
for w :: \<open>'b::len word\<close> |
|
1259 |
by (transfer fixing: take_bit; cases \<open>LENGTH('b)\<close>) |
|
1260 |
(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) |
|
1261 |
||
1262 |
lemma signed_not_eq: |
|
1263 |
\<open>signed (NOT w) = signed_take_bit LENGTH('b) (NOT (signed w))\<close> |
|
1264 |
for w :: \<open>'b::len word\<close> |
|
1265 |
proof (rule bit_eqI) |
|
1266 |
fix n |
|
1267 |
assume \<open>2 ^ n \<noteq> 0\<close> |
|
1268 |
define q where \<open>q = LENGTH('b) - Suc 0\<close> |
|
1269 |
then have *: \<open>LENGTH('b) = Suc q\<close> |
|
1270 |
by simp |
|
1271 |
show \<open>bit (signed (NOT w)) n \<longleftrightarrow> |
|
1272 |
bit (signed_take_bit LENGTH('b) (NOT (signed w))) n\<close> |
|
1273 |
proof (cases \<open>q < n\<close>) |
|
1274 |
case True |
|
1275 |
moreover define r where \<open>r = n - Suc q\<close> |
|
1276 |
ultimately have \<open>n = r + Suc q\<close> |
|
1277 |
by simp |
|
1278 |
moreover from \<open>2 ^ n \<noteq> 0\<close> \<open>n = r + Suc q\<close> |
|
1279 |
have \<open>2 ^ Suc q \<noteq> 0\<close> |
|
1280 |
using exp_add_not_zero_imp_right by blast |
|
1281 |
ultimately show ?thesis |
|
1282 |
by (simp add: * bit_signed_iff bit_not_iff bit_signed_take_bit_iff Bit_Operations.bit_not_iff min_def |
|
1283 |
exp_eq_zero_iff) |
|
1284 |
next |
|
1285 |
case False |
|
1286 |
then show ?thesis |
|
1287 |
by (auto simp add: * bit_signed_iff bit_not_iff bit_signed_take_bit_iff Bit_Operations.bit_not_iff min_def |
|
1288 |
exp_eq_zero_iff) |
|
1289 |
qed |
|
1290 |
qed |
|
1291 |
||
1292 |
lemma signed_and_eq: |
|
1293 |
\<open>signed (v AND w) = signed v AND signed w\<close> |
|
1294 |
for v w :: \<open>'b::len word\<close> |
|
1295 |
by (rule bit_eqI) (simp add: bit_signed_iff bit_and_iff Bit_Operations.bit_and_iff) |
|
1296 |
||
1297 |
lemma signed_or_eq: |
|
1298 |
\<open>signed (v OR w) = signed v OR signed w\<close> |
|
1299 |
for v w :: \<open>'b::len word\<close> |
|
1300 |
by (rule bit_eqI) (simp add: bit_signed_iff bit_or_iff Bit_Operations.bit_or_iff) |
|
1301 |
||
1302 |
lemma signed_xor_eq: |
|
1303 |
\<open>signed (v XOR w) = signed v XOR signed w\<close> |
|
1304 |
for v w :: \<open>'b::len word\<close> |
|
1305 |
by (rule bit_eqI) (simp add: bit_signed_iff bit_xor_iff Bit_Operations.bit_xor_iff) |
|
1306 |
||
1307 |
end |
|
1308 |
||
1309 |
||
1310 |
subsubsection \<open>More\<close> |
|
1311 |
||
1312 |
lemma sint_greater_eq: |
|
1313 |
\<open>- (2 ^ (LENGTH('a) - Suc 0)) \<le> sint w\<close> for w :: \<open>'a::len word\<close> |
|
1314 |
proof (cases \<open>bit w (LENGTH('a) - Suc 0)\<close>) |
|
1315 |
case True |
|
1316 |
then show ?thesis |
|
1317 |
by transfer (simp add: signed_take_bit_eq_if_negative minus_exp_eq_not_mask or_greater_eq ac_simps) |
|
1318 |
next |
|
1319 |
have *: \<open>- (2 ^ (LENGTH('a) - Suc 0)) \<le> (0::int)\<close> |
|
1320 |
by simp |
|
1321 |
case False |
|
1322 |
then show ?thesis |
|
1323 |
by transfer (auto simp add: signed_take_bit_eq intro: order_trans *) |
|
1324 |
qed |
|
1325 |
||
1326 |
lemma sint_less: |
|
1327 |
\<open>sint w < 2 ^ (LENGTH('a) - Suc 0)\<close> for w :: \<open>'a::len word\<close> |
|
1328 |
by (cases \<open>bit w (LENGTH('a) - Suc 0)\<close>; transfer) |
|
1329 |
(simp_all add: signed_take_bit_eq signed_take_bit_def not_eq_complement mask_eq_exp_minus_1 OR_upper) |
|
1330 |
||
1331 |
lemma unat_div_distrib: |
|
1332 |
\<open>unat (v div w) = unat v div unat w\<close> |
|
1333 |
proof transfer |
|
1334 |
fix k l |
|
1335 |
have \<open>nat (take_bit LENGTH('a) k) div nat (take_bit LENGTH('a) l) \<le> nat (take_bit LENGTH('a) k)\<close> |
|
1336 |
by (rule div_le_dividend) |
|
1337 |
also have \<open>nat (take_bit LENGTH('a) k) < 2 ^ LENGTH('a)\<close> |
|
1338 |
by (simp add: nat_less_iff) |
|
1339 |
finally show \<open>(nat \<circ> take_bit LENGTH('a)) (take_bit LENGTH('a) k div take_bit LENGTH('a) l) = |
|
1340 |
(nat \<circ> take_bit LENGTH('a)) k div (nat \<circ> take_bit LENGTH('a)) l\<close> |
|
1341 |
by (simp add: nat_take_bit_eq div_int_pos_iff nat_div_distrib take_bit_nat_eq_self_iff) |
|
1342 |
qed |
|
1343 |
||
1344 |
lemma unat_mod_distrib: |
|
1345 |
\<open>unat (v mod w) = unat v mod unat w\<close> |
|
1346 |
proof transfer |
|
1347 |
fix k l |
|
1348 |
have \<open>nat (take_bit LENGTH('a) k) mod nat (take_bit LENGTH('a) l) \<le> nat (take_bit LENGTH('a) k)\<close> |
|
1349 |
by (rule mod_less_eq_dividend) |
|
1350 |
also have \<open>nat (take_bit LENGTH('a) k) < 2 ^ LENGTH('a)\<close> |
|
1351 |
by (simp add: nat_less_iff) |
|
1352 |
finally show \<open>(nat \<circ> take_bit LENGTH('a)) (take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = |
|
1353 |
(nat \<circ> take_bit LENGTH('a)) k mod (nat \<circ> take_bit LENGTH('a)) l\<close> |
|
1354 |
by (simp add: nat_take_bit_eq mod_int_pos_iff less_le nat_mod_distrib take_bit_nat_eq_self_iff) |
|
1355 |
qed |
|
1356 |
||
1357 |
lemma uint_div_distrib: |
|
1358 |
\<open>uint (v div w) = uint v div uint w\<close> |
|
1359 |
proof - |
|
1360 |
have \<open>int (unat (v div w)) = int (unat v div unat w)\<close> |
|
1361 |
by (simp add: unat_div_distrib) |
|
1362 |
then show ?thesis |
|
1363 |
by (simp add: of_nat_div) |
|
1364 |
qed |
|
1365 |
||
72388 | 1366 |
lemma unat_drop_bit_eq: |
1367 |
\<open>unat (drop_bit n w) = drop_bit n (unat w)\<close> |
|
1368 |
by (rule bit_eqI) (simp add: bit_unsigned_iff bit_drop_bit_eq) |
|
1369 |
||
72262 | 1370 |
lemma uint_mod_distrib: |
1371 |
\<open>uint (v mod w) = uint v mod uint w\<close> |
|
1372 |
proof - |
|
1373 |
have \<open>int (unat (v mod w)) = int (unat v mod unat w)\<close> |
|
1374 |
by (simp add: unat_mod_distrib) |
|
1375 |
then show ?thesis |
|
1376 |
by (simp add: of_nat_mod) |
|
1377 |
qed |
|
1378 |
||
1379 |
context semiring_bit_shifts |
|
1380 |
begin |
|
1381 |
||
1382 |
lemma unsigned_ucast_eq: |
|
1383 |
\<open>unsigned (ucast w :: 'c::len word) = take_bit LENGTH('c) (unsigned w)\<close> |
|
1384 |
for w :: \<open>'b::len word\<close> |
|
1385 |
by (rule bit_eqI) (simp add: bit_unsigned_iff Word.bit_unsigned_iff bit_take_bit_iff exp_eq_zero_iff not_le) |
|
1386 |
||
1387 |
end |
|
1388 |
||
1389 |
context ring_bit_operations |
|
1390 |
begin |
|
1391 |
||
1392 |
lemma signed_ucast_eq: |
|
1393 |
\<open>signed (ucast w :: 'c::len word) = signed_take_bit (LENGTH('c) - Suc 0) (unsigned w)\<close> |
|
1394 |
for w :: \<open>'b::len word\<close> |
|
1395 |
proof (rule bit_eqI) |
|
1396 |
fix n |
|
1397 |
assume \<open>2 ^ n \<noteq> 0\<close> |
|
1398 |
then have \<open>2 ^ (min (LENGTH('c) - Suc 0) n) \<noteq> 0\<close> |
|
1399 |
by (simp add: min_def) |
|
1400 |
(metis (mono_tags) diff_diff_cancel exp_not_zero_imp_exp_diff_not_zero) |
|
1401 |
then show \<open>bit (signed (ucast w :: 'c::len word)) n \<longleftrightarrow> bit (signed_take_bit (LENGTH('c) - Suc 0) (unsigned w)) n\<close> |
|
1402 |
by (simp add: bit_signed_iff bit_unsigned_iff Word.bit_unsigned_iff bit_signed_take_bit_iff exp_eq_zero_iff not_le) |
|
1403 |
qed |
|
1404 |
||
1405 |
lemma signed_scast_eq: |
|
1406 |
\<open>signed (scast w :: 'c::len word) = signed_take_bit (LENGTH('c) - Suc 0) (signed w)\<close> |
|
1407 |
for w :: \<open>'b::len word\<close> |
|
1408 |
proof (rule bit_eqI) |
|
1409 |
fix n |
|
1410 |
assume \<open>2 ^ n \<noteq> 0\<close> |
|
1411 |
then have \<open>2 ^ (min (LENGTH('c) - Suc 0) n) \<noteq> 0\<close> |
|
1412 |
by (simp add: min_def) |
|
1413 |
(metis (mono_tags) diff_diff_cancel exp_not_zero_imp_exp_diff_not_zero) |
|
1414 |
then show \<open>bit (signed (scast w :: 'c::len word)) n \<longleftrightarrow> bit (signed_take_bit (LENGTH('c) - Suc 0) (signed w)) n\<close> |
|
1415 |
by (simp add: bit_signed_iff bit_unsigned_iff Word.bit_signed_iff bit_signed_take_bit_iff exp_eq_zero_iff not_le) |
|
1416 |
qed |
|
1417 |
||
1418 |
end |
|
1419 |
||
72244 | 1420 |
lemma uint_nonnegative: "0 \<le> uint w" |
72262 | 1421 |
by (fact unsigned_greater_eq) |
72244 | 1422 |
|
1423 |
lemma uint_bounded: "uint w < 2 ^ LENGTH('a)" |
|
1424 |
for w :: "'a::len word" |
|
72262 | 1425 |
by (fact unsigned_less) |
72244 | 1426 |
|
1427 |
lemma uint_idem: "uint w mod 2 ^ LENGTH('a) = uint w" |
|
1428 |
for w :: "'a::len word" |
|
72262 | 1429 |
by transfer (simp add: take_bit_eq_mod) |
72244 | 1430 |
|
1431 |
lemma word_uint_eqI: "uint a = uint b \<Longrightarrow> a = b" |
|
72262 | 1432 |
by (fact unsigned_word_eqI) |
72244 | 1433 |
|
1434 |
lemma word_uint_eq_iff: "a = b \<longleftrightarrow> uint a = uint b" |
|
72262 | 1435 |
by (fact word_eq_iff_unsigned) |
1436 |
||
1437 |
lemma uint_word_of_int_eq: |
|
72244 | 1438 |
\<open>uint (word_of_int k :: 'a::len word) = take_bit LENGTH('a) k\<close> |
1439 |
by transfer rule |
|
1440 |
||
1441 |
lemma uint_word_of_int: "uint (word_of_int k :: 'a::len word) = k mod 2 ^ LENGTH('a)" |
|
1442 |
by (simp add: uint_word_of_int_eq take_bit_eq_mod) |
|
1443 |
||
1444 |
lemma word_of_int_uint: "word_of_int (uint w) = w" |
|
1445 |
by transfer simp |
|
1446 |
||
1447 |
lemma word_div_def [code]: |
|
1448 |
"a div b = word_of_int (uint a div uint b)" |
|
1449 |
by transfer rule |
|
1450 |
||
1451 |
lemma word_mod_def [code]: |
|
1452 |
"a mod b = word_of_int (uint a mod uint b)" |
|
1453 |
by transfer rule |
|
1454 |
||
1455 |
lemma split_word_all: "(\<And>x::'a::len word. PROP P x) \<equiv> (\<And>x. PROP P (word_of_int x))" |
|
1456 |
proof |
|
1457 |
fix x :: "'a word" |
|
1458 |
assume "\<And>x. PROP P (word_of_int x)" |
|
1459 |
then have "PROP P (word_of_int (uint x))" . |
|
72262 | 1460 |
then show "PROP P x" |
1461 |
by (simp only: word_of_int_uint) |
|
72244 | 1462 |
qed |
1463 |
||
72262 | 1464 |
lemma sint_uint: |
1465 |
\<open>sint w = signed_take_bit (LENGTH('a) - Suc 0) (uint w)\<close> |
|
72244 | 1466 |
for w :: \<open>'a::len word\<close> |
1467 |
by (cases \<open>LENGTH('a)\<close>; transfer) (simp_all add: signed_take_bit_take_bit) |
|
1468 |
||
72262 | 1469 |
lemma unat_eq_nat_uint: |
72244 | 1470 |
\<open>unat w = nat (uint w)\<close> |
1471 |
by simp |
|
1472 |
||
72262 | 1473 |
lemma ucast_eq: |
72244 | 1474 |
\<open>ucast w = word_of_int (uint w)\<close> |
1475 |
by transfer simp |
|
1476 |
||
72262 | 1477 |
lemma scast_eq: |
72244 | 1478 |
\<open>scast w = word_of_int (sint w)\<close> |
1479 |
by transfer simp |
|
1480 |
||
72262 | 1481 |
lemma uint_0_eq: |
72244 | 1482 |
\<open>uint 0 = 0\<close> |
72262 | 1483 |
by (fact unsigned_0) |
1484 |
||
1485 |
lemma uint_1_eq: |
|
72244 | 1486 |
\<open>uint 1 = 1\<close> |
72262 | 1487 |
by (fact unsigned_1) |
72244 | 1488 |
|
1489 |
lemma word_m1_wi: "- 1 = word_of_int (- 1)" |
|
72262 | 1490 |
by simp |
72244 | 1491 |
|
1492 |
lemma uint_0_iff: "uint x = 0 \<longleftrightarrow> x = 0" |
|
72262 | 1493 |
by (auto simp add: unsigned_word_eqI) |
72244 | 1494 |
|
1495 |
lemma unat_0_iff: "unat x = 0 \<longleftrightarrow> x = 0" |
|
72262 | 1496 |
by (auto simp add: unsigned_word_eqI) |
1497 |
||
1498 |
lemma unat_0: "unat 0 = 0" |
|
1499 |
by (fact unsigned_0) |
|
72244 | 1500 |
|
1501 |
lemma unat_gt_0: "0 < unat x \<longleftrightarrow> x \<noteq> 0" |
|
1502 |
by (auto simp: unat_0_iff [symmetric]) |
|
1503 |
||
72262 | 1504 |
lemma ucast_0: "ucast 0 = 0" |
1505 |
by (fact unsigned_0) |
|
1506 |
||
1507 |
lemma sint_0: "sint 0 = 0" |
|
1508 |
by (fact signed_0) |
|
1509 |
||
1510 |
lemma scast_0: "scast 0 = 0" |
|
1511 |
by (fact signed_0) |
|
1512 |
||
1513 |
lemma sint_n1: "sint (- 1) = - 1" |
|
1514 |
by (fact signed_minus_1) |
|
1515 |
||
1516 |
lemma scast_n1: "scast (- 1) = - 1" |
|
1517 |
by (fact signed_minus_1) |
|
72244 | 1518 |
|
1519 |
lemma uint_1: "uint (1::'a::len word) = 1" |
|
1520 |
by (fact uint_1_eq) |
|
1521 |
||
72262 | 1522 |
lemma unat_1: "unat (1::'a::len word) = 1" |
1523 |
by (fact unsigned_1) |
|
1524 |
||
1525 |
lemma ucast_1: "ucast (1::'a::len word) = 1" |
|
1526 |
by (fact unsigned_1) |
|
72244 | 1527 |
|
1528 |
instantiation word :: (len) size |
|
1529 |
begin |
|
1530 |
||
1531 |
lift_definition size_word :: \<open>'a word \<Rightarrow> nat\<close> |
|
1532 |
is \<open>\<lambda>_. LENGTH('a)\<close> .. |
|
1533 |
||
1534 |
instance .. |
|
1535 |
||
1536 |
end |
|
1537 |
||
1538 |
lemma word_size [code]: |
|
1539 |
\<open>size w = LENGTH('a)\<close> for w :: \<open>'a::len word\<close> |
|
1540 |
by (fact size_word.rep_eq) |
|
1541 |
||
1542 |
lemma word_size_gt_0 [iff]: "0 < size w" |
|
1543 |
for w :: "'a::len word" |
|
1544 |
by (simp add: word_size) |
|
1545 |
||
1546 |
lemmas lens_gt_0 = word_size_gt_0 len_gt_0 |
|
1547 |
||
1548 |
lemma lens_not_0 [iff]: |
|
1549 |
\<open>size w \<noteq> 0\<close> for w :: \<open>'a::len word\<close> |
|
1550 |
by auto |
|
1551 |
||
1552 |
lift_definition source_size :: \<open>('a::len word \<Rightarrow> 'b) \<Rightarrow> nat\<close> |
|
1553 |
is \<open>\<lambda>_. LENGTH('a)\<close> . |
|
1554 |
||
1555 |
lift_definition target_size :: \<open>('a \<Rightarrow> 'b::len word) \<Rightarrow> nat\<close> |
|
1556 |
is \<open>\<lambda>_. LENGTH('b)\<close> .. |
|
1557 |
||
1558 |
lift_definition is_up :: \<open>('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool\<close> |
|
1559 |
is \<open>\<lambda>_. LENGTH('a) \<le> LENGTH('b)\<close> .. |
|
1560 |
||
1561 |
lift_definition is_down :: \<open>('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool\<close> |
|
1562 |
is \<open>\<lambda>_. LENGTH('a) \<ge> LENGTH('b)\<close> .. |
|
1563 |
||
1564 |
lemma is_up_eq: |
|
1565 |
\<open>is_up f \<longleftrightarrow> source_size f \<le> target_size f\<close> |
|
1566 |
for f :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
1567 |
by (simp add: source_size.rep_eq target_size.rep_eq is_up.rep_eq) |
|
1568 |
||
1569 |
lemma is_down_eq: |
|
1570 |
\<open>is_down f \<longleftrightarrow> target_size f \<le> source_size f\<close> |
|
1571 |
for f :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
1572 |
by (simp add: source_size.rep_eq target_size.rep_eq is_down.rep_eq) |
|
1573 |
||
1574 |
lift_definition word_int_case :: \<open>(int \<Rightarrow> 'b) \<Rightarrow> 'a::len word \<Rightarrow> 'b\<close> |
|
1575 |
is \<open>\<lambda>f. f \<circ> take_bit LENGTH('a)\<close> by simp |
|
1576 |
||
1577 |
lemma word_int_case_eq_uint [code]: |
|
1578 |
\<open>word_int_case f w = f (uint w)\<close> |
|
1579 |
by transfer simp |
|
1580 |
||
1581 |
translations |
|
1582 |
"case x of XCONST of_int y \<Rightarrow> b" \<rightleftharpoons> "CONST word_int_case (\<lambda>y. b) x" |
|
1583 |
"case x of (XCONST of_int :: 'a) y \<Rightarrow> b" \<rightharpoonup> "CONST word_int_case (\<lambda>y. b) x" |
|
1584 |
||
1585 |
||
1586 |
subsection \<open>Arithmetic operations\<close> |
|
1587 |
||
1588 |
text \<open>Legacy theorems:\<close> |
|
1589 |
||
1590 |
lemma word_add_def [code]: |
|
1591 |
"a + b = word_of_int (uint a + uint b)" |
|
1592 |
by transfer (simp add: take_bit_add) |
|
1593 |
||
1594 |
lemma word_sub_wi [code]: |
|
1595 |
"a - b = word_of_int (uint a - uint b)" |
|
1596 |
by transfer (simp add: take_bit_diff) |
|
1597 |
||
1598 |
lemma word_mult_def [code]: |
|
1599 |
"a * b = word_of_int (uint a * uint b)" |
|
1600 |
by transfer (simp add: take_bit_eq_mod mod_simps) |
|
1601 |
||
1602 |
lemma word_minus_def [code]: |
|
1603 |
"- a = word_of_int (- uint a)" |
|
1604 |
by transfer (simp add: take_bit_minus) |
|
1605 |
||
1606 |
lemma word_0_wi: |
|
1607 |
"0 = word_of_int 0" |
|
1608 |
by transfer simp |
|
1609 |
||
1610 |
lemma word_1_wi: |
|
1611 |
"1 = word_of_int 1" |
|
1612 |
by transfer simp |
|
1613 |
||
1614 |
lift_definition word_succ :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x + 1" |
|
1615 |
by (auto simp add: take_bit_eq_mod intro: mod_add_cong) |
|
1616 |
||
1617 |
lift_definition word_pred :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x - 1" |
|
1618 |
by (auto simp add: take_bit_eq_mod intro: mod_diff_cong) |
|
1619 |
||
1620 |
lemma word_succ_alt [code]: |
|
1621 |
"word_succ a = word_of_int (uint a + 1)" |
|
1622 |
by transfer (simp add: take_bit_eq_mod mod_simps) |
|
1623 |
||
1624 |
lemma word_pred_alt [code]: |
|
1625 |
"word_pred a = word_of_int (uint a - 1)" |
|
1626 |
by transfer (simp add: take_bit_eq_mod mod_simps) |
|
1627 |
||
1628 |
lemmas word_arith_wis = |
|
1629 |
word_add_def word_sub_wi word_mult_def |
|
1630 |
word_minus_def word_succ_alt word_pred_alt |
|
1631 |
word_0_wi word_1_wi |
|
1632 |
||
1633 |
lemma wi_homs: |
|
1634 |
shows wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)" |
|
1635 |
and wi_hom_sub: "word_of_int a - word_of_int b = word_of_int (a - b)" |
|
1636 |
and wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)" |
|
1637 |
and wi_hom_neg: "- word_of_int a = word_of_int (- a)" |
|
1638 |
and wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)" |
|
1639 |
and wi_hom_pred: "word_pred (word_of_int a) = word_of_int (a - 1)" |
|
1640 |
by (transfer, simp)+ |
|
1641 |
||
1642 |
lemmas wi_hom_syms = wi_homs [symmetric] |
|
1643 |
||
1644 |
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi |
|
1645 |
||
1646 |
lemmas word_of_int_hom_syms = word_of_int_homs [symmetric] |
|
1647 |
||
1648 |
lemma double_eq_zero_iff: |
|
1649 |
\<open>2 * a = 0 \<longleftrightarrow> a = 0 \<or> a = 2 ^ (LENGTH('a) - Suc 0)\<close> |
|
1650 |
for a :: \<open>'a::len word\<close> |
|
1651 |
proof - |
|
1652 |
define n where \<open>n = LENGTH('a) - Suc 0\<close> |
|
1653 |
then have *: \<open>LENGTH('a) = Suc n\<close> |
|
1654 |
by simp |
|
1655 |
have \<open>a = 0\<close> if \<open>2 * a = 0\<close> and \<open>a \<noteq> 2 ^ (LENGTH('a) - Suc 0)\<close> |
|
1656 |
using that by transfer |
|
1657 |
(auto simp add: take_bit_eq_0_iff take_bit_eq_mod *) |
|
1658 |
moreover have \<open>2 ^ LENGTH('a) = (0 :: 'a word)\<close> |
|
1659 |
by transfer simp |
|
1660 |
then have \<open>2 * 2 ^ (LENGTH('a) - Suc 0) = (0 :: 'a word)\<close> |
|
1661 |
by (simp add: *) |
|
1662 |
ultimately show ?thesis |
|
1663 |
by auto |
|
1664 |
qed |
|
1665 |
||
1666 |
||
1667 |
subsection \<open>Ordering\<close> |
|
1668 |
||
72388 | 1669 |
lift_definition word_sle :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> bool\<close> |
1670 |
is \<open>\<lambda>k l. signed_take_bit (LENGTH('a) - Suc 0) k \<le> signed_take_bit (LENGTH('a) - Suc 0) l\<close> |
|
1671 |
by (simp flip: signed_take_bit_decr_length_iff) |
|
1672 |
||
1673 |
lift_definition word_sless :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> bool\<close> |
|
1674 |
is \<open>\<lambda>k l. signed_take_bit (LENGTH('a) - Suc 0) k < signed_take_bit (LENGTH('a) - Suc 0) l\<close> |
|
72244 | 1675 |
by (simp flip: signed_take_bit_decr_length_iff) |
1676 |
||
72388 | 1677 |
notation |
1678 |
word_sle ("'(\<le>s')") and |
|
1679 |
word_sle ("(_/ \<le>s _)" [51, 51] 50) and |
|
1680 |
word_sless ("'(<s')") and |
|
1681 |
word_sless ("(_/ <s _)" [51, 51] 50) |
|
1682 |
||
1683 |
notation (input) |
|
1684 |
word_sle ("(_/ <=s _)" [51, 51] 50) |
|
1685 |
||
72244 | 1686 |
lemma word_sle_eq [code]: |
1687 |
\<open>a <=s b \<longleftrightarrow> sint a \<le> sint b\<close> |
|
1688 |
by transfer simp |
|
1689 |
||
1690 |
lemma [code]: |
|
1691 |
\<open>a <s b \<longleftrightarrow> sint a < sint b\<close> |
|
1692 |
by transfer simp |
|
1693 |
||
72388 | 1694 |
lemma signed_ordering: \<open>ordering word_sle word_sless\<close> |
1695 |
apply (standard; transfer) |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1696 |
using signed_take_bit_decr_length_iff by force+ |
72388 | 1697 |
|
1698 |
lemma signed_linorder: \<open>class.linorder word_sle word_sless\<close> |
|
1699 |
by (standard; transfer) (auto simp add: signed_take_bit_decr_length_iff) |
|
1700 |
||
1701 |
interpretation signed: linorder word_sle word_sless |
|
1702 |
by (fact signed_linorder) |
|
1703 |
||
1704 |
lemma word_sless_eq: |
|
1705 |
\<open>x <s y \<longleftrightarrow> x <=s y \<and> x \<noteq> y\<close> |
|
1706 |
by (fact signed.less_le) |
|
1707 |
||
72244 | 1708 |
lemma word_less_alt: "a < b \<longleftrightarrow> uint a < uint b" |
1709 |
by (fact word_less_def) |
|
1710 |
||
1711 |
lemma word_zero_le [simp]: "0 \<le> y" |
|
1712 |
for y :: "'a::len word" |
|
72388 | 1713 |
by (fact word_coorder.extremum) |
72244 | 1714 |
|
1715 |
lemma word_m1_ge [simp] : "word_pred 0 \<ge> y" (* FIXME: delete *) |
|
72488 | 1716 |
by transfer (simp add: take_bit_minus_one_eq_mask mask_eq_exp_minus_1 ) |
72244 | 1717 |
|
1718 |
lemma word_n1_ge [simp]: "y \<le> -1" |
|
1719 |
for y :: "'a::len word" |
|
1720 |
by (fact word_order.extremum) |
|
1721 |
||
1722 |
lemmas word_not_simps [simp] = |
|
1723 |
word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] |
|
1724 |
||
1725 |
lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> y" |
|
1726 |
for y :: "'a::len word" |
|
1727 |
by (simp add: less_le) |
|
1728 |
||
1729 |
lemmas word_gt_0_no [simp] = word_gt_0 [of "numeral y"] for y |
|
1730 |
||
1731 |
lemma word_sless_alt: "a <s b \<longleftrightarrow> sint a < sint b" |
|
1732 |
by transfer simp |
|
1733 |
||
1734 |
lemma word_le_nat_alt: "a \<le> b \<longleftrightarrow> unat a \<le> unat b" |
|
1735 |
by transfer (simp add: nat_le_eq_zle) |
|
1736 |
||
1737 |
lemma word_less_nat_alt: "a < b \<longleftrightarrow> unat a < unat b" |
|
1738 |
by transfer (auto simp add: less_le [of 0]) |
|
1739 |
||
1740 |
lemmas unat_mono = word_less_nat_alt [THEN iffD1] |
|
1741 |
||
1742 |
instance word :: (len) wellorder |
|
1743 |
proof |
|
1744 |
fix P :: "'a word \<Rightarrow> bool" and a |
|
1745 |
assume *: "(\<And>b. (\<And>a. a < b \<Longrightarrow> P a) \<Longrightarrow> P b)" |
|
1746 |
have "wf (measure unat)" .. |
|
1747 |
moreover have "{(a, b :: ('a::len) word). a < b} \<subseteq> measure unat" |
|
1748 |
by (auto simp add: word_less_nat_alt) |
|
1749 |
ultimately have "wf {(a, b :: ('a::len) word). a < b}" |
|
1750 |
by (rule wf_subset) |
|
1751 |
then show "P a" using * |
|
1752 |
by induction blast |
|
1753 |
qed |
|
1754 |
||
1755 |
lemma wi_less: |
|
1756 |
"(word_of_int n < (word_of_int m :: 'a::len word)) = |
|
1757 |
(n mod 2 ^ LENGTH('a) < m mod 2 ^ LENGTH('a))" |
|
1758 |
by transfer (simp add: take_bit_eq_mod) |
|
1759 |
||
1760 |
lemma wi_le: |
|
1761 |
"(word_of_int n \<le> (word_of_int m :: 'a::len word)) = |
|
1762 |
(n mod 2 ^ LENGTH('a) \<le> m mod 2 ^ LENGTH('a))" |
|
1763 |
by transfer (simp add: take_bit_eq_mod) |
|
1764 |
||
1765 |
||
1766 |
subsection \<open>Bit-wise operations\<close> |
|
1767 |
||
72262 | 1768 |
lemma uint_take_bit_eq: |
72079 | 1769 |
\<open>uint (take_bit n w) = take_bit n (uint w)\<close> |
1770 |
by transfer (simp add: ac_simps) |
|
1771 |
||
72227 | 1772 |
lemma take_bit_word_eq_self: |
1773 |
\<open>take_bit n w = w\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close> |
|
1774 |
using that by transfer simp |
|
1775 |
||
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
1776 |
lemma take_bit_length_eq [simp]: |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
1777 |
\<open>take_bit LENGTH('a) w = w\<close> for w :: \<open>'a::len word\<close> |
72227 | 1778 |
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
|
1779 |
|
71990 | 1780 |
lemma bit_word_of_int_iff: |
1781 |
\<open>bit (word_of_int k :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> bit k n\<close> |
|
1782 |
by transfer rule |
|
1783 |
||
1784 |
lemma bit_uint_iff: |
|
1785 |
\<open>bit (uint w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w n\<close> |
|
1786 |
for w :: \<open>'a::len word\<close> |
|
1787 |
by transfer (simp add: bit_take_bit_iff) |
|
1788 |
||
1789 |
lemma bit_sint_iff: |
|
1790 |
\<open>bit (sint w) n \<longleftrightarrow> n \<ge> LENGTH('a) \<and> bit w (LENGTH('a) - 1) \<or> bit w n\<close> |
|
1791 |
for w :: \<open>'a::len word\<close> |
|
72079 | 1792 |
by transfer (auto simp add: bit_signed_take_bit_iff min_def le_less not_less) |
71990 | 1793 |
|
1794 |
lemma bit_word_ucast_iff: |
|
1795 |
\<open>bit (ucast w :: 'b::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> n < LENGTH('b) \<and> bit w n\<close> |
|
1796 |
for w :: \<open>'a::len word\<close> |
|
72079 | 1797 |
by transfer (simp add: bit_take_bit_iff ac_simps) |
71990 | 1798 |
|
1799 |
lemma bit_word_scast_iff: |
|
1800 |
\<open>bit (scast w :: 'b::len word) n \<longleftrightarrow> |
|
1801 |
n < LENGTH('b) \<and> (bit w n \<or> LENGTH('a) \<le> n \<and> bit w (LENGTH('a) - Suc 0))\<close> |
|
1802 |
for w :: \<open>'a::len word\<close> |
|
72079 | 1803 |
by transfer (auto simp add: bit_signed_take_bit_iff le_less min_def) |
1804 |
||
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
1805 |
lemma bit_word_iff_drop_bit_and [code]: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
1806 |
\<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
|
1807 |
by (simp add: bit_iff_odd_drop_bit odd_iff_mod_2_eq_one and_one_eq) |
72079 | 1808 |
|
1809 |
lemma |
|
72262 | 1810 |
word_not_def: "NOT (a::'a::len word) = word_of_int (NOT (uint a))" |
65268 | 1811 |
and word_and_def: "(a::'a word) AND b = word_of_int (uint a AND uint b)" |
1812 |
and word_or_def: "(a::'a word) OR b = word_of_int (uint a OR uint b)" |
|
1813 |
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
|
1814 |
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
|
1815 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
1816 |
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
|
1817 |
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
|
1818 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
1819 |
lemma even_word_iff [code]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
1820 |
\<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
|
1821 |
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
|
1822 |
|
72079 | 1823 |
lemma map_bit_range_eq_if_take_bit_eq: |
1824 |
\<open>map (bit k) [0..<n] = map (bit l) [0..<n]\<close> |
|
1825 |
if \<open>take_bit n k = take_bit n l\<close> for k l :: int |
|
1826 |
using that proof (induction n arbitrary: k l) |
|
1827 |
case 0 |
|
1828 |
then show ?case |
|
1829 |
by simp |
|
1830 |
next |
|
1831 |
case (Suc n) |
|
1832 |
from Suc.prems have \<open>take_bit n (k div 2) = take_bit n (l div 2)\<close> |
|
1833 |
by (simp add: take_bit_Suc) |
|
1834 |
then have \<open>map (bit (k div 2)) [0..<n] = map (bit (l div 2)) [0..<n]\<close> |
|
1835 |
by (rule Suc.IH) |
|
1836 |
moreover have \<open>bit (r div 2) = bit r \<circ> Suc\<close> for r :: int |
|
1837 |
by (simp add: fun_eq_iff bit_Suc) |
|
1838 |
moreover from Suc.prems have \<open>even k \<longleftrightarrow> even l\<close> |
|
1839 |
by (auto simp add: take_bit_Suc elim!: evenE oddE) arith+ |
|
1840 |
ultimately show ?case |
|
1841 |
by (simp only: map_Suc_upt upt_conv_Cons flip: list.map_comp) simp |
|
1842 |
qed |
|
1843 |
||
72262 | 1844 |
lemma |
1845 |
take_bit_word_Bit0_eq [simp]: \<open>take_bit (numeral n) (numeral (num.Bit0 m) :: 'a::len word) |
|
1846 |
= 2 * take_bit (pred_numeral n) (numeral m)\<close> (is ?P) |
|
1847 |
and take_bit_word_Bit1_eq [simp]: \<open>take_bit (numeral n) (numeral (num.Bit1 m) :: 'a::len word) |
|
1848 |
= 1 + 2 * take_bit (pred_numeral n) (numeral m)\<close> (is ?Q) |
|
1849 |
and take_bit_word_minus_Bit0_eq [simp]: \<open>take_bit (numeral n) (- numeral (num.Bit0 m) :: 'a::len word) |
|
1850 |
= 2 * take_bit (pred_numeral n) (- numeral m)\<close> (is ?R) |
|
1851 |
and take_bit_word_minus_Bit1_eq [simp]: \<open>take_bit (numeral n) (- numeral (num.Bit1 m) :: 'a::len word) |
|
1852 |
= 1 + 2 * take_bit (pred_numeral n) (- numeral (Num.inc m))\<close> (is ?S) |
|
1853 |
proof - |
|
1854 |
define w :: \<open>'a::len word\<close> |
|
1855 |
where \<open>w = numeral m\<close> |
|
1856 |
moreover define q :: nat |
|
1857 |
where \<open>q = pred_numeral n\<close> |
|
1858 |
ultimately have num: |
|
1859 |
\<open>numeral m = w\<close> |
|
1860 |
\<open>numeral (num.Bit0 m) = 2 * w\<close> |
|
1861 |
\<open>numeral (num.Bit1 m) = 1 + 2 * w\<close> |
|
1862 |
\<open>numeral (Num.inc m) = 1 + w\<close> |
|
1863 |
\<open>pred_numeral n = q\<close> |
|
1864 |
\<open>numeral n = Suc q\<close> |
|
1865 |
by (simp_all only: w_def q_def numeral_Bit0 [of m] numeral_Bit1 [of m] ac_simps |
|
1866 |
numeral_inc numeral_eq_Suc flip: mult_2) |
|
1867 |
have even: \<open>take_bit (Suc q) (2 * w) = 2 * take_bit q w\<close> for w :: \<open>'a::len word\<close> |
|
1868 |
by (rule bit_word_eqI) |
|
1869 |
(auto simp add: bit_take_bit_iff bit_double_iff) |
|
1870 |
have odd: \<open>take_bit (Suc q) (1 + 2 * w) = 1 + 2 * take_bit q w\<close> for w :: \<open>'a::len word\<close> |
|
1871 |
by (rule bit_eqI) |
|
1872 |
(auto simp add: bit_take_bit_iff bit_double_iff even_bit_succ_iff) |
|
1873 |
show ?P |
|
1874 |
using even [of w] by (simp add: num) |
|
1875 |
show ?Q |
|
1876 |
using odd [of w] by (simp add: num) |
|
1877 |
show ?R |
|
1878 |
using even [of \<open>- w\<close>] by (simp add: num) |
|
1879 |
show ?S |
|
1880 |
using odd [of \<open>- (1 + w)\<close>] by (simp add: num) |
|
1881 |
qed |
|
1882 |
||
72079 | 1883 |
|
1884 |
subsection \<open>More shift operations\<close> |
|
1885 |
||
72388 | 1886 |
lift_definition signed_drop_bit :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a::len word\<close> |
1887 |
is \<open>\<lambda>n. drop_bit n \<circ> signed_take_bit (LENGTH('a) - Suc 0)\<close> |
|
1888 |
using signed_take_bit_decr_length_iff |
|
1889 |
by (simp add: take_bit_drop_bit) force |
|
1890 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
1891 |
lemma bit_signed_drop_bit_iff [bit_simps]: |
72388 | 1892 |
\<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> |
1893 |
for w :: \<open>'a::len word\<close> |
|
1894 |
apply transfer |
|
1895 |
apply (auto simp add: bit_drop_bit_eq bit_signed_take_bit_iff not_le min_def) |
|
1896 |
apply (metis add.commute le_antisym less_diff_conv less_eq_decr_length_iff) |
|
1897 |
apply (metis le_antisym less_eq_decr_length_iff) |
|
1898 |
done |
|
1899 |
||
72508 | 1900 |
lemma [code]: |
1901 |
\<open>Word.the_int (signed_drop_bit n w) = take_bit LENGTH('a) (drop_bit n (Word.the_signed_int w))\<close> |
|
1902 |
for w :: \<open>'a::len word\<close> |
|
1903 |
by transfer simp |
|
1904 |
||
73816 | 1905 |
lemma signed_drop_bit_of_0 [simp]: |
1906 |
\<open>signed_drop_bit n 0 = 0\<close> |
|
1907 |
by transfer simp |
|
1908 |
||
1909 |
lemma signed_drop_bit_of_minus_1 [simp]: |
|
1910 |
\<open>signed_drop_bit n (- 1) = - 1\<close> |
|
1911 |
by transfer simp |
|
1912 |
||
72488 | 1913 |
lemma signed_drop_bit_signed_drop_bit [simp]: |
1914 |
\<open>signed_drop_bit m (signed_drop_bit n w) = signed_drop_bit (m + n) w\<close> |
|
1915 |
for w :: \<open>'a::len word\<close> |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1916 |
proof (cases \<open>LENGTH('a)\<close>) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1917 |
case 0 |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1918 |
then show ?thesis |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1919 |
using len_not_eq_0 by blast |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1920 |
next |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1921 |
case (Suc n) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1922 |
then show ?thesis |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1923 |
by (force simp add: bit_signed_drop_bit_iff not_le less_diff_conv ac_simps intro!: bit_word_eqI) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1924 |
qed |
72488 | 1925 |
|
72388 | 1926 |
lemma signed_drop_bit_0 [simp]: |
1927 |
\<open>signed_drop_bit 0 w = w\<close> |
|
72488 | 1928 |
by transfer (simp add: take_bit_signed_take_bit) |
72388 | 1929 |
|
1930 |
lemma sint_signed_drop_bit_eq: |
|
1931 |
\<open>sint (signed_drop_bit n w) = drop_bit n (sint w)\<close> |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1932 |
proof (cases \<open>LENGTH('a) = 0 \<or> n=0\<close>) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1933 |
case False |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1934 |
then show ?thesis |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1935 |
apply simp |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1936 |
apply (rule bit_eqI) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1937 |
by (auto simp add: bit_sint_iff bit_drop_bit_eq bit_signed_drop_bit_iff dest: bit_imp_le_length) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1938 |
qed auto |
72388 | 1939 |
|
37660 | 1940 |
|
61799 | 1941 |
subsection \<open>Rotation\<close> |
37660 | 1942 |
|
72079 | 1943 |
lift_definition word_rotr :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close> |
1944 |
is \<open>\<lambda>n k. concat_bit (LENGTH('a) - n mod LENGTH('a)) |
|
1945 |
(drop_bit (n mod LENGTH('a)) (take_bit LENGTH('a) k)) |
|
1946 |
(take_bit (n mod LENGTH('a)) k)\<close> |
|
1947 |
subgoal for n k l |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1948 |
by (simp add: concat_bit_def nat_le_iff less_imp_le |
72079 | 1949 |
take_bit_tightened [of \<open>LENGTH('a)\<close> k l \<open>n mod LENGTH('a::len)\<close>]) |
1950 |
done |
|
1951 |
||
1952 |
lift_definition word_rotl :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close> |
|
1953 |
is \<open>\<lambda>n k. concat_bit (n mod LENGTH('a)) |
|
1954 |
(drop_bit (LENGTH('a) - n mod LENGTH('a)) (take_bit LENGTH('a) k)) |
|
1955 |
(take_bit (LENGTH('a) - n mod LENGTH('a)) k)\<close> |
|
1956 |
subgoal for n k l |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1957 |
by (simp add: concat_bit_def nat_le_iff less_imp_le |
72079 | 1958 |
take_bit_tightened [of \<open>LENGTH('a)\<close> k l \<open>LENGTH('a) - n mod LENGTH('a::len)\<close>]) |
1959 |
done |
|
1960 |
||
1961 |
lift_definition word_roti :: \<open>int \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close> |
|
1962 |
is \<open>\<lambda>r k. concat_bit (LENGTH('a) - nat (r mod int LENGTH('a))) |
|
1963 |
(drop_bit (nat (r mod int LENGTH('a))) (take_bit LENGTH('a) k)) |
|
1964 |
(take_bit (nat (r mod int LENGTH('a))) k)\<close> |
|
1965 |
subgoal for r k l |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
1966 |
by (simp add: concat_bit_def nat_le_iff less_imp_le |
72079 | 1967 |
take_bit_tightened [of \<open>LENGTH('a)\<close> k l \<open>nat (r mod int LENGTH('a::len))\<close>]) |
1968 |
done |
|
1969 |
||
1970 |
lemma word_rotl_eq_word_rotr [code]: |
|
1971 |
\<open>word_rotl n = (word_rotr (LENGTH('a) - n mod LENGTH('a)) :: 'a::len word \<Rightarrow> 'a word)\<close> |
|
1972 |
by (rule ext, cases \<open>n mod LENGTH('a) = 0\<close>; transfer) simp_all |
|
1973 |
||
1974 |
lemma word_roti_eq_word_rotr_word_rotl [code]: |
|
1975 |
\<open>word_roti i w = |
|
1976 |
(if i \<ge> 0 then word_rotr (nat i) w else word_rotl (nat (- i)) w)\<close> |
|
1977 |
proof (cases \<open>i \<ge> 0\<close>) |
|
1978 |
case True |
|
1979 |
moreover define n where \<open>n = nat i\<close> |
|
1980 |
ultimately have \<open>i = int n\<close> |
|
1981 |
by simp |
|
1982 |
moreover have \<open>word_roti (int n) = (word_rotr n :: _ \<Rightarrow> 'a word)\<close> |
|
1983 |
by (rule ext, transfer) (simp add: nat_mod_distrib) |
|
1984 |
ultimately show ?thesis |
|
1985 |
by simp |
|
1986 |
next |
|
1987 |
case False |
|
1988 |
moreover define n where \<open>n = nat (- i)\<close> |
|
1989 |
ultimately have \<open>i = - int n\<close> \<open>n > 0\<close> |
|
1990 |
by simp_all |
|
1991 |
moreover have \<open>word_roti (- int n) = (word_rotl n :: _ \<Rightarrow> 'a word)\<close> |
|
1992 |
by (rule ext, transfer) |
|
1993 |
(simp add: zmod_zminus1_eq_if flip: of_nat_mod of_nat_diff) |
|
1994 |
ultimately show ?thesis |
|
1995 |
by simp |
|
1996 |
qed |
|
1997 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
1998 |
lemma bit_word_rotr_iff [bit_simps]: |
72079 | 1999 |
\<open>bit (word_rotr m w) n \<longleftrightarrow> |
2000 |
n < LENGTH('a) \<and> bit w ((n + m) mod LENGTH('a))\<close> |
|
2001 |
for w :: \<open>'a::len word\<close> |
|
2002 |
proof transfer |
|
2003 |
fix k :: int and m n :: nat |
|
2004 |
define q where \<open>q = m mod LENGTH('a)\<close> |
|
2005 |
have \<open>q < LENGTH('a)\<close> |
|
2006 |
by (simp add: q_def) |
|
2007 |
then have \<open>q \<le> LENGTH('a)\<close> |
|
2008 |
by simp |
|
2009 |
have \<open>m mod LENGTH('a) = q\<close> |
|
2010 |
by (simp add: q_def) |
|
2011 |
moreover have \<open>(n + m) mod LENGTH('a) = (n + q) mod LENGTH('a)\<close> |
|
2012 |
by (subst mod_add_right_eq [symmetric]) (simp add: \<open>m mod LENGTH('a) = q\<close>) |
|
2013 |
moreover have \<open>n < LENGTH('a) \<and> |
|
2014 |
bit (concat_bit (LENGTH('a) - q) (drop_bit q (take_bit LENGTH('a) k)) (take_bit q k)) n \<longleftrightarrow> |
|
2015 |
n < LENGTH('a) \<and> bit k ((n + q) mod LENGTH('a))\<close> |
|
2016 |
using \<open>q < LENGTH('a)\<close> |
|
2017 |
by (cases \<open>q + n \<ge> LENGTH('a)\<close>) |
|
2018 |
(auto simp add: bit_concat_bit_iff bit_drop_bit_eq |
|
2019 |
bit_take_bit_iff le_mod_geq ac_simps) |
|
2020 |
ultimately show \<open>n < LENGTH('a) \<and> |
|
2021 |
bit (concat_bit (LENGTH('a) - m mod LENGTH('a)) |
|
2022 |
(drop_bit (m mod LENGTH('a)) (take_bit LENGTH('a) k)) |
|
2023 |
(take_bit (m mod LENGTH('a)) k)) n |
|
2024 |
\<longleftrightarrow> n < LENGTH('a) \<and> |
|
2025 |
(n + m) mod LENGTH('a) < LENGTH('a) \<and> |
|
2026 |
bit k ((n + m) mod LENGTH('a))\<close> |
|
2027 |
by simp |
|
2028 |
qed |
|
2029 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
2030 |
lemma bit_word_rotl_iff [bit_simps]: |
72079 | 2031 |
\<open>bit (word_rotl m w) n \<longleftrightarrow> |
2032 |
n < LENGTH('a) \<and> bit w ((n + (LENGTH('a) - m mod LENGTH('a))) mod LENGTH('a))\<close> |
|
2033 |
for w :: \<open>'a::len word\<close> |
|
2034 |
by (simp add: word_rotl_eq_word_rotr bit_word_rotr_iff) |
|
2035 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
2036 |
lemma bit_word_roti_iff [bit_simps]: |
72079 | 2037 |
\<open>bit (word_roti k w) n \<longleftrightarrow> |
2038 |
n < LENGTH('a) \<and> bit w (nat ((int n + k) mod int LENGTH('a)))\<close> |
|
2039 |
for w :: \<open>'a::len word\<close> |
|
2040 |
proof transfer |
|
2041 |
fix k l :: int and n :: nat |
|
2042 |
define m where \<open>m = nat (k mod int LENGTH('a))\<close> |
|
2043 |
have \<open>m < LENGTH('a)\<close> |
|
2044 |
by (simp add: nat_less_iff m_def) |
|
2045 |
then have \<open>m \<le> LENGTH('a)\<close> |
|
2046 |
by simp |
|
2047 |
have \<open>k mod int LENGTH('a) = int m\<close> |
|
2048 |
by (simp add: nat_less_iff m_def) |
|
2049 |
moreover have \<open>(int n + k) mod int LENGTH('a) = int ((n + m) mod LENGTH('a))\<close> |
|
2050 |
by (subst mod_add_right_eq [symmetric]) (simp add: of_nat_mod \<open>k mod int LENGTH('a) = int m\<close>) |
|
2051 |
moreover have \<open>n < LENGTH('a) \<and> |
|
2052 |
bit (concat_bit (LENGTH('a) - m) (drop_bit m (take_bit LENGTH('a) l)) (take_bit m l)) n \<longleftrightarrow> |
|
2053 |
n < LENGTH('a) \<and> bit l ((n + m) mod LENGTH('a))\<close> |
|
2054 |
using \<open>m < LENGTH('a)\<close> |
|
2055 |
by (cases \<open>m + n \<ge> LENGTH('a)\<close>) |
|
2056 |
(auto simp add: bit_concat_bit_iff bit_drop_bit_eq |
|
2057 |
bit_take_bit_iff nat_less_iff not_le not_less ac_simps |
|
2058 |
le_diff_conv le_mod_geq) |
|
2059 |
ultimately show \<open>n < LENGTH('a) |
|
2060 |
\<and> bit (concat_bit (LENGTH('a) - nat (k mod int LENGTH('a))) |
|
2061 |
(drop_bit (nat (k mod int LENGTH('a))) (take_bit LENGTH('a) l)) |
|
2062 |
(take_bit (nat (k mod int LENGTH('a))) l)) n \<longleftrightarrow> |
|
2063 |
n < LENGTH('a) |
|
2064 |
\<and> nat ((int n + k) mod int LENGTH('a)) < LENGTH('a) |
|
2065 |
\<and> bit l (nat ((int n + k) mod int LENGTH('a)))\<close> |
|
2066 |
by simp |
|
2067 |
qed |
|
2068 |
||
72262 | 2069 |
lemma uint_word_rotr_eq: |
72079 | 2070 |
\<open>uint (word_rotr n w) = concat_bit (LENGTH('a) - n mod LENGTH('a)) |
2071 |
(drop_bit (n mod LENGTH('a)) (uint w)) |
|
2072 |
(uint (take_bit (n mod LENGTH('a)) w))\<close> |
|
2073 |
for w :: \<open>'a::len word\<close> |
|
2074 |
apply transfer |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2075 |
by (simp add: min.absorb2 take_bit_concat_bit_eq) |
72079 | 2076 |
|
72262 | 2077 |
lemma [code]: |
2078 |
\<open>Word.the_int (word_rotr n w) = concat_bit (LENGTH('a) - n mod LENGTH('a)) |
|
2079 |
(drop_bit (n mod LENGTH('a)) (Word.the_int w)) |
|
2080 |
(Word.the_int (take_bit (n mod LENGTH('a)) w))\<close> |
|
2081 |
for w :: \<open>'a::len word\<close> |
|
2082 |
using uint_word_rotr_eq [of n w] by simp |
|
2083 |
||
72079 | 2084 |
|
61799 | 2085 |
subsection \<open>Split and cat operations\<close> |
37660 | 2086 |
|
72079 | 2087 |
lift_definition word_cat :: \<open>'a::len word \<Rightarrow> 'b::len word \<Rightarrow> 'c::len word\<close> |
2088 |
is \<open>\<lambda>k l. concat_bit LENGTH('b) l (take_bit LENGTH('a) k)\<close> |
|
2089 |
by (simp add: bit_eq_iff bit_concat_bit_iff bit_take_bit_iff) |
|
65268 | 2090 |
|
71990 | 2091 |
lemma word_cat_eq: |
2092 |
\<open>(word_cat v w :: 'c::len word) = push_bit LENGTH('b) (ucast v) + ucast w\<close> |
|
2093 |
for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close> |
|
72128 | 2094 |
by transfer (simp add: concat_bit_eq ac_simps) |
72079 | 2095 |
|
2096 |
lemma word_cat_eq' [code]: |
|
2097 |
\<open>word_cat a b = word_of_int (concat_bit LENGTH('b) (uint b) (uint a))\<close> |
|
2098 |
for a :: \<open>'a::len word\<close> and b :: \<open>'b::len word\<close> |
|
72488 | 2099 |
by transfer (simp add: concat_bit_take_bit_eq) |
71990 | 2100 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
2101 |
lemma bit_word_cat_iff [bit_simps]: |
71990 | 2102 |
\<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> |
2103 |
for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close> |
|
72079 | 2104 |
by transfer (simp add: bit_concat_bit_iff bit_take_bit_iff) |
71990 | 2105 |
|
72488 | 2106 |
definition word_split :: \<open>'a::len word \<Rightarrow> 'b::len word \<times> 'c::len word\<close> |
2107 |
where \<open>word_split w = |
|
2108 |
(ucast (drop_bit LENGTH('c) w) :: 'b::len word, ucast w :: 'c::len word)\<close> |
|
65268 | 2109 |
|
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
2110 |
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
|
2111 |
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
|
2112 |
|
37660 | 2113 |
|
72292 | 2114 |
subsection \<open>More on conversions\<close> |
2115 |
||
2116 |
lemma int_word_sint: |
|
2117 |
\<open>sint (word_of_int x :: 'a::len word) = (x + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) - 2 ^ (LENGTH('a) - 1)\<close> |
|
72488 | 2118 |
by transfer (simp flip: take_bit_eq_mod add: signed_take_bit_eq_take_bit_shift) |
46010 | 2119 |
|
72128 | 2120 |
lemma sint_sbintrunc': "sint (word_of_int bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) bin" |
72292 | 2121 |
by simp |
65268 | 2122 |
|
72488 | 2123 |
lemma uint_sint: "uint w = take_bit LENGTH('a) (sint w)" |
65328 | 2124 |
for w :: "'a::len word" |
72488 | 2125 |
by transfer (simp add: take_bit_signed_take_bit) |
65268 | 2126 |
|
72128 | 2127 |
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
|
2128 |
for w :: "'a::len word" |
72292 | 2129 |
by transfer (simp add: min_def) |
37660 | 2130 |
|
46057 | 2131 |
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
|
2132 |
"LENGTH('a::len) \<le> n \<Longrightarrow> |
72128 | 2133 |
word_of_int (take_bit n w) = (word_of_int w :: 'a word)" |
72292 | 2134 |
by transfer simp |
45805 | 2135 |
|
65268 | 2136 |
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
|
2137 |
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
|
2138 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2139 |
declare word_numeral_alt [symmetric, code_abbrev] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2140 |
|
65268 | 2141 |
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
|
2142 |
by (simp only: word_numeral_alt wi_hom_neg) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2143 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2144 |
declare word_neg_numeral_alt [symmetric, code_abbrev] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2145 |
|
45805 | 2146 |
lemma uint_bintrunc [simp]: |
65268 | 2147 |
"uint (numeral bin :: 'a word) = |
72128 | 2148 |
take_bit (LENGTH('a::len)) (numeral bin)" |
72292 | 2149 |
by transfer rule |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2150 |
|
65268 | 2151 |
lemma uint_bintrunc_neg [simp]: |
72128 | 2152 |
"uint (- numeral bin :: 'a word) = take_bit (LENGTH('a::len)) (- numeral bin)" |
72292 | 2153 |
by transfer rule |
37660 | 2154 |
|
45805 | 2155 |
lemma sint_sbintrunc [simp]: |
72128 | 2156 |
"sint (numeral bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) (numeral bin)" |
72292 | 2157 |
by transfer simp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2158 |
|
65268 | 2159 |
lemma sint_sbintrunc_neg [simp]: |
72128 | 2160 |
"sint (- numeral bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) (- numeral bin)" |
72292 | 2161 |
by transfer simp |
37660 | 2162 |
|
45805 | 2163 |
lemma unat_bintrunc [simp]: |
72128 | 2164 |
"unat (numeral bin :: 'a::len word) = nat (take_bit (LENGTH('a)) (numeral bin))" |
72079 | 2165 |
by transfer simp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2166 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2167 |
lemma unat_bintrunc_neg [simp]: |
72128 | 2168 |
"unat (- numeral bin :: 'a::len word) = nat (take_bit (LENGTH('a)) (- numeral bin))" |
72079 | 2169 |
by transfer simp |
37660 | 2170 |
|
65328 | 2171 |
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
|
2172 |
for v w :: "'a::len word" |
72292 | 2173 |
by transfer simp |
37660 | 2174 |
|
65268 | 2175 |
lemma uint_ge_0 [iff]: "0 \<le> uint x" |
72292 | 2176 |
by (fact unsigned_greater_eq) |
45805 | 2177 |
|
70185 | 2178 |
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
|
2179 |
for x :: "'a::len word" |
72292 | 2180 |
by (fact unsigned_less) |
45805 | 2181 |
|
70185 | 2182 |
lemma sint_ge: "- (2 ^ (LENGTH('a) - 1)) \<le> sint x" |
65268 | 2183 |
for x :: "'a::len word" |
72292 | 2184 |
using sint_greater_eq [of x] by simp |
45805 | 2185 |
|
70185 | 2186 |
lemma sint_lt: "sint x < 2 ^ (LENGTH('a) - 1)" |
65268 | 2187 |
for x :: "'a::len word" |
72292 | 2188 |
using sint_less [of x] by simp |
37660 | 2189 |
|
70185 | 2190 |
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
|
2191 |
for x :: "'a::len word" |
45805 | 2192 |
by (simp only: diff_less_0_iff_less uint_lt2p) |
2193 |
||
70185 | 2194 |
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
|
2195 |
for x :: "'a::len word" |
45805 | 2196 |
by (simp only: not_le uint_m2p_neg) |
37660 | 2197 |
|
70185 | 2198 |
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
|
2199 |
for w :: "'a::len word" |
72488 | 2200 |
using uint_bounded [of w] by (rule less_le_trans) simp |
37660 | 2201 |
|
45805 | 2202 |
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
|
2203 |
by (fact uint_ge_0 [THEN leD, THEN antisym_conv1]) |
37660 | 2204 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
2205 |
lemma uint_nat: "uint w = int (unat w)" |
72079 | 2206 |
by transfer simp |
65268 | 2207 |
|
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 |
lemma uint_numeral: "uint (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)" |
72292 | 2209 |
by (simp flip: take_bit_eq_mod add: of_nat_take_bit) |
65268 | 2210 |
|
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
|
2211 |
lemma uint_neg_numeral: "uint (- numeral b :: 'a::len word) = - numeral b mod 2 ^ LENGTH('a)" |
72292 | 2212 |
by (simp flip: take_bit_eq_mod add: of_nat_take_bit) |
65268 | 2213 |
|
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
|
2214 |
lemma unat_numeral: "unat (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)" |
72079 | 2215 |
by transfer (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq) |
37660 | 2216 |
|
65268 | 2217 |
lemma sint_numeral: |
2218 |
"sint (numeral b :: 'a::len word) = |
|
72292 | 2219 |
(numeral b + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) - 2 ^ (LENGTH('a) - 1)" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2220 |
by (metis int_word_sint word_numeral_alt) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2221 |
|
65268 | 2222 |
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0" |
72292 | 2223 |
by (fact of_int_0) |
45958 | 2224 |
|
65268 | 2225 |
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1" |
72292 | 2226 |
by (fact of_int_1) |
45958 | 2227 |
|
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2228 |
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
|
2229 |
by (simp add: wi_hom_syms) |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
2230 |
|
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 |
lemma word_of_int_numeral [simp] : "(word_of_int (numeral bin) :: 'a::len word) = numeral bin" |
72292 | 2232 |
by (fact of_int_numeral) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2233 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2234 |
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
|
2235 |
"(word_of_int (- numeral bin) :: 'a::len word) = - numeral bin" |
72292 | 2236 |
by (fact of_int_neg_numeral) |
65268 | 2237 |
|
2238 |
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
|
2239 |
"word_int_case f (word_of_int i :: 'b word) = f (i mod 2 ^ LENGTH('b::len))" |
72079 | 2240 |
by transfer (simp add: take_bit_eq_mod) |
65268 | 2241 |
|
2242 |
lemma word_int_split: |
|
2243 |
"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
|
2244 |
(\<forall>i. x = (word_of_int i :: 'b::len word) \<and> 0 \<le> i \<and> i < 2 ^ LENGTH('b) \<longrightarrow> P (f i))" |
72079 | 2245 |
by transfer (auto simp add: take_bit_eq_mod) |
65268 | 2246 |
|
2247 |
lemma word_int_split_asm: |
|
2248 |
"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
|
2249 |
(\<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 | 2250 |
by transfer (auto simp add: take_bit_eq_mod) |
45805 | 2251 |
|
65268 | 2252 |
lemma uint_range_size: "0 \<le> uint w \<and> uint w < 2 ^ size w" |
72292 | 2253 |
by transfer simp |
37660 | 2254 |
|
65268 | 2255 |
lemma sint_range_size: "- (2 ^ (size w - Suc 0)) \<le> sint w \<and> sint w < 2 ^ (size w - Suc 0)" |
72488 | 2256 |
by (simp add: word_size sint_greater_eq sint_less) |
37660 | 2257 |
|
65268 | 2258 |
lemma sint_above_size: "2 ^ (size w - 1) \<le> x \<Longrightarrow> sint w < x" |
2259 |
for w :: "'a::len word" |
|
45805 | 2260 |
unfolding word_size by (rule less_le_trans [OF sint_lt]) |
2261 |
||
65268 | 2262 |
lemma sint_below_size: "x \<le> - (2 ^ (size w - 1)) \<Longrightarrow> x \<le> sint w" |
2263 |
for w :: "'a::len word" |
|
45805 | 2264 |
unfolding word_size by (rule order_trans [OF _ sint_ge]) |
37660 | 2265 |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2266 |
|
61799 | 2267 |
subsection \<open>Testing bits\<close> |
46010 | 2268 |
|
72488 | 2269 |
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
|
2270 |
for w :: "'a::len word" |
72292 | 2271 |
by transfer (simp add: bit_take_bit_iff) |
37660 | 2272 |
|
46057 | 2273 |
lemma bin_nth_sint: |
70185 | 2274 |
"LENGTH('a) \<le> n \<Longrightarrow> |
72488 | 2275 |
bit (sint w) n = bit (sint w) (LENGTH('a) - 1)" |
65268 | 2276 |
for w :: "'a::len word" |
72292 | 2277 |
by (transfer fixing: n) (simp add: bit_signed_take_bit_iff le_diff_conv min_def) |
37660 | 2278 |
|
2279 |
lemma num_of_bintr': |
|
72128 | 2280 |
"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
|
2281 |
numeral a = (numeral b :: 'a word)" |
72292 | 2282 |
proof (transfer fixing: a b) |
2283 |
assume \<open>take_bit LENGTH('a) (numeral a :: int) = numeral b\<close> |
|
2284 |
then have \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (numeral a :: int)) = take_bit LENGTH('a) (numeral b)\<close> |
|
2285 |
by simp |
|
2286 |
then show \<open>take_bit LENGTH('a) (numeral a :: int) = take_bit LENGTH('a) (numeral b)\<close> |
|
2287 |
by simp |
|
2288 |
qed |
|
37660 | 2289 |
|
2290 |
lemma num_of_sbintr': |
|
72241 | 2291 |
"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
|
2292 |
numeral a = (numeral b :: 'a word)" |
72292 | 2293 |
proof (transfer fixing: a b) |
2294 |
assume \<open>signed_take_bit (LENGTH('a) - 1) (numeral a :: int) = numeral b\<close> |
|
2295 |
then have \<open>take_bit LENGTH('a) (signed_take_bit (LENGTH('a) - 1) (numeral a :: int)) = take_bit LENGTH('a) (numeral b)\<close> |
|
2296 |
by simp |
|
2297 |
then show \<open>take_bit LENGTH('a) (numeral a :: int) = take_bit LENGTH('a) (numeral b)\<close> |
|
72488 | 2298 |
by (simp add: take_bit_signed_take_bit) |
72292 | 2299 |
qed |
2300 |
||
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
2301 |
lemma num_abs_bintr: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2302 |
"(numeral x :: 'a word) = |
72128 | 2303 |
word_of_int (take_bit (LENGTH('a::len)) (numeral x))" |
72292 | 2304 |
by transfer simp |
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
2305 |
|
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
2306 |
lemma num_abs_sbintr: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2307 |
"(numeral x :: 'a word) = |
72128 | 2308 |
word_of_int (signed_take_bit (LENGTH('a::len) - 1) (numeral x))" |
72488 | 2309 |
by transfer (simp add: take_bit_signed_take_bit) |
46962
5bdcdb28be83
make more word theorems respect int/bin distinction
huffman
parents:
46656
diff
changeset
|
2310 |
|
67408 | 2311 |
text \<open> |
2312 |
\<open>cast\<close> -- note, no arg for new length, as it's determined by type of result, |
|
2313 |
thus in \<open>cast w = w\<close>, the type means cast to length of \<open>w\<close>! |
|
2314 |
\<close> |
|
37660 | 2315 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
2316 |
lemma bit_ucast_iff: |
72079 | 2317 |
\<open>bit (ucast a :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a::len) \<and> Parity.bit a n\<close> |
2318 |
by transfer (simp add: bit_take_bit_iff) |
|
2319 |
||
2320 |
lemma ucast_id [simp]: "ucast w = w" |
|
2321 |
by transfer simp |
|
2322 |
||
2323 |
lemma scast_id [simp]: "scast w = w" |
|
72488 | 2324 |
by transfer (simp add: take_bit_signed_take_bit) |
37660 | 2325 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
2326 |
lemma ucast_mask_eq: |
72082 | 2327 |
\<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
|
2328 |
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
|
2329 |
|
67408 | 2330 |
\<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
|
2331 |
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
|
2332 |
"ucast (numeral w :: 'a::len word) = |
72128 | 2333 |
word_of_int (take_bit (LENGTH('a)) (numeral w))" |
72079 | 2334 |
by transfer simp |
65268 | 2335 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2336 |
(* TODO: neg_numeral *) |
37660 | 2337 |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
46000
diff
changeset
|
2338 |
lemma scast_sbintr [simp]: |
65268 | 2339 |
"scast (numeral w ::'a::len word) = |
72128 | 2340 |
word_of_int (signed_take_bit (LENGTH('a) - Suc 0) (numeral w))" |
72079 | 2341 |
by transfer simp |
37660 | 2342 |
|
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
|
2343 |
lemma source_size: "source_size (c::'a::len word \<Rightarrow> _) = LENGTH('a)" |
72079 | 2344 |
by transfer simp |
46011 | 2345 |
|
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
|
2346 |
lemma target_size: "target_size (c::_ \<Rightarrow> 'b::len word) = LENGTH('b)" |
72079 | 2347 |
by transfer simp |
46011 | 2348 |
|
70185 | 2349 |
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
|
2350 |
for c :: "'a::len word \<Rightarrow> 'b::len word" |
72079 | 2351 |
by transfer simp |
65268 | 2352 |
|
70185 | 2353 |
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
|
2354 |
for c :: "'a::len word \<Rightarrow> 'b::len word" |
72079 | 2355 |
by transfer simp |
2356 |
||
2357 |
lemma is_up_down: |
|
2358 |
\<open>is_up c \<longleftrightarrow> is_down d\<close> |
|
2359 |
for c :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
2360 |
and d :: \<open>'b::len word \<Rightarrow> 'a::len word\<close> |
|
2361 |
by transfer simp |
|
2362 |
||
2363 |
context |
|
2364 |
fixes dummy_types :: \<open>'a::len \<times> 'b::len\<close> |
|
2365 |
begin |
|
2366 |
||
2367 |
private abbreviation (input) UCAST :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
2368 |
where \<open>UCAST == ucast\<close> |
|
2369 |
||
2370 |
private abbreviation (input) SCAST :: \<open>'a::len word \<Rightarrow> 'b::len word\<close> |
|
2371 |
where \<open>SCAST == scast\<close> |
|
2372 |
||
2373 |
lemma down_cast_same: |
|
2374 |
\<open>UCAST = scast\<close> if \<open>is_down UCAST\<close> |
|
2375 |
by (rule ext, use that in transfer) (simp add: take_bit_signed_take_bit) |
|
2376 |
||
2377 |
lemma sint_up_scast: |
|
2378 |
\<open>sint (SCAST w) = sint w\<close> if \<open>is_up SCAST\<close> |
|
2379 |
using that by transfer (simp add: min_def Suc_leI le_diff_iff) |
|
2380 |
||
2381 |
lemma uint_up_ucast: |
|
2382 |
\<open>uint (UCAST w) = uint w\<close> if \<open>is_up UCAST\<close> |
|
2383 |
using that by transfer (simp add: min_def) |
|
2384 |
||
2385 |
lemma ucast_up_ucast: |
|
2386 |
\<open>ucast (UCAST w) = ucast w\<close> if \<open>is_up UCAST\<close> |
|
2387 |
using that by transfer (simp add: ac_simps) |
|
2388 |
||
2389 |
lemma ucast_up_ucast_id: |
|
2390 |
\<open>ucast (UCAST w) = w\<close> if \<open>is_up UCAST\<close> |
|
2391 |
using that by (simp add: ucast_up_ucast) |
|
2392 |
||
2393 |
lemma scast_up_scast: |
|
2394 |
\<open>scast (SCAST w) = scast w\<close> if \<open>is_up SCAST\<close> |
|
2395 |
using that by transfer (simp add: ac_simps) |
|
2396 |
||
2397 |
lemma scast_up_scast_id: |
|
2398 |
\<open>scast (SCAST w) = w\<close> if \<open>is_up SCAST\<close> |
|
2399 |
using that by (simp add: scast_up_scast) |
|
2400 |
||
2401 |
lemma isduu: |
|
2402 |
\<open>is_up UCAST\<close> if \<open>is_down d\<close> |
|
2403 |
for d :: \<open>'b word \<Rightarrow> 'a word\<close> |
|
2404 |
using that is_up_down [of UCAST d] by simp |
|
2405 |
||
2406 |
lemma isdus: |
|
2407 |
\<open>is_up SCAST\<close> if \<open>is_down d\<close> |
|
2408 |
for d :: \<open>'b word \<Rightarrow> 'a word\<close> |
|
2409 |
using that is_up_down [of SCAST d] by simp |
|
2410 |
||
37660 | 2411 |
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id] |
72079 | 2412 |
lemmas scast_down_scast_id = isdus [THEN scast_up_scast_id] |
37660 | 2413 |
|
2414 |
lemma up_ucast_surj: |
|
72079 | 2415 |
\<open>surj (ucast :: 'b word \<Rightarrow> 'a word)\<close> if \<open>is_up UCAST\<close> |
2416 |
by (rule surjI) (use that in \<open>rule ucast_up_ucast_id\<close>) |
|
37660 | 2417 |
|
2418 |
lemma up_scast_surj: |
|
72079 | 2419 |
\<open>surj (scast :: 'b word \<Rightarrow> 'a word)\<close> if \<open>is_up SCAST\<close> |
2420 |
by (rule surjI) (use that in \<open>rule scast_up_scast_id\<close>) |
|
37660 | 2421 |
|
2422 |
lemma down_ucast_inj: |
|
72079 | 2423 |
\<open>inj_on UCAST A\<close> if \<open>is_down (ucast :: 'b word \<Rightarrow> 'a word)\<close> |
2424 |
by (rule inj_on_inverseI) (use that in \<open>rule ucast_down_ucast_id\<close>) |
|
2425 |
||
2426 |
lemma down_scast_inj: |
|
2427 |
\<open>inj_on SCAST A\<close> if \<open>is_down (scast :: 'b word \<Rightarrow> 'a word)\<close> |
|
2428 |
by (rule inj_on_inverseI) (use that in \<open>rule scast_down_scast_id\<close>) |
|
2429 |
||
2430 |
lemma ucast_down_wi: |
|
2431 |
\<open>UCAST (word_of_int x) = word_of_int x\<close> if \<open>is_down UCAST\<close> |
|
2432 |
using that by transfer simp |
|
2433 |
||
2434 |
lemma ucast_down_no: |
|
2435 |
\<open>UCAST (numeral bin) = numeral bin\<close> if \<open>is_down UCAST\<close> |
|
2436 |
using that by transfer simp |
|
2437 |
||
2438 |
end |
|
37660 | 2439 |
|
2440 |
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def |
|
2441 |
||
72000 | 2442 |
lemma bit_last_iff: |
2443 |
\<open>bit w (LENGTH('a) - Suc 0) \<longleftrightarrow> sint w < 0\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
2444 |
for w :: \<open>'a::len word\<close> |
|
2445 |
proof - |
|
2446 |
have \<open>?P \<longleftrightarrow> bit (uint w) (LENGTH('a) - Suc 0)\<close> |
|
2447 |
by (simp add: bit_uint_iff) |
|
2448 |
also have \<open>\<dots> \<longleftrightarrow> ?Q\<close> |
|
72010 | 2449 |
by (simp add: sint_uint) |
72000 | 2450 |
finally show ?thesis . |
2451 |
qed |
|
2452 |
||
2453 |
lemma drop_bit_eq_zero_iff_not_bit_last: |
|
2454 |
\<open>drop_bit (LENGTH('a) - Suc 0) w = 0 \<longleftrightarrow> \<not> bit w (LENGTH('a) - Suc 0)\<close> |
|
2455 |
for w :: "'a::len word" |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2456 |
proof (cases \<open>LENGTH('a)\<close>) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2457 |
case (Suc n) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2458 |
then show ?thesis |
72000 | 2459 |
apply transfer |
2460 |
apply (simp add: take_bit_drop_bit) |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2461 |
by (simp add: bit_iff_odd_drop_bit drop_bit_take_bit min.absorb2 odd_iff_mod_2_eq_one) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2462 |
qed auto |
72000 | 2463 |
|
37660 | 2464 |
|
61799 | 2465 |
subsection \<open>Word Arithmetic\<close> |
37660 | 2466 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
2467 |
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
|
2468 |
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
|
2469 |
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
|
2470 |
lemmas word_le_no [simp] = word_le_def [of "numeral a" "numeral b"] for a b |
72079 | 2471 |
lemmas word_sless_no [simp] = word_sless_eq [of "numeral a" "numeral b"] for a b |
2472 |
lemmas word_sle_no [simp] = word_sle_eq [of "numeral a" "numeral b"] for a b |
|
37660 | 2473 |
|
65268 | 2474 |
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
|
2475 |
for v w :: "'a::len word" |
72079 | 2476 |
by (unfold word_size) simp |
37660 | 2477 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2478 |
lemmas size_0_same = size_0_same' [unfolded word_size] |
37660 | 2479 |
|
2480 |
lemmas unat_eq_0 = unat_0_iff |
|
2481 |
lemmas unat_eq_zero = unat_0_iff |
|
2482 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2483 |
|
61799 | 2484 |
subsection \<open>Transferring goals from words to ints\<close> |
37660 | 2485 |
|
65268 | 2486 |
lemma word_ths: |
2487 |
shows word_succ_p1: "word_succ a = a + 1" |
|
2488 |
and word_pred_m1: "word_pred a = a - 1" |
|
2489 |
and word_pred_succ: "word_pred (word_succ a) = a" |
|
2490 |
and word_succ_pred: "word_succ (word_pred a) = a" |
|
2491 |
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
|
2492 |
by (transfer, simp add: algebra_simps)+ |
37660 | 2493 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2494 |
lemma uint_cong: "x = y \<Longrightarrow> uint x = uint y" |
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2495 |
by simp |
37660 | 2496 |
|
55818 | 2497 |
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
|
2498 |
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
|
2499 |
shows "uint (a + b) = (uint a + uint b) mod 2 ^ LENGTH('a::len)" |
70185 | 2500 |
and "uint (a - b) = (uint a - uint b) mod 2 ^ LENGTH('a)" |
2501 |
and "uint (a * b) = uint a * uint b mod 2 ^ LENGTH('a)" |
|
2502 |
and "uint (- a) = - uint a mod 2 ^ LENGTH('a)" |
|
2503 |
and "uint (word_succ a) = (uint a + 1) mod 2 ^ LENGTH('a)" |
|
2504 |
and "uint (word_pred a) = (uint a - 1) mod 2 ^ LENGTH('a)" |
|
2505 |
and "uint (0 :: 'a word) = 0 mod 2 ^ LENGTH('a)" |
|
2506 |
and "uint (1 :: 'a word) = 1 mod 2 ^ LENGTH('a)" |
|
72262 | 2507 |
by (simp_all only: word_arith_wis uint_word_of_int_eq flip: take_bit_eq_mod) |
55818 | 2508 |
|
2509 |
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
|
2510 |
fixes a b :: "'a::len word" |
72128 | 2511 |
shows "uint (a + b) = take_bit (LENGTH('a)) (uint a + uint b)" |
2512 |
and "uint (a - b) = take_bit (LENGTH('a)) (uint a - uint b)" |
|
2513 |
and "uint (a * b) = take_bit (LENGTH('a)) (uint a * uint b)" |
|
2514 |
and "uint (- a) = take_bit (LENGTH('a)) (- uint a)" |
|
2515 |
and "uint (word_succ a) = take_bit (LENGTH('a)) (uint a + 1)" |
|
2516 |
and "uint (word_pred a) = take_bit (LENGTH('a)) (uint a - 1)" |
|
2517 |
and "uint (0 :: 'a word) = take_bit (LENGTH('a)) 0" |
|
2518 |
and "uint (1 :: 'a word) = take_bit (LENGTH('a)) 1" |
|
2519 |
by (simp_all add: uint_word_ariths take_bit_eq_mod) |
|
55818 | 2520 |
|
2521 |
lemma sint_word_ariths: |
|
2522 |
fixes a b :: "'a::len word" |
|
72128 | 2523 |
shows "sint (a + b) = signed_take_bit (LENGTH('a) - 1) (sint a + sint b)" |
2524 |
and "sint (a - b) = signed_take_bit (LENGTH('a) - 1) (sint a - sint b)" |
|
2525 |
and "sint (a * b) = signed_take_bit (LENGTH('a) - 1) (sint a * sint b)" |
|
2526 |
and "sint (- a) = signed_take_bit (LENGTH('a) - 1) (- sint a)" |
|
2527 |
and "sint (word_succ a) = signed_take_bit (LENGTH('a) - 1) (sint a + 1)" |
|
2528 |
and "sint (word_pred a) = signed_take_bit (LENGTH('a) - 1) (sint a - 1)" |
|
2529 |
and "sint (0 :: 'a word) = signed_take_bit (LENGTH('a) - 1) 0" |
|
2530 |
and "sint (1 :: 'a word) = signed_take_bit (LENGTH('a) - 1) 1" |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2531 |
subgoal |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2532 |
by transfer (simp add: signed_take_bit_add) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2533 |
subgoal |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2534 |
by transfer (simp add: signed_take_bit_diff) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2535 |
subgoal |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2536 |
by transfer (simp add: signed_take_bit_mult) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2537 |
subgoal |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2538 |
by transfer (simp add: signed_take_bit_minus) |
72488 | 2539 |
apply (metis of_int_sint scast_id sint_sbintrunc' wi_hom_succ) |
2540 |
apply (metis of_int_sint scast_id sint_sbintrunc' wi_hom_pred) |
|
2541 |
apply (simp_all add: sint_uint) |
|
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
2542 |
done |
45604 | 2543 |
|
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
58061
diff
changeset
|
2544 |
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
|
2545 |
unfolding word_pred_m1 by simp |
37660 | 2546 |
|
2547 |
lemma succ_pred_no [simp]: |
|
65268 | 2548 |
"word_succ (numeral w) = numeral w + 1" |
2549 |
"word_pred (numeral w) = numeral w - 1" |
|
2550 |
"word_succ (- numeral w) = - numeral w + 1" |
|
2551 |
"word_pred (- numeral w) = - numeral w - 1" |
|
2552 |
by (simp_all add: word_succ_p1 word_pred_m1) |
|
2553 |
||
2554 |
lemma word_sp_01 [simp]: |
|
2555 |
"word_succ (- 1) = 0 \<and> word_succ 0 = 1 \<and> word_pred 0 = - 1 \<and> word_pred 1 = 0" |
|
2556 |
by (simp_all add: word_succ_p1 word_pred_m1) |
|
37660 | 2557 |
|
67408 | 2558 |
\<comment> \<open>alternative approach to lifting arithmetic equalities\<close> |
65268 | 2559 |
lemma word_of_int_Ex: "\<exists>y. x = word_of_int y" |
37660 | 2560 |
by (rule_tac x="uint x" in exI) simp |
2561 |
||
2562 |
||
61799 | 2563 |
subsection \<open>Order on fixed-length words\<close> |
37660 | 2564 |
|
72262 | 2565 |
lift_definition udvd :: \<open>'a::len word \<Rightarrow> 'a::len word \<Rightarrow> bool\<close> (infixl \<open>udvd\<close> 50) |
2566 |
is \<open>\<lambda>k l. take_bit LENGTH('a) k dvd take_bit LENGTH('a) l\<close> by simp |
|
2567 |
||
2568 |
lemma udvd_iff_dvd: |
|
2569 |
\<open>x udvd y \<longleftrightarrow> unat x dvd unat y\<close> |
|
2570 |
by transfer (simp add: nat_dvd_iff) |
|
2571 |
||
2572 |
lemma udvd_iff_dvd_int: |
|
2573 |
\<open>v udvd w \<longleftrightarrow> uint v dvd uint w\<close> |
|
2574 |
by transfer rule |
|
2575 |
||
2576 |
lemma udvdI [intro]: |
|
2577 |
\<open>v udvd w\<close> if \<open>unat w = unat v * unat u\<close> |
|
2578 |
proof - |
|
2579 |
from that have \<open>unat v dvd unat w\<close> .. |
|
2580 |
then show ?thesis |
|
2581 |
by (simp add: udvd_iff_dvd) |
|
2582 |
qed |
|
2583 |
||
2584 |
lemma udvdE [elim]: |
|
2585 |
fixes v w :: \<open>'a::len word\<close> |
|
2586 |
assumes \<open>v udvd w\<close> |
|
2587 |
obtains u :: \<open>'a word\<close> where \<open>unat w = unat v * unat u\<close> |
|
2588 |
proof (cases \<open>v = 0\<close>) |
|
2589 |
case True |
|
2590 |
moreover from True \<open>v udvd w\<close> have \<open>w = 0\<close> |
|
2591 |
by transfer simp |
|
2592 |
ultimately show thesis |
|
2593 |
using that by simp |
|
2594 |
next |
|
2595 |
case False |
|
2596 |
then have \<open>unat v > 0\<close> |
|
2597 |
by (simp add: unat_gt_0) |
|
2598 |
from \<open>v udvd w\<close> have \<open>unat v dvd unat w\<close> |
|
2599 |
by (simp add: udvd_iff_dvd) |
|
2600 |
then obtain n where \<open>unat w = unat v * n\<close> .. |
|
2601 |
moreover have \<open>n < 2 ^ LENGTH('a)\<close> |
|
2602 |
proof (rule ccontr) |
|
2603 |
assume \<open>\<not> n < 2 ^ LENGTH('a)\<close> |
|
2604 |
then have \<open>n \<ge> 2 ^ LENGTH('a)\<close> |
|
2605 |
by (simp add: not_le) |
|
2606 |
then have \<open>unat v * n \<ge> 2 ^ LENGTH('a)\<close> |
|
2607 |
using \<open>unat v > 0\<close> mult_le_mono [of 1 \<open>unat v\<close> \<open>2 ^ LENGTH('a)\<close> n] |
|
2608 |
by simp |
|
72292 | 2609 |
with \<open>unat w = unat v * n\<close> |
2610 |
have \<open>unat w \<ge> 2 ^ LENGTH('a)\<close> |
|
72262 | 2611 |
by simp |
72292 | 2612 |
with unsigned_less [of w, where ?'a = nat] show False |
2613 |
by linarith |
|
72262 | 2614 |
qed |
2615 |
ultimately have \<open>unat w = unat v * unat (word_of_nat n :: 'a word)\<close> |
|
2616 |
by (auto simp add: take_bit_nat_eq_self_iff intro: sym) |
|
2617 |
with that show thesis . |
|
2618 |
qed |
|
2619 |
||
2620 |
lemma udvd_imp_mod_eq_0: |
|
2621 |
\<open>w mod v = 0\<close> if \<open>v udvd w\<close> |
|
2622 |
using that by transfer simp |
|
2623 |
||
2624 |
lemma mod_eq_0_imp_udvd [intro?]: |
|
2625 |
\<open>v udvd w\<close> if \<open>w mod v = 0\<close> |
|
2626 |
proof - |
|
2627 |
from that have \<open>unat (w mod v) = unat 0\<close> |
|
2628 |
by simp |
|
2629 |
then have \<open>unat w mod unat v = 0\<close> |
|
2630 |
by (simp add: unat_mod_distrib) |
|
2631 |
then have \<open>unat v dvd unat w\<close> .. |
|
2632 |
then show ?thesis |
|
2633 |
by (simp add: udvd_iff_dvd) |
|
2634 |
qed |
|
2635 |
||
72280 | 2636 |
lemma udvd_imp_dvd: |
2637 |
\<open>v dvd w\<close> if \<open>v udvd w\<close> for v w :: \<open>'a::len word\<close> |
|
2638 |
proof - |
|
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2639 |
from that obtain u :: \<open>'a word\<close> where \<open>unat w = unat v * unat u\<close> .. |
72280 | 2640 |
then have \<open>(word_of_nat (unat w) :: 'a word) = word_of_nat (unat v * unat u)\<close> |
2641 |
by simp |
|
2642 |
then have \<open>w = v * u\<close> |
|
2643 |
by simp |
|
2644 |
then show \<open>v dvd w\<close> .. |
|
2645 |
qed |
|
2646 |
||
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2647 |
lemma exp_dvd_iff_exp_udvd: |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2648 |
\<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
|
2649 |
proof |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2650 |
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
|
2651 |
by (rule udvd_imp_dvd) |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2652 |
next |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2653 |
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
|
2654 |
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
|
2655 |
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
|
2656 |
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
|
2657 |
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
|
2658 |
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
|
2659 |
qed |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72280
diff
changeset
|
2660 |
|
72262 | 2661 |
lemma udvd_nat_alt: |
2662 |
\<open>a udvd b \<longleftrightarrow> (\<exists>n. unat b = n * unat a)\<close> |
|
2663 |
by (auto simp add: udvd_iff_dvd) |
|
2664 |
||
2665 |
lemma udvd_unfold_int: |
|
2666 |
\<open>a udvd b \<longleftrightarrow> (\<exists>n\<ge>0. uint b = n * uint a)\<close> |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2667 |
unfolding udvd_iff_dvd_int |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2668 |
by (metis dvd_div_mult_self dvd_triv_right uint_div_distrib uint_ge_0) |
37660 | 2669 |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2670 |
lemma unat_minus_one: |
72079 | 2671 |
\<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
|
2672 |
proof - |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2673 |
have "0 \<le> uint w" by (fact uint_nonnegative) |
72079 | 2674 |
moreover from that have "0 \<noteq> uint w" |
65328 | 2675 |
by (simp add: uint_0_iff) |
2676 |
ultimately have "1 \<le> uint w" |
|
2677 |
by arith |
|
70185 | 2678 |
from uint_lt2p [of w] have "uint w - 1 < 2 ^ LENGTH('a)" |
65328 | 2679 |
by arith |
70185 | 2680 |
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
|
2681 |
by (auto intro: mod_pos_pos_trivial) |
70185 | 2682 |
with \<open>1 \<le> uint w\<close> have "nat ((uint w - 1) mod 2 ^ LENGTH('a)) = nat (uint w) - 1" |
72079 | 2683 |
by (auto simp del: nat_uint_eq) |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2684 |
then show ?thesis |
72292 | 2685 |
by (simp only: unat_eq_nat_uint word_arith_wis mod_diff_right_eq) |
2686 |
(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
|
2687 |
qed |
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2688 |
|
65328 | 2689 |
lemma measure_unat: "p \<noteq> 0 \<Longrightarrow> unat (p - 1) < unat p" |
37660 | 2690 |
by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric]) |
65268 | 2691 |
|
45604 | 2692 |
lemmas uint_add_ge0 [simp] = add_nonneg_nonneg [OF uint_ge_0 uint_ge_0] |
2693 |
lemmas uint_mult_ge0 [simp] = mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0] |
|
37660 | 2694 |
|
70185 | 2695 |
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
|
2696 |
for x :: "'a::len word" and y :: "'b::len word" |
37660 | 2697 |
using uint_ge_0 [of y] uint_lt2p [of x] by arith |
2698 |
||
2699 |
||
61799 | 2700 |
subsection \<open>Conditions for the addition (etc) of two words to overflow\<close> |
37660 | 2701 |
|
65268 | 2702 |
lemma uint_add_lem: |
70185 | 2703 |
"(uint x + uint y < 2 ^ LENGTH('a)) = |
65328 | 2704 |
(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
|
2705 |
for x y :: "'a::len word" |
71997 | 2706 |
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 | 2707 |
|
65268 | 2708 |
lemma uint_mult_lem: |
70185 | 2709 |
"(uint x * uint y < 2 ^ LENGTH('a)) = |
65328 | 2710 |
(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
|
2711 |
for x y :: "'a::len word" |
71997 | 2712 |
by (metis mod_pos_pos_trivial uint_lt2p uint_mult_ge0 uint_word_ariths(3)) |
37660 | 2713 |
|
65328 | 2714 |
lemma uint_sub_lem: "uint x \<ge> uint y \<longleftrightarrow> uint (x - y) = uint x - uint y" |
72292 | 2715 |
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 | 2716 |
|
2717 |
lemma uint_add_le: "uint (x + y) \<le> uint x + uint y" |
|
71997 | 2718 |
unfolding uint_word_ariths by (simp add: zmod_le_nonneg_dividend) |
37660 | 2719 |
|
65328 | 2720 |
lemma uint_sub_ge: "uint (x - y) \<ge> uint x - uint y" |
72488 | 2721 |
unfolding uint_word_ariths |
2722 |
by (simp flip: take_bit_eq_mod add: take_bit_int_greater_eq_self_iff) |
|
2723 |
||
2724 |
lemma int_mod_ge: \<open>a \<le> a mod n\<close> if \<open>a < n\<close> \<open>0 < n\<close> |
|
2725 |
for a n :: int |
|
2726 |
proof (cases \<open>a < 0\<close>) |
|
2727 |
case True |
|
2728 |
with \<open>0 < n\<close> show ?thesis |
|
2729 |
by (metis less_trans not_less pos_mod_conj) |
|
2730 |
||
2731 |
next |
|
2732 |
case False |
|
2733 |
with \<open>a < n\<close> show ?thesis |
|
2734 |
by simp |
|
2735 |
qed |
|
2736 |
||
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2737 |
lemma mod_add_if_z: |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2738 |
"\<lbrakk>x < z; y < z; 0 \<le> y; 0 \<le> x; 0 \<le> z\<rbrakk> \<Longrightarrow> |
65328 | 2739 |
(x + y) mod z = (if x + y < z then x + y else x + y - z)" |
2740 |
for x y z :: int |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2741 |
apply (simp add: not_less) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2742 |
by (metis (no_types) add_strict_mono diff_ge_0_iff_ge diff_less_eq minus_mod_self2 mod_pos_pos_trivial) |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2743 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2744 |
lemma uint_plus_if': |
65328 | 2745 |
"uint (a + b) = |
70185 | 2746 |
(if uint a + uint b < 2 ^ LENGTH('a) then uint a + uint b |
2747 |
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
|
2748 |
for a b :: "'a::len word" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2749 |
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
|
2750 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2751 |
lemma mod_sub_if_z: |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2752 |
"\<lbrakk>x < z; y < z; 0 \<le> y; 0 \<le> x; 0 \<le> z\<rbrakk> \<Longrightarrow> |
65328 | 2753 |
(x - y) mod z = (if y \<le> x then x - y else x - y + z)" |
2754 |
for x y z :: int |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2755 |
using mod_pos_pos_trivial [of "x - y + z" z] by (auto simp add: not_le) |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2756 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2757 |
lemma uint_sub_if': |
65328 | 2758 |
"uint (a - b) = |
2759 |
(if uint b \<le> uint a then uint a - uint b |
|
70185 | 2760 |
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
|
2761 |
for a b :: "'a::len word" |
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2762 |
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
|
2763 |
|
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
2764 |
|
61799 | 2765 |
subsection \<open>Definition of \<open>uint_arith\<close>\<close> |
37660 | 2766 |
|
2767 |
lemma word_of_int_inverse: |
|
70185 | 2768 |
"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
|
2769 |
for a :: "'a::len word" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2770 |
by transfer (simp add: take_bit_int_eq_self) |
37660 | 2771 |
|
2772 |
lemma uint_split: |
|
70185 | 2773 |
"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
|
2774 |
for x :: "'a::len word" |
72262 | 2775 |
by transfer (auto simp add: take_bit_eq_mod) |
37660 | 2776 |
|
2777 |
lemma uint_split_asm: |
|
70185 | 2778 |
"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
|
2779 |
for x :: "'a::len word" |
72262 | 2780 |
by auto (metis take_bit_int_eq_self_iff) |
37660 | 2781 |
|
2782 |
lemmas uint_splits = uint_split uint_split_asm |
|
2783 |
||
65268 | 2784 |
lemmas uint_arith_simps = |
37660 | 2785 |
word_le_def word_less_alt |
72292 | 2786 |
word_uint_eq_iff |
37660 | 2787 |
uint_sub_if' uint_plus_if' |
2788 |
||
70185 | 2789 |
\<comment> \<open>use this to stop, eg. \<open>2 ^ LENGTH(32)\<close> being simplified\<close> |
65268 | 2790 |
lemma power_False_cong: "False \<Longrightarrow> a ^ b = c ^ d" |
37660 | 2791 |
by auto |
2792 |
||
67408 | 2793 |
\<comment> \<open>\<open>uint_arith_tac\<close>: reduce to arithmetic on int, try to solve by arith\<close> |
61799 | 2794 |
ML \<open> |
72292 | 2795 |
val uint_arith_simpset = |
2796 |
@{context} |
|
2797 |
|> fold Simplifier.add_simp @{thms uint_arith_simps} |
|
2798 |
|> fold Splitter.add_split @{thms if_split_asm} |
|
2799 |
|> fold Simplifier.add_cong @{thms power_False_cong} |
|
2800 |
|> simpset_of; |
|
2801 |
||
65268 | 2802 |
fun uint_arith_tacs ctxt = |
37660 | 2803 |
let |
2804 |
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
|
2805 |
Arith_Data.arith_tac ctxt n t |
37660 | 2806 |
handle Cooper.COOPER _ => Seq.empty; |
65268 | 2807 |
in |
42793 | 2808 |
[ clarify_tac ctxt 1, |
72292 | 2809 |
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
|
2810 |
ALLGOALS (full_simp_tac |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2811 |
(put_simpset HOL_ss ctxt |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2812 |
|> fold Splitter.add_split @{thms uint_splits} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
2813 |
|> fold Simplifier.add_cong @{thms power_False_cong})), |
65268 | 2814 |
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
|
2815 |
ALLGOALS (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN |
60754 | 2816 |
REPEAT (eresolve_tac ctxt [conjE] n) THEN |
65268 | 2817 |
REPEAT (dresolve_tac ctxt @{thms word_of_int_inverse} n |
2818 |
THEN assume_tac ctxt n |
|
58963
26bf09b95dda
proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents:
58874
diff
changeset
|
2819 |
THEN assume_tac ctxt n)), |
37660 | 2820 |
TRYALL arith_tac' ] |
2821 |
end |
|
2822 |
||
2823 |
fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt)) |
|
61799 | 2824 |
\<close> |
37660 | 2825 |
|
65268 | 2826 |
method_setup uint_arith = |
61799 | 2827 |
\<open>Scan.succeed (SIMPLE_METHOD' o uint_arith_tac)\<close> |
37660 | 2828 |
"solving word arithmetic via integers and arith" |
2829 |
||
2830 |
||
61799 | 2831 |
subsection \<open>More on overflows and monotonicity\<close> |
37660 | 2832 |
|
65328 | 2833 |
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
|
2834 |
for x y :: "'a::len word" |
37660 | 2835 |
unfolding word_size by uint_arith |
2836 |
||
2837 |
lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size] |
|
2838 |
||
65328 | 2839 |
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
|
2840 |
for x y :: "'a::len word" |
37660 | 2841 |
by uint_arith |
2842 |
||
70185 | 2843 |
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
|
2844 |
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
|
2845 |
by (simp add: ac_simps no_olen_add) |
37660 | 2846 |
|
45604 | 2847 |
lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric]] |
2848 |
||
2849 |
lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem] |
|
2850 |
lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1] |
|
2851 |
lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem] |
|
37660 | 2852 |
lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def] |
2853 |
lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def] |
|
45604 | 2854 |
lemmas word_sub_le = word_sub_le_iff [THEN iffD2] |
37660 | 2855 |
|
65328 | 2856 |
lemma word_less_sub1: "x \<noteq> 0 \<Longrightarrow> 1 < x \<longleftrightarrow> 0 < x - 1" |
2857 |
for x :: "'a::len word" |
|
37660 | 2858 |
by uint_arith |
2859 |
||
65328 | 2860 |
lemma word_le_sub1: "x \<noteq> 0 \<Longrightarrow> 1 \<le> x \<longleftrightarrow> 0 \<le> x - 1" |
2861 |
for x :: "'a::len word" |
|
37660 | 2862 |
by uint_arith |
2863 |
||
65328 | 2864 |
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
|
2865 |
for x z :: "'a::len word" |
37660 | 2866 |
by uint_arith |
2867 |
||
65328 | 2868 |
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
|
2869 |
for x z :: "'a::len word" |
37660 | 2870 |
by uint_arith |
2871 |
||
65328 | 2872 |
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
|
2873 |
for x ab c :: "'a::len word" |
37660 | 2874 |
by uint_arith |
2875 |
||
65328 | 2876 |
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
|
2877 |
for x ab c :: "'a::len word" |
37660 | 2878 |
by uint_arith |
2879 |
||
65328 | 2880 |
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
|
2881 |
for a b c :: "'a::len word" |
37660 | 2882 |
by uint_arith |
2883 |
||
65328 | 2884 |
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
|
2885 |
for a b c :: "'a::len word" |
37660 | 2886 |
by uint_arith |
2887 |
||
2888 |
lemmas le_plus = le_plus' [rotated] |
|
2889 |
||
46011 | 2890 |
lemmas le_minus = leD [THEN thin_rl, THEN le_minus'] (* FIXME *) |
37660 | 2891 |
|
65328 | 2892 |
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
|
2893 |
for x y z :: "'a::len word" |
37660 | 2894 |
by uint_arith |
2895 |
||
65328 | 2896 |
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
|
2897 |
for x y z :: "'a::len word" |
37660 | 2898 |
by uint_arith |
2899 |
||
65328 | 2900 |
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
|
2901 |
for x y z :: "'a::len word" |
37660 | 2902 |
by uint_arith |
2903 |
||
65328 | 2904 |
lemma word_less_minus_mono: "a < c \<Longrightarrow> d < b \<Longrightarrow> a - b < a \<Longrightarrow> c - d < c \<Longrightarrow> a - b < c - d" |
2905 |
for a b c d :: "'a::len word" |
|
37660 | 2906 |
by uint_arith |
2907 |
||
65328 | 2908 |
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
|
2909 |
for x y z :: "'a::len word" |
37660 | 2910 |
by uint_arith |
2911 |
||
65328 | 2912 |
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
|
2913 |
for x y z :: "'a::len word" |
37660 | 2914 |
by uint_arith |
2915 |
||
65268 | 2916 |
lemma word_le_minus_mono: |
65328 | 2917 |
"a \<le> c \<Longrightarrow> d \<le> b \<Longrightarrow> a - b \<le> a \<Longrightarrow> c - d \<le> c \<Longrightarrow> a - b \<le> c - d" |
2918 |
for a b c d :: "'a::len word" |
|
37660 | 2919 |
by uint_arith |
2920 |
||
65328 | 2921 |
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
|
2922 |
for x y y' :: "'a::len word" |
37660 | 2923 |
by uint_arith |
2924 |
||
65328 | 2925 |
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
|
2926 |
for x y y' :: "'a::len word" |
37660 | 2927 |
by uint_arith |
2928 |
||
65328 | 2929 |
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
|
2930 |
for a b c :: "'a::len word" |
65328 | 2931 |
by uint_arith |
2932 |
||
2933 |
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
|
2934 |
for x y z :: "'a::len word" |
37660 | 2935 |
by uint_arith |
2936 |
||
65328 | 2937 |
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
|
2938 |
for x y z :: "'a::len word" |
37660 | 2939 |
by uint_arith |
2940 |
||
65328 | 2941 |
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
|
2942 |
for x y z :: "'a::len word" |
37660 | 2943 |
by uint_arith |
2944 |
||
65328 | 2945 |
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
|
2946 |
for x z k :: "'a::len word" |
37660 | 2947 |
by uint_arith |
2948 |
||
65328 | 2949 |
lemma inc_le: "i < m \<Longrightarrow> i + 1 \<le> m" |
2950 |
for i m :: "'a::len word" |
|
37660 | 2951 |
by uint_arith |
2952 |
||
65328 | 2953 |
lemma inc_i: "1 \<le> i \<Longrightarrow> i < m \<Longrightarrow> 1 \<le> i + 1 \<and> i + 1 \<le> m" |
2954 |
for i m :: "'a::len word" |
|
37660 | 2955 |
by uint_arith |
2956 |
||
2957 |
lemma udvd_incr_lem: |
|
65268 | 2958 |
"up < uq \<Longrightarrow> up = ua + n * uint K \<Longrightarrow> |
65328 | 2959 |
uq = ua + n' * uint K \<Longrightarrow> up + uint K \<le> uq" |
71997 | 2960 |
by auto (metis int_distrib(1) linorder_not_less mult.left_neutral mult_right_mono uint_nonnegative zless_imp_add1_zle) |
37660 | 2961 |
|
65268 | 2962 |
lemma udvd_incr': |
2963 |
"p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow> |
|
65328 | 2964 |
uint q = ua + n' * uint K \<Longrightarrow> p + K \<le> q" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2965 |
unfolding word_less_alt word_le_def |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2966 |
by (metis (full_types) order_trans udvd_incr_lem uint_add_le) |
37660 | 2967 |
|
65268 | 2968 |
lemma udvd_decr': |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2969 |
assumes "p < q" "uint p = ua + n * uint K" "uint q = ua + n' * uint K" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2970 |
shows "uint q = ua + n' * uint K \<Longrightarrow> p \<le> q - K" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2971 |
proof - |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2972 |
have "\<And>w wa. uint (w::'a word) \<le> uint wa + uint (w - wa)" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2973 |
by (metis (no_types) add_diff_cancel_left' diff_add_cancel uint_add_le) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2974 |
moreover have "uint K + uint p \<le> uint q" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2975 |
using assms by (metis (no_types) add_diff_cancel_left' diff_add_cancel udvd_incr_lem word_less_def) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2976 |
ultimately show ?thesis |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2977 |
by (meson add_le_cancel_left order_trans word_less_eq_iff_unsigned) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2978 |
qed |
37660 | 2979 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
2980 |
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
|
2981 |
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
|
2982 |
lemmas udvd_decr0 = udvd_decr' [where ua=0, unfolded add_0_left] |
37660 | 2983 |
|
65328 | 2984 |
lemma udvd_minus_le': "xy < k \<Longrightarrow> z udvd xy \<Longrightarrow> z udvd k \<Longrightarrow> xy \<le> k - z" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2985 |
unfolding udvd_unfold_int |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2986 |
by (meson udvd_decr0) |
37660 | 2987 |
|
65268 | 2988 |
lemma udvd_incr2_K: |
65328 | 2989 |
"p < a + s \<Longrightarrow> a \<le> a + s \<Longrightarrow> K udvd s \<Longrightarrow> K udvd p - a \<Longrightarrow> a \<le> p \<Longrightarrow> |
2990 |
0 < K \<Longrightarrow> p \<le> p + K \<and> p + K \<le> a + s" |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2991 |
unfolding udvd_unfold_int |
62390 | 2992 |
apply (simp add: uint_arith_simps split: if_split_asm) |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2993 |
apply (metis (no_types, hide_lams) le_add_diff_inverse le_less_trans udvd_incr_lem) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
2994 |
using uint_lt2p [of s] by simp |
37660 | 2995 |
|
2996 |
||
61799 | 2997 |
subsection \<open>Arithmetic type class instantiations\<close> |
37660 | 2998 |
|
2999 |
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
|
3000 |
word_zero_le [THEN leD, THEN antisym_conv1] |
37660 | 3001 |
|
65328 | 3002 |
lemma word_of_int_nat: "0 \<le> x \<Longrightarrow> word_of_int x = of_nat (nat x)" |
72262 | 3003 |
by simp |
37660 | 3004 |
|
67408 | 3005 |
text \<open> |
3006 |
note that \<open>iszero_def\<close> is only for class \<open>comm_semiring_1_cancel\<close>, |
|
3007 |
which requires word length \<open>\<ge> 1\<close>, ie \<open>'a::len word\<close> |
|
3008 |
\<close> |
|
46603 | 3009 |
lemma iszero_word_no [simp]: |
65268 | 3010 |
"iszero (numeral bin :: 'a::len word) = |
72128 | 3011 |
iszero (take_bit LENGTH('a) (numeral bin :: int))" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3012 |
by (metis iszero_def uint_0_iff uint_bintrunc) |
65268 | 3013 |
|
61799 | 3014 |
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
|
3015 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3016 |
lemmas word_eq_numeral_iff_iszero [simp] = |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3017 |
eq_numeral_iff_iszero [where 'a="'a::len word"] |
46603 | 3018 |
|
37660 | 3019 |
|
61799 | 3020 |
subsection \<open>Word and nat\<close> |
37660 | 3021 |
|
70185 | 3022 |
lemma word_nchotomy: "\<forall>w :: 'a::len word. \<exists>n. w = of_nat n \<and> n < 2 ^ LENGTH('a)" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3023 |
by (metis of_nat_unat ucast_id unsigned_less) |
37660 | 3024 |
|
70185 | 3025 |
lemma of_nat_eq: "of_nat n = w \<longleftrightarrow> (\<exists>q. n = unat w + q * 2 ^ LENGTH('a))" |
65328 | 3026 |
for w :: "'a::len word" |
68157 | 3027 |
using mod_div_mult_eq [of n "2 ^ LENGTH('a)", symmetric] |
72292 | 3028 |
by (auto simp flip: take_bit_eq_mod) |
37660 | 3029 |
|
65328 | 3030 |
lemma of_nat_eq_size: "of_nat n = w \<longleftrightarrow> (\<exists>q. n = unat w + q * 2 ^ size w)" |
37660 | 3031 |
unfolding word_size by (rule of_nat_eq) |
3032 |
||
70185 | 3033 |
lemma of_nat_0: "of_nat m = (0::'a::len word) \<longleftrightarrow> (\<exists>q. m = q * 2 ^ LENGTH('a))" |
37660 | 3034 |
by (simp add: of_nat_eq) |
3035 |
||
70185 | 3036 |
lemma of_nat_2p [simp]: "of_nat (2 ^ LENGTH('a)) = (0::'a::len word)" |
45805 | 3037 |
by (fact mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]]) |
37660 | 3038 |
|
65328 | 3039 |
lemma of_nat_gt_0: "of_nat k \<noteq> 0 \<Longrightarrow> 0 < k" |
37660 | 3040 |
by (cases k) auto |
3041 |
||
70185 | 3042 |
lemma of_nat_neq_0: "0 < k \<Longrightarrow> k < 2 ^ LENGTH('a::len) \<Longrightarrow> of_nat k \<noteq> (0 :: 'a word)" |
65328 | 3043 |
by (auto simp add : of_nat_0) |
3044 |
||
3045 |
lemma Abs_fnat_hom_add: "of_nat a + of_nat b = of_nat (a + b)" |
|
37660 | 3046 |
by simp |
3047 |
||
65328 | 3048 |
lemma Abs_fnat_hom_mult: "of_nat a * of_nat b = (of_nat (a * b) :: 'a::len word)" |
72262 | 3049 |
by (simp add: wi_hom_mult) |
37660 | 3050 |
|
65328 | 3051 |
lemma Abs_fnat_hom_Suc: "word_succ (of_nat a) = of_nat (Suc a)" |
72262 | 3052 |
by transfer (simp add: ac_simps) |
37660 | 3053 |
|
3054 |
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
|
3055 |
by simp |
37660 | 3056 |
|
3057 |
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
|
3058 |
by simp |
37660 | 3059 |
|
65268 | 3060 |
lemmas Abs_fnat_homs = |
3061 |
Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc |
|
37660 | 3062 |
Abs_fnat_hom_0 Abs_fnat_hom_1 |
3063 |
||
65328 | 3064 |
lemma word_arith_nat_add: "a + b = of_nat (unat a + unat b)" |
3065 |
by simp |
|
3066 |
||
3067 |
lemma word_arith_nat_mult: "a * b = of_nat (unat a * unat b)" |
|
37660 | 3068 |
by simp |
3069 |
||
65328 | 3070 |
lemma word_arith_nat_Suc: "word_succ a = of_nat (Suc (unat a))" |
37660 | 3071 |
by (subst Abs_fnat_hom_Suc [symmetric]) simp |
3072 |
||
65328 | 3073 |
lemma word_arith_nat_div: "a div b = of_nat (unat a div unat b)" |
72262 | 3074 |
by (metis of_int_of_nat_eq of_nat_unat of_nat_div word_div_def) |
3075 |
||
65328 | 3076 |
lemma word_arith_nat_mod: "a mod b = of_nat (unat a mod unat b)" |
72262 | 3077 |
by (metis of_int_of_nat_eq of_nat_mod of_nat_unat word_mod_def) |
37660 | 3078 |
|
3079 |
lemmas word_arith_nat_defs = |
|
3080 |
word_arith_nat_add word_arith_nat_mult |
|
3081 |
word_arith_nat_Suc Abs_fnat_hom_0 |
|
3082 |
Abs_fnat_hom_1 word_arith_nat_div |
|
65268 | 3083 |
word_arith_nat_mod |
37660 | 3084 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3085 |
lemma unat_cong: "x = y \<Longrightarrow> unat x = unat y" |
72292 | 3086 |
by (fact arg_cong) |
3087 |
||
3088 |
lemma unat_of_nat: |
|
3089 |
\<open>unat (word_of_nat x :: 'a::len word) = x mod 2 ^ LENGTH('a)\<close> |
|
3090 |
by transfer (simp flip: take_bit_eq_mod add: nat_take_bit_eq) |
|
65268 | 3091 |
|
37660 | 3092 |
lemmas unat_word_ariths = word_arith_nat_defs |
45604 | 3093 |
[THEN trans [OF unat_cong unat_of_nat]] |
37660 | 3094 |
|
3095 |
lemmas word_sub_less_iff = word_sub_le_iff |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3096 |
[unfolded linorder_not_less [symmetric] Not_eq_iff] |
37660 | 3097 |
|
65268 | 3098 |
lemma unat_add_lem: |
70185 | 3099 |
"unat x + unat y < 2 ^ LENGTH('a) \<longleftrightarrow> unat (x + y) = unat x + unat y" |
65328 | 3100 |
for x y :: "'a::len word" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3101 |
by (metis mod_less unat_word_ariths(1) unsigned_less) |
37660 | 3102 |
|
65268 | 3103 |
lemma unat_mult_lem: |
70185 | 3104 |
"unat x * unat y < 2 ^ LENGTH('a) \<longleftrightarrow> unat (x * y) = unat x * unat y" |
65363 | 3105 |
for x y :: "'a::len word" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3106 |
by (metis mod_less unat_word_ariths(2) unsigned_less) |
71997 | 3107 |
|
3108 |
lemma unat_plus_if': |
|
3109 |
\<open>unat (a + b) = |
|
3110 |
(if unat a + unat b < 2 ^ LENGTH('a) |
|
3111 |
then unat a + unat b |
|
3112 |
else unat a + unat b - 2 ^ LENGTH('a))\<close> for a b :: \<open>'a::len word\<close> |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3113 |
apply (auto simp: unat_word_ariths not_less le_iff_add) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3114 |
by (metis add.commute add_less_cancel_right add_strict_mono mod_less unsigned_less) |
65328 | 3115 |
|
3116 |
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
|
3117 |
for a b x :: "'a::len word" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3118 |
using word_le_plus_either by blast |
37660 | 3119 |
|
65328 | 3120 |
lemmas un_ui_le = |
3121 |
trans [OF word_le_nat_alt [symmetric] word_le_def] |
|
37660 | 3122 |
|
3123 |
lemma unat_sub_if_size: |
|
65328 | 3124 |
"unat (x - y) = |
3125 |
(if unat y \<le> unat x |
|
3126 |
then unat x - unat y |
|
3127 |
else unat x + 2 ^ size x - unat y)" |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3128 |
proof - |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3129 |
{ assume xy: "\<not> uint y \<le> uint x" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3130 |
have "nat (uint x - uint y + 2 ^ LENGTH('a)) = nat (uint x + 2 ^ LENGTH('a) - uint y)" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3131 |
by simp |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3132 |
also have "... = nat (uint x + 2 ^ LENGTH('a)) - nat (uint y)" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3133 |
by (simp add: nat_diff_distrib') |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3134 |
also have "... = nat (uint x) + 2 ^ LENGTH('a) - nat (uint y)" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3135 |
by (metis nat_add_distrib nat_eq_numeral_power_cancel_iff order_less_imp_le unsigned_0 unsigned_greater_eq unsigned_less) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3136 |
finally have "nat (uint x - uint y + 2 ^ LENGTH('a)) = nat (uint x) + 2 ^ LENGTH('a) - nat (uint y)" . |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3137 |
} |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3138 |
then show ?thesis |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3139 |
unfolding word_size |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3140 |
by (metis nat_diff_distrib' uint_sub_if' un_ui_le unat_eq_nat_uint unsigned_greater_eq) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3141 |
qed |
37660 | 3142 |
|
3143 |
lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size] |
|
3144 |
||
71997 | 3145 |
lemma uint_div: |
3146 |
\<open>uint (x div y) = uint x div uint y\<close> |
|
72262 | 3147 |
by (fact uint_div_distrib) |
71997 | 3148 |
|
3149 |
lemma unat_div: |
|
3150 |
\<open>unat (x div y) = unat x div unat y\<close> |
|
72262 | 3151 |
by (fact unat_div_distrib) |
71997 | 3152 |
|
3153 |
lemma uint_mod: |
|
3154 |
\<open>uint (x mod y) = uint x mod uint y\<close> |
|
72262 | 3155 |
by (fact uint_mod_distrib) |
71997 | 3156 |
|
72079 | 3157 |
lemma unat_mod: |
3158 |
\<open>unat (x mod y) = unat x mod unat y\<close> |
|
72262 | 3159 |
by (fact unat_mod_distrib) |
71997 | 3160 |
|
37660 | 3161 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3162 |
text \<open>Definition of \<open>unat_arith\<close> tactic\<close> |
37660 | 3163 |
|
70185 | 3164 |
lemma unat_split: "P (unat x) \<longleftrightarrow> (\<forall>n. of_nat n = x \<and> n < 2^LENGTH('a) \<longrightarrow> P n)" |
65328 | 3165 |
for x :: "'a::len word" |
72262 | 3166 |
by auto (metis take_bit_nat_eq_self_iff) |
37660 | 3167 |
|
70185 | 3168 |
lemma unat_split_asm: "P (unat x) \<longleftrightarrow> (\<nexists>n. of_nat n = x \<and> n < 2^LENGTH('a) \<and> \<not> P n)" |
65328 | 3169 |
for x :: "'a::len word" |
72262 | 3170 |
by auto (metis take_bit_nat_eq_self_iff) |
37660 | 3171 |
|
72292 | 3172 |
lemma of_nat_inverse: |
3173 |
\<open>word_of_nat r = a \<Longrightarrow> r < 2 ^ LENGTH('a) \<Longrightarrow> unat a = r\<close> |
|
3174 |
for a :: \<open>'a::len word\<close> |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3175 |
by (metis mod_if unat_of_nat) |
72292 | 3176 |
|
3177 |
lemma word_unat_eq_iff: |
|
3178 |
\<open>v = w \<longleftrightarrow> unat v = unat w\<close> |
|
3179 |
for v w :: \<open>'a::len word\<close> |
|
3180 |
by (fact word_eq_iff_unsigned) |
|
37660 | 3181 |
|
3182 |
lemmas unat_splits = unat_split unat_split_asm |
|
3183 |
||
3184 |
lemmas unat_arith_simps = |
|
3185 |
word_le_nat_alt word_less_nat_alt |
|
72292 | 3186 |
word_unat_eq_iff |
37660 | 3187 |
unat_sub_if' unat_plus_if' unat_div unat_mod |
3188 |
||
67408 | 3189 |
\<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 | 3190 |
ML \<open> |
72292 | 3191 |
val unat_arith_simpset = |
72954 | 3192 |
@{context} (* TODO: completely explicitly determined simpset *) |
3193 |
|> fold Simplifier.del_simp @{thms unsigned_of_nat unsigned_of_int} |
|
72292 | 3194 |
|> fold Simplifier.add_simp @{thms unat_arith_simps} |
3195 |
|> fold Splitter.add_split @{thms if_split_asm} |
|
3196 |
|> fold Simplifier.add_cong @{thms power_False_cong} |
|
3197 |
|> simpset_of |
|
37660 | 3198 |
|
65268 | 3199 |
fun unat_arith_tacs ctxt = |
37660 | 3200 |
let |
3201 |
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
|
3202 |
Arith_Data.arith_tac ctxt n t |
37660 | 3203 |
handle Cooper.COOPER _ => Seq.empty; |
65268 | 3204 |
in |
42793 | 3205 |
[ clarify_tac ctxt 1, |
72292 | 3206 |
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
|
3207 |
ALLGOALS (full_simp_tac |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
3208 |
(put_simpset HOL_ss ctxt |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
3209 |
|> fold Splitter.add_split @{thms unat_splits} |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
51375
diff
changeset
|
3210 |
|> fold Simplifier.add_cong @{thms power_False_cong})), |
65268 | 3211 |
rewrite_goals_tac ctxt @{thms word_size}, |
60754 | 3212 |
ALLGOALS (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN |
3213 |
REPEAT (eresolve_tac ctxt [conjE] n) THEN |
|
3214 |
REPEAT (dresolve_tac ctxt @{thms of_nat_inverse} n THEN assume_tac ctxt n)), |
|
65268 | 3215 |
TRYALL arith_tac' ] |
37660 | 3216 |
end |
3217 |
||
3218 |
fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt)) |
|
61799 | 3219 |
\<close> |
37660 | 3220 |
|
65268 | 3221 |
method_setup unat_arith = |
61799 | 3222 |
\<open>Scan.succeed (SIMPLE_METHOD' o unat_arith_tac)\<close> |
37660 | 3223 |
"solving word arithmetic via natural numbers and arith" |
3224 |
||
65328 | 3225 |
lemma no_plus_overflow_unat_size: "x \<le> x + y \<longleftrightarrow> unat x + unat y < 2 ^ size x" |
3226 |
for x y :: "'a::len word" |
|
37660 | 3227 |
unfolding word_size by unat_arith |
3228 |
||
65328 | 3229 |
lemmas no_olen_add_nat = |
3230 |
no_plus_overflow_unat_size [unfolded word_size] |
|
3231 |
||
3232 |
lemmas unat_plus_simple = |
|
3233 |
trans [OF no_olen_add_nat unat_add_lem] |
|
3234 |
||
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3235 |
lemma word_div_mult: "\<lbrakk>0 < y; unat x * unat y < 2 ^ LENGTH('a)\<rbrakk> \<Longrightarrow> x * y div y = x" |
65328 | 3236 |
for x y :: "'a::len word" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3237 |
by (simp add: unat_eq_zero unat_mult_lem word_arith_nat_div) |
37660 | 3238 |
|
70185 | 3239 |
lemma div_lt': "i \<le> k div x \<Longrightarrow> unat i * unat x < 2 ^ LENGTH('a)" |
65328 | 3240 |
for i k x :: "'a::len word" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3241 |
by unat_arith (meson le_less_trans less_mult_imp_div_less not_le unsigned_less) |
37660 | 3242 |
|
3243 |
lemmas div_lt'' = order_less_imp_le [THEN div_lt'] |
|
3244 |
||
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3245 |
lemma div_lt_mult: "\<lbrakk>i < k div x; 0 < x\<rbrakk> \<Longrightarrow> i * x < k" |
65328 | 3246 |
for i k x :: "'a::len word" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3247 |
by (metis div_le_mono div_lt'' not_le unat_div word_div_mult word_less_iff_unsigned) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3248 |
|
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3249 |
lemma div_le_mult: "\<lbrakk>i \<le> k div x; 0 < x\<rbrakk> \<Longrightarrow> i * x \<le> k" |
65328 | 3250 |
for i k x :: "'a::len word" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3251 |
by (metis div_lt' less_mult_imp_div_less not_less unat_arith_simps(2) unat_div unat_mult_lem) |
37660 | 3252 |
|
70185 | 3253 |
lemma div_lt_uint': "i \<le> k div x \<Longrightarrow> uint i * uint x < 2 ^ LENGTH('a)" |
65328 | 3254 |
for i k x :: "'a::len word" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3255 |
unfolding uint_nat |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3256 |
by (metis div_lt' int_ops(7) of_nat_unat uint_mult_lem unat_mult_lem) |
37660 | 3257 |
|
3258 |
lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint'] |
|
3259 |
||
70185 | 3260 |
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
|
3261 |
for x y z :: "'a::len word" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3262 |
by (metis add.commute diff_add_cancel no_olen_add) |
71997 | 3263 |
|
37660 | 3264 |
lemmas plus_minus_not_NULL = order_less_imp_le [THEN plus_minus_not_NULL_ab] |
3265 |
||
3266 |
lemmas plus_minus_no_overflow = |
|
3267 |
order_less_imp_le [THEN plus_minus_no_overflow_ab] |
|
65268 | 3268 |
|
37660 | 3269 |
lemmas mcs = word_less_minus_cancel word_less_minus_mono_left |
3270 |
word_le_minus_cancel word_le_minus_mono_left |
|
3271 |
||
45604 | 3272 |
lemmas word_l_diffs = mcs [where y = "w + x", unfolded add_diff_cancel] for w x |
3273 |
lemmas word_diff_ls = mcs [where z = "w + x", unfolded add_diff_cancel] for w x |
|
3274 |
lemmas word_plus_mcs = word_diff_ls [where y = "v + x", unfolded add_diff_cancel] for v x |
|
37660 | 3275 |
|
72292 | 3276 |
lemma le_unat_uoi: |
3277 |
\<open>y \<le> unat z \<Longrightarrow> unat (word_of_nat y :: 'a word) = y\<close> |
|
3278 |
for z :: \<open>'a::len word\<close> |
|
3279 |
by transfer (simp add: nat_take_bit_eq take_bit_nat_eq_self_iff le_less_trans) |
|
37660 | 3280 |
|
66808
1907167b6038
elementary definition of division on natural numbers
haftmann
parents:
66453
diff
changeset
|
3281 |
lemmas thd = times_div_less_eq_dividend |
37660 | 3282 |
|
71997 | 3283 |
lemmas uno_simps [THEN le_unat_uoi] = mod_le_divisor div_le_dividend |
37660 | 3284 |
|
65328 | 3285 |
lemma word_mod_div_equality: "(n div b) * b + (n mod b) = n" |
3286 |
for n b :: "'a::len word" |
|
71997 | 3287 |
by (fact div_mult_mod_eq) |
37660 | 3288 |
|
65328 | 3289 |
lemma word_div_mult_le: "a div b * b \<le> a" |
3290 |
for a b :: "'a::len word" |
|
71997 | 3291 |
by (metis div_le_mult mult_not_zero order.not_eq_order_implies_strict order_refl word_zero_le) |
37660 | 3292 |
|
65328 | 3293 |
lemma word_mod_less_divisor: "0 < n \<Longrightarrow> m mod n < n" |
3294 |
for m n :: "'a::len word" |
|
71997 | 3295 |
by (simp add: unat_arith_simps) |
3296 |
||
65328 | 3297 |
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
|
3298 |
by (induct n) (simp_all add: wi_hom_mult [symmetric]) |
37660 | 3299 |
|
65328 | 3300 |
lemma word_arith_power_alt: "a ^ n = (word_of_int (uint a ^ n) :: 'a::len word)" |
37660 | 3301 |
by (simp add : word_of_int_power_hom [symmetric]) |
3302 |
||
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3303 |
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
|
3304 |
for n :: "'a::len word" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3305 |
by unat_arith |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3306 |
|
37660 | 3307 |
|
61799 | 3308 |
subsection \<open>Cardinality, finiteness of set of words\<close> |
37660 | 3309 |
|
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
|
3310 |
lemma inj_on_word_of_int: \<open>inj_on (word_of_int :: int \<Rightarrow> 'a word) {0..<2 ^ LENGTH('a::len)}\<close> |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3311 |
unfolding inj_on_def |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3312 |
by (metis atLeastLessThan_iff word_of_int_inverse) |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
3313 |
|
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
|
3314 |
lemma range_uint: \<open>range (uint :: 'a word \<Rightarrow> int) = {0..<2 ^ LENGTH('a::len)}\<close> |
72488 | 3315 |
apply transfer |
3316 |
apply (auto simp add: image_iff) |
|
3317 |
apply (metis take_bit_int_eq_self_iff) |
|
3318 |
done |
|
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
3319 |
|
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
|
3320 |
lemma UNIV_eq: \<open>(UNIV :: 'a word set) = word_of_int ` {0..<2 ^ LENGTH('a::len)}\<close> |
72488 | 3321 |
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
|
3322 |
|
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
|
3323 |
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
|
3324 |
by (simp add: UNIV_eq card_image inj_on_word_of_int) |
37660 | 3325 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3326 |
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
|
3327 |
for x :: "'a::len word" |
65328 | 3328 |
unfolding word_size by (rule card_word) |
37660 | 3329 |
|
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
|
3330 |
instance word :: (len) finite |
71948
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
3331 |
by standard (simp add: UNIV_eq) |
6ede899d26d3
fundamental construction of word type following existing transfer rules
haftmann
parents:
71947
diff
changeset
|
3332 |
|
37660 | 3333 |
|
61799 | 3334 |
subsection \<open>Bitwise Operations on Words\<close> |
37660 | 3335 |
|
46011 | 3336 |
lemma word_wi_log_defs: |
71149 | 3337 |
"NOT (word_of_int a) = word_of_int (NOT a)" |
46011 | 3338 |
"word_of_int a AND word_of_int b = word_of_int (a AND b)" |
3339 |
"word_of_int a OR word_of_int b = word_of_int (a OR b)" |
|
3340 |
"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
|
3341 |
by (transfer, rule refl)+ |
47372 | 3342 |
|
46011 | 3343 |
lemma word_no_log_defs [simp]: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3344 |
"NOT (numeral a) = word_of_int (NOT (numeral a))" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54225
diff
changeset
|
3345 |
"NOT (- numeral a) = word_of_int (NOT (- numeral a))" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3346 |
"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
|
3347 |
"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
|
3348 |
"- 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
|
3349 |
"- 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
|
3350 |
"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
|
3351 |
"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
|
3352 |
"- 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
|
3353 |
"- 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
|
3354 |
"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
|
3355 |
"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
|
3356 |
"- 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
|
3357 |
"- numeral a XOR - numeral b = word_of_int (- numeral a XOR - numeral b)" |
47372 | 3358 |
by (transfer, rule refl)+ |
37660 | 3359 |
|
61799 | 3360 |
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
|
3361 |
|
88ef116e0522
add simp rules for bitwise word operations with 1
huffman
parents:
46057
diff
changeset
|
3362 |
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
|
3363 |
"NOT (1::'a::len word) = -2" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
3364 |
"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
|
3365 |
"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
|
3366 |
"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
|
3367 |
"- 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
|
3368 |
"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
|
3369 |
"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
|
3370 |
"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
|
3371 |
"- 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
|
3372 |
"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
|
3373 |
"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
|
3374 |
"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
|
3375 |
"- numeral a XOR 1 = word_of_int (- numeral a XOR 1)" |
47372 | 3376 |
by (transfer, simp)+ |
46064
88ef116e0522
add simp rules for bitwise word operations with 1
huffman
parents:
46057
diff
changeset
|
3377 |
|
61799 | 3378 |
text \<open>Special cases for when one of the arguments equals -1.\<close> |
56979 | 3379 |
|
3380 |
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
|
3381 |
"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
|
3382 |
"(-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
|
3383 |
"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
|
3384 |
"(-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
|
3385 |
"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
|
3386 |
" (-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
|
3387 |
"x XOR (-1::'a::len word) = NOT x" |
56979 | 3388 |
by (transfer, simp)+ |
3389 |
||
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3390 |
lemma uint_and: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3391 |
\<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
|
3392 |
by transfer simp |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3393 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3394 |
lemma uint_or: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3395 |
\<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
|
3396 |
by transfer simp |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3397 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3398 |
lemma uint_xor: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3399 |
\<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
|
3400 |
by transfer simp |
47372 | 3401 |
|
67408 | 3402 |
\<comment> \<open>get from commutativity, associativity etc of \<open>int_and\<close> etc to same for \<open>word_and etc\<close>\<close> |
65268 | 3403 |
lemmas bwsimps = |
46013 | 3404 |
wi_hom_add |
37660 | 3405 |
word_wi_log_defs |
3406 |
||
3407 |
lemma word_bw_assocs: |
|
3408 |
"(x AND y) AND z = x AND y AND z" |
|
3409 |
"(x OR y) OR z = x OR y OR z" |
|
3410 |
"(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
|
3411 |
for x :: "'a::len word" |
72508 | 3412 |
by (fact ac_simps)+ |
65268 | 3413 |
|
37660 | 3414 |
lemma word_bw_comms: |
3415 |
"x AND y = y AND x" |
|
3416 |
"x OR y = y OR x" |
|
3417 |
"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
|
3418 |
for x :: "'a::len word" |
72508 | 3419 |
by (fact ac_simps)+ |
65268 | 3420 |
|
37660 | 3421 |
lemma word_bw_lcs: |
3422 |
"y AND x AND z = x AND y AND z" |
|
3423 |
"y OR x OR z = x OR y OR z" |
|
3424 |
"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
|
3425 |
for x :: "'a::len word" |
72508 | 3426 |
by (fact ac_simps)+ |
37660 | 3427 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3428 |
lemma word_log_esimps: |
37660 | 3429 |
"x AND 0 = 0" |
3430 |
"x AND -1 = x" |
|
3431 |
"x OR 0 = x" |
|
3432 |
"x OR -1 = -1" |
|
3433 |
"x XOR 0 = x" |
|
3434 |
"x XOR -1 = NOT x" |
|
3435 |
"0 AND x = 0" |
|
3436 |
"-1 AND x = x" |
|
3437 |
"0 OR x = x" |
|
3438 |
"-1 OR x = -1" |
|
3439 |
"0 XOR x = x" |
|
3440 |
"-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
|
3441 |
for x :: "'a::len word" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3442 |
by simp_all |
37660 | 3443 |
|
3444 |
lemma word_not_dist: |
|
3445 |
"NOT (x OR y) = NOT x AND NOT y" |
|
3446 |
"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
|
3447 |
for x :: "'a::len word" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3448 |
by simp_all |
37660 | 3449 |
|
3450 |
lemma word_bw_same: |
|
3451 |
"x AND x = x" |
|
3452 |
"x OR x = x" |
|
3453 |
"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
|
3454 |
for x :: "'a::len word" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3455 |
by simp_all |
37660 | 3456 |
|
3457 |
lemma word_ao_absorbs [simp]: |
|
3458 |
"x AND (y OR x) = x" |
|
3459 |
"x OR y AND x = x" |
|
3460 |
"x AND (x OR y) = x" |
|
3461 |
"y AND x OR x = x" |
|
3462 |
"(y OR x) AND x = x" |
|
3463 |
"x OR x AND y = x" |
|
3464 |
"(x OR y) AND x = x" |
|
3465 |
"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
|
3466 |
for x :: "'a::len word" |
72508 | 3467 |
by (auto intro: bit_eqI simp add: bit_and_iff bit_or_iff) |
37660 | 3468 |
|
71149 | 3469 |
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
|
3470 |
for x :: "'a::len word" |
72508 | 3471 |
by (fact bit.double_compl) |
37660 | 3472 |
|
65328 | 3473 |
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
|
3474 |
for x :: "'a::len word" |
72508 | 3475 |
by (fact bit.conj_disj_distrib2) |
37660 | 3476 |
|
65328 | 3477 |
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
|
3478 |
for x :: "'a::len word" |
72508 | 3479 |
by (fact bit.disj_conj_distrib2) |
3480 |
||
65328 | 3481 |
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
|
3482 |
for x :: "'a::len word" |
72508 | 3483 |
by (simp add: not_eq_complement) |
3484 |
||
65328 | 3485 |
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
|
3486 |
for x :: "'a::len word" |
47372 | 3487 |
by transfer (simp add: plus_and_or) |
37660 | 3488 |
|
65328 | 3489 |
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
|
3490 |
for x :: "'a::len word" |
65328 | 3491 |
by auto |
3492 |
||
3493 |
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
|
3494 |
for x' :: "'a::len word" |
65328 | 3495 |
by auto |
3496 |
||
3497 |
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
|
3498 |
for w w' :: "'a::len word" |
48196 | 3499 |
by (auto intro: leoa leao) |
37660 | 3500 |
|
65328 | 3501 |
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
|
3502 |
for x y :: "'a::len word" |
72488 | 3503 |
by (simp add: or_greater_eq uint_or word_le_def) |
37660 | 3504 |
|
71997 | 3505 |
lemmas le_word_or1 = xtrans(3) [OF word_bw_comms (2) le_word_or2] |
3506 |
lemmas word_and_le1 = xtrans(3) [OF word_ao_absorbs (4) [symmetric] le_word_or2] |
|
3507 |
lemmas word_and_le2 = xtrans(3) [OF word_ao_absorbs (8) [symmetric] le_word_or2] |
|
37660 | 3508 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3509 |
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
|
3510 |
\<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
|
3511 |
\<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
|
3512 |
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
|
3513 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3514 |
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
|
3515 |
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
|
3516 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3517 |
lemma bit_word_reverse_iff [bit_simps]: |
71990 | 3518 |
\<open>bit (word_reverse w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w (LENGTH('a) - Suc n)\<close> |
3519 |
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
|
3520 |
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
|
3521 |
(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
|
3522 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3523 |
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
|
3524 |
by (rule bit_word_eqI) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3525 |
(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
|
3526 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3527 |
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
|
3528 |
by (metis word_rev_rev) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3529 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3530 |
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
|
3531 |
by simp |
37660 | 3532 |
|
65328 | 3533 |
lemma uint_2p: "(0::'a::len word) < 2 ^ n \<Longrightarrow> uint (2 ^ n::'a::len word) = 2 ^ n" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3534 |
by (cases \<open>n < LENGTH('a)\<close>; transfer; force) |
37660 | 3535 |
|
65268 | 3536 |
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
|
3537 |
by (induct n) (simp_all add: wi_hom_syms) |
37660 | 3538 |
|
3539 |
||
61799 | 3540 |
subsubsection \<open>shift functions in terms of lists of bools\<close> |
37660 | 3541 |
|
71997 | 3542 |
text \<open>TODO: rules for \<^term>\<open>- (numeral n)\<close>\<close> |
3543 |
||
3544 |
lemma drop_bit_word_numeral [simp]: |
|
3545 |
\<open>drop_bit (numeral n) (numeral k) = |
|
3546 |
(word_of_int (drop_bit (numeral n) (take_bit LENGTH('a) (numeral k))) :: 'a::len word)\<close> |
|
3547 |
by transfer simp |
|
3548 |
||
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3549 |
lemma False_map2_or: "\<lbrakk>set xs \<subseteq> {False}; length ys = length xs\<rbrakk> \<Longrightarrow> map2 (\<or>) xs ys = ys" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3550 |
by (induction xs arbitrary: ys) (auto simp: length_Suc_conv) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3551 |
|
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3552 |
lemma align_lem_or: |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3553 |
assumes "length xs = n + m" "length ys = n + m" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3554 |
and "drop m xs = replicate n False" "take m ys = replicate m False" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3555 |
shows "map2 (\<or>) xs ys = take m xs @ drop m ys" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3556 |
using assms |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3557 |
proof (induction xs arbitrary: ys m) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3558 |
case (Cons a xs) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3559 |
then show ?case |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3560 |
by (cases m) (auto simp: length_Suc_conv False_map2_or) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3561 |
qed auto |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3562 |
|
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3563 |
lemma False_map2_and: "\<lbrakk>set xs \<subseteq> {False}; length ys = length xs\<rbrakk> \<Longrightarrow> map2 (\<and>) xs ys = xs" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3564 |
by (induction xs arbitrary: ys) (auto simp: length_Suc_conv) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3565 |
|
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3566 |
lemma align_lem_and: |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3567 |
assumes "length xs = n + m" "length ys = n + m" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3568 |
and "drop m xs = replicate n False" "take m ys = replicate m False" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3569 |
shows "map2 (\<and>) xs ys = replicate (n + m) False" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3570 |
using assms |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3571 |
proof (induction xs arbitrary: ys m) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3572 |
case (Cons a xs) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3573 |
then show ?case |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3574 |
by (cases m) (auto simp: length_Suc_conv set_replicate_conv_if False_map2_and) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3575 |
qed auto |
37660 | 3576 |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
3577 |
|
61799 | 3578 |
subsubsection \<open>Mask\<close> |
37660 | 3579 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3580 |
lemma minus_1_eq_mask: |
72082 | 3581 |
\<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
|
3582 |
by (rule bit_eqI) (simp add: bit_exp_iff bit_mask_iff exp_eq_zero_iff) |
72079 | 3583 |
|
3584 |
lemma mask_eq_decr_exp: |
|
72082 | 3585 |
\<open>mask n = 2 ^ n - (1 :: 'a::len word)\<close> |
3586 |
by (fact mask_eq_exp_minus_1) |
|
71953 | 3587 |
|
3588 |
lemma mask_Suc_rec: |
|
72082 | 3589 |
\<open>mask (Suc n) = 2 * mask n + (1 :: 'a::len word)\<close> |
3590 |
by (simp add: mask_eq_exp_minus_1) |
|
71953 | 3591 |
|
3592 |
context |
|
3593 |
begin |
|
3594 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3595 |
qualified lemma bit_mask_iff [bit_simps]: |
71990 | 3596 |
\<open>bit (mask m :: 'a::len word) n \<longleftrightarrow> n < min LENGTH('a) m\<close> |
72082 | 3597 |
by (simp add: bit_mask_iff exp_eq_zero_iff not_le) |
71953 | 3598 |
|
3599 |
end |
|
3600 |
||
72128 | 3601 |
lemma mask_bin: "mask n = word_of_int (take_bit n (- 1))" |
72488 | 3602 |
by transfer (simp add: take_bit_minus_one_eq_mask) |
37660 | 3603 |
|
72128 | 3604 |
lemma and_mask_bintr: "w AND mask n = word_of_int (take_bit n (uint w))" |
72488 | 3605 |
by transfer (simp add: ac_simps take_bit_eq_mask) |
37660 | 3606 |
|
72128 | 3607 |
lemma and_mask_wi: "word_of_int i AND mask n = word_of_int (take_bit n i)" |
72488 | 3608 |
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
|
3609 |
|
65328 | 3610 |
lemma and_mask_wi': |
72128 | 3611 |
"word_of_int i AND mask n = (word_of_int (take_bit (min LENGTH('a) n) i) :: 'a::len word)" |
72488 | 3612 |
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
|
3613 |
|
72128 | 3614 |
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
|
3615 |
unfolding word_numeral_alt by (rule and_mask_wi) |
37660 | 3616 |
|
45811 | 3617 |
lemma and_mask_mod_2p: "w AND mask n = word_of_int (uint w mod 2 ^ n)" |
72128 | 3618 |
by (simp only: and_mask_bintr take_bit_eq_mod) |
37660 | 3619 |
|
72130
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
3620 |
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
|
3621 |
\<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
|
3622 |
by transfer simp |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
3623 |
|
37660 | 3624 |
lemma and_mask_lt_2p: "uint (w AND mask n) < 2 ^ n" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3625 |
by (metis take_bit_eq_mask take_bit_int_less_exp unsigned_take_bit_eq) |
37660 | 3626 |
|
65363 | 3627 |
lemma mask_eq_iff: "w AND mask n = w \<longleftrightarrow> uint w < 2 ^ n" |
72292 | 3628 |
apply (auto simp flip: take_bit_eq_mask) |
3629 |
apply (metis take_bit_int_eq_self_iff uint_take_bit_eq) |
|
3630 |
apply (simp add: take_bit_int_eq_self unsigned_take_bit_eq word_uint_eqI) |
|
37660 | 3631 |
done |
3632 |
||
65328 | 3633 |
lemma and_mask_dvd: "2 ^ n dvd uint w \<longleftrightarrow> w AND mask n = 0" |
72262 | 3634 |
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 | 3635 |
|
65328 | 3636 |
lemma and_mask_dvd_nat: "2 ^ n dvd unat w \<longleftrightarrow> w AND mask n = 0" |
72262 | 3637 |
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 | 3638 |
|
65328 | 3639 |
lemma word_2p_lem: "n < size w \<Longrightarrow> w < 2 ^ n = (uint w < 2 ^ n)" |
3640 |
for w :: "'a::len word" |
|
72262 | 3641 |
by transfer simp |
37660 | 3642 |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3643 |
lemma less_mask_eq: |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3644 |
fixes x :: "'a::len word" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3645 |
assumes "x < 2 ^ n" shows "x AND mask n = x" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3646 |
by (metis (no_types) assms lt2p_lem mask_eq_iff not_less word_2p_lem word_size) |
37660 | 3647 |
|
45604 | 3648 |
lemmas mask_eq_iff_w2p = trans [OF mask_eq_iff word_2p_lem [symmetric]] |
3649 |
||
3650 |
lemmas and_mask_less' = iffD2 [OF word_2p_lem and_mask_lt_2p, simplified word_size] |
|
37660 | 3651 |
|
72082 | 3652 |
lemma and_mask_less_size: "n < size x \<Longrightarrow> x AND mask n < 2 ^ n" |
3653 |
for x :: \<open>'a::len word\<close> |
|
37660 | 3654 |
unfolding word_size by (erule and_mask_less') |
3655 |
||
65328 | 3656 |
lemma word_mod_2p_is_mask [OF refl]: "c = 2 ^ n \<Longrightarrow> c > 0 \<Longrightarrow> x mod c = x AND mask n" |
3657 |
for c x :: "'a::len word" |
|
3658 |
by (auto simp: word_mod_def uint_2p and_mask_mod_2p) |
|
37660 | 3659 |
|
3660 |
lemma mask_eqs: |
|
3661 |
"(a AND mask n) + b AND mask n = a + b AND mask n" |
|
3662 |
"a + (b AND mask n) AND mask n = a + b AND mask n" |
|
3663 |
"(a AND mask n) - b AND mask n = a - b AND mask n" |
|
3664 |
"a - (b AND mask n) AND mask n = a - b AND mask n" |
|
3665 |
"a * (b AND mask n) AND mask n = a * b AND mask n" |
|
3666 |
"(b AND mask n) * a AND mask n = b * a AND mask n" |
|
3667 |
"(a AND mask n) + (b AND mask n) AND mask n = a + b AND mask n" |
|
3668 |
"(a AND mask n) - (b AND mask n) AND mask n = a - b AND mask n" |
|
3669 |
"(a AND mask n) * (b AND mask n) AND mask n = a * b AND mask n" |
|
3670 |
"- (a AND mask n) AND mask n = - a AND mask n" |
|
3671 |
"word_succ (a AND mask n) AND mask n = word_succ a AND mask n" |
|
3672 |
"word_pred (a AND mask n) AND mask n = word_pred a AND mask n" |
|
3673 |
using word_of_int_Ex [where x=a] word_of_int_Ex [where x=b] |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3674 |
unfolding take_bit_eq_mask [symmetric] |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3675 |
by (transfer; simp add: take_bit_eq_mod mod_simps)+ |
65328 | 3676 |
|
3677 |
lemma mask_power_eq: "(x AND mask n) ^ k AND mask n = x ^ k AND mask n" |
|
72082 | 3678 |
for x :: \<open>'a::len word\<close> |
37660 | 3679 |
using word_of_int_Ex [where x=x] |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3680 |
unfolding take_bit_eq_mask [symmetric] |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3681 |
by (transfer; simp add: take_bit_eq_mod mod_simps)+ |
37660 | 3682 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
3683 |
lemma mask_full [simp]: "mask LENGTH('a) = (- 1 :: 'a::len word)" |
72079 | 3684 |
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
|
3685 |
|
37660 | 3686 |
|
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3687 |
subsubsection \<open>Slices\<close> |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3688 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3689 |
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
|
3690 |
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
|
3691 |
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
|
3692 |
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
|
3693 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3694 |
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
|
3695 |
\<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
|
3696 |
\<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
|
3697 |
for w :: \<open>'a::len word\<close> |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3698 |
by (auto simp add: slice1_def bit_ucast_iff bit_drop_bit_eq bit_push_bit_iff not_less not_le ac_simps |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3699 |
dest: bit_imp_le_length) |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3700 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3701 |
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
|
3702 |
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
|
3703 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3704 |
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
|
3705 |
\<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
|
3706 |
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
|
3707 |
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
|
3708 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3709 |
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
|
3710 |
unfolding slice1_def by simp |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3711 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3712 |
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
|
3713 |
unfolding slice_def by auto |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3714 |
|
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3715 |
lemma ucast_slice1: "ucast w = slice1 (size w) w" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3716 |
unfolding slice1_def by (simp add: size_word.rep_eq) |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3717 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3718 |
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
|
3719 |
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
|
3720 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3721 |
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
|
3722 |
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
|
3723 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3724 |
lemma rev_slice1: |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3725 |
\<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
|
3726 |
if \<open>n + k = LENGTH('a) + LENGTH('b)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3727 |
proof (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3728 |
fix m |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3729 |
assume *: \<open>m < LENGTH('a)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3730 |
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
|
3731 |
by simp |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3732 |
show \<open>bit (slice1 n (word_reverse w :: 'b word) :: 'a word) m \<longleftrightarrow> bit (word_reverse (slice1 k w :: 'a word)) m\<close> |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3733 |
unfolding bit_slice1_iff bit_word_reverse_iff |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3734 |
using * ** |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3735 |
by (cases \<open>n \<le> LENGTH('a)\<close>; cases \<open>k \<le> LENGTH('a)\<close>) auto |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3736 |
qed |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3737 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3738 |
lemma rev_slice: |
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3739 |
"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
|
3740 |
slice n (word_reverse (w::'b word)) = word_reverse (slice k w :: 'a word)" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3741 |
unfolding slice_def word_size |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3742 |
by (simp add: rev_slice1) |
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3743 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3744 |
|
61799 | 3745 |
subsubsection \<open>Revcast\<close> |
37660 | 3746 |
|
72027
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3747 |
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
|
3748 |
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
|
3749 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72515
diff
changeset
|
3750 |
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
|
3751 |
\<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
|
3752 |
\<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
|
3753 |
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
|
3754 |
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
|
3755 |
|
759532ef0885
prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents:
72010
diff
changeset
|
3756 |
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
|
3757 |
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
|
3758 |
|
65268 | 3759 |
lemma revcast_rev_ucast [OF refl refl refl]: |
3760 |
"cs = [rc, uc] \<Longrightarrow> rc = revcast (word_reverse w) \<Longrightarrow> uc = ucast w \<Longrightarrow> |
|
37660 | 3761 |
rc = word_reverse uc" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3762 |
by (metis rev_slice1 revcast_slice1 ucast_slice1 word_size) |
37660 | 3763 |
|
45811 | 3764 |
lemma revcast_ucast: "revcast w = word_reverse (ucast (word_reverse w))" |
3765 |
using revcast_rev_ucast [of "word_reverse w"] by simp |
|
3766 |
||
3767 |
lemma ucast_revcast: "ucast w = word_reverse (revcast (word_reverse w))" |
|
3768 |
by (fact revcast_rev_ucast [THEN word_rev_gal']) |
|
3769 |
||
3770 |
lemma ucast_rev_revcast: "ucast (word_reverse w) = word_reverse (revcast w)" |
|
3771 |
by (fact revcast_ucast [THEN word_rev_gal']) |
|
37660 | 3772 |
|
3773 |
||
65328 | 3774 |
text "linking revcast and cast via shift" |
37660 | 3775 |
|
3776 |
lemmas wsst_TYs = source_size target_size word_size |
|
3777 |
||
65268 | 3778 |
lemmas sym_notr = |
37660 | 3779 |
not_iff [THEN iffD2, THEN not_sym, THEN not_iff [THEN iffD1]] |
3780 |
||
3781 |
||
61799 | 3782 |
subsection \<open>Split and cat\<close> |
37660 | 3783 |
|
40827
abbc05c20e24
code preprocessor setup for numerals on word type;
haftmann
parents:
39910
diff
changeset
|
3784 |
lemmas word_split_bin' = word_split_def |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3785 |
lemmas word_cat_bin' = word_cat_eq |
37660 | 3786 |
|
65268 | 3787 |
\<comment> \<open>this odd result is analogous to \<open>ucast_id\<close>, |
61799 | 3788 |
result to the length given by the result type\<close> |
37660 | 3789 |
|
3790 |
lemma word_cat_id: "word_cat a b = b" |
|
72488 | 3791 |
by transfer (simp add: take_bit_concat_bit_eq) |
65336 | 3792 |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3793 |
lemma word_cat_split_alt: "\<lbrakk>size w \<le> size u + size v; word_split w = (u,v)\<rbrakk> \<Longrightarrow> word_cat u v = w" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3794 |
unfolding word_split_def |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3795 |
by (rule bit_word_eqI) (auto simp add: bit_word_cat_iff not_less word_size bit_ucast_iff bit_drop_bit_eq) |
37660 | 3796 |
|
45604 | 3797 |
lemmas word_cat_split_size = sym [THEN [2] word_cat_split_alt [symmetric]] |
37660 | 3798 |
|
3799 |
||
61799 | 3800 |
subsubsection \<open>Split and slice\<close> |
37660 | 3801 |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3802 |
lemma split_slices: |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3803 |
assumes "word_split w = (u, v)" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3804 |
shows "u = slice (size v) w \<and> v = slice 0 w" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3805 |
unfolding word_size |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3806 |
proof (intro conjI) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3807 |
have \<section>: "\<And>n. \<lbrakk>ucast (drop_bit LENGTH('b) w) = u; LENGTH('c) < LENGTH('b)\<rbrakk> \<Longrightarrow> \<not> bit u n" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3808 |
by (metis bit_take_bit_iff bit_word_of_int_iff diff_is_0_eq' drop_bit_take_bit less_imp_le less_nat_zero_code of_int_uint unsigned_drop_bit_eq) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3809 |
show "u = slice LENGTH('b) w" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3810 |
proof (rule bit_word_eqI) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3811 |
show "bit u n = bit ((slice LENGTH('b) w)::'a word) n" if "n < LENGTH('a)" for n |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3812 |
using assms bit_imp_le_length |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3813 |
unfolding word_split_def bit_slice_iff |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3814 |
by (fastforce simp add: \<section> ac_simps word_size bit_ucast_iff bit_drop_bit_eq) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3815 |
qed |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3816 |
show "v = slice 0 w" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3817 |
by (metis Pair_inject assms ucast_slice word_split_bin') |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3818 |
qed |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3819 |
|
37660 | 3820 |
|
45816
6a04efd99f25
replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents:
45811
diff
changeset
|
3821 |
lemma slice_cat1 [OF refl]: |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3822 |
"\<lbrakk>wc = word_cat a b; size a + size b \<le> size wc\<rbrakk> \<Longrightarrow> slice (size b) wc = a" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3823 |
by (rule bit_word_eqI) (auto simp add: bit_slice_iff bit_word_cat_iff word_size) |
37660 | 3824 |
|
3825 |
lemmas slice_cat2 = trans [OF slice_id word_cat_id] |
|
3826 |
||
3827 |
lemma cat_slices: |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3828 |
"\<lbrakk>a = slice n c; b = slice 0 c; n = size b; size c \<le> size a + size b\<rbrakk> \<Longrightarrow> word_cat a b = c" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3829 |
by (rule bit_word_eqI) (auto simp add: bit_slice_iff bit_word_cat_iff word_size) |
37660 | 3830 |
|
3831 |
lemma word_split_cat_alt: |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3832 |
assumes "w = word_cat u v" and size: "size u + size v \<le> size w" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3833 |
shows "word_split w = (u,v)" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3834 |
proof - |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3835 |
have "ucast ((drop_bit LENGTH('c) (word_cat u v))::'a word) = u" "ucast ((word_cat u v)::'a word) = v" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3836 |
using assms |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3837 |
by (auto simp add: word_size bit_ucast_iff bit_drop_bit_eq bit_word_cat_iff intro: bit_eqI) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3838 |
then show ?thesis |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3839 |
by (simp add: assms(1) word_split_bin') |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3840 |
qed |
37660 | 3841 |
|
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3842 |
lemma horner_sum_uint_exp_Cons_eq: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3843 |
\<open>horner_sum uint (2 ^ LENGTH('a)) (w # ws) = |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3844 |
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
|
3845 |
for ws :: \<open>'a::len word list\<close> |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3846 |
by (simp add: bintr_uint concat_bit_eq push_bit_eq_mult) |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3847 |
|
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3848 |
lemma bit_horner_sum_uint_exp_iff: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3849 |
\<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
|
3850 |
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
|
3851 |
for ws :: \<open>'a::len word list\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3852 |
proof (induction ws arbitrary: n) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3853 |
case Nil |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3854 |
then show ?case |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3855 |
by simp |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3856 |
next |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3857 |
case (Cons w ws) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3858 |
then show ?case |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3859 |
by (cases \<open>n \<ge> LENGTH('a)\<close>) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3860 |
(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
|
3861 |
qed |
37660 | 3862 |
|
3863 |
||
61799 | 3864 |
subsection \<open>Rotation\<close> |
37660 | 3865 |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3866 |
lemma word_rotr_word_rotr_eq: \<open>word_rotr m (word_rotr n w) = word_rotr (m + n) w\<close> |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3867 |
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
|
3868 |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3869 |
lemma word_rot_lem: "\<lbrakk>l + k = d + k mod l; n < l\<rbrakk> \<Longrightarrow> ((d + n) mod l) = n" for l::nat |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3870 |
by (metis (no_types, lifting) add.commute add.right_neutral add_diff_cancel_left' mod_if mod_mult_div_eq mod_mult_self2 mod_self) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3871 |
|
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3872 |
lemma word_rot_rl [simp]: \<open>word_rotl k (word_rotr k v) = v\<close> |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3873 |
proof (rule bit_word_eqI) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3874 |
show "bit (word_rotl k (word_rotr k v)) n = bit v n" if "n < LENGTH('a)" for n |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3875 |
using that |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3876 |
by (auto simp: word_rot_lem word_rotl_eq_word_rotr word_rotr_word_rotr_eq bit_word_rotr_iff algebra_simps split: nat_diff_split) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3877 |
qed |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3878 |
|
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3879 |
lemma word_rot_lr [simp]: \<open>word_rotr k (word_rotl k v) = v\<close> |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3880 |
proof (rule bit_word_eqI) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3881 |
show "bit (word_rotr k (word_rotl k v)) n = bit v n" if "n < LENGTH('a)" for n |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3882 |
using that |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3883 |
by (auto simp add: word_rot_lem word_rotl_eq_word_rotr word_rotr_word_rotr_eq bit_word_rotr_iff algebra_simps split: nat_diff_split) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3884 |
qed |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3885 |
|
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3886 |
lemma word_rot_gal: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3887 |
\<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
|
3888 |
by auto |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3889 |
|
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3890 |
lemma word_rot_gal': |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3891 |
\<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
|
3892 |
by auto |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3893 |
|
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3894 |
lemma word_rotr_rev: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3895 |
\<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
|
3896 |
proof (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3897 |
fix m |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3898 |
assume \<open>m < LENGTH('a)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3899 |
moreover have \<open>1 + |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3900 |
((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
|
3901 |
((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
|
3902 |
int LENGTH('a)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3903 |
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
|
3904 |
int LENGTH('a) = 0\<close>) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3905 |
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
|
3906 |
apply simp_all |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3907 |
apply (auto simp add: algebra_simps) |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3908 |
apply (metis (mono_tags, hide_lams) Abs_fnat_hom_add mod_Suc mod_mult_self2_is_0 of_nat_Suc of_nat_mod semiring_char_0_class.of_nat_neq_0) |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3909 |
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
|
3910 |
done |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3911 |
then have \<open>int ((m + n) mod LENGTH('a)) = |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3912 |
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
|
3913 |
using \<open>m < LENGTH('a)\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3914 |
by (simp only: of_nat_mod mod_simps) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3915 |
(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
|
3916 |
then have \<open>(m + n) mod LENGTH('a) = |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3917 |
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
|
3918 |
by simp |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3919 |
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
|
3920 |
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
|
3921 |
qed |
65268 | 3922 |
|
37660 | 3923 |
lemma word_roti_0 [simp]: "word_roti 0 w = w" |
72079 | 3924 |
by transfer simp |
37660 | 3925 |
|
65336 | 3926 |
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
|
3927 |
by (rule bit_word_eqI) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3928 |
(simp add: bit_word_roti_iff nat_less_iff mod_simps ac_simps) |
65268 | 3929 |
|
67118 | 3930 |
lemma word_roti_conv_mod': |
3931 |
"word_roti n w = word_roti (n mod int (size w)) w" |
|
72079 | 3932 |
by transfer simp |
37660 | 3933 |
|
3934 |
lemmas word_roti_conv_mod = word_roti_conv_mod' [unfolded word_size] |
|
3935 |
||
3936 |
||
61799 | 3937 |
subsubsection \<open>"Word rotation commutes with bit-wise operations\<close> |
37660 | 3938 |
|
67408 | 3939 |
\<comment> \<open>using locale to not pollute lemma namespace\<close> |
65268 | 3940 |
locale word_rotate |
37660 | 3941 |
begin |
3942 |
||
3943 |
lemma word_rot_logs: |
|
71149 | 3944 |
"word_rotl n (NOT v) = NOT (word_rotl n v)" |
3945 |
"word_rotr n (NOT v) = NOT (word_rotr n v)" |
|
37660 | 3946 |
"word_rotl n (x AND y) = word_rotl n x AND word_rotl n y" |
3947 |
"word_rotr n (x AND y) = word_rotr n x AND word_rotr n y" |
|
3948 |
"word_rotl n (x OR y) = word_rotl n x OR word_rotl n y" |
|
3949 |
"word_rotr n (x OR y) = word_rotr n x OR word_rotr n y" |
|
3950 |
"word_rotl n (x XOR y) = word_rotl n x XOR word_rotl n y" |
|
65268 | 3951 |
"word_rotr n (x XOR y) = word_rotr n x XOR word_rotr n y" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
3952 |
by (rule bit_word_eqI, auto simp add: bit_word_rotl_iff bit_word_rotr_iff bit_and_iff bit_or_iff bit_xor_iff bit_not_iff algebra_simps not_le)+ |
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72083
diff
changeset
|
3953 |
|
37660 | 3954 |
end |
3955 |
||
3956 |
lemmas word_rot_logs = word_rotate.word_rot_logs |
|
3957 |
||
65336 | 3958 |
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
|
3959 |
by transfer simp_all |
37660 | 3960 |
|
3961 |
lemma word_roti_0' [simp] : "word_roti n 0 = 0" |
|
72079 | 3962 |
by transfer simp |
37660 | 3963 |
|
72079 | 3964 |
declare word_roti_eq_word_rotr_word_rotl [simp] |
37660 | 3965 |
|
3966 |
||
61799 | 3967 |
subsection \<open>Maximum machine word\<close> |
37660 | 3968 |
|
3969 |
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
|
3970 |
fixes x :: "'a::len word" |
70185 | 3971 |
obtains n where "x = word_of_int n" and "0 \<le> n" and "n < 2^LENGTH('a)" |
72292 | 3972 |
by (rule that [of \<open>uint x\<close>]) simp_all |
37660 | 3973 |
|
3974 |
lemma word_nat_cases [cases type: word]: |
|
65336 | 3975 |
fixes x :: "'a::len word" |
70185 | 3976 |
obtains n where "x = of_nat n" and "n < 2^LENGTH('a)" |
72292 | 3977 |
by (rule that [of \<open>unat x\<close>]) simp_all |
37660 | 3978 |
|
73788 | 3979 |
lemma max_word_max [intro!]: |
3980 |
\<open>n \<le> - 1\<close> for n :: \<open>'a::len word\<close> |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71955
diff
changeset
|
3981 |
by (fact word_order.extremum) |
65268 | 3982 |
|
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
|
3983 |
lemma word_of_int_2p_len: "word_of_int (2 ^ LENGTH('a)) = (0::'a::len word)" |
72292 | 3984 |
by simp |
37660 | 3985 |
|
70185 | 3986 |
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
|
3987 |
by (fact word_exp_length_eq_0) |
37660 | 3988 |
|
73788 | 3989 |
lemma max_word_wrap: |
3990 |
\<open>x + 1 = 0 \<Longrightarrow> x = - 1\<close> for x :: \<open>'a::len word\<close> |
|
71946 | 3991 |
by (simp add: eq_neg_iff_add_eq_0) |
3992 |
||
73788 | 3993 |
lemma word_and_max: |
3994 |
\<open>x AND - 1 = x\<close> for x :: \<open>'a::len word\<close> |
|
71946 | 3995 |
by (fact word_log_esimps) |
3996 |
||
73788 | 3997 |
lemma word_or_max: |
3998 |
\<open>x OR - 1 = - 1\<close> for x :: \<open>'a::len word\<close> |
|
71946 | 3999 |
by (fact word_log_esimps) |
37660 | 4000 |
|
65336 | 4001 |
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
|
4002 |
for x y z :: "'a::len word" |
72508 | 4003 |
by (fact bit.conj_disj_distrib) |
37660 | 4004 |
|
65336 | 4005 |
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
|
4006 |
for x y z :: "'a::len word" |
72508 | 4007 |
by (fact bit.disj_conj_distrib) |
37660 | 4008 |
|
65336 | 4009 |
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
|
4010 |
for x :: "'a::len word" |
72508 | 4011 |
by (fact bit.conj_cancel_right) |
37660 | 4012 |
|
73788 | 4013 |
lemma word_or_not [simp]: |
4014 |
\<open>x OR NOT x = - 1\<close> for x :: \<open>'a::len word\<close> |
|
72508 | 4015 |
by (fact bit.disj_cancel_right) |
37660 | 4016 |
|
65336 | 4017 |
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
|
4018 |
for x y :: "'a::len word" |
72508 | 4019 |
by (fact bit.xor_def) |
37660 | 4020 |
|
65336 | 4021 |
lemma uint_lt_0 [simp]: "uint x < 0 = False" |
37660 | 4022 |
by (simp add: linorder_not_less) |
4023 |
||
65336 | 4024 |
lemma word_less_1 [simp]: "x < 1 \<longleftrightarrow> x = 0" |
4025 |
for x :: "'a::len word" |
|
37660 | 4026 |
by (simp add: word_less_nat_alt unat_0_iff) |
4027 |
||
4028 |
lemma uint_plus_if_size: |
|
65268 | 4029 |
"uint (x + y) = |
65336 | 4030 |
(if uint x + uint y < 2^size x |
4031 |
then uint x + uint y |
|
4032 |
else uint x + uint y - 2^size x)" |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4033 |
by (simp add: take_bit_eq_mod word_size uint_word_of_int_eq uint_plus_if') |
37660 | 4034 |
|
4035 |
lemma unat_plus_if_size: |
|
65363 | 4036 |
"unat (x + y) = |
65336 | 4037 |
(if unat x + unat y < 2^size x |
4038 |
then unat x + unat y |
|
4039 |
else unat x + unat y - 2^size x)" |
|
65363 | 4040 |
for x y :: "'a::len word" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4041 |
by (simp add: size_word.rep_eq unat_arith_simps) |
37660 | 4042 |
|
65336 | 4043 |
lemma word_neq_0_conv: "w \<noteq> 0 \<longleftrightarrow> 0 < w" |
4044 |
for w :: "'a::len word" |
|
72262 | 4045 |
by (fact word_coorder.not_eq_extremum) |
65336 | 4046 |
|
4047 |
lemma max_lt: "unat (max a b div c) = unat (max a b) div unat c" |
|
4048 |
for c :: "'a::len word" |
|
55818 | 4049 |
by (fact unat_div) |
37660 | 4050 |
|
4051 |
lemma uint_sub_if_size: |
|
65268 | 4052 |
"uint (x - y) = |
65336 | 4053 |
(if uint y \<le> uint x |
4054 |
then uint x - uint y |
|
4055 |
else uint x - uint y + 2^size x)" |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4056 |
by (simp add: size_word.rep_eq uint_sub_if') |
65336 | 4057 |
|
72130
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4058 |
lemma unat_sub: |
9e5862223442
dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents:
72128
diff
changeset
|
4059 |
\<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
|
4060 |
if \<open>b \<le> a\<close> |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4061 |
by (meson that unat_sub_if_size word_le_nat_alt) |
37660 | 4062 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4063 |
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
|
4064 |
lemmas word_le_sub1_numberof [simp] = word_le_sub1 [of "numeral w"] for w |
65268 | 4065 |
|
70185 | 4066 |
lemma word_of_int_minus: "word_of_int (2^LENGTH('a) - i) = (word_of_int (-i)::'a::len word)" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4067 |
by simp |
72292 | 4068 |
|
4069 |
lemma word_of_int_inj: |
|
4070 |
\<open>(word_of_int x :: 'a::len word) = word_of_int y \<longleftrightarrow> x = y\<close> |
|
4071 |
if \<open>0 \<le> x \<and> x < 2 ^ LENGTH('a)\<close> \<open>0 \<le> y \<and> y < 2 ^ LENGTH('a)\<close> |
|
4072 |
using that by (transfer fixing: x y) (simp add: take_bit_int_eq_self) |
|
37660 | 4073 |
|
65336 | 4074 |
lemma word_le_less_eq: "x \<le> y \<longleftrightarrow> x = y \<or> x < y" |
4075 |
for x y :: "'z::len word" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46962
diff
changeset
|
4076 |
by (auto simp add: order_class.le_less) |
37660 | 4077 |
|
4078 |
lemma mod_plus_cong: |
|
65336 | 4079 |
fixes b b' :: int |
4080 |
assumes 1: "b = b'" |
|
4081 |
and 2: "x mod b' = x' mod b'" |
|
4082 |
and 3: "y mod b' = y' mod b'" |
|
4083 |
and 4: "x' + y' = z'" |
|
37660 | 4084 |
shows "(x + y) mod b = z' mod b'" |
4085 |
proof - |
|
4086 |
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
|
4087 |
by (simp add: mod_add_eq) |
37660 | 4088 |
also have "\<dots> = (x' + y') mod b'" |
64593
50c715579715
reoriented congruence rules in non-explosive direction
haftmann
parents:
64243
diff
changeset
|
4089 |
by (simp add: mod_add_eq) |
65336 | 4090 |
finally show ?thesis |
4091 |
by (simp add: 4) |
|
37660 | 4092 |
qed |
4093 |
||
4094 |
lemma mod_minus_cong: |
|
65336 | 4095 |
fixes b b' :: int |
4096 |
assumes "b = b'" |
|
4097 |
and "x mod b' = x' mod b'" |
|
4098 |
and "y mod b' = y' mod b'" |
|
4099 |
and "x' - y' = z'" |
|
37660 | 4100 |
shows "(x - y) mod b = z' mod b'" |
65336 | 4101 |
using assms [symmetric] by (auto intro: mod_diff_cong) |
4102 |
||
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4103 |
lemma word_induct_less [case_names zero less]: |
72262 | 4104 |
\<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> |
4105 |
for m :: \<open>'a::len word\<close> |
|
4106 |
proof - |
|
4107 |
define q where \<open>q = unat m\<close> |
|
4108 |
with less have \<open>\<And>n. n < word_of_nat q \<Longrightarrow> P n \<Longrightarrow> P (1 + n)\<close> |
|
4109 |
by simp |
|
4110 |
then have \<open>P (word_of_nat q :: 'a word)\<close> |
|
4111 |
proof (induction q) |
|
4112 |
case 0 |
|
4113 |
show ?case |
|
4114 |
by (simp add: zero) |
|
4115 |
next |
|
4116 |
case (Suc q) |
|
4117 |
show ?case |
|
4118 |
proof (cases \<open>1 + word_of_nat q = (0 :: 'a word)\<close>) |
|
4119 |
case True |
|
4120 |
then show ?thesis |
|
4121 |
by (simp add: zero) |
|
4122 |
next |
|
4123 |
case False |
|
4124 |
then have *: \<open>word_of_nat q < (word_of_nat (Suc q) :: 'a word)\<close> |
|
4125 |
by (simp add: unatSuc word_less_nat_alt) |
|
4126 |
then have **: \<open>n < (1 + word_of_nat q :: 'a word) \<longleftrightarrow> n \<le> (word_of_nat q :: 'a word)\<close> for n |
|
4127 |
by (metis (no_types, lifting) add.commute inc_le le_less_trans not_less of_nat_Suc) |
|
4128 |
have \<open>P (word_of_nat q)\<close> |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4129 |
by (simp add: "**" Suc.IH Suc.prems) |
72262 | 4130 |
with * have \<open>P (1 + word_of_nat q)\<close> |
4131 |
by (rule Suc.prems) |
|
4132 |
then show ?thesis |
|
4133 |
by simp |
|
4134 |
qed |
|
4135 |
qed |
|
4136 |
with \<open>q = unat m\<close> show ?thesis |
|
4137 |
by simp |
|
4138 |
qed |
|
65268 | 4139 |
|
65363 | 4140 |
lemma word_induct: "P 0 \<Longrightarrow> (\<And>n. P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P m" |
65336 | 4141 |
for P :: "'a::len word \<Rightarrow> bool" |
72262 | 4142 |
by (rule word_induct_less) |
65336 | 4143 |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4144 |
lemma word_induct2 [case_names zero suc, induct type]: "P 0 \<Longrightarrow> (\<And>n. 1 + n \<noteq> 0 \<Longrightarrow> P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P n" |
65336 | 4145 |
for P :: "'b::len word \<Rightarrow> bool" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4146 |
by (induction rule: word_induct_less; force) |
37660 | 4147 |
|
55816
e8dd03241e86
cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents:
55415
diff
changeset
|
4148 |
|
61799 | 4149 |
subsection \<open>Recursion combinator for words\<close> |
46010 | 4150 |
|
54848 | 4151 |
definition word_rec :: "'a \<Rightarrow> ('b::len word \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'b word \<Rightarrow> 'a" |
65336 | 4152 |
where "word_rec forZero forSuc n = rec_nat forZero (forSuc \<circ> of_nat) (unat n)" |
37660 | 4153 |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4154 |
lemma word_rec_0 [simp]: "word_rec z s 0 = z" |
37660 | 4155 |
by (simp add: word_rec_def) |
4156 |
||
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4157 |
lemma word_rec_Suc [simp]: "1 + n \<noteq> 0 \<Longrightarrow> word_rec z s (1 + n) = s n (word_rec z s n)" |
65363 | 4158 |
for n :: "'a::len word" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4159 |
by (simp add: unatSuc word_rec_def) |
37660 | 4160 |
|
65363 | 4161 |
lemma word_rec_Pred: "n \<noteq> 0 \<Longrightarrow> word_rec z s n = s (n - 1) (word_rec z s (n - 1))" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4162 |
by (metis add.commute diff_add_cancel word_rec_Suc) |
37660 | 4163 |
|
65336 | 4164 |
lemma word_rec_in: "f (word_rec z (\<lambda>_. f) n) = word_rec (f z) (\<lambda>_. f) n" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4165 |
by (induct n) (simp_all add: word_rec_Suc) |
37660 | 4166 |
|
67399 | 4167 |
lemma word_rec_in2: "f n (word_rec z f n) = word_rec (f 0 z) (f \<circ> (+) 1) n" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4168 |
by (induct n) (simp_all add: word_rec_Suc) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4169 |
|
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4170 |
|
37660 | 4171 |
|
65268 | 4172 |
lemma word_rec_twice: |
67399 | 4173 |
"m \<le> n \<Longrightarrow> word_rec z f n = word_rec (word_rec z f (n - m)) (f \<circ> (+) (n - m)) m" |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4174 |
proof (induction n arbitrary: z f) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4175 |
case zero |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4176 |
then show ?case |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4177 |
by (metis diff_0_right word_le_0_iff word_rec_0) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4178 |
next |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4179 |
case (suc n z f) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4180 |
show ?case |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4181 |
proof (cases "1 + (n - m) = 0") |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4182 |
case True |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4183 |
then show ?thesis |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4184 |
by (simp add: add_diff_eq) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4185 |
next |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4186 |
case False |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4187 |
then have eq: "1 + n - m = 1 + (n - m)" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4188 |
by simp |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4189 |
with False have "m \<le> n" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4190 |
by (metis "suc.prems" add.commute dual_order.antisym eq_iff_diff_eq_0 inc_le leI) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4191 |
with False "suc.hyps" show ?thesis |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4192 |
using suc.IH [of "f 0 z" "f \<circ> (+) 1"] |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4193 |
by (simp add: word_rec_in2 eq add.assoc o_def) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4194 |
qed |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4195 |
qed |
37660 | 4196 |
|
4197 |
lemma word_rec_id: "word_rec z (\<lambda>_. id) n = z" |
|
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4198 |
by (induct n) auto |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4199 |
|
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4200 |
lemma word_rec_id_eq: "(\<And>m. m < n \<Longrightarrow> f m = id) \<Longrightarrow> word_rec z f n = z" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4201 |
by (induction n) (auto simp add: unatSuc unat_arith_simps(2)) |
37660 | 4202 |
|
65268 | 4203 |
lemma word_rec_max: |
72735
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4204 |
assumes "\<forall>m\<ge>n. m \<noteq> - 1 \<longrightarrow> f m = id" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4205 |
shows "word_rec z f (- 1) = word_rec z f n" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4206 |
proof - |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4207 |
have \<section>: "\<And>m. \<lbrakk>m < - 1 - n\<rbrakk> \<Longrightarrow> (f \<circ> (+) n) m = id" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4208 |
using assms |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4209 |
by (metis (mono_tags, lifting) add.commute add_diff_cancel_left' comp_apply less_le olen_add_eqv plus_minus_no_overflow word_n1_ge) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4210 |
have "word_rec z f (- 1) = word_rec (word_rec z f (- 1 - (- 1 - n))) (f \<circ> (+) (- 1 - (- 1 - n))) (- 1 - n)" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4211 |
by (meson word_n1_ge word_rec_twice) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4212 |
also have "... = word_rec z f n" |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4213 |
by (metis (no_types, lifting) \<section> diff_add_cancel minus_diff_eq uminus_add_conv_diff word_rec_id_eq) |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4214 |
finally show ?thesis . |
bbe5d3ef2052
Stepan Holub's stronger version of comm_append_are_replicate, and a de-applied Word.thy
paulson <lp15@cam.ac.uk>
parents:
72611
diff
changeset
|
4215 |
qed |
65336 | 4216 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4217 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4218 |
subsection \<open>More\<close> |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4219 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4220 |
lemma mask_1: "mask 1 = 1" |
72512 | 4221 |
by simp |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4222 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4223 |
lemma mask_Suc_0: "mask (Suc 0) = 1" |
72512 | 4224 |
by simp |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4225 |
|
72488 | 4226 |
lemma bin_last_bintrunc: "odd (take_bit l n) \<longleftrightarrow> l > 0 \<and> odd n" |
72079 | 4227 |
by simp |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4228 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
4229 |
|
72512 | 4230 |
lemma push_bit_word_beyond [simp]: |
4231 |
\<open>push_bit n w = 0\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close> |
|
4232 |
using that by (transfer fixing: n) (simp add: take_bit_push_bit) |
|
4233 |
||
4234 |
lemma drop_bit_word_beyond [simp]: |
|
4235 |
\<open>drop_bit n w = 0\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close> |
|
4236 |
using that by (transfer fixing: n) (simp add: drop_bit_take_bit) |
|
4237 |
||
4238 |
lemma signed_drop_bit_beyond: |
|
4239 |
\<open>signed_drop_bit n w = (if bit w (LENGTH('a) - Suc 0) then - 1 else 0)\<close> |
|
4240 |
if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close> |
|
4241 |
by (rule bit_word_eqI) (simp add: bit_signed_drop_bit_iff that) |
|
4242 |
||
4243 |
||
72489 | 4244 |
subsection \<open>SMT support\<close> |
4245 |
||
69605 | 4246 |
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
|
4247 |
|
41060
4199fdcfa3c0
moved smt_word.ML into the directory of the Word library
boehmes
parents:
40827
diff
changeset
|
4248 |
end |