author | haftmann |
Tue, 11 Feb 2020 19:03:57 +0100 | |
changeset 71442 | d45495e897f4 |
parent 71426 | 745e518d3d0b |
child 71535 | b612edee9b0c |
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 |
|
71181 | 31 |
lemma stable_imp_drop_eq: |
32 |
\<open>drop_bit n a = a\<close> if \<open>a div 2 = a\<close> |
|
33 |
by (induction n) (simp_all add: that) |
|
34 |
||
71418 | 35 |
sublocale "and": semilattice \<open>(AND)\<close> |
36 |
by standard (auto simp add: bit_eq_iff bit_and_iff) |
|
37 |
||
38 |
sublocale or: semilattice_neutr \<open>(OR)\<close> 0 |
|
39 |
by standard (auto simp add: bit_eq_iff bit_or_iff) |
|
40 |
||
41 |
sublocale xor: comm_monoid \<open>(XOR)\<close> 0 |
|
42 |
by standard (auto simp add: bit_eq_iff bit_xor_iff) |
|
43 |
||
71412 | 44 |
lemma zero_and_eq [simp]: |
45 |
"0 AND a = 0" |
|
46 |
by (simp add: bit_eq_iff bit_and_iff) |
|
47 |
||
48 |
lemma and_zero_eq [simp]: |
|
49 |
"a AND 0 = 0" |
|
50 |
by (simp add: bit_eq_iff bit_and_iff) |
|
51 |
||
71418 | 52 |
lemma one_and_eq [simp]: |
53 |
"1 AND a = of_bool (odd a)" |
|
54 |
by (simp add: bit_eq_iff bit_and_iff) (auto simp add: bit_1_iff) |
|
71412 | 55 |
|
71418 | 56 |
lemma and_one_eq [simp]: |
57 |
"a AND 1 = of_bool (odd a)" |
|
58 |
using one_and_eq [of a] by (simp add: ac_simps) |
|
59 |
||
60 |
lemma one_or_eq [simp]: |
|
61 |
"1 OR a = a + of_bool (even a)" |
|
62 |
by (simp add: bit_eq_iff bit_or_iff add.commute [of _ 1] even_bit_succ_iff) (auto simp add: bit_1_iff) |
|
71412 | 63 |
|
71418 | 64 |
lemma or_one_eq [simp]: |
65 |
"a OR 1 = a + of_bool (even a)" |
|
66 |
using one_or_eq [of a] by (simp add: ac_simps) |
|
71412 | 67 |
|
71418 | 68 |
lemma one_xor_eq [simp]: |
69 |
"1 XOR a = a + of_bool (even a) - of_bool (odd a)" |
|
70 |
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) |
|
71 |
||
71419 | 72 |
lemma xor_one_eq [simp]: |
71418 | 73 |
"a XOR 1 = a + of_bool (even a) - of_bool (odd a)" |
74 |
using one_xor_eq [of a] by (simp add: ac_simps) |
|
71412 | 75 |
|
71409 | 76 |
lemma take_bit_and [simp]: |
77 |
\<open>take_bit n (a AND b) = take_bit n a AND take_bit n b\<close> |
|
78 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_and_iff) |
|
79 |
||
80 |
lemma take_bit_or [simp]: |
|
81 |
\<open>take_bit n (a OR b) = take_bit n a OR take_bit n b\<close> |
|
82 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_or_iff) |
|
83 |
||
84 |
lemma take_bit_xor [simp]: |
|
85 |
\<open>take_bit n (a XOR b) = take_bit n a XOR take_bit n b\<close> |
|
86 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_xor_iff) |
|
87 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
88 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
89 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
90 |
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
|
91 |
fixes not :: \<open>'a \<Rightarrow> 'a\<close> (\<open>NOT\<close>) |
71186 | 92 |
assumes bit_not_iff: \<open>\<And>n. bit (NOT a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit a n\<close> |
71409 | 93 |
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
|
94 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
95 |
|
71409 | 96 |
text \<open> |
97 |
For the sake of code generation \<^const>\<open>not\<close> is specified as |
|
98 |
definitional class operation. Note that \<^const>\<open>not\<close> has no |
|
99 |
sensible definition for unlimited but only positive bit strings |
|
100 |
(type \<^typ>\<open>nat\<close>). |
|
101 |
\<close> |
|
102 |
||
71186 | 103 |
lemma bits_minus_1_mod_2_eq [simp]: |
104 |
\<open>(- 1) mod 2 = 1\<close> |
|
105 |
by (simp add: mod_2_eq_odd) |
|
106 |
||
71409 | 107 |
lemma not_eq_complement: |
108 |
\<open>NOT a = - a - 1\<close> |
|
109 |
using minus_eq_not_minus_1 [of \<open>a + 1\<close>] by simp |
|
110 |
||
111 |
lemma minus_eq_not_plus_1: |
|
112 |
\<open>- a = NOT a + 1\<close> |
|
113 |
using not_eq_complement [of a] by simp |
|
114 |
||
115 |
lemma bit_minus_iff: |
|
116 |
\<open>bit (- a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit (a - 1) n\<close> |
|
117 |
by (simp add: minus_eq_not_minus_1 bit_not_iff) |
|
118 |
||
71418 | 119 |
lemma even_not_iff [simp]: |
120 |
"even (NOT a) \<longleftrightarrow> odd a" |
|
121 |
using bit_not_iff [of a 0] by auto |
|
122 |
||
71409 | 123 |
lemma bit_not_exp_iff: |
124 |
\<open>bit (NOT (2 ^ m)) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n \<noteq> m\<close> |
|
125 |
by (auto simp add: bit_not_iff bit_exp_iff) |
|
126 |
||
71186 | 127 |
lemma bit_minus_1_iff [simp]: |
128 |
\<open>bit (- 1) n \<longleftrightarrow> 2 ^ n \<noteq> 0\<close> |
|
71409 | 129 |
by (simp add: bit_minus_iff) |
130 |
||
131 |
lemma bit_minus_exp_iff: |
|
132 |
\<open>bit (- (2 ^ m)) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n \<ge> m\<close> |
|
133 |
oops |
|
134 |
||
135 |
lemma bit_minus_2_iff [simp]: |
|
136 |
\<open>bit (- 2) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> n > 0\<close> |
|
137 |
by (simp add: bit_minus_iff bit_1_iff) |
|
71186 | 138 |
|
71418 | 139 |
lemma not_one [simp]: |
140 |
"NOT 1 = - 2" |
|
141 |
by (simp add: bit_eq_iff bit_not_iff) (simp add: bit_1_iff) |
|
142 |
||
143 |
sublocale "and": semilattice_neutr \<open>(AND)\<close> \<open>- 1\<close> |
|
144 |
apply standard |
|
71426 | 145 |
apply (simp add: bit_eq_iff bit_and_iff) |
146 |
apply (auto simp add: exp_eq_0_imp_not_bit bit_exp_iff) |
|
71418 | 147 |
done |
148 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
149 |
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
|
150 |
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
|
151 |
proof - |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
152 |
interpret bit: boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close> |
71186 | 153 |
apply standard |
71426 | 154 |
apply (simp_all add: bit_eq_iff bit_and_iff bit_or_iff bit_not_iff) |
155 |
apply (auto simp add: exp_eq_0_imp_not_bit bit_exp_iff) |
|
71186 | 156 |
done |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
157 |
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
|
158 |
by standard |
71426 | 159 |
show \<open>boolean_algebra.xor (AND) (OR) NOT = (XOR)\<close> |
71186 | 160 |
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 | 161 |
apply (simp_all add: bit_exp_iff, simp_all add: bit_def) |
162 |
apply (metis local.bit_exp_iff local.bits_div_by_0) |
|
163 |
apply (metis local.bit_exp_iff local.bits_div_by_0) |
|
71186 | 164 |
done |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
165 |
qed |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
166 |
|
71412 | 167 |
lemma push_bit_minus: |
168 |
\<open>push_bit n (- a) = - push_bit n a\<close> |
|
169 |
by (simp add: push_bit_eq_mult) |
|
170 |
||
71409 | 171 |
lemma take_bit_not_take_bit: |
172 |
\<open>take_bit n (NOT (take_bit n a)) = take_bit n (NOT a)\<close> |
|
173 |
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
|
174 |
|
71418 | 175 |
lemma take_bit_not_iff: |
176 |
"take_bit n (NOT a) = take_bit n (NOT b) \<longleftrightarrow> take_bit n a = take_bit n b" |
|
177 |
apply (simp add: bit_eq_iff bit_not_iff bit_take_bit_iff) |
|
178 |
apply (simp add: bit_exp_iff) |
|
179 |
apply (use local.exp_eq_0_imp_not_bit in blast) |
|
180 |
done |
|
181 |
||
71426 | 182 |
definition set_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
183 |
where \<open>set_bit n a = a OR 2 ^ n\<close> |
|
184 |
||
185 |
definition unset_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
186 |
where \<open>unset_bit n a = a AND NOT (2 ^ n)\<close> |
|
187 |
||
188 |
definition flip_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
189 |
where \<open>flip_bit n a = a XOR 2 ^ n\<close> |
|
190 |
||
191 |
lemma bit_set_bit_iff: |
|
192 |
\<open>bit (set_bit m a) n \<longleftrightarrow> bit a n \<or> (m = n \<and> 2 ^ n \<noteq> 0)\<close> |
|
193 |
by (auto simp add: set_bit_def bit_or_iff bit_exp_iff) |
|
194 |
||
195 |
lemma even_set_bit_iff: |
|
196 |
\<open>even (set_bit m a) \<longleftrightarrow> even a \<and> m \<noteq> 0\<close> |
|
197 |
using bit_set_bit_iff [of m a 0] by auto |
|
198 |
||
199 |
lemma bit_unset_bit_iff: |
|
200 |
\<open>bit (unset_bit m a) n \<longleftrightarrow> bit a n \<and> m \<noteq> n\<close> |
|
201 |
by (auto simp add: unset_bit_def bit_and_iff bit_not_iff bit_exp_iff exp_eq_0_imp_not_bit) |
|
202 |
||
203 |
lemma even_unset_bit_iff: |
|
204 |
\<open>even (unset_bit m a) \<longleftrightarrow> even a \<or> m = 0\<close> |
|
205 |
using bit_unset_bit_iff [of m a 0] by auto |
|
206 |
||
207 |
lemma bit_flip_bit_iff: |
|
208 |
\<open>bit (flip_bit m a) n \<longleftrightarrow> (m = n \<longleftrightarrow> \<not> bit a n) \<and> 2 ^ n \<noteq> 0\<close> |
|
209 |
by (auto simp add: flip_bit_def bit_xor_iff bit_exp_iff exp_eq_0_imp_not_bit) |
|
210 |
||
211 |
lemma even_flip_bit_iff: |
|
212 |
\<open>even (flip_bit m a) \<longleftrightarrow> \<not> (even a \<longleftrightarrow> m = 0)\<close> |
|
213 |
using bit_flip_bit_iff [of m a 0] by auto |
|
214 |
||
215 |
lemma set_bit_0 [simp]: |
|
216 |
\<open>set_bit 0 a = 1 + 2 * (a div 2)\<close> |
|
217 |
proof (rule bit_eqI) |
|
218 |
fix m |
|
219 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
220 |
then show \<open>bit (set_bit 0 a) m = bit (1 + 2 * (a div 2)) m\<close> |
|
221 |
by (simp add: bit_set_bit_iff bit_double_iff even_bit_succ_iff) |
|
222 |
(cases m, simp_all) |
|
223 |
qed |
|
224 |
||
225 |
lemma set_bit_Suc [simp]: |
|
226 |
\<open>set_bit (Suc n) a = a mod 2 + 2 * set_bit n (a div 2)\<close> |
|
227 |
proof (rule bit_eqI) |
|
228 |
fix m |
|
229 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
230 |
show \<open>bit (set_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * set_bit n (a div 2)) m\<close> |
|
231 |
proof (cases m) |
|
232 |
case 0 |
|
233 |
then show ?thesis |
|
234 |
by (simp add: even_set_bit_iff) |
|
235 |
next |
|
236 |
case (Suc m) |
|
237 |
with * have \<open>2 ^ m \<noteq> 0\<close> |
|
238 |
using mult_2 by auto |
|
239 |
show ?thesis |
|
240 |
by (cases a rule: parity_cases) |
|
241 |
(simp_all add: bit_set_bit_iff bit_double_iff even_bit_succ_iff *, |
|
242 |
simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close>) |
|
243 |
qed |
|
244 |
qed |
|
245 |
||
246 |
lemma unset_bit_0 [simp]: |
|
247 |
\<open>unset_bit 0 a = 2 * (a div 2)\<close> |
|
248 |
proof (rule bit_eqI) |
|
249 |
fix m |
|
250 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
251 |
then show \<open>bit (unset_bit 0 a) m = bit (2 * (a div 2)) m\<close> |
|
252 |
by (simp add: bit_unset_bit_iff bit_double_iff) |
|
253 |
(cases m, simp_all) |
|
254 |
qed |
|
255 |
||
256 |
lemma unset_bit_Suc [simp]: |
|
257 |
\<open>unset_bit (Suc n) a = a mod 2 + 2 * unset_bit n (a div 2)\<close> |
|
258 |
proof (rule bit_eqI) |
|
259 |
fix m |
|
260 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
261 |
then show \<open>bit (unset_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * unset_bit n (a div 2)) m\<close> |
|
262 |
proof (cases m) |
|
263 |
case 0 |
|
264 |
then show ?thesis |
|
265 |
by (simp add: even_unset_bit_iff) |
|
266 |
next |
|
267 |
case (Suc m) |
|
268 |
show ?thesis |
|
269 |
by (cases a rule: parity_cases) |
|
270 |
(simp_all add: bit_unset_bit_iff bit_double_iff even_bit_succ_iff *, |
|
271 |
simp_all add: Suc) |
|
272 |
qed |
|
273 |
qed |
|
274 |
||
275 |
lemma flip_bit_0 [simp]: |
|
276 |
\<open>flip_bit 0 a = of_bool (even a) + 2 * (a div 2)\<close> |
|
277 |
proof (rule bit_eqI) |
|
278 |
fix m |
|
279 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
280 |
then show \<open>bit (flip_bit 0 a) m = bit (of_bool (even a) + 2 * (a div 2)) m\<close> |
|
281 |
by (simp add: bit_flip_bit_iff bit_double_iff even_bit_succ_iff) |
|
282 |
(cases m, simp_all) |
|
283 |
qed |
|
284 |
||
285 |
lemma flip_bit_Suc [simp]: |
|
286 |
\<open>flip_bit (Suc n) a = a mod 2 + 2 * flip_bit n (a div 2)\<close> |
|
287 |
proof (rule bit_eqI) |
|
288 |
fix m |
|
289 |
assume *: \<open>2 ^ m \<noteq> 0\<close> |
|
290 |
show \<open>bit (flip_bit (Suc n) a) m \<longleftrightarrow> bit (a mod 2 + 2 * flip_bit n (a div 2)) m\<close> |
|
291 |
proof (cases m) |
|
292 |
case 0 |
|
293 |
then show ?thesis |
|
294 |
by (simp add: even_flip_bit_iff) |
|
295 |
next |
|
296 |
case (Suc m) |
|
297 |
with * have \<open>2 ^ m \<noteq> 0\<close> |
|
298 |
using mult_2 by auto |
|
299 |
show ?thesis |
|
300 |
by (cases a rule: parity_cases) |
|
301 |
(simp_all add: bit_flip_bit_iff bit_double_iff even_bit_succ_iff, |
|
302 |
simp_all add: Suc \<open>2 ^ m \<noteq> 0\<close>) |
|
303 |
qed |
|
304 |
qed |
|
305 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
306 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
307 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
308 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
309 |
subsubsection \<open>Instance \<^typ>\<open>nat\<close>\<close> |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
310 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
311 |
locale zip_nat = single: abel_semigroup f |
71420 | 312 |
for f :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infixl \<open>\<^bold>*\<close> 70) + |
313 |
assumes end_of_bits: \<open>\<not> False \<^bold>* False\<close> |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
314 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
315 |
|
71420 | 316 |
function F :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> (infixl \<open>\<^bold>\<times>\<close> 70) |
317 |
where \<open>m \<^bold>\<times> n = (if m = 0 \<and> n = 0 then 0 |
|
318 |
else of_bool (odd m \<^bold>* odd n) + 2 * ((m div 2) \<^bold>\<times> (n div 2)))\<close> |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
319 |
by auto |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
320 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
321 |
termination |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
322 |
by (relation "measure (case_prod (+))") auto |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
323 |
|
71420 | 324 |
declare F.simps [simp del] |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
325 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
326 |
lemma rec: |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
327 |
"m \<^bold>\<times> n = of_bool (odd m \<^bold>* odd n) + (m div 2) \<^bold>\<times> (n div 2) * 2" |
71420 | 328 |
proof (cases \<open>m = 0 \<and> n = 0\<close>) |
329 |
case True |
|
330 |
then have \<open>m \<^bold>\<times> n = 0\<close> |
|
331 |
using True by (simp add: F.simps [of 0 0]) |
|
332 |
moreover have \<open>(m div 2) \<^bold>\<times> (n div 2) = m \<^bold>\<times> n\<close> |
|
333 |
using True by simp |
|
334 |
ultimately show ?thesis |
|
335 |
using True by (simp add: end_of_bits) |
|
336 |
next |
|
337 |
case False |
|
338 |
then show ?thesis |
|
339 |
by (auto simp add: ac_simps F.simps [of m n]) |
|
340 |
qed |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
341 |
|
71420 | 342 |
lemma bit_eq_iff: |
343 |
\<open>bit (m \<^bold>\<times> n) q \<longleftrightarrow> bit m q \<^bold>* bit n q\<close> |
|
344 |
proof (induction q arbitrary: m n) |
|
345 |
case 0 |
|
346 |
then show ?case |
|
347 |
by (simp add: rec [of m n]) |
|
348 |
next |
|
349 |
case (Suc n) |
|
350 |
then show ?case |
|
351 |
by (simp add: rec [of m n]) |
|
352 |
qed |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
353 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
354 |
sublocale abel_semigroup F |
71420 | 355 |
by standard (simp_all add: Parity.bit_eq_iff bit_eq_iff ac_simps) |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
356 |
|
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 |
instantiation nat :: semiring_bit_operations |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
360 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
361 |
|
71420 | 362 |
global_interpretation and_nat: zip_nat \<open>(\<and>)\<close> |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
363 |
defines and_nat = and_nat.F |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
364 |
by standard auto |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
365 |
|
71420 | 366 |
global_interpretation and_nat: semilattice \<open>(AND) :: nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
367 |
proof (rule semilattice.intro, fact and_nat.abel_semigroup_axioms, standard) |
71420 | 368 |
show \<open>n AND n = n\<close> for n :: nat |
369 |
by (simp add: bit_eq_iff and_nat.bit_eq_iff) |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
370 |
qed |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
371 |
|
71420 | 372 |
global_interpretation or_nat: zip_nat \<open>(\<or>)\<close> |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
373 |
defines or_nat = or_nat.F |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
374 |
by standard auto |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
375 |
|
71420 | 376 |
global_interpretation or_nat: semilattice \<open>(OR) :: nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
377 |
proof (rule semilattice.intro, fact or_nat.abel_semigroup_axioms, standard) |
71420 | 378 |
show \<open>n OR n = n\<close> for n :: nat |
379 |
by (simp add: bit_eq_iff or_nat.bit_eq_iff) |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
380 |
qed |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
381 |
|
71420 | 382 |
global_interpretation xor_nat: zip_nat \<open>(\<noteq>)\<close> |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
383 |
defines xor_nat = xor_nat.F |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
384 |
by standard auto |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
385 |
|
71186 | 386 |
instance proof |
387 |
fix m n q :: nat |
|
388 |
show \<open>bit (m AND n) q \<longleftrightarrow> bit m q \<and> bit n q\<close> |
|
71420 | 389 |
by (fact and_nat.bit_eq_iff) |
71186 | 390 |
show \<open>bit (m OR n) q \<longleftrightarrow> bit m q \<or> bit n q\<close> |
71420 | 391 |
by (fact or_nat.bit_eq_iff) |
71186 | 392 |
show \<open>bit (m XOR n) q \<longleftrightarrow> bit m q \<noteq> bit n q\<close> |
71420 | 393 |
by (fact xor_nat.bit_eq_iff) |
71186 | 394 |
qed |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
395 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
396 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
397 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
398 |
lemma Suc_0_and_eq [simp]: |
71419 | 399 |
\<open>Suc 0 AND n = of_bool (odd n)\<close> |
400 |
using one_and_eq [of n] by simp |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
401 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
402 |
lemma and_Suc_0_eq [simp]: |
71419 | 403 |
\<open>n AND Suc 0 = of_bool (odd n)\<close> |
404 |
using and_one_eq [of n] by simp |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
405 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
406 |
lemma Suc_0_or_eq [simp]: |
71419 | 407 |
\<open>Suc 0 OR n = n + of_bool (even n)\<close> |
408 |
using one_or_eq [of n] by simp |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
409 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
410 |
lemma or_Suc_0_eq [simp]: |
71419 | 411 |
\<open>n OR Suc 0 = n + of_bool (even n)\<close> |
412 |
using or_one_eq [of n] by simp |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
413 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
414 |
lemma Suc_0_xor_eq [simp]: |
71419 | 415 |
\<open>Suc 0 XOR n = n + of_bool (even n) - of_bool (odd n)\<close> |
416 |
using one_xor_eq [of n] by simp |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
417 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
418 |
lemma xor_Suc_0_eq [simp]: |
71419 | 419 |
\<open>n XOR Suc 0 = n + of_bool (even n) - of_bool (odd n)\<close> |
420 |
using xor_one_eq [of n] by simp |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
421 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
422 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
423 |
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
|
424 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
425 |
locale zip_int = single: abel_semigroup f |
71420 | 426 |
for f :: \<open>bool \<Rightarrow> bool \<Rightarrow> bool\<close> (infixl \<open>\<^bold>*\<close> 70) |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
427 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
428 |
|
71420 | 429 |
function F :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> (infixl \<open>\<^bold>\<times>\<close> 70) |
430 |
where \<open>k \<^bold>\<times> l = (if k \<in> {0, - 1} \<and> l \<in> {0, - 1} |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
431 |
then - of_bool (odd k \<^bold>* odd l) |
71420 | 432 |
else of_bool (odd k \<^bold>* odd l) + 2 * ((k div 2) \<^bold>\<times> (l div 2)))\<close> |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
433 |
by auto |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
434 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
435 |
termination |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
436 |
by (relation "measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>))") auto |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
437 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
438 |
declare F.simps [simp del] |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
439 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
440 |
lemma rec: |
71420 | 441 |
\<open>k \<^bold>\<times> l = of_bool (odd k \<^bold>* odd l) + 2 * ((k div 2) \<^bold>\<times> (l div 2))\<close> |
442 |
proof (cases \<open>k \<in> {0, - 1} \<and> l \<in> {0, - 1}\<close>) |
|
443 |
case True |
|
444 |
then have \<open>(k div 2) \<^bold>\<times> (l div 2) = k \<^bold>\<times> l\<close> |
|
445 |
by auto |
|
446 |
moreover have \<open>of_bool (odd k \<^bold>* odd l) = - (k \<^bold>\<times> l)\<close> |
|
447 |
using True by (simp add: F.simps [of k l]) |
|
448 |
ultimately show ?thesis by simp |
|
449 |
next |
|
450 |
case False |
|
451 |
then show ?thesis |
|
452 |
by (auto simp add: ac_simps F.simps [of k l]) |
|
453 |
qed |
|
454 |
||
455 |
lemma bit_eq_iff: |
|
456 |
\<open>bit (k \<^bold>\<times> l) n \<longleftrightarrow> bit k n \<^bold>* bit l n\<close> |
|
457 |
proof (induction n arbitrary: k l) |
|
458 |
case 0 |
|
459 |
then show ?case |
|
460 |
by (simp add: rec [of k l]) |
|
461 |
next |
|
462 |
case (Suc n) |
|
463 |
then show ?case |
|
464 |
by (simp add: rec [of k l]) |
|
465 |
qed |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
466 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
467 |
sublocale abel_semigroup F |
71420 | 468 |
by standard (simp_all add: Parity.bit_eq_iff bit_eq_iff ac_simps) |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
469 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
470 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
471 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
472 |
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
|
473 |
begin |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
474 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
475 |
global_interpretation and_int: zip_int "(\<and>)" |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
476 |
defines and_int = and_int.F |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
477 |
by standard |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
478 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
479 |
global_interpretation and_int: semilattice "(AND) :: int \<Rightarrow> int \<Rightarrow> int" |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
480 |
proof (rule semilattice.intro, fact and_int.abel_semigroup_axioms, standard) |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
481 |
show "k AND k = k" for k :: int |
71420 | 482 |
by (simp add: bit_eq_iff and_int.bit_eq_iff) |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
483 |
qed |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
484 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
485 |
global_interpretation or_int: zip_int "(\<or>)" |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
486 |
defines or_int = or_int.F |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
487 |
by standard |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
488 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
489 |
global_interpretation or_int: semilattice "(OR) :: int \<Rightarrow> int \<Rightarrow> int" |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
490 |
proof (rule semilattice.intro, fact or_int.abel_semigroup_axioms, standard) |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
491 |
show "k OR k = k" for k :: int |
71420 | 492 |
by (simp add: bit_eq_iff or_int.bit_eq_iff) |
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
493 |
qed |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
494 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
495 |
global_interpretation xor_int: zip_int "(\<noteq>)" |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
496 |
defines xor_int = xor_int.F |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
497 |
by standard |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
498 |
|
71420 | 499 |
definition not_int :: \<open>int \<Rightarrow> int\<close> |
500 |
where \<open>not_int k = - k - 1\<close> |
|
501 |
||
502 |
lemma not_int_rec: |
|
503 |
"NOT k = of_bool (even k) + 2 * NOT (k div 2)" for k :: int |
|
504 |
by (auto simp add: not_int_def elim: oddE) |
|
505 |
||
506 |
lemma even_not_iff_int: |
|
507 |
\<open>even (NOT k) \<longleftrightarrow> odd k\<close> for k :: int |
|
508 |
by (simp add: not_int_def) |
|
509 |
||
510 |
lemma not_int_div_2: |
|
511 |
\<open>NOT k div 2 = NOT (k div 2)\<close> for k :: int |
|
512 |
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
|
513 |
|
71186 | 514 |
lemma bit_not_iff_int: |
515 |
\<open>bit (NOT k) n \<longleftrightarrow> \<not> bit k n\<close> |
|
516 |
for k :: int |
|
71420 | 517 |
by (induction n arbitrary: k) (simp_all add: not_int_div_2 even_not_iff_int) |
71186 | 518 |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
519 |
instance proof |
71186 | 520 |
fix k l :: int and n :: nat |
71409 | 521 |
show \<open>- k = NOT (k - 1)\<close> |
522 |
by (simp add: not_int_def) |
|
71186 | 523 |
show \<open>bit (k AND l) n \<longleftrightarrow> bit k n \<and> bit l n\<close> |
71420 | 524 |
by (fact and_int.bit_eq_iff) |
71186 | 525 |
show \<open>bit (k OR l) n \<longleftrightarrow> bit k n \<or> bit l n\<close> |
71420 | 526 |
by (fact or_int.bit_eq_iff) |
71186 | 527 |
show \<open>bit (k XOR l) n \<longleftrightarrow> bit k n \<noteq> bit l n\<close> |
71420 | 528 |
by (fact xor_int.bit_eq_iff) |
529 |
qed (simp_all add: bit_not_iff_int) |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
530 |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
531 |
end |
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
532 |
|
71442 | 533 |
|
534 |
subsubsection \<open>Instances for \<^typ>\<open>integer\<close> and \<^typ>\<open>natural\<close>\<close> |
|
535 |
||
536 |
unbundle integer.lifting natural.lifting |
|
537 |
||
538 |
context |
|
539 |
includes lifting_syntax |
|
540 |
begin |
|
541 |
||
542 |
lemma transfer_rule_bit_integer [transfer_rule]: |
|
543 |
\<open>((pcr_integer :: int \<Rightarrow> integer \<Rightarrow> bool) ===> (=)) bit bit\<close> |
|
544 |
by (unfold bit_def) transfer_prover |
|
545 |
||
546 |
lemma transfer_rule_bit_natural [transfer_rule]: |
|
547 |
\<open>((pcr_natural :: nat \<Rightarrow> natural \<Rightarrow> bool) ===> (=)) bit bit\<close> |
|
548 |
by (unfold bit_def) transfer_prover |
|
549 |
||
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
550 |
end |
71442 | 551 |
|
552 |
instantiation integer :: ring_bit_operations |
|
553 |
begin |
|
554 |
||
555 |
lift_definition not_integer :: \<open>integer \<Rightarrow> integer\<close> |
|
556 |
is not . |
|
557 |
||
558 |
lift_definition and_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close> |
|
559 |
is \<open>and\<close> . |
|
560 |
||
561 |
lift_definition or_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close> |
|
562 |
is or . |
|
563 |
||
564 |
lift_definition xor_integer :: \<open>integer \<Rightarrow> integer \<Rightarrow> integer\<close> |
|
565 |
is xor . |
|
566 |
||
567 |
instance proof |
|
568 |
fix k l :: \<open>integer\<close> and n :: nat |
|
569 |
show \<open>- k = NOT (k - 1)\<close> |
|
570 |
by transfer (simp add: minus_eq_not_minus_1) |
|
571 |
show \<open>bit (NOT k) n \<longleftrightarrow> (2 :: integer) ^ n \<noteq> 0 \<and> \<not> bit k n\<close> |
|
572 |
by transfer (fact bit_not_iff) |
|
573 |
show \<open>bit (k AND l) n \<longleftrightarrow> bit k n \<and> bit l n\<close> |
|
574 |
by transfer (fact and_int.bit_eq_iff) |
|
575 |
show \<open>bit (k OR l) n \<longleftrightarrow> bit k n \<or> bit l n\<close> |
|
576 |
by transfer (fact or_int.bit_eq_iff) |
|
577 |
show \<open>bit (k XOR l) n \<longleftrightarrow> bit k n \<noteq> bit l n\<close> |
|
578 |
by transfer (fact xor_int.bit_eq_iff) |
|
579 |
qed |
|
580 |
||
581 |
end |
|
582 |
||
583 |
instantiation natural :: semiring_bit_operations |
|
584 |
begin |
|
585 |
||
586 |
lift_definition and_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close> |
|
587 |
is \<open>and\<close> . |
|
588 |
||
589 |
lift_definition or_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close> |
|
590 |
is or . |
|
591 |
||
592 |
lift_definition xor_natural :: \<open>natural \<Rightarrow> natural \<Rightarrow> natural\<close> |
|
593 |
is xor . |
|
594 |
||
595 |
instance proof |
|
596 |
fix m n :: \<open>natural\<close> and q :: nat |
|
597 |
show \<open>bit (m AND n) q \<longleftrightarrow> bit m q \<and> bit n q\<close> |
|
598 |
by transfer (fact and_nat.bit_eq_iff) |
|
599 |
show \<open>bit (m OR n) q \<longleftrightarrow> bit m q \<or> bit n q\<close> |
|
600 |
by transfer (fact or_nat.bit_eq_iff) |
|
601 |
show \<open>bit (m XOR n) q \<longleftrightarrow> bit m q \<noteq> bit n q\<close> |
|
602 |
by transfer (fact xor_nat.bit_eq_iff) |
|
603 |
qed |
|
604 |
||
605 |
end |
|
606 |
||
607 |
lifting_update integer.lifting |
|
608 |
lifting_forget integer.lifting |
|
609 |
||
610 |
lifting_update natural.lifting |
|
611 |
lifting_forget natural.lifting |
|
612 |
||
613 |
end |