author  nipkow 
Wed, 14 May 2003 10:22:09 +0200  
changeset 14025  d9b155757dc8 
parent 13913  b3ed67af04b8 
child 14050  826037db30cd 
permissions  rwrr 
13462  1 
(* Title: HOL/List.thy 
2 
ID: $Id$ 

3 
Author: Tobias Nipkow 

4 
License: GPL (GNU GENERAL PUBLIC LICENSE) 

923  5 
*) 
6 

13114  7 
header {* The datatype of finite lists *} 
13122  8 

9 
theory List = PreList: 

923  10 

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

923  14 

15 
consts 

13366  16 
"@" :: "'a list => 'a list => 'a list" (infixr 65) 
17 
filter:: "('a => bool) => 'a list => 'a list" 

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

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

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

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

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

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

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

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

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

27 
list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" 

28 
map :: "('a=>'b) => ('a list => 'b list)" 

29 
mem :: "'a => 'a list => bool" (infixl 55) 

30 
nth :: "'a list => nat => 'a" (infixl "!" 100) 

31 
list_update :: "'a list => nat => 'a => 'a list" 

32 
take:: "nat => 'a list => 'a list" 

33 
drop:: "nat => 'a list => 'a list" 

34 
takeWhile :: "('a => bool) => 'a list => 'a list" 

35 
dropWhile :: "('a => bool) => 'a list => 'a list" 

36 
rev :: "'a list => 'a list" 

37 
zip :: "'a list => 'b list => ('a * 'b) list" 

38 
upt :: "nat => nat => nat list" ("(1[_../_'(])") 

39 
remdups :: "'a list => 'a list" 

40 
null:: "'a list => bool" 

41 
"distinct":: "'a list => bool" 

42 
replicate :: "nat => 'a => 'a list" 

923  43 

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

45 

923  46 
syntax 
13366  47 
 {* list Enumeration *} 
48 
"@list" :: "args => 'a list" ("[(_)]") 

923  49 

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

923  52 

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

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

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

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

58 

13366  59 
upto:: "nat => nat => nat list" ("(1[_../_])") 
5427  60 

923  61 
translations 
13366  62 
"[x, xs]" == "x#[xs]" 
63 
"[x]" == "x#[]" 

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

923  65 

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

68 

13366  69 
"[i..j]" == "[i..(Suc j)(]" 
5427  70 

71 

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

72 
syntax (xsymbols) 
13366  73 
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])") 
3342
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset

74 

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

75 

13142  76 
text {* 
13366  77 
Function @{text size} is overloaded for all datatypes.Users may 
78 
refer to the list version as @{text length}. *} 

13142  79 

80 
syntax length :: "'a list => nat" 

81 
translations "length" => "size :: _ list => nat" 

13114  82 

13142  83 
typed_print_translation {* 
13366  84 
let 
85 
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] = 

86 
Syntax.const "length" $ t 

87 
 size_tr' _ _ _ = raise Match; 

