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