author  haftmann 
Tue, 25 Jul 2006 16:43:32 +0200  
changeset 20189  1be8b181dafa 
parent 20184  73b5efaf2aef 
child 20217  25b068a99d2b 
permissions  rwrr 
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 
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19623
diff
changeset

9 
imports PreList FunDef 
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" 

19390  47 
splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" 
15302  48 
sublist :: "'a list => nat set => 'a list" 
17086  49 
(* For efficiency *) 
50 
mem :: "'a => 'a list => bool" (infixl 55) 

51 
list_inter :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" 

52 
list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" 

53 
list_all:: "('a => bool) => ('a list => bool)" 

54 
itrev :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" 

55 
filtermap :: "('a \<Rightarrow> 'b option) \<Rightarrow> 'a list \<Rightarrow> 'b list" 

56 
map_filter :: "('a => 'b) => ('a => bool) => 'a list => 'b list" 

15302  57 

19363  58 
abbreviation 
59 
upto:: "nat => nat => nat list" ("(1[_../_])") 

60 
"[i..j] == [i..<(Suc j)]" 

19302  61 

923  62 

13146  63 
nonterminals lupdbinds lupdbind 
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the ith
nipkow
parents:
4643
diff
changeset

64 

923  65 
syntax 
13366  66 
 {* list Enumeration *} 
67 
"@list" :: "args => 'a list" ("[(_)]") 

923  68 

13366  69 
 {* Special syntax for filter *} 
70 
"@filter" :: "[pttrn, 'a list, bool] => 'a list" ("(1[_:_./ _])") 

923  71 

13366  72 
 {* list update *} 
73 
"_lupdbind":: "['a, 'a] => lupdbind" ("(2_ :=/ _)") 

74 
"" :: "lupdbind => lupdbinds" ("_") 

75 
"_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds" ("_,/ _") 

76 
"_LUpdate" :: "['a, lupdbinds] => 'a" ("_/[(_)]" [900,0] 900) 

5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the ith
nipkow
parents:
4643
diff
changeset

77 

923  78 
translations 
13366  79 
"[x, xs]" == "x#[xs]" 
80 
"[x]" == "x#[]" 

81 
"[x:xs . P]"== "filter (%x. P) xs" 

923  82 

13366  83 
"_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs" 
84 
"xs[i:=x]" == "list_update xs i x" 

5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the ith
nipkow
parents:
4643
diff
changeset

85 

5427  86 

12114
a8e860c86252
eliminated old "symbols" syntax, use "xsymbols" instead;
wenzelm
parents:
10832
diff
changeset

87 
syntax (xsymbols) 
13366  88 
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])") 
14565  89 
syntax (HTML output) 
90 
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])") 

3342
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset

91 

ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset

92 

13142  93 
text {* 
14589  94 
Function @{text size} is overloaded for all datatypes. Users may 
13366  95 
refer to the list version as @{text length}. *} 
13142  96 

19363  97 
abbreviation 
98 
length :: "'a list => nat" 

99 
"length == size" 

15302  100 

5183  101 
primrec 
15307  102 
"hd(x#xs) = x" 
103 

5183  104 
primrec 
15307  105 
"tl([]) = []" 
106 
"tl(x#xs) = xs" 

107 

5183  108 
primrec 
15307  109 
"null([]) = True" 
110 
"null(x#xs) = False" 

111 

8972  112 
primrec 
15307  113 
"last(x#xs) = (if xs=[] then x else last xs)" 
114 

5183  115 
primrec 
15307  116 
"butlast []= []" 
117 
"butlast(x#xs) = (if xs=[] then [] else x#butlast xs)" 

118 

5183  119 
primrec 
15307  120 
"set [] = {}" 
121 
"set (x#xs) = insert x (set xs)" 

122 

5183  123 
primrec 
15307  124 
"map f [] = []" 
125 
"map f (x#xs) = f(x)#map f xs" 

126 

5183  127 
primrec 
15307  128 
append_Nil:"[]@ys = ys" 
129 
append_Cons: "(x#xs)@ys = x#(xs@ys)" 

130 

5183  131 
primrec 
15307  132 
"rev([]) = []" 
133 
"rev(x#xs) = rev(xs) @ [x]" 

134 

5183  135 
primrec 
15307  136 
"filter P [] = []" 
137 
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" 

138 

5183  139 
primrec 
15307  140 
foldl_Nil:"foldl f a [] = a" 
141 
foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs" 

142 

8000  143 
primrec 
15307  144 
"foldr f [] a = a" 
145 
"foldr f (x#xs) a = f x (foldr f xs a)" 

146 

5183  147 
primrec 
15307  148 
"concat([]) = []" 
149 
"concat(x#xs) = x @ concat(xs)" 

150 

5183  151 
primrec 
15307  152 
drop_Nil:"drop n [] = []" 
153 
drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs  Suc(m) => drop m xs)" 

154 
 {*Warning: simpset does not contain this definition, but separate 

155 
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} 

156 

5183  157 
primrec 
15307  158 
take_Nil:"take n [] = []" 
159 
take_Cons: "take n (x#xs) = (case n of 0 => []  Suc(m) => x # take m xs)" 

160 
 {*Warning: simpset does not contain this definition, but separate 

161 
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} 

162 

13142  163 
primrec 
15307  164 
nth_Cons:"(x#xs)!n = (case n of 0 => x  (Suc k) => xs!k)" 
165 
 {*Warning: simpset does not contain this definition, but separate 

166 
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} 

167 

5183  168 
primrec 
15307  169 
"[][i:=v] = []" 
170 
"(x#xs)[i:=v] = (case i of 0 => v # xs  Suc j => x # xs[j:=v])" 

171 

172 
primrec 

173 
"takeWhile P [] = []" 

174 
"takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" 

175 

5183  176 
primrec 
15307  177 
"dropWhile P [] = []" 
178 
"dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" 

179 

5183  180 
primrec 
15307  181 
"zip xs [] = []" 
182 
zip_Cons: "zip xs (y#ys) = (case xs of [] => []  z#zs => (z,y)#zip zs ys)" 

183 
 {*Warning: simpset does not contain this definition, but separate 

184 
theorems for @{text "xs = []"} and @{text "xs = z # zs"} *} 

185 

5427  186 
primrec 
15425  187 
upt_0: "[i..<0] = []" 
188 
upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])" 

15307  189 

5183  190 
primrec 
15307  191 
"distinct [] = True" 
192 
"distinct (x#xs) = (x ~: set xs \<and> distinct xs)" 

193 

5183  194 
primrec 
15307  195 
"remdups [] = []" 
196 
"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" 