88 
in [("size", size_tr')] end 

13114  89 
*} 
3437
bea2faf1641d
Replacing the primrec definition of "length" by a translation to the builtin
paulson
parents:
3401
diff
changeset

90 

5183  91 
primrec 
13145  92 
"hd(x#xs) = x" 
5183  93 
primrec 
13145  94 
"tl([]) = []" 
95 
"tl(x#xs) = xs" 

5183  96 
primrec 
13145  97 
"null([]) = True" 
98 
"null(x#xs) = False" 

8972  99 
primrec 
13145  100 
"last(x#xs) = (if xs=[] then x else last xs)" 
5183  101 
primrec 
13145  102 
"butlast []= []" 
103 
"butlast(x#xs) = (if xs=[] then [] else x#butlast xs)" 

5183  104 
primrec 
13145  105 
"x mem [] = False" 
106 
"x mem (y#ys) = (if y=x then True else x mem ys)" 

5518  107 
primrec 
13145  108 
"set [] = {}" 
109 
"set (x#xs) = insert x (set xs)" 

5183  110 
primrec 
13145  111 
list_all_Nil:"list_all P [] = True" 
112 
list_all_Cons: "list_all P (x#xs) = (P(x) \<and> list_all P xs)" 

5518  113 
primrec 
13145  114 
"map f [] = []" 
115 
"map f (x#xs) = f(x)#map f xs" 

5183  116 
primrec 
13145  117 
append_Nil:"[]@ys = ys" 
118 
append_Cons: "(x#xs)@ys = x#(xs@ys)" 

5183  119 
primrec 
13145  120 
"rev([]) = []" 
121 
"rev(x#xs) = rev(xs) @ [x]" 

5183  122 
primrec 
13145  123 
"filter P [] = []" 
124 
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" 

5183  125 
primrec 
13145  126 
foldl_Nil:"foldl f a [] = a" 
127 
foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs" 

5183  128 
primrec 
13145  129 
"foldr f [] a = a" 
130 
"foldr f (x#xs) a = f x (foldr f xs a)" 

8000  131 
primrec 
13145  132 
"concat([]) = []" 
133 
"concat(x#xs) = x @ concat(xs)" 

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

137 
 {* Warning: simpset does not contain this definition *} 

138 
 {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *} 

5183  139 
primrec 
13145  140 
take_Nil:"take n [] = []" 
141 
take_Cons: "take n (x#xs) = (case n of 0 => []  Suc(m) => x # take m xs)" 

142 
 {* Warning: simpset does not contain this definition *} 

143 
 {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *} 

5183  144 
primrec 
13145  145 
nth_Cons:"(x#xs)!n = (case n of 0 => x  (Suc k) => xs!k)" 
146 
 {* Warning: simpset does not contain this definition *} 

147 
 {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *} 

13142  148 
primrec 
13145  149 
"[][i:=v] = []" 
150 
"(x#xs)[i:=v] = 

151 
(case i of 0 => v # xs 

152 
 Suc j => x # xs[j:=v])" 

5183  153 
primrec 
13145  154 
"takeWhile P [] = []" 
155 
"takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" 

5183  156 
primrec 
13145  157 
"dropWhile P [] = []" 
158 
"dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" 

5183  159 
primrec 
13145  160 
"zip xs [] = []" 
161 
zip_Cons: "zip xs (y#ys) = (case xs of [] => []  z#zs => (z,y)#zip zs ys)" 

162 
 {* Warning: simpset does not contain this definition *} 

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

5427  164 
primrec 
13145  165 
upt_0: "[i..0(] = []" 
166 
upt_Suc: "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])" 

5183  167 
primrec 
13145  168 
"distinct [] = True" 
169 
"distinct (x#xs) = (x ~: set xs \<and> distinct xs)" 

5183  170 
primrec 
13145  171 
"remdups [] = []" 
172 
"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" 

5183  173 
primrec 
13147  174 
replicate_0: "replicate 0 x = []" 
13145  175 
replicate_Suc: "replicate (Suc n) x = x # replicate n x" 
8115  176 
defs 
13114  177 
list_all2_def: 
13142  178 
"list_all2 P xs ys == length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y)" 
8115  179 

3196  180 

13142  181 
subsection {* Lexicographic orderings on lists *} 
5281  182 

183 
consts 

13145  184 
lexn :: "('a * 'a)set => nat => ('a list * 'a list)set" 
5281  185 
primrec 
13145  186 
"lexn r 0 = {}" 
187 
"lexn r (Suc n) = 

188 
(prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int 

189 
{(xs,ys). length xs = Suc n \<and> length ys = Suc n}" 

5281  190 

191 
constdefs 

13145  192 
lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" 
193 
"lex r == \<Union>n. lexn r n" 

5281  194 

13145  195 
lexico :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" 
196 
"lexico r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))" 

9336  197 

13145  198 
sublist :: "'a list => nat set => 'a list" 
199 
"sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..size xs(]))" 

5281  200 

13114  201 

13142  202 
lemma not_Cons_self [simp]: "xs \<noteq> x # xs" 
13145  203 
by (induct xs) auto 
13114  204 

13142  205 
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric] 
13114  206 

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

13142  210 
lemma length_induct: 
13145  211 
"(!!xs. \<forall>ys. length ys < length xs > P ys ==> P xs) ==> P xs" 
212 
by (rule measure_induct [of length]) rules 

13114  213 

214 

13142  215 
subsection {* @{text lists}: the listforming operator over sets *} 
13114  216 

13142  217 
consts lists :: "'a set => 'a list set" 
218 
inductive "lists A" 

13145  219 
intros 
220 
Nil [intro!]: "[]: lists A" 

221 
Cons [intro!]: "[ a: A;l: lists A] ==> a#l : lists A" 

13114  222 

13142  223 
inductive_cases listsE [elim!]: "x#l : lists A" 
13114  224 

13366  225 
lemma lists_mono [mono]: "A \<subseteq> B ==> lists A \<subseteq> lists B" 
13145  226 
by (unfold lists.defs) (blast intro!: lfp_mono) 
13114  227 

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

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

229 
assumes l: "l: lists A" shows "l: lists B ==> l: lists (A Int B)" using l 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

230 
by induct blast+ 
13142  231 

232 
lemma lists_Int_eq [simp]: "lists (A \<inter> B) = lists A \<inter> lists B" 

13145  233 
apply (rule mono_Int [THEN equalityI]) 
234 
apply (simp add: mono_def lists_mono) 

235 
apply (blast intro!: lists_IntI) 

236 
done 

13114  237 

13142  238 
lemma append_in_lists_conv [iff]: 
13145  239 
"(xs @ ys : lists A) = (xs : lists A \<and> ys : lists A)" 
240 
by (induct xs) auto 

13142  241 

242 

243 
subsection {* @{text length} *} 

13114  244 

13142  245 
text {* 
13145  246 
Needs to come before @{text "@"} because of theorem @{text 
247 
append_eq_append_conv}. 

13142  248 
*} 
13114  249 

13142  250 
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" 
13145  251 
by (induct xs) auto 
13114  252 

13142  253 
lemma length_map [simp]: "length (map f xs) = length xs" 
13145  254 
by (induct xs) auto 
13114  255 

13142  256 
lemma length_rev [simp]: "length (rev xs) = length xs" 
13145  257 
by (induct xs) auto 
13114  258 

13142  259 
lemma length_tl [simp]: "length (tl xs) = length xs  1" 
13145  260 
by (cases xs) auto 
13114  261 

13142  262 
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" 
13145  263 
by (induct xs) auto 
13114  264 

13142  265 
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" 
13145  266 
by (induct xs) auto 
13114  267 

268 
lemma length_Suc_conv: 

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

13142  271 

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

274 
apply (induct xs) 

275 
apply simp 

276 
apply simp 

277 
apply blast 

278 
done 

279 

13114  280 

13142  281 
subsection {* @{text "@"}  append *} 
13114  282 

13142  283 
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" 
13145  284 
by (induct xs) auto 
13114  285 

13142  286 
lemma append_Nil2 [simp]: "xs @ [] = xs" 
13145  287 
by (induct xs) auto 
3507  288 

13142  289 
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" 
13145  290 
by (induct xs) auto 
13114  291 

13142  292 
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])" 
13145  293 
by (induct xs) auto 
13114  294 

13142  295 
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" 
13145  296 
by (induct xs) auto 
13114  297 

13142  298 
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])" 
13145  299 
by (induct xs) auto 
13114  300 

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

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

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

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

304 
apply (induct xs) 
13145  305 
apply (case_tac ys) 
306 
apply simp 

