author  haftmann 
Wed, 11 Apr 2007 08:28:14 +0200  
changeset 22633  a47e4fd7ebc1 
parent 22551  e52f5400e331 
child 22793  dc13dfd588b2 
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 
21455  9 
imports PreList 
21754
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
wenzelm
parents:
21548
diff
changeset

10 
uses "Tools/string_syntax.ML" 
15131  11 
begin 
923  12 

13142  13 
datatype 'a list = 
13366  14 
Nil ("[]") 
15 
 Cons 'a "'a list" (infixr "#" 65) 

923  16 

15392  17 
subsection{*Basic list processing functions*} 
15302  18 

923  19 
consts 
13366  20 
"@" :: "'a list => 'a list => 'a list" (infixr 65) 
21 
filter:: "('a => bool) => 'a list => 'a list" 

22 
concat:: "'a list list => 'a list" 

23 
foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b" 

24 
foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b" 

25 
hd:: "'a list => 'a" 

26 
tl:: "'a list => 'a list" 

27 
last:: "'a list => 'a" 

28 
butlast :: "'a list => 'a list" 

29 
set :: "'a list => 'a set" 

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 
"distinct":: "'a list => bool" 
43 
replicate :: "nat => 'a => 'a list" 

19390  44 
splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" 
15302  45 

19363  46 
abbreviation 
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

47 
upto:: "nat => nat => nat list" ("(1[_../_])") where 
19363  48 
"[i..j] == [i..<(Suc j)]" 
19302  49 

923  50 

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

52 

923  53 
syntax 
13366  54 
 {* list Enumeration *} 
55 
"@list" :: "args => 'a list" ("[(_)]") 

923  56 

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

923  59 

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

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

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

64 
"_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

65 

923  66 
translations 
13366  67 
"[x, xs]" == "x#[xs]" 
68 
"[x]" == "x#[]" 

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

923  70 

13366  71 
"_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs" 
72 
"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

73 

5427  74 

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

75 
syntax (xsymbols) 
13366  76 
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])") 
14565  77 
syntax (HTML output) 
78 
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])") 

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

79 

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

80 

13142  81 
text {* 
14589  82 
Function @{text size} is overloaded for all datatypes. Users may 
13366  83 
refer to the list version as @{text length}. *} 
13142  84 

19363  85 
abbreviation 
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

86 
length :: "'a list => nat" where 
19363  87 
"length == size" 
15302  88 

5183  89 
primrec 
15307  90 
"hd(x#xs) = x" 
91 

5183  92 
primrec 
15307  93 
"tl([]) = []" 
94 
"tl(x#xs) = xs" 

95 

5183  96 
primrec 
15307  97 
"last(x#xs) = (if xs=[] then x else last xs)" 
98 

5183  99 
primrec 
15307  100 
"butlast []= []" 
101 
"butlast(x#xs) = (if xs=[] then [] else x#butlast xs)" 

102 

5183  103 
primrec 
15307  104 
"set [] = {}" 
105 
"set (x#xs) = insert x (set xs)" 

106 

5183  107 
primrec 
15307  108 
"map f [] = []" 
109 
"map f (x#xs) = f(x)#map f xs" 

110 

5183  111 
primrec 
21754
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
wenzelm
parents:
21548
diff
changeset

112 
append_Nil: "[]@ys = ys" 
15307  113 
append_Cons: "(x#xs)@ys = x#(xs@ys)" 
114 

5183  115 
primrec 
15307  116 
"rev([]) = []" 
117 
"rev(x#xs) = rev(xs) @ [x]" 

118 

5183  119 
primrec 
15307  120 
"filter P [] = []" 
121 
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" 

122 

5183  123 
primrec 
15307  124 
foldl_Nil:"foldl f a [] = a" 
125 
foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs" 

126 

8000  127 
primrec 
15307  128 
"foldr f [] a = a" 
129 
"foldr f (x#xs) a = f x (foldr f xs a)" 

130 

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

134 