197 

5183  198 
primrec 
15307  199 
"remove1 x [] = []" 
200 
"remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)" 

201 

15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

202 
primrec 
15307  203 
replicate_0: "replicate 0 x = []" 
204 
replicate_Suc: "replicate (Suc n) x = x # replicate n x" 

205 

8115  206 
defs 
15302  207 
rotate1_def: "rotate1 xs == (case xs of [] \<Rightarrow> []  x#xs \<Rightarrow> xs @ [x])" 
208 
rotate_def: "rotate n == rotate1 ^ n" 

209 

210 
list_all2_def: 

211 
"list_all2 P xs ys == 

212 
length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y)" 

213 

214 
sublist_def: 

15425  215 
"sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..<size xs]))" 
5281  216 

17086  217 
primrec 
19390  218 
"splice [] ys = ys" 
219 
"splice (x#xs) ys = (if ys=[] then x#xs else x # hd ys # splice xs (tl ys))" 

220 
 {*Warning: simpset does not contain the second eqn but a derived one. *} 

221 

222 
primrec 

17086  223 
"x mem [] = False" 
224 
"x mem (y#ys) = (if y=x then True else x mem ys)" 

225 

226 
primrec 

227 
"list_inter [] bs = []" 

228 
"list_inter (a#as) bs = 

229 
(if a \<in> set bs then a#(list_inter as bs) else list_inter as bs)" 

230 

231 
primrec 

232 
"list_all P [] = True" 

233 
"list_all P (x#xs) = (P(x) \<and> list_all P xs)" 

234 

235 
primrec 

236 
"list_ex P [] = False" 

237 
"list_ex P (x#xs) = (P x \<or> list_ex P xs)" 

238 

239 
primrec 

240 
"filtermap f [] = []" 

241 
"filtermap f (x#xs) = 

242 
(case f x of None \<Rightarrow> filtermap f xs 

243 
 Some y \<Rightarrow> y # (filtermap f xs))" 

244 

245 
primrec 

246 
"map_filter f P [] = []" 

247 
"map_filter f P (x#xs) = (if P x then f x # map_filter f P xs else 

248 
map_filter f P xs)" 

249 

250 
primrec 

251 
"itrev [] ys = ys" 

252 
"itrev (x#xs) ys = itrev xs (x#ys)" 

253 

13114  254 

13142  255 
lemma not_Cons_self [simp]: "xs \<noteq> x # xs" 
13145  256 
by (induct xs) auto 
13114  257 

13142  258 
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric] 
13114  259 

13142  260 
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)" 
13145  261 
by (induct xs) auto 
13114  262 

13142  263 
lemma length_induct: 
13145  264 
"(!!xs. \<forall>ys. length ys < length xs > P ys ==> P xs) ==> P xs" 
17589  265 
by (rule measure_induct [of length]) iprover 
13114  266 

267 

19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset

268 
subsubsection {* @{text null} *} 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset

269 

07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset

270 
lemma null_empty: "null xs = (xs = [])" 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset

271 
by (cases xs) simp_all 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset

272 

20105  273 
lemma empty_null [code inline]: 
19787  274 
"(xs = []) = null xs" 
19817  275 
using null_empty [symmetric] . 
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset

276 

15392  277 
subsubsection {* @{text length} *} 
13114  278 

13142  279 
text {* 
13145  280 
Needs to come before @{text "@"} because of theorem @{text 
281 
append_eq_append_conv}. 

13142  282 
*} 
13114  283 

13142  284 
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" 
13145  285 
by (induct xs) auto 
13114  286 

13142  287 
lemma length_map [simp]: "length (map f xs) = length xs" 
13145  288 
by (induct xs) auto 
13114  289 

13142  290 
lemma length_rev [simp]: "length (rev xs) = length xs" 
13145  291 
by (induct xs) auto 
13114  292 

13142  293 
lemma length_tl [simp]: "length (tl xs) = length xs  1" 
13145  294 
by (cases xs) auto 
13114  295 

13142  296 
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" 
13145  297 
by (induct xs) auto 
13114  298 

13142  299 
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" 
13145  300 
by (induct xs) auto 
13114  301 

302 
lemma length_Suc_conv: 

13145  303 
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" 
304 
by (induct xs) auto 

13142  305 

14025  306 
lemma Suc_length_conv: 
307 
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" 

14208  308 
apply (induct xs, simp, simp) 
14025  309 
apply blast 
310 
done 

311 

14099  312 
lemma impossible_Cons [rule_format]: 
313 
"length xs <= length ys > xs = x # ys = False" 

14208  314 
apply (induct xs, auto) 
14099  315 
done 
316 

14247  317 
lemma list_induct2[consumes 1]: "\<And>ys. 
318 
\<lbrakk> length xs = length ys; 

319 
P [] []; 

320 
\<And>x xs y ys. \<lbrakk> length xs = length ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk> 

321 
\<Longrightarrow> P xs ys" 

322 
apply(induct xs) 

323 
apply simp 

324 
apply(case_tac ys) 

325 
apply simp 

326 
apply(simp) 

327 
done 

13114  328 

15392  329 
subsubsection {* @{text "@"}  append *} 
13114  330 

13142  331 
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" 
13145  332 
by (induct xs) auto 
13114  333 

13142  334 
lemma append_Nil2 [simp]: "xs @ [] = xs" 
13145  335 
by (induct xs) auto 
3507  336 

13142  337 
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" 
13145  338 
by (induct xs) auto 
13114  339 

13142  340 
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])" 
13145  341 
by (induct xs) auto 
13114  342 

13142  343 
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" 
13145  344 
by (induct xs) auto 
13114  345 

13142  346 
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])" 
13145  347 
by (induct xs) auto 
13114  348 

13883
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

349 
lemma append_eq_append_conv [simp]: 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

350 
"!!ys. length xs = length ys \<or> length us = length vs 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

351 
==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)" 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

352 
apply (induct xs) 
14208  353 
apply (case_tac ys, simp, force) 
354 
apply (case_tac ys, force, simp) 

13145  355 
done 
13142  356 

14495  357 
lemma append_eq_append_conv2: "!!ys zs ts. 
358 
(xs @ ys = zs @ ts) = 

359 
(EX us. xs = zs @ us & us @ ys = ts  xs @ us = zs & ys = us@ ts)" 

360 
apply (induct xs) 

361 
apply fastsimp 

362 
apply(case_tac zs) 

363 
apply simp 

364 
apply fastsimp 

365 
done 

366 

13142  367 
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)" 
13145  368 
by simp 
13142  369 

