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