67909
|
1 |
(* Author: Florian Haftmann, TUM
|
|
2 |
*)
|
|
3 |
|
70912
|
4 |
section \<open>Proof(s) of concept for algebraically founded lists of bits\<close>
|
67909
|
5 |
|
|
6 |
theory Bit_Lists
|
|
7 |
imports Main
|
|
8 |
begin
|
|
9 |
|
70912
|
10 |
subsection \<open>Bit representations\<close>
|
|
11 |
|
|
12 |
subsubsection \<open>Mere syntactic bit representation\<close>
|
|
13 |
|
|
14 |
class bit_representation =
|
|
15 |
fixes bits_of :: "'a \<Rightarrow> bool list"
|
|
16 |
and of_bits :: "bool list \<Rightarrow> 'a"
|
|
17 |
assumes of_bits_of [simp]: "of_bits (bits_of a) = a"
|
|
18 |
|
|
19 |
|
|
20 |
subsubsection \<open>Algebraic bit representation\<close>
|
|
21 |
|
67909
|
22 |
context comm_semiring_1
|
|
23 |
begin
|
70912
|
24 |
|
|
25 |
primrec radix_value :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b list \<Rightarrow> 'a"
|
|
26 |
where "radix_value f b [] = 0"
|
|
27 |
| "radix_value f b (a # as) = f a + radix_value f b as * b"
|
|
28 |
|
|
29 |
abbreviation (input) unsigned_of_bits :: "bool list \<Rightarrow> 'a"
|
|
30 |
where "unsigned_of_bits \<equiv> radix_value of_bool 2"
|
|
31 |
|
|
32 |
lemma unsigned_of_bits_replicate_False [simp]:
|
|
33 |
"unsigned_of_bits (replicate n False) = 0"
|
|
34 |
by (induction n) simp_all
|
|
35 |
|
|
36 |
end
|
|
37 |
|
|
38 |
context unique_euclidean_semiring_with_nat
|
|
39 |
begin
|
67909
|
40 |
|
70912
|
41 |
lemma unsigned_of_bits_append [simp]:
|
|
42 |
"unsigned_of_bits (bs @ cs) = unsigned_of_bits bs
|
|
43 |
+ push_bit (length bs) (unsigned_of_bits cs)"
|
|
44 |
by (induction bs) (simp_all add: push_bit_double,
|
|
45 |
simp_all add: algebra_simps)
|
|
46 |
|
|
47 |
lemma unsigned_of_bits_take [simp]:
|
|
48 |
"unsigned_of_bits (take n bs) = take_bit n (unsigned_of_bits bs)"
|
|
49 |
proof (induction bs arbitrary: n)
|
|
50 |
case Nil
|
|
51 |
then show ?case
|
|
52 |
by simp
|
|
53 |
next
|
|
54 |
case (Cons b bs)
|
|
55 |
then show ?case
|
|
56 |
by (cases n) (simp_all add: ac_simps)
|
|
57 |
qed
|
|
58 |
|
|
59 |
lemma unsigned_of_bits_drop [simp]:
|
|
60 |
"unsigned_of_bits (drop n bs) = drop_bit n (unsigned_of_bits bs)"
|
|
61 |
proof (induction bs arbitrary: n)
|
|
62 |
case Nil
|
|
63 |
then show ?case
|
|
64 |
by simp
|
|
65 |
next
|
|
66 |
case (Cons b bs)
|
|
67 |
then show ?case
|
|
68 |
by (cases n) simp_all
|
|
69 |
qed
|
|
70 |
|
|
71 |
end
|
|
72 |
|
|
73 |
|
|
74 |
subsubsection \<open>Instances\<close>
|
|
75 |
|
|
76 |
text \<open>Unclear whether a \<^typ>\<open>bool\<close> instantiation is needed or not\<close>
|
|
77 |
|
|
78 |
instantiation nat :: bit_representation
|
|
79 |
begin
|
|
80 |
|
|
81 |
fun bits_of_nat :: "nat \<Rightarrow> bool list"
|
|
82 |
where "bits_of (n::nat) =
|
|
83 |
(if n = 0 then [] else odd n # bits_of (n div 2))"
|
|
84 |
|
|
85 |
lemma bits_of_nat_simps [simp]:
|
|
86 |
"bits_of (0::nat) = []"
|
|
87 |
"n > 0 \<Longrightarrow> bits_of n = odd n # bits_of (n div 2)" for n :: nat
|
|
88 |
by simp_all
|
|
89 |
|
|
90 |
declare bits_of_nat.simps [simp del]
|
|
91 |
|
|
92 |
definition of_bits_nat :: "bool list \<Rightarrow> nat"
|
|
93 |
where [simp]: "of_bits_nat = unsigned_of_bits"
|
|
94 |
\<comment> \<open>remove simp\<close>
|
|
95 |
|
|
96 |
instance proof
|
|
97 |
show "of_bits (bits_of n) = n" for n :: nat
|
|
98 |
by (induction n rule: nat_bit_induct) simp_all
|
|
99 |
qed
|
67909
|
100 |
|
|
101 |
end
|
|
102 |
|
70912
|
103 |
lemma bits_of_Suc_0 [simp]:
|
|
104 |
"bits_of (Suc 0) = [True]"
|
|
105 |
by simp
|
|
106 |
|
|
107 |
lemma bits_of_1_nat [simp]:
|
|
108 |
"bits_of (1 :: nat) = [True]"
|
|
109 |
by simp
|
|
110 |
|
|
111 |
lemma bits_of_nat_numeral_simps [simp]:
|
|
112 |
"bits_of (numeral Num.One :: nat) = [True]" (is ?One)
|
|
113 |
"bits_of (numeral (Num.Bit0 n) :: nat) = False # bits_of (numeral n :: nat)" (is ?Bit0)
|
|
114 |
"bits_of (numeral (Num.Bit1 n) :: nat) = True # bits_of (numeral n :: nat)" (is ?Bit1)
|
|
115 |
proof -
|
|
116 |
show ?One
|
|
117 |
by simp
|
|
118 |
define m :: nat where "m = numeral n"
|
|
119 |
then have "m > 0" and *: "numeral n = m" "numeral (Num.Bit0 n) = 2 * m" "numeral (Num.Bit1 n) = Suc (2 * m)"
|
|
120 |
by simp_all
|
|
121 |
from \<open>m > 0\<close> show ?Bit0 ?Bit1
|
|
122 |
by (simp_all add: *)
|
|
123 |
qed
|
|
124 |
|
|
125 |
lemma unsigned_of_bits_of_nat [simp]:
|
|
126 |
"unsigned_of_bits (bits_of n) = n" for n :: nat
|
|
127 |
using of_bits_of [of n] by simp
|
|
128 |
|
|
129 |
instantiation int :: bit_representation
|
67909
|
130 |
begin
|
|
131 |
|
70912
|
132 |
fun bits_of_int :: "int \<Rightarrow> bool list"
|
|
133 |
where "bits_of_int k = odd k #
|
|
134 |
(if k = 0 \<or> k = - 1 then [] else bits_of_int (k div 2))"
|
|
135 |
|
|
136 |
lemma bits_of_int_simps [simp]:
|
|
137 |
"bits_of (0 :: int) = [False]"
|
|
138 |
"bits_of (- 1 :: int) = [True]"
|
|
139 |
"k \<noteq> 0 \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> bits_of k = odd k # bits_of (k div 2)" for k :: int
|
|
140 |
by simp_all
|
|
141 |
|
|
142 |
lemma bits_of_not_Nil [simp]:
|
|
143 |
"bits_of k \<noteq> []" for k :: int
|
|
144 |
by simp
|
|
145 |
|
|
146 |
declare bits_of_int.simps [simp del]
|
|
147 |
|
|
148 |
definition of_bits_int :: "bool list \<Rightarrow> int"
|
|
149 |
where "of_bits_int bs = (if bs = [] \<or> \<not> last bs then unsigned_of_bits bs
|
|
150 |
else unsigned_of_bits bs - 2 ^ length bs)"
|
|
151 |
|
|
152 |
lemma of_bits_int_simps [simp]:
|
|
153 |
"of_bits [] = (0 :: int)"
|
|
154 |
"of_bits [False] = (0 :: int)"
|
|
155 |
"of_bits [True] = (- 1 :: int)"
|
|
156 |
"of_bits (bs @ [b]) = (unsigned_of_bits bs :: int) - (2 ^ length bs) * of_bool b"
|
|
157 |
"of_bits (False # bs) = 2 * (of_bits bs :: int)"
|
|
158 |
"bs \<noteq> [] \<Longrightarrow> of_bits (True # bs) = 1 + 2 * (of_bits bs :: int)"
|
|
159 |
by (simp_all add: of_bits_int_def push_bit_of_1)
|
|
160 |
|
|
161 |
instance proof
|
|
162 |
show "of_bits (bits_of k) = k" for k :: int
|
|
163 |
by (induction k rule: int_bit_induct) simp_all
|
|
164 |
qed
|
|
165 |
|
|
166 |
lemma bits_of_1_int [simp]:
|
|
167 |
"bits_of (1 :: int) = [True, False]"
|
|
168 |
by simp
|
|
169 |
|
|
170 |
lemma bits_of_int_numeral_simps [simp]:
|
|
171 |
"bits_of (numeral Num.One :: int) = [True, False]" (is ?One)
|
|
172 |
"bits_of (numeral (Num.Bit0 n) :: int) = False # bits_of (numeral n :: int)" (is ?Bit0)
|
|
173 |
"bits_of (numeral (Num.Bit1 n) :: int) = True # bits_of (numeral n :: int)" (is ?Bit1)
|
|
174 |
"bits_of (- numeral (Num.Bit0 n) :: int) = False # bits_of (- numeral n :: int)" (is ?nBit0)
|
|
175 |
"bits_of (- numeral (Num.Bit1 n) :: int) = True # bits_of (- numeral (Num.inc n) :: int)" (is ?nBit1)
|
|
176 |
proof -
|
|
177 |
show ?One
|
|
178 |
by simp
|
|
179 |
define k :: int where "k = numeral n"
|
|
180 |
then have "k > 0" and *: "numeral n = k" "numeral (Num.Bit0 n) = 2 * k" "numeral (Num.Bit1 n) = 2 * k + 1"
|
|
181 |
"numeral (Num.inc n) = k + 1"
|
|
182 |
by (simp_all add: add_One)
|
|
183 |
have "- (2 * k) div 2 = - k" "(- (2 * k) - 1) div 2 = - k - 1"
|
|
184 |
by simp_all
|
|
185 |
with \<open>k > 0\<close> show ?Bit0 ?Bit1 ?nBit0 ?nBit1
|
|
186 |
by (simp_all add: *)
|
|
187 |
qed
|
|
188 |
|
|
189 |
lemma of_bits_append [simp]:
|
|
190 |
"of_bits (bs @ cs) = of_bits bs + push_bit (length bs) (of_bits cs :: int)"
|
|
191 |
if "bs \<noteq> []" "\<not> last bs"
|
|
192 |
using that proof (induction bs rule: list_nonempty_induct)
|
|
193 |
case (single b)
|
|
194 |
then show ?case
|
|
195 |
by simp
|
|
196 |
next
|
|
197 |
case (cons b bs)
|
|
198 |
then show ?case
|
|
199 |
by (cases b) (simp_all add: push_bit_double)
|
|
200 |
qed
|
|
201 |
|
|
202 |
lemma of_bits_replicate_False [simp]:
|
|
203 |
"of_bits (replicate n False) = (0 :: int)"
|
|
204 |
by (auto simp add: of_bits_int_def)
|
|
205 |
|
|
206 |
lemma of_bits_drop [simp]:
|
|
207 |
"of_bits (drop n bs) = drop_bit n (of_bits bs :: int)"
|
|
208 |
if "n < length bs"
|
|
209 |
using that proof (induction bs arbitrary: n)
|
|
210 |
case Nil
|
|
211 |
then show ?case
|
|
212 |
by simp
|
|
213 |
next
|
|
214 |
case (Cons b bs)
|
|
215 |
show ?case
|
|
216 |
proof (cases n)
|
|
217 |
case 0
|
|
218 |
then show ?thesis
|
|
219 |
by simp
|
|
220 |
next
|
|
221 |
case (Suc n)
|
|
222 |
with Cons.prems have "bs \<noteq> []"
|
|
223 |
by auto
|
|
224 |
with Suc Cons.IH [of n] Cons.prems show ?thesis
|
|
225 |
by (cases b) simp_all
|
|
226 |
qed
|
|
227 |
qed
|
67909
|
228 |
|
|
229 |
end
|
|
230 |
|
70912
|
231 |
|
|
232 |
subsection \<open>Syntactic bit operations\<close>
|
|
233 |
|
|
234 |
class bit_operations = bit_representation +
|
|
235 |
fixes not :: "'a \<Rightarrow> 'a" ("NOT _" [70] 71)
|
|
236 |
and "and" :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "AND" 64)
|
|
237 |
and or :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "OR" 59)
|
|
238 |
and xor :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "XOR" 59)
|
|
239 |
and shift_left :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixl "<<" 55)
|
|
240 |
and shift_right :: "'a \<Rightarrow> nat \<Rightarrow> 'a" (infixl ">>" 55)
|
|
241 |
assumes not_eq: "not = of_bits \<circ> map Not \<circ> bits_of"
|
|
242 |
and and_eq: "length bs = length cs \<Longrightarrow>
|
|
243 |
of_bits bs AND of_bits cs = of_bits (map2 (\<and>) bs cs)"
|
|
244 |
and semilattice_and: "semilattice (AND)"
|
|
245 |
and or_eq: "length bs = length cs \<Longrightarrow>
|
|
246 |
of_bits bs OR of_bits cs = of_bits (map2 (\<or>) bs cs)"
|
|
247 |
and semilattice_or: "semilattice (OR)"
|
|
248 |
and xor_eq: "length bs = length cs \<Longrightarrow>
|
|
249 |
of_bits bs XOR of_bits cs = of_bits (map2 (\<noteq>) bs cs)"
|
|
250 |
and abel_semigroup_xor: "abel_semigroup (XOR)"
|
|
251 |
and shift_right_eq: "a << n = of_bits (replicate n False @ bits_of a)"
|
|
252 |
and shift_left_eq: "n < length (bits_of a) \<Longrightarrow> a >> n = of_bits (drop n (bits_of a))"
|
67909
|
253 |
begin
|
|
254 |
|
70912
|
255 |
text \<open>
|
|
256 |
We want the bitwise operations to bind slightly weaker
|
|
257 |
than \<open>+\<close> and \<open>-\<close>, but \<open>~~\<close> to
|
|
258 |
bind slightly stronger than \<open>*\<close>.
|
|
259 |
\<close>
|
|
260 |
|
|
261 |
sublocale "and": semilattice "(AND)"
|
|
262 |
by (fact semilattice_and)
|
|
263 |
|
|
264 |
sublocale or: semilattice "(OR)"
|
|
265 |
by (fact semilattice_or)
|
|
266 |
|
|
267 |
sublocale xor: abel_semigroup "(XOR)"
|
|
268 |
by (fact abel_semigroup_xor)
|
|
269 |
|
|
270 |
end
|
|
271 |
|
|
272 |
|
|
273 |
subsubsection \<open>Instance \<^typ>\<open>nat\<close>\<close>
|
|
274 |
|
|
275 |
locale zip_nat = single: abel_semigroup f
|
|
276 |
for f :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infixl "\<^bold>*" 70) +
|
|
277 |
assumes end_of_bits: "\<not> False \<^bold>* False"
|
|
278 |
begin
|
|
279 |
|
|
280 |
lemma False_P_imp:
|
|
281 |
"False \<^bold>* True \<and> P" if "False \<^bold>* P"
|
|
282 |
using that end_of_bits by (cases P) simp_all
|
|
283 |
|
|
284 |
function F :: "nat \<Rightarrow> nat \<Rightarrow> nat" (infixl "\<^bold>\<times>" 70)
|
|
285 |
where "m \<^bold>\<times> n = (if m = 0 \<and> n = 0 then 0
|
|
286 |
else of_bool (odd m \<^bold>* odd n) + (m div 2) \<^bold>\<times> (n div 2) * 2)"
|
67909
|
287 |
by auto
|
|
288 |
|
|
289 |
termination
|
70912
|
290 |
by (relation "measure (case_prod (+))") auto
|
67909
|
291 |
|
70912
|
292 |
lemma zero_left_eq:
|
|
293 |
"0 \<^bold>\<times> n = of_bool (False \<^bold>* True) * n"
|
|
294 |
by (induction n rule: nat_bit_induct) (simp_all add: end_of_bits)
|
|
295 |
|
|
296 |
lemma zero_right_eq:
|
|
297 |
"m \<^bold>\<times> 0 = of_bool (True \<^bold>* False) * m"
|
|
298 |
by (induction m rule: nat_bit_induct) (simp_all add: end_of_bits)
|
67909
|
299 |
|
70912
|
300 |
lemma simps [simp]:
|
|
301 |
"0 \<^bold>\<times> 0 = 0"
|
|
302 |
"0 \<^bold>\<times> n = of_bool (False \<^bold>* True) * n"
|
|
303 |
"m \<^bold>\<times> 0 = of_bool (True \<^bold>* False) * m"
|
|
304 |
"m > 0 \<Longrightarrow> n > 0 \<Longrightarrow> m \<^bold>\<times> n = of_bool (odd m \<^bold>* odd n) + (m div 2) \<^bold>\<times> (n div 2) * 2"
|
|
305 |
by (simp_all only: zero_left_eq zero_right_eq) simp
|
67909
|
306 |
|
70912
|
307 |
lemma rec:
|
|
308 |
"m \<^bold>\<times> n = of_bool (odd m \<^bold>* odd n) + (m div 2) \<^bold>\<times> (n div 2) * 2"
|
|
309 |
by (cases "m = 0 \<and> n = 0") (auto simp add: end_of_bits)
|
|
310 |
|
|
311 |
declare F.simps [simp del]
|
67909
|
312 |
|
70912
|
313 |
sublocale abel_semigroup F
|
|
314 |
proof
|
|
315 |
show "m \<^bold>\<times> n \<^bold>\<times> q = m \<^bold>\<times> (n \<^bold>\<times> q)" for m n q :: nat
|
|
316 |
proof (induction m arbitrary: n q rule: nat_bit_induct)
|
|
317 |
case zero
|
|
318 |
show ?case
|
|
319 |
by simp
|
|
320 |
next
|
|
321 |
case (even m)
|
|
322 |
with rec [of "2 * m"] rec [of _ q] show ?case
|
|
323 |
by (cases "even n") (auto dest: False_P_imp)
|
|
324 |
next
|
|
325 |
case (odd m)
|
|
326 |
with rec [of "Suc (2 * m)"] rec [of _ q] show ?case
|
|
327 |
by (cases "even n"; cases "even q")
|
|
328 |
(auto dest: False_P_imp simp add: ac_simps)
|
|
329 |
qed
|
|
330 |
show "m \<^bold>\<times> n = n \<^bold>\<times> m" for m n :: nat
|
|
331 |
proof (induction m arbitrary: n rule: nat_bit_induct)
|
|
332 |
case zero
|
|
333 |
show ?case
|
|
334 |
by (simp add: ac_simps)
|
|
335 |
next
|
|
336 |
case (even m)
|
|
337 |
with rec [of "2 * m" n] rec [of n "2 * m"] show ?case
|
|
338 |
by (simp add: ac_simps)
|
|
339 |
next
|
|
340 |
case (odd m)
|
|
341 |
with rec [of "Suc (2 * m)" n] rec [of n "Suc (2 * m)"] show ?case
|
|
342 |
by (simp add: ac_simps)
|
|
343 |
qed
|
|
344 |
qed
|
67909
|
345 |
|
70912
|
346 |
lemma self [simp]:
|
|
347 |
"n \<^bold>\<times> n = of_bool (True \<^bold>* True) * n"
|
|
348 |
by (induction n rule: nat_bit_induct) (simp_all add: end_of_bits)
|
67909
|
349 |
|
70912
|
350 |
lemma even_iff [simp]:
|
|
351 |
"even (m \<^bold>\<times> n) \<longleftrightarrow> \<not> (odd m \<^bold>* odd n)"
|
|
352 |
proof (induction m arbitrary: n rule: nat_bit_induct)
|
|
353 |
case zero
|
|
354 |
show ?case
|
|
355 |
by (cases "even n") (simp_all add: end_of_bits)
|
|
356 |
next
|
|
357 |
case (even m)
|
|
358 |
then show ?case
|
|
359 |
by (simp add: rec [of "2 * m"])
|
|
360 |
next
|
|
361 |
case (odd m)
|
|
362 |
then show ?case
|
|
363 |
by (simp add: rec [of "Suc (2 * m)"])
|
|
364 |
qed
|
|
365 |
|
|
366 |
lemma of_bits:
|
|
367 |
"of_bits bs \<^bold>\<times> of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: nat)"
|
|
368 |
if "length bs = length cs" for bs cs
|
|
369 |
using that proof (induction bs cs rule: list_induct2)
|
|
370 |
case Nil
|
|
371 |
then show ?case
|
|
372 |
by simp
|
|
373 |
next
|
|
374 |
case (Cons b bs c cs)
|
|
375 |
then show ?case
|
|
376 |
by (cases "of_bits bs = (0::nat) \<or> of_bits cs = (0::nat)")
|
|
377 |
(auto simp add: ac_simps end_of_bits rec [of "Suc 0"])
|
|
378 |
qed
|
67909
|
379 |
|
|
380 |
end
|
|
381 |
|
70912
|
382 |
instantiation nat :: bit_operations
|
67909
|
383 |
begin
|
|
384 |
|
70912
|
385 |
definition not_nat :: "nat \<Rightarrow> nat"
|
|
386 |
where "not_nat = of_bits \<circ> map Not \<circ> bits_of"
|
|
387 |
|
|
388 |
global_interpretation and_nat: zip_nat "(\<and>)"
|
|
389 |
defines and_nat = and_nat.F
|
|
390 |
by standard auto
|
67909
|
391 |
|
70912
|
392 |
global_interpretation and_nat: semilattice "(AND) :: nat \<Rightarrow> nat \<Rightarrow> nat"
|
|
393 |
proof (rule semilattice.intro, fact and_nat.abel_semigroup_axioms, standard)
|
|
394 |
show "n AND n = n" for n :: nat
|
|
395 |
by (simp add: and_nat.self)
|
|
396 |
qed
|
|
397 |
|
|
398 |
declare and_nat.simps [simp] \<comment> \<open>inconsistent declaration handling by
|
|
399 |
\<open>global_interpretation\<close> in \<open>instantiation\<close>\<close>
|
67909
|
400 |
|
70912
|
401 |
lemma zero_nat_and_eq [simp]:
|
|
402 |
"0 AND n = 0" for n :: nat
|
|
403 |
by simp
|
|
404 |
|
|
405 |
lemma and_zero_nat_eq [simp]:
|
|
406 |
"n AND 0 = 0" for n :: nat
|
|
407 |
by simp
|
|
408 |
|
|
409 |
global_interpretation or_nat: zip_nat "(\<or>)"
|
|
410 |
defines or_nat = or_nat.F
|
|
411 |
by standard auto
|
|
412 |
|
|
413 |
global_interpretation or_nat: semilattice "(OR) :: nat \<Rightarrow> nat \<Rightarrow> nat"
|
|
414 |
proof (rule semilattice.intro, fact or_nat.abel_semigroup_axioms, standard)
|
|
415 |
show "n OR n = n" for n :: nat
|
|
416 |
by (simp add: or_nat.self)
|
67909
|
417 |
qed
|
|
418 |
|
70912
|
419 |
declare or_nat.simps [simp] \<comment> \<open>inconsistent declaration handling by
|
|
420 |
\<open>global_interpretation\<close> in \<open>instantiation\<close>\<close>
|
|
421 |
|
|
422 |
lemma zero_nat_or_eq [simp]:
|
|
423 |
"0 OR n = n" for n :: nat
|
|
424 |
by simp
|
|
425 |
|
|
426 |
lemma or_zero_nat_eq [simp]:
|
|
427 |
"n OR 0 = n" for n :: nat
|
|
428 |
by simp
|
|
429 |
|
|
430 |
global_interpretation xor_nat: zip_nat "(\<noteq>)"
|
|
431 |
defines xor_nat = xor_nat.F
|
|
432 |
by standard auto
|
|
433 |
|
|
434 |
declare xor_nat.simps [simp] \<comment> \<open>inconsistent declaration handling by
|
|
435 |
\<open>global_interpretation\<close> in \<open>instantiation\<close>\<close>
|
|
436 |
|
|
437 |
lemma zero_nat_xor_eq [simp]:
|
|
438 |
"0 XOR n = n" for n :: nat
|
|
439 |
by simp
|
|
440 |
|
|
441 |
lemma xor_zero_nat_eq [simp]:
|
|
442 |
"n XOR 0 = n" for n :: nat
|
|
443 |
by simp
|
|
444 |
|
|
445 |
definition shift_left_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat"
|
|
446 |
where [simp]: "m << n = push_bit n m" for m :: nat
|
|
447 |
|
|
448 |
definition shift_right_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat"
|
|
449 |
where [simp]: "m >> n = drop_bit n m" for m :: nat
|
|
450 |
|
|
451 |
instance proof
|
|
452 |
show "semilattice ((AND) :: nat \<Rightarrow> _)"
|
|
453 |
by (fact and_nat.semilattice_axioms)
|
|
454 |
show "semilattice ((OR):: nat \<Rightarrow> _)"
|
|
455 |
by (fact or_nat.semilattice_axioms)
|
|
456 |
show "abel_semigroup ((XOR):: nat \<Rightarrow> _)"
|
|
457 |
by (fact xor_nat.abel_semigroup_axioms)
|
|
458 |
show "(not :: nat \<Rightarrow> _) = of_bits \<circ> map Not \<circ> bits_of"
|
|
459 |
by (fact not_nat_def)
|
|
460 |
show "of_bits bs AND of_bits cs = (of_bits (map2 (\<and>) bs cs) :: nat)"
|
|
461 |
if "length bs = length cs" for bs cs
|
|
462 |
using that by (fact and_nat.of_bits)
|
|
463 |
show "of_bits bs OR of_bits cs = (of_bits (map2 (\<or>) bs cs) :: nat)"
|
|
464 |
if "length bs = length cs" for bs cs
|
|
465 |
using that by (fact or_nat.of_bits)
|
|
466 |
show "of_bits bs XOR of_bits cs = (of_bits (map2 (\<noteq>) bs cs) :: nat)"
|
|
467 |
if "length bs = length cs" for bs cs
|
|
468 |
using that by (fact xor_nat.of_bits)
|
|
469 |
show "m << n = of_bits (replicate n False @ bits_of m)"
|
|
470 |
for m n :: nat
|
67909
|
471 |
by simp
|
70912
|
472 |
show "m >> n = of_bits (drop n (bits_of m))"
|
|
473 |
for m n :: nat
|
67909
|
474 |
by simp
|
|
475 |
qed
|
|
476 |
|
70912
|
477 |
end
|
|
478 |
|
|
479 |
global_interpretation or_nat: semilattice_neutr "(OR)" "0 :: nat"
|
|
480 |
by standard simp
|
|
481 |
|
|
482 |
global_interpretation xor_nat: comm_monoid "(XOR)" "0 :: nat"
|
|
483 |
by standard simp
|
|
484 |
|
|
485 |
lemma not_nat_simps [simp]:
|
|
486 |
"NOT 0 = (0::nat)"
|
|
487 |
"n > 0 \<Longrightarrow> NOT n = of_bool (even n) + 2 * NOT (n div 2)" for n :: nat
|
|
488 |
by (simp_all add: not_eq)
|
|
489 |
|
|
490 |
lemma not_1_nat [simp]:
|
|
491 |
"NOT 1 = (0::nat)"
|
|
492 |
by simp
|
|
493 |
|
|
494 |
lemma not_Suc_0 [simp]:
|
|
495 |
"NOT (Suc 0) = (0::nat)"
|
|
496 |
by simp
|
|
497 |
|
|
498 |
lemma Suc_0_and_eq [simp]:
|
|
499 |
"Suc 0 AND n = n mod 2"
|
|
500 |
by (cases n) auto
|
|
501 |
|
|
502 |
lemma and_Suc_0_eq [simp]:
|
|
503 |
"n AND Suc 0 = n mod 2"
|
|
504 |
using Suc_0_and_eq [of n] by (simp add: ac_simps)
|
|
505 |
|
|
506 |
lemma Suc_0_or_eq [simp]:
|
|
507 |
"Suc 0 OR n = n + of_bool (even n)"
|
|
508 |
by (cases n) (simp_all add: ac_simps)
|
|
509 |
|
|
510 |
lemma or_Suc_0_eq [simp]:
|
|
511 |
"n OR Suc 0 = n + of_bool (even n)"
|
|
512 |
using Suc_0_or_eq [of n] by (simp add: ac_simps)
|
|
513 |
|
|
514 |
lemma Suc_0_xor_eq [simp]:
|
|
515 |
"Suc 0 XOR n = n + of_bool (even n) - of_bool (odd n)"
|
|
516 |
by (cases n) (simp_all add: ac_simps)
|
|
517 |
|
|
518 |
lemma xor_Suc_0_eq [simp]:
|
|
519 |
"n XOR Suc 0 = n + of_bool (even n) - of_bool (odd n)"
|
|
520 |
using Suc_0_xor_eq [of n] by (simp add: ac_simps)
|
|
521 |
|
|
522 |
|
|
523 |
subsubsection \<open>Instance \<^typ>\<open>int\<close>\<close>
|
|
524 |
|
|
525 |
abbreviation (input) complement :: "int \<Rightarrow> int"
|
|
526 |
where "complement k \<equiv> - k - 1"
|
|
527 |
|
|
528 |
lemma complement_half:
|
|
529 |
"complement (k * 2) div 2 = complement k"
|
|
530 |
by simp
|
|
531 |
|
|
532 |
locale zip_int = single: abel_semigroup f
|
|
533 |
for f :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infixl "\<^bold>*" 70)
|
|
534 |
begin
|
|
535 |
|
|
536 |
lemma False_False_imp_True_True:
|
|
537 |
"True \<^bold>* True" if "False \<^bold>* False"
|
|
538 |
proof (rule ccontr)
|
|
539 |
assume "\<not> True \<^bold>* True"
|
|
540 |
with that show False
|
|
541 |
using single.assoc [of False True True]
|
|
542 |
by (cases "False \<^bold>* True") simp_all
|
|
543 |
qed
|
|
544 |
|
|
545 |
function F :: "int \<Rightarrow> int \<Rightarrow> int" (infixl "\<^bold>\<times>" 70)
|
|
546 |
where "k \<^bold>\<times> l = (if k \<in> {0, - 1} \<and> l \<in> {0, - 1}
|
|
547 |
then - of_bool (odd k \<^bold>* odd l)
|
|
548 |
else of_bool (odd k \<^bold>* odd l) + (k div 2) \<^bold>\<times> (l div 2) * 2)"
|
|
549 |
by auto
|
|
550 |
|
|
551 |
termination
|
|
552 |
by (relation "measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>))") auto
|
|
553 |
|
|
554 |
lemma zero_left_eq:
|
|
555 |
"0 \<^bold>\<times> l = (case (False \<^bold>* False, False \<^bold>* True)
|
|
556 |
of (False, False) \<Rightarrow> 0
|
|
557 |
| (False, True) \<Rightarrow> l
|
|
558 |
| (True, False) \<Rightarrow> complement l
|
|
559 |
| (True, True) \<Rightarrow> - 1)"
|
|
560 |
by (induction l rule: int_bit_induct)
|
|
561 |
(simp_all split: bool.split)
|
|
562 |
|
|
563 |
lemma minus_left_eq:
|
|
564 |
"- 1 \<^bold>\<times> l = (case (True \<^bold>* False, True \<^bold>* True)
|
|
565 |
of (False, False) \<Rightarrow> 0
|
|
566 |
| (False, True) \<Rightarrow> l
|
|
567 |
| (True, False) \<Rightarrow> complement l
|
|
568 |
| (True, True) \<Rightarrow> - 1)"
|
|
569 |
by (induction l rule: int_bit_induct)
|
|
570 |
(simp_all split: bool.split)
|
|
571 |
|
|
572 |
lemma zero_right_eq:
|
|
573 |
"k \<^bold>\<times> 0 = (case (False \<^bold>* False, False \<^bold>* True)
|
|
574 |
of (False, False) \<Rightarrow> 0
|
|
575 |
| (False, True) \<Rightarrow> k
|
|
576 |
| (True, False) \<Rightarrow> complement k
|
|
577 |
| (True, True) \<Rightarrow> - 1)"
|
|
578 |
by (induction k rule: int_bit_induct)
|
|
579 |
(simp_all add: ac_simps split: bool.split)
|
67909
|
580 |
|
70912
|
581 |
lemma minus_right_eq:
|
|
582 |
"k \<^bold>\<times> - 1 = (case (True \<^bold>* False, True \<^bold>* True)
|
|
583 |
of (False, False) \<Rightarrow> 0
|
|
584 |
| (False, True) \<Rightarrow> k
|
|
585 |
| (True, False) \<Rightarrow> complement k
|
|
586 |
| (True, True) \<Rightarrow> - 1)"
|
|
587 |
by (induction k rule: int_bit_induct)
|
|
588 |
(simp_all add: ac_simps split: bool.split)
|
|
589 |
|
|
590 |
lemma simps [simp]:
|
|
591 |
"0 \<^bold>\<times> 0 = - of_bool (False \<^bold>* False)"
|
|
592 |
"- 1 \<^bold>\<times> 0 = - of_bool (True \<^bold>* False)"
|
|
593 |
"0 \<^bold>\<times> - 1 = - of_bool (False \<^bold>* True)"
|
|
594 |
"- 1 \<^bold>\<times> - 1 = - of_bool (True \<^bold>* True)"
|
|
595 |
"0 \<^bold>\<times> l = (case (False \<^bold>* False, False \<^bold>* True)
|
|
596 |
of (False, False) \<Rightarrow> 0
|
|
597 |
| (False, True) \<Rightarrow> l
|
|
598 |
| (True, False) \<Rightarrow> complement l
|
|
599 |
| (True, True) \<Rightarrow> - 1)"
|
|
600 |
"- 1 \<^bold>\<times> l = (case (True \<^bold>* False, True \<^bold>* True)
|
|
601 |
of (False, False) \<Rightarrow> 0
|
|
602 |
| (False, True) \<Rightarrow> l
|
|
603 |
| (True, False) \<Rightarrow> complement l
|
|
604 |
| (True, True) \<Rightarrow> - 1)"
|
|
605 |
"k \<^bold>\<times> 0 = (case (False \<^bold>* False, False \<^bold>* True)
|
|
606 |
of (False, False) \<Rightarrow> 0
|
|
607 |
| (False, True) \<Rightarrow> k
|
|
608 |
| (True, False) \<Rightarrow> complement k
|
|
609 |
| (True, True) \<Rightarrow> - 1)"
|
|
610 |
"k \<^bold>\<times> - 1 = (case (True \<^bold>* False, True \<^bold>* True)
|
|
611 |
of (False, False) \<Rightarrow> 0
|
|
612 |
| (False, True) \<Rightarrow> k
|
|
613 |
| (True, False) \<Rightarrow> complement k
|
|
614 |
| (True, True) \<Rightarrow> - 1)"
|
|
615 |
"k \<noteq> 0 \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> l \<noteq> 0 \<Longrightarrow> l \<noteq> - 1
|
|
616 |
\<Longrightarrow> k \<^bold>\<times> l = of_bool (odd k \<^bold>* odd l) + (k div 2) \<^bold>\<times> (l div 2) * 2"
|
|
617 |
by simp_all[4] (simp_all only: zero_left_eq minus_left_eq zero_right_eq minus_right_eq, simp)
|
|
618 |
|
|
619 |
declare F.simps [simp del]
|
|
620 |
|
|
621 |
lemma rec:
|
|
622 |
"k \<^bold>\<times> l = of_bool (odd k \<^bold>* odd l) + (k div 2) \<^bold>\<times> (l div 2) * 2"
|
|
623 |
by (cases "k \<in> {0, - 1} \<and> l \<in> {0, - 1}") (auto simp add: ac_simps F.simps [of k l] split: bool.split)
|
|
624 |
|
|
625 |
sublocale abel_semigroup F
|
|
626 |
proof
|
|
627 |
show "k \<^bold>\<times> l \<^bold>\<times> r = k \<^bold>\<times> (l \<^bold>\<times> r)" for k l r :: int
|
|
628 |
proof (induction k arbitrary: l r rule: int_bit_induct)
|
67909
|
629 |
case zero
|
70912
|
630 |
have "complement l \<^bold>\<times> r = complement (l \<^bold>\<times> r)" if "False \<^bold>* False" "\<not> False \<^bold>* True"
|
|
631 |
proof (induction l arbitrary: r rule: int_bit_induct)
|
|
632 |
case zero
|
|
633 |
from that show ?case
|
|
634 |
by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
|
|
635 |
next
|
|
636 |
case minus
|
|
637 |
from that show ?case
|
|
638 |
by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
|
|
639 |
next
|
|
640 |
case (even l)
|
|
641 |
with that rec [of _ r] show ?case
|
|
642 |
by (cases "even r")
|
|
643 |
(auto simp add: complement_half ac_simps False_False_imp_True_True split: bool.splits)
|
|
644 |
next
|
|
645 |
case (odd l)
|
|
646 |
moreover have "- l - 1 = - 1 - l"
|
|
647 |
by simp
|
|
648 |
ultimately show ?case
|
|
649 |
using that rec [of _ r]
|
|
650 |
by (cases "even r")
|
|
651 |
(auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
|
|
652 |
qed
|
67909
|
653 |
then show ?case
|
70912
|
654 |
by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
|
|
655 |
next
|
|
656 |
case minus
|
|
657 |
have "complement l \<^bold>\<times> r = complement (l \<^bold>\<times> r)" if "\<not> True \<^bold>* True" "False \<^bold>* True"
|
|
658 |
proof (induction l arbitrary: r rule: int_bit_induct)
|
|
659 |
case zero
|
|
660 |
from that show ?case
|
|
661 |
by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
|
|
662 |
next
|
|
663 |
case minus
|
|
664 |
from that show ?case
|
|
665 |
by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
|
|
666 |
next
|
|
667 |
case (even l)
|
|
668 |
with that rec [of _ r] show ?case
|
|
669 |
by (cases "even r")
|
|
670 |
(auto simp add: complement_half ac_simps False_False_imp_True_True split: bool.splits)
|
|
671 |
next
|
|
672 |
case (odd l)
|
|
673 |
moreover have "- l - 1 = - 1 - l"
|
|
674 |
by simp
|
|
675 |
ultimately show ?case
|
|
676 |
using that rec [of _ r]
|
|
677 |
by (cases "even r")
|
|
678 |
(auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
|
|
679 |
qed
|
|
680 |
then show ?case
|
|
681 |
by (auto simp add: ac_simps False_False_imp_True_True split: bool.splits)
|
|
682 |
next
|
|
683 |
case (even k)
|
|
684 |
with rec [of "k * 2"] rec [of _ r] show ?case
|
|
685 |
by (cases "even r"; cases "even l") (auto simp add: ac_simps False_False_imp_True_True)
|
|
686 |
next
|
|
687 |
case (odd k)
|
|
688 |
with rec [of "1 + k * 2"] rec [of _ r] show ?case
|
|
689 |
by (cases "even r"; cases "even l") (auto simp add: ac_simps False_False_imp_True_True)
|
|
690 |
qed
|
|
691 |
show "k \<^bold>\<times> l = l \<^bold>\<times> k" for k l :: int
|
|
692 |
proof (induction k arbitrary: l rule: int_bit_induct)
|
|
693 |
case zero
|
|
694 |
show ?case
|
67909
|
695 |
by simp
|
|
696 |
next
|
70912
|
697 |
case minus
|
|
698 |
show ?case
|
|
699 |
by simp
|
67909
|
700 |
next
|
70912
|
701 |
case (even k)
|
|
702 |
with rec [of "k * 2" l] rec [of l "k * 2"] show ?case
|
|
703 |
by (simp add: ac_simps)
|
|
704 |
next
|
|
705 |
case (odd k)
|
|
706 |
with rec [of "k * 2 + 1" l] rec [of l "k * 2 + 1"] show ?case
|
|
707 |
by (simp add: ac_simps)
|
67909
|
708 |
qed
|
|
709 |
qed
|
|
710 |
|
70912
|
711 |
lemma self [simp]:
|
|
712 |
"k \<^bold>\<times> k = (case (False \<^bold>* False, True \<^bold>* True)
|
|
713 |
of (False, False) \<Rightarrow> 0
|
|
714 |
| (False, True) \<Rightarrow> k
|
|
715 |
| (True, True) \<Rightarrow> - 1)"
|
|
716 |
by (induction k rule: int_bit_induct) (auto simp add: False_False_imp_True_True split: bool.split)
|
|
717 |
|
|
718 |
lemma even_iff [simp]:
|
|
719 |
"even (k \<^bold>\<times> l) \<longleftrightarrow> \<not> (odd k \<^bold>* odd l)"
|
|
720 |
proof (induction k arbitrary: l rule: int_bit_induct)
|
|
721 |
case zero
|
|
722 |
show ?case
|
|
723 |
by (cases "even l") (simp_all split: bool.splits)
|
|
724 |
next
|
|
725 |
case minus
|
|
726 |
show ?case
|
|
727 |
by (cases "even l") (simp_all split: bool.splits)
|
|
728 |
next
|
|
729 |
case (even k)
|
|
730 |
then show ?case
|
|
731 |
by (simp add: rec [of "k * 2"])
|
|
732 |
next
|
|
733 |
case (odd k)
|
|
734 |
then show ?case
|
|
735 |
by (simp add: rec [of "1 + k * 2"])
|
|
736 |
qed
|
|
737 |
|
|
738 |
lemma of_bits:
|
|
739 |
"of_bits bs \<^bold>\<times> of_bits cs = (of_bits (map2 (\<^bold>*) bs cs) :: int)"
|
|
740 |
if "length bs = length cs" and "\<not> False \<^bold>* False" for bs cs
|
|
741 |
using \<open>length bs = length cs\<close> proof (induction bs cs rule: list_induct2)
|
|
742 |
case Nil
|
|
743 |
then show ?case
|
|
744 |
using \<open>\<not> False \<^bold>* False\<close> by (auto simp add: False_False_imp_True_True split: bool.splits)
|
|
745 |
next
|
|
746 |
case (Cons b bs c cs)
|
|
747 |
show ?case
|
|
748 |
proof (cases "bs = []")
|
|
749 |
case True
|
|
750 |
with Cons show ?thesis
|
|
751 |
using \<open>\<not> False \<^bold>* False\<close> by (cases b; cases c)
|
|
752 |
(auto simp add: ac_simps split: bool.splits)
|
|
753 |
next
|
|
754 |
case False
|
|
755 |
with Cons.hyps have "cs \<noteq> []"
|
|
756 |
by auto
|
|
757 |
with \<open>bs \<noteq> []\<close> have "map2 (\<^bold>*) bs cs \<noteq> []"
|
|
758 |
by (simp add: zip_eq_Nil_iff)
|
|
759 |
from \<open>bs \<noteq> []\<close> \<open>cs \<noteq> []\<close> \<open>map2 (\<^bold>*) bs cs \<noteq> []\<close> Cons show ?thesis
|
|
760 |
by (cases b; cases c)
|
|
761 |
(auto simp add: \<open>\<not> False \<^bold>* False\<close> ac_simps
|
|
762 |
rec [of "of_bits bs * 2"] rec [of "of_bits cs * 2"]
|
|
763 |
rec [of "1 + of_bits bs * 2"])
|
|
764 |
qed
|
|
765 |
qed
|
67909
|
766 |
|
|
767 |
end
|
|
768 |
|
70912
|
769 |
instantiation int :: bit_operations
|
|
770 |
begin
|
|
771 |
|
|
772 |
definition not_int :: "int \<Rightarrow> int"
|
|
773 |
where "not_int = complement"
|
|
774 |
|
|
775 |
global_interpretation and_int: zip_int "(\<and>)"
|
|
776 |
defines and_int = and_int.F
|
|
777 |
by standard
|
|
778 |
|
|
779 |
declare and_int.simps [simp] \<comment> \<open>inconsistent declaration handling by
|
|
780 |
\<open>global_interpretation\<close> in \<open>instantiation\<close>\<close>
|
|
781 |
|
|
782 |
global_interpretation and_int: semilattice "(AND) :: int \<Rightarrow> int \<Rightarrow> int"
|
|
783 |
proof (rule semilattice.intro, fact and_int.abel_semigroup_axioms, standard)
|
|
784 |
show "k AND k = k" for k :: int
|
|
785 |
by (simp add: and_int.self)
|
|
786 |
qed
|
|
787 |
|
|
788 |
lemma zero_int_and_eq [simp]:
|
|
789 |
"0 AND k = 0" for k :: int
|
|
790 |
by simp
|
|
791 |
|
|
792 |
lemma and_zero_int_eq [simp]:
|
|
793 |
"k AND 0 = 0" for k :: int
|
|
794 |
by simp
|
|
795 |
|
|
796 |
lemma minus_int_and_eq [simp]:
|
|
797 |
"- 1 AND k = k" for k :: int
|
|
798 |
by simp
|
|
799 |
|
|
800 |
lemma and_minus_int_eq [simp]:
|
|
801 |
"k AND - 1 = k" for k :: int
|
|
802 |
by simp
|
|
803 |
|
|
804 |
global_interpretation or_int: zip_int "(\<or>)"
|
|
805 |
defines or_int = or_int.F
|
|
806 |
by standard
|
|
807 |
|
|
808 |
declare or_int.simps [simp] \<comment> \<open>inconsistent declaration handling by
|
|
809 |
\<open>global_interpretation\<close> in \<open>instantiation\<close>\<close>
|
|
810 |
|
|
811 |
global_interpretation or_int: semilattice "(OR) :: int \<Rightarrow> int \<Rightarrow> int"
|
|
812 |
proof (rule semilattice.intro, fact or_int.abel_semigroup_axioms, standard)
|
|
813 |
show "k OR k = k" for k :: int
|
|
814 |
by (simp add: or_int.self)
|
|
815 |
qed
|
|
816 |
|
|
817 |
lemma zero_int_or_eq [simp]:
|
|
818 |
"0 OR k = k" for k :: int
|
|
819 |
by simp
|
|
820 |
|
|
821 |
lemma and_zero_or_eq [simp]:
|
|
822 |
"k OR 0 = k" for k :: int
|
|
823 |
by simp
|
|
824 |
|
|
825 |
lemma minus_int_or_eq [simp]:
|
|
826 |
"- 1 OR k = - 1" for k :: int
|
|
827 |
by simp
|
67909
|
828 |
|
70912
|
829 |
lemma or_minus_int_eq [simp]:
|
|
830 |
"k OR - 1 = - 1" for k :: int
|
|
831 |
by simp
|
|
832 |
|
|
833 |
global_interpretation xor_int: zip_int "(\<noteq>)"
|
|
834 |
defines xor_int = xor_int.F
|
|
835 |
by standard
|
|
836 |
|
|
837 |
declare xor_int.simps [simp] \<comment> \<open>inconsistent declaration handling by
|
|
838 |
\<open>global_interpretation\<close> in \<open>instantiation\<close>\<close>
|
|
839 |
|
|
840 |
lemma zero_int_xor_eq [simp]:
|
|
841 |
"0 XOR k = k" for k :: int
|
|
842 |
by simp
|
|
843 |
|
|
844 |
lemma and_zero_xor_eq [simp]:
|
|
845 |
"k XOR 0 = k" for k :: int
|
|
846 |
by simp
|
|
847 |
|
|
848 |
lemma minus_int_xor_eq [simp]:
|
|
849 |
"- 1 XOR k = complement k" for k :: int
|
|
850 |
by simp
|
|
851 |
|
|
852 |
lemma xor_minus_int_eq [simp]:
|
|
853 |
"k XOR - 1 = complement k" for k :: int
|
|
854 |
by simp
|
|
855 |
|
|
856 |
definition shift_left_int :: "int \<Rightarrow> nat \<Rightarrow> int"
|
|
857 |
where [simp]: "k << n = push_bit n k" for k :: int
|
|
858 |
|
|
859 |
definition shift_right_int :: "int \<Rightarrow> nat \<Rightarrow> int"
|
|
860 |
where [simp]: "k >> n = drop_bit n k" for k :: int
|
|
861 |
|
|
862 |
instance proof
|
|
863 |
show "semilattice ((AND) :: int \<Rightarrow> _)"
|
|
864 |
by (fact and_int.semilattice_axioms)
|
|
865 |
show "semilattice ((OR) :: int \<Rightarrow> _)"
|
|
866 |
by (fact or_int.semilattice_axioms)
|
|
867 |
show "abel_semigroup ((XOR) :: int \<Rightarrow> _)"
|
|
868 |
by (fact xor_int.abel_semigroup_axioms)
|
|
869 |
show "(not :: int \<Rightarrow> _) = of_bits \<circ> map Not \<circ> bits_of"
|
|
870 |
proof (rule sym, rule ext)
|
|
871 |
fix k :: int
|
|
872 |
show "(of_bits \<circ> map Not \<circ> bits_of) k = NOT k"
|
|
873 |
by (induction k rule: int_bit_induct) (simp_all add: not_int_def)
|
|
874 |
qed
|
|
875 |
show "of_bits bs AND of_bits cs = (of_bits (map2 (\<and>) bs cs) :: int)"
|
|
876 |
if "length bs = length cs" for bs cs
|
|
877 |
using that by (rule and_int.of_bits) simp
|
|
878 |
show "of_bits bs OR of_bits cs = (of_bits (map2 (\<or>) bs cs) :: int)"
|
|
879 |
if "length bs = length cs" for bs cs
|
|
880 |
using that by (rule or_int.of_bits) simp
|
|
881 |
show "of_bits bs XOR of_bits cs = (of_bits (map2 (\<noteq>) bs cs) :: int)"
|
|
882 |
if "length bs = length cs" for bs cs
|
|
883 |
using that by (rule xor_int.of_bits) simp
|
|
884 |
show "k << n = of_bits (replicate n False @ bits_of k)"
|
|
885 |
for k :: int and n :: nat
|
|
886 |
by (cases "n = 0") simp_all
|
|
887 |
show "k >> n = of_bits (drop n (bits_of k))"
|
|
888 |
if "n < length (bits_of k)"
|
|
889 |
for k :: int and n :: nat
|
|
890 |
using that by simp
|
|
891 |
qed
|
67909
|
892 |
|
|
893 |
end
|
70912
|
894 |
|
|
895 |
global_interpretation and_int: semilattice_neutr "(AND)" "- 1 :: int"
|
|
896 |
by standard simp
|
|
897 |
|
|
898 |
global_interpretation or_int: semilattice_neutr "(OR)" "0 :: int"
|
|
899 |
by standard simp
|
|
900 |
|
|
901 |
global_interpretation xor_int: comm_monoid "(XOR)" "0 :: int"
|
|
902 |
by standard simp
|
|
903 |
|
|
904 |
lemma not_int_simps [simp]:
|
|
905 |
"NOT 0 = (- 1 :: int)"
|
|
906 |
"NOT - 1 = (0 :: int)"
|
|
907 |
"k \<noteq> 0 \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> NOT k = of_bool (even k) + 2 * NOT (k div 2)" for k :: int
|
|
908 |
by (auto simp add: not_int_def elim: oddE)
|
|
909 |
|
|
910 |
lemma not_one_int [simp]:
|
|
911 |
"NOT 1 = (- 2 :: int)"
|
|
912 |
by simp
|
|
913 |
|
|
914 |
lemma one_and_int_eq [simp]:
|
|
915 |
"1 AND k = k mod 2" for k :: int
|
|
916 |
using and_int.rec [of 1] by (simp add: mod2_eq_if)
|
|
917 |
|
|
918 |
lemma and_one_int_eq [simp]:
|
|
919 |
"k AND 1 = k mod 2" for k :: int
|
|
920 |
using one_and_int_eq [of 1] by (simp add: ac_simps)
|
|
921 |
|
|
922 |
lemma one_or_int_eq [simp]:
|
|
923 |
"1 OR k = k + of_bool (even k)" for k :: int
|
|
924 |
using or_int.rec [of 1] by (auto elim: oddE)
|
|
925 |
|
|
926 |
lemma or_one_int_eq [simp]:
|
|
927 |
"k OR 1 = k + of_bool (even k)" for k :: int
|
|
928 |
using one_or_int_eq [of k] by (simp add: ac_simps)
|
|
929 |
|
|
930 |
lemma one_xor_int_eq [simp]:
|
|
931 |
"1 XOR k = k + of_bool (even k) - of_bool (odd k)" for k :: int
|
|
932 |
using xor_int.rec [of 1] by (auto elim: oddE)
|
|
933 |
|
|
934 |
lemma xor_one_int_eq [simp]:
|
|
935 |
"k XOR 1 = k + of_bool (even k) - of_bool (odd k)" for k :: int
|
|
936 |
using one_xor_int_eq [of k] by (simp add: ac_simps)
|
|
937 |
|
|
938 |
end
|