307 
apply force 

308 
apply (case_tac ys) 

309 
apply force 

310 
apply simp 

311 
done 

13142  312 

313 
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)" 

13145  314 
by simp 
13142  315 

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

13145  317 
by simp 
13114  318 

13142  319 
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)" 
13145  320 
by simp 
13114  321 

13142  322 
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" 
13145  323 
using append_same_eq [of _ _ "[]"] by auto 
3507  324 

13142  325 
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])" 
13145  326 
using append_same_eq [of "[]"] by auto 
13114  327 

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

13142  331 
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" 
13145  332 
by (induct xs) auto 
13114  333 

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

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

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

343 

13142  344 
text {* Trivial rules for solving @{text "@"}equations automatically. *} 
13114  345 

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

13145  347 
by simp 
13114  348 

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

13114  352 

13142  353 
lemma append_eq_appendI: 
13145  354 
"[ xs @ xs1 = zs; ys = xs1 @ us ] ==> xs @ ys = zs @ us" 
355 
by (drule sym) simp 

13114  356 

357 

13142  358 
text {* 
13145  359 
Simplification procedure for all list equalities. 
360 
Currently only tries to rearrange @{text "@"} to see if 

361 
 both lists end in a singleton list, 

362 
 or both lists end in the same list. 

13142  363 
*} 
364 

365 
ML_setup {* 

3507  366 
local 
367 

13122  368 
val append_assoc = thm "append_assoc"; 
369 
val append_Nil = thm "append_Nil"; 

370 
val append_Cons = thm "append_Cons"; 

371 
val append1_eq_conv = thm "append1_eq_conv"; 

372 
val append_same_eq = thm "append_same_eq"; 

373 

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

377 
 last t = t; 

13114  378 

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

13462  380 
 list1 _ = false; 
13114  381 

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

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

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

13114  386 

387 
val rearr_tac = 

13462  388 
simp_tac (HOL_basic_ss addsimps [append_assoc, append_Nil, append_Cons]); 
13114  389 

390 
fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) = 

13462  391 
let 
392 
val lastl = last lhs and lastr = last rhs; 

393 
fun rearr conv = 

394 
let 

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

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

397 
val appT = [listT,listT] > listT 

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

399 
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

400 
val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2)); 
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset

401 
val thm = Tactic.prove sg [] [] eq (K (rearr_tac 1)); 
13462  402 
in Some ((conv RS (thm RS trans)) RS eq_reflection) end; 
13114  403 

13462  404 
in 
405 
if list1 lastl andalso list1 lastr then rearr append1_eq_conv 

406 
else if lastl aconv lastr then rearr append_same_eq 

407 
else None 

408 
end; 

409 

13114  410 
in 
13462  411 

412 
val list_eq_simproc = 

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

414 

13114  415 
end; 
416 

417 
Addsimprocs [list_eq_simproc]; 

418 
*} 

419 

420 

13142  421 
subsection {* @{text map} *} 
13114  422 

13142  423 
lemma map_ext: "(!!x. x : set xs > f x = g x) ==> map f xs = map g xs" 
13145  424 
by (induct xs) simp_all 
13114  425 

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

13142  429 
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" 
13145  430 
by (induct xs) auto 
13114  431 

13142  432 
lemma map_compose: "map (f o g) xs = map f (map g xs)" 
13145  433 
by (induct xs) (auto simp add: o_def) 
13114  434 

13142  435 
lemma rev_map: "rev (map f xs) = map f (rev xs)" 
13145  436 
by (induct xs) auto 
13114  437 

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

440 

13366  441 
lemma map_cong [recdef_cong]: 
13145  442 
"xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys" 
443 
 {* a congruence rule for @{text map} *} 

13737  444 
by simp 
13114  445 

13142  446 
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])" 
13145  447 
by (cases xs) auto 
13114  448 

13142  449 
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])" 
13145  450 
by (cases xs) auto 
13114  451 

14025  452 
lemma map_eq_Cons_conv[iff]: 
453 
"(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" 

13145  454 
by (cases xs) auto 
13114  455 

14025  456 
lemma Cons_eq_map_conv[iff]: 
457 
"(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)" 

458 
by (cases ys) auto 

459 

13114  460 
lemma map_injective: 
14025  461 
"!!xs. map f xs = map f ys ==> (\<forall>x y. f x = f y > x = y) ==> xs = ys" 
462 
by (induct ys) auto 

13114  463 

464 
lemma inj_mapI: "inj f ==> inj (map f)" 

13585  465 
by (rules dest: map_injective injD intro: inj_onI) 
13114  466 

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

13145  468 
apply (unfold inj_on_def) 
469 
apply clarify 

470 
apply (erule_tac x = "[x]" in ballE) 

471 
apply (erule_tac x = "[y]" in ballE) 

472 
apply simp 

473 
apply blast 

474 
apply blast 

475 
done 

13114  476 

477 
lemma inj_map: "inj (map f) = inj f" 

13145  478 
by (blast dest: inj_mapD intro: inj_mapI) 
13114  479 

480 

13142  481 
subsection {* @{text rev} *} 
13114  482 

13142  483 
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" 
13145  484 
by (induct xs) auto 
13114  485 

13142  486 
lemma rev_rev_ident [simp]: "rev (rev xs) = xs" 
13145  487 
by (induct xs) auto 
13114  488 

13142  489 
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])" 
13145  490 
by (induct xs) auto 
13114  491 

13142  492 
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])" 
13145  493 
by (induct xs) auto 
13114  494 

13142  495 
lemma rev_is_rev_conv [iff]: "!!ys. (rev xs = rev ys) = (xs = ys)" 
13145  496 
apply (induct xs) 
497 
apply force 

498 
apply (case_tac ys) 

499 
apply simp 

500 
apply force 

501 
done 

13114  502 

