author | nipkow |
Wed, 20 Dec 2017 14:53:34 +0100 | |
changeset 67229 | 4ecf0ef70efb |
parent 67120 | 491fd7f0b5df |
child 67399 | eab6ce8368fa |
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 |
|
67120 | 120 |
text \<open>Link between \<open>bin\<close> and \<open>bool list\<close>.\<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 = |
67120 | 241 |
(n < size bl \<and> rev bl ! n \<or> 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 |
|
67120 | 301 |
case (Cons b bs) |
302 |
with bl_to_bin_lt2p_aux[where w=1] show ?case |
|
303 |
by (simp add: bl_to_bin_def) |
|
65363 | 304 |
qed |
62701 | 305 |
|
306 |
lemma bl_to_bin_lt2p: "bl_to_bin bs < 2 ^ length bs" |
|
307 |
by (metis bin_bl_bin bintr_lt2p bl_bin_bl) |
|
24333 | 308 |
|
65363 | 309 |
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
|
310 |
apply (induct bs arbitrary: w) |
24333 | 311 |
apply clarsimp |
312 |
apply clarsimp |
|
46652 | 313 |
apply (drule meta_spec, erule order_trans [rotated], |
314 |
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
|
315 |
apply (simp add: Bit_def) |
24333 | 316 |
done |
317 |
||
65363 | 318 |
lemma bl_to_bin_ge0: "bl_to_bin bs \<ge> 0" |
24333 | 319 |
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
|
320 |
apply (rule xtrans(4)) |
24333 | 321 |
apply (rule bl_to_bin_ge2p_aux) |
46617
8c5d10d41391
make bool list functions respect int/bin distinction
huffman
parents:
46240
diff
changeset
|
322 |
apply simp |
24333 | 323 |
done |
324 |
||
65363 | 325 |
lemma butlast_rest_bin: "butlast (bin_to_bl n w) = bin_to_bl (n - 1) (bin_rest w)" |
24333 | 326 |
apply (unfold bin_to_bl_def) |
327 |
apply (cases w rule: bin_exhaust) |
|
328 |
apply (cases n, clarsimp) |
|
329 |
apply clarsimp |
|
330 |
apply (auto simp add: bin_to_bl_aux_alt) |
|
331 |
done |
|
332 |
||
65363 | 333 |
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
|
334 |
using butlast_rest_bin [where w="bl_to_bin bl" and n="length bl"] by simp |
24333 | 335 |
|
26557 | 336 |
lemma butlast_rest_bl2bin_aux: |
65363 | 337 |
"bl \<noteq> [] \<Longrightarrow> bl_to_bin_aux (butlast bl) w = bin_rest (bl_to_bin_aux bl w)" |
26557 | 338 |
by (induct bl arbitrary: w) auto |
65363 | 339 |
|
340 |
lemma butlast_rest_bl2bin: "bl_to_bin (butlast bl) = bin_rest (bl_to_bin bl)" |
|
341 |
by (cases bl) (auto simp: bl_to_bin_def butlast_rest_bl2bin_aux) |
|
24333 | 342 |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
343 |
lemma trunc_bl2bin_aux: |
65363 | 344 |
"bintrunc m (bl_to_bin_aux bl w) = |
26557 | 345 |
bl_to_bin_aux (drop (length bl - m) bl) (bintrunc (m - length bl) w)" |
53438 | 346 |
proof (induct bl arbitrary: w) |
65363 | 347 |
case Nil |
348 |
show ?case by simp |
|
53438 | 349 |
next |
65363 | 350 |
case (Cons b bl) |
351 |
show ?case |
|
53438 | 352 |
proof (cases "m - length bl") |
65363 | 353 |
case 0 |
354 |
then have "Suc (length bl) - m = Suc (length bl - m)" by simp |
|
53438 | 355 |
with Cons show ?thesis by simp |
356 |
next |
|
65363 | 357 |
case (Suc n) |
358 |
then have "m - Suc (length bl) = n" by simp |
|
359 |
with Cons Suc show ?thesis by simp |
|
53438 | 360 |
qed |
361 |
qed |
|
24333 | 362 |
|
65363 | 363 |
lemma trunc_bl2bin: "bintrunc m (bl_to_bin bl) = bl_to_bin (drop (length bl - m) bl)" |
364 |
by (simp add: bl_to_bin_def trunc_bl2bin_aux) |
|
365 |
||
366 |
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
|
367 |
by (simp add: trunc_bl2bin) |
24333 | 368 |
|
65363 | 369 |
lemma bl2bin_drop: "bl_to_bin (drop k bl) = bintrunc (length bl - k) (bl_to_bin bl)" |
24333 | 370 |
apply (rule trans) |
371 |
prefer 2 |
|
372 |
apply (rule trunc_bl2bin [symmetric]) |
|
65363 | 373 |
apply (cases "k \<le> length bl") |
24333 | 374 |
apply auto |
375 |
done |
|
376 |
||
65363 | 377 |
lemma nth_rest_power_bin: "bin_nth ((bin_rest ^^ k) w) n = bin_nth w (n + k)" |
378 |
apply (induct k arbitrary: n) |
|
379 |
apply clarsimp |
|
24333 | 380 |
apply clarsimp |
381 |
apply (simp only: bin_nth.Suc [symmetric] add_Suc) |
|
382 |
done |
|
383 |
||
65363 | 384 |
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 | 385 |
apply (rule nth_equalityI) |
386 |
apply simp |
|
387 |
apply (clarsimp simp add: nth_bin_to_bl nth_rest_power_bin) |
|
388 |
done |
|
389 |
||
65363 | 390 |
lemma hd_butlast: "size xs > 1 \<Longrightarrow> hd (butlast xs) = hd xs" |
24465 | 391 |
by (cases xs) auto |
24333 | 392 |
|
65363 | 393 |
lemma last_bin_last': "size xs > 0 \<Longrightarrow> last xs \<longleftrightarrow> bin_last (bl_to_bin_aux xs w)" |
26557 | 394 |
by (induct xs arbitrary: w) auto |
24333 | 395 |
|
65363 | 396 |
lemma last_bin_last: "size xs > 0 \<Longrightarrow> last xs \<longleftrightarrow> bin_last (bl_to_bin xs)" |
24333 | 397 |
unfolding bl_to_bin_def by (erule last_bin_last') |
65363 | 398 |
|
399 |
lemma bin_last_last: "bin_last w \<longleftrightarrow> last (bin_to_bl (Suc n) w)" |
|
400 |
by (simp add: bin_to_bl_def) (auto simp: bin_to_bl_aux_alt) |
|
401 |
||
24333 | 402 |
|
65363 | 403 |
subsection \<open>Links between bit-wise operations and operations on bool lists\<close> |
404 |
||
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
405 |
lemma bl_xor_aux_bin: |
65363 | 406 |
"map2 (\<lambda>x y. x \<noteq> y) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = |
407 |
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
|
408 |
apply (induct n arbitrary: v w bs cs) |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
409 |
apply simp |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
410 |
apply (case_tac v rule: bin_exhaust) |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
411 |
apply (case_tac w rule: bin_exhaust) |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
412 |
apply clarsimp |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
413 |
apply (case_tac b) |
65363 | 414 |
apply auto |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
415 |
done |
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
416 |
|
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
417 |
lemma bl_or_aux_bin: |
65363 | 418 |
"map2 (op \<or>) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = |
419 |
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
|
420 |
apply (induct n arbitrary: v w bs cs) |
24333 | 421 |
apply simp |
422 |
apply (case_tac v rule: bin_exhaust) |
|
423 |
apply (case_tac w rule: bin_exhaust) |
|
424 |
apply clarsimp |
|
425 |
done |
|
65363 | 426 |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
427 |
lemma bl_and_aux_bin: |
65363 | 428 |
"map2 (op \<and>) (bin_to_bl_aux n v bs) (bin_to_bl_aux n w cs) = |
429 |
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
|
430 |
apply (induct n arbitrary: v w bs cs) |
24333 | 431 |
apply simp |
432 |
apply (case_tac v rule: bin_exhaust) |
|
433 |
apply (case_tac w rule: bin_exhaust) |
|
434 |
apply clarsimp |
|
435 |
done |
|
65363 | 436 |
|
437 |
lemma bl_not_aux_bin: "map Not (bin_to_bl_aux n w cs) = bin_to_bl_aux n (NOT w) (map Not cs)" |
|
438 |
by (induct n arbitrary: w cs) auto |
|
24333 | 439 |
|
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
440 |
lemma bl_not_bin: "map Not (bin_to_bl n w) = bin_to_bl n (NOT w)" |
65363 | 441 |
by (simp add: bin_to_bl_def bl_not_aux_bin) |
24333 | 442 |
|
65363 | 443 |
lemma bl_and_bin: "map2 (op \<and>) (bin_to_bl n v) (bin_to_bl n w) = bin_to_bl n (v AND w)" |
444 |
by (simp add: bin_to_bl_def bl_and_aux_bin) |
|
24333 | 445 |
|
65363 | 446 |
lemma bl_or_bin: "map2 (op \<or>) (bin_to_bl n v) (bin_to_bl n w) = bin_to_bl n (v OR w)" |
447 |
by (simp add: bin_to_bl_def bl_or_aux_bin) |
|
24333 | 448 |
|
65363 | 449 |
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)" |
450 |
by (simp only: bin_to_bl_def bl_xor_aux_bin map2_Nil) |
|
24333 | 451 |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
452 |
lemma drop_bin2bl_aux: |
65363 | 453 |
"drop m (bin_to_bl_aux n bin bs) = |
24333 | 454 |
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
|
455 |
apply (induct n arbitrary: m bin bs, clarsimp) |
24333 | 456 |
apply clarsimp |
457 |
apply (case_tac bin rule: bin_exhaust) |
|
65363 | 458 |
apply (case_tac "m \<le> n", simp) |
24333 | 459 |
apply (case_tac "m - n", simp) |
460 |
apply simp |
|
65363 | 461 |
apply (rule_tac f = "\<lambda>nat. drop nat bs" in arg_cong) |
24333 | 462 |
apply simp |
463 |
done |
|
464 |
||
465 |
lemma drop_bin2bl: "drop m (bin_to_bl n bin) = bin_to_bl (n - m) bin" |
|
65363 | 466 |
by (simp add: bin_to_bl_def drop_bin2bl_aux) |
24333 | 467 |
|
65363 | 468 |
lemma take_bin2bl_lem1: "take m (bin_to_bl_aux m w bs) = bin_to_bl m w" |
469 |
apply (induct m arbitrary: w bs) |
|
470 |
apply clarsimp |
|
24333 | 471 |
apply clarsimp |
472 |
apply (simp add: bin_to_bl_aux_alt) |
|
473 |
apply (simp add: bin_to_bl_def) |
|
474 |
apply (simp add: bin_to_bl_aux_alt) |
|
475 |
done |
|
476 |
||
65363 | 477 |
lemma take_bin2bl_lem: "take m (bin_to_bl_aux (m + n) w bs) = take m (bin_to_bl (m + n) w)" |
478 |
by (induct n arbitrary: w bs) (simp_all (no_asm) add: bin_to_bl_def take_bin2bl_lem1, simp) |
|
24333 | 479 |
|
65363 | 480 |
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
|
481 |
apply (induct n arbitrary: b c) |
24333 | 482 |
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
|
483 |
apply (clarsimp simp: Let_def split: prod.split_asm) |
24333 | 484 |
apply (simp add: bin_to_bl_def) |
485 |
apply (simp add: take_bin2bl_lem) |
|
486 |
done |
|
487 |
||
65363 | 488 |
lemma bin_split_take1: |
489 |
"k = m + n \<Longrightarrow> bin_split n c = (a, b) \<Longrightarrow> bin_to_bl m a = take m (bin_to_bl k c)" |
|
24333 | 490 |
by (auto elim: bin_split_take) |
65363 | 491 |
|
492 |
lemma nth_takefill: "m < n \<Longrightarrow> takefill fill n l ! m = (if m < length l then l ! m else fill)" |
|
493 |
apply (induct n arbitrary: m l) |
|
494 |
apply clarsimp |
|
24333 | 495 |
apply clarsimp |
496 |
apply (case_tac m) |
|
497 |
apply (simp split: list.split) |
|
498 |
apply (simp split: list.split) |
|
499 |
done |
|
500 |
||
65363 | 501 |
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
|
502 |
by (induct n arbitrary: l) (auto split: list.split) |
24333 | 503 |
|
65363 | 504 |
lemma takefill_replicate [simp]: "takefill fill n (replicate m fill) = replicate n fill" |
505 |
by (simp add: takefill_alt replicate_add [symmetric]) |
|
24333 | 506 |
|
65363 | 507 |
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
|
508 |
by (induct m arbitrary: l n) (auto split: list.split) |
24333 | 509 |
|
510 |
lemma length_takefill [simp]: "length (takefill fill n l) = n" |
|
65363 | 511 |
by (simp add: takefill_alt) |
24333 | 512 |
|
67120 | 513 |
lemma take_takefill': "n = k + m \<Longrightarrow> take k (takefill fill n w) = takefill fill k w" |
514 |
by (induct k arbitrary: w n) (auto split: list.split) |
|
24333 | 515 |
|
67120 | 516 |
lemma drop_takefill: "drop k (takefill fill (m + k) w) = takefill fill m (drop k w)" |
517 |
by (induct k arbitrary: w) (auto split: list.split) |
|
24333 | 518 |
|
65363 | 519 |
lemma takefill_le [simp]: "m \<le> n \<Longrightarrow> takefill x m (takefill x n l) = takefill x m l" |
24333 | 520 |
by (auto simp: le_iff_add takefill_le') |
521 |
||
65363 | 522 |
lemma take_takefill [simp]: "m \<le> n \<Longrightarrow> take m (takefill fill n w) = takefill fill m w" |
24333 | 523 |
by (auto simp: le_iff_add take_takefill') |
65363 | 524 |
|
525 |
lemma takefill_append: "takefill fill (m + length xs) (xs @ w) = xs @ (takefill fill m w)" |
|
24333 | 526 |
by (induct xs) auto |
527 |
||
65363 | 528 |
lemma takefill_same': "l = length xs \<Longrightarrow> takefill fill l xs = xs" |
529 |
by (induct xs arbitrary: l) auto |
|
530 |
||
24333 | 531 |
lemmas takefill_same [simp] = takefill_same' [OF refl] |
532 |
||
65363 | 533 |
lemma takefill_bintrunc: "takefill False n bl = rev (bin_to_bl n (bl_to_bin (rev bl)))" |
24333 | 534 |
apply (rule nth_equalityI) |
535 |
apply simp |
|
536 |
apply (clarsimp simp: nth_takefill nth_rev nth_bin_to_bl bin_nth_of_bl) |
|
537 |
done |
|
538 |
||
65363 | 539 |
lemma bl_bin_bl_rtf: "bin_to_bl n (bl_to_bin bl) = rev (takefill False n (rev bl))" |
540 |
by (simp add: takefill_bintrunc) |
|
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
541 |
|
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
542 |
lemma bl_bin_bl_rep_drop: |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
543 |
"bin_to_bl n (bl_to_bin bl) = |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
544 |
replicate (n - length bl) False @ drop (length bl - n) bl" |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
545 |
by (simp add: bl_bin_bl_rtf takefill_alt rev_take) |
24333 | 546 |
|
547 |
lemma tf_rev: |
|
65363 | 548 |
"n + k = m + length bl \<Longrightarrow> takefill x m (rev (takefill y n bl)) = |
24333 | 549 |
rev (takefill y m (rev (takefill x k (rev bl))))" |
550 |
apply (rule nth_equalityI) |
|
551 |
apply (auto simp add: nth_takefill nth_rev) |
|
65363 | 552 |
apply (rule_tac f = "\<lambda>n. bl ! n" in arg_cong) |
553 |
apply arith |
|
24333 | 554 |
done |
555 |
||
65363 | 556 |
lemma takefill_minus: "0 < n \<Longrightarrow> takefill fill (Suc (n - 1)) w = takefill fill n w" |
24333 | 557 |
by auto |
558 |
||
65363 | 559 |
lemmas takefill_Suc_cases = |
45604 | 560 |
list.cases [THEN takefill.Suc [THEN trans]] |
24333 | 561 |
|
562 |
lemmas takefill_Suc_Nil = takefill_Suc_cases (1) |
|
563 |
lemmas takefill_Suc_Cons = takefill_Suc_cases (2) |
|
564 |
||
65363 | 565 |
lemmas takefill_minus_simps = takefill_Suc_cases [THEN [2] |
45604 | 566 |
takefill_minus [symmetric, THEN trans]] |
24333 | 567 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46655
diff
changeset
|
568 |
lemma takefill_numeral_Nil [simp]: |
47219
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47108
diff
changeset
|
569 |
"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
|
570 |
by (simp add: numeral_eq_Suc) |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46655
diff
changeset
|
571 |
|
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46655
diff
changeset
|
572 |
lemma takefill_numeral_Cons [simp]: |
47219
172c031ad743
restate various simp rules for word operations using pred_numeral
huffman
parents:
47108
diff
changeset
|
573 |
"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
|
574 |
by (simp add: numeral_eq_Suc) |
24333 | 575 |
|
65363 | 576 |
|
577 |
subsection \<open>Links with function \<open>bl_to_bin\<close>\<close> |
|
24333 | 578 |
|
65363 | 579 |
lemma bl_to_bin_aux_cat: |
580 |
"\<And>nv v. bl_to_bin_aux bs (bin_cat w nv v) = |
|
26557 | 581 |
bin_cat w (nv + length bs) (bl_to_bin_aux bs v)" |
65363 | 582 |
by (induct bs) (simp, simp add: bin_cat_Suc_Bit [symmetric] del: bin_cat.simps) |
24333 | 583 |
|
65363 | 584 |
lemma bin_to_bl_aux_cat: |
585 |
"\<And>w bs. bin_to_bl_aux (nv + nw) (bin_cat v nw w) bs = |
|
24333 | 586 |
bin_to_bl_aux nv v (bin_to_bl_aux nw w bs)" |
65363 | 587 |
by (induct nw) auto |
24333 | 588 |
|
65363 | 589 |
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
|
590 |
using bl_to_bin_aux_cat [where nv = "0" and v = "0"] |
65363 | 591 |
by (simp add: bl_to_bin_def [symmetric]) |
24333 | 592 |
|
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
593 |
lemma bin_to_bl_cat: |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
594 |
"bin_to_bl (nv + nw) (bin_cat v nw w) = |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
595 |
bin_to_bl_aux nv v (bin_to_bl nw w)" |
65363 | 596 |
by (simp add: bin_to_bl_def bin_to_bl_aux_cat) |
24333 | 597 |
|
65363 | 598 |
lemmas bl_to_bin_aux_app_cat = |
24333 | 599 |
trans [OF bl_to_bin_aux_append bl_to_bin_aux_alt] |
600 |
||
601 |
lemmas bin_to_bl_aux_cat_app = |
|
602 |
trans [OF bin_to_bl_aux_cat bin_to_bl_aux_alt] |
|
603 |
||
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
604 |
lemma bl_to_bin_app_cat: |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
605 |
"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
|
606 |
by (simp only: bl_to_bin_aux_app_cat bl_to_bin_def) |
24333 | 607 |
|
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
608 |
lemma bin_to_bl_cat_app: |
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
609 |
"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
|
610 |
by (simp only: bin_to_bl_def bin_to_bl_aux_cat_app) |
24333 | 611 |
|
65363 | 612 |
text \<open>\<open>bl_to_bin_app_cat_alt\<close> and \<open>bl_to_bin_app_cat\<close> are easily interderivable.\<close> |
613 |
lemma bl_to_bin_app_cat_alt: "bin_cat (bl_to_bin cs) n w = bl_to_bin (cs @ bin_to_bl n w)" |
|
614 |
by (simp add: bl_to_bin_app_cat) |
|
24333 | 615 |
|
65363 | 616 |
lemma mask_lem: "(bl_to_bin (True # replicate n False)) = bl_to_bin (replicate n True) + 1" |
24333 | 617 |
apply (unfold bl_to_bin_def) |
618 |
apply (induct n) |
|
46645
573aff6b9b0a
adapt lemma mask_lem to respect int/bin distinction
huffman
parents:
46617
diff
changeset
|
619 |
apply simp |
65363 | 620 |
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
|
621 |
apply (simp add: Bit_B0_2t Bit_B1_2t) |
24333 | 622 |
done |
623 |
||
65363 | 624 |
|
625 |
subsection \<open>Function \<open>bl_of_nth\<close>\<close> |
|
626 |
||
24333 | 627 |
lemma length_bl_of_nth [simp]: "length (bl_of_nth n f) = n" |
628 |
by (induct n) auto |
|
629 |
||
65363 | 630 |
lemma nth_bl_of_nth [simp]: "m < n \<Longrightarrow> rev (bl_of_nth n f) ! m = f m" |
24333 | 631 |
apply (induct n) |
632 |
apply simp |
|
65363 | 633 |
apply (clarsimp simp add: nth_append) |
634 |
apply (rule_tac f = "f" in arg_cong) |
|
24333 | 635 |
apply simp |
636 |
done |
|
637 |
||
65363 | 638 |
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 | 639 |
by (induct n) auto |
640 |
||
65363 | 641 |
lemma bl_of_nth_nth_le: "n \<le> length xs \<Longrightarrow> bl_of_nth n (nth (rev xs)) = drop (length xs - n) xs" |
642 |
apply (induct n arbitrary: xs) |
|
643 |
apply clarsimp |
|
24333 | 644 |
apply clarsimp |
645 |
apply (rule trans [OF _ hd_Cons_tl]) |
|
646 |
apply (frule Suc_le_lessD) |
|
647 |
apply (simp add: nth_rev trans [OF drop_Suc drop_tl, symmetric]) |
|
648 |
apply (subst hd_drop_conv_nth) |
|
65363 | 649 |
apply force |
650 |
apply simp_all |
|
651 |
apply (rule_tac f = "\<lambda>n. drop n xs" in arg_cong) |
|
24333 | 652 |
apply simp |
653 |
done |
|
654 |
||
45854
40554613b4f0
replace many uses of 'lemmas' with explicit 'lemma'
huffman
parents:
45847
diff
changeset
|
655 |
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
|
656 |
by (simp add: bl_of_nth_nth_le) |
24333 | 657 |
|
658 |
lemma size_rbl_pred: "length (rbl_pred bl) = length bl" |
|
659 |
by (induct bl) auto |
|
660 |
||
661 |
lemma size_rbl_succ: "length (rbl_succ bl) = length bl" |
|
662 |
by (induct bl) auto |
|
663 |
||
65363 | 664 |
lemma size_rbl_add: "length (rbl_add bl cl) = length bl" |
665 |
by (induct bl arbitrary: cl) (auto simp: Let_def size_rbl_succ) |
|
24333 | 666 |
|
65363 | 667 |
lemma size_rbl_mult: "length (rbl_mult bl cl) = length bl" |
668 |
by (induct bl arbitrary: cl) (auto simp add: Let_def size_rbl_add) |
|
24333 | 669 |
|
65363 | 670 |
lemmas rbl_sizes [simp] = |
24333 | 671 |
size_rbl_pred size_rbl_succ size_rbl_add size_rbl_mult |
672 |
||
673 |
lemmas rbl_Nils = |
|
674 |
rbl_pred.Nil rbl_succ.Nil rbl_add.Nil rbl_mult.Nil |
|
675 |
||
65363 | 676 |
lemma rbl_pred: "rbl_pred (rev (bin_to_bl n bin)) = rev (bin_to_bl n (bin - 1))" |
24333 | 677 |
apply (unfold bin_to_bl_def) |
65363 | 678 |
apply (induct n arbitrary: bin) |
679 |
apply simp |
|
24333 | 680 |
apply clarsimp |
681 |
apply (case_tac bin rule: bin_exhaust) |
|
682 |
apply (case_tac b) |
|
46653 | 683 |
apply (clarsimp simp: bin_to_bl_aux_alt)+ |
24333 | 684 |
done |
685 |
||
65363 | 686 |
lemma rbl_succ: "rbl_succ (rev (bin_to_bl n bin)) = rev (bin_to_bl n (bin + 1))" |
24333 | 687 |
apply (unfold bin_to_bl_def) |
65363 | 688 |
apply (induct n arbitrary: bin) |
689 |
apply simp |
|
24333 | 690 |
apply clarsimp |
691 |
apply (case_tac bin rule: bin_exhaust) |
|
692 |
apply (case_tac b) |
|
46653 | 693 |
apply (clarsimp simp: bin_to_bl_aux_alt)+ |
24333 | 694 |
done |
695 |
||
65363 | 696 |
lemma rbl_add: |
697 |
"\<And>bina binb. rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) = |
|
24333 | 698 |
rev (bin_to_bl n (bina + binb))" |
699 |
apply (unfold bin_to_bl_def) |
|
65363 | 700 |
apply (induct n) |
701 |
apply simp |
|
24333 | 702 |
apply clarsimp |
703 |
apply (case_tac bina rule: bin_exhaust) |
|
704 |
apply (case_tac binb rule: bin_exhaust) |
|
705 |
apply (case_tac b) |
|
706 |
apply (case_tac [!] "ba") |
|
57514
bdc2c6b40bf2
prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents:
57492
diff
changeset
|
707 |
apply (auto simp: rbl_succ bin_to_bl_aux_alt Let_def ac_simps) |
24333 | 708 |
done |
709 |
||
65363 | 710 |
lemma rbl_add_app2: "length blb \<ge> length bla \<Longrightarrow> rbl_add bla (blb @ blc) = rbl_add bla blb" |
711 |
apply (induct bla arbitrary: blb) |
|
712 |
apply simp |
|
24333 | 713 |
apply clarsimp |
714 |
apply (case_tac blb, clarsimp) |
|
715 |
apply (clarsimp simp: Let_def) |
|
716 |
done |
|
717 |
||
65363 | 718 |
lemma rbl_add_take2: |
67120 | 719 |
"length blb \<ge> length bla \<Longrightarrow> rbl_add bla (take (length bla) blb) = rbl_add bla blb" |
65363 | 720 |
apply (induct bla arbitrary: blb) |
721 |
apply simp |
|
24333 | 722 |
apply clarsimp |
723 |
apply (case_tac blb, clarsimp) |
|
724 |
apply (clarsimp simp: Let_def) |
|
725 |
done |
|
726 |
||
65363 | 727 |
lemma rbl_add_long: |
728 |
"m \<ge> n \<Longrightarrow> rbl_add (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) = |
|
24333 | 729 |
rev (bin_to_bl n (bina + binb))" |
730 |
apply (rule box_equals [OF _ rbl_add_take2 rbl_add]) |
|
65363 | 731 |
apply (rule_tac f = "rbl_add (rev (bin_to_bl n bina))" in arg_cong) |
24333 | 732 |
apply (rule rev_swap [THEN iffD1]) |
733 |
apply (simp add: rev_take drop_bin2bl) |
|
734 |
apply simp |
|
735 |
done |
|
736 |
||
65363 | 737 |
lemma rbl_mult_app2: "length blb \<ge> length bla \<Longrightarrow> rbl_mult bla (blb @ blc) = rbl_mult bla blb" |
738 |
apply (induct bla arbitrary: blb) |
|
739 |
apply simp |
|
24333 | 740 |
apply clarsimp |
741 |
apply (case_tac blb, clarsimp) |
|
742 |
apply (clarsimp simp: Let_def rbl_add_app2) |
|
743 |
done |
|
744 |
||
65363 | 745 |
lemma rbl_mult_take2: |
746 |
"length blb \<ge> length bla \<Longrightarrow> rbl_mult bla (take (length bla) blb) = rbl_mult bla blb" |
|
24333 | 747 |
apply (rule trans) |
748 |
apply (rule rbl_mult_app2 [symmetric]) |
|
749 |
apply simp |
|
65363 | 750 |
apply (rule_tac f = "rbl_mult bla" in arg_cong) |
24333 | 751 |
apply (rule append_take_drop_id) |
752 |
done |
|
65363 | 753 |
|
754 |
lemma rbl_mult_gt1: |
|
755 |
"m \<ge> length bl \<Longrightarrow> |
|
756 |
rbl_mult bl (rev (bin_to_bl m binb)) = |
|
24333 | 757 |
rbl_mult bl (rev (bin_to_bl (length bl) binb))" |
758 |
apply (rule trans) |
|
759 |
apply (rule rbl_mult_take2 [symmetric]) |
|
760 |
apply simp_all |
|
65363 | 761 |
apply (rule_tac f = "rbl_mult bl" in arg_cong) |
24333 | 762 |
apply (rule rev_swap [THEN iffD1]) |
763 |
apply (simp add: rev_take drop_bin2bl) |
|
764 |
done |
|
65363 | 765 |
|
766 |
lemma rbl_mult_gt: |
|
767 |
"m > n \<Longrightarrow> |
|
768 |
rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl m binb)) = |
|
24333 | 769 |
rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb))" |
770 |
by (auto intro: trans [OF rbl_mult_gt1]) |
|
65363 | 771 |
|
24333 | 772 |
lemmas rbl_mult_Suc = lessI [THEN rbl_mult_gt] |
773 |
||
65363 | 774 |
lemma rbbl_Cons: "b # rev (bin_to_bl n x) = rev (bin_to_bl (Suc n) (x BIT b))" |
775 |
by (simp add: bin_to_bl_def) (simp add: bin_to_bl_aux_alt) |
|
46653 | 776 |
|
65363 | 777 |
lemma rbl_mult: |
778 |
"rbl_mult (rev (bin_to_bl n bina)) (rev (bin_to_bl n binb)) = |
|
24333 | 779 |
rev (bin_to_bl n (bina * binb))" |
65363 | 780 |
apply (induct n arbitrary: bina binb) |
24333 | 781 |
apply simp |
782 |
apply (unfold bin_to_bl_def) |
|
783 |
apply clarsimp |
|
784 |
apply (case_tac bina rule: bin_exhaust) |
|
785 |
apply (case_tac binb rule: bin_exhaust) |
|
786 |
apply (case_tac b) |
|
787 |
apply (case_tac [!] "ba") |
|
46653 | 788 |
apply (auto simp: bin_to_bl_aux_alt Let_def) |
789 |
apply (auto simp: rbbl_Cons rbl_mult_Suc rbl_add) |
|
24333 | 790 |
done |
791 |
||
65363 | 792 |
lemma rbl_add_split: |
793 |
"P (rbl_add (y # ys) (x # xs)) = |
|
794 |
(\<forall>ws. length ws = length ys \<longrightarrow> ws = rbl_add ys xs \<longrightarrow> |
|
795 |
(y \<longrightarrow> ((x \<longrightarrow> P (False # rbl_succ ws)) \<and> (\<not> x \<longrightarrow> P (True # ws)))) \<and> |
|
796 |
(\<not> y \<longrightarrow> P (x # ws)))" |
|
797 |
by (cases y) (auto simp: Let_def) |
|
24333 | 798 |
|
65363 | 799 |
lemma rbl_mult_split: |
800 |
"P (rbl_mult (y # ys) xs) = |
|
801 |
(\<forall>ws. length ws = Suc (length ys) \<longrightarrow> ws = False # rbl_mult ys xs \<longrightarrow> |
|
802 |
(y \<longrightarrow> P (rbl_add ws xs)) \<and> (\<not> y \<longrightarrow> P ws))" |
|
803 |
by (auto simp: Let_def) |
|
24333 | 804 |
|
805 |
||
65363 | 806 |
subsection \<open>Repeated splitting or concatenation\<close> |
807 |
||
808 |
lemma sclem: "size (concat (map (bin_to_bl n) xs)) = length xs * n" |
|
24333 | 809 |
by (induct xs) auto |
810 |
||
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
811 |
lemma bin_cat_foldl_lem: |
65363 | 812 |
"foldl (\<lambda>u. bin_cat u n) x xs = |
813 |
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
|
814 |
apply (induct xs arbitrary: x) |
24333 | 815 |
apply simp |
816 |
apply (simp (no_asm)) |
|
817 |
apply (frule asm_rl) |
|
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
818 |
apply (drule meta_spec) |
24333 | 819 |
apply (erule trans) |
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
820 |
apply (drule_tac x = "bin_cat y n a" in meta_spec) |
65363 | 821 |
apply (simp add: bin_cat_assoc_sym min.absorb2) |
24333 | 822 |
done |
823 |
||
65363 | 824 |
lemma bin_rcat_bl: "bin_rcat n wl = bl_to_bin (concat (map (bin_to_bl n) wl))" |
24333 | 825 |
apply (unfold bin_rcat_def) |
826 |
apply (rule sym) |
|
827 |
apply (induct wl) |
|
65363 | 828 |
apply (auto simp add: bl_to_bin_append) |
829 |
apply (simp add: bl_to_bin_aux_alt sclem) |
|
830 |
apply (simp add: bin_cat_foldl_lem [symmetric]) |
|
24333 | 831 |
done |
832 |
||
833 |
lemmas bin_rsplit_aux_simps = bin_rsplit_aux.simps bin_rsplitl_aux.simps |
|
834 |
lemmas rsplit_aux_simps = bin_rsplit_aux_simps |
|
835 |
||
62390 | 836 |
lemmas th_if_simp1 = if_split [where P = "op = l", THEN iffD1, THEN conjunct1, THEN mp] for l |
837 |
lemmas th_if_simp2 = if_split [where P = "op = l", THEN iffD1, THEN conjunct2, THEN mp] for l |
|
24333 | 838 |
|
839 |
lemmas rsplit_aux_simp1s = rsplit_aux_simps [THEN th_if_simp1] |
|
840 |
||
841 |
lemmas rsplit_aux_simp2ls = rsplit_aux_simps [THEN th_if_simp2] |
|
842 |
(* these safe to [simp add] as require calculating m - n *) |
|
843 |
lemmas bin_rsplit_aux_simp2s [simp] = rsplit_aux_simp2ls [unfolded Let_def] |
|
844 |
lemmas rbscl = bin_rsplit_aux_simp2s (2) |
|
845 |
||
65363 | 846 |
lemmas rsplit_aux_0_simps [simp] = |
24333 | 847 |
rsplit_aux_simp1s [OF disjI1] rsplit_aux_simp1s [OF disjI2] |
848 |
||
65363 | 849 |
lemma bin_rsplit_aux_append: "bin_rsplit_aux n m c (bs @ cs) = bin_rsplit_aux n m c bs @ cs" |
26557 | 850 |
apply (induct n m c bs rule: bin_rsplit_aux.induct) |
24333 | 851 |
apply (subst bin_rsplit_aux.simps) |
852 |
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
|
853 |
apply (clarsimp split: prod.split) |
24333 | 854 |
done |
855 |
||
65363 | 856 |
lemma bin_rsplitl_aux_append: "bin_rsplitl_aux n m c (bs @ cs) = bin_rsplitl_aux n m c bs @ cs" |
26557 | 857 |
apply (induct n m c bs rule: bin_rsplitl_aux.induct) |
24333 | 858 |
apply (subst bin_rsplitl_aux.simps) |
859 |
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
|
860 |
apply (clarsimp split: prod.split) |
24333 | 861 |
done |
862 |
||
863 |
lemmas rsplit_aux_apps [where bs = "[]"] = |
|
864 |
bin_rsplit_aux_append bin_rsplitl_aux_append |
|
865 |
||
866 |
lemmas rsplit_def_auxs = bin_rsplit_def bin_rsplitl_def |
|
867 |
||
65363 | 868 |
lemmas rsplit_aux_alts = rsplit_aux_apps |
24333 | 869 |
[unfolded append_Nil rsplit_def_auxs [symmetric]] |
870 |
||
65363 | 871 |
lemma bin_split_minus: "0 < n \<Longrightarrow> bin_split (Suc (n - 1)) w = bin_split n w" |
24333 | 872 |
by auto |
873 |
||
874 |
lemmas bin_split_minus_simp = |
|
45604 | 875 |
bin_split.Suc [THEN [2] bin_split_minus [symmetric, THEN trans]] |
24333 | 876 |
|
65363 | 877 |
lemma bin_split_pred_simp [simp]: |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46655
diff
changeset
|
878 |
"(0::nat) < numeral bin \<Longrightarrow> |
65363 | 879 |
bin_split (numeral bin) w = |
880 |
(let (w1, w2) = bin_split (numeral bin - 1) (bin_rest w) |
|
881 |
in (w1, w2 BIT bin_last w))" |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46655
diff
changeset
|
882 |
by (simp only: bin_split_minus_simp) |
24333 | 883 |
|
884 |
lemma bin_rsplit_aux_simp_alt: |
|
26557 | 885 |
"bin_rsplit_aux n m c bs = |
65363 | 886 |
(if m = 0 \<or> n = 0 then bs |
887 |
else let (a, b) = bin_split n c in bin_rsplit n (m - n, a) @ b # bs)" |
|
888 |
apply (simp add: bin_rsplit_aux.simps [of n m c bs]) |
|
26557 | 889 |
apply (subst rsplit_aux_alts) |
890 |
apply (simp add: bin_rsplit_def) |
|
24333 | 891 |
done |
892 |
||
65363 | 893 |
lemmas bin_rsplit_simp_alt = |
45604 | 894 |
trans [OF bin_rsplit_def bin_rsplit_aux_simp_alt] |
24333 | 895 |
|
896 |
lemmas bthrs = bin_rsplit_simp_alt [THEN [2] trans] |
|
897 |
||
65363 | 898 |
lemma bin_rsplit_size_sign' [rule_format]: |
899 |
"n > 0 \<Longrightarrow> rev sw = bin_rsplit n (nw, w) \<Longrightarrow> \<forall>v\<in>set sw. bintrunc n v = v" |
|
62957 | 900 |
apply (induct sw arbitrary: nw w) |
24333 | 901 |
apply clarsimp |
902 |
apply clarsimp |
|
903 |
apply (drule bthrs) |
|
62390 | 904 |
apply (simp (no_asm_use) add: Let_def split: prod.split_asm if_split_asm) |
24333 | 905 |
apply clarify |
906 |
apply (drule split_bintrunc) |
|
907 |
apply simp |
|
908 |
done |
|
909 |
||
65363 | 910 |
lemmas bin_rsplit_size_sign = bin_rsplit_size_sign' [OF asm_rl |
45604 | 911 |
rev_rev_ident [THEN trans] set_rev [THEN equalityD2 [THEN subsetD]]] |
24333 | 912 |
|
913 |
lemma bin_nth_rsplit [rule_format] : |
|
65363 | 914 |
"n > 0 \<Longrightarrow> m < n \<Longrightarrow> |
915 |
\<forall>w k nw. |
|
916 |
rev sw = bin_rsplit n (nw, w) \<longrightarrow> |
|
917 |
k < size sw \<longrightarrow> bin_nth (sw ! k) m = bin_nth w (k * n + m)" |
|
24333 | 918 |
apply (induct sw) |
919 |
apply clarsimp |
|
920 |
apply clarsimp |
|
921 |
apply (drule bthrs) |
|
62390 | 922 |
apply (simp (no_asm_use) add: Let_def split: prod.split_asm if_split_asm) |
24333 | 923 |
apply clarify |
924 |
apply (erule allE, erule impE, erule exI) |
|
925 |
apply (case_tac k) |
|
65363 | 926 |
apply clarsimp |
24333 | 927 |
prefer 2 |
928 |
apply clarsimp |
|
929 |
apply (erule allE) |
|
930 |
apply (erule (1) impE) |
|
65363 | 931 |
apply (drule bin_nth_split, erule conjE, erule allE, erule trans, simp add: ac_simps)+ |
24333 | 932 |
done |
933 |
||
65363 | 934 |
lemma bin_rsplit_all: "0 < nw \<Longrightarrow> nw \<le> n \<Longrightarrow> bin_rsplit n (nw, w) = [bintrunc n w]" |
935 |
by (auto simp: bin_rsplit_def rsplit_aux_simp2ls split: prod.split dest!: split_bintrunc) |
|
24333 | 936 |
|
65363 | 937 |
lemma bin_rsplit_l [rule_format]: |
938 |
"\<forall>bin. bin_rsplitl n (m, bin) = bin_rsplit n (m, bintrunc m bin)" |
|
24333 | 939 |
apply (rule_tac a = "m" in wf_less_than [THEN wf_induct]) |
65363 | 940 |
apply (simp (no_asm) add: bin_rsplitl_def bin_rsplit_def) |
24333 | 941 |
apply (rule allI) |
942 |
apply (subst bin_rsplitl_aux.simps) |
|
943 |
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
|
944 |
apply (clarsimp simp: Let_def split: prod.split) |
24333 | 945 |
apply (drule bin_split_trunc) |
946 |
apply (drule sym [THEN trans], assumption) |
|
26557 | 947 |
apply (subst rsplit_aux_alts(1)) |
948 |
apply (subst rsplit_aux_alts(2)) |
|
949 |
apply clarsimp |
|
950 |
unfolding bin_rsplit_def bin_rsplitl_def |
|
951 |
apply simp |
|
24333 | 952 |
done |
26557 | 953 |
|
65363 | 954 |
lemma bin_rsplit_rcat [rule_format]: |
955 |
"n > 0 \<longrightarrow> bin_rsplit n (n * size ws, bin_rcat n ws) = map (bintrunc n) ws" |
|
24333 | 956 |
apply (unfold bin_rsplit_def bin_rcat_def) |
65363 | 957 |
apply (rule_tac xs = ws in rev_induct) |
24333 | 958 |
apply clarsimp |
959 |
apply clarsimp |
|
26557 | 960 |
apply (subst rsplit_aux_alts) |
961 |
unfolding bin_split_cat |
|
962 |
apply simp |
|
24333 | 963 |
done |
964 |
||
965 |
lemma bin_rsplit_aux_len_le [rule_format] : |
|
26557 | 966 |
"\<forall>ws m. n \<noteq> 0 \<longrightarrow> ws = bin_rsplit_aux n nw w bs \<longrightarrow> |
967 |
length ws \<le> m \<longleftrightarrow> nw + length bs * n \<le> m * n" |
|
54871 | 968 |
proof - |
65363 | 969 |
have *: R |
970 |
if d: "i \<le> j \<or> m < j'" |
|
971 |
and R1: "i * k \<le> j * k \<Longrightarrow> R" |
|
972 |
and R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R" |
|
973 |
for i j j' k k' m :: nat and R |
|
974 |
using d |
|
975 |
apply safe |
|
976 |
apply (rule R1, erule mult_le_mono1) |
|
977 |
apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]]) |
|
978 |
done |
|
979 |
have **: "0 < sc \<Longrightarrow> sc - n + (n + lb * n) \<le> m * n \<longleftrightarrow> sc + lb * n \<le> m * n" |
|
980 |
for sc m n lb :: nat |
|
981 |
apply safe |
|
982 |
apply arith |
|
983 |
apply (case_tac "sc \<ge> n") |
|
984 |
apply arith |
|
985 |
apply (insert linorder_le_less_linear [of m lb]) |
|
986 |
apply (erule_tac k=n and k'=n in *) |
|
987 |
apply arith |
|
988 |
apply simp |
|
989 |
done |
|
54871 | 990 |
show ?thesis |
991 |
apply (induct n nw w bs rule: bin_rsplit_aux.induct) |
|
992 |
apply (subst bin_rsplit_aux.simps) |
|
65363 | 993 |
apply (simp add: ** Let_def split: prod.split) |
54871 | 994 |
done |
995 |
qed |
|
24333 | 996 |
|
65363 | 997 |
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" |
998 |
by (auto simp: bin_rsplit_def bin_rsplit_aux_len_le) |
|
999 |
||
45997
13392893ea12
use 'induct arbitrary' instead of 'rule_format' attribute
huffman
parents:
45996
diff
changeset
|
1000 |
lemma bin_rsplit_aux_len: |
65363 | 1001 |
"n \<noteq> 0 \<Longrightarrow> length (bin_rsplit_aux n nw w cs) = (nw + n - 1) div n + length cs" |
26557 | 1002 |
apply (induct n nw w cs rule: bin_rsplit_aux.induct) |
24333 | 1003 |
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
|
1004 |
apply (clarsimp simp: Let_def split: prod.split) |
24333 | 1005 |
apply (erule thin_rl) |
27651
16a26996c30e
moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents:
27105
diff
changeset
|
1006 |
apply (case_tac m) |
65363 | 1007 |
apply simp |
1008 |
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
|
1009 |
apply (auto simp add: div_add_self2) |
24333 | 1010 |
done |
1011 |
||
65363 | 1012 |
lemma bin_rsplit_len: "n \<noteq> 0 \<Longrightarrow> length (bin_rsplit n (nw, w)) = (nw + n - 1) div n" |
1013 |
by (auto simp: bin_rsplit_def bin_rsplit_aux_len) |
|
24333 | 1014 |
|
26557 | 1015 |
lemma bin_rsplit_aux_len_indep: |
1016 |
"n \<noteq> 0 \<Longrightarrow> length bs = length cs \<Longrightarrow> |
|
1017 |
length (bin_rsplit_aux n nw v bs) = |
|
1018 |
length (bin_rsplit_aux n nw w cs)" |
|
1019 |
proof (induct n nw w cs arbitrary: v bs rule: bin_rsplit_aux.induct) |
|
65363 | 1020 |
case (1 n m w cs v bs) |
1021 |
show ?case |
|
26557 | 1022 |
proof (cases "m = 0") |
65363 | 1023 |
case True |
1024 |
with \<open>length bs = length cs\<close> show ?thesis by simp |
|
26557 | 1025 |
next |
1026 |
case False |
|
67120 | 1027 |
from "1.hyps" \<open>m \<noteq> 0\<close> \<open>n \<noteq> 0\<close> |
1028 |
have hyp: "\<And>v bs. length bs = Suc (length cs) \<Longrightarrow> |
|
26557 | 1029 |
length (bin_rsplit_aux n (m - n) v bs) = |
1030 |
length (bin_rsplit_aux n (m - n) (fst (bin_split n w)) (snd (bin_split n w) # cs))" |
|
65363 | 1031 |
by auto |
1032 |
from \<open>length bs = length cs\<close> \<open>n \<noteq> 0\<close> show ?thesis |
|
1033 |
by (auto simp add: bin_rsplit_aux_simp_alt Let_def bin_rsplit_len split: prod.split) |
|
26557 | 1034 |
qed |
1035 |
qed |
|
24333 | 1036 |
|
65363 | 1037 |
lemma bin_rsplit_len_indep: |
1038 |
"n \<noteq> 0 \<Longrightarrow> length (bin_rsplit n (nw, v)) = length (bin_rsplit n (nw, w))" |
|
24333 | 1039 |
apply (unfold bin_rsplit_def) |
26557 | 1040 |
apply (simp (no_asm)) |
24333 | 1041 |
apply (erule bin_rsplit_aux_len_indep) |
1042 |
apply (rule refl) |
|
1043 |
done |
|
1044 |
||
54874 | 1045 |
|
61799 | 1046 |
text \<open>Even more bit operations\<close> |
54874 | 1047 |
|
1048 |
instantiation int :: bitss |
|
1049 |
begin |
|
1050 |
||
65363 | 1051 |
definition [iff]: "i !! n \<longleftrightarrow> bin_nth i n" |
54874 | 1052 |
|
65363 | 1053 |
definition "lsb i = i !! 0" for i :: int |
54874 | 1054 |
|
65363 | 1055 |
definition "set_bit i n b = bin_sc n b i" |
54874 | 1056 |
|
1057 |
definition |
|
1058 |
"set_bits f = |
|
65363 | 1059 |
(if \<exists>n. \<forall>n'\<ge>n. \<not> f n' then |
1060 |
let n = LEAST n. \<forall>n'\<ge>n. \<not> f n' |
|
1061 |
in bl_to_bin (rev (map f [0..<n])) |
|
1062 |
else if \<exists>n. \<forall>n'\<ge>n. f n' then |
|
1063 |
let n = LEAST n. \<forall>n'\<ge>n. f n' |
|
1064 |
in sbintrunc n (bl_to_bin (True # rev (map f [0..<n]))) |
|
1065 |
else 0 :: int)" |
|
54874 | 1066 |
|
65363 | 1067 |
definition "shiftl x n = x * 2 ^ n" for x :: int |
54874 | 1068 |
|
65363 | 1069 |
definition "shiftr x n = x div 2 ^ n" for x :: int |
54874 | 1070 |
|
65363 | 1071 |
definition "msb x \<longleftrightarrow> x < 0" for x :: int |
54874 | 1072 |
|
1073 |
instance .. |
|
1074 |
||
24333 | 1075 |
end |
54874 | 1076 |
|
1077 |
end |