author | haftmann |
Thu, 17 Sep 2020 09:57:31 +0000 | |
changeset 72262 | a282abb07642 |
parent 72261 | 5193570b739a |
child 72281 | beeadb35e357 |
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 |
||
72262 | 278 |
lemma take_bit_not_eq_mask_diff: |
279 |
\<open>take_bit n (NOT a) = mask n - take_bit n a\<close> |
|
280 |
proof - |
|
281 |
have \<open>take_bit n (NOT a) = take_bit n (NOT (take_bit n a))\<close> |
|
282 |
by (simp add: take_bit_not_take_bit) |
|
283 |
also have \<open>\<dots> = mask n AND NOT (take_bit n a)\<close> |
|
284 |
by (simp add: take_bit_eq_mask ac_simps) |
|
285 |
also have \<open>\<dots> = mask n - take_bit n a\<close> |
|
286 |
by (subst disjunctive_diff) |
|
287 |
(auto simp add: bit_take_bit_iff bit_mask_iff exp_eq_0_imp_not_bit) |
|
288 |
finally show ?thesis |
|
289 |
by simp |
|
290 |
qed |
|
291 |
||
72079 | 292 |
lemma mask_eq_take_bit_minus_one: |
293 |
\<open>mask n = take_bit n (- 1)\<close> |
|
294 |
by (simp add: bit_eq_iff bit_mask_iff bit_take_bit_iff conj_commute) |
|
295 |
||
71922 | 296 |
lemma take_bit_minus_one_eq_mask: |
297 |
\<open>take_bit n (- 1) = mask n\<close> |
|
72079 | 298 |
by (simp add: mask_eq_take_bit_minus_one) |
71922 | 299 |
|
72010 | 300 |
lemma minus_exp_eq_not_mask: |
301 |
\<open>- (2 ^ n) = NOT (mask n)\<close> |
|
302 |
by (rule bit_eqI) (simp add: bit_minus_iff bit_not_iff flip: mask_eq_exp_minus_1) |
|
303 |
||
71922 | 304 |
lemma push_bit_minus_one_eq_not_mask: |
305 |
\<open>push_bit n (- 1) = NOT (mask n)\<close> |
|
72010 | 306 |
by (simp add: push_bit_eq_mult minus_exp_eq_not_mask) |
307 |
||
308 |
lemma take_bit_not_mask_eq_0: |
|
309 |
\<open>take_bit m (NOT (mask n)) = 0\<close> if \<open>n \<ge> m\<close> |
|
310 |
by (rule bit_eqI) (use that in \<open>simp add: bit_take_bit_iff bit_not_iff bit_mask_iff\<close>) |
|
71922 | 311 |
|
72079 | 312 |
lemma take_bit_mask [simp]: |
313 |
\<open>take_bit m (mask n) = mask (min m n)\<close> |
|
314 |
by (simp add: mask_eq_take_bit_minus_one) |
|
315 |
||
71426 | 316 |
definition set_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
71991 | 317 |
where \<open>set_bit n a = a OR push_bit n 1\<close> |
71426 | 318 |
|
319 |
definition unset_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
71991 | 320 |
where \<open>unset_bit n a = a AND NOT (push_bit n 1)\<close> |
71426 | 321 |
|
322 |
definition flip_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
71991 | 323 |
where \<open>flip_bit n a = a XOR push_bit n 1\<close> |
71426 | 324 |
|
325 |
lemma bit_set_bit_iff: |
|
326 |
\<open>bit (set_bit m a) n \<longleftrightarrow> bit a n \<or> (m = n \<and> 2 ^ n \<noteq> 0)\<close> |
|
71991 | 327 |
by (auto simp add: set_bit_def push_bit_of_1 bit_or_iff bit_exp_iff) |
71426 | 328 |
|
329 |
lemma even_set_bit_iff: |
|
330 |
\<open>even (set_bit m a) \<longleftrightarrow> even a \<and> m \<noteq> 0\<close> |
|
331 |
using bit_set_bit_iff [of m a 0] by auto |
|
332 |
||
333 |
lemma bit_unset_bit_iff: |
|
334 |
\<open>bit (unset_bit m a) n \<longleftrightarrow> bit a n \<and> m \<noteq> n\<close> |
|
71991 | 335 |
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 | 336 |
|
337 |
lemma even_unset_bit_iff: |
|
338 |
\<open>even (unset_bit m a) \<longleftrightarrow> even a \<or> m = 0\<close> |
|
339 |
using bit_unset_bit_iff [of m a 0] by auto |
|
340 |
||
341 |
lemma bit_flip_bit_iff: |
|
342 |
\<open>bit (flip_bit m a) n \<longleftrightarrow> (m = n \<longleftrightarrow> \<not> bit a n) \<and> 2 ^ n \<noteq> 0\<close> |
|
71991 | 343 |
by (auto simp add: flip_bit_def push_bit_of_1 bit_xor_iff bit_exp_iff exp_eq_0_imp_not_bit) |
71426 | 344 |
|
345 |
lemma even_flip_bit_iff: |
|
346 |
\<open>even (flip_bit m a) \<longleftrightarrow> \<not> (even a \<longleftrightarrow> m = 0)\<close> |
|
347 |
using bit_flip_bit_iff [of m a 0] by auto |
|
348 |
||
349 |
lemma set_bit_0 [simp]: |
|
350 |
\<open>set_bit 0 a = 1 + 2 * (a div 2)\<close> |
|
351 |
proof (rule bit_eqI) |
|
352 |
fix m |
|
353 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
354 |
then show \<open>bit (set_bit 0 a) m = bit (1 + 2 * (a div 2)) m\<close> |
|
355 |
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
|
356 |
(cases m, simp_all add: bit_Suc) |
71426 | 357 |
qed |
358 |
||
71821 | 359 |
lemma set_bit_Suc: |
71426 | 360 |
\<open>set_bit (Suc n) a = a mod 2 + 2 * set_bit n (a div 2)\<close> |
361 |
proof (rule bit_eqI) |
|
362 |
fix m |
|
363 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
364 |
show \<open>bit (set_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * set_bit n (a div 2)) m\<close> |
|
365 |
proof (cases m) |
|
366 |
case 0 |
|
367 |
then show ?thesis |
|
368 |
by (simp add: even_set_bit_iff) |
|
369 |
next |
|
370 |
case (Suc m) |
|
371 |
with * have \<open>2 ^ m \<noteq> 0\<close> |
|
372 |
using mult_2 by auto |
|
373 |
show ?thesis |
|
374 |
by (cases a rule: parity_cases) |
|
375 |
(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
|
376 |
simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close> bit_Suc) |
71426 | 377 |
qed |
378 |
qed |
|
379 |
||
380 |
lemma unset_bit_0 [simp]: |
|
381 |
\<open>unset_bit 0 a = 2 * (a div 2)\<close> |
|
382 |
proof (rule bit_eqI) |
|
383 |
fix m |
|
384 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
385 |
then show \<open>bit (unset_bit 0 a) m = bit (2 * (a div 2)) m\<close> |
|
386 |
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
|
387 |
(cases m, simp_all add: bit_Suc) |
71426 | 388 |
qed |
389 |
||
71821 | 390 |
lemma unset_bit_Suc: |
71426 | 391 |
\<open>unset_bit (Suc n) a = a mod 2 + 2 * unset_bit n (a div 2)\<close> |
392 |
proof (rule bit_eqI) |
|
393 |
fix m |
|
394 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
395 |
then show \<open>bit (unset_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * unset_bit n (a div 2)) m\<close> |
|
396 |
proof (cases m) |
|
397 |
case 0 |
|
398 |
then show ?thesis |
|
399 |
by (simp add: even_unset_bit_iff) |
|
400 |
next |
|
401 |
case (Suc m) |
|
402 |
show ?thesis |
|
403 |
by (cases a rule: parity_cases) |
|
404 |
(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
|
405 |
simp_all add: Suc bit_Suc) |
71426 | 406 |
qed |
407 |
qed |
|
408 |
||
409 |
lemma flip_bit_0 [simp]: |
|
410 |
\<open>flip_bit 0 a = of_bool (even a) + 2 * (a div 2)\<close> |
|
411 |
proof (rule bit_eqI) |
|
412 |
fix m |
|
413 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
414 |
then show \<open>bit (flip_bit 0 a) m = bit (of_bool (even a) + 2 * (a div 2)) m\<close> |
|
415 |
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
|
416 |
(cases m, simp_all add: bit_Suc) |
71426 | 417 |
qed |
418 |
||
71821 | 419 |
lemma flip_bit_Suc: |
71426 | 420 |
\<open>flip_bit (Suc n) a = a mod 2 + 2 * flip_bit n (a div 2)\<close> |
421 |
proof (rule bit_eqI) |
|
422 |
fix m |
|
423 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
424 |
show \<open>bit (flip_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * flip_bit n (a div 2)) m\<close> |
|
425 |
proof (cases m) |
|
426 |
case 0 |
|
427 |
then show ?thesis |
|
428 |
by (simp add: even_flip_bit_iff) |
|
429 |
next |
|
430 |
case (Suc m) |
|
431 |
with * have \<open>2 ^ m \<noteq> 0\<close> |
|
432 |
using mult_2 by auto |
|
433 |
show ?thesis |
|
434 |
by (cases a rule: parity_cases) |
|
435 |
(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
|
436 |
simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close> bit_Suc) |
71426 | 437 |
qed |
438 |
qed |
|
439 |
||
72009 | 440 |
lemma flip_bit_eq_if: |
441 |
\<open>flip_bit n a = (if bit a n then unset_bit else set_bit) n a\<close> |
|
442 |
by (rule bit_eqI) (auto simp add: bit_set_bit_iff bit_unset_bit_iff bit_flip_bit_iff) |
|
443 |
||
71986 | 444 |
lemma take_bit_set_bit_eq: |
72009 | 445 |
\<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 | 446 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_set_bit_iff) |
447 |
||
448 |
lemma take_bit_unset_bit_eq: |
|
72009 | 449 |
\<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 | 450 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_unset_bit_iff) |
451 |
||
452 |
lemma take_bit_flip_bit_eq: |
|
72009 | 453 |
\<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 | 454 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_flip_bit_iff) |
455 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
456 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
457 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
458 |
|
71956 | 459 |
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
|
460 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
461 |
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
|
462 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
463 |
|
71420 | 464 |
definition not_int :: \<open>int \<Rightarrow> int\<close> |
465 |
where \<open>not_int k = - k - 1\<close> |
|
466 |
||
467 |
lemma not_int_rec: |
|
468 |
"NOT k = of_bool (even k) + 2 * NOT (k div 2)" for k :: int |
|
469 |
by (auto simp add: not_int_def elim: oddE) |
|
470 |
||
471 |
lemma even_not_iff_int: |
|
472 |
\<open>even (NOT k) \<longleftrightarrow> odd k\<close> for k :: int |
|
473 |
by (simp add: not_int_def) |
|
474 |
||
475 |
lemma not_int_div_2: |
|
476 |
\<open>NOT k div 2 = NOT (k div 2)\<close> for k :: int |
|
477 |
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
|
478 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
479 |
lemma bit_not_int_iff: |
71186 | 480 |
\<open>bit (NOT k) n \<longleftrightarrow> \<not> bit k n\<close> |
481 |
for k :: int |
|
71535
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71442
diff
changeset
|
482 |
by (induction n arbitrary: k) (simp_all add: not_int_div_2 even_not_iff_int bit_Suc) |
71186 | 483 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
484 |
function and_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
485 |
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
|
486 |
then - of_bool (odd k \<and> odd l) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
487 |
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
|
488 |
by auto |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
489 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
490 |
termination |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
491 |
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
|
492 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
493 |
declare and_int.simps [simp del] |
71802 | 494 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
495 |
lemma and_int_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
496 |
\<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
|
497 |
for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
498 |
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
|
499 |
case True |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
500 |
then show ?thesis |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
501 |
by auto (simp_all add: and_int.simps) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
502 |
next |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
503 |
case False |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
504 |
then show ?thesis |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
505 |
by (auto simp add: ac_simps and_int.simps [of k l]) |
71802 | 506 |
qed |
507 |
||
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
508 |
lemma bit_and_int_iff: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
509 |
\<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
|
510 |
proof (induction n arbitrary: k l) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
511 |
case 0 |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
512 |
then show ?case |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
513 |
by (simp add: and_int_rec [of k l]) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
514 |
next |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
515 |
case (Suc n) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
516 |
then show ?case |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
517 |
by (simp add: and_int_rec [of k l] bit_Suc) |
71802 | 518 |
qed |
519 |
||
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
520 |
lemma even_and_iff_int: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
521 |
\<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
|
522 |
using bit_and_int_iff [of k l 0] by auto |
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 or_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
525 |
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
|
526 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
527 |
lemma or_int_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
528 |
\<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
|
529 |
for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
530 |
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
|
531 |
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
|
532 |
(simp add: not_int_def) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
533 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
534 |
lemma bit_or_int_iff: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
535 |
\<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
|
536 |
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
|
537 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
538 |
definition xor_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
539 |
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
|
540 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
541 |
lemma xor_int_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
542 |
\<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
|
543 |
for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
544 |
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
|
545 |
(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
|
546 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
547 |
lemma bit_xor_int_iff: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
548 |
\<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
|
549 |
by (auto simp add: xor_int_def bit_or_int_iff bit_and_int_iff bit_not_int_iff) |
71802 | 550 |
|
72082 | 551 |
definition mask_int :: \<open>nat \<Rightarrow> int\<close> |
552 |
where \<open>mask n = (2 :: int) ^ n - 1\<close> |
|
553 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
554 |
instance proof |
71186 | 555 |
fix k l :: int and n :: nat |
71409 | 556 |
show \<open>- k = NOT (k - 1)\<close> |
557 |
by (simp add: not_int_def) |
|
71186 | 558 |
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
|
559 |
by (fact bit_and_int_iff) |
71186 | 560 |
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
|
561 |
by (fact bit_or_int_iff) |
71186 | 562 |
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
|
563 |
by (fact bit_xor_int_iff) |
72082 | 564 |
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
|
565 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
566 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
567 |
|
72009 | 568 |
|
72241 | 569 |
lemma mask_half_int: |
570 |
\<open>mask n div 2 = (mask (n - 1) :: int)\<close> |
|
571 |
by (cases n) (simp_all add: mask_eq_exp_minus_1 algebra_simps) |
|
572 |
||
72028 | 573 |
lemma mask_nonnegative_int [simp]: |
574 |
\<open>mask n \<ge> (0::int)\<close> |
|
575 |
by (simp add: mask_eq_exp_minus_1) |
|
576 |
||
577 |
lemma not_mask_negative_int [simp]: |
|
578 |
\<open>\<not> mask n < (0::int)\<close> |
|
579 |
by (simp add: not_less) |
|
580 |
||
71802 | 581 |
lemma not_nonnegative_int_iff [simp]: |
582 |
\<open>NOT k \<ge> 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
583 |
by (simp add: not_int_def) |
|
584 |
||
585 |
lemma not_negative_int_iff [simp]: |
|
586 |
\<open>NOT k < 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
587 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less not_le) |
|
588 |
||
589 |
lemma and_nonnegative_int_iff [simp]: |
|
590 |
\<open>k AND l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<or> l \<ge> 0\<close> for k l :: int |
|
591 |
proof (induction k arbitrary: l rule: int_bit_induct) |
|
592 |
case zero |
|
593 |
then show ?case |
|
594 |
by simp |
|
595 |
next |
|
596 |
case minus |
|
597 |
then show ?case |
|
598 |
by simp |
|
599 |
next |
|
600 |
case (even k) |
|
601 |
then show ?case |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
602 |
using and_int_rec [of \<open>k * 2\<close> l] by (simp add: pos_imp_zdiv_nonneg_iff) |
71802 | 603 |
next |
604 |
case (odd k) |
|
605 |
from odd have \<open>0 \<le> k AND l div 2 \<longleftrightarrow> 0 \<le> k \<or> 0 \<le> l div 2\<close> |
|
606 |
by simp |
|
607 |
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> |
|
608 |
by simp |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
609 |
with and_int_rec [of \<open>1 + k * 2\<close> l] |
71802 | 610 |
show ?case |
611 |
by auto |
|
612 |
qed |
|
613 |
||
614 |
lemma and_negative_int_iff [simp]: |
|
615 |
\<open>k AND l < 0 \<longleftrightarrow> k < 0 \<and> l < 0\<close> for k l :: int |
|
616 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
617 |
||
72009 | 618 |
lemma and_less_eq: |
619 |
\<open>k AND l \<le> k\<close> if \<open>l < 0\<close> for k l :: int |
|
620 |
using that proof (induction k arbitrary: l rule: int_bit_induct) |
|
621 |
case zero |
|
622 |
then show ?case |
|
623 |
by simp |
|
624 |
next |
|
625 |
case minus |
|
626 |
then show ?case |
|
627 |
by simp |
|
628 |
next |
|
629 |
case (even k) |
|
630 |
from even.IH [of \<open>l div 2\<close>] even.hyps even.prems |
|
631 |
show ?case |
|
632 |
by (simp add: and_int_rec [of _ l]) |
|
633 |
next |
|
634 |
case (odd k) |
|
635 |
from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems |
|
636 |
show ?case |
|
637 |
by (simp add: and_int_rec [of _ l]) |
|
638 |
qed |
|
639 |
||
71802 | 640 |
lemma or_nonnegative_int_iff [simp]: |
641 |
\<open>k OR l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<and> l \<ge> 0\<close> for k l :: int |
|
642 |
by (simp only: or_eq_not_not_and not_nonnegative_int_iff) simp |
|
643 |
||
644 |
lemma or_negative_int_iff [simp]: |
|
645 |
\<open>k OR l < 0 \<longleftrightarrow> k < 0 \<or> l < 0\<close> for k l :: int |
|
646 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
647 |
||
72009 | 648 |
lemma or_greater_eq: |
649 |
\<open>k OR l \<ge> k\<close> if \<open>l \<ge> 0\<close> for k l :: int |
|
650 |
using that proof (induction k arbitrary: l rule: int_bit_induct) |
|
651 |
case zero |
|
652 |
then show ?case |
|
653 |
by simp |
|
654 |
next |
|
655 |
case minus |
|
656 |
then show ?case |
|
657 |
by simp |
|
658 |
next |
|
659 |
case (even k) |
|
660 |
from even.IH [of \<open>l div 2\<close>] even.hyps even.prems |
|
661 |
show ?case |
|
662 |
by (simp add: or_int_rec [of _ l]) |
|
663 |
next |
|
664 |
case (odd k) |
|
665 |
from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems |
|
666 |
show ?case |
|
667 |
by (simp add: or_int_rec [of _ l]) |
|
668 |
qed |
|
669 |
||
71802 | 670 |
lemma xor_nonnegative_int_iff [simp]: |
671 |
\<open>k XOR l \<ge> 0 \<longleftrightarrow> (k \<ge> 0 \<longleftrightarrow> l \<ge> 0)\<close> for k l :: int |
|
672 |
by (simp only: bit.xor_def or_nonnegative_int_iff) auto |
|
673 |
||
674 |
lemma xor_negative_int_iff [simp]: |
|
675 |
\<open>k XOR l < 0 \<longleftrightarrow> (k < 0) \<noteq> (l < 0)\<close> for k l :: int |
|
676 |
by (subst Not_eq_iff [symmetric]) (auto simp add: not_less) |
|
677 |
||
678 |
lemma set_bit_nonnegative_int_iff [simp]: |
|
679 |
\<open>set_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
680 |
by (simp add: set_bit_def) |
|
681 |
||
682 |
lemma set_bit_negative_int_iff [simp]: |
|
683 |
\<open>set_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
684 |
by (simp add: set_bit_def) |
|
685 |
||
686 |
lemma unset_bit_nonnegative_int_iff [simp]: |
|
687 |
\<open>unset_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
688 |
by (simp add: unset_bit_def) |
|
689 |
||
690 |
lemma unset_bit_negative_int_iff [simp]: |
|
691 |
\<open>unset_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
692 |
by (simp add: unset_bit_def) |
|
693 |
||
694 |
lemma flip_bit_nonnegative_int_iff [simp]: |
|
695 |
\<open>flip_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
696 |
by (simp add: flip_bit_def) |
|
697 |
||
698 |
lemma flip_bit_negative_int_iff [simp]: |
|
699 |
\<open>flip_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
700 |
by (simp add: flip_bit_def) |
|
701 |
||
71986 | 702 |
lemma set_bit_greater_eq: |
703 |
\<open>set_bit n k \<ge> k\<close> for k :: int |
|
704 |
by (simp add: set_bit_def or_greater_eq) |
|
705 |
||
706 |
lemma unset_bit_less_eq: |
|
707 |
\<open>unset_bit n k \<le> k\<close> for k :: int |
|
708 |
by (simp add: unset_bit_def and_less_eq) |
|
709 |
||
72009 | 710 |
lemma set_bit_eq: |
711 |
\<open>set_bit n k = k + of_bool (\<not> bit k n) * 2 ^ n\<close> for k :: int |
|
712 |
proof (rule bit_eqI) |
|
713 |
fix m |
|
714 |
show \<open>bit (set_bit n k) m \<longleftrightarrow> bit (k + of_bool (\<not> bit k n) * 2 ^ n) m\<close> |
|
715 |
proof (cases \<open>m = n\<close>) |
|
716 |
case True |
|
717 |
then show ?thesis |
|
718 |
apply (simp add: bit_set_bit_iff) |
|
719 |
apply (simp add: bit_iff_odd div_plus_div_distrib_dvd_right) |
|
720 |
done |
|
721 |
next |
|
722 |
case False |
|
723 |
then show ?thesis |
|
724 |
apply (clarsimp simp add: bit_set_bit_iff) |
|
725 |
apply (subst disjunctive_add) |
|
726 |
apply (clarsimp simp add: bit_exp_iff) |
|
727 |
apply (clarsimp simp add: bit_or_iff bit_exp_iff) |
|
728 |
done |
|
729 |
qed |
|
730 |
qed |
|
731 |
||
732 |
lemma unset_bit_eq: |
|
733 |
\<open>unset_bit n k = k - of_bool (bit k n) * 2 ^ n\<close> for k :: int |
|
734 |
proof (rule bit_eqI) |
|
735 |
fix m |
|
736 |
show \<open>bit (unset_bit n k) m \<longleftrightarrow> bit (k - of_bool (bit k n) * 2 ^ n) m\<close> |
|
737 |
proof (cases \<open>m = n\<close>) |
|
738 |
case True |
|
739 |
then show ?thesis |
|
740 |
apply (simp add: bit_unset_bit_iff) |
|
741 |
apply (simp add: bit_iff_odd) |
|
742 |
using div_plus_div_distrib_dvd_right [of \<open>2 ^ n\<close> \<open>- (2 ^ n)\<close> k] |
|
743 |
apply (simp add: dvd_neg_div) |
|
744 |
done |
|
745 |
next |
|
746 |
case False |
|
747 |
then show ?thesis |
|
748 |
apply (clarsimp simp add: bit_unset_bit_iff) |
|
749 |
apply (subst disjunctive_diff) |
|
750 |
apply (clarsimp simp add: bit_exp_iff) |
|
751 |
apply (clarsimp simp add: bit_and_iff bit_not_iff bit_exp_iff) |
|
752 |
done |
|
753 |
qed |
|
754 |
qed |
|
755 |
||
72227 | 756 |
context ring_bit_operations |
757 |
begin |
|
758 |
||
759 |
lemma even_of_int_iff: |
|
760 |
\<open>even (of_int k) \<longleftrightarrow> even k\<close> |
|
761 |
by (induction k rule: int_bit_induct) simp_all |
|
762 |
||
763 |
lemma bit_of_int_iff: |
|
764 |
\<open>bit (of_int k) n \<longleftrightarrow> (2::'a) ^ n \<noteq> 0 \<and> bit k n\<close> |
|
765 |
proof (cases \<open>(2::'a) ^ n = 0\<close>) |
|
766 |
case True |
|
767 |
then show ?thesis |
|
768 |
by (simp add: exp_eq_0_imp_not_bit) |
|
769 |
next |
|
770 |
case False |
|
771 |
then have \<open>bit (of_int k) n \<longleftrightarrow> bit k n\<close> |
|
772 |
proof (induction k arbitrary: n rule: int_bit_induct) |
|
773 |
case zero |
|
774 |
then show ?case |
|
775 |
by simp |
|
776 |
next |
|
777 |
case minus |
|
778 |
then show ?case |
|
779 |
by simp |
|
780 |
next |
|
781 |
case (even k) |
|
782 |
then show ?case |
|
783 |
using bit_double_iff [of \<open>of_int k\<close> n] Parity.bit_double_iff [of k n] |
|
784 |
by (cases n) (auto simp add: ac_simps dest: mult_not_zero) |
|
785 |
next |
|
786 |
case (odd k) |
|
787 |
then show ?case |
|
788 |
using bit_double_iff [of \<open>of_int k\<close> n] |
|
789 |
by (cases n) (auto simp add: ac_simps bit_double_iff even_bit_succ_iff Parity.bit_Suc dest: mult_not_zero) |
|
790 |
qed |
|
791 |
with False show ?thesis |
|
792 |
by simp |
|
793 |
qed |
|
794 |
||
795 |
lemma push_bit_of_int: |
|
796 |
\<open>push_bit n (of_int k) = of_int (push_bit n k)\<close> |
|
797 |
by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult) |
|
798 |
||
799 |
lemma of_int_push_bit: |
|
800 |
\<open>of_int (push_bit n k) = push_bit n (of_int k)\<close> |
|
801 |
by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult) |
|
802 |
||
803 |
lemma take_bit_of_int: |
|
804 |
\<open>take_bit n (of_int k) = of_int (take_bit n k)\<close> |
|
805 |
by (rule bit_eqI) (simp add: bit_take_bit_iff Parity.bit_take_bit_iff bit_of_int_iff) |
|
806 |
||
807 |
lemma of_int_take_bit: |
|
808 |
\<open>of_int (take_bit n k) = take_bit n (of_int k)\<close> |
|
809 |
by (rule bit_eqI) (simp add: bit_take_bit_iff Parity.bit_take_bit_iff bit_of_int_iff) |
|
810 |
||
811 |
lemma of_int_not_eq: |
|
812 |
\<open>of_int (NOT k) = NOT (of_int k)\<close> |
|
813 |
by (rule bit_eqI) (simp add: bit_not_iff Bit_Operations.bit_not_iff bit_of_int_iff) |
|
814 |
||
815 |
lemma of_int_and_eq: |
|
816 |
\<open>of_int (k AND l) = of_int k AND of_int l\<close> |
|
817 |
by (rule bit_eqI) (simp add: bit_of_int_iff bit_and_iff Bit_Operations.bit_and_iff) |
|
818 |
||
819 |
lemma of_int_or_eq: |
|
820 |
\<open>of_int (k OR l) = of_int k OR of_int l\<close> |
|
821 |
by (rule bit_eqI) (simp add: bit_of_int_iff bit_or_iff Bit_Operations.bit_or_iff) |
|
822 |
||
823 |
lemma of_int_xor_eq: |
|
824 |
\<open>of_int (k XOR l) = of_int k XOR of_int l\<close> |
|
825 |
by (rule bit_eqI) (simp add: bit_of_int_iff bit_xor_iff Bit_Operations.bit_xor_iff) |
|
826 |
||
827 |
lemma of_int_mask_eq: |
|
828 |
\<open>of_int (mask n) = mask n\<close> |
|
829 |
by (induction n) (simp_all add: mask_Suc_double Bit_Operations.mask_Suc_double of_int_or_eq) |
|
830 |
||
831 |
end |
|
832 |
||
71442 | 833 |
|
72028 | 834 |
subsection \<open>Bit concatenation\<close> |
835 |
||
836 |
definition concat_bit :: \<open>nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int\<close> |
|
72227 | 837 |
where \<open>concat_bit n k l = take_bit n k OR push_bit n l\<close> |
72028 | 838 |
|
839 |
lemma bit_concat_bit_iff: |
|
840 |
\<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 | 841 |
by (simp add: concat_bit_def bit_or_iff bit_and_iff bit_take_bit_iff bit_push_bit_iff ac_simps) |
72028 | 842 |
|
843 |
lemma concat_bit_eq: |
|
844 |
\<open>concat_bit n k l = take_bit n k + push_bit n l\<close> |
|
845 |
by (simp add: concat_bit_def take_bit_eq_mask |
|
846 |
bit_and_iff bit_mask_iff bit_push_bit_iff disjunctive_add) |
|
847 |
||
848 |
lemma concat_bit_0 [simp]: |
|
849 |
\<open>concat_bit 0 k l = l\<close> |
|
850 |
by (simp add: concat_bit_def) |
|
851 |
||
852 |
lemma concat_bit_Suc: |
|
853 |
\<open>concat_bit (Suc n) k l = k mod 2 + 2 * concat_bit n (k div 2) l\<close> |
|
854 |
by (simp add: concat_bit_eq take_bit_Suc push_bit_double) |
|
855 |
||
856 |
lemma concat_bit_of_zero_1 [simp]: |
|
857 |
\<open>concat_bit n 0 l = push_bit n l\<close> |
|
858 |
by (simp add: concat_bit_def) |
|
859 |
||
860 |
lemma concat_bit_of_zero_2 [simp]: |
|
861 |
\<open>concat_bit n k 0 = take_bit n k\<close> |
|
862 |
by (simp add: concat_bit_def take_bit_eq_mask) |
|
863 |
||
864 |
lemma concat_bit_nonnegative_iff [simp]: |
|
865 |
\<open>concat_bit n k l \<ge> 0 \<longleftrightarrow> l \<ge> 0\<close> |
|
866 |
by (simp add: concat_bit_def) |
|
867 |
||
868 |
lemma concat_bit_negative_iff [simp]: |
|
869 |
\<open>concat_bit n k l < 0 \<longleftrightarrow> l < 0\<close> |
|
870 |
by (simp add: concat_bit_def) |
|
871 |
||
872 |
lemma concat_bit_assoc: |
|
873 |
\<open>concat_bit n k (concat_bit m l r) = concat_bit (m + n) (concat_bit n k l) r\<close> |
|
874 |
by (rule bit_eqI) (auto simp add: bit_concat_bit_iff ac_simps) |
|
875 |
||
876 |
lemma concat_bit_assoc_sym: |
|
877 |
\<open>concat_bit m (concat_bit n k l) r = concat_bit (min m n) k (concat_bit (m - n) l r)\<close> |
|
878 |
by (rule bit_eqI) (auto simp add: bit_concat_bit_iff ac_simps min_def) |
|
879 |
||
72227 | 880 |
lemma concat_bit_eq_iff: |
881 |
\<open>concat_bit n k l = concat_bit n r s |
|
882 |
\<longleftrightarrow> take_bit n k = take_bit n r \<and> l = s\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
883 |
proof |
|
884 |
assume ?Q |
|
885 |
then show ?P |
|
886 |
by (simp add: concat_bit_def) |
|
887 |
next |
|
888 |
assume ?P |
|
889 |
then have *: \<open>bit (concat_bit n k l) m = bit (concat_bit n r s) m\<close> for m |
|
890 |
by (simp add: bit_eq_iff) |
|
891 |
have \<open>take_bit n k = take_bit n r\<close> |
|
892 |
proof (rule bit_eqI) |
|
893 |
fix m |
|
894 |
from * [of m] |
|
895 |
show \<open>bit (take_bit n k) m \<longleftrightarrow> bit (take_bit n r) m\<close> |
|
896 |
by (auto simp add: bit_take_bit_iff bit_concat_bit_iff) |
|
897 |
qed |
|
898 |
moreover have \<open>push_bit n l = push_bit n s\<close> |
|
899 |
proof (rule bit_eqI) |
|
900 |
fix m |
|
901 |
from * [of m] |
|
902 |
show \<open>bit (push_bit n l) m \<longleftrightarrow> bit (push_bit n s) m\<close> |
|
903 |
by (auto simp add: bit_push_bit_iff bit_concat_bit_iff) |
|
904 |
qed |
|
905 |
then have \<open>l = s\<close> |
|
906 |
by (simp add: push_bit_eq_mult) |
|
907 |
ultimately show ?Q |
|
908 |
by (simp add: concat_bit_def) |
|
909 |
qed |
|
910 |
||
911 |
lemma take_bit_concat_bit_eq: |
|
912 |
\<open>take_bit m (concat_bit n k l) = concat_bit (min m n) k (take_bit (m - n) l)\<close> |
|
913 |
by (rule bit_eqI) |
|
914 |
(auto simp add: bit_take_bit_iff bit_concat_bit_iff min_def) |
|
915 |
||
72028 | 916 |
|
72241 | 917 |
subsection \<open>Taking bits with sign propagation\<close> |
72010 | 918 |
|
72241 | 919 |
context ring_bit_operations |
920 |
begin |
|
72010 | 921 |
|
72241 | 922 |
definition signed_take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
923 |
where \<open>signed_take_bit n a = take_bit n a OR (of_bool (bit a n) * NOT (mask n))\<close> |
|
72227 | 924 |
|
72241 | 925 |
lemma signed_take_bit_eq_if_positive: |
926 |
\<open>signed_take_bit n a = take_bit n a\<close> if \<open>\<not> bit a n\<close> |
|
72010 | 927 |
using that by (simp add: signed_take_bit_def) |
928 |
||
72241 | 929 |
lemma signed_take_bit_eq_if_negative: |
930 |
\<open>signed_take_bit n a = take_bit n a OR NOT (mask n)\<close> if \<open>bit a n\<close> |
|
931 |
using that by (simp add: signed_take_bit_def) |
|
932 |
||
933 |
lemma even_signed_take_bit_iff: |
|
934 |
\<open>even (signed_take_bit m a) \<longleftrightarrow> even a\<close> |
|
935 |
by (auto simp add: signed_take_bit_def even_or_iff even_mask_iff bit_double_iff) |
|
936 |
||
937 |
lemma bit_signed_take_bit_iff: |
|
938 |
\<open>bit (signed_take_bit m a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> bit a (min m n)\<close> |
|
939 |
by (simp add: signed_take_bit_def bit_take_bit_iff bit_or_iff bit_not_iff bit_mask_iff min_def not_le) |
|
940 |
(use exp_eq_0_imp_not_bit in blast) |
|
72010 | 941 |
|
942 |
lemma signed_take_bit_0 [simp]: |
|
72241 | 943 |
\<open>signed_take_bit 0 a = - (a mod 2)\<close> |
72010 | 944 |
by (simp add: signed_take_bit_def odd_iff_mod_2_eq_one) |
945 |
||
946 |
lemma signed_take_bit_Suc: |
|
72241 | 947 |
\<open>signed_take_bit (Suc n) a = a mod 2 + 2 * signed_take_bit n (a div 2)\<close> |
948 |
proof (rule bit_eqI) |
|
949 |
fix m |
|
950 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
951 |
show \<open>bit (signed_take_bit (Suc n) a) m \<longleftrightarrow> |
|
952 |
bit (a mod 2 + 2 * signed_take_bit n (a div 2)) m\<close> |
|
953 |
proof (cases m) |
|
954 |
case 0 |
|
955 |
then show ?thesis |
|
956 |
by (simp add: even_signed_take_bit_iff) |
|
957 |
next |
|
958 |
case (Suc m) |
|
959 |
with * have \<open>2 ^ m \<noteq> 0\<close> |
|
960 |
by (metis mult_not_zero power_Suc) |
|
961 |
with Suc show ?thesis |
|
962 |
by (simp add: bit_signed_take_bit_iff mod2_eq_if bit_double_iff even_bit_succ_iff |
|
963 |
ac_simps flip: bit_Suc) |
|
964 |
qed |
|
965 |
qed |
|
72010 | 966 |
|
72187 | 967 |
lemma signed_take_bit_of_0 [simp]: |
968 |
\<open>signed_take_bit n 0 = 0\<close> |
|
969 |
by (simp add: signed_take_bit_def) |
|
970 |
||
971 |
lemma signed_take_bit_of_minus_1 [simp]: |
|
972 |
\<open>signed_take_bit n (- 1) = - 1\<close> |
|
72241 | 973 |
by (simp add: signed_take_bit_def take_bit_minus_one_eq_mask mask_eq_exp_minus_1) |
72187 | 974 |
|
72241 | 975 |
lemma signed_take_bit_Suc_1 [simp]: |
976 |
\<open>signed_take_bit (Suc n) 1 = 1\<close> |
|
977 |
by (simp add: signed_take_bit_Suc) |
|
978 |
||
979 |
lemma signed_take_bit_rec: |
|
980 |
\<open>signed_take_bit n a = (if n = 0 then - (a mod 2) else a mod 2 + 2 * signed_take_bit (n - 1) (a div 2))\<close> |
|
981 |
by (cases n) (simp_all add: signed_take_bit_Suc) |
|
72187 | 982 |
|
983 |
lemma signed_take_bit_eq_iff_take_bit_eq: |
|
72241 | 984 |
\<open>signed_take_bit n a = signed_take_bit n b \<longleftrightarrow> take_bit (Suc n) a = take_bit (Suc n) b\<close> |
985 |
proof - |
|
986 |
have \<open>bit (signed_take_bit n a) = bit (signed_take_bit n b) \<longleftrightarrow> bit (take_bit (Suc n) a) = bit (take_bit (Suc n) b)\<close> |
|
987 |
by (simp add: fun_eq_iff bit_signed_take_bit_iff bit_take_bit_iff not_le less_Suc_eq_le min_def) |
|
988 |
(use exp_eq_0_imp_not_bit in fastforce) |
|
72187 | 989 |
then show ?thesis |
72241 | 990 |
by (simp add: bit_eq_iff fun_eq_iff) |
72187 | 991 |
qed |
992 |
||
72241 | 993 |
lemma signed_take_bit_signed_take_bit [simp]: |
994 |
\<open>signed_take_bit m (signed_take_bit n a) = signed_take_bit (min m n) a\<close> |
|
995 |
proof (rule bit_eqI) |
|
996 |
fix q |
|
997 |
show \<open>bit (signed_take_bit m (signed_take_bit n a)) q \<longleftrightarrow> |
|
998 |
bit (signed_take_bit (min m n) a) q\<close> |
|
999 |
by (simp add: bit_signed_take_bit_iff min_def bit_or_iff bit_not_iff bit_mask_iff bit_take_bit_iff) |
|
1000 |
(use le_Suc_ex exp_add_not_zero_imp in blast) |
|
1001 |
qed |
|
1002 |
||
1003 |
lemma signed_take_bit_take_bit: |
|
1004 |
\<open>signed_take_bit m (take_bit n a) = (if n \<le> m then take_bit n else signed_take_bit m) a\<close> |
|
1005 |
by (rule bit_eqI) (auto simp add: bit_signed_take_bit_iff min_def bit_take_bit_iff) |
|
1006 |
||
72187 | 1007 |
lemma take_bit_signed_take_bit: |
72241 | 1008 |
\<open>take_bit m (signed_take_bit n a) = take_bit m a\<close> if \<open>m \<le> Suc n\<close> |
72187 | 1009 |
using that by (rule le_SucE; intro bit_eqI) |
1010 |
(auto simp add: bit_take_bit_iff bit_signed_take_bit_iff min_def less_Suc_eq) |
|
1011 |
||
72241 | 1012 |
end |
1013 |
||
1014 |
text \<open>Modulus centered around 0\<close> |
|
1015 |
||
1016 |
lemma signed_take_bit_eq_concat_bit: |
|
1017 |
\<open>signed_take_bit n k = concat_bit n k (- of_bool (bit k n))\<close> |
|
1018 |
by (simp add: concat_bit_def signed_take_bit_def push_bit_minus_one_eq_not_mask) |
|
1019 |
||
72187 | 1020 |
lemma signed_take_bit_add: |
1021 |
\<open>signed_take_bit n (signed_take_bit n k + signed_take_bit n l) = signed_take_bit n (k + l)\<close> |
|
72241 | 1022 |
for k l :: int |
72187 | 1023 |
proof - |
1024 |
have \<open>take_bit (Suc n) |
|
1025 |
(take_bit (Suc n) (signed_take_bit n k) + |
|
1026 |
take_bit (Suc n) (signed_take_bit n l)) = |
|
1027 |
take_bit (Suc n) (k + l)\<close> |
|
1028 |
by (simp add: take_bit_signed_take_bit take_bit_add) |
|
1029 |
then show ?thesis |
|
1030 |
by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_add) |
|
1031 |
qed |
|
1032 |
||
1033 |
lemma signed_take_bit_diff: |
|
1034 |
\<open>signed_take_bit n (signed_take_bit n k - signed_take_bit n l) = signed_take_bit n (k - l)\<close> |
|
72241 | 1035 |
for k l :: int |
72187 | 1036 |
proof - |
1037 |
have \<open>take_bit (Suc n) |
|
1038 |
(take_bit (Suc n) (signed_take_bit n k) - |
|
1039 |
take_bit (Suc n) (signed_take_bit n l)) = |
|
1040 |
take_bit (Suc n) (k - l)\<close> |
|
1041 |
by (simp add: take_bit_signed_take_bit take_bit_diff) |
|
1042 |
then show ?thesis |
|
1043 |
by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_diff) |
|
1044 |
qed |
|
1045 |
||
1046 |
lemma signed_take_bit_minus: |
|
1047 |
\<open>signed_take_bit n (- signed_take_bit n k) = signed_take_bit n (- k)\<close> |
|
72241 | 1048 |
for k :: int |
72187 | 1049 |
proof - |
1050 |
have \<open>take_bit (Suc n) |
|
1051 |
(- take_bit (Suc n) (signed_take_bit n k)) = |
|
1052 |
take_bit (Suc n) (- k)\<close> |
|
1053 |
by (simp add: take_bit_signed_take_bit take_bit_minus) |
|
1054 |
then show ?thesis |
|
1055 |
by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_minus) |
|
1056 |
qed |
|
1057 |
||
1058 |
lemma signed_take_bit_mult: |
|
1059 |
\<open>signed_take_bit n (signed_take_bit n k * signed_take_bit n l) = signed_take_bit n (k * l)\<close> |
|
72241 | 1060 |
for k l :: int |
72187 | 1061 |
proof - |
1062 |
have \<open>take_bit (Suc n) |
|
1063 |
(take_bit (Suc n) (signed_take_bit n k) * |
|
1064 |
take_bit (Suc n) (signed_take_bit n l)) = |
|
1065 |
take_bit (Suc n) (k * l)\<close> |
|
1066 |
by (simp add: take_bit_signed_take_bit take_bit_mult) |
|
1067 |
then show ?thesis |
|
1068 |
by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_mult) |
|
1069 |
qed |
|
1070 |
||
72010 | 1071 |
lemma signed_take_bit_eq_take_bit_minus: |
1072 |
\<open>signed_take_bit n k = take_bit (Suc n) k - 2 ^ Suc n * of_bool (bit k n)\<close> |
|
72241 | 1073 |
for k :: int |
72010 | 1074 |
proof (cases \<open>bit k n\<close>) |
1075 |
case True |
|
1076 |
have \<open>signed_take_bit n k = take_bit (Suc n) k OR NOT (mask (Suc n))\<close> |
|
1077 |
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) |
|
1078 |
then have \<open>signed_take_bit n k = take_bit (Suc n) k + NOT (mask (Suc n))\<close> |
|
1079 |
by (simp add: disjunctive_add bit_take_bit_iff bit_not_iff bit_mask_iff) |
|
1080 |
with True show ?thesis |
|
1081 |
by (simp flip: minus_exp_eq_not_mask) |
|
1082 |
next |
|
1083 |
case False |
|
72241 | 1084 |
show ?thesis |
1085 |
by (rule bit_eqI) (simp add: False bit_signed_take_bit_iff bit_take_bit_iff min_def less_Suc_eq) |
|
72010 | 1086 |
qed |
1087 |
||
1088 |
lemma signed_take_bit_eq_take_bit_shift: |
|
1089 |
\<open>signed_take_bit n k = take_bit (Suc n) (k + 2 ^ n) - 2 ^ n\<close> |
|
72241 | 1090 |
for k :: int |
72010 | 1091 |
proof - |
1092 |
have *: \<open>take_bit n k OR 2 ^ n = take_bit n k + 2 ^ n\<close> |
|
1093 |
by (simp add: disjunctive_add bit_exp_iff bit_take_bit_iff) |
|
1094 |
have \<open>take_bit n k - 2 ^ n = take_bit n k + NOT (mask n)\<close> |
|
1095 |
by (simp add: minus_exp_eq_not_mask) |
|
1096 |
also have \<open>\<dots> = take_bit n k OR NOT (mask n)\<close> |
|
1097 |
by (rule disjunctive_add) |
|
1098 |
(simp add: bit_exp_iff bit_take_bit_iff bit_not_iff bit_mask_iff) |
|
1099 |
finally have **: \<open>take_bit n k - 2 ^ n = take_bit n k OR NOT (mask n)\<close> . |
|
1100 |
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> |
|
1101 |
by (simp only: take_bit_add) |
|
1102 |
also have \<open>take_bit (Suc n) k = 2 ^ n * of_bool (bit k n) + take_bit n k\<close> |
|
1103 |
by (simp add: take_bit_Suc_from_most) |
|
1104 |
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> |
|
1105 |
by (simp add: ac_simps) |
|
1106 |
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> |
|
1107 |
by (rule disjunctive_add) |
|
1108 |
(auto simp add: disjunctive_add bit_take_bit_iff bit_double_iff bit_exp_iff) |
|
1109 |
finally show ?thesis |
|
72241 | 1110 |
using * ** by (simp add: signed_take_bit_def concat_bit_Suc min_def ac_simps) |
72010 | 1111 |
qed |
1112 |
||
1113 |
lemma signed_take_bit_nonnegative_iff [simp]: |
|
1114 |
\<open>0 \<le> signed_take_bit n k \<longleftrightarrow> \<not> bit k n\<close> |
|
72241 | 1115 |
for k :: int |
72028 | 1116 |
by (simp add: signed_take_bit_def not_less concat_bit_def) |
72010 | 1117 |
|
1118 |
lemma signed_take_bit_negative_iff [simp]: |
|
1119 |
\<open>signed_take_bit n k < 0 \<longleftrightarrow> bit k n\<close> |
|
72241 | 1120 |
for k :: int |
72028 | 1121 |
by (simp add: signed_take_bit_def not_less concat_bit_def) |
72010 | 1122 |
|
72261 | 1123 |
lemma signed_take_bit_int_eq_self_iff: |
1124 |
\<open>signed_take_bit n k = k \<longleftrightarrow> - (2 ^ n) \<le> k \<and> k < 2 ^ n\<close> |
|
1125 |
for k :: int |
|
1126 |
by (auto simp add: signed_take_bit_eq_take_bit_shift take_bit_int_eq_self_iff algebra_simps) |
|
1127 |
||
72262 | 1128 |
lemma signed_take_bit_int_eq_self: |
1129 |
\<open>signed_take_bit n k = k\<close> if \<open>- (2 ^ n) \<le> k\<close> \<open>k < 2 ^ n\<close> |
|
1130 |
for k :: int |
|
1131 |
using that by (simp add: signed_take_bit_int_eq_self_iff) |
|
1132 |
||
72261 | 1133 |
lemma signed_take_bit_int_less_eq_self_iff: |
1134 |
\<open>signed_take_bit n k \<le> k \<longleftrightarrow> - (2 ^ n) \<le> k\<close> |
|
1135 |
for k :: int |
|
1136 |
by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_less_eq_self_iff algebra_simps) |
|
1137 |
linarith |
|
1138 |
||
1139 |
lemma signed_take_bit_int_less_self_iff: |
|
1140 |
\<open>signed_take_bit n k < k \<longleftrightarrow> 2 ^ n \<le> k\<close> |
|
1141 |
for k :: int |
|
1142 |
by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_less_self_iff algebra_simps) |
|
1143 |
||
1144 |
lemma signed_take_bit_int_greater_self_iff: |
|
1145 |
\<open>k < signed_take_bit n k \<longleftrightarrow> k < - (2 ^ n)\<close> |
|
1146 |
for k :: int |
|
1147 |
by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_greater_self_iff algebra_simps) |
|
1148 |
linarith |
|
1149 |
||
1150 |
lemma signed_take_bit_int_greater_eq_self_iff: |
|
1151 |
\<open>k \<le> signed_take_bit n k \<longleftrightarrow> k < 2 ^ n\<close> |
|
1152 |
for k :: int |
|
1153 |
by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_greater_eq_self_iff algebra_simps) |
|
1154 |
||
1155 |
lemma signed_take_bit_int_greater_eq: |
|
72010 | 1156 |
\<open>k + 2 ^ Suc n \<le> signed_take_bit n k\<close> if \<open>k < - (2 ^ n)\<close> |
72241 | 1157 |
for k :: int |
72262 | 1158 |
using that take_bit_int_greater_eq [of \<open>k + 2 ^ n\<close> \<open>Suc n\<close>] |
72010 | 1159 |
by (simp add: signed_take_bit_eq_take_bit_shift) |
1160 |
||
72261 | 1161 |
lemma signed_take_bit_int_less_eq: |
72010 | 1162 |
\<open>signed_take_bit n k \<le> k - 2 ^ Suc n\<close> if \<open>k \<ge> 2 ^ n\<close> |
72241 | 1163 |
for k :: int |
72262 | 1164 |
using that take_bit_int_less_eq [of \<open>Suc n\<close> \<open>k + 2 ^ n\<close>] |
72010 | 1165 |
by (simp add: signed_take_bit_eq_take_bit_shift) |
1166 |
||
1167 |
lemma signed_take_bit_Suc_bit0 [simp]: |
|
72241 | 1168 |
\<open>signed_take_bit (Suc n) (numeral (Num.Bit0 k)) = signed_take_bit n (numeral k) * (2 :: int)\<close> |
72010 | 1169 |
by (simp add: signed_take_bit_Suc) |
1170 |
||
1171 |
lemma signed_take_bit_Suc_bit1 [simp]: |
|
72241 | 1172 |
\<open>signed_take_bit (Suc n) (numeral (Num.Bit1 k)) = signed_take_bit n (numeral k) * 2 + (1 :: int)\<close> |
72010 | 1173 |
by (simp add: signed_take_bit_Suc) |
1174 |
||
1175 |
lemma signed_take_bit_Suc_minus_bit0 [simp]: |
|
72241 | 1176 |
\<open>signed_take_bit (Suc n) (- numeral (Num.Bit0 k)) = signed_take_bit n (- numeral k) * (2 :: int)\<close> |
72010 | 1177 |
by (simp add: signed_take_bit_Suc) |
1178 |
||
1179 |
lemma signed_take_bit_Suc_minus_bit1 [simp]: |
|
72241 | 1180 |
\<open>signed_take_bit (Suc n) (- numeral (Num.Bit1 k)) = signed_take_bit n (- numeral k - 1) * 2 + (1 :: int)\<close> |
72010 | 1181 |
by (simp add: signed_take_bit_Suc) |
1182 |
||
1183 |
lemma signed_take_bit_numeral_bit0 [simp]: |
|
72241 | 1184 |
\<open>signed_take_bit (numeral l) (numeral (Num.Bit0 k)) = signed_take_bit (pred_numeral l) (numeral k) * (2 :: int)\<close> |
72010 | 1185 |
by (simp add: signed_take_bit_rec) |
1186 |
||
1187 |
lemma signed_take_bit_numeral_bit1 [simp]: |
|
72241 | 1188 |
\<open>signed_take_bit (numeral l) (numeral (Num.Bit1 k)) = signed_take_bit (pred_numeral l) (numeral k) * 2 + (1 :: int)\<close> |
72010 | 1189 |
by (simp add: signed_take_bit_rec) |
1190 |
||
1191 |
lemma signed_take_bit_numeral_minus_bit0 [simp]: |
|
72241 | 1192 |
\<open>signed_take_bit (numeral l) (- numeral (Num.Bit0 k)) = signed_take_bit (pred_numeral l) (- numeral k) * (2 :: int)\<close> |
72010 | 1193 |
by (simp add: signed_take_bit_rec) |
1194 |
||
1195 |
lemma signed_take_bit_numeral_minus_bit1 [simp]: |
|
72241 | 1196 |
\<open>signed_take_bit (numeral l) (- numeral (Num.Bit1 k)) = signed_take_bit (pred_numeral l) (- numeral k - 1) * 2 + (1 :: int)\<close> |
72010 | 1197 |
by (simp add: signed_take_bit_rec) |
1198 |
||
1199 |
lemma signed_take_bit_code [code]: |
|
72241 | 1200 |
\<open>signed_take_bit n a = |
1201 |
(let l = take_bit (Suc n) a |
|
1202 |
in if bit l n then l + push_bit (Suc n) (- 1) else l)\<close> |
|
72010 | 1203 |
proof - |
72241 | 1204 |
have *: \<open>take_bit (Suc n) a + push_bit n (- 2) = |
1205 |
take_bit (Suc n) a OR NOT (mask (Suc n))\<close> |
|
1206 |
by (auto simp add: bit_take_bit_iff bit_push_bit_iff bit_not_iff bit_mask_iff disjunctive_add |
|
1207 |
simp flip: push_bit_minus_one_eq_not_mask) |
|
72010 | 1208 |
show ?thesis |
1209 |
by (rule bit_eqI) |
|
72241 | 1210 |
(auto simp add: Let_def * bit_signed_take_bit_iff bit_take_bit_iff min_def less_Suc_eq bit_not_iff bit_mask_iff bit_or_iff) |
72010 | 1211 |
qed |
1212 |
||
1213 |
||
71956 | 1214 |
subsection \<open>Instance \<^typ>\<open>nat\<close>\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1215 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1216 |
instantiation nat :: semiring_bit_operations |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1217 |
begin |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1218 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1219 |
definition and_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1220 |
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
|
1221 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1222 |
definition or_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1223 |
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
|
1224 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1225 |
definition xor_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1226 |
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
|
1227 |
|
72082 | 1228 |
definition mask_nat :: \<open>nat \<Rightarrow> nat\<close> |
1229 |
where \<open>mask n = (2 :: nat) ^ n - 1\<close> |
|
1230 |
||
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1231 |
instance proof |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1232 |
fix m n q :: nat |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1233 |
show \<open>bit (m AND n) q \<longleftrightarrow> bit m q \<and> bit n q\<close> |
72227 | 1234 |
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
|
1235 |
show \<open>bit (m OR n) q \<longleftrightarrow> bit m q \<or> bit n q\<close> |
72227 | 1236 |
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
|
1237 |
show \<open>bit (m XOR n) q \<longleftrightarrow> bit m q \<noteq> bit n q\<close> |
72227 | 1238 |
by (auto simp add: bit_nat_iff xor_nat_def bit_xor_iff less_le bit_eq_iff) |
72082 | 1239 |
qed (simp add: mask_nat_def) |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1240 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1241 |
end |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1242 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1243 |
lemma and_nat_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1244 |
\<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
|
1245 |
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
|
1246 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1247 |
lemma or_nat_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1248 |
\<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
|
1249 |
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
|
1250 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1251 |
lemma xor_nat_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1252 |
\<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
|
1253 |
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
|
1254 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1255 |
lemma Suc_0_and_eq [simp]: |
71822 | 1256 |
\<open>Suc 0 AND n = n mod 2\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1257 |
using one_and_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1258 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1259 |
lemma and_Suc_0_eq [simp]: |
71822 | 1260 |
\<open>n AND Suc 0 = n mod 2\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1261 |
using and_one_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1262 |
|
71822 | 1263 |
lemma Suc_0_or_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1264 |
\<open>Suc 0 OR n = n + of_bool (even n)\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1265 |
using one_or_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1266 |
|
71822 | 1267 |
lemma or_Suc_0_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1268 |
\<open>n OR Suc 0 = n + of_bool (even n)\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1269 |
using or_one_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1270 |
|
71822 | 1271 |
lemma Suc_0_xor_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1272 |
\<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
|
1273 |
using one_xor_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1274 |
|
71822 | 1275 |
lemma xor_Suc_0_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1276 |
\<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
|
1277 |
using xor_one_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1278 |
|
72227 | 1279 |
context semiring_bit_operations |
1280 |
begin |
|
1281 |
||
1282 |
lemma of_nat_and_eq: |
|
1283 |
\<open>of_nat (m AND n) = of_nat m AND of_nat n\<close> |
|
1284 |
by (rule bit_eqI) (simp add: bit_of_nat_iff bit_and_iff Bit_Operations.bit_and_iff) |
|
1285 |
||
1286 |
lemma of_nat_or_eq: |
|
1287 |
\<open>of_nat (m OR n) = of_nat m OR of_nat n\<close> |
|
1288 |
by (rule bit_eqI) (simp add: bit_of_nat_iff bit_or_iff Bit_Operations.bit_or_iff) |
|
1289 |
||
1290 |
lemma of_nat_xor_eq: |
|
1291 |
\<open>of_nat (m XOR n) = of_nat m XOR of_nat n\<close> |
|
1292 |
by (rule bit_eqI) (simp add: bit_of_nat_iff bit_xor_iff Bit_Operations.bit_xor_iff) |
|
1293 |
||
1294 |
end |
|
1295 |
||
1296 |
context ring_bit_operations |
|
1297 |
begin |
|
1298 |
||
1299 |
lemma of_nat_mask_eq: |
|
1300 |
\<open>of_nat (mask n) = mask n\<close> |
|
1301 |
by (induction n) (simp_all add: mask_Suc_double Bit_Operations.mask_Suc_double of_nat_or_eq) |
|
1302 |
||
1303 |
end |
|
1304 |
||
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1305 |
|
71956 | 1306 |
subsection \<open>Instances for \<^typ>\<open>integer\<close> and \<^typ>\<open>natural\<close>\<close> |
71442 | 1307 |
|
1308 |
unbundle integer.lifting natural.lifting |
|
1309 |
||
1310 |
instantiation integer :: ring_bit_operations |
|
1311 |
begin |
|
1312 |
||
1313 |
lift_definition not_integer :: \<open>integer \<Rightarrow> integer\<close> |
|
1314 |
is not . |
|
1315 |
||
1316 |
lift_definition and_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close> |
|
1317 |
is \<open>and\<close> . |
|
1318 |
||
1319 |
lift_definition or_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close> |
|
1320 |
is or . |
|
1321 |
||
1322 |
lift_definition xor_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close> |
|
1323 |
is xor . |
|
1324 |
||
72082 | 1325 |
lift_definition mask_integer :: \<open>nat \<Rightarrow> integer\<close> |
1326 |
is mask . |
|
1327 |
||
1328 |
instance by (standard; transfer) |
|
1329 |
(simp_all add: minus_eq_not_minus_1 mask_eq_exp_minus_1 |
|
1330 |
bit_not_iff bit_and_iff bit_or_iff bit_xor_iff) |
|
71442 | 1331 |
|
1332 |
end |
|
1333 |
||
72083
3ec876181527
further refinement of code equations for mask operation
haftmann
parents:
72082
diff
changeset
|
1334 |
lemma [code]: |
3ec876181527
further refinement of code equations for mask operation
haftmann
parents:
72082
diff
changeset
|
1335 |
\<open>mask n = 2 ^ n - (1::integer)\<close> |
3ec876181527
further refinement of code equations for mask operation
haftmann
parents:
72082
diff
changeset
|
1336 |
by (simp add: mask_eq_exp_minus_1) |
3ec876181527
further refinement of code equations for mask operation
haftmann
parents:
72082
diff
changeset
|
1337 |
|
71442 | 1338 |
instantiation natural :: semiring_bit_operations |
1339 |
begin |
|
1340 |
||
1341 |
lift_definition and_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close> |
|
1342 |
is \<open>and\<close> . |
|
1343 |
||
1344 |
lift_definition or_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close> |
|
1345 |
is or . |
|
1346 |
||
1347 |
lift_definition xor_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close> |
|
1348 |
is xor . |
|
1349 |
||
72082 | 1350 |
lift_definition mask_natural :: \<open>nat \<Rightarrow> natural\<close> |
1351 |
is mask . |
|
1352 |
||
1353 |
instance by (standard; transfer) |
|
1354 |
(simp_all add: mask_eq_exp_minus_1 bit_and_iff bit_or_iff bit_xor_iff) |
|
71442 | 1355 |
|
1356 |
end |
|
1357 |
||
72083
3ec876181527
further refinement of code equations for mask operation
haftmann
parents:
72082
diff
changeset
|
1358 |
lemma [code]: |
3ec876181527
further refinement of code equations for mask operation
haftmann
parents:
72082
diff
changeset
|
1359 |
\<open>integer_of_natural (mask n) = mask n\<close> |
3ec876181527
further refinement of code equations for mask operation
haftmann
parents:
72082
diff
changeset
|
1360 |
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
|
1361 |
|
71442 | 1362 |
lifting_update integer.lifting |
1363 |
lifting_forget integer.lifting |
|
1364 |
||
1365 |
lifting_update natural.lifting |
|
1366 |
lifting_forget natural.lifting |
|
1367 |
||
71800 | 1368 |
|
1369 |
subsection \<open>Key ideas of bit operations\<close> |
|
1370 |
||
1371 |
text \<open> |
|
1372 |
When formalizing bit operations, it is tempting to represent |
|
1373 |
bit values as explicit lists over a binary type. This however |
|
1374 |
is a bad idea, mainly due to the inherent ambiguities in |
|
1375 |
representation concerning repeating leading bits. |
|
1376 |
||
1377 |
Hence this approach avoids such explicit lists altogether |
|
1378 |
following an algebraic path: |
|
1379 |
||
1380 |
\<^item> Bit values are represented by numeric types: idealized |
|
1381 |
unbounded bit values can be represented by type \<^typ>\<open>int\<close>, |
|
1382 |
bounded bit values by quotient types over \<^typ>\<open>int\<close>. |
|
1383 |
||
1384 |
\<^item> (A special case are idealized unbounded bit values ending |
|
1385 |
in @{term [source] 0} which can be represented by type \<^typ>\<open>nat\<close> but |
|
1386 |
only support a restricted set of operations). |
|
1387 |
||
1388 |
\<^item> From this idea follows that |
|
1389 |
||
1390 |
\<^item> multiplication by \<^term>\<open>2 :: int\<close> is a bit shift to the left and |
|
1391 |
||
1392 |
\<^item> division by \<^term>\<open>2 :: int\<close> is a bit shift to the right. |
|
1393 |
||
1394 |
\<^item> Concerning bounded bit values, iterated shifts to the left |
|
1395 |
may result in eliminating all bits by shifting them all |
|
1396 |
beyond the boundary. The property \<^prop>\<open>(2 :: int) ^ n \<noteq> 0\<close> |
|
1397 |
represents that \<^term>\<open>n\<close> is \<^emph>\<open>not\<close> beyond that boundary. |
|
1398 |
||
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71956
diff
changeset
|
1399 |
\<^item> The projection on a single bit is then @{thm bit_iff_odd [where ?'a = int, no_vars]}. |
71800 | 1400 |
|
1401 |
\<^item> This leads to the most fundamental properties of bit values: |
|
1402 |
||
1403 |
\<^item> Equality rule: @{thm bit_eqI [where ?'a = int, no_vars]} |
|
1404 |
||
1405 |
\<^item> Induction rule: @{thm bits_induct [where ?'a = int, no_vars]} |
|
1406 |
||
1407 |
\<^item> Typical operations are characterized as follows: |
|
1408 |
||
1409 |
\<^item> Singleton \<^term>\<open>n\<close>th bit: \<^term>\<open>(2 :: int) ^ n\<close> |
|
1410 |
||
71956 | 1411 |
\<^item> Bit mask upto bit \<^term>\<open>n\<close>: @{thm mask_eq_exp_minus_1 [where ?'a = int, no_vars]} |
71800 | 1412 |
|
1413 |
\<^item> Left shift: @{thm push_bit_eq_mult [where ?'a = int, no_vars]} |
|
1414 |
||
1415 |
\<^item> Right shift: @{thm drop_bit_eq_div [where ?'a = int, no_vars]} |
|
1416 |
||
1417 |
\<^item> Truncation: @{thm take_bit_eq_mod [where ?'a = int, no_vars]} |
|
1418 |
||
1419 |
\<^item> Negation: @{thm bit_not_iff [where ?'a = int, no_vars]} |
|
1420 |
||
1421 |
\<^item> And: @{thm bit_and_iff [where ?'a = int, no_vars]} |
|
1422 |
||
1423 |
\<^item> Or: @{thm bit_or_iff [where ?'a = int, no_vars]} |
|
1424 |
||
1425 |
\<^item> Xor: @{thm bit_xor_iff [where ?'a = int, no_vars]} |
|
1426 |
||
1427 |
\<^item> Set a single bit: @{thm set_bit_def [where ?'a = int, no_vars]} |
|
1428 |
||
1429 |
\<^item> Unset a single bit: @{thm unset_bit_def [where ?'a = int, no_vars]} |
|
1430 |
||
1431 |
\<^item> Flip a single bit: @{thm flip_bit_def [where ?'a = int, no_vars]} |
|
72028 | 1432 |
|
72241 | 1433 |
\<^item> Signed truncation, or modulus centered around \<^term>\<open>0::int\<close>: @{thm signed_take_bit_def [no_vars]} |
72028 | 1434 |
|
72241 | 1435 |
\<^item> Bit concatenation: @{thm concat_bit_def [no_vars]} |
72028 | 1436 |
|
1437 |
\<^item> (Bounded) conversion from and to a list of bits: @{thm horner_sum_bit_eq_take_bit [where ?'a = int, no_vars]} |
|
71800 | 1438 |
\<close> |
1439 |
||
71442 | 1440 |
end |