author | haftmann |
Fri, 03 Jul 2020 06:18:29 +0000 | |
changeset 71991 | 8bff286878bf |
parent 71989 | bad75618fb82 |
child 71997 | 4a013c92a091 |
permissions | -rw-r--r-- |
65363 | 1 |
(* Title: HOL/Word/Bits_Int.thy |
2 |
Author: Jeremy Dawson and Gerwin Klein, NICTA |
|
24333 | 3 |
|
65363 | 4 |
Definitions and basic theorems for bit-wise logical operations |
5 |
for integers expressed using Pls, Min, BIT, |
|
6 |
and converting them to and from lists of bools. |
|
7 |
*) |
|
24333 | 8 |
|
70190 | 9 |
section \<open>Bitwise Operations on integers\<close> |
24350 | 10 |
|
54854
3324a0078636
prefer "Bits" as theory name for abstract bit operations, similar to "Orderings", "Lattices", "Groups" etc.
haftmann
parents:
54848
diff
changeset
|
11 |
theory Bits_Int |
71986 | 12 |
imports Misc_Auxiliary Bits |
24333 | 13 |
begin |
14 |
||
70190 | 15 |
subsection \<open>Implicit bit representation of \<^typ>\<open>int\<close>\<close> |
16 |
||
71941 | 17 |
abbreviation (input) bin_last :: "int \<Rightarrow> bool" |
18 |
where "bin_last \<equiv> odd" |
|
19 |
||
20 |
lemma bin_last_def: |
|
21 |
"bin_last w \<longleftrightarrow> w mod 2 = 1" |
|
22 |
by (fact odd_iff_mod_2_eq_one) |
|
70190 | 23 |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
24 |
abbreviation (input) bin_rest :: "int \<Rightarrow> int" |
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
25 |
where "bin_rest w \<equiv> w div 2" |
70190 | 26 |
|
27 |
lemma bin_last_numeral_simps [simp]: |
|
28 |
"\<not> bin_last 0" |
|
29 |
"bin_last 1" |
|
30 |
"bin_last (- 1)" |
|
31 |
"bin_last Numeral1" |
|
32 |
"\<not> bin_last (numeral (Num.Bit0 w))" |
|
33 |
"bin_last (numeral (Num.Bit1 w))" |
|
34 |
"\<not> bin_last (- numeral (Num.Bit0 w))" |
|
35 |
"bin_last (- numeral (Num.Bit1 w))" |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
36 |
by simp_all |
70190 | 37 |
|
38 |
lemma bin_rest_numeral_simps [simp]: |
|
39 |
"bin_rest 0 = 0" |
|
40 |
"bin_rest 1 = 0" |
|
41 |
"bin_rest (- 1) = - 1" |
|
42 |
"bin_rest Numeral1 = 0" |
|
43 |
"bin_rest (numeral (Num.Bit0 w)) = numeral w" |
|
44 |
"bin_rest (numeral (Num.Bit1 w)) = numeral w" |
|
45 |
"bin_rest (- numeral (Num.Bit0 w)) = - numeral w" |
|
46 |
"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
|
47 |
by simp_all |
70190 | 48 |
|
49 |
lemma bin_rl_eqI: "\<lbrakk>bin_rest x = bin_rest y; bin_last x = bin_last y\<rbrakk> \<Longrightarrow> x = y" |
|
71986 | 50 |
by (auto elim: oddE) |
70190 | 51 |
|
52 |
lemma [simp]: |
|
53 |
shows bin_rest_lt0: "bin_rest i < 0 \<longleftrightarrow> i < 0" |
|
54 |
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
|
55 |
by auto |
70190 | 56 |
|
57 |
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
|
58 |
by auto |
70190 | 59 |
|
60 |
||
61 |
subsection \<open>Explicit bit representation of \<^typ>\<open>int\<close>\<close> |
|
62 |
||
63 |
primrec bl_to_bin_aux :: "bool list \<Rightarrow> int \<Rightarrow> int" |
|
64 |
where |
|
65 |
Nil: "bl_to_bin_aux [] w = w" |
|
71986 | 66 |
| Cons: "bl_to_bin_aux (b # bs) w = bl_to_bin_aux bs (of_bool b + 2 * w)" |
70190 | 67 |
|
68 |
definition bl_to_bin :: "bool list \<Rightarrow> int" |
|
69 |
where "bl_to_bin bs = bl_to_bin_aux bs 0" |
|
70 |
||
71 |
primrec bin_to_bl_aux :: "nat \<Rightarrow> int \<Rightarrow> bool list \<Rightarrow> bool list" |
|
72 |
where |
|
73 |
Z: "bin_to_bl_aux 0 w bl = bl" |
|
74 |
| Suc: "bin_to_bl_aux (Suc n) w bl = bin_to_bl_aux n (bin_rest w) ((bin_last w) # bl)" |
|
75 |
||
76 |
definition bin_to_bl :: "nat \<Rightarrow> int \<Rightarrow> bool list" |
|
77 |
where "bin_to_bl n w = bin_to_bl_aux n w []" |
|
78 |
||
79 |
lemma bin_to_bl_aux_zero_minus_simp [simp]: |
|
80 |
"0 < n \<Longrightarrow> bin_to_bl_aux n 0 bl = bin_to_bl_aux (n - 1) 0 (False # bl)" |
|
81 |
by (cases n) auto |
|
82 |
||
83 |
lemma bin_to_bl_aux_minus1_minus_simp [simp]: |
|
84 |
"0 < n \<Longrightarrow> bin_to_bl_aux n (- 1) bl = bin_to_bl_aux (n - 1) (- 1) (True # bl)" |
|
85 |
by (cases n) auto |
|
86 |
||
87 |
lemma bin_to_bl_aux_one_minus_simp [simp]: |
|
88 |
"0 < n \<Longrightarrow> bin_to_bl_aux n 1 bl = bin_to_bl_aux (n - 1) 0 (True # bl)" |
|
89 |
by (cases n) auto |
|
90 |
||
91 |
lemma bin_to_bl_aux_Bit0_minus_simp [simp]: |
|
92 |
"0 < n \<Longrightarrow> |
|
93 |
bin_to_bl_aux n (numeral (Num.Bit0 w)) bl = bin_to_bl_aux (n - 1) (numeral w) (False # bl)" |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
94 |
by (cases n) simp_all |
70190 | 95 |
|
96 |
lemma bin_to_bl_aux_Bit1_minus_simp [simp]: |
|
97 |
"0 < n \<Longrightarrow> |
|
98 |
bin_to_bl_aux n (numeral (Num.Bit1 w)) bl = bin_to_bl_aux (n - 1) (numeral w) (True # bl)" |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
99 |
by (cases n) simp_all |
70190 | 100 |
|
101 |
lemma bl_to_bin_aux_append: "bl_to_bin_aux (bs @ cs) w = bl_to_bin_aux cs (bl_to_bin_aux bs w)" |
|
102 |
by (induct bs arbitrary: w) auto |
|
103 |
||
104 |
lemma bin_to_bl_aux_append: "bin_to_bl_aux n w bs @ cs = bin_to_bl_aux n w (bs @ cs)" |
|
105 |
by (induct n arbitrary: w bs) auto |
|
106 |
||
107 |
lemma bl_to_bin_append: "bl_to_bin (bs @ cs) = bl_to_bin_aux cs (bl_to_bin bs)" |
|
108 |
unfolding bl_to_bin_def by (rule bl_to_bin_aux_append) |
|
109 |
||
110 |
lemma bin_to_bl_aux_alt: "bin_to_bl_aux n w bs = bin_to_bl n w @ bs" |
|
111 |
by (simp add: bin_to_bl_def bin_to_bl_aux_append) |
|
112 |
||
113 |
lemma bin_to_bl_0 [simp]: "bin_to_bl 0 bs = []" |
|
114 |
by (auto simp: bin_to_bl_def) |
|
115 |
||
116 |
lemma size_bin_to_bl_aux: "length (bin_to_bl_aux n w bs) = n + length bs" |
|
117 |
by (induct n arbitrary: w bs) auto |
|
118 |
||
119 |
lemma size_bin_to_bl [simp]: "length (bin_to_bl n w) = n" |
|
120 |
by (simp add: bin_to_bl_def size_bin_to_bl_aux) |
|
121 |
||
122 |
lemma bl_bin_bl': "bin_to_bl (n + length bs) (bl_to_bin_aux bs w) = bin_to_bl_aux n w bs" |
|
123 |
apply (induct bs arbitrary: w n) |
|
124 |
apply auto |
|
125 |
apply (simp_all only: add_Suc [symmetric]) |
|
126 |
apply (auto simp add: bin_to_bl_def) |
|
127 |
done |
|
128 |
||
129 |
lemma bl_bin_bl [simp]: "bin_to_bl (length bs) (bl_to_bin bs) = bs" |
|
130 |
unfolding bl_to_bin_def |
|
131 |
apply (rule box_equals) |
|
132 |
apply (rule bl_bin_bl') |
|
133 |
prefer 2 |
|
134 |
apply (rule bin_to_bl_aux.Z) |
|
135 |
apply simp |
|
136 |
done |
|
137 |
||
138 |
lemma bl_to_bin_inj: "bl_to_bin bs = bl_to_bin cs \<Longrightarrow> length bs = length cs \<Longrightarrow> bs = cs" |
|
139 |
apply (rule_tac box_equals) |
|
140 |
defer |
|
141 |
apply (rule bl_bin_bl) |
|
142 |
apply (rule bl_bin_bl) |
|
143 |
apply simp |
|
144 |
done |
|
145 |
||
146 |
lemma bl_to_bin_False [simp]: "bl_to_bin (False # bl) = bl_to_bin bl" |
|
147 |
by (auto simp: bl_to_bin_def) |
|
148 |
||
149 |
lemma bl_to_bin_Nil [simp]: "bl_to_bin [] = 0" |
|
150 |
by (auto simp: bl_to_bin_def) |
|
151 |
||
152 |
lemma bin_to_bl_zero_aux: "bin_to_bl_aux n 0 bl = replicate n False @ bl" |
|
153 |
by (induct n arbitrary: bl) (auto simp: replicate_app_Cons_same) |
|
154 |
||
155 |
lemma bin_to_bl_zero: "bin_to_bl n 0 = replicate n False" |
|
156 |
by (simp add: bin_to_bl_def bin_to_bl_zero_aux) |
|
157 |
||
158 |
lemma bin_to_bl_minus1_aux: "bin_to_bl_aux n (- 1) bl = replicate n True @ bl" |
|
159 |
by (induct n arbitrary: bl) (auto simp: replicate_app_Cons_same) |
|
160 |
||
161 |
lemma bin_to_bl_minus1: "bin_to_bl n (- 1) = replicate n True" |
|
162 |
by (simp add: bin_to_bl_def bin_to_bl_minus1_aux) |
|
163 |
||
164 |
||
165 |
subsection \<open>Bit projection\<close> |
|
166 |
||
71949 | 167 |
abbreviation (input) bin_nth :: \<open>int \<Rightarrow> nat \<Rightarrow> bool\<close> |
168 |
where \<open>bin_nth \<equiv> bit\<close> |
|
70190 | 169 |
|
170 |
lemma bin_nth_eq_iff: "bin_nth x = bin_nth y \<longleftrightarrow> x = y" |
|
71949 | 171 |
by (simp add: bit_eq_iff fun_eq_iff) |
70190 | 172 |
|
173 |
lemma bin_eqI: |
|
174 |
"x = y" if "\<And>n. bin_nth x n \<longleftrightarrow> bin_nth y n" |
|
175 |
using that bin_nth_eq_iff [of x y] by (simp add: fun_eq_iff) |
|
176 |
||
177 |
lemma bin_eq_iff: "x = y \<longleftrightarrow> (\<forall>n. bin_nth x n = bin_nth y n)" |
|
71949 | 178 |
by (fact bit_eq_iff) |
70190 | 179 |
|
180 |
lemma bin_nth_zero [simp]: "\<not> bin_nth 0 n" |
|
71949 | 181 |
by simp |
70190 | 182 |
|
183 |
lemma bin_nth_1 [simp]: "bin_nth 1 n \<longleftrightarrow> n = 0" |
|
71949 | 184 |
by (cases n) (simp_all add: bit_Suc) |
70190 | 185 |
|
186 |
lemma bin_nth_minus1 [simp]: "bin_nth (- 1) n" |
|
71949 | 187 |
by (induction n) (simp_all add: bit_Suc) |
70190 | 188 |
|
189 |
lemma bin_nth_numeral: "bin_rest x = y \<Longrightarrow> bin_nth x (numeral n) = bin_nth y (pred_numeral n)" |
|
71949 | 190 |
by (simp add: numeral_eq_Suc bit_Suc) |
70190 | 191 |
|
192 |
lemmas bin_nth_numeral_simps [simp] = |
|
193 |
bin_nth_numeral [OF bin_rest_numeral_simps(2)] |
|
194 |
bin_nth_numeral [OF bin_rest_numeral_simps(5)] |
|
195 |
bin_nth_numeral [OF bin_rest_numeral_simps(6)] |
|
196 |
bin_nth_numeral [OF bin_rest_numeral_simps(7)] |
|
197 |
bin_nth_numeral [OF bin_rest_numeral_simps(8)] |
|
198 |
||
199 |
lemmas bin_nth_simps = |
|
71949 | 200 |
bit_0 bit_Suc bin_nth_zero bin_nth_minus1 |
70190 | 201 |
bin_nth_numeral_simps |
202 |
||
203 |
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 | 204 |
by (auto simp add: bit_exp_iff) |
205 |
||
70190 | 206 |
lemma nth_rest_power_bin: "bin_nth ((bin_rest ^^ k) w) n = bin_nth w (n + k)" |
207 |
apply (induct k arbitrary: n) |
|
208 |
apply clarsimp |
|
209 |
apply clarsimp |
|
71949 | 210 |
apply (simp only: bit_Suc [symmetric] add_Suc) |
70190 | 211 |
done |
212 |
||
213 |
lemma bin_nth_numeral_unfold: |
|
214 |
"bin_nth (numeral (num.Bit0 x)) n \<longleftrightarrow> n > 0 \<and> bin_nth (numeral x) (n - 1)" |
|
215 |
"bin_nth (numeral (num.Bit1 x)) n \<longleftrightarrow> (n > 0 \<longrightarrow> bin_nth (numeral x) (n - 1))" |
|
71941 | 216 |
by (cases n; simp)+ |
70190 | 217 |
|
218 |
||
219 |
subsection \<open>Truncating\<close> |
|
220 |
||
221 |
definition bin_sign :: "int \<Rightarrow> int" |
|
222 |
where "bin_sign k = (if k \<ge> 0 then 0 else - 1)" |
|
223 |
||
224 |
lemma bin_sign_simps [simp]: |
|
225 |
"bin_sign 0 = 0" |
|
226 |
"bin_sign 1 = 0" |
|
227 |
"bin_sign (- 1) = - 1" |
|
228 |
"bin_sign (numeral k) = 0" |
|
229 |
"bin_sign (- numeral k) = -1" |
|
71986 | 230 |
by (simp_all add: bin_sign_def) |
70190 | 231 |
|
232 |
lemma bin_sign_rest [simp]: "bin_sign (bin_rest w) = bin_sign w" |
|
71986 | 233 |
by (simp add: bin_sign_def) |
70190 | 234 |
|
71947 | 235 |
abbreviation (input) bintrunc :: "nat \<Rightarrow> int \<Rightarrow> int" |
236 |
where \<open>bintrunc \<equiv> take_bit\<close> |
|
70190 | 237 |
|
71986 | 238 |
lemma bintrunc_mod2p: "bintrunc n w = w mod 2 ^ n" |
239 |
by (fact take_bit_eq_mod) |
|
240 |
||
70190 | 241 |
primrec sbintrunc :: "nat \<Rightarrow> int \<Rightarrow> int" |
242 |
where |
|
71986 | 243 |
Z : "sbintrunc 0 bin = (if odd bin then - 1 else 0)" |
244 |
| Suc : "sbintrunc (Suc n) bin = of_bool (odd bin) + 2 * sbintrunc n (bin div 2)" |
|
70190 | 245 |
|
246 |
lemma sbintrunc_mod2p: "sbintrunc n w = (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n" |
|
247 |
proof (induction n arbitrary: w) |
|
248 |
case 0 |
|
249 |
then show ?case |
|
71941 | 250 |
by (auto simp add: odd_iff_mod_2_eq_one) |
70190 | 251 |
next |
252 |
case (Suc n) |
|
71986 | 253 |
from Suc [of \<open>w div 2\<close>] |
254 |
show ?case |
|
255 |
using even_succ_mod_exp [of \<open>(b * 2 + 2 * 2 ^ n)\<close> \<open>Suc (Suc n)\<close> for b :: int] |
|
256 |
by (auto elim!: evenE oddE simp add: mult_mod_right ac_simps) |
|
70190 | 257 |
qed |
258 |
||
259 |
lemma sign_bintr: "bin_sign (bintrunc n w) = 0" |
|
260 |
by (simp add: bintrunc_mod2p bin_sign_def) |
|
261 |
||
262 |
lemma bintrunc_n_0 [simp]: "bintrunc n 0 = 0" |
|
263 |
by (simp add: bintrunc_mod2p) |
|
264 |
||
265 |
lemma sbintrunc_n_0 [simp]: "sbintrunc n 0 = 0" |
|
266 |
by (simp add: sbintrunc_mod2p) |
|
267 |
||
268 |
lemma sbintrunc_n_minus1 [simp]: "sbintrunc n (- 1) = -1" |
|
269 |
by (induct n) auto |
|
270 |
||
271 |
lemma bintrunc_Suc_numeral: |
|
272 |
"bintrunc (Suc n) 1 = 1" |
|
71986 | 273 |
"bintrunc (Suc n) (- 1) = 1 + 2 * bintrunc n (- 1)" |
274 |
"bintrunc (Suc n) (numeral (Num.Bit0 w)) = 2 * bintrunc n (numeral w)" |
|
275 |
"bintrunc (Suc n) (numeral (Num.Bit1 w)) = 1 + 2 * bintrunc n (numeral w)" |
|
276 |
"bintrunc (Suc n) (- numeral (Num.Bit0 w)) = 2 * bintrunc n (- numeral w)" |
|
277 |
"bintrunc (Suc n) (- numeral (Num.Bit1 w)) = 1 + 2 * bintrunc n (- numeral (w + Num.One))" |
|
278 |
by (simp_all add: take_bit_Suc) |
|
70190 | 279 |
|
280 |
lemma sbintrunc_0_numeral [simp]: |
|
281 |
"sbintrunc 0 1 = -1" |
|
282 |
"sbintrunc 0 (numeral (Num.Bit0 w)) = 0" |
|
283 |
"sbintrunc 0 (numeral (Num.Bit1 w)) = -1" |
|
284 |
"sbintrunc 0 (- numeral (Num.Bit0 w)) = 0" |
|
285 |
"sbintrunc 0 (- numeral (Num.Bit1 w)) = -1" |
|
286 |
by simp_all |
|
287 |
||
288 |
lemma sbintrunc_Suc_numeral: |
|
289 |
"sbintrunc (Suc n) 1 = 1" |
|
71986 | 290 |
"sbintrunc (Suc n) (numeral (Num.Bit0 w)) = 2 * sbintrunc n (numeral w)" |
291 |
"sbintrunc (Suc n) (numeral (Num.Bit1 w)) = 1 + 2 * sbintrunc n (numeral w)" |
|
292 |
"sbintrunc (Suc n) (- numeral (Num.Bit0 w)) = 2 * sbintrunc n (- numeral w)" |
|
293 |
"sbintrunc (Suc n) (- numeral (Num.Bit1 w)) = 1 + 2 * sbintrunc n (- numeral (w + Num.One))" |
|
70190 | 294 |
by simp_all |
295 |
||
296 |
lemma bin_sign_lem: "(bin_sign (sbintrunc n bin) = -1) = bin_nth bin n" |
|
71986 | 297 |
apply (rule sym) |
298 |
apply (induct n arbitrary: bin) |
|
299 |
apply (simp_all add: bit_Suc bin_sign_def) |
|
300 |
done |
|
70190 | 301 |
|
302 |
lemma nth_bintr: "bin_nth (bintrunc m w) n \<longleftrightarrow> n < m \<and> bin_nth w n" |
|
71949 | 303 |
by (fact bit_take_bit_iff) |
70190 | 304 |
|
305 |
lemma nth_sbintr: "bin_nth (sbintrunc m w) n = (if n < m then bin_nth w n else bin_nth w m)" |
|
306 |
apply (induct n arbitrary: w m) |
|
307 |
apply (case_tac m) |
|
308 |
apply simp_all |
|
309 |
apply (case_tac m) |
|
71949 | 310 |
apply (simp_all add: bit_Suc) |
70190 | 311 |
done |
312 |
||
313 |
lemma bin_nth_Bit0: |
|
314 |
"bin_nth (numeral (Num.Bit0 w)) n \<longleftrightarrow> |
|
315 |
(\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)" |
|
71986 | 316 |
using bit_double_iff [of \<open>numeral w :: int\<close> n] |
317 |
by (auto intro: exI [of _ \<open>n - 1\<close>]) |
|
70190 | 318 |
|
319 |
lemma bin_nth_Bit1: |
|
320 |
"bin_nth (numeral (Num.Bit1 w)) n \<longleftrightarrow> |
|
321 |
n = 0 \<or> (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)" |
|
71986 | 322 |
using even_bit_succ_iff [of \<open>2 * numeral w :: int\<close> n] |
323 |
bit_double_iff [of \<open>numeral w :: int\<close> n] |
|
324 |
by auto |
|
70190 | 325 |
|
326 |
lemma bintrunc_bintrunc_l: "n \<le> m \<Longrightarrow> bintrunc m (bintrunc n w) = bintrunc n w" |
|
71947 | 327 |
by (simp add: min.absorb2) |
70190 | 328 |
|
329 |
lemma sbintrunc_sbintrunc_l: "n \<le> m \<Longrightarrow> sbintrunc m (sbintrunc n w) = sbintrunc n w" |
|
330 |
by (rule bin_eqI) (auto simp: nth_sbintr) |
|
331 |
||
332 |
lemma bintrunc_bintrunc_ge: "n \<le> m \<Longrightarrow> bintrunc n (bintrunc m w) = bintrunc n w" |
|
333 |
by (rule bin_eqI) (auto simp: nth_bintr) |
|
334 |
||
335 |
lemma bintrunc_bintrunc_min [simp]: "bintrunc m (bintrunc n w) = bintrunc (min m n) w" |
|
336 |
by (rule bin_eqI) (auto simp: nth_bintr) |
|
337 |
||
338 |
lemma sbintrunc_sbintrunc_min [simp]: "sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w" |
|
339 |
by (rule bin_eqI) (auto simp: nth_sbintr min.absorb1 min.absorb2) |
|
340 |
||
341 |
lemmas sbintrunc_Suc_Pls = |
|
342 |
sbintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
|
343 |
||
344 |
lemmas sbintrunc_Suc_Min = |
|
345 |
sbintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
|
346 |
||
71986 | 347 |
lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min |
70190 | 348 |
sbintrunc_Suc_numeral |
349 |
||
350 |
lemmas sbintrunc_Pls = |
|
351 |
sbintrunc.Z [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
|
352 |
||
353 |
lemmas sbintrunc_Min = |
|
354 |
sbintrunc.Z [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps] |
|
355 |
||
356 |
lemmas sbintrunc_0_simps = |
|
71986 | 357 |
sbintrunc_Pls sbintrunc_Min |
70190 | 358 |
|
359 |
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs |
|
360 |
||
361 |
lemma bintrunc_minus: "0 < n \<Longrightarrow> bintrunc (Suc (n - 1)) w = bintrunc n w" |
|
362 |
by auto |
|
363 |
||
364 |
lemma sbintrunc_minus: "0 < n \<Longrightarrow> sbintrunc (Suc (n - 1)) w = sbintrunc n w" |
|
365 |
by auto |
|
366 |
||
367 |
lemmas sbintrunc_minus_simps = |
|
368 |
sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans]] |
|
369 |
||
71984 | 370 |
lemma sbintrunc_BIT_I: |
371 |
\<open>0 < n \<Longrightarrow> |
|
372 |
sbintrunc (n - 1) 0 = y \<Longrightarrow> |
|
373 |
sbintrunc n 0 = 2 * y\<close> |
|
374 |
by simp |
|
375 |
||
376 |
lemma sbintrunc_Suc_Is: |
|
377 |
\<open>sbintrunc n (- 1) = y \<Longrightarrow> |
|
378 |
sbintrunc (Suc n) (- 1) = 1 + 2 * y\<close> |
|
71986 | 379 |
by auto |
70190 | 380 |
|
381 |
lemma sbintrunc_Suc_lem: "sbintrunc (Suc n) x = y \<Longrightarrow> m = Suc n \<Longrightarrow> sbintrunc m x = y" |
|
382 |
by auto |
|
383 |
||
384 |
lemmas sbintrunc_Suc_Ialts = |
|
385 |
sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem] |
|
386 |
||
387 |
lemma sbintrunc_bintrunc_lt: "m > n \<Longrightarrow> sbintrunc n (bintrunc m w) = sbintrunc n w" |
|
388 |
by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr) |
|
389 |
||
390 |
lemma bintrunc_sbintrunc_le: "m \<le> Suc n \<Longrightarrow> bintrunc m (sbintrunc n w) = bintrunc m w" |
|
391 |
apply (rule bin_eqI) |
|
392 |
using le_Suc_eq less_Suc_eq_le apply (auto simp: nth_sbintr nth_bintr) |
|
393 |
done |
|
394 |
||
395 |
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le] |
|
396 |
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt] |
|
397 |
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l] |
|
398 |
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l] |
|
399 |
||
400 |
lemma bintrunc_sbintrunc' [simp]: "0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w" |
|
71947 | 401 |
by (cases n) simp_all |
70190 | 402 |
|
403 |
lemma sbintrunc_bintrunc' [simp]: "0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w" |
|
71947 | 404 |
by (cases n) simp_all |
70190 | 405 |
|
406 |
lemma bin_sbin_eq_iff: "bintrunc (Suc n) x = bintrunc (Suc n) y \<longleftrightarrow> sbintrunc n x = sbintrunc n y" |
|
407 |
apply (rule iffI) |
|
408 |
apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc]) |
|
409 |
apply simp |
|
410 |
apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc]) |
|
411 |
apply simp |
|
412 |
done |
|
413 |
||
414 |
lemma bin_sbin_eq_iff': |
|
415 |
"0 < n \<Longrightarrow> bintrunc n x = bintrunc n y \<longleftrightarrow> sbintrunc (n - 1) x = sbintrunc (n - 1) y" |
|
71947 | 416 |
by (cases n) (simp_all add: bin_sbin_eq_iff) |
70190 | 417 |
|
418 |
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def] |
|
419 |
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def] |
|
420 |
||
421 |
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l] |
|
422 |
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l] |
|
423 |
||
424 |
(* although bintrunc_minus_simps, if added to default simpset, |
|
425 |
tends to get applied where it's not wanted in developing the theories, |
|
426 |
we get a version for when the word length is given literally *) |
|
427 |
||
428 |
lemmas nat_non0_gr = |
|
429 |
trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl] |
|
430 |
||
431 |
lemma bintrunc_numeral: |
|
71986 | 432 |
"bintrunc (numeral k) x = of_bool (odd x) + 2 * bintrunc (pred_numeral k) (x div 2)" |
433 |
by (simp add: numeral_eq_Suc take_bit_Suc mod_2_eq_odd) |
|
70190 | 434 |
|
435 |
lemma sbintrunc_numeral: |
|
71986 | 436 |
"sbintrunc (numeral k) x = of_bool (odd x) + 2 * sbintrunc (pred_numeral k) (x div 2)" |
70190 | 437 |
by (simp add: numeral_eq_Suc) |
438 |
||
439 |
lemma bintrunc_numeral_simps [simp]: |
|
71986 | 440 |
"bintrunc (numeral k) (numeral (Num.Bit0 w)) = |
441 |
2 * bintrunc (pred_numeral k) (numeral w)" |
|
442 |
"bintrunc (numeral k) (numeral (Num.Bit1 w)) = |
|
443 |
1 + 2 * bintrunc (pred_numeral k) (numeral w)" |
|
444 |
"bintrunc (numeral k) (- numeral (Num.Bit0 w)) = |
|
445 |
2 * bintrunc (pred_numeral k) (- numeral w)" |
|
70190 | 446 |
"bintrunc (numeral k) (- numeral (Num.Bit1 w)) = |
71986 | 447 |
1 + 2 * bintrunc (pred_numeral k) (- numeral (w + Num.One))" |
70190 | 448 |
"bintrunc (numeral k) 1 = 1" |
449 |
by (simp_all add: bintrunc_numeral) |
|
450 |
||
451 |
lemma sbintrunc_numeral_simps [simp]: |
|
71986 | 452 |
"sbintrunc (numeral k) (numeral (Num.Bit0 w)) = |
453 |
2 * sbintrunc (pred_numeral k) (numeral w)" |
|
454 |
"sbintrunc (numeral k) (numeral (Num.Bit1 w)) = |
|
455 |
1 + 2 * sbintrunc (pred_numeral k) (numeral w)" |
|
70190 | 456 |
"sbintrunc (numeral k) (- numeral (Num.Bit0 w)) = |
71986 | 457 |
2 * sbintrunc (pred_numeral k) (- numeral w)" |
70190 | 458 |
"sbintrunc (numeral k) (- numeral (Num.Bit1 w)) = |
71986 | 459 |
1 + 2 * sbintrunc (pred_numeral k) (- numeral (w + Num.One))" |
70190 | 460 |
"sbintrunc (numeral k) 1 = 1" |
461 |
by (simp_all add: sbintrunc_numeral) |
|
462 |
||
463 |
lemma no_bintr_alt1: "bintrunc n = (\<lambda>w. w mod 2 ^ n :: int)" |
|
464 |
by (rule ext) (rule bintrunc_mod2p) |
|
465 |
||
466 |
lemma range_bintrunc: "range (bintrunc n) = {i. 0 \<le> i \<and> i < 2 ^ n}" |
|
467 |
apply (unfold no_bintr_alt1) |
|
468 |
apply (auto simp add: image_iff) |
|
469 |
apply (rule exI) |
|
470 |
apply (rule sym) |
|
471 |
using int_mod_lem [symmetric, of "2 ^ n"] |
|
472 |
apply auto |
|
473 |
done |
|
474 |
||
475 |
lemma no_sbintr_alt2: "sbintrunc n = (\<lambda>w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)" |
|
476 |
by (rule ext) (simp add : sbintrunc_mod2p) |
|
477 |
||
478 |
lemma range_sbintrunc: "range (sbintrunc n) = {i. - (2 ^ n) \<le> i \<and> i < 2 ^ n}" |
|
479 |
apply (unfold no_sbintr_alt2) |
|
480 |
apply (auto simp add: image_iff eq_diff_eq) |
|
481 |
||
482 |
apply (rule exI) |
|
483 |
apply (auto intro: int_mod_lem [THEN iffD1, symmetric]) |
|
484 |
done |
|
485 |
||
486 |
lemma sb_inc_lem: "a + 2^k < 0 \<Longrightarrow> a + 2^k + 2^(Suc k) \<le> (a + 2^k) mod 2^(Suc k)" |
|
487 |
for a :: int |
|
488 |
using int_mod_ge' [where n = "2 ^ (Suc k)" and b = "a + 2 ^ k"] |
|
489 |
by simp |
|
490 |
||
491 |
lemma sb_inc_lem': "a < - (2^k) \<Longrightarrow> a + 2^k + 2^(Suc k) \<le> (a + 2^k) mod 2^(Suc k)" |
|
492 |
for a :: int |
|
493 |
by (rule sb_inc_lem) simp |
|
494 |
||
495 |
lemma sbintrunc_inc: "x < - (2^n) \<Longrightarrow> x + 2^(Suc n) \<le> sbintrunc n x" |
|
496 |
unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp |
|
497 |
||
498 |
lemma sb_dec_lem: "0 \<le> - (2 ^ k) + a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a" |
|
499 |
for a :: int |
|
500 |
using int_mod_le'[where n = "2 ^ (Suc k)" and b = "a + 2 ^ k"] by simp |
|
501 |
||
502 |
lemma sb_dec_lem': "2 ^ k \<le> a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a" |
|
503 |
for a :: int |
|
504 |
by (rule sb_dec_lem) simp |
|
505 |
||
506 |
lemma sbintrunc_dec: "x \<ge> (2 ^ n) \<Longrightarrow> x - 2 ^ (Suc n) >= sbintrunc n x" |
|
507 |
unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp |
|
508 |
||
509 |
lemma bintr_ge0: "0 \<le> bintrunc n w" |
|
510 |
by (simp add: bintrunc_mod2p) |
|
511 |
||
512 |
lemma bintr_lt2p: "bintrunc n w < 2 ^ n" |
|
513 |
by (simp add: bintrunc_mod2p) |
|
514 |
||
515 |
lemma bintr_Min: "bintrunc n (- 1) = 2 ^ n - 1" |
|
516 |
by (simp add: bintrunc_mod2p m1mod2k) |
|
517 |
||
518 |
lemma sbintr_ge: "- (2 ^ n) \<le> sbintrunc n w" |
|
519 |
by (simp add: sbintrunc_mod2p) |
|
520 |
||
521 |
lemma sbintr_lt: "sbintrunc n w < 2 ^ n" |
|
522 |
by (simp add: sbintrunc_mod2p) |
|
523 |
||
524 |
lemma sign_Pls_ge_0: "bin_sign bin = 0 \<longleftrightarrow> bin \<ge> 0" |
|
525 |
for bin :: int |
|
526 |
by (simp add: bin_sign_def) |
|
527 |
||
528 |
lemma sign_Min_lt_0: "bin_sign bin = -1 \<longleftrightarrow> bin < 0" |
|
529 |
for bin :: int |
|
530 |
by (simp add: bin_sign_def) |
|
531 |
||
532 |
lemma bin_rest_trunc: "bin_rest (bintrunc n bin) = bintrunc (n - 1) (bin_rest bin)" |
|
71947 | 533 |
by (simp add: take_bit_rec [of n bin]) |
70190 | 534 |
|
535 |
lemma bin_rest_power_trunc: |
|
536 |
"(bin_rest ^^ k) (bintrunc n bin) = bintrunc (n - k) ((bin_rest ^^ k) bin)" |
|
537 |
by (induct k) (auto simp: bin_rest_trunc) |
|
538 |
||
539 |
lemma bin_rest_trunc_i: "bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)" |
|
71947 | 540 |
by (auto simp add: take_bit_Suc) |
70190 | 541 |
|
542 |
lemma bin_rest_strunc: "bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)" |
|
543 |
by (induct n arbitrary: bin) auto |
|
544 |
||
545 |
lemma bintrunc_rest [simp]: "bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)" |
|
71947 | 546 |
by (induct n arbitrary: bin) (simp_all add: take_bit_Suc) |
70190 | 547 |
|
548 |
lemma sbintrunc_rest [simp]: "sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)" |
|
71986 | 549 |
by (induct n arbitrary: bin) simp_all |
70190 | 550 |
|
551 |
lemma bintrunc_rest': "bintrunc n \<circ> bin_rest \<circ> bintrunc n = bin_rest \<circ> bintrunc n" |
|
552 |
by (rule ext) auto |
|
553 |
||
554 |
lemma sbintrunc_rest': "sbintrunc n \<circ> bin_rest \<circ> sbintrunc n = bin_rest \<circ> sbintrunc n" |
|
555 |
by (rule ext) auto |
|
556 |
||
557 |
lemma rco_lem: "f \<circ> g \<circ> f = g \<circ> f \<Longrightarrow> f \<circ> (g \<circ> f) ^^ n = g ^^ n \<circ> f" |
|
558 |
apply (rule ext) |
|
559 |
apply (induct_tac n) |
|
560 |
apply (simp_all (no_asm)) |
|
561 |
apply (drule fun_cong) |
|
562 |
apply (unfold o_def) |
|
563 |
apply (erule trans) |
|
564 |
apply simp |
|
565 |
done |
|
566 |
||
567 |
lemmas rco_bintr = bintrunc_rest' |
|
568 |
[THEN rco_lem [THEN fun_cong], unfolded o_def] |
|
569 |
lemmas rco_sbintr = sbintrunc_rest' |
|
570 |
[THEN rco_lem [THEN fun_cong], unfolded o_def] |
|
571 |
||
71991 | 572 |
lemma sbintrunc_code [code]: |
573 |
"sbintrunc n k = |
|
574 |
(let l = take_bit (Suc n) k |
|
575 |
in if bit l n then l - push_bit n 2 else l)" |
|
576 |
proof (induction n arbitrary: k) |
|
577 |
case 0 |
|
578 |
then show ?case |
|
579 |
by (simp add: mod_2_eq_odd and_one_eq) |
|
580 |
next |
|
581 |
case (Suc n) |
|
582 |
from Suc [of \<open>k div 2\<close>] |
|
583 |
show ?case |
|
584 |
by (auto simp add: Let_def push_bit_eq_mult algebra_simps take_bit_Suc [of \<open>Suc n\<close>] bit_Suc elim!: evenE oddE) |
|
585 |
qed |
|
586 |
||
70190 | 587 |
|
588 |
subsection \<open>Splitting and concatenation\<close> |
|
589 |
||
71944 | 590 |
definition bin_split :: \<open>nat \<Rightarrow> int \<Rightarrow> int \<times> int\<close> |
591 |
where [simp]: \<open>bin_split n k = (drop_bit n k, take_bit n k)\<close> |
|
71943 | 592 |
|
70190 | 593 |
lemma [code]: |
71986 | 594 |
"bin_split (Suc n) w = (let (w1, w2) = bin_split n (w div 2) in (w1, of_bool (odd w) + 2 * w2))" |
70190 | 595 |
"bin_split 0 w = (w, 0)" |
71986 | 596 |
by (simp_all add: drop_bit_Suc take_bit_Suc mod_2_eq_odd) |
70190 | 597 |
|
598 |
primrec bin_cat :: "int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int" |
|
599 |
where |
|
600 |
Z: "bin_cat w 0 v = w" |
|
71986 | 601 |
| Suc: "bin_cat w (Suc n) v = of_bool (odd v) + 2 * bin_cat w n (v div 2)" |
70190 | 602 |
|
71943 | 603 |
lemma bin_cat_eq_push_bit_add_take_bit: |
604 |
\<open>bin_cat k n l = push_bit n k + take_bit n l\<close> |
|
605 |
by (induction n arbitrary: k l) |
|
71986 | 606 |
(simp_all add: take_bit_Suc push_bit_double mod_2_eq_odd) |
71943 | 607 |
|
70190 | 608 |
lemma bin_sign_cat: "bin_sign (bin_cat x n y) = bin_sign x" |
71986 | 609 |
proof - |
610 |
have \<open>0 \<le> x\<close> if \<open>0 \<le> x * 2 ^ n + y mod 2 ^ n\<close> |
|
611 |
proof - |
|
612 |
from that have \<open>x \<noteq> - 1\<close> |
|
613 |
using int_mod_le' [of \<open>y mod 2 ^ n\<close> \<open>2 ^ n\<close>] by auto |
|
614 |
have *: \<open>- 1 \<le> (- (y mod 2 ^ n)) div 2 ^ n\<close> |
|
615 |
by (simp add: zdiv_zminus1_eq_if) |
|
616 |
from that have \<open>- (y mod 2 ^ n) \<le> x * 2 ^ n\<close> |
|
617 |
by simp |
|
618 |
then have \<open>(- (y mod 2 ^ n)) div 2 ^ n \<le> (x * 2 ^ n) div 2 ^ n\<close> |
|
619 |
using zdiv_mono1 zero_less_numeral zero_less_power by blast |
|
620 |
with * have \<open>- 1 \<le> x * 2 ^ n div 2 ^ n\<close> by simp |
|
621 |
with \<open>x \<noteq> - 1\<close> show ?thesis |
|
622 |
by simp |
|
623 |
qed |
|
624 |
then show ?thesis |
|
625 |
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) |
|
626 |
qed |
|
70190 | 627 |
|
628 |
lemma bin_cat_assoc: "bin_cat (bin_cat x m y) n z = bin_cat x (m + n) (bin_cat y n z)" |
|
629 |
by (induct n arbitrary: z) auto |
|
630 |
||
631 |
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" |
|
632 |
apply (induct n arbitrary: z m) |
|
633 |
apply clarsimp |
|
634 |
apply (case_tac m, auto) |
|
635 |
done |
|
636 |
||
637 |
definition bin_rcat :: "nat \<Rightarrow> int list \<Rightarrow> int" |
|
638 |
where "bin_rcat n = foldl (\<lambda>u v. bin_cat u n v) 0" |
|
639 |
||
640 |
fun bin_rsplit_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" |
|
641 |
where "bin_rsplit_aux n m c bs = |
|
642 |
(if m = 0 \<or> n = 0 then bs |
|
643 |
else |
|
644 |
let (a, b) = bin_split n c |
|
645 |
in bin_rsplit_aux n (m - n) a (b # bs))" |
|
646 |
||
647 |
definition bin_rsplit :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list" |
|
648 |
where "bin_rsplit n w = bin_rsplit_aux n (fst w) (snd w) []" |
|
649 |
||
650 |
fun bin_rsplitl_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list" |
|
651 |
where "bin_rsplitl_aux n m c bs = |
|
652 |
(if m = 0 \<or> n = 0 then bs |
|
653 |
else |
|
654 |
let (a, b) = bin_split (min m n) c |
|
655 |
in bin_rsplitl_aux n (m - n) a (b # bs))" |
|
656 |
||
657 |
definition bin_rsplitl :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list" |
|
658 |
where "bin_rsplitl n w = bin_rsplitl_aux n (fst w) (snd w) []" |
|
659 |
||
660 |
declare bin_rsplit_aux.simps [simp del] |
|
661 |
declare bin_rsplitl_aux.simps [simp del] |
|
662 |
||
663 |
lemma bin_nth_cat: |
|
664 |
"bin_nth (bin_cat x k y) n = |
|
665 |
(if n < k then bin_nth y n else bin_nth x (n - k))" |
|
666 |
apply (induct k arbitrary: n y) |
|
71949 | 667 |
apply simp |
668 |
apply (case_tac n) |
|
669 |
apply (simp_all add: bit_Suc) |
|
70190 | 670 |
done |
671 |
||
71944 | 672 |
lemma bin_nth_drop_bit_iff: |
673 |
\<open>bin_nth (drop_bit n c) k \<longleftrightarrow> bin_nth c (n + k)\<close> |
|
71949 | 674 |
by (simp add: bit_drop_bit_eq) |
71944 | 675 |
|
676 |
lemma bin_nth_take_bit_iff: |
|
677 |
\<open>bin_nth (take_bit n c) k \<longleftrightarrow> k < n \<and> bin_nth c k\<close> |
|
71949 | 678 |
by (fact bit_take_bit_iff) |
71944 | 679 |
|
70190 | 680 |
lemma bin_nth_split: |
681 |
"bin_split n c = (a, b) \<Longrightarrow> |
|
682 |
(\<forall>k. bin_nth a k = bin_nth c (n + k)) \<and> |
|
683 |
(\<forall>k. bin_nth b k = (k < n \<and> bin_nth c k))" |
|
71944 | 684 |
by (auto simp add: bin_nth_drop_bit_iff bin_nth_take_bit_iff) |
70190 | 685 |
|
686 |
lemma bin_cat_zero [simp]: "bin_cat 0 n w = bintrunc n w" |
|
71947 | 687 |
by (simp add: bin_cat_eq_push_bit_add_take_bit) |
70190 | 688 |
|
689 |
lemma bintr_cat1: "bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b" |
|
71944 | 690 |
by (metis bin_cat_assoc bin_cat_zero) |
70190 | 691 |
|
692 |
lemma bintr_cat: "bintrunc m (bin_cat a n b) = |
|
693 |
bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)" |
|
694 |
by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr) |
|
695 |
||
696 |
lemma bintr_cat_same [simp]: "bintrunc n (bin_cat a n b) = bintrunc n b" |
|
697 |
by (auto simp add : bintr_cat) |
|
698 |
||
699 |
lemma cat_bintr [simp]: "bin_cat a n (bintrunc n b) = bin_cat a n b" |
|
71947 | 700 |
by (simp add: bin_cat_eq_push_bit_add_take_bit) |
70190 | 701 |
|
702 |
lemma split_bintrunc: "bin_split n c = (a, b) \<Longrightarrow> b = bintrunc n c" |
|
71947 | 703 |
by simp |
70190 | 704 |
|
705 |
lemma bin_cat_split: "bin_split n w = (u, v) \<Longrightarrow> w = bin_cat u n v" |
|
71944 | 706 |
by (auto simp add: bin_cat_eq_push_bit_add_take_bit bits_ident) |
707 |
||
708 |
lemma drop_bit_bin_cat_eq: |
|
709 |
\<open>drop_bit n (bin_cat v n w) = v\<close> |
|
710 |
by (induct n arbitrary: w) |
|
71986 | 711 |
(simp_all add: drop_bit_Suc) |
71944 | 712 |
|
713 |
lemma take_bit_bin_cat_eq: |
|
714 |
\<open>take_bit n (bin_cat v n w) = take_bit n w\<close> |
|
715 |
by (induct n arbitrary: w) |
|
71986 | 716 |
(simp_all add: take_bit_Suc mod_2_eq_odd) |
70190 | 717 |
|
718 |
lemma bin_split_cat: "bin_split n (bin_cat v n w) = (v, bintrunc n w)" |
|
71947 | 719 |
by (simp add: drop_bit_bin_cat_eq take_bit_bin_cat_eq) |
70190 | 720 |
|
721 |
lemma bin_split_zero [simp]: "bin_split n 0 = (0, 0)" |
|
71944 | 722 |
by simp |
70190 | 723 |
|
724 |
lemma bin_split_minus1 [simp]: |
|
725 |
"bin_split n (- 1) = (- 1, bintrunc n (- 1))" |
|
71947 | 726 |
by simp |
70190 | 727 |
|
728 |
lemma bin_split_trunc: |
|
729 |
"bin_split (min m n) c = (a, b) \<Longrightarrow> |
|
730 |
bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)" |
|
731 |
apply (induct n arbitrary: m b c, clarsimp) |
|
732 |
apply (simp add: bin_rest_trunc Let_def split: prod.split_asm) |
|
733 |
apply (case_tac m) |
|
71946 | 734 |
apply (auto simp: Let_def drop_bit_Suc take_bit_Suc mod_2_eq_odd split: prod.split_asm) |
70190 | 735 |
done |
736 |
||
737 |
lemma bin_split_trunc1: |
|
738 |
"bin_split n c = (a, b) \<Longrightarrow> |
|
739 |
bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)" |
|
740 |
apply (induct n arbitrary: m b c, clarsimp) |
|
741 |
apply (simp add: bin_rest_trunc Let_def split: prod.split_asm) |
|
742 |
apply (case_tac m) |
|
71986 | 743 |
apply (auto simp: Let_def drop_bit_Suc take_bit_Suc mod_2_eq_odd split: prod.split_asm) |
70190 | 744 |
done |
745 |
||
746 |
lemma bin_cat_num: "bin_cat a n b = a * 2 ^ n + bintrunc n b" |
|
71947 | 747 |
by (simp add: bin_cat_eq_push_bit_add_take_bit push_bit_eq_mult) |
70190 | 748 |
|
749 |
lemma bin_split_num: "bin_split n b = (b div 2 ^ n, b mod 2 ^ n)" |
|
71944 | 750 |
by (simp add: drop_bit_eq_div take_bit_eq_mod) |
70190 | 751 |
|
752 |
lemmas bin_rsplit_aux_simps = bin_rsplit_aux.simps bin_rsplitl_aux.simps |
|
753 |
lemmas rsplit_aux_simps = bin_rsplit_aux_simps |
|
754 |
||
755 |
lemmas th_if_simp1 = if_split [where P = "(=) l", THEN iffD1, THEN conjunct1, THEN mp] for l |
|
756 |
lemmas th_if_simp2 = if_split [where P = "(=) l", THEN iffD1, THEN conjunct2, THEN mp] for l |
|
757 |
||
758 |
lemmas rsplit_aux_simp1s = rsplit_aux_simps [THEN th_if_simp1] |
|
759 |
||
760 |
lemmas rsplit_aux_simp2ls = rsplit_aux_simps [THEN th_if_simp2] |
|
761 |
\<comment> \<open>these safe to \<open>[simp add]\<close> as require calculating \<open>m - n\<close>\<close> |
|
762 |
lemmas bin_rsplit_aux_simp2s [simp] = rsplit_aux_simp2ls [unfolded Let_def] |
|
763 |
lemmas rbscl = bin_rsplit_aux_simp2s (2) |
|
764 |
||
765 |
lemmas rsplit_aux_0_simps [simp] = |
|
766 |
rsplit_aux_simp1s [OF disjI1] rsplit_aux_simp1s [OF disjI2] |
|
767 |
||
768 |
lemma bin_rsplit_aux_append: "bin_rsplit_aux n m c (bs @ cs) = bin_rsplit_aux n m c bs @ cs" |
|
769 |
apply (induct n m c bs rule: bin_rsplit_aux.induct) |
|
770 |
apply (subst bin_rsplit_aux.simps) |
|
771 |
apply (subst bin_rsplit_aux.simps) |
|
772 |
apply (clarsimp split: prod.split) |
|
773 |
done |
|
774 |
||
775 |
lemma bin_rsplitl_aux_append: "bin_rsplitl_aux n m c (bs @ cs) = bin_rsplitl_aux n m c bs @ cs" |
|
776 |
apply (induct n m c bs rule: bin_rsplitl_aux.induct) |
|
777 |
apply (subst bin_rsplitl_aux.simps) |
|
778 |
apply (subst bin_rsplitl_aux.simps) |
|
779 |
apply (clarsimp split: prod.split) |
|
780 |
done |
|
781 |
||
782 |
lemmas rsplit_aux_apps [where bs = "[]"] = |
|
783 |
bin_rsplit_aux_append bin_rsplitl_aux_append |
|
784 |
||
785 |
lemmas rsplit_def_auxs = bin_rsplit_def bin_rsplitl_def |
|
786 |
||
787 |
lemmas rsplit_aux_alts = rsplit_aux_apps |
|
788 |
[unfolded append_Nil rsplit_def_auxs [symmetric]] |
|
789 |
||
790 |
lemma bin_split_minus: "0 < n \<Longrightarrow> bin_split (Suc (n - 1)) w = bin_split n w" |
|
791 |
by auto |
|
792 |
||
793 |
lemma bin_split_pred_simp [simp]: |
|
794 |
"(0::nat) < numeral bin \<Longrightarrow> |
|
795 |
bin_split (numeral bin) w = |
|
796 |
(let (w1, w2) = bin_split (numeral bin - 1) (bin_rest w) |
|
71986 | 797 |
in (w1, of_bool (odd w) + 2 * w2))" |
798 |
by (simp add: take_bit_rec drop_bit_rec mod_2_eq_odd) |
|
70190 | 799 |
|
800 |
lemma bin_rsplit_aux_simp_alt: |
|
801 |
"bin_rsplit_aux n m c bs = |
|
802 |
(if m = 0 \<or> n = 0 then bs |
|
803 |
else let (a, b) = bin_split n c in bin_rsplit n (m - n, a) @ b # bs)" |
|
804 |
apply (simp add: bin_rsplit_aux.simps [of n m c bs]) |
|
805 |
apply (subst rsplit_aux_alts) |
|
806 |
apply (simp add: bin_rsplit_def) |
|
807 |
done |
|
808 |
||
809 |
lemmas bin_rsplit_simp_alt = |
|
810 |
trans [OF bin_rsplit_def bin_rsplit_aux_simp_alt] |
|
811 |
||
812 |
lemmas bthrs = bin_rsplit_simp_alt [THEN [2] trans] |
|
813 |
||
814 |
lemma bin_rsplit_size_sign' [rule_format]: |
|
815 |
"n > 0 \<Longrightarrow> rev sw = bin_rsplit n (nw, w) \<Longrightarrow> \<forall>v\<in>set sw. bintrunc n v = v" |
|
816 |
apply (induct sw arbitrary: nw w) |
|
817 |
apply clarsimp |
|
818 |
apply clarsimp |
|
819 |
apply (drule bthrs) |
|
820 |
apply (simp (no_asm_use) add: Let_def split: prod.split_asm if_split_asm) |
|
821 |
apply clarify |
|
71947 | 822 |
apply simp |
70190 | 823 |
done |
824 |
||
825 |
lemmas bin_rsplit_size_sign = bin_rsplit_size_sign' [OF asm_rl |
|
826 |
rev_rev_ident [THEN trans] set_rev [THEN equalityD2 [THEN subsetD]]] |
|
827 |
||
828 |
lemma bin_nth_rsplit [rule_format] : |
|
829 |
"n > 0 \<Longrightarrow> m < n \<Longrightarrow> |
|
830 |
\<forall>w k nw. |
|
831 |
rev sw = bin_rsplit n (nw, w) \<longrightarrow> |
|
832 |
k < size sw \<longrightarrow> bin_nth (sw ! k) m = bin_nth w (k * n + m)" |
|
833 |
apply (induct sw) |
|
834 |
apply clarsimp |
|
835 |
apply clarsimp |
|
836 |
apply (drule bthrs) |
|
837 |
apply (simp (no_asm_use) add: Let_def split: prod.split_asm if_split_asm) |
|
838 |
apply (erule allE, erule impE, erule exI) |
|
839 |
apply (case_tac k) |
|
840 |
apply clarsimp |
|
841 |
prefer 2 |
|
842 |
apply clarsimp |
|
843 |
apply (erule allE) |
|
844 |
apply (erule (1) impE) |
|
71949 | 845 |
apply (simp add: bit_drop_bit_eq ac_simps) |
846 |
apply (simp add: bit_take_bit_iff ac_simps) |
|
70190 | 847 |
done |
848 |
||
849 |
lemma bin_rsplit_all: "0 < nw \<Longrightarrow> nw \<le> n \<Longrightarrow> bin_rsplit n (nw, w) = [bintrunc n w]" |
|
71947 | 850 |
by (auto simp: bin_rsplit_def rsplit_aux_simp2ls split: prod.split dest!: split_bintrunc) |
70190 | 851 |
|
852 |
lemma bin_rsplit_l [rule_format]: |
|
853 |
"\<forall>bin. bin_rsplitl n (m, bin) = bin_rsplit n (m, bintrunc m bin)" |
|
854 |
apply (rule_tac a = "m" in wf_less_than [THEN wf_induct]) |
|
855 |
apply (simp (no_asm) add: bin_rsplitl_def bin_rsplit_def) |
|
856 |
apply (rule allI) |
|
857 |
apply (subst bin_rsplitl_aux.simps) |
|
858 |
apply (subst bin_rsplit_aux.simps) |
|
859 |
apply (clarsimp simp: Let_def split: prod.split) |
|
71947 | 860 |
apply (simp add: ac_simps) |
70190 | 861 |
apply (subst rsplit_aux_alts(1)) |
862 |
apply (subst rsplit_aux_alts(2)) |
|
863 |
apply clarsimp |
|
864 |
unfolding bin_rsplit_def bin_rsplitl_def |
|
71944 | 865 |
apply (simp add: drop_bit_take_bit) |
866 |
apply (case_tac \<open>x < n\<close>) |
|
867 |
apply (simp_all add: not_less min_def) |
|
70190 | 868 |
done |
869 |
||
870 |
lemma bin_rsplit_rcat [rule_format]: |
|
871 |
"n > 0 \<longrightarrow> bin_rsplit n (n * size ws, bin_rcat n ws) = map (bintrunc n) ws" |
|
872 |
apply (unfold bin_rsplit_def bin_rcat_def) |
|
873 |
apply (rule_tac xs = ws in rev_induct) |
|
874 |
apply clarsimp |
|
875 |
apply clarsimp |
|
876 |
apply (subst rsplit_aux_alts) |
|
71947 | 877 |
apply (simp add: drop_bit_bin_cat_eq take_bit_bin_cat_eq) |
70190 | 878 |
done |
879 |
||
880 |
lemma bin_rsplit_aux_len_le [rule_format] : |
|
881 |
"\<forall>ws m. n \<noteq> 0 \<longrightarrow> ws = bin_rsplit_aux n nw w bs \<longrightarrow> |
|
882 |
length ws \<le> m \<longleftrightarrow> nw + length bs * n \<le> m * n" |
|
883 |
proof - |
|
884 |
have *: R |
|
885 |
if d: "i \<le> j \<or> m < j'" |
|
886 |
and R1: "i * k \<le> j * k \<Longrightarrow> R" |
|
887 |
and R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R" |
|
888 |
for i j j' k k' m :: nat and R |
|
889 |
using d |
|
890 |
apply safe |
|
891 |
apply (rule R1, erule mult_le_mono1) |
|
892 |
apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]]) |
|
893 |
done |
|
894 |
have **: "0 < sc \<Longrightarrow> sc - n + (n + lb * n) \<le> m * n \<longleftrightarrow> sc + lb * n \<le> m * n" |
|
895 |
for sc m n lb :: nat |
|
896 |
apply safe |
|
897 |
apply arith |
|
898 |
apply (case_tac "sc \<ge> n") |
|
899 |
apply arith |
|
900 |
apply (insert linorder_le_less_linear [of m lb]) |
|
901 |
apply (erule_tac k=n and k'=n in *) |
|
902 |
apply arith |
|
903 |
apply simp |
|
904 |
done |
|
905 |
show ?thesis |
|
906 |
apply (induct n nw w bs rule: bin_rsplit_aux.induct) |
|
907 |
apply (subst bin_rsplit_aux.simps) |
|
908 |
apply (simp add: ** Let_def split: prod.split) |
|
909 |
done |
|
910 |
qed |
|
911 |
||
912 |
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" |
|
913 |
by (auto simp: bin_rsplit_def bin_rsplit_aux_len_le) |
|
914 |
||
915 |
lemma bin_rsplit_aux_len: |
|
916 |
"n \<noteq> 0 \<Longrightarrow> length (bin_rsplit_aux n nw w cs) = (nw + n - 1) div n + length cs" |
|
917 |
apply (induct n nw w cs rule: bin_rsplit_aux.induct) |
|
918 |
apply (subst bin_rsplit_aux.simps) |
|
919 |
apply (clarsimp simp: Let_def split: prod.split) |
|
920 |
apply (erule thin_rl) |
|
921 |
apply (case_tac m) |
|
922 |
apply simp |
|
923 |
apply (case_tac "m \<le> n") |
|
924 |
apply (auto simp add: div_add_self2) |
|
925 |
done |
|
926 |
||
927 |
lemma bin_rsplit_len: "n \<noteq> 0 \<Longrightarrow> length (bin_rsplit n (nw, w)) = (nw + n - 1) div n" |
|
928 |
by (auto simp: bin_rsplit_def bin_rsplit_aux_len) |
|
929 |
||
930 |
lemma bin_rsplit_aux_len_indep: |
|
931 |
"n \<noteq> 0 \<Longrightarrow> length bs = length cs \<Longrightarrow> |
|
932 |
length (bin_rsplit_aux n nw v bs) = |
|
933 |
length (bin_rsplit_aux n nw w cs)" |
|
934 |
proof (induct n nw w cs arbitrary: v bs rule: bin_rsplit_aux.induct) |
|
935 |
case (1 n m w cs v bs) |
|
936 |
show ?case |
|
937 |
proof (cases "m = 0") |
|
938 |
case True |
|
939 |
with \<open>length bs = length cs\<close> show ?thesis by simp |
|
940 |
next |
|
941 |
case False |
|
71944 | 942 |
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 | 943 |
have hyp: "\<And>v bs. length bs = Suc (length cs) \<Longrightarrow> |
944 |
length (bin_rsplit_aux n (m - n) v bs) = |
|
71944 | 945 |
length (bin_rsplit_aux n (m - n) (drop_bit n w) (take_bit n w # cs))" |
946 |
using bin_rsplit_aux_len by fastforce |
|
70190 | 947 |
from \<open>length bs = length cs\<close> \<open>n \<noteq> 0\<close> show ?thesis |
948 |
by (auto simp add: bin_rsplit_aux_simp_alt Let_def bin_rsplit_len split: prod.split) |
|
949 |
qed |
|
950 |
qed |
|
951 |
||
952 |
lemma bin_rsplit_len_indep: |
|
953 |
"n \<noteq> 0 \<Longrightarrow> length (bin_rsplit n (nw, v)) = length (bin_rsplit n (nw, w))" |
|
954 |
apply (unfold bin_rsplit_def) |
|
955 |
apply (simp (no_asm)) |
|
956 |
apply (erule bin_rsplit_aux_len_indep) |
|
957 |
apply (rule refl) |
|
958 |
done |
|
959 |
||
960 |
||
61799 | 961 |
subsection \<open>Logical operations\<close> |
24353 | 962 |
|
70191 | 963 |
primrec bin_sc :: "nat \<Rightarrow> bool \<Rightarrow> int \<Rightarrow> int" |
964 |
where |
|
71986 | 965 |
Z: "bin_sc 0 b w = of_bool b + 2 * bin_rest w" |
966 |
| Suc: "bin_sc (Suc n) b w = of_bool (odd w) + 2 * bin_sc n b (w div 2)" |
|
967 |
||
968 |
lemma bin_nth_sc [simp]: "bit (bin_sc n b w) n \<longleftrightarrow> b" |
|
969 |
by (induction n arbitrary: w) (simp_all add: bit_Suc) |
|
70191 | 970 |
|
971 |
lemma bin_sc_sc_same [simp]: "bin_sc n c (bin_sc n b w) = bin_sc n c w" |
|
71986 | 972 |
by (induction n arbitrary: w) (simp_all add: bit_Suc) |
70191 | 973 |
|
974 |
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)" |
|
975 |
apply (induct n arbitrary: w m) |
|
976 |
apply (case_tac [!] m) |
|
977 |
apply auto |
|
978 |
done |
|
979 |
||
980 |
lemma bin_nth_sc_gen: "bin_nth (bin_sc n b w) m = (if m = n then b else bin_nth w m)" |
|
71949 | 981 |
apply (induct n arbitrary: w m) |
982 |
apply (case_tac m; simp add: bit_Suc) |
|
983 |
apply (case_tac m; simp add: bit_Suc) |
|
984 |
done |
|
70191 | 985 |
|
71986 | 986 |
lemma bin_sc_eq: |
987 |
\<open>bin_sc n False = unset_bit n\<close> |
|
988 |
\<open>bin_sc n True = Bit_Operations.set_bit n\<close> |
|
989 |
by (simp_all add: fun_eq_iff bit_eq_iff) |
|
990 |
(simp_all add: bin_nth_sc_gen bit_set_bit_iff bit_unset_bit_iff) |
|
991 |
||
70191 | 992 |
lemma bin_sc_nth [simp]: "bin_sc n (bin_nth w n) w = w" |
71986 | 993 |
by (rule bit_eqI) (simp add: bin_nth_sc_gen) |
70191 | 994 |
|
995 |
lemma bin_sign_sc [simp]: "bin_sign (bin_sc n b w) = bin_sign w" |
|
71986 | 996 |
proof (induction n arbitrary: w) |
997 |
case 0 |
|
998 |
then show ?case |
|
999 |
by (auto simp add: bin_sign_def) (use bin_rest_ge_0 in fastforce) |
|
1000 |
next |
|
1001 |
case (Suc n) |
|
1002 |
from Suc [of \<open>w div 2\<close>] |
|
1003 |
show ?case by (auto simp add: bin_sign_def split: if_splits) |
|
1004 |
qed |
|
1005 |
||
1006 |
lemma bin_sc_bintr [simp]: |
|
1007 |
"bintrunc m (bin_sc n x (bintrunc m w)) = bintrunc m (bin_sc n x w)" |
|
1008 |
apply (cases x) |
|
1009 |
apply (simp_all add: bin_sc_eq bit_eq_iff) |
|
1010 |
apply (auto simp add: bit_take_bit_iff bit_set_bit_iff bit_unset_bit_iff) |
|
70191 | 1011 |
done |
1012 |
||
1013 |
lemma bin_clr_le: "bin_sc n False w \<le> w" |
|
71986 | 1014 |
by (simp add: bin_sc_eq unset_bit_less_eq) |
70191 | 1015 |
|
1016 |
lemma bin_set_ge: "bin_sc n True w \<ge> w" |
|
71986 | 1017 |
by (simp add: bin_sc_eq set_bit_greater_eq) |
70191 | 1018 |
|
1019 |
lemma bintr_bin_clr_le: "bintrunc n (bin_sc m False w) \<le> bintrunc n w" |
|
71986 | 1020 |
by (simp add: bin_sc_eq take_bit_unset_bit_eq unset_bit_less_eq) |
70191 | 1021 |
|
1022 |
lemma bintr_bin_set_ge: "bintrunc n (bin_sc m True w) \<ge> bintrunc n w" |
|
71986 | 1023 |
by (simp add: bin_sc_eq take_bit_set_bit_eq set_bit_greater_eq) |
70191 | 1024 |
|
1025 |
lemma bin_sc_FP [simp]: "bin_sc n False 0 = 0" |
|
1026 |
by (induct n) auto |
|
1027 |
||
1028 |
lemma bin_sc_TM [simp]: "bin_sc n True (- 1) = - 1" |
|
1029 |
by (induct n) auto |
|
1030 |
||
1031 |
lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP |
|
1032 |
||
1033 |
lemma bin_sc_minus: "0 < n \<Longrightarrow> bin_sc (Suc (n - 1)) b w = bin_sc n b w" |
|
1034 |
by auto |
|
1035 |
||
1036 |
lemmas bin_sc_Suc_minus = |
|
1037 |
trans [OF bin_sc_minus [symmetric] bin_sc.Suc] |
|
1038 |
||
1039 |
lemma bin_sc_numeral [simp]: |
|
1040 |
"bin_sc (numeral k) b w = |
|
71986 | 1041 |
of_bool (odd w) + 2 * bin_sc (pred_numeral k) b (w div 2)" |
70191 | 1042 |
by (simp add: numeral_eq_Suc) |
1043 |
||
1044 |
instantiation int :: bit_operations |
|
25762 | 1045 |
begin |
1046 |
||
70191 | 1047 |
definition [iff]: "i !! n \<longleftrightarrow> bin_nth i n" |
1048 |
||
1049 |
definition "lsb i = i !! 0" for i :: int |
|
1050 |
||
1051 |
definition "set_bit i n b = bin_sc n b i" |
|
1052 |
||
1053 |
definition "shiftl x n = x * 2 ^ n" for x :: int |
|
1054 |
||
1055 |
definition "shiftr x n = x div 2 ^ n" for x :: int |
|
1056 |
||
1057 |
definition "msb x \<longleftrightarrow> x < 0" for x :: int |
|
1058 |
||
25762 | 1059 |
instance .. |
1060 |
||
1061 |
end |
|
24353 | 1062 |
|
71943 | 1063 |
lemma shiftl_eq_push_bit: |
1064 |
\<open>k << n = push_bit n k\<close> for k :: int |
|
1065 |
by (simp add: shiftl_int_def push_bit_eq_mult) |
|
1066 |
||
1067 |
lemma shiftr_eq_drop_bit: |
|
1068 |
\<open>k >> n = drop_bit n k\<close> for k :: int |
|
1069 |
by (simp add: shiftr_int_def drop_bit_eq_div) |
|
1070 |
||
70191 | 1071 |
|
61799 | 1072 |
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
|
1073 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1074 |
lemmas int_not_def = not_int_def |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1075 |
|
24333 | 1076 |
lemma int_not_simps [simp]: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1077 |
"NOT (0::int) = -1" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1078 |
"NOT (1::int) = -2" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1079 |
"NOT (- 1::int) = 0" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1080 |
"NOT (numeral w::int) = - numeral (w + Num.One)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1081 |
"NOT (- numeral (Num.Bit0 w)::int) = numeral (Num.BitM w)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1082 |
"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
|
1083 |
by (simp_all add: not_int_def) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1084 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1085 |
lemma int_not_not: "NOT (NOT x) = x" |
67120 | 1086 |
for x :: int |
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1087 |
by (fact bit.double_compl) |
46017 | 1088 |
|
67120 | 1089 |
lemma int_and_0 [simp]: "0 AND x = 0" |
1090 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1091 |
by (fact bit.conj_zero_left) |
46019 | 1092 |
|
67120 | 1093 |
lemma int_and_m1 [simp]: "-1 AND x = x" |
1094 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1095 |
by (fact bit.conj_one_left) |
46019 | 1096 |
|
67120 | 1097 |
lemma int_or_zero [simp]: "0 OR x = x" |
1098 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1099 |
by (fact bit.disj_zero_left) |
46018 | 1100 |
|
67120 | 1101 |
lemma int_or_minus1 [simp]: "-1 OR x = -1" |
1102 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1103 |
by (fact bit.disj_one_left) |
46017 | 1104 |
|
67120 | 1105 |
lemma int_xor_zero [simp]: "0 XOR x = x" |
1106 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1107 |
by (fact bit.xor_zero_left) |
46018 | 1108 |
|
67120 | 1109 |
|
61799 | 1110 |
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
|
1111 |
|
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1112 |
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
|
1113 |
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
|
1114 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
1115 |
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
|
1116 |
by simp |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1117 |
|
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1118 |
lemma bin_rest_AND [simp]: "bin_rest (x AND y) = bin_rest x AND bin_rest y" |
71986 | 1119 |
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
|
1120 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
1121 |
lemma bin_last_AND [simp]: "bin_last (x AND y) \<longleftrightarrow> bin_last x \<and> bin_last y" |
71986 | 1122 |
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
|
1123 |
|
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1124 |
lemma bin_rest_OR [simp]: "bin_rest (x OR y) = bin_rest x OR bin_rest y" |
71986 | 1125 |
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
|
1126 |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
1127 |
lemma bin_last_OR [simp]: "bin_last (x OR y) \<longleftrightarrow> bin_last x \<or> bin_last y" |
71986 | 1128 |
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
|
1129 |
|
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1130 |
lemma bin_rest_XOR [simp]: "bin_rest (x XOR y) = bin_rest x XOR bin_rest y" |
71986 | 1131 |
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
|
1132 |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1133 |
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 | 1134 |
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
|
1135 |
|
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1136 |
lemma bin_nth_ops: |
67120 | 1137 |
"\<And>x y. bin_nth (x AND y) n \<longleftrightarrow> bin_nth x n \<and> bin_nth y n" |
1138 |
"\<And>x y. bin_nth (x OR y) n \<longleftrightarrow> bin_nth x n \<or> bin_nth y n" |
|
1139 |
"\<And>x y. bin_nth (x XOR y) n \<longleftrightarrow> bin_nth x n \<noteq> bin_nth y n" |
|
1140 |
"\<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
|
1141 |
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
|
1142 |
|
67120 | 1143 |
|
61799 | 1144 |
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
|
1145 |
|
67120 | 1146 |
lemma int_xor_minus1 [simp]: "-1 XOR x = NOT x" |
1147 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1148 |
by (fact bit.xor_one_left) |
46018 | 1149 |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1150 |
lemma int_xor_extra_simps [simp]: |
67120 | 1151 |
"w XOR 0 = w" |
1152 |
"w XOR -1 = NOT w" |
|
1153 |
for w :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1154 |
by simp_all |
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1155 |
|
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1156 |
lemma int_or_extra_simps [simp]: |
67120 | 1157 |
"w OR 0 = w" |
1158 |
"w OR -1 = -1" |
|
1159 |
for w :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1160 |
by simp_all |
24333 | 1161 |
|
37667 | 1162 |
lemma int_and_extra_simps [simp]: |
67120 | 1163 |
"w AND 0 = 0" |
1164 |
"w AND -1 = w" |
|
1165 |
for w :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1166 |
by simp_all |
24333 | 1167 |
|
67120 | 1168 |
text \<open>Commutativity of the above.\<close> |
24333 | 1169 |
lemma bin_ops_comm: |
67120 | 1170 |
fixes x y :: int |
1171 |
shows int_and_comm: "x AND y = y AND x" |
|
1172 |
and int_or_comm: "x OR y = y OR x" |
|
1173 |
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
|
1174 |
by (simp_all add: ac_simps) |
24333 | 1175 |
|
1176 |
lemma bin_ops_same [simp]: |
|
67120 | 1177 |
"x AND x = x" |
1178 |
"x OR x = x" |
|
1179 |
"x XOR x = 0" |
|
1180 |
for x :: int |
|
71957
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1181 |
by simp_all |
24333 | 1182 |
|
65363 | 1183 |
lemmas bin_log_esimps = |
24333 | 1184 |
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
|
1185 |
int_and_0 int_and_m1 int_or_zero int_or_minus1 int_xor_zero int_xor_minus1 |
24333 | 1186 |
|
67120 | 1187 |
|
1188 |
subsubsection \<open>Basic properties of logical (bit-wise) operations\<close> |
|
24333 | 1189 |
|
67120 | 1190 |
lemma bbw_ao_absorb: "x AND (y OR x) = x \<and> x OR (y AND x) = x" |
1191 |
for x y :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1192 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1193 |
|
1194 |
lemma bbw_ao_absorbs_other: |
|
67120 | 1195 |
"x AND (x OR y) = x \<and> (y AND x) OR x = x" |
1196 |
"(y OR x) AND x = x \<and> x OR (x AND y) = x" |
|
1197 |
"(x OR y) AND x = x \<and> (x AND y) OR x = x" |
|
1198 |
for x y :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1199 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24353 | 1200 |
|
24333 | 1201 |
lemmas bbw_ao_absorbs [simp] = bbw_ao_absorb bbw_ao_absorbs_other |
1202 |
||
67120 | 1203 |
lemma int_xor_not: "(NOT x) XOR y = NOT (x XOR y) \<and> x XOR (NOT y) = NOT (x XOR y)" |
1204 |
for x y :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1205 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1206 |
|
67120 | 1207 |
lemma int_and_assoc: "(x AND y) AND z = x AND (y AND z)" |
1208 |
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
|
1209 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1210 |
|
67120 | 1211 |
lemma int_or_assoc: "(x OR y) OR z = x OR (y OR z)" |
1212 |
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
|
1213 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1214 |
|
67120 | 1215 |
lemma int_xor_assoc: "(x XOR y) XOR z = x XOR (y XOR z)" |
1216 |
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
|
1217 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1218 |
|
1219 |
lemmas bbw_assocs = int_and_assoc int_or_assoc int_xor_assoc |
|
1220 |
||
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1221 |
(* BH: Why are these declared as simp rules??? *) |
65363 | 1222 |
lemma bbw_lcs [simp]: |
67120 | 1223 |
"y AND (x AND z) = x AND (y AND z)" |
1224 |
"y OR (x OR z) = x OR (y OR z)" |
|
1225 |
"y XOR (x XOR z) = x XOR (y XOR z)" |
|
1226 |
for x y :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1227 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1228 |
|
65363 | 1229 |
lemma bbw_not_dist: |
67120 | 1230 |
"NOT (x OR y) = (NOT x) AND (NOT y)" |
1231 |
"NOT (x AND y) = (NOT x) OR (NOT y)" |
|
1232 |
for x y :: int |
|
45543
827bf668c822
HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents:
45529
diff
changeset
|
1233 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1234 |
|
67120 | 1235 |
lemma bbw_oa_dist: "(x AND y) OR z = (x OR z) AND (y OR z)" |
1236 |
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
|
1237 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1238 |
|
67120 | 1239 |
lemma bbw_ao_dist: "(x OR y) AND z = (x AND z) OR (y AND z)" |
1240 |
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
|
1241 |
by (auto simp add: bin_eq_iff bin_nth_ops) |
24333 | 1242 |
|
24367
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
huffman
parents:
24366
diff
changeset
|
1243 |
(* |
3e29eafabe16
AC rules for bitwise logical operators no longer declared simp
huffman
parents:
24366
diff
changeset
|
1244 |
Why were these declared simp??? |
65363 | 1245 |
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
|
1246 |
*) |
24333 | 1247 |
|
67120 | 1248 |
|
61799 | 1249 |
subsubsection \<open>Simplification with numerals\<close> |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1250 |
|
67120 | 1251 |
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
|
1252 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1253 |
lemma bin_rest_neg_numeral_BitM [simp]: |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1254 |
"bin_rest (- numeral (Num.BitM w)) = - numeral w" |
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
1255 |
by simp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1256 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1257 |
lemma bin_last_neg_numeral_BitM [simp]: |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
54489
diff
changeset
|
1258 |
"bin_last (- numeral (Num.BitM w))" |
71941 | 1259 |
by simp |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1260 |
|
67120 | 1261 |
(* FIXME: The rule sets below are very large (24 rules for each |
1262 |
operator). Is there a simpler way to do this? *) |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1263 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1264 |
lemma int_and_numerals [simp]: |
71986 | 1265 |
"numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND numeral y)" |
1266 |
"numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (2 :: int) * (numeral x AND numeral y)" |
|
1267 |
"numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND numeral y)" |
|
1268 |
"numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x AND numeral y)" |
|
1269 |
"numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND - numeral y)" |
|
1270 |
"numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (2 :: int) * (numeral x AND - numeral (y + Num.One))" |
|
1271 |
"numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND - numeral y)" |
|
1272 |
"numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x AND - numeral (y + Num.One))" |
|
1273 |
"- numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (2 :: int) * (- numeral x AND numeral y)" |
|
1274 |
"- numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (2 :: int) * (- numeral x AND numeral y)" |
|
1275 |
"- numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (2 :: int) * (- numeral (x + Num.One) AND numeral y)" |
|
1276 |
"- numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) AND numeral y)" |
|
1277 |
"- numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (- numeral x AND - numeral y)" |
|
1278 |
"- numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (2 :: int) * (- numeral x AND - numeral (y + Num.One))" |
|
1279 |
"- numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (- numeral (x + Num.One) AND - numeral y)" |
|
1280 |
"- 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
|
1281 |
"(1::int) AND numeral (Num.Bit0 y) = 0" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1282 |
"(1::int) AND numeral (Num.Bit1 y) = 1" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1283 |
"(1::int) AND - numeral (Num.Bit0 y) = 0" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1284 |
"(1::int) AND - numeral (Num.Bit1 y) = 1" |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1285 |
"numeral (Num.Bit0 x) AND (1::int) = 0" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1286 |
"numeral (Num.Bit1 x) AND (1::int) = 1" |
54489
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1287 |
"- numeral (Num.Bit0 x) AND (1::int) = 0" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1288 |
"- numeral (Num.Bit1 x) AND (1::int) = 1" |
67120 | 1289 |
by (rule bin_rl_eqI; simp)+ |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1290 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1291 |
lemma int_or_numerals [simp]: |
71986 | 1292 |
"numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (2 :: int) * (numeral x OR numeral y)" |
1293 |
"numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR numeral y)" |
|
1294 |
"numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x OR numeral y)" |
|
1295 |
"numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR numeral y)" |
|
1296 |
"numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (2 :: int) * (numeral x OR - numeral y)" |
|
1297 |
"numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR - numeral (y + Num.One))" |
|
1298 |
"numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x OR - numeral y)" |
|
1299 |
"numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR - numeral (y + Num.One))" |
|
1300 |
"- numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (2 :: int) * (- numeral x OR numeral y)" |
|
1301 |
"- numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x OR numeral y)" |
|
1302 |
"- numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR numeral y)" |
|
1303 |
"- numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR numeral y)" |
|
1304 |
"- numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (2 :: int) * (- numeral x OR - numeral y)" |
|
1305 |
"- numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x OR - numeral (y + Num.One))" |
|
1306 |
"- numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR - numeral y)" |
|
1307 |
"- 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
|
1308 |
"(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
|
1309 |
"(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
|
1310 |
"(1::int) OR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1311 |
"(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
|
1312 |
"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
|
1313 |
"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
|
1314 |
"- numeral (Num.Bit0 x) OR (1::int) = - numeral (Num.BitM x)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1315 |
"- numeral (Num.Bit1 x) OR (1::int) = - numeral (Num.Bit1 x)" |
67120 | 1316 |
by (rule bin_rl_eqI; simp)+ |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1317 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1318 |
lemma int_xor_numerals [simp]: |
71986 | 1319 |
"numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (2 :: int) * (numeral x XOR numeral y)" |
1320 |
"numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x XOR numeral y)" |
|
1321 |
"numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x XOR numeral y)" |
|
1322 |
"numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (2 :: int) * (numeral x XOR numeral y)" |
|
1323 |
"numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (2 :: int) * (numeral x XOR - numeral y)" |
|
1324 |
"numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x XOR - numeral (y + Num.One))" |
|
1325 |
"numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x XOR - numeral y)" |
|
1326 |
"numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (2 :: int) * (numeral x XOR - numeral (y + Num.One))" |
|
1327 |
"- numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (2 :: int) * (- numeral x XOR numeral y)" |
|
1328 |
"- numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x XOR numeral y)" |
|
1329 |
"- numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) XOR numeral y)" |
|
1330 |
"- numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (2 :: int) * (- numeral (x + Num.One) XOR numeral y)" |
|
1331 |
"- numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (2 :: int) * (- numeral x XOR - numeral y)" |
|
1332 |
"- numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x XOR - numeral (y + Num.One))" |
|
1333 |
"- numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) XOR - numeral y)" |
|
1334 |
"- 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
|
1335 |
"(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
|
1336 |
"(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
|
1337 |
"(1::int) XOR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1338 |
"(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
|
1339 |
"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
|
1340 |
"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
|
1341 |
"- numeral (Num.Bit0 x) XOR (1::int) = - numeral (Num.BitM x)" |
03ff4d1e6784
eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents:
54427
diff
changeset
|
1342 |
"- numeral (Num.Bit1 x) XOR (1::int) = - numeral (Num.Bit0 (x + Num.One))" |
67120 | 1343 |
by (rule bin_rl_eqI; simp)+ |
1344 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46610
diff
changeset
|
1345 |
|
61799 | 1346 |
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
|
1347 |
|
71986 | 1348 |
lemma plus_and_or: "(x AND y) + (x OR y) = x + y" for x y :: int |
1349 |
proof (induction x arbitrary: y rule: int_bit_induct) |
|
1350 |
case zero |
|
1351 |
then show ?case |
|
1352 |
by simp |
|
1353 |
next |
|
1354 |
case minus |
|
1355 |
then show ?case |
|
1356 |
by simp |
|
1357 |
next |
|
1358 |
case (even x) |
|
1359 |
from even.IH [of \<open>y div 2\<close>] |
|
1360 |
show ?case |
|
1361 |
by (auto simp add: and_int_rec [of _ y] or_int_rec [of _ y] elim: oddE) |
|
1362 |
next |
|
1363 |
case (odd x) |
|
1364 |
from odd.IH [of \<open>y div 2\<close>] |
|
1365 |
show ?case |
|
1366 |
by (auto simp add: and_int_rec [of _ y] or_int_rec [of _ y] elim: oddE) |
|
1367 |
qed |
|
24333 | 1368 |
|
67120 | 1369 |
lemma le_int_or: "bin_sign y = 0 \<Longrightarrow> x \<le> x OR y" |
1370 |
for x y :: int |
|
71986 | 1371 |
by (simp add: bin_sign_def or_greater_eq split: if_splits) |
24333 | 1372 |
|
1373 |
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
|
1374 |
xtrans(3) [OF bbw_ao_absorbs (2) [THEN conjunct2, symmetric] le_int_or] |
24333 | 1375 |
|
67120 | 1376 |
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
|
1377 |
lemma bin_add_not: "x + NOT x = (-1::int)" |
71986 | 1378 |
by (simp add: not_int_def) |
1379 |
||
1380 |
lemma AND_mod: "x AND (2 ^ n - 1) = x mod 2 ^ n" |
|
70169 | 1381 |
for x :: int |
71986 | 1382 |
by (simp flip: take_bit_eq_mod add: take_bit_eq_mask mask_eq_exp_minus_1) |
70169 | 1383 |
|
67120 | 1384 |
|
70172 | 1385 |
subsubsection \<open>Comparison\<close> |
1386 |
||
1387 |
lemma AND_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
1388 |
fixes x y :: int |
|
1389 |
assumes "0 \<le> x" |
|
1390 |
shows "0 \<le> x AND y" |
|
71986 | 1391 |
using assms by simp |
70172 | 1392 |
|
1393 |
lemma OR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
1394 |
fixes x y :: int |
|
1395 |
assumes "0 \<le> x" "0 \<le> y" |
|
1396 |
shows "0 \<le> x OR y" |
|
71986 | 1397 |
using assms by simp |
70172 | 1398 |
|
1399 |
lemma XOR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
1400 |
fixes x y :: int |
|
1401 |
assumes "0 \<le> x" "0 \<le> y" |
|
1402 |
shows "0 \<le> x XOR y" |
|
71986 | 1403 |
using assms by simp |
70172 | 1404 |
|
1405 |
lemma AND_upper1 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
1406 |
fixes x y :: int |
|
1407 |
assumes "0 \<le> x" |
|
1408 |
shows "x AND y \<le> x" |
|
71986 | 1409 |
using assms by (induction x arbitrary: y rule: int_bit_induct) |
1410 |
(simp_all add: and_int_rec [of \<open>_ * 2\<close>] and_int_rec [of \<open>1 + _ * 2\<close>] add_increasing) |
|
70172 | 1411 |
|
1412 |
lemmas AND_upper1' [simp] = order_trans [OF AND_upper1] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
1413 |
lemmas AND_upper1'' [simp] = order_le_less_trans [OF AND_upper1] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
1414 |
||
1415 |
lemma AND_upper2 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
1416 |
fixes x y :: int |
|
1417 |
assumes "0 \<le> y" |
|
1418 |
shows "x AND y \<le> y" |
|
71986 | 1419 |
using assms AND_upper1 [of y x] by (simp add: ac_simps) |
70172 | 1420 |
|
1421 |
lemmas AND_upper2' [simp] = order_trans [OF AND_upper2] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
1422 |
lemmas AND_upper2'' [simp] = order_le_less_trans [OF AND_upper2] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
1423 |
||
1424 |
lemma OR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
1425 |
fixes x y :: int |
|
1426 |
assumes "0 \<le> x" "x < 2 ^ n" "y < 2 ^ n" |
|
1427 |
shows "x OR y < 2 ^ n" |
|
71986 | 1428 |
using assms proof (induction x arbitrary: y n rule: int_bit_induct) |
1429 |
case zero |
|
1430 |
then show ?case |
|
1431 |
by simp |
|
1432 |
next |
|
1433 |
case minus |
|
1434 |
then show ?case |
|
1435 |
by simp |
|
1436 |
next |
|
1437 |
case (even x) |
|
1438 |
from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps |
|
1439 |
show ?case |
|
1440 |
by (cases n) (auto simp add: or_int_rec [of \<open>_ * 2\<close>] elim: oddE) |
|
1441 |
next |
|
1442 |
case (odd x) |
|
1443 |
from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps |
|
70172 | 1444 |
show ?case |
71986 | 1445 |
by (cases n) (auto simp add: or_int_rec [of \<open>1 + _ * 2\<close>], linarith) |
1446 |
qed |
|
70172 | 1447 |
|
1448 |
lemma XOR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close> |
|
1449 |
fixes x y :: int |
|
1450 |
assumes "0 \<le> x" "x < 2 ^ n" "y < 2 ^ n" |
|
1451 |
shows "x XOR y < 2 ^ n" |
|
71986 | 1452 |
using assms proof (induction x arbitrary: y n rule: int_bit_induct) |
1453 |
case zero |
|
1454 |
then show ?case |
|
1455 |
by simp |
|
70172 | 1456 |
next |
71986 | 1457 |
case minus |
1458 |
then show ?case |
|
1459 |
by simp |
|
70172 | 1460 |
next |
71986 | 1461 |
case (even x) |
1462 |
from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps |
|
1463 |
show ?case |
|
1464 |
by (cases n) (auto simp add: xor_int_rec [of \<open>_ * 2\<close>] elim: oddE) |
|
1465 |
next |
|
1466 |
case (odd x) |
|
1467 |
from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps |
|
70172 | 1468 |
show ?case |
71986 | 1469 |
by (cases n) (auto simp add: xor_int_rec [of \<open>1 + _ * 2\<close>]) |
70172 | 1470 |
qed |
1471 |
||
1472 |
||
61799 | 1473 |
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
|
1474 |
|
65363 | 1475 |
lemma bin_trunc_ao: |
67120 | 1476 |
"bintrunc n x AND bintrunc n y = bintrunc n (x AND y)" |
1477 |
"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
|
1478 |
by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr) |
24364 | 1479 |
|
67120 | 1480 |
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
|
1481 |
by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr) |
24364 | 1482 |
|
67120 | 1483 |
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
|
1484 |
by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr) |
24364 | 1485 |
|
67120 | 1486 |
text \<open>Want theorems of the form of \<open>bin_trunc_xor\<close>.\<close> |
1487 |
lemma bintr_bintr_i: "x = bintrunc n y \<Longrightarrow> bintrunc n x = bintrunc n y" |
|
24364 | 1488 |
by auto |
1489 |
||
1490 |
lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i] |
|
1491 |
lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i] |
|
1492 |
||
70190 | 1493 |
|
1494 |
subsubsection \<open>More lemmas\<close> |
|
1495 |
||
1496 |
lemma not_int_cmp_0 [simp]: |
|
1497 |
fixes i :: int shows |
|
1498 |
"0 < NOT i \<longleftrightarrow> i < -1" |
|
1499 |
"0 \<le> NOT i \<longleftrightarrow> i < 0" |
|
1500 |
"NOT i < 0 \<longleftrightarrow> i \<ge> 0" |
|
1501 |
"NOT i \<le> 0 \<longleftrightarrow> i \<ge> -1" |
|
1502 |
by(simp_all add: int_not_def) arith+ |
|
1503 |
||
1504 |
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
|
1505 |
by (fact bit.conj_disj_distrib) |
70190 | 1506 |
|
1507 |
lemmas int_and_ac = bbw_lcs(1) int_and_comm int_and_assoc |
|
1508 |
||
1509 |
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
|
1510 |
by simp |
70190 | 1511 |
|
1512 |
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
|
1513 |
by (simp add: bit_eq_iff bit_and_iff bit_not_iff) |
70190 | 1514 |
|
1515 |
lemma and_xor_dist: fixes x :: int shows |
|
1516 |
"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
|
1517 |
by (fact bit.conj_xor_distrib) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1518 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1519 |
lemma int_and_lt0 [simp]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1520 |
\<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
|
1521 |
by (fact and_negative_int_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1522 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1523 |
lemma int_and_ge0 [simp]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1524 |
\<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
|
1525 |
by (fact and_nonnegative_int_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1526 |
|
70190 | 1527 |
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
|
1528 |
by (fact and_one_eq) |
70190 | 1529 |
|
1530 |
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
|
1531 |
by (fact one_and_eq) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1532 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1533 |
lemma int_or_lt0 [simp]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1534 |
\<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
|
1535 |
by (fact or_negative_int_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1536 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1537 |
lemma int_or_ge0 [simp]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1538 |
\<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
|
1539 |
by (fact or_nonnegative_int_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1540 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1541 |
lemma int_xor_lt0 [simp]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1542 |
\<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
|
1543 |
by (fact xor_negative_int_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1544 |
|
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1545 |
lemma int_xor_ge0 [simp]: |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1546 |
\<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
|
1547 |
by (fact xor_nonnegative_int_iff) |
3e162c63371a
build bit operations on word on library theory on bit operations
haftmann
parents:
71949
diff
changeset
|
1548 |
|
71941 | 1549 |
lemma even_conv_AND: |
1550 |
\<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
|
1551 |
by (simp add: and_one_eq mod2_eq_if) |
71941 | 1552 |
|
70190 | 1553 |
lemma bin_last_conv_AND: |
1554 |
"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
|
1555 |
by (simp add: and_one_eq mod2_eq_if) |
70190 | 1556 |
|
1557 |
lemma bitval_bin_last: |
|
1558 |
"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
|
1559 |
by (simp add: and_one_eq mod2_eq_if) |
70190 | 1560 |
|
1561 |
lemma bin_sign_and: |
|
1562 |
"bin_sign (i AND j) = - (bin_sign i * bin_sign j)" |
|
1563 |
by(simp add: bin_sign_def) |
|
1564 |
||
1565 |
lemma int_not_neg_numeral: "NOT (- numeral n) = (Num.sub n num.One :: int)" |
|
1566 |
by(simp add: int_not_def) |
|
1567 |
||
1568 |
lemma int_neg_numeral_pOne_conv_not: "- numeral (n + num.One) = (NOT (numeral n) :: int)" |
|
1569 |
by(simp add: int_not_def) |
|
70169 | 1570 |
|
67120 | 1571 |
|
61799 | 1572 |
subsection \<open>Setting and clearing bits\<close> |
24364 | 1573 |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1574 |
lemma bin_last_conv_lsb: "bin_last = lsb" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1575 |
by(clarsimp simp add: lsb_int_def fun_eq_iff) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1576 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1577 |
lemma int_lsb_numeral [simp]: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1578 |
"lsb (0 :: int) = False" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1579 |
"lsb (1 :: int) = True" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1580 |
"lsb (Numeral1 :: int) = True" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1581 |
"lsb (- 1 :: int) = True" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1582 |
"lsb (- Numeral1 :: int) = True" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1583 |
"lsb (numeral (num.Bit0 w) :: int) = False" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1584 |
"lsb (numeral (num.Bit1 w) :: int) = True" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1585 |
"lsb (- numeral (num.Bit0 w) :: int) = False" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1586 |
"lsb (- numeral (num.Bit1 w) :: int) = True" |
71941 | 1587 |
by (simp_all add: lsb_int_def) |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1588 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1589 |
lemma int_set_bit_0 [simp]: fixes x :: int shows |
71986 | 1590 |
"set_bit x 0 b = of_bool b + 2 * (x div 2)" |
1591 |
by (auto simp add: set_bit_int_def intro: bin_rl_eqI) |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1592 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1593 |
lemma int_set_bit_Suc: fixes x :: int shows |
71986 | 1594 |
"set_bit x (Suc n) b = of_bool (odd x) + 2 * set_bit (x div 2) n b" |
1595 |
by (auto simp add: set_bit_int_def intro: bin_rl_eqI) |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1596 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1597 |
lemma bin_last_set_bit: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1598 |
"bin_last (set_bit x n b) = (if n > 0 then bin_last x else b)" |
71986 | 1599 |
by (cases n) (simp_all add: int_set_bit_Suc) |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1600 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1601 |
lemma bin_rest_set_bit: |
71986 | 1602 |
"bin_rest (set_bit x n b) = (if n > 0 then set_bit (x div 2) (n - 1) b else x div 2)" |
1603 |
by (cases n) (simp_all add: int_set_bit_Suc) |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1604 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1605 |
lemma int_set_bit_numeral: fixes x :: int shows |
71986 | 1606 |
"set_bit x (numeral w) b = of_bool (odd x) + 2 * set_bit (x div 2) (pred_numeral w) b" |
1607 |
by (simp add: set_bit_int_def) |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1608 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1609 |
lemmas int_set_bit_numerals [simp] = |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1610 |
int_set_bit_numeral[where x="numeral w'"] |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1611 |
int_set_bit_numeral[where x="- numeral w'"] |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1612 |
int_set_bit_numeral[where x="Numeral1"] |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1613 |
int_set_bit_numeral[where x="1"] |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1614 |
int_set_bit_numeral[where x="0"] |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1615 |
int_set_bit_Suc[where x="numeral w'"] |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1616 |
int_set_bit_Suc[where x="- numeral w'"] |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1617 |
int_set_bit_Suc[where x="Numeral1"] |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1618 |
int_set_bit_Suc[where x="1"] |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1619 |
int_set_bit_Suc[where x="0"] |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1620 |
for w' |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1621 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1622 |
lemma int_shiftl_BIT: fixes x :: int |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1623 |
shows int_shiftl0 [simp]: "x << 0 = x" |
71986 | 1624 |
and int_shiftl_Suc [simp]: "x << Suc n = 2 * (x << n)" |
1625 |
by (auto simp add: shiftl_int_def) |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1626 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1627 |
lemma int_0_shiftl [simp]: "0 << n = (0 :: int)" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1628 |
by(induct n) simp_all |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1629 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1630 |
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
|
1631 |
by(cases n)(simp_all) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1632 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1633 |
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
|
1634 |
by(cases n)(simp_all) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1635 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1636 |
lemma bin_nth_shiftl [simp]: "bin_nth (x << n) m \<longleftrightarrow> n \<le> m \<and> bin_nth x (m - n)" |
71986 | 1637 |
by (simp add: bit_push_bit_iff_int shiftl_eq_push_bit) |
1638 |
||
1639 |
lemma bin_last_shiftr: "odd (x >> n) \<longleftrightarrow> x !! n" for x :: int |
|
1640 |
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
|
1641 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1642 |
lemma bin_rest_shiftr [simp]: "bin_rest (x >> n) = x >> Suc n" |
71986 | 1643 |
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
|
1644 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1645 |
lemma bin_nth_shiftr [simp]: "bin_nth (x >> n) m = bin_nth x (n + m)" |
71986 | 1646 |
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
|
1647 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1648 |
lemma bin_nth_conv_AND: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1649 |
fixes x :: int shows |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1650 |
"bin_nth x n \<longleftrightarrow> x AND (1 << n) \<noteq> 0" |
71986 | 1651 |
by (simp add: bit_eq_iff) |
1652 |
(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
|
1653 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1654 |
lemma int_shiftl_numeral [simp]: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1655 |
"(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
|
1656 |
"(- numeral w :: int) << numeral w' = - numeral (num.Bit0 w) << pred_numeral w'" |
71986 | 1657 |
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
|
1658 |
(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
|
1659 |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
1660 |
lemma int_shiftl_One_numeral [simp]: |
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
1661 |
"(1 :: int) << numeral w = 2 << pred_numeral w" |
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
1662 |
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
|
1663 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1664 |
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
|
1665 |
by(induct n) simp_all |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1666 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1667 |
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
|
1668 |
by (metis not_le shiftl_ge_0) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1669 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1670 |
lemma int_shiftl_test_bit: "(n << i :: int) !! m \<longleftrightarrow> m \<ge> i \<and> n !! (m - i)" |
71986 | 1671 |
by simp |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1672 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1673 |
lemma int_0shiftr [simp]: "(0 :: int) >> x = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1674 |
by(simp add: shiftr_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1675 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1676 |
lemma int_minus1_shiftr [simp]: "(-1 :: int) >> x = -1" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1677 |
by(simp add: shiftr_int_def div_eq_minus1) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1678 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1679 |
lemma int_shiftr_ge_0 [simp]: fixes i :: int shows "i >> n \<ge> 0 \<longleftrightarrow> i \<ge> 0" |
71986 | 1680 |
by (simp add: shiftr_eq_drop_bit) |
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1681 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1682 |
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
|
1683 |
by (metis int_shiftr_ge_0 not_less) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1684 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1685 |
lemma int_shiftr_numeral [simp]: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1686 |
"(1 :: int) >> numeral w' = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1687 |
"(numeral num.One :: int) >> numeral w' = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1688 |
"(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
|
1689 |
"(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
|
1690 |
"(- 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
|
1691 |
"(- numeral (num.Bit1 w) :: int) >> numeral w' = - numeral (Num.inc w) >> pred_numeral w'" |
71986 | 1692 |
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
|
1693 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1694 |
lemma int_shiftr_numeral_Suc0 [simp]: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1695 |
"(1 :: int) >> Suc 0 = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1696 |
"(numeral num.One :: int) >> Suc 0 = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1697 |
"(numeral (num.Bit0 w) :: int) >> Suc 0 = numeral w" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1698 |
"(numeral (num.Bit1 w) :: int) >> Suc 0 = numeral w" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1699 |
"(- numeral (num.Bit0 w) :: int) >> Suc 0 = - numeral w" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1700 |
"(- numeral (num.Bit1 w) :: int) >> Suc 0 = - numeral (Num.inc w)" |
71986 | 1701 |
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
|
1702 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1703 |
lemma bin_nth_minus_p2: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1704 |
assumes sign: "bin_sign x = 0" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1705 |
and y: "y = 1 << n" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1706 |
and m: "m < n" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1707 |
and x: "x < y" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1708 |
shows "bin_nth (x - y) m = bin_nth x m" |
71986 | 1709 |
proof - |
1710 |
from sign y x have \<open>x \<ge> 0\<close> and \<open>y = 2 ^ n\<close> and \<open>x < 2 ^ n\<close> |
|
1711 |
by (simp_all add: bin_sign_def shiftl_eq_push_bit push_bit_eq_mult split: if_splits) |
|
1712 |
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> |
|
1713 |
proof (induction m arbitrary: x n) |
|
1714 |
case 0 |
|
1715 |
then show ?case |
|
1716 |
by simp |
|
1717 |
next |
|
1718 |
case (Suc m) |
|
1719 |
moreover define q where \<open>q = n - 1\<close> |
|
1720 |
ultimately have n: \<open>n = Suc q\<close> |
|
1721 |
by simp |
|
1722 |
have \<open>(x - 2 ^ Suc q) div 2 = x div 2 - 2 ^ q\<close> |
|
1723 |
by simp |
|
1724 |
moreover from Suc.IH [of \<open>x div 2\<close> q] Suc.prems |
|
1725 |
have \<open>bit (x div 2) m \<longleftrightarrow> bit (x div 2 - 2 ^ q) m\<close> |
|
1726 |
by (simp add: n) |
|
1727 |
ultimately show ?case |
|
1728 |
by (simp add: bit_Suc n) |
|
1729 |
qed |
|
1730 |
with \<open>y = 2 ^ n\<close> show ?thesis |
|
1731 |
by simp |
|
70183
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1732 |
qed |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1733 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1734 |
lemma bin_clr_conv_NAND: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1735 |
"bin_sc n False i = i AND NOT (1 << n)" |
71941 | 1736 |
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
|
1737 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1738 |
lemma bin_set_conv_OR: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1739 |
"bin_sc n True i = i OR (1 << n)" |
71941 | 1740 |
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
|
1741 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1742 |
lemma msb_conv_bin_sign: "msb x \<longleftrightarrow> bin_sign x = -1" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1743 |
by(simp add: bin_sign_def not_le msb_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1744 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1745 |
lemma msb_bin_rest [simp]: "msb (bin_rest x) = msb x" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1746 |
by(simp add: msb_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1747 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1748 |
lemma int_msb_and [simp]: "msb ((x :: int) AND y) \<longleftrightarrow> msb x \<and> msb y" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1749 |
by(simp add: msb_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1750 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1751 |
lemma int_msb_or [simp]: "msb ((x :: int) OR y) \<longleftrightarrow> msb x \<or> msb y" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1752 |
by(simp add: msb_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1753 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1754 |
lemma int_msb_xor [simp]: "msb ((x :: int) XOR y) \<longleftrightarrow> msb x \<noteq> msb y" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1755 |
by(simp add: msb_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1756 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1757 |
lemma int_msb_not [simp]: "msb (NOT (x :: int)) \<longleftrightarrow> \<not> msb x" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1758 |
by(simp add: msb_int_def not_less) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1759 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1760 |
lemma msb_shiftl [simp]: "msb ((x :: int) << n) \<longleftrightarrow> msb x" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1761 |
by(simp add: msb_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1762 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1763 |
lemma msb_shiftr [simp]: "msb ((x :: int) >> r) \<longleftrightarrow> msb x" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1764 |
by(simp add: msb_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1765 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1766 |
lemma msb_bin_sc [simp]: "msb (bin_sc n b x) \<longleftrightarrow> msb x" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1767 |
by(simp add: msb_conv_bin_sign) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1768 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1769 |
lemma msb_set_bit [simp]: "msb (set_bit (x :: int) n b) \<longleftrightarrow> msb x" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1770 |
by(simp add: msb_conv_bin_sign set_bit_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1771 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1772 |
lemma msb_0 [simp]: "msb (0 :: int) = False" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1773 |
by(simp add: msb_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1774 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1775 |
lemma msb_1 [simp]: "msb (1 :: int) = False" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1776 |
by(simp add: msb_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1777 |
|
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1778 |
lemma msb_numeral [simp]: |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1779 |
"msb (numeral n :: int) = False" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1780 |
"msb (- numeral n :: int) = True" |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1781 |
by(simp_all add: msb_int_def) |
3ea80c950023
incorporated various material from the AFP into the distribution
haftmann
parents:
70175
diff
changeset
|
1782 |
|
70190 | 1783 |
|
1784 |
subsection \<open>Semantic interpretation of \<^typ>\<open>bool list\<close> as \<^typ>\<open>int\<close>\<close> |
|
1785 |
||
1786 |
lemma bin_bl_bin': "bl_to_bin (bin_to_bl_aux n w bs) = bl_to_bin_aux bs (bintrunc n w)" |
|
71986 | 1787 |
by (induct n arbitrary: w bs) (auto simp: bl_to_bin_def take_bit_Suc ac_simps mod_2_eq_odd) |
70190 | 1788 |
|
1789 |
lemma bin_bl_bin [simp]: "bl_to_bin (bin_to_bl n w) = bintrunc n w" |
|
1790 |
by (auto simp: bin_to_bl_def bin_bl_bin') |
|
1791 |
||
1792 |
lemma bl_to_bin_rep_F: "bl_to_bin (replicate n False @ bl) = bl_to_bin bl" |
|
1793 |
by (simp add: bin_to_bl_zero_aux [symmetric] bin_bl_bin') (simp add: bl_to_bin_def) |
|
1794 |
||
1795 |
lemma bin_to_bl_trunc [simp]: "n \<le> m \<Longrightarrow> bin_to_bl n (bintrunc m w) = bin_to_bl n w" |
|
1796 |
by (auto intro: bl_to_bin_inj) |
|
1797 |
||
1798 |
lemma bin_to_bl_aux_bintr: |
|
1799 |
"bin_to_bl_aux n (bintrunc m bin) bl = |
|
1800 |
replicate (n - m) False @ bin_to_bl_aux (min n m) bin bl" |
|
1801 |
apply (induct n arbitrary: m bin bl) |
|
1802 |
apply clarsimp |
|
1803 |
apply clarsimp |
|
1804 |
apply (case_tac "m") |
|
1805 |
apply (clarsimp simp: bin_to_bl_zero_aux) |
|
1806 |
apply (erule thin_rl) |
|
1807 |
apply (induct_tac n) |
|
71947 | 1808 |
apply (auto simp add: take_bit_Suc) |
70190 | 1809 |
done |
1810 |
||
1811 |
lemma bin_to_bl_bintr: |
|
1812 |
"bin_to_bl n (bintrunc m bin) = replicate (n - m) False @ bin_to_bl (min n m) bin" |
|
1813 |
unfolding bin_to_bl_def by (rule bin_to_bl_aux_bintr) |
|
1814 |
||
1815 |
lemma bl_to_bin_rep_False: "bl_to_bin (replicate n False) = 0" |
|
1816 |
by (induct n) auto |
|
1817 |
||
1818 |
lemma len_bin_to_bl_aux: "length (bin_to_bl_aux n w bs) = n + length bs" |
|
1819 |
by (fact size_bin_to_bl_aux) |
|
1820 |
||
1821 |
lemma len_bin_to_bl: "length (bin_to_bl n w) = n" |
|
1822 |
by (fact size_bin_to_bl) (* FIXME: duplicate *) |
|
1823 |
||
1824 |
lemma sign_bl_bin': "bin_sign (bl_to_bin_aux bs w) = bin_sign w" |
|
71986 | 1825 |
by (induction bs arbitrary: w) (simp_all add: bin_sign_def) |
70190 | 1826 |
|
1827 |
lemma sign_bl_bin: "bin_sign (bl_to_bin bs) = 0" |
|
1828 |
by (simp add: bl_to_bin_def sign_bl_bin') |
|
1829 |
||
1830 |
lemma bl_sbin_sign_aux: "hd (bin_to_bl_aux (Suc n) w bs) = (bin_sign (sbintrunc n w) = -1)" |
|
71986 | 1831 |
by (induction n arbitrary: w bs) (simp_all add: bin_sign_def) |
70190 | 1832 |
|
1833 |
lemma bl_sbin_sign: "hd (bin_to_bl (Suc n) w) = (bin_sign (sbintrunc n w) = -1)" |
|
1834 |
unfolding bin_to_bl_def by (rule bl_sbin_sign_aux) |
|
1835 |
||
1836 |
lemma bin_nth_of_bl_aux: |
|
1837 |
"bin_nth (bl_to_bin_aux bl w) n = |
|
1838 |
(n < size bl \<and> rev bl ! n \<or> n \<ge> length bl \<and> bin_nth w (n - size bl))" |
|
71986 | 1839 |
apply (induction bl arbitrary: w) |
1840 |
apply simp_all |
|
1841 |
apply safe |
|
1842 |
apply (simp_all add: not_le nth_append bit_double_iff even_bit_succ_iff split: if_splits) |
|
70190 | 1843 |
done |
1844 |
||
1845 |
lemma bin_nth_of_bl: "bin_nth (bl_to_bin bl) n = (n < length bl \<and> rev bl ! n)" |
|
1846 |
by (simp add: bl_to_bin_def bin_nth_of_bl_aux) |
|
1847 |
||
1848 |
lemma bin_nth_bl: "n < m \<Longrightarrow> bin_nth w n = nth (rev (bin_to_bl m w)) n" |
|
1849 |
apply (induct n arbitrary: m w) |
|
1850 |
apply clarsimp |
|
1851 |
apply (case_tac m, clarsimp) |
|
1852 |
apply (clarsimp simp: bin_to_bl_def) |
|
1853 |
apply (simp add: bin_to_bl_aux_alt) |
|
1854 |
apply (case_tac m, clarsimp) |
|
1855 |
apply (clarsimp simp: bin_to_bl_def) |
|
71949 | 1856 |
apply (simp add: bin_to_bl_aux_alt bit_Suc) |
70190 | 1857 |
done |
1858 |
||
1859 |
lemma nth_bin_to_bl_aux: |
|
1860 |
"n < m + length bl \<Longrightarrow> (bin_to_bl_aux m w bl) ! n = |
|
1861 |
(if n < m then bin_nth w (m - 1 - n) else bl ! (n - m))" |
|
71986 | 1862 |
apply (induction bl arbitrary: w) |
1863 |
apply simp_all |
|
1864 |
apply (metis add.right_neutral bin_nth_bl bin_to_bl_def diff_Suc_less less_Suc_eq_0_disj less_imp_Suc_add list.size(3) nth_rev_alt size_bin_to_bl_aux) |
|
1865 |
apply (metis One_nat_def Suc_pred add_diff_cancel_left' add_diff_cancel_right' bin_to_bl_aux_alt bin_to_bl_def cancel_comm_monoid_add_class.diff_cancel diff_Suc_Suc diff_is_0_eq diff_zero le_add_diff_inverse le_eq_less_or_eq less_Suc_eq_0_disj less_antisym less_imp_Suc_add list.size(3) nat_less_le nth_append order_refl size_bin_to_bl_aux) |
|
70190 | 1866 |
done |
1867 |
||
1868 |
lemma nth_bin_to_bl: "n < m \<Longrightarrow> (bin_to_bl m w) ! n = bin_nth w (m - Suc n)" |
|
1869 |
by (simp add: bin_to_bl_def nth_bin_to_bl_aux) |
|
1870 |
||
1871 |
lemma bl_to_bin_lt2p_aux: "bl_to_bin_aux bs w < (w + 1) * (2 ^ length bs)" |
|
71986 | 1872 |
proof (induction bs arbitrary: w) |
1873 |
case Nil |
|
1874 |
then show ?case |
|
1875 |
by simp |
|
1876 |
next |
|
1877 |
case (Cons b bs) |
|
1878 |
from Cons.IH [of \<open>1 + 2 * w\<close>] Cons.IH [of \<open>2 * w\<close>] |
|
1879 |
show ?case |
|
1880 |
apply (auto simp add: algebra_simps) |
|
1881 |
apply (subst mult_2 [of \<open>2 ^ length bs\<close>]) |
|
1882 |
apply (simp only: add.assoc) |
|
1883 |
apply (rule pos_add_strict) |
|
1884 |
apply simp_all |
|
1885 |
done |
|
1886 |
qed |
|
70190 | 1887 |
|
1888 |
lemma bl_to_bin_lt2p_drop: "bl_to_bin bs < 2 ^ length (dropWhile Not bs)" |
|
1889 |
proof (induct bs) |
|
1890 |
case Nil |
|
1891 |
then show ?case by simp |
|
1892 |
next |
|
1893 |
case (Cons b bs) |
|
1894 |
with bl_to_bin_lt2p_aux[where w=1] show ?case |
|
1895 |
by (simp add: bl_to_bin_def) |
|
1896 |
qed |
|
1897 |
||
1898 |
lemma bl_to_bin_lt2p: "bl_to_bin bs < 2 ^ length bs" |
|
1899 |
by (metis bin_bl_bin bintr_lt2p bl_bin_bl) |
|
1900 |
||
1901 |
lemma bl_to_bin_ge2p_aux: "bl_to_bin_aux bs w \<ge> w * (2 ^ length bs)" |
|
71986 | 1902 |
proof (induction bs arbitrary: w) |
1903 |
case Nil |
|
1904 |
then show ?case |
|
1905 |
by simp |
|
1906 |
next |
|
1907 |
case (Cons b bs) |
|
1908 |
from Cons.IH [of \<open>1 + 2 * w\<close>] Cons.IH [of \<open>2 * w\<close>] |
|
1909 |
show ?case |
|
1910 |
apply (auto simp add: algebra_simps) |
|
1911 |
apply (rule add_le_imp_le_left [of \<open>2 ^ length bs\<close>]) |
|
1912 |
apply (rule add_increasing) |
|
1913 |
apply simp_all |
|
1914 |
done |
|
1915 |
qed |
|
70190 | 1916 |
|
1917 |
lemma bl_to_bin_ge0: "bl_to_bin bs \<ge> 0" |
|
1918 |
apply (unfold bl_to_bin_def) |
|
1919 |
apply (rule xtrans(4)) |
|
1920 |
apply (rule bl_to_bin_ge2p_aux) |
|
1921 |
apply simp |
|
1922 |
done |
|
1923 |
||
1924 |
lemma butlast_rest_bin: "butlast (bin_to_bl n w) = bin_to_bl (n - 1) (bin_rest w)" |
|
1925 |
apply (unfold bin_to_bl_def) |
|
1926 |
apply (cases n, clarsimp) |
|
1927 |
apply clarsimp |
|
1928 |
apply (auto simp add: bin_to_bl_aux_alt) |
|
1929 |
done |
|
1930 |
||
1931 |
lemma butlast_bin_rest: "butlast bl = bin_to_bl (length bl - Suc 0) (bin_rest (bl_to_bin bl))" |
|
1932 |
using butlast_rest_bin [where w="bl_to_bin bl" and n="length bl"] by simp |
|
1933 |
||
1934 |
lemma butlast_rest_bl2bin_aux: |
|
1935 |
"bl \<noteq> [] \<Longrightarrow> bl_to_bin_aux (butlast bl) w = bin_rest (bl_to_bin_aux bl w)" |
|
1936 |
by (induct bl arbitrary: w) auto |
|
1937 |
||
1938 |
lemma butlast_rest_bl2bin: "bl_to_bin (butlast bl) = bin_rest (bl_to_bin bl)" |
|
1939 |
by (cases bl) (auto simp: bl_to_bin_def butlast_rest_bl2bin_aux) |
|
1940 |
||
1941 |
lemma trunc_bl2bin_aux: |
|
1942 |
"bintrunc m (bl_to_bin_aux bl w) = |
|
1943 |
bl_to_bin_aux (drop (length bl - m) bl) (bintrunc (m - length bl) w)" |
|
1944 |
proof (induct bl arbitrary: w) |
|
1945 |
case Nil |
|
1946 |
show ?case by simp |
|
1947 |
next |
|
1948 |
case (Cons b bl) |
|
1949 |
show ?case |
|
1950 |
proof (cases "m - length bl") |
|
1951 |
case 0 |
|
1952 |
then have "Suc (length bl) - m = Suc (length bl - m)" by simp |
|
1953 |
with Cons show ?thesis by simp |
|
1954 |
next |
|
1955 |
case (Suc n) |
|
1956 |
then have "m - Suc (length bl) = n" by simp |
|
71986 | 1957 |
with Cons Suc show ?thesis by (simp add: take_bit_Suc ac_simps) |
70190 | 1958 |
qed |
1959 |
qed |
|
1960 |
||
1961 |
lemma trunc_bl2bin: "bintrunc m (bl_to_bin bl) = bl_to_bin (drop (length bl - m) bl)" |
|
1962 |
by (simp add: bl_to_bin_def trunc_bl2bin_aux) |
|
1963 |
||
1964 |
lemma trunc_bl2bin_len [simp]: "bintrunc (length bl) (bl_to_bin bl) = bl_to_bin bl" |
|
1965 |
by (simp add: trunc_bl2bin) |
|
1966 |
||
1967 |
lemma bl2bin_drop: "bl_to_bin (drop k bl) = bintrunc (length bl - k) (bl_to_bin bl)" |
|
1968 |
apply (rule trans) |
|
1969 |
prefer 2 |
|
1970 |
apply (rule trunc_bl2bin [symmetric]) |
|
1971 |
apply (cases "k \<le> length bl") |
|
1972 |
apply auto |
|
1973 |
done |
|
1974 |
||
1975 |
lemma take_rest_power_bin: "m \<le> n \<Longrightarrow> take m (bin_to_bl n w) = bin_to_bl m ((bin_rest ^^ (n - m)) w)" |
|
1976 |
apply (rule nth_equalityI) |
|
1977 |
apply simp |
|
1978 |
apply (clarsimp simp add: nth_bin_to_bl nth_rest_power_bin) |
|
1979 |
done |
|
1980 |
||
1981 |
lemma last_bin_last': "size xs > 0 \<Longrightarrow> last xs \<longleftrightarrow> bin_last (bl_to_bin_aux xs w)" |
|
1982 |
by (induct xs arbitrary: w) auto |
|
1983 |
||
1984 |
lemma last_bin_last: "size xs > 0 \<Longrightarrow> last xs \<longleftrightarrow> bin_last (bl_to_bin xs)" |
|
1985 |
unfolding bl_to_bin_def by (erule last_bin_last') |
|
1986 |
||
1987 |
lemma bin_last_last: "bin_last w \<longleftrightarrow> last (bin_to_bl (Suc n) w)" |
|
1988 |
by (simp add: bin_to_bl_def) (auto simp: bin_to_bl_aux_alt) |
|
1989 |
||
1990 |
lemma drop_bin2bl_aux: |
|
1991 |
"drop m (bin_to_bl_aux n bin bs) = |
|
1992 |
bin_to_bl_aux (n - m) bin (drop (m - n) bs)" |
|
71986 | 1993 |
apply (induction n arbitrary: m bin bs) |
1994 |
apply auto |
|
1995 |
apply (case_tac "m \<le> n") |
|
1996 |
apply (auto simp add: not_le Suc_diff_le) |
|
1997 |
apply (case_tac "m - n") |
|
1998 |
apply auto |
|
1999 |
apply (use Suc_diff_Suc in fastforce) |
|
70190 | 2000 |
done |
2001 |
||
2002 |
lemma drop_bin2bl: "drop m (bin_to_bl n bin) = bin_to_bl (n - m) bin" |
|
2003 |
by (simp add: bin_to_bl_def drop_bin2bl_aux) |
|
2004 |
||
2005 |
lemma take_bin2bl_lem1: "take m (bin_to_bl_aux m w bs) = bin_to_bl m w" |
|
2006 |
apply (induct m arbitrary: w bs) |
|
2007 |
apply clarsimp |
|
2008 |
apply clarsimp |
|
2009 |
apply (simp add: bin_to_bl_aux_alt) |
|
2010 |
apply (simp add: bin_to_bl_def) |
|
2011 |
apply (simp add: bin_to_bl_aux_alt) |
|
2012 |
done |
|
2013 |
||
2014 |
lemma take_bin2bl_lem: "take m (bin_to_bl_aux (m + n) w bs) = take m (bin_to_bl (m + n) w)" |
|
2015 |
by (induct n arbitrary: w bs) (simp_all (no_asm) add: bin_to_bl_def take_bin2bl_lem1, simp) |
|
2016 |
||
2017 |
lemma bin_split_take: "bin_split n c = (a, b) \<Longrightarrow> bin_to_bl m a = take m (bin_to_bl (m + n) c)" |
|
2018 |
apply (induct n arbitrary: b c) |
|
2019 |
apply clarsimp |
|
2020 |
apply (clarsimp simp: Let_def split: prod.split_asm) |
|
2021 |
apply (simp add: bin_to_bl_def) |
|
71945
4b1264316270
replaced operation with weak abstraction by input abbreviation
haftmann
parents:
71944
diff
changeset
|
2022 |
apply (simp add: take_bin2bl_lem drop_bit_Suc) |
70190 | 2023 |
done |
2024 |
||
71944 | 2025 |
lemma bin_to_bl_drop_bit: |
2026 |
"k = m + n \<Longrightarrow> bin_to_bl m (drop_bit n c) = take m (bin_to_bl k c)" |
|
2027 |
using bin_split_take by simp |
|
2028 |
||
70190 | 2029 |
lemma bin_split_take1: |
2030 |
"k = m + n \<Longrightarrow> bin_split n c = (a, b) \<Longrightarrow> bin_to_bl m a = take m (bin_to_bl k c)" |
|
71944 | 2031 |
using bin_split_take by simp |
70190 | 2032 |
|
2033 |
lemma takefill_bintrunc: "takefill False n bl = rev (bin_to_bl n (bl_to_bin (rev bl)))" |
|
2034 |
apply (rule nth_equalityI) |
|
2035 |
apply simp |
|
2036 |
apply (clarsimp simp: nth_takefill nth_rev nth_bin_to_bl bin_nth_of_bl) |
|
2037 |
done |
|
2038 |
||
2039 |
lemma bl_bin_bl_rtf: "bin_to_bl n (bl_to_bin bl) = rev (takefill False n (rev bl))" |
|
2040 |
by (simp add: takefill_bintrunc) |
|
2041 |
||
2042 |
lemma bl_bin_bl_rep_drop: |
|
2043 |
"bin_to_bl n (bl_to_bin bl) = |
|
2044 |
replicate (n - length bl) False @ drop (length bl - n) bl" |
|
2045 |
by (simp add: bl_bin_bl_rtf takefill_alt rev_take) |
|
2046 |
||
2047 |
lemma bl_to_bin_aux_cat: |
|
71986 | 2048 |
"bl_to_bin_aux bs (bin_cat w nv v) = |
70190 | 2049 |
bin_cat w (nv + length bs) (bl_to_bin_aux bs v)" |
71986 | 2050 |
by (rule bit_eqI) |
2051 |
(auto simp add: bin_nth_of_bl_aux bin_nth_cat algebra_simps) |
|
70190 | 2052 |
|
2053 |
lemma bin_to_bl_aux_cat: |
|
2054 |
"\<And>w bs. bin_to_bl_aux (nv + nw) (bin_cat v nw w) bs = |
|
2055 |
bin_to_bl_aux nv v (bin_to_bl_aux nw w bs)" |
|
2056 |
by (induct nw) auto |
|
2057 |
||
2058 |
lemma bl_to_bin_aux_alt: "bl_to_bin_aux bs w = bin_cat w (length bs) (bl_to_bin bs)" |
|
2059 |
using bl_to_bin_aux_cat [where nv = "0" and v = "0"] |
|
2060 |
by (simp add: bl_to_bin_def [symmetric]) |
|
2061 |
||
2062 |
lemma bin_to_bl_cat: |
|
2063 |
"bin_to_bl (nv + nw) (bin_cat v nw w) = |
|
2064 |
bin_to_bl_aux nv v (bin_to_bl nw w)" |
|
2065 |
by (simp add: bin_to_bl_def bin_to_bl_aux_cat) |
|
2066 |
||
2067 |
lemmas bl_to_bin_aux_app_cat = |
|
2068 |
trans [OF bl_to_bin_aux_append bl_to_bin_aux_alt] |
|
2069 |
||
2070 |
lemmas bin_to_bl_aux_cat_app = |
|
2071 |
trans [OF bin_to_bl_aux_cat bin_to_bl_aux_alt] |
|
2072 |
||
2073 |
lemma bl_to_bin_app_cat: |
|
2074 |
"bl_to_bin (bsa @ bs) = bin_cat (bl_to_bin bsa) (length bs) (bl_to_bin bs)" |
|
2075 |
by (simp only: bl_to_bin_aux_app_cat bl_to_bin_def) |
|
2076 |
||
2077 |
lemma bin_to_bl_cat_app: |
|
2078 |
"bin_to_bl (n + nw) (bin_cat w nw wa) = bin_to_bl n w @ bin_to_bl nw wa" |
|
2079 |
by (simp only: bin_to_bl_def bin_to_bl_aux_cat_app) |
|
2080 |
||
2081 |
text \<open>\<open>bl_to_bin_app_cat_alt\<close> and \<open>bl_to_bin_app_cat\<close> are easily interderivable.\<close> |
|
2082 |
lemma bl_to_bin_app_cat_alt: "bin_cat (bl_to_bin cs) n w = bl_to_bin (cs @ bin_to_bl n w)" |
|
2083 |
by (simp add: bl_to_bin_app_cat) |
|
2084 |
||
2085 |
lemma mask_lem: "(bl_to_bin (True # replicate n False)) = bl_to_bin (replicate n True) + 1" |
|
2086 |
apply (unfold bl_to_bin_def) |
|
2087 |
apply (induct n) |
|
2088 |
apply simp |
|
2089 |
apply (simp only: Suc_eq_plus1 replicate_add append_Cons [symmetric] bl_to_bin_aux_append) |
|
71986 | 2090 |
apply simp |
2091 |
done |
|
2092 |
||
2093 |
lemma bin_exhaust: |
|
2094 |
"(\<And>x b. bin = of_bool b + 2 * x \<Longrightarrow> Q) \<Longrightarrow> Q" for bin :: int |
|
2095 |
apply (cases \<open>even bin\<close>) |
|
2096 |
apply (auto elim!: evenE oddE) |
|
2097 |
apply fastforce |
|
2098 |
apply fastforce |
|
70190 | 2099 |
done |
2100 |
||
2101 |
primrec rbl_succ :: "bool list \<Rightarrow> bool list" |
|
2102 |
where |
|
2103 |
Nil: "rbl_succ Nil = Nil" |
|
2104 |
| Cons: "rbl_succ (x # xs) = (if x then False # rbl_succ xs else True # xs)" |
|
2105 |
||
2106 |
primrec rbl_pred :: "bool list \<Rightarrow> bool list" |
|
2107 |
where |
|
2108 |
Nil: "rbl_pred Nil = Nil" |
|
2109 |
| Cons: "rbl_pred (x # xs) = (if x then False # xs else True # rbl_pred xs)" |
|
2110 |
||
2111 |
primrec rbl_add :: "bool list \<Rightarrow> bool list \<Rightarrow> bool list" |
|
2112 |
where \<comment> \<open>result is length of first arg, second arg may be longer\<close> |
|
2113 |
Nil: "rbl_add Nil x = Nil" |
|
2114 |
| Cons: "rbl_add (y # ys) x = |
|
2115 |
(let ws = rbl_add ys (tl x) |
|
2116 |
in (y \<noteq> hd x) # (if hd x \<and> y then rbl_succ ws else ws))" |
|
2117 |
||
2118 |
primrec rbl_mult :: "bool list \<Rightarrow> bool list \<Rightarrow> bool list" |
|
2119 |
where \<comment> \<open>result is length of first arg, second arg may be longer\<close> |
|
2120 |
Nil: "rbl_mult Nil x = Nil" |
|
2121 |
| Cons: "rbl_mult (y # ys) x = |
|
2122 |
(let ws = False # rbl_mult ys x |
|
2123 |
in if y then rbl_add ws x else ws)" |
|
2124 |
||
2125 |
lemma size_rbl_pred: "length (rbl_pred bl) = length bl" |
|
2126 |
by (induct bl) auto |
|
2127 |
||
2128 |
lemma size_rbl_succ: "length (rbl_succ bl) = length bl" |
|
2129 |
by (induct bl) auto |
|
2130 |
||
2131 |
lemma size_rbl_add: "length (rbl_add bl cl) = length bl" |
|
2132 |
by (induct bl arbitrary: cl) (auto simp: Let_def size_rbl_succ) |
|
2133 |
||
2134 |
lemma size_rbl_mult: "length (rbl_mult bl cl) = length bl" |
|
2135 |
by (induct bl arbitrary: cl) (auto simp add: Let_def size_rbl_add) |
|
2136 |
||
2137 |
lemmas rbl_sizes [simp] = |
|
2138 |
size_rbl_pred size_rbl_succ size_rbl_add size_rbl_mult |
|
2139 |
||
2140 |
lemmas rbl_Nils = |
|
2141 |
rbl_pred.Nil rbl_succ.Nil rbl_add.Nil rbl_mult.Nil |
|
2142 |
||
2143 |
lemma rbl_add_app2: "length blb \<ge> length bla \<Longrightarrow> rbl_add bla (blb @ blc) = rbl_add bla blb" |
|
2144 |
apply (induct bla arbitrary: blb) |
|
2145 |
apply simp |
|
2146 |
apply clarsimp |
|
2147 |
apply (case_tac blb, clarsimp) |
|
2148 |
apply (clarsimp simp: Let_def) |
|
2149 |
done |
|
2150 |
||
2151 |
lemma rbl_add_take2: |
|
2152 |
"length blb \<ge> length bla \<Longrightarrow> rbl_add bla (take (length bla) blb) = rbl_add bla blb" |
|
2153 |
apply (induct bla arbitrary: blb) |
|
2154 |
apply simp |
|
2155 |
apply clarsimp |
|
2156 |
apply (case_tac blb, clarsimp) |
|
2157 |
apply (clarsimp simp: Let_def) |
|
2158 |
done |
|
2159 |
||
2160 |
lemma rbl_mult_app2: "length blb \<ge> length bla \<Longrightarrow> rbl_mult bla (blb @ blc) = rbl_mult bla blb" |
|
2161 |
apply (induct bla arbitrary: blb) |
|
2162 |
apply simp |
|
2163 |
apply clarsimp |
|
2164 |
apply (case_tac blb, clarsimp) |
|
2165 |
apply (clarsimp simp: Let_def rbl_add_app2) |
|
2166 |
done |
|
2167 |
||
2168 |
lemma rbl_mult_take2: |
|
2169 |
"length blb \<ge> length bla \<Longrightarrow> rbl_mult bla (take (length bla) blb) = rbl_mult bla blb" |
|
2170 |
apply (rule trans) |
|
2171 |
apply (rule rbl_mult_app2 [symmetric]) |
|
2172 |
apply simp |
|
2173 |
apply (rule_tac f = "rbl_mult bla" in arg_cong) |
|
2174 |
apply (rule append_take_drop_id) |
|
2175 |
done |
|
2176 |
||
2177 |
lemma rbl_add_split: |
|
2178 |
"P (rbl_add (y # ys) (x # xs)) = |
|
2179 |
(\<forall>ws. length ws = length ys \<longrightarrow> ws = rbl_add ys xs \<longrightarrow> |
|
2180 |
(y \<longrightarrow> ((x \<longrightarrow> P (False # rbl_succ ws)) \<and> (\<not> x \<longrightarrow> P (True # ws)))) \<and> |
|
2181 |
(\<not> y \<longrightarrow> P (x # ws)))" |
|
2182 |
by (cases y) (auto simp: Let_def) |
|
2183 |
||
2184 |
lemma rbl_mult_split: |
|
2185 |
"P (rbl_mult (y # ys) xs) = |
|
2186 |
(\<forall>ws. length ws = Suc (length ys) \<longrightarrow> ws = False # rbl_mult ys xs \<longrightarrow> |
|
2187 |
(y \<longrightarrow> P (rbl_add ws xs)) \<and> (\<not> y \<longrightarrow> P ws))" |
|
2188 |
by (auto simp: Let_def) |
|
2189 |
||
2190 |
lemma rbl_pred: "rbl_pred (rev (bin_to_bl n bin)) = rev (bin_to_bl n (bin - 1))" |
|
71986 | 2191 |
proof (unfold bin_to_bl_def, induction n arbitrary: bin) |
2192 |
case 0 |
|
2193 |
then show ?case |
|
2194 |
by simp |
|
2195 |
next |
|
2196 |
case (Suc n) |
|
2197 |
obtain b k where \<open>bin = of_bool b + 2 * k\<close> |
|
2198 |
using bin_exhaust by blast |
|
2199 |
moreover have \<open>(2 * k - 1) div 2 = k - 1\<close> |
|
2200 |
using even_succ_div_2 [of \<open>2 * (k - 1)\<close>] |
|
2201 |
by simp |
|
2202 |
ultimately show ?case |
|
2203 |
using Suc [of \<open>bin div 2\<close>] |
|
2204 |
by simp (simp add: bin_to_bl_aux_alt) |
|
2205 |
qed |
|
70190 | 2206 |
|
2207 |
lemma rbl_succ: "rbl_succ (rev (bin_to_bl n bin)) = rev (bin_to_bl n (bin + 1))" |
|
2208 |
apply (unfold bin_to_bl_def) |
|
71986 | 2209 |
apply (induction n arbitrary: bin) |
2210 |
apply simp_all |
|
70190 | 2211 |
apply (case_tac bin rule: bin_exhaust) |
71986 | 2212 |
apply simp |
2213 |
apply (simp add: bin_to_bl_aux_alt ac_simps) |
|
70190 | 2214 |
done |
2215 |
||
2216 |
lemma rbl_add: |
|
2217 |
"\<And>bina binb. rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) = |
|
2218 |
rev (bin_to_bl n (bina + binb))" |
|
2219 |
apply (unfold bin_to_bl_def) |
|
2220 |
apply (induct n) |
|
2221 |
apply simp |
|
2222 |
apply clarsimp |
|
2223 |
apply (case_tac bina rule: bin_exhaust) |
|
2224 |
apply (case_tac binb rule: bin_exhaust) |
|
2225 |
apply (case_tac b) |
|
2226 |
apply (case_tac [!] "ba") |
|
2227 |
apply (auto simp: rbl_succ bin_to_bl_aux_alt Let_def ac_simps) |
|
2228 |
done |
|
2229 |
||
2230 |
lemma rbl_add_long: |
|
2231 |
"m \<ge> n \<Longrightarrow> rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) = |
|
2232 |
rev (bin_to_bl n (bina + binb))" |
|
2233 |
apply (rule box_equals [OF _ rbl_add_take2 rbl_add]) |
|
2234 |
apply (rule_tac f = "rbl_add (rev (bin_to_bl n bina))" in arg_cong) |
|
2235 |
apply (rule rev_swap [THEN iffD1]) |
|
2236 |
apply (simp add: rev_take drop_bin2bl) |
|
2237 |
apply simp |
|
2238 |
done |
|
2239 |
||
2240 |
lemma rbl_mult_gt1: |
|
2241 |
"m \<ge> length bl \<Longrightarrow> |
|
2242 |
rbl_mult bl (rev (bin_to_bl m binb)) = |
|
2243 |
rbl_mult bl (rev (bin_to_bl (length bl) binb))" |
|
2244 |
apply (rule trans) |
|
2245 |
apply (rule rbl_mult_take2 [symmetric]) |
|
2246 |
apply simp_all |
|
2247 |
apply (rule_tac f = "rbl_mult bl" in arg_cong) |
|
2248 |
apply (rule rev_swap [THEN iffD1]) |
|
2249 |
apply (simp add: rev_take drop_bin2bl) |
|
2250 |
done |
|
2251 |
||
2252 |
lemma rbl_mult_gt: |
|
2253 |
"m > n \<Longrightarrow> |
|
2254 |
rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) = |
|
2255 |
rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb))" |
|
2256 |
by (auto intro: trans [OF rbl_mult_gt1]) |
|
2257 |
||
2258 |
lemmas rbl_mult_Suc = lessI [THEN rbl_mult_gt] |
|
2259 |
||
71986 | 2260 |
lemma rbbl_Cons: "b # rev (bin_to_bl n x) = rev (bin_to_bl (Suc n) (of_bool b + 2 * x))" |
70190 | 2261 |
by (simp add: bin_to_bl_def) (simp add: bin_to_bl_aux_alt) |
2262 |
||
2263 |
lemma rbl_mult: |
|
2264 |
"rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) = |
|
2265 |
rev (bin_to_bl n (bina * binb))" |
|
2266 |
apply (induct n arbitrary: bina binb) |
|
71986 | 2267 |
apply simp_all |
70190 | 2268 |
apply (unfold bin_to_bl_def) |
2269 |
apply clarsimp |
|
2270 |
apply (case_tac bina rule: bin_exhaust) |
|
2271 |
apply (case_tac binb rule: bin_exhaust) |
|
71986 | 2272 |
apply simp |
2273 |
apply (simp add: bin_to_bl_aux_alt) |
|
2274 |
apply (simp add: rbbl_Cons rbl_mult_Suc rbl_add algebra_simps) |
|
70190 | 2275 |
done |
2276 |
||
2277 |
lemma sclem: "size (concat (map (bin_to_bl n) xs)) = length xs * n" |
|
2278 |
by (induct xs) auto |
|
2279 |
||
2280 |
lemma bin_cat_foldl_lem: |
|
2281 |
"foldl (\<lambda>u. bin_cat u n) x xs = |
|
2282 |
bin_cat x (size xs * n) (foldl (\<lambda>u. bin_cat u n) y xs)" |
|
2283 |
apply (induct xs arbitrary: x) |
|
2284 |
apply simp |
|
2285 |
apply (simp (no_asm)) |
|
2286 |
apply (frule asm_rl) |
|
2287 |
apply (drule meta_spec) |
|
2288 |
apply (erule trans) |
|
2289 |
apply (drule_tac x = "bin_cat y n a" in meta_spec) |
|
2290 |
apply (simp add: bin_cat_assoc_sym min.absorb2) |
|
2291 |
done |
|
2292 |
||
2293 |
lemma bin_rcat_bl: "bin_rcat n wl = bl_to_bin (concat (map (bin_to_bl n) wl))" |
|
2294 |
apply (unfold bin_rcat_def) |
|
2295 |
apply (rule sym) |
|
2296 |
apply (induct wl) |
|
2297 |
apply (auto simp add: bl_to_bin_append) |
|
2298 |
apply (simp add: bl_to_bin_aux_alt sclem) |
|
2299 |
apply (simp add: bin_cat_foldl_lem [symmetric]) |
|
2300 |
done |
|
2301 |
||
2302 |
lemma bin_last_bl_to_bin: "bin_last (bl_to_bin bs) \<longleftrightarrow> bs \<noteq> [] \<and> last bs" |
|
2303 |
by(cases "bs = []")(auto simp add: bl_to_bin_def last_bin_last'[where w=0]) |
|
2304 |
||
2305 |
lemma bin_rest_bl_to_bin: "bin_rest (bl_to_bin bs) = bl_to_bin (butlast bs)" |
|
2306 |
by(cases "bs = []")(simp_all add: bl_to_bin_def butlast_rest_bl2bin_aux) |
|
2307 |
||
2308 |
lemma bl_xor_aux_bin: |
|
2309 |
"map2 (\<lambda>x y. x \<noteq> y) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = |
|
2310 |
bin_to_bl_aux n (v XOR w) (map2 (\<lambda>x y. x \<noteq> y) bs cs)" |
|
71986 | 2311 |
apply (induction n arbitrary: v w bs cs) |
2312 |
apply auto |
|
70190 | 2313 |
apply (case_tac v rule: bin_exhaust) |
2314 |
apply (case_tac w rule: bin_exhaust) |
|
2315 |
apply clarsimp |
|
2316 |
done |
|
2317 |
||
2318 |
lemma bl_or_aux_bin: |
|
2319 |
"map2 (\<or>) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = |
|
2320 |
bin_to_bl_aux n (v OR w) (map2 (\<or>) bs cs)" |
|
71986 | 2321 |
by (induct n arbitrary: v w bs cs) simp_all |
70190 | 2322 |
|
2323 |
lemma bl_and_aux_bin: |
|
2324 |
"map2 (\<and>) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = |
|
2325 |
bin_to_bl_aux n (v AND w) (map2 (\<and>) bs cs)" |
|
71986 | 2326 |
by (induction n arbitrary: v w bs cs) simp_all |
70190 | 2327 |
|
2328 |
lemma bl_not_aux_bin: "map Not (bin_to_bl_aux n w cs) = bin_to_bl_aux n (NOT w) (map Not cs)" |
|
2329 |
by (induct n arbitrary: w cs) auto |
|
2330 |
||
2331 |
lemma bl_not_bin: "map Not (bin_to_bl n w) = bin_to_bl n (NOT w)" |
|
2332 |
by (simp add: bin_to_bl_def bl_not_aux_bin) |
|
2333 |
||
2334 |
lemma bl_and_bin: "map2 (\<and>) (bin_to_bl n v) (bin_to_bl n w) = bin_to_bl n (v AND w)" |
|
2335 |
by (simp add: bin_to_bl_def bl_and_aux_bin) |
|
2336 |
||
2337 |
lemma bl_or_bin: "map2 (\<or>) (bin_to_bl n v) (bin_to_bl n w) = bin_to_bl n (v OR w)" |
|
2338 |
by (simp add: bin_to_bl_def bl_or_aux_bin) |
|
2339 |
||
70193 | 2340 |
lemma bl_xor_bin: "map2 (\<noteq>) (bin_to_bl n v) (bin_to_bl n w) = bin_to_bl n (v XOR w)" |
2341 |
using bl_xor_aux_bin by (simp add: bin_to_bl_def) |
|
70190 | 2342 |
|
70169 | 2343 |
end |