5183  135 
primrec 
15307  136 
drop_Nil:"drop n [] = []" 
137 
drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs  Suc(m) => drop m xs)" 

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

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

140 

5183  141 
primrec 
15307  142 
take_Nil:"take n [] = []" 
143 
take_Cons: "take n (x#xs) = (case n of 0 => []  Suc(m) => x # take m xs)" 

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

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

146 

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

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

151 

5183  152 
primrec 
15307  153 
"[][i:=v] = []" 
154 
"(x#xs)[i:=v] = (case i of 0 => v # xs  Suc j => x # xs[j:=v])" 

155 

156 
primrec 

157 
"takeWhile P [] = []" 

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

159 

5183  160 
primrec 
15307  161 
"dropWhile P [] = []" 
162 
"dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" 

163 

5183  164 
primrec 
15307  165 
"zip xs [] = []" 
166 
zip_Cons: "zip xs (y#ys) = (case xs of [] => []  z#zs => (z,y)#zip zs ys)" 

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

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

169 

5427  170 
primrec 
15425  171 
upt_0: "[i..<0] = []" 
172 
upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])" 

15307  173 

5183  174 
primrec 
15307  175 
"distinct [] = True" 
176 
"distinct (x#xs) = (x ~: set xs \<and> distinct xs)" 

177 

5183  178 
primrec 
15307  179 
"remdups [] = []" 
180 
"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" 

181 

5183  182 
primrec 
15307  183 
"remove1 x [] = []" 
184 
"remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)" 

185 

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

186 
primrec 
15307  187 
replicate_0: "replicate 0 x = []" 
188 
replicate_Suc: "replicate (Suc n) x = x # replicate n x" 

189 

21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

190 
definition 
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

191 
rotate1 :: "'a list \<Rightarrow> 'a list" where 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

192 
"rotate1 xs = (case xs of [] \<Rightarrow> []  x#xs \<Rightarrow> xs @ [x])" 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

193 

eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

194 
definition 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

195 
rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

196 
"rotate n = rotate1 ^ n" 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

197 

eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

198 
definition 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

199 
list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" where 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

200 
"list_all2 P xs ys = 
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

201 
(length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y))" 
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

202 

eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

203 
definition 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

204 
sublist :: "'a list => nat set => 'a list" where 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset

205 
"sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))" 
17086  206 

207 
primrec 

21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

208 
"splice [] ys = ys" 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

209 
"splice (x#xs) ys = (if ys=[] then x#xs else x # hd ys # splice xs (tl ys))" 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

210 
 {*Warning: simpset does not contain the second eqn but a derived one. *} 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

211 

580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

212 

580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

213 
subsubsection {* @{const Nil} and @{const Cons} *} 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

214 

580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

215 
lemma not_Cons_self [simp]: 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

216 
"xs \<noteq> x # xs" 
13145  217 
by (induct xs) auto 
13114  218 

13142  219 
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric] 
13114  220 

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

13142  224 
lemma length_induct: 
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

225 
"(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs" 
17589  226 
by (rule measure_induct [of length]) iprover 
13114  227 

228 

21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

229 
subsubsection {* @{const length} *} 
13114  230 

13142  231 
text {* 
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

232 
Needs to come before @{text "@"} because of theorem @{text 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

233 
append_eq_append_conv}. 
13142  234 
*} 
13114  235 

13142  236 
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" 
13145  237 
by (induct xs) auto 
13114  238 

13142  239 
lemma length_map [simp]: "length (map f xs) = length xs" 
13145  240 
by (induct xs) auto 
13114  241 

13142  242 
lemma length_rev [simp]: "length (rev xs) = length xs" 
13145  243 
by (induct xs) auto 
13114  244 

13142  245 
lemma length_tl [simp]: "length (tl xs) = length xs  1" 
13145  246 
by (cases xs) auto 
13114  247 

13142  248 
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" 
13145  249 
by (induct xs) auto 
13114  250 

13142  251 
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" 
13145  252 
by (induct xs) auto 
13114  253 