370 
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)" 

13145  371 
by simp 
13114  372 

13142  373 
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)" 
13145  374 
by simp 
13114  375 

13142  376 
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" 
13145  377 
using append_same_eq [of _ _ "[]"] by auto 
3507  378 

13142  379 
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])" 
13145  380 
using append_same_eq [of "[]"] by auto 
13114  381 

13142  382 
lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs" 
13145  383 
by (induct xs) auto 
13114  384 

13142  385 
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" 
13145  386 
by (induct xs) auto 
13114  387 

13142  388 
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs" 
13145  389 
by (simp add: hd_append split: list.split) 
13114  390 

13142  391 
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys  z#zs => zs @ ys)" 
13145  392 
by (simp split: list.split) 
13114  393 

13142  394 
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys" 
13145  395 
by (simp add: tl_append split: list.split) 
13114  396 

397 

14300  398 
lemma Cons_eq_append_conv: "x#xs = ys@zs = 
399 
(ys = [] & x#xs = zs  (EX ys'. x#ys' = ys & xs = ys'@zs))" 

400 
by(cases ys) auto 

401 

15281  402 
lemma append_eq_Cons_conv: "(ys@zs = x#xs) = 
403 
(ys = [] & zs = x#xs  (EX ys'. ys = x#ys' & ys'@zs = xs))" 

404 
by(cases ys) auto 

405 

14300  406 

13142  407 
text {* Trivial rules for solving @{text "@"}equations automatically. *} 
13114  408 

409 
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys" 

13145  410 
by simp 
13114  411 

13142  412 
lemma Cons_eq_appendI: 
13145  413 
"[ x # xs1 = ys; xs = xs1 @ zs ] ==> x # xs = ys @ zs" 
414 
by (drule sym) simp 

13114  415 

13142  416 
lemma append_eq_appendI: 
13145  417 
"[ xs @ xs1 = zs; ys = xs1 @ us ] ==> xs @ ys = zs @ us" 
418 
by (drule sym) simp 

13114  419 

420 

13142  421 
text {* 
13145  422 
Simplification procedure for all list equalities. 
423 
Currently only tries to rearrange @{text "@"} to see if 

424 
 both lists end in a singleton list, 

425 
 or both lists end in the same list. 

13142  426 
*} 
427 

428 
ML_setup {* 

3507  429 
local 
430 

13122  431 
val append_assoc = thm "append_assoc"; 
432 
val append_Nil = thm "append_Nil"; 

433 
val append_Cons = thm "append_Cons"; 

434 
val append1_eq_conv = thm "append1_eq_conv"; 

435 
val append_same_eq = thm "append_same_eq"; 

436 

13114  437 
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) = 
13462  438 
(case xs of Const("List.list.Nil",_) => cons  _ => last xs) 
439 
 last (Const("List.op @",_) $ _ $ ys) = last ys 

440 
 last t = t; 

13114  441 

442 
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true 

13462  443 
 list1 _ = false; 
13114  444 

445 
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) = 

13462  446 
(case xs of Const("List.list.Nil",_) => xs  _ => cons $ butlast xs) 
447 
 butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys 

448 
 butlast xs = Const("List.list.Nil",fastype_of xs); 

13114  449 

16973  450 
val rearr_ss = HOL_basic_ss addsimps [append_assoc, append_Nil, append_Cons]; 
451 

20044
92cc2f4c7335
simprocs: no theory argument  use simpset context instead;
wenzelm
parents:
19890
diff
changeset

452 
fun list_eq ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) = 
13462  453 
let 
454 
val lastl = last lhs and lastr = last rhs; 

455 
fun rearr conv = 

456 
let 

457 
val lhs1 = butlast lhs and rhs1 = butlast rhs; 

458 
val Type(_,listT::_) = eqT 

459 
val appT = [listT,listT] > listT 

460 
val app = Const("List.op @",appT) 

461 
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

462 
val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2)); 
20044
92cc2f4c7335
simprocs: no theory argument  use simpset context instead;
wenzelm
parents:
19890
diff
changeset

463 
val thm = Goal.prove (Simplifier.the_context ss) [] [] eq 
17877
67d5ab1cb0d8
Simplifier.inherit_context instead of Simplifier.inherit_bounds;
wenzelm
parents:
17830
diff
changeset

464 
(K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1)); 
15531  465 
in SOME ((conv RS (thm RS trans)) RS eq_reflection) end; 
13114  466 

13462  467 
in 
468 
if list1 lastl andalso list1 lastr then rearr append1_eq_conv 

469 
else if lastl aconv lastr then rearr append_same_eq 

15531  470 
else NONE 
13462  471 
end; 
472 

13114  473 
in 
13462  474 

475 
val list_eq_simproc = 

20044
92cc2f4c7335
simprocs: no theory argument  use simpset context instead;
wenzelm
parents:
19890
diff
changeset

476 
Simplifier.simproc (Theory.sign_of (the_context ())) "list_eq" ["(xs::'a list) = ys"] (K list_eq); 
13462  477 

13114  478 
end; 
479 

480 
Addsimprocs [list_eq_simproc]; 

481 
*} 

482 

483 

15392  484 
subsubsection {* @{text map} *} 
13114  485 

13142  486 
lemma map_ext: "(!!x. x : set xs > f x = g x) ==> map f xs = map g xs" 
13145  487 
by (induct xs) simp_all 
13114  488 

13142  489 
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)" 
13145  490 
by (rule ext, induct_tac xs) auto 
13114  491 

13142  492 
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" 
13145  493 
by (induct xs) auto 
13114  494 

13142  495 
lemma map_compose: "map (f o g) xs = map f (map g xs)" 
13145  496 
by (induct xs) (auto simp add: o_def) 
13114  497 

13142  498 
lemma rev_map: "rev (map f xs) = map f (rev xs)" 
13145  499 
by (induct xs) auto 
13114  500 

13737  501 
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)" 
502 
by (induct xs) auto 

503 

19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19623
diff
changeset

504 
lemma map_cong [fundef_cong, recdef_cong]: 
13145  505 
"xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys" 
506 
 {* a congruence rule for @{text map} *} 

13737  507 
by simp 
13114  508 

13142  509 
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])" 
13145  510 
by (cases xs) auto 
13114  511 

13142  512 
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])" 
13145  513 
by (cases xs) auto 
13114  514 

18447  515 
lemma map_eq_Cons_conv: 
14025  516 
"(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" 
13145  517 
by (cases xs) auto 
13114  518 

18447  519 
lemma Cons_eq_map_conv: 
14025  520 
"(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)" 
521 
by (cases ys) auto 

