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