254 
lemma length_Suc_conv: 

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

13142  257 

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

14208  260 
apply (induct xs, simp, simp) 
14025  261 
apply blast 
262 
done 

263 

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

20503  266 
apply (induct xs) 
267 
apply auto 

14099  268 
done 
269 

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

272 
P [] []; 

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

274 
\<Longrightarrow> P xs ys" 

275 
apply(induct xs) 

276 
apply simp 

277 
apply(case_tac ys) 

278 
apply simp 

279 
apply(simp) 

280 
done 

13114  281 

22493
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset

282 
lemma list_induct2': 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset

283 
"\<lbrakk> P [] []; 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset

284 
\<And>x xs. P (x#xs) []; 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset

285 
\<And>y ys. P [] (y#ys); 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset

286 
\<And>x xs y ys. P xs ys \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk> 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset

287 
\<Longrightarrow> P xs ys" 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset

288 
by (induct xs arbitrary: ys) (case_tac x, auto)+ 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset

289 

22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

290 
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False" 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

291 
apply(rule Eq_FalseI) 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

292 
by auto 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

293 

cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

294 
(* 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

295 
Reduces xs=ys to False if xs and ys cannot be of the same length. 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

296 
This is the case if the atomic sublists of one are a submultiset 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

297 
of those of the other list and there are fewer Cons's in one than the other. 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

298 
*) 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

299 
ML_setup {* 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

300 
local 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

301 

cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

302 
fun len (Const("List.list.Nil",_)) acc = acc 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

303 
 len (Const("List.list.Cons",_) $ _ $ xs) (ts,n) = len xs (ts,n+1) 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

304 
 len (Const("List.op @",_) $ xs $ ys) acc = len xs (len ys acc) 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

305 
 len (Const("List.rev",_) $ xs) acc = len xs acc 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

306 
 len (Const("List.map",_) $ _ $ xs) acc = len xs acc 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

307 
 len t (ts,n) = (t::ts,n); 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

308 

cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

309 
fun list_eq ss (Const(_,eqT) $ lhs $ rhs) = 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

310 
let 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

311 
val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0); 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

312 
fun prove_neq() = 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

313 
let 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

314 
val Type(_,listT::_) = eqT; 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

315 
val size = Const("Nat.size", listT > HOLogic.natT); 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

316 
val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs); 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

317 
val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len); 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

318 
val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len 
22633  319 
(K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1)); 
320 
in SOME (thm RS @{thm neq_if_length_neq}) end 

22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

321 
in 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

322 
if m < n andalso gen_submultiset (op aconv) (ls,rs) orelse 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

323 
n < m andalso gen_submultiset (op aconv) (rs,ls) 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

324 
then prove_neq() else NONE 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

325 
end; 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

326 

cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

327 
in 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

328 

cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

329 
val list_neq_simproc = 
22633  330 
Simplifier.simproc @{theory} "list_neq" ["(xs::'a list) = ys"] (K list_eq); 
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

331 

cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

332 
end; 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

333 

cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

334 
Addsimprocs [list_neq_simproc]; 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

335 
*} 
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

336 

cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset

337 

15392  338 
subsubsection {* @{text "@"}  append *} 
13114  339 

13142  340 
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" 
13145  341 
by (induct xs) auto 
13114  342 

13142  343 
lemma append_Nil2 [simp]: "xs @ [] = xs" 
13145  344 
by (induct xs) auto 
3507  345 

13142  346 
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" 
13145  347 
by (induct xs) auto 
13114  348 

13142  349 
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])" 
13145  350 
by (induct xs) auto 
13114  351 

13142  352 
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" 
13145  353 
by (induct xs) auto 
13114  354 

13142  355 
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])" 
13145  356 
by (induct xs) auto 
13114  357 

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

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

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

