author | nipkow |
Tue, 20 Sep 2005 13:17:55 +0200 | |
changeset 17501 | acbebb72e85a |
parent 17090 | 603f23d71ada |
child 17589 | 58eeffd73be1 |
permissions | -rw-r--r-- |
13462 | 1 |
(* Title: HOL/List.thy |
2 |
ID: $Id$ |
|
3 |
Author: Tobias Nipkow |
|
923 | 4 |
*) |
5 |
||
13114 | 6 |
header {* The datatype of finite lists *} |
13122 | 7 |
|
15131 | 8 |
theory List |
15140 | 9 |
imports PreList |
15131 | 10 |
begin |
923 | 11 |
|
13142 | 12 |
datatype 'a list = |
13366 | 13 |
Nil ("[]") |
14 |
| Cons 'a "'a list" (infixr "#" 65) |
|
923 | 15 |
|
15392 | 16 |
subsection{*Basic list processing functions*} |
15302 | 17 |
|
923 | 18 |
consts |
13366 | 19 |
"@" :: "'a list => 'a list => 'a list" (infixr 65) |
20 |
filter:: "('a => bool) => 'a list => 'a list" |
|
21 |
concat:: "'a list list => 'a list" |
|
22 |
foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b" |
|
23 |
foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b" |
|
24 |
hd:: "'a list => 'a" |
|
25 |
tl:: "'a list => 'a list" |
|
26 |
last:: "'a list => 'a" |
|
27 |
butlast :: "'a list => 'a list" |
|
28 |
set :: "'a list => 'a set" |
|
29 |
list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" |
|
30 |
map :: "('a=>'b) => ('a list => 'b list)" |
|
31 |
nth :: "'a list => nat => 'a" (infixl "!" 100) |
|
32 |
list_update :: "'a list => nat => 'a => 'a list" |
|
33 |
take:: "nat => 'a list => 'a list" |
|
34 |
drop:: "nat => 'a list => 'a list" |
|
35 |
takeWhile :: "('a => bool) => 'a list => 'a list" |
|
36 |
dropWhile :: "('a => bool) => 'a list => 'a list" |
|
37 |
rev :: "'a list => 'a list" |
|
38 |
zip :: "'a list => 'b list => ('a * 'b) list" |
|
15425 | 39 |
upt :: "nat => nat => nat list" ("(1[_..</_'])") |
13366 | 40 |
remdups :: "'a list => 'a list" |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
41 |
remove1 :: "'a => 'a list => 'a list" |
13366 | 42 |
null:: "'a list => bool" |
43 |
"distinct":: "'a list => bool" |
|
44 |
replicate :: "nat => 'a => 'a list" |
|
15302 | 45 |
rotate1 :: "'a list \<Rightarrow> 'a list" |
46 |
rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" |
|
47 |
sublist :: "'a list => nat set => 'a list" |
|
17086 | 48 |
(* For efficiency *) |
49 |
mem :: "'a => 'a list => bool" (infixl 55) |
|
50 |
list_inter :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
|
51 |
list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" |
|
52 |
list_all:: "('a => bool) => ('a list => bool)" |
|
53 |
itrev :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
|
54 |
filtermap :: "('a \<Rightarrow> 'b option) \<Rightarrow> 'a list \<Rightarrow> 'b list" |
|
55 |
map_filter :: "('a => 'b) => ('a => bool) => 'a list => 'b list" |
|
15302 | 56 |
|
923 | 57 |
|
13146 | 58 |
nonterminals lupdbinds lupdbind |
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
59 |
|
923 | 60 |
syntax |
13366 | 61 |
-- {* list Enumeration *} |
62 |
"@list" :: "args => 'a list" ("[(_)]") |
|
923 | 63 |
|
13366 | 64 |
-- {* Special syntax for filter *} |
65 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list" ("(1[_:_./ _])") |
|
923 | 66 |
|
13366 | 67 |
-- {* list update *} |
68 |
"_lupdbind":: "['a, 'a] => lupdbind" ("(2_ :=/ _)") |
|
69 |
"" :: "lupdbind => lupdbinds" ("_") |
|
70 |
"_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds" ("_,/ _") |
|
71 |
"_LUpdate" :: "['a, lupdbinds] => 'a" ("_/[(_)]" [900,0] 900) |
|
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
72 |
|
13366 | 73 |
upto:: "nat => nat => nat list" ("(1[_../_])") |
5427 | 74 |
|
923 | 75 |
translations |
13366 | 76 |
"[x, xs]" == "x#[xs]" |
77 |
"[x]" == "x#[]" |
|
78 |
"[x:xs . P]"== "filter (%x. P) xs" |
|
923 | 79 |
|
13366 | 80 |
"_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs" |
81 |
"xs[i:=x]" == "list_update xs i x" |
|
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
82 |
|
15425 | 83 |
"[i..j]" == "[i..<(Suc j)]" |
5427 | 84 |
|
85 |
||
12114
a8e860c86252
eliminated old "symbols" syntax, use "xsymbols" instead;
wenzelm
parents:
10832
diff
changeset
|
86 |
syntax (xsymbols) |
13366 | 87 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])") |
14565 | 88 |
syntax (HTML output) |
89 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])") |
|
3342
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
90 |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
91 |
|
13142 | 92 |
text {* |
14589 | 93 |
Function @{text size} is overloaded for all datatypes. Users may |
13366 | 94 |
refer to the list version as @{text length}. *} |
13142 | 95 |
|
96 |
syntax length :: "'a list => nat" |
|
97 |
translations "length" => "size :: _ list => nat" |
|
13114 | 98 |
|
13142 | 99 |
typed_print_translation {* |
13366 | 100 |
let |
101 |
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] = |
|
102 |
Syntax.const "length" $ t |
|
103 |
| size_tr' _ _ _ = raise Match; |
|
104 |
in [("size", size_tr')] end |
|
13114 | 105 |
*} |
3437
bea2faf1641d
Replacing the primrec definition of "length" by a translation to the built-in
paulson
parents:
3401
diff
changeset
|
106 |
|
15302 | 107 |
|
5183 | 108 |
primrec |
15307 | 109 |
"hd(x#xs) = x" |
110 |
||
5183 | 111 |
primrec |
15307 | 112 |
"tl([]) = []" |
113 |
"tl(x#xs) = xs" |
|
114 |
||
5183 | 115 |
primrec |
15307 | 116 |
"null([]) = True" |
117 |
"null(x#xs) = False" |
|
118 |
||
8972 | 119 |
primrec |
15307 | 120 |
"last(x#xs) = (if xs=[] then x else last xs)" |
121 |
||
5183 | 122 |
primrec |
15307 | 123 |
"butlast []= []" |
124 |
"butlast(x#xs) = (if xs=[] then [] else x#butlast xs)" |
|
125 |
||
5183 | 126 |
primrec |
15307 | 127 |
"set [] = {}" |
128 |
"set (x#xs) = insert x (set xs)" |
|
129 |
||
5183 | 130 |
primrec |
15307 | 131 |
"map f [] = []" |
132 |
"map f (x#xs) = f(x)#map f xs" |
|
133 |
||
5183 | 134 |
primrec |
15307 | 135 |
append_Nil:"[]@ys = ys" |
136 |
append_Cons: "(x#xs)@ys = x#(xs@ys)" |
|
137 |
||
5183 | 138 |
primrec |
15307 | 139 |
"rev([]) = []" |
140 |
"rev(x#xs) = rev(xs) @ [x]" |
|
141 |
||
5183 | 142 |
primrec |
15307 | 143 |
"filter P [] = []" |
144 |
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" |
|
145 |
||
5183 | 146 |
primrec |
15307 | 147 |
foldl_Nil:"foldl f a [] = a" |
148 |
foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs" |
|
149 |
||
8000 | 150 |
primrec |
15307 | 151 |
"foldr f [] a = a" |
152 |
"foldr f (x#xs) a = f x (foldr f xs a)" |
|
153 |
||
5183 | 154 |
primrec |
15307 | 155 |
"concat([]) = []" |
156 |
"concat(x#xs) = x @ concat(xs)" |
|
157 |
||
5183 | 158 |
primrec |
15307 | 159 |
drop_Nil:"drop n [] = []" |
160 |
drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)" |
|
161 |
-- {*Warning: simpset does not contain this definition, but separate |
|
162 |
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
163 |
||
5183 | 164 |
primrec |
15307 | 165 |
take_Nil:"take n [] = []" |
166 |
take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)" |
|
167 |
-- {*Warning: simpset does not contain this definition, but separate |
|
168 |
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
169 |
||
13142 | 170 |
primrec |
15307 | 171 |
nth_Cons:"(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)" |
172 |
-- {*Warning: simpset does not contain this definition, but separate |
|
173 |
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
174 |
||
5183 | 175 |
primrec |
15307 | 176 |
"[][i:=v] = []" |
177 |
"(x#xs)[i:=v] = (case i of 0 => v # xs | Suc j => x # xs[j:=v])" |
|
178 |
||
179 |
primrec |
|
180 |
"takeWhile P [] = []" |
|
181 |
"takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" |
|
182 |
||
5183 | 183 |
primrec |
15307 | 184 |
"dropWhile P [] = []" |
185 |
"dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" |
|
186 |
||
5183 | 187 |
primrec |
15307 | 188 |
"zip xs [] = []" |
189 |
zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)" |
|
190 |
-- {*Warning: simpset does not contain this definition, but separate |
|
191 |
theorems for @{text "xs = []"} and @{text "xs = z # zs"} *} |
|
192 |
||
5427 | 193 |
primrec |
15425 | 194 |
upt_0: "[i..<0] = []" |
195 |
upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])" |
|
15307 | 196 |
|
5183 | 197 |
primrec |
15307 | 198 |
"distinct [] = True" |
199 |
"distinct (x#xs) = (x ~: set xs \<and> distinct xs)" |
|
200 |
||
5183 | 201 |
primrec |
15307 | 202 |
"remdups [] = []" |
203 |
"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" |
|
204 |
||
5183 | 205 |
primrec |
15307 | 206 |
"remove1 x [] = []" |
207 |
"remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)" |
|
208 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
209 |
primrec |
15307 | 210 |
replicate_0: "replicate 0 x = []" |
211 |
replicate_Suc: "replicate (Suc n) x = x # replicate n x" |
|
212 |
||
8115 | 213 |
defs |
15302 | 214 |
rotate1_def: "rotate1 xs == (case xs of [] \<Rightarrow> [] | x#xs \<Rightarrow> xs @ [x])" |
215 |
rotate_def: "rotate n == rotate1 ^ n" |
|
216 |
||
217 |
list_all2_def: |
|
218 |
"list_all2 P xs ys == |
|
219 |
length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y)" |
|
220 |
||
221 |
sublist_def: |
|
15425 | 222 |
"sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..<size xs]))" |
5281 | 223 |
|
17086 | 224 |
primrec |
225 |
"x mem [] = False" |
|
226 |
"x mem (y#ys) = (if y=x then True else x mem ys)" |
|
227 |
||
228 |
primrec |
|
229 |
"list_inter [] bs = []" |
|
230 |
"list_inter (a#as) bs = |
|
231 |
(if a \<in> set bs then a#(list_inter as bs) else list_inter as bs)" |
|
232 |
||
233 |
primrec |
|
234 |
"list_all P [] = True" |
|
235 |
"list_all P (x#xs) = (P(x) \<and> list_all P xs)" |
|
236 |
||
237 |
primrec |
|
238 |
"list_ex P [] = False" |
|
239 |
"list_ex P (x#xs) = (P x \<or> list_ex P xs)" |
|
240 |
||
241 |
primrec |
|
242 |
"filtermap f [] = []" |
|
243 |
"filtermap f (x#xs) = |
|
244 |
(case f x of None \<Rightarrow> filtermap f xs |
|
245 |
| Some y \<Rightarrow> y # (filtermap f xs))" |
|
246 |
||
247 |
primrec |
|
248 |
"map_filter f P [] = []" |
|
249 |
"map_filter f P (x#xs) = (if P x then f x # map_filter f P xs else |
|
250 |
map_filter f P xs)" |
|
251 |
||
252 |
primrec |
|
253 |
"itrev [] ys = ys" |
|
254 |
"itrev (x#xs) ys = itrev xs (x#ys)" |
|
255 |
||
13114 | 256 |
|
13142 | 257 |
lemma not_Cons_self [simp]: "xs \<noteq> x # xs" |
13145 | 258 |
by (induct xs) auto |
13114 | 259 |
|
13142 | 260 |
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric] |
13114 | 261 |
|
13142 | 262 |
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)" |
13145 | 263 |
by (induct xs) auto |
13114 | 264 |
|
13142 | 265 |
lemma length_induct: |
13145 | 266 |
"(!!xs. \<forall>ys. length ys < length xs --> P ys ==> P xs) ==> P xs" |
267 |
by (rule measure_induct [of length]) rules |
|
13114 | 268 |
|
269 |
||
15392 | 270 |
subsubsection {* @{text length} *} |
13114 | 271 |
|
13142 | 272 |
text {* |
13145 | 273 |
Needs to come before @{text "@"} because of theorem @{text |
274 |
append_eq_append_conv}. |
|
13142 | 275 |
*} |
13114 | 276 |
|
13142 | 277 |
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" |
13145 | 278 |
by (induct xs) auto |
13114 | 279 |
|
13142 | 280 |
lemma length_map [simp]: "length (map f xs) = length xs" |
13145 | 281 |
by (induct xs) auto |
13114 | 282 |
|
13142 | 283 |
lemma length_rev [simp]: "length (rev xs) = length xs" |
13145 | 284 |
by (induct xs) auto |
13114 | 285 |
|
13142 | 286 |
lemma length_tl [simp]: "length (tl xs) = length xs - 1" |
13145 | 287 |
by (cases xs) auto |
13114 | 288 |
|
13142 | 289 |
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" |
13145 | 290 |
by (induct xs) auto |
13114 | 291 |
|
13142 | 292 |
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" |
13145 | 293 |
by (induct xs) auto |
13114 | 294 |
|
295 |
lemma length_Suc_conv: |
|
13145 | 296 |
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
297 |
by (induct xs) auto |
|
13142 | 298 |
|
14025 | 299 |
lemma Suc_length_conv: |
300 |
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
|
14208 | 301 |
apply (induct xs, simp, simp) |
14025 | 302 |
apply blast |
303 |
done |
|
304 |
||
14099 | 305 |
lemma impossible_Cons [rule_format]: |
306 |
"length xs <= length ys --> xs = x # ys = False" |
|
14208 | 307 |
apply (induct xs, auto) |
14099 | 308 |
done |
309 |
||
14247 | 310 |
lemma list_induct2[consumes 1]: "\<And>ys. |
311 |
\<lbrakk> length xs = length ys; |
|
312 |
P [] []; |
|
313 |
\<And>x xs y ys. \<lbrakk> length xs = length ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk> |
|
314 |
\<Longrightarrow> P xs ys" |
|
315 |
apply(induct xs) |
|
316 |
apply simp |
|
317 |
apply(case_tac ys) |
|
318 |
apply simp |
|
319 |
apply(simp) |
|
320 |
done |
|
13114 | 321 |
|
15392 | 322 |
subsubsection {* @{text "@"} -- append *} |
13114 | 323 |
|
13142 | 324 |
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" |
13145 | 325 |
by (induct xs) auto |
13114 | 326 |
|
13142 | 327 |
lemma append_Nil2 [simp]: "xs @ [] = xs" |
13145 | 328 |
by (induct xs) auto |
3507 | 329 |
|
13142 | 330 |
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" |
13145 | 331 |
by (induct xs) auto |
13114 | 332 |
|
13142 | 333 |
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])" |
13145 | 334 |
by (induct xs) auto |
13114 | 335 |
|
13142 | 336 |
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" |
13145 | 337 |
by (induct xs) auto |
13114 | 338 |
|
13142 | 339 |
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])" |
13145 | 340 |
by (induct xs) auto |
13114 | 341 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
342 |
lemma append_eq_append_conv [simp]: |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
343 |
"!!ys. length xs = length ys \<or> length us = length vs |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
344 |
==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)" |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
345 |
apply (induct xs) |
14208 | 346 |
apply (case_tac ys, simp, force) |
347 |
apply (case_tac ys, force, simp) |
|
13145 | 348 |
done |
13142 | 349 |
|
14495 | 350 |
lemma append_eq_append_conv2: "!!ys zs ts. |
351 |
(xs @ ys = zs @ ts) = |
|
352 |
(EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)" |
|
353 |
apply (induct xs) |
|
354 |
apply fastsimp |
|
355 |
apply(case_tac zs) |
|
356 |
apply simp |
|
357 |
apply fastsimp |
|
358 |
done |
|
359 |
||
13142 | 360 |
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)" |
13145 | 361 |
by simp |
13142 | 362 |
|
363 |
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)" |
|
13145 | 364 |
by simp |
13114 | 365 |
|
13142 | 366 |
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)" |
13145 | 367 |
by simp |
13114 | 368 |
|
13142 | 369 |
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" |
13145 | 370 |
using append_same_eq [of _ _ "[]"] by auto |
3507 | 371 |
|
13142 | 372 |
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])" |
13145 | 373 |
using append_same_eq [of "[]"] by auto |
13114 | 374 |
|
13142 | 375 |
lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs" |
13145 | 376 |
by (induct xs) auto |
13114 | 377 |
|
13142 | 378 |
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" |
13145 | 379 |
by (induct xs) auto |
13114 | 380 |
|
13142 | 381 |
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs" |
13145 | 382 |
by (simp add: hd_append split: list.split) |
13114 | 383 |
|
13142 | 384 |
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)" |
13145 | 385 |
by (simp split: list.split) |
13114 | 386 |
|
13142 | 387 |
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys" |
13145 | 388 |
by (simp add: tl_append split: list.split) |
13114 | 389 |
|
390 |
||
14300 | 391 |
lemma Cons_eq_append_conv: "x#xs = ys@zs = |
392 |
(ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))" |
|
393 |
by(cases ys) auto |
|
394 |
||
15281 | 395 |
lemma append_eq_Cons_conv: "(ys@zs = x#xs) = |
396 |
(ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))" |
|
397 |
by(cases ys) auto |
|
398 |
||
14300 | 399 |
|
13142 | 400 |
text {* Trivial rules for solving @{text "@"}-equations automatically. *} |
13114 | 401 |
|
402 |
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys" |
|
13145 | 403 |
by simp |
13114 | 404 |
|
13142 | 405 |
lemma Cons_eq_appendI: |
13145 | 406 |
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs" |
407 |
by (drule sym) simp |
|
13114 | 408 |
|
13142 | 409 |
lemma append_eq_appendI: |
13145 | 410 |
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us" |
411 |
by (drule sym) simp |
|
13114 | 412 |
|
413 |
||
13142 | 414 |
text {* |
13145 | 415 |
Simplification procedure for all list equalities. |
416 |
Currently only tries to rearrange @{text "@"} to see if |
|
417 |
- both lists end in a singleton list, |
|
418 |
- or both lists end in the same list. |
|
13142 | 419 |
*} |
420 |
||
421 |
ML_setup {* |
|
3507 | 422 |
local |
423 |
||
13122 | 424 |
val append_assoc = thm "append_assoc"; |
425 |
val append_Nil = thm "append_Nil"; |
|
426 |
val append_Cons = thm "append_Cons"; |
|
427 |
val append1_eq_conv = thm "append1_eq_conv"; |
|
428 |
val append_same_eq = thm "append_same_eq"; |
|
429 |
||
13114 | 430 |
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) = |
13462 | 431 |
(case xs of Const("List.list.Nil",_) => cons | _ => last xs) |
432 |
| last (Const("List.op @",_) $ _ $ ys) = last ys |
|
433 |
| last t = t; |
|
13114 | 434 |
|
435 |
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true |
|
13462 | 436 |
| list1 _ = false; |
13114 | 437 |
|
438 |
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) = |
|
13462 | 439 |
(case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs) |
440 |
| butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys |
|
441 |
| butlast xs = Const("List.list.Nil",fastype_of xs); |
|
13114 | 442 |
|
16973 | 443 |
val rearr_ss = HOL_basic_ss addsimps [append_assoc, append_Nil, append_Cons]; |
444 |
||
445 |
fun list_eq sg ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) = |
|
13462 | 446 |
let |
447 |
val lastl = last lhs and lastr = last rhs; |
|
448 |
fun rearr conv = |
|
449 |
let |
|
450 |
val lhs1 = butlast lhs and rhs1 = butlast rhs; |
|
451 |
val Type(_,listT::_) = eqT |
|
452 |
val appT = [listT,listT] ---> listT |
|
453 |
val app = Const("List.op @",appT) |
|
454 |
val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr) |
|
13480
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
455 |
val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2)); |
16973 | 456 |
val thm = Tactic.prove sg [] [] eq |
457 |
(K (simp_tac (Simplifier.inherit_bounds ss rearr_ss) 1)); |
|
15531 | 458 |
in SOME ((conv RS (thm RS trans)) RS eq_reflection) end; |
13114 | 459 |
|
13462 | 460 |
in |
461 |
if list1 lastl andalso list1 lastr then rearr append1_eq_conv |
|
462 |
else if lastl aconv lastr then rearr append_same_eq |
|
15531 | 463 |
else NONE |
13462 | 464 |
end; |
465 |
||
13114 | 466 |
in |
13462 | 467 |
|
468 |
val list_eq_simproc = |
|
469 |
Simplifier.simproc (Theory.sign_of (the_context ())) "list_eq" ["(xs::'a list) = ys"] list_eq; |
|
470 |
||
13114 | 471 |
end; |
472 |
||
473 |
Addsimprocs [list_eq_simproc]; |
|
474 |
*} |
|
475 |
||
476 |
||
15392 | 477 |
subsubsection {* @{text map} *} |
13114 | 478 |
|
13142 | 479 |
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs" |
13145 | 480 |
by (induct xs) simp_all |
13114 | 481 |
|
13142 | 482 |
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)" |
13145 | 483 |
by (rule ext, induct_tac xs) auto |
13114 | 484 |
|
13142 | 485 |
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" |
13145 | 486 |
by (induct xs) auto |
13114 | 487 |
|
13142 | 488 |
lemma map_compose: "map (f o g) xs = map f (map g xs)" |
13145 | 489 |
by (induct xs) (auto simp add: o_def) |
13114 | 490 |
|
13142 | 491 |
lemma rev_map: "rev (map f xs) = map f (rev xs)" |
13145 | 492 |
by (induct xs) auto |
13114 | 493 |
|
13737 | 494 |
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)" |
495 |
by (induct xs) auto |
|
496 |
||
13366 | 497 |
lemma map_cong [recdef_cong]: |
13145 | 498 |
"xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys" |
499 |
-- {* a congruence rule for @{text map} *} |
|
13737 | 500 |
by simp |
13114 | 501 |
|
13142 | 502 |
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])" |
13145 | 503 |
by (cases xs) auto |
13114 | 504 |
|
13142 | 505 |
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])" |
13145 | 506 |
by (cases xs) auto |
13114 | 507 |
|
14025 | 508 |
lemma map_eq_Cons_conv[iff]: |
509 |
"(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" |
|
13145 | 510 |
by (cases xs) auto |
13114 | 511 |
|
14025 | 512 |
lemma Cons_eq_map_conv[iff]: |
513 |
"(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)" |
|
514 |
by (cases ys) auto |
|
515 |
||
14111 | 516 |
lemma ex_map_conv: |
517 |
"(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)" |
|
518 |
by(induct ys, auto) |
|
519 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
520 |
lemma map_eq_imp_length_eq: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
521 |
"!!xs. map f xs = map f ys ==> length xs = length ys" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
522 |
apply (induct ys) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
523 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
524 |
apply(simp (no_asm_use)) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
525 |
apply clarify |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
526 |
apply(simp (no_asm_use)) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
527 |
apply fast |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
528 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
529 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
530 |
lemma map_inj_on: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
531 |
"[| map f xs = map f ys; inj_on f (set xs Un set ys) |] |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
532 |
==> xs = ys" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
533 |
apply(frule map_eq_imp_length_eq) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
534 |
apply(rotate_tac -1) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
535 |
apply(induct rule:list_induct2) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
536 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
537 |
apply(simp) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
538 |
apply (blast intro:sym) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
539 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
540 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
541 |
lemma inj_on_map_eq_map: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
542 |
"inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
543 |
by(blast dest:map_inj_on) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
544 |
|
13114 | 545 |
lemma map_injective: |
14338 | 546 |
"!!xs. map f xs = map f ys ==> inj f ==> xs = ys" |
547 |
by (induct ys) (auto dest!:injD) |
|
13114 | 548 |
|
14339 | 549 |
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" |
550 |
by(blast dest:map_injective) |
|
551 |
||
13114 | 552 |
lemma inj_mapI: "inj f ==> inj (map f)" |
13585 | 553 |
by (rules dest: map_injective injD intro: inj_onI) |
13114 | 554 |
|
555 |
lemma inj_mapD: "inj (map f) ==> inj f" |
|
14208 | 556 |
apply (unfold inj_on_def, clarify) |
13145 | 557 |
apply (erule_tac x = "[x]" in ballE) |
14208 | 558 |
apply (erule_tac x = "[y]" in ballE, simp, blast) |
13145 | 559 |
apply blast |
560 |
done |
|
13114 | 561 |
|
14339 | 562 |
lemma inj_map[iff]: "inj (map f) = inj f" |
13145 | 563 |
by (blast dest: inj_mapD intro: inj_mapI) |
13114 | 564 |
|
15303 | 565 |
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A" |
566 |
apply(rule inj_onI) |
|
567 |
apply(erule map_inj_on) |
|
568 |
apply(blast intro:inj_onI dest:inj_onD) |
|
569 |
done |
|
570 |
||
14343 | 571 |
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs" |
572 |
by (induct xs, auto) |
|
13114 | 573 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
574 |
lemma map_fun_upd [simp]: "y \<notin> set xs \<Longrightarrow> map (f(y:=v)) xs = map f xs" |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
575 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
576 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
577 |
lemma map_fst_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
578 |
"length xs = length ys \<Longrightarrow> map fst (zip xs ys) = xs" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
579 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
580 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
581 |
lemma map_snd_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
582 |
"length xs = length ys \<Longrightarrow> map snd (zip xs ys) = ys" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
583 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
584 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
585 |
|
15392 | 586 |
subsubsection {* @{text rev} *} |
13114 | 587 |
|
13142 | 588 |
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" |
13145 | 589 |
by (induct xs) auto |
13114 | 590 |
|
13142 | 591 |
lemma rev_rev_ident [simp]: "rev (rev xs) = xs" |
13145 | 592 |
by (induct xs) auto |
13114 | 593 |
|
15870 | 594 |
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)" |
595 |
by auto |
|
596 |
||
13142 | 597 |
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])" |
13145 | 598 |
by (induct xs) auto |
13114 | 599 |
|
13142 | 600 |
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])" |
13145 | 601 |
by (induct xs) auto |
13114 | 602 |
|
15870 | 603 |
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])" |
604 |
by (cases xs) auto |
|
605 |
||
606 |
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])" |
|
607 |
by (cases xs) auto |
|
608 |
||
13142 | 609 |
lemma rev_is_rev_conv [iff]: "!!ys. (rev xs = rev ys) = (xs = ys)" |
14208 | 610 |
apply (induct xs, force) |
611 |
apply (case_tac ys, simp, force) |
|
13145 | 612 |
done |
13114 | 613 |
|
15439 | 614 |
lemma inj_on_rev[iff]: "inj_on rev A" |
615 |
by(simp add:inj_on_def) |
|
616 |
||
13366 | 617 |
lemma rev_induct [case_names Nil snoc]: |
618 |
"[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs" |
|
15489
d136af442665
Replaced application of subst by simplesubst in proof of rev_induct
berghofe
parents:
15439
diff
changeset
|
619 |
apply(simplesubst rev_rev_ident[symmetric]) |
13145 | 620 |
apply(rule_tac list = "rev xs" in list.induct, simp_all) |
621 |
done |
|
13114 | 622 |
|
13145 | 623 |
ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}-- "compatibility" |
13114 | 624 |
|
13366 | 625 |
lemma rev_exhaust [case_names Nil snoc]: |
626 |
"(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P" |
|
13145 | 627 |
by (induct xs rule: rev_induct) auto |
13114 | 628 |
|
13366 | 629 |
lemmas rev_cases = rev_exhaust |
630 |
||
13114 | 631 |
|
15392 | 632 |
subsubsection {* @{text set} *} |
13114 | 633 |
|
13142 | 634 |
lemma finite_set [iff]: "finite (set xs)" |
13145 | 635 |
by (induct xs) auto |
13114 | 636 |
|
13142 | 637 |
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)" |
13145 | 638 |
by (induct xs) auto |
13114 | 639 |
|
14099 | 640 |
lemma hd_in_set: "l = x#xs \<Longrightarrow> x\<in>set l" |
14208 | 641 |
by (case_tac l, auto) |
14099 | 642 |
|
13142 | 643 |
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" |
13145 | 644 |
by auto |
13114 | 645 |
|
14099 | 646 |
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" |
647 |
by auto |
|
648 |
||
13142 | 649 |
lemma set_empty [iff]: "(set xs = {}) = (xs = [])" |
13145 | 650 |
by (induct xs) auto |
13114 | 651 |
|
15245 | 652 |
lemma set_empty2[iff]: "({} = set xs) = (xs = [])" |
653 |
by(induct xs) auto |
|
654 |
||
13142 | 655 |
lemma set_rev [simp]: "set (rev xs) = set xs" |
13145 | 656 |
by (induct xs) auto |
13114 | 657 |
|
13142 | 658 |
lemma set_map [simp]: "set (map f xs) = f`(set xs)" |
13145 | 659 |
by (induct xs) auto |
13114 | 660 |
|
13142 | 661 |
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}" |
13145 | 662 |
by (induct xs) auto |
13114 | 663 |
|
15425 | 664 |
lemma set_upt [simp]: "set[i..<j] = {k. i \<le> k \<and> k < j}" |
14208 | 665 |
apply (induct j, simp_all) |
666 |
apply (erule ssubst, auto) |
|
13145 | 667 |
done |
13114 | 668 |
|
13142 | 669 |
lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)" |
15113 | 670 |
proof (induct xs) |
671 |
case Nil show ?case by simp |
|
672 |
case (Cons a xs) |
|
673 |
show ?case |
|
674 |
proof |
|
675 |
assume "x \<in> set (a # xs)" |
|
676 |
with prems show "\<exists>ys zs. a # xs = ys @ x # zs" |
|
677 |
by (simp, blast intro: Cons_eq_appendI) |
|
678 |
next |
|
679 |
assume "\<exists>ys zs. a # xs = ys @ x # zs" |
|
680 |
then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast |
|
681 |
show "x \<in> set (a # xs)" |
|
682 |
by (cases ys, auto simp add: eq) |
|
683 |
qed |
|
684 |
qed |
|
13142 | 685 |
|
13508 | 686 |
lemma finite_list: "finite A ==> EX l. set l = A" |
687 |
apply (erule finite_induct, auto) |
|
688 |
apply (rule_tac x="x#l" in exI, auto) |
|
689 |
done |
|
690 |
||
14388 | 691 |
lemma card_length: "card (set xs) \<le> length xs" |
692 |
by (induct xs) (auto simp add: card_insert_if) |
|
13114 | 693 |
|
15168 | 694 |
|
15392 | 695 |
subsubsection {* @{text filter} *} |
13114 | 696 |
|
13142 | 697 |
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" |
13145 | 698 |
by (induct xs) auto |
13114 | 699 |
|
15305 | 700 |
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)" |
701 |
by (induct xs) simp_all |
|
702 |
||
13142 | 703 |
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs" |
13145 | 704 |
by (induct xs) auto |
13114 | 705 |
|
16998 | 706 |
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs" |
707 |
by (induct xs) (auto simp add: le_SucI) |
|
708 |
||
13142 | 709 |
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs" |
13145 | 710 |
by (induct xs) auto |
13114 | 711 |
|
13142 | 712 |
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []" |
13145 | 713 |
by (induct xs) auto |
13114 | 714 |
|
16998 | 715 |
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" |
716 |
by (induct xs) simp_all |
|
717 |
||
718 |
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)" |
|
719 |
apply (induct xs) |
|
720 |
apply auto |
|
721 |
apply(cut_tac P=P and xs=xs in length_filter_le) |
|
722 |
apply simp |
|
723 |
done |
|
13114 | 724 |
|
16965 | 725 |
lemma filter_map: |
726 |
"filter P (map f xs) = map f (filter (P o f) xs)" |
|
727 |
by (induct xs) simp_all |
|
728 |
||
729 |
lemma length_filter_map[simp]: |
|
730 |
"length (filter P (map f xs)) = length(filter (P o f) xs)" |
|
731 |
by (simp add:filter_map) |
|
732 |
||
13142 | 733 |
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" |
13145 | 734 |
by auto |
13114 | 735 |
|
15246 | 736 |
lemma length_filter_less: |
737 |
"\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs" |
|
738 |
proof (induct xs) |
|
739 |
case Nil thus ?case by simp |
|
740 |
next |
|
741 |
case (Cons x xs) thus ?case |
|
742 |
apply (auto split:split_if_asm) |
|
743 |
using length_filter_le[of P xs] apply arith |
|
744 |
done |
|
745 |
qed |
|
13114 | 746 |
|
15281 | 747 |
lemma length_filter_conv_card: |
748 |
"length(filter p xs) = card{i. i < length xs & p(xs!i)}" |
|
749 |
proof (induct xs) |
|
750 |
case Nil thus ?case by simp |
|
751 |
next |
|
752 |
case (Cons x xs) |
|
753 |
let ?S = "{i. i < length xs & p(xs!i)}" |
|
754 |
have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite) |
|
755 |
show ?case (is "?l = card ?S'") |
|
756 |
proof (cases) |
|
757 |
assume "p x" |
|
758 |
hence eq: "?S' = insert 0 (Suc ` ?S)" |
|
759 |
by(auto simp add: nth_Cons image_def split:nat.split elim:lessE) |
|
760 |
have "length (filter p (x # xs)) = Suc(card ?S)" |
|
761 |
using Cons by simp |
|
762 |
also have "\<dots> = Suc(card(Suc ` ?S))" using fin |
|
763 |
by (simp add: card_image inj_Suc) |
|
764 |
also have "\<dots> = card ?S'" using eq fin |
|
765 |
by (simp add:card_insert_if) (simp add:image_def) |
|
766 |
finally show ?thesis . |
|
767 |
next |
|
768 |
assume "\<not> p x" |
|
769 |
hence eq: "?S' = Suc ` ?S" |
|
770 |
by(auto simp add: nth_Cons image_def split:nat.split elim:lessE) |
|
771 |
have "length (filter p (x # xs)) = card ?S" |
|
772 |
using Cons by simp |
|
773 |
also have "\<dots> = card(Suc ` ?S)" using fin |
|
774 |
by (simp add: card_image inj_Suc) |
|
775 |
also have "\<dots> = card ?S'" using eq fin |
|
776 |
by (simp add:card_insert_if) |
|
777 |
finally show ?thesis . |
|
778 |
qed |
|
779 |
qed |
|
780 |
||
17501 | 781 |
lemma filter_cong: |
782 |
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys" |
|
783 |
apply simp |
|
784 |
apply(erule thin_rl) |
|
785 |
by (induct ys) simp_all |
|
786 |
||
15281 | 787 |
|
15392 | 788 |
subsubsection {* @{text concat} *} |
13114 | 789 |
|
13142 | 790 |
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" |
13145 | 791 |
by (induct xs) auto |
13114 | 792 |
|
13142 | 793 |
lemma concat_eq_Nil_conv [iff]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])" |
13145 | 794 |
by (induct xss) auto |
13114 | 795 |
|
13142 | 796 |
lemma Nil_eq_concat_conv [iff]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])" |
13145 | 797 |
by (induct xss) auto |
13114 | 798 |
|
13142 | 799 |
lemma set_concat [simp]: "set (concat xs) = \<Union>(set ` set xs)" |
13145 | 800 |
by (induct xs) auto |
13114 | 801 |
|
13142 | 802 |
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" |
13145 | 803 |
by (induct xs) auto |
13114 | 804 |
|
13142 | 805 |
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" |
13145 | 806 |
by (induct xs) auto |
13114 | 807 |
|
13142 | 808 |
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" |
13145 | 809 |
by (induct xs) auto |
13114 | 810 |
|
811 |
||
15392 | 812 |
subsubsection {* @{text nth} *} |
13114 | 813 |
|
13142 | 814 |
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x" |
13145 | 815 |
by auto |
13114 | 816 |
|
13142 | 817 |
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n" |
13145 | 818 |
by auto |
13114 | 819 |
|
13142 | 820 |
declare nth.simps [simp del] |
13114 | 821 |
|
822 |
lemma nth_append: |
|
13145 | 823 |
"!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))" |
14208 | 824 |
apply (induct "xs", simp) |
825 |
apply (case_tac n, auto) |
|
13145 | 826 |
done |
13114 | 827 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
828 |
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x" |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
829 |
by (induct "xs") auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
830 |
|
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
831 |
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n" |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
832 |
by (induct "xs") auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
833 |
|
13142 | 834 |
lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)" |
14208 | 835 |
apply (induct xs, simp) |
836 |
apply (case_tac n, auto) |
|
13145 | 837 |
done |
13114 | 838 |
|
13142 | 839 |
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}" |
15251 | 840 |
apply (induct xs, simp, simp) |
13145 | 841 |
apply safe |
14208 | 842 |
apply (rule_tac x = 0 in exI, simp) |
843 |
apply (rule_tac x = "Suc i" in exI, simp) |
|
844 |
apply (case_tac i, simp) |
|
13145 | 845 |
apply (rename_tac j) |
14208 | 846 |
apply (rule_tac x = j in exI, simp) |
13145 | 847 |
done |
13114 | 848 |
|
17501 | 849 |
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)" |
850 |
by(auto simp:set_conv_nth) |
|
851 |
||
13145 | 852 |
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)" |
853 |
by (auto simp add: set_conv_nth) |
|
13114 | 854 |
|
13142 | 855 |
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs" |
13145 | 856 |
by (auto simp add: set_conv_nth) |
13114 | 857 |
|
858 |
lemma all_nth_imp_all_set: |
|
13145 | 859 |
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x" |
860 |
by (auto simp add: set_conv_nth) |
|
13114 | 861 |
|
862 |
lemma all_set_conv_all_nth: |
|
13145 | 863 |
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))" |
864 |
by (auto simp add: set_conv_nth) |
|
13114 | 865 |
|
866 |
||
15392 | 867 |
subsubsection {* @{text list_update} *} |
13114 | 868 |
|
13142 | 869 |
lemma length_list_update [simp]: "!!i. length(xs[i:=x]) = length xs" |
13145 | 870 |
by (induct xs) (auto split: nat.split) |
13114 | 871 |
|
872 |
lemma nth_list_update: |
|
13145 | 873 |
"!!i j. i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)" |
874 |
by (induct xs) (auto simp add: nth_Cons split: nat.split) |
|
13114 | 875 |
|
13142 | 876 |
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x" |
13145 | 877 |
by (simp add: nth_list_update) |
13114 | 878 |
|
13142 | 879 |
lemma nth_list_update_neq [simp]: "!!i j. i \<noteq> j ==> xs[i:=x]!j = xs!j" |
13145 | 880 |
by (induct xs) (auto simp add: nth_Cons split: nat.split) |
13114 | 881 |
|
13142 | 882 |
lemma list_update_overwrite [simp]: |
13145 | 883 |
"!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]" |
884 |
by (induct xs) (auto split: nat.split) |
|
13114 | 885 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
886 |
lemma list_update_id[simp]: "!!i. i < length xs ==> xs[i := xs!i] = xs" |
14208 | 887 |
apply (induct xs, simp) |
14187 | 888 |
apply(simp split:nat.splits) |
889 |
done |
|
890 |
||
17501 | 891 |
lemma list_update_beyond[simp]: "\<And>i. length xs \<le> i \<Longrightarrow> xs[i:=x] = xs" |
892 |
apply (induct xs) |
|
893 |
apply simp |
|
894 |
apply (case_tac i) |
|
895 |
apply simp_all |
|
896 |
done |
|
897 |
||
13114 | 898 |
lemma list_update_same_conv: |
13145 | 899 |
"!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)" |
900 |
by (induct xs) (auto split: nat.split) |
|
13114 | 901 |
|
14187 | 902 |
lemma list_update_append1: |
903 |
"!!i. i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys" |
|
14208 | 904 |
apply (induct xs, simp) |
14187 | 905 |
apply(simp split:nat.split) |
906 |
done |
|
907 |
||
15868 | 908 |
lemma list_update_append: |
909 |
"!!n. (xs @ ys) [n:= x] = |
|
910 |
(if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))" |
|
911 |
by (induct xs) (auto split:nat.splits) |
|
912 |
||
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
913 |
lemma list_update_length [simp]: |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
914 |
"(xs @ x # ys)[length xs := y] = (xs @ y # ys)" |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
915 |
by (induct xs, auto) |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
916 |
|
13114 | 917 |
lemma update_zip: |
13145 | 918 |
"!!i xy xs. length xs = length ys ==> |
919 |
(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" |
|
920 |
by (induct ys) (auto, case_tac xs, auto split: nat.split) |
|
13114 | 921 |
|
922 |
lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)" |
|
13145 | 923 |
by (induct xs) (auto split: nat.split) |
13114 | 924 |
|
925 |
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A" |
|
13145 | 926 |
by (blast dest!: set_update_subset_insert [THEN subsetD]) |
13114 | 927 |
|
15868 | 928 |
lemma set_update_memI: "!!n. n < length xs \<Longrightarrow> x \<in> set (xs[n := x])" |
929 |
by (induct xs) (auto split:nat.splits) |
|
930 |
||
13114 | 931 |
|
15392 | 932 |
subsubsection {* @{text last} and @{text butlast} *} |
13114 | 933 |
|
13142 | 934 |
lemma last_snoc [simp]: "last (xs @ [x]) = x" |
13145 | 935 |
by (induct xs) auto |
13114 | 936 |
|
13142 | 937 |
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" |
13145 | 938 |
by (induct xs) auto |
13114 | 939 |
|
14302 | 940 |
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x" |
941 |
by(simp add:last.simps) |
|
942 |
||
943 |
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs" |
|
944 |
by(simp add:last.simps) |
|
945 |
||
946 |
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)" |
|
947 |
by (induct xs) (auto) |
|
948 |
||
949 |
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs" |
|
950 |
by(simp add:last_append) |
|
951 |
||
952 |
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys" |
|
953 |
by(simp add:last_append) |
|
954 |
||
955 |
||
13142 | 956 |
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1" |
13145 | 957 |
by (induct xs rule: rev_induct) auto |
13114 | 958 |
|
959 |
lemma butlast_append: |
|
13145 | 960 |
"!!ys. butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)" |
961 |
by (induct xs) auto |
|
13114 | 962 |
|
13142 | 963 |
lemma append_butlast_last_id [simp]: |
13145 | 964 |
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs" |
965 |
by (induct xs) auto |
|
13114 | 966 |
|
13142 | 967 |
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs" |
13145 | 968 |
by (induct xs) (auto split: split_if_asm) |
13114 | 969 |
|
970 |
lemma in_set_butlast_appendI: |
|
13145 | 971 |
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))" |
972 |
by (auto dest: in_set_butlastD simp add: butlast_append) |
|
13114 | 973 |
|
17501 | 974 |
lemma last_drop[simp]: "!!n. n < length xs \<Longrightarrow> last (drop n xs) = last xs" |
975 |
apply (induct xs) |
|
976 |
apply simp |
|
977 |
apply (auto split:nat.split) |
|
978 |
done |
|
979 |
||
13142 | 980 |
|
15392 | 981 |
subsubsection {* @{text take} and @{text drop} *} |
13114 | 982 |
|
13142 | 983 |
lemma take_0 [simp]: "take 0 xs = []" |
13145 | 984 |
by (induct xs) auto |
13114 | 985 |
|
13142 | 986 |
lemma drop_0 [simp]: "drop 0 xs = xs" |
13145 | 987 |
by (induct xs) auto |
13114 | 988 |
|
13142 | 989 |
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" |
13145 | 990 |
by simp |
13114 | 991 |
|
13142 | 992 |
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" |
13145 | 993 |
by simp |
13114 | 994 |
|
13142 | 995 |
declare take_Cons [simp del] and drop_Cons [simp del] |
13114 | 996 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
997 |
lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
998 |
by(clarsimp simp add:neq_Nil_conv) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
999 |
|
14187 | 1000 |
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)" |
1001 |
by(cases xs, simp_all) |
|
1002 |
||
1003 |
lemma drop_tl: "!!n. drop n (tl xs) = tl(drop n xs)" |
|
1004 |
by(induct xs, simp_all add:drop_Cons drop_Suc split:nat.split) |
|
1005 |
||
1006 |
lemma nth_via_drop: "!!n. drop n xs = y#ys \<Longrightarrow> xs!n = y" |
|
14208 | 1007 |
apply (induct xs, simp) |
14187 | 1008 |
apply(simp add:drop_Cons nth_Cons split:nat.splits) |
1009 |
done |
|
1010 |
||
13913 | 1011 |
lemma take_Suc_conv_app_nth: |
1012 |
"!!i. i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]" |
|
14208 | 1013 |
apply (induct xs, simp) |
1014 |
apply (case_tac i, auto) |
|
13913 | 1015 |
done |
1016 |
||
14591 | 1017 |
lemma drop_Suc_conv_tl: |
1018 |
"!!i. i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs" |
|
1019 |
apply (induct xs, simp) |
|
1020 |
apply (case_tac i, auto) |
|
1021 |
done |
|
1022 |
||
13142 | 1023 |
lemma length_take [simp]: "!!xs. length (take n xs) = min (length xs) n" |
13145 | 1024 |
by (induct n) (auto, case_tac xs, auto) |
13114 | 1025 |
|
13142 | 1026 |
lemma length_drop [simp]: "!!xs. length (drop n xs) = (length xs - n)" |
13145 | 1027 |
by (induct n) (auto, case_tac xs, auto) |
13114 | 1028 |
|
13142 | 1029 |
lemma take_all [simp]: "!!xs. length xs <= n ==> take n xs = xs" |
13145 | 1030 |
by (induct n) (auto, case_tac xs, auto) |
13114 | 1031 |
|
13142 | 1032 |
lemma drop_all [simp]: "!!xs. length xs <= n ==> drop n xs = []" |
13145 | 1033 |
by (induct n) (auto, case_tac xs, auto) |
13114 | 1034 |
|
13142 | 1035 |
lemma take_append [simp]: |
13145 | 1036 |
"!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)" |
1037 |
by (induct n) (auto, case_tac xs, auto) |
|
13114 | 1038 |
|
13142 | 1039 |
lemma drop_append [simp]: |
13145 | 1040 |
"!!xs. drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys" |
1041 |
by (induct n) (auto, case_tac xs, auto) |
|
13114 | 1042 |
|
13142 | 1043 |
lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs" |
14208 | 1044 |
apply (induct m, auto) |
1045 |
apply (case_tac xs, auto) |
|
15236
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
nipkow
parents:
15176
diff
changeset
|
1046 |
apply (case_tac n, auto) |
13145 | 1047 |
done |
13114 | 1048 |
|
13142 | 1049 |
lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs" |
14208 | 1050 |
apply (induct m, auto) |
1051 |
apply (case_tac xs, auto) |
|
13145 | 1052 |
done |
13114 | 1053 |
|
1054 |
lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)" |
|
14208 | 1055 |
apply (induct m, auto) |
1056 |
apply (case_tac xs, auto) |
|
13145 | 1057 |
done |
13114 | 1058 |
|
14802 | 1059 |
lemma drop_take: "!!m n. drop n (take m xs) = take (m-n) (drop n xs)" |
1060 |
apply(induct xs) |
|
1061 |
apply simp |
|
1062 |
apply(simp add: take_Cons drop_Cons split:nat.split) |
|
1063 |
done |
|
1064 |
||
13142 | 1065 |
lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs" |
14208 | 1066 |
apply (induct n, auto) |
1067 |
apply (case_tac xs, auto) |
|
13145 | 1068 |
done |
13114 | 1069 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1070 |
lemma take_eq_Nil[simp]: "!!n. (take n xs = []) = (n = 0 \<or> xs = [])" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1071 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1072 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1073 |
apply(simp add:take_Cons split:nat.split) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1074 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1075 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1076 |
lemma drop_eq_Nil[simp]: "!!n. (drop n xs = []) = (length xs <= n)" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1077 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1078 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1079 |
apply(simp add:drop_Cons split:nat.split) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1080 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1081 |
|
13114 | 1082 |
lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)" |
14208 | 1083 |
apply (induct n, auto) |
1084 |
apply (case_tac xs, auto) |
|
13145 | 1085 |
done |
13114 | 1086 |
|
13142 | 1087 |
lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)" |
14208 | 1088 |
apply (induct n, auto) |
1089 |
apply (case_tac xs, auto) |
|
13145 | 1090 |
done |
13114 | 1091 |
|
1092 |
lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)" |
|
14208 | 1093 |
apply (induct xs, auto) |
1094 |
apply (case_tac i, auto) |
|
13145 | 1095 |
done |
13114 | 1096 |
|
1097 |
lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)" |
|
14208 | 1098 |
apply (induct xs, auto) |
1099 |
apply (case_tac i, auto) |
|
13145 | 1100 |
done |
13114 | 1101 |
|
13142 | 1102 |
lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i" |
14208 | 1103 |
apply (induct xs, auto) |
1104 |
apply (case_tac n, blast) |
|
1105 |
apply (case_tac i, auto) |
|
13145 | 1106 |
done |
13114 | 1107 |
|
13142 | 1108 |
lemma nth_drop [simp]: |
13145 | 1109 |
"!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)" |
14208 | 1110 |
apply (induct n, auto) |
1111 |
apply (case_tac xs, auto) |
|
13145 | 1112 |
done |
3507 | 1113 |
|
14025 | 1114 |
lemma set_take_subset: "\<And>n. set(take n xs) \<subseteq> set xs" |
1115 |
by(induct xs)(auto simp:take_Cons split:nat.split) |
|
1116 |
||
1117 |
lemma set_drop_subset: "\<And>n. set(drop n xs) \<subseteq> set xs" |
|
1118 |
by(induct xs)(auto simp:drop_Cons split:nat.split) |
|
1119 |
||
14187 | 1120 |
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs" |
1121 |
using set_take_subset by fast |
|
1122 |
||
1123 |
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs" |
|
1124 |
using set_drop_subset by fast |
|
1125 |
||
13114 | 1126 |
lemma append_eq_conv_conj: |
13145 | 1127 |
"!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)" |
14208 | 1128 |
apply (induct xs, simp, clarsimp) |
1129 |
apply (case_tac zs, auto) |
|
13145 | 1130 |
done |
13142 | 1131 |
|
14050 | 1132 |
lemma take_add [rule_format]: |
1133 |
"\<forall>i. i+j \<le> length(xs) --> take (i+j) xs = take i xs @ take j (drop i xs)" |
|
1134 |
apply (induct xs, auto) |
|
1135 |
apply (case_tac i, simp_all) |
|
1136 |
done |
|
1137 |
||
14300 | 1138 |
lemma append_eq_append_conv_if: |
1139 |
"!! ys\<^isub>1. (xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) = |
|
1140 |
(if size xs\<^isub>1 \<le> size ys\<^isub>1 |
|
1141 |
then xs\<^isub>1 = take (size xs\<^isub>1) ys\<^isub>1 \<and> xs\<^isub>2 = drop (size xs\<^isub>1) ys\<^isub>1 @ ys\<^isub>2 |
|
1142 |
else take (size ys\<^isub>1) xs\<^isub>1 = ys\<^isub>1 \<and> drop (size ys\<^isub>1) xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>2)" |
|
1143 |
apply(induct xs\<^isub>1) |
|
1144 |
apply simp |
|
1145 |
apply(case_tac ys\<^isub>1) |
|
1146 |
apply simp_all |
|
1147 |
done |
|
1148 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1149 |
lemma take_hd_drop: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1150 |
"!!n. n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (n+1) xs" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1151 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1152 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1153 |
apply(simp add:drop_Cons split:nat.split) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1154 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1155 |
|
17501 | 1156 |
lemma id_take_nth_drop: |
1157 |
"i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" |
|
1158 |
proof - |
|
1159 |
assume si: "i < length xs" |
|
1160 |
hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto |
|
1161 |
moreover |
|
1162 |
from si have "take (Suc i) xs = take i xs @ [xs!i]" |
|
1163 |
apply (rule_tac take_Suc_conv_app_nth) by arith |
|
1164 |
ultimately show ?thesis by auto |
|
1165 |
qed |
|
1166 |
||
1167 |
lemma upd_conv_take_nth_drop: |
|
1168 |
"i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs" |
|
1169 |
proof - |
|
1170 |
assume i: "i < length xs" |
|
1171 |
have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]" |
|
1172 |
by(rule arg_cong[OF id_take_nth_drop[OF i]]) |
|
1173 |
also have "\<dots> = take i xs @ a # drop (Suc i) xs" |
|
1174 |
using i by (simp add: list_update_append) |
|
1175 |
finally show ?thesis . |
|
1176 |
qed |
|
1177 |
||
13114 | 1178 |
|
15392 | 1179 |
subsubsection {* @{text takeWhile} and @{text dropWhile} *} |
13114 | 1180 |
|
13142 | 1181 |
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs" |
13145 | 1182 |
by (induct xs) auto |
13114 | 1183 |
|
13142 | 1184 |
lemma takeWhile_append1 [simp]: |
13145 | 1185 |
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs" |
1186 |
by (induct xs) auto |
|
13114 | 1187 |
|
13142 | 1188 |
lemma takeWhile_append2 [simp]: |
13145 | 1189 |
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys" |
1190 |
by (induct xs) auto |
|
13114 | 1191 |
|
13142 | 1192 |
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs" |
13145 | 1193 |
by (induct xs) auto |
13114 | 1194 |
|
13142 | 1195 |
lemma dropWhile_append1 [simp]: |
13145 | 1196 |
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys" |
1197 |
by (induct xs) auto |
|
13114 | 1198 |
|
13142 | 1199 |
lemma dropWhile_append2 [simp]: |
13145 | 1200 |
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys" |
1201 |
by (induct xs) auto |
|
13114 | 1202 |
|
13142 | 1203 |
lemma set_take_whileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x" |
13145 | 1204 |
by (induct xs) (auto split: split_if_asm) |
13114 | 1205 |
|
13913 | 1206 |
lemma takeWhile_eq_all_conv[simp]: |
1207 |
"(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)" |
|
1208 |
by(induct xs, auto) |
|
1209 |
||
1210 |
lemma dropWhile_eq_Nil_conv[simp]: |
|
1211 |
"(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)" |
|
1212 |
by(induct xs, auto) |
|
1213 |
||
1214 |
lemma dropWhile_eq_Cons_conv: |
|
1215 |
"(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)" |
|
1216 |
by(induct xs, auto) |
|
1217 |
||
17501 | 1218 |
text{* The following two lemmmas could be generalized to an arbitrary |
1219 |
property. *} |
|
1220 |
||
1221 |
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> |
|
1222 |
takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))" |
|
1223 |
by(induct xs) (auto simp: takeWhile_tail[where l="[]"]) |
|
1224 |
||
1225 |
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> |
|
1226 |
dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)" |
|
1227 |
apply(induct xs) |
|
1228 |
apply simp |
|
1229 |
apply auto |
|
1230 |
apply(subst dropWhile_append2) |
|
1231 |
apply auto |
|
1232 |
done |
|
1233 |
||
13114 | 1234 |
|
15392 | 1235 |
subsubsection {* @{text zip} *} |
13114 | 1236 |
|
13142 | 1237 |
lemma zip_Nil [simp]: "zip [] ys = []" |
13145 | 1238 |
by (induct ys) auto |
13114 | 1239 |
|
13142 | 1240 |
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys" |
13145 | 1241 |
by simp |
13114 | 1242 |
|
13142 | 1243 |
declare zip_Cons [simp del] |
13114 | 1244 |
|
15281 | 1245 |
lemma zip_Cons1: |
1246 |
"zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)" |
|
1247 |
by(auto split:list.split) |
|
1248 |
||
13142 | 1249 |
lemma length_zip [simp]: |
13145 | 1250 |
"!!xs. length (zip xs ys) = min (length xs) (length ys)" |
14208 | 1251 |
apply (induct ys, simp) |
1252 |
apply (case_tac xs, auto) |
|
13145 | 1253 |
done |
13114 | 1254 |
|
1255 |
lemma zip_append1: |
|
13145 | 1256 |
"!!xs. zip (xs @ ys) zs = |
1257 |
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)" |
|
14208 | 1258 |
apply (induct zs, simp) |
1259 |
apply (case_tac xs, simp_all) |
|
13145 | 1260 |
done |
13114 | 1261 |
|
1262 |
lemma zip_append2: |
|
13145 | 1263 |
"!!ys. zip xs (ys @ zs) = |
1264 |
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs" |
|
14208 | 1265 |
apply (induct xs, simp) |
1266 |
apply (case_tac ys, simp_all) |
|
13145 | 1267 |
done |
13114 | 1268 |
|
13142 | 1269 |
lemma zip_append [simp]: |
1270 |
"[| length xs = length us; length ys = length vs |] ==> |
|
13145 | 1271 |
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs" |
1272 |
by (simp add: zip_append1) |
|
13114 | 1273 |
|
1274 |
lemma zip_rev: |
|
14247 | 1275 |
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)" |
1276 |
by (induct rule:list_induct2, simp_all) |
|
13114 | 1277 |
|
13142 | 1278 |
lemma nth_zip [simp]: |
13145 | 1279 |
"!!i xs. [| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)" |
14208 | 1280 |
apply (induct ys, simp) |
13145 | 1281 |
apply (case_tac xs) |
1282 |
apply (simp_all add: nth.simps split: nat.split) |
|
1283 |
done |
|
13114 | 1284 |
|
1285 |
lemma set_zip: |
|
13145 | 1286 |
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}" |
1287 |
by (simp add: set_conv_nth cong: rev_conj_cong) |
|
13114 | 1288 |
|
1289 |
lemma zip_update: |
|
13145 | 1290 |
"length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]" |
1291 |
by (rule sym, simp add: update_zip) |
|
13114 | 1292 |
|
13142 | 1293 |
lemma zip_replicate [simp]: |
13145 | 1294 |
"!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)" |
14208 | 1295 |
apply (induct i, auto) |
1296 |
apply (case_tac j, auto) |
|
13145 | 1297 |
done |
13114 | 1298 |
|
13142 | 1299 |
|
15392 | 1300 |
subsubsection {* @{text list_all2} *} |
13114 | 1301 |
|
14316
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
1302 |
lemma list_all2_lengthD [intro?]: |
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
1303 |
"list_all2 P xs ys ==> length xs = length ys" |
13145 | 1304 |
by (simp add: list_all2_def) |
13114 | 1305 |
|
17090 | 1306 |
lemma list_all2_Nil [iff,code]: "list_all2 P [] ys = (ys = [])" |
13145 | 1307 |
by (simp add: list_all2_def) |
13114 | 1308 |
|
13142 | 1309 |
lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])" |
13145 | 1310 |
by (simp add: list_all2_def) |
13114 | 1311 |
|
17090 | 1312 |
lemma list_all2_Cons [iff,code]: |
13145 | 1313 |
"list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)" |
1314 |
by (auto simp add: list_all2_def) |
|
13114 | 1315 |
|
1316 |
lemma list_all2_Cons1: |
|
13145 | 1317 |
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)" |
1318 |
by (cases ys) auto |
|
13114 | 1319 |
|
1320 |
lemma list_all2_Cons2: |
|
13145 | 1321 |
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)" |
1322 |
by (cases xs) auto |
|
13114 | 1323 |
|
13142 | 1324 |
lemma list_all2_rev [iff]: |
13145 | 1325 |
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys" |
1326 |
by (simp add: list_all2_def zip_rev cong: conj_cong) |
|
13114 | 1327 |
|
13863 | 1328 |
lemma list_all2_rev1: |
1329 |
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)" |
|
1330 |
by (subst list_all2_rev [symmetric]) simp |
|
1331 |
||
13114 | 1332 |
lemma list_all2_append1: |
13145 | 1333 |
"list_all2 P (xs @ ys) zs = |
1334 |
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and> |
|
1335 |
list_all2 P xs us \<and> list_all2 P ys vs)" |
|
1336 |
apply (simp add: list_all2_def zip_append1) |
|
1337 |
apply (rule iffI) |
|
1338 |
apply (rule_tac x = "take (length xs) zs" in exI) |
|
1339 |
apply (rule_tac x = "drop (length xs) zs" in exI) |
|
14208 | 1340 |
apply (force split: nat_diff_split simp add: min_def, clarify) |
13145 | 1341 |
apply (simp add: ball_Un) |
1342 |
done |
|
13114 | 1343 |
|
1344 |
lemma list_all2_append2: |
|
13145 | 1345 |
"list_all2 P xs (ys @ zs) = |
1346 |
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and> |
|
1347 |
list_all2 P us ys \<and> list_all2 P vs zs)" |
|
1348 |
apply (simp add: list_all2_def zip_append2) |
|
1349 |
apply (rule iffI) |
|
1350 |
apply (rule_tac x = "take (length ys) xs" in exI) |
|
1351 |
apply (rule_tac x = "drop (length ys) xs" in exI) |
|
14208 | 1352 |
apply (force split: nat_diff_split simp add: min_def, clarify) |
13145 | 1353 |
apply (simp add: ball_Un) |
1354 |
done |
|
13114 | 1 |