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