360 
==> (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

361 
apply (induct xs) 
14208  362 
apply (case_tac ys, simp, force) 
363 
apply (case_tac ys, force, simp) 

13145  364 
done 
13142  365 

14495  366 
lemma append_eq_append_conv2: "!!ys zs ts. 
367 
(xs @ ys = zs @ ts) = 

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

369 
apply (induct xs) 

370 
apply fastsimp 

371 
apply(case_tac zs) 

372 
apply simp 

373 
apply fastsimp 

374 
done 

375 

13142  376 
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)" 
13145  377 
by simp 
13142  378 

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

13145  380 
by simp 
13114  381 

13142  382 
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)" 
13145  383 
by simp 
13114  384 

13142  385 
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" 
13145  386 
using append_same_eq [of _ _ "[]"] by auto 
3507  387 

13142  388 
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])" 
13145  389 
using append_same_eq [of "[]"] by auto 
13114  390 

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

13142  394 
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" 
13145  395 
by (induct xs) auto 
13114  396 

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

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

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

406 

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

409 
by(cases ys) auto 

410 

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

413 
by(cases ys) auto 

414 

14300  415 

13142  416 
text {* Trivial rules for solving @{text "@"}equations automatically. *} 
13114  417 

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

13145  419 
by simp 
13114  420 

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

13114  424 

13142  425 
lemma append_eq_appendI: 
13145  426 
"[ xs @ xs1 = zs; ys = xs1 @ us ] ==> xs @ ys = zs @ us" 
427 
by (drule sym) simp 

13114  428 

429 

13142  430 
text {* 
13145  431 
Simplification procedure for all list equalities. 
432 
Currently only tries to rearrange @{text "@"} to see if 

433 
 both lists end in a singleton list, 

434 
 or both lists end in the same list. 

13142  435 
*} 
436 

437 
ML_setup {* 

3507  438 
local 
439 

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

443 
 last t = t; 

13114  444 

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

13462  446 
 list1 _ = false; 
13114  447 

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

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

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

13114  452 

22633  453 
val rearr_ss = HOL_basic_ss addsimps [@{thm append_assoc}, 
454 
@{thm append_Nil}, @{thm append_Cons}]; 

16973  455 

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

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

459 
fun rearr conv = 

460 
let 

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

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

463 
val appT = [listT,listT] > listT 

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

465 
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

466 
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

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

468 
(K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1)); 
15531  469 
in SOME ((conv RS (thm RS trans)) RS eq_reflection) end; 
13114  470 

13462  471 
in 
22633  472 
if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv} 
473 
else if lastl aconv lastr then rearr @{thm append_same_eq} 

15531  474 
else NONE 
13462  475 
end; 
476 

13114  477 
in 
13462  478 

479 
val list_eq_simproc = 

22633  480 
Simplifier.simproc @{theory} "list_eq" ["(xs::'a list) = ys"] (K list_eq); 
13462  481 

13114  482 
end; 
483 

484 
Addsimprocs [list_eq_simproc]; 

485 
*} 

486 

487 

15392  488 
subsubsection {* @{text map} *} 
13114  489 

13142  490 
lemma map_ext: "(!!x. x : set xs > f x = g x) ==> map f xs = map g xs" 
13145  491 
by (induct xs) simp_all 
13114  492 

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

13142  496 
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" 
13145  497 
by (induct xs) auto 
13114  498 

13142  499 
lemma map_compose: "map (f o g) xs = map f (map g xs)" 
13145  500 
by (induct xs) (auto simp add: o_def) 
13114  501 

13142  502 
lemma rev_map: "rev (map f xs) = map f (rev xs)" 
13145  503 
by (induct xs) auto 
13114  504 

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

507 

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

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

13737  511 
by simp 
13114  512 

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

13142  516 
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])" 
13145  517 
by (cases xs) auto 
13114  518 

18447  519 
lemma map_eq_Cons_conv: 
14025  520 
"(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" 
13145  521 
by (cases xs) auto 
13114  522 

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

526 

18447  527 
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1] 
528 
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1] 

529 
declare map_eq_Cons_D [dest!] Cons_eq_map_D [dest!] 

530 

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

