src/HOL/List.thy
author nipkow
Mon May 13 15:27:28 2002 +0200 (2002-05-13)
changeset 13145 59bc43b51aa2
parent 13142 1ebd8ed5a1a0
child 13146 f43153b63361
permissions -rw-r--r--
*** empty log message ***
nipkow@13145
     1
(*Title:HOL/List.thy
nipkow@13145
     2
ID: $Id$
nipkow@13145
     3
Author: Tobias Nipkow
nipkow@13145
     4
Copyright 1994 TU Muenchen
clasohm@923
     5
*)
clasohm@923
     6
wenzelm@13114
     7
header {* The datatype of finite lists *}
wenzelm@13122
     8
wenzelm@13122
     9
theory List = PreList:
clasohm@923
    10
wenzelm@13142
    11
datatype 'a list =
nipkow@13145
    12
Nil("[]")
nipkow@13145
    13
| Cons 'a"'a list"(infixr "#" 65)
clasohm@923
    14
clasohm@923
    15
consts
nipkow@13145
    16
"@" :: "'a list => 'a list => 'a list"(infixr 65)
nipkow@13145
    17
filter:: "('a => bool) => 'a list => 'a list"
nipkow@13145
    18
concat:: "'a list list => 'a list"
nipkow@13145
    19
foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b"
nipkow@13145
    20
foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b"
nipkow@13145
    21
hd:: "'a list => 'a"
nipkow@13145
    22
tl:: "'a list => 'a list"
nipkow@13145
    23
last:: "'a list => 'a"
nipkow@13145
    24
butlast :: "'a list => 'a list"
nipkow@13145
    25
set :: "'a list => 'a set"
nipkow@13145
    26
list_all:: "('a => bool) => ('a list => bool)"
nipkow@13145
    27
list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool"
nipkow@13145
    28
map :: "('a=>'b) => ('a list => 'b list)"
nipkow@13145
    29
mem :: "'a => 'a list => bool"(infixl 55)
nipkow@13145
    30
nth :: "'a list => nat => 'a" (infixl "!" 100)
nipkow@13145
    31
list_update :: "'a list => nat => 'a => 'a list"
nipkow@13145
    32
take:: "nat => 'a list => 'a list"
nipkow@13145
    33
drop:: "nat => 'a list => 'a list"
nipkow@13145
    34
takeWhile :: "('a => bool) => 'a list => 'a list"
nipkow@13145
    35
dropWhile :: "('a => bool) => 'a list => 'a list"
nipkow@13145
    36
rev :: "'a list => 'a list"
nipkow@13145
    37
zip :: "'a list => 'b list => ('a * 'b) list"
nipkow@13145
    38
upt :: "nat => nat => nat list" ("(1[_../_'(])")
nipkow@13145
    39
remdups :: "'a list => 'a list"
nipkow@13145
    40
null:: "'a list => bool"
nipkow@13145
    41
"distinct":: "'a list => bool"
nipkow@13145
    42
replicate :: "nat => 'a => 'a list"
clasohm@923
    43
nipkow@5077
    44
nonterminals
nipkow@13145
    45
lupdbindslupdbind
nipkow@5077
    46
clasohm@923
    47
syntax
nipkow@13145
    48
-- {* list Enumeration *}
nipkow@13145
    49
"@list" :: "args => 'a list"("[(_)]")
clasohm@923
    50
nipkow@13145
    51
-- {* Special syntax for filter *}
nipkow@13145
    52
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_:_./ _])")
clasohm@923
    53
nipkow@13145
    54
-- {* list update *}
nipkow@13145
    55
"_lupdbind":: "['a, 'a] => lupdbind"("(2_ :=/ _)")
nipkow@13145
    56
"" :: "lupdbind => lupdbinds" ("_")
nipkow@13145
    57
"_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds" ("_,/ _")
nipkow@13145
    58
"_LUpdate" :: "['a, lupdbinds] => 'a" ("_/[(_)]" [900,0] 900)
nipkow@5077
    59
nipkow@13145
    60
upto:: "nat => nat => nat list" ("(1[_../_])")
nipkow@5427
    61
clasohm@923
    62
translations
nipkow@13145
    63
"[x, xs]" == "x#[xs]"
nipkow@13145
    64
"[x]" == "x#[]"
nipkow@13145
    65
"[x:xs . P]"== "filter (%x. P) xs"
clasohm@923
    66
nipkow@13145
    67
"_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs"
nipkow@13145
    68
"xs[i:=x]" == "list_update xs i x"
nipkow@5077
    69
nipkow@13145
    70
"[i..j]" == "[i..(Suc j)(]"
nipkow@5427
    71
nipkow@5427
    72
wenzelm@12114
    73
syntax (xsymbols)
nipkow@13145
    74
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])")
paulson@3342
    75
paulson@3342
    76
wenzelm@13142
    77
text {*
nipkow@13145
    78
Function @{text size} is overloaded for all datatypes.Users may
nipkow@13145
    79
refer to the list version as @{text length}. *}
wenzelm@13142
    80
wenzelm@13142
    81
syntax length :: "'a list => nat"
wenzelm@13142
    82
translations "length" => "size :: _ list => nat"
wenzelm@13114
    83
wenzelm@13142
    84
