author | haftmann |
Thu, 31 Oct 2019 09:02:02 +0000 | |
changeset 70973 | a7a52ba0717d |
parent 70927 | cc204e10385c |
permissions | -rw-r--r-- |
64015 | 1 |
(* Author: Florian Haftmann, TUM |
2 |
*) |
|
3 |
||
4 |
section \<open>Proof of concept for algebraically founded bit word types\<close> |
|
5 |
||
6 |
theory Word_Type |
|
7 |
imports |
|
8 |
Main |
|
70925 | 9 |
"HOL-ex.Bit_Lists" |
66453
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents:
64593
diff
changeset
|
10 |
"HOL-Library.Type_Length" |
64015 | 11 |
begin |
12 |
||
70925 | 13 |
subsection \<open>Preliminaries\<close> |
14 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
15 |
lemma take_bit_uminus: |
70171 | 16 |
"take_bit n (- (take_bit n k)) = take_bit n (- k)" for k :: int |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
17 |
by (simp add: take_bit_eq_mod mod_minus_eq) |
64015 | 18 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
19 |
lemma take_bit_minus: |
70171 | 20 |
"take_bit n (take_bit n k - take_bit n l) = take_bit n (k - l)" for k l :: int |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
21 |
by (simp add: take_bit_eq_mod mod_diff_eq) |
64015 | 22 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
23 |
lemma take_bit_nonnegative [simp]: |
70171 | 24 |
"take_bit n k \<ge> 0" for k :: int |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
25 |
by (simp add: take_bit_eq_mod) |
64015 | 26 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
27 |
definition signed_take_bit :: "nat \<Rightarrow> int \<Rightarrow> int" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
28 |
where signed_take_bit_eq_take_bit: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
29 |
"signed_take_bit n k = take_bit (Suc n) (k + 2 ^ n) - 2 ^ n" |
64015 | 30 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
31 |
lemma signed_take_bit_eq_take_bit': |
70171 | 32 |
"signed_take_bit (n - Suc 0) k = take_bit n (k + 2 ^ (n - 1)) - 2 ^ (n - 1)" if "n > 0" |
33 |
using that by (simp add: signed_take_bit_eq_take_bit) |
|
64015 | 34 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
35 |
lemma signed_take_bit_0 [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
36 |
"signed_take_bit 0 k = - (k mod 2)" |
64015 | 37 |
proof (cases "even k") |
38 |
case True |
|
39 |
then have "odd (k + 1)" |
|
40 |
by simp |
|
41 |
then have "(k + 1) mod 2 = 1" |
|
42 |
by (simp add: even_iff_mod_2_eq_zero) |
|
43 |
with True show ?thesis |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
44 |
by (simp add: signed_take_bit_eq_take_bit) |
64015 | 45 |
next |
46 |
case False |
|
47 |
then show ?thesis |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
48 |
by (simp add: signed_take_bit_eq_take_bit odd_iff_mod_2_eq_one) |
64015 | 49 |
qed |
50 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
51 |
lemma signed_take_bit_Suc [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
52 |
"signed_take_bit (Suc n) k = signed_take_bit n (k div 2) * 2 + k mod 2" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
53 |
by (simp add: odd_iff_mod_2_eq_one signed_take_bit_eq_take_bit algebra_simps) |
64015 | 54 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
55 |
lemma signed_take_bit_of_0 [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
56 |
"signed_take_bit n 0 = 0" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
57 |
by (simp add: signed_take_bit_eq_take_bit take_bit_eq_mod) |
64015 | 58 |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
59 |
lemma signed_take_bit_of_minus_1 [simp]: |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
60 |
"signed_take_bit n (- 1) = - 1" |
64015 | 61 |
by (induct n) simp_all |
62 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
63 |
lemma signed_take_bit_eq_iff_take_bit_eq: |
70171 | 64 |
"signed_take_bit (n - Suc 0) k = signed_take_bit (n - Suc 0) l \<longleftrightarrow> take_bit n k = take_bit n l" (is "?P \<longleftrightarrow> ?Q") |
65 |
if "n > 0" |
|
64015 | 66 |
proof - |
70171 | 67 |
from that obtain m where m: "n = Suc m" |
64015 | 68 |
by (cases n) auto |
69 |
show ?thesis |
|
70 |
proof |
|
71 |
assume ?Q |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
72 |
have "take_bit (Suc m) (k + 2 ^ m) = |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
73 |
take_bit (Suc m) (take_bit (Suc m) k + take_bit (Suc m) (2 ^ m))" |
67961 | 74 |
by (simp only: take_bit_add) |
64015 | 75 |
also have "\<dots> = |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
76 |
take_bit (Suc m) (take_bit (Suc m) l + take_bit (Suc m) (2 ^ m))" |
64015 | 77 |
by (simp only: \<open>?Q\<close> m [symmetric]) |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
78 |
also have "\<dots> = take_bit (Suc m) (l + 2 ^ m)" |
67961 | 79 |
by (simp only: take_bit_add) |
64015 | 80 |
finally show ?P |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
81 |
by (simp only: signed_take_bit_eq_take_bit m) simp |
64015 | 82 |
next |
83 |
assume ?P |
|
70171 | 84 |
with that have "(k + 2 ^ (n - Suc 0)) mod 2 ^ n = (l + 2 ^ (n - Suc 0)) mod 2 ^ n" |
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
85 |
by (simp add: signed_take_bit_eq_take_bit' take_bit_eq_mod) |
64015 | 86 |
then have "(i + (k + 2 ^ (n - Suc 0))) mod 2 ^ n = (i + (l + 2 ^ (n - Suc 0))) mod 2 ^ n" for i |
87 |
by (metis mod_add_eq) |
|
88 |
then have "k mod 2 ^ n = l mod 2 ^ n" |
|
89 |
by (metis add_diff_cancel_right' uminus_add_conv_diff) |
|
90 |
then show ?Q |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
91 |
by (simp add: take_bit_eq_mod) |
64015 | 92 |
qed |
93 |
qed |
|
94 |
||
95 |
||
96 |
subsection \<open>Bit strings as quotient type\<close> |
|
97 |
||
98 |
subsubsection \<open>Basic properties\<close> |
|
99 |
||
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
100 |
quotient_type (overloaded) 'a word = int / "\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len0) l" |
64015 | 101 |
by (auto intro!: equivpI reflpI sympI transpI) |
102 |
||
103 |
instantiation word :: (len0) "{semiring_numeral, comm_semiring_0, comm_ring}" |
|
104 |
begin |
|
105 |
||
106 |
lift_definition zero_word :: "'a word" |
|
107 |
is 0 |
|
108 |
. |
|
109 |
||
110 |
lift_definition one_word :: "'a word" |
|
111 |
is 1 |
|
112 |
. |
|
113 |
||
114 |
lift_definition plus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
115 |
is plus |
|
67961 | 116 |
by (subst take_bit_add [symmetric]) (simp add: take_bit_add) |
64015 | 117 |
|
118 |
lift_definition uminus_word :: "'a word \<Rightarrow> 'a word" |
|
119 |
is uminus |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
120 |
by (subst take_bit_uminus [symmetric]) (simp add: take_bit_uminus) |
64015 | 121 |
|
122 |
lift_definition minus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
123 |
is minus |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
124 |
by (subst take_bit_minus [symmetric]) (simp add: take_bit_minus) |
64015 | 125 |
|
126 |
lift_definition times_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
127 |
is times |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
128 |
by (auto simp add: take_bit_eq_mod intro: mod_mult_cong) |
64015 | 129 |
|
130 |
instance |
|
131 |
by standard (transfer; simp add: algebra_simps)+ |
|
132 |
||
133 |
end |
|
134 |
||
135 |
instance word :: (len) comm_ring_1 |
|
136 |
by standard (transfer; simp)+ |
|
137 |
||
70903 | 138 |
quickcheck_generator word |
139 |
constructors: |
|
140 |
"zero_class.zero :: ('a::len0) word", |
|
141 |
"numeral :: num \<Rightarrow> ('a::len0) word", |
|
142 |
"uminus :: ('a::len0) word \<Rightarrow> ('a::len0) word" |
|
143 |
||
70973 | 144 |
context |
145 |
includes lifting_syntax |
|
146 |
notes power_transfer [transfer_rule] |
|
147 |
begin |
|
148 |
||
149 |
lemma power_transfer_word [transfer_rule]: |
|
150 |
\<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close> |
|
151 |
by transfer_prover |
|
152 |
||
153 |
end |
|
154 |
||
64015 | 155 |
|
156 |
subsubsection \<open>Conversions\<close> |
|
157 |
||
70927 | 158 |
context |
159 |
includes lifting_syntax |
|
160 |
notes transfer_rule_numeral [transfer_rule] |
|
161 |
transfer_rule_of_nat [transfer_rule] |
|
162 |
transfer_rule_of_int [transfer_rule] |
|
163 |
begin |
|
70348
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
164 |
|
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
165 |
lemma [transfer_rule]: |
70927 | 166 |
"((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) numeral numeral" |
167 |
by transfer_prover |
|
168 |
||
169 |
lemma [transfer_rule]: |
|
170 |
"((=) ===> pcr_word) int of_nat" |
|
171 |
by transfer_prover |
|
64015 | 172 |
|
173 |
lemma [transfer_rule]: |
|
70927 | 174 |
"((=) ===> pcr_word) (\<lambda>k. k) of_int" |
64015 | 175 |
proof - |
70927 | 176 |
have "((=) ===> pcr_word) of_int of_int" |
64015 | 177 |
by transfer_prover |
178 |
then show ?thesis by (simp add: id_def) |
|
179 |
qed |
|
180 |
||
70927 | 181 |
end |
182 |
||
70973 | 183 |
lemma abs_word_eq: |
184 |
"abs_word = of_int" |
|
185 |
by (rule ext) (transfer, rule) |
|
186 |
||
64015 | 187 |
context semiring_1 |
188 |
begin |
|
189 |
||
190 |
lift_definition unsigned :: "'b::len0 word \<Rightarrow> 'a" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
191 |
is "of_nat \<circ> nat \<circ> take_bit LENGTH('b)" |
64015 | 192 |
by simp |
193 |
||
194 |
lemma unsigned_0 [simp]: |
|
195 |
"unsigned 0 = 0" |
|
196 |
by transfer simp |
|
197 |
||
198 |
end |
|
199 |
||
200 |
context semiring_char_0 |
|
201 |
begin |
|
202 |
||
203 |
lemma word_eq_iff_unsigned: |
|
204 |
"a = b \<longleftrightarrow> unsigned a = unsigned b" |
|
205 |
by safe (transfer; simp add: eq_nat_nat_iff) |
|
206 |
||
207 |
end |
|
208 |
||
70903 | 209 |
instantiation word :: (len0) equal |
210 |
begin |
|
211 |
||
212 |
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" |
|
213 |
where "equal_word a b \<longleftrightarrow> (unsigned a :: int) = unsigned b" |
|
214 |
||
215 |
instance proof |
|
216 |
fix a b :: "'a word" |
|
217 |
show "HOL.equal a b \<longleftrightarrow> a = b" |
|
218 |
using word_eq_iff_unsigned [of a b] by (auto simp add: equal_word_def) |
|
219 |
qed |
|
220 |
||
221 |
end |
|
222 |
||
64015 | 223 |
context ring_1 |
224 |
begin |
|
225 |
||
226 |
lift_definition signed :: "'b::len word \<Rightarrow> 'a" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
227 |
is "of_int \<circ> signed_take_bit (LENGTH('b) - 1)" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
228 |
by (simp add: signed_take_bit_eq_iff_take_bit_eq [symmetric]) |
64015 | 229 |
|
230 |
lemma signed_0 [simp]: |
|
231 |
"signed 0 = 0" |
|
232 |
by transfer simp |
|
233 |
||
234 |
end |
|
235 |
||
236 |
lemma unsigned_of_nat [simp]: |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
237 |
"unsigned (of_nat n :: 'a word) = take_bit LENGTH('a::len) n" |
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
238 |
by transfer (simp add: nat_eq_iff take_bit_eq_mod zmod_int) |
64015 | 239 |
|
240 |
lemma of_nat_unsigned [simp]: |
|
241 |
"of_nat (unsigned a) = a" |
|
242 |
by transfer simp |
|
243 |
||
244 |
lemma of_int_unsigned [simp]: |
|
245 |
"of_int (unsigned a) = a" |
|
246 |
by transfer simp |
|
247 |
||
70973 | 248 |
lemma unsigned_nat_less: |
249 |
\<open>unsigned a < (2 ^ LENGTH('a) :: nat)\<close> for a :: \<open>'a::len0 word\<close> |
|
250 |
by transfer (simp add: take_bit_eq_mod) |
|
251 |
||
252 |
lemma unsigned_int_less: |
|
253 |
\<open>unsigned a < (2 ^ LENGTH('a) :: int)\<close> for a :: \<open>'a::len0 word\<close> |
|
254 |
by transfer (simp add: take_bit_eq_mod) |
|
255 |
||
64015 | 256 |
context ring_char_0 |
257 |
begin |
|
258 |
||
259 |
lemma word_eq_iff_signed: |
|
260 |
"a = b \<longleftrightarrow> signed a = signed b" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
261 |
by safe (transfer; auto simp add: signed_take_bit_eq_iff_take_bit_eq) |
64015 | 262 |
|
263 |
end |
|
264 |
||
265 |
lemma signed_of_int [simp]: |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
266 |
"signed (of_int k :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) k" |
64015 | 267 |
by transfer simp |
268 |
||
269 |
lemma of_int_signed [simp]: |
|
270 |
"of_int (signed a) = a" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
271 |
by transfer (simp add: signed_take_bit_eq_take_bit take_bit_eq_mod mod_simps) |
64015 | 272 |
|
273 |
||
274 |
subsubsection \<open>Properties\<close> |
|
275 |
||
70973 | 276 |
lemma length_cases: |
277 |
obtains (triv) "LENGTH('a::len) = 1" "take_bit LENGTH('a) 2 = (0 :: int)" |
|
278 |
| (take_bit_2) "take_bit LENGTH('a) 2 = (2 :: int)" |
|
279 |
proof (cases "LENGTH('a) \<ge> 2") |
|
280 |
case False |
|
281 |
then have "LENGTH('a) = 1" |
|
282 |
by (auto simp add: not_le dest: less_2_cases) |
|
283 |
then have "take_bit LENGTH('a) 2 = (0 :: int)" |
|
284 |
by simp |
|
285 |
with \<open>LENGTH('a) = 1\<close> triv show ?thesis |
|
286 |
by simp |
|
287 |
next |
|
288 |
case True |
|
289 |
then obtain n where "LENGTH('a) = Suc (Suc n)" |
|
290 |
by (auto dest: le_Suc_ex) |
|
291 |
then have "take_bit LENGTH('a) 2 = (2 :: int)" |
|
292 |
by simp |
|
293 |
with take_bit_2 show ?thesis |
|
294 |
by simp |
|
295 |
qed |
|
296 |
||
64015 | 297 |
|
298 |
subsubsection \<open>Division\<close> |
|
299 |
||
300 |
instantiation word :: (len0) modulo |
|
301 |
begin |
|
302 |
||
303 |
lift_definition divide_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
304 |
is "\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b" |
64015 | 305 |
by simp |
306 |
||
307 |
lift_definition modulo_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
308 |
is "\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b" |
64015 | 309 |
by simp |
310 |
||
311 |
instance .. |
|
312 |
||
313 |
end |
|
314 |
||
70973 | 315 |
lemma zero_word_div_eq [simp]: |
316 |
\<open>0 div a = 0\<close> for a :: \<open>'a::len0 word\<close> |
|
317 |
by transfer simp |
|
318 |
||
319 |
lemma div_zero_word_eq [simp]: |
|
320 |
\<open>a div 0 = 0\<close> for a :: \<open>'a::len0 word\<close> |
|
321 |
by transfer simp |
|
322 |
||
70927 | 323 |
context |
324 |
includes lifting_syntax |
|
325 |
begin |
|
326 |
||
70348
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
327 |
lemma [transfer_rule]: |
70927 | 328 |
"(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)" |
70348
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
329 |
proof - |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
330 |
have even_word_unfold: "even k \<longleftrightarrow> (\<exists>l. take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l))" (is "?P \<longleftrightarrow> ?Q") |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
331 |
for k :: int |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
332 |
proof |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
333 |
assume ?P |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
334 |
then show ?Q |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
335 |
by auto |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
336 |
next |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
337 |
assume ?Q |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
338 |
then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" .. |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
339 |
then have "even (take_bit LENGTH('a) k)" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
340 |
by simp |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
341 |
then show ?P |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
342 |
by simp |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
343 |
qed |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
344 |
show ?thesis by (simp only: even_word_unfold [abs_def] dvd_def [where ?'a = "'a word", abs_def]) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
345 |
transfer_prover |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
346 |
qed |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
347 |
|
70927 | 348 |
end |
349 |
||
70348
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
350 |
instance word :: (len) semiring_modulo |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
351 |
proof |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
352 |
show "a div b * b + a mod b = a" for a b :: "'a word" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
353 |
proof transfer |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
354 |
fix k l :: int |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
355 |
define r :: int where "r = 2 ^ LENGTH('a)" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
356 |
then have r: "take_bit LENGTH('a) k = k mod r" for k |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
357 |
by (simp add: take_bit_eq_mod) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
358 |
have "k mod r = ((k mod r) div (l mod r) * (l mod r) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
359 |
+ (k mod r) mod (l mod r)) mod r" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
360 |
by (simp add: div_mult_mod_eq) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
361 |
also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
362 |
+ (k mod r) mod (l mod r)) mod r" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
363 |
by (simp add: mod_add_left_eq) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
364 |
also have "... = (((k mod r) div (l mod r) * l) mod r |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
365 |
+ (k mod r) mod (l mod r)) mod r" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
366 |
by (simp add: mod_mult_right_eq) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
367 |
finally have "k mod r = ((k mod r) div (l mod r) * l |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
368 |
+ (k mod r) mod (l mod r)) mod r" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
369 |
by (simp add: mod_simps) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
370 |
with r show "take_bit LENGTH('a) (take_bit LENGTH('a) k div take_bit LENGTH('a) l * l |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
371 |
+ take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
372 |
by simp |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
373 |
qed |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
374 |
qed |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
375 |
|
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
376 |
instance word :: (len) semiring_parity |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
377 |
proof |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
378 |
show "\<not> 2 dvd (1::'a word)" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
379 |
by transfer simp |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
380 |
show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
381 |
for a :: "'a word" |
70973 | 382 |
by (transfer; cases rule: length_cases [where ?'a = 'a]) (simp_all add: mod_2_eq_odd) |
70348
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
383 |
show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
384 |
for a :: "'a word" |
70973 | 385 |
by (transfer; cases rule: length_cases [where ?'a = 'a]) (simp_all add: mod_2_eq_odd) |
70348
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
386 |
qed |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
387 |
|
64015 | 388 |
|
389 |
subsubsection \<open>Orderings\<close> |
|
390 |
||
391 |
instantiation word :: (len0) linorder |
|
392 |
begin |
|
393 |
||
394 |
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
395 |
is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b" |
64015 | 396 |
by simp |
397 |
||
398 |
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
399 |
is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b" |
64015 | 400 |
by simp |
401 |
||
402 |
instance |
|
403 |
by standard (transfer; auto)+ |
|
404 |
||
405 |
end |
|
406 |
||
407 |
context linordered_semidom |
|
408 |
begin |
|
409 |
||
410 |
lemma word_less_eq_iff_unsigned: |
|
411 |
"a \<le> b \<longleftrightarrow> unsigned a \<le> unsigned b" |
|
412 |
by (transfer fixing: less_eq) (simp add: nat_le_eq_zle) |
|
413 |
||
414 |
lemma word_less_iff_unsigned: |
|
415 |
"a < b \<longleftrightarrow> unsigned a < unsigned b" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
416 |
by (transfer fixing: less) (auto dest: preorder_class.le_less_trans [OF take_bit_nonnegative]) |
64015 | 417 |
|
418 |
end |
|
419 |
||
70973 | 420 |
lemma word_greater_zero_iff: |
421 |
\<open>a > 0 \<longleftrightarrow> a \<noteq> 0\<close> for a :: \<open>'a::len0 word\<close> |
|
422 |
by transfer (simp add: less_le) |
|
423 |
||
424 |
lemma of_nat_word_eq_iff: |
|
425 |
\<open>of_nat m = (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m = take_bit LENGTH('a) n\<close> |
|
426 |
by transfer (simp add: take_bit_of_nat) |
|
427 |
||
428 |
lemma of_nat_word_less_eq_iff: |
|
429 |
\<open>of_nat m \<le> (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m \<le> take_bit LENGTH('a) n\<close> |
|
430 |
by transfer (simp add: take_bit_of_nat) |
|
431 |
||
432 |
lemma of_nat_word_less_iff: |
|
433 |
\<open>of_nat m < (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m < take_bit LENGTH('a) n\<close> |
|
434 |
by transfer (simp add: take_bit_of_nat) |
|
435 |
||
436 |
lemma of_nat_word_eq_0_iff: |
|
437 |
\<open>of_nat n = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd n\<close> |
|
438 |
using of_nat_word_eq_iff [where ?'a = 'a, of n 0] by (simp add: take_bit_eq_0_iff) |
|
439 |
||
440 |
lemma of_int_word_eq_iff: |
|
441 |
\<open>of_int k = (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> |
|
442 |
by transfer rule |
|
443 |
||
444 |
lemma of_int_word_less_eq_iff: |
|
445 |
\<open>of_int k \<le> (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k \<le> take_bit LENGTH('a) l\<close> |
|
446 |
by transfer rule |
|
447 |
||
448 |
lemma of_int_word_less_iff: |
|
449 |
\<open>of_int k < (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k < take_bit LENGTH('a) l\<close> |
|
450 |
by transfer rule |
|
451 |
||
452 |
lemma of_int_word_eq_0_iff: |
|
453 |
\<open>of_int k = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd k\<close> |
|
454 |
using of_int_word_eq_iff [where ?'a = 'a, of k 0] by (simp add: take_bit_eq_0_iff) |
|
455 |
||
456 |
||
70925 | 457 |
subsection \<open>Bit operation on \<^typ>\<open>'a word\<close>\<close> |
458 |
||
459 |
context unique_euclidean_semiring_with_nat |
|
460 |
begin |
|
461 |
||
462 |
primrec n_bits_of :: "nat \<Rightarrow> 'a \<Rightarrow> bool list" |
|
463 |
where |
|
464 |
"n_bits_of 0 a = []" |
|
465 |
| "n_bits_of (Suc n) a = odd a # n_bits_of n (a div 2)" |
|
466 |
||
467 |
lemma n_bits_of_eq_iff: |
|
468 |
"n_bits_of n a = n_bits_of n b \<longleftrightarrow> take_bit n a = take_bit n b" |
|
469 |
apply (induction n arbitrary: a b) |
|
70973 | 470 |
apply (auto elim!: evenE oddE) |
471 |
apply (metis dvd_triv_right even_plus_one_iff) |
|
472 |
apply (metis dvd_triv_right even_plus_one_iff) |
|
70925 | 473 |
done |
474 |
||
475 |
lemma take_n_bits_of [simp]: |
|
476 |
"take m (n_bits_of n a) = n_bits_of (min m n) a" |
|
477 |
proof - |
|
478 |
define q and v and w where "q = min m n" and "v = m - q" and "w = n - q" |
|
479 |
then have "v = 0 \<or> w = 0" |
|
480 |
by auto |
|
481 |
then have "take (q + v) (n_bits_of (q + w) a) = n_bits_of q a" |
|
482 |
by (induction q arbitrary: a) auto |
|
483 |
with q_def v_def w_def show ?thesis |
|
484 |
by simp |
|
485 |
qed |
|
486 |
||
487 |
lemma unsigned_of_bits_n_bits_of [simp]: |
|
488 |
"unsigned_of_bits (n_bits_of n a) = take_bit n a" |
|
489 |
by (induction n arbitrary: a) (simp_all add: ac_simps) |
|
490 |
||
64015 | 491 |
end |
70925 | 492 |
|
493 |
lemma unsigned_of_bits_eq_of_bits: |
|
494 |
"unsigned_of_bits bs = (of_bits (bs @ [False]) :: int)" |
|
495 |
by (simp add: of_bits_int_def) |
|
496 |
||
497 |
||
498 |
instantiation word :: (len) bit_representation |
|
499 |
begin |
|
500 |
||
501 |
lift_definition bits_of_word :: "'a word \<Rightarrow> bool list" |
|
502 |
is "n_bits_of LENGTH('a)" |
|
503 |
by (simp add: n_bits_of_eq_iff) |
|
504 |
||
505 |
lift_definition of_bits_word :: "bool list \<Rightarrow> 'a word" |
|
506 |
is unsigned_of_bits . |
|
507 |
||
508 |
instance proof |
|
509 |
fix a :: "'a word" |
|
510 |
show "of_bits (bits_of a) = a" |
|
511 |
by transfer simp |
|
512 |
qed |
|
513 |
||
514 |
end |
|
515 |
||
516 |
lemma take_bit_complement_iff: |
|
517 |
"take_bit n (complement k) = take_bit n (complement l) \<longleftrightarrow> take_bit n k = take_bit n l" |
|
518 |
for k l :: int |
|
519 |
by (simp add: take_bit_eq_mod mod_eq_dvd_iff dvd_diff_commute) |
|
520 |
||
521 |
lemma take_bit_not_iff: |
|
522 |
"take_bit n (NOT k) = take_bit n (NOT l) \<longleftrightarrow> take_bit n k = take_bit n l" |
|
523 |
for k l :: int |
|
524 |
by (simp add: not_int_def take_bit_complement_iff) |
|
525 |
||
526 |
lemma n_bits_of_not: |
|
527 |
"n_bits_of n (NOT k) = map Not (n_bits_of n k)" |
|
528 |
for k :: int |
|
529 |
by (induction n arbitrary: k) (simp_all add: not_div_2) |
|
530 |
||
531 |
lemma take_bit_and [simp]: |
|
532 |
"take_bit n (k AND l) = take_bit n k AND take_bit n l" |
|
533 |
for k l :: int |
|
534 |
apply (induction n arbitrary: k l) |
|
535 |
apply simp |
|
536 |
apply (subst and_int.rec) |
|
537 |
apply (subst (2) and_int.rec) |
|
538 |
apply simp |
|
539 |
done |
|
540 |
||
541 |
lemma take_bit_or [simp]: |
|
542 |
"take_bit n (k OR l) = take_bit n k OR take_bit n l" |
|
543 |
for k l :: int |
|
544 |
apply (induction n arbitrary: k l) |
|
545 |
apply simp |
|
546 |
apply (subst or_int.rec) |
|
547 |
apply (subst (2) or_int.rec) |
|
548 |
apply simp |
|
549 |
done |
|
550 |
||
551 |
lemma take_bit_xor [simp]: |
|
552 |
"take_bit n (k XOR l) = take_bit n k XOR take_bit n l" |
|
553 |
for k l :: int |
|
554 |
apply (induction n arbitrary: k l) |
|
555 |
apply simp |
|
556 |
apply (subst xor_int.rec) |
|
557 |
apply (subst (2) xor_int.rec) |
|
558 |
apply simp |
|
559 |
done |
|
560 |
||
561 |
instantiation word :: (len) bit_operations |
|
562 |
begin |
|
563 |
||
564 |
lift_definition not_word :: "'a word \<Rightarrow> 'a word" |
|
565 |
is not |
|
566 |
by (simp add: take_bit_not_iff) |
|
567 |
||
568 |
lift_definition and_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
569 |
is "and" |
|
570 |
by simp |
|
571 |
||
572 |
lift_definition or_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
573 |
is or |
|
574 |
by simp |
|
575 |
||
576 |
lift_definition xor_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
577 |
is xor |
|
578 |
by simp |
|
579 |
||
580 |
lift_definition shift_left_word :: "'a word \<Rightarrow> nat \<Rightarrow> 'a word" |
|
581 |
is shift_left |
|
582 |
proof - |
|
583 |
show "take_bit LENGTH('a) (k << n) = take_bit LENGTH('a) (l << n)" |
|
584 |
if "take_bit LENGTH('a) k = take_bit LENGTH('a) l" for k l :: int and n :: nat |
|
585 |
proof - |
|
586 |
from that |
|
587 |
have "take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k) |
|
588 |
= take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)" |
|
589 |
by simp |
|
590 |
moreover have "min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n" |
|
591 |
by simp |
|
592 |
ultimately show ?thesis by (simp add: take_bit_push_bit) |
|
593 |
qed |
|
594 |
qed |
|
595 |
||
596 |
lift_definition shift_right_word :: "'a word \<Rightarrow> nat \<Rightarrow> 'a word" |
|
597 |
is "\<lambda>k n. drop_bit n (take_bit LENGTH('a) k)" |
|
598 |
by simp |
|
599 |
||
600 |
instance proof |
|
601 |
show "semilattice ((AND) :: 'a word \<Rightarrow> _)" |
|
602 |
by standard (transfer; simp add: ac_simps)+ |
|
603 |
show "semilattice ((OR) :: 'a word \<Rightarrow> _)" |
|
604 |
by standard (transfer; simp add: ac_simps)+ |
|
605 |
show "abel_semigroup ((XOR) :: 'a word \<Rightarrow> _)" |
|
606 |
by standard (transfer; simp add: ac_simps)+ |
|
607 |
show "not = (of_bits \<circ> map Not \<circ> bits_of :: 'a word \<Rightarrow> 'a word)" |
|
608 |
proof |
|
609 |
fix a :: "'a word" |
|
610 |
have "NOT a = of_bits (map Not (bits_of a))" |
|
611 |
by transfer (simp flip: unsigned_of_bits_take n_bits_of_not add: take_map) |
|
612 |
then show "NOT a = (of_bits \<circ> map Not \<circ> bits_of) a" |
|
613 |
by simp |
|
614 |
qed |
|
615 |
show "of_bits bs AND of_bits cs = (of_bits (map2 (\<and>) bs cs) :: 'a word)" |
|
616 |
if "length bs = length cs" for bs cs |
|
617 |
using that apply transfer |
|
618 |
apply (simp only: unsigned_of_bits_eq_of_bits) |
|
619 |
apply (subst and_eq) |
|
620 |
apply simp_all |
|
621 |
done |
|
622 |
show "of_bits bs OR of_bits cs = (of_bits (map2 (\<or>) bs cs) :: 'a word)" |
|
623 |
if "length bs = length cs" for bs cs |
|
624 |
using that apply transfer |
|
625 |
apply (simp only: unsigned_of_bits_eq_of_bits) |
|
626 |
apply (subst or_eq) |
|
627 |
apply simp_all |
|
628 |
done |
|
629 |
show "of_bits bs XOR of_bits cs = (of_bits (map2 (\<noteq>) bs cs) :: 'a word)" |
|
630 |
if "length bs = length cs" for bs cs |
|
631 |
using that apply transfer |
|
632 |
apply (simp only: unsigned_of_bits_eq_of_bits) |
|
633 |
apply (subst xor_eq) |
|
634 |
apply simp_all |
|
635 |
done |
|
636 |
show "a << n = of_bits (replicate n False @ bits_of a)" |
|
637 |
for a :: "'a word" and n :: nat |
|
638 |
by transfer (simp add: push_bit_take_bit) |
|
639 |
show "a >> n = of_bits (drop n (bits_of a))" |
|
640 |
if "n < length (bits_of a)" |
|
641 |
for a :: "'a word" and n :: nat |
|
642 |
using that by transfer simp |
|
643 |
qed |
|
644 |
||
70973 | 645 |
|
646 |
subsection \<open>Bit structure on \<^typ>\<open>'a word\<close>\<close> |
|
647 |
||
648 |
lemma word_bit_induct [case_names zero even odd]: |
|
649 |
\<open>P a\<close> if word_zero: \<open>P 0\<close> |
|
650 |
and word_even: \<open>\<And>a. P a \<Longrightarrow> 0 < a \<Longrightarrow> a < 2 ^ (LENGTH('a) - 1) \<Longrightarrow> P (2 * a)\<close> |
|
651 |
and word_odd: \<open>\<And>a. P a \<Longrightarrow> a < 2 ^ (LENGTH('a) - 1) \<Longrightarrow> P (1 + 2 * a)\<close> |
|
652 |
for P and a :: \<open>'a::len word\<close> |
|
653 |
proof - |
|
654 |
define m :: nat where \<open>m = LENGTH('a) - 1\<close> |
|
655 |
then have l: \<open>LENGTH('a) = Suc m\<close> |
|
656 |
by simp |
|
657 |
define n :: nat where \<open>n = unsigned a\<close> |
|
658 |
then have \<open>n < 2 ^ LENGTH('a)\<close> |
|
659 |
by (simp add: unsigned_nat_less) |
|
660 |
then have \<open>n < 2 * 2 ^ m\<close> |
|
661 |
by (simp add: l) |
|
662 |
then have \<open>P (of_nat n)\<close> |
|
663 |
proof (induction n rule: nat_bit_induct) |
|
664 |
case zero |
|
665 |
show ?case |
|
666 |
by simp (rule word_zero) |
|
667 |
next |
|
668 |
case (even n) |
|
669 |
then have \<open>n < 2 ^ m\<close> |
|
670 |
by simp |
|
671 |
with even.IH have \<open>P (of_nat n)\<close> |
|
672 |
by simp |
|
673 |
moreover from \<open>n < 2 ^ m\<close> even.hyps have \<open>0 < (of_nat n :: 'a word)\<close> |
|
674 |
by (auto simp add: word_greater_zero_iff of_nat_word_eq_0_iff l) |
|
675 |
moreover from \<open>n < 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - 1)\<close> |
|
676 |
using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>] |
|
677 |
by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l) |
|
678 |
ultimately have \<open>P (2 * of_nat n)\<close> |
|
679 |
by (rule word_even) |
|
680 |
then show ?case |
|
681 |
by simp |
|
682 |
next |
|
683 |
case (odd n) |
|
684 |
then have \<open>Suc n \<le> 2 ^ m\<close> |
|
685 |
by simp |
|
686 |
with odd.IH have \<open>P (of_nat n)\<close> |
|
687 |
by simp |
|
688 |
moreover from \<open>Suc n \<le> 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - 1)\<close> |
|
689 |
using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>] |
|
690 |
by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l) |
|
691 |
ultimately have \<open>P (1 + 2 * of_nat n)\<close> |
|
692 |
by (rule word_odd) |
|
693 |
then show ?case |
|
694 |
by simp |
|
695 |
qed |
|
696 |
then show ?thesis |
|
697 |
by (simp add: n_def) |
|
698 |
qed |
|
699 |
||
70925 | 700 |
end |
701 |
||
702 |
global_interpretation bit_word: boolean_algebra "(AND)" "(OR)" NOT 0 "- 1 :: 'a::len word" |
|
703 |
rewrites "bit_word.xor = ((XOR) :: 'a word \<Rightarrow> _)" |
|
704 |
proof - |
|
705 |
interpret bit_word: boolean_algebra "(AND)" "(OR)" NOT 0 "- 1 :: 'a word" |
|
706 |
proof |
|
707 |
show "a AND (b OR c) = a AND b OR a AND c" |
|
708 |
for a b c :: "'a word" |
|
709 |
by transfer (simp add: bit_int.conj_disj_distrib) |
|
710 |
show "a OR b AND c = (a OR b) AND (a OR c)" |
|
711 |
for a b c :: "'a word" |
|
712 |
by transfer (simp add: bit_int.disj_conj_distrib) |
|
713 |
show "a AND NOT a = 0" for a :: "'a word" |
|
714 |
by transfer simp |
|
715 |
show "a OR NOT a = - 1" for a :: "'a word" |
|
716 |
by transfer simp |
|
717 |
qed (transfer; simp)+ |
|
718 |
show "boolean_algebra (AND) (OR) NOT 0 (- 1 :: 'a word)" |
|
719 |
by (fact bit_word.boolean_algebra_axioms) |
|
720 |
show "bit_word.xor = ((XOR) :: 'a word \<Rightarrow> _)" |
|
721 |
proof (rule ext)+ |
|
722 |
fix a b :: "'a word" |
|
723 |
have "a XOR b = a AND NOT b OR NOT a AND b" |
|
724 |
by transfer (simp add: bit_int.xor_def) |
|
725 |
then show "bit_word.xor a b = a XOR b" |
|
726 |
by (simp add: bit_word.xor_def) |
|
727 |
qed |
|
728 |
qed |
|
729 |
||
730 |
end |