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