1 (* Title: HOL/Library/Word.thy
3 Author: Sebastian Skalberg (TU Muenchen)
6 header {* Binary Words *}
12 subsection {* Auxilary Lemmas *}
14 lemma max_le [intro!]: "[| x \<le> z; y \<le> z |] ==> max x y \<le> z"
15 by (simp add: max_def)
18 fixes x :: "'a::linorder"
20 shows "max (f x) (f y) \<le> f (max x y)"
22 from mf and le_maxI1 [of x y]
23 have fx: "f x \<le> f (max x y)"
25 from mf and le_maxI2 [of y x]
26 have fy: "f y \<le> f (max x y)"
29 show "max (f x) (f y) \<le> f (max x y)"
33 declare zero_le_power [intro]
34 and zero_less_power [intro]
36 lemma int_nat_two_exp: "2 ^ k = int (2 ^ k)"
37 by (simp add: zpower_int [symmetric])
46 bitval :: "bit => nat"
53 bitnot :: "bit => bit"
54 bitand :: "bit => bit => bit" (infixr "bitand" 35)
55 bitor :: "bit => bit => bit" (infixr "bitor" 30)
56 bitxor :: "bit => bit => bit" (infixr "bitxor" 30)
59 bitnot ("\<not>\<^sub>b _" [40] 40) and
60 bitand (infixr "\<and>\<^sub>b" 35) and
61 bitor (infixr "\<or>\<^sub>b" 30) and
62 bitxor (infixr "\<oplus>\<^sub>b" 30)
64 notation (HTML output)
65 bitnot ("\<not>\<^sub>b _" [40] 40) and
66 bitand (infixr "\<and>\<^sub>b" 35) and
67 bitor (infixr "\<or>\<^sub>b" 30) and
68 bitxor (infixr "\<oplus>\<^sub>b" 30)
71 bitnot_zero: "(bitnot \<zero>) = \<one>"
72 bitnot_one : "(bitnot \<one>) = \<zero>"
75 bitand_zero: "(\<zero> bitand y) = \<zero>"
76 bitand_one: "(\<one> bitand y) = y"
79 bitor_zero: "(\<zero> bitor y) = y"
80 bitor_one: "(\<one> bitor y) = \<one>"
83 bitxor_zero: "(\<zero> bitxor y) = y"
84 bitxor_one: "(\<one> bitxor y) = (bitnot y)"
86 lemma bitnot_bitnot [simp]: "(bitnot (bitnot b)) = b"
89 lemma bitand_cancel [simp]: "(b bitand b) = b"
92 lemma bitor_cancel [simp]: "(b bitor b) = b"
95 lemma bitxor_cancel [simp]: "(b bitxor b) = \<zero>"
98 subsection {* Bit Vectors *}
100 text {* First, a couple of theorems expressing case analysis and
101 induction principles for bit vectors. *}
103 lemma bit_list_cases:
104 assumes empty: "w = [] ==> P w"
105 and zero: "!!bs. w = \<zero> # bs ==> P w"
106 and one: "!!bs. w = \<one> # bs ==> P w"
114 assume [simp]: "w = b # bs"
118 hence "w = \<zero> # bs"
124 hence "w = \<one> # bs"
131 lemma bit_list_induct:
132 assumes empty: "P []"
133 and zero: "!!bs. P bs ==> P (\<zero>#bs)"
134 and one: "!!bs. P bs ==> P (\<one>#bs)"
136 proof (induct w,simp_all add: empty)
138 assume [intro!]: "P bs"
140 by (cases b,auto intro!: zero one)
144 bv_msb :: "bit list => bit" where
145 "bv_msb w = (if w = [] then \<zero> else hd w)"
148 bv_extend :: "[nat,bit,bit list]=>bit list" where
149 "bv_extend i b w = (replicate (i - length w) b) @ w"
152 bv_not :: "bit list => bit list" where
153 "bv_not w = map bitnot w"
155 lemma bv_length_extend [simp]: "length w \<le> i ==> length (bv_extend i b w) = i"
156 by (simp add: bv_extend_def)
158 lemma bv_not_Nil [simp]: "bv_not [] = []"
159 by (simp add: bv_not_def)
161 lemma bv_not_Cons [simp]: "bv_not (b#bs) = (bitnot b) # bv_not bs"
162 by (simp add: bv_not_def)
164 lemma bv_not_bv_not [simp]: "bv_not (bv_not w) = w"
165 by (rule bit_list_induct [of _ w],simp_all)
167 lemma bv_msb_Nil [simp]: "bv_msb [] = \<zero>"
168 by (simp add: bv_msb_def)
170 lemma bv_msb_Cons [simp]: "bv_msb (b#bs) = b"
171 by (simp add: bv_msb_def)
173 lemma bv_msb_bv_not [simp]: "0 < length w ==> bv_msb (bv_not w) = (bitnot (bv_msb w))"
174 by (cases w,simp_all)
176 lemma bv_msb_one_length [simp,intro]: "bv_msb w = \<one> ==> 0 < length w"
177 by (cases w,simp_all)
179 lemma length_bv_not [simp]: "length (bv_not w) = length w"
180 by (induct w,simp_all)
183 bv_to_nat :: "bit list => nat" where
184 "bv_to_nat = foldl (%bn b. 2 * bn + bitval b) 0"
186 lemma bv_to_nat_Nil [simp]: "bv_to_nat [] = 0"
187 by (simp add: bv_to_nat_def)
189 lemma bv_to_nat_helper [simp]: "bv_to_nat (b # bs) = bitval b * 2 ^ length bs + bv_to_nat bs"
191 let ?bv_to_nat' = "foldl (\<lambda>bn b. 2 * bn + bitval b)"
192 have helper: "\<And>base. ?bv_to_nat' base bs = base * 2 ^ length bs + ?bv_to_nat' 0 bs"
194 case Nil show ?case by simp
196 case (Cons x xs base)
198 apply (simp only: foldl.simps)
199 apply (subst Cons [of "2 * base + bitval x"])
201 apply (subst Cons [of "bitval x"])
202 apply (simp add: add_mult_distrib)
205 show ?thesis by (simp add: bv_to_nat_def) (rule helper)
208 lemma bv_to_nat0 [simp]: "bv_to_nat (\<zero>#bs) = bv_to_nat bs"
211 lemma bv_to_nat1 [simp]: "bv_to_nat (\<one>#bs) = 2 ^ length bs + bv_to_nat bs"
214 lemma bv_to_nat_upper_range: "bv_to_nat w < 2 ^ length w"
215 proof (induct w,simp_all)
217 assume "bv_to_nat bs < 2 ^ length bs"
218 show "bitval b * 2 ^ length bs + bv_to_nat bs < 2 * 2 ^ length bs"
219 proof (cases b,simp_all)
220 have "bv_to_nat bs < 2 ^ length bs"
222 also have "... < 2 * 2 ^ length bs"
224 finally show "bv_to_nat bs < 2 * 2 ^ length bs"
227 have "bv_to_nat bs < 2 ^ length bs"
229 hence "2 ^ length bs + bv_to_nat bs < 2 ^ length bs + 2 ^ length bs"
231 also have "... = 2 * (2 ^ length bs)"
233 finally show "bv_to_nat bs < 2 ^ length bs"
238 lemma bv_extend_longer [simp]:
239 assumes wn: "n \<le> length w"
240 shows "bv_extend n b w = w"
241 by (simp add: bv_extend_def wn)
243 lemma bv_extend_shorter [simp]:
244 assumes wn: "length w < n"
245 shows "bv_extend n b w = bv_extend n b (b#w)"
248 have s: "n - Suc (length w) + 1 = n - length w"
250 have "bv_extend n b w = replicate (n - length w) b @ w"
251 by (simp add: bv_extend_def)
252 also have "... = replicate (n - Suc (length w) + 1) b @ w"
254 also have "... = (replicate (n - Suc (length w)) b @ replicate 1 b) @ w"
255 by (subst replicate_add,rule)
256 also have "... = replicate (n - Suc (length w)) b @ b # w"
258 also have "... = bv_extend n b (b#w)"
259 by (simp add: bv_extend_def)
260 finally show "bv_extend n b w = bv_extend n b (b#w)"
265 rem_initial :: "bit => bit list => bit list"
268 "rem_initial b [] = []"
269 "rem_initial b (x#xs) = (if b = x then rem_initial b xs else x#xs)"
271 lemma rem_initial_length: "length (rem_initial b w) \<le> length w"
272 by (rule bit_list_induct [of _ w],simp_all (no_asm),safe,simp_all)
274 lemma rem_initial_equal:
275 assumes p: "length (rem_initial b w) = length w"
276 shows "rem_initial b w = w"
278 have "length (rem_initial b w) = length w --> rem_initial b w = w"
279 proof (induct w,simp_all,clarify)
281 assume "length (rem_initial b xs) = length xs --> rem_initial b xs = xs"
282 assume f: "length (rem_initial b xs) = Suc (length xs)"
283 with rem_initial_length [of b xs]
284 show "rem_initial b xs = b#xs"
291 lemma bv_extend_rem_initial: "bv_extend (length w) b (rem_initial b w) = w"
292 proof (induct w,simp_all,safe)
294 assume ind: "bv_extend (length xs) b (rem_initial b xs) = xs"
295 from rem_initial_length [of b xs]
296 have [simp]: "Suc (length xs) - length (rem_initial b xs) = 1 + (length xs - length (rem_initial b xs))"
298 have "bv_extend (Suc (length xs)) b (rem_initial b xs) = replicate (Suc (length xs) - length (rem_initial b xs)) b @ (rem_initial b xs)"
299 by (simp add: bv_extend_def)
300 also have "... = replicate (1 + (length xs - length (rem_initial b xs))) b @ rem_initial b xs"
302 also have "... = (replicate 1 b @ replicate (length xs - length (rem_initial b xs)) b) @ rem_initial b xs"
303 by (subst replicate_add,rule refl)
304 also have "... = b # bv_extend (length xs) b (rem_initial b xs)"
305 by (auto simp add: bv_extend_def [symmetric])
306 also have "... = b # xs"
308 finally show "bv_extend (Suc (length xs)) b (rem_initial b xs) = b # xs"
312 lemma rem_initial_append1:
313 assumes "rem_initial b xs ~= []"
314 shows "rem_initial b (xs @ ys) = rem_initial b xs @ ys"
316 have "rem_initial b xs ~= [] --> rem_initial b (xs @ ys) = rem_initial b xs @ ys" (is "?P xs ys")
322 lemma rem_initial_append2:
323 assumes "rem_initial b xs = []"
324 shows "rem_initial b (xs @ ys) = rem_initial b ys"
326 have "rem_initial b xs = [] --> rem_initial b (xs @ ys) = rem_initial b ys" (is "?P xs ys")
333 norm_unsigned :: "bit list => bit list" where
334 "norm_unsigned = rem_initial \<zero>"
336 lemma norm_unsigned_Nil [simp]: "norm_unsigned [] = []"
337 by (simp add: norm_unsigned_def)
339 lemma norm_unsigned_Cons0 [simp]: "norm_unsigned (\<zero>#bs) = norm_unsigned bs"
340 by (simp add: norm_unsigned_def)
342 lemma norm_unsigned_Cons1 [simp]: "norm_unsigned (\<one>#bs) = \<one>#bs"
343 by (simp add: norm_unsigned_def)
345 lemma norm_unsigned_idem [simp]: "norm_unsigned (norm_unsigned w) = norm_unsigned w"
346 by (rule bit_list_induct [of _ w],simp_all)
349 nat_to_bv_helper :: "nat => bit list => bit list"
351 recdef nat_to_bv_helper "measure (\<lambda>n. n)"
352 "nat_to_bv_helper n = (%bs. (if n = 0 then bs
353 else nat_to_bv_helper (n div 2) ((if n mod 2 = 0 then \<zero> else \<one>)#bs)))"
356 nat_to_bv :: "nat => bit list" where
357 "nat_to_bv n = nat_to_bv_helper n []"
359 lemma nat_to_bv0 [simp]: "nat_to_bv 0 = []"
360 by (simp add: nat_to_bv_def)
362 lemmas [simp del] = nat_to_bv_helper.simps
365 assumes zero: "(n::nat) = 0 ==> R"
366 and div : "[| n div 2 < n ; 0 < n |] ==> R"
368 proof (cases "n = 0")
383 lemma int_wf_ge_induct:
384 assumes ind : "!!i::int. (!!j. [| k \<le> j ; j < i |] ==> P j) ==> P i"
386 proof (rule wf_induct_rule [OF wf_int_ge_less_than])
388 assume ih: "(\<And>y\<Colon>int. (y, x) \<in> int_ge_less_than k \<Longrightarrow> P y)"
390 by (rule ind, simp add: int_ge_less_than_def)
393 lemma unfold_nat_to_bv_helper:
394 "nat_to_bv_helper b l = nat_to_bv_helper b [] @ l"
396 have "\<forall>l. nat_to_bv_helper b l = nat_to_bv_helper b [] @ l"
397 proof (induct b rule: less_induct)
399 assume ind: "!!j. j < n \<Longrightarrow> \<forall> l. nat_to_bv_helper j l = nat_to_bv_helper j [] @ l"
400 show "\<forall>l. nat_to_bv_helper n l = nat_to_bv_helper n [] @ l"
403 show "nat_to_bv_helper n l = nat_to_bv_helper n [] @ l"
404 proof (cases "n < 0")
407 by (simp add: nat_to_bv_helper.simps)
411 proof (rule n_div_2_cases [of n])
412 assume [simp]: "n = 0"
414 apply (simp only: nat_to_bv_helper.simps [of n])
418 assume n2n: "n div 2 < n"
419 assume [simp]: "0 < n"
420 hence n20: "0 \<le> n div 2"
422 from ind [of "n div 2"] and n2n n20
423 have ind': "\<forall>l. nat_to_bv_helper (n div 2) l = nat_to_bv_helper (n div 2) [] @ l"
426 apply (simp only: nat_to_bv_helper.simps [of n])
429 apply (simp only: if_False)
431 apply (subst spec [OF ind',of "\<zero>#l"])
432 apply (subst spec [OF ind',of "\<one>#l"])
433 apply (subst spec [OF ind',of "[\<one>]"])
434 apply (subst spec [OF ind',of "[\<zero>]"])
445 lemma nat_to_bv_non0 [simp]: "0 < n ==> nat_to_bv n = nat_to_bv (n div 2) @ [if n mod 2 = 0 then \<zero> else \<one>]"
447 assume [simp]: "0 < n"
449 apply (subst nat_to_bv_def [of n])
450 apply (simp only: nat_to_bv_helper.simps [of n])
451 apply (subst unfold_nat_to_bv_helper)
454 apply (subst nat_to_bv_def [of "n div 2"])
459 lemma bv_to_nat_dist_append: "bv_to_nat (l1 @ l2) = bv_to_nat l1 * 2 ^ length l2 + bv_to_nat l2"
461 have "\<forall>l2. bv_to_nat (l1 @ l2) = bv_to_nat l1 * 2 ^ length l2 + bv_to_nat l2"
462 proof (induct l1,simp_all)
464 assume ind: "\<forall>l2. bv_to_nat (xs @ l2) = bv_to_nat xs * 2 ^ length l2 + bv_to_nat l2"
465 show "\<forall>l2. bitval x * 2 ^ (length xs + length l2) + bv_to_nat xs * 2 ^ length l2 = (bitval x * 2 ^ length xs + bv_to_nat xs) * 2 ^ length l2"
468 show "bitval x * 2 ^ (length xs + length l2) + bv_to_nat xs * 2 ^ length l2 = (bitval x * 2 ^ length xs + bv_to_nat xs) * 2 ^ length l2"
470 have "(2::nat) ^ (length xs + length l2) = 2 ^ length xs * 2 ^ length l2"
471 by (induct "length xs",simp_all)
472 hence "bitval x * 2 ^ (length xs + length l2) + bv_to_nat xs * 2 ^ length l2 =
473 bitval x * 2 ^ length xs * 2 ^ length l2 + bv_to_nat xs * 2 ^ length l2"
475 also have "... = (bitval x * 2 ^ length xs + bv_to_nat xs) * 2 ^ length l2"
476 by (simp add: ring_distrib)
477 finally show ?thesis .
485 lemma bv_nat_bv [simp]: "bv_to_nat (nat_to_bv n) = n"
486 proof (induct n rule: less_induct)
488 assume ind: "!!j. j < n \<Longrightarrow> bv_to_nat (nat_to_bv j) = j"
489 show "bv_to_nat (nat_to_bv n) = n"
490 proof (rule n_div_2_cases [of n])
491 assume [simp]: "n = 0"
495 assume nn: "n div 2 < n"
498 have ind': "bv_to_nat (nat_to_bv (n div 2)) = n div 2"
500 from n0 have n0': "n \<noteq> 0"
503 apply (subst nat_to_bv_def)
504 apply (simp only: nat_to_bv_helper.simps [of n])
505 apply (simp only: n0' if_False)
506 apply (subst unfold_nat_to_bv_helper)
507 apply (subst bv_to_nat_dist_append)
508 apply (fold nat_to_bv_def)
509 apply (simp add: ind' split del: split_if)
510 apply (cases "n mod 2 = 0")
513 with mod_div_equality [of n 2]
514 show "n div 2 * 2 = n"
517 assume "n mod 2 = Suc 0"
518 with mod_div_equality [of n 2]
519 show "Suc (n div 2 * 2) = n"
525 lemma bv_to_nat_type [simp]: "bv_to_nat (norm_unsigned w) = bv_to_nat w"
526 by (rule bit_list_induct,simp_all)
528 lemma length_norm_unsigned_le [simp]: "length (norm_unsigned w) \<le> length w"
529 by (rule bit_list_induct,simp_all)
531 lemma bv_to_nat_rew_msb: "bv_msb w = \<one> ==> bv_to_nat w = 2 ^ (length w - 1) + bv_to_nat (tl w)"
532 by (rule bit_list_cases [of w],simp_all)
534 lemma norm_unsigned_result: "norm_unsigned xs = [] \<or> bv_msb (norm_unsigned xs) = \<one>"
535 proof (rule length_induct [of _ xs])
537 assume ind: "\<forall>ys. length ys < length xs --> norm_unsigned ys = [] \<or> bv_msb (norm_unsigned ys) = \<one>"
538 show "norm_unsigned xs = [] \<or> bv_msb (norm_unsigned xs) = \<one>"
539 proof (rule bit_list_cases [of xs],simp_all)
541 assume [simp]: "xs = \<zero>#bs"
543 have "length bs < length xs --> norm_unsigned bs = [] \<or> bv_msb (norm_unsigned bs) = \<one>"
545 thus "norm_unsigned bs = [] \<or> bv_msb (norm_unsigned bs) = \<one>"
550 lemma norm_empty_bv_to_nat_zero:
551 assumes nw: "norm_unsigned w = []"
552 shows "bv_to_nat w = 0"
554 have "bv_to_nat w = bv_to_nat (norm_unsigned w)"
556 also have "... = bv_to_nat []"
560 finally show ?thesis .
563 lemma bv_to_nat_lower_limit:
564 assumes w0: "0 < bv_to_nat w"
565 shows "2 ^ (length (norm_unsigned w) - 1) \<le> bv_to_nat w"
567 from w0 and norm_unsigned_result [of w]
568 have msbw: "bv_msb (norm_unsigned w) = \<one>"
569 by (auto simp add: norm_empty_bv_to_nat_zero)
570 have "2 ^ (length (norm_unsigned w) - 1) \<le> bv_to_nat (norm_unsigned w)"
571 by (subst bv_to_nat_rew_msb [OF msbw],simp)
576 lemmas [simp del] = nat_to_bv_non0
578 lemma norm_unsigned_length [intro!]: "length (norm_unsigned w) \<le> length w"
579 by (subst norm_unsigned_def,rule rem_initial_length)
581 lemma norm_unsigned_equal: "length (norm_unsigned w) = length w ==> norm_unsigned w = w"
582 by (simp add: norm_unsigned_def,rule rem_initial_equal)
584 lemma bv_extend_norm_unsigned: "bv_extend (length w) \<zero> (norm_unsigned w) = w"
585 by (simp add: norm_unsigned_def,rule bv_extend_rem_initial)
587 lemma norm_unsigned_append1 [simp]: "norm_unsigned xs \<noteq> [] ==> norm_unsigned (xs @ ys) = norm_unsigned xs @ ys"
588 by (simp add: norm_unsigned_def,rule rem_initial_append1)
590 lemma norm_unsigned_append2 [simp]: "norm_unsigned xs = [] ==> norm_unsigned (xs @ ys) = norm_unsigned ys"
591 by (simp add: norm_unsigned_def,rule rem_initial_append2)
593 lemma bv_to_nat_zero_imp_empty [rule_format]:
594 "bv_to_nat w = 0 \<longrightarrow> norm_unsigned w = []"
595 by (rule bit_list_induct [of _ w],simp_all)
597 lemma bv_to_nat_nzero_imp_nempty:
598 assumes "bv_to_nat w \<noteq> 0"
599 shows "norm_unsigned w \<noteq> []"
601 have "bv_to_nat w \<noteq> 0 --> norm_unsigned w \<noteq> []"
602 by (rule bit_list_induct [of _ w],simp_all)
608 assumes ass: "nat_to_bv (bv_to_nat w) = norm_unsigned w"
609 shows "nat_to_bv (2 * bv_to_nat w + bitval x) = norm_unsigned (w @ [x])"
611 assume [simp]: "x = \<one>"
613 apply (simp add: nat_to_bv_non0)
617 assume "Suc (2 * bv_to_nat w) = 2 * q"
618 hence orig: "(2 * bv_to_nat w + 1) mod 2 = 2 * q mod 2" (is "?lhs = ?rhs")
620 have "?lhs = (1 + 2 * bv_to_nat w) mod 2"
621 by (simp add: add_commute)
623 by (subst mod_add1_eq) simp
624 finally have eq1: "?lhs = 1" .
628 show "nat_to_bv (Suc (2 * bv_to_nat w) div 2) @ [\<zero>] = norm_unsigned (w @ [\<one>])"
631 have "nat_to_bv (Suc (2 * bv_to_nat w) div 2) @ [\<one>] = nat_to_bv ((1 + 2 * bv_to_nat w) div 2) @ [\<one>]"
632 by (simp add: add_commute)
633 also have "... = nat_to_bv (bv_to_nat w) @ [\<one>]"
634 by (subst div_add1_eq,simp)
635 also have "... = norm_unsigned w @ [\<one>]"
636 by (subst ass,rule refl)
637 also have "... = norm_unsigned (w @ [\<one>])"
638 by (cases "norm_unsigned w",simp_all)
639 finally show "nat_to_bv (Suc (2 * bv_to_nat w) div 2) @ [\<one>] = norm_unsigned (w @ [\<one>])"
643 assume [simp]: "x = \<zero>"
645 proof (cases "bv_to_nat w = 0")
646 assume "bv_to_nat w = 0"
648 by (simp add: bv_to_nat_zero_imp_empty)
650 assume "bv_to_nat w \<noteq> 0"
653 apply (subst nat_to_bv_non0)
657 apply (cases "norm_unsigned w")
658 apply (simp_all add: norm_empty_bv_to_nat_zero)
663 lemma nat_helper2: "nat_to_bv (2 ^ length xs + bv_to_nat xs) = \<one> # xs"
665 have "\<forall>xs. nat_to_bv (2 ^ length (rev xs) + bv_to_nat (rev xs)) = \<one> # (rev xs)" (is "\<forall>xs. ?P xs")
669 proof (rule length_induct [of _ xs])
671 assume ind: "\<forall>ys. length ys < length xs --> ?P ys"
674 assume [simp]: "xs = []"
676 by (simp add: nat_to_bv_non0)
679 assume [simp]: "xs = y # ys"
682 apply (subst bv_to_nat_dist_append)
685 have "nat_to_bv (2 * 2 ^ length ys + (bv_to_nat (rev ys) * 2 + bitval y)) =
686 nat_to_bv (2 * (2 ^ length ys + bv_to_nat (rev ys)) + bitval y)"
687 by (simp add: add_ac mult_ac)
688 also have "... = nat_to_bv (2 * (bv_to_nat (\<one>#rev ys)) + bitval y)"
690 also have "... = norm_unsigned (\<one>#rev ys) @ [y]"
693 have "nat_to_bv (2 ^ length (rev ys) + bv_to_nat (rev ys)) = \<one> # rev ys"
695 hence [simp]: "nat_to_bv (2 ^ length ys + bv_to_nat (rev ys)) = \<one> # rev ys"
698 apply (subst nat_helper1)
702 also have "... = (\<one>#rev ys) @ [y]"
704 also have "... = \<one> # rev ys @ [y]"
706 finally show "nat_to_bv (2 * 2 ^ length ys + (bv_to_nat (rev ys) * 2 + bitval y)) = \<one> # rev ys @ [y]"
712 hence "nat_to_bv (2 ^ length (rev (rev xs)) + bv_to_nat (rev (rev xs))) = \<one> # rev (rev xs)"
718 lemma nat_bv_nat [simp]: "nat_to_bv (bv_to_nat w) = norm_unsigned w"
719 proof (rule bit_list_induct [of _ w],simp_all)
721 assume "nat_to_bv (bv_to_nat xs) = norm_unsigned xs"
722 have "bv_to_nat xs = bv_to_nat (norm_unsigned xs)"
724 have "bv_to_nat xs < 2 ^ length xs"
725 by (rule bv_to_nat_upper_range)
726 show "nat_to_bv (2 ^ length xs + bv_to_nat xs) = \<one> # xs"
727 by (rule nat_helper2)
730 lemma bv_to_nat_qinj:
731 assumes one: "bv_to_nat xs = bv_to_nat ys"
732 and len: "length xs = length ys"
736 have "nat_to_bv (bv_to_nat xs) = nat_to_bv (bv_to_nat ys)"
738 hence xsys: "norm_unsigned xs = norm_unsigned ys"
740 have "xs = bv_extend (length xs) \<zero> (norm_unsigned xs)"
741 by (simp add: bv_extend_norm_unsigned)
742 also have "... = bv_extend (length ys) \<zero> (norm_unsigned ys)"
743 by (simp add: xsys len)
745 by (simp add: bv_extend_norm_unsigned)
746 finally show ?thesis .
749 lemma norm_unsigned_nat_to_bv [simp]:
750 "norm_unsigned (nat_to_bv n) = nat_to_bv n"
752 have "norm_unsigned (nat_to_bv n) = nat_to_bv (bv_to_nat (norm_unsigned (nat_to_bv n)))"
753 by (subst nat_bv_nat,simp)
754 also have "... = nat_to_bv n"
756 finally show ?thesis .
759 lemma length_nat_to_bv_upper_limit:
760 assumes nk: "n \<le> 2 ^ k - 1"
761 shows "length (nat_to_bv n) \<le> k"
762 proof (cases "n = 0")
765 by (simp add: nat_to_bv_def nat_to_bv_helper.simps)
768 hence n0: "0 < n" by simp
771 assume "~ length (nat_to_bv n) \<le> k"
772 hence "k < length (nat_to_bv n)"
774 hence "k \<le> length (nat_to_bv n) - 1"
776 hence "(2::nat) ^ k \<le> 2 ^ (length (nat_to_bv n) - 1)"
778 also have "... = 2 ^ (length (norm_unsigned (nat_to_bv n)) - 1)"
780 also have "... \<le> bv_to_nat (nat_to_bv n)"
781 by (rule bv_to_nat_lower_limit,simp add: n0)
784 finally have "2 ^ k \<le> n" .
794 lemma length_nat_to_bv_lower_limit:
795 assumes nk: "2 ^ k \<le> n"
796 shows "k < length (nat_to_bv n)"
798 assume "~ k < length (nat_to_bv n)"
799 hence lnk: "length (nat_to_bv n) \<le> k"
801 have "n = bv_to_nat (nat_to_bv n)"
803 also have "... < 2 ^ length (nat_to_bv n)"
804 by (rule bv_to_nat_upper_range)
805 also from lnk have "... \<le> 2 ^ k"
807 finally have "n < 2 ^ k" .
813 subsection {* Unsigned Arithmetic Operations *}
816 bv_add :: "[bit list, bit list ] => bit list" where
817 "bv_add w1 w2 = nat_to_bv (bv_to_nat w1 + bv_to_nat w2)"
819 lemma bv_add_type1 [simp]: "bv_add (norm_unsigned w1) w2 = bv_add w1 w2"
820 by (simp add: bv_add_def)
822 lemma bv_add_type2 [simp]: "bv_add w1 (norm_unsigned w2) = bv_add w1 w2"
823 by (simp add: bv_add_def)
825 lemma bv_add_returntype [simp]: "norm_unsigned (bv_add w1 w2) = bv_add w1 w2"
826 by (simp add: bv_add_def)
828 lemma bv_add_length: "length (bv_add w1 w2) \<le> Suc (max (length w1) (length w2))"
829 proof (unfold bv_add_def,rule length_nat_to_bv_upper_limit)
830 from bv_to_nat_upper_range [of w1] and bv_to_nat_upper_range [of w2]
831 have "bv_to_nat w1 + bv_to_nat w2 \<le> (2 ^ length w1 - 1) + (2 ^ length w2 - 1)"
833 also have "... \<le> max (2 ^ length w1 - 1) (2 ^ length w2 - 1) + max (2 ^ length w1 - 1) (2 ^ length w2 - 1)"
834 by (rule add_mono,safe intro!: le_maxI1 le_maxI2)
835 also have "... = 2 * max (2 ^ length w1 - 1) (2 ^ length w2 - 1)"
837 also have "... \<le> 2 ^ Suc (max (length w1) (length w2)) - 2"
838 proof (cases "length w1 \<le> length w2")
839 assume w1w2: "length w1 \<le> length w2"
840 hence "(2::nat) ^ length w1 \<le> 2 ^ length w2"
842 hence "(2::nat) ^ length w1 - 1 \<le> 2 ^ length w2 - 1"
844 with w1w2 show ?thesis
845 by (simp add: diff_mult_distrib2 split: split_max)
847 assume [simp]: "~ (length w1 \<le> length w2)"
848 have "~ ((2::nat) ^ length w1 - 1 \<le> 2 ^ length w2 - 1)"
850 assume "(2::nat) ^ length w1 - 1 \<le> 2 ^ length w2 - 1"
851 hence "((2::nat) ^ length w1 - 1) + 1 \<le> (2 ^ length w2 - 1) + 1"
852 by (rule add_right_mono)
853 hence "(2::nat) ^ length w1 \<le> 2 ^ length w2"
855 hence "length w1 \<le> length w2"
861 by (simp add: diff_mult_distrib2 split: split_max)
863 finally show "bv_to_nat w1 + bv_to_nat w2 \<le> 2 ^ Suc (max (length w1) (length w2)) - 1"
868 bv_mult :: "[bit list, bit list ] => bit list" where
869 "bv_mult w1 w2 = nat_to_bv (bv_to_nat w1 * bv_to_nat w2)"
871 lemma bv_mult_type1 [simp]: "bv_mult (norm_unsigned w1) w2 = bv_mult w1 w2"
872 by (simp add: bv_mult_def)
874 lemma bv_mult_type2 [simp]: "bv_mult w1 (norm_unsigned w2) = bv_mult w1 w2"
875 by (simp add: bv_mult_def)
877 lemma bv_mult_returntype [simp]: "norm_unsigned (bv_mult w1 w2) = bv_mult w1 w2"
878 by (simp add: bv_mult_def)
880 lemma bv_mult_length: "length (bv_mult w1 w2) \<le> length w1 + length w2"
881 proof (unfold bv_mult_def,rule length_nat_to_bv_upper_limit)
882 from bv_to_nat_upper_range [of w1] and bv_to_nat_upper_range [of w2]
883 have h: "bv_to_nat w1 \<le> 2 ^ length w1 - 1 \<and> bv_to_nat w2 \<le> 2 ^ length w2 - 1"
885 have "bv_to_nat w1 * bv_to_nat w2 \<le> (2 ^ length w1 - 1) * (2 ^ length w2 - 1)"
887 apply (rule mult_mono)
890 also have "... < 2 ^ length w1 * 2 ^ length w2"
891 by (rule mult_strict_mono,auto)
892 also have "... = 2 ^ (length w1 + length w2)"
893 by (simp add: power_add)
894 finally show "bv_to_nat w1 * bv_to_nat w2 \<le> 2 ^ (length w1 + length w2) - 1"
898 subsection {* Signed Vectors *}
901 norm_signed :: "bit list => bit list"
904 norm_signed_Nil: "norm_signed [] = []"
905 norm_signed_Cons: "norm_signed (b#bs) = (case b of \<zero> => if norm_unsigned bs = [] then [] else b#norm_unsigned bs | \<one> => b#rem_initial b bs)"
907 lemma norm_signed0 [simp]: "norm_signed [\<zero>] = []"
910 lemma norm_signed1 [simp]: "norm_signed [\<one>] = [\<one>]"
913 lemma norm_signed01 [simp]: "norm_signed (\<zero>#\<one>#xs) = \<zero>#\<one>#xs"
916 lemma norm_signed00 [simp]: "norm_signed (\<zero>#\<zero>#xs) = norm_signed (\<zero>#xs)"
919 lemma norm_signed10 [simp]: "norm_signed (\<one>#\<zero>#xs) = \<one>#\<zero>#xs"
922 lemma norm_signed11 [simp]: "norm_signed (\<one>#\<one>#xs) = norm_signed (\<one>#xs)"
925 lemmas [simp del] = norm_signed_Cons
928 int_to_bv :: "int => bit list" where
929 "int_to_bv n = (if 0 \<le> n
930 then norm_signed (\<zero>#nat_to_bv (nat n))
931 else norm_signed (bv_not (\<zero>#nat_to_bv (nat (-n- 1)))))"
933 lemma int_to_bv_ge0 [simp]: "0 \<le> n ==> int_to_bv n = norm_signed (\<zero> # nat_to_bv (nat n))"
934 by (simp add: int_to_bv_def)
936 lemma int_to_bv_lt0 [simp]: "n < 0 ==> int_to_bv n = norm_signed (bv_not (\<zero>#nat_to_bv (nat (-n- 1))))"
937 by (simp add: int_to_bv_def)
939 lemma norm_signed_idem [simp]: "norm_signed (norm_signed w) = norm_signed w"
940 proof (rule bit_list_induct [of _ w],simp_all)
942 assume "norm_signed (norm_signed xs) = norm_signed xs"
943 show "norm_signed (norm_signed (\<zero>#xs)) = norm_signed (\<zero>#xs)"
944 proof (rule bit_list_cases [of xs],simp_all)
946 assume [symmetric,simp]: "xs = \<zero>#ys"
947 show "norm_signed (norm_signed (\<zero>#ys)) = norm_signed (\<zero>#ys)"
952 assume "norm_signed (norm_signed xs) = norm_signed xs"
953 show "norm_signed (norm_signed (\<one>#xs)) = norm_signed (\<one>#xs)"
954 proof (rule bit_list_cases [of xs],simp_all)
956 assume [symmetric,simp]: "xs = \<one>#ys"
957 show "norm_signed (norm_signed (\<one>#ys)) = norm_signed (\<one>#ys)"
963 bv_to_int :: "bit list => int" where
965 (case bv_msb w of \<zero> => int (bv_to_nat w)
966 | \<one> => - int (bv_to_nat (bv_not w) + 1))"
968 lemma bv_to_int_Nil [simp]: "bv_to_int [] = 0"
969 by (simp add: bv_to_int_def)
971 lemma bv_to_int_Cons0 [simp]: "bv_to_int (\<zero>#bs) = int (bv_to_nat bs)"
972 by (simp add: bv_to_int_def)
974 lemma bv_to_int_Cons1 [simp]: "bv_to_int (\<one>#bs) = - int (bv_to_nat (bv_not bs) + 1)"
975 by (simp add: bv_to_int_def)
977 lemma bv_to_int_type [simp]: "bv_to_int (norm_signed w) = bv_to_int w"
978 proof (rule bit_list_induct [of _ w],simp_all)
980 assume ind: "bv_to_int (norm_signed xs) = bv_to_int xs"
981 show "bv_to_int (norm_signed (\<zero>#xs)) = int (bv_to_nat xs)"
982 proof (rule bit_list_cases [of xs],simp_all)
984 assume [simp]: "xs = \<zero>#ys"
986 show "bv_to_int (norm_signed (\<zero>#ys)) = int (bv_to_nat ys)"
991 assume ind: "bv_to_int (norm_signed xs) = bv_to_int xs"
992 show "bv_to_int (norm_signed (\<one>#xs)) = -1 - int (bv_to_nat (bv_not xs))"
993 proof (rule bit_list_cases [of xs],simp_all)
995 assume [simp]: "xs = \<one>#ys"
997 show "bv_to_int (norm_signed (\<one>#ys)) = -1 - int (bv_to_nat (bv_not ys))"
1002 lemma bv_to_int_upper_range: "bv_to_int w < 2 ^ (length w - 1)"
1003 proof (rule bit_list_cases [of w],simp_all)
1005 from bv_to_nat_upper_range
1006 show "int (bv_to_nat bs) < 2 ^ length bs"
1007 by (simp add: int_nat_two_exp)
1010 have "-1 - int (bv_to_nat (bv_not bs)) \<le> 0"
1012 also have "... < 2 ^ length bs"
1013 by (induct bs,simp_all)
1014 finally show "-1 - int (bv_to_nat (bv_not bs)) < 2 ^ length bs"
1018 lemma bv_to_int_lower_range: "- (2 ^ (length w - 1)) \<le> bv_to_int w"
1019 proof (rule bit_list_cases [of w],simp_all)
1020 fix bs :: "bit list"
1021 have "- (2 ^ length bs) \<le> (0::int)"
1022 by (induct bs,simp_all)
1023 also have "... \<le> int (bv_to_nat bs)"
1025 finally show "- (2 ^ length bs) \<le> int (bv_to_nat bs)"
1029 from bv_to_nat_upper_range [of "bv_not bs"]
1030 show "- (2 ^ length bs) \<le> -1 - int (bv_to_nat (bv_not bs))"
1031 by (simp add: int_nat_two_exp)
1034 lemma int_bv_int [simp]: "int_to_bv (bv_to_int w) = norm_signed w"
1035 proof (rule bit_list_cases [of w],simp)
1037 assume [simp]: "w = \<zero>#xs"
1040 apply (subst norm_signed_Cons [of "\<zero>" "xs"])
1042 using norm_unsigned_result [of xs]
1044 apply (rule bit_list_cases [of "norm_unsigned xs"])
1049 assume [simp]: "w = \<one>#xs"
1051 apply (simp del: int_to_bv_lt0)
1052 apply (rule bit_list_induct [of _ xs])
1054 apply (subst int_to_bv_lt0)
1055 apply (subgoal_tac "- int (bv_to_nat (bv_not (\<zero> # bs))) + -1 < 0 + 0")
1057 apply (rule add_le_less_mono)
1060 apply (simp del: bv_to_nat1 bv_to_nat_helper)
1065 lemma bv_int_bv [simp]: "bv_to_int (int_to_bv i) = i"
1066 by (cases "0 \<le> i",simp_all)
1068 lemma bv_msb_norm [simp]: "bv_msb (norm_signed w) = bv_msb w"
1069 by (rule bit_list_cases [of w],simp_all add: norm_signed_Cons)
1071 lemma norm_signed_length: "length (norm_signed w) \<le> length w"
1072 apply (cases w,simp_all)
1073 apply (subst norm_signed_Cons)
1074 apply (case_tac "a",simp_all)
1075 apply (rule rem_initial_length)
1078 lemma norm_signed_equal: "length (norm_signed w) = length w ==> norm_signed w = w"
1079 proof (rule bit_list_cases [of w],simp_all)
1081 assume "length (norm_signed (\<zero>#xs)) = Suc (length xs)"
1082 thus "norm_signed (\<zero>#xs) = \<zero>#xs"
1083 apply (simp add: norm_signed_Cons)
1086 apply (rule norm_unsigned_equal)
1091 assume "length (norm_signed (\<one>#xs)) = Suc (length xs)"
1092 thus "norm_signed (\<one>#xs) = \<one>#xs"
1093 apply (simp add: norm_signed_Cons)
1094 apply (rule rem_initial_equal)
1099 lemma bv_extend_norm_signed: "bv_msb w = b ==> bv_extend (length w) b (norm_signed w) = w"
1100 proof (rule bit_list_cases [of w],simp_all)
1102 show "bv_extend (Suc (length xs)) \<zero> (norm_signed (\<zero>#xs)) = \<zero>#xs"
1103 proof (simp add: norm_signed_list_def,auto)
1104 assume "norm_unsigned xs = []"
1105 hence xx: "rem_initial \<zero> xs = []"
1106 by (simp add: norm_unsigned_def)
1107 have "bv_extend (Suc (length xs)) \<zero> (\<zero>#rem_initial \<zero> xs) = \<zero>#xs"
1108 apply (simp add: bv_extend_def replicate_app_Cons_same)
1109 apply (fold bv_extend_def)
1110 apply (rule bv_extend_rem_initial)
1112 thus "bv_extend (Suc (length xs)) \<zero> [\<zero>] = \<zero>#xs"
1115 show "bv_extend (Suc (length xs)) \<zero> (\<zero>#norm_unsigned xs) = \<zero>#xs"
1116 apply (simp add: norm_unsigned_def)
1117 apply (simp add: bv_extend_def replicate_app_Cons_same)
1118 apply (fold bv_extend_def)
1119 apply (rule bv_extend_rem_initial)
1124 show "bv_extend (Suc (length xs)) \<one> (norm_signed (\<one>#xs)) = \<one>#xs"
1125 apply (simp add: norm_signed_Cons)
1126 apply (simp add: bv_extend_def replicate_app_Cons_same)
1127 apply (fold bv_extend_def)
1128 apply (rule bv_extend_rem_initial)
1132 lemma bv_to_int_qinj:
1133 assumes one: "bv_to_int xs = bv_to_int ys"
1134 and len: "length xs = length ys"
1138 have "int_to_bv (bv_to_int xs) = int_to_bv (bv_to_int ys)"
1140 hence xsys: "norm_signed xs = norm_signed ys"
1142 hence xsys': "bv_msb xs = bv_msb ys"
1144 have "bv_msb xs = bv_msb (norm_signed xs)"
1146 also have "... = bv_msb (norm_signed ys)"
1148 also have "... = bv_msb ys"
1150 finally show ?thesis .
1152 have "xs = bv_extend (length xs) (bv_msb xs) (norm_signed xs)"
1153 by (simp add: bv_extend_norm_signed)
1154 also have "... = bv_extend (length ys) (bv_msb ys) (norm_signed ys)"
1155 by (simp add: xsys xsys' len)
1156 also have "... = ys"
1157 by (simp add: bv_extend_norm_signed)
1158 finally show ?thesis .
1161 lemma int_to_bv_returntype [simp]: "norm_signed (int_to_bv w) = int_to_bv w"
1162 by (simp add: int_to_bv_def)
1164 lemma bv_to_int_msb0: "0 \<le> bv_to_int w1 ==> bv_msb w1 = \<zero>"
1165 by (rule bit_list_cases,simp_all)
1167 lemma bv_to_int_msb1: "bv_to_int w1 < 0 ==> bv_msb w1 = \<one>"
1168 by (rule bit_list_cases,simp_all)
1170 lemma bv_to_int_lower_limit_gt0:
1171 assumes w0: "0 < bv_to_int w"
1172 shows "2 ^ (length (norm_signed w) - 2) \<le> bv_to_int w"
1175 have "0 \<le> bv_to_int w"
1177 hence [simp]: "bv_msb w = \<zero>"
1178 by (rule bv_to_int_msb0)
1179 have "2 ^ (length (norm_signed w) - 2) \<le> bv_to_int (norm_signed w)"
1180 proof (rule bit_list_cases [of w])
1187 assume weq: "w = \<zero> # w'"
1189 proof (simp add: norm_signed_Cons,safe)
1190 assume "norm_unsigned w' = []"
1193 by (simp add: norm_empty_bv_to_nat_zero)
1195 assume w'0: "norm_unsigned w' \<noteq> []"
1196 have "0 < bv_to_nat w'"
1198 assume "~ (0 < bv_to_nat w')"
1199 hence "bv_to_nat w' = 0"
1201 hence "norm_unsigned w' = []"
1202 by (simp add: bv_to_nat_zero_imp_empty)
1207 with bv_to_nat_lower_limit [of w']
1208 show "2 ^ (length (norm_unsigned w') - Suc 0) \<le> int (bv_to_nat w')"
1209 by (simp add: int_nat_two_exp)
1213 assume "w = \<one> # w'"
1215 have "bv_msb w = \<zero>"
1221 also have "... = bv_to_int w"
1223 finally show ?thesis .
1226 lemma norm_signed_result: "norm_signed w = [] \<or> norm_signed w = [\<one>] \<or> bv_msb (norm_signed w) \<noteq> bv_msb (tl (norm_signed w))"
1227 apply (rule bit_list_cases [of w],simp_all)
1228 apply (case_tac "bs",simp_all)
1229 apply (case_tac "a",simp_all)
1230 apply (simp add: norm_signed_Cons)
1235 assume msb: "\<zero> = bv_msb (norm_unsigned l)"
1236 assume "norm_unsigned l \<noteq> []"
1237 with norm_unsigned_result [of l]
1238 have "bv_msb (norm_unsigned l) = \<one>"
1245 assume p: "\<one> = bv_msb (tl (norm_signed (\<one> # xs)))"
1246 have "\<one> \<noteq> bv_msb (tl (norm_signed (\<one> # xs)))"
1247 by (rule bit_list_induct [of _ xs],simp_all)
1253 lemma bv_to_int_upper_limit_lem1:
1254 assumes w0: "bv_to_int w < -1"
1255 shows "bv_to_int w < - (2 ^ (length (norm_signed w) - 2))"
1258 have "bv_to_int w < 0"
1260 hence msbw [simp]: "bv_msb w = \<one>"
1261 by (rule bv_to_int_msb1)
1262 have "bv_to_int w = bv_to_int (norm_signed w)"
1264 also from norm_signed_result [of w]
1265 have "... < - (2 ^ (length (norm_signed w) - 2))"
1267 assume "norm_signed w = []"
1268 hence "bv_to_int (norm_signed w) = 0"
1274 assume "norm_signed w = [\<one>]"
1275 hence "bv_to_int (norm_signed w) = -1"
1281 assume "bv_msb (norm_signed w) \<noteq> bv_msb (tl (norm_signed w))"
1282 hence msb_tl: "\<one> \<noteq> bv_msb (tl (norm_signed w))"
1284 show "bv_to_int (norm_signed w) < - (2 ^ (length (norm_signed w) - 2))"
1285 proof (rule bit_list_cases [of "norm_signed w"])
1286 assume "norm_signed w = []"
1287 hence "bv_to_int (norm_signed w) = 0"
1294 assume nw: "norm_signed w = \<zero> # w'"
1296 have "bv_msb (norm_signed w) = \<one>"
1303 assume weq: "norm_signed w = \<one> # w'"
1305 proof (rule bit_list_cases [of w'])
1306 assume w'eq: "w' = []"
1308 have "bv_to_int (norm_signed w) < -1"
1315 assume w'eq: "w' = \<zero> # w''"
1317 apply (simp add: weq w'eq)
1318 apply (subgoal_tac "- int (bv_to_nat (bv_not w'')) + -1 < 0 + 0")
1319 apply (simp add: int_nat_two_exp)
1320 apply (rule add_le_less_mono)
1325 assume w'eq: "w' = \<one> # w''"
1332 finally show ?thesis .
1335 lemma length_int_to_bv_upper_limit_gt0:
1337 and wk: "i \<le> 2 ^ (k - 1) - 1"
1338 shows "length (int_to_bv i) \<le> k"
1342 by (cases "k - 1",simp_all)
1343 assume "~ length (int_to_bv i) \<le> k"
1344 hence "k < length (int_to_bv i)"
1346 hence "k \<le> length (int_to_bv i) - 1"
1348 hence a: "k - 1 \<le> length (int_to_bv i) - 2"
1350 hence "(2::int) ^ (k - 1) \<le> 2 ^ (length (int_to_bv i) - 2)" by simp
1351 also have "... \<le> i"
1353 have "2 ^ (length (norm_signed (int_to_bv i)) - 2) \<le> bv_to_int (int_to_bv i)"
1354 proof (rule bv_to_int_lower_limit_gt0)
1356 show "0 < bv_to_int (int_to_bv i)"
1362 finally have "2 ^ (k - 1) \<le> i" .
1368 lemma pos_length_pos:
1369 assumes i0: "0 < bv_to_int w"
1370 shows "0 < length w"
1372 from norm_signed_result [of w]
1373 have "0 < length (norm_signed w)"
1375 assume ii: "norm_signed w = []"
1376 have "bv_to_int (norm_signed w) = 0"
1378 hence "bv_to_int w = 0"
1384 assume ii: "norm_signed w = []"
1385 assume jj: "bv_msb w \<noteq> \<zero>"
1386 have "\<zero> = bv_msb (norm_signed w)"
1388 also have "... \<noteq> \<zero>"
1390 finally show False by simp
1392 also have "... \<le> length w"
1393 by (rule norm_signed_length)
1394 finally show ?thesis
1398 lemma neg_length_pos:
1399 assumes i0: "bv_to_int w < -1"
1400 shows "0 < length w"
1402 from norm_signed_result [of w]
1403 have "0 < length (norm_signed w)"
1405 assume ii: "norm_signed w = []"
1406 have "bv_to_int (norm_signed w) = 0"
1408 hence "bv_to_int w = 0"
1414 assume ii: "norm_signed w = []"
1415 assume jj: "bv_msb w \<noteq> \<zero>"
1416 have "\<zero> = bv_msb (norm_signed w)"
1418 also have "... \<noteq> \<zero>"
1420 finally show False by simp
1422 also have "... \<le> length w"
1423 by (rule norm_signed_length)
1424 finally show ?thesis
1428 lemma length_int_to_bv_lower_limit_gt0:
1429 assumes wk: "2 ^ (k - 1) \<le> i"
1430 shows "k < length (int_to_bv i)"
1432 have "0 < (2::int) ^ (k - 1)"
1433 by (rule zero_less_power,simp)
1434 also have "... \<le> i"
1436 finally have i0: "0 < i"
1438 have lii0: "0 < length (int_to_bv i)"
1439 apply (rule pos_length_pos)
1440 apply (simp,rule i0)
1442 assume "~ k < length (int_to_bv i)"
1443 hence "length (int_to_bv i) \<le> k"
1446 have a: "length (int_to_bv i) - 1 \<le> k - 1"
1448 have "i < 2 ^ (length (int_to_bv i) - 1)"
1450 have "i = bv_to_int (int_to_bv i)"
1452 also have "... < 2 ^ (length (int_to_bv i) - 1)"
1453 by (rule bv_to_int_upper_range)
1454 finally show ?thesis .
1456 also have "(2::int) ^ (length (int_to_bv i) - 1) \<le> 2 ^ (k - 1)" using a
1458 finally have "i < 2 ^ (k - 1)" .
1464 lemma length_int_to_bv_upper_limit_lem1:
1465 assumes w1: "i < -1"
1466 and wk: "- (2 ^ (k - 1)) \<le> i"
1467 shows "length (int_to_bv i) \<le> k"
1471 by (cases "k - 1",simp_all)
1472 assume "~ length (int_to_bv i) \<le> k"
1473 hence "k < length (int_to_bv i)"
1475 hence "k \<le> length (int_to_bv i) - 1"
1477 hence a: "k - 1 \<le> length (int_to_bv i) - 2"
1479 have "i < - (2 ^ (length (int_to_bv i) - 2))"
1481 have "i = bv_to_int (int_to_bv i)"
1483 also have "... < - (2 ^ (length (norm_signed (int_to_bv i)) - 2))"
1484 by (rule bv_to_int_upper_limit_lem1,simp,rule w1)
1485 finally show ?thesis by simp
1487 also have "... \<le> -(2 ^ (k - 1))"
1489 have "(2::int) ^ (k - 1) \<le> 2 ^ (length (int_to_bv i) - 2)" using a
1494 finally have "i < -(2 ^ (k - 1))" .
1500 lemma length_int_to_bv_lower_limit_lem1:
1501 assumes wk: "i < -(2 ^ (k - 1))"
1502 shows "k < length (int_to_bv i)"
1505 have "i \<le> -(2 ^ (k - 1)) - 1"
1507 also have "... < -1"
1509 have "0 < (2::int) ^ (k - 1)"
1510 by (rule zero_less_power,simp)
1511 hence "-((2::int) ^ (k - 1)) < 0"
1513 thus ?thesis by simp
1515 finally have i1: "i < -1" .
1516 have lii0: "0 < length (int_to_bv i)"
1517 apply (rule neg_length_pos)
1518 apply (simp,rule i1)
1520 assume "~ k < length (int_to_bv i)"
1521 hence "length (int_to_bv i) \<le> k"
1524 have a: "length (int_to_bv i) - 1 \<le> k - 1"
1526 hence "(2::int) ^ (length (int_to_bv i) - 1) \<le> 2 ^ (k - 1)" by simp
1527 hence "-((2::int) ^ (k - 1)) \<le> - (2 ^ (length (int_to_bv i) - 1))"
1529 also have "... \<le> i"
1531 have "- (2 ^ (length (int_to_bv i) - 1)) \<le> bv_to_int (int_to_bv i)"
1532 by (rule bv_to_int_lower_range)
1535 finally show ?thesis .
1537 finally have "-(2 ^ (k - 1)) \<le> i" .
1543 subsection {* Signed Arithmetic Operations *}
1545 subsubsection {* Conversion from unsigned to signed *}
1548 utos :: "bit list => bit list" where
1549 "utos w = norm_signed (\<zero> # w)"
1551 lemma utos_type [simp]: "utos (norm_unsigned w) = utos w"
1552 by (simp add: utos_def norm_signed_Cons)
1554 lemma utos_returntype [simp]: "norm_signed (utos w) = utos w"
1555 by (simp add: utos_def)
1557 lemma utos_length: "length (utos w) \<le> Suc (length w)"
1558 by (simp add: utos_def norm_signed_Cons)
1560 lemma bv_to_int_utos: "bv_to_int (utos w) = int (bv_to_nat w)"
1561 proof (simp add: utos_def norm_signed_Cons,safe)
1562 assume "norm_unsigned w = []"
1563 hence "bv_to_nat (norm_unsigned w) = 0"
1565 thus "bv_to_nat w = 0"
1569 subsubsection {* Unary minus *}
1572 bv_uminus :: "bit list => bit list" where
1573 "bv_uminus w = int_to_bv (- bv_to_int w)"
1575 lemma bv_uminus_type [simp]: "bv_uminus (norm_signed w) = bv_uminus w"
1576 by (simp add: bv_uminus_def)
1578 lemma bv_uminus_returntype [simp]: "norm_signed (bv_uminus w) = bv_uminus w"
1579 by (simp add: bv_uminus_def)
1581 lemma bv_uminus_length: "length (bv_uminus w) \<le> Suc (length w)"
1583 have "1 < -bv_to_int w \<or> -bv_to_int w = 1 \<or> -bv_to_int w = 0 \<or> -bv_to_int w = -1 \<or> -bv_to_int w < -1"
1587 assume p: "1 < - bv_to_int w"
1588 have lw: "0 < length w"
1589 apply (rule neg_length_pos)
1594 proof (simp add: bv_uminus_def,rule length_int_to_bv_upper_limit_gt0,simp_all)
1596 show "bv_to_int w < 0"
1599 have "-(2^(length w - 1)) \<le> bv_to_int w"
1600 by (rule bv_to_int_lower_range)
1601 hence "- bv_to_int w \<le> 2^(length w - 1)"
1603 also from lw have "... < 2 ^ length w"
1605 finally show "- bv_to_int w < 2 ^ length w"
1609 assume p: "- bv_to_int w = 1"
1610 hence lw: "0 < length w"
1611 by (cases w,simp_all)
1614 apply (simp add: bv_uminus_def)
1616 apply (simp (no_asm) add: nat_to_bv_non0)
1619 assume "- bv_to_int w = 0"
1621 by (simp add: bv_uminus_def)
1623 assume p: "- bv_to_int w = -1"
1625 by (simp add: bv_uminus_def)
1627 assume p: "- bv_to_int w < -1"
1629 apply (simp add: bv_uminus_def)
1630 apply (rule length_int_to_bv_upper_limit_lem1)
1634 have "bv_to_int w < 2 ^ (length w - 1)"
1635 by (rule bv_to_int_upper_range)
1636 also have "... \<le> 2 ^ length w" by simp
1637 finally show "bv_to_int w \<le> 2 ^ length w"
1643 lemma bv_uminus_length_utos: "length (bv_uminus (utos w)) \<le> Suc (length w)"
1645 have "-bv_to_int (utos w) = 0 \<or> -bv_to_int (utos w) = -1 \<or> -bv_to_int (utos w) < -1"
1646 apply (simp add: bv_to_int_utos)
1650 assume "-bv_to_int (utos w) = 0"
1652 by (simp add: bv_uminus_def)
1654 assume "-bv_to_int (utos w) = -1"
1656 by (simp add: bv_uminus_def)
1658 assume p: "-bv_to_int (utos w) < -1"
1660 apply (simp add: bv_uminus_def)
1661 apply (rule length_int_to_bv_upper_limit_lem1)
1663 apply (simp add: bv_to_int_utos)
1664 using bv_to_nat_upper_range [of w]
1665 apply (simp add: int_nat_two_exp)
1671 bv_sadd :: "[bit list, bit list ] => bit list" where
1672 "bv_sadd w1 w2 = int_to_bv (bv_to_int w1 + bv_to_int w2)"
1674 lemma bv_sadd_type1 [simp]: "bv_sadd (norm_signed w1) w2 = bv_sadd w1 w2"
1675 by (simp add: bv_sadd_def)
1677 lemma bv_sadd_type2 [simp]: "bv_sadd w1 (norm_signed w2) = bv_sadd w1 w2"
1678 by (simp add: bv_sadd_def)
1680 lemma bv_sadd_returntype [simp]: "norm_signed (bv_sadd w1 w2) = bv_sadd w1 w2"
1681 by (simp add: bv_sadd_def)
1684 assumes lw: "0 < max (length w1) (length w2)"
1685 shows "((2::int) ^ (length w1 - 1)) + (2 ^ (length w2 - 1)) \<le> 2 ^ max (length w1) (length w2)"
1687 have "((2::int) ^ (length w1 - 1)) + (2 ^ (length w2 - 1)) \<le> 2 ^ (max (length w1) (length w2) - 1) + 2 ^ (max (length w1) (length w2) - 1)"
1688 apply (cases "length w1 \<le> length w2")
1689 apply (auto simp add: max_def)
1691 also have "... = 2 ^ max (length w1) (length w2)"
1696 apply (subst power_Suc [symmetric])
1697 apply (simp del: power.simps)
1700 finally show ?thesis .
1703 lemma bv_sadd_length: "length (bv_sadd w1 w2) \<le> Suc (max (length w1) (length w2))"
1705 let ?Q = "bv_to_int w1 + bv_to_int w2"
1707 have helper: "?Q \<noteq> 0 ==> 0 < max (length w1) (length w2)"
1709 assume p: "?Q \<noteq> 0"
1710 show "0 < max (length w1) (length w2)"
1711 proof (simp add: less_max_iff_disj,rule)
1712 assume [simp]: "w1 = []"
1713 show "w2 \<noteq> []"
1714 proof (rule ccontr,simp)
1715 assume [simp]: "w2 = []"
1723 have "0 < ?Q \<or> ?Q = 0 \<or> ?Q = -1 \<or> ?Q < -1"
1729 by (simp add: bv_sadd_def)
1733 by (simp add: bv_sadd_def)
1737 apply (simp add: bv_sadd_def)
1738 apply (rule length_int_to_bv_upper_limit_gt0)
1741 from bv_to_int_upper_range [of w2]
1742 have "bv_to_int w2 \<le> 2 ^ (length w2 - 1)"
1744 with bv_to_int_upper_range [of w1]
1745 have "bv_to_int w1 + bv_to_int w2 < (2 ^ (length w1 - 1)) + (2 ^ (length w2 - 1))"
1746 by (rule zadd_zless_mono)
1747 also have "... \<le> 2 ^ max (length w1) (length w2)"
1748 apply (rule adder_helper)
1753 finally show "?Q < 2 ^ max (length w1) (length w2)"
1759 apply (simp add: bv_sadd_def)
1760 apply (rule length_int_to_bv_upper_limit_lem1,simp_all)
1763 have "(2 ^ (length w1 - 1)) + 2 ^ (length w2 - 1) \<le> (2::int) ^ max (length w1) (length w2)"
1764 apply (rule adder_helper)
1769 hence "-((2::int) ^ max (length w1) (length w2)) \<le> - (2 ^ (length w1 - 1)) + -(2 ^ (length w2 - 1))"
1771 also have "- (2 ^ (length w1 - 1)) + -(2 ^ (length w2 - 1)) \<le> ?Q"
1772 apply (rule add_mono)
1773 apply (rule bv_to_int_lower_range [of w1])
1774 apply (rule bv_to_int_lower_range [of w2])
1776 finally show "- (2^max (length w1) (length w2)) \<le> ?Q" .
1782 bv_sub :: "[bit list, bit list] => bit list" where
1783 "bv_sub w1 w2 = bv_sadd w1 (bv_uminus w2)"
1785 lemma bv_sub_type1 [simp]: "bv_sub (norm_signed w1) w2 = bv_sub w1 w2"
1786 by (simp add: bv_sub_def)
1788 lemma bv_sub_type2 [simp]: "bv_sub w1 (norm_signed w2) = bv_sub w1 w2"
1789 by (simp add: bv_sub_def)
1791 lemma bv_sub_returntype [simp]: "norm_signed (bv_sub w1 w2) = bv_sub w1 w2"
1792 by (simp add: bv_sub_def)
1794 lemma bv_sub_length: "length (bv_sub w1 w2) \<le> Suc (max (length w1) (length w2))"
1795 proof (cases "bv_to_int w2 = 0")
1796 assume p: "bv_to_int w2 = 0"
1798 proof (simp add: bv_sub_def bv_sadd_def bv_uminus_def p)
1799 have "length (norm_signed w1) \<le> length w1"
1800 by (rule norm_signed_length)
1801 also have "... \<le> max (length w1) (length w2)"
1803 also have "... \<le> Suc (max (length w1) (length w2))"
1805 finally show "length (norm_signed w1) \<le> Suc (max (length w1) (length w2))"
1809 assume "bv_to_int w2 \<noteq> 0"
1810 hence "0 < length w2"
1811 by (cases w2,simp_all)
1812 hence lmw: "0 < max (length w1) (length w2)"
1815 let ?Q = "bv_to_int w1 - bv_to_int w2"
1817 have "0 < ?Q \<or> ?Q = 0 \<or> ?Q = -1 \<or> ?Q < -1"
1823 by (simp add: bv_sub_def bv_sadd_def bv_uminus_def)
1827 by (simp add: bv_sub_def bv_sadd_def bv_uminus_def)
1831 apply (simp add: bv_sub_def bv_sadd_def bv_uminus_def)
1832 apply (rule length_int_to_bv_upper_limit_gt0)
1835 from bv_to_int_lower_range [of w2]
1836 have v2: "- bv_to_int w2 \<le> 2 ^ (length w2 - 1)"
1838 have "bv_to_int w1 + - bv_to_int w2 < (2 ^ (length w1 - 1)) + (2 ^ (length w2 - 1))"
1839 apply (rule zadd_zless_mono)
1840 apply (rule bv_to_int_upper_range [of w1])
1843 also have "... \<le> 2 ^ max (length w1) (length w2)"
1844 apply (rule adder_helper)
1847 finally show "?Q < 2 ^ max (length w1) (length w2)"
1853 apply (simp add: bv_sub_def bv_sadd_def bv_uminus_def)
1854 apply (rule length_int_to_bv_upper_limit_lem1)
1857 have "(2 ^ (length w1 - 1)) + 2 ^ (length w2 - 1) \<le> (2::int) ^ max (length w1) (length w2)"
1858 apply (rule adder_helper)
1861 hence "-((2::int) ^ max (length w1) (length w2)) \<le> - (2 ^ (length w1 - 1)) + -(2 ^ (length w2 - 1))"
1863 also have "- (2 ^ (length w1 - 1)) + -(2 ^ (length w2 - 1)) \<le> bv_to_int w1 + -bv_to_int w2"
1864 apply (rule add_mono)
1865 apply (rule bv_to_int_lower_range [of w1])
1866 using bv_to_int_upper_range [of w2]
1869 finally show "- (2^max (length w1) (length w2)) \<le> ?Q"
1876 bv_smult :: "[bit list, bit list] => bit list" where
1877 "bv_smult w1 w2 = int_to_bv (bv_to_int w1 * bv_to_int w2)"
1879 lemma bv_smult_type1 [simp]: "bv_smult (norm_signed w1) w2 = bv_smult w1 w2"
1880 by (simp add: bv_smult_def)
1882 lemma bv_smult_type2 [simp]: "bv_smult w1 (norm_signed w2) = bv_smult w1 w2"
1883 by (simp add: bv_smult_def)
1885 lemma bv_smult_returntype [simp]: "norm_signed (bv_smult w1 w2) = bv_smult w1 w2"
1886 by (simp add: bv_smult_def)
1888 lemma bv_smult_length: "length (bv_smult w1 w2) \<le> length w1 + length w2"
1890 let ?Q = "bv_to_int w1 * bv_to_int w2"
1892 have lmw: "?Q \<noteq> 0 ==> 0 < length w1 \<and> 0 < length w2"
1895 have "0 < ?Q \<or> ?Q = 0 \<or> ?Q = -1 \<or> ?Q < -1"
1898 proof (safe dest!: iffD1 [OF mult_eq_0_iff])
1899 assume "bv_to_int w1 = 0"
1901 by (simp add: bv_smult_def)
1903 assume "bv_to_int w2 = 0"
1905 by (simp add: bv_smult_def)
1909 apply (simp add: bv_smult_def p)
1918 proof (simp add: zero_less_mult_iff,safe)
1919 assume bi1: "0 < bv_to_int w1"
1920 assume bi2: "0 < bv_to_int w2"
1922 apply (simp add: bv_smult_def)
1923 apply (rule length_int_to_bv_upper_limit_gt0)
1926 have "?Q < 2 ^ (length w1 - 1) * 2 ^ (length w2 - 1)"
1927 apply (rule mult_strict_mono)
1928 apply (rule bv_to_int_upper_range)
1929 apply (rule bv_to_int_upper_range)
1930 apply (rule zero_less_power)
1935 also have "... \<le> 2 ^ (length w1 + length w2 - Suc 0)"
1937 apply (subst zpower_zadd_distrib [symmetric])
1940 finally show "?Q < 2 ^ (length w1 + length w2 - Suc 0)"
1944 assume bi1: "bv_to_int w1 < 0"
1945 assume bi2: "bv_to_int w2 < 0"
1947 apply (simp add: bv_smult_def)
1948 apply (rule length_int_to_bv_upper_limit_gt0)
1951 have "-bv_to_int w1 * -bv_to_int w2 \<le> 2 ^ (length w1 - 1) * 2 ^(length w2 - 1)"
1952 apply (rule mult_mono)
1953 using bv_to_int_lower_range [of w1]
1955 using bv_to_int_lower_range [of w2]
1957 apply (rule zero_le_power,simp)
1961 hence "?Q \<le> 2 ^ (length w1 - 1) * 2 ^(length w2 - 1)"
1963 also have "... < 2 ^ (length w1 + length w2 - Suc 0)"
1965 apply (subst zpower_zadd_distrib [symmetric])
1972 finally show "?Q < 2 ^ (length w1 + length w2 - Suc 0)" .
1978 apply (subst bv_smult_def)
1979 apply (rule length_int_to_bv_upper_limit_lem1)
1982 have "(2::int) ^ (length w1 - 1) * 2 ^(length w2 - 1) \<le> 2 ^ (length w1 + length w2 - Suc 0)"
1984 apply (subst zpower_zadd_distrib [symmetric])
1987 hence "-((2::int) ^ (length w1 + length w2 - Suc 0)) \<le> -(2^(length w1 - 1) * 2 ^ (length w2 - 1))"
1989 also have "... \<le> ?Q"
1992 have q: "bv_to_int w1 * bv_to_int w2 < 0"
1995 proof (simp add: mult_less_0_iff,safe)
1996 assume bi1: "0 < bv_to_int w1"
1997 assume bi2: "bv_to_int w2 < 0"
1998 have "-bv_to_int w2 * bv_to_int w1 \<le> ((2::int)^(length w2 - 1)) * (2 ^ (length w1 - 1))"
1999 apply (rule mult_mono)
2000 using bv_to_int_lower_range [of w2]
2002 using bv_to_int_upper_range [of w1]
2004 apply (rule zero_le_power,simp)
2008 hence "-?Q \<le> ((2::int)^(length w1 - 1)) * (2 ^ (length w2 - 1))"
2009 by (simp add: zmult_ac)
2010 thus "-(((2::int)^(length w1 - Suc 0)) * (2 ^ (length w2 - Suc 0))) \<le> ?Q"
2013 assume bi1: "bv_to_int w1 < 0"
2014 assume bi2: "0 < bv_to_int w2"
2015 have "-bv_to_int w1 * bv_to_int w2 \<le> ((2::int)^(length w1 - 1)) * (2 ^ (length w2 - 1))"
2016 apply (rule mult_mono)
2017 using bv_to_int_lower_range [of w1]
2019 using bv_to_int_upper_range [of w2]
2021 apply (rule zero_le_power,simp)
2025 hence "-?Q \<le> ((2::int)^(length w1 - 1)) * (2 ^ (length w2 - 1))"
2026 by (simp add: zmult_ac)
2027 thus "-(((2::int)^(length w1 - Suc 0)) * (2 ^ (length w2 - Suc 0))) \<le> ?Q"
2031 finally show "-(2 ^ (length w1 + length w2 - Suc 0)) \<le> ?Q"
2037 lemma bv_msb_one: "bv_msb w = \<one> ==> 0 < bv_to_nat w"
2038 by (cases w,simp_all)
2040 lemma bv_smult_length_utos: "length (bv_smult (utos w1) w2) \<le> length w1 + length w2"
2042 let ?Q = "bv_to_int (utos w1) * bv_to_int w2"
2044 have lmw: "?Q \<noteq> 0 ==> 0 < length (utos w1) \<and> 0 < length w2"
2047 have "0 < ?Q \<or> ?Q = 0 \<or> ?Q = -1 \<or> ?Q < -1"
2050 proof (safe dest!: iffD1 [OF mult_eq_0_iff])
2051 assume "bv_to_int (utos w1) = 0"
2053 by (simp add: bv_smult_def)
2055 assume "bv_to_int w2 = 0"
2057 by (simp add: bv_smult_def)
2061 proof (simp add: zero_less_mult_iff,safe)
2062 assume biw2: "0 < bv_to_int w2"
2064 apply (simp add: bv_smult_def)
2065 apply (rule length_int_to_bv_upper_limit_gt0)
2068 have "?Q < 2 ^ length w1 * 2 ^ (length w2 - 1)"
2069 apply (rule mult_strict_mono)
2070 apply (simp add: bv_to_int_utos int_nat_two_exp)
2071 apply (rule bv_to_nat_upper_range)
2072 apply (rule bv_to_int_upper_range)
2073 apply (rule zero_less_power,simp)
2077 also have "... \<le> 2 ^ (length w1 + length w2 - Suc 0)"
2079 apply (subst zpower_zadd_distrib [symmetric])
2086 finally show "?Q < 2 ^ (length w1 + length w2 - Suc 0)"
2090 assume "bv_to_int (utos w1) < 0"
2092 by (simp add: bv_to_int_utos)
2097 apply (simp add: bv_smult_def)
2105 apply (subst bv_smult_def)
2106 apply (rule length_int_to_bv_upper_limit_lem1)
2109 have "(2::int) ^ length w1 * 2 ^(length w2 - 1) \<le> 2 ^ (length w1 + length w2 - Suc 0)"
2111 apply (subst zpower_zadd_distrib [symmetric])
2118 hence "-((2::int) ^ (length w1 + length w2 - Suc 0)) \<le> -(2^ length w1 * 2 ^ (length w2 - 1))"
2120 also have "... \<le> ?Q"
2123 have q: "bv_to_int (utos w1) * bv_to_int w2 < 0"
2126 proof (simp add: mult_less_0_iff,safe)
2127 assume bi1: "0 < bv_to_int (utos w1)"
2128 assume bi2: "bv_to_int w2 < 0"
2129 have "-bv_to_int w2 * bv_to_int (utos w1) \<le> ((2::int)^(length w2 - 1)) * (2 ^ length w1)"
2130 apply (rule mult_mono)
2131 using bv_to_int_lower_range [of w2]
2133 apply (simp add: bv_to_int_utos)
2134 using bv_to_nat_upper_range [of w1]
2135 apply (simp add: int_nat_two_exp)
2136 apply (rule zero_le_power,simp)
2140 hence "-?Q \<le> ((2::int)^length w1) * (2 ^ (length w2 - 1))"
2141 by (simp add: zmult_ac)
2142 thus "-(((2::int)^length w1) * (2 ^ (length w2 - Suc 0))) \<le> ?Q"
2145 assume bi1: "bv_to_int (utos w1) < 0"
2146 thus "-(((2::int)^length w1) * (2 ^ (length w2 - Suc 0))) \<le> ?Q"
2147 by (simp add: bv_to_int_utos)
2150 finally show "-(2 ^ (length w1 + length w2 - Suc 0)) \<le> ?Q"
2156 lemma bv_smult_sym: "bv_smult w1 w2 = bv_smult w2 w1"
2157 by (simp add: bv_smult_def zmult_ac)
2159 subsection {* Structural operations *}
2162 bv_select :: "[bit list,nat] => bit" where
2163 "bv_select w i = w ! (length w - 1 - i)"
2166 bv_chop :: "[bit list,nat] => bit list * bit list" where
2167 "bv_chop w i = (let len = length w in (take (len - i) w,drop (len - i) w))"
2170 bv_slice :: "[bit list,nat*nat] => bit list" where
2171 "bv_slice w = (\<lambda>(b,e). fst (bv_chop (snd (bv_chop w (b+1))) e))"
2173 lemma bv_select_rev:
2174 assumes notnull: "n < length w"
2175 shows "bv_select w n = rev w ! n"
2177 have "\<forall>n. n < length w --> bv_select w n = rev w ! n"
2178 proof (rule length_induct [of _ w],auto simp add: bv_select_def)
2179 fix xs :: "bit list"
2181 assume ind: "\<forall>ys::bit list. length ys < length xs --> (\<forall>n. n < length ys --> ys ! (length ys - Suc n) = rev ys ! n)"
2182 assume notx: "n < length xs"
2183 show "xs ! (length xs - Suc n) = rev xs ! n"
2191 assume [simp]: "xs = y # ys"
2193 proof (auto simp add: nth_append)
2194 assume noty: "n < length ys"
2195 from spec [OF ind,of ys]
2196 have "\<forall>n. n < length ys --> ys ! (length ys - Suc n) = rev ys ! n"
2198 hence "n < length ys --> ys ! (length ys - Suc n) = rev ys ! n"
2200 hence "ys ! (length ys - Suc n) = rev ys ! n"
2202 thus "(y # ys) ! (length ys - n) = rev ys ! n"
2203 by (simp add: nth_Cons' noty linorder_not_less [symmetric])
2205 assume "~ n < length ys"
2206 hence x: "length ys \<le> n"
2209 have "n < Suc (length ys)"
2211 hence "n \<le> length ys"
2214 have "length ys = n"
2216 thus "y = [y] ! (n - length ys)"
2221 hence "n < length w --> bv_select w n = rev w ! n"
2227 lemma bv_chop_append: "bv_chop (w1 @ w2) (length w2) = (w1,w2)"
2228 by (simp add: bv_chop_def Let_def)
2230 lemma append_bv_chop_id: "fst (bv_chop w l) @ snd (bv_chop w l) = w"
2231 by (simp add: bv_chop_def Let_def)
2233 lemma bv_chop_length_fst [simp]: "length (fst (bv_chop w i)) = length w - i"
2234 by (simp add: bv_chop_def Let_def)
2236 lemma bv_chop_length_snd [simp]: "length (snd (bv_chop w i)) = min i (length w)"
2237 by (simp add: bv_chop_def Let_def)
2239 lemma bv_slice_length [simp]: "[| j \<le> i; i < length w |] ==> length (bv_slice w (i,j)) = i - j + 1"
2240 by (auto simp add: bv_slice_def)
2243 length_nat :: "nat => nat" where
2244 "length_nat x = (LEAST n. x < 2 ^ n)"
2246 lemma length_nat: "length (nat_to_bv n) = length_nat n"
2247 apply (simp add: length_nat_def)
2248 apply (rule Least_equality [symmetric])
2250 apply (rule length_nat_to_bv_upper_limit)
2254 assume "~ n < 2 ^ length (nat_to_bv n)"
2255 hence "2 ^ length (nat_to_bv n) \<le> n"
2257 hence "length (nat_to_bv n) < length (nat_to_bv n)"
2258 by (rule length_nat_to_bv_lower_limit)
2263 lemma length_nat_0 [simp]: "length_nat 0 = 0"
2264 by (simp add: length_nat_def Least_equality)
2266 lemma length_nat_non0:
2268 shows "length_nat n = Suc (length_nat (n div 2))"
2269 apply (simp add: length_nat [symmetric])
2270 apply (subst nat_to_bv_non0 [of n])
2271 apply (simp_all add: n0)
2275 length_int :: "int => nat" where
2277 (if 0 < x then Suc (length_nat (nat x))
2278 else if x = 0 then 0
2279 else Suc (length_nat (nat (-x - 1))))"
2281 lemma length_int: "length (int_to_bv i) = length_int i"
2282 proof (cases "0 < i")
2284 hence "length (int_to_bv i) = length (norm_signed (\<zero> # norm_unsigned (nat_to_bv (nat i))))"
2286 also from norm_unsigned_result [of "nat_to_bv (nat i)"]
2287 have "... = Suc (length_nat (nat i))"
2289 apply (simp del: norm_unsigned_nat_to_bv)
2290 apply (drule norm_empty_bv_to_nat_zero)
2293 apply (cases "norm_unsigned (nat_to_bv (nat i))")
2294 apply (drule norm_empty_bv_to_nat_zero [of "nat_to_bv (nat i)"])
2299 apply (simp add: length_nat [symmetric])
2301 finally show ?thesis
2303 by (simp add: length_int_def)
2306 hence i0: "i \<le> 0"
2309 proof (cases "i = 0")
2312 by (simp add: length_int_def)
2314 assume "i \<noteq> 0"
2318 hence "length (int_to_bv i) = length (norm_signed (\<one> # bv_not (norm_unsigned (nat_to_bv (nat (- i) - 1)))))"
2319 by (simp add: int_to_bv_def nat_diff_distrib)
2320 also from norm_unsigned_result [of "nat_to_bv (nat (- i) - 1)"]
2321 have "... = Suc (length_nat (nat (- i) - 1))"
2323 apply (simp del: norm_unsigned_nat_to_bv)
2324 apply (drule norm_empty_bv_to_nat_zero [of "nat_to_bv (nat (-i) - Suc 0)"])
2327 apply (cases "- i - 1 = 0")
2329 apply (simp add: length_nat [symmetric])
2330 apply (cases "norm_unsigned (nat_to_bv (nat (- i) - 1))")
2337 by (simp add: length_int_def nat_diff_distrib del: int_to_bv_lt0)
2341 lemma length_int_0 [simp]: "length_int 0 = 0"
2342 by (simp add: length_int_def)
2344 lemma length_int_gt0: "0 < i ==> length_int i = Suc (length_nat (nat i))"
2345 by (simp add: length_int_def)
2347 lemma length_int_lt0: "i < 0 ==> length_int i = Suc (length_nat (nat (- i) - 1))"
2348 by (simp add: length_int_def nat_diff_distrib)
2350 lemma bv_chopI: "[| w = w1 @ w2 ; i = length w2 |] ==> bv_chop w i = (w1,w2)"
2351 by (simp add: bv_chop_def Let_def)
2353 lemma bv_sliceI: "[| j \<le> i ; i < length w ; w = w1 @ w2 @ w3 ; Suc i = length w2 + j ; j = length w3 |] ==> bv_slice w (i,j) = w2"
2354 apply (simp add: bv_slice_def)
2355 apply (subst bv_chopI [of "w1 @ w2 @ w3" w1 "w2 @ w3"])
2359 apply (subst bv_chopI [of "w2 @ w3" w2 w3],simp_all)
2362 lemma bv_slice_bv_slice:
2363 assumes ki: "k \<le> i"
2366 and lw: "l < length w"
2367 shows "bv_slice w (j,i) = bv_slice (bv_slice w (l,k)) (j-k,i-k)"
2369 def w1 == "fst (bv_chop w (Suc l))"
2370 and w2 == "fst (bv_chop (snd (bv_chop w (Suc l))) (Suc j))"
2371 and w3 == "fst (bv_chop (snd (bv_chop (snd (bv_chop w (Suc l))) (Suc j))) i)"
2372 and w4 == "fst (bv_chop (snd (bv_chop (snd (bv_chop (snd (bv_chop w (Suc l))) (Suc j))) i)) k)"
2373 and w5 == "snd (bv_chop (snd (bv_chop (snd (bv_chop (snd (bv_chop w (Suc l))) (Suc j))) i)) k)"
2376 have w_def: "w = w1 @ w2 @ w3 @ w4 @ w5"
2377 by (simp add: w_defs append_bv_chop_id)
2381 apply (subst bv_sliceI [where ?j = i and ?i = j and ?w = w and ?w1.0 = "w1 @ w2" and ?w2.0 = w3 and ?w3.0 = "w4 @ w5"])
2384 apply (simp add: w_defs min_def)
2385 apply (simp add: w_defs min_def)
2386 apply (subst bv_sliceI [where ?j = k and ?i = l and ?w = w and ?w1.0 = w1 and ?w2.0 = "w2 @ w3 @ w4" and ?w3.0 = w5])
2389 apply (simp add: w_defs min_def)
2390 apply (simp add: w_defs min_def)
2391 apply (subst bv_sliceI [where ?j = "i-k" and ?i = "j-k" and ?w = "w2 @ w3 @ w4" and ?w1.0 = w2 and ?w2.0 = w3 and ?w3.0 = w4])
2393 apply (simp_all add: w_defs min_def)
2397 lemma bv_to_nat_extend [simp]: "bv_to_nat (bv_extend n \<zero> w) = bv_to_nat w"
2398 apply (simp add: bv_extend_def)
2399 apply (subst bv_to_nat_dist_append)
2401 apply (induct "n - length w")
2405 lemma bv_msb_extend_same [simp]: "bv_msb w = b ==> bv_msb (bv_extend n b w) = b"
2406 apply (simp add: bv_extend_def)
2407 apply (induct "n - length w")
2411 lemma bv_to_int_extend [simp]:
2412 assumes a: "bv_msb w = b"
2413 shows "bv_to_int (bv_extend n b w) = bv_to_int w"
2414 proof (cases "bv_msb w")
2415 assume [simp]: "bv_msb w = \<zero>"
2416 with a have [simp]: "b = \<zero>"
2419 by (simp add: bv_to_int_def)
2421 assume [simp]: "bv_msb w = \<one>"
2422 with a have [simp]: "b = \<one>"
2425 apply (simp add: bv_to_int_def)
2426 apply (simp add: bv_extend_def)
2427 apply (induct "n - length w",simp_all)
2431 lemma length_nat_mono [simp]: "x \<le> y ==> length_nat x \<le> length_nat y"
2433 assume xy: "x \<le> y"
2434 assume "~ length_nat x \<le> length_nat y"
2435 hence lxly: "length_nat y < length_nat x"
2437 hence "length_nat y < (LEAST n. x < 2 ^ n)"
2438 by (simp add: length_nat_def)
2439 hence "~ x < 2 ^ length_nat y"
2440 by (rule not_less_Least)
2441 hence xx: "2 ^ length_nat y \<le> x"
2443 have yy: "y < 2 ^ length_nat y"
2444 apply (simp add: length_nat_def)
2446 apply (subgoal_tac "y < 2 ^ y",assumption)
2447 apply (cases "0 \<le> y")
2448 apply (induct y,simp_all)
2451 have "y < x" by simp
2457 lemma length_nat_mono_int [simp]: "x \<le> y ==> length_nat x \<le> length_nat y"
2458 apply (rule length_nat_mono)
2462 lemma length_nat_pos [simp,intro!]: "0 < x ==> 0 < length_nat x"
2463 by (simp add: length_nat_non0)
2465 lemma length_int_mono_gt0: "[| 0 \<le> x ; x \<le> y |] ==> length_int x \<le> length_int y"
2466 by (cases "x = 0",simp_all add: length_int_gt0 nat_le_eq_zle)
2468 lemma length_int_mono_lt0: "[| x \<le> y ; y \<le> 0 |] ==> length_int y \<le> length_int x" apply (cases "y = 0",simp_all add: length_int_lt0)
2471 lemmas [simp] = length_nat_non0
2473 lemma "nat_to_bv (number_of Numeral.Pls) = []"
2477 fast_bv_to_nat_helper :: "[bit list, int] => int"
2480 fast_bv_to_nat_Nil: "fast_bv_to_nat_helper [] k = k"
2481 fast_bv_to_nat_Cons: "fast_bv_to_nat_helper (b#bs) k = fast_bv_to_nat_helper bs (k BIT (bit_case bit.B0 bit.B1 b))"
2483 lemma fast_bv_to_nat_Cons0: "fast_bv_to_nat_helper (\<zero>#bs) bin = fast_bv_to_nat_helper bs (bin BIT bit.B0)"
2486 lemma fast_bv_to_nat_Cons1: "fast_bv_to_nat_helper (\<one>#bs) bin = fast_bv_to_nat_helper bs (bin BIT bit.B1)"
2489 lemma fast_bv_to_nat_def: "bv_to_nat bs == number_of (fast_bv_to_nat_helper bs Numeral.Pls)"
2490 proof (simp add: bv_to_nat_def)
2491 have "\<forall> bin. \<not> (neg (number_of bin :: int)) \<longrightarrow> (foldl (%bn b. 2 * bn + bitval b) (number_of bin) bs) = number_of (fast_bv_to_nat_helper bs bin)"
2492 apply (induct bs,simp)
2493 apply (case_tac a,simp_all)
2495 thus "foldl (\<lambda>bn b. 2 * bn + bitval b) 0 bs \<equiv> number_of (fast_bv_to_nat_helper bs Numeral.Pls)"
2496 by (simp del: nat_numeral_0_eq_0 add: nat_numeral_0_eq_0 [symmetric])
2499 declare fast_bv_to_nat_Cons [simp del]
2500 declare fast_bv_to_nat_Cons0 [simp]
2501 declare fast_bv_to_nat_Cons1 [simp]
2504 (*comments containing lcp are the removal of fast_bv_of_nat*)
2506 fun is_const_bool (Const("True",_)) = true
2507 | is_const_bool (Const("False",_)) = true
2508 | is_const_bool _ = false
2509 fun is_const_bit (Const("Word.bit.Zero",_)) = true
2510 | is_const_bit (Const("Word.bit.One",_)) = true
2511 | is_const_bit _ = false
2512 fun num_is_usable (Const("Numeral.Pls",_)) = true
2513 | num_is_usable (Const("Numeral.Min",_)) = false
2514 | num_is_usable (Const("Numeral.Bit",_) $ w $ b) =
2515 num_is_usable w andalso is_const_bool b
2516 | num_is_usable _ = false
2517 fun vec_is_usable (Const("List.list.Nil",_)) = true
2518 | vec_is_usable (Const("List.list.Cons",_) $ b $ bs) =
2519 vec_is_usable bs andalso is_const_bit b
2520 | vec_is_usable _ = false
2521 (*lcp** val fast1_th = PureThy.get_thm thy "Word.fast_nat_to_bv_def"*)
2522 val fast2_th = @{thm "Word.fast_bv_to_nat_def"};
2523 (*lcp** fun f sg thms (Const("Word.nat_to_bv",_) $ (Const(@{const_name Numeral.number_of},_) $ t)) =
2525 then SOME (Drule.cterm_instantiate [(cterm_of sg (Var(("w",0),Type("IntDef.int",[]))),cterm_of sg t)] fast1_th)
2528 fun g sg thms (Const("Word.bv_to_nat",_) $ (t as (Const("List.list.Cons",_) $ _ $ _))) =
2529 if vec_is_usable t then
2530 SOME (Drule.cterm_instantiate [(cterm_of sg (Var(("bs",0),Type("List.list",[Type("Word.bit",[])]))),cterm_of sg t)] fast2_th)
2533 (*lcp** val simproc1 = Simplifier.simproc thy "nat_to_bv" ["Word.nat_to_bv (number_of w)"] f *)
2534 val simproc2 = Simplifier.simproc @{theory} "bv_to_nat" ["Word.bv_to_nat (x # xs)"] g
2536 (fn thy => (Simplifier.change_simpset_of thy (fn ss => ss addsimprocs [(*lcp*simproc1,*)simproc2]);
2540 declare bv_to_nat1 [simp del]
2541 declare bv_to_nat_helper [simp del]
2544 bv_mapzip :: "[bit => bit => bit,bit list, bit list] => bit list" where
2545 "bv_mapzip f w1 w2 =
2546 (let g = bv_extend (max (length w1) (length w2)) \<zero>
2547 in map (split f) (zip (g w1) (g w2)))"
2549 lemma bv_length_bv_mapzip [simp]:
2550 "length (bv_mapzip f w1 w2) = max (length w1) (length w2)"
2551 by (simp add: bv_mapzip_def Let_def split: split_max)
2553 lemma bv_mapzip_Nil [simp]: "bv_mapzip f [] [] = []"
2554 by (simp add: bv_mapzip_def Let_def)
2556 lemma bv_mapzip_Cons [simp]: "length w1 = length w2 ==>
2557 bv_mapzip f (x#w1) (y#w2) = f x y # bv_mapzip f w1 w2"
2558 by (simp add: bv_mapzip_def Let_def)