18447  533 
by(induct ys, auto simp add: Cons_eq_map_conv) 
14111  534 

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

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

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

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

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

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

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

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

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

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

544 

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

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

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

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

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

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

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

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

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

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

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

555 

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

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

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

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

559 

13114  560 
lemma map_injective: 
14338  561 
"!!xs. map f xs = map f ys ==> inj f ==> xs = ys" 
562 
by (induct ys) (auto dest!:injD) 

13114  563 

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

566 

13114  567 
lemma inj_mapI: "inj f ==> inj (map f)" 
17589  568 
by (iprover dest: map_injective injD intro: inj_onI) 
13114  569 

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

14208  571 
apply (unfold inj_on_def, clarify) 
13145  572 
apply (erule_tac x = "[x]" in ballE) 
14208  573 
apply (erule_tac x = "[y]" in ballE, simp, blast) 
13145  574 
apply blast 
575 
done 

13114  576 

14339  577 
lemma inj_map[iff]: "inj (map f) = inj f" 
13145  578 
by (blast dest: inj_mapD intro: inj_mapI) 
13114  579 

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

582 
apply(erule map_inj_on) 

583 
apply(blast intro:inj_onI dest:inj_onD) 

584 
done 

585 

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

13114  588 

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

589 
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

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

591 

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

592 
lemma map_fst_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 fst (zip xs ys) = xs" 
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 
lemma map_snd_zip[simp]: 
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset

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

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

599 

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

600 

15392  601 
subsubsection {* @{text rev} *} 
13114  602 

13142  603 
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" 
13145  604 
by (induct xs) auto 
13114  605 

13142  606 
lemma rev_rev_ident [simp]: "rev (rev xs) = xs" 
13145  607 
by (induct xs) auto 
13114  608 

15870  609 
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)" 
610 
by auto 

611 

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

13142  615 
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])" 
13145  616 
by (induct xs) auto 
13114  617 

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

620 

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

622 
by (cases xs) auto 

623 

21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

624 
lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)" 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset

625 
apply (induct xs arbitrary: ys, force) 
14208  626 
apply (case_tac ys, simp, force) 
13145  627 
done 
13114  628 

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

631 

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

634 
apply(simplesubst rev_rev_ident[symmetric]) 
13145  635 
apply(rule_tac list = "rev xs" in list.induct, simp_all) 
636 
done 

13114  637 

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

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

13145  642 
by (induct xs rule: rev_induct) auto 
13114  643 

13366  644 
lemmas rev_cases = rev_exhaust 
645 

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

648 

13114  649 

15392  650 
subsubsection {* @{text set} *} 
13114  651 

13142  652 
lemma finite_set [iff]: "finite (set xs)" 
13145  653 
by (induct xs) auto 
13114  654 

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

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

14099  660 

13142  661 
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" 
13145  662 
by auto 
13114  663 

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

666 

13142  667 
lemma set_empty [iff]: "(set xs = {}) = (xs = [])" 
13145  668 
by (induct xs) auto 
13114  669 

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

672 

13142  673 
lemma set_rev [simp]: "set (rev xs) = set xs" 
13145  674 
by (induct xs) auto 
13114  675 

13142  676 
lemma set_map [simp]: "set (map f xs) = f`(set xs)" 
13145  677 
by (induct xs) auto 
13114  678 

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

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

13145  685 
done 
13114  686 

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

690 
case (Cons a xs) 

691 
show ?case 

692 
proof 

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

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

695 
by (simp, blast intro: Cons_eq_appendI) 

696 
next 

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

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

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

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

701 
qed 

702 
qed 

13142  703 

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

706 
proof (induct xs) 

707 
case Nil show ?case by simp 

708 
next 

709 
case (Cons a xs) 

710 
show ?case 

711 
proof cases 

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

713 
next 

714 
assume "x \<noteq> a" 

715 
show ?case 

716 
proof 

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

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

719 
by(fastsimp intro!: Cons_eq_appendI) 

720 
next 

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

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

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

724 
qed 

725 
qed 

726 
qed 

727 

