author | nipkow |
Tue, 16 May 2017 11:40:08 +0200 | |
changeset 65842 | 42420ae446a2 |
parent 65363 | 5eb619751b14 |
child 67120 | 491fd7f0b5df |
permissions | -rw-r--r-- |
65363 | 1 |
(* Title: HOL/Word/Bool_List_Representation.thy |
2 |
Author: Jeremy Dawson, NICTA |
|
24333 | 3 |
|
65363 | 4 |
Theorems to do with integers, expressed using Pls, Min, BIT, |
5 |
theorems linking them to lists of booleans, and repeated splitting |
|
6 |
and concatenation. |
|
7 |
*) |
|
24333 | 8 |
|
58874 | 9 |
section "Bool lists and integers" |
24333 | 10 |
|
37658 | 11 |
theory Bool_List_Representation |
65363 | 12 |
imports Bits_Int |
26557 | 13 |
begin |
24333 | 14 |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
15 |
definition map2 :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> 'c list" |
65363 | 16 |
where "map2 f as bs = map (case_prod f) (zip as bs)" |
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
17 |
|
65363 | 18 |
lemma map2_Nil [simp, code]: "map2 f [] ys = []" |
19 |
by (auto simp: map2_def) |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
20 |
|
65363 | 21 |
lemma map2_Nil2 [simp, code]: "map2 f xs [] = []" |
22 |
by (auto simp: map2_def) |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
23 |
|
65363 | 24 |
lemma map2_Cons [simp, code]: "map2 f (x # xs) (y # ys) = f x y # map2 f xs ys" |
25 |
by (auto simp: map2_def) |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
26 |
|
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
27 |
|
61799 | 28 |
subsection \<open>Operations on lists of booleans\<close> |
37657 | 29 |
|
54848 | 30 |
primrec bl_to_bin_aux :: "bool list \<Rightarrow> int \<Rightarrow> int" |
65363 | 31 |
where |
32 |
Nil: "bl_to_bin_aux [] w = w" |
|
33 |
| Cons: "bl_to_bin_aux (b # bs) w = bl_to_bin_aux bs (w BIT b)" |
|
37657 | 34 |
|
54848 | 35 |
definition bl_to_bin :: "bool list \<Rightarrow> int" |
65363 | 36 |
where "bl_to_bin bs = bl_to_bin_aux bs 0" |
37667 | 37 |
|
54848 | 38 |
primrec bin_to_bl_aux :: "nat \<Rightarrow> int \<Rightarrow> bool list \<Rightarrow> bool list" |
65363 | 39 |
where |
40 |
Z: "bin_to_bl_aux 0 w bl = bl" |
|
41 |
| Suc: "bin_to_bl_aux (Suc n) w bl = bin_to_bl_aux n (bin_rest w) ((bin_last w) # bl)" |
|
37657 | 42 |
|
54848 | 43 |
definition bin_to_bl :: "nat \<Rightarrow> int \<Rightarrow> bool list" |
65363 | 44 |
where "bin_to_bl n w = bin_to_bl_aux n w []" |
37657 | 45 |
|
54848 | 46 |
primrec bl_of_nth :: "nat \<Rightarrow> (nat \<Rightarrow> bool) \<Rightarrow> bool list" |
65363 | 47 |
where |
48 |
Suc: "bl_of_nth (Suc n) f = f n # bl_of_nth n f" |
|
37657 | 49 |
| Z: "bl_of_nth 0 f = []" |
50 |
||
54848 | 51 |
primrec takefill :: "'a \<Rightarrow> nat \<Rightarrow> 'a list \<Rightarrow> 'a list" |
52 |
where |
|
65363 | 53 |
Z: "takefill fill 0 xs = []" |
54 |
| Suc: "takefill fill (Suc n) xs = |
|
55 |
(case xs of |
|
56 |
[] \<Rightarrow> fill # takefill fill n xs |
|
57 |
| y # ys \<Rightarrow> y # takefill fill n ys)" |
|
37657 | 58 |
|
59 |
||
24465 | 60 |
subsection "Arithmetic in terms of bool lists" |
61 |
||
61799 | 62 |
text \<open> |
44939
5930d35c976d
removed unused legacy lemma names, some comment cleanup.
kleing
parents:
41842
diff
changeset
|
63 |
Arithmetic operations in terms of the reversed bool list, |
65363 | 64 |
assuming input list(s) the same length, and don't extend them. |
61799 | 65 |
\<close> |
24465 | 66 |
|
65363 | 67 |
primrec rbl_succ :: "bool list \<Rightarrow> bool list" |
68 |
where |
|
69 |
Nil: "rbl_succ Nil = Nil" |
|
26557 | 70 |
| Cons: "rbl_succ (x # xs) = (if x then False # rbl_succ xs else True # xs)" |
24465 | 71 |
|
65363 | 72 |
primrec rbl_pred :: "bool list \<Rightarrow> bool list" |
73 |
where |
|
74 |
Nil: "rbl_pred Nil = Nil" |
|
26557 | 75 |
| Cons: "rbl_pred (x # xs) = (if x then False # xs else True # rbl_pred xs)" |
24465 | 76 |
|
65363 | 77 |
primrec rbl_add :: "bool list \<Rightarrow> bool list \<Rightarrow> bool list" |
78 |
where \<comment> "result is length of first arg, second arg may be longer" |
|
79 |
Nil: "rbl_add Nil x = Nil" |
|
80 |
| Cons: "rbl_add (y # ys) x = |
|
81 |
(let ws = rbl_add ys (tl x) |
|
82 |
in (y \<noteq> hd x) # (if hd x \<and> y then rbl_succ ws else ws))" |
|
24465 | 83 |
|
65363 | 84 |
primrec rbl_mult :: "bool list \<Rightarrow> bool list \<Rightarrow> bool list" |
85 |
where \<comment> "result is length of first arg, second arg may be longer" |
|
86 |
Nil: "rbl_mult Nil x = Nil" |
|
87 |
| Cons: "rbl_mult (y # ys) x = |
|
88 |
(let ws = False # rbl_mult ys x |
|
89 |
in if y then rbl_add ws x else ws)" |
|
24333 | 90 |
|
65363 | 91 |
lemma butlast_power: "(butlast ^^ n) bl = take (length bl - n) bl" |
24333 | 92 |
by (induct n) (auto simp: butlast_take) |
93 |
||
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
45997
diff
changeset
|
94 |
lemma bin_to_bl_aux_zero_minus_simp [simp]: |
65363 | 95 |
"0 < n \<Longrightarrow> bin_to_bl_aux n 0 bl = bin_to_bl_aux (n - 1) 0 (False # bl)" |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
45997
diff
changeset
|
96 |
by (cases n) auto |
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
45997
diff
changeset
|
97 |
|
46617
8c5d10d41391
make bool list functions respect int/bin distinction
huffman
parents:
46240
diff
changeset
|
98 |
lemma bin_to_bl_aux_minus1_minus_simp [simp]: |
65363 | 99 |
"0 < n \<Longrightarrow> bin_to_bl_aux n (- 1) bl = bin_to_bl_aux (n - 1) (- 1) (True # bl)" |
24333 | 100 |
by (cases n) auto |
101 |
||
46617
8c5d10d41391
make bool list functions respect int/bin distinction
huffman
parents:
46240
diff
changeset
|
102 |
lemma bin_to_bl_aux_one_minus_simp [simp]: |
65363 | 103 |
"0 < n \<Longrightarrow> bin_to_bl_aux n 1 bl = bin_to_bl_aux (n - 1) 0 (True # bl)" |
24333 | 104 |
by (cases n) auto |
105 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26008
diff
changeset
|
106 |
lemma bin_to_bl_aux_Bit_minus_simp [simp]: |
65363 | 107 |
"0 < n \<Longrightarrow> bin_to_bl_aux n (w BIT b) bl = bin_to_bl_aux (n - 1) w (b # bl)" |
24333 | 108 |
by (cases n) auto |
109 |
||
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26008
diff
changeset
|
110 |
lemma bin_to_bl_aux_Bit0_minus_simp [simp]: |
65363 | 111 |
"0 < n \<Longrightarrow> |
112 |
bin_to_bl_aux n (numeral (Num.Bit0 w)) bl = bin_to_bl_aux (n - 1) (numeral w) (False # bl)" |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26008
diff
changeset
|
113 |
by (cases n) auto |
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26008
diff
changeset
|
114 |
|
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26008
diff
changeset
|
115 |
lemma bin_to_bl_aux_Bit1_minus_simp [simp]: |
65363 | 116 |
"0 < n \<Longrightarrow> |
117 |
bin_to_bl_aux n (numeral (Num.Bit1 w)) bl = bin_to_bl_aux (n - 1) (numeral w) (True # bl)" |
|
26086
3c243098b64a
New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents:
26008
diff
changeset
|
118 |
by (cases n) auto |
24333 | 119 |
|
61799 | 120 |
text \<open>Link between bin and bool list.\<close> |
24465 | 121 |
|
65363 | 122 |
lemma bl_to_bin_aux_append: "bl_to_bin_aux (bs @ cs) w = bl_to_bin_aux cs (bl_to_bin_aux bs w)" |
26557 | 123 |
by (induct bs arbitrary: w) auto |
24465 | 124 |
|
65363 | 125 |
lemma bin_to_bl_aux_append: "bin_to_bl_aux n w bs @ cs = bin_to_bl_aux n w (bs @ cs)" |
26557 | 126 |
by (induct n arbitrary: w bs) auto |
24333 | 127 |
|
65363 | 128 |
lemma bl_to_bin_append: "bl_to_bin (bs @ cs) = bl_to_bin_aux cs (bl_to_bin bs)" |
24465 | 129 |
unfolding bl_to_bin_def by (rule bl_to_bin_aux_append) |
130 |
||
65363 | 131 |
lemma bin_to_bl_aux_alt: "bin_to_bl_aux n w bs = bin_to_bl n w @ bs" |
132 |
by (simp add: bin_to_bl_def bin_to_bl_aux_append) |
|
24333 | 133 |
|
45996
e69d631fe9af
declare simp rules immediately, instead of using 'declare' commands
huffman
parents:
45854
diff
changeset
|
134 |
lemma bin_to_bl_0 [simp]: "bin_to_bl 0 bs = []" |
65363 | 135 |
by (auto simp: bin_to_bl_def) |
24333 | 136 |
|
65363 | 137 |
lemma size_bin_to_bl_aux: "size (bin_to_bl_aux n w bs) = n + length bs" |
26557 | 138 |
by (induct n arbitrary: w bs) auto |
24333 | 139 |
|
65363 | 140 |
lemma size_bin_to_bl [simp]: "size (bin_to_bl n w) = n" |
141 |
by (simp add: bin_to_bl_def size_bin_to_bl_aux) |
|
24333 | 142 |
|
65363 | 143 |
lemma bin_bl_bin': "bl_to_bin (bin_to_bl_aux n w bs) = bl_to_bin_aux bs (bintrunc n w)" |
144 |
by (induct n arbitrary: w bs) (auto simp: bl_to_bin_def) |
|
24465 | 145 |
|
45996
e69d631fe9af
declare simp rules immediately, instead of using 'declare' commands
huffman
parents:
45854
diff
changeset
|
146 |
lemma bin_bl_bin [simp]: "bl_to_bin (bin_to_bl n w) = bintrunc n w" |
65363 | 147 |
by (auto simp: bin_to_bl_def bin_bl_bin') |
24465 | 148 |
|
65363 | 149 |
lemma bl_bin_bl': "bin_to_bl (n + length bs) (bl_to_bin_aux bs w) = bin_to_bl_aux n w bs" |
26557 | 150 |
apply (induct bs arbitrary: w n) |
24465 | 151 |
apply auto |
65363 | 152 |
apply (simp_all only: add_Suc [symmetric]) |
153 |
apply (auto simp add: bin_to_bl_def) |
|
24465 | 154 |
done |
155 |
||
45996
e69d631fe9af
declare simp rules immediately, instead of using 'declare' commands
huffman
parents:
45854
diff
changeset
|
156 |
lemma bl_bin_bl [simp]: "bin_to_bl (length bs) (bl_to_bin bs) = bs" |
24465 | 157 |
unfolding bl_to_bin_def |
158 |
apply (rule box_equals) |
|
159 |
apply (rule bl_bin_bl') |
|
160 |
prefer 2 |
|
161 |
apply (rule bin_to_bl_aux.Z) |
|
162 |
apply simp |
|
163 |
done |
|
65363 | 164 |
|
165 |
lemma bl_to_bin_inj: "bl_to_bin bs = bl_to_bin cs \<Longrightarrow> length bs = length cs \<Longrightarrow> bs = cs" |
|
24465 | 166 |
apply (rule_tac box_equals) |
167 |
defer |
|
168 |
apply (rule bl_bin_bl) |
|
169 |
apply (rule bl_bin_bl) |
|
170 |
apply simp |
|
171 |
done |
|
172 |
||
45996
e69d631fe9af
declare simp rules immediately, instead of using 'declare' commands
huffman
parents:
45854
diff
changeset
|
173 |
lemma bl_to_bin_False [simp]: "bl_to_bin (False # bl) = bl_to_bin bl" |
65363 | 174 |
by (auto simp: bl_to_bin_def) |
45996
e69d631fe9af
declare simp rules immediately, instead of using 'declare' commands
huffman
parents:
45854
diff
changeset
|
175 |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
45997
diff
changeset
|
176 |
lemma bl_to_bin_Nil [simp]: "bl_to_bin [] = 0" |
65363 | 177 |
by (auto simp: bl_to_bin_def) |
24465 | 178 |
|
65363 | 179 |
lemma bin_to_bl_zero_aux: "bin_to_bl_aux n 0 bl = replicate n False @ bl" |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
45997
diff
changeset
|
180 |
by (induct n arbitrary: bl) (auto simp: replicate_app_Cons_same) |
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
45997
diff
changeset
|
181 |
|
46617
8c5d10d41391
make bool list functions respect int/bin distinction
huffman
parents:
46240
diff
changeset
|
182 |
lemma bin_to_bl_zero: "bin_to_bl n 0 = replicate n False" |
65363 | 183 |
by (simp add: bin_to_bl_def bin_to_bl_zero_aux) |
46617
8c5d10d41391
make bool list functions respect int/bin distinction
huffman
parents:
46240
diff
changeset
|
184 |
|
65363 | 185 |
lemma bin_to_bl_minus1_aux: "bin_to_bl_aux n (- 1) bl = replicate n True @ bl" |
26557 | 186 |
by (induct n arbitrary: bl) (auto simp: replicate_app_Cons_same) |
24333 | 187 |
|
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
57514
diff
changeset
|
188 |
lemma bin_to_bl_minus1: "bin_to_bl n (- 1) = replicate n True" |
65363 | 189 |
by (simp add: bin_to_bl_def bin_to_bl_minus1_aux) |
24333 | 190 |
|
65363 | 191 |
lemma bl_to_bin_rep_F: "bl_to_bin (replicate n False @ bl) = bl_to_bin bl" |
192 |
by (simp add: bin_to_bl_zero_aux [symmetric] bin_bl_bin') (simp add: bl_to_bin_def) |
|
24465 | 193 |
|
65363 | 194 |
lemma bin_to_bl_trunc [simp]: "n \<le> m \<Longrightarrow> bin_to_bl n (bintrunc m w) = bin_to_bl n w" |
24465 | 195 |
by (auto intro: bl_to_bin_inj) |
196 |
||
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
197 |
lemma bin_to_bl_aux_bintr: |
65363 | 198 |
"bin_to_bl_aux n (bintrunc m bin) bl = |
24333 | 199 |
replicate (n - m) False @ bin_to_bl_aux (min n m) bin bl" |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
200 |
apply (induct n arbitrary: m bin bl) |
24333 | 201 |
apply clarsimp |
202 |
apply clarsimp |
|
203 |
apply (case_tac "m") |
|
65363 | 204 |
apply (clarsimp simp: bin_to_bl_zero_aux) |
24333 | 205 |
apply (erule thin_rl) |
65363 | 206 |
apply (induct_tac n) |
24333 | 207 |
apply auto |
208 |
done |
|
209 |
||
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
210 |
lemma bin_to_bl_bintr: |
65363 | 211 |
"bin_to_bl n (bintrunc m bin) = replicate (n - m) False @ bin_to_bl (min n m) bin" |
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
212 |
unfolding bin_to_bl_def by (rule bin_to_bl_aux_bintr) |
24333 | 213 |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
45997
diff
changeset
|
214 |
lemma bl_to_bin_rep_False: "bl_to_bin (replicate n False) = 0" |
24465 | 215 |
by (induct n) auto |
216 |
||
65363 | 217 |
lemma len_bin_to_bl_aux: "length (bin_to_bl_aux n w bs) = n + length bs" |
54869 | 218 |
by (fact size_bin_to_bl_aux) |
24333 | 219 |
|
65363 | 220 |
lemma len_bin_to_bl: "length (bin_to_bl n w) = n" |
46617
8c5d10d41391
make bool list functions respect int/bin distinction
huffman
parents:
46240
diff
changeset
|
221 |
by (fact size_bin_to_bl) (* FIXME: duplicate *) |
65363 | 222 |
|
223 |
lemma sign_bl_bin': "bin_sign (bl_to_bin_aux bs w) = bin_sign w" |
|
26557 | 224 |
by (induct bs arbitrary: w) auto |
65363 | 225 |
|
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
45997
diff
changeset
|
226 |
lemma sign_bl_bin: "bin_sign (bl_to_bin bs) = 0" |
65363 | 227 |
by (simp add: bl_to_bin_def sign_bl_bin') |
228 |
||
229 |
lemma bl_sbin_sign_aux: "hd (bin_to_bl_aux (Suc n) w bs) = (bin_sign (sbintrunc n w) = -1)" |
|
26557 | 230 |
apply (induct n arbitrary: w bs) |
24333 | 231 |
apply clarsimp |
26557 | 232 |
apply (cases w rule: bin_exhaust) |
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
53438
diff
changeset
|
233 |
apply simp |
24333 | 234 |
done |
65363 | 235 |
|
236 |
lemma bl_sbin_sign: "hd (bin_to_bl (Suc n) w) = (bin_sign (sbintrunc n w) = -1)" |
|
24333 | 237 |
unfolding bin_to_bl_def by (rule bl_sbin_sign_aux) |
238 |
||
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
239 |
lemma bin_nth_of_bl_aux: |
65363 | 240 |
"bin_nth (bl_to_bin_aux bl w) n = |
241 |
(n < size bl \<and> rev bl ! n | n \<ge> length bl \<and> bin_nth w (n - size bl))" |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
242 |
apply (induct bl arbitrary: w) |
24333 | 243 |
apply clarsimp |
244 |
apply clarsimp |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
245 |
apply (cut_tac x=n and y="size bl" in linorder_less_linear) |
24333 | 246 |
apply (erule disjE, simp add: nth_append)+ |
26557 | 247 |
apply auto |
24333 | 248 |
done |
249 |
||
65363 | 250 |
lemma bin_nth_of_bl: "bin_nth (bl_to_bin bl) n = (n < length bl \<and> rev bl ! n)" |
251 |
by (simp add: bl_to_bin_def bin_nth_of_bl_aux) |
|
24333 | 252 |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
253 |
lemma bin_nth_bl: "n < m \<Longrightarrow> bin_nth w n = nth (rev (bin_to_bl m w)) n" |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
254 |
apply (induct n arbitrary: m w) |
24333 | 255 |
apply clarsimp |
256 |
apply (case_tac m, clarsimp) |
|
257 |
apply (clarsimp simp: bin_to_bl_def) |
|
258 |
apply (simp add: bin_to_bl_aux_alt) |
|
259 |
apply clarsimp |
|
260 |
apply (case_tac m, clarsimp) |
|
261 |
apply (clarsimp simp: bin_to_bl_def) |
|
262 |
apply (simp add: bin_to_bl_aux_alt) |
|
263 |
done |
|
264 |
||
65363 | 265 |
lemma nth_rev: "n < length xs \<Longrightarrow> rev xs ! n = xs ! (length xs - 1 - n)" |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
266 |
apply (induct xs) |
24465 | 267 |
apply simp |
65363 | 268 |
apply (clarsimp simp add: nth_append nth.simps split: nat.split) |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
269 |
apply (rule_tac f = "\<lambda>n. xs ! n" in arg_cong) |
24465 | 270 |
apply arith |
271 |
done |
|
272 |
||
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
273 |
lemma nth_rev_alt: "n < length ys \<Longrightarrow> ys ! n = rev ys ! (length ys - Suc n)" |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
274 |
by (simp add: nth_rev) |
24465 | 275 |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
276 |
lemma nth_bin_to_bl_aux: |
65363 | 277 |
"n < m + length bl \<Longrightarrow> (bin_to_bl_aux m w bl) ! n = |
24333 | 278 |
(if n < m then bin_nth w (m - 1 - n) else bl ! (n - m))" |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
279 |
apply (induct m arbitrary: w n bl) |
24333 | 280 |
apply clarsimp |
281 |
apply clarsimp |
|
282 |
apply (case_tac w rule: bin_exhaust) |
|
283 |
apply simp |
|
284 |
done |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
285 |
|
65363 | 286 |
lemma nth_bin_to_bl: "n < m \<Longrightarrow> (bin_to_bl m w) ! n = bin_nth w (m - Suc n)" |
287 |
by (simp add: bin_to_bl_def nth_bin_to_bl_aux) |
|
24333 | 288 |
|
65363 | 289 |
lemma bl_to_bin_lt2p_aux: "bl_to_bin_aux bs w < (w + 1) * (2 ^ length bs)" |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
290 |
apply (induct bs arbitrary: w) |
24333 | 291 |
apply clarsimp |
292 |
apply clarsimp |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
293 |
apply (drule meta_spec, erule xtrans(8) [rotated], simp add: Bit_def)+ |
24333 | 294 |
done |
295 |
||
65363 | 296 |
lemma bl_to_bin_lt2p_drop: "bl_to_bin bs < 2 ^ length (dropWhile Not bs)" |
62701 | 297 |
proof (induct bs) |
65363 | 298 |
case Nil |
299 |
then show ?case by simp |
|
300 |
next |
|
62701 | 301 |
case (Cons b bs) with bl_to_bin_lt2p_aux[where w=1] |
302 |
show ?case unfolding bl_to_bin_def by simp |
|
65363 | 303 |
qed |
62701 | 304 |
|
305 |
lemma bl_to_bin_lt2p: "bl_to_bin bs < 2 ^ length bs" |
|
306 |
by (metis bin_bl_bin bintr_lt2p bl_bin_bl) |
|
24333 | 307 |
|
65363 | 308 |
lemma bl_to_bin_ge2p_aux: "bl_to_bin_aux bs w \<ge> w * (2 ^ length bs)" |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
309 |
apply (induct bs arbitrary: w) |
24333 | 310 |
apply clarsimp |
311 |
apply clarsimp |
|
46652 | 312 |
apply (drule meta_spec, erule order_trans [rotated], |
313 |
simp add: Bit_B0_2t Bit_B1_2t algebra_simps)+ |
|
54847
d6cf9a5b9be9
prefer plain bool over dedicated type for binary digits
haftmann
parents:
53438
diff
changeset
|
314 |
apply (simp add: Bit_def) |
24333 | 315 |
done |
316 |
||
65363 | 317 |
lemma bl_to_bin_ge0: "bl_to_bin bs \<ge> 0" |
24333 | 318 |
apply (unfold bl_to_bin_def) |
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
319 |
apply (rule xtrans(4)) |
24333 | 320 |
apply (rule bl_to_bin_ge2p_aux) |
46617
8c5d10d41391
make bool list functions respect int/bin distinction
huffman
parents:
46240
diff
changeset
|
321 |
apply simp |
24333 | 322 |
done |
323 |
||
65363 | 324 |
lemma butlast_rest_bin: "butlast (bin_to_bl n w) = bin_to_bl (n - 1) (bin_rest w)" |
24333 | 325 |
apply (unfold bin_to_bl_def) |
326 |
apply (cases w rule: bin_exhaust) |
|
327 |
apply (cases n, clarsimp) |
|
328 |
apply clarsimp |
|
329 |
apply (auto simp add: bin_to_bl_aux_alt) |
|
330 |
done |
|
331 |
||
65363 | 332 |
lemma butlast_bin_rest: "butlast bl = bin_to_bl (length bl - Suc 0) (bin_rest (bl_to_bin bl))" |
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
333 |
using butlast_rest_bin [where w="bl_to_bin bl" and n="length bl"] by simp |
24333 | 334 |
|
26557 | 335 |
lemma butlast_rest_bl2bin_aux: |
65363 | 336 |
"bl \<noteq> [] \<Longrightarrow> bl_to_bin_aux (butlast bl) w = bin_rest (bl_to_bin_aux bl w)" |
26557 | 337 |
by (induct bl arbitrary: w) auto |
65363 | 338 |
|
339 |
lemma butlast_rest_bl2bin: "bl_to_bin (butlast bl) = bin_rest (bl_to_bin bl)" |
|
340 |
by (cases bl) (auto simp: bl_to_bin_def butlast_rest_bl2bin_aux) |
|
24333 | 341 |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
342 |
lemma trunc_bl2bin_aux: |
65363 | 343 |
"bintrunc m (bl_to_bin_aux bl w) = |
26557 | 344 |
bl_to_bin_aux (drop (length bl - m) bl) (bintrunc (m - length bl) w)" |
53438 | 345 |
proof (induct bl arbitrary: w) |
65363 | 346 |
case Nil |
347 |
show ?case by simp |
|
53438 | 348 |
next |
65363 | 349 |
case (Cons b bl) |
350 |
show ?case |
|
53438 | 351 |
proof (cases "m - length bl") |
65363 | 352 |
case 0 |
353 |
then have "Suc (length bl) - m = Suc (length bl - m)" by simp |
|
53438 | 354 |
with Cons show ?thesis by simp |
355 |
next |
|
65363 | 356 |
case (Suc n) |
357 |
then have "m - Suc (length bl) = n" by simp |
|
358 |
with Cons Suc show ?thesis by simp |
|
53438 | 359 |
qed |
360 |
qed |
|
24333 | 361 |
|
65363 | 362 |
lemma trunc_bl2bin: "bintrunc m (bl_to_bin bl) = bl_to_bin (drop (length bl - m) bl)" |
363 |
by (simp add: bl_to_bin_def trunc_bl2bin_aux) |
|
364 |
||
365 |
lemma trunc_bl2bin_len [simp]: "bintrunc (length bl) (bl_to_bin bl) = bl_to_bin bl" |
|
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
366 |
by (simp add: trunc_bl2bin) |
24333 | 367 |
|
65363 | 368 |
lemma bl2bin_drop: "bl_to_bin (drop k bl) = bintrunc (length bl - k) (bl_to_bin bl)" |
24333 | 369 |
apply (rule trans) |
370 |
prefer 2 |
|
371 |
apply (rule trunc_bl2bin [symmetric]) |
|
65363 | 372 |
apply (cases "k \<le> length bl") |
24333 | 373 |
apply auto |
374 |
done |
|
375 |
||
65363 | 376 |
lemma nth_rest_power_bin: "bin_nth ((bin_rest ^^ k) w) n = bin_nth w (n + k)" |
377 |
apply (induct k arbitrary: n) |
|
378 |
apply clarsimp |
|
24333 | 379 |
apply clarsimp |
380 |
apply (simp only: bin_nth.Suc [symmetric] add_Suc) |
|
381 |
done |
|
382 |
||
65363 | 383 |
lemma take_rest_power_bin: "m \<le> n \<Longrightarrow> take m (bin_to_bl n w) = bin_to_bl m ((bin_rest ^^ (n - m)) w)" |
24333 | 384 |
apply (rule nth_equalityI) |
385 |
apply simp |
|
386 |
apply (clarsimp simp add: nth_bin_to_bl nth_rest_power_bin) |
|
387 |
done |
|
388 |
||
65363 | 389 |
lemma hd_butlast: "size xs > 1 \<Longrightarrow> hd (butlast xs) = hd xs" |
24465 | 390 |
by (cases xs) auto |
24333 | 391 |
|
65363 | 392 |
lemma last_bin_last': "size xs > 0 \<Longrightarrow> last xs \<longleftrightarrow> bin_last (bl_to_bin_aux xs w)" |
26557 | 393 |
by (induct xs arbitrary: w) auto |
24333 | 394 |
|
65363 | 395 |
lemma last_bin_last: "size xs > 0 \<Longrightarrow> last xs \<longleftrightarrow> bin_last (bl_to_bin xs)" |
24333 | 396 |
unfolding bl_to_bin_def by (erule last_bin_last') |
65363 | 397 |
|
398 |
lemma bin_last_last: "bin_last w \<longleftrightarrow> last (bin_to_bl (Suc n) w)" |
|
399 |
by (simp add: bin_to_bl_def) (auto simp: bin_to_bl_aux_alt) |
|
400 |
||
24333 | 401 |
|
65363 | 402 |
subsection \<open>Links between bit-wise operations and operations on bool lists\<close> |
403 |
||
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
404 |
lemma bl_xor_aux_bin: |
65363 | 405 |
"map2 (\<lambda>x y. x \<noteq> y) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = |
406 |
bin_to_bl_aux n (v XOR w) (map2 (\<lambda>x y. x \<noteq> y) bs cs)" |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
407 |
apply (induct n arbitrary: v w bs cs) |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
408 |
apply simp |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
409 |
apply (case_tac v rule: bin_exhaust) |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
410 |
apply (case_tac w rule: bin_exhaust) |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
411 |
apply clarsimp |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
412 |
apply (case_tac b) |
65363 | 413 |
apply auto |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
414 |
done |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
415 |
|
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
416 |
lemma bl_or_aux_bin: |
65363 | 417 |
"map2 (op \<or>) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = |
418 |
bin_to_bl_aux n (v OR w) (map2 (op \<or>) bs cs)" |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
419 |
apply (induct n arbitrary: v w bs cs) |
24333 | 420 |
apply simp |
421 |
apply (case_tac v rule: bin_exhaust) |
|
422 |
apply (case_tac w rule: bin_exhaust) |
|
423 |
apply clarsimp |
|
424 |
done |
|
65363 | 425 |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
426 |
lemma bl_and_aux_bin: |
65363 | 427 |
"map2 (op \<and>) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = |
428 |
bin_to_bl_aux n (v AND w) (map2 (op \<and>) bs cs)" |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
429 |
apply (induct n arbitrary: v w bs cs) |
24333 | 430 |
apply simp |
431 |
apply (case_tac v rule: bin_exhaust) |
|
432 |
apply (case_tac w rule: bin_exhaust) |
|
433 |
apply clarsimp |
|
434 |
done |
|
65363 | 435 |
|
436 |
lemma bl_not_aux_bin: "map Not (bin_to_bl_aux n w cs) = bin_to_bl_aux n (NOT w) (map Not cs)" |
|
437 |
by (induct n arbitrary: w cs) auto |
|
24333 | 438 |
|
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
439 |
lemma bl_not_bin: "map Not (bin_to_bl n w) = bin_to_bl n (NOT w)" |
65363 | 440 |
by (simp add: bin_to_bl_def bl_not_aux_bin) |
24333 | 441 |
|
65363 | 442 |
lemma bl_and_bin: "map2 (op \<and>) (bin_to_bl n v) (bin_to_bl n w) = bin_to_bl n (v AND w)" |
443 |
by (simp add: bin_to_bl_def bl_and_aux_bin) |
|
24333 | 444 |
|
65363 | 445 |
lemma bl_or_bin: "map2 (op \<or>) (bin_to_bl n v) (bin_to_bl n w) = bin_to_bl n (v OR w)" |
446 |
by (simp add: bin_to_bl_def bl_or_aux_bin) |
|
24333 | 447 |
|
65363 | 448 |
lemma bl_xor_bin: "map2 (\<lambda>x y. x \<noteq> y) (bin_to_bl n v) (bin_to_bl n w) = bin_to_bl n (v XOR w)" |
449 |
by (simp only: bin_to_bl_def bl_xor_aux_bin map2_Nil) |
|
24333 | 450 |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
451 |
lemma drop_bin2bl_aux: |
65363 | 452 |
"drop m (bin_to_bl_aux n bin bs) = |
24333 | 453 |
bin_to_bl_aux (n - m) bin (drop (m - n) bs)" |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
454 |
apply (induct n arbitrary: m bin bs, clarsimp) |
24333 | 455 |
apply clarsimp |
456 |
apply (case_tac bin rule: bin_exhaust) |
|
65363 | 457 |
apply (case_tac "m \<le> n", simp) |
24333 | 458 |
apply (case_tac "m - n", simp) |
459 |
apply simp |
|
65363 | 460 |
apply (rule_tac f = "\<lambda>nat. drop nat bs" in arg_cong) |
24333 | 461 |
apply simp |
462 |
done |
|
463 |
||
464 |
lemma drop_bin2bl: "drop m (bin_to_bl n bin) = bin_to_bl (n - m) bin" |
|
65363 | 465 |
by (simp add: bin_to_bl_def drop_bin2bl_aux) |
24333 | 466 |
|
65363 | 467 |
lemma take_bin2bl_lem1: "take m (bin_to_bl_aux m w bs) = bin_to_bl m w" |
468 |
apply (induct m arbitrary: w bs) |
|
469 |
apply clarsimp |
|
24333 | 470 |
apply clarsimp |
471 |
apply (simp add: bin_to_bl_aux_alt) |
|
472 |
apply (simp add: bin_to_bl_def) |
|
473 |
apply (simp add: bin_to_bl_aux_alt) |
|
474 |
done |
|
475 |
||
65363 | 476 |
lemma take_bin2bl_lem: "take m (bin_to_bl_aux (m + n) w bs) = take m (bin_to_bl (m + n) w)" |
477 |
by (induct n arbitrary: w bs) (simp_all (no_asm) add: bin_to_bl_def take_bin2bl_lem1, simp) |
|
24333 | 478 |
|
65363 | 479 |
lemma bin_split_take: "bin_split n c = (a, b) \<Longrightarrow> bin_to_bl m a = take m (bin_to_bl (m + n) c)" |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
480 |
apply (induct n arbitrary: b c) |
24333 | 481 |
apply clarsimp |
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
482 |
apply (clarsimp simp: Let_def split: prod.split_asm) |
24333 | 483 |
apply (simp add: bin_to_bl_def) |
484 |
apply (simp add: take_bin2bl_lem) |
|
485 |
done |
|
486 |
||
65363 | 487 |
lemma bin_split_take1: |
488 |
"k = m + n \<Longrightarrow> bin_split n c = (a, b) \<Longrightarrow> bin_to_bl m a = take m (bin_to_bl k c)" |
|
24333 | 489 |
by (auto elim: bin_split_take) |
65363 | 490 |
|
491 |
lemma nth_takefill: "m < n \<Longrightarrow> takefill fill n l ! m = (if m < length l then l ! m else fill)" |
|
492 |
apply (induct n arbitrary: m l) |
|
493 |
apply clarsimp |
|
24333 | 494 |
apply clarsimp |
495 |
apply (case_tac m) |
|
496 |
apply (simp split: list.split) |
|
497 |
apply (simp split: list.split) |
|
498 |
done |
|
499 |
||
65363 | 500 |
lemma takefill_alt: "takefill fill n l = take n l @ replicate (n - length l) fill" |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
501 |
by (induct n arbitrary: l) (auto split: list.split) |
24333 | 502 |
|
65363 | 503 |
lemma takefill_replicate [simp]: "takefill fill n (replicate m fill) = replicate n fill" |
504 |
by (simp add: takefill_alt replicate_add [symmetric]) |
|
24333 | 505 |
|
65363 | 506 |
lemma takefill_le': "n = m + k \<Longrightarrow> takefill x m (takefill x n l) = takefill x m l" |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
507 |
by (induct m arbitrary: l n) (auto split: list.split) |
24333 | 508 |
|
509 |
lemma length_takefill [simp]: "length (takefill fill n l) = n" |
|
65363 | 510 |
by (simp add: takefill_alt) |
24333 | 511 |
|
65363 | 512 |
lemma take_takefill': "\<And>w n. n = k + m \<Longrightarrow> take k (takefill fill n w) = takefill fill k w" |
513 |
by (induct k) (auto split: list.split) |
|
24333 | 514 |
|
65363 | 515 |
lemma drop_takefill: "\<And>w. drop k (takefill fill (m + k) w) = takefill fill m (drop k w)" |
516 |
by (induct k) (auto split: list.split) |
|
24333 | 517 |
|
65363 | 518 |
lemma takefill_le [simp]: "m \<le> n \<Longrightarrow> takefill x m (takefill x n l) = takefill x m l" |
24333 | 519 |
by (auto simp: le_iff_add takefill_le') |
520 |
||
65363 | 521 |
lemma take_takefill [simp]: "m \<le> n \<Longrightarrow> take m (takefill fill n w) = takefill fill m w" |
24333 | 522 |
by (auto simp: le_iff_add take_takefill') |
65363 | 523 |
|
524 |
lemma takefill_append: "takefill fill (m + length xs) (xs @ w) = xs @ (takefill fill m w)" |
|
24333 | 525 |
by (induct xs) auto |
526 |
||
65363 | 527 |
lemma takefill_same': "l = length xs \<Longrightarrow> takefill fill l xs = xs" |
528 |
by (induct xs arbitrary: l) auto |
|
529 |
||
24333 | 530 |
lemmas takefill_same [simp] = takefill_same' [OF refl] |
531 |
||
65363 | 532 |
lemma takefill_bintrunc: "takefill False n bl = rev (bin_to_bl n (bl_to_bin (rev bl)))" |
24333 | 533 |
apply (rule nth_equalityI) |
534 |
apply simp |
|
535 |
apply (clarsimp simp: nth_takefill nth_rev nth_bin_to_bl bin_nth_of_bl) |
|
536 |
done |
|
537 |
||
65363 | 538 |
lemma bl_bin_bl_rtf: "bin_to_bl n (bl_to_bin bl) = rev (takefill False n (rev bl))" |
539 |
by (simp add: takefill_bintrunc) |
|
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
540 |
|
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
541 |
lemma bl_bin_bl_rep_drop: |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
542 |
"bin_to_bl n (bl_to_bin bl) = |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
543 |
replicate (n - length bl) False @ drop (length bl - n) bl" |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
544 |
by (simp add: bl_bin_bl_rtf takefill_alt rev_take) |
24333 | 545 |
|
546 |
lemma tf_rev: |
|
65363 | 547 |
"n + k = m + length bl \<Longrightarrow> takefill x m (rev (takefill y n bl)) = |
24333 | 548 |
rev (takefill y m (rev (takefill x k (rev bl))))" |
549 |
apply (rule nth_equalityI) |
|
550 |
apply (auto simp add: nth_takefill nth_rev) |
|
65363 | 551 |
apply (rule_tac f = "\<lambda>n. bl ! n" in arg_cong) |
552 |
apply arith |
|
24333 | 553 |
done |
554 |
||
65363 | 555 |
lemma takefill_minus: "0 < n \<Longrightarrow> takefill fill (Suc (n - 1)) w = takefill fill n w" |
24333 | 556 |
by auto |
557 |
||
65363 | 558 |
lemmas takefill_Suc_cases = |
45604 | 559 |
list.cases [THEN takefill.Suc [THEN trans]] |
24333 | 560 |
|
561 |
lemmas takefill_Suc_Nil = takefill_Suc_cases (1) |
|
562 |
lemmas takefill_Suc_Cons = takefill_Suc_cases (2) |
|
563 |
||
65363 | 564 |
lemmas takefill_minus_simps = takefill_Suc_cases [THEN [2] |
45604 | 565 |
takefill_minus [symmetric, THEN trans]] |
24333 | 566 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46655
diff
changeset
|
567 |
lemma takefill_numeral_Nil [simp]: |
47219
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47108
diff
changeset
|
568 |
"takefill fill (numeral k) [] = fill # takefill fill (pred_numeral k) []" |
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47108
diff
changeset
|
569 |
by (simp add: numeral_eq_Suc) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46655
diff
changeset
|
570 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46655
diff
changeset
|
571 |
lemma takefill_numeral_Cons [simp]: |
47219
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47108
diff
changeset
|
572 |
"takefill fill (numeral k) (x # xs) = x # takefill fill (pred_numeral k) xs" |
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47108
diff
changeset
|
573 |
by (simp add: numeral_eq_Suc) |
24333 | 574 |
|
65363 | 575 |
|
576 |
subsection \<open>Links with function \<open>bl_to_bin\<close>\<close> |
|
24333 | 577 |
|
65363 | 578 |
lemma bl_to_bin_aux_cat: |
579 |
"\<And>nv v. bl_to_bin_aux bs (bin_cat w nv v) = |
|
26557 | 580 |
bin_cat w (nv + length bs) (bl_to_bin_aux bs v)" |
65363 | 581 |
by (induct bs) (simp, simp add: bin_cat_Suc_Bit [symmetric] del: bin_cat.simps) |
24333 | 582 |
|
65363 | 583 |
lemma bin_to_bl_aux_cat: |
584 |
"\<And>w bs. bin_to_bl_aux (nv + nw) (bin_cat v nw w) bs = |
|
24333 | 585 |
bin_to_bl_aux nv v (bin_to_bl_aux nw w bs)" |
65363 | 586 |
by (induct nw) auto |
24333 | 587 |
|
65363 | 588 |
lemma bl_to_bin_aux_alt: "bl_to_bin_aux bs w = bin_cat w (length bs) (bl_to_bin bs)" |
46001
0b562d564d5f
redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents:
45997
diff
changeset
|
589 |
using bl_to_bin_aux_cat [where nv = "0" and v = "0"] |
65363 | 590 |
by (simp add: bl_to_bin_def [symmetric]) |
24333 | 591 |
|
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
592 |
lemma bin_to_bl_cat: |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
593 |
"bin_to_bl (nv + nw) (bin_cat v nw w) = |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
594 |
bin_to_bl_aux nv v (bin_to_bl nw w)" |
65363 | 595 |
by (simp add: bin_to_bl_def bin_to_bl_aux_cat) |
24333 | 596 |
|
65363 | 597 |
lemmas bl_to_bin_aux_app_cat = |
24333 | 598 |
trans [OF bl_to_bin_aux_append bl_to_bin_aux_alt] |
599 |
||
600 |
lemmas bin_to_bl_aux_cat_app = |
|
601 |
trans [OF bin_to_bl_aux_cat bin_to_bl_aux_alt] |
|
602 |
||
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
603 |
lemma bl_to_bin_app_cat: |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
604 |
"bl_to_bin (bsa @ bs) = bin_cat (bl_to_bin bsa) (length bs) (bl_to_bin bs)" |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
605 |
by (simp only: bl_to_bin_aux_app_cat bl_to_bin_def) |
24333 | 606 |
|
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
607 |
lemma bin_to_bl_cat_app: |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
608 |
"bin_to_bl (n + nw) (bin_cat w nw wa) = bin_to_bl n w @ bin_to_bl nw wa" |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
609 |
by (simp only: bin_to_bl_def bin_to_bl_aux_cat_app) |
24333 | 610 |
|
65363 | 611 |
text \<open>\<open>bl_to_bin_app_cat_alt\<close> and \<open>bl_to_bin_app_cat\<close> are easily interderivable.\<close> |
612 |
lemma bl_to_bin_app_cat_alt: "bin_cat (bl_to_bin cs) n w = bl_to_bin (cs @ bin_to_bl n w)" |
|
613 |
by (simp add: bl_to_bin_app_cat) |
|
24333 | 614 |
|
65363 | 615 |
lemma mask_lem: "(bl_to_bin (True # replicate n False)) = bl_to_bin (replicate n True) + 1" |
24333 | 616 |
apply (unfold bl_to_bin_def) |
617 |
apply (induct n) |
|
46645
573aff6b9b0a
adapt lemma mask_lem to respect int/bin distinction
huffman
parents:
46617
diff
changeset
|
618 |
apply simp |
65363 | 619 |
apply (simp only: Suc_eq_plus1 replicate_add append_Cons [symmetric] bl_to_bin_aux_append) |
46645
573aff6b9b0a
adapt lemma mask_lem to respect int/bin distinction
huffman
parents:
46617
diff
changeset
|
620 |
apply (simp add: Bit_B0_2t Bit_B1_2t) |
24333 | 621 |
done |
622 |
||
65363 | 623 |
|
624 |
subsection \<open>Function \<open>bl_of_nth\<close>\<close> |
|
625 |
||
24333 | 626 |
lemma length_bl_of_nth [simp]: "length (bl_of_nth n f) = n" |
627 |
by (induct n) auto |
|
628 |
||
65363 | 629 |
lemma nth_bl_of_nth [simp]: "m < n \<Longrightarrow> rev (bl_of_nth n f) ! m = f m" |
24333 | 630 |
apply (induct n) |
631 |
apply simp |
|
65363 | 632 |
apply (clarsimp simp add: nth_append) |
633 |
apply (rule_tac f = "f" in arg_cong) |
|
24333 | 634 |
apply simp |
635 |
done |
|
636 |
||
65363 | 637 |
lemma bl_of_nth_inj: "(\<And>k. k < n \<Longrightarrow> f k = g k) \<Longrightarrow> bl_of_nth n f = bl_of_nth n g" |
24333 | 638 |
by (induct n) auto |
639 |
||
65363 | 640 |
lemma bl_of_nth_nth_le: "n \<le> length xs \<Longrightarrow> bl_of_nth n (nth (rev xs)) = drop (length xs - n) xs" |
641 |
apply (induct n arbitrary: xs) |
|
642 |
apply clarsimp |
|
24333 | 643 |
apply clarsimp |
644 |
apply (rule trans [OF _ hd_Cons_tl]) |
|
645 |
apply (frule Suc_le_lessD) |
|
646 |
apply (simp add: nth_rev trans [OF drop_Suc drop_tl, symmetric]) |
|
647 |
apply (subst hd_drop_conv_nth) |
|
65363 | 648 |
apply force |
649 |
apply simp_all |
|
650 |
apply (rule_tac f = "\<lambda>n. drop n xs" in arg_cong) |
|
24333 | 651 |
apply simp |
652 |
done |
|
653 |
||
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
654 |
lemma bl_of_nth_nth [simp]: "bl_of_nth (length xs) (op ! (rev xs)) = xs" |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
655 |
by (simp add: bl_of_nth_nth_le) |
24333 | 656 |
|
657 |
lemma size_rbl_pred: "length (rbl_pred bl) = length bl" |
|
658 |
by (induct bl) auto |
|
659 |
||
660 |
lemma size_rbl_succ: "length (rbl_succ bl) = length bl" |
|
661 |
by (induct bl) auto |
|
662 |
||
65363 | 663 |
lemma size_rbl_add: "length (rbl_add bl cl) = length bl" |
664 |
by (induct bl arbitrary: cl) (auto simp: Let_def size_rbl_succ) |
|
24333 | 665 |
|
65363 | 666 |
lemma size_rbl_mult: "length (rbl_mult bl cl) = length bl" |
667 |
by (induct bl arbitrary: cl) (auto simp add: Let_def size_rbl_add) |
|
24333 | 668 |
|
65363 | 669 |
lemmas rbl_sizes [simp] = |
24333 | 670 |
size_rbl_pred size_rbl_succ size_rbl_add size_rbl_mult |
671 |
||
672 |
lemmas rbl_Nils = |
|
673 |
rbl_pred.Nil rbl_succ.Nil rbl_add.Nil rbl_mult.Nil |
|
674 |
||
65363 | 675 |
lemma rbl_pred: "rbl_pred (rev (bin_to_bl n bin)) = rev (bin_to_bl n (bin - 1))" |
24333 | 676 |
apply (unfold bin_to_bl_def) |
65363 | 677 |
apply (induct n arbitrary: bin) |
678 |
apply simp |
|
24333 | 679 |
apply clarsimp |
680 |
apply (case_tac bin rule: bin_exhaust) |
|
681 |
apply (case_tac b) |
|
46653 | 682 |
apply (clarsimp simp: bin_to_bl_aux_alt)+ |
24333 | 683 |
done |
684 |
||
65363 | 685 |
lemma rbl_succ: "rbl_succ (rev (bin_to_bl n bin)) = rev (bin_to_bl n (bin + 1))" |
24333 | 686 |
apply (unfold bin_to_bl_def) |
65363 | 687 |
apply (induct n arbitrary: bin) |
688 |
apply simp |
|
24333 | 689 |
apply clarsimp |
690 |
apply (case_tac bin rule: bin_exhaust) |
|
691 |
apply (case_tac b) |
|
46653 | 692 |
apply (clarsimp simp: bin_to_bl_aux_alt)+ |
24333 | 693 |
done |
694 |
||
65363 | 695 |
lemma rbl_add: |
696 |
"\<And>bina binb. rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) = |
|
24333 | 697 |
rev (bin_to_bl n (bina + binb))" |
698 |
apply (unfold bin_to_bl_def) |
|
65363 | 699 |
apply (induct n) |
700 |
apply simp |
|
24333 | 701 |
apply clarsimp |
702 |
apply (case_tac bina rule: bin_exhaust) |
|
703 |
apply (case_tac binb rule: bin_exhaust) |
|
704 |
apply (case_tac b) |
|
705 |
apply (case_tac [!] "ba") |
|
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57492
diff
changeset
|
706 |
apply (auto simp: rbl_succ bin_to_bl_aux_alt Let_def ac_simps) |
24333 | 707 |
done |
708 |
||
65363 | 709 |
lemma rbl_add_app2: "length blb \<ge> length bla \<Longrightarrow> rbl_add bla (blb @ blc) = rbl_add bla blb" |
710 |
apply (induct bla arbitrary: blb) |
|
711 |
apply simp |
|
24333 | 712 |
apply clarsimp |
713 |
apply (case_tac blb, clarsimp) |
|
714 |
apply (clarsimp simp: Let_def) |
|
715 |
done |
|
716 |
||
65363 | 717 |
lemma rbl_add_take2: |
718 |
"length blb >= length bla ==> rbl_add bla (take (length bla) blb) = rbl_add bla blb" |
|
719 |
apply (induct bla arbitrary: blb) |
|
720 |
apply simp |
|
24333 | 721 |
apply clarsimp |
722 |
apply (case_tac blb, clarsimp) |
|
723 |
apply (clarsimp simp: Let_def) |
|
724 |
done |
|
725 |
||
65363 | 726 |
lemma rbl_add_long: |
727 |
"m \<ge> n \<Longrightarrow> rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) = |
|
24333 | 728 |
rev (bin_to_bl n (bina + binb))" |
729 |
apply (rule box_equals [OF _ rbl_add_take2 rbl_add]) |
|
65363 | 730 |
apply (rule_tac f = "rbl_add (rev (bin_to_bl n bina))" in arg_cong) |
24333 | 731 |
apply (rule rev_swap [THEN iffD1]) |
732 |
apply (simp add: rev_take drop_bin2bl) |
|
733 |
apply simp |
|
734 |
done |
|
735 |
||
65363 | 736 |
lemma rbl_mult_app2: "length blb \<ge> length bla \<Longrightarrow> rbl_mult bla (blb @ blc) = rbl_mult bla blb" |
737 |
apply (induct bla arbitrary: blb) |
|
738 |
apply simp |
|
24333 | 739 |
apply clarsimp |
740 |
apply (case_tac blb, clarsimp) |
|
741 |
apply (clarsimp simp: Let_def rbl_add_app2) |
|
742 |
done |
|
743 |
||
65363 | 744 |
lemma rbl_mult_take2: |
745 |
"length blb \<ge> length bla \<Longrightarrow> rbl_mult bla (take (length bla) blb) = rbl_mult bla blb" |
|
24333 | 746 |
apply (rule trans) |
747 |
apply (rule rbl_mult_app2 [symmetric]) |
|
748 |
apply simp |
|
65363 | 749 |
apply (rule_tac f = "rbl_mult bla" in arg_cong) |
24333 | 750 |
apply (rule append_take_drop_id) |
751 |
done |
|
65363 | 752 |
|
753 |
lemma rbl_mult_gt1: |
|
754 |
"m \<ge> length bl \<Longrightarrow> |
|
755 |
rbl_mult bl (rev (bin_to_bl m binb)) = |
|
24333 | 756 |
rbl_mult bl (rev (bin_to_bl (length bl) binb))" |
757 |
apply (rule trans) |
|
758 |
apply (rule rbl_mult_take2 [symmetric]) |
|
759 |
apply simp_all |
|
65363 | 760 |
apply (rule_tac f = "rbl_mult bl" in arg_cong) |
24333 | 761 |
apply (rule rev_swap [THEN iffD1]) |
762 |
apply (simp add: rev_take drop_bin2bl) |
|
763 |
done |
|
65363 | 764 |
|
765 |
lemma rbl_mult_gt: |
|
766 |
"m > n \<Longrightarrow> |
|
767 |
rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) = |
|
24333 | 768 |
rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb))" |
769 |
by (auto intro: trans [OF rbl_mult_gt1]) |
|
65363 | 770 |
|
24333 | 771 |
lemmas rbl_mult_Suc = lessI [THEN rbl_mult_gt] |
772 |
||
65363 | 773 |
lemma rbbl_Cons: "b # rev (bin_to_bl n x) = rev (bin_to_bl (Suc n) (x BIT b))" |
774 |
by (simp add: bin_to_bl_def) (simp add: bin_to_bl_aux_alt) |
|
46653 | 775 |
|
65363 | 776 |
lemma rbl_mult: |
777 |
"rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) = |
|
24333 | 778 |
rev (bin_to_bl n (bina * binb))" |
65363 | 779 |
apply (induct n arbitrary: bina binb) |
24333 | 780 |
apply simp |
781 |
apply (unfold bin_to_bl_def) |
|
782 |
apply clarsimp |
|
783 |
apply (case_tac bina rule: bin_exhaust) |
|
784 |
apply (case_tac binb rule: bin_exhaust) |
|
785 |
apply (case_tac b) |
|
786 |
apply (case_tac [!] "ba") |
|
46653 | 787 |
apply (auto simp: bin_to_bl_aux_alt Let_def) |
788 |
apply (auto simp: rbbl_Cons rbl_mult_Suc rbl_add) |
|
24333 | 789 |
done |
790 |
||
65363 | 791 |
lemma rbl_add_split: |
792 |
"P (rbl_add (y # ys) (x # xs)) = |
|
793 |
(\<forall>ws. length ws = length ys \<longrightarrow> ws = rbl_add ys xs \<longrightarrow> |
|
794 |
(y \<longrightarrow> ((x \<longrightarrow> P (False # rbl_succ ws)) \<and> (\<not> x \<longrightarrow> P (True # ws)))) \<and> |
|
795 |
(\<not> y \<longrightarrow> P (x # ws)))" |
|
796 |
by (cases y) (auto simp: Let_def) |
|
24333 | 797 |
|
65363 | 798 |
lemma rbl_mult_split: |
799 |
"P (rbl_mult (y # ys) xs) = |
|
800 |
(\<forall>ws. length ws = Suc (length ys) \<longrightarrow> ws = False # rbl_mult ys xs \<longrightarrow> |
|
801 |
(y \<longrightarrow> P (rbl_add ws xs)) \<and> (\<not> y \<longrightarrow> P ws))" |
|
802 |
by (auto simp: Let_def) |
|
24333 | 803 |
|
804 |
||
65363 | 805 |
subsection \<open>Repeated splitting or concatenation\<close> |
806 |
||
807 |
lemma sclem: "size (concat (map (bin_to_bl n) xs)) = length xs * n" |
|
24333 | 808 |
by (induct xs) auto |
809 |
||
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
810 |
lemma bin_cat_foldl_lem: |
65363 | 811 |
"foldl (\<lambda>u. bin_cat u n) x xs = |
812 |
bin_cat x (size xs * n) (foldl (\<lambda>u. bin_cat u n) y xs)" |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
813 |
apply (induct xs arbitrary: x) |
24333 | 814 |
apply simp |
815 |
apply (simp (no_asm)) |
|
816 |
apply (frule asm_rl) |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
817 |
apply (drule meta_spec) |
24333 | 818 |
apply (erule trans) |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
819 |
apply (drule_tac x = "bin_cat y n a" in meta_spec) |
65363 | 820 |
apply (simp add: bin_cat_assoc_sym min.absorb2) |
24333 | 821 |
done |
822 |
||
65363 | 823 |
lemma bin_rcat_bl: "bin_rcat n wl = bl_to_bin (concat (map (bin_to_bl n) wl))" |
24333 | 824 |
apply (unfold bin_rcat_def) |
825 |
apply (rule sym) |
|
826 |
apply (induct wl) |
|
65363 | 827 |
apply (auto simp add: bl_to_bin_append) |
828 |
apply (simp add: bl_to_bin_aux_alt sclem) |
|
829 |
apply (simp add: bin_cat_foldl_lem [symmetric]) |
|
24333 | 830 |
done |
831 |
||
832 |
lemmas bin_rsplit_aux_simps = bin_rsplit_aux.simps bin_rsplitl_aux.simps |
|
833 |
lemmas rsplit_aux_simps = bin_rsplit_aux_simps |
|
834 |
||
62390 | 835 |
lemmas th_if_simp1 = if_split [where P = "op = l", THEN iffD1, THEN conjunct1, THEN mp] for l |
836 |
lemmas th_if_simp2 = if_split [where P = "op = l", THEN iffD1, THEN conjunct2, THEN mp] for l |
|
24333 | 837 |
|
838 |
lemmas rsplit_aux_simp1s = rsplit_aux_simps [THEN th_if_simp1] |
|
839 |
||
840 |
lemmas rsplit_aux_simp2ls = rsplit_aux_simps [THEN th_if_simp2] |
|
841 |
(* these safe to [simp add] as require calculating m - n *) |
|
842 |
lemmas bin_rsplit_aux_simp2s [simp] = rsplit_aux_simp2ls [unfolded Let_def] |
|
843 |
lemmas rbscl = bin_rsplit_aux_simp2s (2) |
|
844 |
||
65363 | 845 |
lemmas rsplit_aux_0_simps [simp] = |
24333 | 846 |
rsplit_aux_simp1s [OF disjI1] rsplit_aux_simp1s [OF disjI2] |
847 |
||
65363 | 848 |
lemma bin_rsplit_aux_append: "bin_rsplit_aux n m c (bs @ cs) = bin_rsplit_aux n m c bs @ cs" |
26557 | 849 |
apply (induct n m c bs rule: bin_rsplit_aux.induct) |
24333 | 850 |
apply (subst bin_rsplit_aux.simps) |
851 |
apply (subst bin_rsplit_aux.simps) |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
852 |
apply (clarsimp split: prod.split) |
24333 | 853 |
done |
854 |
||
65363 | 855 |
lemma bin_rsplitl_aux_append: "bin_rsplitl_aux n m c (bs @ cs) = bin_rsplitl_aux n m c bs @ cs" |
26557 | 856 |
apply (induct n m c bs rule: bin_rsplitl_aux.induct) |
24333 | 857 |
apply (subst bin_rsplitl_aux.simps) |
858 |
apply (subst bin_rsplitl_aux.simps) |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
859 |
apply (clarsimp split: prod.split) |
24333 | 860 |
done |
861 |
||
862 |
lemmas rsplit_aux_apps [where bs = "[]"] = |
|
863 |
bin_rsplit_aux_append bin_rsplitl_aux_append |
|
864 |
||
865 |
lemmas rsplit_def_auxs = bin_rsplit_def bin_rsplitl_def |
|
866 |
||
65363 | 867 |
lemmas rsplit_aux_alts = rsplit_aux_apps |
24333 | 868 |
[unfolded append_Nil rsplit_def_auxs [symmetric]] |
869 |
||
65363 | 870 |
lemma bin_split_minus: "0 < n \<Longrightarrow> bin_split (Suc (n - 1)) w = bin_split n w" |
24333 | 871 |
by auto |
872 |
||
873 |
lemmas bin_split_minus_simp = |
|
45604 | 874 |
bin_split.Suc [THEN [2] bin_split_minus [symmetric, THEN trans]] |
24333 | 875 |
|
65363 | 876 |
lemma bin_split_pred_simp [simp]: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46655
diff
changeset
|
877 |
"(0::nat) < numeral bin \<Longrightarrow> |
65363 | 878 |
bin_split (numeral bin) w = |
879 |
(let (w1, w2) = bin_split (numeral bin - 1) (bin_rest w) |
|
880 |
in (w1, w2 BIT bin_last w))" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46655
diff
changeset
|
881 |
by (simp only: bin_split_minus_simp) |
24333 | 882 |
|
883 |
lemma bin_rsplit_aux_simp_alt: |
|
26557 | 884 |
"bin_rsplit_aux n m c bs = |
65363 | 885 |
(if m = 0 \<or> n = 0 then bs |
886 |
else let (a, b) = bin_split n c in bin_rsplit n (m - n, a) @ b # bs)" |
|
887 |
apply (simp add: bin_rsplit_aux.simps [of n m c bs]) |
|
26557 | 888 |
apply (subst rsplit_aux_alts) |
889 |
apply (simp add: bin_rsplit_def) |
|
24333 | 890 |
done |
891 |
||
65363 | 892 |
lemmas bin_rsplit_simp_alt = |
45604 | 893 |
trans [OF bin_rsplit_def bin_rsplit_aux_simp_alt] |
24333 | 894 |
|
895 |
lemmas bthrs = bin_rsplit_simp_alt [THEN [2] trans] |
|
896 |
||
65363 | 897 |
lemma bin_rsplit_size_sign' [rule_format]: |
898 |
"n > 0 \<Longrightarrow> rev sw = bin_rsplit n (nw, w) \<Longrightarrow> \<forall>v\<in>set sw. bintrunc n v = v" |
|
62957 | 899 |
apply (induct sw arbitrary: nw w) |
24333 | 900 |
apply clarsimp |
901 |
apply clarsimp |
|
902 |
apply (drule bthrs) |
|
62390 | 903 |
apply (simp (no_asm_use) add: Let_def split: prod.split_asm if_split_asm) |
24333 | 904 |
apply clarify |
905 |
apply (drule split_bintrunc) |
|
906 |
apply simp |
|
907 |
done |
|
908 |
||
65363 | 909 |
lemmas bin_rsplit_size_sign = bin_rsplit_size_sign' [OF asm_rl |
45604 | 910 |
rev_rev_ident [THEN trans] set_rev [THEN equalityD2 [THEN subsetD]]] |
24333 | 911 |
|
912 |
lemma bin_nth_rsplit [rule_format] : |
|
65363 | 913 |
"n > 0 \<Longrightarrow> m < n \<Longrightarrow> |
914 |
\<forall>w k nw. |
|
915 |
rev sw = bin_rsplit n (nw, w) \<longrightarrow> |
|
916 |
k < size sw \<longrightarrow> bin_nth (sw ! k) m = bin_nth w (k * n + m)" |
|
24333 | 917 |
apply (induct sw) |
918 |
apply clarsimp |
|
919 |
apply clarsimp |
|
920 |
apply (drule bthrs) |
|
62390 | 921 |
apply (simp (no_asm_use) add: Let_def split: prod.split_asm if_split_asm) |
24333 | 922 |
apply clarify |
923 |
apply (erule allE, erule impE, erule exI) |
|
924 |
apply (case_tac k) |
|
65363 | 925 |
apply clarsimp |
24333 | 926 |
prefer 2 |
927 |
apply clarsimp |
|
928 |
apply (erule allE) |
|
929 |
apply (erule (1) impE) |
|
65363 | 930 |
apply (drule bin_nth_split, erule conjE, erule allE, erule trans, simp add: ac_simps)+ |
24333 | 931 |
done |
932 |
||
65363 | 933 |
lemma bin_rsplit_all: "0 < nw \<Longrightarrow> nw \<le> n \<Longrightarrow> bin_rsplit n (nw, w) = [bintrunc n w]" |
934 |
by (auto simp: bin_rsplit_def rsplit_aux_simp2ls split: prod.split dest!: split_bintrunc) |
|
24333 | 935 |
|
65363 | 936 |
lemma bin_rsplit_l [rule_format]: |
937 |
"\<forall>bin. bin_rsplitl n (m, bin) = bin_rsplit n (m, bintrunc m bin)" |
|
24333 | 938 |
apply (rule_tac a = "m" in wf_less_than [THEN wf_induct]) |
65363 | 939 |
apply (simp (no_asm) add: bin_rsplitl_def bin_rsplit_def) |
24333 | 940 |
apply (rule allI) |
941 |
apply (subst bin_rsplitl_aux.simps) |
|
942 |
apply (subst bin_rsplit_aux.simps) |
|
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
943 |
apply (clarsimp simp: Let_def split: prod.split) |
24333 | 944 |
apply (drule bin_split_trunc) |
945 |
apply (drule sym [THEN trans], assumption) |
|
26557 | 946 |
apply (subst rsplit_aux_alts(1)) |
947 |
apply (subst rsplit_aux_alts(2)) |
|
948 |
apply clarsimp |
|
949 |
unfolding bin_rsplit_def bin_rsplitl_def |
|
950 |
apply simp |
|
24333 | 951 |
done |
26557 | 952 |
|
65363 | 953 |
lemma bin_rsplit_rcat [rule_format]: |
954 |
"n > 0 \<longrightarrow> bin_rsplit n (n * size ws, bin_rcat n ws) = map (bintrunc n) ws" |
|
24333 | 955 |
apply (unfold bin_rsplit_def bin_rcat_def) |
65363 | 956 |
apply (rule_tac xs = ws in rev_induct) |
24333 | 957 |
apply clarsimp |
958 |
apply clarsimp |
|
26557 | 959 |
apply (subst rsplit_aux_alts) |
960 |
unfolding bin_split_cat |
|
961 |
apply simp |
|
24333 | 962 |
done |
963 |
||
964 |
lemma bin_rsplit_aux_len_le [rule_format] : |
|
26557 | 965 |
"\<forall>ws m. n \<noteq> 0 \<longrightarrow> ws = bin_rsplit_aux n nw w bs \<longrightarrow> |
966 |
length ws \<le> m \<longleftrightarrow> nw + length bs * n \<le> m * n" |
|
54871 | 967 |
proof - |
65363 | 968 |
have *: R |
969 |
if d: "i \<le> j \<or> m < j'" |
|
970 |
and R1: "i * k \<le> j * k \<Longrightarrow> R" |
|
971 |
and R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R" |
|
972 |
for i j j' k k' m :: nat and R |
|
973 |
using d |
|
974 |
apply safe |
|
975 |
apply (rule R1, erule mult_le_mono1) |
|
976 |
apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]]) |
|
977 |
done |
|
978 |
have **: "0 < sc \<Longrightarrow> sc - n + (n + lb * n) \<le> m * n \<longleftrightarrow> sc + lb * n \<le> m * n" |
|
979 |
for sc m n lb :: nat |
|
980 |
apply safe |
|
981 |
apply arith |
|
982 |
apply (case_tac "sc \<ge> n") |
|
983 |
apply arith |
|
984 |
apply (insert linorder_le_less_linear [of m lb]) |
|
985 |
apply (erule_tac k=n and k'=n in *) |
|
986 |
apply arith |
|
987 |
apply simp |
|
988 |
done |
|
54871 | 989 |
show ?thesis |
990 |
apply (induct n nw w bs rule: bin_rsplit_aux.induct) |
|
991 |
apply (subst bin_rsplit_aux.simps) |
|
65363 | 992 |
apply (simp add: ** Let_def split: prod.split) |
54871 | 993 |
done |
994 |
qed |
|
24333 | 995 |
|
65363 | 996 |
lemma bin_rsplit_len_le: "n \<noteq> 0 \<longrightarrow> ws = bin_rsplit n (nw, w) \<longrightarrow> length ws \<le> m \<longleftrightarrow> nw \<le> m * n" |
997 |
by (auto simp: bin_rsplit_def bin_rsplit_aux_len_le) |
|
998 |
||
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
999 |
lemma bin_rsplit_aux_len: |
65363 | 1000 |
"n \<noteq> 0 \<Longrightarrow> length (bin_rsplit_aux n nw w cs) = (nw + n - 1) div n + length cs" |
26557 | 1001 |
apply (induct n nw w cs rule: bin_rsplit_aux.induct) |
24333 | 1002 |
apply (subst bin_rsplit_aux.simps) |
53062
3af1a6020014
some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents:
47219
diff
changeset
|
1003 |
apply (clarsimp simp: Let_def split: prod.split) |
24333 | 1004 |
apply (erule thin_rl) |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27105
diff
changeset
|
1005 |
apply (case_tac m) |
65363 | 1006 |
apply simp |
1007 |
apply (case_tac "m \<le> n") |
|
64507
eace715f4988
avoid import of Complex_Main into Word library (amending 34b7e2da95f6), e.g. to avoid intrusion of const "ii" into theories without complex numbers;
wenzelm
parents:
63648
diff
changeset
|
1008 |
apply (auto simp add: div_add_self2) |
24333 | 1009 |
done |
1010 |
||
65363 | 1011 |
lemma bin_rsplit_len: "n \<noteq> 0 \<Longrightarrow> length (bin_rsplit n (nw, w)) = (nw + n - 1) div n" |
1012 |
by (auto simp: bin_rsplit_def bin_rsplit_aux_len) |
|
24333 | 1013 |
|
26557 | 1014 |
lemma bin_rsplit_aux_len_indep: |
1015 |
"n \<noteq> 0 \<Longrightarrow> length bs = length cs \<Longrightarrow> |
|
1016 |
length (bin_rsplit_aux n nw v bs) = |
|
1017 |
length (bin_rsplit_aux n nw w cs)" |
|
1018 |
proof (induct n nw w cs arbitrary: v bs rule: bin_rsplit_aux.induct) |
|
65363 | 1019 |
case (1 n m w cs v bs) |
1020 |
show ?case |
|
26557 | 1021 |
proof (cases "m = 0") |
65363 | 1022 |
case True |
1023 |
with \<open>length bs = length cs\<close> show ?thesis by simp |
|
26557 | 1024 |
next |
1025 |
case False |
|
61799 | 1026 |
from "1.hyps" \<open>m \<noteq> 0\<close> \<open>n \<noteq> 0\<close> have hyp: "\<And>v bs. length bs = Suc (length cs) \<Longrightarrow> |
26557 | 1027 |
length (bin_rsplit_aux n (m - n) v bs) = |
1028 |
length (bin_rsplit_aux n (m - n) (fst (bin_split n w)) (snd (bin_split n w) # cs))" |
|
65363 | 1029 |
by auto |
1030 |
from \<open>length bs = length cs\<close> \<open>n \<noteq> 0\<close> show ?thesis |
|
1031 |
by (auto simp add: bin_rsplit_aux_simp_alt Let_def bin_rsplit_len split: prod.split) |
|
26557 | 1032 |
qed |
1033 |
qed |
|
24333 | 1034 |
|
65363 | 1035 |
lemma bin_rsplit_len_indep: |
1036 |
"n \<noteq> 0 \<Longrightarrow> length (bin_rsplit n (nw, v)) = length (bin_rsplit n (nw, w))" |
|
24333 | 1037 |
apply (unfold bin_rsplit_def) |
26557 | 1038 |
apply (simp (no_asm)) |
24333 | 1039 |
apply (erule bin_rsplit_aux_len_indep) |
1040 |
apply (rule refl) |
|
1041 |
done |
|
1042 |
||
54874 | 1043 |
|
61799 | 1044 |
text \<open>Even more bit operations\<close> |
54874 | 1045 |
|
1046 |
instantiation int :: bitss |
|
1047 |
begin |
|
1048 |
||
65363 | 1049 |
definition [iff]: "i !! n \<longleftrightarrow> bin_nth i n" |
54874 | 1050 |
|
65363 | 1051 |
definition "lsb i = i !! 0" for i :: int |
54874 | 1052 |
|
65363 | 1053 |
definition "set_bit i n b = bin_sc n b i" |
54874 | 1054 |
|
1055 |
definition |
|
1056 |
"set_bits f = |
|
65363 | 1057 |
(if \<exists>n. \<forall>n'\<ge>n. \<not> f n' then |
1058 |
let n = LEAST n. \<forall>n'\<ge>n. \<not> f n' |
|
1059 |
in bl_to_bin (rev (map f [0..<n])) |
|
1060 |
else if \<exists>n. \<forall>n'\<ge>n. f n' then |
|
1061 |
let n = LEAST n. \<forall>n'\<ge>n. f n' |
|
1062 |
in sbintrunc n (bl_to_bin (True # rev (map f [0..<n]))) |
|
1063 |
else 0 :: int)" |
|
54874 | 1064 |
|
65363 | 1065 |
definition "shiftl x n = x * 2 ^ n" for x :: int |
54874 | 1066 |
|
65363 | 1067 |
definition "shiftr x n = x div 2 ^ n" for x :: int |
54874 | 1068 |
|
65363 | 1069 |
definition "msb x \<longleftrightarrow> x < 0" for x :: int |
54874 | 1070 |
|
1071 |
instance .. |
|
1072 |
||
24333 | 1073 |
end |
54874 | 1074 |
|
1075 |
end |