typed_print_translation {*
nipkow@13145
    85
let
nipkow@13145
    86
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
nipkow@13145
    87
Syntax.const "length" $ t
nipkow@13145
    88
| size_tr' _ _ _ = raise Match;
nipkow@13145
    89
in [("size", size_tr')] end
wenzelm@13114
    90
*}
paulson@3437
    91
berghofe@5183
    92
primrec
nipkow@13145
    93
"hd(x#xs) = x"
berghofe@5183
    94
primrec
nipkow@13145
    95
"tl([]) = []"
nipkow@13145
    96
"tl(x#xs) = xs"
berghofe@5183
    97
primrec
nipkow@13145
    98
"null([]) = True"
nipkow@13145
    99
"null(x#xs) = False"
paulson@8972
   100
primrec
nipkow@13145
   101
"last(x#xs) = (if xs=[] then x else last xs)"
berghofe@5183
   102
primrec
nipkow@13145
   103
"butlast []= []"
nipkow@13145
   104
"butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
berghofe@5183
   105
primrec
nipkow@13145
   106
"x mem [] = False"
nipkow@13145
   107
"x mem (y#ys) = (if y=x then True else x mem ys)"
oheimb@5518
   108
primrec
nipkow@13145
   109
"set [] = {}"
nipkow@13145
   110
"set (x#xs) = insert x (set xs)"
berghofe@5183
   111
primrec
nipkow@13145
   112
list_all_Nil:"list_all P [] = True"
nipkow@13145
   113
list_all_Cons: "list_all P (x#xs) = (P(x) \<and> list_all P xs)"
oheimb@5518
   114
primrec
nipkow@13145
   115
"map f [] = []"
nipkow@13145
   116
"map f (x#xs) = f(x)#map f xs"
berghofe@5183
   117
primrec
nipkow@13145
   118
append_Nil:"[]@ys = ys"
nipkow@13145
   119
append_Cons: "(x#xs)@ys = x#(xs@ys)"
berghofe@5183
   120
primrec
nipkow@13145
   121
"rev([]) = []"
nipkow@13145
   122
"rev(x#xs) = rev(xs) @ [x]"
berghofe@5183
   123
primrec
nipkow@13145
   124
"filter P [] = []"
nipkow@13145
   125
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
berghofe@5183
   126
primrec
nipkow@13145
   127
foldl_Nil:"foldl f a [] = a"
nipkow@13145
   128
foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs"
berghofe@5183
   129
primrec
nipkow@13145
   130
"foldr f [] a = a"
nipkow@13145
   131
"foldr f (x#xs) a = f x (foldr f xs a)"
paulson@8000
   132
primrec
nipkow@13145
   133
"concat([]) = []"
nipkow@13145
   134
"concat(x#xs) = x @ concat(xs)"
berghofe@5183
   135
primrec
nipkow@13145
   136
drop_Nil:"drop n [] = []"
nipkow@13145
   137
drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
nipkow@13145
   138
-- {* Warning: simpset does not contain this definition *}
nipkow@13145
   139
-- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
berghofe@5183
   140
primrec
nipkow@13145
   141
take_Nil:"take n [] = []"
nipkow@13145
   142
take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
nipkow@13145
   143
-- {* Warning: simpset does not contain this definition *}
nipkow@13145
   144
-- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
berghofe@5183
   145
primrec
nipkow@13145
   146
nth_Cons:"(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
nipkow@13145
   147
-- {* Warning: simpset does not contain this definition *}
nipkow@13145
   148
-- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
wenzelm@13142
   149
primrec
nipkow@13145
   150
"[][i:=v] = []"
nipkow@13145
   151
"(x#xs)[i:=v] =
nipkow@13145
   152
(case i of 0 => v # xs
nipkow@13145
   153
| Suc j => x # xs[j:=v])"
berghofe@5183
   154
primrec
nipkow@13145
   155
"takeWhile P [] = []"
nipkow@13145
   156
"takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
berghofe@5183
   157
primrec
nipkow@13145
   158
"dropWhile P [] = []"
nipkow@13145
   159
"dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
berghofe@5183
   160
primrec
nipkow@13145
   161
"zip xs [] = []"
nipkow@13145
   162
zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"
nipkow@13145
   163
-- {* Warning: simpset does not contain this definition *}
nipkow@13145
   164
-- {* but separate theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
nipkow@5427
   165
primrec
nipkow@13145
   166
upt_0: "[i..0(] = []"
nipkow@13145
   167
upt_Suc: "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])"
berghofe@5183
   168
primrec
nipkow@13145
   169
"distinct [] = True"
nipkow@13145
   170
"distinct (x#xs) = (x ~: set xs \<and> distinct xs)"
berghofe@5183
   171
primrec
nipkow@13145
   172
"remdups [] = []"
nipkow@13145
   173
"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
berghofe@5183
   174
primrec
nipkow@13145
   175
replicate_0: "replicate0x = []"
nipkow@13145
   176
replicate_Suc: "replicate (Suc n) x = x # replicate n x"
nipkow@8115
   177
defs
wenzelm@13114
   178
 list_all2_def:
wenzelm@13142
   179
 "list_all2 P xs ys == length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y)"
nipkow@8115
   180
paulson@3196
   181
wenzelm@13142
   182
subsection {* Lexicographic orderings on lists *}
nipkow@5281
   183
nipkow@5281
   184
consts
nipkow@13145
   185
lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
nipkow@5281
   186
primrec
nipkow@13145
   187
"lexn r 0 = {}"
nipkow@13145
   188
"lexn r (Suc n) =
nipkow@13145
   189
(prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int
nipkow@13145
   190
{(xs,ys). length xs = Suc n \<and> length ys = Suc n}"
nipkow@5281
   191
nipkow@5281
   192
constdefs
nipkow@13145
   193
lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
nipkow@13145
   194
"lex r == \<Union>n. lexn r n"
nipkow@5281
   195
nipkow@13145
   196
lexico :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
nipkow@13145
   197
"lexico r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"
paulson@9336
   198
nipkow@13145
   199
sublist :: "'a list => nat set => 'a list"
nipkow@13145
   200
"sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..size xs(]))"
nipkow@5281
   201
wenzelm@13114
   202
wenzelm@13142
   203
lemma not_Cons_self [simp]: "xs \<noteq> x # xs"
nipkow@13145
   204
by (induct xs) auto
wenzelm@13114
   205
wenzelm@13142
   206
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric]
wenzelm@13114
   207
wenzelm@13142
   208
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
nipkow@13145
   209
by (induct xs) auto
wenzelm@13114
   210
wenzelm@13142
   211
lemma length_induct:
nipkow@13145
   212
"(!!xs. \<forall>ys. length ys < length xs --> P ys ==> P xs) ==> P xs"
nipkow@13145
   213
by (rule measure_induct [of length]) rules
wenzelm@13114
   214
wenzelm@13114
   215
wenzelm@13142
   216
subsection {* @{text lists}: the list-forming operator over sets *}
wenzelm@13114
   217
wenzelm@13142
   218
consts lists :: "'a set => 'a list set"
wenzelm@13142
   219
inductive "lists A"
nipkow@13145
   220
intros
nipkow@13145
   221
Nil [intro!]: "[]: lists A"
nipkow@13145
   222
Cons [intro!]: "[| a: A;l: lists A|] ==> a#l : lists A"
wenzelm@13114
   223
wenzelm@13142
   224
inductive_cases listsE [elim!]: "x#l : lists A"
wenzelm@13114
   225
wenzelm@13142
   226
lemma lists_mono: "A \<subseteq> B ==> lists A \<subseteq> lists B"
nipkow@13145
   227
by (unfold lists.defs) (blast intro!: lfp_mono)
wenzelm@13114
   228
wenzelm@13142
   229
lemma lists_IntI [rule_format]:
nipkow@13145
   230
"l: lists A ==> l: lists B --> l: lists (A Int B)"
nipkow@13145
   231
apply (erule lists.induct)
nipkow@13145
   232
apply blast+
nipkow@13145
   233
done
wenzelm@13142
   234
wenzelm@13142
   235
lemma lists_Int_eq [simp]: "lists (A \<inter> B) = lists A \<inter> lists B"
nipkow@13145
   236
apply (rule mono_Int [THEN equalityI])
nipkow@13145
   237
apply (simp add: mono_def lists_mono)
nipkow@13145
   238
apply (blast intro!: lists_IntI)
nipkow@13145
   239
done
wenzelm@13114
   240
wenzelm@13142
   241
lemma append_in_lists_conv [iff]:
nipkow@13145
   242
"(xs @ ys : lists A) = (xs : lists A \<and> ys : lists A)"
nipkow@13145
   243
by (induct xs) auto
wenzelm@13142
   244
wenzelm@13142
   245
wenzelm@13142
   246
subsection {* @{text length} *}
wenzelm@13114
   247
wenzelm@13142
   248
text {*
nipkow@13145
   249
Needs to come before @{text "@"} because of theorem @{text
nipkow@13145
   250
append_eq_append_conv}.
wenzelm@13142
   251
*}
wenzelm@13114
   252
wenzelm@13142
   253
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
nipkow@13145
   254
by (induct xs) auto
wenzelm@13114
   255
wenzelm@13142
   256
lemma length_map [simp]: "length (map f xs) = length xs"
nipkow@13145
   257
by (induct xs) auto
wenzelm@13114
   258
wenzelm@13142
   259
lemma length_rev [simp]: "length (rev xs) = length xs"
nipkow@13145
   260
by (induct xs) auto
wenzelm@13114
   261
wenzelm@13142
   262
lemma length_tl [simp]: "length (tl xs) = length xs - 1"
nipkow@13145
   263
by (cases xs) auto
wenzelm@13114
   264
wenzelm@13142
   265
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
nipkow@13145
   266
by (induct xs) auto
wenzelm@13114
   267
wenzelm@13142
   268
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
nipkow@13145
   269
by (induct xs) auto
wenzelm@13114
   270
wenzelm@13114
   271
lemma length_Suc_conv:
nipkow@13145
   272
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
nipkow@13145
   273
by (induct xs) auto
wenzelm@13142
   274
wenzelm@13114
   275
wenzelm@13142
   276
subsection {* @{text "@"} -- append *}
wenzelm@13114
   277
wenzelm@13142
   278
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
nipkow@13145
   279
by (induct xs) auto
wenzelm@13114
   280
wenzelm@13142
   281
lemma append_Nil2 [simp]: "xs @ [] = xs"
nipkow@13145
   282
by (induct xs) auto
nipkow@3507
   283
wenzelm@13142
   284
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
nipkow@13145
   285
by (induct xs) auto
wenzelm@13114
   286
wenzelm@13142
   287
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
nipkow@13145
   288
by (induct xs) auto
wenzelm@13114
   289
wenzelm@13142
   290
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
nipkow@13145
   291
by (induct xs) auto
wenzelm@13114
   292
wenzelm@13142
   293
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
nipkow@13145
   294
by (induct xs) auto
wenzelm@13114
   295
wenzelm@13142
   296
lemma append_eq_append_conv [rule_format, simp]:
wenzelm@13142
   297
 "\<forall>ys. length xs = length ys \<or> length us = length vs
nipkow@13145
   298
 --> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
nipkow@13145
   299
apply (induct_tac xs)
nipkow@13145
   300
 apply(rule allI)
nipkow@13145
   301
 apply (case_tac ys)
nipkow@13145
   302
apply simp
nipkow@13145
   303
 apply force
nipkow@13145
   304
apply (rule allI)
nipkow@13145
   305
apply (case_tac ys)
nipkow@13145
   306
 apply force
nipkow@13145
   307
apply simp
nipkow@13145
   308
done
wenzelm@13142
   309
wenzelm@13142
   310
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)"
nipkow@13145
   311
by simp
wenzelm@13142
   312
wenzelm@13142
   313
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
nipkow@13145
   314
by simp
wenzelm@13114
   315
wenzelm@13142
   316
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)"
nipkow@13145
   317
by simp
wenzelm@13114
   318
wenzelm@13142
   319
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
nipkow@13145
   320
using append_same_eq [of _ _ "[]"] by auto
nipkow@3507
   321
wenzelm@13142
   322
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
nipkow@13145
   323
using append_same_eq [of "[]"] by auto
wenzelm@13114
   324
wenzelm@13142
   325
lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
nipkow@13145
   326
by (induct xs) auto
wenzelm@13114
   327
wenzelm@13142
   328
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
nipkow@13145
   329
by (induct xs) auto
wenzelm@13114
   330
wenzelm@13142
   331
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
nipkow@13145
   332
by (simp add: hd_append split: list.split)
wenzelm@13114
   333
wenzelm@13142
   334
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
nipkow@13145
   335
by (simp split: list.split)
wenzelm@13114
   336
wenzelm@13142
   337
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
nipkow@13145
   338
by (simp add: tl_append split: list.split)
wenzelm@13114
   339
wenzelm@13114
   340
wenzelm@13142
   341
text {* Trivial rules for solving @{text "@"}-equations automatically. *}
wenzelm@13114
   342
wenzelm@13114
   343
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
nipkow@13145
   344
by simp
wenzelm@13114
   345
wenzelm@13142
   346
lemma Cons_eq_appendI:
nipkow@13145
   347
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
nipkow@13145
   348
by (drule sym) simp
wenzelm@13114
   349
wenzelm@13142
   350
lemma append_eq_appendI:
nipkow@13145
   351
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
nipkow@13145
   352
by (drule sym) simp
wenzelm@13114
   353
wenzelm@13114
   354
wenzelm@13142
   355
text {*
nipkow@13145
   356
Simplification procedure for all list equalities.
nipkow@13145
   357
Currently only tries to rearrange @{text "@"} to see if
nipkow@13145
   358
- both lists end in a singleton list,
nipkow@13145
   359
- or both lists end in the same list.
wenzelm@13142
   360
*}
wenzelm@13142
   361
wenzelm@13142
   362
ML_setup {*
nipkow@3507
   363
local
nipkow@3507
   364
wenzelm@13122
   365
val append_assoc = thm "append_assoc";
wenzelm@13122
   366
val append_Nil = thm "append_Nil";
wenzelm@13122
   367
val append_Cons = thm "append_Cons";
wenzelm@13122
   368
val append1_eq_conv = thm "append1_eq_conv";
wenzelm@13122
   369
val append_same_eq = thm "append_same_eq";
wenzelm@13122
   370
wenzelm@13114
   371
val list_eq_pattern =
nipkow@13145
   372
Thm.read_cterm (Theory.sign_of (the_context ())) ("(xs::'a list) = ys",HOLogic.boolT)
wenzelm@13114
   373
wenzelm@13114
   374
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
nipkow@13145
   375
(case xs of Const("List.list.Nil",_) => cons | _ => last xs)
nipkow@13145
   376
| last (Const("List.op @",_) $ _ $ ys) = last ys
nipkow@13145
   377
| last t = t
wenzelm@13114
   378
wenzelm@13114
   379
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
nipkow@13145
   380
| list1 _ = false
wenzelm@13114
   381
wenzelm@13114
   382
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
nipkow@13145
   383
(case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
nipkow@13145
   384
| butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
nipkow@13145
   385
| butlast xs = Const("List.list.Nil",fastype_of xs)
wenzelm@13114
   386
wenzelm@13114
   387
val rearr_tac =
nipkow@13145
   388
simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons])
wenzelm@13114
   389
wenzelm@13114
   390
fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
nipkow@13145
   391
let
nipkow@13145
   392
val lastl = last lhs and lastr = last rhs
nipkow@13145
   393
fun rearr conv =
nipkow@13145
   394
let val lhs1 = butlast lhs and rhs1 = butlast rhs
nipkow@13145
   395
val Type(_,listT::_) = eqT
nipkow@13145
   396
val appT = [listT,listT] ---> listT
nipkow@13145
   397
val app = Const("List.op @",appT)
nipkow@13145
   398
val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
nipkow@13145
   399
val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2)))
nipkow@13145
   400