13366  503 
lemma rev_induct [case_names Nil snoc]: 
504 
"[ P []; !!x xs. P xs ==> P (xs @ [x]) ] ==> P xs" 

13145  505 
apply(subst rev_rev_ident[symmetric]) 
506 
apply(rule_tac list = "rev xs" in list.induct, simp_all) 

507 
done 

13114  508 

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

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

13145  513 
by (induct xs rule: rev_induct) auto 
13114  514 

13366  515 
lemmas rev_cases = rev_exhaust 
516 

13114  517 

13142  518 
subsection {* @{text set} *} 
13114  519 

13142  520 
lemma finite_set [iff]: "finite (set xs)" 
13145  521 
by (induct xs) auto 
13114  522 

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

13142  526 
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" 
13145  527 
by auto 
13114  528 

13142  529 
lemma set_empty [iff]: "(set xs = {}) = (xs = [])" 
13145  530 
by (induct xs) auto 
13114  531 

13142  532 
lemma set_rev [simp]: "set (rev xs) = set xs" 
13145  533 
by (induct xs) auto 
13114  534 

13142  535 
lemma set_map [simp]: "set (map f xs) = f`(set xs)" 
13145  536 
by (induct xs) auto 
13114  537 

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

13142  541 
lemma set_upt [simp]: "set[i..j(] = {k. i \<le> k \<and> k < j}" 
13145  542 
apply (induct j) 
543 
apply simp_all 

544 
apply(erule ssubst) 

545 
apply auto 

546 
done 

13114  547 

13142  548 
lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)" 
13145  549 
apply (induct xs) 
550 
apply simp 

551 
apply simp 

552 
apply (rule iffI) 

553 
apply (blast intro: eq_Nil_appendI Cons_eq_appendI) 

554 
apply (erule exE)+ 

555 
apply (case_tac ys) 

556 
apply auto 

557 
done 

13142  558 

559 
lemma in_lists_conv_set: "(xs : lists A) = (\<forall>x \<in> set xs. x : A)" 

13145  560 
 {* eliminate @{text lists} in favour of @{text set} *} 
561 
by (induct xs) auto 

13142  562 

563 
lemma in_listsD [dest!]: "xs \<in> lists A ==> \<forall>x\<in>set xs. x \<in> A" 

13145  564 
by (rule in_lists_conv_set [THEN iffD1]) 
13142  565 

566 
lemma in_listsI [intro!]: "\<forall>x\<in>set xs. x \<in> A ==> xs \<in> lists A" 

13145  567 
by (rule in_lists_conv_set [THEN iffD2]) 
13114  568 

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

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

572 
done 

573 

13114  574 

13142  575 
subsection {* @{text mem} *} 
13114  576 

577 
lemma set_mem_eq: "(x mem xs) = (x : set xs)" 

13145  578 
by (induct xs) auto 
13114  579 

580 

13142  581 
subsection {* @{text list_all} *} 
13114  582 

13142  583 
lemma list_all_conv: "list_all P xs = (\<forall>x \<in> set xs. P x)" 
13145  584 
by (induct xs) auto 
13114  585 

13142  586 
lemma list_all_append [simp]: 
13145  587 
"list_all P (xs @ ys) = (list_all P xs \<and> list_all P ys)" 
588 
by (induct xs) auto 

13114  589 

590 

13142  591 
subsection {* @{text filter} *} 
13114  592 

13142  593 
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" 
13145  594 
by (induct xs) auto 
13114  595 

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

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

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

13142  605 
lemma length_filter [simp]: "length (filter P xs) \<le> length xs" 
13145  606 
by (induct xs) (auto simp add: le_SucI) 
13114  607 

13142  608 
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" 
13145  609 
by auto 
13114  610 

611 

13142  612 
subsection {* @{text concat} *} 
13114  613 

13142  614 
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" 
13145  615 
by (induct xs) auto 
13114  616 

13142  617 
lemma concat_eq_Nil_conv [iff]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])" 
13145  618 
by (induct xss) auto 
13114  619 

13142  620 
lemma Nil_eq_concat_conv [iff]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])" 
13145  621 
by (induct xss) auto 
13114  622 

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

13142  626 
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" 
13145  627 
by (induct xs) auto 
13114  628 

13142  629 
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" 
13145  630 
by (induct xs) auto 
13114  631 

13142  632 
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" 
13145  633 
by (induct xs) auto 
13114  634 

635 

13142  636 
subsection {* @{text nth} *} 
13114  637 

13142  638 
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x" 
13145  639 
by auto 
13114  640 

13142  641 
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n" 
13145  642 
by auto 
13114  643 

13142  644 
declare nth.simps [simp del] 
13114  645 

646 
lemma nth_append: 

13145  647 
"!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n  length xs))" 
648 
apply(induct "xs") 

649 
apply simp 

650 
apply (case_tac n) 

651 
apply auto 

652 
done 

13114  653 

13142  654 
lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)" 
13145  655 
apply(induct xs) 
656 
apply simp 

657 
apply (case_tac n) 

658 
apply auto 

659 
done 

13114  660 

13142  661 
lemma set_conv_nth: "set xs = {xs!i  i. i < length xs}" 
13145  662 
apply (induct_tac xs) 
663 
apply simp 

664 
apply simp 

665 
apply safe 

666 
apply (rule_tac x = 0 in exI) 

667 
apply simp 

668 
apply (rule_tac x = "Suc i" in exI) 

669 
apply simp 

670 
apply (case_tac i) 

671 
apply simp 

672 
apply (rename_tac j) 

673 
apply (rule_tac x = j in exI) 

674 
apply simp 

675 
done 

13114  676 

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

13114  679 

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

683 
lemma all_nth_imp_all_set: 

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

13114  686 

687 
lemma all_set_conv_all_nth: 

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

