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