val thm = prove_goalw_cterm [] ct (K [rearr_tac 1])
nipkow@13145
   401
handle ERROR =>
nipkow@13145
   402
error("The error(s) above occurred while trying to prove " ^
nipkow@13145
   403
string_of_cterm ct)
nipkow@13145
   404
in Some((conv RS (thm RS trans)) RS eq_reflection) end
wenzelm@13114
   405
nipkow@13145
   406
in if list1 lastl andalso list1 lastr
nipkow@13145
   407
 then rearr append1_eq_conv
nipkow@13145
   408
 else
nipkow@13145
   409
 if lastl aconv lastr
nipkow@13145
   410
 then rearr append_same_eq
nipkow@13145
   411
 else None
nipkow@13145
   412
end
wenzelm@13114
   413
in
wenzelm@13114
   414
val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq
wenzelm@13114
   415
end;
wenzelm@13114
   416
wenzelm@13114
   417
Addsimprocs [list_eq_simproc];
wenzelm@13114
   418
*}
wenzelm@13114
   419
wenzelm@13114
   420
wenzelm@13142
   421
subsection {* @{text map} *}
wenzelm@13114
   422
wenzelm@13142
   423
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
nipkow@13145
   424
by (induct xs) simp_all
wenzelm@13114
   425
wenzelm@13142
   426
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
nipkow@13145
   427
by (rule ext, induct_tac xs) auto
wenzelm@13114
   428
wenzelm@13142
   429
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
nipkow@13145
   430
by (induct xs) auto
wenzelm@13114
   431
wenzelm@13142
   432
lemma map_compose: "map (f o g) xs = map f (map g xs)"
nipkow@13145
   433
by (induct xs) (auto simp add: o_def)
wenzelm@13114
   434
wenzelm@13142
   435
lemma rev_map: "rev (map f xs) = map f (rev xs)"
nipkow@13145
   436
by (induct xs) auto
wenzelm@13114
   437
wenzelm@13114
   438
lemma map_cong:
nipkow@13145
   439
"xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys"
nipkow@13145
   440
-- {* a congruence rule for @{text map} *}
nipkow@13145
   441
by (clarify, induct ys) auto
wenzelm@13114
   442
wenzelm@13142
   443
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
nipkow@13145
   444
by (cases xs) auto
wenzelm@13114
   445
wenzelm@13142
   446
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
nipkow@13145
   447
by (cases xs) auto
wenzelm@13114
   448
wenzelm@13114
   449
lemma map_eq_Cons:
nipkow@13145
   450
"(map f xs = y # ys) = (\<exists>x xs'. xs = x # xs' \<and> f x = y \<and> map f xs' = ys)"
nipkow@13145
   451
by (cases xs) auto
wenzelm@13114
   452
wenzelm@13114
   453
lemma map_injective:
nipkow@13145
   454
"!!xs. map f xs = map f ys ==> (\<forall>x y. f x = f y --> x = y) ==> xs = ys"
nipkow@13145
   455
by (induct ys) (auto simp add: map_eq_Cons)
wenzelm@13114
   456
wenzelm@13114
   457
lemma inj_mapI: "inj f ==> inj (map f)"
nipkow@13145
   458
by (rules dest: map_injective injD intro: injI)
wenzelm@13114
   459
wenzelm@13114
   460
lemma inj_mapD: "inj (map f) ==> inj f"
nipkow@13145
   461
apply (unfold inj_on_def)
nipkow@13145
   462
apply clarify
nipkow@13145
   463
apply (erule_tac x = "[x]" in ballE)
nipkow@13145
   464
 apply (erule_tac x = "[y]" in ballE)
nipkow@13145
   465
apply simp
nipkow@13145
   466
 apply blast
nipkow@13145
   467
apply blast
nipkow@13145
   468
done
wenzelm@13114
   469
wenzelm@13114
   470
lemma inj_map: "inj (map f) = inj f"
nipkow@13145
   471
by (blast dest: inj_mapD intro: inj_mapI)
wenzelm@13114
   472
wenzelm@13114
   473
wenzelm@13142
   474
subsection {* @{text rev} *}
wenzelm@13114
   475
wenzelm@13142
   476
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
nipkow@13145
   477
by (induct xs) auto
wenzelm@13114
   478
wenzelm@13142
   479
lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
nipkow@13145
   480
by (induct xs) auto
wenzelm@13114
   481
wenzelm@13142
   482
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
nipkow@13145
   483
by (induct xs) auto
wenzelm@13114
   484
wenzelm@13142
   485
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
nipkow@13145
   486
by (induct xs) auto
wenzelm@13114
   487
wenzelm@13142
   488
lemma rev_is_rev_conv [iff]: "!!ys. (rev xs = rev ys) = (xs = ys)"
nipkow@13145
   489
apply (induct xs)
nipkow@13145
   490
 apply force
nipkow@13145
   491
apply (case_tac ys)
nipkow@13145
   492
 apply simp
nipkow@13145
   493
apply force
nipkow@13145
   494
done
wenzelm@13114
   495
wenzelm@13142
   496
lemma rev_induct: "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
nipkow@13145
   497
apply(subst rev_rev_ident[symmetric])
nipkow@13145
   498
apply(rule_tac list = "rev xs" in list.induct, simp_all)
nipkow@13145
   499
done
wenzelm@13114
   500
nipkow@13145
   501
ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}-- "compatibility"
wenzelm@13114
   502
nipkow@13145
   503
lemma rev_exhaust: "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"
nipkow@13145
   504
by (induct xs rule: rev_induct) auto
wenzelm@13114
   505
wenzelm@13114
   506
wenzelm@13142
   507
subsection {* @{text set} *}
wenzelm@13114
   508