13114  690 

691 

13142  692 
subsection {* @{text list_update} *} 
13114  693 

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

697 
lemma nth_list_update: 

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

13114  700 

13142  701 
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x" 
13145  702 
by (simp add: nth_list_update) 
13114  703 

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

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

13114  710 

711 
lemma list_update_same_conv: 

13145  712 
"!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)" 
713 
by (induct xs) (auto split: nat.split) 

13114  714 

715 
lemma update_zip: 

13145  716 
"!!i xy xs. length xs = length ys ==> 
717 
(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" 

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

13114  719 

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

13145  721 
by (induct xs) (auto split: nat.split) 
13114  722 

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

13145  724 
by (blast dest!: set_update_subset_insert [THEN subsetD]) 
13114  725 

726 

13142  727 
subsection {* @{text last} and @{text butlast} *} 
13114  728 

13142  729 
lemma last_snoc [simp]: "last (xs @ [x]) = x" 
13145  730 
by (induct xs) auto 
13114  731 

13142  732 
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" 
13145  733 
by (induct xs) auto 
13114  734 

13142  735 
lemma length_butlast [simp]: "length (butlast xs) = length xs  1" 
13145  736 
by (induct xs rule: rev_induct) auto 
13114  737 

738 
lemma butlast_append: 

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

13114  741 

13142  742 
lemma append_butlast_last_id [simp]: 
13145  743 
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs" 
744 
by (induct xs) auto 

13114  745 

13142  746 
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs" 
13145  747 
by (induct xs) (auto split: split_if_asm) 
13114  748 

749 
lemma in_set_butlast_appendI: 

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

13114  752 

13142  753 

754 
subsection {* @{text take} and @{text drop} *} 

13114  755 

13142  756 
lemma take_0 [simp]: "take 0 xs = []" 
13145  757 
by (induct xs) auto 
13114  758 

13142  759 
lemma drop_0 [simp]: "drop 0 xs = xs" 
13145  760 
by (induct xs) auto 
13114  761 

13142  762 
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" 
13145  763 
by simp 
13114  764 

13142  765 
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" 
13145  766 
by simp 
13114  767 

13142  768 
declare take_Cons [simp del] and drop_Cons [simp del] 
13114  769 

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

772 
apply(induct xs) 

773 
apply simp 

774 
apply(case_tac i) 

775 
apply auto 

776 
done 

777 

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

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

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

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

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

13114  793 

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

13114  797 

13142  798 
lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs" 
13145  799 
apply (induct m) 
800 
apply auto 

801 
apply (case_tac xs) 

802 
apply auto 

803 
apply (case_tac na) 

804 
apply auto 

805 
done 

13114  806 

13142  807 
lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs" 
13145  808 
apply (induct m) 
809 
apply auto 

810 
apply (case_tac xs) 

811 
apply auto 

812 
done 

13114  813 

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

13145  815 
apply (induct m) 
816 
apply auto 

817 
apply (case_tac xs) 

818 
apply auto 

819 
done 

13114  820 

13142  821 
lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs" 
13145  822 
apply (induct n) 
823 
apply auto 

824 
apply (case_tac xs) 

825 
apply auto 

826 
done 

13114  827 

828 
lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)" 

13145  829 
apply (induct n) 
830 
apply auto 

831 
apply (case_tac xs) 

832 
apply auto 

833 
done 

13114  834 

13142  835 
lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)" 
13145  836 
apply (induct n) 
837 
apply auto 

838 
apply (case_tac xs) 

839 
apply auto 

840 
done 

13114  841 

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

13145  843 
apply (induct xs) 
844 
apply auto 

845 
apply (case_tac i) 

846 
apply auto 

847 
done 

13114  848 

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

13145  850 
apply (induct xs) 
851 
apply auto 

852 
apply (case_tac i) 

853 
apply auto 

854 
done 

13114  855 

13142  856 
lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i" 
13145  857 
apply (induct xs) 
858 
apply auto 

859 
apply (case_tac n) 

860 
apply(blast ) 

861 
apply (case_tac i) 

862 
apply auto 

863 
done 

13114  864 

13142  865 
lemma nth_drop [simp]: 
13145  866 
"!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)" 
867 
apply (induct n) 

868 
apply auto 

869 
apply (case_tac xs) 

870 
apply auto 

871 
done 

3507  872 

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

875 

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

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

878 

13114  879 
lemma append_eq_conv_conj: 
13145  880 
"!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)" 
881 
apply(induct xs) 

882 
apply simp 

883 
apply clarsimp 

884 
apply (case_tac zs) 

885 
apply auto 

886 
done 

13142  887 

13114  888 

13142  889 
subsection {* @{text takeWhile} and @{text dropWhile} *} 
13114  890 

13142  891 
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs" 
13145  892 
by (induct xs) auto 
13114  893 

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

13114  897 

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

13114  901 

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

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

13114  908 

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

13114  912 

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

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

918 
by(induct xs, auto) 

919 

920 
lemma dropWhile_eq_Nil_conv[simp]: 

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

922 
by(induct xs, auto) 

923 

924 
lemma dropWhile_eq_Cons_conv: 

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

926 
by(induct xs, auto) 

927 

13114  928 

13142  929 
subsection {* @{text zip} *} 
13114  930 

13142  931 
lemma zip_Nil [simp]: "zip [] ys = []" 
13145  932 
by (induct ys) auto 
13114  933 

13142  934 
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys" 
13145  935 
by simp 
13114  936 

13142  937 
declare zip_Cons [simp del] 
13114  938 

13142  939 
lemma length_zip [simp]: 
13145  940 
"!!xs. length (zip xs ys) = min (length xs) (length ys)" 
941 
apply(induct ys) 

942 
apply simp 

943 
apply (case_tac xs) 

944 
apply auto 

945 
done 