522 

18447  523 
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1] 
524 
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1] 

525 
declare map_eq_Cons_D [dest!] Cons_eq_map_D [dest!] 

526 

14111  527 
lemma ex_map_conv: 
528 
"(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)" 

18447  529 
by(induct ys, auto simp add: Cons_eq_map_conv) 
14111  530 

15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

531 
lemma map_eq_imp_length_eq: 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

532 
"!!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

533 
apply (induct ys) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

534 
apply simp 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

535 
apply(simp (no_asm_use)) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

536 
apply clarify 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

537 
apply(simp (no_asm_use)) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

538 
apply fast 
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 map_inj_on: 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

542 
"[ 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

543 
==> xs = ys" 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

544 
apply(frule map_eq_imp_length_eq) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

545 
apply(rotate_tac 1) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

546 
apply(induct rule:list_induct2) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

547 
apply simp 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

548 
apply(simp) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

549 
apply (blast intro:sym) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

550 
done 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

551 

78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

552 
lemma inj_on_map_eq_map: 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

553 
"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

554 
by(blast dest:map_inj_on) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

555 

13114  556 
lemma map_injective: 
14338  557 
"!!xs. map f xs = map f ys ==> inj f ==> xs = ys" 
558 
by (induct ys) (auto dest!:injD) 

13114  559 

14339  560 
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" 
561 
by(blast dest:map_injective) 

562 

13114  563 
lemma inj_mapI: "inj f ==> inj (map f)" 
17589  564 
by (iprover dest: map_injective injD intro: inj_onI) 
13114  565 

566 
lemma inj_mapD: "inj (map f) ==> inj f" 

14208  567 
apply (unfold inj_on_def, clarify) 
13145  568 
apply (erule_tac x = "[x]" in ballE) 
14208  569 
apply (erule_tac x = "[y]" in ballE, simp, blast) 
13145  570 
apply blast 
571 
done 

13114  572 

14339  573 
lemma inj_map[iff]: "inj (map f) = inj f" 
13145  574 
by (blast dest: inj_mapD intro: inj_mapI) 
13114  575 

15303  576 
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A" 
577 
apply(rule inj_onI) 

578 
apply(erule map_inj_on) 

579 
apply(blast intro:inj_onI dest:inj_onD) 

580 
done 

581 

14343  582 
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs" 
583 
by (induct xs, auto) 

13114  584 

14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset

585 
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

586 
by (induct xs) auto 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset

587 

15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

588 
lemma map_fst_zip[simp]: 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

589 
"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

590 
by (induct rule:list_induct2, simp_all) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

591 

78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

592 
lemma map_snd_zip[simp]: 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

593 
"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

594 
by (induct rule:list_induct2, simp_all) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

595 

78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

596 

15392  597 
subsubsection {* @{text rev} *} 
13114  598 

13142  599 
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" 
13145  600 
by (induct xs) auto 
13114  601 

13142  602 
lemma rev_rev_ident [simp]: "rev (rev xs) = xs" 
13145  603 
by (induct xs) auto 
13114  604 

15870  605 
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)" 
606 
by auto 

607 

13142  608 
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])" 
13145  609 
by (induct xs) auto 
13114  610 

13142  611 
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])" 
13145  612 
by (induct xs) auto 
13114  613 

15870  614 
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])" 
615 
by (cases xs) auto 

616 

617 
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])" 

618 
by (cases xs) auto 

619 

13142  620 
lemma rev_is_rev_conv [iff]: "!!ys. (rev xs = rev ys) = (xs = ys)" 
14208  621 
apply (induct xs, force) 
622 
apply (case_tac ys, simp, force) 

13145  623 
done 
13114  624 

15439  625 
lemma inj_on_rev[iff]: "inj_on rev A" 
626 
by(simp add:inj_on_def) 

627 

13366  628 
lemma rev_induct [case_names Nil snoc]: 
629 
"[ 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

630 
apply(simplesubst rev_rev_ident[symmetric]) 
13145  631 
apply(rule_tac list = "rev xs" in list.induct, simp_all) 
632 
done 

13114  633 

13145  634 
ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *} "compatibility" 
13114  635 

13366  636 
lemma rev_exhaust [case_names Nil snoc]: 
637 
"(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P" 

13145  638 
by (induct xs rule: rev_induct) auto 
13114  639 

13366  640 
lemmas rev_cases = rev_exhaust 
641 

18423  642 
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])" 
643 
by(rule rev_cases[of xs]) auto 

644 

13114  645 

15392  646 
subsubsection {* @{text set} *} 
13114  647 

13142  648 
lemma finite_set [iff]: "finite (set xs)" 
13145  649 
by (induct xs) auto 
13114  650 

13142  651 
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)" 
13145  652 
by (induct xs) auto 
13114  653 

17830  654 
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs" 
655 
by(cases xs) auto 

14099  656 

13142  657 
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" 
13145  658 
by auto 
13114  659 

14099  660 
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" 
661 
by auto 

662 

13142  663 
lemma set_empty [iff]: "(set xs = {}) = (xs = [])" 
13145  664 
by (induct xs) auto 
13114  665 

15245  666 
lemma set_empty2[iff]: "({} = set xs) = (xs = [])" 
667 
by(induct xs) auto 

668 

13142  669 
lemma set_rev [simp]: "set (rev xs) = set xs" 
13145  670 
by (induct xs) auto 
13114  671 

13142  672 
lemma set_map [simp]: "set (map f xs) = f`(set xs)" 
13145  673 
by (induct xs) auto 
13114  674 

13142  675 
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}" 
13145  676 
by (induct xs) auto 
13114  677 

15425  678 
lemma set_upt [simp]: "set[i..<j] = {k. i \<le> k \<and> k < j}" 
14208  679 
apply (induct j, simp_all) 
680 
apply (erule ssubst, auto) 

13145  681 
done 
13114  682 

13142  683 
lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)" 
15113  684 
proof (induct xs) 
685 
case Nil show ?case by simp 

686 
case (Cons a xs) 

687 
show ?case 

688 
proof 

689 
assume "x \<in> set (a # xs)" 

690 
with prems show "\<exists>ys zs. a # xs = ys @ x # zs" 

691 
by (simp, blast intro: Cons_eq_appendI) 

692 
next 

693 
assume "\<exists>ys zs. a # xs = ys @ x # zs" 

694 
then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast 

695 
show "x \<in> set (a # xs)" 

696 
by (cases ys, auto simp add: eq) 

697 
qed 

698 
qed 

13142  699 

18049  700 
lemma in_set_conv_decomp_first: 
701 
"(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)" 