wenzelm@13142
   509
lemma finite_set [iff]: "finite (set xs)"
nipkow@13145
   510
by (induct xs) auto
wenzelm@13114
   511
wenzelm@13142
   512
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
nipkow@13145
   513
by (induct xs) auto
wenzelm@13114
   514
wenzelm@13142
   515
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
nipkow@13145
   516
by auto
wenzelm@13114
   517
wenzelm@13142
   518
lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
nipkow@13145
   519
by (induct xs) auto
wenzelm@13114
   520
wenzelm@13142
   521
lemma set_rev [simp]: "set (rev xs) = set xs"
nipkow@13145
   522
by (induct xs) auto
wenzelm@13114
   523
wenzelm@13142
   524
lemma set_map [simp]: "set (map f xs) = f`(set xs)"
nipkow@13145
   525
by (induct xs) auto
wenzelm@13114
   526
wenzelm@13142
   527
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
nipkow@13145
   528
by (induct xs) auto
wenzelm@13114
   529
wenzelm@13142
   530
lemma set_upt [simp]: "set[i..j(] = {k. i \<le> k \<and> k < j}"
nipkow@13145
   531
apply (induct j)
nipkow@13145
   532
 apply simp_all
nipkow@13145
   533
apply(erule ssubst)
nipkow@13145
   534
apply auto
nipkow@13145
   535
apply arith
nipkow@13145
   536
done
wenzelm@13114
   537
wenzelm@13142
   538
lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)"
nipkow@13145
   539
apply (induct xs)
nipkow@13145
   540
 apply simp
nipkow@13145
   541
apply simp
nipkow@13145
   542
apply (rule iffI)
nipkow@13145
   543
 apply (blast intro: eq_Nil_appendI Cons_eq_appendI)
nipkow@13145
   544
apply (erule exE)+
nipkow@13145
   545
apply (case_tac ys)
nipkow@13145
   546
apply auto
nipkow@13145
   547
done
wenzelm@13142
   548
wenzelm@13142
   549
lemma in_lists_conv_set: "(xs : lists A) = (\<forall>x \<in> set xs. x : A)"
nipkow@13145
   550
-- {* eliminate @{text lists} in favour of @{text set} *}
nipkow@13145
   551
by (induct xs) auto
wenzelm@13142
   552
wenzelm@13142
   553
lemma in_listsD [dest!]: "xs \<in> lists A ==> \<forall>x\<in>set xs. x \<in> A"
nipkow@13145
   554
by (rule in_lists_conv_set [THEN iffD1])
wenzelm@13142
   555
wenzelm@13142
   556
lemma in_listsI [intro!]: "\<forall>x\<in>set xs. x \<in> A ==> xs \<in> lists A"
nipkow@13145
   557
by (rule in_lists_conv_set [THEN iffD2])
wenzelm@13114
   558
wenzelm@13114
   559
wenzelm@13142
   560
subsection {* @{text mem} *}
wenzelm@13114
   561
wenzelm@13114
   562
lemma set_mem_eq: "(x mem xs) = (x : set xs)"
nipkow@13145
   563
by (induct xs) auto
wenzelm@13114
   564
wenzelm@13114
   565
wenzelm@13142
   566
subsection {* @{text list_all} *}
wenzelm@13114
   567
wenzelm@13142
   568
lemma list_all_conv: "list_all P xs = (\<forall>x \<in> set xs. P x)"
nipkow@13145
   569
by (induct xs) auto
wenzelm@13114
   570
wenzelm@13142
   571
lemma list_all_append [simp]:
nipkow@13145
   572
"list_all P (xs @ ys) = (list_all P xs \<and> list_all P ys)"
nipkow@13145
   573
by (induct xs) auto
wenzelm@13114
   574
wenzelm@13114
   575
wenzelm@13142
   576
subsection {* @{text filter} *}
wenzelm@13114
   577
wenzelm@13142
   578
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
nipkow@13145
   579
by (induct xs) auto
wenzelm@13114
   580
wenzelm@13142
   581
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
nipkow@13145
   582
by (induct xs) auto
wenzelm@13114
   583
wenzelm@13142
   584
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
nipkow@13145
   585
by (induct xs) auto
wenzelm@13114
   586
wenzelm@13142
   587
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
nipkow@13145
   588
by (induct xs) auto
wenzelm@13114
   589
wenzelm@13142
   590
lemma length_filter [simp]: "length (filter P xs) \<le> length xs"
nipkow@13145
   591
by (induct xs) (auto simp add: le_SucI)
wenzelm@13114
   592
wenzelm@13142
   593
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
nipkow@13145
   594
by auto
wenzelm@13114
   595
wenzelm@13114
   596
wenzelm@13142
   597
subsection {* @{text concat} *}
wenzelm@13114
   598
wenzelm@13142
   599
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
nipkow@13145
   600
by (induct xs) auto
wenzelm@13114
   601
wenzelm@13142
   602
lemma concat_eq_Nil_conv [iff]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
   603
by (induct xss) auto
wenzelm@13114
   604
wenzelm@13142
   605
lemma Nil_eq_concat_conv [iff]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
   606
by (induct xss) auto
wenzelm@13114
   607
wenzelm@13142
   608
lemma set_concat [simp]: "set (concat xs) = \<Union>(set ` set xs)"
nipkow@13145
   609
by (induct xs) auto
wenzelm@13114
   610
wenzelm@13142
   611
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
nipkow@13145
   612
by (induct xs) auto
wenzelm@13114
   613
wenzelm@13142
   614
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
nipkow@13145
   615
by (induct xs) auto
wenzelm@13114
   616
wenzelm@13142
   617
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
nipkow@13145
   618
by (induct xs) auto
wenzelm@13114
   619
wenzelm@13114
   620
wenzelm@13142
   621
subsection {* @{text nth} *}
wenzelm@13114
   622
wenzelm@13142
   623
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x"
nipkow@13145
   624
by auto
wenzelm@13114
   625
wenzelm@13142
   626
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n"
nipkow@13145
   627
by auto
wenzelm@13114
   628
wenzelm@13142
   629
declare nth.simps [simp del]
wenzelm@13114
   630
wenzelm@13114
   631
lemma nth_append:
nipkow@13145
   632
"!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
nipkow@13145
   633
apply(induct "xs")
nipkow@13145
   634
 apply simp
nipkow@13145
   635
apply (case_tac n)
nipkow@13145
   636
 apply auto
nipkow@13145
   637
done
wenzelm@13114
   638
wenzelm@13142
   639
lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)"
nipkow@13145
   640
apply(induct xs)
nipkow@13145
   641
 apply simp
nipkow@13145
   642
apply (case_tac n)
nipkow@13145
   643
 apply auto
nipkow@13145
   644
done
wenzelm@13114
   645
wenzelm@13142
   646
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
nipkow@13145
   647
apply (induct_tac xs)
nipkow@13145
   648
 apply simp
nipkow@13145
   649
apply simp
nipkow@13145
   650
apply safe
nipkow@13145
   651
apply (rule_tac x = 0 in exI)
nipkow@13145
   652
apply simp
nipkow@13145
   653
 apply (rule_tac x = "Suc i" in exI)
nipkow@13145
   654
 apply simp
nipkow@13145
   655
apply (case_tac i)
nipkow@13145
   656
 apply simp
nipkow@13145
   657
apply (rename_tac j)
nipkow@13145
   658
apply (rule_tac x = j in exI)
nipkow@13145
   659
apply simp
nipkow@13145
   660
done
wenzelm@13114
   661
nipkow@13145
   662
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"
nipkow@13145
   663
by (auto simp add: set_conv_nth)
wenzelm@13114
   664
wenzelm@13142
   665
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
nipkow@13145
   666
by (auto simp add: set_conv_nth)
wenzelm@13114
   667
wenzelm@13114
   668
lemma all_nth_imp_all_set:
nipkow@13145
   669
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x"
nipkow@13145
   670
by (auto simp add: set_conv_nth)
wenzelm@13114
   671
wenzelm@13114
   672
lemma all_set_conv_all_nth:
nipkow@13145
   673
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
nipkow@13145
   674
by (auto simp add: set_conv_nth)
wenzelm@13114
   675
wenzelm@13114
   676
wenzelm@13142
   677
subsection {* @{text list_update} *}
wenzelm@13114
   678
wenzelm@13142
   679
lemma length_list_update [simp]: "!!i. length(xs[i:=x]) = length xs"
nipkow@13145
   680
by (induct xs) (auto split: nat.split)
wenzelm@13114
   681
wenzelm@13114
   682
lemma nth_list_update:
nipkow@13145
   683
"!!i j. i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"
nipkow@13145
   684
by (induct xs) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
   685
wenzelm@13142
   686
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
nipkow@13145
   687
by (simp add: nth_list_update)
wenzelm@13114
   688
wenzelm@13142
   689
lemma nth_list_update_neq [simp]: "!!i j. i \<noteq> j ==> xs[i:=x]!j = xs!j"
nipkow@13145
   690