13114  946 

947 
lemma zip_append1: 

13145  948 
"!!xs. zip (xs @ ys) zs = 
949 
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)" 

950 
apply (induct zs) 

951 
apply simp 

952 
apply (case_tac xs) 

953 
apply simp_all 

954 
done 

13114  955 

956 
lemma zip_append2: 

13145  957 
"!!ys. zip xs (ys @ zs) = 
958 
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs" 

959 
apply (induct xs) 

960 
apply simp 

961 
apply (case_tac ys) 

962 
apply simp_all 

963 
done 

13114  964 

13142  965 
lemma zip_append [simp]: 
966 
"[ length xs = length us; length ys = length vs ] ==> 

13145  967 
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs" 
968 
by (simp add: zip_append1) 

13114  969 

970 
lemma zip_rev: 

13145  971 
"!!xs. length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)" 
972 
apply(induct ys) 

973 
apply simp 

974 
apply (case_tac xs) 

975 
apply simp_all 

976 
done 

13114  977 

13142  978 
lemma nth_zip [simp]: 
13145  979 
"!!i xs. [ i < length xs; i < length ys] ==> (zip xs ys)!i = (xs!i, ys!i)" 
980 
apply (induct ys) 

981 
apply simp 

982 
apply (case_tac xs) 

983 
apply (simp_all add: nth.simps split: nat.split) 

984 
done 

13114  985 

986 
lemma set_zip: 

13145  987 
"set (zip xs ys) = {(xs!i, ys!i)  i. i < min (length xs) (length ys)}" 
988 
by (simp add: set_conv_nth cong: rev_conj_cong) 

13114  989 

990 
lemma zip_update: 

13145  991 
"length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]" 
992 
by (rule sym, simp add: update_zip) 

13114  993 

13142  994 
lemma zip_replicate [simp]: 
13145  995 
"!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)" 
996 
apply (induct i) 

997 
apply auto 

998 
apply (case_tac j) 

999 
apply auto 

1000 
done 

13114  1001 

13142  1002 

1003 
subsection {* @{text list_all2} *} 

13114  1004 

1005 
lemma list_all2_lengthD: "list_all2 P xs ys ==> length xs = length ys" 

13145  1006 
by (simp add: list_all2_def) 
13114  1007 

13142  1008 
lemma list_all2_Nil [iff]: "list_all2 P [] ys = (ys = [])" 
13145  1009 
by (simp add: list_all2_def) 
13114  1010 

13142  1011 
lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])" 
13145  1012 
by (simp add: list_all2_def) 
13114  1013 

13142  1014 
lemma list_all2_Cons [iff]: 
13145  1015 
"list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)" 
1016 
by (auto simp add: list_all2_def) 

13114  1017 

1018 
lemma list_all2_Cons1: 

13145  1019 
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)" 
1020 
by (cases ys) auto 

13114  1021 

1022 
lemma list_all2_Cons2: 

13145  1023 
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)" 
1024 
by (cases xs) auto 

13114  1025 

13142  1026 
lemma list_all2_rev [iff]: 
13145  1027 
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys" 
1028 
by (simp add: list_all2_def zip_rev cong: conj_cong) 

13114  1029 

13863  1030 
lemma list_all2_rev1: 
1031 
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)" 

1032 
by (subst list_all2_rev [symmetric]) simp 

1033 

13114  1034 
lemma list_all2_append1: 
13145  1035 
"list_all2 P (xs @ ys) zs = 
1036 
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and> 

1037 
list_all2 P xs us \<and> list_all2 P ys vs)" 

1038 
apply (simp add: list_all2_def zip_append1) 

1039 
apply (rule iffI) 

1040 
apply (rule_tac x = "take (length xs) zs" in exI) 

1041 
apply (rule_tac x = "drop (length xs) zs" in exI) 

1042 
apply (force split: nat_diff_split simp add: min_def) 

1043 
apply clarify 

1044 
apply (simp add: ball_Un) 

1045 
done 

13114  1046 

1047 
lemma list_all2_append2: 

13145  1048 
"list_all2 P xs (ys @ zs) = 
1049 
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and> 

1050 
list_all2 P us ys \<and> list_all2 P vs zs)" 

1051 
apply (simp add: list_all2_def zip_append2) 

1052 
apply (rule iffI) 

1053 
apply (rule_tac x = "take (length ys) xs" in exI) 

1054 
apply (rule_tac x = "drop (length ys) xs" in exI) 

1055 
apply (force split: nat_diff_split simp add: min_def) 

1056 
apply clarify 

1057 
apply (simp add: ball_Un) 

1058 
done 

13114  1059 

13863  1060 
lemma list_all2_append: 
1061 
"\<And>b. length a = length b \<Longrightarrow> 

1062 
list_all2 P (a@c) (b@d) = (list_all2 P a b \<and> list_all2 P c d)" 

1063 
apply (induct a) 

1064 
apply simp 

1065 
apply (case_tac b) 

1066 
apply auto 

1067 
done 

1068 

1069 
lemma list_all2_appendI [intro?, trans]: 

1070 
"\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)" 

1071 
by (simp add: list_all2_append list_all2_lengthD) 

1072 

13114  1073 
lemma list_all2_conv_all_nth: 
13145  1074 
"list_all2 P xs ys = 
1075 
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))" 

1076 
by (force simp add: list_all2_def set_zip) 

13114  1077 

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

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

1079 
assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c" 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

1080 
shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs" 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

1081 
(is "!!bs cs. PROP ?Q as bs cs") 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

1082 
proof (induct as) 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

1083 
fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs" 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

1084 
show "!!cs. PROP ?Q (x # xs) bs cs" 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

1085 
proof (induct bs) 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

1086 
fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs" 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

1087 
show "PROP ?Q (x # xs) (y # ys) cs" 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