728 
lemmas split_list = in_set_conv_decomp[THEN iffD1, standard] 

729 
lemmas split_list_first = in_set_conv_decomp_first[THEN iffD1, standard] 

730 

731 

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

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

735 
done 

736 

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

13114  739 

15168  740 

15392  741 
subsubsection {* @{text filter} *} 
13114  742 

13142  743 
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" 
13145  744 
by (induct xs) auto 
13114  745 

15305  746 
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)" 
747 
by (induct xs) simp_all 

748 

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

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

754 

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

757 
by(induct xs) simp_all 

758 

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

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

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

767 

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

769 
apply (induct xs) 

770 
apply auto 

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

772 
apply simp 

773 
done 

13114  774 

16965  775 
lemma filter_map: 
776 
"filter P (map f xs) = map f (filter (P o f) xs)" 

777 
by (induct xs) simp_all 

778 

779 
lemma length_filter_map[simp]: 

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

781 
by (simp add:filter_map) 

782 

13142  783 
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" 
13145  784 
by auto 
13114  785 

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

788 
proof (induct xs) 

789 
case Nil thus ?case by simp 

790 
next 

791 
case (Cons x xs) thus ?case 

792 
apply (auto split:split_if_asm) 

793 
using length_filter_le[of P xs] apply arith 

794 
done 

795 
qed 

13114  796 

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

799 
proof (induct xs) 

800 
case Nil thus ?case by simp 

801 
next 

802 
case (Cons x xs) 

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

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

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

806 
proof (cases) 

807 
assume "p x" 

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

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

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

811 
using Cons by simp 

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

813 
by (simp add: card_image inj_Suc) 

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

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

816 
finally show ?thesis . 

817 
next 

818 
assume "\<not> p x" 

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

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

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

822 
using Cons by simp 

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

824 
by (simp add: card_image inj_Suc) 

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

826 
by (simp add:card_insert_if) 

827 
finally show ?thesis . 

828 
qed 

829 
qed 

830 

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