by (induct xs) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
   691
wenzelm@13142
   692
lemma list_update_overwrite [simp]:
nipkow@13145
   693
"!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"
nipkow@13145
   694
by (induct xs) (auto split: nat.split)
wenzelm@13114
   695
wenzelm@13114
   696
lemma list_update_same_conv:
nipkow@13145
   697
"!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
nipkow@13145
   698
by (induct xs) (auto split: nat.split)
wenzelm@13114
   699
wenzelm@13114
   700
lemma update_zip:
nipkow@13145
   701
"!!i xy xs. length xs = length ys ==>
nipkow@13145
   702
(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
nipkow@13145
   703
by (induct ys) (auto, case_tac xs, auto split: nat.split)
wenzelm@13114
   704
wenzelm@13114
   705
lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)"
nipkow@13145
   706
by (induct xs) (auto split: nat.split)
wenzelm@13114
   707
wenzelm@13114
   708
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
nipkow@13145
   709
by (blast dest!: set_update_subset_insert [THEN subsetD])
wenzelm@13114
   710
wenzelm@13114
   711
wenzelm@13142
   712
subsection {* @{text last} and @{text butlast} *}
wenzelm@13114
   713
wenzelm@13142
   714
lemma last_snoc [simp]: "last (xs @ [x]) = x"
nipkow@13145
   715
by (induct xs) auto
wenzelm@13114
   716
wenzelm@13142
   717
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
nipkow@13145
   718
by (induct xs) auto
wenzelm@13114
   719
wenzelm@13142
   720
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
nipkow@13145
   721
by (induct xs rule: rev_induct) auto
wenzelm@13114
   722
wenzelm@13114
   723
lemma butlast_append:
nipkow@13145
   724
"!!ys. butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
nipkow@13145
   725
by (induct xs) auto
wenzelm@13114
   726
wenzelm@13142
   727
lemma append_butlast_last_id [simp]:
nipkow@13145
   728
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
nipkow@13145
   729
by (induct xs) auto
wenzelm@13114
   730
wenzelm@13142
   731
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
nipkow@13145
   732
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
   733
wenzelm@13114
   734
lemma in_set_butlast_appendI:
nipkow@13145
   735
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
nipkow@13145
   736
by (auto dest: in_set_butlastD simp add: butlast_append)
wenzelm@13114
   737
wenzelm@13142
   738
wenzelm@13142
   739
subsection {* @{text take} and @{text drop} *}
wenzelm@13114
   740
wenzelm@13142
   741
lemma take_0 [simp]: "take 0 xs = []"
nipkow@13145
   742
by (induct xs) auto
wenzelm@13114
   743
wenzelm@13142
   744
lemma drop_0 [simp]: "drop 0 xs = xs"
nipkow@13145
   745
by (induct xs) auto
wenzelm@13114
   746
wenzelm@13142
   747
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
nipkow@13145
   748
by simp
wenzelm@13114
   749
wenzelm@13142
   750
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
nipkow@13145
   751
by simp
wenzelm@13114
   752
wenzelm@13142
   753
declare take_Cons [simp del] and drop_Cons [simp del]
wenzelm@13114
   754
wenzelm@13142
   755
lemma length_take [simp]: "!!xs. length (take n xs) = min (length xs) n"
nipkow@13145
   756
by (induct n) (auto, case_tac xs, auto)
wenzelm@13114
   757
wenzelm@13142
   758
lemma length_drop [simp]: "!!xs. length (drop n xs) = (length xs - n)"
nipkow@13145
   759
by (induct n) (auto, case_tac xs, auto)
wenzelm@13114
   760
wenzelm@13142
   761
lemma take_all [simp]: "!!xs. length xs <= n ==> take n xs = xs"
nipkow@13145
   762
by (induct n) (auto, case_tac xs, auto)
wenzelm@13114
   763
wenzelm@13142
   764
lemma drop_all [simp]: "!!xs. length xs <= n ==> drop n xs = []"
nipkow@13145
   765
by (induct n) (auto, case_tac xs, auto)
wenzelm@13114
   766
wenzelm@13142
   767
lemma take_append [simp]:
nipkow@13145
   768
"!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
nipkow@13145
   769
by (induct n) (auto, case_tac xs, auto)
wenzelm@13114
   770
wenzelm@13142
   771
lemma drop_append [simp]:
nipkow@13145
   772
"!!xs. drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
nipkow@13145
   773
by (induct n) (auto, case_tac xs, auto)
wenzelm@13114
   774
wenzelm@13142
   775
lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs"
nipkow@13145
   776
apply (induct m)
nipkow@13145
   777
 apply auto
nipkow@13145
   778
apply (case_tac xs)
nipkow@13145
   779
 apply auto
nipkow@13145
   780
apply (case_tac na)
nipkow@13145
   781
 apply auto
nipkow@13145
   782
done
wenzelm@13114
   783
wenzelm@13142
   784
lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs"
nipkow@13145
   785
apply (induct m)
nipkow@13145
   786
 apply auto
nipkow@13145
   787
apply (case_tac xs)
nipkow@13145
   788
 apply auto
nipkow@13145
   789
done
wenzelm@13114
   790
wenzelm@13114
   791
lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)"
nipkow@13145
   792
apply (induct m)
nipkow@13145
   793
 apply auto
nipkow@13145
   794
apply (case_tac xs)
nipkow@13145
   795
 apply auto
nipkow@13145
   796
done
wenzelm@13114
   797
wenzelm@13142
   798
lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs"
nipkow@13145
   799
apply (induct n)
nipkow@13145
   800
 apply auto
nipkow@13145
   801
apply (case_tac xs)
nipkow@13145
   802
 apply auto
nipkow@13145
   803
done
wenzelm@13114
   804
wenzelm@13114
   805
lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)"
nipkow@13145
   806
apply (induct n)
nipkow@13145
   807
 apply auto
nipkow@13145
   808
apply (case_tac xs)
nipkow@13145
   809
 apply auto
nipkow@13145
   810
done
wenzelm@13114
   811
wenzelm@13142
   812
lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)"
nipkow@13145
   813
apply (induct n)
nipkow@13145
   814
 apply auto
nipkow@13145
   815
apply (case_tac xs)
nipkow@13145
   816
 apply auto
nipkow@13145
   817
done
wenzelm@13114
   818
wenzelm@13114
   819
lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)"
nipkow@13145
   820
apply (induct xs)
nipkow@13145
   821
 apply auto
nipkow@13145
   822
apply (case_tac i)
nipkow@13145
   823
 apply auto
nipkow@13145
   824
done
wenzelm@13114
   825
wenzelm@13114
   826
lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)"
nipkow@13145
   827
apply (induct xs)
nipkow@13145
   828
 apply auto
nipkow@13145
   829
apply (case_tac i)
nipkow@13145
   830
 apply auto
nipkow@13145
   831
done
wenzelm@13114
   832
wenzelm@13142
   833
lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i"
nipkow@13145
   834
apply (induct xs)
nipkow@13145
   835
 apply auto
nipkow@13145
   836
apply (case_tac n)
nipkow@13145
   837
 apply(blast )
nipkow@13145
   838
apply (case_tac i)
nipkow@13145
   839
 apply auto
nipkow@13145
   840
done
wenzelm@13114
   841
wenzelm@13142
   842
lemma nth_drop [simp]:
nipkow@13145
   843
"!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
nipkow@13145
   844
apply (induct n)
nipkow@13145
   845
 apply auto
nipkow@13145
   846
apply (case_tac xs)
nipkow@13145
   847
 apply auto
nipkow@13145
   848
done
nipkow@3507
   849
wenzelm@13114
   850
lemma append_eq_conv_conj:
nipkow@13145
   851
"!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
nipkow@13145
   852
apply(induct xs)
nipkow@13145
   853
 apply simp
nipkow@13145
   854
apply clarsimp
nipkow@13145
   855
apply (case_tac zs)
nipkow@13145
   856
apply auto
nipkow@13145
   857
done
wenzelm@13142
   858
wenzelm@13114
   859
wenzelm@13142
   860
subsection {* @{text takeWhile} and @{text dropWhile} *}
wenzelm@13114
   861
wenzelm@13142
   862
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
nipkow@13145
   863
by (induct xs) auto
wenzelm@13114
   864
wenzelm@13142
   865
lemma takeWhile_append1 [simp]:
nipkow@13145
   866
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"
nipkow@13145
   867
by (induct xs) auto
wenzelm@13114
   868
wenzelm@13142
   869
lemma takeWhile_append2 [simp]:
nipkow@13145
   870
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
nipkow@13145
   871
by (induct xs) auto
wenzelm@13114
   872
wenzelm@13142
   873
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
nipkow@13145
   874
by (induct xs) auto
wenzelm@13114
   875
wenzelm@13142
   876
lemma dropWhile_append1 [simp]:
nipkow@13145
   877
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
nipkow@13145
   878
by (induct xs) auto
wenzelm@13114
   879
wenzelm@13142
   880