702 
proof (induct xs) 

703 
case Nil show ?case by simp 

704 
next 

705 
case (Cons a xs) 

706 
show ?case 

707 
proof cases 

708 
assume "x = a" thus ?case using Cons by force 

709 
next 

710 
assume "x \<noteq> a" 

711 
show ?case 

712 
proof 

713 
assume "x \<in> set (a # xs)" 

714 
from prems show "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys" 

715 
by(fastsimp intro!: Cons_eq_appendI) 

716 
next 

717 
assume "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys" 

718 
then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast 

719 
show "x \<in> set (a # xs)" by (cases ys, auto simp add: eq) 

720 
qed 

721 
qed 

722 
qed 

723 

724 
lemmas split_list = in_set_conv_decomp[THEN iffD1, standard] 

725 
lemmas split_list_first = in_set_conv_decomp_first[THEN iffD1, standard] 

726 

727 

13508  728 
lemma finite_list: "finite A ==> EX l. set l = A" 
729 
apply (erule finite_induct, auto) 

730 
apply (rule_tac x="x#l" in exI, auto) 

731 
done 

732 

14388  733 
lemma card_length: "card (set xs) \<le> length xs" 
734 
by (induct xs) (auto simp add: card_insert_if) 

13114  735 

15168  736 

15392  737 
subsubsection {* @{text filter} *} 
13114  738 

13142  739 
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" 
13145  740 
by (induct xs) auto 
13114  741 

15305  742 
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)" 
743 
by (induct xs) simp_all 

744 

13142  745 
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs" 
13145  746 
by (induct xs) auto 
13114  747 

16998  748 
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs" 
749 
by (induct xs) (auto simp add: le_SucI) 

750 

18423  751 
lemma sum_length_filter_compl: 
752 
"length(filter P xs) + length(filter (%x. ~P x) xs) = length xs" 

753 
by(induct xs) simp_all 

754 

13142  755 
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs" 
13145  756 
by (induct xs) auto 
13114  757 

13142  758 
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []" 
13145  759 
by (induct xs) auto 
13114  760 

16998  761 
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" 
762 
by (induct xs) simp_all 

763 

764 
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)" 

765 
apply (induct xs) 

766 
apply auto 

767 
apply(cut_tac P=P and xs=xs in length_filter_le) 

768 
apply simp 

769 
done 

13114  770 

16965  771 
lemma filter_map: 
772 
"filter P (map f xs) = map f (filter (P o f) xs)" 

773 
by (induct xs) simp_all 

774 

775 
lemma length_filter_map[simp]: 

776 
"length (filter P (map f xs)) = length(filter (P o f) xs)" 

777 
by (simp add:filter_map) 

778 

13142  779 
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" 
13145  780 
by auto 
13114  781 

15246  782 
lemma length_filter_less: 
783 
"\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs" 

784 
proof (induct xs) 

785 
case Nil thus ?case by simp 

786 
next 

787 
case (Cons x xs) thus ?case 

788 
apply (auto split:split_if_asm) 

789 
using length_filter_le[of P xs] apply arith 

790 
done 

791 
qed 

13114  792 

15281  793 
lemma length_filter_conv_card: 
794 
"length(filter p xs) = card{i. i < length xs & p(xs!i)}" 

795 
proof (induct xs) 

796 
case Nil thus ?case by simp 

797 
next 

798 
case (Cons x xs) 

799 
let ?S = "{i. i < length xs & p(xs!i)}" 

800 
have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite) 

801 
show ?case (is "?l = card ?S'") 

802 
proof (cases) 

803 
assume "p x" 

804 
hence eq: "?S' = insert 0 (Suc ` ?S)" 

805 
by(auto simp add: nth_Cons image_def split:nat.split elim:lessE) 

806 
have "length (filter p (x # xs)) = Suc(card ?S)" 

807 
using Cons by simp 

808 
also have "\<dots> = Suc(card(Suc ` ?S))" using fin 

809 
by (simp add: card_image inj_Suc) 

810 
also have "\<dots> = card ?S'" using eq fin 

811 
by (simp add:card_insert_if) (simp add:image_def) 

812 
finally show ?thesis . 

813 
next 

814 
assume "\<not> p x" 

815 
hence eq: "?S' = Suc ` ?S" 

816 
by(auto simp add: nth_Cons image_def split:nat.split elim:lessE) 

817 
have "length (filter p (x # xs)) = card ?S" 

818 
using Cons by simp 

819 
also have "\<dots> = card(Suc ` ?S)" using fin 

820 
by (simp add: card_image inj_Suc) 

821 
also have "\<dots> = card ?S'" using eq fin 

822 
by (simp add:card_insert_if) 

823 
finally show ?thesis . 

824 
qed 

825 
qed 

826 

17629  827 
lemma Cons_eq_filterD: 
828 
"x#xs = filter P ys \<Longrightarrow> 

829 
\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs" 

19585  830 
(is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs") 
17629  831 
proof(induct ys) 
832 
case Nil thus ?case by simp 

833 
next 

834 
case (Cons y ys) 

835 
show ?case (is "\<exists>x. ?Q x") 

836 
proof cases 

837 
assume Py: "P y" 

838 
show ?thesis 

839 
proof cases 

840 
assume xy: "x = y" 

841 
show ?thesis 

842 
proof from Py xy Cons(2) show "?Q []" by simp qed 

843 
next 

844 
assume "x \<noteq> y" with Py Cons(2) show ?thesis by simp 

845 
qed 

846 
next 

847 
assume Py: "\<not> P y" 

848 
with Cons obtain us vs where 1 : "?P (y#ys) (y#us) vs" by fastsimp 

849 
show ?thesis (is "? us. ?Q us") 

850 
proof show "?Q (y#us)" using 1 by simp qed 

851 
qed 

852 
qed 

853 

854 
lemma filter_eq_ConsD: 

855 
"filter P ys = x#xs \<Longrightarrow> 

856 
\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs" 

857 
by(rule Cons_eq_filterD) simp 

858 

859 
lemma filter_eq_Cons_iff: 

860 
"(filter P ys = x#xs) = 

861 
(\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" 

862 
by(auto dest:filter_eq_ConsD) 

863 

864 
lemma Cons_eq_filter_iff: 

865 
"(x#xs = filter P ys) = 

866 
(\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" 

867 
by(auto dest:Cons_eq_filterD) 

868 

19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19623
diff
changeset

869 
lemma filter_cong[fundef_cong, recdef_cong]: 
17501  870 
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys" 
871 
apply simp 

872 
apply(erule thin_rl) 

873 
by (induct ys) simp_all 

874 

15281  875 

15392  876 
subsubsection {* @{text concat} *} 
13114  877 

13142  878 
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" 
13145  879 
by (induct xs) auto 
13114  880 

18447  881 
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])" 
13145  882 
by (induct xss) auto 
13114  883 

18447  884 
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])" 
13145  885 
by (induct xss) auto 
13114  886 