1088 
by (induct cs) (auto intro: tr I1 I2) 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

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

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

1091 

13863  1092 
lemma list_all2_all_nthI [intro?]: 
1093 
"length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b" 

1094 
by (simp add: list_all2_conv_all_nth) 

1095 

1096 
lemma list_all2_nthD [dest?]: 

1097 
"\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" 

1098 
by (simp add: list_all2_conv_all_nth) 

1099 

1100 
lemma list_all2_map1: 

1101 
"list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs" 

1102 
by (simp add: list_all2_conv_all_nth) 

1103 

1104 
lemma list_all2_map2: 

1105 
"list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs" 

1106 
by (auto simp add: list_all2_conv_all_nth) 

1107 

1108 
lemma list_all2_refl: 

1109 
"(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs" 

1110 
by (simp add: list_all2_conv_all_nth) 

1111 

1112 
lemma list_all2_update_cong: 

1113 
"\<lbrakk> i<size xs; list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])" 

1114 
by (simp add: list_all2_conv_all_nth nth_list_update) 

1115 

1116 
lemma list_all2_update_cong2: 

1117 
"\<lbrakk>list_all2 P xs ys; P x y; i < length ys\<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])" 

1118 
by (simp add: list_all2_lengthD list_all2_update_cong) 

1119 

1120 
lemma list_all2_dropI [intro?]: 

1121 
"\<And>n bs. list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)" 

1122 
apply (induct as) 

1123 
apply simp 

1124 
apply (clarsimp simp add: list_all2_Cons1) 

1125 
apply (case_tac n) 

1126 
apply simp 

1127 
apply simp 

1128 
done 

1129 

1130 
lemma list_all2_mono [intro?]: 

1131 
"\<And>y. list_all2 P x y \<Longrightarrow> (\<And>x y. P x y \<Longrightarrow> Q x y) \<Longrightarrow> list_all2 Q x y" 

1132 
apply (induct x) 

1133 
apply simp 

1134 
apply (case_tac y) 

1135 
apply auto 

1136 
done 

1137 

13142  1138 

1139 
subsection {* @{text foldl} *} 

1140 

1141 
lemma foldl_append [simp]: 

13145  1142 
"!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys" 
1143 
by (induct xs) auto 

13142  1144 

1145 
text {* 

13145  1146 
Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more 
1147 
difficult to use because it requires an additional transitivity step. 

13142  1148 
*} 
1149 

1150 
lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl (op +) n ns" 

13145  1151 
by (induct ns) auto 
13142  1152 

1153 
lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl (op +) 0 ns" 

13145  1154 
by (force intro: start_le_sum simp add: in_set_conv_decomp) 
13142  1155 

1156 
lemma sum_eq_0_conv [iff]: 

13145  1157 
"!!m::nat. (foldl (op +) m ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))" 
1158 
by (induct ns) auto 

13114  1159 

1160 

13142  1161 
subsection {* @{text upto} *} 
13114  1162 

13142  1163 
lemma upt_rec: "[i..j(] = (if i<j then i#[Suc i..j(] else [])" 
13145  1164 
 {* Does not terminate! *} 
1165 
by (induct j) auto 

13142  1166 

1167 
lemma upt_conv_Nil [simp]: "j <= i ==> [i..j(] = []" 

13145  1168 
by (subst upt_rec) simp 
13114  1169 

13142  1170 
lemma upt_Suc_append: "i <= j ==> [i..(Suc j)(] = [i..j(]@[j]" 
13145  1171 
 {* Only needed if @{text upt_Suc} is deleted from the simpset. *} 
1172 
by simp 

13114  1173 

13142  1174 
lemma upt_conv_Cons: "i < j ==> [i..j(] = i # [Suc i..j(]" 
13145  1175 
apply(rule trans) 
1176 
apply(subst upt_rec) 

1177 
prefer 2 apply(rule refl) 

1178 
apply simp 

1179 
done 

13114  1180 

13142  1181 
lemma upt_add_eq_append: "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]" 
13145  1182 
 {* LOOPS as a simprule, since @{text "j <= j"}. *} 
1183 
by (induct k) auto 

13114  1184 

13142  1185 
lemma length_upt [simp]: "length [i..j(] = j  i" 
13145  1186 
by (induct j) (auto simp add: Suc_diff_le) 
13114  1187 

13142  1188 
lemma nth_upt [simp]: "i + k < j ==> [i..j(] ! k = i + k" 
13145  1189 
apply (induct j) 
1190 
apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split) 

1191 
done 

13114  1192 

13142  1193 
lemma take_upt [simp]: "!!i. i+m <= n ==> take m [i..n(] = [i..i+m(]" 
13145  1194 
apply (induct m) 
1195 
apply simp 

1196 
apply (subst upt_rec) 

1197 
apply (rule sym) 

1198 
apply (subst upt_rec) 

1199 
apply (simp del: upt.simps) 

1200 
done 

3507  1201 

13114  1202 
lemma map_Suc_upt: "map Suc [m..n(] = [Suc m..n]" 
13145  1203 
by (induct n) auto 
13114  1204 

1205 
lemma nth_map_upt: "!!i. i < nm ==> (map f [m..n(]) ! i = f(m+i)" 

13145  1206 
apply (induct n m rule: diff_induct) 
1207 
prefer 3 apply (subst map_Suc_upt[symmetric]) 

1208 
apply (auto simp add: less_diff_conv nth_upt) 

1209 
done 

13114  1210 

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

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

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

1213 
(!!i. i < k > xs!i = ys!i) ==> take k xs = take k ys" 
0451e0fb3f22
Restructured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset

1214 
apply (atomize, induct k) 
13145  1215 
apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib) 
1216 
apply clarify 

1217 
txt {* Both lists must be nonempty *} 

1218 
apply (case_tac xs) 