lemma dropWhile_append2 [simp]:
nipkow@13145
   881
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
nipkow@13145
   882
by (induct xs) auto
wenzelm@13114
   883
wenzelm@13142
   884
lemma set_take_whileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
nipkow@13145
   885
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
   886
wenzelm@13114
   887
wenzelm@13142
   888
subsection {* @{text zip} *}
wenzelm@13114
   889
wenzelm@13142
   890
lemma zip_Nil [simp]: "zip [] ys = []"
nipkow@13145
   891
by (induct ys) auto
wenzelm@13114
   892
wenzelm@13142
   893
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
nipkow@13145
   894
by simp
wenzelm@13114
   895
wenzelm@13142
   896
declare zip_Cons [simp del]
wenzelm@13114
   897
wenzelm@13142
   898
lemma length_zip [simp]:
nipkow@13145
   899
"!!xs. length (zip xs ys) = min (length xs) (length ys)"
nipkow@13145
   900
apply(induct ys)
nipkow@13145
   901
 apply simp
nipkow@13145
   902
apply (case_tac xs)
nipkow@13145
   903
 apply auto
nipkow@13145
   904
done
wenzelm@13114
   905
wenzelm@13114
   906
lemma zip_append1:
nipkow@13145
   907
"!!xs. zip (xs @ ys) zs =
nipkow@13145
   908
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
nipkow@13145
   909
apply (induct zs)
nipkow@13145
   910
 apply simp
nipkow@13145
   911
apply (case_tac xs)
nipkow@13145
   912
 apply simp_all
nipkow@13145
   913
done
wenzelm@13114
   914
wenzelm@13114
   915
lemma zip_append2:
nipkow@13145
   916
"!!ys. zip xs (ys @ zs) =
nipkow@13145
   917
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
nipkow@13145
   918
apply (induct xs)
nipkow@13145
   919
 apply simp
nipkow@13145
   920
apply (case_tac ys)
nipkow@13145
   921
 apply simp_all
nipkow@13145
   922
done
wenzelm@13114
   923
wenzelm@13142
   924
lemma zip_append [simp]:
wenzelm@13142
   925
 "[| length xs = length us; length ys = length vs |] ==>
nipkow@13145
   926
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
nipkow@13145
   927
by (simp add: zip_append1)
wenzelm@13114
   928
wenzelm@13114
   929
lemma zip_rev:
nipkow@13145
   930
"!!xs. length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
nipkow@13145
   931
apply(induct ys)
nipkow@13145
   932
 apply simp
nipkow@13145
   933
apply (case_tac xs)
nipkow@13145
   934
 apply simp_all
nipkow@13145
   935
done
wenzelm@13114
   936
wenzelm@13142
   937
lemma nth_zip [simp]:
nipkow@13145
   938
"!!i xs. [| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"
nipkow@13145
   939
apply (induct ys)
nipkow@13145
   940
 apply simp
nipkow@13145
   941
apply (case_tac xs)
nipkow@13145
   942
 apply (simp_all add: nth.simps split: nat.split)
nipkow@13145
   943
done
wenzelm@13114
   944
wenzelm@13114
   945
lemma set_zip:
nipkow@13145
   946
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
nipkow@13145
   947
by (simp add: set_conv_nth cong: rev_conj_cong)
wenzelm@13114
   948
wenzelm@13114
   949
lemma zip_update:
nipkow@13145
   950
"length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
nipkow@13145
   951
by (rule sym, simp add: update_zip)
wenzelm@13114
   952
wenzelm@13142
   953
lemma zip_replicate [simp]:
nipkow@13145
   954
"!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
nipkow@13145
   955
apply (induct i)
nipkow@13145
   956
 apply auto
nipkow@13145
   957
apply (case_tac j)
nipkow@13145
   958
 apply auto
nipkow@13145
   959
done
wenzelm@13114
   960
wenzelm@13142
   961
wenzelm@13142
   962
subsection {* @{text list_all2} *}
wenzelm@13114
   963
wenzelm@13114
   964
lemma list_all2_lengthD: "list_all2 P xs ys ==> length xs = length ys"
nipkow@13145
   965
by (simp add: list_all2_def)
wenzelm@13114
   966
wenzelm@13142
   967
lemma list_all2_Nil [iff]: "list_all2 P [] ys = (ys = [])"
nipkow@13145
   968
by (simp add: list_all2_def)
wenzelm@13114
   969
wenzelm@13142
   970
lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])"
nipkow@13145
   971
by (simp add: list_all2_def)
wenzelm@13114
   972
wenzelm@13142
   973
lemma list_all2_Cons [iff]:
nipkow@13145
   974
"list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
nipkow@13145
   975
by (auto simp add: list_all2_def)
wenzelm@13114
   976
wenzelm@13114
   977
lemma list_all2_Cons1:
nipkow@13145
   978
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
nipkow@13145
   979
by (cases ys) auto
wenzelm@13114
   980
wenzelm@13114
   981
lemma list_all2_Cons2:
nipkow@13145
   982
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
nipkow@13145
   983
by (cases xs) auto
wenzelm@13114
   984
wenzelm@13142
   985
lemma list_all2_rev [iff]:
nipkow@13145
   986
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
nipkow@13145
   987
by (simp add: list_all2_def zip_rev cong: conj_cong)
wenzelm@13114
   988
wenzelm@13114
   989
lemma list_all2_append1:
nipkow@13145
   990
"list_all2 P (xs @ ys) zs =
nipkow@13145
   991
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
nipkow@13145
   992
list_all2 P xs us \<and> list_all2 P ys vs)"
nipkow@13145
   993
apply (simp add: list_all2_def zip_append1)
nipkow@13145
   994
apply (rule iffI)
nipkow@13145
   995
 apply (rule_tac x = "take (length xs) zs" in exI)
nipkow@13145
   996
 apply (rule_tac x = "drop (length xs) zs" in exI)
nipkow@13145
   997
 apply (force split: nat_diff_split simp add: min_def)
nipkow@13145
   998
apply clarify
nipkow@13145
   999
apply (simp add: ball_Un)
nipkow@13145
  1000
done
wenzelm@13114
  1001
wenzelm@13114
  1002
lemma list_all2_append2:
nipkow@13145
  1003
"list_all2 P xs (ys @ zs) =
nipkow@13145
  1004
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
nipkow@13145
  1005
list_all2 P us ys \<and> list_all2 P vs zs)"
nipkow@13145
  1006
apply (simp add: list_all2_def zip_append2)
nipkow@13145
  1007
apply (rule iffI)
nipkow@13145
  1008
 apply (rule_tac x = "take (length ys) xs" in exI)
nipkow@13145
  1009
 apply (rule_tac x = "drop (length ys) xs" in exI)
nipkow@13145
  1010
 apply (force split: nat_diff_split simp add: min_def)
nipkow@13145
  1011
apply clarify
nipkow@13145
  1012
apply (simp add: ball_Un)
nipkow@13145
  1013
done
wenzelm@13114
  1014
wenzelm@13114
  1015
lemma list_all2_conv_all_nth:
nipkow@13145
  1016
"list_all2 P xs ys =
nipkow@13145
  1017
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
nipkow@13145
  1018
by (force simp add: list_all2_def set_zip)
wenzelm@13114
  1019
wenzelm@13114
  1020
lemma list_all2_trans[rule_format]:
nipkow@13145
  1021
"\<forall>a b c. P1 a b --> P2 b c --> P3 a c ==>
nipkow@13145
  1022
\<forall>bs cs. list_all2 P1 as bs --> list_all2 P2 bs cs --> list_all2 P3 as cs"
nipkow@13145
  1023
apply(induct_tac as)
nipkow@13145
  1024
 apply simp
nipkow@13145
  1025
apply(rule allI)
nipkow@13145
  1026
apply(induct_tac bs)
nipkow@13145
  1027
 apply simp
nipkow@13145
  1028
apply(rule allI)
nipkow@13145
  1029
apply(induct_tac cs)
nipkow@13145
  1030
 apply auto
nipkow@13145
  1031
done
wenzelm@13142
  1032
wenzelm@13142
  1033
wenzelm@13142
  1034
subsection {* @{text foldl} *}
wenzelm@13142
  1035
wenzelm@13142
  1036
lemma foldl_append [simp]:
nipkow@13145
  1037
"!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
nipkow@13145
  1038
by (induct xs) auto
wenzelm@13142
  1039
wenzelm@13142
  1040
text {*
nipkow@13145
  1041
Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
nipkow@13145
  1042
difficult to use because it requires an additional transitivity step.
wenzelm@13142
  1043
*}
wenzelm@13142
  1044
wenzelm@13142
  1045
lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl (op +) n ns"
nipkow@13145
  1046
by (induct ns) auto
wenzelm@13142
  1047
wenzelm@13142
  1048
lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl (op +) 0 ns"
nipkow@13145
  1049
by (force intro: start_le_sum simp add: in_set_conv_decomp)
wenzelm@13142
  1050
wenzelm@13142
  1051