833 
\<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  834 
(is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs") 
17629  835 
proof(induct ys) 
836 
case Nil thus ?case by simp 

837 
next 

838 
case (Cons y ys) 

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

840 
proof cases 

841 
assume Py: "P y" 

842 
show ?thesis 

843 
proof cases 

844 
assume xy: "x = y" 

845 
show ?thesis 

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

847 
next 

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

849 
qed 

850 
next 

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

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

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

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

855 
qed 

856 
qed 

857 

858 
lemma filter_eq_ConsD: 

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

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

861 
by(rule Cons_eq_filterD) simp 

862 

863 
lemma filter_eq_Cons_iff: 

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

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

866 
by(auto dest:filter_eq_ConsD) 

867 

868 
lemma Cons_eq_filter_iff: 

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

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

871 
by(auto dest:Cons_eq_filterD) 

872 

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

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

876 
apply(erule thin_rl) 

877 
by (induct ys) simp_all 

878 

15281  879 

15392  880 
subsubsection {* @{text concat} *} 
13114  881 

13142  882 
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" 
13145  883 
by (induct xs) auto 
13114  884 

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

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

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

13142  894 
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" 
13145  895 
by (induct xs) auto 
13114  896 

13142  897 
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" 
13145  898 
by (induct xs) auto 
13114  899 

13142  900 
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" 
13145  901 
by (induct xs) auto 
13114  902 

903 

15392  904 
subsubsection {* @{text nth} *} 
13114  905 

13142  906 
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x" 
13145  907 
by auto 
13114  908 

13142  909 
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n" 
13145  910 
by auto 
13114  911 

13142  912 
declare nth.simps [simp del] 
13114  913 

914 
lemma nth_append: 

13145  915 
"!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n  length xs))" 
14208  916 
apply (induct "xs", simp) 
917 
apply (case_tac n, auto) 

13145  918 
done 
13114  919 

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

920 
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

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

922 

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

923 
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

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

925 

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

13145  929 
done 
13114  930 

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

933 

18049  934 

935 
lemma list_eq_iff_nth_eq: 

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

937 
apply(induct xs) 

938 
apply simp apply blast 

939 
apply(case_tac ys) 

940 
apply simp 

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

942 
done 

943 

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

949 
apply (case_tac i, simp) 

13145  950 
apply (rename_tac j) 
14208  951 
apply (rule_tac x = j in exI, simp) 
13145  952 
done 
13114  953 

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

956 

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

13114  959 

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

963 
lemma all_nth_imp_all_set: 

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

13114  966 

967 
lemma all_set_conv_all_nth: 

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

13114  970 

971 

15392  972 
subsubsection {* @{text list_update} *} 
13114  973 

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

977 
lemma nth_list_update: 

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

13114  980 

13142  981 
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x" 
13145  982 
by (simp add: nth_list_update) 
13114  983 

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

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

13114  990 

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

991 
lemma list_update_id[simp]: "!!i. i < length xs ==> xs[i := xs!i] = xs" 
14208  992 
apply (induct xs, simp) 
14187  993 
apply(simp split:nat.splits) 
994 
done 

995 

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

998 
apply simp 

999 
apply (case_tac i) 

1000 
apply simp_all 

1001 
done 

1002 

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

13114  1006 

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

14208  1009 
apply (induct xs, simp) 
14187  1010 
apply(simp split:nat.split) 
1011 
done 

1012 

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

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

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

1017 

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

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

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

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

1021 

13114  1022 
lemma update_zip: 
13145  1023 
"!!i xy xs. length xs = length ys ==> 
1024 
(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" 

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

13114  1026 

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

13145  1028 
by (induct xs) (auto split: nat.split) 
13114  1029 

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

13145  1031 
by (blast dest!: set_update_subset_insert [THEN subsetD]) 
13114  1032 

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

1035 

13114  1036 

15392  1037 
subsubsection {* @{text last} and @{text butlast} *} 
13114  1038 

13142  1039 
lemma last_snoc [simp]: "last (xs @ [x]) = x" 
13145  1040 
by (induct xs) auto 
13114  1041 

13142  1042 
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" 
13145  1043 
by (induct xs) auto 
13114  1044 

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

1047 

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

1049 
by(simp add:last.simps) 

1050 

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

1052 
by (induct xs) (auto) 

1053 

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

1055 
by(simp add:last_append) 

1056 

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

1058 
by(simp add:last_append) 

1059 

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

1062 

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

1064 
by(cases xs) simp_all 

1065 

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

17762  1068 

13142  1069 
lemma length_butlast [simp]: "length (butlast xs) = length xs  1" 
13145  1070 
by (induct xs rule: rev_induct) auto 
13114  1071 

1072 
lemma butlast_append: 

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

13114  1075 

13142  1076 
lemma append_butlast_last_id [simp]: 
13145  1077 
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs" 
1078 
by (induct xs) auto 

13114  1079 

13142  1080 
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs" 
13145  1081 
by (induct xs) (auto split: split_if_asm) 
13114  1082 

1083 
lemma in_set_butlast_appendI: 

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

13114  1086 

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

1089 
apply simp 

1090 
apply (auto split:nat.split) 

1091 
done 

1092 

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

1095 

15392  1096 
subsubsection {* @{text take} and @{text drop} *} 
13114  1097 

13142  1098 
lemma take_0 [simp]: "take 0 xs = []" 
13145  1099 
by (induct xs) auto 
13114  1100 

13142  1101 
lemma drop_0 [simp]: "drop 0 xs = xs" 
13145  1102 
by (induct xs) auto 
13114  1103 

13142  1104 
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" 
13145  1105 
by simp 
13114  1106 

13142  1107 
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" 
13145  1108 
by simp 
13114  1109 

13142  1110 
declare take_Cons [simp del] and drop_Cons [simp del] 
13114  1111 

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

1112 
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

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

1114 

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

1117 

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

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

1120 

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

14208  1122 
apply (induct xs, simp) 
14187  1123 
apply(simp add:drop_Cons nth_Cons split:nat.splits) 
1124 
done 

1125 

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

14208  1128 
apply (induct xs, simp) 
1129 
apply (case_tac i, auto) 

13913  1130 
done 
1131 

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

1134 
apply (induct xs, simp) 

1135 
apply (case_tac i, auto) 

1136 
done 

1137 

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

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

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

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

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

13114  1153 

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

13114  1157 

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

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

1161 
apply (case_tac n, auto) 
13145  1162 
done 
13114  1163 

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

13145  1167 
done 
13114  1168 

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

14208  1170 
apply (induct m, auto) 
1171 
apply (case_tac xs, auto) 

13145  1172 
done 
13114  1173 

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

1176 
apply simp 

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

1178 
done 

1179 

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

13145  1183 
done 
13114  1184 

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

1185 
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

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

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

1188 
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

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

1190 

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

1191 
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

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

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

1194 
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

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

1196 

13114  1197 
lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)" 
14208  1198 
apply (induct n, auto) 
1199 
apply (case_tac xs, auto) 

13145  1200 
done 
13114  1201 

13142  1202 
lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)" 
14208  1203 
apply (induct n, auto) 
1204 
apply (case_tac xs, auto) 

