author | haftmann |
Fri, 08 May 2020 06:26:29 +0000 | |
changeset 71823 | 214b48a1937b |
parent 71822 | 67cc2319104f |
child 71921 | a238074c5a9d |
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 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
4 |
section \<open>Proof of concept for purely algebraically founded lists of bits\<close> |
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 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
12 |
subsection \<open>Bit operations in suitable algebraic structures\<close> |
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) |
|
71186 | 18 |
assumes bit_and_iff: \<open>\<And>n. bit (a AND b) n \<longleftrightarrow> bit a n \<and> bit b n\<close> |
19 |
and bit_or_iff: \<open>\<And>n. bit (a OR b) n \<longleftrightarrow> bit a n \<or> bit b n\<close> |
|
20 |
and bit_xor_iff: \<open>\<And>n. bit (a XOR b) n \<longleftrightarrow> bit a n \<noteq> bit b n\<close> |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
21 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
22 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
23 |
text \<open> |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
24 |
We want the bitwise operations to bind slightly weaker |
71094 | 25 |
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
|
26 |
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
|
27 |
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
|
28 |
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
|
29 |
\<close> |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
30 |
|
71418 | 31 |
sublocale "and": semilattice \<open>(AND)\<close> |
32 |
by standard (auto simp add: bit_eq_iff bit_and_iff) |
|
33 |
||
34 |
sublocale or: semilattice_neutr \<open>(OR)\<close> 0 |
|
35 |
by standard (auto simp add: bit_eq_iff bit_or_iff) |
|
36 |
||
37 |
sublocale xor: comm_monoid \<open>(XOR)\<close> 0 |
|
38 |
by standard (auto simp add: bit_eq_iff bit_xor_iff) |
|
39 |
||
71823 | 40 |
lemma even_and_iff: |
41 |
\<open>even (a AND b) \<longleftrightarrow> even a \<or> even b\<close> |
|
42 |
using bit_and_iff [of a b 0] by auto |
|
43 |
||
44 |
lemma even_or_iff: |
|
45 |
\<open>even (a OR b) \<longleftrightarrow> even a \<and> even b\<close> |
|
46 |
using bit_or_iff [of a b 0] by auto |
|
47 |
||
48 |
lemma even_xor_iff: |
|
49 |
\<open>even (a XOR b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)\<close> |
|
50 |
using bit_xor_iff [of a b 0] by auto |
|
51 |
||
71412 | 52 |
lemma zero_and_eq [simp]: |
53 |
"0 AND a = 0" |
|
54 |
by (simp add: bit_eq_iff bit_and_iff) |
|
55 |
||
56 |
lemma and_zero_eq [simp]: |
|
57 |
"a AND 0 = 0" |
|
58 |
by (simp add: bit_eq_iff bit_and_iff) |
|
59 |
||
71418 | 60 |
lemma one_and_eq [simp]: |
71822 | 61 |
"1 AND a = a mod 2" |
71418 | 62 |
by (simp add: bit_eq_iff bit_and_iff) (auto simp add: bit_1_iff) |
71412 | 63 |
|
71418 | 64 |
lemma and_one_eq [simp]: |
71822 | 65 |
"a AND 1 = a mod 2" |
71418 | 66 |
using one_and_eq [of a] by (simp add: ac_simps) |
67 |
||
71822 | 68 |
lemma one_or_eq: |
71418 | 69 |
"1 OR a = a + of_bool (even a)" |
70 |
by (simp add: bit_eq_iff bit_or_iff add.commute [of _ 1] even_bit_succ_iff) (auto simp add: bit_1_iff) |
|
71412 | 71 |
|
71822 | 72 |
lemma or_one_eq: |
71418 | 73 |
"a OR 1 = a + of_bool (even a)" |
74 |
using one_or_eq [of a] by (simp add: ac_simps) |
|
71412 | 75 |
|
71822 | 76 |
lemma one_xor_eq: |
71418 | 77 |
"1 XOR a = a + of_bool (even a) - of_bool (odd a)" |
78 |
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) |
|
79 |
||
71822 | 80 |
lemma xor_one_eq: |
71418 | 81 |
"a XOR 1 = a + of_bool (even a) - of_bool (odd a)" |
82 |
using one_xor_eq [of a] by (simp add: ac_simps) |
|
71412 | 83 |
|
71409 | 84 |
lemma take_bit_and [simp]: |
85 |
\<open>take_bit n (a AND b) = take_bit n a AND take_bit n b\<close> |
|
86 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_and_iff) |
|
87 |
||
88 |
lemma take_bit_or [simp]: |
|
89 |
\<open>take_bit n (a OR b) = take_bit n a OR take_bit n b\<close> |
|
90 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_or_iff) |
|
91 |
||
92 |
lemma take_bit_xor [simp]: |
|
93 |
\<open>take_bit n (a XOR b) = take_bit n a XOR take_bit n b\<close> |
|
94 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_xor_iff) |
|
95 |
||
71823 | 96 |
definition mask :: \<open>nat \<Rightarrow> 'a\<close> |
97 |
where mask_eq_exp_minus_1: \<open>mask n = 2 ^ n - 1\<close> |
|
98 |
||
99 |
lemma bit_mask_iff: |
|
100 |
\<open>bit (mask m) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n < m\<close> |
|
101 |
by (simp add: mask_eq_exp_minus_1 bit_mask_iff) |
|
102 |
||
103 |
lemma even_mask_iff: |
|
104 |
\<open>even (mask n) \<longleftrightarrow> n = 0\<close> |
|
105 |
using bit_mask_iff [of n 0] by auto |
|
106 |
||
107 |
lemma mask_0 [simp, code]: |
|
108 |
\<open>mask 0 = 0\<close> |
|
109 |
by (simp add: mask_eq_exp_minus_1) |
|
110 |
||
111 |
lemma mask_Suc_exp [code]: |
|
112 |
\<open>mask (Suc n) = 2 ^ n OR mask n\<close> |
|
113 |
by (rule bit_eqI) |
|
114 |
(auto simp add: bit_or_iff bit_mask_iff bit_exp_iff not_less le_less_Suc_eq) |
|
115 |
||
116 |
lemma mask_Suc_double: |
|
117 |
\<open>mask (Suc n) = 2 * mask n OR 1\<close> |
|
118 |
proof (rule bit_eqI) |
|
119 |
fix q |
|
120 |
assume \<open>2 ^ q \<noteq> 0\<close> |
|
121 |
show \<open>bit (mask (Suc n)) q \<longleftrightarrow> bit (2 * mask n OR 1) q\<close> |
|
122 |
by (cases q) |
|
123 |
(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) |
|
124 |
qed |
|
125 |
||
126 |
lemma take_bit_eq_mask [code]: |
|
127 |
\<open>take_bit n a = a AND mask n\<close> |
|
128 |
by (rule bit_eqI) |
|
129 |
(auto simp add: bit_take_bit_iff bit_and_iff bit_mask_iff) |
|
130 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
131 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
132 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
133 |
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
|
134 |
fixes not :: \<open>'a \<Rightarrow> 'a\<close> (\<open>NOT\<close>) |
71186 | 135 |
assumes bit_not_iff: \<open>\<And>n. bit (NOT a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit a n\<close> |
71409 | 136 |
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
|
137 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
138 |
|
71409 | 139 |
text \<open> |
140 |
For the sake of code generation \<^const>\<open>not\<close> is specified as |
|
141 |
definitional class operation. Note that \<^const>\<open>not\<close> has no |
|
142 |
sensible definition for unlimited but only positive bit strings |
|
143 |
(type \<^typ>\<open>nat\<close>). |
|
144 |
\<close> |
|
145 |
||
71186 | 146 |
lemma bits_minus_1_mod_2_eq [simp]: |
147 |
\<open>(- 1) mod 2 = 1\<close> |
|
148 |
by (simp add: mod_2_eq_odd) |
|
149 |
||
71409 | 150 |
lemma not_eq_complement: |
151 |
\<open>NOT a = - a - 1\<close> |
|
152 |
using minus_eq_not_minus_1 [of \<open>a + 1\<close>] by simp |
|
153 |
||
154 |
lemma minus_eq_not_plus_1: |
|
155 |
\<open>- a = NOT a + 1\<close> |
|
156 |
using not_eq_complement [of a] by simp |
|
157 |
||
158 |
lemma bit_minus_iff: |
|
159 |
\<open>bit (- a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit (a - 1) n\<close> |
|
160 |
by (simp add: minus_eq_not_minus_1 bit_not_iff) |
|
161 |
||
71418 | 162 |
lemma even_not_iff [simp]: |
163 |
"even (NOT a) \<longleftrightarrow> odd a" |
|
164 |
using bit_not_iff [of a 0] by auto |
|
165 |
||
71409 | 166 |
lemma bit_not_exp_iff: |
167 |
\<open>bit (NOT (2 ^ m)) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n \<noteq> m\<close> |
|
168 |
by (auto simp add: bit_not_iff bit_exp_iff) |
|
169 |
||
71186 | 170 |
lemma bit_minus_1_iff [simp]: |
171 |
\<open>bit (- 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0\<close> |
|
71409 | 172 |
by (simp add: bit_minus_iff) |
173 |
||
174 |
lemma bit_minus_exp_iff: |
|
175 |
\<open>bit (- (2 ^ m)) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n \<ge> m\<close> |
|
176 |
oops |
|
177 |
||
178 |
lemma bit_minus_2_iff [simp]: |
|
179 |
\<open>bit (- 2) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n > 0\<close> |
|
180 |
by (simp add: bit_minus_iff bit_1_iff) |
|
71186 | 181 |
|
71418 | 182 |
lemma not_one [simp]: |
183 |
"NOT 1 = - 2" |
|
184 |
by (simp add: bit_eq_iff bit_not_iff) (simp add: bit_1_iff) |
|
185 |
||
186 |
sublocale "and": semilattice_neutr \<open>(AND)\<close> \<open>- 1\<close> |
|
187 |
apply standard |
|
71426 | 188 |
apply (simp add: bit_eq_iff bit_and_iff) |
189 |
apply (auto simp add: exp_eq_0_imp_not_bit bit_exp_iff) |
|
71418 | 190 |
done |
191 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
192 |
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
|
193 |
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
|
194 |
proof - |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
195 |
interpret bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close> |
71186 | 196 |
apply standard |
71426 | 197 |
apply (simp_all add: bit_eq_iff bit_and_iff bit_or_iff bit_not_iff) |
198 |
apply (auto simp add: exp_eq_0_imp_not_bit bit_exp_iff) |
|
71186 | 199 |
done |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
200 |
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
|
201 |
by standard |
71426 | 202 |
show \<open>boolean_algebra.xor (AND) (OR) NOT = (XOR)\<close> |
71186 | 203 |
apply (auto simp add: fun_eq_iff bit.xor_def bit_eq_iff bit_and_iff bit_or_iff bit_not_iff bit_xor_iff) |
71418 | 204 |
apply (simp_all add: bit_exp_iff, simp_all add: bit_def) |
205 |
apply (metis local.bit_exp_iff local.bits_div_by_0) |
|
206 |
apply (metis local.bit_exp_iff local.bits_div_by_0) |
|
71186 | 207 |
done |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
208 |
qed |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
209 |
|
71802 | 210 |
lemma and_eq_not_not_or: |
211 |
\<open>a AND b = NOT (NOT a OR NOT b)\<close> |
|
212 |
by simp |
|
213 |
||
214 |
lemma or_eq_not_not_and: |
|
215 |
\<open>a OR b = NOT (NOT a AND NOT b)\<close> |
|
216 |
by simp |
|
217 |
||
71412 | 218 |
lemma push_bit_minus: |
219 |
\<open>push_bit n (- a) = - push_bit n a\<close> |
|
220 |
by (simp add: push_bit_eq_mult) |
|
221 |
||
71409 | 222 |
lemma take_bit_not_take_bit: |
223 |
\<open>take_bit n (NOT (take_bit n a)) = take_bit n (NOT a)\<close> |
|
224 |
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
|
225 |
|
71418 | 226 |
lemma take_bit_not_iff: |
227 |
"take_bit n (NOT a) = take_bit n (NOT b) \<longleftrightarrow> take_bit n a = take_bit n b" |
|
228 |
apply (simp add: bit_eq_iff bit_not_iff bit_take_bit_iff) |
|
229 |
apply (simp add: bit_exp_iff) |
|
230 |
apply (use local.exp_eq_0_imp_not_bit in blast) |
|
231 |
done |
|
232 |
||
71426 | 233 |
definition set_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
234 |
where \<open>set_bit n a = a OR 2 ^ n\<close> |
|
235 |
||
236 |
definition unset_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
237 |
where \<open>unset_bit n a = a AND NOT (2 ^ n)\<close> |
|
238 |
||
239 |
definition flip_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
240 |
where \<open>flip_bit n a = a XOR 2 ^ n\<close> |
|
241 |
||
242 |
lemma bit_set_bit_iff: |
|
243 |
\<open>bit (set_bit m a) n \<longleftrightarrow> bit a n \<or> (m = n \<and> 2 ^ n \<noteq> 0)\<close> |
|
244 |
by (auto simp add: set_bit_def bit_or_iff bit_exp_iff) |
|
245 |
||
246 |
lemma even_set_bit_iff: |
|
247 |
\<open>even (set_bit m a) \<longleftrightarrow> even a \<and> m \<noteq> 0\<close> |
|
248 |
using bit_set_bit_iff [of m a 0] by auto |
|
249 |
||
250 |
lemma bit_unset_bit_iff: |
|
251 |
\<open>bit (unset_bit m a) n \<longleftrightarrow> bit a n \<and> m \<noteq> n\<close> |
|
252 |
by (auto simp add: unset_bit_def bit_and_iff bit_not_iff bit_exp_iff exp_eq_0_imp_not_bit) |
|
253 |
||
254 |
lemma even_unset_bit_iff: |
|
255 |
\<open>even (unset_bit m a) \<longleftrightarrow> even a \<or> m = 0\<close> |
|
256 |
using bit_unset_bit_iff [of m a 0] by auto |
|
257 |
||
258 |
lemma bit_flip_bit_iff: |
|
259 |
\<open>bit (flip_bit m a) n \<longleftrightarrow> (m = n \<longleftrightarrow> \<not> bit a n) \<and> 2 ^ n \<noteq> 0\<close> |
|
260 |
by (auto simp add: flip_bit_def bit_xor_iff bit_exp_iff exp_eq_0_imp_not_bit) |
|
261 |
||
262 |
lemma even_flip_bit_iff: |
|
263 |
\<open>even (flip_bit m a) \<longleftrightarrow> \<not> (even a \<longleftrightarrow> m = 0)\<close> |
|
264 |
using bit_flip_bit_iff [of m a 0] by auto |
|
265 |
||
266 |
lemma set_bit_0 [simp]: |
|
267 |
\<open>set_bit 0 a = 1 + 2 * (a div 2)\<close> |
|
268 |
proof (rule bit_eqI) |
|
269 |
fix m |
|
270 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
271 |
then show \<open>bit (set_bit 0 a) m = bit (1 + 2 * (a div 2)) m\<close> |
|
272 |
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
|
273 |
(cases m, simp_all add: bit_Suc) |
71426 | 274 |
qed |
275 |
||
71821 | 276 |
lemma set_bit_Suc: |
71426 | 277 |
\<open>set_bit (Suc n) a = a mod 2 + 2 * set_bit n (a div 2)\<close> |
278 |
proof (rule bit_eqI) |
|
279 |
fix m |
|
280 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
281 |
show \<open>bit (set_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * set_bit n (a div 2)) m\<close> |
|
282 |
proof (cases m) |
|
283 |
case 0 |
|
284 |
then show ?thesis |
|
285 |
by (simp add: even_set_bit_iff) |
|
286 |
next |
|
287 |
case (Suc m) |
|
288 |
with * have \<open>2 ^ m \<noteq> 0\<close> |
|
289 |
using mult_2 by auto |
|
290 |
show ?thesis |
|
291 |
by (cases a rule: parity_cases) |
|
292 |
(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
|
293 |
simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close> bit_Suc) |
71426 | 294 |
qed |
295 |
qed |
|
296 |
||
297 |
lemma unset_bit_0 [simp]: |
|
298 |
\<open>unset_bit 0 a = 2 * (a div 2)\<close> |
|
299 |
proof (rule bit_eqI) |
|
300 |
fix m |
|
301 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
302 |
then show \<open>bit (unset_bit 0 a) m = bit (2 * (a div 2)) m\<close> |
|
303 |
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
|
304 |
(cases m, simp_all add: bit_Suc) |
71426 | 305 |
qed |
306 |
||
71821 | 307 |
lemma unset_bit_Suc: |
71426 | 308 |
\<open>unset_bit (Suc n) a = a mod 2 + 2 * unset_bit n (a div 2)\<close> |
309 |
proof (rule bit_eqI) |
|
310 |
fix m |
|
311 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
312 |
then show \<open>bit (unset_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * unset_bit n (a div 2)) m\<close> |
|
313 |
proof (cases m) |
|
314 |
case 0 |
|
315 |
then show ?thesis |
|
316 |
by (simp add: even_unset_bit_iff) |
|
317 |
next |
|
318 |
case (Suc m) |
|
319 |
show ?thesis |
|
320 |
by (cases a rule: parity_cases) |
|
321 |
(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
|
322 |
simp_all add: Suc bit_Suc) |
71426 | 323 |
qed |
324 |
qed |
|
325 |
||
326 |
lemma flip_bit_0 [simp]: |
|
327 |
\<open>flip_bit 0 a = of_bool (even a) + 2 * (a div 2)\<close> |
|
328 |
proof (rule bit_eqI) |
|
329 |
fix m |
|
330 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
331 |
then show \<open>bit (flip_bit 0 a) m = bit (of_bool (even a) + 2 * (a div 2)) m\<close> |
|
332 |
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
|
333 |
(cases m, simp_all add: bit_Suc) |
71426 | 334 |
qed |
335 |
||
71821 | 336 |
lemma flip_bit_Suc: |
71426 | 337 |
\<open>flip_bit (Suc n) a = a mod 2 + 2 * flip_bit n (a div 2)\<close> |
338 |
proof (rule bit_eqI) |
|
339 |
fix m |
|
340 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
341 |
show \<open>bit (flip_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * flip_bit n (a div 2)) m\<close> |
|
342 |
proof (cases m) |
|
343 |
case 0 |
|
344 |
then show ?thesis |
|
345 |
by (simp add: even_flip_bit_iff) |
|
346 |
next |
|
347 |
case (Suc m) |
|
348 |
with * have \<open>2 ^ m \<noteq> 0\<close> |
|
349 |
using mult_2 by auto |
|
350 |
show ?thesis |
|
351 |
by (cases a rule: parity_cases) |
|
352 |
(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
|
353 |
simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close> bit_Suc) |
71426 | 354 |
qed |
355 |
qed |
|
356 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
357 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
358 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
359 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
360 |
subsubsection \<open>Instance \<^typ>\<open>int\<close>\<close> |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
361 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
362 |
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
|
363 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
364 |
|
71420 | 365 |
definition not_int :: \<open>int \<Rightarrow> int\<close> |
366 |
where \<open>not_int k = - k - 1\<close> |
|
367 |
||
368 |
lemma not_int_rec: |
|
369 |
"NOT k = of_bool (even k) + 2 * NOT (k div 2)" for k :: int |
|
370 |
by (auto simp add: not_int_def elim: oddE) |
|
371 |
||
372 |
lemma even_not_iff_int: |
|
373 |
\<open>even (NOT k) \<longleftrightarrow> odd k\<close> for k :: int |
|
374 |
by (simp add: not_int_def) |
|
375 |
||
376 |
lemma not_int_div_2: |
|
377 |
\<open>NOT k div 2 = NOT (k div 2)\<close> for k :: int |
|
378 |
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
|
379 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
380 |
lemma bit_not_int_iff: |
71186 | 381 |
\<open>bit (NOT k) n \<longleftrightarrow> \<not> bit k n\<close> |
382 |
for k :: int |
|
71535
b612edee9b0c
more frugal simp rules for bit operations; more pervasive use of bit selector
haftmann
parents:
71442
diff
changeset
|
383 |
by (induction n arbitrary: k) (simp_all add: not_int_div_2 even_not_iff_int bit_Suc) |
71186 | 384 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
385 |
function and_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
386 |
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
|
387 |
then - of_bool (odd k \<and> odd l) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
388 |
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
|
389 |
by auto |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
390 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
391 |
termination |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
392 |
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
|
393 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
394 |
declare and_int.simps [simp del] |
71802 | 395 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
396 |
lemma and_int_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
397 |
\<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
|
398 |
for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
399 |
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
|
400 |
case True |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
401 |
then show ?thesis |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
402 |
by auto (simp_all add: and_int.simps) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
403 |
next |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
404 |
case False |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
405 |
then show ?thesis |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
406 |
by (auto simp add: ac_simps and_int.simps [of k l]) |
71802 | 407 |
qed |
408 |
||
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
409 |
lemma bit_and_int_iff: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
410 |
\<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
|
411 |
proof (induction n arbitrary: k l) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
412 |
case 0 |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
413 |
then show ?case |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
414 |
by (simp add: and_int_rec [of k l]) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
415 |
next |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
416 |
case (Suc n) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
417 |
then show ?case |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
418 |
by (simp add: and_int_rec [of k l] bit_Suc) |
71802 | 419 |
qed |
420 |
||
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
421 |
lemma even_and_iff_int: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
422 |
\<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
|
423 |
using bit_and_int_iff [of k l 0] by auto |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
424 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
425 |
definition or_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
426 |
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
|
427 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
428 |
lemma or_int_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
429 |
\<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
|
430 |
for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
431 |
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
|
432 |
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
|
433 |
(simp add: not_int_def) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
434 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
435 |
lemma bit_or_int_iff: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
436 |
\<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
|
437 |
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
|
438 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
439 |
definition xor_int :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
440 |
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
|
441 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
442 |
lemma xor_int_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
443 |
\<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
|
444 |
for k l :: int |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
445 |
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
|
446 |
(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
|
447 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
448 |
lemma bit_xor_int_iff: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
449 |
\<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
|
450 |
by (auto simp add: xor_int_def bit_or_int_iff bit_and_int_iff bit_not_int_iff) |
71802 | 451 |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
452 |
instance proof |
71186 | 453 |
fix k l :: int and n :: nat |
71409 | 454 |
show \<open>- k = NOT (k - 1)\<close> |
455 |
by (simp add: not_int_def) |
|
71186 | 456 |
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
|
457 |
by (fact bit_and_int_iff) |
71186 | 458 |
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
|
459 |
by (fact bit_or_int_iff) |
71186 | 460 |
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
|
461 |
by (fact bit_xor_int_iff) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
462 |
qed (simp_all add: bit_not_int_iff) |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
463 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
464 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
465 |
|
71802 | 466 |
lemma not_nonnegative_int_iff [simp]: |
467 |
\<open>NOT k \<ge> 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
468 |
by (simp add: not_int_def) |
|
469 |
||
470 |
lemma not_negative_int_iff [simp]: |
|
471 |
\<open>NOT k < 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
472 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less not_le) |
|
473 |
||
474 |
lemma and_nonnegative_int_iff [simp]: |
|
475 |
\<open>k AND l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<or> l \<ge> 0\<close> for k l :: int |
|
476 |
proof (induction k arbitrary: l rule: int_bit_induct) |
|
477 |
case zero |
|
478 |
then show ?case |
|
479 |
by simp |
|
480 |
next |
|
481 |
case minus |
|
482 |
then show ?case |
|
483 |
by simp |
|
484 |
next |
|
485 |
case (even k) |
|
486 |
then show ?case |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
487 |
using and_int_rec [of \<open>k * 2\<close> l] by (simp add: pos_imp_zdiv_nonneg_iff) |
71802 | 488 |
next |
489 |
case (odd k) |
|
490 |
from odd have \<open>0 \<le> k AND l div 2 \<longleftrightarrow> 0 \<le> k \<or> 0 \<le> l div 2\<close> |
|
491 |
by simp |
|
492 |
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> |
|
493 |
by simp |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
494 |
with and_int_rec [of \<open>1 + k * 2\<close> l] |
71802 | 495 |
show ?case |
496 |
by auto |
|
497 |
qed |
|
498 |
||
499 |
lemma and_negative_int_iff [simp]: |
|
500 |
\<open>k AND l < 0 \<longleftrightarrow> k < 0 \<and> l < 0\<close> for k l :: int |
|
501 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
502 |
||
503 |
lemma or_nonnegative_int_iff [simp]: |
|
504 |
\<open>k OR l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<and> l \<ge> 0\<close> for k l :: int |
|
505 |
by (simp only: or_eq_not_not_and not_nonnegative_int_iff) simp |
|
506 |
||
507 |
lemma or_negative_int_iff [simp]: |
|
508 |
\<open>k OR l < 0 \<longleftrightarrow> k < 0 \<or> l < 0\<close> for k l :: int |
|
509 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
510 |
||
511 |
lemma xor_nonnegative_int_iff [simp]: |
|
512 |
\<open>k XOR l \<ge> 0 \<longleftrightarrow> (k \<ge> 0 \<longleftrightarrow> l \<ge> 0)\<close> for k l :: int |
|
513 |
by (simp only: bit.xor_def or_nonnegative_int_iff) auto |
|
514 |
||
515 |
lemma xor_negative_int_iff [simp]: |
|
516 |
\<open>k XOR l < 0 \<longleftrightarrow> (k < 0) \<noteq> (l < 0)\<close> for k l :: int |
|
517 |
by (subst Not_eq_iff [symmetric]) (auto simp add: not_less) |
|
518 |
||
519 |
lemma set_bit_nonnegative_int_iff [simp]: |
|
520 |
\<open>set_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
521 |
by (simp add: set_bit_def) |
|
522 |
||
523 |
lemma set_bit_negative_int_iff [simp]: |
|
524 |
\<open>set_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
525 |
by (simp add: set_bit_def) |
|
526 |
||
527 |
lemma unset_bit_nonnegative_int_iff [simp]: |
|
528 |
\<open>unset_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
529 |
by (simp add: unset_bit_def) |
|
530 |
||
531 |
lemma unset_bit_negative_int_iff [simp]: |
|
532 |
\<open>unset_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
533 |
by (simp add: unset_bit_def) |
|
534 |
||
535 |
lemma flip_bit_nonnegative_int_iff [simp]: |
|
536 |
\<open>flip_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
537 |
by (simp add: flip_bit_def) |
|
538 |
||
539 |
lemma flip_bit_negative_int_iff [simp]: |
|
540 |
\<open>flip_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
541 |
by (simp add: flip_bit_def) |
|
542 |
||
71442 | 543 |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
544 |
subsubsection \<open>Instance \<^typ>\<open>nat\<close>\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
545 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
546 |
instantiation nat :: semiring_bit_operations |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
547 |
begin |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
548 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
549 |
definition and_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
550 |
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
|
551 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
552 |
definition or_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
553 |
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
|
554 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
555 |
definition xor_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
556 |
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
|
557 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
558 |
instance proof |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
559 |
fix m n q :: nat |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
560 |
show \<open>bit (m AND n) q \<longleftrightarrow> bit m q \<and> bit n q\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
561 |
by (auto simp add: and_nat_def bit_and_iff less_le bit_eq_iff) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
562 |
show \<open>bit (m OR n) q \<longleftrightarrow> bit m q \<or> bit n q\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
563 |
by (auto simp add: or_nat_def bit_or_iff less_le bit_eq_iff) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
564 |
show \<open>bit (m XOR n) q \<longleftrightarrow> bit m q \<noteq> bit n q\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
565 |
by (auto simp add: xor_nat_def bit_xor_iff less_le bit_eq_iff) |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
566 |
qed |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
567 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
568 |
end |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
569 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
570 |
lemma and_nat_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
571 |
\<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
|
572 |
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
|
573 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
574 |
lemma or_nat_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
575 |
\<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
|
576 |
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
|
577 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
578 |
lemma xor_nat_rec: |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
579 |
\<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
|
580 |
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
|
581 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
582 |
lemma Suc_0_and_eq [simp]: |
71822 | 583 |
\<open>Suc 0 AND n = n mod 2\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
584 |
using one_and_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
585 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
586 |
lemma and_Suc_0_eq [simp]: |
71822 | 587 |
\<open>n AND Suc 0 = n mod 2\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
588 |
using and_one_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
589 |
|
71822 | 590 |
lemma Suc_0_or_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
591 |
\<open>Suc 0 OR n = n + of_bool (even n)\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
592 |
using one_or_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
593 |
|
71822 | 594 |
lemma or_Suc_0_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
595 |
\<open>n OR Suc 0 = n + of_bool (even n)\<close> |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
596 |
using or_one_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
597 |
|
71822 | 598 |
lemma Suc_0_xor_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
599 |
\<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
|
600 |
using one_xor_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
601 |
|
71822 | 602 |
lemma xor_Suc_0_eq: |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
603 |
\<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
|
604 |
using xor_one_eq [of n] by simp |
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
605 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
606 |
|
71442 | 607 |
subsubsection \<open>Instances for \<^typ>\<open>integer\<close> and \<^typ>\<open>natural\<close>\<close> |
608 |
||
609 |
unbundle integer.lifting natural.lifting |
|
610 |
||
611 |
context |
|
612 |
includes lifting_syntax |
|
613 |
begin |
|
614 |
||
615 |
lemma transfer_rule_bit_integer [transfer_rule]: |
|
616 |
\<open>((pcr_integer :: int \<Rightarrow> integer \<Rightarrow> bool) ===> (=)) bit bit\<close> |
|
617 |
by (unfold bit_def) transfer_prover |
|
618 |
||
619 |
lemma transfer_rule_bit_natural [transfer_rule]: |
|
620 |
\<open>((pcr_natural :: nat \<Rightarrow> natural \<Rightarrow> bool) ===> (=)) bit bit\<close> |
|
621 |
by (unfold bit_def) transfer_prover |
|
622 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
623 |
end |
71442 | 624 |
|
625 |
instantiation integer :: ring_bit_operations |
|
626 |
begin |
|
627 |
||
628 |
lift_definition not_integer :: \<open>integer \<Rightarrow> integer\<close> |
|
629 |
is not . |
|
630 |
||
631 |
lift_definition and_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close> |
|
632 |
is \<open>and\<close> . |
|
633 |
||
634 |
lift_definition or_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close> |
|
635 |
is or . |
|
636 |
||
637 |
lift_definition xor_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close> |
|
638 |
is xor . |
|
639 |
||
640 |
instance proof |
|
641 |
fix k l :: \<open>integer\<close> and n :: nat |
|
642 |
show \<open>- k = NOT (k - 1)\<close> |
|
643 |
by transfer (simp add: minus_eq_not_minus_1) |
|
644 |
show \<open>bit (NOT k) n \<longleftrightarrow> (2 :: integer) ^ n \<noteq> 0 \<and> \<not> bit k n\<close> |
|
645 |
by transfer (fact bit_not_iff) |
|
646 |
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
|
647 |
by transfer (fact bit_and_iff) |
71442 | 648 |
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
|
649 |
by transfer (fact bit_or_iff) |
71442 | 650 |
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
|
651 |
by transfer (fact bit_xor_iff) |
71442 | 652 |
qed |
653 |
||
654 |
end |
|
655 |
||
656 |
instantiation natural :: semiring_bit_operations |
|
657 |
begin |
|
658 |
||
659 |
lift_definition and_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close> |
|
660 |
is \<open>and\<close> . |
|
661 |
||
662 |
lift_definition or_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close> |
|
663 |
is or . |
|
664 |
||
665 |
lift_definition xor_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close> |
|
666 |
is xor . |
|
667 |
||
668 |
instance proof |
|
669 |
fix m n :: \<open>natural\<close> and q :: nat |
|
670 |
show \<open>bit (m AND n) q \<longleftrightarrow> bit m q \<and> bit n q\<close> |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
671 |
by transfer (fact bit_and_iff) |
71442 | 672 |
show \<open>bit (m OR n) q \<longleftrightarrow> bit m q \<or> bit n q\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
673 |
by transfer (fact bit_or_iff) |
71442 | 674 |
show \<open>bit (m XOR n) q \<longleftrightarrow> bit m q \<noteq> bit n q\<close> |
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
675 |
by transfer (fact bit_xor_iff) |
71442 | 676 |
qed |
677 |
||
678 |
end |
|
679 |
||
680 |
lifting_update integer.lifting |
|
681 |
lifting_forget integer.lifting |
|
682 |
||
683 |
lifting_update natural.lifting |
|
684 |
lifting_forget natural.lifting |
|
685 |
||
71800 | 686 |
|
687 |
subsection \<open>Key ideas of bit operations\<close> |
|
688 |
||
689 |
text \<open> |
|
690 |
When formalizing bit operations, it is tempting to represent |
|
691 |
bit values as explicit lists over a binary type. This however |
|
692 |
is a bad idea, mainly due to the inherent ambiguities in |
|
693 |
representation concerning repeating leading bits. |
|
694 |
||
695 |
Hence this approach avoids such explicit lists altogether |
|
696 |
following an algebraic path: |
|
697 |
||
698 |
\<^item> Bit values are represented by numeric types: idealized |
|
699 |
unbounded bit values can be represented by type \<^typ>\<open>int\<close>, |
|
700 |
bounded bit values by quotient types over \<^typ>\<open>int\<close>. |
|
701 |
||
702 |
\<^item> (A special case are idealized unbounded bit values ending |
|
703 |
in @{term [source] 0} which can be represented by type \<^typ>\<open>nat\<close> but |
|
704 |
only support a restricted set of operations). |
|
705 |
||
706 |
\<^item> From this idea follows that |
|
707 |
||
708 |
\<^item> multiplication by \<^term>\<open>2 :: int\<close> is a bit shift to the left and |
|
709 |
||
710 |
\<^item> division by \<^term>\<open>2 :: int\<close> is a bit shift to the right. |
|
711 |
||
712 |
\<^item> Concerning bounded bit values, iterated shifts to the left |
|
713 |
may result in eliminating all bits by shifting them all |
|
714 |
beyond the boundary. The property \<^prop>\<open>(2 :: int) ^ n \<noteq> 0\<close> |
|
715 |
represents that \<^term>\<open>n\<close> is \<^emph>\<open>not\<close> beyond that boundary. |
|
716 |
||
717 |
\<^item> The projection on a single bit is then @{thm bit_def [where ?'a = int, no_vars]}. |
|
718 |
||
719 |
\<^item> This leads to the most fundamental properties of bit values: |
|
720 |
||
721 |
\<^item> Equality rule: @{thm bit_eqI [where ?'a = int, no_vars]} |
|
722 |
||
723 |
\<^item> Induction rule: @{thm bits_induct [where ?'a = int, no_vars]} |
|
724 |
||
725 |
\<^item> Typical operations are characterized as follows: |
|
726 |
||
727 |
\<^item> Singleton \<^term>\<open>n\<close>th bit: \<^term>\<open>(2 :: int) ^ n\<close> |
|
728 |
||
71823 | 729 |
\<^item> Bit mask upto bit \<^term>\<open>n\<close>: @{thm mask_eq_exp_minus_1 [where ?'a = int, no_vars]}} |
71800 | 730 |
|
731 |
\<^item> Left shift: @{thm push_bit_eq_mult [where ?'a = int, no_vars]} |
|
732 |
||
733 |
\<^item> Right shift: @{thm drop_bit_eq_div [where ?'a = int, no_vars]} |
|
734 |
||
735 |
\<^item> Truncation: @{thm take_bit_eq_mod [where ?'a = int, no_vars]} |
|
736 |
||
737 |
\<^item> Negation: @{thm bit_not_iff [where ?'a = int, no_vars]} |
|
738 |
||
739 |
\<^item> And: @{thm bit_and_iff [where ?'a = int, no_vars]} |
|
740 |
||
741 |
\<^item> Or: @{thm bit_or_iff [where ?'a = int, no_vars]} |
|
742 |
||
743 |
\<^item> Xor: @{thm bit_xor_iff [where ?'a = int, no_vars]} |
|
744 |
||
745 |
\<^item> Set a single bit: @{thm set_bit_def [where ?'a = int, no_vars]} |
|
746 |
||
747 |
\<^item> Unset a single bit: @{thm unset_bit_def [where ?'a = int, no_vars]} |
|
748 |
||
749 |
\<^item> Flip a single bit: @{thm flip_bit_def [where ?'a = int, no_vars]} |
|
750 |
\<close> |
|
751 |
||
71442 | 752 |
end |