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