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