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