lemma sum_eq_0_conv [iff]:
nipkow@13145
  1052
"!!m::nat. (foldl (op +) m ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
nipkow@13145
  1053
by (induct ns) auto
wenzelm@13114
  1054
wenzelm@13114
  1055
wenzelm@13142
  1056
subsection {* @{text upto} *}
wenzelm@13114
  1057
wenzelm@13142
  1058
lemma upt_rec: "[i..j(] = (if i<j then i#[Suc i..j(] else [])"
nipkow@13145
  1059
-- {* Does not terminate! *}
nipkow@13145
  1060
by (induct j) auto
wenzelm@13142
  1061
wenzelm@13142
  1062
lemma upt_conv_Nil [simp]: "j <= i ==> [i..j(] = []"
nipkow@13145
  1063
by (subst upt_rec) simp
wenzelm@13114
  1064
wenzelm@13142
  1065
lemma upt_Suc_append: "i <= j ==> [i..(Suc j)(] = [i..j(]@[j]"
nipkow@13145
  1066
-- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
nipkow@13145
  1067
by simp
wenzelm@13114
  1068
wenzelm@13142
  1069
lemma upt_conv_Cons: "i < j ==> [i..j(] = i # [Suc i..j(]"
nipkow@13145
  1070
apply(rule trans)
nipkow@13145
  1071
apply(subst upt_rec)
nipkow@13145
  1072
 prefer 2 apply(rule refl)
nipkow@13145
  1073
apply simp
nipkow@13145
  1074
done
wenzelm@13114
  1075
wenzelm@13142
  1076
lemma upt_add_eq_append: "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]"
nipkow@13145
  1077
-- {* LOOPS as a simprule, since @{text "j <= j"}. *}
nipkow@13145
  1078
by (induct k) auto
wenzelm@13114
  1079
wenzelm@13142
  1080
lemma length_upt [simp]: "length [i..j(] = j - i"
nipkow@13145
  1081
by (induct j) (auto simp add: Suc_diff_le)
wenzelm@13114
  1082
wenzelm@13142
  1083
lemma nth_upt [simp]: "i + k < j ==> [i..j(] ! k = i + k"
nipkow@13145
  1084
apply (induct j)
nipkow@13145
  1085
apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
nipkow@13145
  1086
done
wenzelm@13114
  1087
wenzelm@13142
  1088
lemma take_upt [simp]: "!!i. i+m <= n ==> take m [i..n(] = [i..i+m(]"
nipkow@13145
  1089
apply (induct m)
nipkow@13145
  1090
 apply simp
nipkow@13145
  1091
apply (subst upt_rec)
nipkow@13145
  1092
apply (rule sym)
nipkow@13145
  1093
apply (subst upt_rec)
nipkow@13145
  1094
apply (simp del: upt.simps)
nipkow@13145
  1095
done
nipkow@3507
  1096
wenzelm@13114
  1097
lemma map_Suc_upt: "map Suc [m..n(] = [Suc m..n]"
nipkow@13145
  1098
by (induct n) auto
wenzelm@13114
  1099
wenzelm@13114
  1100
lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..n(]) ! i = f(m+i)"
nipkow@13145
  1101
apply (induct n m rule: diff_induct)
nipkow@13145
  1102
prefer 3 apply (subst map_Suc_upt[symmetric])
nipkow@13145
  1103
apply (auto simp add: less_diff_conv nth_upt)
nipkow@13145
  1104
done
wenzelm@13114
  1105
wenzelm@13142
  1106
lemma nth_take_lemma [rule_format]:
nipkow@13145
  1107
"ALL xs ys. k <= length xs --> k <= length ys
nipkow@13145
  1108
--> (ALL i. i < k --> xs!i = ys!i)
nipkow@13145
  1109
--> take k xs = take k ys"
nipkow@13145
  1110
apply (induct k)
nipkow@13145
  1111
apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib)
nipkow@13145
  1112
apply clarify
nipkow@13145
  1113
txt {* Both lists must be non-empty *}
nipkow@13145
  1114
apply (case_tac xs)
nipkow@13145
  1115
 apply simp
nipkow@13145
  1116
apply (case_tac ys)
nipkow@13145
  1117
 apply clarify
nipkow@13145
  1118
 apply (simp (no_asm_use))
nipkow@13145
  1119
apply clarify
nipkow@13145
  1120