1219 
apply simp 

1220 
apply (case_tac ys) 

1221 
apply clarify 

1222 
apply (simp (no_asm_use)) 

1223 
apply clarify 

1224 
txt {* prenexing's needed, not miniscoping *} 

1225 
apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps) 

1226 
apply blast 

1227 
done 

13114  1228 

1229 
lemma nth_equalityI: 

1230 
"[ length xs = length ys; ALL i < length xs. xs!i = ys!i ] ==> xs = ys" 

13145  1231 
apply (frule nth_take_lemma [OF le_refl eq_imp_le]) 
1232 
apply (simp_all add: take_all) 

1233 
done 

13142  1234 

13863  1235 
(* needs nth_equalityI *) 
1236 
lemma list_all2_antisym: 

1237 
"\<lbrakk> (\<And>x y. \<lbrakk>P x y; Q y x\<rbrakk> \<Longrightarrow> x = y); list_all2 P xs ys; list_all2 Q ys xs \<rbrakk> 

1238 
\<Longrightarrow> xs = ys" 

1239 
apply (simp add: list_all2_conv_all_nth) 

1240 
apply (rule nth_equalityI) 

1241 
apply blast 

1242 
apply simp 

1243 
done 

1244 

13142  1245 
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys" 
13145  1246 
 {* The famous takelemma. *} 
1247 
apply (drule_tac x = "max (length xs) (length ys)" in spec) 

1248 
apply (simp add: le_max_iff_disj take_all) 

1249 
done 

13142  1250 

1251 

1252 
subsection {* @{text "distinct"} and @{text remdups} *} 

1253 

1254 
lemma distinct_append [simp]: 

13145  1255 
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})" 
1256 
by (induct xs) auto 

13142  1257 

1258 
lemma set_remdups [simp]: "set (remdups xs) = set xs" 

13145  1259 
by (induct xs) (auto simp add: insert_absorb) 
13142  1260 

1261 
lemma distinct_remdups [iff]: "distinct (remdups xs)" 

13145  1262 
by (induct xs) auto 
13142  1263 

1264 
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)" 

13145  1265 
by (induct xs) auto 
13114  1266 

13142  1267 
text {* 
13145  1268 
It is best to avoid this indexed version of distinct, but sometimes 
1269 
it is useful. *} 

13142  1270 
lemma distinct_conv_nth: 
13145  1271 
"distinct xs = (\<forall>i j. i < size xs \<and> j < size xs \<and> i \<noteq> j > xs!i \<noteq> xs!j)" 
1272 
apply (induct_tac xs) 

1273 
apply simp 

1274 
apply simp 

1275 
apply (rule iffI) 

1276 
apply clarsimp 

1277 
apply (case_tac i) 

1278 
apply (case_tac j) 

1279 
apply simp 

1280 
apply (simp add: set_conv_nth) 

1281 
apply (case_tac j) 

1282 
apply (clarsimp simp add: set_conv_nth) 

1283 
apply simp 

1284 
apply (rule conjI) 

1285 
apply (clarsimp simp add: set_conv_nth) 

1286 
apply (erule_tac x = 0 in allE) 

1287 
apply (erule_tac x = "Suc i" in allE) 

1288 
apply simp 

1289 
apply clarsimp 

1290 
apply (erule_tac x = "Suc i" in allE) 

1291 
apply (erule_tac x = "Suc j" in allE) 

1292 
apply simp 

1293 
done 

13114  1294 

1295 

13142  1296 
subsection {* @{text replicate} *} 
13114  1297 

13142  1298 
lemma length_replicate [simp]: "length (replicate n x) = n" 
13145  1299 
by (induct n) auto 
13124  1300 

13142  1301 
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)" 
13145  1302 
by (induct n) auto 
13114  1303 

1304 
lemma replicate_app_Cons_same: 

13145  1305 
"(replicate n x) @ (x # xs) = x # replicate n x @ xs" 
1306 
by (induct n) auto 

13114  1307 

13142  1308 
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x" 
13145  1309 
apply(induct n) 
1310 
apply simp 

1311 
apply (simp add: replicate_app_Cons_same) 

1312 
done 

13114  1313 

13142  1314 
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x" 
13145  1315 
by (induct n) auto 
13114  1316 

13142  1317 
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x" 
13145  1318 
by (induct n) auto 
13114  1319 

13142  1320 
lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n  1) x" 
13145  1321 
by (induct n) auto 
13114  1322 

13142  1323 
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x" 
13145  1324 
by (atomize (full), induct n) auto 
13114  1325 

13142  1326 
lemma nth_replicate[simp]: "!!i. i < n ==> (replicate n x)!i = x" 
13145  1327 
apply(induct n) 
1328 
apply simp 

1329 
apply (simp add: nth_Cons split: nat.split) 

1330 
done 

13114  1331 

13142  1332 
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}" 
13145  1333 
by (induct n) auto 
13114  1334 

13142  1335 
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}" 
13145  1336 
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc) 
13114  1337 

13142  1338 
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})" 
13145  1339 
by auto 
13114  1340 

13142  1341 
lemma in_set_replicateD: "x : set (replicate n y) ==> x = y" 
13145  1342 
by (simp add: set_replicate_conv_if split: split_if_asm) 
13114  1343 

1344 

13142  1345 
subsection {* Lexcicographic orderings on lists *} 
3507  1346 

13142  1347 
lemma wf_lexn: "wf r ==> wf (lexn r n)" 
13145  1348 
apply (induct_tac n) 
1349 
apply simp 

1350 
apply simp 

1351 
apply(rule wf_subset) 

1352 
prefer 2 apply (rule Int_lower1) 

1353 
apply(rule wf_prod_fun_image) 

13585  1354 
prefer 2 apply (rule inj_onI) 
13145  1355 
apply auto 