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