txt {* prenexing's needed, not miniscoping *}
nipkow@13145
  1121
apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
nipkow@13145
  1122
apply blast
nipkow@13145
  1123
done
wenzelm@13114
  1124
wenzelm@13114
  1125
lemma nth_equalityI:
wenzelm@13114
  1126
 "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
nipkow@13145
  1127
apply (frule nth_take_lemma [OF le_refl eq_imp_le])
nipkow@13145
  1128
apply (simp_all add: take_all)
nipkow@13145
  1129
done
wenzelm@13142
  1130
wenzelm@13142
  1131
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
nipkow@13145
  1132
-- {* The famous take-lemma. *}
nipkow@13145
  1133
apply (drule_tac x = "max (length xs) (length ys)" in spec)
nipkow@13145
  1134
apply (simp add: le_max_iff_disj take_all)
nipkow@13145
  1135
done
wenzelm@13142
  1136
wenzelm@13142
  1137
wenzelm@13142
  1138
subsection {* @{text "distinct"} and @{text remdups} *}
wenzelm@13142
  1139
wenzelm@13142
  1140
lemma distinct_append [simp]:
nipkow@13145
  1141
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
nipkow@13145
  1142
by (induct xs) auto
wenzelm@13142
  1143
wenzelm@13142
  1144
lemma set_remdups [simp]: "set (remdups xs) = set xs"
nipkow@13145
  1145
by (induct xs) (auto simp add: insert_absorb)
wenzelm@13142
  1146
wenzelm@13142
  1147
lemma distinct_remdups [iff]: "distinct (remdups xs)"
nipkow@13145
  1148
by (induct xs) auto
wenzelm@13142
  1149
wenzelm@13142
  1150
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"
nipkow@13145
  1151
by (induct xs) auto
wenzelm@13114
  1152
wenzelm@13142
  1153
text {*
nipkow@13145
  1154
It is best to avoid this indexed version of distinct, but sometimes
nipkow@13145
  1155
it is useful. *}
wenzelm@13142
  1156
lemma distinct_conv_nth:
nipkow@13145
  1157
"distinct xs = (\<forall>i j. i < size xs \<and> j < size xs \<and> i \<noteq> j --> xs!i \<noteq> xs!j)"
nipkow@13145
  1158
apply (induct_tac xs)
nipkow@13145
  1159
 apply simp
nipkow@13145
  1160
apply simp
nipkow@13145
  1161
apply (rule iffI)
nipkow@13145
  1162
 apply clarsimp
nipkow@13145
  1163
 apply (case_tac i)
nipkow@13145
  1164
apply (case_tac j)
nipkow@13145
  1165
 apply simp
nipkow@13145
  1166
apply (simp add: set_conv_nth)
nipkow@13145
  1167
 apply (case_tac j)
nipkow@13145
  1168
apply (clarsimp simp add: set_conv_nth)
nipkow@13145
  1169
 apply simp
nipkow@13145
  1170
apply (rule conjI)
nipkow@13145
  1171
 apply (clarsimp simp add: set_conv_nth)
nipkow@13145
  1172
 apply (erule_tac x = 0 in allE)
nipkow@13145
  1173
 apply (erule_tac x = "Suc i" in allE)
nipkow@13145
  1174
 apply simp
nipkow@13145
  1175
apply clarsimp
nipkow@13145
  1176
apply (erule_tac x = "Suc i" in allE)
nipkow@13145
  1177
apply (erule_tac x = "Suc j" in allE)
nipkow@13145
  1178
apply simp
nipkow@13145
  1179
done
wenzelm@13114
  1180
wenzelm@13114
  1181
wenzelm@13142
  1182
subsection {* @{text replicate} *}
wenzelm@13114
  1183
wenzelm@13142
  1184
lemma length_replicate [simp]: "length (replicate n x) = n"
nipkow@13145
  1185
by (induct n) auto
nipkow@13124
  1186
wenzelm@13142
  1187
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
nipkow@13145
  1188
by (induct n) auto
wenzelm@13114
  1189
wenzelm@13114
  1190
lemma replicate_app_Cons_same:
nipkow@13145
  1191
"(replicate n x) @ (x # xs) = x # replicate n x @ xs"
nipkow@13145
  1192
by (induct n) auto
wenzelm@13114
  1193
wenzelm@13142
  1194
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
nipkow@13145
  1195
apply(induct n)
nipkow@13145
  1196
 apply simp
nipkow@13145
  1197
apply (simp add: replicate_app_Cons_same)
nipkow@13145
  1198
done
wenzelm@13114
  1199
wenzelm@13142
  1200
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
nipkow@13145
  1201
by (induct n) auto
wenzelm@13114
  1202
wenzelm@13142
  1203
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"
nipkow@13145
  1204
by (induct n) auto
wenzelm@13114
  1205
wenzelm@13142
  1206
lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"
nipkow@13145
  1207
by (induct n) auto
wenzelm@13114
  1208
wenzelm@13142
  1209
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"
nipkow@13145
  1210
by (atomize (full), induct n) auto
wenzelm@13114
  1211
wenzelm@13142
  1212
lemma nth_replicate[simp]: "!!i. i < n ==> (replicate n x)!i = x"
nipkow@13145
  1213
apply(induct n)
nipkow@13145
  1214
 apply simp
nipkow@13145
  1215
apply (simp add: nth_Cons split: nat.split)
nipkow@13145
  1216
done
wenzelm@13114
  1217
wenzelm@13142
  1218
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
nipkow@13145
  1219
by (induct n) auto
wenzelm@13114
  1220
wenzelm@13142
  1221
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
nipkow@13145
  1222
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
wenzelm@13114
  1223
wenzelm@13142
  1224
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
nipkow@13145
  1225
by auto
wenzelm@13114
  1226
wenzelm@13142
  1227
lemma in_set_replicateD: "x : set (replicate n y) ==> x = y"
nipkow@13145
  1228
by (simp add: set_replicate_conv_if split: split_if_asm)
wenzelm@13114
  1229
wenzelm@13114
  1230
wenzelm@13142
  1231
subsection {* Lexcicographic orderings on lists *}
nipkow@3507
  1232
wenzelm@13142
  1233
lemma wf_lexn: "wf r ==> wf (lexn r n)"
nipkow@13145
  1234
apply (induct_tac n)
nipkow@13145
  1235
 apply simp
nipkow@13145
  1236
apply simp
nipkow@13145
  1237
apply(rule wf_subset)
nipkow@13145
  1238
 prefer 2 apply (rule Int_lower1)
nipkow@13145
  1239
apply(rule wf_prod_fun_image)
nipkow@13145
  1240
 prefer 2 apply (rule injI)
nipkow@13145
  1241
apply auto
nipkow@13145
  1242
done
wenzelm@13114
  1243
wenzelm@13114
  1244
lemma lexn_length:
nipkow@13145
  1245
"!!xs ys. (xs, ys) : lexn r n ==> length xs = n \<and> length ys = n"
nipkow@13145
  1246
by (induct n) auto
wenzelm@13114
  1247
wenzelm@13142
  1248
lemma wf_lex [intro!]: "wf r ==> wf (lex r)"
nipkow@13145
  1249
apply (unfold lex_def)
nipkow@13145
  1250
apply (rule wf_UN)
nipkow@13145
  1251
apply (blast intro: wf_lexn)
nipkow@13145
  1252
apply clarify
nipkow@13145
  1253
apply (rename_tac m n)
nipkow@13145
  1254
apply (subgoal_tac "m \<noteq> n")
nipkow@13145
  1255
 prefer 2 apply blast
nipkow@13145
  1256
apply (blast dest: lexn_length not_sym)
nipkow@13145
  1257
done
wenzelm@13114
  1258
wenzelm@13114
  1259
lemma lexn_conv:
nipkow@13145
  1260
"lexn r n =
nipkow@13145
  1261
{(xs,ys). length xs = n \<and> length ys = n \<and>
nipkow@13145
  1262
(\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}"
nipkow@13145
  1263
apply (induct_tac n)
nipkow@13145
  1264
 apply simp
nipkow@13145
  1265
 apply blast
nipkow@13145
  1266
apply (simp add: image_Collect lex_prod_def)
nipkow@13145
  1267
apply auto
nipkow@13145
  1268
apply blast
nipkow@13145
  1269
 apply (rename_tac a xys x xs' y ys')
nipkow@13145
  1270
 apply (rule_tac x = "a # xys" in exI)
nipkow@13145
  1271
 apply simp
nipkow@13145
  1272
apply (case_tac xys)
nipkow@13145
  1273
 apply simp_all
nipkow@13145
  1274
apply blast
nipkow@13145
  1275
done
wenzelm@13114
  1276
wenzelm@13114
  1277
lemma lex_conv:
nipkow@13145
  1278
"lex r =
nipkow@13145
  1279
{(xs,ys). length xs = length ys \<and>
nipkow@13145
  1280
(\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}"
nipkow@13145
  1281
by (force simp add: lex_def lexn_conv)
wenzelm@13114
  1282
wenzelm@13142
  1283
lemma wf_lexico [intro!]: "wf r ==> wf (lexico r)"
nipkow@13145
  1284
by (unfold lexico_def) blast
wenzelm@13114
  1285
wenzelm@13114
  1286
lemma lexico_conv:
nipkow@13145
  1287
"lexico r = {(xs,ys). length xs < length ys |
nipkow@13145
  1288
length xs = length ys \<and> (xs, ys) : lex r}"
nipkow@13145
  1289
by (simp add: lexico_def diag_def lex_prod_def measure_def inv_image_def)
wenzelm@13114
  1290
wenzelm@13142
  1291
lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r"
nipkow@13145
  1292
by (simp add: lex_conv)
wenzelm@13114
  1293
wenzelm@13142
  1294
lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r"
nipkow@13145
  1295
by (simp add:lex_conv)
wenzelm@13114
  1296
wenzelm@13142
  1297
lemma Cons_in_lex [iff]:
nipkow@13145
  1298
"((x # xs, y # ys) : lex r) =
nipkow@13145
  1299
((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)"
nipkow@13145
  1300
apply (simp add: lex_conv)
nipkow@13145
  1301
apply (rule iffI)
nipkow@13145
  1302
 prefer 2 apply (blast intro: Cons_eq_appendI)
nipkow@13145
  1303
apply clarify
nipkow@13145
  1304
apply (case_tac xys)
nipkow@13145
  1305
 apply simp
nipkow@13145
  1306
apply simp
nipkow@13145
  1307
apply blast
nipkow@13145
  1308
done
wenzelm@13114
  1309
wenzelm@13114
  1310
wenzelm@13142
  1311
subsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
wenzelm@13114
  1312
wenzelm@13142
  1313
lemma sublist_empty [simp]: "sublist xs {} = []"
nipkow@13145
  1314
by (auto simp add: sublist_def)
wenzelm@13114
  1315
wenzelm@13142
  1316
lemma sublist_nil [simp]: "sublist [] A = []"
nipkow@13145
  1317
by (auto simp add: sublist_def)
wenzelm@13114
  1318
wenzelm@13114
  1319
lemma sublist_shift_lemma:
nipkow@13145
  1320
"map fst [p:zip xs [i..i + length xs(] . snd p : A] =
nipkow@13145
  1321
map fst [p:zip xs [0..length xs(] . snd p + i : A]"
nipkow@13145
  1322
by (induct xs rule: rev_induct) (simp_all add: add_commute)
wenzelm@13114
  1323
wenzelm@13114
  1324
lemma sublist_append:
nipkow@13145
  1325
"sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
nipkow@13145
  1326
apply (unfold sublist_def)
nipkow@13145
  1327
apply (induct l' rule: rev_induct)
nipkow@13145
  1328
 apply simp
nipkow@13145
  1329
apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
nipkow@13145
  1330
apply (simp add: add_commute)
nipkow@13145
  1331
done
wenzelm@13114
  1332
wenzelm@13114
  1333
lemma sublist_Cons:
nipkow@13145
  1334
"sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
nipkow@13145
  1335
apply (induct l rule: rev_induct)
nipkow@13145
  1336
 apply (simp add: sublist_def)
nipkow@13145
  1337
apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)
nipkow@13145
  1338
done
wenzelm@13114
  1339
wenzelm@13142
  1340
lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"
nipkow@13145
  1341
by (simp add: sublist_Cons)
wenzelm@13114
  1342
wenzelm@13142
  1343
lemma sublist_upt_eq_take [simp]: "sublist l {..n(} = take n l"
nipkow@13145
  1344
apply (induct l rule: rev_induct)
nipkow@13145
  1345
 apply simp
nipkow@13145
  1346
apply (simp split: nat_diff_split add: sublist_append)
nipkow@13145
  1347
done
wenzelm@13114
  1348
wenzelm@13114
  1349
wenzelm@13142
  1350
lemma take_Cons':
nipkow@13145
  1351
"take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
nipkow@13145
  1352
by (cases n) simp_all
wenzelm@13114
  1353
wenzelm@13142
  1354
lemma drop_Cons':
nipkow@13145
  1355
"drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
nipkow@13145
  1356
by (cases n) simp_all
wenzelm@13114
  1357
wenzelm@13142
  1358
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
nipkow@13145
  1359
by (cases n) simp_all
wenzelm@13142
  1360
nipkow@13145
  1361
lemmas [simp] = take_Cons'[of "number_of v",standard]
nipkow@13145
  1362
                drop_Cons'[of "number_of v",standard]
nipkow@13145
  1363
                nth_Cons'[of _ _ "number_of v",standard]
nipkow@3507
  1364
wenzelm@13122
  1365
end