author | haftmann |
Sat, 17 Oct 2020 18:56:36 +0200 | |
changeset 72488 | ee659bca8955 |
parent 72487 | ab32922f139b |
child 72508 | c89d8e8bd8c7 |
permissions | -rw-r--r-- |
65363 | 1 |
(* Title: HOL/Word/Bits_Int.thy |
2 |
Author: Jeremy Dawson and Gerwin Klein, NICTA |
|
3 |
*) |
|
24333 | 4 |
|
70190 | 5 |
section \<open>Bitwise Operations on integers\<close> |
24350 | 6 |
|
54854
3324a0078636
prefer "Bits" as theory name for abstract bit operations, similar to "Orderings", "Lattices", "Groups" etc.
haftmann
parents:
54848
diff
changeset
|
7 |
theory Bits_Int |
72000 | 8 |
imports |
9 |
"HOL-Library.Bit_Operations" |
|
10 |
Traditional_Syntax |
|
72488 | 11 |
Word |
24333 | 12 |
begin |
13 |
||
70190 | 14 |
subsection \<open>Implicit bit representation of \<^typ>\<open>int\<close>\<close> |
15 |
||
71941 | 16 |
abbreviation (input) bin_last :: "int \<Rightarrow> bool" |
17 |
where "bin_last \<equiv> odd" |
|
18 |
||
19 |
lemma bin_last_def: |
|
20 |
"bin_last w \<longleftrightarrow> w mod 2 = 1" |
|
21 |
by (fact odd_iff_mod_2_eq_one) |
|
70190 | 22 |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
23 |
abbreviation (input) bin_rest :: "int \<Rightarrow> int" |
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
24 |
where "bin_rest w \<equiv> w div 2" |
70190 | 25 |
|
26 |
lemma bin_last_numeral_simps [simp]: |
|
27 |
"\<not> bin_last 0" |
|
28 |
"bin_last 1" |
|
29 |
"bin_last (- 1)" |
|
30 |
"bin_last Numeral1" |
|
31 |
"\<not> bin_last (numeral (Num.Bit0 w))" |
|
32 |
"bin_last (numeral (Num.Bit1 w))" |
|
33 |
"\<not> bin_last (- numeral (Num.Bit0 w))" |
|
34 |
"bin_last (- numeral (Num.Bit1 w))" |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
35 |
by simp_all |
70190 | 36 |
|
37 |
lemma bin_rest_numeral_simps [simp]: |
|
38 |
"bin_rest 0 = 0" |
|
39 |
"bin_rest 1 = 0" |
|
40 |
"bin_rest (- 1) = - 1" |
|
41 |
"bin_rest Numeral1 = 0" |
|
42 |
"bin_rest (numeral (Num.Bit0 w)) = numeral w" |
|
43 |
"bin_rest (numeral (Num.Bit1 w)) = numeral w" |
|
44 |
"bin_rest (- numeral (Num.Bit0 w)) = - numeral w" |
|
45 |
"bin_rest (- numeral (Num.Bit1 w)) = - numeral (w + Num.One)" |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
46 |
by simp_all |
70190 | 47 |
|
48 |
lemma bin_rl_eqI: "\<lbrakk>bin_rest x = bin_rest y; bin_last x = bin_last y\<rbrakk> \<Longrightarrow> x = y" |
|
71986 | 49 |
by (auto elim: oddE) |
70190 | 50 |
|
51 |
lemma [simp]: |
|
52 |
shows bin_rest_lt0: "bin_rest i < 0 \<longleftrightarrow> i < 0" |
|
53 |
and bin_rest_ge_0: "bin_rest i \<ge> 0 \<longleftrightarrow> i \<ge> 0" |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
54 |
by auto |
70190 | 55 |
|
56 |
lemma bin_rest_gt_0 [simp]: "bin_rest x > 0 \<longleftrightarrow> x > 1" |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
57 |
by auto |
70190 | 58 |
|
59 |
||
60 |
subsection \<open>Bit projection\<close> |
|
61 |
||
71949 | 62 |
abbreviation (input) bin_nth :: \<open>int \<Rightarrow> nat \<Rightarrow> bool\<close> |
63 |
where \<open>bin_nth \<equiv> bit\<close> |
|
70190 | 64 |
|
65 |
lemma bin_nth_eq_iff: "bin_nth x = bin_nth y \<longleftrightarrow> x = y" |
|
71949 | 66 |
by (simp add: bit_eq_iff fun_eq_iff) |
70190 | 67 |
|
68 |
lemma bin_eqI: |
|
69 |
"x = y" if "\<And>n. bin_nth x n \<longleftrightarrow> bin_nth y n" |
|
70 |
using that bin_nth_eq_iff [of x y] by (simp add: fun_eq_iff) |
|
71 |
||
72 |
lemma bin_eq_iff: "x = y \<longleftrightarrow> (\<forall>n. bin_nth x n = bin_nth y n)" |
|
71949 | 73 |
by (fact bit_eq_iff) |
70190 | 74 |
|
75 |
lemma bin_nth_zero [simp]: "\<not> bin_nth 0 n" |
|
71949 | 76 |
by simp |
70190 | 77 |
|
78 |
lemma bin_nth_1 [simp]: "bin_nth 1 n \<longleftrightarrow> n = 0" |
|
71949 | 79 |
by (cases n) (simp_all add: bit_Suc) |
70190 | 80 |
|
81 |
lemma bin_nth_minus1 [simp]: "bin_nth (- 1) n" |
|
71949 | 82 |
by (induction n) (simp_all add: bit_Suc) |
70190 | 83 |
|
84 |
lemma bin_nth_numeral: "bin_rest x = y \<Longrightarrow> bin_nth x (numeral n) = bin_nth y (pred_numeral n)" |
|
71949 | 85 |
by (simp add: numeral_eq_Suc bit_Suc) |
70190 | 86 |
|
87 |
lemmas bin_nth_numeral_simps [simp] = |
|
88 |
bin_nth_numeral [OF bin_rest_numeral_simps(2)] |
|
89 |
bin_nth_numeral [OF bin_rest_numeral_simps(5)] |
|
90 |
bin_nth_numeral [OF bin_rest_numeral_simps(6)] |
|
91 |
bin_nth_numeral [OF bin_rest_numeral_simps(7)] |
|
92 |
bin_nth_numeral [OF bin_rest_numeral_simps(8)] |
|
93 |
||
94 |
lemmas bin_nth_simps = |
|
71949 | 95 |
bit_0 bit_Suc bin_nth_zero bin_nth_minus1 |
70190 | 96 |
bin_nth_numeral_simps |
97 |
||
98 |
lemma nth_2p_bin: "bin_nth (2 ^ n) m = (m = n)" \<comment> \<open>for use when simplifying with \<open>bin_nth_Bit\<close>\<close> |
|
71949 | 99 |
by (auto simp add: bit_exp_iff) |
100 |
||
70190 | 101 |
lemma nth_rest_power_bin: "bin_nth ((bin_rest ^^ k) w) n = bin_nth w (n + k)" |
102 |
apply (induct k arbitrary: n) |
|
103 |
apply clarsimp |
|
104 |
apply clarsimp |
|
71949 | 105 |
apply (simp only: bit_Suc [symmetric] add_Suc) |
70190 | 106 |
done |
107 |
||
108 |
lemma bin_nth_numeral_unfold: |
|
109 |
"bin_nth (numeral (num.Bit0 x)) n \<longleftrightarrow> n > 0 \<and> bin_nth (numeral x) (n - 1)" |
|
110 |
"bin_nth (numeral (num.Bit1 x)) n \<longleftrightarrow> (n > 0 \<longrightarrow> bin_nth (numeral x) (n - 1))" |
|
71941 | 111 |
by (cases n; simp)+ |
70190 | 112 |
|
113 |
||
114 |
subsection \<open>Truncating\<close> |
|
115 |
||
116 |
definition bin_sign :: "int \<Rightarrow> int" |
|
117 |
where "bin_sign k = (if k \<ge> 0 then 0 else - 1)" |
|
118 |
||
119 |
lemma bin_sign_simps [simp]: |
|
120 |
"bin_sign 0 = 0" |
|
121 |
"bin_sign 1 = 0" |
|
122 |
"bin_sign (- 1) = - 1" |
|
123 |
"bin_sign (numeral k) = 0" |
|
124 |
"bin_sign (- numeral k) = -1" |
|
71986 | 125 |
by (simp_all add: bin_sign_def) |
70190 | 126 |
|
127 |
lemma bin_sign_rest [simp]: "bin_sign (bin_rest w) = bin_sign w" |
|
71986 | 128 |
by (simp add: bin_sign_def) |
70190 | 129 |
|
72010 | 130 |
abbreviation (input) bintrunc :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
71947 | 131 |
where \<open>bintrunc \<equiv> take_bit\<close> |
70190 | 132 |
|
71986 | 133 |
lemma bintrunc_mod2p: "bintrunc n w = w mod 2 ^ n" |
134 |
by (fact take_bit_eq_mod) |
|
135 |
||
72010 | 136 |
abbreviation (input) sbintrunc :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
137 |
where \<open>sbintrunc \<equiv> signed_take_bit\<close> |
|
70190 | 138 |
|
72042 | 139 |
abbreviation (input) norm_sint :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close> |
140 |
where \<open>norm_sint n \<equiv> signed_take_bit (n - 1)\<close> |
|
141 |
||
70190 | 142 |
lemma sbintrunc_mod2p: "sbintrunc n w = (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n" |
72010 | 143 |
by (simp add: bintrunc_mod2p signed_take_bit_eq_take_bit_shift) |
144 |
||
71997 | 145 |
lemma sbintrunc_eq_take_bit: |
146 |
\<open>sbintrunc n k = take_bit (Suc n) (k + 2 ^ n) - 2 ^ n\<close> |
|
72010 | 147 |
by (fact signed_take_bit_eq_take_bit_shift) |
71997 | 148 |
|
70190 | 149 |
lemma sign_bintr: "bin_sign (bintrunc n w) = 0" |
72010 | 150 |
by (simp add: bin_sign_def) |
70190 | 151 |
|
72010 | 152 |
lemma bintrunc_n_0: "bintrunc n 0 = 0" |
153 |
by (fact take_bit_of_0) |
|
70190 | 154 |
|
72010 | 155 |
lemma sbintrunc_n_0: "sbintrunc n 0 = 0" |
156 |
by (fact signed_take_bit_of_0) |
|
70190 | 157 |
|
72010 | 158 |
lemma sbintrunc_n_minus1: "sbintrunc n (- 1) = -1" |
159 |
by (fact signed_take_bit_of_minus_1) |
|
70190 | 160 |
|
161 |
lemma bintrunc_Suc_numeral: |
|
162 |
"bintrunc (Suc n) 1 = 1" |
|
71986 | 163 |
"bintrunc (Suc n) (- 1) = 1 + 2 * bintrunc n (- 1)" |
164 |
"bintrunc (Suc n) (numeral (Num.Bit0 w)) = 2 * bintrunc n (numeral w)" |
|
165 |
"bintrunc (Suc n) (numeral (Num.Bit1 w)) = 1 + 2 * bintrunc n (numeral w)" |
|
166 |
"bintrunc (Suc n) (- numeral (Num.Bit0 w)) = 2 * bintrunc n (- numeral w)" |
|
167 |
"bintrunc (Suc n) (- numeral (Num.Bit1 w)) = 1 + 2 * bintrunc n (- numeral (w + Num.One))" |
|
168 |
by (simp_all add: take_bit_Suc) |
|
70190 | 169 |
|
170 |
lemma sbintrunc_0_numeral [simp]: |
|
171 |
"sbintrunc 0 1 = -1" |
|
172 |
"sbintrunc 0 (numeral (Num.Bit0 w)) = 0" |
|
173 |
"sbintrunc 0 (numeral (Num.Bit1 w)) = -1" |
|
174 |
"sbintrunc 0 (- numeral (Num.Bit0 w)) = 0" |
|
175 |
"sbintrunc 0 (- numeral (Num.Bit1 w)) = -1" |
|
176 |
by simp_all |
|
177 |
||
178 |
lemma sbintrunc_Suc_numeral: |
|
179 |
"sbintrunc (Suc n) 1 = 1" |
|
71986 | 180 |
"sbintrunc (Suc n) (numeral (Num.Bit0 w)) = 2 * sbintrunc n (numeral w)" |
181 |
"sbintrunc (Suc n) (numeral (Num.Bit1 w)) = 1 + 2 * sbintrunc n (numeral w)" |
|
182 |
"sbintrunc (Suc n) (- numeral (Num.Bit0 w)) = 2 * sbintrunc n (- numeral w)" |
|
183 |
"sbintrunc (Suc n) (- numeral (Num.Bit1 w)) = 1 + 2 * sbintrunc n (- numeral (w + Num.One))" |
|
72010 | 184 |
by (simp_all add: signed_take_bit_Suc) |
70190 | 185 |
|
72010 | 186 |
lemma bin_sign_lem: "(bin_sign (sbintrunc n bin) = -1) = bit bin n" |
72028 | 187 |
by (simp add: bin_sign_def) |
70190 | 188 |
|
189 |
lemma nth_bintr: "bin_nth (bintrunc m w) n \<longleftrightarrow> n < m \<and> bin_nth w n" |
|
71949 | 190 |
by (fact bit_take_bit_iff) |
70190 | 191 |
|
192 |
lemma nth_sbintr: "bin_nth (sbintrunc m w) n = (if n < m then bin_nth w n else bin_nth w m)" |
|
72010 | 193 |
by (simp add: bit_signed_take_bit_iff min_def) |
70190 | 194 |
|
195 |
lemma bin_nth_Bit0: |
|
196 |
"bin_nth (numeral (Num.Bit0 w)) n \<longleftrightarrow> |
|
197 |
(\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)" |
|
71986 | 198 |
using bit_double_iff [of \<open>numeral w :: int\<close> n] |
199 |
by (auto intro: exI [of _ \<open>n - 1\<close>]) |
|
70190 | 200 |
|
201 |
lemma bin_nth_Bit1: |
|
202 |
"bin_nth (numeral (Num.Bit1 w)) n \<longleftrightarrow> |
|
203 |
n = 0 \<or> (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)" |
|
71986 | 204 |
using even_bit_succ_iff [of \<open>2 * numeral w :: int\<close> n] |
205 |
bit_double_iff [of \<open>numeral w :: int\<close> n] |
|
206 |
by auto |
|
70190 | 207 |
|
208 |
lemma bintrunc_bintrunc_l: "n \<le> m \<Longrightarrow> bintrunc m (bintrunc n w) = bintrunc n w" |
|
71947 | 209 |
by (simp add: min.absorb2) |
70190 | 210 |
|
211 |
lemma sbintrunc_sbintrunc_l: "n \<le> m \<Longrightarrow> sbintrunc m (sbintrunc n w) = sbintrunc n w" |
|
72010 | 212 |
by (simp add: min_def) |
70190 | 213 |
|
214 |
lemma bintrunc_bintrunc_ge: "n \<le> m \<Longrightarrow> bintrunc n (bintrunc m w) = bintrunc n w" |
|
215 |
by (rule bin_eqI) (auto simp: nth_bintr) |
|
216 |
||
217 |
lemma bintrunc_bintrunc_min [simp]: "bintrunc m (bintrunc n w) = bintrunc (min m n) w" |
|
218 |
by (rule bin_eqI) (auto simp: nth_bintr) |
|
219 |
||
220 |
lemma sbintrunc_sbintrunc_min [simp]: "sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w" |
|
221 |
by (rule bin_eqI) (auto simp: nth_sbintr min.absorb1 min.absorb2) |
|
222 |
||
223 |
lemmas sbintrunc_Suc_Pls = |
|
72241 | 224 |
signed_take_bit_Suc [where a="0::int", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
70190 | 225 |
|
226 |
lemmas sbintrunc_Suc_Min = |
|
72241 | 227 |
signed_take_bit_Suc [where a="-1::int", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
70190 | 228 |
|
71986 | 229 |
lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min |
70190 | 230 |
sbintrunc_Suc_numeral |
231 |
||
232 |
lemmas sbintrunc_Pls = |
|
72241 | 233 |
signed_take_bit_0 [where a="0::int", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
70190 | 234 |
|
235 |
lemmas sbintrunc_Min = |
|
72241 | 236 |
signed_take_bit_0 [where a="-1::int", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
70190 | 237 |
|
238 |
lemmas sbintrunc_0_simps = |
|
71986 | 239 |
sbintrunc_Pls sbintrunc_Min |
70190 | 240 |
|
241 |
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs |
|
242 |
||
243 |
lemma bintrunc_minus: "0 < n \<Longrightarrow> bintrunc (Suc (n - 1)) w = bintrunc n w" |
|
244 |
by auto |
|
245 |
||
246 |
lemma sbintrunc_minus: "0 < n \<Longrightarrow> sbintrunc (Suc (n - 1)) w = sbintrunc n w" |
|
247 |
by auto |
|
248 |
||
249 |
lemmas sbintrunc_minus_simps = |
|
250 |
sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans]] |
|
251 |
||
71984 | 252 |
lemma sbintrunc_BIT_I: |
253 |
\<open>0 < n \<Longrightarrow> |
|
254 |
sbintrunc (n - 1) 0 = y \<Longrightarrow> |
|
255 |
sbintrunc n 0 = 2 * y\<close> |
|
256 |
by simp |
|
257 |
||
258 |
lemma sbintrunc_Suc_Is: |
|
259 |
\<open>sbintrunc n (- 1) = y \<Longrightarrow> |
|
260 |
sbintrunc (Suc n) (- 1) = 1 + 2 * y\<close> |
|
71986 | 261 |
by auto |
70190 | 262 |
|
263 |
lemma sbintrunc_Suc_lem: "sbintrunc (Suc n) x = y \<Longrightarrow> m = Suc n \<Longrightarrow> sbintrunc m x = y" |
|
264 |
by auto |
|
265 |
||
266 |
lemmas sbintrunc_Suc_Ialts = |
|
267 |
sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem] |
|
268 |
||
269 |
lemma sbintrunc_bintrunc_lt: "m > n \<Longrightarrow> sbintrunc n (bintrunc m w) = sbintrunc n w" |
|
270 |
by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr) |
|
271 |
||
272 |
lemma bintrunc_sbintrunc_le: "m \<le> Suc n \<Longrightarrow> bintrunc m (sbintrunc n w) = bintrunc m w" |
|
273 |
apply (rule bin_eqI) |
|
274 |
using le_Suc_eq less_Suc_eq_le apply (auto simp: nth_sbintr nth_bintr) |
|
275 |
done |
|
276 |
||
277 |
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le] |
|
278 |
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt] |
|
279 |
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l] |
|
280 |
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l] |
|
281 |
||
282 |
lemma bintrunc_sbintrunc' [simp]: "0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w" |
|
71947 | 283 |
by (cases n) simp_all |
70190 | 284 |
|
285 |
lemma sbintrunc_bintrunc' [simp]: "0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w" |
|
71947 | 286 |
by (cases n) simp_all |
70190 | 287 |
|
288 |
lemma bin_sbin_eq_iff: "bintrunc (Suc n) x = bintrunc (Suc n) y \<longleftrightarrow> sbintrunc n x = sbintrunc n y" |
|
289 |
apply (rule iffI) |
|
290 |
apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc]) |
|
291 |
apply simp |
|
292 |
apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc]) |
|
293 |
apply simp |
|
294 |
done |
|
295 |
||
296 |
lemma bin_sbin_eq_iff': |
|
297 |
"0 < n \<Longrightarrow> bintrunc n x = bintrunc n y \<longleftrightarrow> sbintrunc (n - 1) x = sbintrunc (n - 1) y" |
|
71947 | 298 |
by (cases n) (simp_all add: bin_sbin_eq_iff) |
70190 | 299 |
|
300 |
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def] |
|
301 |
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def] |
|
302 |
||
303 |
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l] |
|
304 |
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l] |
|
305 |
||
306 |
(* although bintrunc_minus_simps, if added to default simpset, |
|
307 |
tends to get applied where it's not wanted in developing the theories, |
|
308 |
we get a version for when the word length is given literally *) |
|
309 |
||
310 |
lemmas nat_non0_gr = |
|
311 |
trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl] |
|
312 |
||
313 |
lemma bintrunc_numeral: |
|
71986 | 314 |
"bintrunc (numeral k) x = of_bool (odd x) + 2 * bintrunc (pred_numeral k) (x div 2)" |
315 |
by (simp add: numeral_eq_Suc take_bit_Suc mod_2_eq_odd) |
|
70190 | 316 |
|
317 |
lemma sbintrunc_numeral: |
|
71986 | 318 |
"sbintrunc (numeral k) x = of_bool (odd x) + 2 * sbintrunc (pred_numeral k) (x div 2)" |
72010 | 319 |
by (simp add: numeral_eq_Suc signed_take_bit_Suc mod2_eq_if) |
70190 | 320 |
|
321 |
lemma bintrunc_numeral_simps [simp]: |
|
71986 | 322 |
"bintrunc (numeral k) (numeral (Num.Bit0 w)) = |
323 |
2 * bintrunc (pred_numeral k) (numeral w)" |
|
324 |
"bintrunc (numeral k) (numeral (Num.Bit1 w)) = |
|
325 |
1 + 2 * bintrunc (pred_numeral k) (numeral w)" |
|
326 |
"bintrunc (numeral k) (- numeral (Num.Bit0 w)) = |
|
327 |
2 * bintrunc (pred_numeral k) (- numeral w)" |
|
70190 | 328 |
"bintrunc (numeral k) (- numeral (Num.Bit1 w)) = |
71986 | 329 |
1 + 2 * bintrunc (pred_numeral k) (- numeral (w + Num.One))" |
70190 | 330 |
"bintrunc (numeral k) 1 = 1" |
331 |
by (simp_all add: bintrunc_numeral) |
|
332 |
||
333 |
lemma sbintrunc_numeral_simps [simp]: |
|
71986 | 334 |
"sbintrunc (numeral k) (numeral (Num.Bit0 w)) = |
335 |
2 * sbintrunc (pred_numeral k) (numeral w)" |
|
336 |
"sbintrunc (numeral k) (numeral (Num.Bit1 w)) = |
|
337 |
1 + 2 * sbintrunc (pred_numeral k) (numeral w)" |
|
70190 | 338 |
"sbintrunc (numeral k) (- numeral (Num.Bit0 w)) = |
71986 | 339 |
2 * sbintrunc (pred_numeral k) (- numeral w)" |
70190 | 340 |
"sbintrunc (numeral k) (- numeral (Num.Bit1 w)) = |
71986 | 341 |
1 + 2 * sbintrunc (pred_numeral k) (- numeral (w + Num.One))" |
70190 | 342 |
"sbintrunc (numeral k) 1 = 1" |
343 |
by (simp_all add: sbintrunc_numeral) |
|
344 |
||
345 |
lemma no_bintr_alt1: "bintrunc n = (\<lambda>w. w mod 2 ^ n :: int)" |
|
346 |
by (rule ext) (rule bintrunc_mod2p) |
|
347 |
||
348 |
lemma range_bintrunc: "range (bintrunc n) = {i. 0 \<le> i \<and> i < 2 ^ n}" |
|
71997 | 349 |
by (auto simp add: take_bit_eq_mod image_iff) (metis mod_pos_pos_trivial) |
70190 | 350 |
|
351 |
lemma no_sbintr_alt2: "sbintrunc n = (\<lambda>w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)" |
|
352 |
by (rule ext) (simp add : sbintrunc_mod2p) |
|
353 |
||
354 |
lemma range_sbintrunc: "range (sbintrunc n) = {i. - (2 ^ n) \<le> i \<and> i < 2 ^ n}" |
|
71997 | 355 |
proof - |
356 |
have \<open>surj (\<lambda>k::int. k + 2 ^ n)\<close> |
|
357 |
by (rule surjI [of _ \<open>(\<lambda>k. k - 2 ^ n)\<close>]) simp |
|
358 |
moreover have \<open>sbintrunc n = ((\<lambda>k. k - 2 ^ n) \<circ> take_bit (Suc n) \<circ> (\<lambda>k. k + 2 ^ n))\<close> |
|
359 |
by (simp add: sbintrunc_eq_take_bit fun_eq_iff) |
|
360 |
ultimately show ?thesis |
|
361 |
apply (simp only: fun.set_map range_bintrunc) |
|
362 |
apply (auto simp add: image_iff) |
|
363 |
apply presburger |
|
364 |
done |
|
365 |
qed |
|
72010 | 366 |
|
71997 | 367 |
lemma sbintrunc_inc: |
368 |
\<open>k + 2 ^ Suc n \<le> sbintrunc n k\<close> if \<open>k < - (2 ^ n)\<close> |
|
72261 | 369 |
using that by (fact signed_take_bit_int_greater_eq) |
72010 | 370 |
|
71997 | 371 |
lemma sbintrunc_dec: |
372 |
\<open>sbintrunc n k \<le> k - 2 ^ (Suc n)\<close> if \<open>k \<ge> 2 ^ n\<close> |
|
72261 | 373 |
using that by (fact signed_take_bit_int_less_eq) |
70190 | 374 |
|
375 |
lemma bintr_ge0: "0 \<le> bintrunc n w" |
|
376 |
by (simp add: bintrunc_mod2p) |
|
377 |
||
378 |
lemma bintr_lt2p: "bintrunc n w < 2 ^ n" |
|
379 |
by (simp add: bintrunc_mod2p) |
|
380 |
||
381 |
lemma bintr_Min: "bintrunc n (- 1) = 2 ^ n - 1" |
|
71997 | 382 |
by (simp add: stable_imp_take_bit_eq) |
383 |
||
70190 | 384 |
lemma sbintr_ge: "- (2 ^ n) \<le> sbintrunc n w" |
385 |
by (simp add: sbintrunc_mod2p) |
|
386 |
||
387 |
lemma sbintr_lt: "sbintrunc n w < 2 ^ n" |
|
388 |
by (simp add: sbintrunc_mod2p) |
|
389 |
||
390 |
lemma sign_Pls_ge_0: "bin_sign bin = 0 \<longleftrightarrow> bin \<ge> 0" |
|
391 |
for bin :: int |
|
392 |
by (simp add: bin_sign_def) |
|
393 |
||
394 |
lemma sign_Min_lt_0: "bin_sign bin = -1 \<longleftrightarrow> bin < 0" |
|
395 |
for bin :: int |
|
396 |
by (simp add: bin_sign_def) |
|
397 |
||
398 |
lemma bin_rest_trunc: "bin_rest (bintrunc n bin) = bintrunc (n - 1) (bin_rest bin)" |
|
71947 | 399 |
by (simp add: take_bit_rec [of n bin]) |
70190 | 400 |
|
401 |
lemma bin_rest_power_trunc: |
|
402 |
"(bin_rest ^^ k) (bintrunc n bin) = bintrunc (n - k) ((bin_rest ^^ k) bin)" |
|
403 |
by (induct k) (auto simp: bin_rest_trunc) |
|
404 |
||
405 |
lemma bin_rest_trunc_i: "bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)" |
|
71947 | 406 |
by (auto simp add: take_bit_Suc) |
70190 | 407 |
|
408 |
lemma bin_rest_strunc: "bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)" |
|
72010 | 409 |
by (simp add: signed_take_bit_Suc) |
70190 | 410 |
|
411 |
lemma bintrunc_rest [simp]: "bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)" |
|
71947 | 412 |
by (induct n arbitrary: bin) (simp_all add: take_bit_Suc) |
70190 | 413 |
|
414 |
lemma sbintrunc_rest [simp]: "sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)" |
|
72010 | 415 |
by (induct n arbitrary: bin) (simp_all add: signed_take_bit_Suc mod2_eq_if) |
70190 | 416 |
|
417 |
lemma bintrunc_rest': "bintrunc n \<circ> bin_rest \<circ> bintrunc n = bin_rest \<circ> bintrunc n" |
|
418 |
by (rule ext) auto |
|
419 |
||
420 |
lemma sbintrunc_rest': "sbintrunc n \<circ> bin_rest \<circ> sbintrunc n = bin_rest \<circ> sbintrunc n" |
|
421 |
by (rule ext) auto |
|
422 |
||
423 |
lemma rco_lem: "f \<circ> g \<circ> f = g \<circ> f \<Longrightarrow> f \<circ> (g \<circ> f) ^^ n = g ^^ n \<circ> f" |
|
424 |
apply (rule ext) |
|
425 |
apply (induct_tac n) |
|
426 |
apply (simp_all (no_asm)) |
|
427 |
apply (drule fun_cong) |
|
428 |
apply (unfold o_def) |
|
429 |
apply (erule trans) |
|
430 |
apply simp |
|
431 |
done |
|
432 |
||
433 |
lemmas rco_bintr = bintrunc_rest' |
|
434 |
[THEN rco_lem [THEN fun_cong], unfolded o_def] |
|
435 |
lemmas rco_sbintr = sbintrunc_rest' |
|
436 |
[THEN rco_lem [THEN fun_cong], unfolded o_def] |
|
437 |
||
438 |
||
439 |
subsection \<open>Splitting and concatenation\<close> |
|
440 |
||
71944 | 441 |
definition bin_split :: \<open>nat \<Rightarrow> int \<Rightarrow> int \<times> int\<close> |
442 |
where [simp]: \<open>bin_split n k = (drop_bit n k, take_bit n k)\<close> |
|
71943 | 443 |
|
70190 | 444 |
lemma [code]: |
71986 | 445 |
"bin_split (Suc n) w = (let (w1, w2) = bin_split n (w div 2) in (w1, of_bool (odd w) + 2 * w2))" |
70190 | 446 |
"bin_split 0 w = (w, 0)" |
71986 | 447 |
by (simp_all add: drop_bit_Suc take_bit_Suc mod_2_eq_odd) |
70190 | 448 |
|
72028 | 449 |
abbreviation (input) bin_cat :: \<open>int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int\<close> |
450 |
where \<open>bin_cat k n l \<equiv> concat_bit n l k\<close> |
|
70190 | 451 |
|
71943 | 452 |
lemma bin_cat_eq_push_bit_add_take_bit: |
453 |
\<open>bin_cat k n l = push_bit n k + take_bit n l\<close> |
|
72028 | 454 |
by (simp add: concat_bit_eq) |
455 |
||
70190 | 456 |
lemma bin_sign_cat: "bin_sign (bin_cat x n y) = bin_sign x" |
71986 | 457 |
proof - |
458 |
have \<open>0 \<le> x\<close> if \<open>0 \<le> x * 2 ^ n + y mod 2 ^ n\<close> |
|
459 |
proof - |
|
71997 | 460 |
have \<open>y mod 2 ^ n < 2 ^ n\<close> |
461 |
using pos_mod_bound [of \<open>2 ^ n\<close> y] by simp |
|
462 |
then have \<open>\<not> y mod 2 ^ n \<ge> 2 ^ n\<close> |
|
463 |
by (simp add: less_le) |
|
464 |
with that have \<open>x \<noteq> - 1\<close> |
|
465 |
by auto |
|
71986 | 466 |
have *: \<open>- 1 \<le> (- (y mod 2 ^ n)) div 2 ^ n\<close> |
467 |
by (simp add: zdiv_zminus1_eq_if) |
|
468 |
from that have \<open>- (y mod 2 ^ n) \<le> x * 2 ^ n\<close> |
|
469 |
by simp |
|
470 |
then have \<open>(- (y mod 2 ^ n)) div 2 ^ n \<le> (x * 2 ^ n) div 2 ^ n\<close> |
|
471 |
using zdiv_mono1 zero_less_numeral zero_less_power by blast |
|
472 |
with * have \<open>- 1 \<le> x * 2 ^ n div 2 ^ n\<close> by simp |
|
473 |
with \<open>x \<noteq> - 1\<close> show ?thesis |
|
474 |
by simp |
|
475 |
qed |
|
476 |
then show ?thesis |
|
477 |
by (simp add: bin_sign_def not_le not_less bin_cat_eq_push_bit_add_take_bit push_bit_eq_mult take_bit_eq_mod) |
|
478 |
qed |
|
70190 | 479 |
|
480 |
lemma bin_cat_assoc: "bin_cat (bin_cat x m y) n z = bin_cat x (m + n) (bin_cat y n z)" |
|
72028 | 481 |
by (fact concat_bit_assoc) |
70190 | 482 |
|
483 |
lemma bin_cat_assoc_sym: "bin_cat x m (bin_cat y n z) = bin_cat (bin_cat x (m - n) y) (min m n) z" |
|
72028 | 484 |
by (fact concat_bit_assoc_sym) |
70190 | 485 |
|
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72081
diff
changeset
|
486 |
definition bin_rcat :: \<open>nat \<Rightarrow> int list \<Rightarrow> int\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72081
diff
changeset
|
487 |
where \<open>bin_rcat n = horner_sum (take_bit n) (2 ^ n) \<circ> rev\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72081
diff
changeset
|
488 |
|
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72081
diff
changeset
|
489 |
lemma bin_rcat_eq_foldl: |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72081
diff
changeset
|
490 |
\<open>bin_rcat n = foldl (\<lambda>u v. bin_cat u n v) 0\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72081
diff
changeset
|
491 |
proof |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72081
diff
changeset
|
492 |
fix ks :: \<open>int list\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72081
diff
changeset
|
493 |
show \<open>bin_rcat n ks = foldl (\<lambda>u v. bin_cat u n v) 0 ks\<close> |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72081
diff
changeset
|
494 |
by (induction ks rule: rev_induct) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72081
diff
changeset
|
495 |
(simp_all add: bin_rcat_def concat_bit_eq push_bit_eq_mult) |
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72081
diff
changeset
|
496 |
qed |
70190 | 497 |
|
498 |
fun bin_rsplit_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" |
|
499 |
where "bin_rsplit_aux n m c bs = |
|
500 |
(if m = 0 \<or> n = 0 then bs |
|
501 |
else |
|
502 |
let (a, b) = bin_split n c |
|
503 |
in bin_rsplit_aux n (m - n) a (b # bs))" |
|
504 |
||
505 |
definition bin_rsplit :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list" |
|
506 |
where "bin_rsplit n w = bin_rsplit_aux n (fst w) (snd w) []" |
|
507 |
||
72487
ab32922f139b
factored out singular operation into separate theory
haftmann
parents:
72261
diff
changeset
|
508 |
value \<open>bin_rsplit 1705 (3, 88)\<close> |
ab32922f139b
factored out singular operation into separate theory
haftmann
parents:
72261
diff
changeset
|
509 |
|
70190 | 510 |
fun bin_rsplitl_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" |
511 |
where "bin_rsplitl_aux n m c bs = |
|
512 |
(if m = 0 \<or> n = 0 then bs |
|
513 |
else |
|
514 |
let (a, b) = bin_split (min m n) c |
|
515 |
in bin_rsplitl_aux n (m - n) a (b # bs))" |
|
516 |
||
517 |
definition bin_rsplitl :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list" |
|
518 |
where "bin_rsplitl n w = bin_rsplitl_aux n (fst w) (snd w) []" |
|
519 |
||
520 |
declare bin_rsplit_aux.simps [simp del] |
|
521 |
declare bin_rsplitl_aux.simps [simp del] |
|
522 |
||
523 |
lemma bin_nth_cat: |
|
524 |
"bin_nth (bin_cat x k y) n = |
|
525 |
(if n < k then bin_nth y n else bin_nth x (n - k))" |
|
72028 | 526 |
by (simp add: bit_concat_bit_iff) |
70190 | 527 |
|
71944 | 528 |
lemma bin_nth_drop_bit_iff: |
529 |
\<open>bin_nth (drop_bit n c) k \<longleftrightarrow> bin_nth c (n + k)\<close> |
|
71949 | 530 |
by (simp add: bit_drop_bit_eq) |
71944 | 531 |
|
532 |
lemma bin_nth_take_bit_iff: |
|
533 |
\<open>bin_nth (take_bit n c) k \<longleftrightarrow> k < n \<and> bin_nth c k\<close> |
|
71949 | 534 |
by (fact bit_take_bit_iff) |
71944 | 535 |
|
70190 | 536 |
lemma bin_nth_split: |
537 |
"bin_split n c = (a, b) \<Longrightarrow> |
|
538 |
(\<forall>k. bin_nth a k = bin_nth c (n + k)) \<and> |
|
539 |
(\<forall>k. bin_nth b k = (k < n \<and> bin_nth c k))" |
|
71944 | 540 |
by (auto simp add: bin_nth_drop_bit_iff bin_nth_take_bit_iff) |
70190 | 541 |
|
542 |
lemma bin_cat_zero [simp]: "bin_cat 0 n w = bintrunc n w" |
|
71947 | 543 |
by (simp add: bin_cat_eq_push_bit_add_take_bit) |
70190 | 544 |
|
545 |
lemma bintr_cat1: "bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b" |
|
71944 | 546 |
by (metis bin_cat_assoc bin_cat_zero) |
70190 | 547 |
|
548 |
lemma bintr_cat: "bintrunc m (bin_cat a n b) = |
|
549 |
bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)" |
|
72028 | 550 |
|
70190 | 551 |
by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr) |
552 |
||
553 |
lemma bintr_cat_same [simp]: "bintrunc n (bin_cat a n b) = bintrunc n b" |
|
554 |
by (auto simp add : bintr_cat) |
|
555 |
||
556 |
lemma cat_bintr [simp]: "bin_cat a n (bintrunc n b) = bin_cat a n b" |
|
71947 | 557 |
by (simp add: bin_cat_eq_push_bit_add_take_bit) |
70190 | 558 |
|
559 |
lemma split_bintrunc: "bin_split n c = (a, b) \<Longrightarrow> b = bintrunc n c" |
|
71947 | 560 |
by simp |
70190 | 561 |
|
562 |
lemma bin_cat_split: "bin_split n w = (u, v) \<Longrightarrow> w = bin_cat u n v" |
|
71944 | 563 |
by (auto simp add: bin_cat_eq_push_bit_add_take_bit bits_ident) |
564 |
||
565 |
lemma drop_bit_bin_cat_eq: |
|
566 |
\<open>drop_bit n (bin_cat v n w) = v\<close> |
|
72028 | 567 |
by (rule bit_eqI) (simp add: bit_drop_bit_eq bit_concat_bit_iff) |
71944 | 568 |
|
569 |
lemma take_bit_bin_cat_eq: |
|
570 |
\<open>take_bit n (bin_cat v n w) = take_bit n w\<close> |
|
72028 | 571 |
by (rule bit_eqI) (simp add: bit_concat_bit_iff) |
70190 | 572 |
|
573 |
lemma bin_split_cat: "bin_split n (bin_cat v n w) = (v, bintrunc n w)" |
|
71947 | 574 |
by (simp add: drop_bit_bin_cat_eq take_bit_bin_cat_eq) |
70190 | 575 |
|
576 |
lemma bin_split_zero [simp]: "bin_split n 0 = (0, 0)" |
|
71944 | 577 |
by simp |
70190 | 578 |
|
579 |
lemma bin_split_minus1 [simp]: |
|
580 |
"bin_split n (- 1) = (- 1, bintrunc n (- 1))" |
|
71947 | 581 |
by simp |
70190 | 582 |
|
583 |
lemma bin_split_trunc: |
|
584 |
"bin_split (min m n) c = (a, b) \<Longrightarrow> |
|
585 |
bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)" |
|
586 |
apply (induct n arbitrary: m b c, clarsimp) |
|
587 |
apply (simp add: bin_rest_trunc Let_def split: prod.split_asm) |
|
588 |
apply (case_tac m) |
|
71946 | 589 |
apply (auto simp: Let_def drop_bit_Suc take_bit_Suc mod_2_eq_odd split: prod.split_asm) |
70190 | 590 |
done |
591 |
||
592 |
lemma bin_split_trunc1: |
|
593 |
"bin_split n c = (a, b) \<Longrightarrow> |
|
594 |
bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)" |
|
595 |
apply (induct n arbitrary: m b c, clarsimp) |
|
596 |
apply (simp add: bin_rest_trunc Let_def split: prod.split_asm) |
|
597 |
apply (case_tac m) |
|
71986 | 598 |
apply (auto simp: Let_def drop_bit_Suc take_bit_Suc mod_2_eq_odd split: prod.split_asm) |
70190 | 599 |
done |
600 |
||
601 |
lemma bin_cat_num: "bin_cat a n b = a * 2 ^ n + bintrunc n b" |
|
71947 | 602 |
by (simp add: bin_cat_eq_push_bit_add_take_bit push_bit_eq_mult) |
70190 | 603 |
|
604 |
lemma bin_split_num: "bin_split n b = (b div 2 ^ n, b mod 2 ^ n)" |
|
71944 | 605 |
by (simp add: drop_bit_eq_div take_bit_eq_mod) |
70190 | 606 |
|
607 |
lemmas bin_rsplit_aux_simps = bin_rsplit_aux.simps bin_rsplitl_aux.simps |
|
608 |
lemmas rsplit_aux_simps = bin_rsplit_aux_simps |
|
609 |
||
610 |
lemmas th_if_simp1 = if_split [where P = "(=) l", THEN iffD1, THEN conjunct1, THEN mp] for l |
|
611 |
lemmas th_if_simp2 = if_split [where P = "(=) l", THEN iffD1, THEN conjunct2, THEN mp] for l |
|
612 |
||
613 |
lemmas rsplit_aux_simp1s = rsplit_aux_simps [THEN th_if_simp1] |
|
614 |
||
615 |
lemmas rsplit_aux_simp2ls = rsplit_aux_simps [THEN th_if_simp2] |
|
616 |
\<comment> \<open>these safe to \<open>[simp add]\<close> as require calculating \<open>m - n\<close>\<close> |
|
617 |
lemmas bin_rsplit_aux_simp2s [simp] = rsplit_aux_simp2ls [unfolded Let_def] |
|
618 |
lemmas rbscl = bin_rsplit_aux_simp2s (2) |
|
619 |
||
620 |
lemmas rsplit_aux_0_simps [simp] = |
|
621 |
rsplit_aux_simp1s [OF disjI1] rsplit_aux_simp1s [OF disjI2] |
|
622 |
||
623 |
lemma bin_rsplit_aux_append: "bin_rsplit_aux n m c (bs @ cs) = bin_rsplit_aux n m c bs @ cs" |
|
624 |
apply (induct n m c bs rule: bin_rsplit_aux.induct) |
|
625 |
apply (subst bin_rsplit_aux.simps) |
|
626 |
apply (subst bin_rsplit_aux.simps) |
|
627 |
apply (clarsimp split: prod.split) |
|
628 |
done |
|
629 |
||
630 |
lemma bin_rsplitl_aux_append: "bin_rsplitl_aux n m c (bs @ cs) = bin_rsplitl_aux n m c bs @ cs" |
|
631 |
apply (induct n m c bs rule: bin_rsplitl_aux.induct) |
|
632 |
apply (subst bin_rsplitl_aux.simps) |
|
633 |
apply (subst bin_rsplitl_aux.simps) |
|
634 |
apply (clarsimp split: prod.split) |
|
635 |
done |
|
636 |
||
637 |
lemmas rsplit_aux_apps [where bs = "[]"] = |
|
638 |
bin_rsplit_aux_append bin_rsplitl_aux_append |
|
639 |
||
640 |
lemmas rsplit_def_auxs = bin_rsplit_def bin_rsplitl_def |
|
641 |
||
642 |
lemmas rsplit_aux_alts = rsplit_aux_apps |
|
643 |
[unfolded append_Nil rsplit_def_auxs [symmetric]] |
|
644 |
||
645 |
lemma bin_split_minus: "0 < n \<Longrightarrow> bin_split (Suc (n - 1)) w = bin_split n w" |
|
646 |
by auto |
|
647 |
||
648 |
lemma bin_split_pred_simp [simp]: |
|
649 |
"(0::nat) < numeral bin \<Longrightarrow> |
|
650 |
bin_split (numeral bin) w = |
|
651 |
(let (w1, w2) = bin_split (numeral bin - 1) (bin_rest w) |
|
71986 | 652 |
in (w1, of_bool (odd w) + 2 * w2))" |
653 |
by (simp add: take_bit_rec drop_bit_rec mod_2_eq_odd) |
|
70190 | 654 |
|
655 |
lemma bin_rsplit_aux_simp_alt: |
|
656 |
"bin_rsplit_aux n m c bs = |
|
657 |
(if m = 0 \<or> n = 0 then bs |
|
658 |
else let (a, b) = bin_split n c in bin_rsplit n (m - n, a) @ b # bs)" |
|
659 |
apply (simp add: bin_rsplit_aux.simps [of n m c bs]) |
|
660 |
apply (subst rsplit_aux_alts) |
|
661 |
apply (simp add: bin_rsplit_def) |
|
662 |
done |
|
663 |
||
664 |
lemmas bin_rsplit_simp_alt = |
|
665 |
trans [OF bin_rsplit_def bin_rsplit_aux_simp_alt] |
|
666 |
||
667 |
lemmas bthrs = bin_rsplit_simp_alt [THEN [2] trans] |
|
668 |
||
669 |
lemma bin_rsplit_size_sign' [rule_format]: |
|
670 |
"n > 0 \<Longrightarrow> rev sw = bin_rsplit n (nw, w) \<Longrightarrow> \<forall>v\<in>set sw. bintrunc n v = v" |
|
671 |
apply (induct sw arbitrary: nw w) |
|
672 |
apply clarsimp |
|
673 |
apply clarsimp |
|
674 |
apply (drule bthrs) |
|
675 |
apply (simp (no_asm_use) add: Let_def split: prod.split_asm if_split_asm) |
|
676 |
apply clarify |
|
71947 | 677 |
apply simp |
70190 | 678 |
done |
679 |
||
680 |
lemmas bin_rsplit_size_sign = bin_rsplit_size_sign' [OF asm_rl |
|
681 |
rev_rev_ident [THEN trans] set_rev [THEN equalityD2 [THEN subsetD]]] |
|
682 |
||
683 |
lemma bin_nth_rsplit [rule_format] : |
|
684 |
"n > 0 \<Longrightarrow> m < n \<Longrightarrow> |
|
685 |
\<forall>w k nw. |
|
686 |
rev sw = bin_rsplit n (nw, w) \<longrightarrow> |
|
687 |
k < size sw \<longrightarrow> bin_nth (sw ! k) m = bin_nth w (k * n + m)" |
|
688 |
apply (induct sw) |
|
689 |
apply clarsimp |
|
690 |
apply clarsimp |
|
691 |
apply (drule bthrs) |
|
692 |
apply (simp (no_asm_use) add: Let_def split: prod.split_asm if_split_asm) |
|
693 |
apply (erule allE, erule impE, erule exI) |
|
694 |
apply (case_tac k) |
|
695 |
apply clarsimp |
|
696 |
prefer 2 |
|
697 |
apply clarsimp |
|
698 |
apply (erule allE) |
|
699 |
apply (erule (1) impE) |
|
71949 | 700 |
apply (simp add: bit_drop_bit_eq ac_simps) |
701 |
apply (simp add: bit_take_bit_iff ac_simps) |
|
70190 | 702 |
done |
703 |
||
704 |
lemma bin_rsplit_all: "0 < nw \<Longrightarrow> nw \<le> n \<Longrightarrow> bin_rsplit n (nw, w) = [bintrunc n w]" |
|
71947 | 705 |
by (auto simp: bin_rsplit_def rsplit_aux_simp2ls split: prod.split dest!: split_bintrunc) |
70190 | 706 |
|
707 |
lemma bin_rsplit_l [rule_format]: |
|
708 |
"\<forall>bin. bin_rsplitl n (m, bin) = bin_rsplit n (m, bintrunc m bin)" |
|
709 |
apply (rule_tac a = "m" in wf_less_than [THEN wf_induct]) |
|
710 |
apply (simp (no_asm) add: bin_rsplitl_def bin_rsplit_def) |
|
711 |
apply (rule allI) |
|
712 |
apply (subst bin_rsplitl_aux.simps) |
|
713 |
apply (subst bin_rsplit_aux.simps) |
|
714 |
apply (clarsimp simp: Let_def split: prod.split) |
|
71947 | 715 |
apply (simp add: ac_simps) |
70190 | 716 |
apply (subst rsplit_aux_alts(1)) |
717 |
apply (subst rsplit_aux_alts(2)) |
|
718 |
apply clarsimp |
|
719 |
unfolding bin_rsplit_def bin_rsplitl_def |
|
71944 | 720 |
apply (simp add: drop_bit_take_bit) |
721 |
apply (case_tac \<open>x < n\<close>) |
|
722 |
apply (simp_all add: not_less min_def) |
|
70190 | 723 |
done |
724 |
||
725 |
lemma bin_rsplit_rcat [rule_format]: |
|
726 |
"n > 0 \<longrightarrow> bin_rsplit n (n * size ws, bin_rcat n ws) = map (bintrunc n) ws" |
|
72088
a36db1c8238e
separation of reversed bit lists from other material
haftmann
parents:
72081
diff
changeset
|
727 |
apply (unfold bin_rsplit_def bin_rcat_eq_foldl) |
70190 | 728 |
apply (rule_tac xs = ws in rev_induct) |
729 |
apply clarsimp |
|
730 |
apply clarsimp |
|
731 |
apply (subst rsplit_aux_alts) |
|
71947 | 732 |
apply (simp add: drop_bit_bin_cat_eq take_bit_bin_cat_eq) |
70190 | 733 |
done |
734 |
||
735 |
lemma bin_rsplit_aux_len_le [rule_format] : |
|
736 |
"\<forall>ws m. n \<noteq> 0 \<longrightarrow> ws = bin_rsplit_aux n nw w bs \<longrightarrow> |
|
737 |
length ws \<le> m \<longleftrightarrow> nw + length bs * n \<le> m * n" |
|
738 |
proof - |
|
739 |
have *: R |
|
740 |
if d: "i \<le> j \<or> m < j'" |
|
741 |
and R1: "i * k \<le> j * k \<Longrightarrow> R" |
|
742 |
and R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R" |
|
743 |
for i j j' k k' m :: nat and R |
|
744 |
using d |
|
745 |
apply safe |
|
746 |
apply (rule R1, erule mult_le_mono1) |
|
747 |
apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]]) |
|
748 |
done |
|
749 |
have **: "0 < sc \<Longrightarrow> sc - n + (n + lb * n) \<le> m * n \<longleftrightarrow> sc + lb * n \<le> m * n" |
|
750 |
for sc m n lb :: nat |
|
751 |
apply safe |
|
752 |
apply arith |
|
753 |
apply (case_tac "sc \<ge> n") |
|
754 |
apply arith |
|
755 |
apply (insert linorder_le_less_linear [of m lb]) |
|
756 |
apply (erule_tac k=n and k'=n in *) |
|
757 |
apply arith |
|
758 |
apply simp |
|
759 |
done |
|
760 |
show ?thesis |
|
761 |
apply (induct n nw w bs rule: bin_rsplit_aux.induct) |
|
762 |
apply (subst bin_rsplit_aux.simps) |
|
763 |
apply (simp add: ** Let_def split: prod.split) |
|
764 |
done |
|
765 |
qed |
|
766 |
||
767 |
lemma bin_rsplit_len_le: "n \<noteq> 0 \<longrightarrow> ws = bin_rsplit n (nw, w) \<longrightarrow> length ws \<le> m \<longleftrightarrow> nw \<le> m * n" |
|
768 |
by (auto simp: bin_rsplit_def bin_rsplit_aux_len_le) |
|
769 |
||
770 |
lemma bin_rsplit_aux_len: |
|
771 |
"n \<noteq> 0 \<Longrightarrow> length (bin_rsplit_aux n nw w cs) = (nw + n - 1) div n + length cs" |
|
772 |
apply (induct n nw w cs rule: bin_rsplit_aux.induct) |
|
773 |
apply (subst bin_rsplit_aux.simps) |
|
774 |
apply (clarsimp simp: Let_def split: prod.split) |
|
775 |
apply (erule thin_rl) |
|
776 |
apply (case_tac m) |
|
777 |
apply simp |
|
778 |
apply (case_tac "m \<le> n") |
|
779 |
apply (auto simp add: div_add_self2) |
|
780 |
done |
|
781 |
||
782 |
lemma bin_rsplit_len: "n \<noteq> 0 \<Longrightarrow> length (bin_rsplit n (nw, w)) = (nw + n - 1) div n" |
|
783 |
by (auto simp: bin_rsplit_def bin_rsplit_aux_len) |
|
784 |
||
785 |
lemma bin_rsplit_aux_len_indep: |
|
786 |
"n \<noteq> 0 \<Longrightarrow> length bs = length cs \<Longrightarrow> |
|
787 |
length (bin_rsplit_aux n nw v bs) = |
|
788 |
length (bin_rsplit_aux n nw w cs)" |
|
789 |
proof (induct n nw w cs arbitrary: v bs rule: bin_rsplit_aux.induct) |
|
790 |
case (1 n m w cs v bs) |
|
791 |
show ?case |
|
792 |
proof (cases "m = 0") |
|
793 |
case True |
|
794 |
with \<open>length bs = length cs\<close> show ?thesis by simp |
|
795 |
next |
|
796 |
case False |
|
71944 | 797 |
from "1.hyps" [of \<open>bin_split n w\<close> \<open>drop_bit n w\<close> \<open>take_bit n w\<close>] \<open>m \<noteq> 0\<close> \<open>n \<noteq> 0\<close> |
70190 | 798 |
have hyp: "\<And>v bs. length bs = Suc (length cs) \<Longrightarrow> |
799 |
length (bin_rsplit_aux n (m - n) v bs) = |
|
71944 | 800 |
length (bin_rsplit_aux n (m - n) (drop_bit n w) (take_bit n w # cs))" |
801 |
using bin_rsplit_aux_len by fastforce |
|
70190 | 802 |
from \<open>length bs = length cs\<close> \<open>n \<noteq> 0\<close> show ?thesis |
803 |
by (auto simp add: bin_rsplit_aux_simp_alt Let_def bin_rsplit_len split: prod.split) |
|
804 |
qed |
|
805 |
qed |
|
806 |
||
807 |
lemma bin_rsplit_len_indep: |
|
808 |
"n \<noteq> 0 \<Longrightarrow> length (bin_rsplit n (nw, v)) = length (bin_rsplit n (nw, w))" |
|
809 |
apply (unfold bin_rsplit_def) |
|
810 |
apply (simp (no_asm)) |
|
811 |
apply (erule bin_rsplit_aux_len_indep) |
|
812 |
apply (rule refl) |
|
813 |
done |
|
814 |
||
815 |
||
61799 | 816 |
subsection \<open>Logical operations\<close> |
24353 | 817 |
|
70191 | 818 |
primrec bin_sc :: "nat \<Rightarrow> bool \<Rightarrow> int \<Rightarrow> int" |
819 |
where |
|
71986 | 820 |
Z: "bin_sc 0 b w = of_bool b + 2 * bin_rest w" |
821 |
| Suc: "bin_sc (Suc n) b w = of_bool (odd w) + 2 * bin_sc n b (w div 2)" |
|
822 |
||
823 |
lemma bin_nth_sc [simp]: "bit (bin_sc n b w) n \<longleftrightarrow> b" |
|
824 |
by (induction n arbitrary: w) (simp_all add: bit_Suc) |
|
70191 | 825 |
|
826 |
lemma bin_sc_sc_same [simp]: "bin_sc n c (bin_sc n b w) = bin_sc n c w" |
|
71986 | 827 |
by (induction n arbitrary: w) (simp_all add: bit_Suc) |
70191 | 828 |
|
829 |
lemma bin_sc_sc_diff: "m \<noteq> n \<Longrightarrow> bin_sc m c (bin_sc n b w) = bin_sc n b (bin_sc m c w)" |
|
830 |
apply (induct n arbitrary: w m) |
|
831 |
apply (case_tac [!] m) |
|
832 |
apply auto |
|
833 |
done |
|
834 |
||
835 |
lemma bin_nth_sc_gen: "bin_nth (bin_sc n b w) m = (if m = n then b else bin_nth w m)" |
|
71949 | 836 |
apply (induct n arbitrary: w m) |
837 |
apply (case_tac m; simp add: bit_Suc) |
|
838 |
apply (case_tac m; simp add: bit_Suc) |
|
839 |
done |
|
70191 | 840 |
|
71986 | 841 |
lemma bin_sc_eq: |
842 |
\<open>bin_sc n False = unset_bit n\<close> |
|
843 |
\<open>bin_sc n True = Bit_Operations.set_bit n\<close> |
|
844 |
by (simp_all add: fun_eq_iff bit_eq_iff) |
|
845 |
(simp_all add: bin_nth_sc_gen bit_set_bit_iff bit_unset_bit_iff) |
|
846 |
||
70191 | 847 |
lemma bin_sc_nth [simp]: "bin_sc n (bin_nth w n) w = w" |
71986 | 848 |
by (rule bit_eqI) (simp add: bin_nth_sc_gen) |
70191 | 849 |
|
850 |
lemma bin_sign_sc [simp]: "bin_sign (bin_sc n b w) = bin_sign w" |
|
71986 | 851 |
proof (induction n arbitrary: w) |
852 |
case 0 |
|
853 |
then show ?case |
|
854 |
by (auto simp add: bin_sign_def) (use bin_rest_ge_0 in fastforce) |
|
855 |
next |
|
856 |
case (Suc n) |
|
857 |
from Suc [of \<open>w div 2\<close>] |
|
858 |
show ?case by (auto simp add: bin_sign_def split: if_splits) |
|
859 |
qed |
|
860 |
||
861 |
lemma bin_sc_bintr [simp]: |
|
862 |
"bintrunc m (bin_sc n x (bintrunc m w)) = bintrunc m (bin_sc n x w)" |
|
863 |
apply (cases x) |
|
864 |
apply (simp_all add: bin_sc_eq bit_eq_iff) |
|
865 |
apply (auto simp add: bit_take_bit_iff bit_set_bit_iff bit_unset_bit_iff) |
|
70191 | 866 |
done |
867 |
||
868 |
lemma bin_clr_le: "bin_sc n False w \<le> w" |
|
71986 | 869 |
by (simp add: bin_sc_eq unset_bit_less_eq) |
70191 | 870 |
|
871 |
lemma bin_set_ge: "bin_sc n True w \<ge> w" |
|
71986 | 872 |
by (simp add: bin_sc_eq set_bit_greater_eq) |
70191 | 873 |
|
874 |
lemma bintr_bin_clr_le: "bintrunc n (bin_sc m False w) \<le> bintrunc n w" |
|
71986 | 875 |
by (simp add: bin_sc_eq take_bit_unset_bit_eq unset_bit_less_eq) |
70191 | 876 |
|
877 |
lemma bintr_bin_set_ge: "bintrunc n (bin_sc m True w) \<ge> bintrunc n w" |
|
71986 | 878 |
by (simp add: bin_sc_eq take_bit_set_bit_eq set_bit_greater_eq) |
70191 | 879 |
|
880 |
lemma bin_sc_FP [simp]: "bin_sc n False 0 = 0" |
|
881 |
by (induct n) auto |
|
882 |
||
883 |
lemma bin_sc_TM [simp]: "bin_sc n True (- 1) = - 1" |
|
884 |
by (induct n) auto |
|
885 |
||
886 |
lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP |
|
887 |
||
888 |
lemma bin_sc_minus: "0 < n \<Longrightarrow> bin_sc (Suc (n - 1)) b w = bin_sc n b w" |
|
889 |
by auto |
|
890 |
||
891 |
lemmas bin_sc_Suc_minus = |
|
892 |
trans [OF bin_sc_minus [symmetric] bin_sc.Suc] |
|
893 |
||
894 |
lemma bin_sc_numeral [simp]: |
|
895 |
"bin_sc (numeral k) b w = |
|
71986 | 896 |
of_bool (odd w) + 2 * bin_sc (pred_numeral k) b (w div 2)" |
70191 | 897 |
by (simp add: numeral_eq_Suc) |
898 |
||
72000 | 899 |
instantiation int :: semiring_bit_syntax |
25762 | 900 |
begin |
901 |
||
70191 | 902 |
definition [iff]: "i !! n \<longleftrightarrow> bin_nth i n" |
903 |
||
904 |
definition "shiftl x n = x * 2 ^ n" for x :: int |
|
905 |
||
906 |
definition "shiftr x n = x div 2 ^ n" for x :: int |
|
907 |
||
72000 | 908 |
instance by standard |
909 |
(simp_all add: fun_eq_iff shiftl_int_def shiftr_int_def push_bit_eq_mult drop_bit_eq_div) |
|
25762 | 910 |
|
911 |
end |
|
24353 | 912 |
|
71943 | 913 |
lemma shiftl_eq_push_bit: |
914 |
\<open>k << n = push_bit n k\<close> for k :: int |
|
72000 | 915 |
by (fact shiftl_eq_push_bit) |
71943 | 916 |
|
917 |
lemma shiftr_eq_drop_bit: |
|
918 |
\<open>k >> n = drop_bit n k\<close> for k :: int |
|
72000 | 919 |
by (fact shiftr_eq_drop_bit) |
71943 | 920 |
|
70191 | 921 |
|
61799 | 922 |
subsubsection \<open>Basic simplification rules\<close> |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
923 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
924 |
lemmas int_not_def = not_int_def |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
925 |
|
24333 | 926 |
lemma int_not_simps [simp]: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
927 |
"NOT (0::int) = -1" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
928 |
"NOT (1::int) = -2" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
929 |
"NOT (- 1::int) = 0" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
930 |
"NOT (numeral w::int) = - numeral (w + Num.One)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
931 |
"NOT (- numeral (Num.Bit0 w)::int) = numeral (Num.BitM w)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
932 |
"NOT (- numeral (Num.Bit1 w)::int) = numeral (Num.Bit0 w)" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
933 |
by (simp_all add: not_int_def) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
934 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
935 |
lemma int_not_not: "NOT (NOT x) = x" |
67120 | 936 |
for x :: int |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
937 |
by (fact bit.double_compl) |
46017 | 938 |
|
67120 | 939 |
lemma int_and_0 [simp]: "0 AND x = 0" |
940 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
941 |
by (fact bit.conj_zero_left) |
46019 | 942 |
|
67120 | 943 |
lemma int_and_m1 [simp]: "-1 AND x = x" |
944 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
945 |
by (fact bit.conj_one_left) |
46019 | 946 |
|
67120 | 947 |
lemma int_or_zero [simp]: "0 OR x = x" |
948 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
949 |
by (fact bit.disj_zero_left) |
46018 | 950 |
|
67120 | 951 |
lemma int_or_minus1 [simp]: "-1 OR x = -1" |
952 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
953 |
by (fact bit.disj_one_left) |
46017 | 954 |
|
67120 | 955 |
lemma int_xor_zero [simp]: "0 XOR x = x" |
956 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
957 |
by (fact bit.xor_zero_left) |
46018 | 958 |
|
67120 | 959 |
|
61799 | 960 |
subsubsection \<open>Binary destructors\<close> |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
961 |
|
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
962 |
lemma bin_rest_NOT [simp]: "bin_rest (NOT x) = NOT (bin_rest x)" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
963 |
by (fact not_int_div_2) |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
964 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
965 |
lemma bin_last_NOT [simp]: "bin_last (NOT x) \<longleftrightarrow> \<not> bin_last x" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
966 |
by simp |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
967 |
|
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
968 |
lemma bin_rest_AND [simp]: "bin_rest (x AND y) = bin_rest x AND bin_rest y" |
71986 | 969 |
by (subst and_int_rec) auto |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
970 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
971 |
lemma bin_last_AND [simp]: "bin_last (x AND y) \<longleftrightarrow> bin_last x \<and> bin_last y" |
71986 | 972 |
by (subst and_int_rec) auto |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
973 |
|
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
974 |
lemma bin_rest_OR [simp]: "bin_rest (x OR y) = bin_rest x OR bin_rest y" |
71986 | 975 |
by (subst or_int_rec) auto |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
976 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
977 |
lemma bin_last_OR [simp]: "bin_last (x OR y) \<longleftrightarrow> bin_last x \<or> bin_last y" |
71986 | 978 |
by (subst or_int_rec) auto |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
979 |
|
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
980 |
lemma bin_rest_XOR [simp]: "bin_rest (x XOR y) = bin_rest x XOR bin_rest y" |
71986 | 981 |
by (subst xor_int_rec) auto |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
982 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
983 |
lemma bin_last_XOR [simp]: "bin_last (x XOR y) \<longleftrightarrow> (bin_last x \<or> bin_last y) \<and> \<not> (bin_last x \<and> bin_last y)" |
71986 | 984 |
by (subst xor_int_rec) auto |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
985 |
|
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
986 |
lemma bin_nth_ops: |
67120 | 987 |
"\<And>x y. bin_nth (x AND y) n \<longleftrightarrow> bin_nth x n \<and> bin_nth y n" |
988 |
"\<And>x y. bin_nth (x OR y) n \<longleftrightarrow> bin_nth x n \<or> bin_nth y n" |
|
989 |
"\<And>x y. bin_nth (x XOR y) n \<longleftrightarrow> bin_nth x n \<noteq> bin_nth y n" |
|
990 |
"\<And>x. bin_nth (NOT x) n \<longleftrightarrow> \<not> bin_nth x n" |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
991 |
by (simp_all add: bit_and_iff bit_or_iff bit_xor_iff bit_not_iff) |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
992 |
|
67120 | 993 |
|
61799 | 994 |
subsubsection \<open>Derived properties\<close> |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
995 |
|
67120 | 996 |
lemma int_xor_minus1 [simp]: "-1 XOR x = NOT x" |
997 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
998 |
by (fact bit.xor_one_left) |
46018 | 999 |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1000 |
lemma int_xor_extra_simps [simp]: |
67120 | 1001 |
"w XOR 0 = w" |
1002 |
"w XOR -1 = NOT w" |
|
1003 |
for w :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1004 |
by simp_all |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1005 |
|
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1006 |
lemma int_or_extra_simps [simp]: |
67120 | 1007 |
"w OR 0 = w" |
1008 |
"w OR -1 = -1" |
|
1009 |
for w :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1010 |
by simp_all |
24333 | 1011 |
|
37667 | 1012 |
lemma int_and_extra_simps [simp]: |
67120 | 1013 |
"w AND 0 = 0" |
1014 |
"w AND -1 = w" |
|
1015 |
for w :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1016 |
by simp_all |
24333 | 1017 |
|
67120 | 1018 |
text \<open>Commutativity of the above.\<close> |
24333 | 1019 |
lemma bin_ops_comm: |
67120 | 1020 |
fixes x y :: int |
1021 |
shows int_and_comm: "x AND y = y AND x" |
|
1022 |
and int_or_comm: "x OR y = y OR x" |
|
1023 |
and int_xor_comm: "x XOR y = y XOR x" |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1024 |
by (simp_all add: ac_simps) |
24333 | 1025 |
|
1026 |
lemma bin_ops_same [simp]: |
|
67120 | 1027 |
"x AND x = x" |
1028 |
"x OR x = x" |
|
1029 |
"x XOR x = 0" |
|
1030 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1031 |
by simp_all |
24333 | 1032 |
|
65363 | 1033 |
lemmas bin_log_esimps = |
24333 | 1034 |
int_and_extra_simps int_or_extra_simps int_xor_extra_simps |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1035 |
int_and_0 int_and_m1 int_or_zero int_or_minus1 int_xor_zero int_xor_minus1 |
24333 | 1036 |
|
67120 | 1037 |
|
1038 |
subsubsection \<open>Basic properties of logical (bit-wise) operations\<close> |
|
24333 | 1039 |
|
67120 | 1040 |
lemma bbw_ao_absorb: "x AND (y OR x) = x \<and> x OR (y AND x) = x" |
1041 |
for x y :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1042 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1043 |
|
1044 |
lemma bbw_ao_absorbs_other: |
|
67120 | 1045 |
"x AND (x OR y) = x \<and> (y AND x) OR x = x" |
1046 |
"(y OR x) AND x = x \<and> x OR (x AND y) = x" |
|
1047 |
"(x OR y) AND x = x \<and> (x AND y) OR x = x" |
|
1048 |
for x y :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1049 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24353 | 1050 |
|
24333 | 1051 |
lemmas bbw_ao_absorbs [simp] = bbw_ao_absorb bbw_ao_absorbs_other |
1052 |
||
67120 | 1053 |
lemma int_xor_not: "(NOT x) XOR y = NOT (x XOR y) \<and> x XOR (NOT y) = NOT (x XOR y)" |
1054 |
for x y :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1055 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1056 |
|
67120 | 1057 |
lemma int_and_assoc: "(x AND y) AND z = x AND (y AND z)" |
1058 |
for x y z :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1059 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1060 |
|
67120 | 1061 |
lemma int_or_assoc: "(x OR y) OR z = x OR (y OR z)" |
1062 |
for x y z :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1063 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1064 |
|
67120 | 1065 |
lemma int_xor_assoc: "(x XOR y) XOR z = x XOR (y XOR z)" |
1066 |
for x y z :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1067 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1068 |
|
1069 |
lemmas bbw_assocs = int_and_assoc int_or_assoc int_xor_assoc |
|
1070 |
||
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1071 |
(* BH: Why are these declared as simp rules??? *) |
65363 | 1072 |
lemma bbw_lcs [simp]: |
67120 | 1073 |
"y AND (x AND z) = x AND (y AND z)" |
1074 |
"y OR (x OR z) = x OR (y OR z)" |
|
1075 |
"y XOR (x XOR z) = x XOR (y XOR z)" |
|
1076 |
for x y :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1077 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1078 |
|
65363 | 1079 |
lemma bbw_not_dist: |
67120 | 1080 |
"NOT (x OR y) = (NOT x) AND (NOT y)" |
1081 |
"NOT (x AND y) = (NOT x) OR (NOT y)" |
|
1082 |
for x y :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1083 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1084 |
|
67120 | 1085 |
lemma bbw_oa_dist: "(x AND y) OR z = (x OR z) AND (y OR z)" |
1086 |
for x y z :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1087 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1088 |
|
67120 | 1089 |
lemma bbw_ao_dist: "(x OR y) AND z = (x AND z) OR (y AND z)" |
1090 |
for x y z :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1091 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1092 |
|
24367
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
huffman
parents:
24366
diff
changeset
|
1093 |
(* |
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
huffman
parents:
24366
diff
changeset
|
1094 |
Why were these declared simp??? |
65363 | 1095 |
declare bin_ops_comm [simp] bbw_assocs [simp] |
24367
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
huffman
parents:
24366
diff
changeset
|
1096 |
*) |
24333 | 1097 |
|
67120 | 1098 |
|
61799 | 1099 |
subsubsection \<open>Simplification with numerals\<close> |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1100 |
|
67120 | 1101 |
text \<open>Cases for \<open>0\<close> and \<open>-1\<close> are already covered by other simp rules.\<close> |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1102 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1103 |
lemma bin_rest_neg_numeral_BitM [simp]: |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1104 |
"bin_rest (- numeral (Num.BitM w)) = - numeral w" |
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
1105 |
by simp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1106 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1107 |
lemma bin_last_neg_numeral_BitM [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
1108 |
"bin_last (- numeral (Num.BitM w))" |
71941 | 1109 |
by simp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1110 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1111 |
|
61799 | 1112 |
subsubsection \<open>Interactions with arithmetic\<close> |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1113 |
|
67120 | 1114 |
lemma le_int_or: "bin_sign y = 0 \<Longrightarrow> x \<le> x OR y" |
1115 |
for x y :: int |
|
71986 | 1116 |
by (simp add: bin_sign_def or_greater_eq split: if_splits) |
24333 | 1117 |
|
1118 |
lemmas int_and_le = |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
1119 |
xtrans(3) [OF bbw_ao_absorbs (2) [THEN conjunct2, symmetric] le_int_or] |
24333 | 1120 |
|
67120 | 1121 |
text \<open>Interaction between bit-wise and arithmetic: good example of \<open>bin_induction\<close>.\<close> |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1122 |
lemma bin_add_not: "x + NOT x = (-1::int)" |
71986 | 1123 |
by (simp add: not_int_def) |
1124 |
||
1125 |
lemma AND_mod: "x AND (2 ^ n - 1) = x mod 2 ^ n" |
|
70169 | 1126 |
for x :: int |
71986 | 1127 |
by (simp flip: take_bit_eq_mod add: take_bit_eq_mask mask_eq_exp_minus_1) |
70169 | 1128 |
|
67120 | 1129 |
|
61799 | 1130 |
subsubsection \<open>Truncating results of bit-wise operations\<close> |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1131 |
|
65363 | 1132 |
lemma bin_trunc_ao: |
67120 | 1133 |
"bintrunc n x AND bintrunc n y = bintrunc n (x AND y)" |
1134 |
"bintrunc n x OR bintrunc n y = bintrunc n (x OR y)" |
|
72488 | 1135 |
by simp_all |
24364 | 1136 |
|
67120 | 1137 |
lemma bin_trunc_xor: "bintrunc n (bintrunc n x XOR bintrunc n y) = bintrunc n (x XOR y)" |
72488 | 1138 |
by simp |
24364 | 1139 |
|
67120 | 1140 |
lemma bin_trunc_not: "bintrunc n (NOT (bintrunc n x)) = bintrunc n (NOT x)" |
72488 | 1141 |
by (fact take_bit_not_take_bit) |
24364 | 1142 |
|
67120 | 1143 |
text \<open>Want theorems of the form of \<open>bin_trunc_xor\<close>.\<close> |
1144 |
lemma bintr_bintr_i: "x = bintrunc n y \<Longrightarrow> bintrunc n x = bintrunc n y" |
|
24364 | 1145 |
by auto |
1146 |
||
1147 |
lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i] |
|
1148 |
lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i] |
|
1149 |
||
70190 | 1150 |
|
1151 |
subsubsection \<open>More lemmas\<close> |
|
1152 |
||
1153 |
lemma not_int_cmp_0 [simp]: |
|
1154 |
fixes i :: int shows |
|
1155 |
"0 < NOT i \<longleftrightarrow> i < -1" |
|
1156 |
"0 \<le> NOT i \<longleftrightarrow> i < 0" |
|
1157 |
"NOT i < 0 \<longleftrightarrow> i \<ge> 0" |
|
1158 |
"NOT i \<le> 0 \<longleftrightarrow> i \<ge> -1" |
|
1159 |
by(simp_all add: int_not_def) arith+ |
|
1160 |
||
1161 |
lemma bbw_ao_dist2: "(x :: int) AND (y OR z) = x AND y OR x AND z" |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1162 |
by (fact bit.conj_disj_distrib) |
70190 | 1163 |
|
1164 |
lemmas int_and_ac = bbw_lcs(1) int_and_comm int_and_assoc |
|
1165 |
||
1166 |
lemma int_nand_same [simp]: fixes x :: int shows "x AND NOT x = 0" |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1167 |
by simp |
70190 | 1168 |
|
1169 |
lemma int_nand_same_middle: fixes x :: int shows "x AND y AND NOT x = 0" |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1170 |
by (simp add: bit_eq_iff bit_and_iff bit_not_iff) |
70190 | 1171 |
|
1172 |
lemma and_xor_dist: fixes x :: int shows |
|
1173 |
"x AND (y XOR z) = (x AND y) XOR (x AND z)" |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1174 |
by (fact bit.conj_xor_distrib) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1175 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1176 |
lemma int_and_lt0 [simp]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1177 |
\<open>x AND y < 0 \<longleftrightarrow> x < 0 \<and> y < 0\<close> for x y :: int |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1178 |
by (fact and_negative_int_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1179 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1180 |
lemma int_and_ge0 [simp]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1181 |
\<open>x AND y \<ge> 0 \<longleftrightarrow> x \<ge> 0 \<or> y \<ge> 0\<close> for x y :: int |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1182 |
by (fact and_nonnegative_int_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1183 |
|
70190 | 1184 |
lemma int_and_1: fixes x :: int shows "x AND 1 = x mod 2" |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1185 |
by (fact and_one_eq) |
70190 | 1186 |
|
1187 |
lemma int_1_and: fixes x :: int shows "1 AND x = x mod 2" |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1188 |
by (fact one_and_eq) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1189 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1190 |
lemma int_or_lt0 [simp]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1191 |
\<open>x OR y < 0 \<longleftrightarrow> x < 0 \<or> y < 0\<close> for x y :: int |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1192 |
by (fact or_negative_int_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1193 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1194 |
lemma int_or_ge0 [simp]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1195 |
\<open>x OR y \<ge> 0 \<longleftrightarrow> x \<ge> 0 \<and> y \<ge> 0\<close> for x y :: int |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1196 |
by (fact or_nonnegative_int_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1197 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1198 |
lemma int_xor_lt0 [simp]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1199 |
\<open>x XOR y < 0 \<longleftrightarrow> (x < 0) \<noteq> (y < 0)\<close> for x y :: int |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1200 |
by (fact xor_negative_int_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1201 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1202 |
lemma int_xor_ge0 [simp]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1203 |
\<open>x XOR y \<ge> 0 \<longleftrightarrow> (x \<ge> 0 \<longleftrightarrow> y \<ge> 0)\<close> for x y :: int |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1204 |
by (fact xor_nonnegative_int_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1205 |
|
71941 | 1206 |
lemma even_conv_AND: |
1207 |
\<open>even i \<longleftrightarrow> i AND 1 = 0\<close> for i :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1208 |
by (simp add: and_one_eq mod2_eq_if) |
71941 | 1209 |
|
70190 | 1210 |
lemma bin_last_conv_AND: |
1211 |
"bin_last i \<longleftrightarrow> i AND 1 \<noteq> 0" |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1212 |
by (simp add: and_one_eq mod2_eq_if) |
70190 | 1213 |
|
1214 |
lemma bitval_bin_last: |
|
1215 |
"of_bool (bin_last i) = i AND 1" |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1216 |
by (simp add: and_one_eq mod2_eq_if) |
70190 | 1217 |
|
1218 |
lemma bin_sign_and: |
|
1219 |
"bin_sign (i AND j) = - (bin_sign i * bin_sign j)" |
|
1220 |
by(simp add: bin_sign_def) |
|
1221 |
||
1222 |
lemma int_not_neg_numeral: "NOT (- numeral n) = (Num.sub n num.One :: int)" |
|
1223 |
by(simp add: int_not_def) |
|
1224 |
||
1225 |
lemma int_neg_numeral_pOne_conv_not: "- numeral (n + num.One) = (NOT (numeral n) :: int)" |
|
1226 |
by(simp add: int_not_def) |
|
70169 | 1227 |
|
67120 | 1228 |
|
61799 | 1229 |
subsection \<open>Setting and clearing bits\<close> |
24364 | 1230 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1231 |
lemma int_shiftl_BIT: fixes x :: int |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1232 |
shows int_shiftl0 [simp]: "x << 0 = x" |
71986 | 1233 |
and int_shiftl_Suc [simp]: "x << Suc n = 2 * (x << n)" |
1234 |
by (auto simp add: shiftl_int_def) |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1235 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1236 |
lemma int_0_shiftl [simp]: "0 << n = (0 :: int)" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1237 |
by(induct n) simp_all |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1238 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1239 |
lemma bin_last_shiftl: "bin_last (x << n) \<longleftrightarrow> n = 0 \<and> bin_last x" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1240 |
by(cases n)(simp_all) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1241 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1242 |
lemma bin_rest_shiftl: "bin_rest (x << n) = (if n > 0 then x << (n - 1) else bin_rest x)" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1243 |
by(cases n)(simp_all) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1244 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1245 |
lemma bin_nth_shiftl [simp]: "bin_nth (x << n) m \<longleftrightarrow> n \<le> m \<and> bin_nth x (m - n)" |
71986 | 1246 |
by (simp add: bit_push_bit_iff_int shiftl_eq_push_bit) |
1247 |
||
1248 |
lemma bin_last_shiftr: "odd (x >> n) \<longleftrightarrow> x !! n" for x :: int |
|
1249 |
by (simp add: shiftr_eq_drop_bit bit_iff_odd_drop_bit) |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1250 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1251 |
lemma bin_rest_shiftr [simp]: "bin_rest (x >> n) = x >> Suc n" |
71986 | 1252 |
by (simp add: bit_eq_iff shiftr_eq_drop_bit drop_bit_Suc bit_drop_bit_eq drop_bit_half) |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1253 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1254 |
lemma bin_nth_shiftr [simp]: "bin_nth (x >> n) m = bin_nth x (n + m)" |
71986 | 1255 |
by (simp add: shiftr_eq_drop_bit bit_drop_bit_eq) |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1256 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1257 |
lemma bin_nth_conv_AND: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1258 |
fixes x :: int shows |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1259 |
"bin_nth x n \<longleftrightarrow> x AND (1 << n) \<noteq> 0" |
71986 | 1260 |
by (simp add: bit_eq_iff) |
1261 |
(auto simp add: shiftl_eq_push_bit bit_and_iff bit_push_bit_iff bit_exp_iff) |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1262 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1263 |
lemma int_shiftl_numeral [simp]: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1264 |
"(numeral w :: int) << numeral w' = numeral (num.Bit0 w) << pred_numeral w'" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1265 |
"(- numeral w :: int) << numeral w' = - numeral (num.Bit0 w) << pred_numeral w'" |
71986 | 1266 |
by(simp_all add: numeral_eq_Suc shiftl_int_def) |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1267 |
(metis add_One mult_inc semiring_norm(11) semiring_norm(13) semiring_norm(2) semiring_norm(6) semiring_norm(87))+ |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1268 |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
1269 |
lemma int_shiftl_One_numeral [simp]: |
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
1270 |
"(1 :: int) << numeral w = 2 << pred_numeral w" |
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
1271 |
using int_shiftl_numeral [of Num.One w] by simp |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1272 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1273 |
lemma shiftl_ge_0 [simp]: fixes i :: int shows "i << n \<ge> 0 \<longleftrightarrow> i \<ge> 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1274 |
by(induct n) simp_all |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1275 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1276 |
lemma shiftl_lt_0 [simp]: fixes i :: int shows "i << n < 0 \<longleftrightarrow> i < 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1277 |
by (metis not_le shiftl_ge_0) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1278 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1279 |
lemma int_shiftl_test_bit: "(n << i :: int) !! m \<longleftrightarrow> m \<ge> i \<and> n !! (m - i)" |
71986 | 1280 |
by simp |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1281 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1282 |
lemma int_0shiftr [simp]: "(0 :: int) >> x = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1283 |
by(simp add: shiftr_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1284 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1285 |
lemma int_minus1_shiftr [simp]: "(-1 :: int) >> x = -1" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1286 |
by(simp add: shiftr_int_def div_eq_minus1) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1287 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1288 |
lemma int_shiftr_ge_0 [simp]: fixes i :: int shows "i >> n \<ge> 0 \<longleftrightarrow> i \<ge> 0" |
71986 | 1289 |
by (simp add: shiftr_eq_drop_bit) |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1290 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1291 |
lemma int_shiftr_lt_0 [simp]: fixes i :: int shows "i >> n < 0 \<longleftrightarrow> i < 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1292 |
by (metis int_shiftr_ge_0 not_less) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1293 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1294 |
lemma int_shiftr_numeral [simp]: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1295 |
"(1 :: int) >> numeral w' = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1296 |
"(numeral num.One :: int) >> numeral w' = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1297 |
"(numeral (num.Bit0 w) :: int) >> numeral w' = numeral w >> pred_numeral w'" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1298 |
"(numeral (num.Bit1 w) :: int) >> numeral w' = numeral w >> pred_numeral w'" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1299 |
"(- numeral (num.Bit0 w) :: int) >> numeral w' = - numeral w >> pred_numeral w'" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1300 |
"(- numeral (num.Bit1 w) :: int) >> numeral w' = - numeral (Num.inc w) >> pred_numeral w'" |
71986 | 1301 |
by (simp_all add: shiftr_eq_drop_bit numeral_eq_Suc add_One drop_bit_Suc) |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1302 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1303 |
lemma int_shiftr_numeral_Suc0 [simp]: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1304 |
"(1 :: int) >> Suc 0 = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1305 |
"(numeral num.One :: int) >> Suc 0 = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1306 |
"(numeral (num.Bit0 w) :: int) >> Suc 0 = numeral w" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1307 |
"(numeral (num.Bit1 w) :: int) >> Suc 0 = numeral w" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1308 |
"(- numeral (num.Bit0 w) :: int) >> Suc 0 = - numeral w" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1309 |
"(- numeral (num.Bit1 w) :: int) >> Suc 0 = - numeral (Num.inc w)" |
71986 | 1310 |
by (simp_all add: shiftr_eq_drop_bit drop_bit_Suc add_One) |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1311 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1312 |
lemma bin_nth_minus_p2: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1313 |
assumes sign: "bin_sign x = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1314 |
and y: "y = 1 << n" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1315 |
and m: "m < n" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1316 |
and x: "x < y" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1317 |
shows "bin_nth (x - y) m = bin_nth x m" |
71986 | 1318 |
proof - |
1319 |
from sign y x have \<open>x \<ge> 0\<close> and \<open>y = 2 ^ n\<close> and \<open>x < 2 ^ n\<close> |
|
1320 |
by (simp_all add: bin_sign_def shiftl_eq_push_bit push_bit_eq_mult split: if_splits) |
|
1321 |
from \<open>0 \<le> x\<close> \<open>x < 2 ^ n\<close> \<open>m < n\<close> have \<open>bit x m \<longleftrightarrow> bit (x - 2 ^ n) m\<close> |
|
1322 |
proof (induction m arbitrary: x n) |
|
1323 |
case 0 |
|
1324 |
then show ?case |
|
1325 |
by simp |
|
1326 |
next |
|
1327 |
case (Suc m) |
|
1328 |
moreover define q where \<open>q = n - 1\<close> |
|
1329 |
ultimately have n: \<open>n = Suc q\<close> |
|
1330 |
by simp |
|
1331 |
have \<open>(x - 2 ^ Suc q) div 2 = x div 2 - 2 ^ q\<close> |
|
1332 |
by simp |
|
1333 |
moreover from Suc.IH [of \<open>x div 2\<close> q] Suc.prems |
|
1334 |
have \<open>bit (x div 2) m \<longleftrightarrow> bit (x div 2 - 2 ^ q) m\<close> |
|
1335 |
by (simp add: n) |
|
1336 |
ultimately show ?case |
|
1337 |
by (simp add: bit_Suc n) |
|
1338 |
qed |
|
1339 |
with \<open>y = 2 ^ n\<close> show ?thesis |
|
1340 |
by simp |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1341 |
qed |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1342 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1343 |
lemma bin_clr_conv_NAND: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1344 |
"bin_sc n False i = i AND NOT (1 << n)" |
71941 | 1345 |
by (induct n arbitrary: i) (rule bin_rl_eqI; simp)+ |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1346 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1347 |
lemma bin_set_conv_OR: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1348 |
"bin_sc n True i = i OR (1 << n)" |
71941 | 1349 |
by (induct n arbitrary: i) (rule bin_rl_eqI; simp)+ |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1350 |
|
72488 | 1351 |
|
1352 |
subsection \<open>More lemmas on words\<close> |
|
1353 |
||
1354 |
lemma word_rcat_eq: |
|
1355 |
\<open>word_rcat ws = word_of_int (bin_rcat (LENGTH('a::len)) (map uint ws))\<close> |
|
1356 |
for ws :: \<open>'a::len word list\<close> |
|
1357 |
apply (simp add: word_rcat_def bin_rcat_def rev_map) |
|
1358 |
apply transfer |
|
1359 |
apply (simp add: horner_sum_foldr foldr_map comp_def) |
|
1360 |
done |
|
1361 |
||
1362 |
lemma sign_uint_Pls [simp]: "bin_sign (uint x) = 0" |
|
1363 |
by (simp add: sign_Pls_ge_0) |
|
1364 |
||
1365 |
lemmas bin_log_bintrs = bin_trunc_not bin_trunc_xor bin_trunc_and bin_trunc_or |
|
1366 |
||
1367 |
\<comment> \<open>following definitions require both arithmetic and bit-wise word operations\<close> |
|
1368 |
||
1369 |
\<comment> \<open>to get \<open>word_no_log_defs\<close> from \<open>word_log_defs\<close>, using \<open>bin_log_bintrs\<close>\<close> |
|
1370 |
lemmas wils1 = bin_log_bintrs [THEN word_of_int_eq_iff [THEN iffD2], |
|
1371 |
folded uint_word_of_int_eq, THEN eq_reflection] |
|
1372 |
||
1373 |
\<comment> \<open>the binary operations only\<close> (* BH: why is this needed? *) |
|
1374 |
lemmas word_log_binary_defs = |
|
1375 |
word_and_def word_or_def word_xor_def |
|
1376 |
||
1377 |
lemma setBit_no [simp]: "setBit (numeral bin) n = word_of_int (bin_sc n True (numeral bin))" |
|
1378 |
by transfer (simp add: bin_sc_eq) |
|
1379 |
||
1380 |
lemma clearBit_no [simp]: |
|
1381 |
"clearBit (numeral bin) n = word_of_int (bin_sc n False (numeral bin))" |
|
1382 |
by transfer (simp add: bin_sc_eq) |
|
1383 |
||
1384 |
lemma eq_mod_iff: "0 < n \<Longrightarrow> b = b mod n \<longleftrightarrow> 0 \<le> b \<and> b < n" |
|
1385 |
for b n :: int |
|
1386 |
by auto (metis pos_mod_conj)+ |
|
1387 |
||
1388 |
lemma split_uint_lem: "bin_split n (uint w) = (a, b) \<Longrightarrow> |
|
1389 |
a = take_bit (LENGTH('a) - n) a \<and> b = take_bit (LENGTH('a)) b" |
|
1390 |
for w :: "'a::len word" |
|
1391 |
by transfer (simp add: drop_bit_take_bit ac_simps) |
|
1392 |
||
1393 |
\<comment> \<open>limited hom result\<close> |
|
1394 |
lemma word_cat_hom: |
|
1395 |
"LENGTH('a::len) \<le> LENGTH('b::len) + LENGTH('c::len) \<Longrightarrow> |
|
1396 |
(word_cat (word_of_int w :: 'b word) (b :: 'c word) :: 'a word) = |
|
1397 |
word_of_int (bin_cat w (size b) (uint b))" |
|
1398 |
by transfer (simp add: take_bit_concat_bit_eq) |
|
1399 |
||
1400 |
lemma bintrunc_shiftl: |
|
1401 |
"take_bit n (m << i) = take_bit (n - i) m << i" |
|
1402 |
for m :: int |
|
1403 |
by (rule bit_eqI) (auto simp add: bit_take_bit_iff) |
|
1404 |
||
1405 |
lemma uint_shiftl: |
|
1406 |
"uint (n << i) = take_bit (size n) (uint n << i)" |
|
1407 |
by transfer (simp add: push_bit_take_bit shiftl_eq_push_bit) |
|
1408 |
||
70169 | 1409 |
end |