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