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