author | haftmann |
Wed, 23 Oct 2019 16:09:23 +0000 | |
changeset 70927 | cc204e10385c |
parent 70925 | 525853e4ec80 |
child 70973 | a7a52ba0717d |
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 |
||
64015 | 144 |
|
145 |
subsubsection \<open>Conversions\<close> |
|
146 |
||
70927 | 147 |
context |
148 |
includes lifting_syntax |
|
149 |
notes transfer_rule_numeral [transfer_rule] |
|
150 |
transfer_rule_of_nat [transfer_rule] |
|
151 |
transfer_rule_of_int [transfer_rule] |
|
152 |
begin |
|
70348
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
153 |
|
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
154 |
lemma [transfer_rule]: |
70927 | 155 |
"((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) numeral numeral" |
156 |
by transfer_prover |
|
157 |
||
158 |
lemma [transfer_rule]: |
|
159 |
"((=) ===> pcr_word) int of_nat" |
|
160 |
by transfer_prover |
|
64015 | 161 |
|
162 |
lemma [transfer_rule]: |
|
70927 | 163 |
"((=) ===> pcr_word) (\<lambda>k. k) of_int" |
64015 | 164 |
proof - |
70927 | 165 |
have "((=) ===> pcr_word) of_int of_int" |
64015 | 166 |
by transfer_prover |
167 |
then show ?thesis by (simp add: id_def) |
|
168 |
qed |
|
169 |
||
70927 | 170 |
end |
171 |
||
64015 | 172 |
context semiring_1 |
173 |
begin |
|
174 |
||
175 |
lift_definition unsigned :: "'b::len0 word \<Rightarrow> 'a" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
176 |
is "of_nat \<circ> nat \<circ> take_bit LENGTH('b)" |
64015 | 177 |
by simp |
178 |
||
179 |
lemma unsigned_0 [simp]: |
|
180 |
"unsigned 0 = 0" |
|
181 |
by transfer simp |
|
182 |
||
183 |
end |
|
184 |
||
185 |
context semiring_char_0 |
|
186 |
begin |
|
187 |
||
188 |
lemma word_eq_iff_unsigned: |
|
189 |
"a = b \<longleftrightarrow> unsigned a = unsigned b" |
|
190 |
by safe (transfer; simp add: eq_nat_nat_iff) |
|
191 |
||
192 |
end |
|
193 |
||
70903 | 194 |
instantiation word :: (len0) equal |
195 |
begin |
|
196 |
||
197 |
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" |
|
198 |
where "equal_word a b \<longleftrightarrow> (unsigned a :: int) = unsigned b" |
|
199 |
||
200 |
instance proof |
|
201 |
fix a b :: "'a word" |
|
202 |
show "HOL.equal a b \<longleftrightarrow> a = b" |
|
203 |
using word_eq_iff_unsigned [of a b] by (auto simp add: equal_word_def) |
|
204 |
qed |
|
205 |
||
206 |
end |
|
207 |
||
64015 | 208 |
context ring_1 |
209 |
begin |
|
210 |
||
211 |
lift_definition signed :: "'b::len word \<Rightarrow> 'a" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
212 |
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
|
213 |
by (simp add: signed_take_bit_eq_iff_take_bit_eq [symmetric]) |
64015 | 214 |
|
215 |
lemma signed_0 [simp]: |
|
216 |
"signed 0 = 0" |
|
217 |
by transfer simp |
|
218 |
||
219 |
end |
|
220 |
||
221 |
lemma unsigned_of_nat [simp]: |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
222 |
"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
|
223 |
by transfer (simp add: nat_eq_iff take_bit_eq_mod zmod_int) |
64015 | 224 |
|
225 |
lemma of_nat_unsigned [simp]: |
|
226 |
"of_nat (unsigned a) = a" |
|
227 |
by transfer simp |
|
228 |
||
229 |
lemma of_int_unsigned [simp]: |
|
230 |
"of_int (unsigned a) = a" |
|
231 |
by transfer simp |
|
232 |
||
233 |
context ring_char_0 |
|
234 |
begin |
|
235 |
||
236 |
lemma word_eq_iff_signed: |
|
237 |
"a = b \<longleftrightarrow> signed a = signed b" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
238 |
by safe (transfer; auto simp add: signed_take_bit_eq_iff_take_bit_eq) |
64015 | 239 |
|
240 |
end |
|
241 |
||
242 |
lemma signed_of_int [simp]: |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
243 |
"signed (of_int k :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) k" |
64015 | 244 |
by transfer simp |
245 |
||
246 |
lemma of_int_signed [simp]: |
|
247 |
"of_int (signed a) = a" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
248 |
by transfer (simp add: signed_take_bit_eq_take_bit take_bit_eq_mod mod_simps) |
64015 | 249 |
|
250 |
||
251 |
subsubsection \<open>Properties\<close> |
|
252 |
||
253 |
||
254 |
subsubsection \<open>Division\<close> |
|
255 |
||
256 |
instantiation word :: (len0) modulo |
|
257 |
begin |
|
258 |
||
259 |
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
|
260 |
is "\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b" |
64015 | 261 |
by simp |
262 |
||
263 |
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
|
264 |
is "\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b" |
64015 | 265 |
by simp |
266 |
||
267 |
instance .. |
|
268 |
||
269 |
end |
|
270 |
||
70927 | 271 |
context |
272 |
includes lifting_syntax |
|
273 |
begin |
|
274 |
||
70348
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
275 |
lemma [transfer_rule]: |
70927 | 276 |
"(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
|
277 |
proof - |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
278 |
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
|
279 |
for k :: int |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
280 |
proof |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
281 |
assume ?P |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
282 |
then show ?Q |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
283 |
by auto |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
284 |
next |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
285 |
assume ?Q |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
286 |
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
|
287 |
then have "even (take_bit LENGTH('a) k)" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
288 |
by simp |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
289 |
then show ?P |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
290 |
by simp |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
291 |
qed |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
292 |
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
|
293 |
transfer_prover |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
294 |
qed |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
295 |
|
70927 | 296 |
end |
297 |
||
70348
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
298 |
instance word :: (len) semiring_modulo |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
299 |
proof |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
300 |
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
|
301 |
proof transfer |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
302 |
fix k l :: int |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
303 |
define r :: int where "r = 2 ^ LENGTH('a)" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
304 |
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
|
305 |
by (simp add: take_bit_eq_mod) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
306 |
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
|
307 |
+ (k mod r) mod (l mod r)) mod r" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
308 |
by (simp add: div_mult_mod_eq) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
309 |
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
|
310 |
+ (k mod r) mod (l mod r)) mod r" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
311 |
by (simp add: mod_add_left_eq) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
312 |
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
|
313 |
+ (k mod r) mod (l mod r)) mod r" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
314 |
by (simp add: mod_mult_right_eq) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
315 |
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
|
316 |
+ (k mod r) mod (l mod r)) mod r" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
317 |
by (simp add: mod_simps) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
318 |
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
|
319 |
+ 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
|
320 |
by simp |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
321 |
qed |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
322 |
qed |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
323 |
|
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
324 |
instance word :: (len) semiring_parity |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
325 |
proof |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
326 |
show "\<not> 2 dvd (1::'a word)" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
327 |
by transfer simp |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
328 |
consider (triv) "LENGTH('a) = 1" "take_bit LENGTH('a) 2 = (0 :: int)" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
329 |
| (take_bit_2) "take_bit LENGTH('a) 2 = (2 :: int)" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
330 |
proof (cases "LENGTH('a) \<ge> 2") |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
331 |
case False |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
332 |
then have "LENGTH('a) = 1" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
333 |
by (auto simp add: not_le dest: less_2_cases) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
334 |
then have "take_bit LENGTH('a) 2 = (0 :: int)" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
335 |
by simp |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
336 |
with \<open>LENGTH('a) = 1\<close> triv show ?thesis |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
337 |
by simp |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
338 |
next |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
339 |
case True |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
340 |
then obtain n where "LENGTH('a) = Suc (Suc n)" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
341 |
by (auto dest: le_Suc_ex) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
342 |
then have "take_bit LENGTH('a) 2 = (2 :: int)" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
343 |
by simp |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
344 |
with take_bit_2 show ?thesis |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
345 |
by simp |
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 |
note * = this |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
348 |
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
|
349 |
for a :: "'a word" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
350 |
by (transfer; cases rule: *) (simp_all add: mod_2_eq_odd) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
351 |
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
|
352 |
for a :: "'a word" |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
353 |
by (transfer; cases rule: *) (simp_all add: mod_2_eq_odd) |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
354 |
qed |
bde161c740ca
more theorems for proof of concept for word type
haftmann
parents:
70171
diff
changeset
|
355 |
|
64015 | 356 |
|
357 |
subsubsection \<open>Orderings\<close> |
|
358 |
||
359 |
instantiation word :: (len0) linorder |
|
360 |
begin |
|
361 |
||
362 |
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
|
363 |
is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b" |
64015 | 364 |
by simp |
365 |
||
366 |
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
|
367 |
is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b" |
64015 | 368 |
by simp |
369 |
||
370 |
instance |
|
371 |
by standard (transfer; auto)+ |
|
372 |
||
373 |
end |
|
374 |
||
375 |
context linordered_semidom |
|
376 |
begin |
|
377 |
||
378 |
lemma word_less_eq_iff_unsigned: |
|
379 |
"a \<le> b \<longleftrightarrow> unsigned a \<le> unsigned b" |
|
380 |
by (transfer fixing: less_eq) (simp add: nat_le_eq_zle) |
|
381 |
||
382 |
lemma word_less_iff_unsigned: |
|
383 |
"a < b \<longleftrightarrow> unsigned a < unsigned b" |
|
67907
02a14c1cb917
prefer convention to place operation name before type name
haftmann
parents:
67816
diff
changeset
|
384 |
by (transfer fixing: less) (auto dest: preorder_class.le_less_trans [OF take_bit_nonnegative]) |
64015 | 385 |
|
386 |
end |
|
387 |
||
70925 | 388 |
subsection \<open>Bit operation on \<^typ>\<open>'a word\<close>\<close> |
389 |
||
390 |
context unique_euclidean_semiring_with_nat |
|
391 |
begin |
|
392 |
||
393 |
primrec n_bits_of :: "nat \<Rightarrow> 'a \<Rightarrow> bool list" |
|
394 |
where |
|
395 |
"n_bits_of 0 a = []" |
|
396 |
| "n_bits_of (Suc n) a = odd a # n_bits_of n (a div 2)" |
|
397 |
||
398 |
lemma n_bits_of_eq_iff: |
|
399 |
"n_bits_of n a = n_bits_of n b \<longleftrightarrow> take_bit n a = take_bit n b" |
|
400 |
apply (induction n arbitrary: a b) |
|
401 |
apply auto |
|
402 |
apply (metis local.dvd_add_times_triv_left_iff local.dvd_triv_right local.odd_one) |
|
403 |
apply (metis local.dvd_add_times_triv_left_iff local.dvd_triv_right local.odd_one) |
|
404 |
done |
|
405 |
||
406 |
lemma take_n_bits_of [simp]: |
|
407 |
"take m (n_bits_of n a) = n_bits_of (min m n) a" |
|
408 |
proof - |
|
409 |
define q and v and w where "q = min m n" and "v = m - q" and "w = n - q" |
|
410 |
then have "v = 0 \<or> w = 0" |
|
411 |
by auto |
|
412 |
then have "take (q + v) (n_bits_of (q + w) a) = n_bits_of q a" |
|
413 |
by (induction q arbitrary: a) auto |
|
414 |
with q_def v_def w_def show ?thesis |
|
415 |
by simp |
|
416 |
qed |
|
417 |
||
418 |
lemma unsigned_of_bits_n_bits_of [simp]: |
|
419 |
"unsigned_of_bits (n_bits_of n a) = take_bit n a" |
|
420 |
by (induction n arbitrary: a) (simp_all add: ac_simps) |
|
421 |
||
64015 | 422 |
end |
70925 | 423 |
|
424 |
lemma unsigned_of_bits_eq_of_bits: |
|
425 |
"unsigned_of_bits bs = (of_bits (bs @ [False]) :: int)" |
|
426 |
by (simp add: of_bits_int_def) |
|
427 |
||
428 |
||
429 |
instantiation word :: (len) bit_representation |
|
430 |
begin |
|
431 |
||
432 |
lift_definition bits_of_word :: "'a word \<Rightarrow> bool list" |
|
433 |
is "n_bits_of LENGTH('a)" |
|
434 |
by (simp add: n_bits_of_eq_iff) |
|
435 |
||
436 |
lift_definition of_bits_word :: "bool list \<Rightarrow> 'a word" |
|
437 |
is unsigned_of_bits . |
|
438 |
||
439 |
instance proof |
|
440 |
fix a :: "'a word" |
|
441 |
show "of_bits (bits_of a) = a" |
|
442 |
by transfer simp |
|
443 |
qed |
|
444 |
||
445 |
end |
|
446 |
||
447 |
lemma take_bit_complement_iff: |
|
448 |
"take_bit n (complement k) = take_bit n (complement l) \<longleftrightarrow> take_bit n k = take_bit n l" |
|
449 |
for k l :: int |
|
450 |
by (simp add: take_bit_eq_mod mod_eq_dvd_iff dvd_diff_commute) |
|
451 |
||
452 |
lemma take_bit_not_iff: |
|
453 |
"take_bit n (NOT k) = take_bit n (NOT l) \<longleftrightarrow> take_bit n k = take_bit n l" |
|
454 |
for k l :: int |
|
455 |
by (simp add: not_int_def take_bit_complement_iff) |
|
456 |
||
457 |
lemma n_bits_of_not: |
|
458 |
"n_bits_of n (NOT k) = map Not (n_bits_of n k)" |
|
459 |
for k :: int |
|
460 |
by (induction n arbitrary: k) (simp_all add: not_div_2) |
|
461 |
||
462 |
lemma take_bit_and [simp]: |
|
463 |
"take_bit n (k AND l) = take_bit n k AND take_bit n l" |
|
464 |
for k l :: int |
|
465 |
apply (induction n arbitrary: k l) |
|
466 |
apply simp |
|
467 |
apply (subst and_int.rec) |
|
468 |
apply (subst (2) and_int.rec) |
|
469 |
apply simp |
|
470 |
done |
|
471 |
||
472 |
lemma take_bit_or [simp]: |
|
473 |
"take_bit n (k OR l) = take_bit n k OR take_bit n l" |
|
474 |
for k l :: int |
|
475 |
apply (induction n arbitrary: k l) |
|
476 |
apply simp |
|
477 |
apply (subst or_int.rec) |
|
478 |
apply (subst (2) or_int.rec) |
|
479 |
apply simp |
|
480 |
done |
|
481 |
||
482 |
lemma take_bit_xor [simp]: |
|
483 |
"take_bit n (k XOR l) = take_bit n k XOR take_bit n l" |
|
484 |
for k l :: int |
|
485 |
apply (induction n arbitrary: k l) |
|
486 |
apply simp |
|
487 |
apply (subst xor_int.rec) |
|
488 |
apply (subst (2) xor_int.rec) |
|
489 |
apply simp |
|
490 |
done |
|
491 |
||
492 |
instantiation word :: (len) bit_operations |
|
493 |
begin |
|
494 |
||
495 |
lift_definition not_word :: "'a word \<Rightarrow> 'a word" |
|
496 |
is not |
|
497 |
by (simp add: take_bit_not_iff) |
|
498 |
||
499 |
lift_definition and_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
500 |
is "and" |
|
501 |
by simp |
|
502 |
||
503 |
lift_definition or_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
504 |
is or |
|
505 |
by simp |
|
506 |
||
507 |
lift_definition xor_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" |
|
508 |
is xor |
|
509 |
by simp |
|
510 |
||
511 |
lift_definition shift_left_word :: "'a word \<Rightarrow> nat \<Rightarrow> 'a word" |
|
512 |
is shift_left |
|
513 |
proof - |
|
514 |
show "take_bit LENGTH('a) (k << n) = take_bit LENGTH('a) (l << n)" |
|
515 |
if "take_bit LENGTH('a) k = take_bit LENGTH('a) l" for k l :: int and n :: nat |
|
516 |
proof - |
|
517 |
from that |
|
518 |
have "take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k) |
|
519 |
= take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)" |
|
520 |
by simp |
|
521 |
moreover have "min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n" |
|
522 |
by simp |
|
523 |
ultimately show ?thesis by (simp add: take_bit_push_bit) |
|
524 |
qed |
|
525 |
qed |
|
526 |
||
527 |
lift_definition shift_right_word :: "'a word \<Rightarrow> nat \<Rightarrow> 'a word" |
|
528 |
is "\<lambda>k n. drop_bit n (take_bit LENGTH('a) k)" |
|
529 |
by simp |
|
530 |
||
531 |
instance proof |
|
532 |
show "semilattice ((AND) :: 'a word \<Rightarrow> _)" |
|
533 |
by standard (transfer; simp add: ac_simps)+ |
|
534 |
show "semilattice ((OR) :: 'a word \<Rightarrow> _)" |
|
535 |
by standard (transfer; simp add: ac_simps)+ |
|
536 |
show "abel_semigroup ((XOR) :: 'a word \<Rightarrow> _)" |
|
537 |
by standard (transfer; simp add: ac_simps)+ |
|
538 |
show "not = (of_bits \<circ> map Not \<circ> bits_of :: 'a word \<Rightarrow> 'a word)" |
|
539 |
proof |
|
540 |
fix a :: "'a word" |
|
541 |
have "NOT a = of_bits (map Not (bits_of a))" |
|
542 |
by transfer (simp flip: unsigned_of_bits_take n_bits_of_not add: take_map) |
|
543 |
then show "NOT a = (of_bits \<circ> map Not \<circ> bits_of) a" |
|
544 |
by simp |
|
545 |
qed |
|
546 |
show "of_bits bs AND of_bits cs = (of_bits (map2 (\<and>) bs cs) :: 'a word)" |
|
547 |
if "length bs = length cs" for bs cs |
|
548 |
using that apply transfer |
|
549 |
apply (simp only: unsigned_of_bits_eq_of_bits) |
|
550 |
apply (subst and_eq) |
|
551 |
apply simp_all |
|
552 |
done |
|
553 |
show "of_bits bs OR of_bits cs = (of_bits (map2 (\<or>) bs cs) :: 'a word)" |
|
554 |
if "length bs = length cs" for bs cs |
|
555 |
using that apply transfer |
|
556 |
apply (simp only: unsigned_of_bits_eq_of_bits) |
|
557 |
apply (subst or_eq) |
|
558 |
apply simp_all |
|
559 |
done |
|
560 |
show "of_bits bs XOR of_bits cs = (of_bits (map2 (\<noteq>) bs cs) :: 'a word)" |
|
561 |
if "length bs = length cs" for bs cs |
|
562 |
using that apply transfer |
|
563 |
apply (simp only: unsigned_of_bits_eq_of_bits) |
|
564 |
apply (subst xor_eq) |
|
565 |
apply simp_all |
|
566 |
done |
|
567 |
show "a << n = of_bits (replicate n False @ bits_of a)" |
|
568 |
for a :: "'a word" and n :: nat |
|
569 |
by transfer (simp add: push_bit_take_bit) |
|
570 |
show "a >> n = of_bits (drop n (bits_of a))" |
|
571 |
if "n < length (bits_of a)" |
|
572 |
for a :: "'a word" and n :: nat |
|
573 |
using that by transfer simp |
|
574 |
qed |
|
575 |
||
576 |
end |
|
577 |
||
578 |
global_interpretation bit_word: boolean_algebra "(AND)" "(OR)" NOT 0 "- 1 :: 'a::len word" |
|
579 |
rewrites "bit_word.xor = ((XOR) :: 'a word \<Rightarrow> _)" |
|
580 |
proof - |
|
581 |
interpret bit_word: boolean_algebra "(AND)" "(OR)" NOT 0 "- 1 :: 'a word" |
|
582 |
proof |
|
583 |
show "a AND (b OR c) = a AND b OR a AND c" |
|
584 |
for a b c :: "'a word" |
|
585 |
by transfer (simp add: bit_int.conj_disj_distrib) |
|
586 |
show "a OR b AND c = (a OR b) AND (a OR c)" |
|
587 |
for a b c :: "'a word" |
|
588 |
by transfer (simp add: bit_int.disj_conj_distrib) |
|
589 |
show "a AND NOT a = 0" for a :: "'a word" |
|
590 |
by transfer simp |
|
591 |
show "a OR NOT a = - 1" for a :: "'a word" |
|
592 |
by transfer simp |
|
593 |
qed (transfer; simp)+ |
|
594 |
show "boolean_algebra (AND) (OR) NOT 0 (- 1 :: 'a word)" |
|
595 |
by (fact bit_word.boolean_algebra_axioms) |
|
596 |
show "bit_word.xor = ((XOR) :: 'a word \<Rightarrow> _)" |
|
597 |
proof (rule ext)+ |
|
598 |
fix a b :: "'a word" |
|
599 |
have "a XOR b = a AND NOT b OR NOT a AND b" |
|
600 |
by transfer (simp add: bit_int.xor_def) |
|
601 |
then show "bit_word.xor a b = a XOR b" |
|
602 |
by (simp add: bit_word.xor_def) |
|
603 |
qed |
|
604 |
qed |
|
605 |
||
606 |
end |