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