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