author | haftmann |
Sat, 11 Jul 2020 06:21:04 +0000 | |
changeset 72010 | a851ce626b78 |
parent 72009 | febdd4eead56 |
child 72023 | 08348e364739 |
permissions | -rw-r--r-- |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1 |
(* Author: Florian Haftmann, TUM |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
2 |
*) |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
3 |
|
71956 | 4 |
section \<open>Bit operations in suitable algebraic structures\<close> |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
5 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
6 |
theory Bit_Operations |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
7 |
imports |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
8 |
"HOL-Library.Boolean_Algebra" |
71095 | 9 |
Main |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
10 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
11 |
|
72010 | 12 |
subsection \<open>Preliminiaries\<close> \<comment> \<open>TODO move\<close> |
13 |
||
14 |
lemma take_bit_int_less_exp: |
|
15 |
\<open>take_bit n k < 2 ^ n\<close> for k :: int |
|
16 |
by (simp add: take_bit_eq_mod) |
|
17 |
||
18 |
lemma take_bit_Suc_from_most: |
|
19 |
\<open>take_bit (Suc n) k = 2 ^ n * of_bool (bit k n) + take_bit n k\<close> for k :: int |
|
20 |
by (simp only: take_bit_eq_mod power_Suc2) (simp_all add: bit_iff_odd odd_iff_mod_2_eq_one zmod_zmult2_eq) |
|
21 |
||
22 |
lemma take_bit_greater_eq: |
|
23 |
\<open>k + 2 ^ n \<le> take_bit n k\<close> if \<open>k < 0\<close> for k :: int |
|
24 |
proof - |
|
25 |
have \<open>k + 2 ^ n \<le> take_bit n (k + 2 ^ n)\<close> |
|
26 |
proof (cases \<open>k > - (2 ^ n)\<close>) |
|
27 |
case False |
|
28 |
then have \<open>k + 2 ^ n \<le> 0\<close> |
|
29 |
by simp |
|
30 |
also note take_bit_nonnegative |
|
31 |
finally show ?thesis . |
|
32 |
next |
|
33 |
case True |
|
34 |
with that have \<open>0 \<le> k + 2 ^ n\<close> and \<open>k + 2 ^ n < 2 ^ n\<close> |
|
35 |
by simp_all |
|
36 |
then show ?thesis |
|
37 |
by (simp only: take_bit_eq_mod mod_pos_pos_trivial) |
|
38 |
qed |
|
39 |
then show ?thesis |
|
40 |
by (simp add: take_bit_eq_mod) |
|
41 |
qed |
|
42 |
||
43 |
lemma take_bit_less_eq: |
|
44 |
\<open>take_bit n k \<le> k - 2 ^ n\<close> if \<open>2 ^ n \<le> k\<close> and \<open>n > 0\<close> for k :: int |
|
45 |
using that zmod_le_nonneg_dividend [of \<open>k - 2 ^ n\<close> \<open>2 ^ n\<close>] |
|
46 |
by (simp add: take_bit_eq_mod) |
|
47 |
||
48 |
||
71956 | 49 |
subsection \<open>Bit operations\<close> |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
50 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
51 |
class semiring_bit_operations = semiring_bit_shifts + |
71426 | 52 |
fixes "and" :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close> (infixr \<open>AND\<close> 64) |
53 |
and or :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close> (infixr \<open>OR\<close> 59) |
|
54 |
and xor :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close> (infixr \<open>XOR\<close> 59) |
|
71186 | 55 |
assumes bit_and_iff: \<open>\<And>n. bit (a AND b) n \<longleftrightarrow> bit a n \<and> bit b n\<close> |
56 |
and bit_or_iff: \<open>\<And>n. bit (a OR b) n \<longleftrightarrow> bit a n \<or> bit b n\<close> |
|
57 |
and bit_xor_iff: \<open>\<And>n. bit (a XOR b) n \<longleftrightarrow> bit a n \<noteq> bit b n\<close> |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
58 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
59 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
60 |
text \<open> |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
61 |
We want the bitwise operations to bind slightly weaker |
71094 | 62 |
than \<open>+\<close> and \<open>-\<close>. |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
63 |
For the sake of code generation |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
64 |
the operations \<^const>\<open>and\<close>, \<^const>\<open>or\<close> and \<^const>\<open>xor\<close> |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
65 |
are specified as definitional class operations. |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
66 |
\<close> |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
67 |
|
71418 | 68 |
sublocale "and": semilattice \<open>(AND)\<close> |
69 |
by standard (auto simp add: bit_eq_iff bit_and_iff) |
|
70 |
||
71 |
sublocale or: semilattice_neutr \<open>(OR)\<close> 0 |
|
72 |
by standard (auto simp add: bit_eq_iff bit_or_iff) |
|
73 |
||
74 |
sublocale xor: comm_monoid \<open>(XOR)\<close> 0 |
|
75 |
by standard (auto simp add: bit_eq_iff bit_xor_iff) |
|
76 |
||
71823 | 77 |
lemma even_and_iff: |
78 |
\<open>even (a AND b) \<longleftrightarrow> even a \<or> even b\<close> |
|
79 |
using bit_and_iff [of a b 0] by auto |
|
80 |
||
81 |
lemma even_or_iff: |
|
82 |
\<open>even (a OR b) \<longleftrightarrow> even a \<and> even b\<close> |
|
83 |
using bit_or_iff [of a b 0] by auto |
|
84 |
||
85 |
lemma even_xor_iff: |
|
86 |
\<open>even (a XOR b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)\<close> |
|
87 |
using bit_xor_iff [of a b 0] by auto |
|
88 |
||
71412 | 89 |
lemma zero_and_eq [simp]: |
90 |
"0 AND a = 0" |
|
91 |
by (simp add: bit_eq_iff bit_and_iff) |
|
92 |
||
93 |
lemma and_zero_eq [simp]: |
|
94 |
"a AND 0 = 0" |
|
95 |
by (simp add: bit_eq_iff bit_and_iff) |
|
96 |
||
71921 | 97 |
lemma one_and_eq: |
71822 | 98 |
"1 AND a = a mod 2" |
71418 | 99 |
by (simp add: bit_eq_iff bit_and_iff) (auto simp add: bit_1_iff) |
71412 | 100 |
|
71921 | 101 |
lemma and_one_eq: |
71822 | 102 |
"a AND 1 = a mod 2" |
71418 | 103 |
using one_and_eq [of a] by (simp add: ac_simps) |
104 |
||
71822 | 105 |
lemma one_or_eq: |
71418 | 106 |
"1 OR a = a + of_bool (even a)" |
107 |
by (simp add: bit_eq_iff bit_or_iff add.commute [of _ 1] even_bit_succ_iff) (auto simp add: bit_1_iff) |
|
71412 | 108 |
|
71822 | 109 |
lemma or_one_eq: |
71418 | 110 |
"a OR 1 = a + of_bool (even a)" |
111 |
using one_or_eq [of a] by (simp add: ac_simps) |
|
71412 | 112 |
|
71822 | 113 |
lemma one_xor_eq: |
71418 | 114 |
"1 XOR a = a + of_bool (even a) - of_bool (odd a)" |
115 |
by (simp add: bit_eq_iff bit_xor_iff add.commute [of _ 1] even_bit_succ_iff) (auto simp add: bit_1_iff odd_bit_iff_bit_pred elim: oddE) |
|
116 |
||
71822 | 117 |
lemma xor_one_eq: |
71418 | 118 |
"a XOR 1 = a + of_bool (even a) - of_bool (odd a)" |
119 |
using one_xor_eq [of a] by (simp add: ac_simps) |
|
71412 | 120 |
|
71409 | 121 |
lemma take_bit_and [simp]: |
122 |
\<open>take_bit n (a AND b) = take_bit n a AND take_bit n b\<close> |
|
123 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_and_iff) |
|
124 |
||
125 |
lemma take_bit_or [simp]: |
|
126 |
\<open>take_bit n (a OR b) = take_bit n a OR take_bit n b\<close> |
|
127 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_or_iff) |
|
128 |
||
129 |
lemma take_bit_xor [simp]: |
|
130 |
\<open>take_bit n (a XOR b) = take_bit n a XOR take_bit n b\<close> |
|
131 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_xor_iff) |
|
132 |
||
71823 | 133 |
definition mask :: \<open>nat \<Rightarrow> 'a\<close> |
134 |
where mask_eq_exp_minus_1: \<open>mask n = 2 ^ n - 1\<close> |
|
135 |
||
136 |
lemma bit_mask_iff: |
|
137 |
\<open>bit (mask m) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n < m\<close> |
|
138 |
by (simp add: mask_eq_exp_minus_1 bit_mask_iff) |
|
139 |
||
140 |
lemma even_mask_iff: |
|
141 |
\<open>even (mask n) \<longleftrightarrow> n = 0\<close> |
|
142 |
using bit_mask_iff [of n 0] by auto |
|
143 |
||
144 |
lemma mask_0 [simp, code]: |
|
145 |
\<open>mask 0 = 0\<close> |
|
146 |
by (simp add: mask_eq_exp_minus_1) |
|
147 |
||
148 |
lemma mask_Suc_exp [code]: |
|
149 |
\<open>mask (Suc n) = 2 ^ n OR mask n\<close> |
|
150 |
by (rule bit_eqI) |
|
151 |
(auto simp add: bit_or_iff bit_mask_iff bit_exp_iff not_less le_less_Suc_eq) |
|
152 |
||
153 |
lemma mask_Suc_double: |
|
154 |
\<open>mask (Suc n) = 2 * mask n OR 1\<close> |
|
155 |
proof (rule bit_eqI) |
|
156 |
fix q |
|
157 |
assume \<open>2 ^ q \<noteq> 0\<close> |
|
158 |
show \<open>bit (mask (Suc n)) q \<longleftrightarrow> bit (2 * mask n OR 1) q\<close> |
|
159 |
by (cases q) |
|
160 |
(simp_all add: even_mask_iff even_or_iff bit_or_iff bit_mask_iff bit_exp_iff bit_double_iff not_less le_less_Suc_eq bit_1_iff, auto simp add: mult_2) |
|
161 |
qed |
|
162 |
||
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71956
diff
changeset
|
163 |
lemma take_bit_eq_mask: |
71823 | 164 |
\<open>take_bit n a = a AND mask n\<close> |
165 |
by (rule bit_eqI) |
|
166 |
(auto simp add: bit_take_bit_iff bit_and_iff bit_mask_iff) |
|
167 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
168 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
169 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
170 |
class ring_bit_operations = semiring_bit_operations + ring_parity + |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
171 |
fixes not :: \<open>'a \<Rightarrow> 'a\<close> (\<open>NOT\<close>) |
71186 | 172 |
assumes bit_not_iff: \<open>\<And>n. bit (NOT a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit a n\<close> |
71409 | 173 |
assumes minus_eq_not_minus_1: \<open>- a = NOT (a - 1)\<close> |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
174 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
175 |
|
71409 | 176 |
text \<open> |
177 |
For the sake of code generation \<^const>\<open>not\<close> is specified as |
|
178 |
definitional class operation. Note that \<^const>\<open>not\<close> has no |
|
179 |
sensible definition for unlimited but only positive bit strings |
|
180 |
(type \<^typ>\<open>nat\<close>). |
|
181 |
\<close> |
|
182 |
||
71186 | 183 |
lemma bits_minus_1_mod_2_eq [simp]: |
184 |
\<open>(- 1) mod 2 = 1\<close> |
|
185 |
by (simp add: mod_2_eq_odd) |
|
186 |
||
71409 | 187 |
lemma not_eq_complement: |
188 |
\<open>NOT a = - a - 1\<close> |
|
189 |
using minus_eq_not_minus_1 [of \<open>a + 1\<close>] by simp |
|
190 |
||
191 |
lemma minus_eq_not_plus_1: |
|
192 |
\<open>- a = NOT a + 1\<close> |
|
193 |
using not_eq_complement [of a] by simp |
|
194 |
||
195 |
lemma bit_minus_iff: |
|
196 |
\<open>bit (- a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit (a - 1) n\<close> |
|
197 |
by (simp add: minus_eq_not_minus_1 bit_not_iff) |
|
198 |
||
71418 | 199 |
lemma even_not_iff [simp]: |
200 |
"even (NOT a) \<longleftrightarrow> odd a" |
|
201 |
using bit_not_iff [of a 0] by auto |
|
202 |
||
71409 | 203 |
lemma bit_not_exp_iff: |
204 |
\<open>bit (NOT (2 ^ m)) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n \<noteq> m\<close> |
|
205 |
by (auto simp add: bit_not_iff bit_exp_iff) |
|
206 |
||
71186 | 207 |
lemma bit_minus_1_iff [simp]: |
208 |
\<open>bit (- 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0\<close> |
|
71409 | 209 |
by (simp add: bit_minus_iff) |
210 |
||
211 |
lemma bit_minus_exp_iff: |
|
212 |
\<open>bit (- (2 ^ m)) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n \<ge> m\<close> |
|
213 |
oops |
|
214 |
||
215 |
lemma bit_minus_2_iff [simp]: |
|
216 |
\<open>bit (- 2) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n > 0\<close> |
|
217 |
by (simp add: bit_minus_iff bit_1_iff) |
|
71186 | 218 |
|
71418 | 219 |
lemma not_one [simp]: |
220 |
"NOT 1 = - 2" |
|
221 |
by (simp add: bit_eq_iff bit_not_iff) (simp add: bit_1_iff) |
|
222 |
||
223 |
sublocale "and": semilattice_neutr \<open>(AND)\<close> \<open>- 1\<close> |
|
224 |
apply standard |
|
71426 | 225 |
apply (simp add: bit_eq_iff bit_and_iff) |
226 |
apply (auto simp add: exp_eq_0_imp_not_bit bit_exp_iff) |
|
71418 | 227 |
done |
228 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
229 |
sublocale bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close> |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
230 |
rewrites \<open>bit.xor = (XOR)\<close> |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
231 |
proof - |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
232 |
interpret bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close> |
71186 | 233 |
apply standard |
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71956
diff
changeset
|
234 |
apply (simp_all add: bit_eq_iff) |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71956
diff
changeset
|
235 |
apply (auto simp add: bit_and_iff bit_or_iff bit_not_iff bit_exp_iff exp_eq_0_imp_not_bit) |
71186 | 236 |
done |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
237 |
show \<open>boolean_algebra (AND) (OR) NOT 0 (- 1)\<close> |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
238 |
by standard |
71426 | 239 |
show \<open>boolean_algebra.xor (AND) (OR) NOT = (XOR)\<close> |
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71956
diff
changeset
|
240 |
apply (simp add: fun_eq_iff bit_eq_iff bit.xor_def) |
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71956
diff
changeset
|
241 |
apply (auto simp add: bit_and_iff bit_or_iff bit_not_iff bit_xor_iff exp_eq_0_imp_not_bit) |
71186 | 242 |
done |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
243 |
qed |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
244 |
|
71802 | 245 |
lemma and_eq_not_not_or: |
246 |
\<open>a AND b = NOT (NOT a OR NOT b)\<close> |
|
247 |
by simp |
|
248 |
||
249 |
lemma or_eq_not_not_and: |
|
250 |
\<open>a OR b = NOT (NOT a AND NOT b)\<close> |
|
251 |
by simp |
|
252 |
||
72009 | 253 |
lemma not_add_distrib: |
254 |
\<open>NOT (a + b) = NOT a - b\<close> |
|
255 |
by (simp add: not_eq_complement algebra_simps) |
|
256 |
||
257 |
lemma not_diff_distrib: |
|
258 |
\<open>NOT (a - b) = NOT a + b\<close> |
|
259 |
using not_add_distrib [of a \<open>- b\<close>] by simp |
|
260 |
||
71412 | 261 |
lemma push_bit_minus: |
262 |
\<open>push_bit n (- a) = - push_bit n a\<close> |
|
263 |
by (simp add: push_bit_eq_mult) |
|
264 |
||
71409 | 265 |
lemma take_bit_not_take_bit: |
266 |
\<open>take_bit n (NOT (take_bit n a)) = take_bit n (NOT a)\<close> |
|
267 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_not_iff) |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
268 |
|
71418 | 269 |
lemma take_bit_not_iff: |
270 |
"take_bit n (NOT a) = take_bit n (NOT b) \<longleftrightarrow> take_bit n a = take_bit n b" |
|
271 |
apply (simp add: bit_eq_iff bit_not_iff bit_take_bit_iff) |
|
272 |
apply (simp add: bit_exp_iff) |
|
273 |
apply (use local.exp_eq_0_imp_not_bit in blast) |
|
274 |
done |
|
275 |
||
71922 | 276 |
lemma take_bit_minus_one_eq_mask: |
277 |
\<open>take_bit n (- 1) = mask n\<close> |
|
278 |
by (simp add: bit_eq_iff bit_mask_iff bit_take_bit_iff conj_commute) |
|
279 |
||
72010 | 280 |
lemma minus_exp_eq_not_mask: |
281 |
\<open>- (2 ^ n) = NOT (mask n)\<close> |
|
282 |
by (rule bit_eqI) (simp add: bit_minus_iff bit_not_iff flip: mask_eq_exp_minus_1) |
|
283 |
||
71922 | 284 |
lemma push_bit_minus_one_eq_not_mask: |
285 |
\<open>push_bit n (- 1) = NOT (mask n)\<close> |
|
72010 | 286 |
by (simp add: push_bit_eq_mult minus_exp_eq_not_mask) |
287 |
||
288 |
lemma take_bit_not_mask_eq_0: |
|
289 |
\<open>take_bit m (NOT (mask n)) = 0\<close> if \<open>n \<ge> m\<close> |
|
290 |
by (rule bit_eqI) (use that in \<open>simp add: bit_take_bit_iff bit_not_iff bit_mask_iff\<close>) |
|
71922 | 291 |
|
71426 | 292 |
definition set_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
71991 | 293 |
where \<open>set_bit n a = a OR push_bit n 1\<close> |
71426 | 294 |
|
295 |
definition unset_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
71991 | 296 |
where \<open>unset_bit n a = a AND NOT (push_bit n 1)\<close> |
71426 | 297 |
|
298 |
definition flip_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
71991 | 299 |
where \<open>flip_bit n a = a XOR push_bit n 1\<close> |
71426 | 300 |
|
301 |
lemma bit_set_bit_iff: |
|
302 |
\<open>bit (set_bit m a) n \<longleftrightarrow> bit a n \<or> (m = n \<and> 2 ^ n \<noteq> 0)\<close> |
|
71991 | 303 |
by (auto simp add: set_bit_def push_bit_of_1 bit_or_iff bit_exp_iff) |
71426 | 304 |
|
305 |
lemma even_set_bit_iff: |
|
306 |
\<open>even (set_bit m a) \<longleftrightarrow> even a \<and> m \<noteq> 0\<close> |
|
307 |
using bit_set_bit_iff [of m a 0] by auto |
|
308 |
||
309 |
lemma bit_unset_bit_iff: |
|
310 |
\<open>bit (unset_bit m a) n \<longleftrightarrow> bit a n \<and> m \<noteq> n\<close> |
|
71991 | 311 |
by (auto simp add: unset_bit_def push_bit_of_1 bit_and_iff bit_not_iff bit_exp_iff exp_eq_0_imp_not_bit) |
71426 | 312 |
|
313 |
lemma even_unset_bit_iff: |
|
314 |
\<open>even (unset_bit m a) \<longleftrightarrow> even a \<or> m = 0\<close> |
|
315 |
using bit_unset_bit_iff [of m a 0] by auto |
|
316 |
||
317 |
lemma bit_flip_bit_iff: |
|
318 |
\<open>bit (flip_bit m a) n \<longleftrightarrow> (m = n \<longleftrightarrow> \<not> bit a n) \<and> 2 ^ n \<noteq> 0\<close> |
|
71991 | 319 |
by (auto simp add: flip_bit_def push_bit_of_1 bit_xor_iff bit_exp_iff exp_eq_0_imp_not_bit) |
71426 | 320 |
|
321 |
lemma even_flip_bit_iff: |
|
322 |
\<open>even (flip_bit m a) \<longleftrightarrow> \<not> (even a \<longleftrightarrow> m = 0)\<close> |
|
323 |
using bit_flip_bit_iff [of m a 0] by auto |
|
324 |
||
325 |
lemma set_bit_0 [simp]: |
|
326 |
\<open>set_bit 0 a = 1 + 2 * (a div 2)\<close> |
|
327 |
proof (rule bit_eqI) |
|
328 |
fix m |
|
329 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
330 |
then show \<open>bit (set_bit 0 a) m = bit (1 + 2 * (a div 2)) m\<close> |
|
331 |
by (simp add: bit_set_bit_iff bit_double_iff even_bit_succ_iff) |
|
71535
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71442
diff
changeset
|
332 |
(cases m, simp_all add: bit_Suc) |
71426 | 333 |
qed |
334 |
||
71821 | 335 |
lemma set_bit_Suc: |
71426 | 336 |
\<open>set_bit (Suc n) a = a mod 2 + 2 * set_bit n (a div 2)\<close> |
337 |
proof (rule bit_eqI) |
|
338 |
fix m |
|
339 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
340 |
show \<open>bit (set_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * set_bit n (a div 2)) m\<close> |
|
341 |
proof (cases m) |
|
342 |
case 0 |
|
343 |
then show ?thesis |
|
344 |
by (simp add: even_set_bit_iff) |
|
345 |
next |
|
346 |
case (Suc m) |
|
347 |
with * have \<open>2 ^ m \<noteq> 0\<close> |
|
348 |
using mult_2 by auto |
|
349 |
show ?thesis |
|
350 |
by (cases a rule: parity_cases) |
|
351 |
(simp_all add: bit_set_bit_iff bit_double_iff even_bit_succ_iff *, |
|
71535
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71442
diff
changeset
|
352 |
simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close> bit_Suc) |
71426 | 353 |
qed |
354 |
qed |
|
355 |
||
356 |
lemma unset_bit_0 [simp]: |
|
357 |
\<open>unset_bit 0 a = 2 * (a div 2)\<close> |
|
358 |
proof (rule bit_eqI) |
|
359 |
fix m |
|
360 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
361 |
then show \<open>bit (unset_bit 0 a) m = bit (2 * (a div 2)) m\<close> |
|
362 |
by (simp add: bit_unset_bit_iff bit_double_iff) |
|
71535
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71442
diff
changeset
|
363 |
(cases m, simp_all add: bit_Suc) |
71426 | 364 |
qed |
365 |
||
71821 | 366 |
lemma unset_bit_Suc: |
71426 | 367 |
\<open>unset_bit (Suc n) a = a mod 2 + 2 * unset_bit n (a div 2)\<close> |
368 |
proof (rule bit_eqI) |
|
369 |
fix m |
|
370 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
371 |
then show \<open>bit (unset_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * unset_bit n (a div 2)) m\<close> |
|
372 |
proof (cases m) |
|
373 |
case 0 |
|
374 |
then show ?thesis |
|
375 |
by (simp add: even_unset_bit_iff) |
|
376 |
next |
|
377 |
case (Suc m) |
|
378 |
show ?thesis |
|
379 |
by (cases a rule: parity_cases) |
|
380 |
(simp_all add: bit_unset_bit_iff bit_double_iff even_bit_succ_iff *, |
|
71535
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71442
diff
changeset
|
381 |
simp_all add: Suc bit_Suc) |
71426 | 382 |
qed |
383 |
qed |
|
384 |
||
385 |
lemma flip_bit_0 [simp]: |
|
386 |
\<open>flip_bit 0 a = of_bool (even a) + 2 * (a div 2)\<close> |
|
387 |
proof (rule bit_eqI) |
|
388 |
fix m |
|
389 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
390 |
then show \<open>bit (flip_bit 0 a) m = bit (of_bool (even a) + 2 * (a div 2)) m\<close> |
|
391 |
by (simp add: bit_flip_bit_iff bit_double_iff even_bit_succ_iff) |
|
71535
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71442
diff
changeset
|
392 |
(cases m, simp_all add: bit_Suc) |
71426 | 393 |
qed |
394 |
||
71821 | 395 |
lemma flip_bit_Suc: |
71426 | 396 |
\<open>flip_bit (Suc n) a = a mod 2 + 2 * flip_bit n (a div 2)\<close> |
397 |
proof (rule bit_eqI) |
|
398 |
fix m |
|
399 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
400 |
show \<open>bit (flip_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * flip_bit n (a div 2)) m\<close> |
|
401 |
proof (cases m) |
|
402 |
case 0 |
|
403 |
then show ?thesis |
|
404 |
by (simp add: even_flip_bit_iff) |
|
405 |
next |
|
406 |
case (Suc m) |
|
407 |
with * have \<open>2 ^ m \<noteq> 0\<close> |
|
408 |
using mult_2 by auto |
|
409 |
show ?thesis |
|
410 |
by (cases a rule: parity_cases) |
|
411 |
(simp_all add: bit_flip_bit_iff bit_double_iff even_bit_succ_iff, |
|
71535
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71442
diff
changeset
|
412 |
simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close> bit_Suc) |
71426 | 413 |
qed |
414 |
qed |
|
415 |
||
72009 | 416 |
lemma flip_bit_eq_if: |
417 |
\<open>flip_bit n a = (if bit a n then unset_bit else set_bit) n a\<close> |
|
418 |
by (rule bit_eqI) (auto simp add: bit_set_bit_iff bit_unset_bit_iff bit_flip_bit_iff) |
|
419 |
||
71986 | 420 |
lemma take_bit_set_bit_eq: |
72009 | 421 |
\<open>take_bit n (set_bit m a) = (if n \<le> m then take_bit n a else set_bit m (take_bit n a))\<close> |
71986 | 422 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_set_bit_iff) |
423 |
||
424 |
lemma take_bit_unset_bit_eq: |
|
72009 | 425 |
\<open>take_bit n (unset_bit m a) = (if n \<le> m then take_bit n a else unset_bit m (take_bit n a))\<close> |
71986 | 426 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_unset_bit_iff) |
427 |
||
428 |
lemma take_bit_flip_bit_eq: |
|
72009 | 429 |
\<open>take_bit n (flip_bit m a) = (if n \<le> m then take_bit n a else flip_bit m (take_bit n a))\<close> |
71986 | 430 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_flip_bit_iff) |
431 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
432 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
433 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
434 |
|
71956 | 435 |
subsection \<open>Instance \<^typ>\<open>int\<close>\<close> |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
436 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
437 |
instantiation int :: ring_bit_operations |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
438 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
439 |
|
71420 | 440 |
definition not_int :: \<open>int \<Rightarrow> int\<close> |
441 |
where \<open>not_int k = - k - 1\<close> |
|
442 |
||
443 |
lemma not_int_rec: |
|
444 |
"NOT k = of_bool (even k) + 2 * NOT (k div 2)" for k :: int |
|
445 |
by (auto simp add: not_int_def elim: oddE) |
|
446 |
||
447 |
lemma even_not_iff_int: |
|
448 |
\<open>even (NOT k) \<longleftrightarrow> odd k\<close> for k :: int |
|
449 |
by (simp add: not_int_def) |
|
450 |
||
451 |
lemma not_int_div_2: |
|
452 |
\<open>NOT k div 2 = NOT (k div 2)\<close> for k :: int |
|
453 |
by (simp add: not_int_def) |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
454 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
455 |
lemma bit_not_int_iff: |
71186 | 456 |
\<open>bit (NOT k) n \<longleftrightarrow> \<not> bit k n\<close> |
457 |
for k :: int |
|
71535
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71442
diff
changeset
|
458 |
by (induction n arbitrary: k) (simp_all add: not_int_div_2 even_not_iff_int bit_Suc) |
71186 | 459 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
460 |
function and_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
461 |
where \<open>(k::int) AND l = (if k \<in> {0, - 1} \<and> l \<in> {0, - 1} |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
462 |
then - of_bool (odd k \<and> odd l) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
463 |
else of_bool (odd k \<and> odd l) + 2 * ((k div 2) AND (l div 2)))\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
464 |
by auto |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
465 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
466 |
termination |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
467 |
by (relation \<open>measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>))\<close>) auto |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
468 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
469 |
declare and_int.simps [simp del] |
71802 | 470 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
471 |
lemma and_int_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
472 |
\<open>k AND l = of_bool (odd k \<and> odd l) + 2 * ((k div 2) AND (l div 2))\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
473 |
for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
474 |
proof (cases \<open>k \<in> {0, - 1} \<and> l \<in> {0, - 1}\<close>) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
475 |
case True |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
476 |
then show ?thesis |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
477 |
by auto (simp_all add: and_int.simps) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
478 |
next |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
479 |
case False |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
480 |
then show ?thesis |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
481 |
by (auto simp add: ac_simps and_int.simps [of k l]) |
71802 | 482 |
qed |
483 |
||
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
484 |
lemma bit_and_int_iff: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
485 |
\<open>bit (k AND l) n \<longleftrightarrow> bit k n \<and> bit l n\<close> for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
486 |
proof (induction n arbitrary: k l) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
487 |
case 0 |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
488 |
then show ?case |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
489 |
by (simp add: and_int_rec [of k l]) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
490 |
next |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
491 |
case (Suc n) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
492 |
then show ?case |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
493 |
by (simp add: and_int_rec [of k l] bit_Suc) |
71802 | 494 |
qed |
495 |
||
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
496 |
lemma even_and_iff_int: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
497 |
\<open>even (k AND l) \<longleftrightarrow> even k \<or> even l\<close> for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
498 |
using bit_and_int_iff [of k l 0] by auto |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
499 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
500 |
definition or_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
501 |
where \<open>k OR l = NOT (NOT k AND NOT l)\<close> for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
502 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
503 |
lemma or_int_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
504 |
\<open>k OR l = of_bool (odd k \<or> odd l) + 2 * ((k div 2) OR (l div 2))\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
505 |
for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
506 |
using and_int_rec [of \<open>NOT k\<close> \<open>NOT l\<close>] |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
507 |
by (simp add: or_int_def even_not_iff_int not_int_div_2) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
508 |
(simp add: not_int_def) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
509 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
510 |
lemma bit_or_int_iff: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
511 |
\<open>bit (k OR l) n \<longleftrightarrow> bit k n \<or> bit l n\<close> for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
512 |
by (simp add: or_int_def bit_not_int_iff bit_and_int_iff) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
513 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
514 |
definition xor_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
515 |
where \<open>k XOR l = k AND NOT l OR NOT k AND l\<close> for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
516 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
517 |
lemma xor_int_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
518 |
\<open>k XOR l = of_bool (odd k \<noteq> odd l) + 2 * ((k div 2) XOR (l div 2))\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
519 |
for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
520 |
by (simp add: xor_int_def or_int_rec [of \<open>k AND NOT l\<close> \<open>NOT k AND l\<close>] even_and_iff_int even_not_iff_int) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
521 |
(simp add: and_int_rec [of \<open>NOT k\<close> \<open>l\<close>] and_int_rec [of \<open>k\<close> \<open>NOT l\<close>] not_int_div_2) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
522 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
523 |
lemma bit_xor_int_iff: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
524 |
\<open>bit (k XOR l) n \<longleftrightarrow> bit k n \<noteq> bit l n\<close> for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
525 |
by (auto simp add: xor_int_def bit_or_int_iff bit_and_int_iff bit_not_int_iff) |
71802 | 526 |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
527 |
instance proof |
71186 | 528 |
fix k l :: int and n :: nat |
71409 | 529 |
show \<open>- k = NOT (k - 1)\<close> |
530 |
by (simp add: not_int_def) |
|
71186 | 531 |
show \<open>bit (k AND l) n \<longleftrightarrow> bit k n \<and> bit l n\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
532 |
by (fact bit_and_int_iff) |
71186 | 533 |
show \<open>bit (k OR l) n \<longleftrightarrow> bit k n \<or> bit l n\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
534 |
by (fact bit_or_int_iff) |
71186 | 535 |
show \<open>bit (k XOR l) n \<longleftrightarrow> bit k n \<noteq> bit l n\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
536 |
by (fact bit_xor_int_iff) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
537 |
qed (simp_all add: bit_not_int_iff) |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
538 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
539 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
540 |
|
72009 | 541 |
lemma disjunctive_add: |
542 |
\<open>k + l = k OR l\<close> if \<open>\<And>n. \<not> bit k n \<or> \<not> bit l n\<close> for k l :: int |
|
543 |
\<comment> \<open>TODO: may integrate (indirectly) into \<^class>\<open>semiring_bits\<close> premises\<close> |
|
544 |
proof (rule bit_eqI) |
|
545 |
fix n |
|
546 |
from that have \<open>bit (k + l) n \<longleftrightarrow> bit k n \<or> bit l n\<close> |
|
547 |
proof (induction n arbitrary: k l) |
|
548 |
case 0 |
|
549 |
from this [of 0] show ?case |
|
550 |
by auto |
|
551 |
next |
|
552 |
case (Suc n) |
|
553 |
have \<open>bit ((k + l) div 2) n \<longleftrightarrow> bit (k div 2 + l div 2) n\<close> |
|
554 |
using Suc.prems [of 0] div_add1_eq [of k l] by auto |
|
555 |
also have \<open>bit (k div 2 + l div 2) n \<longleftrightarrow> bit (k div 2) n \<or> bit (l div 2) n\<close> |
|
556 |
by (rule Suc.IH) (use Suc.prems in \<open>simp flip: bit_Suc\<close>) |
|
557 |
finally show ?case |
|
558 |
by (simp add: bit_Suc) |
|
559 |
qed |
|
560 |
also have \<open>\<dots> \<longleftrightarrow> bit (k OR l) n\<close> |
|
561 |
by (simp add: bit_or_iff) |
|
562 |
finally show \<open>bit (k + l) n \<longleftrightarrow> bit (k OR l) n\<close> . |
|
563 |
qed |
|
564 |
||
565 |
lemma disjunctive_diff: |
|
566 |
\<open>k - l = k AND NOT l\<close> if \<open>\<And>n. bit l n \<Longrightarrow> bit k n\<close> for k l :: int |
|
567 |
proof - |
|
568 |
have \<open>NOT k + l = NOT k OR l\<close> |
|
569 |
by (rule disjunctive_add) (auto simp add: bit_not_iff dest: that) |
|
570 |
then have \<open>NOT (NOT k + l) = NOT (NOT k OR l)\<close> |
|
571 |
by simp |
|
572 |
then show ?thesis |
|
573 |
by (simp add: not_add_distrib) |
|
574 |
qed |
|
575 |
||
71802 | 576 |
lemma not_nonnegative_int_iff [simp]: |
577 |
\<open>NOT k \<ge> 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
578 |
by (simp add: not_int_def) |
|
579 |
||
580 |
lemma not_negative_int_iff [simp]: |
|
581 |
\<open>NOT k < 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
582 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less not_le) |
|
583 |
||
584 |
lemma and_nonnegative_int_iff [simp]: |
|
585 |
\<open>k AND l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<or> l \<ge> 0\<close> for k l :: int |
|
586 |
proof (induction k arbitrary: l rule: int_bit_induct) |
|
587 |
case zero |
|
588 |
then show ?case |
|
589 |
by simp |
|
590 |
next |
|
591 |
case minus |
|
592 |
then show ?case |
|
593 |
by simp |
|
594 |
next |
|
595 |
case (even k) |
|
596 |
then show ?case |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
597 |
using and_int_rec [of \<open>k * 2\<close> l] by (simp add: pos_imp_zdiv_nonneg_iff) |
71802 | 598 |
next |
599 |
case (odd k) |
|
600 |
from odd have \<open>0 \<le> k AND l div 2 \<longleftrightarrow> 0 \<le> k \<or> 0 \<le> l div 2\<close> |
|
601 |
by simp |
|
602 |
then have \<open>0 \<le> (1 + k * 2) div 2 AND l div 2 \<longleftrightarrow> 0 \<le> (1 + k * 2) div 2\<or> 0 \<le> l div 2\<close> |
|
603 |
by simp |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
604 |
with and_int_rec [of \<open>1 + k * 2\<close> l] |
71802 | 605 |
show ?case |
606 |
by auto |
|
607 |
qed |
|
608 |
||
609 |
lemma and_negative_int_iff [simp]: |
|
610 |
\<open>k AND l < 0 \<longleftrightarrow> k < 0 \<and> l < 0\<close> for k l :: int |
|
611 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
612 |
||
72009 | 613 |
lemma and_less_eq: |
614 |
\<open>k AND l \<le> k\<close> if \<open>l < 0\<close> for k l :: int |
|
615 |
using that proof (induction k arbitrary: l rule: int_bit_induct) |
|
616 |
case zero |
|
617 |
then show ?case |
|
618 |
by simp |
|
619 |
next |
|
620 |
case minus |
|
621 |
then show ?case |
|
622 |
by simp |
|
623 |
next |
|
624 |
case (even k) |
|
625 |
from even.IH [of \<open>l div 2\<close>] even.hyps even.prems |
|
626 |
show ?case |
|
627 |
by (simp add: and_int_rec [of _ l]) |
|
628 |
next |
|
629 |
case (odd k) |
|
630 |
from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems |
|
631 |
show ?case |
|
632 |
by (simp add: and_int_rec [of _ l]) |
|
633 |
qed |
|
634 |
||
71802 | 635 |
lemma or_nonnegative_int_iff [simp]: |
636 |
\<open>k OR l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<and> l \<ge> 0\<close> for k l :: int |
|
637 |
by (simp only: or_eq_not_not_and not_nonnegative_int_iff) simp |
|
638 |
||
639 |
lemma or_negative_int_iff [simp]: |
|
640 |
\<open>k OR l < 0 \<longleftrightarrow> k < 0 \<or> l < 0\<close> for k l :: int |
|
641 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
642 |
||
72009 | 643 |
lemma or_greater_eq: |
644 |
\<open>k OR l \<ge> k\<close> if \<open>l \<ge> 0\<close> for k l :: int |
|
645 |
using that proof (induction k arbitrary: l rule: int_bit_induct) |
|
646 |
case zero |
|
647 |
then show ?case |
|
648 |
by simp |
|
649 |
next |
|
650 |
case minus |
|
651 |
then show ?case |
|
652 |
by simp |
|
653 |
next |
|
654 |
case (even k) |
|
655 |
from even.IH [of \<open>l div 2\<close>] even.hyps even.prems |
|
656 |
show ?case |
|
657 |
by (simp add: or_int_rec [of _ l]) |
|
658 |
next |
|
659 |
case (odd k) |
|
660 |
from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems |
|
661 |
show ?case |
|
662 |
by (simp add: or_int_rec [of _ l]) |
|
663 |
qed |
|
664 |
||
71802 | 665 |
lemma xor_nonnegative_int_iff [simp]: |
666 |
\<open>k XOR l \<ge> 0 \<longleftrightarrow> (k \<ge> 0 \<longleftrightarrow> l \<ge> 0)\<close> for k l :: int |
|
667 |
by (simp only: bit.xor_def or_nonnegative_int_iff) auto |
|
668 |
||
669 |
lemma xor_negative_int_iff [simp]: |
|
670 |
\<open>k XOR l < 0 \<longleftrightarrow> (k < 0) \<noteq> (l < 0)\<close> for k l :: int |
|
671 |
by (subst Not_eq_iff [symmetric]) (auto simp add: not_less) |
|
672 |
||
72010 | 673 |
lemma mask_nonnegative: |
674 |
\<open>(mask n :: int) \<ge> 0\<close> |
|
675 |
by (simp add: mask_eq_exp_minus_1) |
|
676 |
||
71802 | 677 |
lemma set_bit_nonnegative_int_iff [simp]: |
678 |
\<open>set_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
679 |
by (simp add: set_bit_def) |
|
680 |
||
681 |
lemma set_bit_negative_int_iff [simp]: |
|
682 |
\<open>set_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
683 |
by (simp add: set_bit_def) |
|
684 |
||
685 |
lemma unset_bit_nonnegative_int_iff [simp]: |
|
686 |
\<open>unset_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
687 |
by (simp add: unset_bit_def) |
|
688 |
||
689 |
lemma unset_bit_negative_int_iff [simp]: |
|
690 |
\<open>unset_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
691 |
by (simp add: unset_bit_def) |
|
692 |
||
693 |
lemma flip_bit_nonnegative_int_iff [simp]: |
|
694 |
\<open>flip_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
695 |
by (simp add: flip_bit_def) |
|
696 |
||
697 |
lemma flip_bit_negative_int_iff [simp]: |
|
698 |
\<open>flip_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
699 |
by (simp add: flip_bit_def) |
|
700 |
||
71986 | 701 |
lemma set_bit_greater_eq: |
702 |
\<open>set_bit n k \<ge> k\<close> for k :: int |
|
703 |
by (simp add: set_bit_def or_greater_eq) |
|
704 |
||
705 |
lemma unset_bit_less_eq: |
|
706 |
\<open>unset_bit n k \<le> k\<close> for k :: int |
|
707 |
by (simp add: unset_bit_def and_less_eq) |
|
708 |
||
72009 | 709 |
lemma set_bit_eq: |
710 |
\<open>set_bit n k = k + of_bool (\<not> bit k n) * 2 ^ n\<close> for k :: int |
|
711 |
proof (rule bit_eqI) |
|
712 |
fix m |
|
713 |
show \<open>bit (set_bit n k) m \<longleftrightarrow> bit (k + of_bool (\<not> bit k n) * 2 ^ n) m\<close> |
|
714 |
proof (cases \<open>m = n\<close>) |
|
715 |
case True |
|
716 |
then show ?thesis |
|
717 |
apply (simp add: bit_set_bit_iff) |
|
718 |
apply (simp add: bit_iff_odd div_plus_div_distrib_dvd_right) |
|
719 |
done |
|
720 |
next |
|
721 |
case False |
|
722 |
then show ?thesis |
|
723 |
apply (clarsimp simp add: bit_set_bit_iff) |
|
724 |
apply (subst disjunctive_add) |
|
725 |
apply (clarsimp simp add: bit_exp_iff) |
|
726 |
apply (clarsimp simp add: bit_or_iff bit_exp_iff) |
|
727 |
done |
|
728 |
qed |
|
729 |
qed |
|
730 |
||
731 |
lemma unset_bit_eq: |
|
732 |
\<open>unset_bit n k = k - of_bool (bit k n) * 2 ^ n\<close> for k :: int |
|
733 |
proof (rule bit_eqI) |
|
734 |
fix m |
|
735 |
show \<open>bit (unset_bit n k) m \<longleftrightarrow> bit (k - of_bool (bit k n) * 2 ^ n) m\<close> |
|
736 |
proof (cases \<open>m = n\<close>) |
|
737 |
case True |
|
738 |
then show ?thesis |
|
739 |
apply (simp add: bit_unset_bit_iff) |
|
740 |
apply (simp add: bit_iff_odd) |
|
741 |
using div_plus_div_distrib_dvd_right [of \<open>2 ^ n\<close> \<open>- (2 ^ n)\<close> k] |
|
742 |
apply (simp add: dvd_neg_div) |
|
743 |
done |
|
744 |
next |
|
745 |
case False |
|
746 |
then show ?thesis |
|
747 |
apply (clarsimp simp add: bit_unset_bit_iff) |
|
748 |
apply (subst disjunctive_diff) |
|
749 |
apply (clarsimp simp add: bit_exp_iff) |
|
750 |
apply (clarsimp simp add: bit_and_iff bit_not_iff bit_exp_iff) |
|
751 |
done |
|
752 |
qed |
|
753 |
qed |
|
754 |
||
71442 | 755 |
|
72010 | 756 |
subsection \<open>Taking bit with sign propagation\<close> |
757 |
||
758 |
definition signed_take_bit :: "nat \<Rightarrow> int \<Rightarrow> int" |
|
759 |
where \<open>signed_take_bit n k = take_bit n k OR (NOT (mask n) * of_bool (bit k n))\<close> |
|
760 |
||
761 |
lemma signed_take_bit_eq: |
|
762 |
\<open>signed_take_bit n k = take_bit n k\<close> if \<open>\<not> bit k n\<close> |
|
763 |
using that by (simp add: signed_take_bit_def) |
|
764 |
||
765 |
lemma signed_take_bit_eq_or: |
|
766 |
\<open>signed_take_bit n k = take_bit n k OR NOT (mask n)\<close> if \<open>bit k n\<close> |
|
767 |
using that by (simp add: signed_take_bit_def) |
|
768 |
||
769 |
lemma signed_take_bit_0 [simp]: |
|
770 |
\<open>signed_take_bit 0 k = - (k mod 2)\<close> |
|
771 |
by (simp add: signed_take_bit_def odd_iff_mod_2_eq_one) |
|
772 |
||
773 |
lemma mask_half_int: |
|
774 |
\<open>mask n div 2 = (mask (n - 1) :: int)\<close> |
|
775 |
by (cases n) (simp_all add: mask_eq_exp_minus_1 algebra_simps) |
|
776 |
||
777 |
lemma signed_take_bit_Suc: |
|
778 |
\<open>signed_take_bit (Suc n) k = k mod 2 + 2 * signed_take_bit n (k div 2)\<close> |
|
779 |
by (unfold signed_take_bit_def or_int_rec [of \<open>take_bit (Suc n) k\<close>]) |
|
780 |
(simp add: bit_Suc take_bit_Suc even_or_iff even_mask_iff odd_iff_mod_2_eq_one not_int_div_2 mask_half_int) |
|
781 |
||
782 |
lemma signed_take_bit_rec: |
|
783 |
\<open>signed_take_bit n k = (if n = 0 then - (k mod 2) else k mod 2 + 2 * signed_take_bit (n - 1) (k div 2))\<close> |
|
784 |
by (cases n) (simp_all add: signed_take_bit_Suc) |
|
785 |
||
786 |
lemma bit_signed_take_bit_iff: |
|
787 |
\<open>bit (signed_take_bit m k) n = bit k (min m n)\<close> |
|
788 |
by (simp add: signed_take_bit_def bit_or_iff bit_take_bit_iff bit_not_iff bit_mask_iff min_def) |
|
789 |
||
790 |
text \<open>Modulus centered around 0\<close> |
|
791 |
||
792 |
lemma signed_take_bit_eq_take_bit_minus: |
|
793 |
\<open>signed_take_bit n k = take_bit (Suc n) k - 2 ^ Suc n * of_bool (bit k n)\<close> |
|
794 |
proof (cases \<open>bit k n\<close>) |
|
795 |
case True |
|
796 |
have \<open>signed_take_bit n k = take_bit (Suc n) k OR NOT (mask (Suc n))\<close> |
|
797 |
by (rule bit_eqI) (auto simp add: bit_signed_take_bit_iff min_def bit_take_bit_iff bit_or_iff bit_not_iff bit_mask_iff less_Suc_eq True) |
|
798 |
then have \<open>signed_take_bit n k = take_bit (Suc n) k + NOT (mask (Suc n))\<close> |
|
799 |
by (simp add: disjunctive_add bit_take_bit_iff bit_not_iff bit_mask_iff) |
|
800 |
with True show ?thesis |
|
801 |
by (simp flip: minus_exp_eq_not_mask) |
|
802 |
next |
|
803 |
case False |
|
804 |
then show ?thesis |
|
805 |
by (simp add: bit_eq_iff bit_take_bit_iff bit_signed_take_bit_iff min_def) |
|
806 |
(auto intro: bit_eqI simp add: less_Suc_eq) |
|
807 |
qed |
|
808 |
||
809 |
lemma signed_take_bit_eq_take_bit_shift: |
|
810 |
\<open>signed_take_bit n k = take_bit (Suc n) (k + 2 ^ n) - 2 ^ n\<close> |
|
811 |
proof - |
|
812 |
have *: \<open>take_bit n k OR 2 ^ n = take_bit n k + 2 ^ n\<close> |
|
813 |
by (simp add: disjunctive_add bit_exp_iff bit_take_bit_iff) |
|
814 |
have \<open>take_bit n k - 2 ^ n = take_bit n k + NOT (mask n)\<close> |
|
815 |
by (simp add: minus_exp_eq_not_mask) |
|
816 |
also have \<open>\<dots> = take_bit n k OR NOT (mask n)\<close> |
|
817 |
by (rule disjunctive_add) |
|
818 |
(simp add: bit_exp_iff bit_take_bit_iff bit_not_iff bit_mask_iff) |
|
819 |
finally have **: \<open>take_bit n k - 2 ^ n = take_bit n k OR NOT (mask n)\<close> . |
|
820 |
have \<open>take_bit (Suc n) (k + 2 ^ n) = take_bit (Suc n) (take_bit (Suc n) k + take_bit (Suc n) (2 ^ n))\<close> |
|
821 |
by (simp only: take_bit_add) |
|
822 |
also have \<open>take_bit (Suc n) k = 2 ^ n * of_bool (bit k n) + take_bit n k\<close> |
|
823 |
by (simp add: take_bit_Suc_from_most) |
|
824 |
finally have \<open>take_bit (Suc n) (k + 2 ^ n) = take_bit (Suc n) (2 ^ (n + of_bool (bit k n)) + take_bit n k)\<close> |
|
825 |
by (simp add: ac_simps) |
|
826 |
also have \<open>2 ^ (n + of_bool (bit k n)) + take_bit n k = 2 ^ (n + of_bool (bit k n)) OR take_bit n k\<close> |
|
827 |
by (rule disjunctive_add) |
|
828 |
(auto simp add: disjunctive_add bit_take_bit_iff bit_double_iff bit_exp_iff) |
|
829 |
finally show ?thesis |
|
830 |
using * ** by (simp add: signed_take_bit_def take_bit_Suc min_def ac_simps) |
|
831 |
qed |
|
832 |
||
833 |
lemma signed_take_bit_of_0 [simp]: |
|
834 |
\<open>signed_take_bit n 0 = 0\<close> |
|
835 |
by (simp add: signed_take_bit_def) |
|
836 |
||
837 |
lemma signed_take_bit_of_minus_1 [simp]: |
|
838 |
\<open>signed_take_bit n (- 1) = - 1\<close> |
|
839 |
by (simp add: signed_take_bit_def take_bit_minus_one_eq_mask) |
|
840 |
||
841 |
lemma signed_take_bit_eq_iff_take_bit_eq: |
|
842 |
\<open>signed_take_bit n k = signed_take_bit n l \<longleftrightarrow> take_bit (Suc n) k = take_bit (Suc n) l\<close> |
|
843 |
proof (cases \<open>bit k n \<longleftrightarrow> bit l n\<close>) |
|
844 |
case True |
|
845 |
moreover have \<open>take_bit n k OR NOT (mask n) = take_bit n k - 2 ^ n\<close> |
|
846 |
for k :: int |
|
847 |
by (auto simp add: disjunctive_add [symmetric] bit_not_iff bit_mask_iff bit_take_bit_iff minus_exp_eq_not_mask) |
|
848 |
ultimately show ?thesis |
|
849 |
by (simp add: signed_take_bit_def take_bit_Suc_from_most) |
|
850 |
next |
|
851 |
case False |
|
852 |
then have \<open>signed_take_bit n k \<noteq> signed_take_bit n l\<close> and \<open>take_bit (Suc n) k \<noteq> take_bit (Suc n) l\<close> |
|
853 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_signed_take_bit_iff min_def) |
|
854 |
then show ?thesis |
|
855 |
by simp |
|
856 |
qed |
|
857 |
||
858 |
lemma signed_take_bit_signed_take_bit [simp]: |
|
859 |
\<open>signed_take_bit m (signed_take_bit n k) = signed_take_bit (min m n) k\<close> |
|
860 |
by (rule bit_eqI) (auto simp add: bit_signed_take_bit_iff min_def bit_or_iff bit_not_iff bit_mask_iff bit_take_bit_iff) |
|
861 |
||
862 |
lemma take_bit_signed_take_bit: |
|
863 |
\<open>take_bit m (signed_take_bit n k) = take_bit m k\<close> if \<open>m \<le> n\<close> |
|
864 |
using that by (auto simp add: bit_eq_iff bit_take_bit_iff bit_signed_take_bit_iff min_def) |
|
865 |
||
866 |
lemma take_bit_Suc_signed_take_bit [simp]: |
|
867 |
\<open>take_bit (Suc n) (signed_take_bit n a) = take_bit (Suc n) a\<close> |
|
868 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_signed_take_bit_iff min_def less_Suc_eq) |
|
869 |
||
870 |
lemma signed_take_bit_take_bit: |
|
871 |
\<open>signed_take_bit m (take_bit n k) = (if n \<le> m then take_bit n else signed_take_bit m) k\<close> |
|
872 |
by (rule bit_eqI) (auto simp add: bit_signed_take_bit_iff min_def bit_take_bit_iff) |
|
873 |
||
874 |
lemma signed_take_bit_nonnegative_iff [simp]: |
|
875 |
\<open>0 \<le> signed_take_bit n k \<longleftrightarrow> \<not> bit k n\<close> |
|
876 |
by (simp add: signed_take_bit_def not_less mask_nonnegative) |
|
877 |
||
878 |
lemma signed_take_bit_negative_iff [simp]: |
|
879 |
\<open>signed_take_bit n k < 0 \<longleftrightarrow> bit k n\<close> |
|
880 |
by (simp add: signed_take_bit_def not_less mask_nonnegative) |
|
881 |
||
882 |
lemma signed_take_bit_greater_eq: |
|
883 |
\<open>k + 2 ^ Suc n \<le> signed_take_bit n k\<close> if \<open>k < - (2 ^ n)\<close> |
|
884 |
using that take_bit_greater_eq [of \<open>k + 2 ^ n\<close> \<open>Suc n\<close>] |
|
885 |
by (simp add: signed_take_bit_eq_take_bit_shift) |
|
886 |
||
887 |
lemma signed_take_bit_less_eq: |
|
888 |
\<open>signed_take_bit n k \<le> k - 2 ^ Suc n\<close> if \<open>k \<ge> 2 ^ n\<close> |
|
889 |
using that take_bit_less_eq [of \<open>Suc n\<close> \<open>k + 2 ^ n\<close>] |
|
890 |
by (simp add: signed_take_bit_eq_take_bit_shift) |
|
891 |
||
892 |
lemma signed_take_bit_Suc_1 [simp]: |
|
893 |
\<open>signed_take_bit (Suc n) 1 = 1\<close> |
|
894 |
by (simp add: signed_take_bit_Suc) |
|
895 |
||
896 |
lemma signed_take_bit_Suc_bit0 [simp]: |
|
897 |
\<open>signed_take_bit (Suc n) (numeral (Num.Bit0 k)) = signed_take_bit n (numeral k) * 2\<close> |
|
898 |
by (simp add: signed_take_bit_Suc) |
|
899 |
||
900 |
lemma signed_take_bit_Suc_bit1 [simp]: |
|
901 |
\<open>signed_take_bit (Suc n) (numeral (Num.Bit1 k)) = signed_take_bit n (numeral k) * 2 + 1\<close> |
|
902 |
by (simp add: signed_take_bit_Suc) |
|
903 |
||
904 |
lemma signed_take_bit_Suc_minus_bit0 [simp]: |
|
905 |
\<open>signed_take_bit (Suc n) (- numeral (Num.Bit0 k)) = signed_take_bit n (- numeral k) * 2\<close> |
|
906 |
by (simp add: signed_take_bit_Suc) |
|
907 |
||
908 |
lemma signed_take_bit_Suc_minus_bit1 [simp]: |
|
909 |
\<open>signed_take_bit (Suc n) (- numeral (Num.Bit1 k)) = signed_take_bit n (- numeral k - 1) * 2 + 1\<close> |
|
910 |
by (simp add: signed_take_bit_Suc) |
|
911 |
||
912 |
lemma signed_take_bit_numeral_bit0 [simp]: |
|
913 |
\<open>signed_take_bit (numeral l) (numeral (Num.Bit0 k)) = signed_take_bit (pred_numeral l) (numeral k) * 2\<close> |
|
914 |
by (simp add: signed_take_bit_rec) |
|
915 |
||
916 |
lemma signed_take_bit_numeral_bit1 [simp]: |
|
917 |
\<open>signed_take_bit (numeral l) (numeral (Num.Bit1 k)) = signed_take_bit (pred_numeral l) (numeral k) * 2 + 1\<close> |
|
918 |
by (simp add: signed_take_bit_rec) |
|
919 |
||
920 |
lemma signed_take_bit_numeral_minus_bit0 [simp]: |
|
921 |
\<open>signed_take_bit (numeral l) (- numeral (Num.Bit0 k)) = signed_take_bit (pred_numeral l) (- numeral k) * 2\<close> |
|
922 |
by (simp add: signed_take_bit_rec) |
|
923 |
||
924 |
lemma signed_take_bit_numeral_minus_bit1 [simp]: |
|
925 |
\<open>signed_take_bit (numeral l) (- numeral (Num.Bit1 k)) = signed_take_bit (pred_numeral l) (- numeral k - 1) * 2 + 1\<close> |
|
926 |
by (simp add: signed_take_bit_rec) |
|
927 |
||
928 |
lemma signed_take_bit_code [code]: |
|
929 |
\<open>signed_take_bit n k = |
|
930 |
(let l = k AND mask (Suc n) |
|
931 |
in if bit l n then l - (push_bit n 2) else l)\<close> |
|
932 |
proof - |
|
933 |
have *: \<open>(k AND mask (Suc n)) - 2 * 2 ^ n = k AND mask (Suc n) OR NOT (mask (Suc n))\<close> |
|
934 |
apply (subst disjunctive_add [symmetric]) |
|
935 |
apply (simp_all add: bit_and_iff bit_mask_iff bit_not_iff) |
|
936 |
apply (simp flip: minus_exp_eq_not_mask) |
|
937 |
done |
|
938 |
show ?thesis |
|
939 |
by (rule bit_eqI) |
|
940 |
(auto simp add: Let_def bit_and_iff bit_signed_take_bit_iff push_bit_eq_mult min_def not_le bit_mask_iff bit_exp_iff less_Suc_eq * bit_or_iff bit_not_iff) |
|
941 |
qed |
|
942 |
||
943 |
||
71956 | 944 |
subsection \<open>Instance \<^typ>\<open>nat\<close>\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
945 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
946 |
instantiation nat :: semiring_bit_operations |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
947 |
begin |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
948 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
949 |
definition and_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
950 |
where \<open>m AND n = nat (int m AND int n)\<close> for m n :: nat |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
951 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
952 |
definition or_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
953 |
where \<open>m OR n = nat (int m OR int n)\<close> for m n :: nat |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
954 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
955 |
definition xor_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
956 |
where \<open>m XOR n = nat (int m XOR int n)\<close> for m n :: nat |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
957 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
958 |
instance proof |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
959 |
fix m n q :: nat |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
960 |
show \<open>bit (m AND n) q \<longleftrightarrow> bit m q \<and> bit n q\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
961 |
by (auto simp add: and_nat_def bit_and_iff less_le bit_eq_iff) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
962 |
show \<open>bit (m OR n) q \<longleftrightarrow> bit m q \<or> bit n q\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
963 |
by (auto simp add: or_nat_def bit_or_iff less_le bit_eq_iff) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
964 |
show \<open>bit (m XOR n) q \<longleftrightarrow> bit m q \<noteq> bit n q\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
965 |
by (auto simp add: xor_nat_def bit_xor_iff less_le bit_eq_iff) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
966 |
qed |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
967 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
968 |
end |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
969 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
970 |
lemma and_nat_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
971 |
\<open>m AND n = of_bool (odd m \<and> odd n) + 2 * ((m div 2) AND (n div 2))\<close> for m n :: nat |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
972 |
by (simp add: and_nat_def and_int_rec [of \<open>int m\<close> \<open>int n\<close>] zdiv_int nat_add_distrib nat_mult_distrib) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
973 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
974 |
lemma or_nat_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
975 |
\<open>m OR n = of_bool (odd m \<or> odd n) + 2 * ((m div 2) OR (n div 2))\<close> for m n :: nat |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
976 |
by (simp add: or_nat_def or_int_rec [of \<open>int m\<close> \<open>int n\<close>] zdiv_int nat_add_distrib nat_mult_distrib) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
977 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
978 |
lemma xor_nat_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
979 |
\<open>m XOR n = of_bool (odd m \<noteq> odd n) + 2 * ((m div 2) XOR (n div 2))\<close> for m n :: nat |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
980 |
by (simp add: xor_nat_def xor_int_rec [of \<open>int m\<close> \<open>int n\<close>] zdiv_int nat_add_distrib nat_mult_distrib) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
981 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
982 |
lemma Suc_0_and_eq [simp]: |
71822 | 983 |
\<open>Suc 0 AND n = n mod 2\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
984 |
using one_and_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
985 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
986 |
lemma and_Suc_0_eq [simp]: |
71822 | 987 |
\<open>n AND Suc 0 = n mod 2\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
988 |
using and_one_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
989 |
|
71822 | 990 |
lemma Suc_0_or_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
991 |
\<open>Suc 0 OR n = n + of_bool (even n)\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
992 |
using one_or_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
993 |
|
71822 | 994 |
lemma or_Suc_0_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
995 |
\<open>n OR Suc 0 = n + of_bool (even n)\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
996 |
using or_one_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
997 |
|
71822 | 998 |
lemma Suc_0_xor_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
999 |
\<open>Suc 0 XOR n = n + of_bool (even n) - of_bool (odd n)\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1000 |
using one_xor_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1001 |
|
71822 | 1002 |
lemma xor_Suc_0_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1003 |
\<open>n XOR Suc 0 = n + of_bool (even n) - of_bool (odd n)\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1004 |
using xor_one_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1005 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1006 |
|
71956 | 1007 |
subsection \<open>Instances for \<^typ>\<open>integer\<close> and \<^typ>\<open>natural\<close>\<close> |
71442 | 1008 |
|
1009 |
unbundle integer.lifting natural.lifting |
|
1010 |
||
1011 |
instantiation integer :: ring_bit_operations |
|
1012 |
begin |
|
1013 |
||
1014 |
lift_definition not_integer :: \<open>integer \<Rightarrow> integer\<close> |
|
1015 |
is not . |
|
1016 |
||
1017 |
lift_definition and_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close> |
|
1018 |
is \<open>and\<close> . |
|
1019 |
||
1020 |
lift_definition or_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close> |
|
1021 |
is or . |
|
1022 |
||
1023 |
lift_definition xor_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close> |
|
1024 |
is xor . |
|
1025 |
||
1026 |
instance proof |
|
1027 |
fix k l :: \<open>integer\<close> and n :: nat |
|
1028 |
show \<open>- k = NOT (k - 1)\<close> |
|
1029 |
by transfer (simp add: minus_eq_not_minus_1) |
|
1030 |
show \<open>bit (NOT k) n \<longleftrightarrow> (2 :: integer) ^ n \<noteq> 0 \<and> \<not> bit k n\<close> |
|
1031 |
by transfer (fact bit_not_iff) |
|
1032 |
show \<open>bit (k AND l) n \<longleftrightarrow> bit k n \<and> bit l n\<close> |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1033 |
by transfer (fact bit_and_iff) |
71442 | 1034 |
show \<open>bit (k OR l) n \<longleftrightarrow> bit k n \<or> bit l n\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1035 |
by transfer (fact bit_or_iff) |
71442 | 1036 |
show \<open>bit (k XOR l) n \<longleftrightarrow> bit k n \<noteq> bit l n\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1037 |
by transfer (fact bit_xor_iff) |
71442 | 1038 |
qed |
1039 |
||
1040 |
end |
|
1041 |
||
1042 |
instantiation natural :: semiring_bit_operations |
|
1043 |
begin |
|
1044 |
||
1045 |
lift_definition and_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close> |
|
1046 |
is \<open>and\<close> . |
|
1047 |
||
1048 |
lift_definition or_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close> |
|
1049 |
is or . |
|
1050 |
||
1051 |
lift_definition xor_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close> |
|
1052 |
is xor . |
|
1053 |
||
1054 |
instance proof |
|
1055 |
fix m n :: \<open>natural\<close> and q :: nat |
|
1056 |
show \<open>bit (m AND n) q \<longleftrightarrow> bit m q \<and> bit n q\<close> |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1057 |
by transfer (fact bit_and_iff) |
71442 | 1058 |
show \<open>bit (m OR n) q \<longleftrightarrow> bit m q \<or> bit n q\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1059 |
by transfer (fact bit_or_iff) |
71442 | 1060 |
show \<open>bit (m XOR n) q \<longleftrightarrow> bit m q \<noteq> bit n q\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1061 |
by transfer (fact bit_xor_iff) |
71442 | 1062 |
qed |
1063 |
||
1064 |
end |
|
1065 |
||
1066 |
lifting_update integer.lifting |
|
1067 |
lifting_forget integer.lifting |
|
1068 |
||
1069 |
lifting_update natural.lifting |
|
1070 |
lifting_forget natural.lifting |
|
1071 |
||
71800 | 1072 |
|
1073 |
subsection \<open>Key ideas of bit operations\<close> |
|
1074 |
||
1075 |
text \<open> |
|
1076 |
When formalizing bit operations, it is tempting to represent |
|
1077 |
bit values as explicit lists over a binary type. This however |
|
1078 |
is a bad idea, mainly due to the inherent ambiguities in |
|
1079 |
representation concerning repeating leading bits. |
|
1080 |
||
1081 |
Hence this approach avoids such explicit lists altogether |
|
1082 |
following an algebraic path: |
|
1083 |
||
1084 |
\<^item> Bit values are represented by numeric types: idealized |
|
1085 |
unbounded bit values can be represented by type \<^typ>\<open>int\<close>, |
|
1086 |
bounded bit values by quotient types over \<^typ>\<open>int\<close>. |
|
1087 |
||
1088 |
\<^item> (A special case are idealized unbounded bit values ending |
|
1089 |
in @{term [source] 0} which can be represented by type \<^typ>\<open>nat\<close> but |
|
1090 |
only support a restricted set of operations). |
|
1091 |
||
1092 |
\<^item> From this idea follows that |
|
1093 |
||
1094 |
\<^item> multiplication by \<^term>\<open>2 :: int\<close> is a bit shift to the left and |
|
1095 |
||
1096 |
\<^item> division by \<^term>\<open>2 :: int\<close> is a bit shift to the right. |
|
1097 |
||
1098 |
\<^item> Concerning bounded bit values, iterated shifts to the left |
|
1099 |
may result in eliminating all bits by shifting them all |
|
1100 |
beyond the boundary. The property \<^prop>\<open>(2 :: int) ^ n \<noteq> 0\<close> |
|
1101 |
represents that \<^term>\<open>n\<close> is \<^emph>\<open>not\<close> beyond that boundary. |
|
1102 |
||
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71956
diff
changeset
|
1103 |
\<^item> The projection on a single bit is then @{thm bit_iff_odd [where ?'a = int, no_vars]}. |
71800 | 1104 |
|
1105 |
\<^item> This leads to the most fundamental properties of bit values: |
|
1106 |
||
1107 |
\<^item> Equality rule: @{thm bit_eqI [where ?'a = int, no_vars]} |
|
1108 |
||
1109 |
\<^item> Induction rule: @{thm bits_induct [where ?'a = int, no_vars]} |
|
1110 |
||
1111 |
\<^item> Typical operations are characterized as follows: |
|
1112 |
||
1113 |
\<^item> Singleton \<^term>\<open>n\<close>th bit: \<^term>\<open>(2 :: int) ^ n\<close> |
|
1114 |
||
71956 | 1115 |
\<^item> Bit mask upto bit \<^term>\<open>n\<close>: @{thm mask_eq_exp_minus_1 [where ?'a = int, no_vars]} |
71800 | 1116 |
|
1117 |
\<^item> Left shift: @{thm push_bit_eq_mult [where ?'a = int, no_vars]} |
|
1118 |
||
1119 |
\<^item> Right shift: @{thm drop_bit_eq_div [where ?'a = int, no_vars]} |
|
1120 |
||
1121 |
\<^item> Truncation: @{thm take_bit_eq_mod [where ?'a = int, no_vars]} |
|
1122 |
||
1123 |
\<^item> Negation: @{thm bit_not_iff [where ?'a = int, no_vars]} |
|
1124 |
||
1125 |
\<^item> And: @{thm bit_and_iff [where ?'a = int, no_vars]} |
|
1126 |
||
1127 |
\<^item> Or: @{thm bit_or_iff [where ?'a = int, no_vars]} |
|
1128 |
||
1129 |
\<^item> Xor: @{thm bit_xor_iff [where ?'a = int, no_vars]} |
|
1130 |
||
1131 |
\<^item> Set a single bit: @{thm set_bit_def [where ?'a = int, no_vars]} |
|
1132 |
||
1133 |
\<^item> Unset a single bit: @{thm unset_bit_def [where ?'a = int, no_vars]} |
|
1134 |
||
1135 |
\<^item> Flip a single bit: @{thm flip_bit_def [where ?'a = int, no_vars]} |
|
1136 |
\<close> |
|
1137 |
||
71442 | 1138 |
end |