| author | wenzelm |
| Wed, 14 Feb 2024 14:41:18 +0100 | |
| changeset 79604 | 0e8ac7db1f4d |
| parent 79590 | b14c4cb37d99 |
| child 79610 | ad29777e8746 |
| 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 |
| 74101 | 7 |
imports Presburger Groups_List |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
8 |
begin |
| 74101 | 9 |
|
10 |
subsection \<open>Abstract bit structures\<close> |
|
11 |
||
|
79541
4f40225936d1
common type class for trivial properties on div/mod
haftmann
parents:
79531
diff
changeset
|
12 |
class semiring_bits = semiring_parity + semiring_modulo_trivial + |
|
79480
c7cb1bf6efa0
consolidated name of lemma analogously to nat/int/word_bit_induct
haftmann
parents:
79117
diff
changeset
|
13 |
assumes bit_induct [case_names stable rec]: |
| 74101 | 14 |
\<open>(\<And>a. a div 2 = a \<Longrightarrow> P a) |
15 |
\<Longrightarrow> (\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a)) |
|
16 |
\<Longrightarrow> P a\<close> |
|
| 79555 | 17 |
assumes half_div_exp_eq: \<open>a div 2 div 2 ^ n = a div 2 ^ Suc n\<close> |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
18 |
and even_double_div_exp_iff: \<open>2 ^ Suc n \<noteq> 0 \<Longrightarrow> even (2 * a div 2 ^ Suc n) \<longleftrightarrow> even (a div 2 ^ n)\<close> |
| 79585 | 19 |
and even_mod_exp_div_exp_iff: \<open>even (a mod 2 ^ m div 2 ^ n) \<longleftrightarrow> m \<le> n \<or> even (a div 2 ^ n)\<close> |
| 74101 | 20 |
fixes bit :: \<open>'a \<Rightarrow> nat \<Rightarrow> bool\<close> |
21 |
assumes bit_iff_odd: \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close> |
|
22 |
begin |
|
23 |
||
24 |
text \<open> |
|
25 |
Having \<^const>\<open>bit\<close> as definitional class operation |
|
26 |
takes into account that specific instances can be implemented |
|
27 |
differently wrt. code generation. |
|
28 |
\<close> |
|
29 |
||
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
30 |
lemma half_1 [simp]: |
| 74101 | 31 |
\<open>1 div 2 = 0\<close> |
| 79488 | 32 |
using even_half_succ_eq [of 0] by simp |
| 74101 | 33 |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
34 |
lemma div_exp_eq_funpow_half: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
35 |
\<open>a div 2 ^ n = ((\<lambda>a. a div 2) ^^ n) a\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
36 |
proof - |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
37 |
have \<open>((\<lambda>a. a div 2) ^^ n) = (\<lambda>a. a div 2 ^ n)\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
38 |
by (induction n) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
39 |
(simp_all del: funpow.simps power.simps add: power_0 funpow_Suc_right half_div_exp_eq) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
40 |
then show ?thesis |
| 79481 | 41 |
by simp |
42 |
qed |
|
43 |
||
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
44 |
lemma div_exp_eq: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
45 |
\<open>a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
46 |
by (simp add: div_exp_eq_funpow_half Groups.add.commute [of m] funpow_add) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
47 |
|
| 75085 | 48 |
lemma bit_0: |
| 74101 | 49 |
\<open>bit a 0 \<longleftrightarrow> odd a\<close> |
50 |
by (simp add: bit_iff_odd) |
|
51 |
||
52 |
lemma bit_Suc: |
|
53 |
\<open>bit a (Suc n) \<longleftrightarrow> bit (a div 2) n\<close> |
|
54 |
using div_exp_eq [of a 1 n] by (simp add: bit_iff_odd) |
|
55 |
||
56 |
lemma bit_rec: |
|
57 |
\<open>bit a n \<longleftrightarrow> (if n = 0 then odd a else bit (a div 2) (n - 1))\<close> |
|
| 75085 | 58 |
by (cases n) (simp_all add: bit_Suc bit_0) |
| 74101 | 59 |
|
60 |
context |
|
61 |
fixes a |
|
62 |
assumes stable: \<open>a div 2 = a\<close> |
|
63 |
begin |
|
64 |
||
65 |
lemma bits_stable_imp_add_self: |
|
66 |
\<open>a + a mod 2 = 0\<close> |
|
67 |
proof - |
|
68 |
have \<open>a div 2 * 2 + a mod 2 = a\<close> |
|
69 |
by (fact div_mult_mod_eq) |
|
70 |
then have \<open>a * 2 + a mod 2 = a\<close> |
|
71 |
by (simp add: stable) |
|
72 |
then show ?thesis |
|
73 |
by (simp add: mult_2_right ac_simps) |
|
74 |
qed |
|
75 |
||
76 |
lemma stable_imp_bit_iff_odd: |
|
77 |
\<open>bit a n \<longleftrightarrow> odd a\<close> |
|
| 75085 | 78 |
by (induction n) (simp_all add: stable bit_Suc bit_0) |
| 74101 | 79 |
|
80 |
end |
|
81 |
||
| 79585 | 82 |
lemma bit_iff_odd_imp_stable: |
| 74101 | 83 |
\<open>a div 2 = a\<close> if \<open>\<And>n. bit a n \<longleftrightarrow> odd a\<close> |
|
79480
c7cb1bf6efa0
consolidated name of lemma analogously to nat/int/word_bit_induct
haftmann
parents:
79117
diff
changeset
|
84 |
using that proof (induction a rule: bit_induct) |
| 74101 | 85 |
case (stable a) |
86 |
then show ?case |
|
87 |
by simp |
|
88 |
next |
|
89 |
case (rec a b) |
|
90 |
from rec.prems [of 1] have [simp]: \<open>b = odd a\<close> |
|
| 75085 | 91 |
by (simp add: rec.hyps bit_Suc bit_0) |
| 74101 | 92 |
from rec.hyps have hyp: \<open>(of_bool (odd a) + 2 * a) div 2 = a\<close> |
93 |
by simp |
|
94 |
have \<open>bit a n \<longleftrightarrow> odd a\<close> for n |
|
95 |
using rec.prems [of \<open>Suc n\<close>] by (simp add: hyp bit_Suc) |
|
96 |
then have \<open>a div 2 = a\<close> |
|
97 |
by (rule rec.IH) |
|
98 |
then have \<open>of_bool (odd a) + 2 * a = 2 * (a div 2) + of_bool (odd a)\<close> |
|
99 |
by (simp add: ac_simps) |
|
100 |
also have \<open>\<dots> = a\<close> |
|
101 |
using mult_div_mod_eq [of 2 a] |
|
102 |
by (simp add: of_bool_odd_eq_mod_2) |
|
103 |
finally show ?case |
|
104 |
using \<open>a div 2 = a\<close> by (simp add: hyp) |
|
105 |
qed |
|
106 |
||
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
107 |
lemma even_succ_div_exp [simp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
108 |
\<open>(1 + a) div 2 ^ n = a div 2 ^ n\<close> if \<open>even a\<close> and \<open>n > 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
109 |
proof (cases n) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
110 |
case 0 |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
111 |
with that show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
112 |
by simp |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
113 |
next |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
114 |
case (Suc n) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
115 |
with \<open>even a\<close> have \<open>(1 + a) div 2 ^ Suc n = a div 2 ^ Suc n\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
116 |
proof (induction n) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
117 |
case 0 |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
118 |
then show ?case |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
119 |
by simp |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
120 |
next |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
121 |
case (Suc n) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
122 |
then show ?case |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
123 |
using div_exp_eq [of _ 1 \<open>Suc n\<close>, symmetric] |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
124 |
by simp |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
125 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
126 |
with Suc show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
127 |
by simp |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
128 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
129 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
130 |
lemma even_succ_mod_exp [simp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
131 |
\<open>(1 + a) mod 2 ^ n = 1 + (a mod 2 ^ n)\<close> if \<open>even a\<close> and \<open>n > 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
132 |
using div_mult_mod_eq [of \<open>1 + a\<close> \<open>2 ^ n\<close>] div_mult_mod_eq [of a \<open>2 ^ n\<close>] that |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
133 |
by simp (metis (full_types) add.left_commute add_left_imp_eq) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
134 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
135 |
named_theorems bit_simps \<open>Simplification rules for \<^const>\<open>bit\<close>\<close> |
| 74101 | 136 |
|
| 79017 | 137 |
definition possible_bit :: \<open>'a itself \<Rightarrow> nat \<Rightarrow> bool\<close> |
138 |
where \<open>possible_bit TYPE('a) n \<longleftrightarrow> 2 ^ n \<noteq> 0\<close>
|
|
|
79018
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
139 |
\<comment> \<open>This auxiliary avoids non-termination with extensionality.\<close> |
| 79017 | 140 |
|
141 |
lemma possible_bit_0 [simp]: |
|
142 |
\<open>possible_bit TYPE('a) 0\<close>
|
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
143 |
by (simp add: possible_bit_def) |
|
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
144 |
|
|
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
145 |
lemma fold_possible_bit: |
| 79017 | 146 |
\<open>2 ^ n = 0 \<longleftrightarrow> \<not> possible_bit TYPE('a) n\<close>
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
147 |
by (simp add: possible_bit_def) |
|
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
148 |
|
|
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
149 |
lemma bit_imp_possible_bit: |
| 79017 | 150 |
\<open>possible_bit TYPE('a) n\<close> if \<open>bit a n\<close>
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
151 |
by (rule ccontr) (use that in \<open>auto simp add: bit_iff_odd possible_bit_def\<close>) |
| 79017 | 152 |
|
153 |
lemma impossible_bit: |
|
154 |
\<open>\<not> bit a n\<close> if \<open>\<not> possible_bit TYPE('a) n\<close>
|
|
155 |
using that by (blast dest: bit_imp_possible_bit) |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
156 |
|
|
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
157 |
lemma possible_bit_less_imp: |
| 79017 | 158 |
\<open>possible_bit TYPE('a) j\<close> if \<open>possible_bit TYPE('a) i\<close> \<open>j \<le> i\<close>
|
159 |
using power_add [of 2 j \<open>i - j\<close>] that mult_not_zero [of \<open>2 ^ j\<close> \<open>2 ^ (i - j)\<close>] |
|
160 |
by (simp add: possible_bit_def) |
|
161 |
||
162 |
lemma possible_bit_min [simp]: |
|
163 |
\<open>possible_bit TYPE('a) (min i j) \<longleftrightarrow> possible_bit TYPE('a) i \<or> possible_bit TYPE('a) j\<close>
|
|
164 |
by (auto simp add: min_def elim: possible_bit_less_imp) |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
165 |
|
| 74101 | 166 |
lemma bit_eqI: |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
167 |
\<open>a = b\<close> if \<open>\<And>n. possible_bit TYPE('a) n \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
|
| 74101 | 168 |
proof - |
169 |
have \<open>bit a n \<longleftrightarrow> bit b n\<close> for n |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
170 |
proof (cases \<open>possible_bit TYPE('a) n\<close>)
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
171 |
case False |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
172 |
then show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
173 |
by (simp add: impossible_bit) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
174 |
next |
| 74101 | 175 |
case True |
176 |
then show ?thesis |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
177 |
by (rule that) |
| 74101 | 178 |
qed |
|
79480
c7cb1bf6efa0
consolidated name of lemma analogously to nat/int/word_bit_induct
haftmann
parents:
79117
diff
changeset
|
179 |
then show ?thesis proof (induction a arbitrary: b rule: bit_induct) |
| 74101 | 180 |
case (stable a) |
181 |
from stable(2) [of 0] have **: \<open>even b \<longleftrightarrow> even a\<close> |
|
| 75085 | 182 |
by (simp add: bit_0) |
| 74101 | 183 |
have \<open>b div 2 = b\<close> |
| 79585 | 184 |
proof (rule bit_iff_odd_imp_stable) |
| 74101 | 185 |
fix n |
186 |
from stable have *: \<open>bit b n \<longleftrightarrow> bit a n\<close> |
|
187 |
by simp |
|
188 |
also have \<open>bit a n \<longleftrightarrow> odd a\<close> |
|
189 |
using stable by (simp add: stable_imp_bit_iff_odd) |
|
190 |
finally show \<open>bit b n \<longleftrightarrow> odd b\<close> |
|
191 |
by (simp add: **) |
|
192 |
qed |
|
193 |
from ** have \<open>a mod 2 = b mod 2\<close> |
|
194 |
by (simp add: mod2_eq_if) |
|
195 |
then have \<open>a mod 2 + (a + b) = b mod 2 + (a + b)\<close> |
|
196 |
by simp |
|
197 |
then have \<open>a + a mod 2 + b = b + b mod 2 + a\<close> |
|
198 |
by (simp add: ac_simps) |
|
199 |
with \<open>a div 2 = a\<close> \<open>b div 2 = b\<close> show ?case |
|
200 |
by (simp add: bits_stable_imp_add_self) |
|
201 |
next |
|
202 |
case (rec a p) |
|
203 |
from rec.prems [of 0] have [simp]: \<open>p = odd b\<close> |
|
| 75085 | 204 |
by (simp add: bit_0) |
| 74101 | 205 |
from rec.hyps have \<open>bit a n \<longleftrightarrow> bit (b div 2) n\<close> for n |
206 |
using rec.prems [of \<open>Suc n\<close>] by (simp add: bit_Suc) |
|
207 |
then have \<open>a = b div 2\<close> |
|
208 |
by (rule rec.IH) |
|
209 |
then have \<open>2 * a = 2 * (b div 2)\<close> |
|
210 |
by simp |
|
211 |
then have \<open>b mod 2 + 2 * a = b mod 2 + 2 * (b div 2)\<close> |
|
212 |
by simp |
|
213 |
also have \<open>\<dots> = b\<close> |
|
214 |
by (fact mod_mult_div_eq) |
|
215 |
finally show ?case |
|
216 |
by (auto simp add: mod2_eq_if) |
|
217 |
qed |
|
218 |
qed |
|
219 |
||
220 |
lemma bit_eq_rec: |
|
221 |
\<open>a = b \<longleftrightarrow> (even a \<longleftrightarrow> even b) \<and> a div 2 = b div 2\<close> (is \<open>?P = ?Q\<close>) |
|
222 |
proof |
|
223 |
assume ?P |
|
224 |
then show ?Q |
|
225 |
by simp |
|
226 |
next |
|
227 |
assume ?Q |
|
228 |
then have \<open>even a \<longleftrightarrow> even b\<close> and \<open>a div 2 = b div 2\<close> |
|
229 |
by simp_all |
|
230 |
show ?P |
|
231 |
proof (rule bit_eqI) |
|
232 |
fix n |
|
233 |
show \<open>bit a n \<longleftrightarrow> bit b n\<close> |
|
234 |
proof (cases n) |
|
235 |
case 0 |
|
236 |
with \<open>even a \<longleftrightarrow> even b\<close> show ?thesis |
|
| 75085 | 237 |
by (simp add: bit_0) |
| 74101 | 238 |
next |
239 |
case (Suc n) |
|
240 |
moreover from \<open>a div 2 = b div 2\<close> have \<open>bit (a div 2) n = bit (b div 2) n\<close> |
|
241 |
by simp |
|
242 |
ultimately show ?thesis |
|
243 |
by (simp add: bit_Suc) |
|
244 |
qed |
|
245 |
qed |
|
246 |
qed |
|
247 |
||
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
248 |
lemma bit_eq_iff: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
249 |
\<open>a = b \<longleftrightarrow> (\<forall>n. possible_bit TYPE('a) n \<longrightarrow> bit a n \<longleftrightarrow> bit b n)\<close>
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
250 |
by (auto intro: bit_eqI simp add: possible_bit_def) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
251 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
252 |
lemma bit_0_eq [simp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
253 |
\<open>bit 0 = \<bottom>\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
254 |
proof - |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
255 |
have \<open>0 div 2 ^ n = 0\<close> for n |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
256 |
unfolding div_exp_eq_funpow_half by (induction n) simp_all |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
257 |
then show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
258 |
by (simp add: fun_eq_iff bit_iff_odd) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
259 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
260 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
261 |
lemma bit_double_Suc_iff: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
262 |
\<open>bit (2 * a) (Suc n) \<longleftrightarrow> possible_bit TYPE('a) (Suc n) \<and> bit a n\<close>
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
263 |
using even_double_div_exp_iff [of n a] |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
264 |
by (cases \<open>possible_bit TYPE('a) (Suc n)\<close>)
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
265 |
(auto simp add: bit_iff_odd possible_bit_def) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
266 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
267 |
lemma bit_double_iff [bit_simps]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
268 |
\<open>bit (2 * a) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n \<noteq> 0 \<and> bit a (n - 1)\<close>
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
269 |
by (cases n) (simp_all add: bit_0 bit_double_Suc_iff) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
270 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
271 |
lemma even_bit_succ_iff: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
272 |
\<open>bit (1 + a) n \<longleftrightarrow> bit a n \<or> n = 0\<close> if \<open>even a\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
273 |
using that by (cases \<open>n = 0\<close>) (simp_all add: bit_iff_odd) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
274 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
275 |
lemma odd_bit_iff_bit_pred: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
276 |
\<open>bit a n \<longleftrightarrow> bit (a - 1) n \<or> n = 0\<close> if \<open>odd a\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
277 |
proof - |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
278 |
from \<open>odd a\<close> obtain b where \<open>a = 2 * b + 1\<close> .. |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
279 |
moreover have \<open>bit (2 * b) n \<or> n = 0 \<longleftrightarrow> bit (1 + 2 * b) n\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
280 |
using even_bit_succ_iff by simp |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
281 |
ultimately show ?thesis by (simp add: ac_simps) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
282 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
283 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
284 |
lemma bit_exp_iff [bit_simps]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
285 |
\<open>bit (2 ^ m) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n = m\<close>
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
286 |
proof (cases \<open>possible_bit TYPE('a) n\<close>)
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
287 |
case False |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
288 |
then show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
289 |
by (simp add: impossible_bit) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
290 |
next |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
291 |
case True |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
292 |
then show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
293 |
proof (induction n arbitrary: m) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
294 |
case 0 |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
295 |
show ?case |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
296 |
by (simp add: bit_0) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
297 |
next |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
298 |
case (Suc n) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
299 |
then have \<open>possible_bit TYPE('a) n\<close>
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
300 |
by (simp add: possible_bit_less_imp) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
301 |
show ?case |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
302 |
proof (cases m) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
303 |
case 0 |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
304 |
then show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
305 |
by (simp add: bit_Suc) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
306 |
next |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
307 |
case (Suc m) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
308 |
with Suc.IH [of m] \<open>possible_bit TYPE('a) n\<close> show ?thesis
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
309 |
by (simp add: bit_double_Suc_iff) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
310 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
311 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
312 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
313 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
314 |
lemma bit_1_iff [bit_simps]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
315 |
\<open>bit 1 n \<longleftrightarrow> n = 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
316 |
using bit_exp_iff [of 0 n] by auto |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
317 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
318 |
lemma bit_2_iff [bit_simps]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
319 |
\<open>bit 2 n \<longleftrightarrow> possible_bit TYPE('a) 1 \<and> n = 1\<close>
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
320 |
using bit_exp_iff [of 1 n] by auto |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
321 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
322 |
lemma bit_of_bool_iff [bit_simps]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
323 |
\<open>bit (of_bool b) n \<longleftrightarrow> n = 0 \<and> b\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
324 |
by (simp add: bit_1_iff) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
325 |
|
| 74101 | 326 |
lemma bit_mod_2_iff [simp]: |
327 |
\<open>bit (a mod 2) n \<longleftrightarrow> n = 0 \<and> odd a\<close> |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
328 |
by (simp add: mod_2_eq_odd bit_simps) |
| 74101 | 329 |
|
330 |
lemma bit_disjunctive_add_iff: |
|
331 |
\<open>bit (a + b) n \<longleftrightarrow> bit a n \<or> bit b n\<close> |
|
332 |
if \<open>\<And>n. \<not> bit a n \<or> \<not> bit b n\<close> |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
333 |
proof (cases \<open>possible_bit TYPE('a) n\<close>)
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
334 |
case False |
| 74101 | 335 |
then show ?thesis |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
336 |
by (auto dest: impossible_bit) |
| 74101 | 337 |
next |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
338 |
case True |
| 74101 | 339 |
with that show ?thesis proof (induction n arbitrary: a b) |
340 |
case 0 |
|
341 |
from "0.prems"(1) [of 0] show ?case |
|
| 75085 | 342 |
by (auto simp add: bit_0) |
| 74101 | 343 |
next |
344 |
case (Suc n) |
|
345 |
from Suc.prems(1) [of 0] have even: \<open>even a \<or> even b\<close> |
|
| 75085 | 346 |
by (auto simp add: bit_0) |
| 74101 | 347 |
have bit: \<open>\<not> bit (a div 2) n \<or> \<not> bit (b div 2) n\<close> for n |
348 |
using Suc.prems(1) [of \<open>Suc n\<close>] by (simp add: bit_Suc) |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
349 |
from Suc.prems(2) have \<open>possible_bit TYPE('a) (Suc n)\<close> \<open>possible_bit TYPE('a) n\<close>
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
350 |
by (simp_all add: possible_bit_less_imp) |
| 74101 | 351 |
have \<open>a + b = (a div 2 * 2 + a mod 2) + (b div 2 * 2 + b mod 2)\<close> |
352 |
using div_mult_mod_eq [of a 2] div_mult_mod_eq [of b 2] by simp |
|
353 |
also have \<open>\<dots> = of_bool (odd a \<or> odd b) + 2 * (a div 2 + b div 2)\<close> |
|
354 |
using even by (auto simp add: algebra_simps mod2_eq_if) |
|
355 |
finally have \<open>bit ((a + b) div 2) n \<longleftrightarrow> bit (a div 2 + b div 2) n\<close> |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
356 |
using \<open>possible_bit TYPE('a) (Suc n)\<close> by simp (simp_all flip: bit_Suc add: bit_double_iff possible_bit_def)
|
| 74101 | 357 |
also have \<open>\<dots> \<longleftrightarrow> bit (a div 2) n \<or> bit (b div 2) n\<close> |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
358 |
using bit \<open>possible_bit TYPE('a) n\<close> by (rule Suc.IH)
|
| 74101 | 359 |
finally show ?case |
360 |
by (simp add: bit_Suc) |
|
361 |
qed |
|
362 |
qed |
|
363 |
||
364 |
end |
|
365 |
||
366 |
lemma nat_bit_induct [case_names zero even odd]: |
|
| 79017 | 367 |
\<open>P n\<close> if zero: \<open>P 0\<close> |
368 |
and even: \<open>\<And>n. P n \<Longrightarrow> n > 0 \<Longrightarrow> P (2 * n)\<close> |
|
369 |
and odd: \<open>\<And>n. P n \<Longrightarrow> P (Suc (2 * n))\<close> |
|
| 74101 | 370 |
proof (induction n rule: less_induct) |
371 |
case (less n) |
|
| 79017 | 372 |
show \<open>P n\<close> |
373 |
proof (cases \<open>n = 0\<close>) |
|
| 74101 | 374 |
case True with zero show ?thesis by simp |
375 |
next |
|
376 |
case False |
|
| 79017 | 377 |
with less have hyp: \<open>P (n div 2)\<close> by simp |
| 74101 | 378 |
show ?thesis |
| 79017 | 379 |
proof (cases \<open>even n\<close>) |
| 74101 | 380 |
case True |
| 79017 | 381 |
then have \<open>n \<noteq> 1\<close> |
| 74101 | 382 |
by auto |
| 79017 | 383 |
with \<open>n \<noteq> 0\<close> have \<open>n div 2 > 0\<close> |
| 74101 | 384 |
by simp |
| 79017 | 385 |
with \<open>even n\<close> hyp even [of \<open>n div 2\<close>] show ?thesis |
| 74101 | 386 |
by simp |
387 |
next |
|
388 |
case False |
|
| 79017 | 389 |
with hyp odd [of \<open>n div 2\<close>] show ?thesis |
| 74101 | 390 |
by simp |
391 |
qed |
|
392 |
qed |
|
393 |
qed |
|
394 |
||
395 |
instantiation nat :: semiring_bits |
|
396 |
begin |
|
397 |
||
398 |
definition bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> bool\<close> |
|
399 |
where \<open>bit_nat m n \<longleftrightarrow> odd (m div 2 ^ n)\<close> |
|
400 |
||
401 |
instance |
|
402 |
proof |
|
403 |
show \<open>P n\<close> if stable: \<open>\<And>n. n div 2 = n \<Longrightarrow> P n\<close> |
|
404 |
and rec: \<open>\<And>n b. P n \<Longrightarrow> (of_bool b + 2 * n) div 2 = n \<Longrightarrow> P (of_bool b + 2 * n)\<close> |
|
405 |
for P and n :: nat |
|
406 |
proof (induction n rule: nat_bit_induct) |
|
407 |
case zero |
|
408 |
from stable [of 0] show ?case |
|
409 |
by simp |
|
410 |
next |
|
411 |
case (even n) |
|
412 |
with rec [of n False] show ?case |
|
413 |
by simp |
|
414 |
next |
|
415 |
case (odd n) |
|
416 |
with rec [of n True] show ?case |
|
417 |
by simp |
|
418 |
qed |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
419 |
show \<open>even (q mod 2 ^ m div 2 ^ n) \<longleftrightarrow> m \<le> n \<or> even (q div 2 ^ n)\<close> for q m n :: nat |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
420 |
proof (cases \<open>m \<le> n\<close>) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
421 |
case True |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
422 |
moreover define r where \<open>r = n - m\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
423 |
ultimately have \<open>n = m + r\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
424 |
by simp |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
425 |
with True show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
426 |
by (simp add: power_add div_mult2_eq) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
427 |
next |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
428 |
case False |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
429 |
moreover define r where \<open>r = m - Suc n\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
430 |
ultimately have \<open>m = n + Suc r\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
431 |
by simp |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
432 |
moreover have \<open>even (q mod 2 ^ (n + Suc r) div 2 ^ n) \<longleftrightarrow> even (q div 2 ^ n)\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
433 |
by (simp only: power_add) (simp add: mod_mult2_eq dvd_mod_iff) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
434 |
ultimately show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
435 |
by simp |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
436 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
437 |
qed (auto simp add: div_mult2_eq bit_nat_def) |
| 74101 | 438 |
|
439 |
end |
|
440 |
||
| 79017 | 441 |
lemma possible_bit_nat [simp]: |
442 |
\<open>possible_bit TYPE(nat) n\<close> |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
443 |
by (simp add: possible_bit_def) |
|
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
444 |
|
| 79069 | 445 |
lemma bit_Suc_0_iff [bit_simps]: |
446 |
\<open>bit (Suc 0) n \<longleftrightarrow> n = 0\<close> |
|
447 |
using bit_1_iff [of n, where ?'a = nat] by simp |
|
448 |
||
| 74497 | 449 |
lemma not_bit_Suc_0_Suc [simp]: |
450 |
\<open>\<not> bit (Suc 0) (Suc n)\<close> |
|
451 |
by (simp add: bit_Suc) |
|
452 |
||
453 |
lemma not_bit_Suc_0_numeral [simp]: |
|
454 |
\<open>\<not> bit (Suc 0) (numeral n)\<close> |
|
455 |
by (simp add: numeral_eq_Suc) |
|
456 |
||
| 74101 | 457 |
context semiring_bits |
458 |
begin |
|
459 |
||
460 |
lemma bit_of_nat_iff [bit_simps]: |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
461 |
\<open>bit (of_nat m) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> bit m n\<close>
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
462 |
proof (cases \<open>possible_bit TYPE('a) n\<close>)
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
463 |
case False |
| 74101 | 464 |
then show ?thesis |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
465 |
by (simp add: impossible_bit) |
| 74101 | 466 |
next |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
467 |
case True |
| 74101 | 468 |
then have \<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close> |
469 |
proof (induction m arbitrary: n rule: nat_bit_induct) |
|
470 |
case zero |
|
471 |
then show ?case |
|
472 |
by simp |
|
473 |
next |
|
474 |
case (even m) |
|
475 |
then show ?case |
|
476 |
by (cases n) |
|
| 75085 | 477 |
(auto simp add: bit_double_iff Bit_Operations.bit_double_iff possible_bit_def bit_0 dest: mult_not_zero) |
| 74101 | 478 |
next |
479 |
case (odd m) |
|
480 |
then show ?case |
|
481 |
by (cases n) |
|
| 75085 | 482 |
(auto simp add: bit_double_iff even_bit_succ_iff possible_bit_def |
483 |
Bit_Operations.bit_Suc Bit_Operations.bit_0 dest: mult_not_zero) |
|
| 74101 | 484 |
qed |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
485 |
with True show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
486 |
by simp |
| 74101 | 487 |
qed |
488 |
||
489 |
end |
|
490 |
||
| 79017 | 491 |
lemma int_bit_induct [case_names zero minus even odd]: |
492 |
\<open>P k\<close> if zero_int: \<open>P 0\<close> |
|
493 |
and minus_int: \<open>P (- 1)\<close> |
|
494 |
and even_int: \<open>\<And>k. P k \<Longrightarrow> k \<noteq> 0 \<Longrightarrow> P (k * 2)\<close> |
|
495 |
and odd_int: \<open>\<And>k. P k \<Longrightarrow> k \<noteq> - 1 \<Longrightarrow> P (1 + (k * 2))\<close> for k :: int |
|
496 |
proof (cases \<open>k \<ge> 0\<close>) |
|
497 |
case True |
|
498 |
define n where \<open>n = nat k\<close> |
|
499 |
with True have \<open>k = int n\<close> |
|
500 |
by simp |
|
501 |
then show \<open>P k\<close> |
|
502 |
proof (induction n arbitrary: k rule: nat_bit_induct) |
|
503 |
case zero |
|
504 |
then show ?case |
|
505 |
by (simp add: zero_int) |
|
506 |
next |
|
507 |
case (even n) |
|
508 |
have \<open>P (int n * 2)\<close> |
|
509 |
by (rule even_int) (use even in simp_all) |
|
510 |
with even show ?case |
|
511 |
by (simp add: ac_simps) |
|
512 |
next |
|
513 |
case (odd n) |
|
514 |
have \<open>P (1 + (int n * 2))\<close> |
|
515 |
by (rule odd_int) (use odd in simp_all) |
|
516 |
with odd show ?case |
|
517 |
by (simp add: ac_simps) |
|
518 |
qed |
|
519 |
next |
|
520 |
case False |
|
521 |
define n where \<open>n = nat (- k - 1)\<close> |
|
522 |
with False have \<open>k = - int n - 1\<close> |
|
523 |
by simp |
|
524 |
then show \<open>P k\<close> |
|
525 |
proof (induction n arbitrary: k rule: nat_bit_induct) |
|
526 |
case zero |
|
527 |
then show ?case |
|
528 |
by (simp add: minus_int) |
|
529 |
next |
|
530 |
case (even n) |
|
531 |
have \<open>P (1 + (- int (Suc n) * 2))\<close> |
|
532 |
by (rule odd_int) (use even in \<open>simp_all add: algebra_simps\<close>) |
|
533 |
also have \<open>\<dots> = - int (2 * n) - 1\<close> |
|
534 |
by (simp add: algebra_simps) |
|
535 |
finally show ?case |
|
536 |
using even.prems by simp |
|
537 |
next |
|
538 |
case (odd n) |
|
539 |
have \<open>P (- int (Suc n) * 2)\<close> |
|
540 |
by (rule even_int) (use odd in \<open>simp_all add: algebra_simps\<close>) |
|
541 |
also have \<open>\<dots> = - int (Suc (2 * n)) - 1\<close> |
|
542 |
by (simp add: algebra_simps) |
|
543 |
finally show ?case |
|
544 |
using odd.prems by simp |
|
545 |
qed |
|
546 |
qed |
|
547 |
||
| 74101 | 548 |
instantiation int :: semiring_bits |
549 |
begin |
|
550 |
||
551 |
definition bit_int :: \<open>int \<Rightarrow> nat \<Rightarrow> bool\<close> |
|
552 |
where \<open>bit_int k n \<longleftrightarrow> odd (k div 2 ^ n)\<close> |
|
553 |
||
554 |
instance |
|
555 |
proof |
|
556 |
show \<open>P k\<close> if stable: \<open>\<And>k. k div 2 = k \<Longrightarrow> P k\<close> |
|
557 |
and rec: \<open>\<And>k b. P k \<Longrightarrow> (of_bool b + 2 * k) div 2 = k \<Longrightarrow> P (of_bool b + 2 * k)\<close> |
|
558 |
for P and k :: int |
|
559 |
proof (induction k rule: int_bit_induct) |
|
560 |
case zero |
|
561 |
from stable [of 0] show ?case |
|
562 |
by simp |
|
563 |
next |
|
564 |
case minus |
|
565 |
from stable [of \<open>- 1\<close>] show ?case |
|
566 |
by simp |
|
567 |
next |
|
568 |
case (even k) |
|
569 |
with rec [of k False] show ?case |
|
570 |
by (simp add: ac_simps) |
|
571 |
next |
|
572 |
case (odd k) |
|
573 |
with rec [of k True] show ?case |
|
574 |
by (simp add: ac_simps) |
|
575 |
qed |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
576 |
show \<open>even (k mod 2 ^ m div 2 ^ n) \<longleftrightarrow> m \<le> n \<or> even (k div 2 ^ n)\<close> for k :: int and m n :: nat |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
577 |
proof (cases \<open>m \<le> n\<close>) |
| 74101 | 578 |
case True |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
579 |
moreover define r where \<open>r = n - m\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
580 |
ultimately have \<open>n = m + r\<close> |
| 74101 | 581 |
by simp |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
582 |
with True show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
583 |
by (simp add: power_add zdiv_zmult2_eq) |
| 74101 | 584 |
next |
585 |
case False |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
586 |
moreover define r where \<open>r = m - Suc n\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
587 |
ultimately have \<open>m = n + Suc r\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
588 |
by simp |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
589 |
moreover have \<open>even (k mod 2 ^ (n + Suc r) div 2 ^ n) \<longleftrightarrow> even (k div 2 ^ n)\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
590 |
by (simp only: power_add) (simp add: zmod_zmult2_eq dvd_mod_iff) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
591 |
ultimately show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
592 |
by simp |
| 74101 | 593 |
qed |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
594 |
qed (auto simp add: zdiv_zmult2_eq bit_int_def) |
| 74101 | 595 |
|
596 |
end |
|
597 |
||
| 79017 | 598 |
lemma possible_bit_int [simp]: |
599 |
\<open>possible_bit TYPE(int) n\<close> |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
600 |
by (simp add: possible_bit_def) |
|
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
601 |
|
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
602 |
lemma bit_nat_iff [bit_simps]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
603 |
\<open>bit (nat k) n \<longleftrightarrow> k \<ge> 0 \<and> bit k n\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
604 |
proof (cases \<open>k \<ge> 0\<close>) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
605 |
case True |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
606 |
moreover define m where \<open>m = nat k\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
607 |
ultimately have \<open>k = int m\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
608 |
by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
609 |
then show ?thesis |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
610 |
by (simp add: bit_simps) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
611 |
next |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
612 |
case False |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
613 |
then show ?thesis |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
614 |
by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
615 |
qed |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
616 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
617 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
618 |
subsection \<open>Bit operations\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
619 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
620 |
class semiring_bit_operations = semiring_bits + |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
621 |
fixes "and" :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close> (infixr \<open>AND\<close> 64) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
622 |
and or :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close> (infixr \<open>OR\<close> 59) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
623 |
and xor :: \<open>'a \<Rightarrow> 'a \<Rightarrow> 'a\<close> (infixr \<open>XOR\<close> 59) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
624 |
and mask :: \<open>nat \<Rightarrow> 'a\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
625 |
and set_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
626 |
and unset_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
627 |
and flip_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
628 |
and push_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
629 |
and drop_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
630 |
and take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
|
79008
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
631 |
assumes and_rec: \<open>a AND b = of_bool (odd a \<and> odd b) + 2 * ((a div 2) AND (b div 2))\<close> |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
632 |
and or_rec: \<open>a OR b = of_bool (odd a \<or> odd b) + 2 * ((a div 2) OR (b div 2))\<close> |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
633 |
and xor_rec: \<open>a XOR b = of_bool (odd a \<noteq> odd b) + 2 * ((a div 2) XOR (b div 2))\<close> |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
634 |
and mask_eq_exp_minus_1: \<open>mask n = 2 ^ n - 1\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
635 |
and set_bit_eq_or: \<open>set_bit n a = a OR push_bit n 1\<close> |
| 79489 | 636 |
and unset_bit_eq_or_xor: \<open>unset_bit n a = (a OR push_bit n 1) XOR push_bit n 1\<close> |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
637 |
and flip_bit_eq_xor: \<open>flip_bit n a = a XOR push_bit n 1\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
638 |
and push_bit_eq_mult: \<open>push_bit n a = a * 2 ^ n\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
639 |
and drop_bit_eq_div: \<open>drop_bit n a = a div 2 ^ n\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
640 |
and take_bit_eq_mod: \<open>take_bit n a = a mod 2 ^ n\<close> |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
641 |
begin |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
642 |
|
| 74101 | 643 |
text \<open> |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
644 |
We want the bitwise operations to bind slightly weaker |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
645 |
than \<open>+\<close> and \<open>-\<close>. |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
646 |
|
| 74101 | 647 |
Logically, \<^const>\<open>push_bit\<close>, |
648 |
\<^const>\<open>drop_bit\<close> and \<^const>\<open>take_bit\<close> are just aliases; having them |
|
649 |
as separate operations makes proofs easier, otherwise proof automation |
|
650 |
would fiddle with concrete expressions \<^term>\<open>2 ^ n\<close> in a way obfuscating the basic |
|
651 |
algebraic relationships between those operations. |
|
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
652 |
|
| 79068 | 653 |
For the sake of code generation operations |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
654 |
are specified as definitional class operations, |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
655 |
taking into account that specific instances of these can be implemented |
| 74101 | 656 |
differently wrt. code generation. |
657 |
\<close> |
|
658 |
||
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
659 |
lemma bit_iff_odd_drop_bit: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
660 |
\<open>bit a n \<longleftrightarrow> odd (drop_bit n a)\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
661 |
by (simp add: bit_iff_odd drop_bit_eq_div) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
662 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
663 |
lemma even_drop_bit_iff_not_bit: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
664 |
\<open>even (drop_bit n a) \<longleftrightarrow> \<not> bit a n\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
665 |
by (simp add: bit_iff_odd_drop_bit) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
666 |
|
|
79008
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
667 |
lemma bit_and_iff [bit_simps]: |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
668 |
\<open>bit (a AND b) n \<longleftrightarrow> bit a n \<and> bit b n\<close> |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
669 |
proof (induction n arbitrary: a b) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
670 |
case 0 |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
671 |
show ?case |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
672 |
by (simp add: bit_0 and_rec [of a b] even_bit_succ_iff) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
673 |
next |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
674 |
case (Suc n) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
675 |
from Suc [of \<open>a div 2\<close> \<open>b div 2\<close>] |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
676 |
show ?case |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
677 |
by (simp add: and_rec [of a b] bit_Suc) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
678 |
(auto simp flip: bit_Suc simp add: bit_double_iff dest: bit_imp_possible_bit) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
679 |
qed |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
680 |
|
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
681 |
lemma bit_or_iff [bit_simps]: |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
682 |
\<open>bit (a OR b) n \<longleftrightarrow> bit a n \<or> bit b n\<close> |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
683 |
proof (induction n arbitrary: a b) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
684 |
case 0 |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
685 |
show ?case |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
686 |
by (simp add: bit_0 or_rec [of a b] even_bit_succ_iff) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
687 |
next |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
688 |
case (Suc n) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
689 |
from Suc [of \<open>a div 2\<close> \<open>b div 2\<close>] |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
690 |
show ?case |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
691 |
by (simp add: or_rec [of a b] bit_Suc) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
692 |
(auto simp flip: bit_Suc simp add: bit_double_iff dest: bit_imp_possible_bit) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
693 |
qed |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
694 |
|
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
695 |
lemma bit_xor_iff [bit_simps]: |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
696 |
\<open>bit (a XOR b) n \<longleftrightarrow> bit a n \<noteq> bit b n\<close> |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
697 |
proof (induction n arbitrary: a b) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
698 |
case 0 |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
699 |
show ?case |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
700 |
by (simp add: bit_0 xor_rec [of a b] even_bit_succ_iff) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
701 |
next |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
702 |
case (Suc n) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
703 |
from Suc [of \<open>a div 2\<close> \<open>b div 2\<close>] |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
704 |
show ?case |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
705 |
by (simp add: xor_rec [of a b] bit_Suc) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
706 |
(auto simp flip: bit_Suc simp add: bit_double_iff dest: bit_imp_possible_bit) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
707 |
qed |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
708 |
|
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
709 |
sublocale "and": semilattice \<open>(AND)\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
710 |
by standard (auto simp add: bit_eq_iff bit_and_iff) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
711 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
712 |
sublocale or: semilattice_neutr \<open>(OR)\<close> 0 |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
713 |
by standard (auto simp add: bit_eq_iff bit_or_iff) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
714 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
715 |
sublocale xor: comm_monoid \<open>(XOR)\<close> 0 |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
716 |
by standard (auto simp add: bit_eq_iff bit_xor_iff) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
717 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
718 |
lemma even_and_iff: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
719 |
\<open>even (a AND b) \<longleftrightarrow> even a \<or> even b\<close> |
| 75085 | 720 |
using bit_and_iff [of a b 0] by (auto simp add: bit_0) |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
721 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
722 |
lemma even_or_iff: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
723 |
\<open>even (a OR b) \<longleftrightarrow> even a \<and> even b\<close> |
| 75085 | 724 |
using bit_or_iff [of a b 0] by (auto simp add: bit_0) |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
725 |
|
| 79588 | 726 |
lemma disjunctive_add: |
727 |
\<open>a + b = a OR b\<close> if \<open>\<And>n. \<not> bit a n \<or> \<not> bit b n\<close> |
|
728 |
by (rule bit_eqI) (use that in \<open>simp add: bit_disjunctive_add_iff bit_or_iff\<close>) |
|
729 |
||
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
730 |
lemma even_xor_iff: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
731 |
\<open>even (a XOR b) \<longleftrightarrow> (even a \<longleftrightarrow> even b)\<close> |
| 75085 | 732 |
using bit_xor_iff [of a b 0] by (auto simp add: bit_0) |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
733 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
734 |
lemma zero_and_eq [simp]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
735 |
\<open>0 AND a = 0\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
736 |
by (simp add: bit_eq_iff bit_and_iff) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
737 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
738 |
lemma and_zero_eq [simp]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
739 |
\<open>a AND 0 = 0\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
740 |
by (simp add: bit_eq_iff bit_and_iff) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
741 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
742 |
lemma one_and_eq: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
743 |
\<open>1 AND a = a mod 2\<close> |
| 75085 | 744 |
by (simp add: bit_eq_iff bit_and_iff) (auto simp add: bit_1_iff bit_0) |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
745 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
746 |
lemma and_one_eq: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
747 |
\<open>a AND 1 = a mod 2\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
748 |
using one_and_eq [of a] by (simp add: ac_simps) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
749 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
750 |
lemma one_or_eq: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
751 |
\<open>1 OR a = a + of_bool (even a)\<close> |
| 75085 | 752 |
by (simp add: bit_eq_iff bit_or_iff add.commute [of _ 1] even_bit_succ_iff) |
753 |
(auto simp add: bit_1_iff bit_0) |
|
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
754 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
755 |
lemma or_one_eq: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
756 |
\<open>a OR 1 = a + of_bool (even a)\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
757 |
using one_or_eq [of a] by (simp add: ac_simps) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
758 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
759 |
lemma one_xor_eq: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
760 |
\<open>1 XOR a = a + of_bool (even a) - of_bool (odd a)\<close> |
| 75085 | 761 |
by (simp add: bit_eq_iff bit_xor_iff add.commute [of _ 1] even_bit_succ_iff) |
762 |
(auto simp add: bit_1_iff odd_bit_iff_bit_pred bit_0 elim: oddE) |
|
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
763 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
764 |
lemma xor_one_eq: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
765 |
\<open>a XOR 1 = a + of_bool (even a) - of_bool (odd a)\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
766 |
using one_xor_eq [of a] by (simp add: ac_simps) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
767 |
|
| 74163 | 768 |
lemma xor_self_eq [simp]: |
769 |
\<open>a XOR a = 0\<close> |
|
770 |
by (rule bit_eqI) (simp add: bit_simps) |
|
771 |
||
| 79588 | 772 |
lemma mask_0 [simp]: |
773 |
\<open>mask 0 = 0\<close> |
|
774 |
by (simp add: mask_eq_exp_minus_1) |
|
775 |
||
776 |
lemma inc_mask_eq_exp: |
|
777 |
\<open>mask n + 1 = 2 ^ n\<close> |
|
778 |
proof (induction n) |
|
779 |
case 0 |
|
780 |
then show ?case |
|
781 |
by simp |
|
782 |
next |
|
783 |
case (Suc n) |
|
784 |
from Suc.IH [symmetric] have \<open>2 ^ Suc n = 2 * mask n + 2\<close> |
|
785 |
by (simp add: algebra_simps) |
|
786 |
also have \<open>\<dots> = 2 * mask n + 1 + 1\<close> |
|
787 |
by (simp add: add.assoc) |
|
788 |
finally have *: \<open>2 ^ Suc n = 2 * mask n + 1 + 1\<close> . |
|
789 |
then show ?case |
|
790 |
by (simp add: mask_eq_exp_minus_1) |
|
791 |
qed |
|
792 |
||
793 |
lemma mask_Suc_double: |
|
794 |
\<open>mask (Suc n) = 1 OR 2 * mask n\<close> |
|
795 |
proof - |
|
796 |
have \<open>mask (Suc n) + 1 = (mask n + 1) + (mask n + 1)\<close> |
|
797 |
by (simp add: inc_mask_eq_exp mult_2) |
|
798 |
also have \<open>\<dots> = (1 OR 2 * mask n) + 1\<close> |
|
799 |
by (simp add: one_or_eq mult_2_right algebra_simps) |
|
800 |
finally show ?thesis |
|
801 |
by simp |
|
802 |
qed |
|
803 |
||
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
804 |
lemma bit_mask_iff [bit_simps]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
805 |
\<open>bit (mask m) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n < m\<close>
|
| 79588 | 806 |
proof (cases \<open>possible_bit TYPE('a) n\<close>)
|
807 |
case False |
|
808 |
then show ?thesis |
|
809 |
by (simp add: impossible_bit) |
|
810 |
next |
|
811 |
case True |
|
812 |
then have \<open>bit (mask m) n \<longleftrightarrow> n < m\<close> |
|
813 |
proof (induction m arbitrary: n) |
|
814 |
case 0 |
|
815 |
then show ?case |
|
816 |
by (simp add: bit_iff_odd) |
|
817 |
next |
|
818 |
case (Suc m) |
|
819 |
show ?case |
|
820 |
proof (cases n) |
|
821 |
case 0 |
|
822 |
then show ?thesis |
|
823 |
by (simp add: bit_0 mask_Suc_double even_or_iff) |
|
824 |
next |
|
825 |
case (Suc n) |
|
826 |
with Suc.prems have \<open>possible_bit TYPE('a) n\<close>
|
|
827 |
using possible_bit_less_imp by auto |
|
828 |
with Suc.IH [of n] have \<open>bit (mask m) n \<longleftrightarrow> n < m\<close> . |
|
829 |
with Suc.prems show ?thesis |
|
830 |
by (simp add: Suc mask_Suc_double bit_simps) |
|
831 |
qed |
|
832 |
qed |
|
833 |
with True show ?thesis |
|
834 |
by simp |
|
835 |
qed |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
836 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
837 |
lemma even_mask_iff: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
838 |
\<open>even (mask n) \<longleftrightarrow> n = 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
839 |
using bit_mask_iff [of n 0] by (auto simp add: bit_0) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
840 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
841 |
lemma mask_Suc_0 [simp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
842 |
\<open>mask (Suc 0) = 1\<close> |
| 79588 | 843 |
by (simp add: mask_Suc_double) |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
844 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
845 |
lemma mask_Suc_exp: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
846 |
\<open>mask (Suc n) = 2 ^ n OR mask n\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
847 |
by (auto simp add: bit_eq_iff bit_simps) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
848 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
849 |
lemma mask_numeral: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
850 |
\<open>mask (numeral n) = 1 + 2 * mask (pred_numeral n)\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
851 |
by (simp add: numeral_eq_Suc mask_Suc_double one_or_eq ac_simps) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
852 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
853 |
lemma push_bit_0_id [simp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
854 |
\<open>push_bit 0 = id\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
855 |
by (simp add: fun_eq_iff push_bit_eq_mult) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
856 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
857 |
lemma push_bit_Suc [simp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
858 |
\<open>push_bit (Suc n) a = push_bit n (a * 2)\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
859 |
by (simp add: push_bit_eq_mult ac_simps) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
860 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
861 |
lemma push_bit_double: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
862 |
\<open>push_bit n (a * 2) = push_bit n a * 2\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
863 |
by (simp add: push_bit_eq_mult ac_simps) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
864 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
865 |
lemma bit_push_bit_iff [bit_simps]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
866 |
\<open>bit (push_bit m a) n \<longleftrightarrow> m \<le> n \<and> possible_bit TYPE('a) n \<and> bit a (n - m)\<close>
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
867 |
proof (induction n arbitrary: m) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
868 |
case 0 |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
869 |
then show ?case |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
870 |
by (auto simp add: bit_0 push_bit_eq_mult) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
871 |
next |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
872 |
case (Suc n) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
873 |
show ?case |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
874 |
proof (cases m) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
875 |
case 0 |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
876 |
then show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
877 |
by (auto simp add: bit_imp_possible_bit) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
878 |
next |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
879 |
case (Suc m) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
880 |
with Suc.prems Suc.IH [of m] show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
881 |
apply (simp add: push_bit_double) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
882 |
apply (simp add: bit_simps mult.commute [of _ 2]) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
883 |
apply (auto simp add: possible_bit_less_imp) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
884 |
done |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
885 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
886 |
qed |
| 74101 | 887 |
|
| 79590 | 888 |
lemma funpow_double_eq_push_bit: |
889 |
\<open>times 2 ^^ n = push_bit n\<close> |
|
890 |
by (induction n) (simp_all add: fun_eq_iff push_bit_double ac_simps) |
|
891 |
||
| 74101 | 892 |
lemma div_push_bit_of_1_eq_drop_bit: |
893 |
\<open>a div push_bit n 1 = drop_bit n a\<close> |
|
894 |
by (simp add: push_bit_eq_mult drop_bit_eq_div) |
|
895 |
||
896 |
lemma bits_ident: |
|
| 79017 | 897 |
\<open>push_bit n (drop_bit n a) + take_bit n a = a\<close> |
| 74101 | 898 |
using div_mult_mod_eq by (simp add: push_bit_eq_mult take_bit_eq_mod drop_bit_eq_div) |
899 |
||
900 |
lemma push_bit_push_bit [simp]: |
|
| 79017 | 901 |
\<open>push_bit m (push_bit n a) = push_bit (m + n) a\<close> |
| 74101 | 902 |
by (simp add: push_bit_eq_mult power_add ac_simps) |
903 |
||
904 |
lemma push_bit_of_0 [simp]: |
|
| 79017 | 905 |
\<open>push_bit n 0 = 0\<close> |
| 74101 | 906 |
by (simp add: push_bit_eq_mult) |
907 |
||
| 74592 | 908 |
lemma push_bit_of_1 [simp]: |
| 79017 | 909 |
\<open>push_bit n 1 = 2 ^ n\<close> |
| 74101 | 910 |
by (simp add: push_bit_eq_mult) |
911 |
||
912 |
lemma push_bit_add: |
|
| 79017 | 913 |
\<open>push_bit n (a + b) = push_bit n a + push_bit n b\<close> |
| 74101 | 914 |
by (simp add: push_bit_eq_mult algebra_simps) |
915 |
||
916 |
lemma push_bit_numeral [simp]: |
|
917 |
\<open>push_bit (numeral l) (numeral k) = push_bit (pred_numeral l) (numeral (Num.Bit0 k))\<close> |
|
918 |
by (simp add: numeral_eq_Suc mult_2_right) (simp add: numeral_Bit0) |
|
919 |
||
920 |
lemma take_bit_0 [simp]: |
|
921 |
"take_bit 0 a = 0" |
|
922 |
by (simp add: take_bit_eq_mod) |
|
923 |
||
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
924 |
lemma bit_take_bit_iff [bit_simps]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
925 |
\<open>bit (take_bit m a) n \<longleftrightarrow> n < m \<and> bit a n\<close> |
| 79585 | 926 |
by (simp add: take_bit_eq_mod bit_iff_odd even_mod_exp_div_exp_iff not_le) |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
927 |
|
| 74101 | 928 |
lemma take_bit_Suc: |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
929 |
\<open>take_bit (Suc n) a = take_bit n (a div 2) * 2 + a mod 2\<close> (is \<open>?lhs = ?rhs\<close>) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
930 |
proof (rule bit_eqI) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
931 |
fix m |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
932 |
assume \<open>possible_bit TYPE('a) m\<close>
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
933 |
then show \<open>bit ?lhs m \<longleftrightarrow> bit ?rhs m\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
934 |
apply (cases a rule: parity_cases; cases m) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
935 |
apply (simp_all add: bit_simps even_bit_succ_iff mult.commute [of _ 2] add.commute [of _ 1] flip: bit_Suc) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
936 |
apply (simp_all add: bit_0) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
937 |
done |
| 74101 | 938 |
qed |
939 |
||
940 |
lemma take_bit_rec: |
|
941 |
\<open>take_bit n a = (if n = 0 then 0 else take_bit (n - 1) (a div 2) * 2 + a mod 2)\<close> |
|
942 |
by (cases n) (simp_all add: take_bit_Suc) |
|
943 |
||
944 |
lemma take_bit_Suc_0 [simp]: |
|
945 |
\<open>take_bit (Suc 0) a = a mod 2\<close> |
|
946 |
by (simp add: take_bit_eq_mod) |
|
947 |
||
948 |
lemma take_bit_of_0 [simp]: |
|
| 79017 | 949 |
\<open>take_bit n 0 = 0\<close> |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
950 |
by (rule bit_eqI) (simp add: bit_simps) |
| 74101 | 951 |
|
952 |
lemma take_bit_of_1 [simp]: |
|
| 79017 | 953 |
\<open>take_bit n 1 = of_bool (n > 0)\<close> |
| 74101 | 954 |
by (cases n) (simp_all add: take_bit_Suc) |
955 |
||
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
956 |
lemma bit_drop_bit_eq [bit_simps]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
957 |
\<open>bit (drop_bit n a) = bit a \<circ> (+) n\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
958 |
by rule (simp add: drop_bit_eq_div bit_iff_odd div_exp_eq) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
959 |
|
| 74101 | 960 |
lemma drop_bit_of_0 [simp]: |
| 79017 | 961 |
\<open>drop_bit n 0 = 0\<close> |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
962 |
by (rule bit_eqI) (simp add: bit_simps) |
| 74101 | 963 |
|
964 |
lemma drop_bit_of_1 [simp]: |
|
| 79017 | 965 |
\<open>drop_bit n 1 = of_bool (n = 0)\<close> |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
966 |
by (rule bit_eqI) (simp add: bit_simps ac_simps) |
| 74101 | 967 |
|
968 |
lemma drop_bit_0 [simp]: |
|
| 79017 | 969 |
\<open>drop_bit 0 = id\<close> |
| 74101 | 970 |
by (simp add: fun_eq_iff drop_bit_eq_div) |
971 |
||
972 |
lemma drop_bit_Suc: |
|
| 79017 | 973 |
\<open>drop_bit (Suc n) a = drop_bit n (a div 2)\<close> |
| 74101 | 974 |
using div_exp_eq [of a 1] by (simp add: drop_bit_eq_div) |
975 |
||
976 |
lemma drop_bit_rec: |
|
| 79017 | 977 |
\<open>drop_bit n a = (if n = 0 then a else drop_bit (n - 1) (a div 2))\<close> |
| 74101 | 978 |
by (cases n) (simp_all add: drop_bit_Suc) |
979 |
||
980 |
lemma drop_bit_half: |
|
| 79017 | 981 |
\<open>drop_bit n (a div 2) = drop_bit n a div 2\<close> |
| 74101 | 982 |
by (induction n arbitrary: a) (simp_all add: drop_bit_Suc) |
983 |
||
984 |
lemma drop_bit_of_bool [simp]: |
|
| 79017 | 985 |
\<open>drop_bit n (of_bool b) = of_bool (n = 0 \<and> b)\<close> |
| 74101 | 986 |
by (cases n) simp_all |
987 |
||
988 |
lemma even_take_bit_eq [simp]: |
|
989 |
\<open>even (take_bit n a) \<longleftrightarrow> n = 0 \<or> even a\<close> |
|
990 |
by (simp add: take_bit_rec [of n a]) |
|
991 |
||
992 |
lemma take_bit_take_bit [simp]: |
|
| 79017 | 993 |
\<open>take_bit m (take_bit n a) = take_bit (min m n) a\<close> |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
994 |
by (rule bit_eqI) (simp add: bit_simps) |
| 74101 | 995 |
|
996 |
lemma drop_bit_drop_bit [simp]: |
|
| 79017 | 997 |
\<open>drop_bit m (drop_bit n a) = drop_bit (m + n) a\<close> |
| 74101 | 998 |
by (simp add: drop_bit_eq_div power_add div_exp_eq ac_simps) |
999 |
||
1000 |
lemma push_bit_take_bit: |
|
| 79017 | 1001 |
\<open>push_bit m (take_bit n a) = take_bit (m + n) (push_bit m a)\<close> |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
1002 |
by (rule bit_eqI) (auto simp add: bit_simps) |
| 74101 | 1003 |
|
1004 |
lemma take_bit_push_bit: |
|
| 79017 | 1005 |
\<open>take_bit m (push_bit n a) = push_bit n (take_bit (m - n) a)\<close> |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
1006 |
by (rule bit_eqI) (auto simp add: bit_simps) |
| 74101 | 1007 |
|
1008 |
lemma take_bit_drop_bit: |
|
| 79017 | 1009 |
\<open>take_bit m (drop_bit n a) = drop_bit n (take_bit (m + n) a)\<close> |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
1010 |
by (rule bit_eqI) (auto simp add: bit_simps) |
| 74101 | 1011 |
|
1012 |
lemma drop_bit_take_bit: |
|
| 79017 | 1013 |
\<open>drop_bit m (take_bit n a) = take_bit (n - m) (drop_bit m a)\<close> |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
1014 |
by (rule bit_eqI) (auto simp add: bit_simps) |
| 74101 | 1015 |
|
1016 |
lemma even_push_bit_iff [simp]: |
|
1017 |
\<open>even (push_bit n a) \<longleftrightarrow> n \<noteq> 0 \<or> even a\<close> |
|
1018 |
by (simp add: push_bit_eq_mult) auto |
|
1019 |
||
1020 |
lemma stable_imp_drop_bit_eq: |
|
1021 |
\<open>drop_bit n a = a\<close> |
|
1022 |
if \<open>a div 2 = a\<close> |
|
1023 |
by (induction n) (simp_all add: that drop_bit_Suc) |
|
1024 |
||
1025 |
lemma stable_imp_take_bit_eq: |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
1026 |
\<open>take_bit n a = (if even a then 0 else mask n)\<close> |
| 74101 | 1027 |
if \<open>a div 2 = a\<close> |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
1028 |
by (rule bit_eqI) (use that in \<open>simp add: bit_simps stable_imp_bit_iff_odd\<close>) |
| 74101 | 1029 |
|
1030 |
lemma exp_dvdE: |
|
1031 |
assumes \<open>2 ^ n dvd a\<close> |
|
1032 |
obtains b where \<open>a = push_bit n b\<close> |
|
1033 |
proof - |
|
1034 |
from assms obtain b where \<open>a = 2 ^ n * b\<close> .. |
|
1035 |
then have \<open>a = push_bit n b\<close> |
|
1036 |
by (simp add: push_bit_eq_mult ac_simps) |
|
1037 |
with that show thesis . |
|
1038 |
qed |
|
1039 |
||
1040 |
lemma take_bit_eq_0_iff: |
|
1041 |
\<open>take_bit n a = 0 \<longleftrightarrow> 2 ^ n dvd a\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
1042 |
proof |
|
1043 |
assume ?P |
|
1044 |
then show ?Q |
|
1045 |
by (simp add: take_bit_eq_mod mod_0_imp_dvd) |
|
1046 |
next |
|
1047 |
assume ?Q |
|
1048 |
then obtain b where \<open>a = push_bit n b\<close> |
|
1049 |
by (rule exp_dvdE) |
|
1050 |
then show ?P |
|
1051 |
by (simp add: take_bit_push_bit) |
|
1052 |
qed |
|
1053 |
||
1054 |
lemma take_bit_tightened: |
|
| 79068 | 1055 |
\<open>take_bit m a = take_bit m b\<close> if \<open>take_bit n a = take_bit n b\<close> and \<open>m \<le> n\<close> |
| 74101 | 1056 |
proof - |
1057 |
from that have \<open>take_bit m (take_bit n a) = take_bit m (take_bit n b)\<close> |
|
1058 |
by simp |
|
1059 |
then have \<open>take_bit (min m n) a = take_bit (min m n) b\<close> |
|
1060 |
by simp |
|
1061 |
with that show ?thesis |
|
1062 |
by (simp add: min_def) |
|
1063 |
qed |
|
1064 |
||
1065 |
lemma take_bit_eq_self_iff_drop_bit_eq_0: |
|
1066 |
\<open>take_bit n a = a \<longleftrightarrow> drop_bit n a = 0\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
1067 |
proof |
|
1068 |
assume ?P |
|
1069 |
show ?Q |
|
1070 |
proof (rule bit_eqI) |
|
1071 |
fix m |
|
1072 |
from \<open>?P\<close> have \<open>a = take_bit n a\<close> .. |
|
1073 |
also have \<open>\<not> bit (take_bit n a) (n + m)\<close> |
|
1074 |
unfolding bit_simps |
|
| 79068 | 1075 |
by (simp add: bit_simps) |
| 74101 | 1076 |
finally show \<open>bit (drop_bit n a) m \<longleftrightarrow> bit 0 m\<close> |
1077 |
by (simp add: bit_simps) |
|
1078 |
qed |
|
1079 |
next |
|
1080 |
assume ?Q |
|
1081 |
show ?P |
|
1082 |
proof (rule bit_eqI) |
|
1083 |
fix m |
|
1084 |
from \<open>?Q\<close> have \<open>\<not> bit (drop_bit n a) (m - n)\<close> |
|
1085 |
by simp |
|
1086 |
then have \<open> \<not> bit a (n + (m - n))\<close> |
|
1087 |
by (simp add: bit_simps) |
|
1088 |
then show \<open>bit (take_bit n a) m \<longleftrightarrow> bit a m\<close> |
|
1089 |
by (cases \<open>m < n\<close>) (auto simp add: bit_simps) |
|
1090 |
qed |
|
1091 |
qed |
|
1092 |
||
1093 |
lemma drop_bit_exp_eq: |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1094 |
\<open>drop_bit m (2 ^ n) = of_bool (m \<le> n \<and> possible_bit TYPE('a) n) * 2 ^ (n - m)\<close>
|
|
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1095 |
by (auto simp add: bit_eq_iff bit_simps) |
| 74101 | 1096 |
|
| 71409 | 1097 |
lemma take_bit_and [simp]: |
1098 |
\<open>take_bit n (a AND b) = take_bit n a AND take_bit n b\<close> |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1099 |
by (auto simp add: bit_eq_iff bit_simps) |
| 71409 | 1100 |
|
1101 |
lemma take_bit_or [simp]: |
|
1102 |
\<open>take_bit n (a OR b) = take_bit n a OR take_bit n b\<close> |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1103 |
by (auto simp add: bit_eq_iff bit_simps) |
| 71409 | 1104 |
|
1105 |
lemma take_bit_xor [simp]: |
|
1106 |
\<open>take_bit n (a XOR b) = take_bit n a XOR take_bit n b\<close> |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1107 |
by (auto simp add: bit_eq_iff bit_simps) |
| 71409 | 1108 |
|
| 72239 | 1109 |
lemma push_bit_and [simp]: |
1110 |
\<open>push_bit n (a AND b) = push_bit n a AND push_bit n b\<close> |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1111 |
by (auto simp add: bit_eq_iff bit_simps) |
| 72239 | 1112 |
|
1113 |
lemma push_bit_or [simp]: |
|
1114 |
\<open>push_bit n (a OR b) = push_bit n a OR push_bit n b\<close> |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1115 |
by (auto simp add: bit_eq_iff bit_simps) |
| 72239 | 1116 |
|
1117 |
lemma push_bit_xor [simp]: |
|
1118 |
\<open>push_bit n (a XOR b) = push_bit n a XOR push_bit n b\<close> |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1119 |
by (auto simp add: bit_eq_iff bit_simps) |
| 72239 | 1120 |
|
1121 |
lemma drop_bit_and [simp]: |
|
1122 |
\<open>drop_bit n (a AND b) = drop_bit n a AND drop_bit n b\<close> |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1123 |
by (auto simp add: bit_eq_iff bit_simps) |
| 72239 | 1124 |
|
1125 |
lemma drop_bit_or [simp]: |
|
1126 |
\<open>drop_bit n (a OR b) = drop_bit n a OR drop_bit n b\<close> |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1127 |
by (auto simp add: bit_eq_iff bit_simps) |
| 72239 | 1128 |
|
1129 |
lemma drop_bit_xor [simp]: |
|
1130 |
\<open>drop_bit n (a XOR b) = drop_bit n a XOR drop_bit n b\<close> |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1131 |
by (auto simp add: bit_eq_iff bit_simps) |
| 72239 | 1132 |
|
| 74592 | 1133 |
lemma take_bit_of_mask [simp]: |
| 72830 | 1134 |
\<open>take_bit m (mask n) = mask (min m n)\<close> |
1135 |
by (rule bit_eqI) (simp add: bit_simps) |
|
1136 |
||
|
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71956
diff
changeset
|
1137 |
lemma take_bit_eq_mask: |
| 71823 | 1138 |
\<open>take_bit n a = a AND mask n\<close> |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1139 |
by (auto simp add: bit_eq_iff bit_simps) |
| 71823 | 1140 |
|
|
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1141 |
lemma or_eq_0_iff: |
|
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1142 |
\<open>a OR b = 0 \<longleftrightarrow> a = 0 \<and> b = 0\<close> |
| 72792 | 1143 |
by (auto simp add: bit_eq_iff bit_or_iff) |
|
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1144 |
|
| 72508 | 1145 |
lemma bit_iff_and_drop_bit_eq_1: |
1146 |
\<open>bit a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close> |
|
1147 |
by (simp add: bit_iff_odd_drop_bit and_one_eq odd_iff_mod_2_eq_one) |
|
1148 |
||
1149 |
lemma bit_iff_and_push_bit_not_eq_0: |
|
1150 |
\<open>bit a n \<longleftrightarrow> a AND push_bit n 1 \<noteq> 0\<close> |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
1151 |
by (cases \<open>possible_bit TYPE('a) n\<close>) (simp_all add: bit_eq_iff bit_simps impossible_bit)
|
| 72508 | 1152 |
|
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1153 |
lemma bit_set_bit_iff [bit_simps]: |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1154 |
\<open>bit (set_bit m a) n \<longleftrightarrow> bit a n \<or> (m = n \<and> possible_bit TYPE('a) n)\<close>
|
| 79017 | 1155 |
by (auto simp add: set_bit_eq_or bit_or_iff bit_exp_iff) |
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1156 |
|
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1157 |
lemma even_set_bit_iff: |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1158 |
\<open>even (set_bit m a) \<longleftrightarrow> even a \<and> m \<noteq> 0\<close> |
| 75085 | 1159 |
using bit_set_bit_iff [of m a 0] by (auto simp add: bit_0) |
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1160 |
|
|
79031
4596a14d9a95
slightly more elementary characterization of unset_bit
haftmann
parents:
79030
diff
changeset
|
1161 |
lemma bit_unset_bit_iff [bit_simps]: |
|
4596a14d9a95
slightly more elementary characterization of unset_bit
haftmann
parents:
79030
diff
changeset
|
1162 |
\<open>bit (unset_bit m a) n \<longleftrightarrow> bit a n \<and> m \<noteq> n\<close> |
| 79489 | 1163 |
by (auto simp add: unset_bit_eq_or_xor bit_simps dest: bit_imp_possible_bit) |
|
79031
4596a14d9a95
slightly more elementary characterization of unset_bit
haftmann
parents:
79030
diff
changeset
|
1164 |
|
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1165 |
lemma even_unset_bit_iff: |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1166 |
\<open>even (unset_bit m a) \<longleftrightarrow> even a \<or> m = 0\<close> |
| 75085 | 1167 |
using bit_unset_bit_iff [of m a 0] by (auto simp add: bit_0) |
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1168 |
|
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1169 |
lemma bit_flip_bit_iff [bit_simps]: |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1170 |
\<open>bit (flip_bit m a) n \<longleftrightarrow> (m = n \<longleftrightarrow> \<not> bit a n) \<and> possible_bit TYPE('a) n\<close>
|
|
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1171 |
by (auto simp add: bit_eq_iff bit_simps flip_bit_eq_xor bit_imp_possible_bit) |
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1172 |
|
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1173 |
lemma even_flip_bit_iff: |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1174 |
\<open>even (flip_bit m a) \<longleftrightarrow> \<not> (even a \<longleftrightarrow> m = 0)\<close> |
| 75085 | 1175 |
using bit_flip_bit_iff [of m a 0] by (auto simp: possible_bit_def bit_0) |
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1176 |
|
| 79489 | 1177 |
lemma and_exp_eq_0_iff_not_bit: |
1178 |
\<open>a AND 2 ^ n = 0 \<longleftrightarrow> \<not> bit a n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
1179 |
using bit_imp_possible_bit[of a n] |
|
1180 |
by (auto simp add: bit_eq_iff bit_simps) |
|
1181 |
||
1182 |
lemma bit_sum_mult_2_cases: |
|
1183 |
assumes a: \<open>\<forall>j. \<not> bit a (Suc j)\<close> |
|
1184 |
shows \<open>bit (a + 2 * b) n = (if n = 0 then odd a else bit (2 * b) n)\<close> |
|
1185 |
proof - |
|
1186 |
from a have \<open>n = 0\<close> if \<open>bit a n\<close> for n using that |
|
1187 |
by (cases n) simp_all |
|
1188 |
then have \<open>a = 0 \<or> a = 1\<close> |
|
1189 |
by (auto simp add: bit_eq_iff bit_1_iff) |
|
1190 |
then show ?thesis |
|
1191 |
by (cases n) (auto simp add: bit_0 bit_double_iff even_bit_succ_iff) |
|
1192 |
qed |
|
1193 |
||
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1194 |
lemma set_bit_0 [simp]: |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1195 |
\<open>set_bit 0 a = 1 + 2 * (a div 2)\<close> |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1196 |
by (auto simp add: bit_eq_iff bit_simps even_bit_succ_iff simp flip: bit_Suc) |
|
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1197 |
|
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1198 |
lemma set_bit_Suc: |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1199 |
\<open>set_bit (Suc n) a = a mod 2 + 2 * set_bit n (a div 2)\<close> |
| 75085 | 1200 |
by (auto simp add: bit_eq_iff bit_sum_mult_2_cases bit_simps bit_0 simp flip: bit_Suc |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1201 |
elim: possible_bit_less_imp) |
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1202 |
|
| 79489 | 1203 |
lemma unset_bit_0 [simp]: |
1204 |
\<open>unset_bit 0 a = 2 * (a div 2)\<close> |
|
1205 |
by (auto simp add: bit_eq_iff bit_simps simp flip: bit_Suc) |
|
1206 |
||
1207 |
lemma unset_bit_Suc: |
|
1208 |
\<open>unset_bit (Suc n) a = a mod 2 + 2 * unset_bit n (a div 2)\<close> |
|
1209 |
by (auto simp add: bit_eq_iff bit_sum_mult_2_cases bit_simps bit_0 simp flip: bit_Suc) |
|
1210 |
||
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1211 |
lemma flip_bit_0 [simp]: |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1212 |
\<open>flip_bit 0 a = of_bool (even a) + 2 * (a div 2)\<close> |
| 75085 | 1213 |
by (auto simp add: bit_eq_iff bit_simps even_bit_succ_iff bit_0 simp flip: bit_Suc) |
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1214 |
|
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1215 |
lemma flip_bit_Suc: |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1216 |
\<open>flip_bit (Suc n) a = a mod 2 + 2 * flip_bit n (a div 2)\<close> |
| 75085 | 1217 |
by (auto simp add: bit_eq_iff bit_sum_mult_2_cases bit_simps bit_0 simp flip: bit_Suc |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1218 |
elim: possible_bit_less_imp) |
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1219 |
|
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1220 |
lemma flip_bit_eq_if: |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1221 |
\<open>flip_bit n a = (if bit a n then unset_bit else set_bit) n a\<close> |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1222 |
by (rule bit_eqI) (auto simp add: bit_set_bit_iff bit_unset_bit_iff bit_flip_bit_iff) |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1223 |
|
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1224 |
lemma take_bit_set_bit_eq: |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1225 |
\<open>take_bit n (set_bit m a) = (if n \<le> m then take_bit n a else set_bit m (take_bit n a))\<close> |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1226 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_set_bit_iff) |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1227 |
|
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1228 |
lemma take_bit_unset_bit_eq: |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1229 |
\<open>take_bit n (unset_bit m a) = (if n \<le> m then take_bit n a else unset_bit m (take_bit n a))\<close> |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1230 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_unset_bit_iff) |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1231 |
|
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1232 |
lemma take_bit_flip_bit_eq: |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1233 |
\<open>take_bit n (flip_bit m a) = (if n \<le> m then take_bit n a else flip_bit m (take_bit n a))\<close> |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1234 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff bit_flip_bit_iff) |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1235 |
|
| 75085 | 1236 |
lemma bit_1_0 [simp]: |
1237 |
\<open>bit 1 0\<close> |
|
1238 |
by (simp add: bit_0) |
|
1239 |
||
| 74497 | 1240 |
lemma not_bit_1_Suc [simp]: |
1241 |
\<open>\<not> bit 1 (Suc n)\<close> |
|
1242 |
by (simp add: bit_Suc) |
|
1243 |
||
1244 |
lemma push_bit_Suc_numeral [simp]: |
|
1245 |
\<open>push_bit (Suc n) (numeral k) = push_bit n (numeral (Num.Bit0 k))\<close> |
|
1246 |
by (simp add: numeral_eq_Suc mult_2_right) (simp add: numeral_Bit0) |
|
1247 |
||
| 74592 | 1248 |
lemma mask_eq_0_iff [simp]: |
1249 |
\<open>mask n = 0 \<longleftrightarrow> n = 0\<close> |
|
1250 |
by (cases n) (simp_all add: mask_Suc_double or_eq_0_iff) |
|
1251 |
||
| 79017 | 1252 |
lemma bit_horner_sum_bit_iff [bit_simps]: |
1253 |
\<open>bit (horner_sum of_bool 2 bs) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n < length bs \<and> bs ! n\<close>
|
|
1254 |
proof (induction bs arbitrary: n) |
|
1255 |
case Nil |
|
1256 |
then show ?case |
|
1257 |
by simp |
|
1258 |
next |
|
1259 |
case (Cons b bs) |
|
1260 |
show ?case |
|
1261 |
proof (cases n) |
|
1262 |
case 0 |
|
1263 |
then show ?thesis |
|
1264 |
by (simp add: bit_0) |
|
1265 |
next |
|
1266 |
case (Suc m) |
|
1267 |
with bit_rec [of _ n] Cons.prems Cons.IH [of m] |
|
1268 |
show ?thesis |
|
1269 |
by (simp add: bit_simps) |
|
1270 |
(auto simp add: possible_bit_less_imp bit_simps simp flip: bit_Suc) |
|
1271 |
qed |
|
1272 |
qed |
|
1273 |
||
1274 |
lemma horner_sum_bit_eq_take_bit: |
|
1275 |
\<open>horner_sum of_bool 2 (map (bit a) [0..<n]) = take_bit n a\<close> |
|
1276 |
by (rule bit_eqI) (auto simp add: bit_simps) |
|
1277 |
||
1278 |
lemma take_bit_horner_sum_bit_eq: |
|
1279 |
\<open>take_bit n (horner_sum of_bool 2 bs) = horner_sum of_bool 2 (take n bs)\<close> |
|
1280 |
by (auto simp add: bit_eq_iff bit_take_bit_iff bit_horner_sum_bit_iff) |
|
1281 |
||
1282 |
lemma take_bit_sum: |
|
1283 |
\<open>take_bit n a = (\<Sum>k = 0..<n. push_bit k (of_bool (bit a k)))\<close> |
|
1284 |
by (simp flip: horner_sum_bit_eq_take_bit add: horner_sum_eq_sum push_bit_eq_mult) |
|
1285 |
||
| 79071 | 1286 |
lemma set_bit_eq: |
1287 |
\<open>set_bit n a = a + of_bool (\<not> bit a n) * 2 ^ n\<close> |
|
1288 |
proof - |
|
1289 |
have \<open>set_bit n a = a OR of_bool (\<not> bit a n) * 2 ^ n\<close> |
|
1290 |
by (rule bit_eqI) (auto simp add: bit_simps) |
|
1291 |
then show ?thesis |
|
1292 |
by (subst disjunctive_add) (auto simp add: bit_simps) |
|
1293 |
qed |
|
1294 |
||
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1295 |
end |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1296 |
|
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1297 |
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
|
1298 |
fixes not :: \<open>'a \<Rightarrow> 'a\<close> (\<open>NOT\<close>) |
|
79072
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1299 |
assumes not_eq_complement: \<open>NOT a = - 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
|
1300 |
begin |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1301 |
|
| 71409 | 1302 |
text \<open> |
1303 |
For the sake of code generation \<^const>\<open>not\<close> is specified as |
|
1304 |
definitional class operation. Note that \<^const>\<open>not\<close> has no |
|
1305 |
sensible definition for unlimited but only positive bit strings |
|
1306 |
(type \<^typ>\<open>nat\<close>). |
|
1307 |
\<close> |
|
1308 |
||
| 71186 | 1309 |
lemma bits_minus_1_mod_2_eq [simp]: |
1310 |
\<open>(- 1) mod 2 = 1\<close> |
|
1311 |
by (simp add: mod_2_eq_odd) |
|
1312 |
||
| 71409 | 1313 |
lemma minus_eq_not_plus_1: |
1314 |
\<open>- a = NOT a + 1\<close> |
|
1315 |
using not_eq_complement [of a] by simp |
|
1316 |
||
|
79072
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1317 |
lemma minus_eq_not_minus_1: |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1318 |
\<open>- a = NOT (a - 1)\<close> |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1319 |
using not_eq_complement [of \<open>a - 1\<close>] by simp (simp add: algebra_simps) |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1320 |
|
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1321 |
lemma not_rec: |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1322 |
\<open>NOT a = of_bool (even a) + 2 * NOT (a div 2)\<close> |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1323 |
by (simp add: not_eq_complement algebra_simps mod_2_eq_odd flip: minus_mod_eq_mult_div) |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1324 |
|
| 79590 | 1325 |
lemma decr_eq_not_minus: |
1326 |
\<open>a - 1 = NOT (- a)\<close> |
|
1327 |
using not_eq_complement [of \<open>- a\<close>] by simp |
|
1328 |
||
| 71418 | 1329 |
lemma even_not_iff [simp]: |
|
79018
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1330 |
\<open>even (NOT a) \<longleftrightarrow> odd a\<close> |
|
79072
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1331 |
by (simp add: not_eq_complement) |
|
79018
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1332 |
|
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1333 |
lemma bit_not_iff [bit_simps]: |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1334 |
\<open>bit (NOT a) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> \<not> bit a n\<close>
|
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1335 |
proof (cases \<open>possible_bit TYPE('a) n\<close>)
|
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1336 |
case False |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1337 |
then show ?thesis |
| 79068 | 1338 |
by (auto dest: bit_imp_possible_bit) |
|
79018
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1339 |
next |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1340 |
case True |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1341 |
moreover have \<open>bit (NOT a) n \<longleftrightarrow> \<not> bit a n\<close> |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1342 |
using \<open>possible_bit TYPE('a) n\<close> proof (induction n arbitrary: a)
|
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1343 |
case 0 |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1344 |
then show ?case |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1345 |
by (simp add: bit_0) |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1346 |
next |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1347 |
case (Suc n) |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1348 |
from Suc.prems Suc.IH [of \<open>a div 2\<close>] |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1349 |
show ?case |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1350 |
by (simp add: impossible_bit possible_bit_less_imp not_rec [of a] even_bit_succ_iff bit_double_iff flip: bit_Suc) |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1351 |
qed |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1352 |
ultimately show ?thesis |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1353 |
by simp |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1354 |
qed |
| 71418 | 1355 |
|
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
1356 |
lemma bit_not_exp_iff [bit_simps]: |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1357 |
\<open>bit (NOT (2 ^ m)) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n \<noteq> m\<close>
|
| 71409 | 1358 |
by (auto simp add: bit_not_iff bit_exp_iff) |
1359 |
||
|
79018
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1360 |
lemma bit_minus_iff [bit_simps]: |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1361 |
\<open>bit (- a) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> \<not> bit (a - 1) n\<close>
|
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1362 |
by (simp add: minus_eq_not_minus_1 bit_not_iff) |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1363 |
|
| 71186 | 1364 |
lemma bit_minus_1_iff [simp]: |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1365 |
\<open>bit (- 1) n \<longleftrightarrow> possible_bit TYPE('a) n\<close>
|
| 71409 | 1366 |
by (simp add: bit_minus_iff) |
1367 |
||
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
1368 |
lemma bit_minus_exp_iff [bit_simps]: |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1369 |
\<open>bit (- (2 ^ m)) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n \<ge> m\<close>
|
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
1370 |
by (auto simp add: bit_simps simp flip: mask_eq_exp_minus_1) |
| 71409 | 1371 |
|
1372 |
lemma bit_minus_2_iff [simp]: |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1373 |
\<open>bit (- 2) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> n > 0\<close>
|
| 71409 | 1374 |
by (simp add: bit_minus_iff bit_1_iff) |
| 71186 | 1375 |
|
| 79590 | 1376 |
lemma bit_decr_iff: |
1377 |
\<open>bit (a - 1) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> \<not> bit (- a) n\<close>
|
|
1378 |
by (simp add: decr_eq_not_minus bit_not_iff) |
|
1379 |
||
|
79018
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1380 |
lemma bit_not_iff_eq: |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1381 |
\<open>bit (NOT a) n \<longleftrightarrow> 2 ^ n \<noteq> 0 \<and> \<not> bit a n\<close> |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1382 |
by (simp add: bit_simps possible_bit_def) |
|
7449ff77029e
base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents:
79017
diff
changeset
|
1383 |
|
| 74495 | 1384 |
lemma not_one_eq [simp]: |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1385 |
\<open>NOT 1 = - 2\<close> |
| 71418 | 1386 |
by (simp add: bit_eq_iff bit_not_iff) (simp add: bit_1_iff) |
1387 |
||
1388 |
sublocale "and": semilattice_neutr \<open>(AND)\<close> \<open>- 1\<close> |
|
| 72239 | 1389 |
by standard (rule bit_eqI, simp add: bit_and_iff) |
| 71418 | 1390 |
|
|
74123
7c5842b06114
clarified abstract and concrete boolean algebras
haftmann
parents:
74108
diff
changeset
|
1391 |
sublocale bit: abstract_boolean_algebra \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close> |
|
7c5842b06114
clarified abstract and concrete boolean algebras
haftmann
parents:
74108
diff
changeset
|
1392 |
by standard (auto simp add: bit_and_iff bit_or_iff bit_not_iff intro: bit_eqI) |
|
7c5842b06114
clarified abstract and concrete boolean algebras
haftmann
parents:
74108
diff
changeset
|
1393 |
|
|
7c5842b06114
clarified abstract and concrete boolean algebras
haftmann
parents:
74108
diff
changeset
|
1394 |
sublocale bit: abstract_boolean_algebra_sym_diff \<open>(AND)\<close> \<open>(OR)\<close> NOT 0 \<open>- 1\<close> \<open>(XOR)\<close> |
|
7c5842b06114
clarified abstract and concrete boolean algebras
haftmann
parents:
74108
diff
changeset
|
1395 |
apply standard |
|
7c5842b06114
clarified abstract and concrete boolean algebras
haftmann
parents:
74108
diff
changeset
|
1396 |
apply (rule bit_eqI) |
|
7c5842b06114
clarified abstract and concrete boolean algebras
haftmann
parents:
74108
diff
changeset
|
1397 |
apply (auto simp add: bit_simps) |
|
7c5842b06114
clarified abstract and concrete boolean algebras
haftmann
parents:
74108
diff
changeset
|
1398 |
done |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1399 |
|
| 71802 | 1400 |
lemma and_eq_not_not_or: |
1401 |
\<open>a AND b = NOT (NOT a OR NOT b)\<close> |
|
1402 |
by simp |
|
1403 |
||
1404 |
lemma or_eq_not_not_and: |
|
1405 |
\<open>a OR b = NOT (NOT a AND NOT b)\<close> |
|
1406 |
by simp |
|
1407 |
||
| 72009 | 1408 |
lemma not_add_distrib: |
1409 |
\<open>NOT (a + b) = NOT a - b\<close> |
|
1410 |
by (simp add: not_eq_complement algebra_simps) |
|
1411 |
||
1412 |
lemma not_diff_distrib: |
|
1413 |
\<open>NOT (a - b) = NOT a + b\<close> |
|
1414 |
using not_add_distrib [of a \<open>- b\<close>] by simp |
|
1415 |
||
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1416 |
lemma and_eq_minus_1_iff: |
|
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1417 |
\<open>a AND b = - 1 \<longleftrightarrow> a = - 1 \<and> b = - 1\<close> |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1418 |
by (auto simp: bit_eq_iff bit_simps) |
|
72281
beeadb35e357
more thorough treatment of division, particularly signed division on int and word
haftmann
parents:
72262
diff
changeset
|
1419 |
|
| 72239 | 1420 |
lemma disjunctive_diff: |
1421 |
\<open>a - b = a AND NOT b\<close> if \<open>\<And>n. bit b n \<Longrightarrow> bit a n\<close> |
|
1422 |
proof - |
|
1423 |
have \<open>NOT a + b = NOT a OR b\<close> |
|
1424 |
by (rule disjunctive_add) (auto simp add: bit_not_iff dest: that) |
|
1425 |
then have \<open>NOT (NOT a + b) = NOT (NOT a OR b)\<close> |
|
1426 |
by simp |
|
1427 |
then show ?thesis |
|
1428 |
by (simp add: not_add_distrib) |
|
1429 |
qed |
|
1430 |
||
| 71412 | 1431 |
lemma push_bit_minus: |
1432 |
\<open>push_bit n (- a) = - push_bit n a\<close> |
|
1433 |
by (simp add: push_bit_eq_mult) |
|
1434 |
||
| 71409 | 1435 |
lemma take_bit_not_take_bit: |
1436 |
\<open>take_bit n (NOT (take_bit n a)) = take_bit n (NOT a)\<close> |
|
1437 |
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
|
1438 |
|
| 71418 | 1439 |
lemma take_bit_not_iff: |
|
73969
ca2a35c0fe6e
operations for symbolic computation of bit operations
haftmann
parents:
73871
diff
changeset
|
1440 |
\<open>take_bit n (NOT a) = take_bit n (NOT b) \<longleftrightarrow> take_bit n a = take_bit n b\<close> |
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
1441 |
by (auto simp add: bit_eq_iff bit_simps) |
| 71418 | 1442 |
|
| 72262 | 1443 |
lemma take_bit_not_eq_mask_diff: |
1444 |
\<open>take_bit n (NOT a) = mask n - take_bit n a\<close> |
|
1445 |
proof - |
|
1446 |
have \<open>take_bit n (NOT a) = take_bit n (NOT (take_bit n a))\<close> |
|
1447 |
by (simp add: take_bit_not_take_bit) |
|
1448 |
also have \<open>\<dots> = mask n AND NOT (take_bit n a)\<close> |
|
1449 |
by (simp add: take_bit_eq_mask ac_simps) |
|
1450 |
also have \<open>\<dots> = mask n - take_bit n a\<close> |
|
1451 |
by (subst disjunctive_diff) |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
1452 |
(auto simp add: bit_take_bit_iff bit_mask_iff bit_imp_possible_bit) |
| 72262 | 1453 |
finally show ?thesis |
1454 |
by simp |
|
1455 |
qed |
|
1456 |
||
| 72079 | 1457 |
lemma mask_eq_take_bit_minus_one: |
1458 |
\<open>mask n = take_bit n (- 1)\<close> |
|
1459 |
by (simp add: bit_eq_iff bit_mask_iff bit_take_bit_iff conj_commute) |
|
1460 |
||
| 74592 | 1461 |
lemma take_bit_minus_one_eq_mask [simp]: |
| 71922 | 1462 |
\<open>take_bit n (- 1) = mask n\<close> |
| 72079 | 1463 |
by (simp add: mask_eq_take_bit_minus_one) |
| 71922 | 1464 |
|
| 72010 | 1465 |
lemma minus_exp_eq_not_mask: |
1466 |
\<open>- (2 ^ n) = NOT (mask n)\<close> |
|
1467 |
by (rule bit_eqI) (simp add: bit_minus_iff bit_not_iff flip: mask_eq_exp_minus_1) |
|
1468 |
||
| 74592 | 1469 |
lemma push_bit_minus_one_eq_not_mask [simp]: |
| 71922 | 1470 |
\<open>push_bit n (- 1) = NOT (mask n)\<close> |
| 72010 | 1471 |
by (simp add: push_bit_eq_mult minus_exp_eq_not_mask) |
1472 |
||
1473 |
lemma take_bit_not_mask_eq_0: |
|
1474 |
\<open>take_bit m (NOT (mask n)) = 0\<close> if \<open>n \<ge> m\<close> |
|
1475 |
by (rule bit_eqI) (use that in \<open>simp add: bit_take_bit_iff bit_not_iff bit_mask_iff\<close>) |
|
| 71922 | 1476 |
|
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1477 |
lemma unset_bit_eq_and_not: |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1478 |
\<open>unset_bit n a = a AND NOT (push_bit n 1)\<close> |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1479 |
by (rule bit_eqI) (auto simp add: bit_simps) |
| 71426 | 1480 |
|
| 74497 | 1481 |
lemma push_bit_Suc_minus_numeral [simp]: |
1482 |
\<open>push_bit (Suc n) (- numeral k) = push_bit n (- numeral (Num.Bit0 k))\<close> |
|
1483 |
apply (simp only: numeral_Bit0) |
|
1484 |
apply simp |
|
1485 |
apply (simp only: numeral_mult mult_2_right numeral_add) |
|
1486 |
done |
|
1487 |
||
1488 |
lemma push_bit_minus_numeral [simp]: |
|
1489 |
\<open>push_bit (numeral l) (- numeral k) = push_bit (pred_numeral l) (- numeral (Num.Bit0 k))\<close> |
|
1490 |
by (simp only: numeral_eq_Suc push_bit_Suc_minus_numeral) |
|
1491 |
||
| 74592 | 1492 |
lemma take_bit_Suc_minus_1_eq: |
| 74498 | 1493 |
\<open>take_bit (Suc n) (- 1) = 2 ^ Suc n - 1\<close> |
| 74592 | 1494 |
by (simp add: mask_eq_exp_minus_1) |
1495 |
||
1496 |
lemma take_bit_numeral_minus_1_eq: |
|
| 74498 | 1497 |
\<open>take_bit (numeral k) (- 1) = 2 ^ numeral k - 1\<close> |
| 74592 | 1498 |
by (simp add: mask_eq_exp_minus_1) |
1499 |
||
1500 |
lemma push_bit_mask_eq: |
|
1501 |
\<open>push_bit m (mask n) = mask (n + m) AND NOT (mask m)\<close> |
|
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
1502 |
by (rule bit_eqI) (auto simp add: bit_simps not_less possible_bit_less_imp) |
| 74592 | 1503 |
|
1504 |
lemma slice_eq_mask: |
|
1505 |
\<open>push_bit n (take_bit m (drop_bit n a)) = a AND mask (m + n) AND NOT (mask n)\<close> |
|
1506 |
by (rule bit_eqI) (auto simp add: bit_simps) |
|
1507 |
||
1508 |
lemma push_bit_numeral_minus_1 [simp]: |
|
1509 |
\<open>push_bit (numeral n) (- 1) = - (2 ^ numeral n)\<close> |
|
1510 |
by (simp add: push_bit_eq_mult) |
|
| 74498 | 1511 |
|
| 79071 | 1512 |
lemma unset_bit_eq: |
1513 |
\<open>unset_bit n a = a - of_bool (bit a n) * 2 ^ n\<close> |
|
1514 |
proof - |
|
1515 |
have \<open>unset_bit n a = a AND NOT (of_bool (bit a n) * 2 ^ n)\<close> |
|
1516 |
by (rule bit_eqI) (auto simp add: bit_simps) |
|
1517 |
then show ?thesis |
|
1518 |
by (subst disjunctive_diff) (auto simp add: bit_simps simp flip: push_bit_eq_mult) |
|
1519 |
qed |
|
1520 |
||
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1521 |
end |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1522 |
|
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1523 |
|
| 79070 | 1524 |
subsection \<open>Common algebraic structure\<close> |
1525 |
||
1526 |
class linordered_euclidean_semiring_bit_operations = |
|
1527 |
linordered_euclidean_semiring + semiring_bit_operations |
|
1528 |
begin |
|
1529 |
||
1530 |
lemma possible_bit [simp]: |
|
1531 |
\<open>possible_bit TYPE('a) n\<close>
|
|
1532 |
by (simp add: possible_bit_def) |
|
1533 |
||
1534 |
lemma take_bit_of_exp [simp]: |
|
1535 |
\<open>take_bit m (2 ^ n) = of_bool (n < m) * 2 ^ n\<close> |
|
1536 |
by (simp add: take_bit_eq_mod exp_mod_exp) |
|
1537 |
||
1538 |
lemma take_bit_of_2 [simp]: |
|
1539 |
\<open>take_bit n 2 = of_bool (2 \<le> n) * 2\<close> |
|
1540 |
using take_bit_of_exp [of n 1] by simp |
|
1541 |
||
1542 |
lemma push_bit_eq_0_iff [simp]: |
|
1543 |
\<open>push_bit n a = 0 \<longleftrightarrow> a = 0\<close> |
|
1544 |
by (simp add: push_bit_eq_mult) |
|
1545 |
||
1546 |
lemma take_bit_add: |
|
1547 |
\<open>take_bit n (take_bit n a + take_bit n b) = take_bit n (a + b)\<close> |
|
1548 |
by (simp add: take_bit_eq_mod mod_simps) |
|
1549 |
||
1550 |
lemma take_bit_of_1_eq_0_iff [simp]: |
|
1551 |
\<open>take_bit n 1 = 0 \<longleftrightarrow> n = 0\<close> |
|
1552 |
by (simp add: take_bit_eq_mod) |
|
1553 |
||
1554 |
lemma drop_bit_Suc_bit0 [simp]: |
|
1555 |
\<open>drop_bit (Suc n) (numeral (Num.Bit0 k)) = drop_bit n (numeral k)\<close> |
|
1556 |
by (simp add: drop_bit_Suc numeral_Bit0_div_2) |
|
1557 |
||
1558 |
lemma drop_bit_Suc_bit1 [simp]: |
|
1559 |
\<open>drop_bit (Suc n) (numeral (Num.Bit1 k)) = drop_bit n (numeral k)\<close> |
|
1560 |
by (simp add: drop_bit_Suc numeral_Bit1_div_2) |
|
1561 |
||
1562 |
lemma drop_bit_numeral_bit0 [simp]: |
|
1563 |
\<open>drop_bit (numeral l) (numeral (Num.Bit0 k)) = drop_bit (pred_numeral l) (numeral k)\<close> |
|
1564 |
by (simp add: drop_bit_rec numeral_Bit0_div_2) |
|
1565 |
||
1566 |
lemma drop_bit_numeral_bit1 [simp]: |
|
1567 |
\<open>drop_bit (numeral l) (numeral (Num.Bit1 k)) = drop_bit (pred_numeral l) (numeral k)\<close> |
|
1568 |
by (simp add: drop_bit_rec numeral_Bit1_div_2) |
|
1569 |
||
1570 |
lemma take_bit_Suc_1 [simp]: |
|
1571 |
\<open>take_bit (Suc n) 1 = 1\<close> |
|
1572 |
by (simp add: take_bit_Suc) |
|
1573 |
||
1574 |
lemma take_bit_Suc_bit0: |
|
1575 |
\<open>take_bit (Suc n) (numeral (Num.Bit0 k)) = take_bit n (numeral k) * 2\<close> |
|
1576 |
by (simp add: take_bit_Suc numeral_Bit0_div_2) |
|
1577 |
||
1578 |
lemma take_bit_Suc_bit1: |
|
1579 |
\<open>take_bit (Suc n) (numeral (Num.Bit1 k)) = take_bit n (numeral k) * 2 + 1\<close> |
|
1580 |
by (simp add: take_bit_Suc numeral_Bit1_div_2 mod_2_eq_odd) |
|
1581 |
||
1582 |
lemma take_bit_numeral_1 [simp]: |
|
1583 |
\<open>take_bit (numeral l) 1 = 1\<close> |
|
1584 |
by (simp add: take_bit_rec [of \<open>numeral l\<close> 1]) |
|
1585 |
||
1586 |
lemma take_bit_numeral_bit0: |
|
1587 |
\<open>take_bit (numeral l) (numeral (Num.Bit0 k)) = take_bit (pred_numeral l) (numeral k) * 2\<close> |
|
1588 |
by (simp add: take_bit_rec numeral_Bit0_div_2) |
|
1589 |
||
1590 |
lemma take_bit_numeral_bit1: |
|
1591 |
\<open>take_bit (numeral l) (numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (numeral k) * 2 + 1\<close> |
|
1592 |
by (simp add: take_bit_rec numeral_Bit1_div_2 mod_2_eq_odd) |
|
1593 |
||
1594 |
lemma bit_of_nat_iff_bit [bit_simps]: |
|
1595 |
\<open>bit (of_nat m) n \<longleftrightarrow> bit m n\<close> |
|
1596 |
proof - |
|
1597 |
have \<open>even (m div 2 ^ n) \<longleftrightarrow> even (of_nat (m div 2 ^ n))\<close> |
|
1598 |
by simp |
|
1599 |
also have \<open>of_nat (m div 2 ^ n) = of_nat m div of_nat (2 ^ n)\<close> |
|
1600 |
by (simp add: of_nat_div) |
|
1601 |
finally show ?thesis |
|
1602 |
by (simp add: bit_iff_odd semiring_bits_class.bit_iff_odd) |
|
1603 |
qed |
|
1604 |
||
1605 |
lemma drop_bit_mask_eq: |
|
1606 |
\<open>drop_bit m (mask n) = mask (n - m)\<close> |
|
1607 |
by (rule bit_eqI) (auto simp add: bit_simps possible_bit_def) |
|
1608 |
||
| 79071 | 1609 |
lemma bit_push_bit_iff': |
1610 |
\<open>bit (push_bit m a) n \<longleftrightarrow> m \<le> n \<and> bit a (n - m)\<close> |
|
1611 |
by (simp add: bit_simps) |
|
1612 |
||
1613 |
lemma mask_half: |
|
1614 |
\<open>mask n div 2 = mask (n - 1)\<close> |
|
1615 |
by (cases n) (simp_all add: mask_Suc_double one_or_eq) |
|
1616 |
||
1617 |
lemma take_bit_Suc_from_most: |
|
1618 |
\<open>take_bit (Suc n) a = 2 ^ n * of_bool (bit a n) + take_bit n a\<close> |
|
1619 |
using mod_mult2_eq' [of a \<open>2 ^ n\<close> 2] |
|
1620 |
by (simp only: take_bit_eq_mod power_Suc2) |
|
1621 |
(simp_all add: bit_iff_odd odd_iff_mod_2_eq_one) |
|
1622 |
||
1623 |
lemma take_bit_nonnegative [simp]: |
|
1624 |
\<open>0 \<le> take_bit n a\<close> |
|
1625 |
using horner_sum_nonnegative by (simp flip: horner_sum_bit_eq_take_bit) |
|
1626 |
||
1627 |
lemma not_take_bit_negative [simp]: |
|
1628 |
\<open>\<not> take_bit n a < 0\<close> |
|
1629 |
by (simp add: not_less) |
|
1630 |
||
1631 |
lemma bit_imp_take_bit_positive: |
|
1632 |
\<open>0 < take_bit m a\<close> if \<open>n < m\<close> and \<open>bit a n\<close> |
|
1633 |
proof (rule ccontr) |
|
1634 |
assume \<open>\<not> 0 < take_bit m a\<close> |
|
1635 |
then have \<open>take_bit m a = 0\<close> |
|
1636 |
by (auto simp add: not_less intro: order_antisym) |
|
1637 |
then have \<open>bit (take_bit m a) n = bit 0 n\<close> |
|
1638 |
by simp |
|
1639 |
with that show False |
|
1640 |
by (simp add: bit_take_bit_iff) |
|
1641 |
qed |
|
1642 |
||
1643 |
lemma take_bit_mult: |
|
1644 |
\<open>take_bit n (take_bit n a * take_bit n b) = take_bit n (a * b)\<close> |
|
1645 |
by (simp add: take_bit_eq_mod mod_mult_eq) |
|
1646 |
||
1647 |
lemma drop_bit_push_bit: |
|
1648 |
\<open>drop_bit m (push_bit n a) = drop_bit (m - n) (push_bit (n - m) a)\<close> |
|
1649 |
by (cases \<open>m \<le> n\<close>) |
|
1650 |
(auto simp add: mult.left_commute [of _ \<open>2 ^ n\<close>] mult.commute [of _ \<open>2 ^ n\<close>] mult.assoc |
|
1651 |
mult.commute [of a] drop_bit_eq_div push_bit_eq_mult not_le power_add Orderings.not_le dest!: le_Suc_ex less_imp_Suc_add) |
|
1652 |
||
| 79070 | 1653 |
end |
1654 |
||
1655 |
||
| 71956 | 1656 |
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
|
1657 |
|
|
79030
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1658 |
locale fold2_bit_int = |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1659 |
fixes f :: \<open>bool \<Rightarrow> bool \<Rightarrow> bool\<close> |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1660 |
begin |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1661 |
|
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1662 |
context |
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1663 |
begin |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1664 |
|
|
79030
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1665 |
function F :: \<open>int \<Rightarrow> int \<Rightarrow> int\<close> |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1666 |
where \<open>F k l = (if k \<in> {0, - 1} \<and> l \<in> {0, - 1}
|
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1667 |
then - of_bool (f (odd k) (odd l)) |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1668 |
else of_bool (f (odd k) (odd l)) + 2 * (F (k div 2) (l div 2)))\<close> |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1669 |
by auto |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1670 |
|
|
79030
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1671 |
private termination proof (relation \<open>measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>))\<close>) |
| 79017 | 1672 |
have less_eq: \<open>\<bar>k div 2\<bar> \<le> \<bar>k\<bar>\<close> for k :: int |
1673 |
by (cases k) (simp_all add: divide_int_def nat_add_distrib) |
|
1674 |
then have less: \<open>\<bar>k div 2\<bar> < \<bar>k\<bar>\<close> if \<open>k \<notin> {0, - 1}\<close> for k :: int
|
|
1675 |
using that by (auto simp add: less_le [of k]) |
|
| 74101 | 1676 |
show \<open>wf (measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>)))\<close> |
1677 |
by simp |
|
1678 |
show \<open>((k div 2, l div 2), k, l) \<in> measure (\<lambda>(k, l). nat (\<bar>k\<bar> + \<bar>l\<bar>))\<close> |
|
1679 |
if \<open>\<not> (k \<in> {0, - 1} \<and> l \<in> {0, - 1})\<close> for k l
|
|
1680 |
proof - |
|
1681 |
from that have *: \<open>k \<notin> {0, - 1} \<or> l \<notin> {0, - 1}\<close>
|
|
1682 |
by simp |
|
1683 |
then have \<open>0 < \<bar>k\<bar> + \<bar>l\<bar>\<close> |
|
1684 |
by auto |
|
1685 |
moreover from * have \<open>\<bar>k div 2\<bar> + \<bar>l div 2\<bar> < \<bar>k\<bar> + \<bar>l\<bar>\<close> |
|
1686 |
proof |
|
1687 |
assume \<open>k \<notin> {0, - 1}\<close>
|
|
1688 |
then have \<open>\<bar>k div 2\<bar> < \<bar>k\<bar>\<close> |
|
1689 |
by (rule less) |
|
1690 |
with less_eq [of l] show ?thesis |
|
1691 |
by auto |
|
1692 |
next |
|
1693 |
assume \<open>l \<notin> {0, - 1}\<close>
|
|
1694 |
then have \<open>\<bar>l div 2\<bar> < \<bar>l\<bar>\<close> |
|
1695 |
by (rule less) |
|
1696 |
with less_eq [of k] show ?thesis |
|
1697 |
by auto |
|
1698 |
qed |
|
1699 |
ultimately show ?thesis |
|
|
79030
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1700 |
by (simp only: in_measure split_def fst_conv snd_conv nat_mono_iff) |
| 74101 | 1701 |
qed |
1702 |
qed |
|
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1703 |
|
|
79030
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1704 |
declare F.simps [simp del] |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1705 |
|
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1706 |
lemma rec: |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1707 |
\<open>F k l = of_bool (f (odd k) (odd l)) + 2 * (F (k div 2) (l div 2))\<close> |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1708 |
for k l :: int |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1709 |
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
|
1710 |
case True |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1711 |
then show ?thesis |
|
79030
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1712 |
by (auto simp add: F.simps [of 0] F.simps [of \<open>- 1\<close>]) |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1713 |
next |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1714 |
case False |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1715 |
then show ?thesis |
|
79030
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1716 |
by (auto simp add: ac_simps F.simps [of k l]) |
| 71802 | 1717 |
qed |
1718 |
||
|
79030
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1719 |
lemma bit_iff: |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1720 |
\<open>bit (F k l) n \<longleftrightarrow> f (bit k n) (bit l n)\<close> for k l :: int |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1721 |
proof (induction n arbitrary: k l) |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1722 |
case 0 |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1723 |
then show ?case |
|
79030
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1724 |
by (simp add: rec [of k l] bit_0) |
|
71804
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1725 |
next |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1726 |
case (Suc n) |
|
6fd70ed18199
simplified construction of binary bit operations
haftmann
parents:
71802
diff
changeset
|
1727 |
then show ?case |
|
79030
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1728 |
by (simp add: rec [of k l] bit_Suc) |
| 71802 | 1729 |
qed |
1730 |
||
|
79030
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1731 |
end |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1732 |
|
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1733 |
end |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1734 |
|
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1735 |
instantiation int :: ring_bit_operations |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1736 |
begin |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1737 |
|
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1738 |
definition not_int :: \<open>int \<Rightarrow> int\<close> |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1739 |
where \<open>not_int k = - k - 1\<close> |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1740 |
|
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1741 |
global_interpretation and_int: fold2_bit_int \<open>(\<and>)\<close> |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1742 |
defines and_int = and_int.F . |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1743 |
|
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1744 |
global_interpretation or_int: fold2_bit_int \<open>(\<or>)\<close> |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1745 |
defines or_int = or_int.F . |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1746 |
|
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1747 |
global_interpretation xor_int: fold2_bit_int \<open>(\<noteq>)\<close> |
|
bdea2b95817b
more direct characterization of binary bit operations
haftmann
parents:
79018
diff
changeset
|
1748 |
defines xor_int = xor_int.F . |
| 71802 | 1749 |
|
| 72082 | 1750 |
definition mask_int :: \<open>nat \<Rightarrow> int\<close> |
1751 |
where \<open>mask n = (2 :: int) ^ n - 1\<close> |
|
1752 |
||
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1753 |
definition push_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1754 |
where \<open>push_bit_int n k = k * 2 ^ n\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1755 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1756 |
definition drop_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1757 |
where \<open>drop_bit_int n k = k div 2 ^ n\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1758 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1759 |
definition take_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1760 |
where \<open>take_bit_int n k = k mod 2 ^ n\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1761 |
|
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1762 |
definition set_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1763 |
where \<open>set_bit n k = k OR push_bit n 1\<close> for k :: int |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1764 |
|
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1765 |
definition unset_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1766 |
where \<open>unset_bit n k = k AND NOT (push_bit n 1)\<close> for k :: int |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1767 |
|
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1768 |
definition flip_bit_int :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1769 |
where \<open>flip_bit n k = k XOR push_bit n 1\<close> for k :: int |
|
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1770 |
|
|
79072
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1771 |
lemma not_int_div_2: |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1772 |
\<open>NOT k div 2 = NOT (k div 2)\<close> for k :: int |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1773 |
by (simp add: not_int_def) |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1774 |
|
| 79068 | 1775 |
lemma bit_not_int_iff: |
|
79072
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1776 |
\<open>bit (NOT k) n \<longleftrightarrow> \<not> bit k n\<close> for k :: int |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1777 |
proof (rule sym, induction n arbitrary: k) |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1778 |
case 0 |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1779 |
then show ?case |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1780 |
by (simp add: bit_0 not_int_def) |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1781 |
next |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1782 |
case (Suc n) |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1783 |
then show ?case |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1784 |
by (simp add: bit_Suc not_int_div_2) |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1785 |
qed |
| 79068 | 1786 |
|
|
71042
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1787 |
instance proof |
|
73682
78044b2f001c
explicit type class operations for type-specific implementations
haftmann
parents:
73535
diff
changeset
|
1788 |
fix k l :: int and m n :: nat |
| 79489 | 1789 |
show \<open>unset_bit n k = (k OR push_bit n 1) XOR push_bit n 1\<close> |
|
79031
4596a14d9a95
slightly more elementary characterization of unset_bit
haftmann
parents:
79030
diff
changeset
|
1790 |
by (rule bit_eqI) |
| 79489 | 1791 |
(auto simp add: unset_bit_int_def |
1792 |
and_int.bit_iff or_int.bit_iff xor_int.bit_iff bit_not_int_iff push_bit_int_def bit_simps) |
|
|
79031
4596a14d9a95
slightly more elementary characterization of unset_bit
haftmann
parents:
79030
diff
changeset
|
1793 |
qed (fact and_int.rec or_int.rec xor_int.rec mask_int_def set_bit_int_def flip_bit_int_def |
|
79072
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
1794 |
push_bit_int_def drop_bit_int_def take_bit_int_def 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
|
1795 |
|
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1796 |
end |
|
400e9512f1d3
proof-of-concept theory for bit operations without a constructivistic representation and a minimal common logical foundation
haftmann
parents:
diff
changeset
|
1797 |
|
| 79070 | 1798 |
instance int :: linordered_euclidean_semiring_bit_operations .. |
1799 |
||
1800 |
context ring_bit_operations |
|
1801 |
begin |
|
1802 |
||
1803 |
lemma even_of_int_iff: |
|
1804 |
\<open>even (of_int k) \<longleftrightarrow> even k\<close> |
|
1805 |
by (induction k rule: int_bit_induct) simp_all |
|
1806 |
||
1807 |
lemma bit_of_int_iff [bit_simps]: |
|
1808 |
\<open>bit (of_int k) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> bit k n\<close>
|
|
1809 |
proof (cases \<open>possible_bit TYPE('a) n\<close>)
|
|
1810 |
case False |
|
1811 |
then show ?thesis |
|
1812 |
by (simp add: impossible_bit) |
|
1813 |
next |
|
1814 |
case True |
|
1815 |
then have \<open>bit (of_int k) n \<longleftrightarrow> bit k n\<close> |
|
1816 |
proof (induction k arbitrary: n rule: int_bit_induct) |
|
1817 |
case zero |
|
1818 |
then show ?case |
|
1819 |
by simp |
|
1820 |
next |
|
1821 |
case minus |
|
1822 |
then show ?case |
|
1823 |
by simp |
|
1824 |
next |
|
1825 |
case (even k) |
|
1826 |
then show ?case |
|
1827 |
using bit_double_iff [of \<open>of_int k\<close> n] Bit_Operations.bit_double_iff [of k n] |
|
1828 |
by (cases n) (auto simp add: ac_simps possible_bit_def dest: mult_not_zero) |
|
1829 |
next |
|
1830 |
case (odd k) |
|
1831 |
then show ?case |
|
1832 |
using bit_double_iff [of \<open>of_int k\<close> n] |
|
1833 |
by (cases n) |
|
1834 |
(auto simp add: ac_simps bit_double_iff even_bit_succ_iff Bit_Operations.bit_0 Bit_Operations.bit_Suc |
|
1835 |
possible_bit_def dest: mult_not_zero) |
|
1836 |
qed |
|
1837 |
with True show ?thesis |
|
1838 |
by simp |
|
1839 |
qed |
|
1840 |
||
1841 |
lemma push_bit_of_int: |
|
1842 |
\<open>push_bit n (of_int k) = of_int (push_bit n k)\<close> |
|
1843 |
by (simp add: push_bit_eq_mult Bit_Operations.push_bit_eq_mult) |
|
1844 |
||
1845 |
lemma of_int_push_bit: |
|
1846 |
\<open>of_int (push_bit n k) = push_bit n (of_int k)\<close> |
|
1847 |
by (simp add: push_bit_eq_mult Bit_Operations.push_bit_eq_mult) |
|
1848 |
||
1849 |
lemma take_bit_of_int: |
|
1850 |
\<open>take_bit n (of_int k) = of_int (take_bit n k)\<close> |
|
1851 |
by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_int_iff) |
|
1852 |
||
1853 |
lemma of_int_take_bit: |
|
1854 |
\<open>of_int (take_bit n k) = take_bit n (of_int k)\<close> |
|
1855 |
by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_int_iff) |
|
1856 |
||
1857 |
lemma of_int_not_eq: |
|
1858 |
\<open>of_int (NOT k) = NOT (of_int k)\<close> |
|
1859 |
by (rule bit_eqI) (simp add: bit_not_iff Bit_Operations.bit_not_iff bit_of_int_iff) |
|
1860 |
||
1861 |
lemma of_int_not_numeral: |
|
1862 |
\<open>of_int (NOT (numeral k)) = NOT (numeral k)\<close> |
|
1863 |
by (simp add: local.of_int_not_eq) |
|
1864 |
||
1865 |
lemma of_int_and_eq: |
|
1866 |
\<open>of_int (k AND l) = of_int k AND of_int l\<close> |
|
1867 |
by (rule bit_eqI) (simp add: bit_of_int_iff bit_and_iff Bit_Operations.bit_and_iff) |
|
1868 |
||
1869 |
lemma of_int_or_eq: |
|
1870 |
\<open>of_int (k OR l) = of_int k OR of_int l\<close> |
|
1871 |
by (rule bit_eqI) (simp add: bit_of_int_iff bit_or_iff Bit_Operations.bit_or_iff) |
|
1872 |
||
1873 |
lemma of_int_xor_eq: |
|
1874 |
\<open>of_int (k XOR l) = of_int k XOR of_int l\<close> |
|
1875 |
by (rule bit_eqI) (simp add: bit_of_int_iff bit_xor_iff Bit_Operations.bit_xor_iff) |
|
1876 |
||
1877 |
lemma of_int_mask_eq: |
|
1878 |
\<open>of_int (mask n) = mask n\<close> |
|
1879 |
by (induction n) (simp_all add: mask_Suc_double Bit_Operations.mask_Suc_double of_int_or_eq) |
|
1880 |
||
1881 |
end |
|
1882 |
||
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1883 |
lemma take_bit_int_less_exp [simp]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1884 |
\<open>take_bit n k < 2 ^ n\<close> for k :: int |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1885 |
by (simp add: take_bit_eq_mod) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1886 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1887 |
lemma take_bit_int_eq_self_iff: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1888 |
\<open>take_bit n k = k \<longleftrightarrow> 0 \<le> k \<and> k < 2 ^ n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1889 |
for k :: int |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1890 |
proof |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1891 |
assume ?P |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1892 |
moreover note take_bit_int_less_exp [of n k] take_bit_nonnegative [of n k] |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1893 |
ultimately show ?Q |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1894 |
by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1895 |
next |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1896 |
assume ?Q |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1897 |
then show ?P |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1898 |
by (simp add: take_bit_eq_mod) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1899 |
qed |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1900 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1901 |
lemma take_bit_int_eq_self: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1902 |
\<open>take_bit n k = k\<close> if \<open>0 \<le> k\<close> \<open>k < 2 ^ n\<close> for k :: int |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1903 |
using that by (simp add: take_bit_int_eq_self_iff) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
1904 |
|
| 72028 | 1905 |
lemma mask_nonnegative_int [simp]: |
1906 |
\<open>mask n \<ge> (0::int)\<close> |
|
| 79071 | 1907 |
by (simp add: mask_eq_exp_minus_1 add_le_imp_le_diff) |
| 72028 | 1908 |
|
1909 |
lemma not_mask_negative_int [simp]: |
|
1910 |
\<open>\<not> mask n < (0::int)\<close> |
|
1911 |
by (simp add: not_less) |
|
1912 |
||
| 71802 | 1913 |
lemma not_nonnegative_int_iff [simp]: |
1914 |
\<open>NOT k \<ge> 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
1915 |
by (simp add: not_int_def) |
|
1916 |
||
1917 |
lemma not_negative_int_iff [simp]: |
|
1918 |
\<open>NOT k < 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
1919 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less not_le) |
|
1920 |
||
1921 |
lemma and_nonnegative_int_iff [simp]: |
|
1922 |
\<open>k AND l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<or> l \<ge> 0\<close> for k l :: int |
|
1923 |
proof (induction k arbitrary: l rule: int_bit_induct) |
|
1924 |
case zero |
|
1925 |
then show ?case |
|
1926 |
by simp |
|
1927 |
next |
|
1928 |
case minus |
|
1929 |
then show ?case |
|
1930 |
by simp |
|
1931 |
next |
|
1932 |
case (even k) |
|
1933 |
then show ?case |
|
| 79068 | 1934 |
using and_int.rec [of \<open>k * 2\<close> l] |
| 74101 | 1935 |
by (simp add: pos_imp_zdiv_nonneg_iff zero_le_mult_iff) |
| 71802 | 1936 |
next |
1937 |
case (odd k) |
|
1938 |
from odd have \<open>0 \<le> k AND l div 2 \<longleftrightarrow> 0 \<le> k \<or> 0 \<le> l div 2\<close> |
|
1939 |
by simp |
|
| 74101 | 1940 |
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> |
| 71802 | 1941 |
by simp |
| 79068 | 1942 |
with and_int.rec [of \<open>1 + k * 2\<close> l] |
| 71802 | 1943 |
show ?case |
| 74101 | 1944 |
by (auto simp add: zero_le_mult_iff not_le) |
| 71802 | 1945 |
qed |
1946 |
||
1947 |
lemma and_negative_int_iff [simp]: |
|
1948 |
\<open>k AND l < 0 \<longleftrightarrow> k < 0 \<and> l < 0\<close> for k l :: int |
|
1949 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
1950 |
||
| 72009 | 1951 |
lemma and_less_eq: |
1952 |
\<open>k AND l \<le> k\<close> if \<open>l < 0\<close> for k l :: int |
|
1953 |
using that proof (induction k arbitrary: l rule: int_bit_induct) |
|
1954 |
case zero |
|
1955 |
then show ?case |
|
1956 |
by simp |
|
1957 |
next |
|
1958 |
case minus |
|
1959 |
then show ?case |
|
1960 |
by simp |
|
1961 |
next |
|
1962 |
case (even k) |
|
1963 |
from even.IH [of \<open>l div 2\<close>] even.hyps even.prems |
|
1964 |
show ?case |
|
| 79068 | 1965 |
by (simp add: and_int.rec [of _ l]) |
| 72009 | 1966 |
next |
1967 |
case (odd k) |
|
1968 |
from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems |
|
1969 |
show ?case |
|
| 79068 | 1970 |
by (simp add: and_int.rec [of _ l]) |
| 72009 | 1971 |
qed |
1972 |
||
| 71802 | 1973 |
lemma or_nonnegative_int_iff [simp]: |
1974 |
\<open>k OR l \<ge> 0 \<longleftrightarrow> k \<ge> 0 \<and> l \<ge> 0\<close> for k l :: int |
|
1975 |
by (simp only: or_eq_not_not_and not_nonnegative_int_iff) simp |
|
1976 |
||
1977 |
lemma or_negative_int_iff [simp]: |
|
1978 |
\<open>k OR l < 0 \<longleftrightarrow> k < 0 \<or> l < 0\<close> for k l :: int |
|
1979 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
1980 |
||
| 72009 | 1981 |
lemma or_greater_eq: |
1982 |
\<open>k OR l \<ge> k\<close> if \<open>l \<ge> 0\<close> for k l :: int |
|
1983 |
using that proof (induction k arbitrary: l rule: int_bit_induct) |
|
1984 |
case zero |
|
1985 |
then show ?case |
|
1986 |
by simp |
|
1987 |
next |
|
1988 |
case minus |
|
1989 |
then show ?case |
|
1990 |
by simp |
|
1991 |
next |
|
1992 |
case (even k) |
|
1993 |
from even.IH [of \<open>l div 2\<close>] even.hyps even.prems |
|
1994 |
show ?case |
|
| 79068 | 1995 |
by (simp add: or_int.rec [of _ l]) |
| 72009 | 1996 |
next |
1997 |
case (odd k) |
|
1998 |
from odd.IH [of \<open>l div 2\<close>] odd.hyps odd.prems |
|
1999 |
show ?case |
|
| 79068 | 2000 |
by (simp add: or_int.rec [of _ l]) |
| 72009 | 2001 |
qed |
2002 |
||
| 71802 | 2003 |
lemma xor_nonnegative_int_iff [simp]: |
2004 |
\<open>k XOR l \<ge> 0 \<longleftrightarrow> (k \<ge> 0 \<longleftrightarrow> l \<ge> 0)\<close> for k l :: int |
|
2005 |
by (simp only: bit.xor_def or_nonnegative_int_iff) auto |
|
2006 |
||
2007 |
lemma xor_negative_int_iff [simp]: |
|
2008 |
\<open>k XOR l < 0 \<longleftrightarrow> (k < 0) \<noteq> (l < 0)\<close> for k l :: int |
|
2009 |
by (subst Not_eq_iff [symmetric]) (auto simp add: not_less) |
|
2010 |
||
| 72488 | 2011 |
lemma OR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
| 79017 | 2012 |
\<open>x OR y < 2 ^ n\<close> if \<open>0 \<le> x\<close> \<open>x < 2 ^ n\<close> \<open>y < 2 ^ n\<close> for x y :: int |
2013 |
using that proof (induction x arbitrary: y n rule: int_bit_induct) |
|
| 72488 | 2014 |
case zero |
2015 |
then show ?case |
|
2016 |
by simp |
|
2017 |
next |
|
2018 |
case minus |
|
2019 |
then show ?case |
|
2020 |
by simp |
|
2021 |
next |
|
2022 |
case (even x) |
|
2023 |
from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps |
|
| 79068 | 2024 |
show ?case |
2025 |
by (cases n) (auto simp add: or_int.rec [of \<open>_ * 2\<close>] elim: oddE) |
|
| 72488 | 2026 |
next |
2027 |
case (odd x) |
|
2028 |
from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps |
|
2029 |
show ?case |
|
| 79068 | 2030 |
by (cases n) (auto simp add: or_int.rec [of \<open>1 + _ * 2\<close>], linarith) |
| 72488 | 2031 |
qed |
2032 |
||
2033 |
lemma XOR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
| 79017 | 2034 |
\<open>x XOR y < 2 ^ n\<close> if \<open>0 \<le> x\<close> \<open>x < 2 ^ n\<close> \<open>y < 2 ^ n\<close> for x y :: int |
2035 |
using that proof (induction x arbitrary: y n rule: int_bit_induct) |
|
| 72488 | 2036 |
case zero |
2037 |
then show ?case |
|
2038 |
by simp |
|
2039 |
next |
|
2040 |
case minus |
|
2041 |
then show ?case |
|
2042 |
by simp |
|
2043 |
next |
|
2044 |
case (even x) |
|
2045 |
from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps |
|
| 79068 | 2046 |
show ?case |
2047 |
by (cases n) (auto simp add: xor_int.rec [of \<open>_ * 2\<close>] elim: oddE) |
|
| 72488 | 2048 |
next |
2049 |
case (odd x) |
|
2050 |
from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps |
|
2051 |
show ?case |
|
| 79068 | 2052 |
by (cases n) (auto simp add: xor_int.rec [of \<open>1 + _ * 2\<close>]) |
| 72488 | 2053 |
qed |
2054 |
||
2055 |
lemma AND_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
| 79017 | 2056 |
\<open>0 \<le> x AND y\<close> if \<open>0 \<le> x\<close> for x y :: int |
2057 |
using that by simp |
|
| 72488 | 2058 |
|
2059 |
lemma OR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
| 79017 | 2060 |
\<open>0 \<le> x OR y\<close> if \<open>0 \<le> x\<close> \<open>0 \<le> y\<close> for x y :: int |
2061 |
using that by simp |
|
| 72488 | 2062 |
|
2063 |
lemma XOR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
| 79017 | 2064 |
\<open>0 \<le> x XOR y\<close> if \<open>0 \<le> x\<close> \<open>0 \<le> y\<close> for x y :: int |
2065 |
using that by simp |
|
| 72488 | 2066 |
|
2067 |
lemma AND_upper1 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
| 79017 | 2068 |
\<open>x AND y \<le> x\<close> if \<open>0 \<le> x\<close> for x y :: int |
2069 |
using that proof (induction x arbitrary: y rule: int_bit_induct) |
|
| 73535 | 2070 |
case (odd k) |
2071 |
then have \<open>k AND y div 2 \<le> k\<close> |
|
2072 |
by simp |
|
| 79068 | 2073 |
then show ?case |
2074 |
by (simp add: and_int.rec [of \<open>1 + _ * 2\<close>]) |
|
2075 |
qed (simp_all add: and_int.rec [of \<open>_ * 2\<close>]) |
|
| 72488 | 2076 |
|
| 79017 | 2077 |
lemma AND_upper1' [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
2078 |
\<open>y AND x \<le> z\<close> if \<open>0 \<le> y\<close> \<open>y \<le> z\<close> for x y z :: int |
|
2079 |
using _ \<open>y \<le> z\<close> by (rule order_trans) (use \<open>0 \<le> y\<close> in simp) |
|
2080 |
||
2081 |
lemma AND_upper1'' [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2082 |
\<open>y AND x < z\<close> if \<open>0 \<le> y\<close> \<open>y < z\<close> for x y z :: int |
|
2083 |
using _ \<open>y < z\<close> by (rule order_le_less_trans) (use \<open>0 \<le> y\<close> in simp) |
|
| 72488 | 2084 |
|
2085 |
lemma AND_upper2 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
| 79017 | 2086 |
\<open>x AND y \<le> y\<close> if \<open>0 \<le> y\<close> for x y :: int |
2087 |
using that AND_upper1 [of y x] by (simp add: ac_simps) |
|
2088 |
||
2089 |
lemma AND_upper2' [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2090 |
\<open>x AND y \<le> z\<close> if \<open>0 \<le> y\<close> \<open>y \<le> z\<close> for x y :: int |
|
2091 |
using that AND_upper1' [of y z x] by (simp add: ac_simps) |
|
2092 |
||
2093 |
lemma AND_upper2'' [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
2094 |
\<open>x AND y < z\<close> if \<open>0 \<le> y\<close> \<open>y < z\<close> for x y :: int |
|
2095 |
using that AND_upper1'' [of y z x] by (simp add: ac_simps) |
|
2096 |
||
2097 |
lemma plus_and_or: |
|
2098 |
\<open>(x AND y) + (x OR y) = x + y\<close> for x y :: int |
|
| 72488 | 2099 |
proof (induction x arbitrary: y rule: int_bit_induct) |
2100 |
case zero |
|
2101 |
then show ?case |
|
2102 |
by simp |
|
2103 |
next |
|
2104 |
case minus |
|
2105 |
then show ?case |
|
2106 |
by simp |
|
2107 |
next |
|
2108 |
case (even x) |
|
2109 |
from even.IH [of \<open>y div 2\<close>] |
|
2110 |
show ?case |
|
| 79068 | 2111 |
by (auto simp add: and_int.rec [of _ y] or_int.rec [of _ y] elim: oddE) |
| 72488 | 2112 |
next |
2113 |
case (odd x) |
|
2114 |
from odd.IH [of \<open>y div 2\<close>] |
|
2115 |
show ?case |
|
| 79068 | 2116 |
by (auto simp add: and_int.rec [of _ y] or_int.rec [of _ y] elim: oddE) |
| 72488 | 2117 |
qed |
2118 |
||
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2119 |
lemma push_bit_minus_one: |
| 79017 | 2120 |
\<open>push_bit n (- 1 :: int) = - (2 ^ n)\<close> |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2121 |
by (simp add: push_bit_eq_mult) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2122 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2123 |
lemma minus_1_div_exp_eq_int: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2124 |
\<open>- 1 div (2 :: int) ^ n = - 1\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2125 |
by (induction n) (use div_exp_eq [symmetric, of \<open>- 1 :: int\<close> 1] in \<open>simp_all add: ac_simps\<close>) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2126 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2127 |
lemma drop_bit_minus_one [simp]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2128 |
\<open>drop_bit n (- 1 :: int) = - 1\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2129 |
by (simp add: drop_bit_eq_div minus_1_div_exp_eq_int) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2130 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2131 |
lemma take_bit_minus: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2132 |
\<open>take_bit n (- take_bit n k) = take_bit n (- k)\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2133 |
for k :: int |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2134 |
by (simp add: take_bit_eq_mod mod_minus_eq) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2135 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2136 |
lemma take_bit_diff: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2137 |
\<open>take_bit n (take_bit n k - take_bit n l) = take_bit n (k - l)\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2138 |
for k l :: int |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2139 |
by (simp add: take_bit_eq_mod mod_diff_eq) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2140 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2141 |
lemma (in ring_1) of_nat_nat_take_bit_eq [simp]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2142 |
\<open>of_nat (nat (take_bit n k)) = of_int (take_bit n k)\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2143 |
by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2144 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2145 |
lemma take_bit_minus_small_eq: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2146 |
\<open>take_bit n (- k) = 2 ^ n - k\<close> if \<open>0 < k\<close> \<open>k \<le> 2 ^ n\<close> for k :: int |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2147 |
proof - |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2148 |
define m where \<open>m = nat k\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2149 |
with that have \<open>k = int m\<close> and \<open>0 < m\<close> and \<open>m \<le> 2 ^ n\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2150 |
by simp_all |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2151 |
have \<open>(2 ^ n - m) mod 2 ^ n = 2 ^ n - m\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2152 |
using \<open>0 < m\<close> by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2153 |
then have \<open>int ((2 ^ n - m) mod 2 ^ n) = int (2 ^ n - m)\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2154 |
by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2155 |
then have \<open>(2 ^ n - int m) mod 2 ^ n = 2 ^ n - int m\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2156 |
using \<open>m \<le> 2 ^ n\<close> by (simp only: of_nat_mod of_nat_diff) simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2157 |
with \<open>k = int m\<close> have \<open>(2 ^ n - k) mod 2 ^ n = 2 ^ n - k\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2158 |
by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2159 |
then show ?thesis |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2160 |
by (simp add: take_bit_eq_mod) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2161 |
qed |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2162 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2163 |
lemma push_bit_nonnegative_int_iff [simp]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2164 |
\<open>push_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2165 |
by (simp add: push_bit_eq_mult zero_le_mult_iff power_le_zero_eq) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2166 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2167 |
lemma push_bit_negative_int_iff [simp]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2168 |
\<open>push_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2169 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2170 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2171 |
lemma drop_bit_nonnegative_int_iff [simp]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2172 |
\<open>drop_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2173 |
by (induction n) (auto simp add: drop_bit_Suc drop_bit_half) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2174 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2175 |
lemma drop_bit_negative_int_iff [simp]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2176 |
\<open>drop_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2177 |
by (subst Not_eq_iff [symmetric]) (simp add: not_less) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2178 |
|
| 71802 | 2179 |
lemma set_bit_nonnegative_int_iff [simp]: |
2180 |
\<open>set_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
| 79068 | 2181 |
by (simp add: set_bit_eq_or) |
| 71802 | 2182 |
|
2183 |
lemma set_bit_negative_int_iff [simp]: |
|
2184 |
\<open>set_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
| 79068 | 2185 |
by (simp add: set_bit_eq_or) |
| 71802 | 2186 |
|
2187 |
lemma unset_bit_nonnegative_int_iff [simp]: |
|
2188 |
\<open>unset_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
| 79068 | 2189 |
by (simp add: unset_bit_eq_and_not) |
| 71802 | 2190 |
|
2191 |
lemma unset_bit_negative_int_iff [simp]: |
|
2192 |
\<open>unset_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
| 79068 | 2193 |
by (simp add: unset_bit_eq_and_not) |
| 71802 | 2194 |
|
2195 |
lemma flip_bit_nonnegative_int_iff [simp]: |
|
2196 |
\<open>flip_bit n k \<ge> 0 \<longleftrightarrow> k \<ge> 0\<close> for k :: int |
|
| 79068 | 2197 |
by (simp add: flip_bit_eq_xor) |
| 71802 | 2198 |
|
2199 |
lemma flip_bit_negative_int_iff [simp]: |
|
2200 |
\<open>flip_bit n k < 0 \<longleftrightarrow> k < 0\<close> for k :: int |
|
| 79068 | 2201 |
by (simp add: flip_bit_eq_xor) |
| 71802 | 2202 |
|
| 71986 | 2203 |
lemma set_bit_greater_eq: |
2204 |
\<open>set_bit n k \<ge> k\<close> for k :: int |
|
| 79068 | 2205 |
by (simp add: set_bit_eq_or or_greater_eq) |
| 71986 | 2206 |
|
2207 |
lemma unset_bit_less_eq: |
|
2208 |
\<open>unset_bit n k \<le> k\<close> for k :: int |
|
| 79068 | 2209 |
by (simp add: unset_bit_eq_and_not and_less_eq) |
| 71986 | 2210 |
|
|
75651
f4116b7a6679
Move code lemmas for symbolic computation of bit operations on int to distribution.
haftmann
parents:
75138
diff
changeset
|
2211 |
lemma and_int_unfold: |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2212 |
\<open>k AND l = (if k = 0 \<or> l = 0 then 0 else if k = - 1 then l else if l = - 1 then k |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2213 |
else (k mod 2) * (l mod 2) + 2 * ((k div 2) AND (l div 2)))\<close> for k l :: int |
| 79068 | 2214 |
by (auto simp add: and_int.rec [of k l] zmult_eq_1_iff elim: oddE) |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2215 |
|
|
75651
f4116b7a6679
Move code lemmas for symbolic computation of bit operations on int to distribution.
haftmann
parents:
75138
diff
changeset
|
2216 |
lemma or_int_unfold: |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2217 |
\<open>k OR l = (if k = - 1 \<or> l = - 1 then - 1 else if k = 0 then l else if l = 0 then k |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2218 |
else max (k mod 2) (l mod 2) + 2 * ((k div 2) OR (l div 2)))\<close> for k l :: int |
| 79068 | 2219 |
by (auto simp add: or_int.rec [of k l] elim: oddE) |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2220 |
|
|
75651
f4116b7a6679
Move code lemmas for symbolic computation of bit operations on int to distribution.
haftmann
parents:
75138
diff
changeset
|
2221 |
lemma xor_int_unfold: |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2222 |
\<open>k XOR l = (if k = - 1 then NOT l else if l = - 1 then NOT k else if k = 0 then l else if l = 0 then k |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2223 |
else \<bar>k mod 2 - l mod 2\<bar> + 2 * ((k div 2) XOR (l div 2)))\<close> for k l :: int |
| 79068 | 2224 |
by (auto simp add: xor_int.rec [of k l] not_int_def elim!: oddE) |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2225 |
|
| 74163 | 2226 |
lemma bit_minus_int_iff: |
| 79017 | 2227 |
\<open>bit (- k) n \<longleftrightarrow> bit (NOT (k - 1)) n\<close> for k :: int |
| 74163 | 2228 |
by (simp add: bit_simps) |
2229 |
||
| 74592 | 2230 |
lemma take_bit_incr_eq: |
| 79017 | 2231 |
\<open>take_bit n (k + 1) = 1 + take_bit n k\<close> if \<open>take_bit n k \<noteq> 2 ^ n - 1\<close> for k :: int |
| 74592 | 2232 |
proof - |
2233 |
from that have \<open>2 ^ n \<noteq> k mod 2 ^ n + 1\<close> |
|
2234 |
by (simp add: take_bit_eq_mod) |
|
2235 |
moreover have \<open>k mod 2 ^ n < 2 ^ n\<close> |
|
2236 |
by simp |
|
2237 |
ultimately have *: \<open>k mod 2 ^ n + 1 < 2 ^ n\<close> |
|
2238 |
by linarith |
|
2239 |
have \<open>(k + 1) mod 2 ^ n = (k mod 2 ^ n + 1) mod 2 ^ n\<close> |
|
2240 |
by (simp add: mod_simps) |
|
2241 |
also have \<open>\<dots> = k mod 2 ^ n + 1\<close> |
|
2242 |
using * by (simp add: zmod_trivial_iff) |
|
2243 |
finally have \<open>(k + 1) mod 2 ^ n = k mod 2 ^ n + 1\<close> . |
|
2244 |
then show ?thesis |
|
2245 |
by (simp add: take_bit_eq_mod) |
|
2246 |
qed |
|
2247 |
||
2248 |
lemma take_bit_decr_eq: |
|
| 79017 | 2249 |
\<open>take_bit n (k - 1) = take_bit n k - 1\<close> if \<open>take_bit n k \<noteq> 0\<close> for k :: int |
| 74592 | 2250 |
proof - |
2251 |
from that have \<open>k mod 2 ^ n \<noteq> 0\<close> |
|
2252 |
by (simp add: take_bit_eq_mod) |
|
2253 |
moreover have \<open>k mod 2 ^ n \<ge> 0\<close> \<open>k mod 2 ^ n < 2 ^ n\<close> |
|
2254 |
by simp_all |
|
2255 |
ultimately have *: \<open>k mod 2 ^ n > 0\<close> |
|
2256 |
by linarith |
|
2257 |
have \<open>(k - 1) mod 2 ^ n = (k mod 2 ^ n - 1) mod 2 ^ n\<close> |
|
2258 |
by (simp add: mod_simps) |
|
2259 |
also have \<open>\<dots> = k mod 2 ^ n - 1\<close> |
|
2260 |
by (simp add: zmod_trivial_iff) |
|
2261 |
(use \<open>k mod 2 ^ n < 2 ^ n\<close> * in linarith) |
|
2262 |
finally have \<open>(k - 1) mod 2 ^ n = k mod 2 ^ n - 1\<close> . |
|
2263 |
then show ?thesis |
|
2264 |
by (simp add: take_bit_eq_mod) |
|
2265 |
qed |
|
2266 |
||
2267 |
lemma take_bit_int_greater_eq: |
|
2268 |
\<open>k + 2 ^ n \<le> take_bit n k\<close> if \<open>k < 0\<close> for k :: int |
|
2269 |
proof - |
|
2270 |
have \<open>k + 2 ^ n \<le> take_bit n (k + 2 ^ n)\<close> |
|
2271 |
proof (cases \<open>k > - (2 ^ n)\<close>) |
|
2272 |
case False |
|
2273 |
then have \<open>k + 2 ^ n \<le> 0\<close> |
|
2274 |
by simp |
|
2275 |
also note take_bit_nonnegative |
|
2276 |
finally show ?thesis . |
|
2277 |
next |
|
2278 |
case True |
|
2279 |
with that have \<open>0 \<le> k + 2 ^ n\<close> and \<open>k + 2 ^ n < 2 ^ n\<close> |
|
2280 |
by simp_all |
|
2281 |
then show ?thesis |
|
2282 |
by (simp only: take_bit_eq_mod mod_pos_pos_trivial) |
|
2283 |
qed |
|
2284 |
then show ?thesis |
|
2285 |
by (simp add: take_bit_eq_mod) |
|
2286 |
qed |
|
2287 |
||
2288 |
lemma take_bit_int_less_eq: |
|
2289 |
\<open>take_bit n k \<le> k - 2 ^ n\<close> if \<open>2 ^ n \<le> k\<close> and \<open>n > 0\<close> for k :: int |
|
2290 |
using that zmod_le_nonneg_dividend [of \<open>k - 2 ^ n\<close> \<open>2 ^ n\<close>] |
|
2291 |
by (simp add: take_bit_eq_mod) |
|
2292 |
||
2293 |
lemma take_bit_int_less_eq_self_iff: |
|
| 79017 | 2294 |
\<open>take_bit n k \<le> k \<longleftrightarrow> 0 \<le> k\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) for k :: int |
| 74592 | 2295 |
proof |
2296 |
assume ?P |
|
2297 |
show ?Q |
|
2298 |
proof (rule ccontr) |
|
2299 |
assume \<open>\<not> 0 \<le> k\<close> |
|
2300 |
then have \<open>k < 0\<close> |
|
2301 |
by simp |
|
2302 |
with \<open>?P\<close> |
|
2303 |
have \<open>take_bit n k < 0\<close> |
|
2304 |
by (rule le_less_trans) |
|
2305 |
then show False |
|
2306 |
by simp |
|
2307 |
qed |
|
2308 |
next |
|
2309 |
assume ?Q |
|
2310 |
then show ?P |
|
2311 |
by (simp add: take_bit_eq_mod zmod_le_nonneg_dividend) |
|
2312 |
qed |
|
2313 |
||
2314 |
lemma take_bit_int_less_self_iff: |
|
| 79017 | 2315 |
\<open>take_bit n k < k \<longleftrightarrow> 2 ^ n \<le> k\<close> for k :: int |
| 74592 | 2316 |
by (auto simp add: less_le take_bit_int_less_eq_self_iff take_bit_int_eq_self_iff |
2317 |
intro: order_trans [of 0 \<open>2 ^ n\<close> k]) |
|
2318 |
||
2319 |
lemma take_bit_int_greater_self_iff: |
|
| 79017 | 2320 |
\<open>k < take_bit n k \<longleftrightarrow> k < 0\<close> for k :: int |
| 74592 | 2321 |
using take_bit_int_less_eq_self_iff [of n k] by auto |
2322 |
||
2323 |
lemma take_bit_int_greater_eq_self_iff: |
|
| 79017 | 2324 |
\<open>k \<le> take_bit n k \<longleftrightarrow> k < 2 ^ n\<close> for k :: int |
| 74592 | 2325 |
by (auto simp add: le_less take_bit_int_greater_self_iff take_bit_int_eq_self_iff |
2326 |
dest: sym not_sym intro: less_trans [of k 0 \<open>2 ^ n\<close>]) |
|
2327 |
||
| 79070 | 2328 |
lemma take_bit_tightened_less_eq_int: |
2329 |
\<open>take_bit m k \<le> take_bit n k\<close> if \<open>m \<le> n\<close> for k :: int |
|
2330 |
proof - |
|
2331 |
have \<open>take_bit m (take_bit n k) \<le> take_bit n k\<close> |
|
2332 |
by (simp only: take_bit_int_less_eq_self_iff take_bit_nonnegative) |
|
2333 |
with that show ?thesis |
|
2334 |
by simp |
|
2335 |
qed |
|
2336 |
||
| 74592 | 2337 |
lemma not_exp_less_eq_0_int [simp]: |
2338 |
\<open>\<not> 2 ^ n \<le> (0::int)\<close> |
|
2339 |
by (simp add: power_le_zero_eq) |
|
2340 |
||
2341 |
lemma int_bit_bound: |
|
2342 |
fixes k :: int |
|
2343 |
obtains n where \<open>\<And>m. n \<le> m \<Longrightarrow> bit k m \<longleftrightarrow> bit k n\<close> |
|
2344 |
and \<open>n > 0 \<Longrightarrow> bit k (n - 1) \<noteq> bit k n\<close> |
|
2345 |
proof - |
|
2346 |
obtain q where *: \<open>\<And>m. q \<le> m \<Longrightarrow> bit k m \<longleftrightarrow> bit k q\<close> |
|
2347 |
proof (cases \<open>k \<ge> 0\<close>) |
|
2348 |
case True |
|
2349 |
moreover from power_gt_expt [of 2 \<open>nat k\<close>] |
|
2350 |
have \<open>nat k < 2 ^ nat k\<close> |
|
2351 |
by simp |
|
2352 |
then have \<open>int (nat k) < int (2 ^ nat k)\<close> |
|
2353 |
by (simp only: of_nat_less_iff) |
|
2354 |
ultimately have *: \<open>k div 2 ^ nat k = 0\<close> |
|
2355 |
by simp |
|
2356 |
show thesis |
|
2357 |
proof (rule that [of \<open>nat k\<close>]) |
|
2358 |
fix m |
|
2359 |
assume \<open>nat k \<le> m\<close> |
|
2360 |
then show \<open>bit k m \<longleftrightarrow> bit k (nat k)\<close> |
|
2361 |
by (auto simp add: * bit_iff_odd power_add zdiv_zmult2_eq dest!: le_Suc_ex) |
|
2362 |
qed |
|
2363 |
next |
|
2364 |
case False |
|
2365 |
moreover from power_gt_expt [of 2 \<open>nat (- k)\<close>] |
|
2366 |
have \<open>nat (- k) < 2 ^ nat (- k)\<close> |
|
2367 |
by simp |
|
2368 |
then have \<open>int (nat (- k)) < int (2 ^ nat (- k))\<close> |
|
2369 |
by (simp only: of_nat_less_iff) |
|
2370 |
ultimately have \<open>- k div - (2 ^ nat (- k)) = - 1\<close> |
|
2371 |
by (subst div_pos_neg_trivial) simp_all |
|
2372 |
then have *: \<open>k div 2 ^ nat (- k) = - 1\<close> |
|
2373 |
by simp |
|
2374 |
show thesis |
|
2375 |
proof (rule that [of \<open>nat (- k)\<close>]) |
|
2376 |
fix m |
|
2377 |
assume \<open>nat (- k) \<le> m\<close> |
|
2378 |
then show \<open>bit k m \<longleftrightarrow> bit k (nat (- k))\<close> |
|
2379 |
by (auto simp add: * bit_iff_odd power_add zdiv_zmult2_eq minus_1_div_exp_eq_int dest!: le_Suc_ex) |
|
2380 |
qed |
|
2381 |
qed |
|
2382 |
show thesis |
|
2383 |
proof (cases \<open>\<forall>m. bit k m \<longleftrightarrow> bit k q\<close>) |
|
2384 |
case True |
|
2385 |
then have \<open>bit k 0 \<longleftrightarrow> bit k q\<close> |
|
2386 |
by blast |
|
2387 |
with True that [of 0] show thesis |
|
2388 |
by simp |
|
2389 |
next |
|
2390 |
case False |
|
2391 |
then obtain r where **: \<open>bit k r \<noteq> bit k q\<close> |
|
2392 |
by blast |
|
2393 |
have \<open>r < q\<close> |
|
2394 |
by (rule ccontr) (use * [of r] ** in simp) |
|
2395 |
define N where \<open>N = {n. n < q \<and> bit k n \<noteq> bit k q}\<close>
|
|
2396 |
moreover have \<open>finite N\<close> \<open>r \<in> N\<close> |
|
2397 |
using ** N_def \<open>r < q\<close> by auto |
|
2398 |
moreover define n where \<open>n = Suc (Max N)\<close> |
|
2399 |
ultimately have \<open>\<And>m. n \<le> m \<Longrightarrow> bit k m \<longleftrightarrow> bit k n\<close> |
|
2400 |
apply auto |
|
2401 |
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) |
|
2402 |
apply (metis "*" Max_ge Suc_n_not_le_n \<open>finite N\<close> linorder_not_less mem_Collect_eq) |
|
2403 |
apply (metis "*" Max_ge Suc_n_not_le_n \<open>finite N\<close> linorder_not_less mem_Collect_eq) |
|
2404 |
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) |
|
2405 |
done |
|
2406 |
have \<open>bit k (Max N) \<noteq> bit k n\<close> |
|
2407 |
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) |
|
2408 |
show thesis apply (rule that [of n]) |
|
2409 |
using \<open>\<And>m. n \<le> m \<Longrightarrow> bit k m = bit k n\<close> apply blast |
|
2410 |
using \<open>bit k (Max N) \<noteq> bit k n\<close> n_def by auto |
|
2411 |
qed |
|
2412 |
qed |
|
2413 |
||
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2414 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2415 |
subsection \<open>Instance \<^typ>\<open>nat\<close>\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2416 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2417 |
instantiation nat :: semiring_bit_operations |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2418 |
begin |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2419 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2420 |
definition and_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2421 |
where \<open>m AND n = nat (int m AND int n)\<close> for m n :: nat |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2422 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2423 |
definition or_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2424 |
where \<open>m OR n = nat (int m OR int n)\<close> for m n :: nat |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2425 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2426 |
definition xor_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2427 |
where \<open>m XOR n = nat (int m XOR int n)\<close> for m n :: nat |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2428 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2429 |
definition mask_nat :: \<open>nat \<Rightarrow> nat\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2430 |
where \<open>mask n = (2 :: nat) ^ n - 1\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2431 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2432 |
definition push_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2433 |
where \<open>push_bit_nat n m = m * 2 ^ n\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2434 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2435 |
definition drop_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2436 |
where \<open>drop_bit_nat n m = m div 2 ^ n\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2437 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2438 |
definition take_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2439 |
where \<open>take_bit_nat n m = m mod 2 ^ n\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2440 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2441 |
definition set_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2442 |
where \<open>set_bit m n = n OR push_bit m 1\<close> for m n :: nat |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2443 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2444 |
definition unset_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
| 79489 | 2445 |
where \<open>unset_bit m n = (n OR push_bit m 1) XOR push_bit m 1\<close> for m n :: nat |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2446 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2447 |
definition flip_bit_nat :: \<open>nat \<Rightarrow> nat \<Rightarrow> nat\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2448 |
where \<open>flip_bit m n = n XOR push_bit m 1\<close> for m n :: nat |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2449 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2450 |
instance proof |
|
79031
4596a14d9a95
slightly more elementary characterization of unset_bit
haftmann
parents:
79030
diff
changeset
|
2451 |
fix m n :: nat |
|
79008
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
2452 |
show \<open>m AND n = of_bool (odd m \<and> odd n) + 2 * (m div 2 AND n div 2)\<close> |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
2453 |
by (simp add: and_nat_def and_rec [of \<open>int m\<close> \<open>int n\<close>] nat_add_distrib of_nat_div) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
2454 |
show \<open>m OR n = of_bool (odd m \<or> odd n) + 2 * (m div 2 OR n div 2)\<close> |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
2455 |
by (simp add: or_nat_def or_rec [of \<open>int m\<close> \<open>int n\<close>] nat_add_distrib of_nat_div) |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
2456 |
show \<open>m XOR n = of_bool (odd m \<noteq> odd n) + 2 * (m div 2 XOR n div 2)\<close> |
|
74a4776f7a22
operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents:
78955
diff
changeset
|
2457 |
by (simp add: xor_nat_def xor_rec [of \<open>int m\<close> \<open>int n\<close>] nat_add_distrib of_nat_div) |
| 79489 | 2458 |
qed (simp_all add: mask_nat_def set_bit_nat_def unset_bit_nat_def flip_bit_nat_def |
2459 |
push_bit_nat_def drop_bit_nat_def take_bit_nat_def) |
|
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2460 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2461 |
end |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2462 |
|
| 79070 | 2463 |
instance nat :: linordered_euclidean_semiring_bit_operations .. |
2464 |
||
2465 |
context semiring_bit_operations |
|
2466 |
begin |
|
2467 |
||
2468 |
lemma push_bit_of_nat: |
|
2469 |
\<open>push_bit n (of_nat m) = of_nat (push_bit n m)\<close> |
|
2470 |
by (simp add: push_bit_eq_mult Bit_Operations.push_bit_eq_mult) |
|
2471 |
||
2472 |
lemma of_nat_push_bit: |
|
2473 |
\<open>of_nat (push_bit m n) = push_bit m (of_nat n)\<close> |
|
2474 |
by (simp add: push_bit_eq_mult Bit_Operations.push_bit_eq_mult) |
|
2475 |
||
2476 |
lemma take_bit_of_nat: |
|
2477 |
\<open>take_bit n (of_nat m) = of_nat (take_bit n m)\<close> |
|
2478 |
by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_nat_iff) |
|
2479 |
||
2480 |
lemma of_nat_take_bit: |
|
2481 |
\<open>of_nat (take_bit n m) = take_bit n (of_nat m)\<close> |
|
2482 |
by (rule bit_eqI) (simp add: bit_take_bit_iff Bit_Operations.bit_take_bit_iff bit_of_nat_iff) |
|
2483 |
||
2484 |
lemma of_nat_and_eq: |
|
2485 |
\<open>of_nat (m AND n) = of_nat m AND of_nat n\<close> |
|
2486 |
by (rule bit_eqI) (simp add: bit_of_nat_iff bit_and_iff Bit_Operations.bit_and_iff) |
|
2487 |
||
2488 |
lemma of_nat_or_eq: |
|
2489 |
\<open>of_nat (m OR n) = of_nat m OR of_nat n\<close> |
|
2490 |
by (rule bit_eqI) (simp add: bit_of_nat_iff bit_or_iff Bit_Operations.bit_or_iff) |
|
2491 |
||
2492 |
lemma of_nat_xor_eq: |
|
2493 |
\<open>of_nat (m XOR n) = of_nat m XOR of_nat n\<close> |
|
2494 |
by (rule bit_eqI) (simp add: bit_of_nat_iff bit_xor_iff Bit_Operations.bit_xor_iff) |
|
2495 |
||
2496 |
lemma of_nat_mask_eq: |
|
2497 |
\<open>of_nat (mask n) = mask n\<close> |
|
2498 |
by (induction n) (simp_all add: mask_Suc_double Bit_Operations.mask_Suc_double of_nat_or_eq) |
|
2499 |
||
2500 |
end |
|
2501 |
||
2502 |
context linordered_euclidean_semiring_bit_operations |
|
2503 |
begin |
|
2504 |
||
2505 |
lemma drop_bit_of_nat: |
|
2506 |
"drop_bit n (of_nat m) = of_nat (drop_bit n m)" |
|
2507 |
by (simp add: drop_bit_eq_div Bit_Operations.drop_bit_eq_div of_nat_div [of m "2 ^ n"]) |
|
2508 |
||
2509 |
lemma of_nat_drop_bit: |
|
2510 |
\<open>of_nat (drop_bit m n) = drop_bit m (of_nat n)\<close> |
|
2511 |
by (simp add: drop_bit_eq_div Bit_Operations.drop_bit_eq_div of_nat_div) |
|
2512 |
||
2513 |
end |
|
2514 |
||
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2515 |
lemma take_bit_nat_less_exp [simp]: |
| 79068 | 2516 |
\<open>take_bit n m < 2 ^ n\<close> for n m :: nat |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2517 |
by (simp add: take_bit_eq_mod) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2518 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2519 |
lemma take_bit_nat_eq_self_iff: |
| 79017 | 2520 |
\<open>take_bit n m = m \<longleftrightarrow> m < 2 ^ n\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) for n m :: nat |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2521 |
proof |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2522 |
assume ?P |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2523 |
moreover note take_bit_nat_less_exp [of n m] |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2524 |
ultimately show ?Q |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2525 |
by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2526 |
next |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2527 |
assume ?Q |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2528 |
then show ?P |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2529 |
by (simp add: take_bit_eq_mod) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2530 |
qed |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2531 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2532 |
lemma take_bit_nat_eq_self: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2533 |
\<open>take_bit n m = m\<close> if \<open>m < 2 ^ n\<close> for m n :: nat |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2534 |
using that by (simp add: take_bit_nat_eq_self_iff) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2535 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2536 |
lemma take_bit_nat_less_eq_self [simp]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2537 |
\<open>take_bit n m \<le> m\<close> for n m :: nat |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2538 |
by (simp add: take_bit_eq_mod) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2539 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2540 |
lemma take_bit_nat_less_self_iff: |
| 79017 | 2541 |
\<open>take_bit n m < m \<longleftrightarrow> 2 ^ n \<le> m\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) for m n :: nat |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2542 |
proof |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2543 |
assume ?P |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2544 |
then have \<open>take_bit n m \<noteq> m\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2545 |
by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2546 |
then show \<open>?Q\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2547 |
by (simp add: take_bit_nat_eq_self_iff) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2548 |
next |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2549 |
have \<open>take_bit n m < 2 ^ n\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2550 |
by (fact take_bit_nat_less_exp) |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2551 |
also assume ?Q |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2552 |
finally show ?P . |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2553 |
qed |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2554 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2555 |
lemma Suc_0_and_eq [simp]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2556 |
\<open>Suc 0 AND n = n mod 2\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2557 |
using one_and_eq [of n] by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2558 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2559 |
lemma and_Suc_0_eq [simp]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2560 |
\<open>n AND Suc 0 = n mod 2\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2561 |
using and_one_eq [of n] by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2562 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2563 |
lemma Suc_0_or_eq: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2564 |
\<open>Suc 0 OR n = n + of_bool (even n)\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2565 |
using one_or_eq [of n] by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2566 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2567 |
lemma or_Suc_0_eq: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2568 |
\<open>n OR Suc 0 = n + of_bool (even n)\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2569 |
using or_one_eq [of n] by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2570 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2571 |
lemma Suc_0_xor_eq: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2572 |
\<open>Suc 0 XOR n = n + of_bool (even n) - of_bool (odd n)\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2573 |
using one_xor_eq [of n] by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2574 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2575 |
lemma xor_Suc_0_eq: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2576 |
\<open>n XOR Suc 0 = n + of_bool (even n) - of_bool (odd n)\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2577 |
using xor_one_eq [of n] by simp |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2578 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2579 |
lemma and_nat_unfold [code]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2580 |
\<open>m AND n = (if m = 0 \<or> n = 0 then 0 else (m mod 2) * (n mod 2) + 2 * ((m div 2) AND (n div 2)))\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2581 |
for m n :: nat |
| 79070 | 2582 |
by (auto simp add: and_rec [of m n] elim: oddE) |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2583 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2584 |
lemma or_nat_unfold [code]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2585 |
\<open>m OR n = (if m = 0 then n else if n = 0 then m |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2586 |
else max (m mod 2) (n mod 2) + 2 * ((m div 2) OR (n div 2)))\<close> for m n :: nat |
| 79070 | 2587 |
by (auto simp add: or_rec [of m n] elim: oddE) |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2588 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2589 |
lemma xor_nat_unfold [code]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2590 |
\<open>m XOR n = (if m = 0 then n else if n = 0 then m |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2591 |
else (m mod 2 + n mod 2) mod 2 + 2 * ((m div 2) XOR (n div 2)))\<close> for m n :: nat |
| 79070 | 2592 |
by (auto simp add: xor_rec [of m n] elim!: oddE) |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2593 |
|
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2594 |
lemma [code]: |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2595 |
\<open>unset_bit 0 m = 2 * (m div 2)\<close> |
| 74163 | 2596 |
\<open>unset_bit (Suc n) m = m mod 2 + 2 * unset_bit n (m div 2)\<close> for m n :: nat |
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2597 |
by (simp_all add: unset_bit_Suc) |
| 74495 | 2598 |
|
| 74592 | 2599 |
lemma push_bit_of_Suc_0 [simp]: |
2600 |
\<open>push_bit n (Suc 0) = 2 ^ n\<close> |
|
2601 |
using push_bit_of_1 [where ?'a = nat] by simp |
|
2602 |
||
2603 |
lemma take_bit_of_Suc_0 [simp]: |
|
2604 |
\<open>take_bit n (Suc 0) = of_bool (0 < n)\<close> |
|
2605 |
using take_bit_of_1 [where ?'a = nat] by simp |
|
2606 |
||
2607 |
lemma drop_bit_of_Suc_0 [simp]: |
|
2608 |
\<open>drop_bit n (Suc 0) = of_bool (n = 0)\<close> |
|
2609 |
using drop_bit_of_1 [where ?'a = nat] by simp |
|
2610 |
||
2611 |
lemma Suc_mask_eq_exp: |
|
2612 |
\<open>Suc (mask n) = 2 ^ n\<close> |
|
2613 |
by (simp add: mask_eq_exp_minus_1) |
|
2614 |
||
2615 |
lemma less_eq_mask: |
|
2616 |
\<open>n \<le> mask n\<close> |
|
2617 |
by (simp add: mask_eq_exp_minus_1 le_diff_conv2) |
|
2618 |
(metis Suc_mask_eq_exp diff_Suc_1 diff_le_diff_pow diff_zero le_refl not_less_eq_eq power_0) |
|
2619 |
||
2620 |
lemma less_mask: |
|
2621 |
\<open>n < mask n\<close> if \<open>Suc 0 < n\<close> |
|
2622 |
proof - |
|
2623 |
define m where \<open>m = n - 2\<close> |
|
2624 |
with that have *: \<open>n = m + 2\<close> |
|
2625 |
by simp |
|
2626 |
have \<open>Suc (Suc (Suc m)) < 4 * 2 ^ m\<close> |
|
2627 |
by (induction m) simp_all |
|
2628 |
then have \<open>Suc (m + 2) < Suc (mask (m + 2))\<close> |
|
2629 |
by (simp add: Suc_mask_eq_exp) |
|
2630 |
then have \<open>m + 2 < mask (m + 2)\<close> |
|
2631 |
by (simp add: less_le) |
|
2632 |
with * show ?thesis |
|
2633 |
by simp |
|
2634 |
qed |
|
2635 |
||
2636 |
lemma mask_nat_less_exp [simp]: |
|
2637 |
\<open>(mask n :: nat) < 2 ^ n\<close> |
|
2638 |
by (simp add: mask_eq_exp_minus_1) |
|
2639 |
||
2640 |
lemma mask_nat_positive_iff [simp]: |
|
2641 |
\<open>(0::nat) < mask n \<longleftrightarrow> 0 < n\<close> |
|
2642 |
proof (cases \<open>n = 0\<close>) |
|
2643 |
case True |
|
2644 |
then show ?thesis |
|
2645 |
by simp |
|
2646 |
next |
|
2647 |
case False |
|
2648 |
then have \<open>0 < n\<close> |
|
2649 |
by simp |
|
2650 |
then have \<open>(0::nat) < mask n\<close> |
|
2651 |
using less_eq_mask [of n] by (rule order_less_le_trans) |
|
2652 |
with \<open>0 < n\<close> show ?thesis |
|
2653 |
by simp |
|
2654 |
qed |
|
2655 |
||
2656 |
lemma take_bit_tightened_less_eq_nat: |
|
2657 |
\<open>take_bit m q \<le> take_bit n q\<close> if \<open>m \<le> n\<close> for q :: nat |
|
2658 |
proof - |
|
2659 |
have \<open>take_bit m (take_bit n q) \<le> take_bit n q\<close> |
|
2660 |
by (rule take_bit_nat_less_eq_self) |
|
2661 |
with that show ?thesis |
|
2662 |
by simp |
|
2663 |
qed |
|
2664 |
||
2665 |
lemma push_bit_nat_eq: |
|
2666 |
\<open>push_bit n (nat k) = nat (push_bit n k)\<close> |
|
2667 |
by (cases \<open>k \<ge> 0\<close>) (simp_all add: push_bit_eq_mult nat_mult_distrib not_le mult_nonneg_nonpos2) |
|
2668 |
||
2669 |
lemma drop_bit_nat_eq: |
|
2670 |
\<open>drop_bit n (nat k) = nat (drop_bit n k)\<close> |
|
2671 |
apply (cases \<open>k \<ge> 0\<close>) |
|
2672 |
apply (simp_all add: drop_bit_eq_div nat_div_distrib nat_power_eq not_le) |
|
2673 |
apply (simp add: divide_int_def) |
|
2674 |
done |
|
2675 |
||
2676 |
lemma take_bit_nat_eq: |
|
2677 |
\<open>take_bit n (nat k) = nat (take_bit n k)\<close> if \<open>k \<ge> 0\<close> |
|
2678 |
using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq) |
|
2679 |
||
2680 |
lemma nat_take_bit_eq: |
|
2681 |
\<open>nat (take_bit n k) = take_bit n (nat k)\<close> |
|
2682 |
if \<open>k \<ge> 0\<close> |
|
2683 |
using that by (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq) |
|
2684 |
||
2685 |
lemma nat_mask_eq: |
|
2686 |
\<open>nat (mask n) = mask n\<close> |
|
2687 |
by (simp add: nat_eq_iff of_nat_mask_eq) |
|
2688 |
||
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
2689 |
|
| 74163 | 2690 |
subsection \<open>Symbolic computations on numeral expressions\<close> |
2691 |
||
| 75138 | 2692 |
context semiring_bits |
| 74163 | 2693 |
begin |
2694 |
||
| 75085 | 2695 |
lemma not_bit_numeral_Bit0_0 [simp]: |
2696 |
\<open>\<not> bit (numeral (Num.Bit0 m)) 0\<close> |
|
2697 |
by (simp add: bit_0) |
|
2698 |
||
2699 |
lemma bit_numeral_Bit1_0 [simp]: |
|
2700 |
\<open>bit (numeral (Num.Bit1 m)) 0\<close> |
|
2701 |
by (simp add: bit_0) |
|
2702 |
||
| 79590 | 2703 |
lemma bit_numeral_Bit0_iff: |
2704 |
\<open>bit (numeral (num.Bit0 m)) n |
|
2705 |
\<longleftrightarrow> possible_bit TYPE('a) n \<and> n > 0 \<and> bit (numeral m) (n - 1)\<close>
|
|
2706 |
by (simp only: numeral_Bit0_eq_double [of m] bit_simps) simp |
|
2707 |
||
2708 |
lemma bit_numeral_Bit1_Suc_iff: |
|
2709 |
\<open>bit (numeral (num.Bit1 m)) (Suc n) |
|
2710 |
\<longleftrightarrow> possible_bit TYPE('a) (Suc n) \<and> bit (numeral m) n\<close>
|
|
2711 |
using even_bit_succ_iff [of \<open>2 * numeral m\<close> \<open>Suc n\<close>] |
|
2712 |
by (simp only: numeral_Bit1_eq_inc_double [of m] bit_simps) simp |
|
2713 |
||
| 75138 | 2714 |
end |
2715 |
||
2716 |
context ring_bit_operations |
|
2717 |
begin |
|
2718 |
||
2719 |
lemma not_bit_minus_numeral_Bit0_0 [simp]: |
|
2720 |
\<open>\<not> bit (- numeral (Num.Bit0 m)) 0\<close> |
|
2721 |
by (simp add: bit_0) |
|
2722 |
||
2723 |
lemma bit_minus_numeral_Bit1_0 [simp]: |
|
2724 |
\<open>bit (- numeral (Num.Bit1 m)) 0\<close> |
|
2725 |
by (simp add: bit_0) |
|
2726 |
||
| 79590 | 2727 |
lemma bit_minus_numeral_Bit0_Suc_iff: |
2728 |
\<open>bit (- numeral (num.Bit0 m)) (Suc n) |
|
2729 |
\<longleftrightarrow> possible_bit TYPE('a) (Suc n) \<and> bit (- numeral m) n\<close>
|
|
2730 |
by (simp only: numeral_Bit0_eq_double [of m] minus_mult_right bit_simps) auto |
|
2731 |
||
2732 |
lemma bit_minus_numeral_Bit1_Suc_iff: |
|
2733 |
\<open>bit (- numeral (num.Bit1 m)) (Suc n) |
|
2734 |
\<longleftrightarrow> possible_bit TYPE('a) (Suc n) \<and> \<not> bit (numeral m) n\<close>
|
|
2735 |
by (simp only: numeral_Bit1_eq_inc_double [of m] minus_add_distrib minus_mult_right add_uminus_conv_diff |
|
2736 |
bit_decr_iff bit_double_iff) |
|
2737 |
auto |
|
2738 |
||
2739 |
lemma bit_numeral_BitM_0 [simp]: |
|
2740 |
\<open>bit (numeral (Num.BitM m)) 0\<close> |
|
2741 |
by (simp only: numeral_BitM bit_decr_iff not_bit_minus_numeral_Bit0_0) simp |
|
2742 |
||
2743 |
lemma bit_numeral_BitM_Suc_iff: |
|
2744 |
\<open>bit (numeral (Num.BitM m)) (Suc n) \<longleftrightarrow> possible_bit TYPE('a) (Suc n) \<and> \<not> bit (- numeral m) n\<close>
|
|
2745 |
by (simp_all only: numeral_BitM bit_decr_iff bit_minus_numeral_Bit0_Suc_iff) auto |
|
2746 |
||
| 75138 | 2747 |
end |
2748 |
||
| 78955 | 2749 |
context linordered_euclidean_semiring_bit_operations |
| 75138 | 2750 |
begin |
2751 |
||
2752 |
lemma bit_numeral_iff: |
|
2753 |
\<open>bit (numeral m) n \<longleftrightarrow> bit (numeral m :: nat) n\<close> |
|
2754 |
using bit_of_nat_iff_bit [of \<open>numeral m\<close> n] by simp |
|
2755 |
||
| 74163 | 2756 |
lemma bit_numeral_Bit0_Suc_iff [simp]: |
2757 |
\<open>bit (numeral (Num.Bit0 m)) (Suc n) \<longleftrightarrow> bit (numeral m) n\<close> |
|
2758 |
by (simp add: bit_Suc numeral_Bit0_div_2) |
|
2759 |
||
2760 |
lemma bit_numeral_Bit1_Suc_iff [simp]: |
|
2761 |
\<open>bit (numeral (Num.Bit1 m)) (Suc n) \<longleftrightarrow> bit (numeral m) n\<close> |
|
2762 |
by (simp add: bit_Suc numeral_Bit1_div_2) |
|
2763 |
||
2764 |
lemma bit_numeral_rec: |
|
2765 |
\<open>bit (numeral (Num.Bit0 w)) n \<longleftrightarrow> (case n of 0 \<Rightarrow> False | Suc m \<Rightarrow> bit (numeral w) m)\<close> |
|
2766 |
\<open>bit (numeral (Num.Bit1 w)) n \<longleftrightarrow> (case n of 0 \<Rightarrow> True | Suc m \<Rightarrow> bit (numeral w) m)\<close> |
|
| 75085 | 2767 |
by (cases n; simp add: bit_0)+ |
| 74163 | 2768 |
|
2769 |
lemma bit_numeral_simps [simp]: |
|
2770 |
\<open>\<not> bit 1 (numeral n)\<close> |
|
2771 |
\<open>bit (numeral (Num.Bit0 w)) (numeral n) \<longleftrightarrow> bit (numeral w) (pred_numeral n)\<close> |
|
2772 |
\<open>bit (numeral (Num.Bit1 w)) (numeral n) \<longleftrightarrow> bit (numeral w) (pred_numeral n)\<close> |
|
2773 |
by (simp_all add: bit_1_iff numeral_eq_Suc) |
|
2774 |
||
2775 |
lemma and_numerals [simp]: |
|
2776 |
\<open>1 AND numeral (Num.Bit0 y) = 0\<close> |
|
2777 |
\<open>1 AND numeral (Num.Bit1 y) = 1\<close> |
|
2778 |
\<open>numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = 2 * (numeral x AND numeral y)\<close> |
|
2779 |
\<open>numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = 2 * (numeral x AND numeral y)\<close> |
|
2780 |
\<open>numeral (Num.Bit0 x) AND 1 = 0\<close> |
|
2781 |
\<open>numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = 2 * (numeral x AND numeral y)\<close> |
|
2782 |
\<open>numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = 1 + 2 * (numeral x AND numeral y)\<close> |
|
2783 |
\<open>numeral (Num.Bit1 x) AND 1 = 1\<close> |
|
| 75085 | 2784 |
by (simp_all add: bit_eq_iff) (simp_all add: bit_0 bit_simps bit_Suc bit_numeral_rec split: nat.splits) |
| 74163 | 2785 |
|
2786 |
lemma or_numerals [simp]: |
|
2787 |
\<open>1 OR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)\<close> |
|
2788 |
\<open>1 OR numeral (Num.Bit1 y) = numeral (Num.Bit1 y)\<close> |
|
2789 |
\<open>numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = 2 * (numeral x OR numeral y)\<close> |
|
2790 |
\<open>numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = 1 + 2 * (numeral x OR numeral y)\<close> |
|
2791 |
\<open>numeral (Num.Bit0 x) OR 1 = numeral (Num.Bit1 x)\<close> |
|
2792 |
\<open>numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = 1 + 2 * (numeral x OR numeral y)\<close> |
|
2793 |
\<open>numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = 1 + 2 * (numeral x OR numeral y)\<close> |
|
2794 |
\<open>numeral (Num.Bit1 x) OR 1 = numeral (Num.Bit1 x)\<close> |
|
| 75085 | 2795 |
by (simp_all add: bit_eq_iff) (simp_all add: bit_0 bit_simps bit_Suc bit_numeral_rec split: nat.splits) |
| 74163 | 2796 |
|
2797 |
lemma xor_numerals [simp]: |
|
2798 |
\<open>1 XOR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)\<close> |
|
2799 |
\<open>1 XOR numeral (Num.Bit1 y) = numeral (Num.Bit0 y)\<close> |
|
2800 |
\<open>numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = 2 * (numeral x XOR numeral y)\<close> |
|
2801 |
\<open>numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = 1 + 2 * (numeral x XOR numeral y)\<close> |
|
2802 |
\<open>numeral (Num.Bit0 x) XOR 1 = numeral (Num.Bit1 x)\<close> |
|
2803 |
\<open>numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = 1 + 2 * (numeral x XOR numeral y)\<close> |
|
2804 |
\<open>numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = 2 * (numeral x XOR numeral y)\<close> |
|
2805 |
\<open>numeral (Num.Bit1 x) XOR 1 = numeral (Num.Bit0 x)\<close> |
|
| 75085 | 2806 |
by (simp_all add: bit_eq_iff) (simp_all add: bit_0 bit_simps bit_Suc bit_numeral_rec split: nat.splits) |
| 74163 | 2807 |
|
2808 |
end |
|
2809 |
||
| 79017 | 2810 |
lemma drop_bit_Suc_minus_bit0 [simp]: |
2811 |
\<open>drop_bit (Suc n) (- numeral (Num.Bit0 k)) = drop_bit n (- numeral k :: int)\<close> |
|
2812 |
by (simp add: drop_bit_Suc numeral_Bit0_div_2) |
|
2813 |
||
2814 |
lemma drop_bit_Suc_minus_bit1 [simp]: |
|
2815 |
\<open>drop_bit (Suc n) (- numeral (Num.Bit1 k)) = drop_bit n (- numeral (Num.inc k) :: int)\<close> |
|
2816 |
by (simp add: drop_bit_Suc numeral_Bit1_div_2 add_One) |
|
2817 |
||
2818 |
lemma drop_bit_numeral_minus_bit0 [simp]: |
|
2819 |
\<open>drop_bit (numeral l) (- numeral (Num.Bit0 k)) = drop_bit (pred_numeral l) (- numeral k :: int)\<close> |
|
2820 |
by (simp add: numeral_eq_Suc numeral_Bit0_div_2) |
|
2821 |
||
2822 |
lemma drop_bit_numeral_minus_bit1 [simp]: |
|
2823 |
\<open>drop_bit (numeral l) (- numeral (Num.Bit1 k)) = drop_bit (pred_numeral l) (- numeral (Num.inc k) :: int)\<close> |
|
2824 |
by (simp add: numeral_eq_Suc numeral_Bit1_div_2) |
|
2825 |
||
2826 |
lemma take_bit_Suc_minus_bit0: |
|
2827 |
\<open>take_bit (Suc n) (- numeral (Num.Bit0 k)) = take_bit n (- numeral k) * (2 :: int)\<close> |
|
2828 |
by (simp add: take_bit_Suc numeral_Bit0_div_2) |
|
2829 |
||
2830 |
lemma take_bit_Suc_minus_bit1: |
|
2831 |
\<open>take_bit (Suc n) (- numeral (Num.Bit1 k)) = take_bit n (- numeral (Num.inc k)) * 2 + (1 :: int)\<close> |
|
2832 |
by (simp add: take_bit_Suc numeral_Bit1_div_2 add_One) |
|
2833 |
||
2834 |
lemma take_bit_numeral_minus_bit0: |
|
2835 |
\<open>take_bit (numeral l) (- numeral (Num.Bit0 k)) = take_bit (pred_numeral l) (- numeral k) * (2 :: int)\<close> |
|
2836 |
by (simp add: numeral_eq_Suc numeral_Bit0_div_2 take_bit_Suc_minus_bit0) |
|
2837 |
||
2838 |
lemma take_bit_numeral_minus_bit1: |
|
2839 |
\<open>take_bit (numeral l) (- numeral (Num.Bit1 k)) = take_bit (pred_numeral l) (- numeral (Num.inc k)) * 2 + (1 :: int)\<close> |
|
2840 |
by (simp add: numeral_eq_Suc numeral_Bit1_div_2 take_bit_Suc_minus_bit1) |
|
2841 |
||
| 74495 | 2842 |
lemma and_nat_numerals [simp]: |
2843 |
\<open>Suc 0 AND numeral (Num.Bit0 y) = 0\<close> |
|
2844 |
\<open>Suc 0 AND numeral (Num.Bit1 y) = 1\<close> |
|
2845 |
\<open>numeral (Num.Bit0 x) AND Suc 0 = 0\<close> |
|
2846 |
\<open>numeral (Num.Bit1 x) AND Suc 0 = 1\<close> |
|
2847 |
by (simp_all only: and_numerals flip: One_nat_def) |
|
2848 |
||
2849 |
lemma or_nat_numerals [simp]: |
|
2850 |
\<open>Suc 0 OR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)\<close> |
|
2851 |
\<open>Suc 0 OR numeral (Num.Bit1 y) = numeral (Num.Bit1 y)\<close> |
|
2852 |
\<open>numeral (Num.Bit0 x) OR Suc 0 = numeral (Num.Bit1 x)\<close> |
|
2853 |
\<open>numeral (Num.Bit1 x) OR Suc 0 = numeral (Num.Bit1 x)\<close> |
|
2854 |
by (simp_all only: or_numerals flip: One_nat_def) |
|
2855 |
||
2856 |
lemma xor_nat_numerals [simp]: |
|
2857 |
\<open>Suc 0 XOR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)\<close> |
|
2858 |
\<open>Suc 0 XOR numeral (Num.Bit1 y) = numeral (Num.Bit0 y)\<close> |
|
2859 |
\<open>numeral (Num.Bit0 x) XOR Suc 0 = numeral (Num.Bit1 x)\<close> |
|
2860 |
\<open>numeral (Num.Bit1 x) XOR Suc 0 = numeral (Num.Bit0 x)\<close> |
|
2861 |
by (simp_all only: xor_numerals flip: One_nat_def) |
|
2862 |
||
| 74163 | 2863 |
context ring_bit_operations |
2864 |
begin |
|
2865 |
||
2866 |
lemma minus_numeral_inc_eq: |
|
2867 |
\<open>- numeral (Num.inc n) = NOT (numeral n)\<close> |
|
2868 |
by (simp add: not_eq_complement sub_inc_One_eq add_One) |
|
2869 |
||
2870 |
lemma sub_one_eq_not_neg: |
|
2871 |
\<open>Num.sub n num.One = NOT (- numeral n)\<close> |
|
2872 |
by (simp add: not_eq_complement) |
|
2873 |
||
2874 |
lemma minus_numeral_eq_not_sub_one: |
|
2875 |
\<open>- numeral n = NOT (Num.sub n num.One)\<close> |
|
2876 |
by (simp add: not_eq_complement) |
|
2877 |
||
| 74495 | 2878 |
lemma not_numeral_eq [simp]: |
| 74163 | 2879 |
\<open>NOT (numeral n) = - numeral (Num.inc n)\<close> |
2880 |
by (simp add: minus_numeral_inc_eq) |
|
2881 |
||
2882 |
lemma not_minus_numeral_eq [simp]: |
|
2883 |
\<open>NOT (- numeral n) = Num.sub n num.One\<close> |
|
2884 |
by (simp add: sub_one_eq_not_neg) |
|
2885 |
||
2886 |
lemma minus_not_numeral_eq [simp]: |
|
2887 |
\<open>- (NOT (numeral n)) = numeral (Num.inc n)\<close> |
|
| 74495 | 2888 |
by simp |
2889 |
||
2890 |
lemma not_numeral_BitM_eq: |
|
2891 |
\<open>NOT (numeral (Num.BitM n)) = - numeral (num.Bit0 n)\<close> |
|
| 79068 | 2892 |
by (simp add: inc_BitM_eq) |
| 74495 | 2893 |
|
2894 |
lemma not_numeral_Bit0_eq: |
|
2895 |
\<open>NOT (numeral (Num.Bit0 n)) = - numeral (num.Bit1 n)\<close> |
|
2896 |
by simp |
|
| 74163 | 2897 |
|
2898 |
end |
|
2899 |
||
2900 |
lemma bit_minus_numeral_int [simp]: |
|
2901 |
\<open>bit (- numeral (num.Bit0 w) :: int) (numeral n) \<longleftrightarrow> bit (- numeral w :: int) (pred_numeral n)\<close> |
|
2902 |
\<open>bit (- numeral (num.Bit1 w) :: int) (numeral n) \<longleftrightarrow> \<not> bit (numeral w :: int) (pred_numeral n)\<close> |
|
2903 |
by (simp_all add: bit_minus_iff bit_not_iff numeral_eq_Suc bit_Suc add_One sub_inc_One_eq) |
|
2904 |
||
| 74592 | 2905 |
lemma bit_minus_numeral_Bit0_Suc_iff [simp]: |
2906 |
\<open>bit (- numeral (num.Bit0 w) :: int) (Suc n) \<longleftrightarrow> bit (- numeral w :: int) n\<close> |
|
2907 |
by (simp add: bit_Suc) |
|
2908 |
||
2909 |
lemma bit_minus_numeral_Bit1_Suc_iff [simp]: |
|
2910 |
\<open>bit (- numeral (num.Bit1 w) :: int) (Suc n) \<longleftrightarrow> \<not> bit (numeral w :: int) n\<close> |
|
2911 |
by (simp add: bit_Suc add_One flip: bit_not_int_iff) |
|
2912 |
||
| 74495 | 2913 |
lemma and_not_numerals: |
| 74163 | 2914 |
\<open>1 AND NOT 1 = (0 :: int)\<close> |
2915 |
\<open>1 AND NOT (numeral (Num.Bit0 n)) = (1 :: int)\<close> |
|
2916 |
\<open>1 AND NOT (numeral (Num.Bit1 n)) = (0 :: int)\<close> |
|
2917 |
\<open>numeral (Num.Bit0 m) AND NOT (1 :: int) = numeral (Num.Bit0 m)\<close> |
|
2918 |
\<open>numeral (Num.Bit0 m) AND NOT (numeral (Num.Bit0 n)) = (2 :: int) * (numeral m AND NOT (numeral n))\<close> |
|
2919 |
\<open>numeral (Num.Bit0 m) AND NOT (numeral (Num.Bit1 n)) = (2 :: int) * (numeral m AND NOT (numeral n))\<close> |
|
2920 |
\<open>numeral (Num.Bit1 m) AND NOT (1 :: int) = numeral (Num.Bit0 m)\<close> |
|
2921 |
\<open>numeral (Num.Bit1 m) AND NOT (numeral (Num.Bit0 n)) = 1 + (2 :: int) * (numeral m AND NOT (numeral n))\<close> |
|
2922 |
\<open>numeral (Num.Bit1 m) AND NOT (numeral (Num.Bit1 n)) = (2 :: int) * (numeral m AND NOT (numeral n))\<close> |
|
| 75085 | 2923 |
by (simp_all add: bit_eq_iff) (auto simp add: bit_0 bit_simps bit_Suc bit_numeral_rec BitM_inc_eq sub_inc_One_eq split: nat.split) |
| 74163 | 2924 |
|
2925 |
fun and_not_num :: \<open>num \<Rightarrow> num \<Rightarrow> num option\<close> \<^marker>\<open>contributor \<open>Andreas Lochbihler\<close>\<close> |
|
2926 |
where |
|
2927 |
\<open>and_not_num num.One num.One = None\<close> |
|
2928 |
| \<open>and_not_num num.One (num.Bit0 n) = Some num.One\<close> |
|
2929 |
| \<open>and_not_num num.One (num.Bit1 n) = None\<close> |
|
2930 |
| \<open>and_not_num (num.Bit0 m) num.One = Some (num.Bit0 m)\<close> |
|
2931 |
| \<open>and_not_num (num.Bit0 m) (num.Bit0 n) = map_option num.Bit0 (and_not_num m n)\<close> |
|
2932 |
| \<open>and_not_num (num.Bit0 m) (num.Bit1 n) = map_option num.Bit0 (and_not_num m n)\<close> |
|
2933 |
| \<open>and_not_num (num.Bit1 m) num.One = Some (num.Bit0 m)\<close> |
|
2934 |
| \<open>and_not_num (num.Bit1 m) (num.Bit0 n) = (case and_not_num m n of None \<Rightarrow> Some num.One | Some n' \<Rightarrow> Some (num.Bit1 n'))\<close> |
|
2935 |
| \<open>and_not_num (num.Bit1 m) (num.Bit1 n) = map_option num.Bit0 (and_not_num m n)\<close> |
|
2936 |
||
2937 |
lemma int_numeral_and_not_num: |
|
2938 |
\<open>numeral m AND NOT (numeral n) = (case and_not_num m n of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close> |
|
| 74495 | 2939 |
by (induction m n rule: and_not_num.induct) (simp_all del: not_numeral_eq not_one_eq add: and_not_numerals split: option.splits) |
| 74163 | 2940 |
|
2941 |
lemma int_numeral_not_and_num: |
|
2942 |
\<open>NOT (numeral m) AND numeral n = (case and_not_num n m of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close> |
|
2943 |
using int_numeral_and_not_num [of n m] by (simp add: ac_simps) |
|
2944 |
||
2945 |
lemma and_not_num_eq_None_iff: |
|
2946 |
\<open>and_not_num m n = None \<longleftrightarrow> numeral m AND NOT (numeral n) = (0 :: int)\<close> |
|
| 74495 | 2947 |
by (simp del: not_numeral_eq add: int_numeral_and_not_num split: option.split) |
| 74163 | 2948 |
|
2949 |
lemma and_not_num_eq_Some_iff: |
|
2950 |
\<open>and_not_num m n = Some q \<longleftrightarrow> numeral m AND NOT (numeral n) = (numeral q :: int)\<close> |
|
| 74495 | 2951 |
by (simp del: not_numeral_eq add: int_numeral_and_not_num split: option.split) |
2952 |
||
2953 |
lemma and_minus_numerals [simp]: |
|
2954 |
\<open>1 AND - (numeral (num.Bit0 n)) = (0::int)\<close> |
|
2955 |
\<open>1 AND - (numeral (num.Bit1 n)) = (1::int)\<close> |
|
2956 |
\<open>numeral m AND - (numeral (num.Bit0 n)) = (case and_not_num m (Num.BitM n) of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close> |
|
2957 |
\<open>numeral m AND - (numeral (num.Bit1 n)) = (case and_not_num m (Num.Bit0 n) of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close> |
|
2958 |
\<open>- (numeral (num.Bit0 n)) AND 1 = (0::int)\<close> |
|
2959 |
\<open>- (numeral (num.Bit1 n)) AND 1 = (1::int)\<close> |
|
2960 |
\<open>- (numeral (num.Bit0 n)) AND numeral m = (case and_not_num m (Num.BitM n) of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close> |
|
2961 |
\<open>- (numeral (num.Bit1 n)) AND numeral m = (case and_not_num m (Num.Bit0 n) of None \<Rightarrow> 0 :: int | Some n' \<Rightarrow> numeral n')\<close> |
|
2962 |
by (simp_all del: not_numeral_eq add: ac_simps |
|
2963 |
and_not_numerals one_and_eq not_numeral_BitM_eq not_numeral_Bit0_eq and_not_num_eq_None_iff and_not_num_eq_Some_iff split: option.split) |
|
2964 |
||
2965 |
lemma and_minus_minus_numerals [simp]: |
|
2966 |
\<open>- (numeral m :: int) AND - (numeral n :: int) = NOT ((numeral m - 1) OR (numeral n - 1))\<close> |
|
2967 |
by (simp add: minus_numeral_eq_not_sub_one) |
|
2968 |
||
2969 |
lemma or_not_numerals: |
|
| 74163 | 2970 |
\<open>1 OR NOT 1 = NOT (0 :: int)\<close> |
2971 |
\<open>1 OR NOT (numeral (Num.Bit0 n)) = NOT (numeral (Num.Bit0 n) :: int)\<close> |
|
2972 |
\<open>1 OR NOT (numeral (Num.Bit1 n)) = NOT (numeral (Num.Bit0 n) :: int)\<close> |
|
2973 |
\<open>numeral (Num.Bit0 m) OR NOT (1 :: int) = NOT (1 :: int)\<close> |
|
2974 |
\<open>numeral (Num.Bit0 m) OR NOT (numeral (Num.Bit0 n)) = 1 + (2 :: int) * (numeral m OR NOT (numeral n))\<close> |
|
2975 |
\<open>numeral (Num.Bit0 m) OR NOT (numeral (Num.Bit1 n)) = (2 :: int) * (numeral m OR NOT (numeral n))\<close> |
|
2976 |
\<open>numeral (Num.Bit1 m) OR NOT (1 :: int) = NOT (0 :: int)\<close> |
|
2977 |
\<open>numeral (Num.Bit1 m) OR NOT (numeral (Num.Bit0 n)) = 1 + (2 :: int) * (numeral m OR NOT (numeral n))\<close> |
|
2978 |
\<open>numeral (Num.Bit1 m) OR NOT (numeral (Num.Bit1 n)) = 1 + (2 :: int) * (numeral m OR NOT (numeral n))\<close> |
|
| 74495 | 2979 |
by (simp_all add: bit_eq_iff) |
| 75085 | 2980 |
(auto simp add: bit_0 bit_simps bit_Suc bit_numeral_rec sub_inc_One_eq split: nat.split) |
| 74163 | 2981 |
|
2982 |
fun or_not_num_neg :: \<open>num \<Rightarrow> num \<Rightarrow> num\<close> \<^marker>\<open>contributor \<open>Andreas Lochbihler\<close>\<close> |
|
2983 |
where |
|
2984 |
\<open>or_not_num_neg num.One num.One = num.One\<close> |
|
2985 |
| \<open>or_not_num_neg num.One (num.Bit0 m) = num.Bit1 m\<close> |
|
2986 |
| \<open>or_not_num_neg num.One (num.Bit1 m) = num.Bit1 m\<close> |
|
2987 |
| \<open>or_not_num_neg (num.Bit0 n) num.One = num.Bit0 num.One\<close> |
|
2988 |
| \<open>or_not_num_neg (num.Bit0 n) (num.Bit0 m) = Num.BitM (or_not_num_neg n m)\<close> |
|
2989 |
| \<open>or_not_num_neg (num.Bit0 n) (num.Bit1 m) = num.Bit0 (or_not_num_neg n m)\<close> |
|
2990 |
| \<open>or_not_num_neg (num.Bit1 n) num.One = num.One\<close> |
|
2991 |
| \<open>or_not_num_neg (num.Bit1 n) (num.Bit0 m) = Num.BitM (or_not_num_neg n m)\<close> |
|
2992 |
| \<open>or_not_num_neg (num.Bit1 n) (num.Bit1 m) = Num.BitM (or_not_num_neg n m)\<close> |
|
2993 |
||
2994 |
lemma int_numeral_or_not_num_neg: |
|
2995 |
\<open>numeral m OR NOT (numeral n :: int) = - numeral (or_not_num_neg m n)\<close> |
|
| 74495 | 2996 |
by (induction m n rule: or_not_num_neg.induct) (simp_all del: not_numeral_eq not_one_eq add: or_not_numerals, simp_all) |
| 74163 | 2997 |
|
2998 |
lemma int_numeral_not_or_num_neg: |
|
2999 |
\<open>NOT (numeral m) OR (numeral n :: int) = - numeral (or_not_num_neg n m)\<close> |
|
3000 |
using int_numeral_or_not_num_neg [of n m] by (simp add: ac_simps) |
|
3001 |
||
3002 |
lemma numeral_or_not_num_eq: |
|
3003 |
\<open>numeral (or_not_num_neg m n) = - (numeral m OR NOT (numeral n :: int))\<close> |
|
3004 |
using int_numeral_or_not_num_neg [of m n] by simp |
|
3005 |
||
| 74495 | 3006 |
lemma or_minus_numerals [simp]: |
3007 |
\<open>1 OR - (numeral (num.Bit0 n)) = - (numeral (or_not_num_neg num.One (Num.BitM n)) :: int)\<close> |
|
3008 |
\<open>1 OR - (numeral (num.Bit1 n)) = - (numeral (num.Bit1 n) :: int)\<close> |
|
3009 |
\<open>numeral m OR - (numeral (num.Bit0 n)) = - (numeral (or_not_num_neg m (Num.BitM n)) :: int)\<close> |
|
3010 |
\<open>numeral m OR - (numeral (num.Bit1 n)) = - (numeral (or_not_num_neg m (Num.Bit0 n)) :: int)\<close> |
|
3011 |
\<open>- (numeral (num.Bit0 n)) OR 1 = - (numeral (or_not_num_neg num.One (Num.BitM n)) :: int)\<close> |
|
3012 |
\<open>- (numeral (num.Bit1 n)) OR 1 = - (numeral (num.Bit1 n) :: int)\<close> |
|
3013 |
\<open>- (numeral (num.Bit0 n)) OR numeral m = - (numeral (or_not_num_neg m (Num.BitM n)) :: int)\<close> |
|
3014 |
\<open>- (numeral (num.Bit1 n)) OR numeral m = - (numeral (or_not_num_neg m (Num.Bit0 n)) :: int)\<close> |
|
3015 |
by (simp_all only: or.commute [of _ 1] or.commute [of _ \<open>numeral m\<close>] |
|
3016 |
minus_numeral_eq_not_sub_one or_not_numerals |
|
3017 |
numeral_or_not_num_eq arith_simps minus_minus numeral_One) |
|
3018 |
||
3019 |
lemma or_minus_minus_numerals [simp]: |
|
3020 |
\<open>- (numeral m :: int) OR - (numeral n :: int) = NOT ((numeral m - 1) AND (numeral n - 1))\<close> |
|
3021 |
by (simp add: minus_numeral_eq_not_sub_one) |
|
3022 |
||
| 74163 | 3023 |
lemma xor_minus_numerals [simp]: |
3024 |
\<open>- numeral n XOR k = NOT (neg_numeral_class.sub n num.One XOR k)\<close> |
|
3025 |
\<open>k XOR - numeral n = NOT (k XOR (neg_numeral_class.sub n num.One))\<close> for k :: int |
|
3026 |
by (simp_all add: minus_numeral_eq_not_sub_one) |
|
3027 |
||
| 74592 | 3028 |
definition take_bit_num :: \<open>nat \<Rightarrow> num \<Rightarrow> num option\<close> |
3029 |
where \<open>take_bit_num n m = |
|
|
75651
f4116b7a6679
Move code lemmas for symbolic computation of bit operations on int to distribution.
haftmann
parents:
75138
diff
changeset
|
3030 |
(if take_bit n (numeral m :: nat) = 0 then None else Some (num_of_nat (take_bit n (numeral m :: nat))))\<close> |
| 74592 | 3031 |
|
| 74618 | 3032 |
lemma take_bit_num_simps: |
| 74592 | 3033 |
\<open>take_bit_num 0 m = None\<close> |
3034 |
\<open>take_bit_num (Suc n) Num.One = |
|
3035 |
Some Num.One\<close> |
|
3036 |
\<open>take_bit_num (Suc n) (Num.Bit0 m) = |
|
3037 |
(case take_bit_num n m of None \<Rightarrow> None | Some q \<Rightarrow> Some (Num.Bit0 q))\<close> |
|
3038 |
\<open>take_bit_num (Suc n) (Num.Bit1 m) = |
|
3039 |
Some (case take_bit_num n m of None \<Rightarrow> Num.One | Some q \<Rightarrow> Num.Bit1 q)\<close> |
|
| 74618 | 3040 |
\<open>take_bit_num (numeral r) Num.One = |
| 74592 | 3041 |
Some Num.One\<close> |
| 74618 | 3042 |
\<open>take_bit_num (numeral r) (Num.Bit0 m) = |
3043 |
(case take_bit_num (pred_numeral r) m of None \<Rightarrow> None | Some q \<Rightarrow> Some (Num.Bit0 q))\<close> |
|
3044 |
\<open>take_bit_num (numeral r) (Num.Bit1 m) = |
|
3045 |
Some (case take_bit_num (pred_numeral r) m of None \<Rightarrow> Num.One | Some q \<Rightarrow> Num.Bit1 q)\<close> |
|
| 74592 | 3046 |
by (auto simp add: take_bit_num_def ac_simps mult_2 num_of_nat_double |
| 74618 | 3047 |
take_bit_Suc_bit0 take_bit_Suc_bit1 take_bit_numeral_bit0 take_bit_numeral_bit1) |
3048 |
||
3049 |
lemma take_bit_num_code [code]: |
|
3050 |
\<comment> \<open>Ocaml-style pattern matching is more robust wrt. different representations of \<^typ>\<open>nat\<close>\<close> |
|
3051 |
\<open>take_bit_num n m = (case (n, m) |
|
3052 |
of (0, _) \<Rightarrow> None |
|
3053 |
| (Suc n, Num.One) \<Rightarrow> Some Num.One |
|
3054 |
| (Suc n, Num.Bit0 m) \<Rightarrow> (case take_bit_num n m of None \<Rightarrow> None | Some q \<Rightarrow> Some (Num.Bit0 q)) |
|
3055 |
| (Suc n, Num.Bit1 m) \<Rightarrow> Some (case take_bit_num n m of None \<Rightarrow> Num.One | Some q \<Rightarrow> Num.Bit1 q))\<close> |
|
3056 |
by (cases n; cases m) (simp_all add: take_bit_num_simps) |
|
| 74592 | 3057 |
|
3058 |
context semiring_bit_operations |
|
3059 |
begin |
|
3060 |
||
3061 |
lemma take_bit_num_eq_None_imp: |
|
3062 |
\<open>take_bit m (numeral n) = 0\<close> if \<open>take_bit_num m n = None\<close> |
|
3063 |
proof - |
|
3064 |
from that have \<open>take_bit m (numeral n :: nat) = 0\<close> |
|
3065 |
by (simp add: take_bit_num_def split: if_splits) |
|
3066 |
then have \<open>of_nat (take_bit m (numeral n)) = of_nat 0\<close> |
|
3067 |
by simp |
|
3068 |
then show ?thesis |
|
3069 |
by (simp add: of_nat_take_bit) |
|
3070 |
qed |
|
| 79068 | 3071 |
|
| 74592 | 3072 |
lemma take_bit_num_eq_Some_imp: |
3073 |
\<open>take_bit m (numeral n) = numeral q\<close> if \<open>take_bit_num m n = Some q\<close> |
|
3074 |
proof - |
|
3075 |
from that have \<open>take_bit m (numeral n :: nat) = numeral q\<close> |
|
3076 |
by (auto simp add: take_bit_num_def Num.numeral_num_of_nat_unfold split: if_splits) |
|
3077 |
then have \<open>of_nat (take_bit m (numeral n)) = of_nat (numeral q)\<close> |
|
3078 |
by simp |
|
3079 |
then show ?thesis |
|
3080 |
by (simp add: of_nat_take_bit) |
|
3081 |
qed |
|
3082 |
||
3083 |
lemma take_bit_numeral_numeral: |
|
3084 |
\<open>take_bit (numeral m) (numeral n) = |
|
3085 |
(case take_bit_num (numeral m) n of None \<Rightarrow> 0 | Some q \<Rightarrow> numeral q)\<close> |
|
3086 |
by (auto split: option.split dest: take_bit_num_eq_None_imp take_bit_num_eq_Some_imp) |
|
3087 |
||
3088 |
end |
|
3089 |
||
3090 |
lemma take_bit_numeral_minus_numeral_int: |
|
3091 |
\<open>take_bit (numeral m) (- numeral n :: int) = |
|
3092 |
(case take_bit_num (numeral m) n of None \<Rightarrow> 0 | Some q \<Rightarrow> take_bit (numeral m) (2 ^ numeral m - numeral q))\<close> (is \<open>?lhs = ?rhs\<close>) |
|
3093 |
proof (cases \<open>take_bit_num (numeral m) n\<close>) |
|
3094 |
case None |
|
3095 |
then show ?thesis |
|
3096 |
by (auto dest: take_bit_num_eq_None_imp [where ?'a = int] simp add: take_bit_eq_0_iff) |
|
3097 |
next |
|
3098 |
case (Some q) |
|
3099 |
then have q: \<open>take_bit (numeral m) (numeral n :: int) = numeral q\<close> |
|
3100 |
by (auto dest: take_bit_num_eq_Some_imp) |
|
3101 |
let ?T = \<open>take_bit (numeral m) :: int \<Rightarrow> int\<close> |
|
3102 |
have *: \<open>?T (2 ^ numeral m) = ?T (?T 0)\<close> |
|
3103 |
by (simp add: take_bit_eq_0_iff) |
|
3104 |
have \<open>?lhs = ?T (0 - numeral n)\<close> |
|
3105 |
by simp |
|
3106 |
also have \<open>\<dots> = ?T (?T (?T 0) - ?T (?T (numeral n)))\<close> |
|
3107 |
by (simp only: take_bit_diff) |
|
3108 |
also have \<open>\<dots> = ?T (2 ^ numeral m - ?T (numeral n))\<close> |
|
3109 |
by (simp only: take_bit_diff flip: *) |
|
3110 |
also have \<open>\<dots> = ?rhs\<close> |
|
3111 |
by (simp add: q Some) |
|
3112 |
finally show ?thesis . |
|
3113 |
qed |
|
3114 |
||
| 74618 | 3115 |
declare take_bit_num_simps [simp] |
3116 |
take_bit_numeral_numeral [simp] |
|
| 74592 | 3117 |
take_bit_numeral_minus_numeral_int [simp] |
3118 |
||
| 74163 | 3119 |
|
| 79069 | 3120 |
subsection \<open>Symbolic computations for code generation\<close> |
3121 |
||
3122 |
lemma bit_int_code [code]: |
|
3123 |
\<open>bit (0::int) n \<longleftrightarrow> False\<close> |
|
3124 |
\<open>bit (Int.Neg num.One) n \<longleftrightarrow> True\<close> |
|
3125 |
\<open>bit (Int.Pos num.One) 0 \<longleftrightarrow> True\<close> |
|
3126 |
\<open>bit (Int.Pos (num.Bit0 m)) 0 \<longleftrightarrow> False\<close> |
|
3127 |
\<open>bit (Int.Pos (num.Bit1 m)) 0 \<longleftrightarrow> True\<close> |
|
3128 |
\<open>bit (Int.Neg (num.Bit0 m)) 0 \<longleftrightarrow> False\<close> |
|
3129 |
\<open>bit (Int.Neg (num.Bit1 m)) 0 \<longleftrightarrow> True\<close> |
|
3130 |
\<open>bit (Int.Pos num.One) (Suc n) \<longleftrightarrow> False\<close> |
|
3131 |
\<open>bit (Int.Pos (num.Bit0 m)) (Suc n) \<longleftrightarrow> bit (Int.Pos m) n\<close> |
|
3132 |
\<open>bit (Int.Pos (num.Bit1 m)) (Suc n) \<longleftrightarrow> bit (Int.Pos m) n\<close> |
|
3133 |
\<open>bit (Int.Neg (num.Bit0 m)) (Suc n) \<longleftrightarrow> bit (Int.Neg m) n\<close> |
|
3134 |
\<open>bit (Int.Neg (num.Bit1 m)) (Suc n) \<longleftrightarrow> bit (Int.Neg (Num.inc m)) n\<close> |
|
3135 |
by (simp_all add: Num.add_One bit_0 bit_Suc) |
|
3136 |
||
3137 |
lemma not_int_code [code]: |
|
3138 |
\<open>NOT (0 :: int) = - 1\<close> |
|
3139 |
\<open>NOT (Int.Pos n) = Int.Neg (Num.inc n)\<close> |
|
3140 |
\<open>NOT (Int.Neg n) = Num.sub n num.One\<close> |
|
3141 |
by (simp_all add: Num.add_One not_int_def) |
|
3142 |
||
3143 |
fun and_num :: \<open>num \<Rightarrow> num \<Rightarrow> num option\<close> \<^marker>\<open>contributor \<open>Andreas Lochbihler\<close>\<close> |
|
3144 |
where |
|
3145 |
\<open>and_num num.One num.One = Some num.One\<close> |
|
3146 |
| \<open>and_num num.One (num.Bit0 n) = None\<close> |
|
3147 |
| \<open>and_num num.One (num.Bit1 n) = Some num.One\<close> |
|
3148 |
| \<open>and_num (num.Bit0 m) num.One = None\<close> |
|
3149 |
| \<open>and_num (num.Bit0 m) (num.Bit0 n) = map_option num.Bit0 (and_num m n)\<close> |
|
3150 |
| \<open>and_num (num.Bit0 m) (num.Bit1 n) = map_option num.Bit0 (and_num m n)\<close> |
|
3151 |
| \<open>and_num (num.Bit1 m) num.One = Some num.One\<close> |
|
3152 |
| \<open>and_num (num.Bit1 m) (num.Bit0 n) = map_option num.Bit0 (and_num m n)\<close> |
|
3153 |
| \<open>and_num (num.Bit1 m) (num.Bit1 n) = (case and_num m n of None \<Rightarrow> Some num.One | Some n' \<Rightarrow> Some (num.Bit1 n'))\<close> |
|
3154 |
||
3155 |
context linordered_euclidean_semiring_bit_operations |
|
3156 |
begin |
|
3157 |
||
3158 |
lemma numeral_and_num: |
|
3159 |
\<open>numeral m AND numeral n = (case and_num m n of None \<Rightarrow> 0 | Some n' \<Rightarrow> numeral n')\<close> |
|
3160 |
by (induction m n rule: and_num.induct) (simp_all add: split: option.split) |
|
3161 |
||
3162 |
lemma and_num_eq_None_iff: |
|
3163 |
\<open>and_num m n = None \<longleftrightarrow> numeral m AND numeral n = 0\<close> |
|
3164 |
by (simp add: numeral_and_num split: option.split) |
|
3165 |
||
3166 |
lemma and_num_eq_Some_iff: |
|
3167 |
\<open>and_num m n = Some q \<longleftrightarrow> numeral m AND numeral n = numeral q\<close> |
|
3168 |
by (simp add: numeral_and_num split: option.split) |
|
3169 |
||
3170 |
end |
|
3171 |
||
3172 |
lemma and_int_code [code]: |
|
3173 |
fixes i j :: int shows |
|
3174 |
\<open>0 AND j = 0\<close> |
|
3175 |
\<open>i AND 0 = 0\<close> |
|
3176 |
\<open>Int.Pos n AND Int.Pos m = (case and_num n m of None \<Rightarrow> 0 | Some n' \<Rightarrow> Int.Pos n')\<close> |
|
3177 |
\<open>Int.Neg n AND Int.Neg m = NOT (Num.sub n num.One OR Num.sub m num.One)\<close> |
|
3178 |
\<open>Int.Pos n AND Int.Neg num.One = Int.Pos n\<close> |
|
3179 |
\<open>Int.Pos n AND Int.Neg (num.Bit0 m) = Num.sub (or_not_num_neg (Num.BitM m) n) num.One\<close> |
|
3180 |
\<open>Int.Pos n AND Int.Neg (num.Bit1 m) = Num.sub (or_not_num_neg (num.Bit0 m) n) num.One\<close> |
|
3181 |
\<open>Int.Neg num.One AND Int.Pos m = Int.Pos m\<close> |
|
3182 |
\<open>Int.Neg (num.Bit0 n) AND Int.Pos m = Num.sub (or_not_num_neg (Num.BitM n) m) num.One\<close> |
|
3183 |
\<open>Int.Neg (num.Bit1 n) AND Int.Pos m = Num.sub (or_not_num_neg (num.Bit0 n) m) num.One\<close> |
|
3184 |
apply (auto simp add: and_num_eq_None_iff [where ?'a = int] and_num_eq_Some_iff [where ?'a = int] |
|
3185 |
split: option.split) |
|
3186 |
apply (simp_all only: sub_one_eq_not_neg numeral_or_not_num_eq minus_minus and_not_numerals |
|
3187 |
bit.de_Morgan_disj bit.double_compl and_not_num_eq_None_iff and_not_num_eq_Some_iff ac_simps) |
|
3188 |
done |
|
3189 |
||
3190 |
context linordered_euclidean_semiring_bit_operations |
|
3191 |
begin |
|
3192 |
||
3193 |
fun or_num :: \<open>num \<Rightarrow> num \<Rightarrow> num\<close> \<^marker>\<open>contributor \<open>Andreas Lochbihler\<close>\<close> |
|
3194 |
where |
|
3195 |
\<open>or_num num.One num.One = num.One\<close> |
|
3196 |
| \<open>or_num num.One (num.Bit0 n) = num.Bit1 n\<close> |
|
3197 |
| \<open>or_num num.One (num.Bit1 n) = num.Bit1 n\<close> |
|
3198 |
| \<open>or_num (num.Bit0 m) num.One = num.Bit1 m\<close> |
|
3199 |
| \<open>or_num (num.Bit0 m) (num.Bit0 n) = num.Bit0 (or_num m n)\<close> |
|
3200 |
| \<open>or_num (num.Bit0 m) (num.Bit1 n) = num.Bit1 (or_num m n)\<close> |
|
3201 |
| \<open>or_num (num.Bit1 m) num.One = num.Bit1 m\<close> |
|
3202 |
| \<open>or_num (num.Bit1 m) (num.Bit0 n) = num.Bit1 (or_num m n)\<close> |
|
3203 |
| \<open>or_num (num.Bit1 m) (num.Bit1 n) = num.Bit1 (or_num m n)\<close> |
|
3204 |
||
3205 |
lemma numeral_or_num: |
|
3206 |
\<open>numeral m OR numeral n = numeral (or_num m n)\<close> |
|
3207 |
by (induction m n rule: or_num.induct) simp_all |
|
3208 |
||
3209 |
lemma numeral_or_num_eq: |
|
3210 |
\<open>numeral (or_num m n) = numeral m OR numeral n\<close> |
|
3211 |
by (simp add: numeral_or_num) |
|
3212 |
||
3213 |
end |
|
3214 |
||
3215 |
lemma or_int_code [code]: |
|
3216 |
fixes i j :: int shows |
|
3217 |
\<open>0 OR j = j\<close> |
|
3218 |
\<open>i OR 0 = i\<close> |
|
3219 |
\<open>Int.Pos n OR Int.Pos m = Int.Pos (or_num n m)\<close> |
|
3220 |
\<open>Int.Neg n OR Int.Neg m = NOT (Num.sub n num.One AND Num.sub m num.One)\<close> |
|
3221 |
\<open>Int.Pos n OR Int.Neg num.One = Int.Neg num.One\<close> |
|
3222 |
\<open>Int.Pos n OR Int.Neg (num.Bit0 m) = (case and_not_num (Num.BitM m) n of None \<Rightarrow> -1 | Some n' \<Rightarrow> Int.Neg (Num.inc n'))\<close> |
|
3223 |
\<open>Int.Pos n OR Int.Neg (num.Bit1 m) = (case and_not_num (num.Bit0 m) n of None \<Rightarrow> -1 | Some n' \<Rightarrow> Int.Neg (Num.inc n'))\<close> |
|
3224 |
\<open>Int.Neg num.One OR Int.Pos m = Int.Neg num.One\<close> |
|
3225 |
\<open>Int.Neg (num.Bit0 n) OR Int.Pos m = (case and_not_num (Num.BitM n) m of None \<Rightarrow> -1 | Some n' \<Rightarrow> Int.Neg (Num.inc n'))\<close> |
|
3226 |
\<open>Int.Neg (num.Bit1 n) OR Int.Pos m = (case and_not_num (num.Bit0 n) m of None \<Rightarrow> -1 | Some n' \<Rightarrow> Int.Neg (Num.inc n'))\<close> |
|
3227 |
apply (auto simp add: numeral_or_num_eq split: option.splits) |
|
3228 |
apply (simp_all only: and_not_num_eq_None_iff and_not_num_eq_Some_iff and_not_numerals |
|
3229 |
numeral_or_not_num_eq or_eq_not_not_and bit.double_compl ac_simps flip: numeral_eq_iff [where ?'a = int]) |
|
3230 |
apply simp_all |
|
3231 |
done |
|
3232 |
||
3233 |
fun xor_num :: \<open>num \<Rightarrow> num \<Rightarrow> num option\<close> \<^marker>\<open>contributor \<open>Andreas Lochbihler\<close>\<close> |
|
3234 |
where |
|
3235 |
\<open>xor_num num.One num.One = None\<close> |
|
3236 |
| \<open>xor_num num.One (num.Bit0 n) = Some (num.Bit1 n)\<close> |
|
3237 |
| \<open>xor_num num.One (num.Bit1 n) = Some (num.Bit0 n)\<close> |
|
3238 |
| \<open>xor_num (num.Bit0 m) num.One = Some (num.Bit1 m)\<close> |
|
3239 |
| \<open>xor_num (num.Bit0 m) (num.Bit0 n) = map_option num.Bit0 (xor_num m n)\<close> |
|
3240 |
| \<open>xor_num (num.Bit0 m) (num.Bit1 n) = Some (case xor_num m n of None \<Rightarrow> num.One | Some n' \<Rightarrow> num.Bit1 n')\<close> |
|
3241 |
| \<open>xor_num (num.Bit1 m) num.One = Some (num.Bit0 m)\<close> |
|
3242 |
| \<open>xor_num (num.Bit1 m) (num.Bit0 n) = Some (case xor_num m n of None \<Rightarrow> num.One | Some n' \<Rightarrow> num.Bit1 n')\<close> |
|
3243 |
| \<open>xor_num (num.Bit1 m) (num.Bit1 n) = map_option num.Bit0 (xor_num m n)\<close> |
|
3244 |
||
3245 |
context linordered_euclidean_semiring_bit_operations |
|
3246 |
begin |
|
3247 |
||
3248 |
lemma numeral_xor_num: |
|
3249 |
\<open>numeral m XOR numeral n = (case xor_num m n of None \<Rightarrow> 0 | Some n' \<Rightarrow> numeral n')\<close> |
|
3250 |
by (induction m n rule: xor_num.induct) (simp_all split: option.split) |
|
3251 |
||
3252 |
lemma xor_num_eq_None_iff: |
|
3253 |
\<open>xor_num m n = None \<longleftrightarrow> numeral m XOR numeral n = 0\<close> |
|
3254 |
by (simp add: numeral_xor_num split: option.split) |
|
3255 |
||
3256 |
lemma xor_num_eq_Some_iff: |
|
3257 |
\<open>xor_num m n = Some q \<longleftrightarrow> numeral m XOR numeral n = numeral q\<close> |
|
3258 |
by (simp add: numeral_xor_num split: option.split) |
|
3259 |
||
3260 |
end |
|
3261 |
||
3262 |
lemma xor_int_code [code]: |
|
3263 |
fixes i j :: int shows |
|
3264 |
\<open>0 XOR j = j\<close> |
|
3265 |
\<open>i XOR 0 = i\<close> |
|
3266 |
\<open>Int.Pos n XOR Int.Pos m = (case xor_num n m of None \<Rightarrow> 0 | Some n' \<Rightarrow> Int.Pos n')\<close> |
|
3267 |
\<open>Int.Neg n XOR Int.Neg m = Num.sub n num.One XOR Num.sub m num.One\<close> |
|
3268 |
\<open>Int.Neg n XOR Int.Pos m = NOT (Num.sub n num.One XOR Int.Pos m)\<close> |
|
3269 |
\<open>Int.Pos n XOR Int.Neg m = NOT (Int.Pos n XOR Num.sub m num.One)\<close> |
|
3270 |
by (simp_all add: xor_num_eq_None_iff [where ?'a = int] xor_num_eq_Some_iff [where ?'a = int] split: option.split) |
|
3271 |
||
3272 |
lemma push_bit_int_code [code]: |
|
3273 |
\<open>push_bit 0 i = i\<close> |
|
3274 |
\<open>push_bit (Suc n) i = push_bit n (Int.dup i)\<close> |
|
3275 |
by (simp_all add: ac_simps) |
|
3276 |
||
3277 |
lemma drop_bit_int_code [code]: |
|
3278 |
fixes i :: int shows |
|
3279 |
\<open>drop_bit 0 i = i\<close> |
|
3280 |
\<open>drop_bit (Suc n) 0 = (0 :: int)\<close> |
|
3281 |
\<open>drop_bit (Suc n) (Int.Pos num.One) = 0\<close> |
|
3282 |
\<open>drop_bit (Suc n) (Int.Pos (num.Bit0 m)) = drop_bit n (Int.Pos m)\<close> |
|
3283 |
\<open>drop_bit (Suc n) (Int.Pos (num.Bit1 m)) = drop_bit n (Int.Pos m)\<close> |
|
3284 |
\<open>drop_bit (Suc n) (Int.Neg num.One) = - 1\<close> |
|
3285 |
\<open>drop_bit (Suc n) (Int.Neg (num.Bit0 m)) = drop_bit n (Int.Neg m)\<close> |
|
3286 |
\<open>drop_bit (Suc n) (Int.Neg (num.Bit1 m)) = drop_bit n (Int.Neg (Num.inc m))\<close> |
|
3287 |
by (simp_all add: drop_bit_Suc add_One) |
|
3288 |
||
3289 |
||
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
3290 |
subsection \<open>More properties\<close> |
|
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
3291 |
|
| 72830 | 3292 |
lemma take_bit_eq_mask_iff: |
3293 |
\<open>take_bit n k = mask n \<longleftrightarrow> take_bit n (k + 1) = 0\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
3294 |
for k :: int |
|
3295 |
proof |
|
3296 |
assume ?P |
|
3297 |
then have \<open>take_bit n (take_bit n k + take_bit n 1) = 0\<close> |
|
|
74108
3146646a43a7
simplified hierarchy of type classes for bit operations
haftmann
parents:
74101
diff
changeset
|
3298 |
by (simp add: mask_eq_exp_minus_1 take_bit_eq_0_iff) |
| 72830 | 3299 |
then show ?Q |
3300 |
by (simp only: take_bit_add) |
|
3301 |
next |
|
3302 |
assume ?Q |
|
3303 |
then have \<open>take_bit n (k + 1) - 1 = - 1\<close> |
|
3304 |
by simp |
|
3305 |
then have \<open>take_bit n (take_bit n (k + 1) - 1) = take_bit n (- 1)\<close> |
|
3306 |
by simp |
|
3307 |
moreover have \<open>take_bit n (take_bit n (k + 1) - 1) = take_bit n k\<close> |
|
3308 |
by (simp add: take_bit_eq_mod mod_simps) |
|
3309 |
ultimately show ?P |
|
| 74592 | 3310 |
by simp |
| 72830 | 3311 |
qed |
3312 |
||
3313 |
lemma take_bit_eq_mask_iff_exp_dvd: |
|
3314 |
\<open>take_bit n k = mask n \<longleftrightarrow> 2 ^ n dvd k + 1\<close> |
|
3315 |
for k :: int |
|
3316 |
by (simp add: take_bit_eq_mask_iff flip: take_bit_eq_0_iff) |
|
3317 |
||
| 71442 | 3318 |
|
| 72028 | 3319 |
subsection \<open>Bit concatenation\<close> |
3320 |
||
3321 |
definition concat_bit :: \<open>nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int\<close> |
|
| 72227 | 3322 |
where \<open>concat_bit n k l = take_bit n k OR push_bit n l\<close> |
| 72028 | 3323 |
|
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
3324 |
lemma bit_concat_bit_iff [bit_simps]: |
| 72028 | 3325 |
\<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 | 3326 |
by (simp add: concat_bit_def bit_or_iff bit_and_iff bit_take_bit_iff bit_push_bit_iff ac_simps) |
| 72028 | 3327 |
|
3328 |
lemma concat_bit_eq: |
|
3329 |
\<open>concat_bit n k l = take_bit n k + push_bit n l\<close> |
|
3330 |
by (simp add: concat_bit_def take_bit_eq_mask |
|
3331 |
bit_and_iff bit_mask_iff bit_push_bit_iff disjunctive_add) |
|
3332 |
||
3333 |
lemma concat_bit_0 [simp]: |
|
3334 |
\<open>concat_bit 0 k l = l\<close> |
|
3335 |
by (simp add: concat_bit_def) |
|
3336 |
||
3337 |
lemma concat_bit_Suc: |
|
3338 |
\<open>concat_bit (Suc n) k l = k mod 2 + 2 * concat_bit n (k div 2) l\<close> |
|
3339 |
by (simp add: concat_bit_eq take_bit_Suc push_bit_double) |
|
3340 |
||
3341 |
lemma concat_bit_of_zero_1 [simp]: |
|
3342 |
\<open>concat_bit n 0 l = push_bit n l\<close> |
|
3343 |
by (simp add: concat_bit_def) |
|
3344 |
||
3345 |
lemma concat_bit_of_zero_2 [simp]: |
|
3346 |
\<open>concat_bit n k 0 = take_bit n k\<close> |
|
3347 |
by (simp add: concat_bit_def take_bit_eq_mask) |
|
3348 |
||
3349 |
lemma concat_bit_nonnegative_iff [simp]: |
|
3350 |
\<open>concat_bit n k l \<ge> 0 \<longleftrightarrow> l \<ge> 0\<close> |
|
3351 |
by (simp add: concat_bit_def) |
|
3352 |
||
3353 |
lemma concat_bit_negative_iff [simp]: |
|
3354 |
\<open>concat_bit n k l < 0 \<longleftrightarrow> l < 0\<close> |
|
3355 |
by (simp add: concat_bit_def) |
|
3356 |
||
3357 |
lemma concat_bit_assoc: |
|
3358 |
\<open>concat_bit n k (concat_bit m l r) = concat_bit (m + n) (concat_bit n k l) r\<close> |
|
3359 |
by (rule bit_eqI) (auto simp add: bit_concat_bit_iff ac_simps) |
|
3360 |
||
3361 |
lemma concat_bit_assoc_sym: |
|
3362 |
\<open>concat_bit m (concat_bit n k l) r = concat_bit (min m n) k (concat_bit (m - n) l r)\<close> |
|
3363 |
by (rule bit_eqI) (auto simp add: bit_concat_bit_iff ac_simps min_def) |
|
3364 |
||
| 72227 | 3365 |
lemma concat_bit_eq_iff: |
3366 |
\<open>concat_bit n k l = concat_bit n r s |
|
3367 |
\<longleftrightarrow> take_bit n k = take_bit n r \<and> l = s\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>) |
|
3368 |
proof |
|
3369 |
assume ?Q |
|
3370 |
then show ?P |
|
3371 |
by (simp add: concat_bit_def) |
|
3372 |
next |
|
3373 |
assume ?P |
|
3374 |
then have *: \<open>bit (concat_bit n k l) m = bit (concat_bit n r s) m\<close> for m |
|
3375 |
by (simp add: bit_eq_iff) |
|
3376 |
have \<open>take_bit n k = take_bit n r\<close> |
|
3377 |
proof (rule bit_eqI) |
|
3378 |
fix m |
|
3379 |
from * [of m] |
|
3380 |
show \<open>bit (take_bit n k) m \<longleftrightarrow> bit (take_bit n r) m\<close> |
|
3381 |
by (auto simp add: bit_take_bit_iff bit_concat_bit_iff) |
|
3382 |
qed |
|
3383 |
moreover have \<open>push_bit n l = push_bit n s\<close> |
|
3384 |
proof (rule bit_eqI) |
|
3385 |
fix m |
|
3386 |
from * [of m] |
|
3387 |
show \<open>bit (push_bit n l) m \<longleftrightarrow> bit (push_bit n s) m\<close> |
|
3388 |
by (auto simp add: bit_push_bit_iff bit_concat_bit_iff) |
|
3389 |
qed |
|
3390 |
then have \<open>l = s\<close> |
|
3391 |
by (simp add: push_bit_eq_mult) |
|
3392 |
ultimately show ?Q |
|
3393 |
by (simp add: concat_bit_def) |
|
3394 |
qed |
|
3395 |
||
3396 |
lemma take_bit_concat_bit_eq: |
|
3397 |
\<open>take_bit m (concat_bit n k l) = concat_bit (min m n) k (take_bit (m - n) l)\<close> |
|
3398 |
by (rule bit_eqI) |
|
| 79068 | 3399 |
(auto simp add: bit_take_bit_iff bit_concat_bit_iff min_def) |
| 72227 | 3400 |
|
| 72488 | 3401 |
lemma concat_bit_take_bit_eq: |
3402 |
\<open>concat_bit n (take_bit n b) = concat_bit n b\<close> |
|
3403 |
by (simp add: concat_bit_def [abs_def]) |
|
3404 |
||
| 72028 | 3405 |
|
| 72241 | 3406 |
subsection \<open>Taking bits with sign propagation\<close> |
| 72010 | 3407 |
|
| 72241 | 3408 |
context ring_bit_operations |
3409 |
begin |
|
| 72010 | 3410 |
|
| 72241 | 3411 |
definition signed_take_bit :: \<open>nat \<Rightarrow> 'a \<Rightarrow> 'a\<close> |
3412 |
where \<open>signed_take_bit n a = take_bit n a OR (of_bool (bit a n) * NOT (mask n))\<close> |
|
| 72227 | 3413 |
|
| 72241 | 3414 |
lemma signed_take_bit_eq_if_positive: |
3415 |
\<open>signed_take_bit n a = take_bit n a\<close> if \<open>\<not> bit a n\<close> |
|
| 72010 | 3416 |
using that by (simp add: signed_take_bit_def) |
3417 |
||
| 72241 | 3418 |
lemma signed_take_bit_eq_if_negative: |
3419 |
\<open>signed_take_bit n a = take_bit n a OR NOT (mask n)\<close> if \<open>bit a n\<close> |
|
3420 |
using that by (simp add: signed_take_bit_def) |
|
3421 |
||
3422 |
lemma even_signed_take_bit_iff: |
|
3423 |
\<open>even (signed_take_bit m a) \<longleftrightarrow> even a\<close> |
|
| 75085 | 3424 |
by (auto simp add: bit_0 signed_take_bit_def even_or_iff even_mask_iff bit_double_iff) |
| 72241 | 3425 |
|
|
72611
c7bc3e70a8c7
official collection for bit projection simplifications
haftmann
parents:
72512
diff
changeset
|
3426 |
lemma bit_signed_take_bit_iff [bit_simps]: |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
3427 |
\<open>bit (signed_take_bit m a) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> bit a (min m n)\<close>
|
| 72241 | 3428 |
by (simp add: signed_take_bit_def bit_take_bit_iff bit_or_iff bit_not_iff bit_mask_iff min_def not_le) |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
3429 |
(blast dest: bit_imp_possible_bit) |
| 72010 | 3430 |
|
3431 |
lemma signed_take_bit_0 [simp]: |
|
| 72241 | 3432 |
\<open>signed_take_bit 0 a = - (a mod 2)\<close> |
| 75085 | 3433 |
by (simp add: bit_0 signed_take_bit_def odd_iff_mod_2_eq_one) |
| 72010 | 3434 |
|
3435 |
lemma signed_take_bit_Suc: |
|
| 72241 | 3436 |
\<open>signed_take_bit (Suc n) a = a mod 2 + 2 * signed_take_bit n (a div 2)\<close> |
| 75085 | 3437 |
by (simp add: bit_eq_iff bit_sum_mult_2_cases bit_simps bit_0 possible_bit_less_imp flip: bit_Suc min_Suc_Suc) |
| 72010 | 3438 |
|
| 72187 | 3439 |
lemma signed_take_bit_of_0 [simp]: |
3440 |
\<open>signed_take_bit n 0 = 0\<close> |
|
3441 |
by (simp add: signed_take_bit_def) |
|
3442 |
||
3443 |
lemma signed_take_bit_of_minus_1 [simp]: |
|
3444 |
\<open>signed_take_bit n (- 1) = - 1\<close> |
|
| 74592 | 3445 |
by (simp add: signed_take_bit_def mask_eq_exp_minus_1 possible_bit_def) |
| 72187 | 3446 |
|
| 72241 | 3447 |
lemma signed_take_bit_Suc_1 [simp]: |
3448 |
\<open>signed_take_bit (Suc n) 1 = 1\<close> |
|
3449 |
by (simp add: signed_take_bit_Suc) |
|
3450 |
||
| 74497 | 3451 |
lemma signed_take_bit_numeral_of_1 [simp]: |
3452 |
\<open>signed_take_bit (numeral k) 1 = 1\<close> |
|
3453 |
by (simp add: bit_1_iff signed_take_bit_eq_if_positive) |
|
3454 |
||
| 72241 | 3455 |
lemma signed_take_bit_rec: |
3456 |
\<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> |
|
3457 |
by (cases n) (simp_all add: signed_take_bit_Suc) |
|
| 72187 | 3458 |
|
3459 |
lemma signed_take_bit_eq_iff_take_bit_eq: |
|
| 72241 | 3460 |
\<open>signed_take_bit n a = signed_take_bit n b \<longleftrightarrow> take_bit (Suc n) a = take_bit (Suc n) b\<close> |
3461 |
proof - |
|
3462 |
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> |
|
3463 |
by (simp add: fun_eq_iff bit_signed_take_bit_iff bit_take_bit_iff not_le less_Suc_eq_le min_def) |
|
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
3464 |
(use bit_imp_possible_bit in fastforce) |
| 72187 | 3465 |
then show ?thesis |
|
74309
42523fbf643b
explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents:
74163
diff
changeset
|
3466 |
by (auto simp add: fun_eq_iff intro: bit_eqI) |
| 72187 | 3467 |
qed |
3468 |
||
| 72241 | 3469 |
lemma signed_take_bit_signed_take_bit [simp]: |
3470 |
\<open>signed_take_bit m (signed_take_bit n a) = signed_take_bit (min m n) a\<close> |
|
| 74495 | 3471 |
by (auto simp add: bit_eq_iff bit_simps ac_simps) |
| 72241 | 3472 |
|
3473 |
lemma signed_take_bit_take_bit: |
|
3474 |
\<open>signed_take_bit m (take_bit n a) = (if n \<le> m then take_bit n else signed_take_bit m) a\<close> |
|
3475 |
by (rule bit_eqI) (auto simp add: bit_signed_take_bit_iff min_def bit_take_bit_iff) |
|
3476 |
||
| 72187 | 3477 |
lemma take_bit_signed_take_bit: |
| 72241 | 3478 |
\<open>take_bit m (signed_take_bit n a) = take_bit m a\<close> if \<open>m \<le> Suc n\<close> |
| 72187 | 3479 |
using that by (rule le_SucE; intro bit_eqI) |
3480 |
(auto simp add: bit_take_bit_iff bit_signed_take_bit_iff min_def less_Suc_eq) |
|
3481 |
||
| 72241 | 3482 |
end |
3483 |
||
3484 |
text \<open>Modulus centered around 0\<close> |
|
3485 |
||
3486 |
lemma signed_take_bit_eq_concat_bit: |
|
3487 |
\<open>signed_take_bit n k = concat_bit n k (- of_bool (bit k n))\<close> |
|
| 74592 | 3488 |
by (simp add: concat_bit_def signed_take_bit_def) |
| 72241 | 3489 |
|
| 72187 | 3490 |
lemma signed_take_bit_add: |
3491 |
\<open>signed_take_bit n (signed_take_bit n k + signed_take_bit n l) = signed_take_bit n (k + l)\<close> |
|
| 72241 | 3492 |
for k l :: int |
| 72187 | 3493 |
proof - |
3494 |
have \<open>take_bit (Suc n) |
|
3495 |
(take_bit (Suc n) (signed_take_bit n k) + |
|
3496 |
take_bit (Suc n) (signed_take_bit n l)) = |
|
3497 |
take_bit (Suc n) (k + l)\<close> |
|
3498 |
by (simp add: take_bit_signed_take_bit take_bit_add) |
|
3499 |
then show ?thesis |
|
3500 |
by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_add) |
|
3501 |
qed |
|
3502 |
||
3503 |
lemma signed_take_bit_diff: |
|
3504 |
\<open>signed_take_bit n (signed_take_bit n k - signed_take_bit n l) = signed_take_bit n (k - l)\<close> |
|
| 72241 | 3505 |
for k l :: int |
| 72187 | 3506 |
proof - |
3507 |
have \<open>take_bit (Suc n) |
|
3508 |
(take_bit (Suc n) (signed_take_bit n k) - |
|
3509 |
take_bit (Suc n) (signed_take_bit n l)) = |
|
3510 |
take_bit (Suc n) (k - l)\<close> |
|
3511 |
by (simp add: take_bit_signed_take_bit take_bit_diff) |
|
3512 |
then show ?thesis |
|
3513 |
by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_diff) |
|
3514 |
qed |
|
3515 |
||
3516 |
lemma signed_take_bit_minus: |
|
3517 |
\<open>signed_take_bit n (- signed_take_bit n k) = signed_take_bit n (- k)\<close> |
|
| 72241 | 3518 |
for k :: int |
| 72187 | 3519 |
proof - |
3520 |
have \<open>take_bit (Suc n) |
|
3521 |
(- take_bit (Suc n) (signed_take_bit n k)) = |
|
3522 |
take_bit (Suc n) (- k)\<close> |
|
3523 |
by (simp add: take_bit_signed_take_bit take_bit_minus) |
|
3524 |
then show ?thesis |
|
3525 |
by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_minus) |
|
3526 |
qed |
|
3527 |
||
3528 |
lemma signed_take_bit_mult: |
|
3529 |
\<open>signed_take_bit n (signed_take_bit n k * signed_take_bit n l) = signed_take_bit n (k * l)\<close> |
|
| 72241 | 3530 |
for k l :: int |
| 72187 | 3531 |
proof - |
3532 |
have \<open>take_bit (Suc n) |
|
3533 |
(take_bit (Suc n) (signed_take_bit n k) * |
|
3534 |
take_bit (Suc n) (signed_take_bit n l)) = |
|
3535 |
take_bit (Suc n) (k * l)\<close> |
|
3536 |
by (simp add: take_bit_signed_take_bit take_bit_mult) |
|
3537 |
then show ?thesis |
|
3538 |
by (simp only: signed_take_bit_eq_iff_take_bit_eq take_bit_mult) |
|
3539 |
qed |
|
3540 |
||
| 72010 | 3541 |
lemma signed_take_bit_eq_take_bit_minus: |
3542 |
\<open>signed_take_bit n k = take_bit (Suc n) k - 2 ^ Suc n * of_bool (bit k n)\<close> |
|
| 72241 | 3543 |
for k :: int |
| 72010 | 3544 |
proof (cases \<open>bit k n\<close>) |
3545 |
case True |
|
3546 |
have \<open>signed_take_bit n k = take_bit (Suc n) k OR NOT (mask (Suc n))\<close> |
|
3547 |
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) |
|
3548 |
then have \<open>signed_take_bit n k = take_bit (Suc n) k + NOT (mask (Suc n))\<close> |
|
3549 |
by (simp add: disjunctive_add bit_take_bit_iff bit_not_iff bit_mask_iff) |
|
3550 |
with True show ?thesis |
|
3551 |
by (simp flip: minus_exp_eq_not_mask) |
|
3552 |
next |
|
3553 |
case False |
|
| 72241 | 3554 |
show ?thesis |
3555 |
by (rule bit_eqI) (simp add: False bit_signed_take_bit_iff bit_take_bit_iff min_def less_Suc_eq) |
|
| 72010 | 3556 |
qed |
3557 |
||
3558 |
lemma signed_take_bit_eq_take_bit_shift: |
|
3559 |
\<open>signed_take_bit n k = take_bit (Suc n) (k + 2 ^ n) - 2 ^ n\<close> |
|
| 72241 | 3560 |
for k :: int |
| 72010 | 3561 |
proof - |
3562 |
have *: \<open>take_bit n k OR 2 ^ n = take_bit n k + 2 ^ n\<close> |
|
3563 |
by (simp add: disjunctive_add bit_exp_iff bit_take_bit_iff) |
|
3564 |
have \<open>take_bit n k - 2 ^ n = take_bit n k + NOT (mask n)\<close> |
|
3565 |
by (simp add: minus_exp_eq_not_mask) |
|
3566 |
also have \<open>\<dots> = take_bit n k OR NOT (mask n)\<close> |
|
3567 |
by (rule disjunctive_add) |
|
3568 |
(simp add: bit_exp_iff bit_take_bit_iff bit_not_iff bit_mask_iff) |
|
3569 |
finally have **: \<open>take_bit n k - 2 ^ n = take_bit n k OR NOT (mask n)\<close> . |
|
3570 |
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> |
|
3571 |
by (simp only: take_bit_add) |
|
3572 |
also have \<open>take_bit (Suc n) k = 2 ^ n * of_bool (bit k n) + take_bit n k\<close> |
|
3573 |
by (simp add: take_bit_Suc_from_most) |
|
3574 |
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> |
|
3575 |
by (simp add: ac_simps) |
|
3576 |
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> |
|
3577 |
by (rule disjunctive_add) |
|
3578 |
(auto simp add: disjunctive_add bit_take_bit_iff bit_double_iff bit_exp_iff) |
|
3579 |
finally show ?thesis |
|
| 72241 | 3580 |
using * ** by (simp add: signed_take_bit_def concat_bit_Suc min_def ac_simps) |
| 72010 | 3581 |
qed |
3582 |
||
3583 |
lemma signed_take_bit_nonnegative_iff [simp]: |
|
3584 |
\<open>0 \<le> signed_take_bit n k \<longleftrightarrow> \<not> bit k n\<close> |
|
| 72241 | 3585 |
for k :: int |
| 72028 | 3586 |
by (simp add: signed_take_bit_def not_less concat_bit_def) |
| 72010 | 3587 |
|
3588 |
lemma signed_take_bit_negative_iff [simp]: |
|
3589 |
\<open>signed_take_bit n k < 0 \<longleftrightarrow> bit k n\<close> |
|
| 72241 | 3590 |
for k :: int |
| 72028 | 3591 |
by (simp add: signed_take_bit_def not_less concat_bit_def) |
| 72010 | 3592 |
|
| 73868 | 3593 |
lemma signed_take_bit_int_greater_eq_minus_exp [simp]: |
3594 |
\<open>- (2 ^ n) \<le> signed_take_bit n k\<close> |
|
3595 |
for k :: int |
|
3596 |
by (simp add: signed_take_bit_eq_take_bit_shift) |
|
3597 |
||
3598 |
lemma signed_take_bit_int_less_exp [simp]: |
|
3599 |
\<open>signed_take_bit n k < 2 ^ n\<close> |
|
3600 |
for k :: int |
|
3601 |
using take_bit_int_less_exp [of \<open>Suc n\<close>] |
|
3602 |
by (simp add: signed_take_bit_eq_take_bit_shift) |
|
3603 |
||
| 72261 | 3604 |
lemma signed_take_bit_int_eq_self_iff: |
3605 |
\<open>signed_take_bit n k = k \<longleftrightarrow> - (2 ^ n) \<le> k \<and> k < 2 ^ n\<close> |
|
3606 |
for k :: int |
|
3607 |
by (auto simp add: signed_take_bit_eq_take_bit_shift take_bit_int_eq_self_iff algebra_simps) |
|
3608 |
||
| 72262 | 3609 |
lemma signed_take_bit_int_eq_self: |
3610 |
\<open>signed_take_bit n k = k\<close> if \<open>- (2 ^ n) \<le> k\<close> \<open>k < 2 ^ n\<close> |
|
3611 |
for k :: int |
|
3612 |
using that by (simp add: signed_take_bit_int_eq_self_iff) |
|
3613 |
||
| 72261 | 3614 |
lemma signed_take_bit_int_less_eq_self_iff: |
3615 |
\<open>signed_take_bit n k \<le> k \<longleftrightarrow> - (2 ^ n) \<le> k\<close> |
|
3616 |
for k :: int |
|
3617 |
by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_less_eq_self_iff algebra_simps) |
|
3618 |
linarith |
|
3619 |
||
3620 |
lemma signed_take_bit_int_less_self_iff: |
|
3621 |
\<open>signed_take_bit n k < k \<longleftrightarrow> 2 ^ n \<le> k\<close> |
|
3622 |
for k :: int |
|
3623 |
by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_less_self_iff algebra_simps) |
|
3624 |
||
3625 |
lemma signed_take_bit_int_greater_self_iff: |
|
3626 |
\<open>k < signed_take_bit n k \<longleftrightarrow> k < - (2 ^ n)\<close> |
|
3627 |
for k :: int |
|
3628 |
by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_greater_self_iff algebra_simps) |
|
3629 |
linarith |
|
3630 |
||
3631 |
lemma signed_take_bit_int_greater_eq_self_iff: |
|
3632 |
\<open>k \<le> signed_take_bit n k \<longleftrightarrow> k < 2 ^ n\<close> |
|
3633 |
for k :: int |
|
3634 |
by (simp add: signed_take_bit_eq_take_bit_shift take_bit_int_greater_eq_self_iff algebra_simps) |
|
3635 |
||
3636 |
lemma signed_take_bit_int_greater_eq: |
|
| 72010 | 3637 |
\<open>k + 2 ^ Suc n \<le> signed_take_bit n k\<close> if \<open>k < - (2 ^ n)\<close> |
| 72241 | 3638 |
for k :: int |
| 72262 | 3639 |
using that take_bit_int_greater_eq [of \<open>k + 2 ^ n\<close> \<open>Suc n\<close>] |
| 72010 | 3640 |
by (simp add: signed_take_bit_eq_take_bit_shift) |
3641 |
||
| 72261 | 3642 |
lemma signed_take_bit_int_less_eq: |
| 72010 | 3643 |
\<open>signed_take_bit n k \<le> k - 2 ^ Suc n\<close> if \<open>k \<ge> 2 ^ n\<close> |
| 72241 | 3644 |
for k :: int |
| 72262 | 3645 |
using that take_bit_int_less_eq [of \<open>Suc n\<close> \<open>k + 2 ^ n\<close>] |
| 72010 | 3646 |
by (simp add: signed_take_bit_eq_take_bit_shift) |
3647 |
||
3648 |
lemma signed_take_bit_Suc_bit0 [simp]: |
|
| 72241 | 3649 |
\<open>signed_take_bit (Suc n) (numeral (Num.Bit0 k)) = signed_take_bit n (numeral k) * (2 :: int)\<close> |
| 72010 | 3650 |
by (simp add: signed_take_bit_Suc) |
3651 |
||
3652 |
lemma signed_take_bit_Suc_bit1 [simp]: |
|
| 72241 | 3653 |
\<open>signed_take_bit (Suc n) (numeral (Num.Bit1 k)) = signed_take_bit n (numeral k) * 2 + (1 :: int)\<close> |
| 72010 | 3654 |
by (simp add: signed_take_bit_Suc) |
3655 |
||
3656 |
lemma signed_take_bit_Suc_minus_bit0 [simp]: |
|
| 72241 | 3657 |
\<open>signed_take_bit (Suc n) (- numeral (Num.Bit0 k)) = signed_take_bit n (- numeral k) * (2 :: int)\<close> |
| 72010 | 3658 |
by (simp add: signed_take_bit_Suc) |
3659 |
||
3660 |
lemma signed_take_bit_Suc_minus_bit1 [simp]: |
|
| 72241 | 3661 |
\<open>signed_take_bit (Suc n) (- numeral (Num.Bit1 k)) = signed_take_bit n (- numeral k - 1) * 2 + (1 :: int)\<close> |
| 72010 | 3662 |
by (simp add: signed_take_bit_Suc) |
3663 |
||
3664 |
lemma signed_take_bit_numeral_bit0 [simp]: |
|
| 72241 | 3665 |
\<open>signed_take_bit (numeral l) (numeral (Num.Bit0 k)) = signed_take_bit (pred_numeral l) (numeral k) * (2 :: int)\<close> |
| 72010 | 3666 |
by (simp add: signed_take_bit_rec) |
3667 |
||
3668 |
lemma signed_take_bit_numeral_bit1 [simp]: |
|
| 72241 | 3669 |
\<open>signed_take_bit (numeral l) (numeral (Num.Bit1 k)) = signed_take_bit (pred_numeral l) (numeral k) * 2 + (1 :: int)\<close> |
| 72010 | 3670 |
by (simp add: signed_take_bit_rec) |
3671 |
||
3672 |
lemma signed_take_bit_numeral_minus_bit0 [simp]: |
|
| 72241 | 3673 |
\<open>signed_take_bit (numeral l) (- numeral (Num.Bit0 k)) = signed_take_bit (pred_numeral l) (- numeral k) * (2 :: int)\<close> |
| 72010 | 3674 |
by (simp add: signed_take_bit_rec) |
3675 |
||
3676 |
lemma signed_take_bit_numeral_minus_bit1 [simp]: |
|
| 72241 | 3677 |
\<open>signed_take_bit (numeral l) (- numeral (Num.Bit1 k)) = signed_take_bit (pred_numeral l) (- numeral k - 1) * 2 + (1 :: int)\<close> |
| 72010 | 3678 |
by (simp add: signed_take_bit_rec) |
3679 |
||
3680 |
lemma signed_take_bit_code [code]: |
|
| 72241 | 3681 |
\<open>signed_take_bit n a = |
3682 |
(let l = take_bit (Suc n) a |
|
3683 |
in if bit l n then l + push_bit (Suc n) (- 1) else l)\<close> |
|
| 72010 | 3684 |
proof - |
| 72241 | 3685 |
have *: \<open>take_bit (Suc n) a + push_bit n (- 2) = |
3686 |
take_bit (Suc n) a OR NOT (mask (Suc n))\<close> |
|
3687 |
by (auto simp add: bit_take_bit_iff bit_push_bit_iff bit_not_iff bit_mask_iff disjunctive_add |
|
3688 |
simp flip: push_bit_minus_one_eq_not_mask) |
|
| 72010 | 3689 |
show ?thesis |
3690 |
by (rule bit_eqI) |
|
| 74592 | 3691 |
(auto simp add: Let_def * bit_signed_take_bit_iff bit_take_bit_iff min_def less_Suc_eq bit_not_iff |
3692 |
bit_mask_iff bit_or_iff simp del: push_bit_minus_one_eq_not_mask) |
|
| 72010 | 3693 |
qed |
3694 |
||
3695 |
||
| 71800 | 3696 |
subsection \<open>Key ideas of bit operations\<close> |
3697 |
||
3698 |
text \<open> |
|
3699 |
When formalizing bit operations, it is tempting to represent |
|
3700 |
bit values as explicit lists over a binary type. This however |
|
3701 |
is a bad idea, mainly due to the inherent ambiguities in |
|
3702 |
representation concerning repeating leading bits. |
|
3703 |
||
3704 |
Hence this approach avoids such explicit lists altogether |
|
3705 |
following an algebraic path: |
|
3706 |
||
3707 |
\<^item> Bit values are represented by numeric types: idealized |
|
3708 |
unbounded bit values can be represented by type \<^typ>\<open>int\<close>, |
|
3709 |
bounded bit values by quotient types over \<^typ>\<open>int\<close>. |
|
3710 |
||
3711 |
\<^item> (A special case are idealized unbounded bit values ending |
|
3712 |
in @{term [source] 0} which can be represented by type \<^typ>\<open>nat\<close> but
|
|
3713 |
only support a restricted set of operations). |
|
3714 |
||
3715 |
\<^item> From this idea follows that |
|
3716 |
||
3717 |
\<^item> multiplication by \<^term>\<open>2 :: int\<close> is a bit shift to the left and |
|
3718 |
||
3719 |
\<^item> division by \<^term>\<open>2 :: int\<close> is a bit shift to the right. |
|
3720 |
||
3721 |
\<^item> Concerning bounded bit values, iterated shifts to the left |
|
3722 |
may result in eliminating all bits by shifting them all |
|
3723 |
beyond the boundary. The property \<^prop>\<open>(2 :: int) ^ n \<noteq> 0\<close> |
|
3724 |
represents that \<^term>\<open>n\<close> is \<^emph>\<open>not\<close> beyond that boundary. |
|
3725 |
||
|
71965
d45f5d4c41bd
more class operations for the sake of efficient generated code
haftmann
parents:
71956
diff
changeset
|
3726 |
\<^item> The projection on a single bit is then @{thm bit_iff_odd [where ?'a = int, no_vars]}.
|
| 71800 | 3727 |
|
3728 |
\<^item> This leads to the most fundamental properties of bit values: |
|
3729 |
||
3730 |
\<^item> Equality rule: @{thm bit_eqI [where ?'a = int, no_vars]}
|
|
3731 |
||
|
79480
c7cb1bf6efa0
consolidated name of lemma analogously to nat/int/word_bit_induct
haftmann
parents:
79117
diff
changeset
|
3732 |
\<^item> Induction rule: @{thm bit_induct [where ?'a = int, no_vars]}
|
| 71800 | 3733 |
|
3734 |
\<^item> Typical operations are characterized as follows: |
|
3735 |
||
3736 |
\<^item> Singleton \<^term>\<open>n\<close>th bit: \<^term>\<open>(2 :: int) ^ n\<close> |
|
3737 |
||
| 71956 | 3738 |
\<^item> Bit mask upto bit \<^term>\<open>n\<close>: @{thm mask_eq_exp_minus_1 [where ?'a = int, no_vars]}
|
| 71800 | 3739 |
|
3740 |
\<^item> Left shift: @{thm push_bit_eq_mult [where ?'a = int, no_vars]}
|
|
3741 |
||
3742 |
\<^item> Right shift: @{thm drop_bit_eq_div [where ?'a = int, no_vars]}
|
|
3743 |
||
3744 |
\<^item> Truncation: @{thm take_bit_eq_mod [where ?'a = int, no_vars]}
|
|
3745 |
||
3746 |
\<^item> Negation: @{thm bit_not_iff [where ?'a = int, no_vars]}
|
|
3747 |
||
3748 |
\<^item> And: @{thm bit_and_iff [where ?'a = int, no_vars]}
|
|
3749 |
||
3750 |
\<^item> Or: @{thm bit_or_iff [where ?'a = int, no_vars]}
|
|
3751 |
||
3752 |
\<^item> Xor: @{thm bit_xor_iff [where ?'a = int, no_vars]}
|
|
3753 |
||
| 79068 | 3754 |
\<^item> Set a single bit: @{thm set_bit_eq_or [where ?'a = int, no_vars]}
|
3755 |
||
3756 |
\<^item> Unset a single bit: @{thm unset_bit_eq_and_not [where ?'a = int, no_vars]}
|
|
3757 |
||
3758 |
\<^item> Flip a single bit: @{thm flip_bit_eq_xor [where ?'a = int, no_vars]}
|
|
| 72028 | 3759 |
|
| 72241 | 3760 |
\<^item> Signed truncation, or modulus centered around \<^term>\<open>0::int\<close>: @{thm signed_take_bit_def [no_vars]}
|
| 72028 | 3761 |
|
| 72241 | 3762 |
\<^item> Bit concatenation: @{thm concat_bit_def [no_vars]}
|
| 72028 | 3763 |
|
3764 |
\<^item> (Bounded) conversion from and to a list of bits: @{thm horner_sum_bit_eq_take_bit [where ?'a = int, no_vars]}
|
|
| 71800 | 3765 |
\<close> |
3766 |
||
| 79068 | 3767 |
|
3768 |
subsection \<open>Lemma duplicates and other\<close> |
|
3769 |
||
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3770 |
context semiring_bits |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3771 |
begin |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3772 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3773 |
lemma exp_div_exp_eq [no_atp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3774 |
\<open>2 ^ m div 2 ^ n = of_bool (2 ^ m \<noteq> 0 \<and> m \<ge> n) * 2 ^ (m - n)\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3775 |
apply (rule bit_eqI) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3776 |
using bit_exp_iff div_exp_eq apply (auto simp add: bit_iff_odd possible_bit_def) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3777 |
done |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3778 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3779 |
lemma bits_1_div_2 [no_atp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3780 |
\<open>1 div 2 = 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3781 |
by (fact half_1) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3782 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3783 |
lemma bits_1_div_exp [no_atp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3784 |
\<open>1 div 2 ^ n = of_bool (n = 0)\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3785 |
using div_exp_eq [of 1 1] by (cases n) simp_all |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3786 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3787 |
lemma exp_add_not_zero_imp [no_atp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3788 |
\<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close> if \<open>2 ^ (m + n) \<noteq> 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3789 |
proof - |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3790 |
have \<open>\<not> (2 ^ m = 0 \<or> 2 ^ n = 0)\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3791 |
proof (rule notI) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3792 |
assume \<open>2 ^ m = 0 \<or> 2 ^ n = 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3793 |
then have \<open>2 ^ (m + n) = 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3794 |
by (rule disjE) (simp_all add: power_add) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3795 |
with that show False .. |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3796 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3797 |
then show \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3798 |
by simp_all |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3799 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3800 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3801 |
lemma |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3802 |
exp_add_not_zero_imp_left [no_atp]: \<open>2 ^ m \<noteq> 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3803 |
and exp_add_not_zero_imp_right [no_atp]: \<open>2 ^ n \<noteq> 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3804 |
if \<open>2 ^ (m + n) \<noteq> 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3805 |
proof - |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3806 |
have \<open>\<not> (2 ^ m = 0 \<or> 2 ^ n = 0)\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3807 |
proof (rule notI) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3808 |
assume \<open>2 ^ m = 0 \<or> 2 ^ n = 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3809 |
then have \<open>2 ^ (m + n) = 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3810 |
by (rule disjE) (simp_all add: power_add) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3811 |
with that show False .. |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3812 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3813 |
then show \<open>2 ^ m \<noteq> 0\<close> and \<open>2 ^ n \<noteq> 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3814 |
by simp_all |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3815 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3816 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3817 |
lemma exp_not_zero_imp_exp_diff_not_zero [no_atp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3818 |
\<open>2 ^ (n - m) \<noteq> 0\<close> if \<open>2 ^ n \<noteq> 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3819 |
proof (cases \<open>m \<le> n\<close>) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3820 |
case True |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3821 |
moreover define q where \<open>q = n - m\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3822 |
ultimately have \<open>n = m + q\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3823 |
by simp |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3824 |
with that show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3825 |
by (simp add: exp_add_not_zero_imp_right) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3826 |
next |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3827 |
case False |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3828 |
with that show ?thesis |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3829 |
by simp |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3830 |
qed |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3831 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3832 |
lemma exp_eq_0_imp_not_bit [no_atp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3833 |
\<open>\<not> bit a n\<close> if \<open>2 ^ n = 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3834 |
using that by (simp add: bit_iff_odd) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3835 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3836 |
end |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3837 |
|
| 79116 | 3838 |
context semiring_bit_operations |
3839 |
begin |
|
3840 |
||
| 79588 | 3841 |
lemma even_mask_div_iff [no_atp]: |
3842 |
\<open>even ((2 ^ m - 1) div 2 ^ n) \<longleftrightarrow> 2 ^ n = 0 \<or> m \<le> n\<close> |
|
3843 |
using bit_mask_iff [of m n] by (auto simp add: mask_eq_exp_minus_1 bit_iff_odd possible_bit_def) |
|
3844 |
||
|
79531
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3845 |
lemma mod_exp_eq [no_atp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3846 |
\<open>a mod 2 ^ m mod 2 ^ n = a mod 2 ^ min m n\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3847 |
by (simp flip: take_bit_eq_mod add: ac_simps) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3848 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3849 |
lemma mult_exp_mod_exp_eq [no_atp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3850 |
\<open>m \<le> n \<Longrightarrow> (a * 2 ^ m) mod (2 ^ n) = (a mod 2 ^ (n - m)) * 2 ^ m\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3851 |
by (simp flip: push_bit_eq_mult take_bit_eq_mod add: push_bit_take_bit) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3852 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3853 |
lemma div_exp_mod_exp_eq [no_atp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3854 |
\<open>a div 2 ^ n mod 2 ^ m = a mod (2 ^ (n + m)) div 2 ^ n\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3855 |
by (simp flip: drop_bit_eq_div take_bit_eq_mod add: drop_bit_take_bit) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3856 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3857 |
lemma even_mult_exp_div_exp_iff [no_atp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3858 |
\<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> m > n \<or> 2 ^ n = 0 \<or> (m \<le> n \<and> even (a div 2 ^ (n - m)))\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3859 |
by (simp flip: push_bit_eq_mult drop_bit_eq_div add: even_drop_bit_iff_not_bit bit_simps possible_bit_def) auto |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3860 |
|
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3861 |
lemma mod_exp_div_exp_eq_0 [no_atp]: |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3862 |
\<open>a mod 2 ^ n div 2 ^ n = 0\<close> |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3863 |
by (simp flip: take_bit_eq_mod drop_bit_eq_div add: drop_bit_take_bit) |
|
22a137a6de44
rearranged and reformulated abstract classes for bit structures and operations
haftmann
parents:
79489
diff
changeset
|
3864 |
|
| 79117 | 3865 |
lemmas bits_one_mod_two_eq_one [no_atp] = one_mod_two_eq_one |
3866 |
||
| 79116 | 3867 |
lemmas set_bit_def [no_atp] = set_bit_eq_or |
3868 |
||
3869 |
lemmas unset_bit_def [no_atp] = unset_bit_eq_and_not |
|
3870 |
||
3871 |
lemmas flip_bit_def [no_atp] = flip_bit_eq_xor |
|
3872 |
||
3873 |
end |
|
3874 |
||
3875 |
lemma and_nat_rec [no_atp]: |
|
| 79070 | 3876 |
\<open>m AND n = of_bool (odd m \<and> odd n) + 2 * ((m div 2) AND (n div 2))\<close> for m n :: nat |
3877 |
by (fact and_rec) |
|
3878 |
||
| 79116 | 3879 |
lemma or_nat_rec [no_atp]: |
| 79070 | 3880 |
\<open>m OR n = of_bool (odd m \<or> odd n) + 2 * ((m div 2) OR (n div 2))\<close> for m n :: nat |
3881 |
by (fact or_rec) |
|
3882 |
||
| 79116 | 3883 |
lemma xor_nat_rec [no_atp]: |
| 79070 | 3884 |
\<open>m XOR n = of_bool (odd m \<noteq> odd n) + 2 * ((m div 2) XOR (n div 2))\<close> for m n :: nat |
3885 |
by (fact xor_rec) |
|
3886 |
||
| 79116 | 3887 |
lemma bit_push_bit_iff_nat [no_atp]: |
| 79071 | 3888 |
\<open>bit (push_bit m q) n \<longleftrightarrow> m \<le> n \<and> bit q (n - m)\<close> for q :: nat |
3889 |
by (fact bit_push_bit_iff') |
|
3890 |
||
| 79116 | 3891 |
lemma mask_half_int [no_atp]: |
3892 |
\<open>mask n div 2 = (mask (n - 1) :: int)\<close> |
|
3893 |
by (fact mask_half) |
|
3894 |
||
3895 |
lemma not_int_rec [no_atp]: |
|
| 79068 | 3896 |
\<open>NOT k = of_bool (even k) + 2 * NOT (k div 2)\<close> for k :: int |
3897 |
by (fact not_rec) |
|
3898 |
||
| 79116 | 3899 |
lemma even_not_iff_int [no_atp]: |
| 79068 | 3900 |
\<open>even (NOT k) \<longleftrightarrow> odd k\<close> for k :: int |
3901 |
by (fact even_not_iff) |
|
3902 |
||
|
79072
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
3903 |
lemma bit_not_int_iff': |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
3904 |
\<open>bit (- k - 1) n \<longleftrightarrow> \<not> bit k n\<close> for k :: int |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
3905 |
by (simp flip: not_eq_complement add: bit_simps) |
|
a91050cd5c93
de-duplicated specification of class ring_bit_operations
haftmann
parents:
79071
diff
changeset
|
3906 |
|
| 79116 | 3907 |
lemmas and_int_rec [no_atp] = and_int.rec |
3908 |
||
3909 |
lemma even_and_iff_int [no_atp]: |
|
3910 |
\<open>even (k AND l) \<longleftrightarrow> even k \<or> even l\<close> for k l :: int |
|
3911 |
by (fact even_and_iff) |
|
3912 |
||
3913 |
lemmas bit_and_int_iff [no_atp] = and_int.bit_iff |
|
3914 |
||
3915 |
lemmas or_int_rec [no_atp] = or_int.rec |
|
3916 |
||
3917 |
lemmas bit_or_int_iff [no_atp] = or_int.bit_iff |
|
3918 |
||
3919 |
lemmas xor_int_rec [no_atp] = xor_int.rec |
|
3920 |
||
3921 |
lemmas bit_xor_int_iff [no_atp] = xor_int.bit_iff |
|
3922 |
||
3923 |
lemma drop_bit_push_bit_int [no_atp]: |
|
3924 |
\<open>drop_bit m (push_bit n k) = drop_bit (m - n) (push_bit (n - m) k)\<close> for k :: int |
|
3925 |
by (fact drop_bit_push_bit) |
|
3926 |
||
3927 |
lemma bit_push_bit_iff_int [no_atp] : |
|
3928 |
\<open>bit (push_bit m k) n \<longleftrightarrow> m \<le> n \<and> bit k (n - m)\<close> for k :: int |
|
3929 |
by (fact bit_push_bit_iff') |
|
3930 |
||
| 74097 | 3931 |
no_notation |
| 74391 | 3932 |
not (\<open>NOT\<close>) |
| 74364 | 3933 |
and "and" (infixr \<open>AND\<close> 64) |
| 74097 | 3934 |
and or (infixr \<open>OR\<close> 59) |
3935 |
and xor (infixr \<open>XOR\<close> 59) |
|
3936 |
||
3937 |
bundle bit_operations_syntax |
|
| 74101 | 3938 |
begin |
| 74097 | 3939 |
|
3940 |
notation |
|
| 74391 | 3941 |
not (\<open>NOT\<close>) |
| 74364 | 3942 |
and "and" (infixr \<open>AND\<close> 64) |
| 74097 | 3943 |
and or (infixr \<open>OR\<close> 59) |
3944 |
and xor (infixr \<open>XOR\<close> 59) |
|
3945 |
||
| 71442 | 3946 |
end |
| 74097 | 3947 |
|
3948 |
end |