13145  1205 
done 
13114  1206 

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

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

13145  1210 
done 
13114  1211 

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

14208  1213 
apply (induct xs, auto) 
1214 
apply (case_tac i, auto) 

13145  1215 
done 
13114  1216 

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

1220 
apply (case_tac i, auto) 

13145  1221 
done 
13114  1222 

13142  1223 
lemma nth_drop [simp]: 
13145  1224 
"!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)" 
14208  1225 
apply (induct n, auto) 
1226 
apply (case_tac xs, auto) 

13145  1227 
done 
3507  1228 

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

1231 

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

1234 

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

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

1237 

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

1240 

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

1242 
using set_drop_subset by fast 

1243 

13114  1244 
lemma append_eq_conv_conj: 
13145  1245 
"!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)" 
14208  1246 
apply (induct xs, simp, clarsimp) 
1247 
apply (case_tac zs, auto) 

13145  1248 
done 
13142  1249 

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

1252 
apply (induct xs, auto) 

1253 
apply (case_tac i, simp_all) 

1254 
done 

1255 

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

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

1259 
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 

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

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

1262 
apply simp 

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

1264 
apply simp_all 

1265 
done 

1266 

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

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

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

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

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

1271 
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

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

1273 

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

1276 
proof  

1277 
assume si: "i < length xs" 

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

1279 
moreover 

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

1281 
apply (rule_tac take_Suc_conv_app_nth) by arith 

1282 
ultimately show ?thesis by auto 

1283 
qed 

1284 

1285 
lemma upd_conv_take_nth_drop: 

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

1287 
proof  

1288 
assume i: "i < length xs" 

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

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

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

1292 
using i by (simp add: list_update_append) 

1293 
finally show ?thesis . 

1294 
qed 

1295 

13114  1296 

15392  1297 
subsubsection {* @{text takeWhile} and @{text dropWhile} *} 
13114  1298 

13142  1299 
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs" 
13145  1300 
by (induct xs) auto 
13114  1301 

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

13114  1305 

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

13114  1309 

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

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

13114  1316 

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

13114  1320 

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

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

1326 
by(induct xs, auto) 

1327 

1328 
lemma dropWhile_eq_Nil_conv[simp]: 

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

1330 
by(induct xs, auto) 

1331 

1332 
lemma dropWhile_eq_Cons_conv: 

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

1334 
by(induct xs, auto) 

1335 

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

1338 

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

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

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

1342 

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

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

1345 
apply(induct xs) 

1346 
apply simp 

1347 
apply auto 

1348 
apply(subst dropWhile_append2) 

1349 
apply auto 

1350 
done 

1351 

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

1354 
apply(induct xs) 

1355 
apply simp 

1356 
apply(case_tac xs) 

1357 
apply(auto) 
