24333
|
1 |
(*
|
|
2 |
ID: $Id$
|
|
3 |
Author: Jeremy Dawson, NICTA
|
|
4 |
|
|
5 |
contains theorems to do with integers, expressed using Pls, Min, BIT,
|
|
6 |
theorems linking them to lists of booleans, and repeated splitting
|
|
7 |
and concatenation.
|
|
8 |
*)
|
|
9 |
|
|
10 |
header "Bool lists and integers"
|
|
11 |
|
|
12 |
theory BinBoolList imports BinOperations begin
|
|
13 |
|
24350
|
14 |
subsection "Arithmetic in terms of bool lists"
|
24333
|
15 |
|
|
16 |
consts (* arithmetic operations in terms of the reversed bool list,
|
|
17 |
assuming input list(s) the same length, and don't extend them *)
|
|
18 |
rbl_succ :: "bool list => bool list"
|
|
19 |
rbl_pred :: "bool list => bool list"
|
|
20 |
rbl_add :: "bool list => bool list => bool list"
|
|
21 |
rbl_mult :: "bool list => bool list => bool list"
|
|
22 |
|
|
23 |
primrec
|
|
24 |
Nil: "rbl_succ Nil = Nil"
|
|
25 |
Cons: "rbl_succ (x # xs) = (if x then False # rbl_succ xs else True # xs)"
|
|
26 |
|
|
27 |
primrec
|
|
28 |
Nil : "rbl_pred Nil = Nil"
|
|
29 |
Cons : "rbl_pred (x # xs) = (if x then False # xs else True # rbl_pred xs)"
|
|
30 |
|
|
31 |
primrec (* result is length of first arg, second arg may be longer *)
|
|
32 |
Nil : "rbl_add Nil x = Nil"
|
|
33 |
Cons : "rbl_add (y # ys) x = (let ws = rbl_add ys (tl x) in
|
|
34 |
(y ~= hd x) # (if hd x & y then rbl_succ ws else ws))"
|
|
35 |
|
|
36 |
primrec (* result is length of first arg, second arg may be longer *)
|
|
37 |
Nil : "rbl_mult Nil x = Nil"
|
|
38 |
Cons : "rbl_mult (y # ys) x = (let ws = False # rbl_mult ys x in
|
|
39 |
if y then rbl_add ws x else ws)"
|
|
40 |
|
|
41 |
lemma tl_take: "tl (take n l) = take (n - 1) (tl l)"
|
|
42 |
apply (cases n, clarsimp)
|
|
43 |
apply (cases l, auto)
|
|
44 |
done
|
|
45 |
|
|
46 |
lemma take_butlast [rule_format] :
|
|
47 |
"ALL n. n < length l --> take n (butlast l) = take n l"
|
|
48 |
apply (induct l, clarsimp)
|
|
49 |
apply clarsimp
|
|
50 |
apply (case_tac n)
|
|
51 |
apply auto
|
|
52 |
done
|
|
53 |
|
|
54 |
lemma butlast_take [rule_format] :
|
|
55 |
"ALL n. n <= length l --> butlast (take n l) = take (n - 1) l"
|
|
56 |
apply (induct l, clarsimp)
|
|
57 |
apply clarsimp
|
|
58 |
apply (case_tac "n")
|
|
59 |
apply safe
|
|
60 |
apply simp_all
|
|
61 |
apply (case_tac "nat")
|
|
62 |
apply auto
|
|
63 |
done
|
|
64 |
|
|
65 |
lemma butlast_drop [rule_format] :
|
|
66 |
"ALL n. butlast (drop n l) = drop n (butlast l)"
|
|
67 |
apply (induct l)
|
|
68 |
apply clarsimp
|
|
69 |
apply clarsimp
|
|
70 |
apply safe
|
|
71 |
apply ((case_tac n, auto)[1])+
|
|
72 |
done
|
|
73 |
|
|
74 |
lemma butlast_power:
|
|
75 |
"(butlast ^ n) bl = take (length bl - n) bl"
|
|
76 |
by (induct n) (auto simp: butlast_take)
|
|
77 |
|
|
78 |
lemma bin_to_bl_aux_Pls_minus_simp:
|
|
79 |
"0 < n ==> bin_to_bl_aux n Numeral.Pls bl =
|
|
80 |
bin_to_bl_aux (n - 1) Numeral.Pls (False # bl)"
|
|
81 |
by (cases n) auto
|
|
82 |
|
|
83 |
lemma bin_to_bl_aux_Min_minus_simp:
|
|
84 |
"0 < n ==> bin_to_bl_aux n Numeral.Min bl =
|
|
85 |
bin_to_bl_aux (n - 1) Numeral.Min (True # bl)"
|
|
86 |
by (cases n) auto
|
|
87 |
|
|
88 |
lemma bin_to_bl_aux_Bit_minus_simp:
|
|
89 |
"0 < n ==> bin_to_bl_aux n (w BIT b) bl =
|
|
90 |
bin_to_bl_aux (n - 1) w ((b = bit.B1) # bl)"
|
|
91 |
by (cases n) auto
|
|
92 |
|
|
93 |
declare bin_to_bl_aux_Pls_minus_simp [simp]
|
|
94 |
bin_to_bl_aux_Min_minus_simp [simp]
|
|
95 |
bin_to_bl_aux_Bit_minus_simp [simp]
|
|
96 |
|
|
97 |
(** link between bin and bool list **)
|
|
98 |
|
|
99 |
lemma bl_to_bin_aux_append [rule_format] :
|
|
100 |
"ALL w. bl_to_bin_aux w (bs @ cs) = bl_to_bin_aux (bl_to_bin_aux w bs) cs"
|
|
101 |
by (induct bs) auto
|
|
102 |
|
|
103 |
lemma bin_to_bl_aux_append [rule_format] :
|
|
104 |
"ALL w bs. bin_to_bl_aux n w bs @ cs = bin_to_bl_aux n w (bs @ cs)"
|
|
105 |
by (induct n) auto
|
|
106 |
|
|
107 |
lemma bl_to_bin_append:
|
|
108 |
"bl_to_bin (bs @ cs) = bl_to_bin_aux (bl_to_bin bs) cs"
|
|
109 |
unfolding bl_to_bin_def by (rule bl_to_bin_aux_append)
|
|
110 |
|
|
111 |
lemma bin_to_bl_aux_alt:
|
|
112 |
"bin_to_bl_aux n w bs = bin_to_bl n w @ bs"
|
|
113 |
unfolding bin_to_bl_def by (simp add : bin_to_bl_aux_append)
|
|
114 |
|
|
115 |
lemma bin_to_bl_0: "bin_to_bl 0 bs = []"
|
|
116 |
unfolding bin_to_bl_def by auto
|
|
117 |
|
|
118 |
lemma size_bin_to_bl_aux [rule_format] :
|
|
119 |
"ALL w bs. size (bin_to_bl_aux n w bs) = n + length bs"
|
|
120 |
by (induct n) auto
|
|
121 |
|
|
122 |
lemma size_bin_to_bl: "size (bin_to_bl n w) = n"
|
|
123 |
unfolding bin_to_bl_def by (simp add : size_bin_to_bl_aux)
|
|
124 |
|
|
125 |
lemma bin_bl_bin' [rule_format] :
|
|
126 |
"ALL w bs. bl_to_bin (bin_to_bl_aux n w bs) =
|
|
127 |
bl_to_bin_aux (bintrunc n w) bs"
|
|
128 |
by (induct n) (auto simp add : bl_to_bin_def)
|
|
129 |
|
|
130 |
lemma bin_bl_bin: "bl_to_bin (bin_to_bl n w) = bintrunc n w"
|
|
131 |
unfolding bin_to_bl_def bin_bl_bin' by auto
|
|
132 |
|
|
133 |
lemma bl_bin_bl' [rule_format] :
|
|
134 |
"ALL w n. bin_to_bl (n + length bs) (bl_to_bin_aux w bs) =
|
|
135 |
bin_to_bl_aux n w bs"
|
|
136 |
apply (induct "bs")
|
|
137 |
apply auto
|
|
138 |
apply (simp_all only : add_Suc [symmetric])
|
|
139 |
apply (auto simp add : bin_to_bl_def)
|
|
140 |
done
|
|
141 |
|
|
142 |
lemma bl_bin_bl: "bin_to_bl (length bs) (bl_to_bin bs) = bs"
|
|
143 |
unfolding bl_to_bin_def
|
|
144 |
apply (rule box_equals)
|
|
145 |
apply (rule bl_bin_bl')
|
|
146 |
prefer 2
|
|
147 |
apply (rule bin_to_bl_aux.Z)
|
|
148 |
apply simp
|
|
149 |
done
|
|
150 |
|
|
151 |
declare
|
|
152 |
bin_to_bl_0 [simp]
|
|
153 |
size_bin_to_bl [simp]
|
|
154 |
bin_bl_bin [simp]
|
|
155 |
bl_bin_bl [simp]
|
|
156 |
|
|
157 |
lemma bl_to_bin_inj:
|
|
158 |
"bl_to_bin bs = bl_to_bin cs ==> length bs = length cs ==> bs = cs"
|
|
159 |
apply (rule_tac box_equals)
|
|
160 |
defer
|
|
161 |
apply (rule bl_bin_bl)
|
|
162 |
apply (rule bl_bin_bl)
|
|
163 |
apply simp
|
|
164 |
done
|
|
165 |
|
|
166 |
lemma bl_to_bin_False: "bl_to_bin (False # bl) = bl_to_bin bl"
|
|
167 |
unfolding bl_to_bin_def by auto
|
|
168 |
|
|
169 |
lemma bl_to_bin_Nil: "bl_to_bin [] = Numeral.Pls"
|
|
170 |
unfolding bl_to_bin_def by auto
|
|
171 |
|
|
172 |
lemma bin_to_bl_Pls_aux [rule_format] :
|
|
173 |
"ALL bl. bin_to_bl_aux n Numeral.Pls bl = replicate n False @ bl"
|
|
174 |
by (induct n) (auto simp: replicate_app_Cons_same)
|
|
175 |
|
|
176 |
lemma bin_to_bl_Pls: "bin_to_bl n Numeral.Pls = replicate n False"
|
|
177 |
unfolding bin_to_bl_def by (simp add : bin_to_bl_Pls_aux)
|
|
178 |
|
|
179 |
lemma bin_to_bl_Min_aux [rule_format] :
|
|
180 |
"ALL bl. bin_to_bl_aux n Numeral.Min bl = replicate n True @ bl"
|
|
181 |
by (induct n) (auto simp: replicate_app_Cons_same)
|
|
182 |
|
|
183 |
lemma bin_to_bl_Min: "bin_to_bl n Numeral.Min = replicate n True"
|
|
184 |
unfolding bin_to_bl_def by (simp add : bin_to_bl_Min_aux)
|
|
185 |
|
|
186 |
lemma bl_to_bin_rep_F:
|
|
187 |
"bl_to_bin (replicate n False @ bl) = bl_to_bin bl"
|
|
188 |
apply (simp add: bin_to_bl_Pls_aux [symmetric] bin_bl_bin')
|
|
189 |
apply (simp add: bl_to_bin_def)
|
|
190 |
done
|
|
191 |
|
|
192 |
lemma bin_to_bl_trunc:
|
|
193 |
"n <= m ==> bin_to_bl n (bintrunc m w) = bin_to_bl n w"
|
|
194 |
by (auto intro: bl_to_bin_inj)
|
|
195 |
|
|
196 |
declare
|
|
197 |
bin_to_bl_trunc [simp]
|
|
198 |
bl_to_bin_False [simp]
|
|
199 |
bl_to_bin_Nil [simp]
|
|
200 |
|
|
201 |
lemma bin_to_bl_aux_bintr [rule_format] :
|
|
202 |
"ALL m bin bl. bin_to_bl_aux n (bintrunc m bin) bl =
|
|
203 |
replicate (n - m) False @ bin_to_bl_aux (min n m) bin bl"
|
|
204 |
apply (induct_tac "n")
|
|
205 |
apply clarsimp
|
|
206 |
apply clarsimp
|
|
207 |
apply (case_tac "m")
|
|
208 |
apply (clarsimp simp: bin_to_bl_Pls_aux)
|
|
209 |
apply (erule thin_rl)
|
|
210 |
apply (induct_tac n)
|
|
211 |
apply auto
|
|
212 |
done
|
|
213 |
|
|
214 |
lemmas bin_to_bl_bintr =
|
|
215 |
bin_to_bl_aux_bintr [where bl = "[]", folded bin_to_bl_def]
|
|
216 |
|
|
217 |
lemma bl_to_bin_rep_False: "bl_to_bin (replicate n False) = Numeral.Pls"
|
|
218 |
by (induct n) auto
|
|
219 |
|
|
220 |
lemma len_bin_to_bl_aux [rule_format] :
|
|
221 |
"ALL w bs. length (bin_to_bl_aux n w bs) = n + length bs"
|
|
222 |
by (induct "n") auto
|
|
223 |
|
|
224 |
lemma len_bin_to_bl [simp]: "length (bin_to_bl n w) = n"
|
|
225 |
unfolding bin_to_bl_def len_bin_to_bl_aux by auto
|
|
226 |
|
|
227 |
lemma sign_bl_bin' [rule_format] :
|
|
228 |
"ALL w. bin_sign (bl_to_bin_aux w bs) = bin_sign w"
|
|
229 |
by (induct bs) auto
|
|
230 |
|
|
231 |
lemma sign_bl_bin: "bin_sign (bl_to_bin bs) = Numeral.Pls"
|
|
232 |
unfolding bl_to_bin_def by (simp add : sign_bl_bin')
|
|
233 |
|
|
234 |
lemma bl_sbin_sign_aux [rule_format] :
|
|
235 |
"ALL w bs. hd (bin_to_bl_aux (Suc n) w bs) =
|
|
236 |
(bin_sign (sbintrunc n w) = Numeral.Min)"
|
|
237 |
apply (induct "n")
|
|
238 |
apply clarsimp
|
|
239 |
apply (case_tac w rule: bin_exhaust)
|
|
240 |
apply (simp split add : bit.split)
|
|
241 |
apply clarsimp
|
|
242 |
done
|
|
243 |
|
|
244 |
lemma bl_sbin_sign:
|
|
245 |
"hd (bin_to_bl (Suc n) w) = (bin_sign (sbintrunc n w) = Numeral.Min)"
|
|
246 |
unfolding bin_to_bl_def by (rule bl_sbin_sign_aux)
|
|
247 |
|
|
248 |
lemma bin_nth_of_bl_aux [rule_format] :
|
|
249 |
"ALL w. bin_nth (bl_to_bin_aux w bl) n =
|
|
250 |
(n < size bl & rev bl ! n | n >= length bl & bin_nth w (n - size bl))"
|
|
251 |
apply (induct_tac bl)
|
|
252 |
apply clarsimp
|
|
253 |
apply clarsimp
|
|
254 |
apply (cut_tac x=n and y="size list" in linorder_less_linear)
|
|
255 |
apply (erule disjE, simp add: nth_append)+
|
|
256 |
apply (simp add: nth_append)
|
|
257 |
done
|
|
258 |
|
|
259 |
lemma bin_nth_of_bl: "bin_nth (bl_to_bin bl) n = (n < length bl & rev bl ! n)";
|
|
260 |
unfolding bl_to_bin_def by (simp add : bin_nth_of_bl_aux)
|
|
261 |
|
|
262 |
lemma bin_nth_bl [rule_format] : "ALL m w. n < m -->
|
|
263 |
bin_nth w n = nth (rev (bin_to_bl m w)) n"
|
|
264 |
apply (induct n)
|
|
265 |
apply clarsimp
|
|
266 |
apply (case_tac m, clarsimp)
|
|
267 |
apply (clarsimp simp: bin_to_bl_def)
|
|
268 |
apply (simp add: bin_to_bl_aux_alt)
|
|
269 |
apply clarsimp
|
|
270 |
apply (case_tac m, clarsimp)
|
|
271 |
apply (clarsimp simp: bin_to_bl_def)
|
|
272 |
apply (simp add: bin_to_bl_aux_alt)
|
|
273 |
done
|
|
274 |
|
|
275 |
lemma nth_rev [rule_format] :
|
|
276 |
"n < length xs --> rev xs ! n = xs ! (length xs - 1 - n)"
|
|
277 |
apply (induct_tac "xs")
|
|
278 |
apply simp
|
|
279 |
apply (clarsimp simp add : nth_append nth.simps split add : nat.split)
|
|
280 |
apply (rule_tac f = "%n. list ! n" in arg_cong)
|
|
281 |
apply arith
|
|
282 |
done
|
|
283 |
|
|
284 |
lemmas nth_rev_alt = nth_rev [where xs = "rev ?ys", simplified]
|
|
285 |
|
|
286 |
lemma nth_bin_to_bl_aux [rule_format] :
|
|
287 |
"ALL w n bl. n < m + length bl --> (bin_to_bl_aux m w bl) ! n =
|
|
288 |
(if n < m then bin_nth w (m - 1 - n) else bl ! (n - m))"
|
|
289 |
apply (induct_tac "m")
|
|
290 |
apply clarsimp
|
|
291 |
apply clarsimp
|
|
292 |
apply (case_tac w rule: bin_exhaust)
|
|
293 |
apply clarsimp
|
|
294 |
apply (case_tac "na - n")
|
|
295 |
apply arith
|
|
296 |
apply simp
|
|
297 |
apply (rule_tac f = "%n. bl ! n" in arg_cong)
|
|
298 |
apply arith
|
|
299 |
done
|
|
300 |
|
|
301 |
lemma nth_bin_to_bl: "n < m ==> (bin_to_bl m w) ! n = bin_nth w (m - Suc n)"
|
|
302 |
unfolding bin_to_bl_def by (simp add : nth_bin_to_bl_aux)
|
|
303 |
|
|
304 |
lemma bl_to_bin_lt2p_aux [rule_format] :
|
|
305 |
"ALL w. bl_to_bin_aux w bs < (w + 1) * (2 ^ length bs)"
|
|
306 |
apply (induct "bs")
|
|
307 |
apply clarsimp
|
|
308 |
apply clarsimp
|
|
309 |
apply safe
|
|
310 |
apply (erule allE, erule xtr8 [rotated],
|
|
311 |
simp add: Bit_def ring_simps cong add : number_of_False_cong)+
|
|
312 |
done
|
|
313 |
|
|
314 |
lemma bl_to_bin_lt2p: "bl_to_bin bs < (2 ^ length bs)"
|
|
315 |
apply (unfold bl_to_bin_def)
|
|
316 |
apply (rule xtr1)
|
|
317 |
prefer 2
|
|
318 |
apply (rule bl_to_bin_lt2p_aux)
|
|
319 |
apply simp
|
|
320 |
done
|
|
321 |
|
|
322 |
lemma bl_to_bin_ge2p_aux [rule_format] :
|
|
323 |
"ALL w. bl_to_bin_aux w bs >= w * (2 ^ length bs)"
|
|
324 |
apply (induct bs)
|
|
325 |
apply clarsimp
|
|
326 |
apply clarsimp
|
|
327 |
apply safe
|
|
328 |
apply (erule allE, erule less_eq_less.order_trans [rotated],
|
|
329 |
simp add: Bit_def ring_simps cong add : number_of_False_cong)+
|
|
330 |
done
|
|
331 |
|
|
332 |
lemma bl_to_bin_ge0: "bl_to_bin bs >= 0"
|
|
333 |
apply (unfold bl_to_bin_def)
|
|
334 |
apply (rule xtr4)
|
|
335 |
apply (rule bl_to_bin_ge2p_aux)
|
|
336 |
apply simp
|
|
337 |
done
|
|
338 |
|
|
339 |
lemma butlast_rest_bin:
|
|
340 |
"butlast (bin_to_bl n w) = bin_to_bl (n - 1) (bin_rest w)"
|
|
341 |
apply (unfold bin_to_bl_def)
|
|
342 |
apply (cases w rule: bin_exhaust)
|
|
343 |
apply (cases n, clarsimp)
|
|
344 |
apply clarsimp
|
|
345 |
apply (auto simp add: bin_to_bl_aux_alt)
|
|
346 |
done
|
|
347 |
|
|
348 |
lemmas butlast_bin_rest = butlast_rest_bin
|
|
349 |
[where w="bl_to_bin ?bl" and n="length ?bl", simplified]
|
|
350 |
|
|
351 |
lemma butlast_rest_bl2bin_aux [rule_format] :
|
|
352 |
"ALL w. bl ~= [] -->
|
|
353 |
bl_to_bin_aux w (butlast bl) = bin_rest (bl_to_bin_aux w bl)"
|
|
354 |
by (induct bl) auto
|
|
355 |
|
|
356 |
lemma butlast_rest_bl2bin:
|
|
357 |
"bl_to_bin (butlast bl) = bin_rest (bl_to_bin bl)"
|
|
358 |
apply (unfold bl_to_bin_def)
|
|
359 |
apply (cases bl)
|
|
360 |
apply (auto simp add: butlast_rest_bl2bin_aux)
|
|
361 |
done
|
|
362 |
|
|
363 |
lemma trunc_bl2bin_aux [rule_format] :
|
|
364 |
"ALL w. bintrunc m (bl_to_bin_aux w bl) =
|
|
365 |
bl_to_bin_aux (bintrunc (m - length bl) w) (drop (length bl - m) bl)"
|
|
366 |
apply (induct_tac bl)
|
|
367 |
apply clarsimp
|
|
368 |
apply clarsimp
|
|
369 |
apply safe
|
|
370 |
apply (case_tac "m - size list")
|
|
371 |
apply (simp add : diff_is_0_eq [THEN iffD1, THEN Suc_diff_le])
|
|
372 |
apply simp
|
|
373 |
apply (rule_tac f = "%nat. bl_to_bin_aux (bintrunc nat w BIT bit.B1) list"
|
|
374 |
in arg_cong)
|
|
375 |
apply simp
|
|
376 |
apply (case_tac "m - size list")
|
|
377 |
apply (simp add: diff_is_0_eq [THEN iffD1, THEN Suc_diff_le])
|
|
378 |
apply simp
|
|
379 |
apply (rule_tac f = "%nat. bl_to_bin_aux (bintrunc nat w BIT bit.B0) list"
|
|
380 |
in arg_cong)
|
|
381 |
apply simp
|
|
382 |
done
|
|
383 |
|
|
384 |
lemma trunc_bl2bin:
|
|
385 |
"bintrunc m (bl_to_bin bl) = bl_to_bin (drop (length bl - m) bl)"
|
|
386 |
unfolding bl_to_bin_def by (simp add : trunc_bl2bin_aux)
|
|
387 |
|
|
388 |
lemmas trunc_bl2bin_len [simp] =
|
|
389 |
trunc_bl2bin [of "length bl" bl, simplified, standard]
|
|
390 |
|
|
391 |
lemma bl2bin_drop:
|
|
392 |
"bl_to_bin (drop k bl) = bintrunc (length bl - k) (bl_to_bin bl)"
|
|
393 |
apply (rule trans)
|
|
394 |
prefer 2
|
|
395 |
apply (rule trunc_bl2bin [symmetric])
|
|
396 |
apply (cases "k <= length bl")
|
|
397 |
apply auto
|
|
398 |
done
|
|
399 |
|
|
400 |
lemma nth_rest_power_bin [rule_format] :
|
|
401 |
"ALL n. bin_nth ((bin_rest ^ k) w) n = bin_nth w (n + k)"
|
|
402 |
apply (induct k, clarsimp)
|
|
403 |
apply clarsimp
|
|
404 |
apply (simp only: bin_nth.Suc [symmetric] add_Suc)
|
|
405 |
done
|
|
406 |
|
|
407 |
lemma take_rest_power_bin:
|
|
408 |
"m <= n ==> take m (bin_to_bl n w) = bin_to_bl m ((bin_rest ^ (n - m)) w)"
|
|
409 |
apply (rule nth_equalityI)
|
|
410 |
apply simp
|
|
411 |
apply (clarsimp simp add: nth_bin_to_bl nth_rest_power_bin)
|
|
412 |
done
|
|
413 |
|
|
414 |
lemma hd_butlast: "size xs > 1 ==> hd (butlast xs) = hd xs"
|
|
415 |
by (cases xs) auto
|
|
416 |
|
|
417 |
lemma last_bin_last' [rule_format] :
|
|
418 |
"ALL w. size xs > 0 --> last xs = (bin_last (bl_to_bin_aux w xs) = bit.B1)"
|
|
419 |
by (induct xs) auto
|
|
420 |
|
|
421 |
lemma last_bin_last:
|
|
422 |
"size xs > 0 ==> last xs = (bin_last (bl_to_bin xs) = bit.B1)"
|
|
423 |
unfolding bl_to_bin_def by (erule last_bin_last')
|
|
424 |
|
|
425 |
lemma bin_last_last:
|
|
426 |
"bin_last w = (if last (bin_to_bl (Suc n) w) then bit.B1 else bit.B0)"
|
|
427 |
apply (unfold bin_to_bl_def)
|
|
428 |
apply simp
|
|
429 |
apply (auto simp add: bin_to_bl_aux_alt)
|
|
430 |
done
|
|
431 |
|
|
432 |
(** links between bit-wise operations and operations on bool lists **)
|
|
433 |
|
|
434 |
lemma app2_Nil [simp]: "app2 f [] ys = []"
|
|
435 |
unfolding app2_def by auto
|
|
436 |
|
|
437 |
lemma app2_Cons [simp]:
|
|
438 |
"app2 f (x # xs) (y # ys) = f x y # app2 f xs ys"
|
|
439 |
unfolding app2_def by auto
|
|
440 |
|
|
441 |
lemma bl_xor_aux_bin [rule_format] : "ALL v w bs cs.
|
|
442 |
app2 (%x y. x ~= y) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) =
|
24353
|
443 |
bin_to_bl_aux n (v XOR w) (app2 (%x y. x ~= y) bs cs)"
|
24333
|
444 |
apply (induct_tac n)
|
|
445 |
apply safe
|
|
446 |
apply simp
|
|
447 |
apply (case_tac v rule: bin_exhaust)
|
|
448 |
apply (case_tac w rule: bin_exhaust)
|
|
449 |
apply clarsimp
|
|
450 |
apply (case_tac b)
|
|
451 |
apply (case_tac ba, safe, simp_all)+
|
|
452 |
done
|
|
453 |
|
|
454 |
lemma bl_or_aux_bin [rule_format] : "ALL v w bs cs.
|
|
455 |
app2 (op | ) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) =
|
24353
|
456 |
bin_to_bl_aux n (v OR w) (app2 (op | ) bs cs)"
|
24333
|
457 |
apply (induct_tac n)
|
|
458 |
apply safe
|
|
459 |
apply simp
|
|
460 |
apply (case_tac v rule: bin_exhaust)
|
|
461 |
apply (case_tac w rule: bin_exhaust)
|
|
462 |
apply clarsimp
|
|
463 |
apply (case_tac b)
|
|
464 |
apply (case_tac ba, safe, simp_all)+
|
|
465 |
done
|
|
466 |
|
|
467 |
lemma bl_and_aux_bin [rule_format] : "ALL v w bs cs.
|
|
468 |
app2 (op & ) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) =
|
24353
|
469 |
bin_to_bl_aux n (v AND w) (app2 (op & ) bs cs)"
|
24333
|
470 |
apply (induct_tac n)
|
|
471 |
apply safe
|
|
472 |
apply simp
|
|
473 |
apply (case_tac v rule: bin_exhaust)
|
|
474 |
apply (case_tac w rule: bin_exhaust)
|
|
475 |
apply clarsimp
|
|
476 |
apply (case_tac b)
|
|
477 |
apply (case_tac ba, safe, simp_all)+
|
|
478 |
done
|
|
479 |
|
|
480 |
lemma bl_not_aux_bin [rule_format] :
|
|
481 |
"ALL w cs. map Not (bin_to_bl_aux n w cs) =
|
24353
|
482 |
bin_to_bl_aux n (NOT w) (map Not cs)"
|
24333
|
483 |
apply (induct n)
|
|
484 |
apply clarsimp
|
|
485 |
apply clarsimp
|
|
486 |
apply (case_tac w rule: bin_exhaust)
|
|
487 |
apply (case_tac b)
|
|
488 |
apply auto
|
|
489 |
done
|
|
490 |
|
|
491 |
lemmas bl_not_bin = bl_not_aux_bin
|
|
492 |
[where cs = "[]", unfolded bin_to_bl_def [symmetric] map.simps]
|
|
493 |
|
|
494 |
lemmas bl_and_bin = bl_and_aux_bin [where bs="[]" and cs="[]",
|
|
495 |
unfolded app2_Nil, folded bin_to_bl_def]
|
|
496 |
|
|
497 |
lemmas bl_or_bin = bl_or_aux_bin [where bs="[]" and cs="[]",
|
|
498 |
unfolded app2_Nil, folded bin_to_bl_def]
|
|
499 |
|
|
500 |
lemmas bl_xor_bin = bl_xor_aux_bin [where bs="[]" and cs="[]",
|
|
501 |
unfolded app2_Nil, folded bin_to_bl_def]
|
|
502 |
|
|
503 |
lemma drop_bin2bl_aux [rule_format] :
|
|
504 |
"ALL m bin bs. drop m (bin_to_bl_aux n bin bs) =
|
|
505 |
bin_to_bl_aux (n - m) bin (drop (m - n) bs)"
|
|
506 |
apply (induct n, clarsimp)
|
|
507 |
apply clarsimp
|
|
508 |
apply (case_tac bin rule: bin_exhaust)
|
|
509 |
apply (case_tac "m <= n", simp)
|
|
510 |
apply (case_tac "m - n", simp)
|
|
511 |
apply simp
|
|
512 |
apply (rule_tac f = "%nat. drop nat bs" in arg_cong)
|
|
513 |
apply simp
|
|
514 |
done
|
|
515 |
|
|
516 |
lemma drop_bin2bl: "drop m (bin_to_bl n bin) = bin_to_bl (n - m) bin"
|
|
517 |
unfolding bin_to_bl_def by (simp add : drop_bin2bl_aux)
|
|
518 |
|
|
519 |
lemma take_bin2bl_lem1 [rule_format] :
|
|
520 |
"ALL w bs. take m (bin_to_bl_aux m w bs) = bin_to_bl m w"
|
|
521 |
apply (induct m, clarsimp)
|
|
522 |
apply clarsimp
|
|
523 |
apply (simp add: bin_to_bl_aux_alt)
|
|
524 |
apply (simp add: bin_to_bl_def)
|
|
525 |
apply (simp add: bin_to_bl_aux_alt)
|
|
526 |
done
|
|
527 |
|
|
528 |
lemma take_bin2bl_lem [rule_format] :
|
|
529 |
"ALL w bs. take m (bin_to_bl_aux (m + n) w bs) =
|
|
530 |
take m (bin_to_bl (m + n) w)"
|
|
531 |
apply (induct n)
|
|
532 |
apply clarify
|
|
533 |
apply (simp_all (no_asm) add: bin_to_bl_def take_bin2bl_lem1)
|
|
534 |
apply simp
|
|
535 |
done
|
|
536 |
|
|
537 |
lemma bin_split_take [rule_format] :
|
|
538 |
"ALL b c. bin_split n c = (a, b) -->
|
|
539 |
bin_to_bl m a = take m (bin_to_bl (m + n) c)"
|
|
540 |
apply (induct n)
|
|
541 |
apply clarsimp
|
|
542 |
apply (clarsimp simp: Let_def split: ls_splits)
|
|
543 |
apply (simp add: bin_to_bl_def)
|
|
544 |
apply (simp add: take_bin2bl_lem)
|
|
545 |
done
|
|
546 |
|
|
547 |
lemma bin_split_take1:
|
|
548 |
"k = m + n ==> bin_split n c = (a, b) ==>
|
|
549 |
bin_to_bl m a = take m (bin_to_bl k c)"
|
|
550 |
by (auto elim: bin_split_take)
|
|
551 |
|
|
552 |
lemma nth_takefill [rule_format] : "ALL m l. m < n -->
|
|
553 |
takefill fill n l ! m = (if m < length l then l ! m else fill)"
|
|
554 |
apply (induct n, clarsimp)
|
|
555 |
apply clarsimp
|
|
556 |
apply (case_tac m)
|
|
557 |
apply (simp split: list.split)
|
|
558 |
apply clarsimp
|
|
559 |
apply (erule allE)+
|
|
560 |
apply (erule (1) impE)
|
|
561 |
apply (simp split: list.split)
|
|
562 |
done
|
|
563 |
|
|
564 |
lemma takefill_alt [rule_format] :
|
|
565 |
"ALL l. takefill fill n l = take n l @ replicate (n - length l) fill"
|
|
566 |
by (induct n) (auto split: list.split)
|
|
567 |
|
|
568 |
lemma takefill_replicate [simp]:
|
|
569 |
"takefill fill n (replicate m fill) = replicate n fill"
|
|
570 |
by (simp add : takefill_alt replicate_add [symmetric])
|
|
571 |
|
|
572 |
lemma takefill_le' [rule_format] :
|
|
573 |
"ALL l n. n = m + k --> takefill x m (takefill x n l) = takefill x m l"
|
|
574 |
by (induct m) (auto split: list.split)
|
|
575 |
|
|
576 |
lemma length_takefill [simp]: "length (takefill fill n l) = n"
|
|
577 |
by (simp add : takefill_alt)
|
|
578 |
|
|
579 |
lemma take_takefill':
|
|
580 |
"!!w n. n = k + m ==> take k (takefill fill n w) = takefill fill k w"
|
|
581 |
by (induct k) (auto split add : list.split)
|
|
582 |
|
|
583 |
lemma drop_takefill:
|
|
584 |
"!!w. drop k (takefill fill (m + k) w) = takefill fill m (drop k w)"
|
|
585 |
by (induct k) (auto split add : list.split)
|
|
586 |
|
|
587 |
lemma takefill_le [simp]:
|
|
588 |
"m \<le> n \<Longrightarrow> takefill x m (takefill x n l) = takefill x m l"
|
|
589 |
by (auto simp: le_iff_add takefill_le')
|
|
590 |
|
|
591 |
lemma take_takefill [simp]:
|
|
592 |
"m \<le> n \<Longrightarrow> take m (takefill fill n w) = takefill fill m w"
|
|
593 |
by (auto simp: le_iff_add take_takefill')
|
|
594 |
|
|
595 |
lemma takefill_append:
|
|
596 |
"takefill fill (m + length xs) (xs @ w) = xs @ (takefill fill m w)"
|
|
597 |
by (induct xs) auto
|
|
598 |
|
|
599 |
lemma takefill_same':
|
|
600 |
"l = length xs ==> takefill fill l xs = xs"
|
|
601 |
by clarify (induct xs, auto)
|
|
602 |
|
|
603 |
lemmas takefill_same [simp] = takefill_same' [OF refl]
|
|
604 |
|
|
605 |
lemma takefill_bintrunc:
|
|
606 |
"takefill False n bl = rev (bin_to_bl n (bl_to_bin (rev bl)))"
|
|
607 |
apply (rule nth_equalityI)
|
|
608 |
apply simp
|
|
609 |
apply (clarsimp simp: nth_takefill nth_rev nth_bin_to_bl bin_nth_of_bl)
|
|
610 |
done
|
|
611 |
|
|
612 |
lemma bl_bin_bl_rtf:
|
|
613 |
"bin_to_bl n (bl_to_bin bl) = rev (takefill False n (rev bl))"
|
|
614 |
by (simp add : takefill_bintrunc)
|
|
615 |
|
|
616 |
lemmas bl_bin_bl_rep_drop =
|
|
617 |
bl_bin_bl_rtf [simplified takefill_alt,
|
|
618 |
simplified, simplified rev_take, simplified]
|
|
619 |
|
|
620 |
lemma tf_rev:
|
|
621 |
"n + k = m + length bl ==> takefill x m (rev (takefill y n bl)) =
|
|
622 |
rev (takefill y m (rev (takefill x k (rev bl))))"
|
|
623 |
apply (rule nth_equalityI)
|
|
624 |
apply (auto simp add: nth_takefill nth_rev)
|
|
625 |
apply (rule_tac f = "%n. bl ! n" in arg_cong)
|
|
626 |
apply arith
|
|
627 |
done
|
|
628 |
|
|
629 |
lemma takefill_minus:
|
|
630 |
"0 < n ==> takefill fill (Suc (n - 1)) w = takefill fill n w"
|
|
631 |
by auto
|
|
632 |
|
|
633 |
lemmas takefill_Suc_cases =
|
|
634 |
list.cases [THEN takefill.Suc [THEN trans], standard]
|
|
635 |
|
|
636 |
lemmas takefill_Suc_Nil = takefill_Suc_cases (1)
|
|
637 |
lemmas takefill_Suc_Cons = takefill_Suc_cases (2)
|
|
638 |
|
|
639 |
lemmas takefill_minus_simps = takefill_Suc_cases [THEN [2]
|
|
640 |
takefill_minus [symmetric, THEN trans], standard]
|
|
641 |
|
|
642 |
lemmas takefill_pred_simps [simp] =
|
|
643 |
takefill_minus_simps [where n="number_of bin", simplified nobm1, standard]
|
|
644 |
|
|
645 |
(* links with function bl_to_bin *)
|
|
646 |
|
|
647 |
lemma bl_to_bin_aux_cat:
|
|
648 |
"!!nv v. bl_to_bin_aux (bin_cat w nv v) bs =
|
|
649 |
bin_cat w (nv + length bs) (bl_to_bin_aux v bs)"
|
|
650 |
apply (induct bs)
|
|
651 |
apply simp
|
|
652 |
apply (simp add: bin_cat_Suc_Bit [symmetric] del: bin_cat.simps)
|
|
653 |
done
|
|
654 |
|
|
655 |
lemma bin_to_bl_aux_cat:
|
|
656 |
"!!w bs. bin_to_bl_aux (nv + nw) (bin_cat v nw w) bs =
|
|
657 |
bin_to_bl_aux nv v (bin_to_bl_aux nw w bs)"
|
|
658 |
by (induct nw) auto
|
|
659 |
|
|
660 |
lemmas bl_to_bin_aux_alt =
|
|
661 |
bl_to_bin_aux_cat [where nv = "0" and v = "Numeral.Pls",
|
|
662 |
simplified bl_to_bin_def [symmetric], simplified]
|
|
663 |
|
|
664 |
lemmas bin_to_bl_cat =
|
|
665 |
bin_to_bl_aux_cat [where bs = "[]", folded bin_to_bl_def]
|
|
666 |
|
|
667 |
lemmas bl_to_bin_aux_app_cat =
|
|
668 |
trans [OF bl_to_bin_aux_append bl_to_bin_aux_alt]
|
|
669 |
|
|
670 |
lemmas bin_to_bl_aux_cat_app =
|
|
671 |
trans [OF bin_to_bl_aux_cat bin_to_bl_aux_alt]
|
|
672 |
|
|
673 |
lemmas bl_to_bin_app_cat = bl_to_bin_aux_app_cat
|
|
674 |
[where w = "Numeral.Pls", folded bl_to_bin_def]
|
|
675 |
|
|
676 |
lemmas bin_to_bl_cat_app = bin_to_bl_aux_cat_app
|
|
677 |
[where bs = "[]", folded bin_to_bl_def]
|
|
678 |
|
|
679 |
(* bl_to_bin_app_cat_alt and bl_to_bin_app_cat are easily interderivable *)
|
|
680 |
lemma bl_to_bin_app_cat_alt:
|
|
681 |
"bin_cat (bl_to_bin cs) n w = bl_to_bin (cs @ bin_to_bl n w)"
|
|
682 |
by (simp add : bl_to_bin_app_cat)
|
|
683 |
|
|
684 |
lemma mask_lem: "(bl_to_bin (True # replicate n False)) =
|
|
685 |
Numeral.succ (bl_to_bin (replicate n True))"
|
|
686 |
apply (unfold bl_to_bin_def)
|
|
687 |
apply (induct n)
|
|
688 |
apply simp
|
|
689 |
apply (simp only: Suc_eq_add_numeral_1 replicate_add
|
|
690 |
append_Cons [symmetric] bl_to_bin_aux_append)
|
|
691 |
apply simp
|
|
692 |
done
|
|
693 |
|
|
694 |
(* function bl_of_nth *)
|
|
695 |
lemma length_bl_of_nth [simp]: "length (bl_of_nth n f) = n"
|
|
696 |
by (induct n) auto
|
|
697 |
|
|
698 |
lemma nth_bl_of_nth [simp]:
|
|
699 |
"m < n \<Longrightarrow> rev (bl_of_nth n f) ! m = f m"
|
|
700 |
apply (induct n)
|
|
701 |
apply simp
|
|
702 |
apply (clarsimp simp add : nth_append)
|
|
703 |
apply (rule_tac f = "f" in arg_cong)
|
|
704 |
apply simp
|
|
705 |
done
|
|
706 |
|
|
707 |
lemma bl_of_nth_inj:
|
|
708 |
"(!!k. k < n ==> f k = g k) ==> bl_of_nth n f = bl_of_nth n g"
|
|
709 |
by (induct n) auto
|
|
710 |
|
|
711 |
lemma bl_of_nth_nth_le [rule_format] : "ALL xs.
|
|
712 |
length xs >= n --> bl_of_nth n (nth (rev xs)) = drop (length xs - n) xs";
|
|
713 |
apply (induct n, clarsimp)
|
|
714 |
apply clarsimp
|
|
715 |
apply (rule trans [OF _ hd_Cons_tl])
|
|
716 |
apply (frule Suc_le_lessD)
|
|
717 |
apply (simp add: nth_rev trans [OF drop_Suc drop_tl, symmetric])
|
|
718 |
apply (subst hd_drop_conv_nth)
|
|
719 |
apply force
|
|
720 |
apply simp_all
|
|
721 |
apply (rule_tac f = "%n. drop n xs" in arg_cong)
|
|
722 |
apply simp
|
|
723 |
done
|
|
724 |
|
|
725 |
lemmas bl_of_nth_nth [simp] = order_refl [THEN bl_of_nth_nth_le, simplified]
|
|
726 |
|
|
727 |
lemma size_rbl_pred: "length (rbl_pred bl) = length bl"
|
|
728 |
by (induct bl) auto
|
|
729 |
|
|
730 |
lemma size_rbl_succ: "length (rbl_succ bl) = length bl"
|
|
731 |
by (induct bl) auto
|
|
732 |
|
|
733 |
lemma size_rbl_add:
|
|
734 |
"!!cl. length (rbl_add bl cl) = length bl"
|
|
735 |
by (induct bl) (auto simp: Let_def size_rbl_succ)
|
|
736 |
|
|
737 |
lemma size_rbl_mult:
|
|
738 |
"!!cl. length (rbl_mult bl cl) = length bl"
|
|
739 |
by (induct bl) (auto simp add : Let_def size_rbl_add)
|
|
740 |
|
|
741 |
lemmas rbl_sizes [simp] =
|
|
742 |
size_rbl_pred size_rbl_succ size_rbl_add size_rbl_mult
|
|
743 |
|
|
744 |
lemmas rbl_Nils =
|
|
745 |
rbl_pred.Nil rbl_succ.Nil rbl_add.Nil rbl_mult.Nil
|
|
746 |
|
|
747 |
lemma rbl_pred:
|
|
748 |
"!!bin. rbl_pred (rev (bin_to_bl n bin)) = rev (bin_to_bl n (Numeral.pred bin))"
|
|
749 |
apply (induct n, simp)
|
|
750 |
apply (unfold bin_to_bl_def)
|
|
751 |
apply clarsimp
|
|
752 |
apply (case_tac bin rule: bin_exhaust)
|
|
753 |
apply (case_tac b)
|
|
754 |
apply (clarsimp simp: bin_to_bl_aux_alt)+
|
|
755 |
done
|
|
756 |
|
|
757 |
lemma rbl_succ:
|
|
758 |
"!!bin. rbl_succ (rev (bin_to_bl n bin)) = rev (bin_to_bl n (Numeral.succ bin))"
|
|
759 |
apply (induct n, simp)
|
|
760 |
apply (unfold bin_to_bl_def)
|
|
761 |
apply clarsimp
|
|
762 |
apply (case_tac bin rule: bin_exhaust)
|
|
763 |
apply (case_tac b)
|
|
764 |
apply (clarsimp simp: bin_to_bl_aux_alt)+
|
|
765 |
done
|
|
766 |
|
|
767 |
lemma rbl_add:
|
|
768 |
"!!bina binb. rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) =
|
|
769 |
rev (bin_to_bl n (bina + binb))"
|
|
770 |
apply (induct n, simp)
|
|
771 |
apply (unfold bin_to_bl_def)
|
|
772 |
apply clarsimp
|
|
773 |
apply (case_tac bina rule: bin_exhaust)
|
|
774 |
apply (case_tac binb rule: bin_exhaust)
|
|
775 |
apply (case_tac b)
|
|
776 |
apply (case_tac [!] "ba")
|
|
777 |
apply (auto simp: rbl_succ succ_def bin_to_bl_aux_alt Let_def add_ac)
|
|
778 |
done
|
|
779 |
|
|
780 |
lemma rbl_add_app2:
|
|
781 |
"!!blb. length blb >= length bla ==>
|
|
782 |
rbl_add bla (blb @ blc) = rbl_add bla blb"
|
|
783 |
apply (induct bla, simp)
|
|
784 |
apply clarsimp
|
|
785 |
apply (case_tac blb, clarsimp)
|
|
786 |
apply (clarsimp simp: Let_def)
|
|
787 |
done
|
|
788 |
|
|
789 |
lemma rbl_add_take2:
|
|
790 |
"!!blb. length blb >= length bla ==>
|
|
791 |
rbl_add bla (take (length bla) blb) = rbl_add bla blb"
|
|
792 |
apply (induct bla, simp)
|
|
793 |
apply clarsimp
|
|
794 |
apply (case_tac blb, clarsimp)
|
|
795 |
apply (clarsimp simp: Let_def)
|
|
796 |
done
|
|
797 |
|
|
798 |
lemma rbl_add_long:
|
|
799 |
"m >= n ==> rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) =
|
|
800 |
rev (bin_to_bl n (bina + binb))"
|
|
801 |
apply (rule box_equals [OF _ rbl_add_take2 rbl_add])
|
|
802 |
apply (rule_tac f = "rbl_add (rev (bin_to_bl n bina))" in arg_cong)
|
|
803 |
apply (rule rev_swap [THEN iffD1])
|
|
804 |
apply (simp add: rev_take drop_bin2bl)
|
|
805 |
apply simp
|
|
806 |
done
|
|
807 |
|
|
808 |
lemma rbl_mult_app2:
|
|
809 |
"!!blb. length blb >= length bla ==>
|
|
810 |
rbl_mult bla (blb @ blc) = rbl_mult bla blb"
|
|
811 |
apply (induct bla, simp)
|
|
812 |
apply clarsimp
|
|
813 |
apply (case_tac blb, clarsimp)
|
|
814 |
apply (clarsimp simp: Let_def rbl_add_app2)
|
|
815 |
done
|
|
816 |
|
|
817 |
lemma rbl_mult_take2:
|
|
818 |
"length blb >= length bla ==>
|
|
819 |
rbl_mult bla (take (length bla) blb) = rbl_mult bla blb"
|
|
820 |
apply (rule trans)
|
|
821 |
apply (rule rbl_mult_app2 [symmetric])
|
|
822 |
apply simp
|
|
823 |
apply (rule_tac f = "rbl_mult bla" in arg_cong)
|
|
824 |
apply (rule append_take_drop_id)
|
|
825 |
done
|
|
826 |
|
|
827 |
lemma rbl_mult_gt1:
|
|
828 |
"m >= length bl ==> rbl_mult bl (rev (bin_to_bl m binb)) =
|
|
829 |
rbl_mult bl (rev (bin_to_bl (length bl) binb))"
|
|
830 |
apply (rule trans)
|
|
831 |
apply (rule rbl_mult_take2 [symmetric])
|
|
832 |
apply simp_all
|
|
833 |
apply (rule_tac f = "rbl_mult bl" in arg_cong)
|
|
834 |
apply (rule rev_swap [THEN iffD1])
|
|
835 |
apply (simp add: rev_take drop_bin2bl)
|
|
836 |
done
|
|
837 |
|
|
838 |
lemma rbl_mult_gt:
|
|
839 |
"m > n ==> rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) =
|
|
840 |
rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb))"
|
|
841 |
by (auto intro: trans [OF rbl_mult_gt1])
|
|
842 |
|
|
843 |
lemmas rbl_mult_Suc = lessI [THEN rbl_mult_gt]
|
|
844 |
|
|
845 |
lemma rbbl_Cons:
|
|
846 |
"b # rev (bin_to_bl n x) = rev (bin_to_bl (Suc n) (x BIT If b bit.B1 bit.B0))"
|
|
847 |
apply (unfold bin_to_bl_def)
|
|
848 |
apply simp
|
|
849 |
apply (simp add: bin_to_bl_aux_alt)
|
|
850 |
done
|
|
851 |
|
|
852 |
lemma rbl_mult: "!!bina binb.
|
|
853 |
rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) =
|
|
854 |
rev (bin_to_bl n (bina * binb))"
|
|
855 |
apply (induct n)
|
|
856 |
apply simp
|
|
857 |
apply (unfold bin_to_bl_def)
|
|
858 |
apply clarsimp
|
|
859 |
apply (case_tac bina rule: bin_exhaust)
|
|
860 |
apply (case_tac binb rule: bin_exhaust)
|
|
861 |
apply (case_tac b)
|
|
862 |
apply (case_tac [!] "ba")
|
|
863 |
apply (auto simp: bin_to_bl_aux_alt Let_def)
|
|
864 |
apply (auto simp: rbbl_Cons rbl_mult_Suc rbl_add)
|
|
865 |
done
|
|
866 |
|
|
867 |
lemma rbl_add_split:
|
|
868 |
"P (rbl_add (y # ys) (x # xs)) =
|
|
869 |
(ALL ws. length ws = length ys --> ws = rbl_add ys xs -->
|
|
870 |
(y --> ((x --> P (False # rbl_succ ws)) & (~ x --> P (True # ws)))) & \
|
|
871 |
(~ y --> P (x # ws)))"
|
|
872 |
apply (auto simp add: Let_def)
|
|
873 |
apply (case_tac [!] "y")
|
|
874 |
apply auto
|
|
875 |
done
|
|
876 |
|
|
877 |
lemma rbl_mult_split:
|
|
878 |
"P (rbl_mult (y # ys) xs) =
|
|
879 |
(ALL ws. length ws = Suc (length ys) --> ws = False # rbl_mult ys xs -->
|
|
880 |
(y --> P (rbl_add ws xs)) & (~ y --> P ws))"
|
|
881 |
by (clarsimp simp add : Let_def)
|
|
882 |
|
|
883 |
lemma and_len: "xs = ys ==> xs = ys & length xs = length ys"
|
|
884 |
by auto
|
|
885 |
|
|
886 |
lemma size_if: "size (if p then xs else ys) = (if p then size xs else size ys)"
|
|
887 |
by auto
|
|
888 |
|
|
889 |
lemma tl_if: "tl (if p then xs else ys) = (if p then tl xs else tl ys)"
|
|
890 |
by auto
|
|
891 |
|
|
892 |
lemma hd_if: "hd (if p then xs else ys) = (if p then hd xs else hd ys)"
|
|
893 |
by auto
|
|
894 |
|
|
895 |
lemma if_Not_x: "(if p then ~ x else x) = (p = (~ x))"
|
|
896 |
by auto
|
|
897 |
|
|
898 |
lemma if_x_Not: "(if p then x else ~ x) = (p = x)"
|
|
899 |
by auto
|
|
900 |
|
|
901 |
lemma if_same_and: "(If p x y & If p u v) = (if p then x & u else y & v)"
|
|
902 |
by auto
|
|
903 |
|
|
904 |
lemma if_same_eq: "(If p x y = (If p u v)) = (if p then x = (u) else y = (v))"
|
|
905 |
by auto
|
|
906 |
|
|
907 |
lemma if_same_eq_not:
|
|
908 |
"(If p x y = (~ If p u v)) = (if p then x = (~u) else y = (~v))"
|
|
909 |
by auto
|
|
910 |
|
|
911 |
(* note - if_Cons can cause blowup in the size, if p is complex,
|
|
912 |
so make a simproc *)
|
|
913 |
lemma if_Cons: "(if p then x # xs else y # ys) = If p x y # If p xs ys"
|
|
914 |
by auto
|
|
915 |
|
|
916 |
lemma if_single:
|
|
917 |
"(if xc then [xab] else [an]) = [if xc then xab else an]"
|
|
918 |
by auto
|
|
919 |
|
|
920 |
lemma if_bool_simps:
|
|
921 |
"If p True y = (p | y) & If p False y = (~p & y) &
|
|
922 |
If p y True = (p --> y) & If p y False = (p & y)"
|
|
923 |
by auto
|
|
924 |
|
|
925 |
lemmas if_simps = if_x_Not if_Not_x if_cancel if_True if_False if_bool_simps
|
|
926 |
|
|
927 |
lemmas seqr = eq_reflection [where x = "size ?w"]
|
|
928 |
|
|
929 |
lemmas tl_Nil = tl.simps (1)
|
|
930 |
lemmas tl_Cons = tl.simps (2)
|
|
931 |
|
|
932 |
|
24350
|
933 |
subsection "Repeated splitting or concatenation"
|
24333
|
934 |
|
|
935 |
lemma sclem:
|
|
936 |
"size (concat (map (bin_to_bl n) xs)) = length xs * n"
|
|
937 |
by (induct xs) auto
|
|
938 |
|
|
939 |
lemma bin_cat_foldl_lem [rule_format] :
|
|
940 |
"ALL x. foldl (%u. bin_cat u n) x xs =
|
|
941 |
bin_cat x (size xs * n) (foldl (%u. bin_cat u n) y xs)"
|
|
942 |
apply (induct xs)
|
|
943 |
apply simp
|
|
944 |
apply clarify
|
|
945 |
apply (simp (no_asm))
|
|
946 |
apply (frule asm_rl)
|
|
947 |
apply (drule spec)
|
|
948 |
apply (erule trans)
|
|
949 |
apply (drule_tac x = "bin_cat y n a" in spec)
|
|
950 |
apply (simp add : bin_cat_assoc_sym min_def)
|
|
951 |
done
|
|
952 |
|
|
953 |
lemma bin_rcat_bl:
|
|
954 |
"(bin_rcat n wl) = bl_to_bin (concat (map (bin_to_bl n) wl))"
|
|
955 |
apply (unfold bin_rcat_def)
|
|
956 |
apply (rule sym)
|
|
957 |
apply (induct wl)
|
|
958 |
apply (auto simp add : bl_to_bin_append)
|
|
959 |
apply (simp add : bl_to_bin_aux_alt sclem)
|
|
960 |
apply (simp add : bin_cat_foldl_lem [symmetric])
|
|
961 |
done
|
|
962 |
|
|
963 |
lemmas bin_rsplit_aux_simps = bin_rsplit_aux.simps bin_rsplitl_aux.simps
|
|
964 |
lemmas rsplit_aux_simps = bin_rsplit_aux_simps
|
|
965 |
|
|
966 |
lemmas th_if_simp1 = split_if [where P = "op = ?l",
|
|
967 |
THEN iffD1, THEN conjunct1, THEN mp, standard]
|
|
968 |
lemmas th_if_simp2 = split_if [where P = "op = ?l",
|
|
969 |
THEN iffD1, THEN conjunct2, THEN mp, standard]
|
|
970 |
|
|
971 |
lemmas rsplit_aux_simp1s = rsplit_aux_simps [THEN th_if_simp1]
|
|
972 |
|
|
973 |
lemmas rsplit_aux_simp2ls = rsplit_aux_simps [THEN th_if_simp2]
|
|
974 |
(* these safe to [simp add] as require calculating m - n *)
|
|
975 |
lemmas bin_rsplit_aux_simp2s [simp] = rsplit_aux_simp2ls [unfolded Let_def]
|
|
976 |
lemmas rbscl = bin_rsplit_aux_simp2s (2)
|
|
977 |
|
|
978 |
lemmas rsplit_aux_0_simps [simp] =
|
|
979 |
rsplit_aux_simp1s [OF disjI1] rsplit_aux_simp1s [OF disjI2]
|
|
980 |
|
|
981 |
lemma bin_rsplit_aux_append:
|
|
982 |
"bin_rsplit_aux (n, bs @ cs, m, c) = bin_rsplit_aux (n, bs, m, c) @ cs"
|
|
983 |
apply (rule_tac u=n and v=bs and w=m and x=c in bin_rsplit_aux.induct)
|
|
984 |
apply (subst bin_rsplit_aux.simps)
|
|
985 |
apply (subst bin_rsplit_aux.simps)
|
|
986 |
apply (clarsimp split: ls_splits)
|
|
987 |
done
|
|
988 |
|
|
989 |
lemma bin_rsplitl_aux_append:
|
|
990 |
"bin_rsplitl_aux (n, bs @ cs, m, c) = bin_rsplitl_aux (n, bs, m, c) @ cs"
|
|
991 |
apply (rule_tac u=n and v=bs and w=m and x=c in bin_rsplitl_aux.induct)
|
|
992 |
apply (subst bin_rsplitl_aux.simps)
|
|
993 |
apply (subst bin_rsplitl_aux.simps)
|
|
994 |
apply (clarsimp split: ls_splits)
|
|
995 |
done
|
|
996 |
|
|
997 |
lemmas rsplit_aux_apps [where bs = "[]"] =
|
|
998 |
bin_rsplit_aux_append bin_rsplitl_aux_append
|
|
999 |
|
|
1000 |
lemmas rsplit_def_auxs = bin_rsplit_def bin_rsplitl_def
|
|
1001 |
|
|
1002 |
lemmas rsplit_aux_alts = rsplit_aux_apps
|
|
1003 |
[unfolded append_Nil rsplit_def_auxs [symmetric]]
|
|
1004 |
|
|
1005 |
lemma bin_split_minus: "0 < n ==> bin_split (Suc (n - 1)) w = bin_split n w"
|
|
1006 |
by auto
|
|
1007 |
|
|
1008 |
lemmas bin_split_minus_simp =
|
|
1009 |
bin_split.Suc [THEN [2] bin_split_minus [symmetric, THEN trans], standard]
|
|
1010 |
|
|
1011 |
lemma bin_split_pred_simp [simp]:
|
|
1012 |
"(0::nat) < number_of bin \<Longrightarrow>
|
|
1013 |
bin_split (number_of bin) w =
|
|
1014 |
(let (w1, w2) = bin_split (number_of (Numeral.pred bin)) (bin_rest w)
|
|
1015 |
in (w1, w2 BIT bin_last w))"
|
|
1016 |
by (simp only: nobm1 bin_split_minus_simp)
|
|
1017 |
|
|
1018 |
declare bin_split_pred_simp [simp]
|
|
1019 |
|
|
1020 |
lemma bin_rsplit_aux_simp_alt:
|
|
1021 |
"bin_rsplit_aux (n, bs, m, c) =
|
|
1022 |
(if m = 0 \<or> n = 0
|
|
1023 |
then bs
|
|
1024 |
else let (a, b) = bin_split n c in bin_rsplit n (m - n, a) @ b # bs)"
|
|
1025 |
apply (rule trans)
|
|
1026 |
apply (subst bin_rsplit_aux.simps, rule refl)
|
|
1027 |
apply (simp add: rsplit_aux_alts)
|
|
1028 |
done
|
|
1029 |
|
|
1030 |
lemmas bin_rsplit_simp_alt =
|
|
1031 |
trans [OF bin_rsplit_def [THEN meta_eq_to_obj_eq]
|
|
1032 |
bin_rsplit_aux_simp_alt, standard]
|
|
1033 |
|
|
1034 |
lemmas bthrs = bin_rsplit_simp_alt [THEN [2] trans]
|
|
1035 |
|
|
1036 |
lemma bin_rsplit_size_sign' [rule_format] :
|
|
1037 |
"n > 0 ==> (ALL nw w. rev sw = bin_rsplit n (nw, w) -->
|
|
1038 |
(ALL v: set sw. bintrunc n v = v))"
|
|
1039 |
apply (induct sw)
|
|
1040 |
apply clarsimp
|
|
1041 |
apply clarsimp
|
|
1042 |
apply (drule bthrs)
|
|
1043 |
apply (simp (no_asm_use) add: Let_def split: ls_splits)
|
|
1044 |
apply clarify
|
|
1045 |
apply (erule impE, rule exI, erule exI)
|
|
1046 |
apply (drule split_bintrunc)
|
|
1047 |
apply simp
|
|
1048 |
done
|
|
1049 |
|
|
1050 |
lemmas bin_rsplit_size_sign = bin_rsplit_size_sign' [OF asm_rl
|
|
1051 |
rev_rev_ident [THEN trans] set_rev [THEN equalityD2 [THEN subsetD]],
|
|
1052 |
standard]
|
|
1053 |
|
|
1054 |
lemma bin_nth_rsplit [rule_format] :
|
|
1055 |
"n > 0 ==> m < n ==> (ALL w k nw. rev sw = bin_rsplit n (nw, w) -->
|
|
1056 |
k < size sw --> bin_nth (sw ! k) m = bin_nth w (k * n + m))"
|
|
1057 |
apply (induct sw)
|
|
1058 |
apply clarsimp
|
|
1059 |
apply clarsimp
|
|
1060 |
apply (drule bthrs)
|
|
1061 |
apply (simp (no_asm_use) add: Let_def split: ls_splits)
|
|
1062 |
apply clarify
|
|
1063 |
apply (erule allE, erule impE, erule exI)
|
|
1064 |
apply (case_tac k)
|
|
1065 |
apply clarsimp
|
|
1066 |
prefer 2
|
|
1067 |
apply clarsimp
|
|
1068 |
apply (erule allE)
|
|
1069 |
apply (erule (1) impE)
|
|
1070 |
apply (drule bin_nth_split, erule conjE, erule allE,
|
|
1071 |
erule trans, simp add : add_ac)+
|
|
1072 |
done
|
|
1073 |
|
|
1074 |
lemma bin_rsplit_all:
|
|
1075 |
"0 < nw ==> nw <= n ==> bin_rsplit n (nw, w) = [bintrunc n w]"
|
|
1076 |
unfolding bin_rsplit_def
|
|
1077 |
by (clarsimp dest!: split_bintrunc simp: rsplit_aux_simp2ls split: ls_splits)
|
|
1078 |
|
|
1079 |
lemma bin_rsplit_l [rule_format] :
|
|
1080 |
"ALL bin. bin_rsplitl n (m, bin) = bin_rsplit n (m, bintrunc m bin)"
|
|
1081 |
apply (rule_tac a = "m" in wf_less_than [THEN wf_induct])
|
|
1082 |
apply (simp (no_asm) add : bin_rsplitl_def bin_rsplit_def)
|
|
1083 |
apply (rule allI)
|
|
1084 |
apply (subst bin_rsplitl_aux.simps)
|
|
1085 |
apply (subst bin_rsplit_aux.simps)
|
|
1086 |
apply (clarsimp simp: rsplit_aux_alts Let_def split: ls_splits)
|
|
1087 |
apply (drule bin_split_trunc)
|
|
1088 |
apply (drule sym [THEN trans], assumption)
|
|
1089 |
apply fast
|
|
1090 |
done
|
|
1091 |
|
|
1092 |
lemma bin_rsplit_rcat [rule_format] :
|
|
1093 |
"n > 0 --> bin_rsplit n (n * size ws, bin_rcat n ws) = map (bintrunc n) ws"
|
|
1094 |
apply (unfold bin_rsplit_def bin_rcat_def)
|
|
1095 |
apply (rule_tac xs = "ws" in rev_induct)
|
|
1096 |
apply clarsimp
|
|
1097 |
apply clarsimp
|
|
1098 |
apply (clarsimp simp add: bin_split_cat rsplit_aux_alts)
|
|
1099 |
done
|
|
1100 |
|
|
1101 |
lemma bin_rsplit_aux_len_le [rule_format] :
|
|
1102 |
"ALL ws m. n > 0 --> ws = bin_rsplit_aux (n, bs, nw, w) -->
|
|
1103 |
(length ws <= m) = (nw + length bs * n <= m * n)"
|
|
1104 |
apply (rule_tac u=n and v=bs and w=nw and x=w in bin_rsplit_aux.induct)
|
|
1105 |
apply (subst bin_rsplit_aux.simps)
|
|
1106 |
apply (clarsimp simp: Let_def split: ls_splits)
|
|
1107 |
apply (erule lrlem)
|
|
1108 |
done
|
|
1109 |
|
|
1110 |
lemma bin_rsplit_len_le:
|
|
1111 |
"n > 0 --> ws = bin_rsplit n (nw, w) --> (length ws <= m) = (nw <= m * n)"
|
|
1112 |
unfolding bin_rsplit_def by (clarsimp simp add : bin_rsplit_aux_len_le)
|
|
1113 |
|
|
1114 |
lemma bin_rsplit_aux_len [rule_format] :
|
|
1115 |
"0 < n --> length (bin_rsplit_aux (n, cs, nw, w)) =
|
|
1116 |
(nw + n - 1) div n + length cs"
|
|
1117 |
apply (rule_tac u=n and v=cs and w=nw and x=w in bin_rsplit_aux.induct)
|
|
1118 |
apply (subst bin_rsplit_aux.simps)
|
|
1119 |
apply (clarsimp simp: Let_def split: ls_splits)
|
|
1120 |
apply (erule thin_rl)
|
|
1121 |
apply (case_tac "m <= n")
|
|
1122 |
prefer 2
|
|
1123 |
apply (simp add: div_add_self2 [symmetric])
|
|
1124 |
apply (case_tac m, clarsimp)
|
|
1125 |
apply (simp add: div_add_self2)
|
|
1126 |
done
|
|
1127 |
|
|
1128 |
lemma bin_rsplit_len:
|
|
1129 |
"0 < n ==> length (bin_rsplit n (nw, w)) = (nw + n - 1) div n"
|
|
1130 |
unfolding bin_rsplit_def by (clarsimp simp add : bin_rsplit_aux_len)
|
|
1131 |
|
|
1132 |
lemma bin_rsplit_aux_len_indep [rule_format] :
|
|
1133 |
"0 < n ==> (ALL v bs. length bs = length cs -->
|
|
1134 |
length (bin_rsplit_aux (n, bs, nw, v)) =
|
|
1135 |
length (bin_rsplit_aux (n, cs, nw, w)))"
|
|
1136 |
apply (rule_tac u=n and v=cs and w=nw and x=w in bin_rsplit_aux.induct)
|
|
1137 |
apply clarsimp
|
|
1138 |
apply (simp (no_asm_simp) add: bin_rsplit_aux_simp_alt Let_def
|
|
1139 |
split: ls_splits)
|
|
1140 |
apply clarify
|
|
1141 |
apply (erule allE)+
|
|
1142 |
apply (erule impE)
|
|
1143 |
apply (fast elim!: sym)
|
|
1144 |
apply (simp (no_asm_use) add: rsplit_aux_alts)
|
|
1145 |
apply (erule impE)
|
|
1146 |
apply (rule_tac x="ba # bs" in exI)
|
|
1147 |
apply auto
|
|
1148 |
done
|
|
1149 |
|
|
1150 |
lemma bin_rsplit_len_indep:
|
|
1151 |
"0 < n ==> length (bin_rsplit n (nw, v)) = length (bin_rsplit n (nw, w))"
|
|
1152 |
apply (unfold bin_rsplit_def)
|
|
1153 |
apply (erule bin_rsplit_aux_len_indep)
|
|
1154 |
apply (rule refl)
|
|
1155 |
done
|
|
1156 |
|
|
1157 |
end
|
|
1158 |
|