13142  887 
lemma set_concat [simp]: "set (concat xs) = \<Union>(set ` set xs)" 
13145  888 
by (induct xs) auto 
13114  889 

13142  890 
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" 
13145  891 
by (induct xs) auto 
13114  892 

13142  893 
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" 
13145  894 
by (induct xs) auto 
13114  895 

13142  896 
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" 
13145  897 
by (induct xs) auto 
13114  898 

899 

15392  900 
subsubsection {* @{text nth} *} 
13114  901 

13142  902 
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x" 
13145  903 
by auto 
13114  904 

13142  905 
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n" 
13145  906 
by auto 
13114  907 

13142  908 
declare nth.simps [simp del] 
13114  909 

910 
lemma nth_append: 

13145  911 
"!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n  length xs))" 
14208  912 
apply (induct "xs", simp) 
913 
apply (case_tac n, auto) 

13145  914 
done 
13114  915 

14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset

916 
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

917 
by (induct "xs") auto 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset

918 

4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset

919 
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

920 
by (induct "xs") auto 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset

921 

13142  922 
lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)" 
14208  923 
apply (induct xs, simp) 
924 
apply (case_tac n, auto) 

13145  925 
done 
13114  926 

18423  927 
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0" 
928 
by(cases xs) simp_all 

929 

18049  930 

931 
lemma list_eq_iff_nth_eq: 

932 
"!!ys. (xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))" 

933 
apply(induct xs) 

934 
apply simp apply blast 

935 
apply(case_tac ys) 

936 
apply simp 

937 
apply(simp add:nth_Cons split:nat.split)apply blast 

938 
done 

939 

13142  940 
lemma set_conv_nth: "set xs = {xs!i  i. i < length xs}" 
15251  941 
apply (induct xs, simp, simp) 
13145  942 
apply safe 
14208  943 
apply (rule_tac x = 0 in exI, simp) 
944 
apply (rule_tac x = "Suc i" in exI, simp) 

945 
apply (case_tac i, simp) 

13145  946 
apply (rename_tac j) 
14208  947 
apply (rule_tac x = j in exI, simp) 
13145  948 
done 
13114  949 

17501  950 
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)" 
951 
by(auto simp:set_conv_nth) 

952 

13145  953 
lemma list_ball_nth: "[ n < length xs; !x : set xs. P x] ==> P(xs!n)" 
954 
by (auto simp add: set_conv_nth) 

13114  955 

13142  956 
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs" 
13145  957 
by (auto simp add: set_conv_nth) 
13114  958 

959 
lemma all_nth_imp_all_set: 

13145  960 
"[ !i < length xs. P(xs!i); x : set xs] ==> P x" 
961 
by (auto simp add: set_conv_nth) 

13114  962 

963 
lemma all_set_conv_all_nth: 

13145  964 
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs > P (xs ! i))" 
965 
by (auto simp add: set_conv_nth) 

13114  966 

967 

15392  968 
subsubsection {* @{text list_update} *} 
13114  969 

13142  970 
lemma length_list_update [simp]: "!!i. length(xs[i:=x]) = length xs" 
13145  971 
by (induct xs) (auto split: nat.split) 
13114  972 

973 
lemma nth_list_update: 

13145  974 
"!!i j. i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)" 
975 
by (induct xs) (auto simp add: nth_Cons split: nat.split) 

13114  976 

13142  977 
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x" 
13145  978 
by (simp add: nth_list_update) 
13114  979 

13142  980 
lemma nth_list_update_neq [simp]: "!!i j. i \<noteq> j ==> xs[i:=x]!j = xs!j" 
13145  981 
by (induct xs) (auto simp add: nth_Cons split: nat.split) 
13114  982 

13142  983 
lemma list_update_overwrite [simp]: 
13145  984 
"!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]" 
985 
by (induct xs) (auto split: nat.split) 

13114  986 

14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset

987 
lemma list_update_id[simp]: "!!i. i < length xs ==> xs[i := xs!i] = xs" 
14208  988 
apply (induct xs, simp) 
14187  989 
apply(simp split:nat.splits) 
990 
done 

991 

17501  992 
lemma list_update_beyond[simp]: "\<And>i. length xs \<le> i \<Longrightarrow> xs[i:=x] = xs" 
993 
apply (induct xs) 

994 
apply simp 

995 
apply (case_tac i) 

996 
apply simp_all 

997 
done 

998 

13114  999 
lemma list_update_same_conv: 
13145  1000 
"!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)" 
1001 
by (induct xs) (auto split: nat.split) 

13114  1002 

14187  1003 
lemma list_update_append1: 
1004 
"!!i. i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys" 

14208  1005 
apply (induct xs, simp) 
14187  1006 
apply(simp split:nat.split) 
1007 
done 

1008 

15868  1009 
lemma list_update_append: 
1010 
"!!n. (xs @ ys) [n:= x] = 

1011 
(if n < length xs then xs[n:= x] @ ys else xs @ (ys [nlength xs:= x]))" 

1012 
by (induct xs) (auto split:nat.splits) 

1013 

14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset

1014 
lemma list_update_length [simp]: 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset

1015 
"(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

1016 
by (induct xs, auto) 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset

1017 

13114  1018 
lemma update_zip: 
13145  1019 
"!!i xy xs. length xs = length ys ==> 
1020 
(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" 

1021 
by (induct ys) (auto, case_tac xs, auto split: nat.split) 

13114  1022 

1023 
lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)" 

13145  1024 
by (induct xs) (auto split: nat.split) 
13114  1025 

1026 
lemma set_update_subsetI: "[ set xs <= A; x:A ] ==> set(xs[i := x]) <= A" 

13145  1027 
by (blast dest!: set_update_subset_insert [THEN subsetD]) 
13114  1028 

15868  1029 
lemma set_update_memI: "!!n. n < length xs \<Longrightarrow> x \<in> set (xs[n := x])" 
1030 
by (induct xs) (auto split:nat.splits) 

1031 

13114  1032 

15392  1033 
subsubsection {* @{text last} and @{text butlast} *} 
13114  1034 

13142  1035 
lemma last_snoc [simp]: "last (xs @ [x]) = x" 
13145  1036 
by (induct xs) auto 
13114  1037 

