author | haftmann |
Mon, 02 Aug 2021 10:01:06 +0000 | |
changeset 74101 | d804e93ae9ff |
parent 74097 | src/HOL/Library/Bit_Operations.thy@6d7be1227d02 |
child 74108 | 3146646a43a7 |
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 |
74101 | 7 |
imports Presburger Groups_List |
8 |
begin |
|
9 |
||
10 |
subsection \<open>Abstract bit structures\<close> |
|
11 |
||
12 |
class semiring_bits = semiring_parity + |
|
13 |
assumes bits_induct [case_names stable rec]: |
|
14 |
\<open>(\<And>a. a div 2 = a \<Longrightarrow> P a) |
|
15 |
\<Longrightarrow> (\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a)) |
|
16 |
\<Longrightarrow> P a\<close> |
|
17 |
assumes bits_div_0 [simp]: \<open>0 div a = 0\<close> |
|
18 |
and bits_div_by_1 [simp]: \<open>a div 1 = a\<close> |
|
19 |
and bits_mod_div_trivial [simp]: \<open>a mod b div b = 0\<close> |
|
20 |
and even_succ_div_2 [simp]: \<open>even a \<Longrightarrow> (1 + a) div 2 = a div 2\<close> |
|
21 |
and even_mask_div_iff: \<open>even ((2 ^ m - 1) div 2 ^ n) \<longleftrightarrow> 2 ^ n = 0 \<or> m \<le> n\<close> |
|
22 |
and exp_div_exp_eq: \<open>2 ^ m div 2 ^ n = of_bool (2 ^ m \<noteq> 0 \<and> m \<ge> n) * 2 ^ (m - n)\<close> |
|
23 |
and div_exp_eq: \<open>a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)\<close> |
|
24 |
and mod_exp_eq: \<open>a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n\<close> |
|
25 |
and mult_exp_mod_exp_eq: \<open>m \<le> n \<Longrightarrow> (a * 2 ^ m) mod (2 ^ n) = (a mod 2 ^ (n - m)) * 2 ^ m\<close> |
|
26 |
and div_exp_mod_exp_eq: \<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close> |
|
27 |
and even_mult_exp_div_exp_iff: \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> m > n \<or> 2 ^ n = 0 \<or> (m \<le> n \<and> even (a div 2 ^ (n - m)))\<close> |
|
28 |
fixes bit :: \<open>'a \<Rightarrow> nat \<Rightarrow> bool\<close> |
|
29 |
assumes bit_iff_odd: \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close> |
|
30 |
begin |
|
31 |
||
32 |
text \<open> |
|
33 |
Having \<^const>\<open>bit\<close> as definitional class operation |
|
34 |
takes into account that specific instances can be implemented |
|
35 |
differently wrt. code generation. |
|
36 |
\<close> |
|
37 |
||
38 |
lemma bits_div_by_0 [simp]: |
|
39 |
\<open>a div 0 = 0\<close> |
|
40 |
by (metis add_cancel_right_right bits_mod_div_trivial mod_mult_div_eq mult_not_zero) |
|
41 |
||
42 |
lemma bits_1_div_2 [simp]: |
|
43 |
\<open>1 div 2 = 0\<close> |
|
44 |
using even_succ_div_2 [of 0] by simp |
|
45 |
||
46 |
lemma bits_1_div_exp [simp]: |
|
47 |
\<open>1 div 2 ^ n = of_bool (n = 0)\<close> |
|
48 |
using div_exp_eq [of 1 1] by (cases n) simp_all |
|
49 |
||
50 |
lemma even_succ_div_exp [simp]: |
|
51 |
\<open>(1 + a) div 2 ^ n = a div 2 ^ n\<close> if \<open>even a\<close> and \<open>n > 0\<close> |
|
52 |
proof (cases n) |
|
53 |
case 0 |
|
54 |
with that show ?thesis |
|
55 |
by simp |
|
56 |
next |
|
57 |
case (Suc n) |
|
58 |
with \<open>even a\<close> have \<open>(1 + a) div 2 ^ Suc n = a div 2 ^ Suc n\<close> |
|
59 |
proof (induction n) |
|
60 |
case 0 |
|
61 |
then show ?case |
|
62 |
by simp |
|
63 |
next |
|
64 |
case (Suc n) |
|
65 |
then show ?case |
|
66 |
using div_exp_eq [of _ 1 \<open>Suc n\<close>, symmetric] |
|
67 |
by simp |
|
68 |
qed |
|
69 |
with Suc show ?thesis |
|
70 |
by simp |
|
71 |
qed |
|
72 |
||
73 |
lemma even_succ_mod_exp [simp]: |
|
74 |
\<open>(1 + a) mod 2 ^ n = 1 + (a mod 2 ^ n)\<close> if \<open>even a\<close> and \<open>n > 0\<close> |
|
75 |
using div_mult_mod_eq [of \<open>1 + a\<close> \<open>2 ^ n\<close>] that |
|
76 |
apply simp |
|
77 |
by (metis local.add.left_commute local.add_left_cancel local.div_mult_mod_eq) |
|
78 |
||
79 |
lemma bits_mod_by_1 [simp]: |
|
80 |
\<open>a mod 1 = 0\<close> |
|
81 |
using div_mult_mod_eq [of a 1] by simp |
|
82 |
||
83 |
lemma bits_mod_0 [simp]: |
|
84 |
\<open>0 mod a = 0\<close> |
|
85 |
using div_mult_mod_eq [of 0 a] by simp |
|
86 |
||
87 |
lemma bits_one_mod_two_eq_one [simp]: |
|
88 |
\<open>1 mod 2 = 1\<close> |
|
89 |
by (simp add: mod2_eq_if) |
|
90 |
||
91 |
lemma bit_0 [simp]: |
|
92 |
\<open>bit a 0 \<longleftrightarrow> odd a\<close> |
|
93 |
by (simp add: bit_iff_odd) |
|
94 |
||
95 |
lemma bit_Suc: |
|
96 |
\<open>bit a (Suc n) \<longleftrightarrow> bit (a div 2) n\<close> |
|
97 |
using div_exp_eq [of a 1 n] by (simp add: bit_iff_odd) |
|
98 |
||
99 |
lemma bit_rec: |
|
100 |
\<open>bit a n \<longleftrightarrow> (if n = 0 then odd a else bit (a div 2) (n - 1))\<close> |
|
101 |
by (cases n) (simp_all add: bit_Suc) |
|
102 |
||
103 |
lemma bit_0_eq [simp]: |
|
104 |
\<open>bit 0 = bot\<close> |
|
105 |
by (simp add: fun_eq_iff bit_iff_odd) |
|
106 |
||
107 |
context |
|
108 |
fixes a |
|
109 |
assumes stable: \<open>a div 2 = a\<close> |
|
110 |
begin |
|
111 |
||
112 |
lemma bits_stable_imp_add_self: |
|
113 |
\<open>a + a mod 2 = 0\<close> |
|
114 |
proof - |
|
115 |
have \<open>a div 2 * 2 + a mod 2 = a\<close> |
|
116 |
by (fact div_mult_mod_eq) |
|
117 |
then have \<open>a * 2 + a mod 2 = a\<close> |
|
118 |
by (simp add: stable) |
|
119 |
then show ?thesis |
|
120 |
by (simp add: mult_2_right ac_simps) |
|
121 |
qed |
|
122 |
||
123 |
lemma stable_imp_bit_iff_odd: |
|
124 |
\<open>bit a n \<longleftrightarrow> odd a\<close> |
|
125 |
by (induction n) (simp_all add: stable bit_Suc) |
|
126 |
||
127 |
end |
|
128 |
||
129 |
lemma bit_iff_idd_imp_stable: |
|
130 |
\<open>a div 2 = a\<close> if \<open>\<And>n. bit a n \<longleftrightarrow> odd a\<close> |
|
131 |
using that proof (induction a rule: bits_induct) |
|
132 |
case (stable a) |
|
133 |
then show ?case |
|
134 |
by simp |
|
135 |
next |
|
136 |
case (rec a b) |
|
137 |
from rec.prems [of 1] have [simp]: \<open>b = odd a\<close> |
|
138 |
by (simp add: rec.hyps bit_Suc) |
|
139 |
from rec.hyps have hyp: \<open>(of_bool (odd a) + 2 * a) div 2 = a\<close> |
|
140 |
by simp |
|
141 |
have \<open>bit a n \<longleftrightarrow> odd a\<close> for n |
|
142 |
using rec.prems [of \<open>Suc n\<close>] by (simp add: hyp bit_Suc) |
|
143 |
then have \<open>a div 2 = a\<close> |
|
144 |
by (rule rec.IH) |
|
145 |
then have \<open>of_bool (odd a) + 2 * a = 2 * (a div 2) + of_bool (odd a)\<close> |
|
146 |
by (simp add: ac_simps) |
|
147 |
also have \<open>\<dots> = a\<close> |
|
148 |
using mult_div_mod_eq [of 2 a] |
|
149 |
by (simp add: of_bool_odd_eq_mod_2) |
|
150 |
finally show ?case |
|
151 |
using \<open>a div 2 = a\<close> by (simp add: hyp) |
|
152 |
qed |
|
153 |
||
154 |
lemma exp_eq_0_imp_not_bit: |
|
155 |
\<open>\<not> bit a n\<close> if \<open>2 ^ n = 0\<close> |
|
156 |
using that by (simp add: bit_iff_odd) |
|
157 |
||
158 |
lemma bit_eqI: |
|
159 |
\<open>a = b\<close> if \<open>\<And>n. 2 ^ n \<noteq> 0 \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close> |
|
160 |
proof - |
|
161 |
have \<open>bit a n \<longleftrightarrow> bit b n\<close> for n |
|
162 |
proof (cases \<open>2 ^ n = 0\<close>) |
|
163 |
case True |
|
164 |
then show ?thesis |
|
165 |
by (simp add: exp_eq_0_imp_not_bit) |
|
166 |
next |
|
167 |
case False |
|
168 |
then show ?thesis |
|
169 |
by (rule that) |
|
170 |
qed |
|
171 |
then show ?thesis proof (induction a arbitrary: b rule: bits_induct) |
|
172 |
case (stable a) |
|
173 |
from stable(2) [of 0] have **: \<open>even b \<longleftrightarrow> even a\<close> |
|
174 |
by simp |
|
175 |
have \<open>b div 2 = b\<close> |
|
176 |
proof (rule bit_iff_idd_imp_stable) |
|
177 |
fix n |
|
178 |
from stable have *: \<open>bit b n \<longleftrightarrow> bit a n\<close> |
|
179 |
by simp |
|
180 |
also have \<open>bit a n \<longleftrightarrow> odd a\<close> |
|
181 |
using stable by (simp add: stable_imp_bit_iff_odd) |
|
182 |
finally show \<open>bit b n \<longleftrightarrow> odd b\<close> |
|
183 |
by (simp add: **) |
|
184 |
qed |
|
185 |
from ** have \<open>a mod 2 = b mod 2\<close> |
|
186 |
by (simp add: mod2_eq_if) |
|
187 |
then have \<open>a mod 2 + (a + b) = b mod 2 + (a + b)\<close> |
|
188 |
by simp |
|
189 |
then have \<open>a + a mod 2 + b = b + b mod 2 + a\<close> |
|
190 |
by (simp add: ac_simps) |
|
191 |
with \<open>a div 2 = a\<close> \<open>b div 2 = b\<close> show ?case |
|
192 |
by (simp add: bits_stable_imp_add_self) |
|
193 |
next |
|
194 |
case (rec a p) |
|
195 |
from rec.prems [of 0] have [simp]: \<open>p = odd b\<close> |
|
196 |
by simp |
|
197 |
from rec.hyps have \<open>bit a n \<longleftrightarrow> bit (b div 2) n\<close> for n |
|
198 |
using rec.prems [of \<open>Suc n\<close>] by (simp add: bit_Suc) |
|
199 |
then have \<open>a = b div 2\<close> |
|
200 |
by (rule rec.IH) |
|
201 |
then have \<open>2 * a = 2 * (b div 2)\<close> |
|
202 |
by simp |
|
203 |
then have \<open>b mod 2 + 2 * a = b mod 2 + 2 * (b div 2)\<close> |
|
204 |
by simp |
|
205 |
also have \<open>\<dots> = b\<close> |
|
206 |
by (fact mod_mult_div_eq) |
|
207 |
finally show ?case |
|
208 |
by (auto simp add: mod2_eq_if) |
|
209 |
qed |
|
210 |
qed |
|
211 |
||
212 |
lemma bit_eq_iff: |
|
213 |
\<open>a = b \<longleftrightarrow> (\<forall>n. bit a n \<longleftrightarrow> bit b n)\<close> |
|
214 |
by (auto intro: bit_eqI) |
|
215 |
||
216 |
named_theorems bit_simps \<open>Simplification rules for \<^const>\<open>bit\<close>\<close> |
|
217 |
||
218 |
lemma bit_exp_iff [bit_simps]: |
|
219 |
\<open>bit (2 ^ m) n \<longleftrightarrow> 2 ^ m \<noteq> 0 \<and> m = n\<close> |
|
220 |
by (auto simp add: bit_iff_odd exp_div_exp_eq) |
|
221 |
||
222 |
lemma bit_1_iff [bit_simps]: |
|
223 |
\<open>bit 1 n \<longleftrightarrow> 1 \<noteq> 0 \<and> n = 0\<close> |
|
224 |
using bit_exp_iff [of 0 n] by simp |
|
225 |
||
226 |
lemma bit_2_iff [bit_simps]: |
|
227 |
\<open>bit 2 n \<longleftrightarrow> 2 \<noteq> 0 \<and> n = 1\<close> |
|
228 |
using bit_exp_iff [of 1 n] by auto |
|
229 |
||
230 |
lemma even_bit_succ_iff: |
|
231 |
\<open>bit (1 + a) n \<longleftrightarrow> bit a n \<or> n = 0\<close> if \<open>even a\<close> |
|
232 |
using that by (cases \<open>n = 0\<close>) (simp_all add: bit_iff_odd) |
|
233 |
||
234 |
lemma odd_bit_iff_bit_pred: |
|
235 |
\<open>bit a n \<longleftrightarrow> bit (a - 1) n \<or> n = 0\<close> if \<open>odd a\<close> |
|
236 |
proof - |
|
237 |
from \<open>odd a\<close> obtain b where \<open>a = 2 * b + 1\<close> .. |
|
238 |
moreover have \<open>bit (2 * b) n \<or> n = 0 \<longleftrightarrow> bit (1 + 2 * b) n\<close> |
|
239 |
using even_bit_succ_iff by simp |
|
240 |
ultimately show ?thesis by (simp add: ac_simps) |
|
241 |
qed |
|
242 |
||
243 |
lemma bit_double_iff [bit_simps]: |
|
244 |
\<open>bit (2 * a) n \<longleftrightarrow> bit a (n - 1) \<and> n \<noteq> 0 \<and> 2 ^ n \<noteq> 0\<close> |
|
245 |
using even_mult_exp_div_exp_iff [of a 1 n] |
|
246 |
by (cases n, auto simp add: bit_iff_odd ac_simps) |
|
247 |
||
248 |
lemma bit_eq_rec: |
|
249 |
\<open>a = b \<longleftrightarrow> (even a \<longleftrightarrow> even b) \<and> a div 2 = b div 2\<close> (is \<open>?P = ?Q\<close>) |
|
250 |
proof |
|
251 |
assume ?P |
|
252 |
then show ?Q |
|
253 |
by simp |
|
254 |
next |
|
255 |
assume ?Q |
|
256 |
then have \<open>even a \<longleftrightarrow> even b\<close> and \<open>a div 2 = b div 2\<close> |
|
257 |
by simp_all |
|
258 |
show ?P |
|
259 |
proof (rule bit_eqI) |
|
260 |
fix n |
|
261 |
show \<open>bit a n \<longleftrightarrow> bit b n\<close> |
|
262 |
proof (cases n) |
|
263 |
case 0 |
|
264 |
with \<open>even a \<longleftrightarrow> even b\<close> show ?thesis |
|
265 |
by simp |
|
266 |
next |
|
267 |
case (Suc n) |
|
268 |
moreover from \<open>a div 2 = b div 2\<close> have \<open>bit (a div 2) n = bit (b div 2) n\<close> |
|
269 |
by simp |
|
270 |
ultimately show ?thesis |
|
271 |
by (simp add: bit_Suc) |
|
272 |
qed |
|
273 |
qed |
|
274 |
qed |
|
275 |
||
276 |
lemma bit_mod_2_iff [simp]: |
|
277 |
\<open>bit (a mod 2) n \<longleftrightarrow> n = 0 \<and> odd a\<close> |
|
278 |
by (cases a rule: parity_cases) (simp_all add: bit_iff_odd) |
|
279 |
||
280 |
lemma bit_mask_iff: |
|
281 |
\<open>bit (2 ^ m - 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n < m\<close> |
|
282 |
by (simp add: bit_iff_odd even_mask_div_iff not_le) |
|
283 |
||
284 |
lemma bit_Numeral1_iff [simp]: |
|
285 |
\<open>bit (numeral Num.One) n \<longleftrightarrow> n = 0\<close> |
|
286 |
by (simp add: bit_rec) |
|
287 |
||
288 |
lemma exp_add_not_zero_imp: |
|
289 |
\<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close> if \<open>2 ^ (m + n) \<noteq> 0\<close> |
|
290 |
proof - |
|
291 |
have \<open>\<not> (2 ^ m = 0 \<or> 2 ^ n = 0)\<close> |
|
292 |
proof (rule notI) |
|
293 |
assume \<open>2 ^ m = 0 \<or> 2 ^ n = 0\<close> |
|
294 |
then have \<open>2 ^ (m + n) = 0\<close> |
|
295 |
by (rule disjE) (simp_all add: power_add) |
|
296 |
with that show False .. |
|
297 |
qed |
|
298 |
then show \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close> |
|
299 |
by simp_all |
|
300 |
qed |
|
301 |
||
302 |
lemma bit_disjunctive_add_iff: |
|
303 |
\<open>bit (a + b) n \<longleftrightarrow> bit a n \<or> bit b n\<close> |
|
304 |
if \<open>\<And>n. \<not> bit a n \<or> \<not> bit b n\<close> |
|
305 |
proof (cases \<open>2 ^ n = 0\<close>) |
|
306 |
case True |
|
307 |
then show ?thesis |
|
308 |
by (simp add: exp_eq_0_imp_not_bit) |
|
309 |
next |
|
310 |
case False |
|
311 |
with that show ?thesis proof (induction n arbitrary: a b) |
|
312 |
case 0 |
|
313 |
from "0.prems"(1) [of 0] show ?case |
|
314 |
by auto |
|
315 |
next |
|
316 |
case (Suc n) |
|
317 |
from Suc.prems(1) [of 0] have even: \<open>even a \<or> even b\<close> |
|
318 |
by auto |
|
319 |
have bit: \<open>\<not> bit (a div 2) n \<or> \<not> bit (b div 2) n\<close> for n |
|
320 |
using Suc.prems(1) [of \<open>Suc n\<close>] by (simp add: bit_Suc) |
|
321 |
from Suc.prems(2) have \<open>2 * 2 ^ n \<noteq> 0\<close> \<open>2 ^ n \<noteq> 0\<close> |
|
322 |
by (auto simp add: mult_2) |
|
323 |
have \<open>a + b = (a div 2 * 2 + a mod 2) + (b div 2 * 2 + b mod 2)\<close> |
|
324 |
using div_mult_mod_eq [of a 2] div_mult_mod_eq [of b 2] by simp |
|
325 |
also have \<open>\<dots> = of_bool (odd a \<or> odd b) + 2 * (a div 2 + b div 2)\<close> |
|
326 |
using even by (auto simp add: algebra_simps mod2_eq_if) |
|
327 |
finally have \<open>bit ((a + b) div 2) n \<longleftrightarrow> bit (a div 2 + b div 2) n\<close> |
|
328 |
using \<open>2 * 2 ^ n \<noteq> 0\<close> by simp (simp_all flip: bit_Suc add: bit_double_iff) |
|
329 |
also have \<open>\<dots> \<longleftrightarrow> bit (a div 2) n \<or> bit (b div 2) n\<close> |
|
330 |
using bit \<open>2 ^ n \<noteq> 0\<close> by (rule Suc.IH) |
|
331 |
finally show ?case |
|
332 |
by (simp add: bit_Suc) |
|
333 |
qed |
|
334 |
qed |
|
335 |
||
336 |
lemma |
|
337 |
exp_add_not_zero_imp_left: \<open>2 ^ m \<noteq> 0\<close> |
|
338 |
and exp_add_not_zero_imp_right: \<open>2 ^ n \<noteq> 0\<close> |
|
339 |
if \<open>2 ^ (m + n) \<noteq> 0\<close> |
|
340 |
proof - |
|
341 |
have \<open>\<not> (2 ^ m = 0 \<or> 2 ^ n = 0)\<close> |
|
342 |
proof (rule notI) |
|
343 |
assume \<open>2 ^ m = 0 \<or> 2 ^ n = 0\<close> |
|
344 |
then have \<open>2 ^ (m + n) = 0\<close> |
|
345 |
by (rule disjE) (simp_all add: power_add) |
|
346 |
with that show False .. |
|
347 |
qed |
|
348 |
then show \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close> |
|
349 |
by simp_all |
|
350 |
qed |
|
351 |
||
352 |
lemma exp_not_zero_imp_exp_diff_not_zero: |
|
353 |
\<open>2 ^ (n - m) \<noteq> 0\<close> if \<open>2 ^ n \<noteq> 0\<close> |
|
354 |
proof (cases \<open>m \<le> n\<close>) |
|
355 |
case True |
|
356 |
moreover define q where \<open>q = n - m\<close> |
|
357 |
ultimately have \<open>n = m + q\<close> |
|
358 |
by simp |
|
359 |
with that show ?thesis |
|
360 |
by (simp add: exp_add_not_zero_imp_right) |
|
361 |
next |
|
362 |
case False |
|
363 |
with that show ?thesis |
|
364 |
by simp |
|
365 |
qed |
|
366 |
||
367 |
end |
|
368 |
||
369 |
lemma nat_bit_induct [case_names zero even odd]: |
|
370 |
"P n" if zero: "P 0" |
|
371 |
and even: "\<And>n. P n \<Longrightarrow> n > 0 \<Longrightarrow> P (2 * n)" |
|
372 |
and odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))" |
|
373 |
proof (induction n rule: less_induct) |
|
374 |
case (less n) |
|
375 |
show "P n" |
|
376 |
proof (cases "n = 0") |
|
377 |
case True with zero show ?thesis by simp |
|
378 |
next |
|
379 |
case False |
|
380 |
with less have hyp: "P (n div 2)" by simp |
|
381 |
show ?thesis |
|
382 |
proof (cases "even n") |
|
383 |
case True |
|
384 |
then have "n \<noteq> 1" |
|
385 |
by auto |
|
386 |
with \<open>n \<noteq> 0\<close> have "n div 2 > 0" |
|
387 |
by simp |
|
388 |
with \<open>even n\<close> hyp even [of "n div 2"] show ?thesis |
|
389 |
by simp |
|
390 |
next |
|
391 |
case False |
|
392 |
with hyp odd [of "n div 2"] show ?thesis |
|
393 |
by simp |
|
394 |
qed |
|
395 |
qed |
|
396 |
qed |
|
397 |
||
398 |
instantiation nat :: semiring_bits |
|
399 |
begin |
|
400 |
||
401 |
definition bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> bool\<close> |
|
402 |
where \<open>bit_nat m n \<longleftrightarrow> odd (m div 2 ^ n)\<close> |
|
403 |
||
404 |
instance |
|
405 |
proof |
|
406 |
show \<open>P n\<close> if stable: \<open>\<And>n. n div 2 = n \<Longrightarrow> P n\<close> |
|
407 |
and rec: \<open>\<And>n b. P n \<Longrightarrow> (of_bool b + 2 * n) div 2 = n \<Longrightarrow> P (of_bool b + 2 * n)\<close> |
|
408 |
for P and n :: nat |
|
409 |
proof (induction n rule: nat_bit_induct) |
|
410 |
case zero |
|
411 |
from stable [of 0] show ?case |
|
412 |
by simp |
|
413 |
next |
|
414 |
case (even n) |
|
415 |
with rec [of n False] show ?case |
|
416 |
by simp |
|
417 |
next |
|
418 |
case (odd n) |
|
419 |
with rec [of n True] show ?case |
|
420 |
by simp |
|
421 |
qed |
|
422 |
show \<open>q mod 2 ^ m mod 2 ^ n = q mod 2 ^ min m n\<close> |
|
423 |
for q m n :: nat |
|
424 |
apply (auto simp add: less_iff_Suc_add power_add mod_mod_cancel split: split_min_lin) |
|
425 |
apply (metis div_mult2_eq mod_div_trivial mod_eq_self_iff_div_eq_0 mod_mult_self2_is_0 power_commutes) |
|
426 |
done |
|
427 |
show \<open>(q * 2 ^ m) mod (2 ^ n) = (q mod 2 ^ (n - m)) * 2 ^ m\<close> if \<open>m \<le> n\<close> |
|
428 |
for q m n :: nat |
|
429 |
using that |
|
430 |
apply (auto simp add: mod_mod_cancel div_mult2_eq power_add mod_mult2_eq le_iff_add split: split_min_lin) |
|
431 |
done |
|
432 |
show \<open>even ((2 ^ m - (1::nat)) div 2 ^ n) \<longleftrightarrow> 2 ^ n = (0::nat) \<or> m \<le> n\<close> |
|
433 |
for m n :: nat |
|
434 |
using even_mask_div_iff' [where ?'a = nat, of m n] by simp |
|
435 |
show \<open>even (q * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::nat) ^ n = 0 \<or> m \<le> n \<and> even (q div 2 ^ (n - m))\<close> |
|
436 |
for m n q r :: nat |
|
437 |
apply (auto simp add: not_less power_add ac_simps dest!: le_Suc_ex) |
|
438 |
apply (metis (full_types) dvd_mult dvd_mult_imp_div dvd_power_iff_le not_less not_less_eq order_refl power_Suc) |
|
439 |
done |
|
440 |
qed (auto simp add: div_mult2_eq mod_mult2_eq power_add power_diff bit_nat_def) |
|
441 |
||
442 |
end |
|
443 |
||
444 |
lemma int_bit_induct [case_names zero minus even odd]: |
|
445 |
"P k" if zero_int: "P 0" |
|
446 |
and minus_int: "P (- 1)" |
|
447 |
and even_int: "\<And>k. P k \<Longrightarrow> k \<noteq> 0 \<Longrightarrow> P (k * 2)" |
|
448 |
and odd_int: "\<And>k. P k \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> P (1 + (k * 2))" for k :: int |
|
449 |
proof (cases "k \<ge> 0") |
|
450 |
case True |
|
451 |
define n where "n = nat k" |
|
452 |
with True have "k = int n" |
|
453 |
by simp |
|
454 |
then show "P k" |
|
455 |
proof (induction n arbitrary: k rule: nat_bit_induct) |
|
456 |
case zero |
|
457 |
then show ?case |
|
458 |
by (simp add: zero_int) |
|
459 |
next |
|
460 |
case (even n) |
|
461 |
have "P (int n * 2)" |
|
462 |
by (rule even_int) (use even in simp_all) |
|
463 |
with even show ?case |
|
464 |
by (simp add: ac_simps) |
|
465 |
next |
|
466 |
case (odd n) |
|
467 |
have "P (1 + (int n * 2))" |
|
468 |
by (rule odd_int) (use odd in simp_all) |
|
469 |
with odd show ?case |
|
470 |
by (simp add: ac_simps) |
|
471 |
qed |
|
472 |
next |
|
473 |
case False |
|
474 |
define n where "n = nat (- k - 1)" |
|
475 |
with False have "k = - int n - 1" |
|
476 |
by simp |
|
477 |
then show "P k" |
|
478 |
proof (induction n arbitrary: k rule: nat_bit_induct) |
|
479 |
case zero |
|
480 |
then show ?case |
|
481 |
by (simp add: minus_int) |
|
482 |
next |
|
483 |
case (even n) |
|
484 |
have "P (1 + (- int (Suc n) * 2))" |
|
485 |
by (rule odd_int) (use even in \<open>simp_all add: algebra_simps\<close>) |
|
486 |
also have "\<dots> = - int (2 * n) - 1" |
|
487 |
by (simp add: algebra_simps) |
|
488 |
finally show ?case |
|
489 |
using even.prems by simp |
|
490 |
next |
|
491 |
case (odd n) |
|
492 |
have "P (- int (Suc n) * 2)" |
|
493 |
by (rule even_int) (use odd in \<open>simp_all add: algebra_simps\<close>) |
|
494 |
also have "\<dots> = - int (Suc (2 * n)) - 1" |
|
495 |
by (simp add: algebra_simps) |
|
496 |
finally show ?case |
|
497 |
using odd.prems by simp |
|
498 |
qed |
|
499 |
qed |
|
500 |
||
501 |
context semiring_bits |
|
502 |
begin |
|
503 |
||
504 |
lemma bit_of_bool_iff [bit_simps]: |
|
505 |
\<open>bit (of_bool b) n \<longleftrightarrow> b \<and> n = 0\<close> |
|
506 |
by (simp add: bit_1_iff) |
|
507 |
||
508 |
lemma even_of_nat_iff: |
|
509 |
\<open>even (of_nat n) \<longleftrightarrow> even n\<close> |
|
510 |
by (induction n rule: nat_bit_induct) simp_all |
|
511 |
||
512 |
lemma bit_of_nat_iff [bit_simps]: |
|
513 |
\<open>bit (of_nat m) n \<longleftrightarrow> (2::'a) ^ n \<noteq> 0 \<and> bit m n\<close> |
|
514 |
proof (cases \<open>(2::'a) ^ n = 0\<close>) |
|
515 |
case True |
|
516 |
then show ?thesis |
|
517 |
by (simp add: exp_eq_0_imp_not_bit) |
|
518 |
next |
|
519 |
case False |
|
520 |
then have \<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close> |
|
521 |
proof (induction m arbitrary: n rule: nat_bit_induct) |
|
522 |
case zero |
|
523 |
then show ?case |
|
524 |
by simp |
|
525 |
next |
|
526 |
case (even m) |
|
527 |
then show ?case |
|
528 |
by (cases n) |
|
529 |
(auto simp add: bit_double_iff Bit_Operations.bit_double_iff dest: mult_not_zero) |
|
530 |
next |
|
531 |
case (odd m) |
|
532 |
then show ?case |
|
533 |
by (cases n) |
|
534 |
(auto simp add: bit_double_iff even_bit_succ_iff Bit_Operations.bit_Suc dest: mult_not_zero) |
|
535 |
qed |
|
536 |
with False show ?thesis |
|
537 |
by simp |
|
538 |
qed |
|
539 |
||
540 |
end |
|
541 |
||
542 |
instantiation int :: semiring_bits |
|
543 |
begin |
|
544 |
||
545 |
definition bit_int :: \<open>int \<Rightarrow> nat \<Rightarrow> bool\<close> |
|
546 |
where \<open>bit_int k n \<longleftrightarrow> odd (k div 2 ^ n)\<close> |
|
547 |
||
548 |
instance |
|
549 |
proof |
|
550 |
show \<open>P k\<close> if stable: \<open>\<And>k. k div 2 = k \<Longrightarrow> P k\<close> |
|
551 |
and rec: \<open>\<And>k b. P k \<Longrightarrow> (of_bool b + 2 * k) div 2 = k \<Longrightarrow> P (of_bool b + 2 * k)\<close> |
|
552 |
for P and k :: int |
|
553 |
proof (induction k rule: int_bit_induct) |
|
554 |
case zero |
|
555 |
from stable [of 0] show ?case |
|
556 |
by simp |
|
557 |
next |
|
558 |
case minus |
|
559 |
from stable [of \<open>- 1\<close>] show ?case |
|
560 |
by simp |
|
561 |
next |
|
562 |
case (even k) |
|
563 |
with rec [of k False] show ?case |
|
564 |
by (simp add: ac_simps) |
|
565 |
next |
|
566 |
case (odd k) |
|
567 |
with rec [of k True] show ?case |
|
568 |
by (simp add: ac_simps) |
|
569 |
qed |
|
570 |
show \<open>(2::int) ^ m div 2 ^ n = of_bool ((2::int) ^ m \<noteq> 0 \<and> n \<le> m) * 2 ^ (m - n)\<close> |
|
571 |
for m n :: nat |
|
572 |
proof (cases \<open>m < n\<close>) |
|
573 |
case True |
|
574 |
then have \<open>n = m + (n - m)\<close> |
|
575 |
by simp |
|
576 |
then have \<open>(2::int) ^ m div 2 ^ n = (2::int) ^ m div 2 ^ (m + (n - m))\<close> |
|
577 |
by simp |
|
578 |
also have \<open>\<dots> = (2::int) ^ m div (2 ^ m * 2 ^ (n - m))\<close> |
|
579 |
by (simp add: power_add) |
|
580 |
also have \<open>\<dots> = (2::int) ^ m div 2 ^ m div 2 ^ (n - m)\<close> |
|
581 |
by (simp add: zdiv_zmult2_eq) |
|
582 |
finally show ?thesis using \<open>m < n\<close> by simp |
|
583 |
next |
|
584 |
case False |
|
585 |
then show ?thesis |
|
586 |
by (simp add: power_diff) |
|
587 |
qed |
|
588 |
show \<open>k mod 2 ^ m mod 2 ^ n = k mod 2 ^ min m n\<close> |
|
589 |
for m n :: nat and k :: int |
|
590 |
using mod_exp_eq [of \<open>nat k\<close> m n] |
|
591 |
apply (auto simp add: mod_mod_cancel zdiv_zmult2_eq power_add zmod_zmult2_eq le_iff_add split: split_min_lin) |
|
592 |
apply (auto simp add: less_iff_Suc_add mod_mod_cancel power_add) |
|
593 |
apply (simp only: flip: mult.left_commute [of \<open>2 ^ m\<close>]) |
|
594 |
apply (subst zmod_zmult2_eq) apply simp_all |
|
595 |
done |
|
596 |
show \<open>(k * 2 ^ m) mod (2 ^ n) = (k mod 2 ^ (n - m)) * 2 ^ m\<close> |
|
597 |
if \<open>m \<le> n\<close> for m n :: nat and k :: int |
|
598 |
using that |
|
599 |
apply (auto simp add: power_add zmod_zmult2_eq le_iff_add split: split_min_lin) |
|
600 |
done |
|
601 |
show \<open>even ((2 ^ m - (1::int)) div 2 ^ n) \<longleftrightarrow> 2 ^ n = (0::int) \<or> m \<le> n\<close> |
|
602 |
for m n :: nat |
|
603 |
using even_mask_div_iff' [where ?'a = int, of m n] by simp |
|
604 |
show \<open>even (k * 2 ^ m div 2 ^ n) \<longleftrightarrow> n < m \<or> (2::int) ^ n = 0 \<or> m \<le> n \<and> even (k div 2 ^ (n - m))\<close> |
|
605 |
for m n :: nat and k l :: int |
|
606 |
apply (auto simp add: not_less power_add ac_simps dest!: le_Suc_ex) |
|
607 |
apply (metis Suc_leI dvd_mult dvd_mult_imp_div dvd_power_le dvd_refl power.simps(2)) |
|
608 |
done |
|
609 |
qed (auto simp add: zdiv_zmult2_eq zmod_zmult2_eq power_add power_diff not_le bit_int_def) |
|
610 |
||
611 |
end |
|
612 |
||
613 |
class semiring_bit_shifts = semiring_bits + |
|
614 |
fixes push_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
615 |
assumes push_bit_eq_mult: \<open>push_bit n a = a * 2 ^ n\<close> |
|
616 |
fixes drop_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
617 |
assumes drop_bit_eq_div: \<open>drop_bit n a = a div 2 ^ n\<close> |
|
618 |
fixes take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
619 |
assumes take_bit_eq_mod: \<open>take_bit n a = a mod 2 ^ n\<close> |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
620 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
621 |
|
74101 | 622 |
text \<open> |
623 |
Logically, \<^const>\<open>push_bit\<close>, |
|
624 |
\<^const>\<open>drop_bit\<close> and \<^const>\<open>take_bit\<close> are just aliases; having them |
|
625 |
as separate operations makes proofs easier, otherwise proof automation |
|
626 |
would fiddle with concrete expressions \<^term>\<open>2 ^ n\<close> in a way obfuscating the basic |
|
627 |
algebraic relationships between those operations. |
|
628 |
Having |
|
629 |
them as definitional class operations |
|
630 |
takes into account that specific instances of these can be implemented |
|
631 |
differently wrt. code generation. |
|
632 |
\<close> |
|
633 |
||
634 |
lemma bit_iff_odd_drop_bit: |
|
635 |
\<open>bit a n \<longleftrightarrow> odd (drop_bit n a)\<close> |
|
636 |
by (simp add: bit_iff_odd drop_bit_eq_div) |
|
637 |
||
638 |
lemma even_drop_bit_iff_not_bit: |
|
639 |
\<open>even (drop_bit n a) \<longleftrightarrow> \<not> bit a n\<close> |
|
640 |
by (simp add: bit_iff_odd_drop_bit) |
|
641 |
||
642 |
lemma div_push_bit_of_1_eq_drop_bit: |
|
643 |
\<open>a div push_bit n 1 = drop_bit n a\<close> |
|
644 |
by (simp add: push_bit_eq_mult drop_bit_eq_div) |
|
645 |
||
646 |
lemma bits_ident: |
|
647 |
"push_bit n (drop_bit n a) + take_bit n a = a" |
|
648 |
using div_mult_mod_eq by (simp add: push_bit_eq_mult take_bit_eq_mod drop_bit_eq_div) |
|
649 |
||
650 |
lemma push_bit_push_bit [simp]: |
|
651 |
"push_bit m (push_bit n a) = push_bit (m + n) a" |
|
652 |
by (simp add: push_bit_eq_mult power_add ac_simps) |
|
653 |
||
654 |
lemma push_bit_0_id [simp]: |
|
655 |
"push_bit 0 = id" |
|
656 |
by (simp add: fun_eq_iff push_bit_eq_mult) |
|
657 |
||
658 |
lemma push_bit_of_0 [simp]: |
|
659 |
"push_bit n 0 = 0" |
|
660 |
by (simp add: push_bit_eq_mult) |
|
661 |
||
662 |
lemma push_bit_of_1: |
|
663 |
"push_bit n 1 = 2 ^ n" |
|
664 |
by (simp add: push_bit_eq_mult) |
|
665 |
||
666 |
lemma push_bit_Suc [simp]: |
|
667 |
"push_bit (Suc n) a = push_bit n (a * 2)" |
|
668 |
by (simp add: push_bit_eq_mult ac_simps) |
|
669 |
||
670 |
lemma push_bit_double: |
|
671 |
"push_bit n (a * 2) = push_bit n a * 2" |
|
672 |
by (simp add: push_bit_eq_mult ac_simps) |
|
673 |
||
674 |
lemma push_bit_add: |
|
675 |
"push_bit n (a + b) = push_bit n a + push_bit n b" |
|
676 |
by (simp add: push_bit_eq_mult algebra_simps) |
|
677 |
||
678 |
lemma push_bit_numeral [simp]: |
|
679 |
\<open>push_bit (numeral l) (numeral k) = push_bit (pred_numeral l) (numeral (Num.Bit0 k))\<close> |
|
680 |
by (simp add: numeral_eq_Suc mult_2_right) (simp add: numeral_Bit0) |
|
681 |
||
682 |
lemma take_bit_0 [simp]: |
|
683 |
"take_bit 0 a = 0" |
|
684 |
by (simp add: take_bit_eq_mod) |
|
685 |
||
686 |
lemma take_bit_Suc: |
|
687 |
\<open>take_bit (Suc n) a = take_bit n (a div 2) * 2 + a mod 2\<close> |
|
688 |
proof - |
|
689 |
have \<open>take_bit (Suc n) (a div 2 * 2 + of_bool (odd a)) = take_bit n (a div 2) * 2 + of_bool (odd a)\<close> |
|
690 |
using even_succ_mod_exp [of \<open>2 * (a div 2)\<close> \<open>Suc n\<close>] |
|
691 |
mult_exp_mod_exp_eq [of 1 \<open>Suc n\<close> \<open>a div 2\<close>] |
|
692 |
by (auto simp add: take_bit_eq_mod ac_simps) |
|
693 |
then show ?thesis |
|
694 |
using div_mult_mod_eq [of a 2] by (simp add: mod_2_eq_odd) |
|
695 |
qed |
|
696 |
||
697 |
lemma take_bit_rec: |
|
698 |
\<open>take_bit n a = (if n = 0 then 0 else take_bit (n - 1) (a div 2) * 2 + a mod 2)\<close> |
|
699 |
by (cases n) (simp_all add: take_bit_Suc) |
|
700 |
||
701 |
lemma take_bit_Suc_0 [simp]: |
|
702 |
\<open>take_bit (Suc 0) a = a mod 2\<close> |
|
703 |
by (simp add: take_bit_eq_mod) |
|
704 |
||
705 |
lemma take_bit_of_0 [simp]: |
|
706 |
"take_bit n 0 = 0" |
|
707 |
by (simp add: take_bit_eq_mod) |
|
708 |
||
709 |
lemma take_bit_of_1 [simp]: |
|
710 |
"take_bit n 1 = of_bool (n > 0)" |
|
711 |
by (cases n) (simp_all add: take_bit_Suc) |
|
712 |
||
713 |
lemma drop_bit_of_0 [simp]: |
|
714 |
"drop_bit n 0 = 0" |
|
715 |
by (simp add: drop_bit_eq_div) |
|
716 |
||
717 |
lemma drop_bit_of_1 [simp]: |
|
718 |
"drop_bit n 1 = of_bool (n = 0)" |
|
719 |
by (simp add: drop_bit_eq_div) |
|
720 |
||
721 |
lemma drop_bit_0 [simp]: |
|
722 |
"drop_bit 0 = id" |
|
723 |
by (simp add: fun_eq_iff drop_bit_eq_div) |
|
724 |
||
725 |
lemma drop_bit_Suc: |
|
726 |
"drop_bit (Suc n) a = drop_bit n (a div 2)" |
|
727 |
using div_exp_eq [of a 1] by (simp add: drop_bit_eq_div) |
|
728 |
||
729 |
lemma drop_bit_rec: |
|
730 |
"drop_bit n a = (if n = 0 then a else drop_bit (n - 1) (a div 2))" |
|
731 |
by (cases n) (simp_all add: drop_bit_Suc) |
|
732 |
||
733 |
lemma drop_bit_half: |
|
734 |
"drop_bit n (a div 2) = drop_bit n a div 2" |
|
735 |
by (induction n arbitrary: a) (simp_all add: drop_bit_Suc) |
|
736 |
||
737 |
lemma drop_bit_of_bool [simp]: |
|
738 |
"drop_bit n (of_bool b) = of_bool (n = 0 \<and> b)" |
|
739 |
by (cases n) simp_all |
|
740 |
||
741 |
lemma even_take_bit_eq [simp]: |
|
742 |
\<open>even (take_bit n a) \<longleftrightarrow> n = 0 \<or> even a\<close> |
|
743 |
by (simp add: take_bit_rec [of n a]) |
|
744 |
||
745 |
lemma take_bit_take_bit [simp]: |
|
746 |
"take_bit m (take_bit n a) = take_bit (min m n) a" |
|
747 |
by (simp add: take_bit_eq_mod mod_exp_eq ac_simps) |
|
748 |
||
749 |
lemma drop_bit_drop_bit [simp]: |
|
750 |
"drop_bit m (drop_bit n a) = drop_bit (m + n) a" |
|
751 |
by (simp add: drop_bit_eq_div power_add div_exp_eq ac_simps) |
|
752 |
||
753 |
lemma push_bit_take_bit: |
|
754 |
"push_bit m (take_bit n a) = take_bit (m + n) (push_bit m a)" |
|
755 |
apply (simp add: push_bit_eq_mult take_bit_eq_mod power_add ac_simps) |
|
756 |
using mult_exp_mod_exp_eq [of m \<open>m + n\<close> a] apply (simp add: ac_simps power_add) |
|
757 |
done |
|
758 |
||
759 |
lemma take_bit_push_bit: |
|
760 |
"take_bit m (push_bit n a) = push_bit n (take_bit (m - n) a)" |
|
761 |
proof (cases "m \<le> n") |
|
762 |
case True |
|
763 |
then show ?thesis |
|
764 |
apply (simp add:) |
|
765 |
apply (simp_all add: push_bit_eq_mult take_bit_eq_mod) |
|
766 |
apply (auto dest!: le_Suc_ex simp add: power_add ac_simps) |
|
767 |
using mult_exp_mod_exp_eq [of m m \<open>a * 2 ^ n\<close> for n] |
|
768 |
apply (simp add: ac_simps) |
|
769 |
done |
|
770 |
next |
|
771 |
case False |
|
772 |
then show ?thesis |
|
773 |
using push_bit_take_bit [of n "m - n" a] |
|
774 |
by simp |
|
775 |
qed |
|
776 |
||
777 |
lemma take_bit_drop_bit: |
|
778 |
"take_bit m (drop_bit n a) = drop_bit n (take_bit (m + n) a)" |
|
779 |
by (simp add: drop_bit_eq_div take_bit_eq_mod ac_simps div_exp_mod_exp_eq) |
|
780 |
||
781 |
lemma drop_bit_take_bit: |
|
782 |
"drop_bit m (take_bit n a) = take_bit (n - m) (drop_bit m a)" |
|
783 |
proof (cases "m \<le> n") |
|
784 |
case True |
|
785 |
then show ?thesis |
|
786 |
using take_bit_drop_bit [of "n - m" m a] by simp |
|
787 |
next |
|
788 |
case False |
|
789 |
then obtain q where \<open>m = n + q\<close> |
|
790 |
by (auto simp add: not_le dest: less_imp_Suc_add) |
|
791 |
then have \<open>drop_bit m (take_bit n a) = 0\<close> |
|
792 |
using div_exp_eq [of \<open>a mod 2 ^ n\<close> n q] |
|
793 |
by (simp add: take_bit_eq_mod drop_bit_eq_div) |
|
794 |
with False show ?thesis |
|
795 |
by simp |
|
796 |
qed |
|
797 |
||
798 |
lemma even_push_bit_iff [simp]: |
|
799 |
\<open>even (push_bit n a) \<longleftrightarrow> n \<noteq> 0 \<or> even a\<close> |
|
800 |
by (simp add: push_bit_eq_mult) auto |
|
801 |
||
802 |
lemma bit_push_bit_iff [bit_simps]: |
|
803 |
\<open>bit (push_bit m a) n \<longleftrightarrow> m \<le> n \<and> 2 ^ n \<noteq> 0 \<and> bit a (n - m)\<close> |
|
804 |
by (auto simp add: bit_iff_odd push_bit_eq_mult even_mult_exp_div_exp_iff) |
|
805 |
||
806 |
lemma bit_drop_bit_eq [bit_simps]: |
|
807 |
\<open>bit (drop_bit n a) = bit a \<circ> (+) n\<close> |
|
808 |
by (simp add: bit_iff_odd fun_eq_iff ac_simps flip: drop_bit_eq_div) |
|
809 |
||
810 |
lemma bit_take_bit_iff [bit_simps]: |
|
811 |
\<open>bit (take_bit m a) n \<longleftrightarrow> n < m \<and> bit a n\<close> |
|
812 |
by (simp add: bit_iff_odd drop_bit_take_bit not_le flip: drop_bit_eq_div) |
|
813 |
||
814 |
lemma stable_imp_drop_bit_eq: |
|
815 |
\<open>drop_bit n a = a\<close> |
|
816 |
if \<open>a div 2 = a\<close> |
|
817 |
by (induction n) (simp_all add: that drop_bit_Suc) |
|
818 |
||
819 |
lemma stable_imp_take_bit_eq: |
|
820 |
\<open>take_bit n a = (if even a then 0 else 2 ^ n - 1)\<close> |
|
821 |
if \<open>a div 2 = a\<close> |
|
822 |
proof (rule bit_eqI) |
|
823 |
fix m |
|
824 |
assume \<open>2 ^ m \<noteq> 0\<close> |
|
825 |
with that show \<open>bit (take_bit n a) m \<longleftrightarrow> bit (if even a then 0 else 2 ^ n - 1) m\<close> |
|
826 |
by (simp add: bit_take_bit_iff bit_mask_iff stable_imp_bit_iff_odd) |
|
827 |
qed |
|
828 |
||
829 |
lemma exp_dvdE: |
|
830 |
assumes \<open>2 ^ n dvd a\<close> |
|
831 |
obtains b where \<open>a = push_bit n b\<close> |
|
832 |
proof - |
|
833 |
from assms obtain b where \<open>a = 2 ^ n * b\<close> .. |
|
834 |
then have \<open>a = push_bit n b\<close> |
|
835 |
by (simp add: push_bit_eq_mult ac_simps) |
|
836 |
with that show thesis . |
|
837 |
qed |
|
838 |
||
839 |
lemma take_bit_eq_0_iff: |
|
840 |
\<open>take_bit n a = 0 \<longleftrightarrow> 2 ^ n dvd a\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
841 |
proof |
|
842 |
assume ?P |
|
843 |
then show ?Q |
|
844 |
by (simp add: take_bit_eq_mod mod_0_imp_dvd) |
|
845 |
next |
|
846 |
assume ?Q |
|
847 |
then obtain b where \<open>a = push_bit n b\<close> |
|
848 |
by (rule exp_dvdE) |
|
849 |
then show ?P |
|
850 |
by (simp add: take_bit_push_bit) |
|
851 |
qed |
|
852 |
||
853 |
lemma take_bit_tightened: |
|
854 |
\<open>take_bit m a = take_bit m b\<close> if \<open>take_bit n a = take_bit n b\<close> and \<open>m \<le> n\<close> |
|
855 |
proof - |
|
856 |
from that have \<open>take_bit m (take_bit n a) = take_bit m (take_bit n b)\<close> |
|
857 |
by simp |
|
858 |
then have \<open>take_bit (min m n) a = take_bit (min m n) b\<close> |
|
859 |
by simp |
|
860 |
with that show ?thesis |
|
861 |
by (simp add: min_def) |
|
862 |
qed |
|
863 |
||
864 |
lemma take_bit_eq_self_iff_drop_bit_eq_0: |
|
865 |
\<open>take_bit n a = a \<longleftrightarrow> drop_bit n a = 0\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
866 |
proof |
|
867 |
assume ?P |
|
868 |
show ?Q |
|
869 |
proof (rule bit_eqI) |
|
870 |
fix m |
|
871 |
from \<open>?P\<close> have \<open>a = take_bit n a\<close> .. |
|
872 |
also have \<open>\<not> bit (take_bit n a) (n + m)\<close> |
|
873 |
unfolding bit_simps |
|
874 |
by (simp add: bit_simps) |
|
875 |
finally show \<open>bit (drop_bit n a) m \<longleftrightarrow> bit 0 m\<close> |
|
876 |
by (simp add: bit_simps) |
|
877 |
qed |
|
878 |
next |
|
879 |
assume ?Q |
|
880 |
show ?P |
|
881 |
proof (rule bit_eqI) |
|
882 |
fix m |
|
883 |
from \<open>?Q\<close> have \<open>\<not> bit (drop_bit n a) (m - n)\<close> |
|
884 |
by simp |
|
885 |
then have \<open> \<not> bit a (n + (m - n))\<close> |
|
886 |
by (simp add: bit_simps) |
|
887 |
then show \<open>bit (take_bit n a) m \<longleftrightarrow> bit a m\<close> |
|
888 |
by (cases \<open>m < n\<close>) (auto simp add: bit_simps) |
|
889 |
qed |
|
890 |
qed |
|
891 |
||
892 |
lemma drop_bit_exp_eq: |
|
893 |
\<open>drop_bit m (2 ^ n) = of_bool (m \<le> n \<and> 2 ^ n \<noteq> 0) * 2 ^ (n - m)\<close> |
|
894 |
by (rule bit_eqI) (auto simp add: bit_simps) |
|
895 |
||
896 |
end |
|
897 |
||
898 |
instantiation nat :: semiring_bit_shifts |
|
899 |
begin |
|
900 |
||
901 |
definition push_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
902 |
where \<open>push_bit_nat n m = m * 2 ^ n\<close> |
|
903 |
||
904 |
definition drop_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
905 |
where \<open>drop_bit_nat n m = m div 2 ^ n\<close> |
|
906 |
||
907 |
definition take_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
908 |
where \<open>take_bit_nat n m = m mod 2 ^ n\<close> |
|
909 |
||
910 |
instance |
|
911 |
by standard (simp_all add: push_bit_nat_def drop_bit_nat_def take_bit_nat_def) |
|
912 |
||
913 |
end |
|
914 |
||
915 |
context semiring_bit_shifts |
|
916 |
begin |
|
917 |
||
918 |
lemma push_bit_of_nat: |
|
919 |
\<open>push_bit n (of_nat m) = of_nat (push_bit n m)\<close> |
|
920 |
by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult) |
|
921 |
||
922 |
lemma of_nat_push_bit: |
|
923 |
\<open>of_nat (push_bit m n) = push_bit m (of_nat n)\<close> |
|
924 |
by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult) |
|
925 |
||
926 |
lemma take_bit_of_nat: |
|
927 |
\<open>take_bit n (of_nat m) = of_nat (take_bit n m)\<close> |
|
928 |
by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_nat_iff) |
|
929 |
||
930 |
lemma of_nat_take_bit: |
|
931 |
\<open>of_nat (take_bit n m) = take_bit n (of_nat m)\<close> |
|
932 |
by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_nat_iff) |
|
933 |
||
934 |
end |
|
935 |
||
936 |
instantiation int :: semiring_bit_shifts |
|
937 |
begin |
|
938 |
||
939 |
definition push_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
|
940 |
where \<open>push_bit_int n k = k * 2 ^ n\<close> |
|
941 |
||
942 |
definition drop_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
|
943 |
where \<open>drop_bit_int n k = k div 2 ^ n\<close> |
|
944 |
||
945 |
definition take_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
|
946 |
where \<open>take_bit_int n k = k mod 2 ^ n\<close> |
|
947 |
||
948 |
instance |
|
949 |
by standard (simp_all add: push_bit_int_def drop_bit_int_def take_bit_int_def) |
|
950 |
||
951 |
end |
|
952 |
||
953 |
lemma bit_push_bit_iff_nat: |
|
954 |
\<open>bit (push_bit m q) n \<longleftrightarrow> m \<le> n \<and> bit q (n - m)\<close> for q :: nat |
|
955 |
by (auto simp add: bit_push_bit_iff) |
|
956 |
||
957 |
lemma bit_push_bit_iff_int: |
|
958 |
\<open>bit (push_bit m k) n \<longleftrightarrow> m \<le> n \<and> bit k (n - m)\<close> for k :: int |
|
959 |
by (auto simp add: bit_push_bit_iff) |
|
960 |
||
961 |
lemma take_bit_nat_less_exp [simp]: |
|
962 |
\<open>take_bit n m < 2 ^ n\<close> for n m ::nat |
|
963 |
by (simp add: take_bit_eq_mod) |
|
964 |
||
965 |
lemma take_bit_nonnegative [simp]: |
|
966 |
\<open>take_bit n k \<ge> 0\<close> for k :: int |
|
967 |
by (simp add: take_bit_eq_mod) |
|
968 |
||
969 |
lemma not_take_bit_negative [simp]: |
|
970 |
\<open>\<not> take_bit n k < 0\<close> for k :: int |
|
971 |
by (simp add: not_less) |
|
972 |
||
973 |
lemma take_bit_int_less_exp [simp]: |
|
974 |
\<open>take_bit n k < 2 ^ n\<close> for k :: int |
|
975 |
by (simp add: take_bit_eq_mod) |
|
976 |
||
977 |
lemma take_bit_nat_eq_self_iff: |
|
978 |
\<open>take_bit n m = m \<longleftrightarrow> m < 2 ^ n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
979 |
for n m :: nat |
|
980 |
proof |
|
981 |
assume ?P |
|
982 |
moreover note take_bit_nat_less_exp [of n m] |
|
983 |
ultimately show ?Q |
|
984 |
by simp |
|
985 |
next |
|
986 |
assume ?Q |
|
987 |
then show ?P |
|
988 |
by (simp add: take_bit_eq_mod) |
|
989 |
qed |
|
990 |
||
991 |
lemma take_bit_nat_eq_self: |
|
992 |
\<open>take_bit n m = m\<close> if \<open>m < 2 ^ n\<close> for m n :: nat |
|
993 |
using that by (simp add: take_bit_nat_eq_self_iff) |
|
994 |
||
995 |
lemma take_bit_int_eq_self_iff: |
|
996 |
\<open>take_bit n k = k \<longleftrightarrow> 0 \<le> k \<and> k < 2 ^ n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
997 |
for k :: int |
|
998 |
proof |
|
999 |
assume ?P |
|
1000 |
moreover note take_bit_int_less_exp [of n k] take_bit_nonnegative [of n k] |
|
1001 |
ultimately show ?Q |
|
1002 |
by simp |
|
1003 |
next |
|
1004 |
assume ?Q |
|
1005 |
then show ?P |
|
1006 |
by (simp add: take_bit_eq_mod) |
|
1007 |
qed |
|
1008 |
||
1009 |
lemma take_bit_int_eq_self: |
|
1010 |
\<open>take_bit n k = k\<close> if \<open>0 \<le> k\<close> \<open>k < 2 ^ n\<close> for k :: int |
|
1011 |
using that by (simp add: take_bit_int_eq_self_iff) |
|
1012 |
||
1013 |
lemma take_bit_nat_less_eq_self [simp]: |
|
1014 |
\<open>take_bit n m \<le> m\<close> for n m :: nat |
|
1015 |
by (simp add: take_bit_eq_mod) |
|
1016 |
||
1017 |
lemma take_bit_nat_less_self_iff: |
|
1018 |
\<open>take_bit n m < m \<longleftrightarrow> 2 ^ n \<le> m\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
1019 |
for m n :: nat |
|
1020 |
proof |
|
1021 |
assume ?P |
|
1022 |
then have \<open>take_bit n m \<noteq> m\<close> |
|
1023 |
by simp |
|
1024 |
then show \<open>?Q\<close> |
|
1025 |
by (simp add: take_bit_nat_eq_self_iff) |
|
1026 |
next |
|
1027 |
have \<open>take_bit n m < 2 ^ n\<close> |
|
1028 |
by (fact take_bit_nat_less_exp) |
|
1029 |
also assume ?Q |
|
1030 |
finally show ?P . |
|
1031 |
qed |
|
1032 |
||
1033 |
class unique_euclidean_semiring_with_bit_shifts = |
|
1034 |
unique_euclidean_semiring_with_nat + semiring_bit_shifts |
|
1035 |
begin |
|
1036 |
||
1037 |
lemma take_bit_of_exp [simp]: |
|
1038 |
\<open>take_bit m (2 ^ n) = of_bool (n < m) * 2 ^ n\<close> |
|
1039 |
by (simp add: take_bit_eq_mod exp_mod_exp) |
|
1040 |
||
1041 |
lemma take_bit_of_2 [simp]: |
|
1042 |
\<open>take_bit n 2 = of_bool (2 \<le> n) * 2\<close> |
|
1043 |
using take_bit_of_exp [of n 1] by simp |
|
1044 |
||
1045 |
lemma take_bit_of_mask: |
|
1046 |
\<open>take_bit m (2 ^ n - 1) = 2 ^ min m n - 1\<close> |
|
1047 |
by (simp add: take_bit_eq_mod mask_mod_exp) |
|
1048 |
||
1049 |
lemma push_bit_eq_0_iff [simp]: |
|
1050 |
"push_bit n a = 0 \<longleftrightarrow> a = 0" |
|
1051 |
by (simp add: push_bit_eq_mult) |
|
1052 |
||
1053 |
lemma take_bit_add: |
|
1054 |
"take_bit n (take_bit n a + take_bit n b) = take_bit n (a + b)" |
|
1055 |
by (simp add: take_bit_eq_mod mod_simps) |
|
1056 |
||
1057 |
lemma take_bit_of_1_eq_0_iff [simp]: |
|
1058 |
"take_bit n 1 = 0 \<longleftrightarrow> n = 0" |
|
1059 |
by (simp add: take_bit_eq_mod) |
|
1060 |
||
1061 |
lemma take_bit_Suc_1 [simp]: |
|
1062 |
\<open>take_bit (Suc n) 1 = 1\<close> |
|
1063 |
by (simp add: take_bit_Suc) |
|
1064 |
||
1065 |
lemma take_bit_Suc_bit0 [simp]: |
|
1066 |
\<open>take_bit (Suc n) (numeral (Num.Bit0 k)) = take_bit n (numeral k) * 2\<close> |
|
1067 |
by (simp add: take_bit_Suc numeral_Bit0_div_2) |
|
1068 |
||
1069 |
lemma take_bit_Suc_bit1 [simp]: |
|
1070 |
\<open>take_bit (Suc n) (numeral (Num.Bit1 k)) = take_bit n (numeral k) * 2 + 1\<close> |
|
1071 |
by (simp add: take_bit_Suc numeral_Bit1_div_2 mod_2_eq_odd) |
|
1072 |
||
1073 |
lemma take_bit_numeral_1 [simp]: |
|
1074 |
\<open>take_bit (numeral l) 1 = 1\<close> |
|
1075 |
by (simp add: take_bit_rec [of \<open>numeral l\<close> 1]) |
|
1076 |
||
1077 |
lemma take_bit_numeral_bit0 [simp]: |
|
1078 |
\<open>take_bit (numeral l) (numeral (Num.Bit0 k)) = take_bit (pred_numeral l) (numeral k) * 2\<close> |
|
1079 |
by (simp add: take_bit_rec numeral_Bit0_div_2) |
|
1080 |
||
1081 |
lemma take_bit_numeral_bit1 [simp]: |
|
1082 |
\<open>take_bit (numeral l) (numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (numeral k) * 2 + 1\<close> |
|
1083 |
by (simp add: take_bit_rec numeral_Bit1_div_2 mod_2_eq_odd) |
|
1084 |
||
1085 |
lemma drop_bit_Suc_bit0 [simp]: |
|
1086 |
\<open>drop_bit (Suc n) (numeral (Num.Bit0 k)) = drop_bit n (numeral k)\<close> |
|
1087 |
by (simp add: drop_bit_Suc numeral_Bit0_div_2) |
|
1088 |
||
1089 |
lemma drop_bit_Suc_bit1 [simp]: |
|
1090 |
\<open>drop_bit (Suc n) (numeral (Num.Bit1 k)) = drop_bit n (numeral k)\<close> |
|
1091 |
by (simp add: drop_bit_Suc numeral_Bit1_div_2) |
|
1092 |
||
1093 |
lemma drop_bit_numeral_bit0 [simp]: |
|
1094 |
\<open>drop_bit (numeral l) (numeral (Num.Bit0 k)) = drop_bit (pred_numeral l) (numeral k)\<close> |
|
1095 |
by (simp add: drop_bit_rec numeral_Bit0_div_2) |
|
1096 |
||
1097 |
lemma drop_bit_numeral_bit1 [simp]: |
|
1098 |
\<open>drop_bit (numeral l) (numeral (Num.Bit1 k)) = drop_bit (pred_numeral l) (numeral k)\<close> |
|
1099 |
by (simp add: drop_bit_rec numeral_Bit1_div_2) |
|
1100 |
||
1101 |
lemma drop_bit_of_nat: |
|
1102 |
"drop_bit n (of_nat m) = of_nat (drop_bit n m)" |
|
1103 |
by (simp add: drop_bit_eq_div Bit_Operations.drop_bit_eq_div of_nat_div [of m "2 ^ n"]) |
|
1104 |
||
1105 |
lemma bit_of_nat_iff_bit [bit_simps]: |
|
1106 |
\<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close> |
|
1107 |
proof - |
|
1108 |
have \<open>even (m div 2 ^ n) \<longleftrightarrow> even (of_nat (m div 2 ^ n))\<close> |
|
1109 |
by simp |
|
1110 |
also have \<open>of_nat (m div 2 ^ n) = of_nat m div of_nat (2 ^ n)\<close> |
|
1111 |
by (simp add: of_nat_div) |
|
1112 |
finally show ?thesis |
|
1113 |
by (simp add: bit_iff_odd semiring_bits_class.bit_iff_odd) |
|
1114 |
qed |
|
1115 |
||
1116 |
lemma of_nat_drop_bit: |
|
1117 |
\<open>of_nat (drop_bit m n) = drop_bit m (of_nat n)\<close> |
|
1118 |
by (simp add: drop_bit_eq_div semiring_bit_shifts_class.drop_bit_eq_div of_nat_div) |
|
1119 |
||
1120 |
lemma bit_push_bit_iff_of_nat_iff [bit_simps]: |
|
1121 |
\<open>bit (push_bit m (of_nat r)) n \<longleftrightarrow> m \<le> n \<and> bit (of_nat r) (n - m)\<close> |
|
1122 |
by (auto simp add: bit_push_bit_iff) |
|
1123 |
||
1124 |
lemma take_bit_sum: |
|
1125 |
"take_bit n a = (\<Sum>k = 0..<n. push_bit k (of_bool (bit a k)))" |
|
1126 |
for n :: nat |
|
1127 |
proof (induction n arbitrary: a) |
|
1128 |
case 0 |
|
1129 |
then show ?case |
|
1130 |
by simp |
|
1131 |
next |
|
1132 |
case (Suc n) |
|
1133 |
have "(\<Sum>k = 0..<Suc n. push_bit k (of_bool (bit a k))) = |
|
1134 |
of_bool (odd a) + (\<Sum>k = Suc 0..<Suc n. push_bit k (of_bool (bit a k)))" |
|
1135 |
by (simp add: sum.atLeast_Suc_lessThan ac_simps) |
|
1136 |
also have "(\<Sum>k = Suc 0..<Suc n. push_bit k (of_bool (bit a k))) |
|
1137 |
= (\<Sum>k = 0..<n. push_bit k (of_bool (bit (a div 2) k))) * 2" |
|
1138 |
by (simp only: sum.atLeast_Suc_lessThan_Suc_shift) (simp add: sum_distrib_right push_bit_double drop_bit_Suc bit_Suc) |
|
1139 |
finally show ?case |
|
1140 |
using Suc [of "a div 2"] by (simp add: ac_simps take_bit_Suc mod_2_eq_odd) |
|
1141 |
qed |
|
1142 |
||
1143 |
end |
|
1144 |
||
1145 |
instance nat :: unique_euclidean_semiring_with_bit_shifts .. |
|
1146 |
||
1147 |
instance int :: unique_euclidean_semiring_with_bit_shifts .. |
|
1148 |
||
1149 |
lemma bit_numeral_int_iff [bit_simps]: |
|
1150 |
\<open>bit (numeral m :: int) n \<longleftrightarrow> bit (numeral m :: nat) n\<close> |
|
1151 |
using bit_of_nat_iff_bit [of \<open>numeral m\<close> n] by simp |
|
1152 |
||
1153 |
lemma bit_not_int_iff': |
|
1154 |
\<open>bit (- k - 1) n \<longleftrightarrow> \<not> bit k n\<close> |
|
1155 |
for k :: int |
|
1156 |
proof (induction n arbitrary: k) |
|
1157 |
case 0 |
|
1158 |
show ?case |
|
1159 |
by simp |
|
1160 |
next |
|
1161 |
case (Suc n) |
|
1162 |
have \<open>- k - 1 = - (k + 2) + 1\<close> |
|
1163 |
by simp |
|
1164 |
also have \<open>(- (k + 2) + 1) div 2 = - (k div 2) - 1\<close> |
|
1165 |
proof (cases \<open>even k\<close>) |
|
1166 |
case True |
|
1167 |
then have \<open>- k div 2 = - (k div 2)\<close> |
|
1168 |
by rule (simp flip: mult_minus_right) |
|
1169 |
with True show ?thesis |
|
1170 |
by simp |
|
1171 |
next |
|
1172 |
case False |
|
1173 |
have \<open>4 = 2 * (2::int)\<close> |
|
1174 |
by simp |
|
1175 |
also have \<open>2 * 2 div 2 = (2::int)\<close> |
|
1176 |
by (simp only: nonzero_mult_div_cancel_left) |
|
1177 |
finally have *: \<open>4 div 2 = (2::int)\<close> . |
|
1178 |
from False obtain l where k: \<open>k = 2 * l + 1\<close> .. |
|
1179 |
then have \<open>- k - 2 = 2 * - (l + 2) + 1\<close> |
|
1180 |
by simp |
|
1181 |
then have \<open>(- k - 2) div 2 + 1 = - (k div 2) - 1\<close> |
|
1182 |
by (simp flip: mult_minus_right add: *) (simp add: k) |
|
1183 |
with False show ?thesis |
|
1184 |
by simp |
|
1185 |
qed |
|
1186 |
finally have \<open>(- k - 1) div 2 = - (k div 2) - 1\<close> . |
|
1187 |
with Suc show ?case |
|
1188 |
by (simp add: bit_Suc) |
|
1189 |
qed |
|
1190 |
||
1191 |
lemma bit_minus_int_iff [bit_simps]: |
|
1192 |
\<open>bit (- k) n \<longleftrightarrow> \<not> bit (k - 1) n\<close> |
|
1193 |
for k :: int |
|
1194 |
using bit_not_int_iff' [of \<open>k - 1\<close>] by simp |
|
1195 |
||
1196 |
lemma bit_nat_iff [bit_simps]: |
|
1197 |
\<open>bit (nat k) n \<longleftrightarrow> k \<ge> 0 \<and> bit k n\<close> |
|
1198 |
proof (cases \<open>k \<ge> 0\<close>) |
|
1199 |
case True |
|
1200 |
moreover define m where \<open>m = nat k\<close> |
|
1201 |
ultimately have \<open>k = int m\<close> |
|
1202 |
by simp |
|
1203 |
then show ?thesis |
|
1204 |
by (simp add: bit_simps) |
|
1205 |
next |
|
1206 |
case False |
|
1207 |
then show ?thesis |
|
1208 |
by simp |
|
1209 |
qed |
|
1210 |
||
1211 |
lemma bit_numeral_int_simps [simp]: |
|
1212 |
\<open>bit (1 :: int) (numeral n) \<longleftrightarrow> bit (0 :: int) (pred_numeral n)\<close> |
|
1213 |
\<open>bit (numeral (num.Bit0 w) :: int) (numeral n) \<longleftrightarrow> bit (numeral w :: int) (pred_numeral n)\<close> |
|
1214 |
\<open>bit (numeral (num.Bit1 w) :: int) (numeral n) \<longleftrightarrow> bit (numeral w :: int) (pred_numeral n)\<close> |
|
1215 |
\<open>bit (numeral (Num.BitM w) :: int) (numeral n) \<longleftrightarrow> \<not> bit (- numeral w :: int) (pred_numeral n)\<close> |
|
1216 |
\<open>bit (- numeral (num.Bit0 w) :: int) (numeral n) \<longleftrightarrow> bit (- numeral w :: int) (pred_numeral n)\<close> |
|
1217 |
\<open>bit (- numeral (num.Bit1 w) :: int) (numeral n) \<longleftrightarrow> \<not> bit (numeral w :: int) (pred_numeral n)\<close> |
|
1218 |
\<open>bit (- numeral (Num.BitM w) :: int) (numeral n) \<longleftrightarrow> bit (- (numeral w) :: int) (pred_numeral n)\<close> |
|
1219 |
by (simp_all add: bit_1_iff numeral_eq_Suc bit_Suc add_One sub_inc_One_eq bit_minus_int_iff) |
|
1220 |
||
1221 |
lemma bit_numeral_Bit0_Suc_iff [simp]: |
|
1222 |
\<open>bit (numeral (Num.Bit0 m) :: int) (Suc n) \<longleftrightarrow> bit (numeral m :: int) n\<close> |
|
1223 |
by (simp add: bit_Suc) |
|
1224 |
||
1225 |
lemma bit_numeral_Bit1_Suc_iff [simp]: |
|
1226 |
\<open>bit (numeral (Num.Bit1 m) :: int) (Suc n) \<longleftrightarrow> bit (numeral m :: int) n\<close> |
|
1227 |
by (simp add: bit_Suc) |
|
1228 |
||
1229 |
lemma push_bit_nat_eq: |
|
1230 |
\<open>push_bit n (nat k) = nat (push_bit n k)\<close> |
|
1231 |
by (cases \<open>k \<ge> 0\<close>) (simp_all add: push_bit_eq_mult nat_mult_distrib not_le mult_nonneg_nonpos2) |
|
1232 |
||
1233 |
lemma drop_bit_nat_eq: |
|
1234 |
\<open>drop_bit n (nat k) = nat (drop_bit n k)\<close> |
|
1235 |
apply (cases \<open>k \<ge> 0\<close>) |
|
1236 |
apply (simp_all add: drop_bit_eq_div nat_div_distrib nat_power_eq not_le) |
|
1237 |
apply (simp add: divide_int_def) |
|
1238 |
done |
|
1239 |
||
1240 |
lemma take_bit_nat_eq: |
|
1241 |
\<open>take_bit n (nat k) = nat (take_bit n k)\<close> if \<open>k \<ge> 0\<close> |
|
1242 |
using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq) |
|
1243 |
||
1244 |
lemma nat_take_bit_eq: |
|
1245 |
\<open>nat (take_bit n k) = take_bit n (nat k)\<close> |
|
1246 |
if \<open>k \<ge> 0\<close> |
|
1247 |
using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq) |
|
1248 |
||
1249 |
lemma not_exp_less_eq_0_int [simp]: |
|
1250 |
\<open>\<not> 2 ^ n \<le> (0::int)\<close> |
|
1251 |
by (simp add: power_le_zero_eq) |
|
1252 |
||
1253 |
lemma half_nonnegative_int_iff [simp]: |
|
1254 |
\<open>k div 2 \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
1255 |
proof (cases \<open>k \<ge> 0\<close>) |
|
1256 |
case True |
|
1257 |
then show ?thesis |
|
1258 |
by (auto simp add: divide_int_def sgn_1_pos) |
|
1259 |
next |
|
1260 |
case False |
|
1261 |
then show ?thesis |
|
1262 |
by (auto simp add: divide_int_def not_le elim!: evenE) |
|
1263 |
qed |
|
1264 |
||
1265 |
lemma half_negative_int_iff [simp]: |
|
1266 |
\<open>k div 2 < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
1267 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
1268 |
||
1269 |
lemma push_bit_of_Suc_0 [simp]: |
|
1270 |
"push_bit n (Suc 0) = 2 ^ n" |
|
1271 |
using push_bit_of_1 [where ?'a = nat] by simp |
|
1272 |
||
1273 |
lemma take_bit_of_Suc_0 [simp]: |
|
1274 |
"take_bit n (Suc 0) = of_bool (0 < n)" |
|
1275 |
using take_bit_of_1 [where ?'a = nat] by simp |
|
1276 |
||
1277 |
lemma drop_bit_of_Suc_0 [simp]: |
|
1278 |
"drop_bit n (Suc 0) = of_bool (n = 0)" |
|
1279 |
using drop_bit_of_1 [where ?'a = nat] by simp |
|
1280 |
||
1281 |
lemma push_bit_minus_one: |
|
1282 |
"push_bit n (- 1 :: int) = - (2 ^ n)" |
|
1283 |
by (simp add: push_bit_eq_mult) |
|
1284 |
||
1285 |
lemma minus_1_div_exp_eq_int: |
|
1286 |
\<open>- 1 div (2 :: int) ^ n = - 1\<close> |
|
1287 |
by (induction n) (use div_exp_eq [symmetric, of \<open>- 1 :: int\<close> 1] in \<open>simp_all add: ac_simps\<close>) |
|
1288 |
||
1289 |
lemma drop_bit_minus_one [simp]: |
|
1290 |
\<open>drop_bit n (- 1 :: int) = - 1\<close> |
|
1291 |
by (simp add: drop_bit_eq_div minus_1_div_exp_eq_int) |
|
1292 |
||
1293 |
lemma take_bit_Suc_from_most: |
|
1294 |
\<open>take_bit (Suc n) k = 2 ^ n * of_bool (bit k n) + take_bit n k\<close> for k :: int |
|
1295 |
by (simp only: take_bit_eq_mod power_Suc2) (simp_all add: bit_iff_odd odd_iff_mod_2_eq_one zmod_zmult2_eq) |
|
1296 |
||
1297 |
lemma take_bit_minus: |
|
1298 |
\<open>take_bit n (- take_bit n k) = take_bit n (- k)\<close> |
|
1299 |
for k :: int |
|
1300 |
by (simp add: take_bit_eq_mod mod_minus_eq) |
|
1301 |
||
1302 |
lemma take_bit_diff: |
|
1303 |
\<open>take_bit n (take_bit n k - take_bit n l) = take_bit n (k - l)\<close> |
|
1304 |
for k l :: int |
|
1305 |
by (simp add: take_bit_eq_mod mod_diff_eq) |
|
1306 |
||
1307 |
lemma bit_imp_take_bit_positive: |
|
1308 |
\<open>0 < take_bit m k\<close> if \<open>n < m\<close> and \<open>bit k n\<close> for k :: int |
|
1309 |
proof (rule ccontr) |
|
1310 |
assume \<open>\<not> 0 < take_bit m k\<close> |
|
1311 |
then have \<open>take_bit m k = 0\<close> |
|
1312 |
by (auto simp add: not_less intro: order_antisym) |
|
1313 |
then have \<open>bit (take_bit m k) n = bit 0 n\<close> |
|
1314 |
by simp |
|
1315 |
with that show False |
|
1316 |
by (simp add: bit_take_bit_iff) |
|
1317 |
qed |
|
1318 |
||
1319 |
lemma take_bit_mult: |
|
1320 |
\<open>take_bit n (take_bit n k * take_bit n l) = take_bit n (k * l)\<close> |
|
1321 |
for k l :: int |
|
1322 |
by (simp add: take_bit_eq_mod mod_mult_eq) |
|
1323 |
||
1324 |
lemma (in ring_1) of_nat_nat_take_bit_eq [simp]: |
|
1325 |
\<open>of_nat (nat (take_bit n k)) = of_int (take_bit n k)\<close> |
|
1326 |
by simp |
|
1327 |
||
1328 |
lemma take_bit_minus_small_eq: |
|
1329 |
\<open>take_bit n (- k) = 2 ^ n - k\<close> if \<open>0 < k\<close> \<open>k \<le> 2 ^ n\<close> for k :: int |
|
1330 |
proof - |
|
1331 |
define m where \<open>m = nat k\<close> |
|
1332 |
with that have \<open>k = int m\<close> and \<open>0 < m\<close> and \<open>m \<le> 2 ^ n\<close> |
|
1333 |
by simp_all |
|
1334 |
have \<open>(2 ^ n - m) mod 2 ^ n = 2 ^ n - m\<close> |
|
1335 |
using \<open>0 < m\<close> by simp |
|
1336 |
then have \<open>int ((2 ^ n - m) mod 2 ^ n) = int (2 ^ n - m)\<close> |
|
1337 |
by simp |
|
1338 |
then have \<open>(2 ^ n - int m) mod 2 ^ n = 2 ^ n - int m\<close> |
|
1339 |
using \<open>m \<le> 2 ^ n\<close> by (simp only: of_nat_mod of_nat_diff) simp |
|
1340 |
with \<open>k = int m\<close> have \<open>(2 ^ n - k) mod 2 ^ n = 2 ^ n - k\<close> |
|
1341 |
by simp |
|
1342 |
then show ?thesis |
|
1343 |
by (simp add: take_bit_eq_mod) |
|
1344 |
qed |
|
1345 |
||
1346 |
lemma drop_bit_push_bit_int: |
|
1347 |
\<open>drop_bit m (push_bit n k) = drop_bit (m - n) (push_bit (n - m) k)\<close> for k :: int |
|
1348 |
by (cases \<open>m \<le> n\<close>) (auto simp add: mult.left_commute [of _ \<open>2 ^ n\<close>] mult.commute [of _ \<open>2 ^ n\<close>] mult.assoc |
|
1349 |
mult.commute [of k] drop_bit_eq_div push_bit_eq_mult not_le power_add dest!: le_Suc_ex less_imp_Suc_add) |
|
1350 |
||
1351 |
lemma push_bit_nonnegative_int_iff [simp]: |
|
1352 |
\<open>push_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
1353 |
by (simp add: push_bit_eq_mult zero_le_mult_iff) |
|
1354 |
||
1355 |
lemma push_bit_negative_int_iff [simp]: |
|
1356 |
\<open>push_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
1357 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
1358 |
||
1359 |
lemma drop_bit_nonnegative_int_iff [simp]: |
|
1360 |
\<open>drop_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
1361 |
by (induction n) (simp_all add: drop_bit_Suc drop_bit_half) |
|
1362 |
||
1363 |
lemma drop_bit_negative_int_iff [simp]: |
|
1364 |
\<open>drop_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
1365 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
1366 |
||
1367 |
||
71956 | 1368 |
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
|
1369 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1370 |
class semiring_bit_operations = semiring_bit_shifts + |
71426 | 1371 |
fixes "and" :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close> (infixr \<open>AND\<close> 64) |
1372 |
and or :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close> (infixr \<open>OR\<close> 59) |
|
1373 |
and xor :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close> (infixr \<open>XOR\<close> 59) |
|
72082 | 1374 |
and mask :: \<open>nat \<Rightarrow> 'a\<close> |
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1375 |
and set_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1376 |
and unset_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1377 |
and flip_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1378 |
assumes bit_and_iff [bit_simps]: \<open>bit (a AND b) n \<longleftrightarrow> bit a n \<and> bit b n\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1379 |
and bit_or_iff [bit_simps]: \<open>bit (a OR b) n \<longleftrightarrow> bit a n \<or> bit b n\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1380 |
and bit_xor_iff [bit_simps]: \<open>bit (a XOR b) n \<longleftrightarrow> bit a n \<noteq> bit b n\<close> |
72082 | 1381 |
and mask_eq_exp_minus_1: \<open>mask n = 2 ^ n - 1\<close> |
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1382 |
and set_bit_eq_or: \<open>set_bit n a = a OR push_bit n 1\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1383 |
and bit_unset_bit_iff [bit_simps]: \<open>bit (unset_bit m a) n \<longleftrightarrow> bit a n \<and> m \<noteq> n\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1384 |
and flip_bit_eq_xor: \<open>flip_bit n a = a XOR push_bit 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
|
1385 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1386 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1387 |
text \<open> |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1388 |
We want the bitwise operations to bind slightly weaker |
71094 | 1389 |
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
|
1390 |
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
|
1391 |
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
|
1392 |
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
|
1393 |
\<close> |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1394 |
|
71418 | 1395 |
sublocale "and": semilattice \<open>(AND)\<close> |
1396 |
by standard (auto simp add: bit_eq_iff bit_and_iff) |
|
1397 |
||
1398 |
sublocale or: semilattice_neutr \<open>(OR)\<close> 0 |
|
1399 |
by standard (auto simp add: bit_eq_iff bit_or_iff) |
|
1400 |
||
1401 |
sublocale xor: comm_monoid \<open>(XOR)\<close> 0 |
|
1402 |
by standard (auto simp add: bit_eq_iff bit_xor_iff) |
|
1403 |
||
71823 | 1404 |
lemma even_and_iff: |
1405 |
\<open>even (a AND b) \<longleftrightarrow> even a \<or> even b\<close> |
|
1406 |
using bit_and_iff [of a b 0] by auto |
|
1407 |
||
1408 |
lemma even_or_iff: |
|
1409 |
\<open>even (a OR b) \<longleftrightarrow> even a \<and> even b\<close> |
|
1410 |
using bit_or_iff [of a b 0] by auto |
|
1411 |
||
1412 |
lemma even_xor_iff: |
|
1413 |
\<open>even (a XOR b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)\<close> |
|
1414 |
using bit_xor_iff [of a b 0] by auto |
|
1415 |
||
71412 | 1416 |
lemma zero_and_eq [simp]: |
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1417 |
\<open>0 AND a = 0\<close> |
71412 | 1418 |
by (simp add: bit_eq_iff bit_and_iff) |
1419 |
||
1420 |
lemma and_zero_eq [simp]: |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1421 |
\<open>a AND 0 = 0\<close> |
71412 | 1422 |
by (simp add: bit_eq_iff bit_and_iff) |
1423 |
||
71921 | 1424 |
lemma one_and_eq: |
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1425 |
\<open>1 AND a = a mod 2\<close> |
71418 | 1426 |
by (simp add: bit_eq_iff bit_and_iff) (auto simp add: bit_1_iff) |
71412 | 1427 |
|
71921 | 1428 |
lemma and_one_eq: |
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1429 |
\<open>a AND 1 = a mod 2\<close> |
71418 | 1430 |
using one_and_eq [of a] by (simp add: ac_simps) |
1431 |
||
71822 | 1432 |
lemma one_or_eq: |
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1433 |
\<open>1 OR a = a + of_bool (even a)\<close> |
71418 | 1434 |
by (simp add: bit_eq_iff bit_or_iff add.commute [of _ 1] even_bit_succ_iff) (auto simp add: bit_1_iff) |
71412 | 1435 |
|
71822 | 1436 |
lemma or_one_eq: |
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1437 |
\<open>a OR 1 = a + of_bool (even a)\<close> |
71418 | 1438 |
using one_or_eq [of a] by (simp add: ac_simps) |
71412 | 1439 |
|
71822 | 1440 |
lemma one_xor_eq: |
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1441 |
\<open>1 XOR a = a + of_bool (even a) - of_bool (odd a)\<close> |
71418 | 1442 |
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) |
1443 |
||
71822 | 1444 |
lemma xor_one_eq: |
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1445 |
\<open>a XOR 1 = a + of_bool (even a) - of_bool (odd a)\<close> |
71418 | 1446 |
using one_xor_eq [of a] by (simp add: ac_simps) |
71412 | 1447 |
|
71409 | 1448 |
lemma take_bit_and [simp]: |
1449 |
\<open>take_bit n (a AND b) = take_bit n a AND take_bit n b\<close> |
|
1450 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_and_iff) |
|
1451 |
||
1452 |
lemma take_bit_or [simp]: |
|
1453 |
\<open>take_bit n (a OR b) = take_bit n a OR take_bit n b\<close> |
|
1454 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_or_iff) |
|
1455 |
||
1456 |
lemma take_bit_xor [simp]: |
|
1457 |
\<open>take_bit n (a XOR b) = take_bit n a XOR take_bit n b\<close> |
|
1458 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_xor_iff) |
|
1459 |
||
72239 | 1460 |
lemma push_bit_and [simp]: |
1461 |
\<open>push_bit n (a AND b) = push_bit n a AND push_bit n b\<close> |
|
1462 |
by (rule bit_eqI) (auto simp add: bit_push_bit_iff bit_and_iff) |
|
1463 |
||
1464 |
lemma push_bit_or [simp]: |
|
1465 |
\<open>push_bit n (a OR b) = push_bit n a OR push_bit n b\<close> |
|
1466 |
by (rule bit_eqI) (auto simp add: bit_push_bit_iff bit_or_iff) |
|
1467 |
||
1468 |
lemma push_bit_xor [simp]: |
|
1469 |
\<open>push_bit n (a XOR b) = push_bit n a XOR push_bit n b\<close> |
|
1470 |
by (rule bit_eqI) (auto simp add: bit_push_bit_iff bit_xor_iff) |
|
1471 |
||
1472 |
lemma drop_bit_and [simp]: |
|
1473 |
\<open>drop_bit n (a AND b) = drop_bit n a AND drop_bit n b\<close> |
|
1474 |
by (rule bit_eqI) (auto simp add: bit_drop_bit_eq bit_and_iff) |
|
1475 |
||
1476 |
lemma drop_bit_or [simp]: |
|
1477 |
\<open>drop_bit n (a OR b) = drop_bit n a OR drop_bit n b\<close> |
|
1478 |
by (rule bit_eqI) (auto simp add: bit_drop_bit_eq bit_or_iff) |
|
1479 |
||
1480 |
lemma drop_bit_xor [simp]: |
|
1481 |
\<open>drop_bit n (a XOR b) = drop_bit n a XOR drop_bit n b\<close> |
|
1482 |
by (rule bit_eqI) (auto simp add: bit_drop_bit_eq bit_xor_iff) |
|
1483 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
1484 |
lemma bit_mask_iff [bit_simps]: |
71823 | 1485 |
\<open>bit (mask m) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n < m\<close> |
1486 |
by (simp add: mask_eq_exp_minus_1 bit_mask_iff) |
|
1487 |
||
1488 |
lemma even_mask_iff: |
|
1489 |
\<open>even (mask n) \<longleftrightarrow> n = 0\<close> |
|
1490 |
using bit_mask_iff [of n 0] by auto |
|
1491 |
||
72082 | 1492 |
lemma mask_0 [simp]: |
71823 | 1493 |
\<open>mask 0 = 0\<close> |
1494 |
by (simp add: mask_eq_exp_minus_1) |
|
1495 |
||
72082 | 1496 |
lemma mask_Suc_0 [simp]: |
1497 |
\<open>mask (Suc 0) = 1\<close> |
|
1498 |
by (simp add: mask_eq_exp_minus_1 add_implies_diff sym) |
|
1499 |
||
1500 |
lemma mask_Suc_exp: |
|
71823 | 1501 |
\<open>mask (Suc n) = 2 ^ n OR mask n\<close> |
1502 |
by (rule bit_eqI) |
|
1503 |
(auto simp add: bit_or_iff bit_mask_iff bit_exp_iff not_less le_less_Suc_eq) |
|
1504 |
||
1505 |
lemma mask_Suc_double: |
|
72082 | 1506 |
\<open>mask (Suc n) = 1 OR 2 * mask n\<close> |
71823 | 1507 |
proof (rule bit_eqI) |
1508 |
fix q |
|
1509 |
assume \<open>2 ^ q \<noteq> 0\<close> |
|
72082 | 1510 |
show \<open>bit (mask (Suc n)) q \<longleftrightarrow> bit (1 OR 2 * mask n) q\<close> |
71823 | 1511 |
by (cases q) |
1512 |
(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) |
|
1513 |
qed |
|
1514 |
||
72082 | 1515 |
lemma mask_numeral: |
1516 |
\<open>mask (numeral n) = 1 + 2 * mask (pred_numeral n)\<close> |
|
1517 |
by (simp add: numeral_eq_Suc mask_Suc_double one_or_eq ac_simps) |
|
1518 |
||
72830 | 1519 |
lemma take_bit_mask [simp]: |
1520 |
\<open>take_bit m (mask n) = mask (min m n)\<close> |
|
1521 |
by (rule bit_eqI) (simp add: bit_simps) |
|
1522 |
||
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71956
diff
changeset
|
1523 |
lemma take_bit_eq_mask: |
71823 | 1524 |
\<open>take_bit n a = a AND mask n\<close> |
1525 |
by (rule bit_eqI) |
|
1526 |
(auto simp add: bit_take_bit_iff bit_and_iff bit_mask_iff) |
|
1527 |
||
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1528 |
lemma or_eq_0_iff: |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1529 |
\<open>a OR b = 0 \<longleftrightarrow> a = 0 \<and> b = 0\<close> |
72792 | 1530 |
by (auto simp add: bit_eq_iff bit_or_iff) |
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1531 |
|
72239 | 1532 |
lemma disjunctive_add: |
1533 |
\<open>a + b = a OR b\<close> if \<open>\<And>n. \<not> bit a n \<or> \<not> bit b n\<close> |
|
1534 |
by (rule bit_eqI) (use that in \<open>simp add: bit_disjunctive_add_iff bit_or_iff\<close>) |
|
1535 |
||
72508 | 1536 |
lemma bit_iff_and_drop_bit_eq_1: |
1537 |
\<open>bit a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close> |
|
1538 |
by (simp add: bit_iff_odd_drop_bit and_one_eq odd_iff_mod_2_eq_one) |
|
1539 |
||
1540 |
lemma bit_iff_and_push_bit_not_eq_0: |
|
1541 |
\<open>bit a n \<longleftrightarrow> a AND push_bit n 1 \<noteq> 0\<close> |
|
1542 |
apply (cases \<open>2 ^ n = 0\<close>) |
|
1543 |
apply (simp_all add: push_bit_of_1 bit_eq_iff bit_and_iff bit_push_bit_iff exp_eq_0_imp_not_bit) |
|
1544 |
apply (simp_all add: bit_exp_iff) |
|
1545 |
done |
|
1546 |
||
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1547 |
lemmas set_bit_def = set_bit_eq_or |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1548 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1549 |
lemma bit_set_bit_iff [bit_simps]: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1550 |
\<open>bit (set_bit m a) n \<longleftrightarrow> bit a n \<or> (m = n \<and> 2 ^ n \<noteq> 0)\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1551 |
by (auto simp add: set_bit_def push_bit_of_1 bit_or_iff bit_exp_iff) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1552 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1553 |
lemma even_set_bit_iff: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1554 |
\<open>even (set_bit m a) \<longleftrightarrow> even a \<and> m \<noteq> 0\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1555 |
using bit_set_bit_iff [of m a 0] by auto |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1556 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1557 |
lemma even_unset_bit_iff: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1558 |
\<open>even (unset_bit m a) \<longleftrightarrow> even a \<or> m = 0\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1559 |
using bit_unset_bit_iff [of m a 0] by auto |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1560 |
|
73789 | 1561 |
lemma and_exp_eq_0_iff_not_bit: |
1562 |
\<open>a AND 2 ^ n = 0 \<longleftrightarrow> \<not> bit a n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
1563 |
proof |
|
1564 |
assume ?Q |
|
1565 |
then show ?P |
|
1566 |
by (auto intro: bit_eqI simp add: bit_simps) |
|
1567 |
next |
|
1568 |
assume ?P |
|
1569 |
show ?Q |
|
1570 |
proof (rule notI) |
|
1571 |
assume \<open>bit a n\<close> |
|
1572 |
then have \<open>a AND 2 ^ n = 2 ^ n\<close> |
|
1573 |
by (auto intro: bit_eqI simp add: bit_simps) |
|
1574 |
with \<open>?P\<close> show False |
|
1575 |
using \<open>bit a n\<close> exp_eq_0_imp_not_bit by auto |
|
1576 |
qed |
|
1577 |
qed |
|
1578 |
||
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1579 |
lemmas flip_bit_def = flip_bit_eq_xor |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1580 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1581 |
lemma bit_flip_bit_iff [bit_simps]: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1582 |
\<open>bit (flip_bit m a) n \<longleftrightarrow> (m = n \<longleftrightarrow> \<not> bit a n) \<and> 2 ^ n \<noteq> 0\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1583 |
by (auto simp add: flip_bit_def push_bit_of_1 bit_xor_iff bit_exp_iff exp_eq_0_imp_not_bit) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1584 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1585 |
lemma even_flip_bit_iff: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1586 |
\<open>even (flip_bit m a) \<longleftrightarrow> \<not> (even a \<longleftrightarrow> m = 0)\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1587 |
using bit_flip_bit_iff [of m a 0] by auto |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1588 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1589 |
lemma set_bit_0 [simp]: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1590 |
\<open>set_bit 0 a = 1 + 2 * (a div 2)\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1591 |
proof (rule bit_eqI) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1592 |
fix m |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1593 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1594 |
then show \<open>bit (set_bit 0 a) m = bit (1 + 2 * (a div 2)) m\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1595 |
by (simp add: bit_set_bit_iff bit_double_iff even_bit_succ_iff) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1596 |
(cases m, simp_all add: bit_Suc) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1597 |
qed |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1598 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1599 |
lemma set_bit_Suc: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1600 |
\<open>set_bit (Suc n) a = a mod 2 + 2 * set_bit n (a div 2)\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1601 |
proof (rule bit_eqI) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1602 |
fix m |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1603 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1604 |
show \<open>bit (set_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * set_bit n (a div 2)) m\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1605 |
proof (cases m) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1606 |
case 0 |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1607 |
then show ?thesis |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1608 |
by (simp add: even_set_bit_iff) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1609 |
next |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1610 |
case (Suc m) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1611 |
with * have \<open>2 ^ m \<noteq> 0\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1612 |
using mult_2 by auto |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1613 |
show ?thesis |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1614 |
by (cases a rule: parity_cases) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1615 |
(simp_all add: bit_set_bit_iff bit_double_iff even_bit_succ_iff *, |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1616 |
simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close> bit_Suc) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1617 |
qed |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1618 |
qed |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1619 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1620 |
lemma unset_bit_0 [simp]: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1621 |
\<open>unset_bit 0 a = 2 * (a div 2)\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1622 |
proof (rule bit_eqI) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1623 |
fix m |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1624 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1625 |
then show \<open>bit (unset_bit 0 a) m = bit (2 * (a div 2)) m\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1626 |
by (simp add: bit_unset_bit_iff bit_double_iff) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1627 |
(cases m, simp_all add: bit_Suc) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1628 |
qed |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1629 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1630 |
lemma unset_bit_Suc: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1631 |
\<open>unset_bit (Suc n) a = a mod 2 + 2 * unset_bit n (a div 2)\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1632 |
proof (rule bit_eqI) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1633 |
fix m |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1634 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1635 |
then show \<open>bit (unset_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * unset_bit n (a div 2)) m\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1636 |
proof (cases m) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1637 |
case 0 |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1638 |
then show ?thesis |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1639 |
by (simp add: even_unset_bit_iff) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1640 |
next |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1641 |
case (Suc m) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1642 |
show ?thesis |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1643 |
by (cases a rule: parity_cases) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1644 |
(simp_all add: bit_unset_bit_iff bit_double_iff even_bit_succ_iff *, |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1645 |
simp_all add: Suc bit_Suc) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1646 |
qed |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1647 |
qed |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1648 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1649 |
lemma flip_bit_0 [simp]: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1650 |
\<open>flip_bit 0 a = of_bool (even a) + 2 * (a div 2)\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1651 |
proof (rule bit_eqI) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1652 |
fix m |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1653 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1654 |
then show \<open>bit (flip_bit 0 a) m = bit (of_bool (even a) + 2 * (a div 2)) m\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1655 |
by (simp add: bit_flip_bit_iff bit_double_iff even_bit_succ_iff) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1656 |
(cases m, simp_all add: bit_Suc) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1657 |
qed |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1658 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1659 |
lemma flip_bit_Suc: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1660 |
\<open>flip_bit (Suc n) a = a mod 2 + 2 * flip_bit n (a div 2)\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1661 |
proof (rule bit_eqI) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1662 |
fix m |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1663 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1664 |
show \<open>bit (flip_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * flip_bit n (a div 2)) m\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1665 |
proof (cases m) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1666 |
case 0 |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1667 |
then show ?thesis |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1668 |
by (simp add: even_flip_bit_iff) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1669 |
next |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1670 |
case (Suc m) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1671 |
with * have \<open>2 ^ m \<noteq> 0\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1672 |
using mult_2 by auto |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1673 |
show ?thesis |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1674 |
by (cases a rule: parity_cases) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1675 |
(simp_all add: bit_flip_bit_iff bit_double_iff even_bit_succ_iff, |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1676 |
simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close> bit_Suc) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1677 |
qed |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1678 |
qed |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1679 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1680 |
lemma flip_bit_eq_if: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1681 |
\<open>flip_bit n a = (if bit a n then unset_bit else set_bit) n a\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1682 |
by (rule bit_eqI) (auto simp add: bit_set_bit_iff bit_unset_bit_iff bit_flip_bit_iff) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1683 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1684 |
lemma take_bit_set_bit_eq: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1685 |
\<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> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1686 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_set_bit_iff) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1687 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1688 |
lemma take_bit_unset_bit_eq: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1689 |
\<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> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1690 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_unset_bit_iff) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1691 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1692 |
lemma take_bit_flip_bit_eq: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1693 |
\<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> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1694 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_flip_bit_iff) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1695 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1696 |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1697 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1698 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1699 |
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
|
1700 |
fixes not :: \<open>'a \<Rightarrow> 'a\<close> (\<open>NOT\<close>) |
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
1701 |
assumes bit_not_iff [bit_simps]: \<open>\<And>n. bit (NOT a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit a n\<close> |
71409 | 1702 |
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
|
1703 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1704 |
|
71409 | 1705 |
text \<open> |
1706 |
For the sake of code generation \<^const>\<open>not\<close> is specified as |
|
1707 |
definitional class operation. Note that \<^const>\<open>not\<close> has no |
|
1708 |
sensible definition for unlimited but only positive bit strings |
|
1709 |
(type \<^typ>\<open>nat\<close>). |
|
1710 |
\<close> |
|
1711 |
||
71186 | 1712 |
lemma bits_minus_1_mod_2_eq [simp]: |
1713 |
\<open>(- 1) mod 2 = 1\<close> |
|
1714 |
by (simp add: mod_2_eq_odd) |
|
1715 |
||
71409 | 1716 |
lemma not_eq_complement: |
1717 |
\<open>NOT a = - a - 1\<close> |
|
1718 |
using minus_eq_not_minus_1 [of \<open>a + 1\<close>] by simp |
|
1719 |
||
1720 |
lemma minus_eq_not_plus_1: |
|
1721 |
\<open>- a = NOT a + 1\<close> |
|
1722 |
using not_eq_complement [of a] by simp |
|
1723 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
1724 |
lemma bit_minus_iff [bit_simps]: |
71409 | 1725 |
\<open>bit (- a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit (a - 1) n\<close> |
1726 |
by (simp add: minus_eq_not_minus_1 bit_not_iff) |
|
1727 |
||
71418 | 1728 |
lemma even_not_iff [simp]: |
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1729 |
\<open>even (NOT a) \<longleftrightarrow> odd a\<close> |
71418 | 1730 |
using bit_not_iff [of a 0] by auto |
1731 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
1732 |
lemma bit_not_exp_iff [bit_simps]: |
71409 | 1733 |
\<open>bit (NOT (2 ^ m)) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n \<noteq> m\<close> |
1734 |
by (auto simp add: bit_not_iff bit_exp_iff) |
|
1735 |
||
71186 | 1736 |
lemma bit_minus_1_iff [simp]: |
1737 |
\<open>bit (- 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0\<close> |
|
71409 | 1738 |
by (simp add: bit_minus_iff) |
1739 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
1740 |
lemma bit_minus_exp_iff [bit_simps]: |
71409 | 1741 |
\<open>bit (- (2 ^ m)) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n \<ge> m\<close> |
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
1742 |
by (auto simp add: bit_simps simp flip: mask_eq_exp_minus_1) |
71409 | 1743 |
|
1744 |
lemma bit_minus_2_iff [simp]: |
|
1745 |
\<open>bit (- 2) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n > 0\<close> |
|
1746 |
by (simp add: bit_minus_iff bit_1_iff) |
|
71186 | 1747 |
|
71418 | 1748 |
lemma not_one [simp]: |
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1749 |
\<open>NOT 1 = - 2\<close> |
71418 | 1750 |
by (simp add: bit_eq_iff bit_not_iff) (simp add: bit_1_iff) |
1751 |
||
1752 |
sublocale "and": semilattice_neutr \<open>(AND)\<close> \<open>- 1\<close> |
|
72239 | 1753 |
by standard (rule bit_eqI, simp add: bit_and_iff) |
71418 | 1754 |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1755 |
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
|
1756 |
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
|
1757 |
proof - |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1758 |
interpret bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close> |
72239 | 1759 |
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
|
1760 |
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
|
1761 |
by standard |
71426 | 1762 |
show \<open>boolean_algebra.xor (AND) (OR) NOT = (XOR)\<close> |
72239 | 1763 |
by (rule ext, rule ext, rule bit_eqI) |
1764 |
(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
|
1765 |
qed |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1766 |
|
71802 | 1767 |
lemma and_eq_not_not_or: |
1768 |
\<open>a AND b = NOT (NOT a OR NOT b)\<close> |
|
1769 |
by simp |
|
1770 |
||
1771 |
lemma or_eq_not_not_and: |
|
1772 |
\<open>a OR b = NOT (NOT a AND NOT b)\<close> |
|
1773 |
by simp |
|
1774 |
||
72009 | 1775 |
lemma not_add_distrib: |
1776 |
\<open>NOT (a + b) = NOT a - b\<close> |
|
1777 |
by (simp add: not_eq_complement algebra_simps) |
|
1778 |
||
1779 |
lemma not_diff_distrib: |
|
1780 |
\<open>NOT (a - b) = NOT a + b\<close> |
|
1781 |
using not_add_distrib [of a \<open>- b\<close>] by simp |
|
1782 |
||
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1783 |
lemma (in ring_bit_operations) and_eq_minus_1_iff: |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1784 |
\<open>a AND b = - 1 \<longleftrightarrow> a = - 1 \<and> b = - 1\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1785 |
proof |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1786 |
assume \<open>a = - 1 \<and> b = - 1\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1787 |
then show \<open>a AND b = - 1\<close> |
72792 | 1788 |
by simp |
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1789 |
next |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1790 |
assume \<open>a AND b = - 1\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1791 |
have *: \<open>bit a n\<close> \<open>bit b n\<close> if \<open>2 ^ n \<noteq> 0\<close> for n |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1792 |
proof - |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1793 |
from \<open>a AND b = - 1\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1794 |
have \<open>bit (a AND b) n = bit (- 1) n\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1795 |
by (simp add: bit_eq_iff) |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1796 |
then show \<open>bit a n\<close> \<open>bit b n\<close> |
72792 | 1797 |
using that by (simp_all add: bit_and_iff) |
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1798 |
qed |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1799 |
have \<open>a = - 1\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1800 |
by (rule bit_eqI) (simp add: *) |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1801 |
moreover have \<open>b = - 1\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1802 |
by (rule bit_eqI) (simp add: *) |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1803 |
ultimately show \<open>a = - 1 \<and> b = - 1\<close> |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1804 |
by simp |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1805 |
qed |
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1806 |
|
72239 | 1807 |
lemma disjunctive_diff: |
1808 |
\<open>a - b = a AND NOT b\<close> if \<open>\<And>n. bit b n \<Longrightarrow> bit a n\<close> |
|
1809 |
proof - |
|
1810 |
have \<open>NOT a + b = NOT a OR b\<close> |
|
1811 |
by (rule disjunctive_add) (auto simp add: bit_not_iff dest: that) |
|
1812 |
then have \<open>NOT (NOT a + b) = NOT (NOT a OR b)\<close> |
|
1813 |
by simp |
|
1814 |
then show ?thesis |
|
1815 |
by (simp add: not_add_distrib) |
|
1816 |
qed |
|
1817 |
||
71412 | 1818 |
lemma push_bit_minus: |
1819 |
\<open>push_bit n (- a) = - push_bit n a\<close> |
|
1820 |
by (simp add: push_bit_eq_mult) |
|
1821 |
||
71409 | 1822 |
lemma take_bit_not_take_bit: |
1823 |
\<open>take_bit n (NOT (take_bit n a)) = take_bit n (NOT a)\<close> |
|
1824 |
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
|
1825 |
|
71418 | 1826 |
lemma take_bit_not_iff: |
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1827 |
\<open>take_bit n (NOT a) = take_bit n (NOT b) \<longleftrightarrow> take_bit n a = take_bit n b\<close> |
72239 | 1828 |
apply (simp add: bit_eq_iff) |
1829 |
apply (simp add: bit_not_iff bit_take_bit_iff bit_exp_iff) |
|
1830 |
apply (use exp_eq_0_imp_not_bit in blast) |
|
71418 | 1831 |
done |
1832 |
||
72262 | 1833 |
lemma take_bit_not_eq_mask_diff: |
1834 |
\<open>take_bit n (NOT a) = mask n - take_bit n a\<close> |
|
1835 |
proof - |
|
1836 |
have \<open>take_bit n (NOT a) = take_bit n (NOT (take_bit n a))\<close> |
|
1837 |
by (simp add: take_bit_not_take_bit) |
|
1838 |
also have \<open>\<dots> = mask n AND NOT (take_bit n a)\<close> |
|
1839 |
by (simp add: take_bit_eq_mask ac_simps) |
|
1840 |
also have \<open>\<dots> = mask n - take_bit n a\<close> |
|
1841 |
by (subst disjunctive_diff) |
|
1842 |
(auto simp add: bit_take_bit_iff bit_mask_iff exp_eq_0_imp_not_bit) |
|
1843 |
finally show ?thesis |
|
1844 |
by simp |
|
1845 |
qed |
|
1846 |
||
72079 | 1847 |
lemma mask_eq_take_bit_minus_one: |
1848 |
\<open>mask n = take_bit n (- 1)\<close> |
|
1849 |
by (simp add: bit_eq_iff bit_mask_iff bit_take_bit_iff conj_commute) |
|
1850 |
||
71922 | 1851 |
lemma take_bit_minus_one_eq_mask: |
1852 |
\<open>take_bit n (- 1) = mask n\<close> |
|
72079 | 1853 |
by (simp add: mask_eq_take_bit_minus_one) |
71922 | 1854 |
|
72010 | 1855 |
lemma minus_exp_eq_not_mask: |
1856 |
\<open>- (2 ^ n) = NOT (mask n)\<close> |
|
1857 |
by (rule bit_eqI) (simp add: bit_minus_iff bit_not_iff flip: mask_eq_exp_minus_1) |
|
1858 |
||
71922 | 1859 |
lemma push_bit_minus_one_eq_not_mask: |
1860 |
\<open>push_bit n (- 1) = NOT (mask n)\<close> |
|
72010 | 1861 |
by (simp add: push_bit_eq_mult minus_exp_eq_not_mask) |
1862 |
||
1863 |
lemma take_bit_not_mask_eq_0: |
|
1864 |
\<open>take_bit m (NOT (mask n)) = 0\<close> if \<open>n \<ge> m\<close> |
|
1865 |
by (rule bit_eqI) (use that in \<open>simp add: bit_take_bit_iff bit_not_iff bit_mask_iff\<close>) |
|
71922 | 1866 |
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1867 |
lemma unset_bit_eq_and_not: |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1868 |
\<open>unset_bit n a = a AND NOT (push_bit n 1)\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1869 |
by (rule bit_eqI) (auto simp add: bit_simps) |
71426 | 1870 |
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1871 |
lemmas unset_bit_def = unset_bit_eq_and_not |
71986 | 1872 |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1873 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1874 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1875 |
|
71956 | 1876 |
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
|
1877 |
|
72397 | 1878 |
lemma int_bit_bound: |
1879 |
fixes k :: int |
|
1880 |
obtains n where \<open>\<And>m. n \<le> m \<Longrightarrow> bit k m \<longleftrightarrow> bit k n\<close> |
|
1881 |
and \<open>n > 0 \<Longrightarrow> bit k (n - 1) \<noteq> bit k n\<close> |
|
1882 |
proof - |
|
1883 |
obtain q where *: \<open>\<And>m. q \<le> m \<Longrightarrow> bit k m \<longleftrightarrow> bit k q\<close> |
|
1884 |
proof (cases \<open>k \<ge> 0\<close>) |
|
1885 |
case True |
|
1886 |
moreover from power_gt_expt [of 2 \<open>nat k\<close>] |
|
73869 | 1887 |
have \<open>nat k < 2 ^ nat k\<close> |
1888 |
by simp |
|
1889 |
then have \<open>int (nat k) < int (2 ^ nat k)\<close> |
|
1890 |
by (simp only: of_nat_less_iff) |
|
72397 | 1891 |
ultimately have *: \<open>k div 2 ^ nat k = 0\<close> |
1892 |
by simp |
|
1893 |
show thesis |
|
1894 |
proof (rule that [of \<open>nat k\<close>]) |
|
1895 |
fix m |
|
1896 |
assume \<open>nat k \<le> m\<close> |
|
1897 |
then show \<open>bit k m \<longleftrightarrow> bit k (nat k)\<close> |
|
1898 |
by (auto simp add: * bit_iff_odd power_add zdiv_zmult2_eq dest!: le_Suc_ex) |
|
1899 |
qed |
|
1900 |
next |
|
1901 |
case False |
|
1902 |
moreover from power_gt_expt [of 2 \<open>nat (- k)\<close>] |
|
73869 | 1903 |
have \<open>nat (- k) < 2 ^ nat (- k)\<close> |
72397 | 1904 |
by simp |
73869 | 1905 |
then have \<open>int (nat (- k)) < int (2 ^ nat (- k))\<close> |
1906 |
by (simp only: of_nat_less_iff) |
|
72397 | 1907 |
ultimately have \<open>- k div - (2 ^ nat (- k)) = - 1\<close> |
1908 |
by (subst div_pos_neg_trivial) simp_all |
|
1909 |
then have *: \<open>k div 2 ^ nat (- k) = - 1\<close> |
|
1910 |
by simp |
|
1911 |
show thesis |
|
1912 |
proof (rule that [of \<open>nat (- k)\<close>]) |
|
1913 |
fix m |
|
1914 |
assume \<open>nat (- k) \<le> m\<close> |
|
1915 |
then show \<open>bit k m \<longleftrightarrow> bit k (nat (- k))\<close> |
|
1916 |
by (auto simp add: * bit_iff_odd power_add zdiv_zmult2_eq minus_1_div_exp_eq_int dest!: le_Suc_ex) |
|
1917 |
qed |
|
1918 |
qed |
|
1919 |
show thesis |
|
1920 |
proof (cases \<open>\<forall>m. bit k m \<longleftrightarrow> bit k q\<close>) |
|
1921 |
case True |
|
1922 |
then have \<open>bit k 0 \<longleftrightarrow> bit k q\<close> |
|
1923 |
by blast |
|
1924 |
with True that [of 0] show thesis |
|
1925 |
by simp |
|
1926 |
next |
|
1927 |
case False |
|
1928 |
then obtain r where **: \<open>bit k r \<noteq> bit k q\<close> |
|
1929 |
by blast |
|
1930 |
have \<open>r < q\<close> |
|
1931 |
by (rule ccontr) (use * [of r] ** in simp) |
|
1932 |
define N where \<open>N = {n. n < q \<and> bit k n \<noteq> bit k q}\<close> |
|
1933 |
moreover have \<open>finite N\<close> \<open>r \<in> N\<close> |
|
1934 |
using ** N_def \<open>r < q\<close> by auto |
|
1935 |
moreover define n where \<open>n = Suc (Max N)\<close> |
|
1936 |
ultimately have \<open>\<And>m. n \<le> m \<Longrightarrow> bit k m \<longleftrightarrow> bit k n\<close> |
|
1937 |
apply auto |
|
1938 |
apply (metis (full_types, lifting) "*" Max_ge_iff Suc_n_not_le_n \<open>finite N\<close> all_not_in_conv mem_Collect_eq not_le) |
|
1939 |
apply (metis "*" Max_ge Suc_n_not_le_n \<open>finite N\<close> linorder_not_less mem_Collect_eq) |
|
1940 |
apply (metis "*" Max_ge Suc_n_not_le_n \<open>finite N\<close> linorder_not_less mem_Collect_eq) |
|
1941 |
apply (metis (full_types, lifting) "*" Max_ge_iff Suc_n_not_le_n \<open>finite N\<close> all_not_in_conv mem_Collect_eq not_le) |
|
1942 |
done |
|
1943 |
have \<open>bit k (Max N) \<noteq> bit k n\<close> |
|
1944 |
by (metis (mono_tags, lifting) "*" Max_in N_def \<open>\<And>m. n \<le> m \<Longrightarrow> bit k m = bit k n\<close> \<open>finite N\<close> \<open>r \<in> N\<close> empty_iff le_cases mem_Collect_eq) |
|
1945 |
show thesis apply (rule that [of n]) |
|
1946 |
using \<open>\<And>m. n \<le> m \<Longrightarrow> bit k m = bit k n\<close> apply blast |
|
1947 |
using \<open>bit k (Max N) \<noteq> bit k n\<close> n_def by auto |
|
1948 |
qed |
|
1949 |
qed |
|
1950 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1951 |
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
|
1952 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1953 |
|
71420 | 1954 |
definition not_int :: \<open>int \<Rightarrow> int\<close> |
1955 |
where \<open>not_int k = - k - 1\<close> |
|
1956 |
||
1957 |
lemma not_int_rec: |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1958 |
\<open>NOT k = of_bool (even k) + 2 * NOT (k div 2)\<close> for k :: int |
71420 | 1959 |
by (auto simp add: not_int_def elim: oddE) |
1960 |
||
1961 |
lemma even_not_iff_int: |
|
1962 |
\<open>even (NOT k) \<longleftrightarrow> odd k\<close> for k :: int |
|
1963 |
by (simp add: not_int_def) |
|
1964 |
||
1965 |
lemma not_int_div_2: |
|
1966 |
\<open>NOT k div 2 = NOT (k div 2)\<close> for k :: int |
|
74101 | 1967 |
by (cases k) (simp_all add: not_int_def divide_int_def nat_add_distrib) |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1968 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
1969 |
lemma bit_not_int_iff [bit_simps]: |
71186 | 1970 |
\<open>bit (NOT k) n \<longleftrightarrow> \<not> bit k n\<close> |
72488 | 1971 |
for k :: int |
1972 |
by (simp add: bit_not_int_iff' not_int_def) |
|
71186 | 1973 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1974 |
function and_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1975 |
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
|
1976 |
then - of_bool (odd k \<and> odd l) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1977 |
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
|
1978 |
by auto |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1979 |
|
74101 | 1980 |
termination proof (relation \<open>measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>))\<close>) |
1981 |
show \<open>wf (measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>)))\<close> |
|
1982 |
by simp |
|
1983 |
show \<open>((k div 2, l div 2), k, l) \<in> measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>))\<close> |
|
1984 |
if \<open>\<not> (k \<in> {0, - 1} \<and> l \<in> {0, - 1})\<close> for k l |
|
1985 |
proof - |
|
1986 |
have less_eq: \<open>\<bar>k div 2\<bar> \<le> \<bar>k\<bar>\<close> for k :: int |
|
1987 |
by (cases k) (simp_all add: divide_int_def nat_add_distrib) |
|
1988 |
have less: \<open>\<bar>k div 2\<bar> < \<bar>k\<bar>\<close> if \<open>k \<notin> {0, - 1}\<close> for k :: int |
|
1989 |
proof (cases k) |
|
1990 |
case (nonneg n) |
|
1991 |
with that show ?thesis |
|
1992 |
by (simp add: int_div_less_self) |
|
1993 |
next |
|
1994 |
case (neg n) |
|
1995 |
with that have \<open>n \<noteq> 0\<close> |
|
1996 |
by simp |
|
1997 |
then have \<open>n div 2 < n\<close> |
|
1998 |
by (simp add: div_less_iff_less_mult) |
|
1999 |
with neg that show ?thesis |
|
2000 |
by (simp add: divide_int_def nat_add_distrib) |
|
2001 |
qed |
|
2002 |
from that have *: \<open>k \<notin> {0, - 1} \<or> l \<notin> {0, - 1}\<close> |
|
2003 |
by simp |
|
2004 |
then have \<open>0 < \<bar>k\<bar> + \<bar>l\<bar>\<close> |
|
2005 |
by auto |
|
2006 |
moreover from * have \<open>\<bar>k div 2\<bar> + \<bar>l div 2\<bar> < \<bar>k\<bar> + \<bar>l\<bar>\<close> |
|
2007 |
proof |
|
2008 |
assume \<open>k \<notin> {0, - 1}\<close> |
|
2009 |
then have \<open>\<bar>k div 2\<bar> < \<bar>k\<bar>\<close> |
|
2010 |
by (rule less) |
|
2011 |
with less_eq [of l] show ?thesis |
|
2012 |
by auto |
|
2013 |
next |
|
2014 |
assume \<open>l \<notin> {0, - 1}\<close> |
|
2015 |
then have \<open>\<bar>l div 2\<bar> < \<bar>l\<bar>\<close> |
|
2016 |
by (rule less) |
|
2017 |
with less_eq [of k] show ?thesis |
|
2018 |
by auto |
|
2019 |
qed |
|
2020 |
ultimately show ?thesis |
|
2021 |
by simp |
|
2022 |
qed |
|
2023 |
qed |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2024 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2025 |
declare and_int.simps [simp del] |
71802 | 2026 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2027 |
lemma and_int_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2028 |
\<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
|
2029 |
for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2030 |
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
|
2031 |
case True |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2032 |
then show ?thesis |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2033 |
by auto (simp_all add: and_int.simps) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2034 |
next |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2035 |
case False |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2036 |
then show ?thesis |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2037 |
by (auto simp add: ac_simps and_int.simps [of k l]) |
71802 | 2038 |
qed |
2039 |
||
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2040 |
lemma bit_and_int_iff: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2041 |
\<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
|
2042 |
proof (induction n arbitrary: k l) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2043 |
case 0 |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2044 |
then show ?case |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2045 |
by (simp add: and_int_rec [of k l]) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2046 |
next |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2047 |
case (Suc n) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2048 |
then show ?case |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2049 |
by (simp add: and_int_rec [of k l] bit_Suc) |
71802 | 2050 |
qed |
2051 |
||
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2052 |
lemma even_and_iff_int: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2053 |
\<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
|
2054 |
using bit_and_int_iff [of k l 0] by auto |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2055 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2056 |
definition or_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2057 |
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
|
2058 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2059 |
lemma or_int_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2060 |
\<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
|
2061 |
for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2062 |
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
|
2063 |
by (simp add: or_int_def even_not_iff_int not_int_div_2) |
73535 | 2064 |
(simp_all add: not_int_def) |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2065 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2066 |
lemma bit_or_int_iff: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2067 |
\<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
|
2068 |
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
|
2069 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2070 |
definition xor_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2071 |
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
|
2072 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2073 |
lemma xor_int_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2074 |
\<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
|
2075 |
for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2076 |
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
|
2077 |
(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
|
2078 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2079 |
lemma bit_xor_int_iff: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2080 |
\<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
|
2081 |
by (auto simp add: xor_int_def bit_or_int_iff bit_and_int_iff bit_not_int_iff) |
71802 | 2082 |
|
72082 | 2083 |
definition mask_int :: \<open>nat \<Rightarrow> int\<close> |
2084 |
where \<open>mask n = (2 :: int) ^ n - 1\<close> |
|
2085 |
||
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2086 |
definition set_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2087 |
where \<open>set_bit n k = k OR push_bit n 1\<close> for k :: int |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2088 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2089 |
definition unset_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2090 |
where \<open>unset_bit n k = k AND NOT (push_bit n 1)\<close> for k :: int |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2091 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2092 |
definition flip_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2093 |
where \<open>flip_bit n k = k XOR push_bit n 1\<close> for k :: int |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2094 |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
2095 |
instance proof |
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2096 |
fix k l :: int and m n :: nat |
71409 | 2097 |
show \<open>- k = NOT (k - 1)\<close> |
2098 |
by (simp add: not_int_def) |
|
71186 | 2099 |
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
|
2100 |
by (fact bit_and_int_iff) |
71186 | 2101 |
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
|
2102 |
by (fact bit_or_int_iff) |
71186 | 2103 |
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
|
2104 |
by (fact bit_xor_int_iff) |
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2105 |
show \<open>bit (unset_bit m k) n \<longleftrightarrow> bit k n \<and> m \<noteq> n\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2106 |
proof - |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2107 |
have \<open>unset_bit m k = k AND NOT (push_bit m 1)\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2108 |
by (simp add: unset_bit_int_def) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2109 |
also have \<open>NOT (push_bit m 1 :: int) = - (push_bit m 1 + 1)\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2110 |
by (simp add: not_int_def) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2111 |
finally show ?thesis by (simp only: bit_simps bit_and_int_iff) (auto simp add: bit_simps) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2112 |
qed |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
2113 |
qed (simp_all add: bit_not_int_iff mask_int_def set_bit_int_def flip_bit_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
|
2114 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
2115 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
2116 |
|
72241 | 2117 |
lemma mask_half_int: |
2118 |
\<open>mask n div 2 = (mask (n - 1) :: int)\<close> |
|
2119 |
by (cases n) (simp_all add: mask_eq_exp_minus_1 algebra_simps) |
|
2120 |
||
72028 | 2121 |
lemma mask_nonnegative_int [simp]: |
2122 |
\<open>mask n \<ge> (0::int)\<close> |
|
2123 |
by (simp add: mask_eq_exp_minus_1) |
|
2124 |
||
2125 |
lemma not_mask_negative_int [simp]: |
|
2126 |
\<open>\<not> mask n < (0::int)\<close> |
|
2127 |
by (simp add: not_less) |
|
2128 |
||
71802 | 2129 |
lemma not_nonnegative_int_iff [simp]: |
2130 |
\<open>NOT k \<ge> 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
2131 |
by (simp add: not_int_def) |
|
2132 |
||
2133 |
lemma not_negative_int_iff [simp]: |
|
2134 |
\<open>NOT k < 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
2135 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less not_le) |
|
2136 |
||
2137 |
lemma and_nonnegative_int_iff [simp]: |
|
2138 |
\<open>k AND l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<or> l \<ge> 0\<close> for k l :: int |
|
2139 |
proof (induction k arbitrary: l rule: int_bit_induct) |
|
2140 |
case zero |
|
2141 |
then show ?case |
|
2142 |
by simp |
|
2143 |
next |
|
2144 |
case minus |
|
2145 |
then show ?case |
|
2146 |
by simp |
|
2147 |
next |
|
2148 |
case (even k) |
|
2149 |
then show ?case |
|
74101 | 2150 |
using and_int_rec [of \<open>k * 2\<close> l] |
2151 |
by (simp add: pos_imp_zdiv_nonneg_iff zero_le_mult_iff) |
|
71802 | 2152 |
next |
2153 |
case (odd k) |
|
2154 |
from odd have \<open>0 \<le> k AND l div 2 \<longleftrightarrow> 0 \<le> k \<or> 0 \<le> l div 2\<close> |
|
2155 |
by simp |
|
74101 | 2156 |
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> |
71802 | 2157 |
by simp |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
2158 |
with and_int_rec [of \<open>1 + k * 2\<close> l] |
71802 | 2159 |
show ?case |
74101 | 2160 |
by (auto simp add: zero_le_mult_iff not_le) |
71802 | 2161 |
qed |
2162 |
||
2163 |
lemma and_negative_int_iff [simp]: |
|
2164 |
\<open>k AND l < 0 \<longleftrightarrow> k < 0 \<and> l < 0\<close> for k l :: int |
|
2165 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
2166 |
||
72009 | 2167 |
lemma and_less_eq: |
2168 |
\<open>k AND l \<le> k\<close> if \<open>l < 0\<close> for k l :: int |
|
2169 |
using that proof (induction k arbitrary: l rule: int_bit_induct) |
|
2170 |
case zero |
|
2171 |
then show ?case |
|
2172 |
by simp |
|
2173 |
next |
|
2174 |
case minus |
|
2175 |
then show ?case |
|
2176 |
by simp |
|
2177 |
next |
|
2178 |
case (even k) |
|
2179 |
from even.IH [of \<open>l div 2\<close>] even.hyps even.prems |
|
2180 |
show ?case |
|
2181 |
by (simp add: and_int_rec [of _ l]) |
|
2182 |
next |
|
2183 |
case (odd k) |
|
2184 |
from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems |
|
2185 |
show ?case |
|
2186 |
by (simp add: and_int_rec [of _ l]) |
|
2187 |
qed |
|
2188 |
||
71802 | 2189 |
lemma or_nonnegative_int_iff [simp]: |
2190 |
\<open>k OR l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<and> l \<ge> 0\<close> for k l :: int |
|
2191 |
by (simp only: or_eq_not_not_and not_nonnegative_int_iff) simp |
|
2192 |
||
2193 |
lemma or_negative_int_iff [simp]: |
|
2194 |
\<open>k OR l < 0 \<longleftrightarrow> k < 0 \<or> l < 0\<close> for k l :: int |
|
2195 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
2196 |
||
72009 | 2197 |
lemma or_greater_eq: |
2198 |
\<open>k OR l \<ge> k\<close> if \<open>l \<ge> 0\<close> for k l :: int |
|
2199 |
using that proof (induction k arbitrary: l rule: int_bit_induct) |
|
2200 |
case zero |
|
2201 |
then show ?case |
|
2202 |
by simp |
|
2203 |
next |
|
2204 |
case minus |
|
2205 |
then show ?case |
|
2206 |
by simp |
|
2207 |
next |
|
2208 |
case (even k) |
|
2209 |
from even.IH [of \<open>l div 2\<close>] even.hyps even.prems |
|
2210 |
show ?case |
|
2211 |
by (simp add: or_int_rec [of _ l]) |
|
2212 |
next |
|
2213 |
case (odd k) |
|
2214 |
from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems |
|
2215 |
show ?case |
|
2216 |
by (simp add: or_int_rec [of _ l]) |
|
2217 |
qed |
|
2218 |
||
71802 | 2219 |
lemma xor_nonnegative_int_iff [simp]: |
2220 |
\<open>k XOR l \<ge> 0 \<longleftrightarrow> (k \<ge> 0 \<longleftrightarrow> l \<ge> 0)\<close> for k l :: int |
|
2221 |
by (simp only: bit.xor_def or_nonnegative_int_iff) auto |
|
2222 |
||
2223 |
lemma xor_negative_int_iff [simp]: |
|
2224 |
\<open>k XOR l < 0 \<longleftrightarrow> (k < 0) \<noteq> (l < 0)\<close> for k l :: int |
|
2225 |
by (subst Not_eq_iff [symmetric]) (auto simp add: not_less) |
|
2226 |
||
72488 | 2227 |
lemma OR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
2228 |
fixes x y :: int |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2229 |
assumes \<open>0 \<le> x\<close> \<open>x < 2 ^ n\<close> \<open>y < 2 ^ n\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2230 |
shows \<open>x OR y < 2 ^ n\<close> |
72488 | 2231 |
using assms proof (induction x arbitrary: y n rule: int_bit_induct) |
2232 |
case zero |
|
2233 |
then show ?case |
|
2234 |
by simp |
|
2235 |
next |
|
2236 |
case minus |
|
2237 |
then show ?case |
|
2238 |
by simp |
|
2239 |
next |
|
2240 |
case (even x) |
|
2241 |
from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps |
|
2242 |
show ?case |
|
2243 |
by (cases n) (auto simp add: or_int_rec [of \<open>_ * 2\<close>] elim: oddE) |
|
2244 |
next |
|
2245 |
case (odd x) |
|
2246 |
from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps |
|
2247 |
show ?case |
|
2248 |
by (cases n) (auto simp add: or_int_rec [of \<open>1 + _ * 2\<close>], linarith) |
|
2249 |
qed |
|
2250 |
||
2251 |
lemma XOR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2252 |
fixes x y :: int |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2253 |
assumes \<open>0 \<le> x\<close> \<open>x < 2 ^ n\<close> \<open>y < 2 ^ n\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2254 |
shows \<open>x XOR y < 2 ^ n\<close> |
72488 | 2255 |
using assms proof (induction x arbitrary: y n rule: int_bit_induct) |
2256 |
case zero |
|
2257 |
then show ?case |
|
2258 |
by simp |
|
2259 |
next |
|
2260 |
case minus |
|
2261 |
then show ?case |
|
2262 |
by simp |
|
2263 |
next |
|
2264 |
case (even x) |
|
2265 |
from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps |
|
2266 |
show ?case |
|
2267 |
by (cases n) (auto simp add: xor_int_rec [of \<open>_ * 2\<close>] elim: oddE) |
|
2268 |
next |
|
2269 |
case (odd x) |
|
2270 |
from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps |
|
2271 |
show ?case |
|
2272 |
by (cases n) (auto simp add: xor_int_rec [of \<open>1 + _ * 2\<close>]) |
|
2273 |
qed |
|
2274 |
||
2275 |
lemma AND_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2276 |
fixes x y :: int |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2277 |
assumes \<open>0 \<le> x\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2278 |
shows \<open>0 \<le> x AND y\<close> |
72488 | 2279 |
using assms by simp |
2280 |
||
2281 |
lemma OR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2282 |
fixes x y :: int |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2283 |
assumes \<open>0 \<le> x\<close> \<open>0 \<le> y\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2284 |
shows \<open>0 \<le> x OR y\<close> |
72488 | 2285 |
using assms by simp |
2286 |
||
2287 |
lemma XOR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2288 |
fixes x y :: int |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2289 |
assumes \<open>0 \<le> x\<close> \<open>0 \<le> y\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2290 |
shows \<open>0 \<le> x XOR y\<close> |
72488 | 2291 |
using assms by simp |
2292 |
||
2293 |
lemma AND_upper1 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2294 |
fixes x y :: int |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2295 |
assumes \<open>0 \<le> x\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2296 |
shows \<open>x AND y \<le> x\<close> |
73535 | 2297 |
using assms proof (induction x arbitrary: y rule: int_bit_induct) |
2298 |
case (odd k) |
|
2299 |
then have \<open>k AND y div 2 \<le> k\<close> |
|
2300 |
by simp |
|
2301 |
then show ?case |
|
2302 |
by (simp add: and_int_rec [of \<open>1 + _ * 2\<close>]) |
|
2303 |
qed (simp_all add: and_int_rec [of \<open>_ * 2\<close>]) |
|
72488 | 2304 |
|
2305 |
lemmas AND_upper1' [simp] = order_trans [OF AND_upper1] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2306 |
lemmas AND_upper1'' [simp] = order_le_less_trans [OF AND_upper1] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2307 |
||
2308 |
lemma AND_upper2 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2309 |
fixes x y :: int |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2310 |
assumes \<open>0 \<le> y\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2311 |
shows \<open>x AND y \<le> y\<close> |
72488 | 2312 |
using assms AND_upper1 [of y x] by (simp add: ac_simps) |
2313 |
||
2314 |
lemmas AND_upper2' [simp] = order_trans [OF AND_upper2] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2315 |
lemmas AND_upper2'' [simp] = order_le_less_trans [OF AND_upper2] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2316 |
||
2317 |
lemma plus_and_or: \<open>(x AND y) + (x OR y) = x + y\<close> for x y :: int |
|
2318 |
proof (induction x arbitrary: y rule: int_bit_induct) |
|
2319 |
case zero |
|
2320 |
then show ?case |
|
2321 |
by simp |
|
2322 |
next |
|
2323 |
case minus |
|
2324 |
then show ?case |
|
2325 |
by simp |
|
2326 |
next |
|
2327 |
case (even x) |
|
2328 |
from even.IH [of \<open>y div 2\<close>] |
|
2329 |
show ?case |
|
2330 |
by (auto simp add: and_int_rec [of _ y] or_int_rec [of _ y] elim: oddE) |
|
2331 |
next |
|
2332 |
case (odd x) |
|
2333 |
from odd.IH [of \<open>y div 2\<close>] |
|
2334 |
show ?case |
|
2335 |
by (auto simp add: and_int_rec [of _ y] or_int_rec [of _ y] elim: oddE) |
|
2336 |
qed |
|
2337 |
||
71802 | 2338 |
lemma set_bit_nonnegative_int_iff [simp]: |
2339 |
\<open>set_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
2340 |
by (simp add: set_bit_def) |
|
2341 |
||
2342 |
lemma set_bit_negative_int_iff [simp]: |
|
2343 |
\<open>set_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
2344 |
by (simp add: set_bit_def) |
|
2345 |
||
2346 |
lemma unset_bit_nonnegative_int_iff [simp]: |
|
2347 |
\<open>unset_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
2348 |
by (simp add: unset_bit_def) |
|
2349 |
||
2350 |
lemma unset_bit_negative_int_iff [simp]: |
|
2351 |
\<open>unset_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
2352 |
by (simp add: unset_bit_def) |
|
2353 |
||
2354 |
lemma flip_bit_nonnegative_int_iff [simp]: |
|
2355 |
\<open>flip_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
2356 |
by (simp add: flip_bit_def) |
|
2357 |
||
2358 |
lemma flip_bit_negative_int_iff [simp]: |
|
2359 |
\<open>flip_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
2360 |
by (simp add: flip_bit_def) |
|
2361 |
||
71986 | 2362 |
lemma set_bit_greater_eq: |
2363 |
\<open>set_bit n k \<ge> k\<close> for k :: int |
|
2364 |
by (simp add: set_bit_def or_greater_eq) |
|
2365 |
||
2366 |
lemma unset_bit_less_eq: |
|
2367 |
\<open>unset_bit n k \<le> k\<close> for k :: int |
|
2368 |
by (simp add: unset_bit_def and_less_eq) |
|
2369 |
||
72009 | 2370 |
lemma set_bit_eq: |
2371 |
\<open>set_bit n k = k + of_bool (\<not> bit k n) * 2 ^ n\<close> for k :: int |
|
2372 |
proof (rule bit_eqI) |
|
2373 |
fix m |
|
2374 |
show \<open>bit (set_bit n k) m \<longleftrightarrow> bit (k + of_bool (\<not> bit k n) * 2 ^ n) m\<close> |
|
2375 |
proof (cases \<open>m = n\<close>) |
|
2376 |
case True |
|
2377 |
then show ?thesis |
|
2378 |
apply (simp add: bit_set_bit_iff) |
|
2379 |
apply (simp add: bit_iff_odd div_plus_div_distrib_dvd_right) |
|
2380 |
done |
|
2381 |
next |
|
2382 |
case False |
|
2383 |
then show ?thesis |
|
2384 |
apply (clarsimp simp add: bit_set_bit_iff) |
|
2385 |
apply (subst disjunctive_add) |
|
2386 |
apply (clarsimp simp add: bit_exp_iff) |
|
2387 |
apply (clarsimp simp add: bit_or_iff bit_exp_iff) |
|
2388 |
done |
|
2389 |
qed |
|
2390 |
qed |
|
2391 |
||
2392 |
lemma unset_bit_eq: |
|
2393 |
\<open>unset_bit n k = k - of_bool (bit k n) * 2 ^ n\<close> for k :: int |
|
2394 |
proof (rule bit_eqI) |
|
2395 |
fix m |
|
2396 |
show \<open>bit (unset_bit n k) m \<longleftrightarrow> bit (k - of_bool (bit k n) * 2 ^ n) m\<close> |
|
2397 |
proof (cases \<open>m = n\<close>) |
|
2398 |
case True |
|
2399 |
then show ?thesis |
|
2400 |
apply (simp add: bit_unset_bit_iff) |
|
2401 |
apply (simp add: bit_iff_odd) |
|
2402 |
using div_plus_div_distrib_dvd_right [of \<open>2 ^ n\<close> \<open>- (2 ^ n)\<close> k] |
|
2403 |
apply (simp add: dvd_neg_div) |
|
2404 |
done |
|
2405 |
next |
|
2406 |
case False |
|
2407 |
then show ?thesis |
|
2408 |
apply (clarsimp simp add: bit_unset_bit_iff) |
|
2409 |
apply (subst disjunctive_diff) |
|
2410 |
apply (clarsimp simp add: bit_exp_iff) |
|
2411 |
apply (clarsimp simp add: bit_and_iff bit_not_iff bit_exp_iff) |
|
2412 |
done |
|
2413 |
qed |
|
2414 |
qed |
|
2415 |
||
72830 | 2416 |
lemma take_bit_eq_mask_iff: |
2417 |
\<open>take_bit n k = mask n \<longleftrightarrow> take_bit n (k + 1) = 0\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
2418 |
for k :: int |
|
2419 |
proof |
|
2420 |
assume ?P |
|
2421 |
then have \<open>take_bit n (take_bit n k + take_bit n 1) = 0\<close> |
|
2422 |
by (simp add: mask_eq_exp_minus_1) |
|
2423 |
then show ?Q |
|
2424 |
by (simp only: take_bit_add) |
|
2425 |
next |
|
2426 |
assume ?Q |
|
2427 |
then have \<open>take_bit n (k + 1) - 1 = - 1\<close> |
|
2428 |
by simp |
|
2429 |
then have \<open>take_bit n (take_bit n (k + 1) - 1) = take_bit n (- 1)\<close> |
|
2430 |
by simp |
|
2431 |
moreover have \<open>take_bit n (take_bit n (k + 1) - 1) = take_bit n k\<close> |
|
2432 |
by (simp add: take_bit_eq_mod mod_simps) |
|
2433 |
ultimately show ?P |
|
2434 |
by (simp add: take_bit_minus_one_eq_mask) |
|
2435 |
qed |
|
2436 |
||
2437 |
lemma take_bit_eq_mask_iff_exp_dvd: |
|
2438 |
\<open>take_bit n k = mask n \<longleftrightarrow> 2 ^ n dvd k + 1\<close> |
|
2439 |
for k :: int |
|
2440 |
by (simp add: take_bit_eq_mask_iff flip: take_bit_eq_0_iff) |
|
2441 |
||
72227 | 2442 |
context ring_bit_operations |
2443 |
begin |
|
2444 |
||
2445 |
lemma even_of_int_iff: |
|
2446 |
\<open>even (of_int k) \<longleftrightarrow> even k\<close> |
|
2447 |
by (induction k rule: int_bit_induct) simp_all |
|
2448 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
2449 |
lemma bit_of_int_iff [bit_simps]: |
72227 | 2450 |
\<open>bit (of_int k) n \<longleftrightarrow> (2::'a) ^ n \<noteq> 0 \<and> bit k n\<close> |
2451 |
proof (cases \<open>(2::'a) ^ n = 0\<close>) |
|
2452 |
case True |
|
2453 |
then show ?thesis |
|
2454 |
by (simp add: exp_eq_0_imp_not_bit) |
|
2455 |
next |
|
2456 |
case False |
|
2457 |
then have \<open>bit (of_int k) n \<longleftrightarrow> bit k n\<close> |
|
2458 |
proof (induction k arbitrary: n rule: int_bit_induct) |
|
2459 |
case zero |
|
2460 |
then show ?case |
|
2461 |
by simp |
|
2462 |
next |
|
2463 |
case minus |
|
2464 |
then show ?case |
|
2465 |
by simp |
|
2466 |
next |
|
2467 |
case (even k) |
|
2468 |
then show ?case |
|
74101 | 2469 |
using bit_double_iff [of \<open>of_int k\<close> n] Bit_Operations.bit_double_iff [of k n] |
72227 | 2470 |
by (cases n) (auto simp add: ac_simps dest: mult_not_zero) |
2471 |
next |
|
2472 |
case (odd k) |
|
2473 |
then show ?case |
|
2474 |
using bit_double_iff [of \<open>of_int k\<close> n] |
|
74101 | 2475 |
by (cases n) (auto simp add: ac_simps bit_double_iff even_bit_succ_iff Bit_Operations.bit_Suc dest: mult_not_zero) |
72227 | 2476 |
qed |
2477 |
with False show ?thesis |
|
2478 |
by simp |
|
2479 |
qed |
|
2480 |
||
2481 |
lemma push_bit_of_int: |
|
2482 |
\<open>push_bit n (of_int k) = of_int (push_bit n k)\<close> |
|
2483 |
by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult) |
|
2484 |
||
2485 |
lemma of_int_push_bit: |
|
2486 |
\<open>of_int (push_bit n k) = push_bit n (of_int k)\<close> |
|
2487 |
by (simp add: push_bit_eq_mult semiring_bit_shifts_class.push_bit_eq_mult) |
|
2488 |
||
2489 |
lemma take_bit_of_int: |
|
2490 |
\<open>take_bit n (of_int k) = of_int (take_bit n k)\<close> |
|
74101 | 2491 |
by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_int_iff) |
72227 | 2492 |
|
2493 |
lemma of_int_take_bit: |
|
2494 |
\<open>of_int (take_bit n k) = take_bit n (of_int k)\<close> |
|
74101 | 2495 |
by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_int_iff) |
72227 | 2496 |
|
2497 |
lemma of_int_not_eq: |
|
2498 |
\<open>of_int (NOT k) = NOT (of_int k)\<close> |
|
2499 |
by (rule bit_eqI) (simp add: bit_not_iff Bit_Operations.bit_not_iff bit_of_int_iff) |
|
2500 |
||
2501 |
lemma of_int_and_eq: |
|
2502 |
\<open>of_int (k AND l) = of_int k AND of_int l\<close> |
|
2503 |
by (rule bit_eqI) (simp add: bit_of_int_iff bit_and_iff Bit_Operations.bit_and_iff) |
|
2504 |
||
2505 |
lemma of_int_or_eq: |
|
2506 |
\<open>of_int (k OR l) = of_int k OR of_int l\<close> |
|
2507 |
by (rule bit_eqI) (simp add: bit_of_int_iff bit_or_iff Bit_Operations.bit_or_iff) |
|
2508 |
||
2509 |
lemma of_int_xor_eq: |
|
2510 |
\<open>of_int (k XOR l) = of_int k XOR of_int l\<close> |
|
2511 |
by (rule bit_eqI) (simp add: bit_of_int_iff bit_xor_iff Bit_Operations.bit_xor_iff) |
|
2512 |
||
2513 |
lemma of_int_mask_eq: |
|
2514 |
\<open>of_int (mask n) = mask n\<close> |
|
2515 |
by (induction n) (simp_all add: mask_Suc_double Bit_Operations.mask_Suc_double of_int_or_eq) |
|
2516 |
||
2517 |
end |
|
2518 |
||
74101 | 2519 |
lemma take_bit_incr_eq: |
2520 |
\<open>take_bit n (k + 1) = 1 + take_bit n k\<close> if \<open>take_bit n k \<noteq> 2 ^ n - 1\<close> |
|
2521 |
for k :: int |
|
2522 |
proof - |
|
2523 |
from that have \<open>2 ^ n \<noteq> k mod 2 ^ n + 1\<close> |
|
2524 |
by (simp add: take_bit_eq_mod) |
|
2525 |
moreover have \<open>k mod 2 ^ n < 2 ^ n\<close> |
|
2526 |
by simp |
|
2527 |
ultimately have *: \<open>k mod 2 ^ n + 1 < 2 ^ n\<close> |
|
2528 |
by linarith |
|
2529 |
have \<open>(k + 1) mod 2 ^ n = (k mod 2 ^ n + 1) mod 2 ^ n\<close> |
|
2530 |
by (simp add: mod_simps) |
|
2531 |
also have \<open>\<dots> = k mod 2 ^ n + 1\<close> |
|
2532 |
using * by (simp add: zmod_trivial_iff) |
|
2533 |
finally have \<open>(k + 1) mod 2 ^ n = k mod 2 ^ n + 1\<close> . |
|
2534 |
then show ?thesis |
|
2535 |
by (simp add: take_bit_eq_mod) |
|
2536 |
qed |
|
2537 |
||
2538 |
lemma take_bit_decr_eq: |
|
2539 |
\<open>take_bit n (k - 1) = take_bit n k - 1\<close> if \<open>take_bit n k \<noteq> 0\<close> |
|
2540 |
for k :: int |
|
2541 |
proof - |
|
2542 |
from that have \<open>k mod 2 ^ n \<noteq> 0\<close> |
|
2543 |
by (simp add: take_bit_eq_mod) |
|
2544 |
moreover have \<open>k mod 2 ^ n \<ge> 0\<close> \<open>k mod 2 ^ n < 2 ^ n\<close> |
|
2545 |
by simp_all |
|
2546 |
ultimately have *: \<open>k mod 2 ^ n > 0\<close> |
|
2547 |
by linarith |
|
2548 |
have \<open>(k - 1) mod 2 ^ n = (k mod 2 ^ n - 1) mod 2 ^ n\<close> |
|
2549 |
by (simp add: mod_simps) |
|
2550 |
also have \<open>\<dots> = k mod 2 ^ n - 1\<close> |
|
2551 |
by (simp add: zmod_trivial_iff) |
|
2552 |
(use \<open>k mod 2 ^ n < 2 ^ n\<close> * in linarith) |
|
2553 |
finally have \<open>(k - 1) mod 2 ^ n = k mod 2 ^ n - 1\<close> . |
|
2554 |
then show ?thesis |
|
2555 |
by (simp add: take_bit_eq_mod) |
|
2556 |
qed |
|
2557 |
||
2558 |
lemma take_bit_int_greater_eq: |
|
2559 |
\<open>k + 2 ^ n \<le> take_bit n k\<close> if \<open>k < 0\<close> for k :: int |
|
2560 |
proof - |
|
2561 |
have \<open>k + 2 ^ n \<le> take_bit n (k + 2 ^ n)\<close> |
|
2562 |
proof (cases \<open>k > - (2 ^ n)\<close>) |
|
2563 |
case False |
|
2564 |
then have \<open>k + 2 ^ n \<le> 0\<close> |
|
2565 |
by simp |
|
2566 |
also note take_bit_nonnegative |
|
2567 |
finally show ?thesis . |
|
2568 |
next |
|
2569 |
case True |
|
2570 |
with that have \<open>0 \<le> k + 2 ^ n\<close> and \<open>k + 2 ^ n < 2 ^ n\<close> |
|
2571 |
by simp_all |
|
2572 |
then show ?thesis |
|
2573 |
by (simp only: take_bit_eq_mod mod_pos_pos_trivial) |
|
2574 |
qed |
|
2575 |
then show ?thesis |
|
2576 |
by (simp add: take_bit_eq_mod) |
|
2577 |
qed |
|
2578 |
||
2579 |
lemma take_bit_int_less_eq: |
|
2580 |
\<open>take_bit n k \<le> k - 2 ^ n\<close> if \<open>2 ^ n \<le> k\<close> and \<open>n > 0\<close> for k :: int |
|
2581 |
using that zmod_le_nonneg_dividend [of \<open>k - 2 ^ n\<close> \<open>2 ^ n\<close>] |
|
2582 |
by (simp add: take_bit_eq_mod) |
|
2583 |
||
2584 |
lemma take_bit_int_less_eq_self_iff: |
|
2585 |
\<open>take_bit n k \<le> k \<longleftrightarrow> 0 \<le> k\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
2586 |
for k :: int |
|
2587 |
proof |
|
2588 |
assume ?P |
|
2589 |
show ?Q |
|
2590 |
proof (rule ccontr) |
|
2591 |
assume \<open>\<not> 0 \<le> k\<close> |
|
2592 |
then have \<open>k < 0\<close> |
|
2593 |
by simp |
|
2594 |
with \<open>?P\<close> |
|
2595 |
have \<open>take_bit n k < 0\<close> |
|
2596 |
by (rule le_less_trans) |
|
2597 |
then show False |
|
2598 |
by simp |
|
2599 |
qed |
|
2600 |
next |
|
2601 |
assume ?Q |
|
2602 |
then show ?P |
|
2603 |
by (simp add: take_bit_eq_mod zmod_le_nonneg_dividend) |
|
2604 |
qed |
|
2605 |
||
2606 |
lemma take_bit_int_less_self_iff: |
|
2607 |
\<open>take_bit n k < k \<longleftrightarrow> 2 ^ n \<le> k\<close> |
|
2608 |
for k :: int |
|
2609 |
by (auto simp add: less_le take_bit_int_less_eq_self_iff take_bit_int_eq_self_iff |
|
2610 |
intro: order_trans [of 0 \<open>2 ^ n\<close> k]) |
|
2611 |
||
2612 |
lemma take_bit_int_greater_self_iff: |
|
2613 |
\<open>k < take_bit n k \<longleftrightarrow> k < 0\<close> |
|
2614 |
for k :: int |
|
2615 |
using take_bit_int_less_eq_self_iff [of n k] by auto |
|
2616 |
||
2617 |
lemma take_bit_int_greater_eq_self_iff: |
|
2618 |
\<open>k \<le> take_bit n k \<longleftrightarrow> k < 2 ^ n\<close> |
|
2619 |
for k :: int |
|
2620 |
by (auto simp add: le_less take_bit_int_greater_self_iff take_bit_int_eq_self_iff |
|
2621 |
dest: sym not_sym intro: less_trans [of k 0 \<open>2 ^ n\<close>]) |
|
2622 |
||
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2623 |
lemma minus_numeral_inc_eq: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2624 |
\<open>- numeral (Num.inc n) = NOT (numeral n :: int)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2625 |
by (simp add: not_int_def sub_inc_One_eq add_One) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2626 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2627 |
lemma sub_one_eq_not_neg: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2628 |
\<open>Num.sub n num.One = NOT (- numeral n :: int)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2629 |
by (simp add: not_int_def) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2630 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2631 |
lemma int_not_numerals [simp]: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2632 |
\<open>NOT (numeral (Num.Bit0 n) :: int) = - numeral (Num.Bit1 n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2633 |
\<open>NOT (numeral (Num.Bit1 n) :: int) = - numeral (Num.inc (num.Bit1 n))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2634 |
\<open>NOT (numeral (Num.BitM n) :: int) = - numeral (num.Bit0 n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2635 |
\<open>NOT (- numeral (Num.Bit0 n) :: int) = numeral (Num.BitM n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2636 |
\<open>NOT (- numeral (Num.Bit1 n) :: int) = numeral (Num.Bit0 n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2637 |
by (simp_all add: not_int_def add_One inc_BitM_eq) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2638 |
|
72488 | 2639 |
text \<open>FIXME: The rule sets below are very large (24 rules for each |
2640 |
operator). Is there a simpler way to do this?\<close> |
|
2641 |
||
2642 |
context |
|
2643 |
begin |
|
2644 |
||
2645 |
private lemma eqI: |
|
2646 |
\<open>k = l\<close> |
|
2647 |
if num: \<open>\<And>n. bit k (numeral n) \<longleftrightarrow> bit l (numeral n)\<close> |
|
2648 |
and even: \<open>even k \<longleftrightarrow> even l\<close> |
|
2649 |
for k l :: int |
|
2650 |
proof (rule bit_eqI) |
|
2651 |
fix n |
|
2652 |
show \<open>bit k n \<longleftrightarrow> bit l n\<close> |
|
2653 |
proof (cases n) |
|
2654 |
case 0 |
|
2655 |
with even show ?thesis |
|
2656 |
by simp |
|
2657 |
next |
|
2658 |
case (Suc n) |
|
2659 |
with num [of \<open>num_of_nat (Suc n)\<close>] show ?thesis |
|
2660 |
by (simp only: numeral_num_of_nat) |
|
2661 |
qed |
|
2662 |
qed |
|
2663 |
||
2664 |
lemma int_and_numerals [simp]: |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2665 |
\<open>numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2666 |
\<open>numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (2 :: int) * (numeral x AND numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2667 |
\<open>numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2668 |
\<open>numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x AND numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2669 |
\<open>numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND - numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2670 |
\<open>numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (2 :: int) * (numeral x AND - numeral (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2671 |
\<open>numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND - numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2672 |
\<open>numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x AND - numeral (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2673 |
\<open>- numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (2 :: int) * (- numeral x AND numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2674 |
\<open>- numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (2 :: int) * (- numeral x AND numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2675 |
\<open>- numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (2 :: int) * (- numeral (x + Num.One) AND numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2676 |
\<open>- numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) AND numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2677 |
\<open>- numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (- numeral x AND - numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2678 |
\<open>- numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (2 :: int) * (- numeral x AND - numeral (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2679 |
\<open>- numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (- numeral (x + Num.One) AND - numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2680 |
\<open>- numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) AND - numeral (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2681 |
\<open>(1::int) AND numeral (Num.Bit0 y) = 0\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2682 |
\<open>(1::int) AND numeral (Num.Bit1 y) = 1\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2683 |
\<open>(1::int) AND - numeral (Num.Bit0 y) = 0\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2684 |
\<open>(1::int) AND - numeral (Num.Bit1 y) = 1\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2685 |
\<open>numeral (Num.Bit0 x) AND (1::int) = 0\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2686 |
\<open>numeral (Num.Bit1 x) AND (1::int) = 1\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2687 |
\<open>- numeral (Num.Bit0 x) AND (1::int) = 0\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2688 |
\<open>- numeral (Num.Bit1 x) AND (1::int) = 1\<close> |
72488 | 2689 |
by (auto simp add: bit_and_iff bit_minus_iff even_and_iff bit_double_iff even_bit_succ_iff add_One sub_inc_One_eq intro: eqI) |
2690 |
||
2691 |
lemma int_or_numerals [simp]: |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2692 |
\<open>numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (2 :: int) * (numeral x OR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2693 |
\<open>numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2694 |
\<open>numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x OR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2695 |
\<open>numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2696 |
\<open>numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (2 :: int) * (numeral x OR - numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2697 |
\<open>numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR - numeral (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2698 |
\<open>numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x OR - numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2699 |
\<open>numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR - numeral (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2700 |
\<open>- numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (2 :: int) * (- numeral x OR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2701 |
\<open>- numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x OR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2702 |
\<open>- numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2703 |
\<open>- numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2704 |
\<open>- numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (2 :: int) * (- numeral x OR - numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2705 |
\<open>- numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x OR - numeral (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2706 |
\<open>- numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR - numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2707 |
\<open>- numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR - numeral (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2708 |
\<open>(1::int) OR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2709 |
\<open>(1::int) OR numeral (Num.Bit1 y) = numeral (Num.Bit1 y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2710 |
\<open>(1::int) OR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2711 |
\<open>(1::int) OR - numeral (Num.Bit1 y) = - numeral (Num.Bit1 y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2712 |
\<open>numeral (Num.Bit0 x) OR (1::int) = numeral (Num.Bit1 x)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2713 |
\<open>numeral (Num.Bit1 x) OR (1::int) = numeral (Num.Bit1 x)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2714 |
\<open>- numeral (Num.Bit0 x) OR (1::int) = - numeral (Num.BitM x)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2715 |
\<open>- numeral (Num.Bit1 x) OR (1::int) = - numeral (Num.Bit1 x)\<close> |
72488 | 2716 |
by (auto simp add: bit_or_iff bit_minus_iff even_or_iff bit_double_iff even_bit_succ_iff add_One sub_inc_One_eq sub_BitM_One_eq intro: eqI) |
2717 |
||
2718 |
lemma int_xor_numerals [simp]: |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2719 |
\<open>numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (2 :: int) * (numeral x XOR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2720 |
\<open>numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x XOR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2721 |
\<open>numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x XOR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2722 |
\<open>numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (2 :: int) * (numeral x XOR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2723 |
\<open>numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (2 :: int) * (numeral x XOR - numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2724 |
\<open>numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x XOR - numeral (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2725 |
\<open>numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x XOR - numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2726 |
\<open>numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (2 :: int) * (numeral x XOR - numeral (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2727 |
\<open>- numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (2 :: int) * (- numeral x XOR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2728 |
\<open>- numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x XOR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2729 |
\<open>- numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) XOR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2730 |
\<open>- numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (2 :: int) * (- numeral (x + Num.One) XOR numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2731 |
\<open>- numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (2 :: int) * (- numeral x XOR - numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2732 |
\<open>- numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x XOR - numeral (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2733 |
\<open>- numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) XOR - numeral y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2734 |
\<open>- numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (2 :: int) * (- numeral (x + Num.One) XOR - numeral (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2735 |
\<open>(1::int) XOR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2736 |
\<open>(1::int) XOR numeral (Num.Bit1 y) = numeral (Num.Bit0 y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2737 |
\<open>(1::int) XOR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2738 |
\<open>(1::int) XOR - numeral (Num.Bit1 y) = - numeral (Num.Bit0 (y + Num.One))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2739 |
\<open>numeral (Num.Bit0 x) XOR (1::int) = numeral (Num.Bit1 x)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2740 |
\<open>numeral (Num.Bit1 x) XOR (1::int) = numeral (Num.Bit0 x)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2741 |
\<open>- numeral (Num.Bit0 x) XOR (1::int) = - numeral (Num.BitM x)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
2742 |
\<open>- numeral (Num.Bit1 x) XOR (1::int) = - numeral (Num.Bit0 (x + Num.One))\<close> |
72488 | 2743 |
by (auto simp add: bit_xor_iff bit_minus_iff even_xor_iff bit_double_iff even_bit_succ_iff add_One sub_inc_One_eq sub_BitM_One_eq intro: eqI) |
2744 |
||
2745 |
end |
|
2746 |
||
71442 | 2747 |
|
72028 | 2748 |
subsection \<open>Bit concatenation\<close> |
2749 |
||
2750 |
definition concat_bit :: \<open>nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int\<close> |
|
72227 | 2751 |
where \<open>concat_bit n k l = take_bit n k OR push_bit n l\<close> |
72028 | 2752 |
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
2753 |
lemma bit_concat_bit_iff [bit_simps]: |
72028 | 2754 |
\<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 | 2755 |
by (simp add: concat_bit_def bit_or_iff bit_and_iff bit_take_bit_iff bit_push_bit_iff ac_simps) |
72028 | 2756 |
|
2757 |
lemma concat_bit_eq: |
|
2758 |
\<open>concat_bit n k l = take_bit n k + push_bit n l\<close> |
|
2759 |
by (simp add: concat_bit_def take_bit_eq_mask |
|
2760 |
bit_and_iff bit_mask_iff bit_push_bit_iff disjunctive_add) |
|
2761 |
||
2762 |
lemma concat_bit_0 [simp]: |
|
2763 |
\<open>concat_bit 0 k l = l\<close> |
|
2764 |
by (simp add: concat_bit_def) |
|
2765 |
||
2766 |
lemma concat_bit_Suc: |
|
2767 |
\<open>concat_bit (Suc n) k l = k mod 2 + 2 * concat_bit n (k div 2) l\<close> |
|
2768 |
by (simp add: concat_bit_eq take_bit_Suc push_bit_double) |
|
2769 |
||
2770 |
lemma concat_bit_of_zero_1 [simp]: |
|
2771 |
\<open>concat_bit n 0 l = push_bit n l\<close> |
|
2772 |
by (simp add: concat_bit_def) |
|
2773 |
||
2774 |
lemma concat_bit_of_zero_2 [simp]: |
|
2775 |
\<open>concat_bit n k 0 = take_bit n k\<close> |
|
2776 |
by (simp add: concat_bit_def take_bit_eq_mask) |
|
2777 |
||
2778 |
lemma concat_bit_nonnegative_iff [simp]: |
|
2779 |
\<open>concat_bit n k l \<ge> 0 \<longleftrightarrow> l \<ge> 0\<close> |
|
2780 |
by (simp add: concat_bit_def) |
|
2781 |
||
2782 |
lemma concat_bit_negative_iff [simp]: |
|
2783 |
\<open>concat_bit n k l < 0 \<longleftrightarrow> l < 0\<close> |
|
2784 |
by (simp add: concat_bit_def) |
|
2785 |
||
2786 |
lemma concat_bit_assoc: |
|
2787 |
\<open>concat_bit n k (concat_bit m l r) = concat_bit (m + n) (concat_bit n k l) r\<close> |
|
2788 |
by (rule bit_eqI) (auto simp add: bit_concat_bit_iff ac_simps) |
|
2789 |
||
2790 |
lemma concat_bit_assoc_sym: |
|
2791 |
\<open>concat_bit m (concat_bit n k l) r = concat_bit (min m n) k (concat_bit (m - n) l r)\<close> |
|
2792 |
by (rule bit_eqI) (auto simp add: bit_concat_bit_iff ac_simps min_def) |
|
2793 |
||
72227 | 2794 |
lemma concat_bit_eq_iff: |
2795 |
\<open>concat_bit n k l = concat_bit n r s |
|
2796 |
\<longleftrightarrow> take_bit n k = take_bit n r \<and> l = s\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
2797 |
proof |
|
2798 |
assume ?Q |
|
2799 |
then show ?P |
|
2800 |
by (simp add: concat_bit_def) |
|
2801 |
next |
|
2802 |
assume ?P |
|
2803 |
then have *: \<open>bit (concat_bit n k l) m = bit (concat_bit n r s) m\<close> for m |
|
2804 |
by (simp add: bit_eq_iff) |
|
2805 |
have \<open>take_bit n k = take_bit n r\<close> |
|
2806 |
proof (rule bit_eqI) |
|
2807 |
fix m |
|
2808 |
from * [of m] |
|
2809 |
show \<open>bit (take_bit n k) m \<longleftrightarrow> bit (take_bit n r) m\<close> |
|
2810 |
by (auto simp add: bit_take_bit_iff bit_concat_bit_iff) |
|
2811 |
qed |
|
2812 |
moreover have \<open>push_bit n l = push_bit n s\<close> |
|
2813 |
proof (rule bit_eqI) |
|
2814 |
fix m |
|
2815 |
from * [of m] |
|
2816 |
show \<open>bit (push_bit n l) m \<longleftrightarrow> bit (push_bit n s) m\<close> |
|
2817 |
by (auto simp add: bit_push_bit_iff bit_concat_bit_iff) |
|
2818 |
qed |
|
2819 |
then have \<open>l = s\<close> |
|
2820 |
by (simp add: push_bit_eq_mult) |
|
2821 |
ultimately show ?Q |
|
2822 |
by (simp add: concat_bit_def) |
|
2823 |
qed |
|
2824 |
||
2825 |
lemma take_bit_concat_bit_eq: |
|
2826 |
\<open>take_bit m (concat_bit n k l) = concat_bit (min m n) k (take_bit (m - n) l)\<close> |
|
2827 |
by (rule bit_eqI) |
|
2828 |
(auto simp add: bit_take_bit_iff bit_concat_bit_iff min_def) |
|
2829 |
||
72488 | 2830 |
lemma concat_bit_take_bit_eq: |
2831 |
\<open>concat_bit n (take_bit n b) = concat_bit n b\<close> |
|
2832 |
by (simp add: concat_bit_def [abs_def]) |
|
2833 |
||
72028 | 2834 |
|
72241 | 2835 |
subsection \<open>Taking bits with sign propagation\<close> |
72010 | 2836 |
|
72241 | 2837 |
context ring_bit_operations |
2838 |
begin |
|
72010 | 2839 |
|
72241 | 2840 |
definition signed_take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
2841 |
where \<open>signed_take_bit n a = take_bit n a OR (of_bool (bit a n) * NOT (mask n))\<close> |
|
72227 | 2842 |
|
72241 | 2843 |
lemma signed_take_bit_eq_if_positive: |
2844 |
\<open>signed_take_bit n a = take_bit n a\<close> if \<open>\<not> bit a n\<close> |
|
72010 | 2845 |
using that by (simp add: signed_take_bit_def) |
2846 |
||
72241 | 2847 |
lemma signed_take_bit_eq_if_negative: |
2848 |
\<open>signed_take_bit n a = take_bit n a OR NOT (mask n)\<close> if \<open>bit a n\<close> |
|
2849 |
using that by (simp add: signed_take_bit_def) |
|
2850 |
||
2851 |
lemma even_signed_take_bit_iff: |
|
2852 |
\<open>even (signed_take_bit m a) \<longleftrightarrow> even a\<close> |
|
2853 |
by (auto simp add: signed_take_bit_def even_or_iff even_mask_iff bit_double_iff) |
|
2854 |
||
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
2855 |
lemma bit_signed_take_bit_iff [bit_simps]: |
72241 | 2856 |
\<open>bit (signed_take_bit m a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> bit a (min m n)\<close> |
2857 |
by (simp add: signed_take_bit_def bit_take_bit_iff bit_or_iff bit_not_iff bit_mask_iff min_def not_le) |
|
2858 |
(use exp_eq_0_imp_not_bit in blast) |
|
72010 | 2859 |
|
2860 |
lemma signed_take_bit_0 [simp]: |
|
72241 | 2861 |
\<open>signed_take_bit 0 a = - (a mod 2)\<close> |
72010 | 2862 |
by (simp add: signed_take_bit_def odd_iff_mod_2_eq_one) |
2863 |
||
2864 |
lemma signed_take_bit_Suc: |
|
72241 | 2865 |
\<open>signed_take_bit (Suc n) a = a mod 2 + 2 * signed_take_bit n (a div 2)\<close> |
2866 |
proof (rule bit_eqI) |
|
2867 |
fix m |
|
2868 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
2869 |
show \<open>bit (signed_take_bit (Suc n) a) m \<longleftrightarrow> |
|
2870 |
bit (a mod 2 + 2 * signed_take_bit n (a div 2)) m\<close> |
|
2871 |
proof (cases m) |
|
2872 |
case 0 |
|
2873 |
then show ?thesis |
|
2874 |
by (simp add: even_signed_take_bit_iff) |
|
2875 |
next |
|
2876 |
case (Suc m) |
|
2877 |
with * have \<open>2 ^ m \<noteq> 0\<close> |
|
2878 |
by (metis mult_not_zero power_Suc) |
|
2879 |
with Suc show ?thesis |
|
2880 |
by (simp add: bit_signed_take_bit_iff mod2_eq_if bit_double_iff even_bit_succ_iff |
|
2881 |
ac_simps flip: bit_Suc) |
|
2882 |
qed |
|
2883 |
qed |
|
72010 | 2884 |
|
72187 | 2885 |
lemma signed_take_bit_of_0 [simp]: |
2886 |
\<open>signed_take_bit n 0 = 0\<close> |
|
2887 |
by (simp add: signed_take_bit_def) |
|
2888 |
||
2889 |
lemma signed_take_bit_of_minus_1 [simp]: |
|
2890 |
\<open>signed_take_bit n (- 1) = - 1\<close> |
|
72241 | 2891 |
by (simp add: signed_take_bit_def take_bit_minus_one_eq_mask mask_eq_exp_minus_1) |
72187 | 2892 |
|
72241 | 2893 |
lemma signed_take_bit_Suc_1 [simp]: |
2894 |
\<open>signed_take_bit (Suc n) 1 = 1\<close> |
|
2895 |
by (simp add: signed_take_bit_Suc) |
|
2896 |
||
2897 |
lemma signed_take_bit_rec: |
|
2898 |
\<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> |
|
2899 |
by (cases n) (simp_all add: signed_take_bit_Suc) |
|
72187 | 2900 |
|
2901 |
lemma signed_take_bit_eq_iff_take_bit_eq: |
|
72241 | 2902 |
\<open>signed_take_bit n a = signed_take_bit n b \<longleftrightarrow> take_bit (Suc n) a = take_bit (Suc n) b\<close> |
2903 |
proof - |
|
2904 |
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> |
|
2905 |
by (simp add: fun_eq_iff bit_signed_take_bit_iff bit_take_bit_iff not_le less_Suc_eq_le min_def) |
|
2906 |
(use exp_eq_0_imp_not_bit in fastforce) |
|
72187 | 2907 |
then show ?thesis |
72241 | 2908 |
by (simp add: bit_eq_iff fun_eq_iff) |
72187 | 2909 |
qed |
2910 |
||
72241 | 2911 |
lemma signed_take_bit_signed_take_bit [simp]: |
2912 |
\<open>signed_take_bit m (signed_take_bit n a) = signed_take_bit (min m n) a\<close> |
|
2913 |
proof (rule bit_eqI) |
|
2914 |
fix q |
|
2915 |
show \<open>bit (signed_take_bit m (signed_take_bit n a)) q \<longleftrightarrow> |
|
2916 |
bit (signed_take_bit (min m n) a) q\<close> |
|
2917 |
by (simp add: bit_signed_take_bit_iff min_def bit_or_iff bit_not_iff bit_mask_iff bit_take_bit_iff) |
|
2918 |
(use le_Suc_ex exp_add_not_zero_imp in blast) |
|
2919 |
qed |
|
2920 |
||
2921 |
lemma signed_take_bit_take_bit: |
|
2922 |
\<open>signed_take_bit m (take_bit n a) = (if n \<le> m then take_bit n else signed_take_bit m) a\<close> |
|
2923 |
by (rule bit_eqI) (auto simp add: bit_signed_take_bit_iff min_def bit_take_bit_iff) |
|
2924 |
||
72187 | 2925 |
lemma take_bit_signed_take_bit: |
72241 | 2926 |
\<open>take_bit m (signed_take_bit n a) = take_bit m a\<close> if \<open>m \<le> Suc n\<close> |
72187 | 2927 |
using that by (rule le_SucE; intro bit_eqI) |
2928 |
(auto simp add: bit_take_bit_iff bit_signed_take_bit_iff min_def less_Suc_eq) |
|
2929 |
||
72241 | 2930 |
end |
2931 |
||
2932 |
text \<open>Modulus centered around 0\<close> |
|
2933 |
||
2934 |
lemma signed_take_bit_eq_concat_bit: |
|
2935 |
\<open>signed_take_bit n k = concat_bit n k (- of_bool (bit k n))\<close> |
|
2936 |
by (simp add: concat_bit_def signed_take_bit_def push_bit_minus_one_eq_not_mask) |
|
2937 |
||
72187 | 2938 |
lemma signed_take_bit_add: |
2939 |
\<open>signed_take_bit n (signed_take_bit n k + signed_take_bit n l) = signed_take_bit n (k + l)\<close> |
|
72241 | 2940 |
for k l :: int |
72187 | 2941 |
proof - |
2942 |
have \<open>take_bit (Suc n) |
|
2943 |
(take_bit (Suc n) (signed_take_bit n k) + |
|
2944 |
take_bit (Suc n) (signed_take_bit n l)) = |
|
2945 |
take_bit (Suc n) (k + l)\<close> |
|
2946 |
by (simp add: take_bit_signed_take_bit take_bit_add) |
|
2947 |
then show ?thesis |
|
2948 |
by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_add) |
|
2949 |
qed |
|
2950 |
||
2951 |
lemma signed_take_bit_diff: |
|
2952 |
\<open>signed_take_bit n (signed_take_bit n k - signed_take_bit n l) = signed_take_bit n (k - l)\<close> |
|
72241 | 2953 |
for k l :: int |
72187 | 2954 |
proof - |
2955 |
have \<open>take_bit (Suc n) |
|
2956 |
(take_bit (Suc n) (signed_take_bit n k) - |
|
2957 |
take_bit (Suc n) (signed_take_bit n l)) = |
|
2958 |
take_bit (Suc n) (k - l)\<close> |
|
2959 |
by (simp add: take_bit_signed_take_bit take_bit_diff) |
|
2960 |
then show ?thesis |
|
2961 |
by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_diff) |
|
2962 |
qed |
|
2963 |
||
2964 |
lemma signed_take_bit_minus: |
|
2965 |
\<open>signed_take_bit n (- signed_take_bit n k) = signed_take_bit n (- k)\<close> |
|
72241 | 2966 |
for k :: int |
72187 | 2967 |
proof - |
2968 |
have \<open>take_bit (Suc n) |
|
2969 |
(- take_bit (Suc n) (signed_take_bit n k)) = |
|
2970 |
take_bit (Suc n) (- k)\<close> |
|
2971 |
by (simp add: take_bit_signed_take_bit take_bit_minus) |
|
2972 |
then show ?thesis |
|
2973 |
by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_minus) |
|
2974 |
qed |
|
2975 |
||
2976 |
lemma signed_take_bit_mult: |
|
2977 |
\<open>signed_take_bit n (signed_take_bit n k * signed_take_bit n l) = signed_take_bit n (k * l)\<close> |
|
72241 | 2978 |
for k l :: int |
72187 | 2979 |
proof - |
2980 |
have \<open>take_bit (Suc n) |
|
2981 |
(take_bit (Suc n) (signed_take_bit n k) * |
|
2982 |
take_bit (Suc n) (signed_take_bit n l)) = |
|
2983 |
take_bit (Suc n) (k * l)\<close> |
|
2984 |
by (simp add: take_bit_signed_take_bit take_bit_mult) |
|
2985 |
then show ?thesis |
|
2986 |
by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_mult) |
|
2987 |
qed |
|
2988 |
||
72010 | 2989 |
lemma signed_take_bit_eq_take_bit_minus: |
2990 |
\<open>signed_take_bit n k = take_bit (Suc n) k - 2 ^ Suc n * of_bool (bit k n)\<close> |
|
72241 | 2991 |
for k :: int |
72010 | 2992 |
proof (cases \<open>bit k n\<close>) |
2993 |
case True |
|
2994 |
have \<open>signed_take_bit n k = take_bit (Suc n) k OR NOT (mask (Suc n))\<close> |
|
2995 |
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) |
|
2996 |
then have \<open>signed_take_bit n k = take_bit (Suc n) k + NOT (mask (Suc n))\<close> |
|
2997 |
by (simp add: disjunctive_add bit_take_bit_iff bit_not_iff bit_mask_iff) |
|
2998 |
with True show ?thesis |
|
2999 |
by (simp flip: minus_exp_eq_not_mask) |
|
3000 |
next |
|
3001 |
case False |
|
72241 | 3002 |
show ?thesis |
3003 |
by (rule bit_eqI) (simp add: False bit_signed_take_bit_iff bit_take_bit_iff min_def less_Suc_eq) |
|
72010 | 3004 |
qed |
3005 |
||
3006 |
lemma signed_take_bit_eq_take_bit_shift: |
|
3007 |
\<open>signed_take_bit n k = take_bit (Suc n) (k + 2 ^ n) - 2 ^ n\<close> |
|
72241 | 3008 |
for k :: int |
72010 | 3009 |
proof - |
3010 |
have *: \<open>take_bit n k OR 2 ^ n = take_bit n k + 2 ^ n\<close> |
|
3011 |
by (simp add: disjunctive_add bit_exp_iff bit_take_bit_iff) |
|
3012 |
have \<open>take_bit n k - 2 ^ n = take_bit n k + NOT (mask n)\<close> |
|
3013 |
by (simp add: minus_exp_eq_not_mask) |
|
3014 |
also have \<open>\<dots> = take_bit n k OR NOT (mask n)\<close> |
|
3015 |
by (rule disjunctive_add) |
|
3016 |
(simp add: bit_exp_iff bit_take_bit_iff bit_not_iff bit_mask_iff) |
|
3017 |
finally have **: \<open>take_bit n k - 2 ^ n = take_bit n k OR NOT (mask n)\<close> . |
|
3018 |
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> |
|
3019 |
by (simp only: take_bit_add) |
|
3020 |
also have \<open>take_bit (Suc n) k = 2 ^ n * of_bool (bit k n) + take_bit n k\<close> |
|
3021 |
by (simp add: take_bit_Suc_from_most) |
|
3022 |
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> |
|
3023 |
by (simp add: ac_simps) |
|
3024 |
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> |
|
3025 |
by (rule disjunctive_add) |
|
3026 |
(auto simp add: disjunctive_add bit_take_bit_iff bit_double_iff bit_exp_iff) |
|
3027 |
finally show ?thesis |
|
72241 | 3028 |
using * ** by (simp add: signed_take_bit_def concat_bit_Suc min_def ac_simps) |
72010 | 3029 |
qed |
3030 |
||
3031 |
lemma signed_take_bit_nonnegative_iff [simp]: |
|
3032 |
\<open>0 \<le> signed_take_bit n k \<longleftrightarrow> \<not> bit k n\<close> |
|
72241 | 3033 |
for k :: int |
72028 | 3034 |
by (simp add: signed_take_bit_def not_less concat_bit_def) |
72010 | 3035 |
|
3036 |
lemma signed_take_bit_negative_iff [simp]: |
|
3037 |
\<open>signed_take_bit n k < 0 \<longleftrightarrow> bit k n\<close> |
|
72241 | 3038 |
for k :: int |
72028 | 3039 |
by (simp add: signed_take_bit_def not_less concat_bit_def) |
72010 | 3040 |
|
73868 | 3041 |
lemma signed_take_bit_int_greater_eq_minus_exp [simp]: |
3042 |
\<open>- (2 ^ n) \<le> signed_take_bit n k\<close> |
|
3043 |
for k :: int |
|
3044 |
by (simp add: signed_take_bit_eq_take_bit_shift) |
|
3045 |
||
3046 |
lemma signed_take_bit_int_less_exp [simp]: |
|
3047 |
\<open>signed_take_bit n k < 2 ^ n\<close> |
|
3048 |
for k :: int |
|
3049 |
using take_bit_int_less_exp [of \<open>Suc n\<close>] |
|
3050 |
by (simp add: signed_take_bit_eq_take_bit_shift) |
|
3051 |
||
72261 | 3052 |
lemma signed_take_bit_int_eq_self_iff: |
3053 |
\<open>signed_take_bit n k = k \<longleftrightarrow> - (2 ^ n) \<le> k \<and> k < 2 ^ n\<close> |
|
3054 |
for k :: int |
|
3055 |
by (auto simp add: signed_take_bit_eq_take_bit_shift take_bit_int_eq_self_iff algebra_simps) |
|
3056 |
||
72262 | 3057 |
lemma signed_take_bit_int_eq_self: |
3058 |
\<open>signed_take_bit n k = k\<close> if \<open>- (2 ^ n) \<le> k\<close> \<open>k < 2 ^ n\<close> |
|
3059 |
for k :: int |
|
3060 |
using that by (simp add: signed_take_bit_int_eq_self_iff) |
|
3061 |
||
72261 | 3062 |
lemma signed_take_bit_int_less_eq_self_iff: |
3063 |
\<open>signed_take_bit n k \<le> k \<longleftrightarrow> - (2 ^ n) \<le> k\<close> |
|
3064 |
for k :: int |
|
3065 |
by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_less_eq_self_iff algebra_simps) |
|
3066 |
linarith |
|
3067 |
||
3068 |
lemma signed_take_bit_int_less_self_iff: |
|
3069 |
\<open>signed_take_bit n k < k \<longleftrightarrow> 2 ^ n \<le> k\<close> |
|
3070 |
for k :: int |
|
3071 |
by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_less_self_iff algebra_simps) |
|
3072 |
||
3073 |
lemma signed_take_bit_int_greater_self_iff: |
|
3074 |
\<open>k < signed_take_bit n k \<longleftrightarrow> k < - (2 ^ n)\<close> |
|
3075 |
for k :: int |
|
3076 |
by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_greater_self_iff algebra_simps) |
|
3077 |
linarith |
|
3078 |
||
3079 |
lemma signed_take_bit_int_greater_eq_self_iff: |
|
3080 |
\<open>k \<le> signed_take_bit n k \<longleftrightarrow> k < 2 ^ n\<close> |
|
3081 |
for k :: int |
|
3082 |
by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_greater_eq_self_iff algebra_simps) |
|
3083 |
||
3084 |
lemma signed_take_bit_int_greater_eq: |
|
72010 | 3085 |
\<open>k + 2 ^ Suc n \<le> signed_take_bit n k\<close> if \<open>k < - (2 ^ n)\<close> |
72241 | 3086 |
for k :: int |
72262 | 3087 |
using that take_bit_int_greater_eq [of \<open>k + 2 ^ n\<close> \<open>Suc n\<close>] |
72010 | 3088 |
by (simp add: signed_take_bit_eq_take_bit_shift) |
3089 |
||
72261 | 3090 |
lemma signed_take_bit_int_less_eq: |
72010 | 3091 |
\<open>signed_take_bit n k \<le> k - 2 ^ Suc n\<close> if \<open>k \<ge> 2 ^ n\<close> |
72241 | 3092 |
for k :: int |
72262 | 3093 |
using that take_bit_int_less_eq [of \<open>Suc n\<close> \<open>k + 2 ^ n\<close>] |
72010 | 3094 |
by (simp add: signed_take_bit_eq_take_bit_shift) |
3095 |
||
3096 |
lemma signed_take_bit_Suc_bit0 [simp]: |
|
72241 | 3097 |
\<open>signed_take_bit (Suc n) (numeral (Num.Bit0 k)) = signed_take_bit n (numeral k) * (2 :: int)\<close> |
72010 | 3098 |
by (simp add: signed_take_bit_Suc) |
3099 |
||
3100 |
lemma signed_take_bit_Suc_bit1 [simp]: |
|
72241 | 3101 |
\<open>signed_take_bit (Suc n) (numeral (Num.Bit1 k)) = signed_take_bit n (numeral k) * 2 + (1 :: int)\<close> |
72010 | 3102 |
by (simp add: signed_take_bit_Suc) |
3103 |
||
3104 |
lemma signed_take_bit_Suc_minus_bit0 [simp]: |
|
72241 | 3105 |
\<open>signed_take_bit (Suc n) (- numeral (Num.Bit0 k)) = signed_take_bit n (- numeral k) * (2 :: int)\<close> |
72010 | 3106 |
by (simp add: signed_take_bit_Suc) |
3107 |
||
3108 |
lemma signed_take_bit_Suc_minus_bit1 [simp]: |
|
72241 | 3109 |
\<open>signed_take_bit (Suc n) (- numeral (Num.Bit1 k)) = signed_take_bit n (- numeral k - 1) * 2 + (1 :: int)\<close> |
72010 | 3110 |
by (simp add: signed_take_bit_Suc) |
3111 |
||
3112 |
lemma signed_take_bit_numeral_bit0 [simp]: |
|
72241 | 3113 |
\<open>signed_take_bit (numeral l) (numeral (Num.Bit0 k)) = signed_take_bit (pred_numeral l) (numeral k) * (2 :: int)\<close> |
72010 | 3114 |
by (simp add: signed_take_bit_rec) |
3115 |
||
3116 |
lemma signed_take_bit_numeral_bit1 [simp]: |
|
72241 | 3117 |
\<open>signed_take_bit (numeral l) (numeral (Num.Bit1 k)) = signed_take_bit (pred_numeral l) (numeral k) * 2 + (1 :: int)\<close> |
72010 | 3118 |
by (simp add: signed_take_bit_rec) |
3119 |
||
3120 |
lemma signed_take_bit_numeral_minus_bit0 [simp]: |
|
72241 | 3121 |
\<open>signed_take_bit (numeral l) (- numeral (Num.Bit0 k)) = signed_take_bit (pred_numeral l) (- numeral k) * (2 :: int)\<close> |
72010 | 3122 |
by (simp add: signed_take_bit_rec) |
3123 |
||
3124 |
lemma signed_take_bit_numeral_minus_bit1 [simp]: |
|
72241 | 3125 |
\<open>signed_take_bit (numeral l) (- numeral (Num.Bit1 k)) = signed_take_bit (pred_numeral l) (- numeral k - 1) * 2 + (1 :: int)\<close> |
72010 | 3126 |
by (simp add: signed_take_bit_rec) |
3127 |
||
3128 |
lemma signed_take_bit_code [code]: |
|
72241 | 3129 |
\<open>signed_take_bit n a = |
3130 |
(let l = take_bit (Suc n) a |
|
3131 |
in if bit l n then l + push_bit (Suc n) (- 1) else l)\<close> |
|
72010 | 3132 |
proof - |
72241 | 3133 |
have *: \<open>take_bit (Suc n) a + push_bit n (- 2) = |
3134 |
take_bit (Suc n) a OR NOT (mask (Suc n))\<close> |
|
3135 |
by (auto simp add: bit_take_bit_iff bit_push_bit_iff bit_not_iff bit_mask_iff disjunctive_add |
|
3136 |
simp flip: push_bit_minus_one_eq_not_mask) |
|
72010 | 3137 |
show ?thesis |
3138 |
by (rule bit_eqI) |
|
72241 | 3139 |
(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 | 3140 |
qed |
3141 |
||
3142 |
||
71956 | 3143 |
subsection \<open>Instance \<^typ>\<open>nat\<close>\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3144 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3145 |
instantiation nat :: semiring_bit_operations |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3146 |
begin |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3147 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3148 |
definition and_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3149 |
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
|
3150 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3151 |
definition or_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3152 |
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
|
3153 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3154 |
definition xor_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3155 |
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
|
3156 |
|
72082 | 3157 |
definition mask_nat :: \<open>nat \<Rightarrow> nat\<close> |
3158 |
where \<open>mask n = (2 :: nat) ^ n - 1\<close> |
|
3159 |
||
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3160 |
definition set_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3161 |
where \<open>set_bit m n = n OR push_bit m 1\<close> for m n :: nat |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3162 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3163 |
definition unset_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3164 |
where \<open>unset_bit m n = (if bit n m then n - push_bit m 1 else n)\<close> for m n :: nat |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3165 |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3166 |
definition flip_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3167 |
where \<open>flip_bit m n = n XOR push_bit m 1\<close> for m n :: nat |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3168 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3169 |
instance proof |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3170 |
fix m n q :: nat |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3171 |
show \<open>bit (m AND n) q \<longleftrightarrow> bit m q \<and> bit n q\<close> |
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
3172 |
by (simp add: and_nat_def bit_simps) |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3173 |
show \<open>bit (m OR n) q \<longleftrightarrow> bit m q \<or> bit n q\<close> |
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
3174 |
by (simp add: or_nat_def bit_simps) |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3175 |
show \<open>bit (m XOR n) q \<longleftrightarrow> bit m q \<noteq> bit n q\<close> |
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
3176 |
by (simp add: xor_nat_def bit_simps) |
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3177 |
show \<open>bit (unset_bit m n) q \<longleftrightarrow> bit n q \<and> m \<noteq> q\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3178 |
proof (cases \<open>bit n m\<close>) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3179 |
case False |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3180 |
then show ?thesis by (auto simp add: unset_bit_nat_def) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3181 |
next |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3182 |
case True |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3183 |
have \<open>push_bit m (drop_bit m n) + take_bit m n = n\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3184 |
by (fact bits_ident) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3185 |
also from \<open>bit n m\<close> have \<open>drop_bit m n = 2 * drop_bit (Suc m) n + 1\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3186 |
by (simp add: drop_bit_Suc drop_bit_half even_drop_bit_iff_not_bit ac_simps) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3187 |
finally have \<open>push_bit m (2 * drop_bit (Suc m) n) + take_bit m n + push_bit m 1 = n\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3188 |
by (simp only: push_bit_add ac_simps) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3189 |
then have \<open>n - push_bit m 1 = push_bit m (2 * drop_bit (Suc m) n) + take_bit m n\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3190 |
by simp |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3191 |
then have \<open>n - push_bit m 1 = push_bit m (2 * drop_bit (Suc m) n) OR take_bit m n\<close> |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3192 |
by (simp add: or_nat_def bit_simps flip: disjunctive_add) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3193 |
with \<open>bit n m\<close> show ?thesis |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3194 |
by (auto simp add: unset_bit_nat_def or_nat_def bit_simps) |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3195 |
qed |
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
3196 |
qed (simp_all add: mask_nat_def set_bit_nat_def flip_bit_nat_def) |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3197 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3198 |
end |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3199 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3200 |
lemma and_nat_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3201 |
\<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
|
3202 |
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
|
3203 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3204 |
lemma or_nat_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3205 |
\<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
|
3206 |
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
|
3207 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3208 |
lemma xor_nat_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3209 |
\<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
|
3210 |
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
|
3211 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3212 |
lemma Suc_0_and_eq [simp]: |
71822 | 3213 |
\<open>Suc 0 AND n = n mod 2\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3214 |
using one_and_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3215 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3216 |
lemma and_Suc_0_eq [simp]: |
71822 | 3217 |
\<open>n AND Suc 0 = n mod 2\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3218 |
using and_one_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3219 |
|
71822 | 3220 |
lemma Suc_0_or_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3221 |
\<open>Suc 0 OR n = n + of_bool (even n)\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3222 |
using one_or_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3223 |
|
71822 | 3224 |
lemma or_Suc_0_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3225 |
\<open>n OR Suc 0 = n + of_bool (even n)\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3226 |
using or_one_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3227 |
|
71822 | 3228 |
lemma Suc_0_xor_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3229 |
\<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
|
3230 |
using one_xor_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3231 |
|
71822 | 3232 |
lemma xor_Suc_0_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3233 |
\<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
|
3234 |
using xor_one_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3235 |
|
72227 | 3236 |
context semiring_bit_operations |
3237 |
begin |
|
3238 |
||
3239 |
lemma of_nat_and_eq: |
|
3240 |
\<open>of_nat (m AND n) = of_nat m AND of_nat n\<close> |
|
3241 |
by (rule bit_eqI) (simp add: bit_of_nat_iff bit_and_iff Bit_Operations.bit_and_iff) |
|
3242 |
||
3243 |
lemma of_nat_or_eq: |
|
3244 |
\<open>of_nat (m OR n) = of_nat m OR of_nat n\<close> |
|
3245 |
by (rule bit_eqI) (simp add: bit_of_nat_iff bit_or_iff Bit_Operations.bit_or_iff) |
|
3246 |
||
3247 |
lemma of_nat_xor_eq: |
|
3248 |
\<open>of_nat (m XOR n) = of_nat m XOR of_nat n\<close> |
|
3249 |
by (rule bit_eqI) (simp add: bit_of_nat_iff bit_xor_iff Bit_Operations.bit_xor_iff) |
|
3250 |
||
3251 |
end |
|
3252 |
||
3253 |
context ring_bit_operations |
|
3254 |
begin |
|
3255 |
||
3256 |
lemma of_nat_mask_eq: |
|
3257 |
\<open>of_nat (mask n) = mask n\<close> |
|
3258 |
by (induction n) (simp_all add: mask_Suc_double Bit_Operations.mask_Suc_double of_nat_or_eq) |
|
3259 |
||
3260 |
end |
|
3261 |
||
72830 | 3262 |
lemma Suc_mask_eq_exp: |
3263 |
\<open>Suc (mask n) = 2 ^ n\<close> |
|
3264 |
by (simp add: mask_eq_exp_minus_1) |
|
3265 |
||
3266 |
lemma less_eq_mask: |
|
3267 |
\<open>n \<le> mask n\<close> |
|
3268 |
by (simp add: mask_eq_exp_minus_1 le_diff_conv2) |
|
3269 |
(metis Suc_mask_eq_exp diff_Suc_1 diff_le_diff_pow diff_zero le_refl not_less_eq_eq power_0) |
|
3270 |
||
3271 |
lemma less_mask: |
|
3272 |
\<open>n < mask n\<close> if \<open>Suc 0 < n\<close> |
|
3273 |
proof - |
|
3274 |
define m where \<open>m = n - 2\<close> |
|
3275 |
with that have *: \<open>n = m + 2\<close> |
|
3276 |
by simp |
|
3277 |
have \<open>Suc (Suc (Suc m)) < 4 * 2 ^ m\<close> |
|
3278 |
by (induction m) simp_all |
|
3279 |
then have \<open>Suc (m + 2) < Suc (mask (m + 2))\<close> |
|
3280 |
by (simp add: Suc_mask_eq_exp) |
|
3281 |
then have \<open>m + 2 < mask (m + 2)\<close> |
|
3282 |
by (simp add: less_le) |
|
3283 |
with * show ?thesis |
|
3284 |
by simp |
|
3285 |
qed |
|
3286 |
||
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
3287 |
|
74101 | 3288 |
subsection \<open>Horner sums\<close> |
3289 |
||
3290 |
context semiring_bit_shifts |
|
3291 |
begin |
|
3292 |
||
3293 |
lemma horner_sum_bit_eq_take_bit: |
|
3294 |
\<open>horner_sum of_bool 2 (map (bit a) [0..<n]) = take_bit n a\<close> |
|
3295 |
proof (induction a arbitrary: n rule: bits_induct) |
|
3296 |
case (stable a) |
|
3297 |
moreover have \<open>bit a = (\<lambda>_. odd a)\<close> |
|
3298 |
using stable by (simp add: stable_imp_bit_iff_odd fun_eq_iff) |
|
3299 |
moreover have \<open>{q. q < n} = {0..<n}\<close> |
|
3300 |
by auto |
|
3301 |
ultimately show ?case |
|
3302 |
by (simp add: stable_imp_take_bit_eq horner_sum_eq_sum mask_eq_sum_exp) |
|
3303 |
next |
|
3304 |
case (rec a b) |
|
3305 |
show ?case |
|
3306 |
proof (cases n) |
|
3307 |
case 0 |
|
3308 |
then show ?thesis |
|
3309 |
by simp |
|
3310 |
next |
|
3311 |
case (Suc m) |
|
3312 |
have \<open>map (bit (of_bool b + 2 * a)) [0..<Suc m] = b # map (bit (of_bool b + 2 * a)) [Suc 0..<Suc m]\<close> |
|
3313 |
by (simp only: upt_conv_Cons) simp |
|
3314 |
also have \<open>\<dots> = b # map (bit a) [0..<m]\<close> |
|
3315 |
by (simp only: flip: map_Suc_upt) (simp add: bit_Suc rec.hyps) |
|
3316 |
finally show ?thesis |
|
3317 |
using Suc rec.IH [of m] by (simp add: take_bit_Suc rec.hyps) |
|
3318 |
(simp_all add: ac_simps mod_2_eq_odd) |
|
3319 |
qed |
|
3320 |
qed |
|
3321 |
||
3322 |
end |
|
3323 |
||
3324 |
context unique_euclidean_semiring_with_bit_shifts |
|
3325 |
begin |
|
3326 |
||
3327 |
lemma bit_horner_sum_bit_iff [bit_simps]: |
|
3328 |
\<open>bit (horner_sum of_bool 2 bs) n \<longleftrightarrow> n < length bs \<and> bs ! n\<close> |
|
3329 |
proof (induction bs arbitrary: n) |
|
3330 |
case Nil |
|
3331 |
then show ?case |
|
3332 |
by simp |
|
3333 |
next |
|
3334 |
case (Cons b bs) |
|
3335 |
show ?case |
|
3336 |
proof (cases n) |
|
3337 |
case 0 |
|
3338 |
then show ?thesis |
|
3339 |
by simp |
|
3340 |
next |
|
3341 |
case (Suc m) |
|
3342 |
with bit_rec [of _ n] Cons.prems Cons.IH [of m] |
|
3343 |
show ?thesis by simp |
|
3344 |
qed |
|
3345 |
qed |
|
3346 |
||
3347 |
lemma take_bit_horner_sum_bit_eq: |
|
3348 |
\<open>take_bit n (horner_sum of_bool 2 bs) = horner_sum of_bool 2 (take n bs)\<close> |
|
3349 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_horner_sum_bit_iff) |
|
3350 |
||
3351 |
end |
|
3352 |
||
3353 |
lemma horner_sum_of_bool_2_less: |
|
3354 |
\<open>(horner_sum of_bool 2 bs :: int) < 2 ^ length bs\<close> |
|
3355 |
proof - |
|
3356 |
have \<open>(\<Sum>n = 0..<length bs. of_bool (bs ! n) * (2::int) ^ n) \<le> (\<Sum>n = 0..<length bs. 2 ^ n)\<close> |
|
3357 |
by (rule sum_mono) simp |
|
3358 |
also have \<open>\<dots> = 2 ^ length bs - 1\<close> |
|
3359 |
by (induction bs) simp_all |
|
3360 |
finally show ?thesis |
|
3361 |
by (simp add: horner_sum_eq_sum) |
|
3362 |
qed |
|
3363 |
||
3364 |
||
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3365 |
subsection \<open>Symbolic computations on numeral expressions\<close> \<^marker>\<open>contributor \<open>Andreas Lochbihler\<close>\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3366 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3367 |
fun and_num :: \<open>num \<Rightarrow> num \<Rightarrow> num option\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3368 |
where |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3369 |
\<open>and_num num.One num.One = Some num.One\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3370 |
| \<open>and_num num.One (num.Bit0 n) = None\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3371 |
| \<open>and_num num.One (num.Bit1 n) = Some num.One\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3372 |
| \<open>and_num (num.Bit0 m) num.One = None\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3373 |
| \<open>and_num (num.Bit0 m) (num.Bit0 n) = map_option num.Bit0 (and_num m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3374 |
| \<open>and_num (num.Bit0 m) (num.Bit1 n) = map_option num.Bit0 (and_num m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3375 |
| \<open>and_num (num.Bit1 m) num.One = Some num.One\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3376 |
| \<open>and_num (num.Bit1 m) (num.Bit0 n) = map_option num.Bit0 (and_num m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3377 |
| \<open>and_num (num.Bit1 m) (num.Bit1 n) = (case and_num m n of None \<Rightarrow> Some num.One | Some n' \<Rightarrow> Some (num.Bit1 n'))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3378 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3379 |
fun and_not_num :: \<open>num \<Rightarrow> num \<Rightarrow> num option\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3380 |
where |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3381 |
\<open>and_not_num num.One num.One = None\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3382 |
| \<open>and_not_num num.One (num.Bit0 n) = Some num.One\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3383 |
| \<open>and_not_num num.One (num.Bit1 n) = None\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3384 |
| \<open>and_not_num (num.Bit0 m) num.One = Some (num.Bit0 m)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3385 |
| \<open>and_not_num (num.Bit0 m) (num.Bit0 n) = map_option num.Bit0 (and_not_num m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3386 |
| \<open>and_not_num (num.Bit0 m) (num.Bit1 n) = map_option num.Bit0 (and_not_num m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3387 |
| \<open>and_not_num (num.Bit1 m) num.One = Some (num.Bit0 m)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3388 |
| \<open>and_not_num (num.Bit1 m) (num.Bit0 n) = (case and_not_num m n of None \<Rightarrow> Some num.One | Some n' \<Rightarrow> Some (num.Bit1 n'))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3389 |
| \<open>and_not_num (num.Bit1 m) (num.Bit1 n) = map_option num.Bit0 (and_not_num m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3390 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3391 |
fun or_num :: \<open>num \<Rightarrow> num \<Rightarrow> num\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3392 |
where |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3393 |
\<open>or_num num.One num.One = num.One\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3394 |
| \<open>or_num num.One (num.Bit0 n) = num.Bit1 n\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3395 |
| \<open>or_num num.One (num.Bit1 n) = num.Bit1 n\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3396 |
| \<open>or_num (num.Bit0 m) num.One = num.Bit1 m\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3397 |
| \<open>or_num (num.Bit0 m) (num.Bit0 n) = num.Bit0 (or_num m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3398 |
| \<open>or_num (num.Bit0 m) (num.Bit1 n) = num.Bit1 (or_num m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3399 |
| \<open>or_num (num.Bit1 m) num.One = num.Bit1 m\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3400 |
| \<open>or_num (num.Bit1 m) (num.Bit0 n) = num.Bit1 (or_num m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3401 |
| \<open>or_num (num.Bit1 m) (num.Bit1 n) = num.Bit1 (or_num m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3402 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3403 |
fun or_not_num_neg :: \<open>num \<Rightarrow> num \<Rightarrow> num\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3404 |
where |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3405 |
\<open>or_not_num_neg num.One num.One = num.One\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3406 |
| \<open>or_not_num_neg num.One (num.Bit0 m) = num.Bit1 m\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3407 |
| \<open>or_not_num_neg num.One (num.Bit1 m) = num.Bit1 m\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3408 |
| \<open>or_not_num_neg (num.Bit0 n) num.One = num.Bit0 num.One\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3409 |
| \<open>or_not_num_neg (num.Bit0 n) (num.Bit0 m) = Num.BitM (or_not_num_neg n m)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3410 |
| \<open>or_not_num_neg (num.Bit0 n) (num.Bit1 m) = num.Bit0 (or_not_num_neg n m)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3411 |
| \<open>or_not_num_neg (num.Bit1 n) num.One = num.One\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3412 |
| \<open>or_not_num_neg (num.Bit1 n) (num.Bit0 m) = Num.BitM (or_not_num_neg n m)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3413 |
| \<open>or_not_num_neg (num.Bit1 n) (num.Bit1 m) = Num.BitM (or_not_num_neg n m)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3414 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3415 |
fun xor_num :: \<open>num \<Rightarrow> num \<Rightarrow> num option\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3416 |
where |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3417 |
\<open>xor_num num.One num.One = None\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3418 |
| \<open>xor_num num.One (num.Bit0 n) = Some (num.Bit1 n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3419 |
| \<open>xor_num num.One (num.Bit1 n) = Some (num.Bit0 n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3420 |
| \<open>xor_num (num.Bit0 m) num.One = Some (num.Bit1 m)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3421 |
| \<open>xor_num (num.Bit0 m) (num.Bit0 n) = map_option num.Bit0 (xor_num m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3422 |
| \<open>xor_num (num.Bit0 m) (num.Bit1 n) = Some (case xor_num m n of None \<Rightarrow> num.One | Some n' \<Rightarrow> num.Bit1 n')\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3423 |
| \<open>xor_num (num.Bit1 m) num.One = Some (num.Bit0 m)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3424 |
| \<open>xor_num (num.Bit1 m) (num.Bit0 n) = Some (case xor_num m n of None \<Rightarrow> num.One | Some n' \<Rightarrow> num.Bit1 n')\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3425 |
| \<open>xor_num (num.Bit1 m) (num.Bit1 n) = map_option num.Bit0 (xor_num m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3426 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3427 |
lemma int_numeral_and_num: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3428 |
\<open>numeral m AND numeral n = (case and_num m n of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3429 |
by (induction m n rule: and_num.induct) (simp_all split: option.split) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3430 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3431 |
lemma and_num_eq_None_iff: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3432 |
\<open>and_num m n = None \<longleftrightarrow> numeral m AND numeral n = (0::int)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3433 |
by (simp add: int_numeral_and_num split: option.split) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3434 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3435 |
lemma and_num_eq_Some_iff: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3436 |
\<open>and_num m n = Some q \<longleftrightarrow> numeral m AND numeral n = (numeral q :: int)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3437 |
by (simp add: int_numeral_and_num split: option.split) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3438 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3439 |
lemma int_numeral_and_not_num: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3440 |
\<open>numeral m AND NOT (numeral n) = (case and_not_num m n of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3441 |
by (induction m n rule: and_not_num.induct) (simp_all add: add_One BitM_inc_eq not_int_def split: option.split) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3442 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3443 |
lemma int_numeral_not_and_num: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3444 |
\<open>NOT (numeral m) AND numeral n = (case and_not_num n m of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3445 |
using int_numeral_and_not_num [of n m] by (simp add: ac_simps) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3446 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3447 |
lemma and_not_num_eq_None_iff: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3448 |
\<open>and_not_num m n = None \<longleftrightarrow> numeral m AND NOT (numeral n) = (0::int)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3449 |
by (simp add: int_numeral_and_not_num split: option.split) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3450 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3451 |
lemma and_not_num_eq_Some_iff: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3452 |
\<open>and_not_num m n = Some q \<longleftrightarrow> numeral m AND NOT (numeral n) = (numeral q :: int)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3453 |
by (simp add: int_numeral_and_not_num split: option.split) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3454 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3455 |
lemma int_numeral_or_num: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3456 |
\<open>numeral m OR numeral n = (numeral (or_num m n) :: int)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3457 |
by (induction m n rule: or_num.induct) simp_all |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3458 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3459 |
lemma numeral_or_num_eq: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3460 |
\<open>numeral (or_num m n) = (numeral m OR numeral n :: int)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3461 |
by (simp add: int_numeral_or_num) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3462 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3463 |
lemma int_numeral_or_not_num_neg: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3464 |
\<open>numeral m OR NOT (numeral n :: int) = - numeral (or_not_num_neg m n)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3465 |
by (induction m n rule: or_not_num_neg.induct) (simp_all add: add_One BitM_inc_eq not_int_def) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3466 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3467 |
lemma int_numeral_not_or_num_neg: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3468 |
\<open>NOT (numeral m) OR (numeral n :: int) = - numeral (or_not_num_neg n m)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3469 |
using int_numeral_or_not_num_neg [of n m] by (simp add: ac_simps) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3470 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3471 |
lemma numeral_or_not_num_eq: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3472 |
\<open>numeral (or_not_num_neg m n) = - (numeral m OR NOT (numeral n :: int))\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3473 |
using int_numeral_or_not_num_neg [of m n] by simp |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3474 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3475 |
lemma int_numeral_xor_num: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3476 |
\<open>numeral m XOR numeral n = (case xor_num m n of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3477 |
by (induction m n rule: xor_num.induct) (simp_all split: option.split) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3478 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3479 |
lemma xor_num_eq_None_iff: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3480 |
\<open>xor_num m n = None \<longleftrightarrow> numeral m XOR numeral n = (0::int)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3481 |
by (simp add: int_numeral_xor_num split: option.split) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3482 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3483 |
lemma xor_num_eq_Some_iff: |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3484 |
\<open>xor_num m n = Some q \<longleftrightarrow> numeral m XOR numeral n = (numeral q :: int)\<close> |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3485 |
by (simp add: int_numeral_xor_num split: option.split) |
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3486 |
|
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
3487 |
|
71800 | 3488 |
subsection \<open>Key ideas of bit operations\<close> |
3489 |
||
3490 |
text \<open> |
|
3491 |
When formalizing bit operations, it is tempting to represent |
|
3492 |
bit values as explicit lists over a binary type. This however |
|
3493 |
is a bad idea, mainly due to the inherent ambiguities in |
|
3494 |
representation concerning repeating leading bits. |
|
3495 |
||
3496 |
Hence this approach avoids such explicit lists altogether |
|
3497 |
following an algebraic path: |
|
3498 |
||
3499 |
\<^item> Bit values are represented by numeric types: idealized |
|
3500 |
unbounded bit values can be represented by type \<^typ>\<open>int\<close>, |
|
3501 |
bounded bit values by quotient types over \<^typ>\<open>int\<close>. |
|
3502 |
||
3503 |
\<^item> (A special case are idealized unbounded bit values ending |
|
3504 |
in @{term [source] 0} which can be represented by type \<^typ>\<open>nat\<close> but |
|
3505 |
only support a restricted set of operations). |
|
3506 |
||
3507 |
\<^item> From this idea follows that |
|
3508 |
||
3509 |
\<^item> multiplication by \<^term>\<open>2 :: int\<close> is a bit shift to the left and |
|
3510 |
||
3511 |
\<^item> division by \<^term>\<open>2 :: int\<close> is a bit shift to the right. |
|
3512 |
||
3513 |
\<^item> Concerning bounded bit values, iterated shifts to the left |
|
3514 |
may result in eliminating all bits by shifting them all |
|
3515 |
beyond the boundary. The property \<^prop>\<open>(2 :: int) ^ n \<noteq> 0\<close> |
|
3516 |
represents that \<^term>\<open>n\<close> is \<^emph>\<open>not\<close> beyond that boundary. |
|
3517 |
||
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71956
diff
changeset
|
3518 |
\<^item> The projection on a single bit is then @{thm bit_iff_odd [where ?'a = int, no_vars]}. |
71800 | 3519 |
|
3520 |
\<^item> This leads to the most fundamental properties of bit values: |
|
3521 |
||
3522 |
\<^item> Equality rule: @{thm bit_eqI [where ?'a = int, no_vars]} |
|
3523 |
||
3524 |
\<^item> Induction rule: @{thm bits_induct [where ?'a = int, no_vars]} |
|
3525 |
||
3526 |
\<^item> Typical operations are characterized as follows: |
|
3527 |
||
3528 |
\<^item> Singleton \<^term>\<open>n\<close>th bit: \<^term>\<open>(2 :: int) ^ n\<close> |
|
3529 |
||
71956 | 3530 |
\<^item> Bit mask upto bit \<^term>\<open>n\<close>: @{thm mask_eq_exp_minus_1 [where ?'a = int, no_vars]} |
71800 | 3531 |
|
3532 |
\<^item> Left shift: @{thm push_bit_eq_mult [where ?'a = int, no_vars]} |
|
3533 |
||
3534 |
\<^item> Right shift: @{thm drop_bit_eq_div [where ?'a = int, no_vars]} |
|
3535 |
||
3536 |
\<^item> Truncation: @{thm take_bit_eq_mod [where ?'a = int, no_vars]} |
|
3537 |
||
3538 |
\<^item> Negation: @{thm bit_not_iff [where ?'a = int, no_vars]} |
|
3539 |
||
3540 |
\<^item> And: @{thm bit_and_iff [where ?'a = int, no_vars]} |
|
3541 |
||
3542 |
\<^item> Or: @{thm bit_or_iff [where ?'a = int, no_vars]} |
|
3543 |
||
3544 |
\<^item> Xor: @{thm bit_xor_iff [where ?'a = int, no_vars]} |
|
3545 |
||
3546 |
\<^item> Set a single bit: @{thm set_bit_def [where ?'a = int, no_vars]} |
|
3547 |
||
3548 |
\<^item> Unset a single bit: @{thm unset_bit_def [where ?'a = int, no_vars]} |
|
3549 |
||
3550 |
\<^item> Flip a single bit: @{thm flip_bit_def [where ?'a = int, no_vars]} |
|
72028 | 3551 |
|
72241 | 3552 |
\<^item> Signed truncation, or modulus centered around \<^term>\<open>0::int\<close>: @{thm signed_take_bit_def [no_vars]} |
72028 | 3553 |
|
72241 | 3554 |
\<^item> Bit concatenation: @{thm concat_bit_def [no_vars]} |
72028 | 3555 |
|
3556 |
\<^item> (Bounded) conversion from and to a list of bits: @{thm horner_sum_bit_eq_take_bit [where ?'a = int, no_vars]} |
|
71800 | 3557 |
\<close> |
3558 |
||
74097 | 3559 |
no_notation |
3560 |
"and" (infixr \<open>AND\<close> 64) |
|
3561 |
and or (infixr \<open>OR\<close> 59) |
|
3562 |
and xor (infixr \<open>XOR\<close> 59) |
|
3563 |
||
3564 |
bundle bit_operations_syntax |
|
74101 | 3565 |
begin |
74097 | 3566 |
|
3567 |
notation |
|
3568 |
"and" (infixr \<open>AND\<close> 64) |
|
3569 |
and or (infixr \<open>OR\<close> 59) |
|
3570 |
and xor (infixr \<open>XOR\<close> 59) |
|
3571 |
||
71442 | 3572 |
end |
74097 | 3573 |
|
3574 |
end |