13142  1038 
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" 
13145  1039 
by (induct xs) auto 
13114  1040 

14302  1041 
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x" 
1042 
by(simp add:last.simps) 

1043 

1044 
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs" 

1045 
by(simp add:last.simps) 

1046 

1047 
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)" 

1048 
by (induct xs) (auto) 

1049 

1050 
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs" 

1051 
by(simp add:last_append) 

1052 

1053 
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys" 

1054 
by(simp add:last_append) 

1055 

17762  1056 
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs" 
1057 
by(rule rev_exhaust[of xs]) simp_all 

1058 

1059 
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs" 

1060 
by(cases xs) simp_all 

1061 

17765  1062 
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as" 
1063 
by (induct as) auto 

17762  1064 

13142  1065 
lemma length_butlast [simp]: "length (butlast xs) = length xs  1" 
13145  1066 
by (induct xs rule: rev_induct) auto 
13114  1067 

1068 
lemma butlast_append: 

13145  1069 
"!!ys. butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)" 
1070 
by (induct xs) auto 

13114  1071 

13142  1072 
lemma append_butlast_last_id [simp]: 
13145  1073 
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs" 
1074 
by (induct xs) auto 

13114  1075 

13142  1076 
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs" 
13145  1077 
by (induct xs) (auto split: split_if_asm) 
13114  1078 

1079 
lemma in_set_butlast_appendI: 

13145  1080 
"x : set (butlast xs)  x : set (butlast ys) ==> x : set (butlast (xs @ ys))" 
1081 
by (auto dest: in_set_butlastD simp add: butlast_append) 

13114  1082 

17501  1083 
lemma last_drop[simp]: "!!n. n < length xs \<Longrightarrow> last (drop n xs) = last xs" 
1084 
apply (induct xs) 

1085 
apply simp 

1086 
apply (auto split:nat.split) 

1087 
done 

1088 

17589  1089 
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs  1)" 
1090 
by(induct xs)(auto simp:neq_Nil_conv) 

1091 

15392  1092 
subsubsection {* @{text take} and @{text drop} *} 
13114  1093 

13142  1094 
lemma take_0 [simp]: "take 0 xs = []" 
13145  1095 
by (induct xs) auto 
13114  1096 

13142  1097 
lemma drop_0 [simp]: "drop 0 xs = xs" 
13145  1098 
by (induct xs) auto 
13114  1099 

13142  1100 
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" 
13145  1101 
by simp 
13114  1102 

13142  1103 
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" 
13145  1104 
by simp 
13114  1105 

13142  1106 
declare take_Cons [simp del] and drop_Cons [simp del] 
13114  1107 

15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1108 
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

1109 
by(clarsimp simp add:neq_Nil_conv) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1110 

14187  1111 
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)" 
1112 
by(cases xs, simp_all) 

1113 

1114 
lemma drop_tl: "!!n. drop n (tl xs) = tl(drop n xs)" 

1115 
by(induct xs, simp_all add:drop_Cons drop_Suc split:nat.split) 

1116 

1117 
lemma nth_via_drop: "!!n. drop n xs = y#ys \<Longrightarrow> xs!n = y" 

14208  1118 
apply (induct xs, simp) 
14187  1119 
apply(simp add:drop_Cons nth_Cons split:nat.splits) 
1120 
done 

1121 

13913  1122 
lemma take_Suc_conv_app_nth: 
1123 
"!!i. i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]" 

14208  1124 
apply (induct xs, simp) 
1125 
apply (case_tac i, auto) 

13913  1126 
done 
1127 

14591  1128 
lemma drop_Suc_conv_tl: 
1129 
"!!i. i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs" 

1130 
apply (induct xs, simp) 

1131 
apply (case_tac i, auto) 

1132 
done 

1133 

13142  1134 
lemma length_take [simp]: "!!xs. length (take n xs) = min (length xs) n" 
13145  1135 
by (induct n) (auto, case_tac xs, auto) 
13114  1136 

13142  1137 
lemma length_drop [simp]: "!!xs. length (drop n xs) = (length xs  n)" 
13145  1138 
by (induct n) (auto, case_tac xs, auto) 
13114  1139 

13142  1140 
lemma take_all [simp]: "!!xs. length xs <= n ==> take n xs = xs" 
13145  1141 
by (induct n) (auto, case_tac xs, auto) 
13114  1142 

13142  1143 
lemma drop_all [simp]: "!!xs. length xs <= n ==> drop n xs = []" 
13145  1144 
by (induct n) (auto, case_tac xs, auto) 
13114  1145 

13142  1146 
lemma take_append [simp]: 
13145  1147 
"!!xs. take n (xs @ ys) = (take n xs @ take (n  length xs) ys)" 
1148 
by (induct n) (auto, case_tac xs, auto) 

13114  1149 

13142  1150 
lemma drop_append [simp]: 
13145  1151 
"!!xs. drop n (xs @ ys) = drop n xs @ drop (n  length xs) ys" 
1152 
by (induct n) (auto, case_tac xs, auto) 

13114  1153 

13142  1154 
lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs" 
14208  1155 
apply (induct m, auto) 
1156 
apply (case_tac xs, auto) 

15236
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
nipkow
parents:
15176
diff
changeset

1157 
apply (case_tac n, auto) 
13145  1158 
done 
13114  1159 

13142  1160 
lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs" 
14208  1161 
apply (induct m, auto) 
1162 
apply (case_tac xs, auto) 

13145  1163 
done 
13114  1164 

1165 
lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)" 

14208  1166 
apply (induct m, auto) 
1167 
apply (case_tac xs, auto) 

13145  1168 
done 
13114  1169 

14802  1170 
lemma drop_take: "!!m n. drop n (take m xs) = take (mn) (drop n xs)" 
1171 
apply(induct xs) 

1172 
apply simp 

1173 
apply(simp add: take_Cons drop_Cons split:nat.split) 

1174 
done 

1175 

13142  1176 
lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs" 
14208  1177 
apply (induct n, auto) 
1178 
apply (case_tac xs, auto) 

13145  1179 
done 
13114  1180 

15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1181 
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

1182 
apply(induct xs) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1183 
apply simp 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1184 
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

1185 
done 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1186 

78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1187 
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

1188 
apply(induct xs) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1189 
apply simp 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1190 
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

1191 
done 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1192 

13114  1193 
lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)" 
14208  1194 
apply (induct n, auto) 
1195 
apply (case_tac xs, auto) 

13145  1196 
done 
13114  1197 

13142  1198 
lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)" 
14208  1199 
apply (induct n, auto) 
1200 
apply (case_tac xs, auto) 

13145  1201 
done 
13114  1202 

1203 
lemma rev_take: "!!i. rev (take i xs) = drop (length xs  i) (rev xs)" 

14208  1204 
apply (induct xs, auto) 
1205 
apply (case_tac i, auto) 

13145  1206 
done 
13114  1207 

1208 
lemma rev_drop: "!!i. rev (drop i xs) = take (length xs  i) (rev xs)" 

14208  1209 
apply (induct xs, auto) 
1210 
apply (case_tac i, auto) 

13145  1211 
done 
13114  1212 

13142  1213 
lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i" 
14208  1214 
apply (induct xs, auto) 
1215 
apply (case_tac n, blast) 

1216 
apply (case_tac i, auto) 

13145  1217 
done 
13114  1218 

13142  1219 
lemma nth_drop [simp]: 
13145  1220 
"!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)" 
14208  1221 
apply (induct n, auto) 
1222 
apply (case_tac xs, auto) 

13145  1223 
done 
3507  1224 

18423  1225 
lemma hd_drop_conv_nth: "\<lbrakk> xs \<noteq> []; n < length xs \<rbrakk> \<Longrightarrow> hd(drop n xs) = xs!n" 
1226 
by(simp add: hd_conv_nth) 

1227 

14025  1228 
lemma set_take_subset: "\<And>n. set(take n xs) \<subseteq> set xs" 
1229 
by(induct xs)(auto simp:take_Cons split:nat.split) 

1230 

1231 
lemma set_drop_subset: "\<And>n. set(drop n xs) \<subseteq> set xs" 

1232 
by(induct xs)(auto simp:drop_Cons split:nat.split) 

1233 

14187  1234 
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs" 
1235 
using set_take_subset by fast 

1236 

1237 
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs" 

1238 
using set_drop_subset by fast 

1239 

13114  1240 
lemma append_eq_conv_conj: 
13145  1241 
"!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)" 
14208  1242 
apply (induct xs, simp, clarsimp) 
1243 
apply (case_tac zs, auto) 

13145  1244 
done 
13142  1245 

14050  1246 
lemma take_add [rule_format]: 
1247 
"\<forall>i. i+j \<le> length(xs) > take (i+j) xs = take i xs @ take j (drop i xs)" 

1248 
apply (induct xs, auto) 

1249 
apply (case_tac i, simp_all) 

1250 
done 

1251 

14300  1252 
lemma append_eq_append_conv_if: 
1253 
"!! ys\<^isub>1. (xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) = 

1254 
(if size xs\<^isub>1 \<le> size ys\<^isub>1 

1255 
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 

1256 
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)" 

1257 
apply(induct xs\<^isub>1) 

1258 
apply simp 

1259 
apply(case_tac ys\<^isub>1) 

1260 
apply simp_all 

1261 
done 

1262 

15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1263 
lemma take_hd_drop: 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1264 
"!!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

1265 
apply(induct xs) 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1266 
apply simp 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1267 
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

1268 
done 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

1269 

17501  1270 
lemma id_take_nth_drop: 
1271 
"i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" 

1272 
proof  

1273 
assume si: "i < length xs" 

1274 
hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto 

1275 
moreover 

1276 
from si have "take (Suc i) xs = take i xs @ [xs!i]" 

1277 
apply (rule_tac take_Suc_conv_app_nth) by arith 

1278 
ultimately show ?thesis by auto 

1279 
qed 

1280 

1281 
lemma upd_conv_take_nth_drop: 

1282 
"i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs" 

1283 
proof  

1284 
assume i: "i < length xs" 

1285 
have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]" 

1286 
by(rule arg_cong[OF id_take_nth_drop[OF i]]) 

1287 
also have "\<dots> = take i xs @ a # drop (Suc i) xs" 

1288 
using i by (simp add: list_update_append) 

1289 
finally show ?thesis . 

1290 
qed 

1291 

13114  1292 

15392  1293 
subsubsection {* @{text takeWhile} and @{text dropWhile} *} 
13114  1294 

13142  1295 
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs" 
13145  1296 
by (induct xs) auto 
13114  1297 

13142  1298 
lemma takeWhile_append1 [simp]: 
13145  1299 
"[ x:set xs; ~P(x)] ==> takeWhile P (xs @ ys) = takeWhile P xs" 
1300 
by (induct xs) auto 

13114  1301 

13142  1302 
lemma takeWhile_append2 [simp]: 
13145  1303 
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys" 
1304 
by (induct xs) auto 

13114  1305 

13142  1306 
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs" 
13145  1307 
by (induct xs) auto 
13114  1308 

13142  1309 
lemma dropWhile_append1 [simp]: 
13145  1310 
"[ x : set xs; ~P(x)] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys" 
1311 
by (induct xs) auto 

13114  1312 

13142  1313 
lemma dropWhile_append2 [simp]: 
13145  1314 
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys" 
1315 
by (induct xs) auto 

13114  1316 

13142  1317 
lemma set_take_whileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x" 
13145  1318 
by (induct xs) (auto split: split_if_asm) 
13114  1319 

13913  1320 
lemma takeWhile_eq_all_conv[simp]: 
1321 
"(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)" 

1322 
by(induct xs, auto) 

1323 

1324 
lemma dropWhile_eq_Nil_conv[simp]: 

1325 
"(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)" 

1326 
by(induct xs, auto) 

1327 

1328 
lemma dropWhile_eq_Cons_conv: 

1329 
"(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)" 

1330 
by(induct xs, auto) 

1331 

17501  1332 
text{* The following two lemmmas could be generalized to an arbitrary 
1333 
property. *} 

1334 

1335 
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> 

1336 
takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))" 

1337 
by(induct xs) (auto simp: takeWhile_tail[where l="[]"]) 

1338 

1339 
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> 

1340 
dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)" 

1341 
apply(induct xs) 

1342 
apply simp 

1343 
apply auto 

1344 
apply(subst dropWhile_append2) 

1345 
apply auto 

1346 
done 

1347 

18423  1348 
lemma takeWhile_not_last: 
1349 
"\<lbrakk> xs \<noteq> []; distinct xs\<rbrakk> \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs" 

1350 
apply(induct xs) 

1351 
apply simp 

1352 
apply(case_tac xs) 

1353 
apply(auto) 

1354 
done 

1355 

19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19623
diff
changeset

1356 
lemma takeWhile_cong [fundef_cong, recdef_cong]: 
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset

1357 
"[ l = k; !!x. x : set l ==> P x = Q x ] 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset

1358 
==> takeWhile P l = takeWhile Q k" 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
