src/HOL/List.thy
author nipkow
Tue Mar 15 22:04:02 2011 +0100 (2011-03-15)
changeset 41986 95a67e3f29ad
parent 41842 d8f76db6a207
child 42057 3eba96ff3d3e
permissions -rw-r--r--
added lemma
wenzelm@13462
     1
(*  Title:      HOL/List.thy
wenzelm@13462
     2
    Author:     Tobias Nipkow
clasohm@923
     3
*)
clasohm@923
     4
wenzelm@13114
     5
header {* The datatype of finite lists *}
wenzelm@13122
     6
nipkow@15131
     7
theory List
haftmann@40195
     8
imports Plain Presburger Recdef Code_Numeral Quotient ATP
bulwahn@41463
     9
uses
bulwahn@41463
    10
  ("Tools/list_code.ML")
bulwahn@41463
    11
  ("Tools/list_to_set_comprehension.ML")
nipkow@15131
    12
begin
clasohm@923
    13
wenzelm@13142
    14
datatype 'a list =
wenzelm@13366
    15
    Nil    ("[]")
wenzelm@13366
    16
  | Cons 'a  "'a list"    (infixr "#" 65)
clasohm@923
    17
haftmann@34941
    18
syntax
haftmann@34941
    19
  -- {* list Enumeration *}
wenzelm@35115
    20
  "_list" :: "args => 'a list"    ("[(_)]")
haftmann@34941
    21
haftmann@34941
    22
translations
haftmann@34941
    23
  "[x, xs]" == "x#[xs]"
haftmann@34941
    24
  "[x]" == "x#[]"
haftmann@34941
    25
wenzelm@35115
    26
wenzelm@35115
    27
subsection {* Basic list processing functions *}
nipkow@15302
    28
haftmann@34941
    29
primrec
haftmann@34941
    30
  hd :: "'a list \<Rightarrow> 'a" where
haftmann@34941
    31
  "hd (x # xs) = x"
haftmann@34941
    32
haftmann@34941
    33
primrec
haftmann@34941
    34
  tl :: "'a list \<Rightarrow> 'a list" where
haftmann@34941
    35
    "tl [] = []"
haftmann@34941
    36
  | "tl (x # xs) = xs"
haftmann@34941
    37
haftmann@34941
    38
primrec
haftmann@34941
    39
  last :: "'a list \<Rightarrow> 'a" where
haftmann@34941
    40
  "last (x # xs) = (if xs = [] then x else last xs)"
haftmann@34941
    41
haftmann@34941
    42
primrec
haftmann@34941
    43
  butlast :: "'a list \<Rightarrow> 'a list" where
haftmann@34941
    44
    "butlast []= []"
haftmann@34941
    45
  | "butlast (x # xs) = (if xs = [] then [] else x # butlast xs)"
haftmann@34941
    46
haftmann@34941
    47
primrec
haftmann@34941
    48
  set :: "'a list \<Rightarrow> 'a set" where
haftmann@34941
    49
    "set [] = {}"
haftmann@34941
    50
  | "set (x # xs) = insert x (set xs)"
haftmann@34941
    51
haftmann@34941
    52
primrec
haftmann@34941
    53
  map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list" where
haftmann@34941
    54
    "map f [] = []"
haftmann@34941
    55
  | "map f (x # xs) = f x # map f xs"
haftmann@34941
    56
haftmann@34941
    57
primrec
haftmann@34941
    58
  append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65) where
haftmann@34941
    59
    append_Nil:"[] @ ys = ys"
haftmann@34941
    60
  | append_Cons: "(x#xs) @ ys = x # xs @ ys"
haftmann@34941
    61
haftmann@34941
    62
primrec
haftmann@34941
    63
  rev :: "'a list \<Rightarrow> 'a list" where
haftmann@34941
    64
    "rev [] = []"
haftmann@34941
    65
  | "rev (x # xs) = rev xs @ [x]"
haftmann@34941
    66
haftmann@34941
    67
primrec
haftmann@34941
    68
  filter:: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34941
    69
    "filter P [] = []"
haftmann@34941
    70
  | "filter P (x # xs) = (if P x then x # filter P xs else filter P xs)"
haftmann@34941
    71
haftmann@34941
    72
syntax
haftmann@34941
    73
  -- {* Special syntax for filter *}
wenzelm@35115
    74
  "_filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_<-_./ _])")
haftmann@34941
    75
haftmann@34941
    76
translations
haftmann@34941
    77
  "[x<-xs . P]"== "CONST filter (%x. P) xs"
haftmann@34941
    78
haftmann@34941
    79
syntax (xsymbols)
wenzelm@35115
    80
  "_filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
haftmann@34941
    81
syntax (HTML output)
wenzelm@35115
    82
  "_filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
haftmann@34941
    83
haftmann@34941
    84
primrec
haftmann@34941
    85
  foldl :: "('b \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b" where
haftmann@34941
    86
    foldl_Nil: "foldl f a [] = a"
haftmann@34941
    87
  | foldl_Cons: "foldl f a (x # xs) = foldl f (f a x) xs"
haftmann@34941
    88
haftmann@34941
    89
primrec
haftmann@34941
    90
  foldr :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
haftmann@34941
    91
    "foldr f [] a = a"
haftmann@34941
    92
  | "foldr f (x # xs) a = f x (foldr f xs a)"
haftmann@34941
    93
haftmann@34941
    94
primrec
haftmann@34941
    95
  concat:: "'a list list \<Rightarrow> 'a list" where
haftmann@34941
    96
    "concat [] = []"
haftmann@34941
    97
  | "concat (x # xs) = x @ concat xs"
haftmann@34941
    98
haftmann@39774
    99
definition (in monoid_add)
haftmann@34941
   100
  listsum :: "'a list \<Rightarrow> 'a" where
haftmann@39774
   101
  "listsum xs = foldr plus xs 0"
haftmann@34941
   102
haftmann@34941
   103
primrec
haftmann@34941
   104
  drop:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34941
   105
    drop_Nil: "drop n [] = []"
haftmann@34941
   106
  | drop_Cons: "drop n (x # xs) = (case n of 0 \<Rightarrow> x # xs | Suc m \<Rightarrow> drop m xs)"
haftmann@34941
   107
  -- {*Warning: simpset does not contain this definition, but separate
haftmann@34941
   108
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
haftmann@34941
   109
haftmann@34941
   110
primrec
haftmann@34941
   111
  take:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34941
   112
    take_Nil:"take n [] = []"
haftmann@34941
   113
  | take_Cons: "take n (x # xs) = (case n of 0 \<Rightarrow> [] | Suc m \<Rightarrow> x # take m xs)"
haftmann@34941
   114
  -- {*Warning: simpset does not contain this definition, but separate
haftmann@34941
   115
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
haftmann@34941
   116
haftmann@34941
   117
primrec
haftmann@34941
   118
  nth :: "'a list => nat => 'a" (infixl "!" 100) where
haftmann@34941
   119
  nth_Cons: "(x # xs) ! n = (case n of 0 \<Rightarrow> x | Suc k \<Rightarrow> xs ! k)"
haftmann@34941
   120
  -- {*Warning: simpset does not contain this definition, but separate
haftmann@34941
   121
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
haftmann@34941
   122
haftmann@34941
   123
primrec
haftmann@34941
   124
  list_update :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a list" where
haftmann@34941
   125
    "list_update [] i v = []"
haftmann@34941
   126
  | "list_update (x # xs) i v = (case i of 0 \<Rightarrow> v # xs | Suc j \<Rightarrow> x # list_update xs j v)"
clasohm@923
   127
wenzelm@41229
   128
nonterminal lupdbinds and lupdbind
nipkow@5077
   129
clasohm@923
   130
syntax
wenzelm@13366
   131
  "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")
wenzelm@13366
   132
  "" :: "lupdbind => lupdbinds"    ("_")
wenzelm@13366
   133
  "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")
wenzelm@13366
   134
  "_LUpdate" :: "['a, lupdbinds] => 'a"    ("_/[(_)]" [900,0] 900)
nipkow@5077
   135
clasohm@923
   136
translations
wenzelm@35115
   137
  "_LUpdate xs (_lupdbinds b bs)" == "_LUpdate (_LUpdate xs b) bs"
haftmann@34941
   138
  "xs[i:=x]" == "CONST list_update xs i x"
haftmann@34941
   139
haftmann@34941
   140
primrec
haftmann@34941
   141
  takeWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34941
   142
    "takeWhile P [] = []"
haftmann@34941
   143
  | "takeWhile P (x # xs) = (if P x then x # takeWhile P xs else [])"
haftmann@34941
   144
haftmann@34941
   145
primrec
haftmann@34941
   146
  dropWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34941
   147
    "dropWhile P [] = []"
haftmann@34941
   148
  | "dropWhile P (x # xs) = (if P x then dropWhile P xs else x # xs)"
haftmann@34941
   149
haftmann@34941
   150
primrec
haftmann@34941
   151
  zip :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
haftmann@34941
   152
    "zip xs [] = []"
haftmann@34941
   153
  | zip_Cons: "zip xs (y # ys) = (case xs of [] => [] | z # zs => (z, y) # zip zs ys)"
haftmann@34941
   154
  -- {*Warning: simpset does not contain this definition, but separate
haftmann@34941
   155
       theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
haftmann@34941
   156
haftmann@34941
   157
primrec 
haftmann@34941
   158
  upt :: "nat \<Rightarrow> nat \<Rightarrow> nat list" ("(1[_..</_'])") where
haftmann@34941
   159
    upt_0: "[i..<0] = []"
haftmann@34941
   160
  | upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])"
haftmann@34941
   161
haftmann@34978
   162
definition
haftmann@34978
   163
  insert :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34978
   164
  "insert x xs = (if x \<in> set xs then xs else x # xs)"
haftmann@34978
   165
wenzelm@36176
   166
hide_const (open) insert
wenzelm@36176
   167
hide_fact (open) insert_def
haftmann@34978
   168
haftmann@34941
   169
primrec
haftmann@34941
   170
  remove1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34941
   171
    "remove1 x [] = []"
haftmann@34941
   172
  | "remove1 x (y # xs) = (if x = y then xs else y # remove1 x xs)"
haftmann@34941
   173
haftmann@34941
   174
primrec
haftmann@34941
   175
  removeAll :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34941
   176
    "removeAll x [] = []"
haftmann@34941
   177
  | "removeAll x (y # xs) = (if x = y then removeAll x xs else y # removeAll x xs)"
haftmann@34941
   178
haftmann@40122
   179
primrec
haftmann@39915
   180
  distinct :: "'a list \<Rightarrow> bool" where
haftmann@40122
   181
    "distinct [] \<longleftrightarrow> True"
haftmann@40122
   182
  | "distinct (x # xs) \<longleftrightarrow> x \<notin> set xs \<and> distinct xs"
haftmann@39915
   183
haftmann@39915
   184
primrec
haftmann@39915
   185
  remdups :: "'a list \<Rightarrow> 'a list" where
haftmann@39915
   186
    "remdups [] = []"
haftmann@39915
   187
  | "remdups (x # xs) = (if x \<in> set xs then remdups xs else x # remdups xs)"
haftmann@39915
   188
haftmann@34941
   189
primrec
haftmann@34941
   190
  replicate :: "nat \<Rightarrow> 'a \<Rightarrow> 'a list" where
haftmann@34941
   191
    replicate_0: "replicate 0 x = []"
haftmann@34941
   192
  | replicate_Suc: "replicate (Suc n) x = x # replicate n x"
paulson@3342
   193
wenzelm@13142
   194
text {*
wenzelm@14589
   195
  Function @{text size} is overloaded for all datatypes. Users may
wenzelm@13366
   196
  refer to the list version as @{text length}. *}
wenzelm@13142
   197
wenzelm@19363
   198
abbreviation
haftmann@34941
   199
  length :: "'a list \<Rightarrow> nat" where
haftmann@34941
   200
  "length \<equiv> size"
paulson@15307
   201
haftmann@21061
   202
definition
wenzelm@21404
   203
  rotate1 :: "'a list \<Rightarrow> 'a list" where
wenzelm@21404
   204
  "rotate1 xs = (case xs of [] \<Rightarrow> [] | x#xs \<Rightarrow> xs @ [x])"
wenzelm@21404
   205
wenzelm@21404
   206
definition
wenzelm@21404
   207
  rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@30971
   208
  "rotate n = rotate1 ^^ n"
wenzelm@21404
   209
wenzelm@21404
   210
definition
wenzelm@21404
   211
  list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" where
haftmann@37767
   212
  "list_all2 P xs ys =
haftmann@21061
   213
    (length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y))"
wenzelm@21404
   214
wenzelm@21404
   215
definition
wenzelm@21404
   216
  sublist :: "'a list => nat set => 'a list" where
wenzelm@21404
   217
  "sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))"
nipkow@17086
   218
nipkow@40593
   219
fun splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@40593
   220
"splice [] ys = ys" |
nipkow@40593
   221
"splice xs [] = xs" |
nipkow@40593
   222
"splice (x#xs) (y#ys) = x # y # splice xs ys"
haftmann@21061
   223
nipkow@26771
   224
text{*
nipkow@26771
   225
\begin{figure}[htbp]
nipkow@26771
   226
\fbox{
nipkow@26771
   227
\begin{tabular}{l}
wenzelm@27381
   228
@{lemma "[a,b]@[c,d] = [a,b,c,d]" by simp}\\
wenzelm@27381
   229
@{lemma "length [a,b,c] = 3" by simp}\\
wenzelm@27381
   230
@{lemma "set [a,b,c] = {a,b,c}" by simp}\\
wenzelm@27381
   231
@{lemma "map f [a,b,c] = [f a, f b, f c]" by simp}\\
wenzelm@27381
   232
@{lemma "rev [a,b,c] = [c,b,a]" by simp}\\
wenzelm@27381
   233
@{lemma "hd [a,b,c,d] = a" by simp}\\
wenzelm@27381
   234
@{lemma "tl [a,b,c,d] = [b,c,d]" by simp}\\
wenzelm@27381
   235
@{lemma "last [a,b,c,d] = d" by simp}\\
wenzelm@27381
   236
@{lemma "butlast [a,b,c,d] = [a,b,c]" by simp}\\
wenzelm@27381
   237
@{lemma[source] "filter (\<lambda>n::nat. n<2) [0,2,1] = [0,1]" by simp}\\
wenzelm@27381
   238
@{lemma "concat [[a,b],[c,d,e],[],[f]] = [a,b,c,d,e,f]" by simp}\\
wenzelm@27381
   239
@{lemma "foldl f x [a,b,c] = f (f (f x a) b) c" by simp}\\
wenzelm@27381
   240
@{lemma "foldr f [a,b,c] x = f a (f b (f c x))" by simp}\\
wenzelm@27381
   241
@{lemma "zip [a,b,c] [x,y,z] = [(a,x),(b,y),(c,z)]" by simp}\\
wenzelm@27381
   242
@{lemma "zip [a,b] [x,y,z] = [(a,x),(b,y)]" by simp}\\
wenzelm@27381
   243
@{lemma "splice [a,b,c] [x,y,z] = [a,x,b,y,c,z]" by simp}\\
wenzelm@27381
   244
@{lemma "splice [a,b,c,d] [x,y] = [a,x,b,y,c,d]" by simp}\\
wenzelm@27381
   245
@{lemma "take 2 [a,b,c,d] = [a,b]" by simp}\\
wenzelm@27381
   246
@{lemma "take 6 [a,b,c,d] = [a,b,c,d]" by simp}\\
wenzelm@27381
   247
@{lemma "drop 2 [a,b,c,d] = [c,d]" by simp}\\
wenzelm@27381
   248
@{lemma "drop 6 [a,b,c,d] = []" by simp}\\
wenzelm@27381
   249
@{lemma "takeWhile (%n::nat. n<3) [1,2,3,0] = [1,2]" by simp}\\
wenzelm@27381
   250
@{lemma "dropWhile (%n::nat. n<3) [1,2,3,0] = [3,0]" by simp}\\
wenzelm@27381
   251
@{lemma "distinct [2,0,1::nat]" by simp}\\
wenzelm@27381
   252
@{lemma "remdups [2,0,2,1::nat,2] = [0,1,2]" by simp}\\
haftmann@34978
   253
@{lemma "List.insert 2 [0::nat,1,2] = [0,1,2]" by (simp add: List.insert_def)}\\
haftmann@35295
   254
@{lemma "List.insert 3 [0::nat,1,2] = [3,0,1,2]" by (simp add: List.insert_def)}\\
wenzelm@27381
   255
@{lemma "remove1 2 [2,0,2,1::nat,2] = [0,2,1,2]" by simp}\\
nipkow@27693
   256
@{lemma "removeAll 2 [2,0,2,1::nat,2] = [0,1]" by simp}\\
wenzelm@27381
   257
@{lemma "nth [a,b,c,d] 2 = c" by simp}\\
wenzelm@27381
   258
@{lemma "[a,b,c,d][2 := x] = [a,b,x,d]" by simp}\\
wenzelm@27381
   259
@{lemma "sublist [a,b,c,d,e] {0,2,3} = [a,c,d]" by (simp add:sublist_def)}\\
wenzelm@27381
   260
@{lemma "rotate1 [a,b,c,d] = [b,c,d,a]" by (simp add:rotate1_def)}\\
nipkow@40077
   261
@{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate1_def rotate_def eval_nat_numeral)}\\
nipkow@40077
   262
@{lemma "replicate 4 a = [a,a,a,a]" by (simp add:eval_nat_numeral)}\\
nipkow@40077
   263
@{lemma "[2..<5] = [2,3,4]" by (simp add:eval_nat_numeral)}\\
haftmann@39774
   264
@{lemma "listsum [1,2,3::nat] = 6" by (simp add: listsum_def)}
nipkow@26771
   265
\end{tabular}}
nipkow@26771
   266
\caption{Characteristic examples}
nipkow@26771
   267
\label{fig:Characteristic}
nipkow@26771
   268
\end{figure}
blanchet@29927
   269
Figure~\ref{fig:Characteristic} shows characteristic examples
nipkow@26771
   270
that should give an intuitive understanding of the above functions.
nipkow@26771
   271
*}
nipkow@26771
   272
nipkow@24616
   273
text{* The following simple sort functions are intended for proofs,
nipkow@24616
   274
not for efficient implementations. *}
nipkow@24616
   275
wenzelm@25221
   276
context linorder
wenzelm@25221
   277
begin
wenzelm@25221
   278
haftmann@39915
   279
inductive sorted :: "'a list \<Rightarrow> bool" where
haftmann@39915
   280
  Nil [iff]: "sorted []"
haftmann@39915
   281
| Cons: "\<forall>y\<in>set xs. x \<le> y \<Longrightarrow> sorted xs \<Longrightarrow> sorted (x # xs)"
haftmann@39915
   282
haftmann@39915
   283
lemma sorted_single [iff]:
haftmann@39915
   284
  "sorted [x]"
haftmann@39915
   285
  by (rule sorted.Cons) auto
haftmann@39915
   286
haftmann@39915
   287
lemma sorted_many:
haftmann@39915
   288
  "x \<le> y \<Longrightarrow> sorted (y # zs) \<Longrightarrow> sorted (x # y # zs)"
haftmann@39915
   289
  by (rule sorted.Cons) (cases "y # zs" rule: sorted.cases, auto)
haftmann@39915
   290
haftmann@39915
   291
lemma sorted_many_eq [simp, code]:
haftmann@39915
   292
  "sorted (x # y # zs) \<longleftrightarrow> x \<le> y \<and> sorted (y # zs)"
haftmann@39915
   293
  by (auto intro: sorted_many elim: sorted.cases)
haftmann@39915
   294
haftmann@39915
   295
lemma [code]:
haftmann@39915
   296
  "sorted [] \<longleftrightarrow> True"
haftmann@39915
   297
  "sorted [x] \<longleftrightarrow> True"
haftmann@39915
   298
  by simp_all
nipkow@24697
   299
hoelzl@33639
   300
primrec insort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
hoelzl@33639
   301
"insort_key f x [] = [x]" |
hoelzl@33639
   302
"insort_key f x (y#ys) = (if f x \<le> f y then (x#y#ys) else y#(insort_key f x ys))"
hoelzl@33639
   303
haftmann@35195
   304
definition sort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b list \<Rightarrow> 'b list" where
haftmann@35195
   305
"sort_key f xs = foldr (insort_key f) xs []"
hoelzl@33639
   306
haftmann@40210
   307
definition insort_insert_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
haftmann@40210
   308
  "insort_insert_key f x xs = (if f x \<in> f ` set xs then xs else insort_key f x xs)"
haftmann@40210
   309
hoelzl@33639
   310
abbreviation "sort \<equiv> sort_key (\<lambda>x. x)"
hoelzl@33639
   311
abbreviation "insort \<equiv> insort_key (\<lambda>x. x)"
haftmann@40210
   312
abbreviation "insort_insert \<equiv> insort_insert_key (\<lambda>x. x)"
haftmann@35608
   313
wenzelm@25221
   314
end
wenzelm@25221
   315
nipkow@24616
   316
wenzelm@23388
   317
subsubsection {* List comprehension *}
nipkow@23192
   318
nipkow@24349
   319
text{* Input syntax for Haskell-like list comprehension notation.
nipkow@24349
   320
Typical example: @{text"[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]"},
nipkow@24349
   321
the list of all pairs of distinct elements from @{text xs} and @{text ys}.
nipkow@24349
   322
The syntax is as in Haskell, except that @{text"|"} becomes a dot
nipkow@24349
   323
(like in Isabelle's set comprehension): @{text"[e. x \<leftarrow> xs, \<dots>]"} rather than
nipkow@24349
   324
\verb![e| x <- xs, ...]!.
nipkow@24349
   325
nipkow@24349
   326
The qualifiers after the dot are
nipkow@24349
   327
\begin{description}
nipkow@24349
   328
\item[generators] @{text"p \<leftarrow> xs"},
nipkow@24476
   329
 where @{text p} is a pattern and @{text xs} an expression of list type, or
nipkow@24476
   330
\item[guards] @{text"b"}, where @{text b} is a boolean expression.
nipkow@24476
   331
%\item[local bindings] @ {text"let x = e"}.
nipkow@24349
   332
\end{description}
nipkow@23240
   333
nipkow@24476
   334
Just like in Haskell, list comprehension is just a shorthand. To avoid
nipkow@24476
   335
misunderstandings, the translation into desugared form is not reversed
nipkow@24476
   336
upon output. Note that the translation of @{text"[e. x \<leftarrow> xs]"} is
nipkow@24476
   337
optmized to @{term"map (%x. e) xs"}.
nipkow@23240
   338
nipkow@24349
   339
It is easy to write short list comprehensions which stand for complex
nipkow@24349
   340
expressions. During proofs, they may become unreadable (and
nipkow@24349
   341
mangled). In such cases it can be advisable to introduce separate
nipkow@24349
   342
definitions for the list comprehensions in question.  *}
nipkow@24349
   343
wenzelm@41229
   344
nonterminal lc_qual and lc_quals
nipkow@23192
   345
nipkow@23192
   346
syntax
nipkow@23240
   347
"_listcompr" :: "'a \<Rightarrow> lc_qual \<Rightarrow> lc_quals \<Rightarrow> 'a list"  ("[_ . __")
nipkow@24349
   348
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ <- _")
nipkow@23240
   349
"_lc_test" :: "bool \<Rightarrow> lc_qual" ("_")
nipkow@24476
   350
(*"_lc_let" :: "letbinds => lc_qual"  ("let _")*)
nipkow@23240
   351
"_lc_end" :: "lc_quals" ("]")
nipkow@23240
   352
"_lc_quals" :: "lc_qual \<Rightarrow> lc_quals \<Rightarrow> lc_quals" (", __")
nipkow@24349
   353
"_lc_abs" :: "'a => 'b list => 'b list"
nipkow@23192
   354
nipkow@24476
   355
(* These are easier than ML code but cannot express the optimized
nipkow@24476
   356
   translation of [e. p<-xs]
nipkow@23192
   357
translations
nipkow@24349
   358
"[e. p<-xs]" => "concat(map (_lc_abs p [e]) xs)"
nipkow@23240
   359
"_listcompr e (_lc_gen p xs) (_lc_quals Q Qs)"
nipkow@24349
   360
 => "concat (map (_lc_abs p (_listcompr e Q Qs)) xs)"
nipkow@23240
   361
"[e. P]" => "if P then [e] else []"
nipkow@23240
   362
"_listcompr e (_lc_test P) (_lc_quals Q Qs)"
nipkow@23240
   363
 => "if P then (_listcompr e Q Qs) else []"
nipkow@24349
   364
"_listcompr e (_lc_let b) (_lc_quals Q Qs)"
nipkow@24349
   365
 => "_Let b (_listcompr e Q Qs)"
nipkow@24476
   366
*)
nipkow@23240
   367
nipkow@23279
   368
syntax (xsymbols)
nipkow@24349
   369
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _")
nipkow@23279
   370
syntax (HTML output)
nipkow@24349
   371
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _")
nipkow@24349
   372
nipkow@24349
   373
parse_translation (advanced) {*
nipkow@24349
   374
let
wenzelm@35256
   375
  val NilC = Syntax.const @{const_syntax Nil};
wenzelm@35256
   376
  val ConsC = Syntax.const @{const_syntax Cons};
wenzelm@35256
   377
  val mapC = Syntax.const @{const_syntax map};
wenzelm@35256
   378
  val concatC = Syntax.const @{const_syntax concat};
wenzelm@35256
   379
  val IfC = Syntax.const @{const_syntax If};
wenzelm@35115
   380
nipkow@24476
   381
  fun singl x = ConsC $ x $ NilC;
nipkow@24476
   382
wenzelm@35115
   383
  fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *)
nipkow@24349
   384
    let
wenzelm@29281
   385
      val x = Free (Name.variant (fold Term.add_free_names [p, e] []) "x", dummyT);
nipkow@24476
   386
      val e = if opti then singl e else e;
wenzelm@35115
   387
      val case1 = Syntax.const @{syntax_const "_case1"} $ p $ e;
wenzelm@35256
   388
      val case2 =
wenzelm@35256
   389
        Syntax.const @{syntax_const "_case1"} $
wenzelm@35256
   390
          Syntax.const @{const_syntax dummy_pattern} $ NilC;
wenzelm@35115
   391
      val cs = Syntax.const @{syntax_const "_case2"} $ case1 $ case2;
wenzelm@35115
   392
      val ft = Datatype_Case.case_tr false Datatype.info_of_constr ctxt [x, cs];
nipkow@24349
   393
    in lambda x ft end;
nipkow@24349
   394
wenzelm@35256
   395
  fun abs_tr ctxt (p as Free (s, T)) e opti =
wenzelm@35115
   396
        let
wenzelm@35115
   397
          val thy = ProofContext.theory_of ctxt;
wenzelm@35115
   398
          val s' = Sign.intern_const thy s;
wenzelm@35115
   399
        in
wenzelm@35115
   400
          if Sign.declared_const thy s'
wenzelm@35115
   401
          then (pat_tr ctxt p e opti, false)
wenzelm@35115
   402
          else (lambda p e, true)
nipkow@24349
   403
        end
nipkow@24476
   404
    | abs_tr ctxt p e opti = (pat_tr ctxt p e opti, false);
nipkow@24476
   405
wenzelm@35115
   406
  fun lc_tr ctxt [e, Const (@{syntax_const "_lc_test"}, _) $ b, qs] =
wenzelm@35115
   407
        let
wenzelm@35115
   408
          val res =
wenzelm@35115
   409
            (case qs of
wenzelm@35115
   410
              Const (@{syntax_const "_lc_end"}, _) => singl e
wenzelm@35115
   411
            | Const (@{syntax_const "_lc_quals"}, _) $ q $ qs => lc_tr ctxt [e, q, qs]);
nipkow@24476
   412
        in IfC $ b $ res $ NilC end
wenzelm@35115
   413
    | lc_tr ctxt
wenzelm@35115
   414
          [e, Const (@{syntax_const "_lc_gen"}, _) $ p $ es,
wenzelm@35115
   415
            Const(@{syntax_const "_lc_end"}, _)] =
nipkow@24476
   416
        (case abs_tr ctxt p e true of
wenzelm@35115
   417
          (f, true) => mapC $ f $ es
wenzelm@35115
   418
        | (f, false) => concatC $ (mapC $ f $ es))
wenzelm@35115
   419
    | lc_tr ctxt
wenzelm@35115
   420
          [e, Const (@{syntax_const "_lc_gen"}, _) $ p $ es,
wenzelm@35115
   421
            Const (@{syntax_const "_lc_quals"}, _) $ q $ qs] =
wenzelm@35115
   422
        let val e' = lc_tr ctxt [e, q, qs];
wenzelm@35115
   423
        in concatC $ (mapC $ (fst (abs_tr ctxt p e' false)) $ es) end;
wenzelm@35115
   424
wenzelm@35115
   425
in [(@{syntax_const "_listcompr"}, lc_tr)] end
nipkow@24349
   426
*}
nipkow@23279
   427
nipkow@23240
   428
term "[(x,y,z). b]"
nipkow@24476
   429
term "[(x,y,z). x\<leftarrow>xs]"
nipkow@24476
   430
term "[e x y. x\<leftarrow>xs, y\<leftarrow>ys]"
nipkow@24476
   431
term "[(x,y,z). x<a, x>b]"
nipkow@24476
   432
term "[(x,y,z). x\<leftarrow>xs, x>b]"
nipkow@24476
   433
term "[(x,y,z). x<a, x\<leftarrow>xs]"
nipkow@24349
   434
term "[(x,y). Cons True x \<leftarrow> xs]"
nipkow@24349
   435
term "[(x,y,z). Cons x [] \<leftarrow> xs]"
nipkow@23240
   436
term "[(x,y,z). x<a, x>b, x=d]"
nipkow@23240
   437
term "[(x,y,z). x<a, x>b, y\<leftarrow>ys]"
nipkow@23240
   438
term "[(x,y,z). x<a, x\<leftarrow>xs,y>b]"
nipkow@23240
   439
term "[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]"
nipkow@23240
   440
term "[(x,y,z). x\<leftarrow>xs, x>b, y<a]"
nipkow@23240
   441
term "[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]"
nipkow@23240
   442
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x]"
nipkow@23240
   443
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]"
wenzelm@35115
   444
(*
nipkow@24349
   445
term "[(x,y). x\<leftarrow>xs, let xx = x+x, y\<leftarrow>ys, y \<noteq> xx]"
nipkow@23192
   446
*)
nipkow@23192
   447
bulwahn@41463
   448
use "Tools/list_to_set_comprehension.ML"
bulwahn@41463
   449
bulwahn@41463
   450
simproc_setup list_to_set_comprehension ("set xs") = {* K List_to_Set_Comprehension.simproc *}
bulwahn@41463
   451
wenzelm@35115
   452
haftmann@21061
   453
subsubsection {* @{const Nil} and @{const Cons} *}
haftmann@21061
   454
haftmann@21061
   455
lemma not_Cons_self [simp]:
haftmann@21061
   456
  "xs \<noteq> x # xs"
nipkow@13145
   457
by (induct xs) auto
wenzelm@13114
   458
wenzelm@41697
   459
lemma not_Cons_self2 [simp]:
wenzelm@41697
   460
  "x # xs \<noteq> xs"
wenzelm@41697
   461
by (rule not_Cons_self [symmetric])
wenzelm@13114
   462
wenzelm@13142
   463
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
nipkow@13145
   464
by (induct xs) auto
wenzelm@13114
   465
wenzelm@13142
   466
lemma length_induct:
haftmann@21061
   467
  "(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs"
nipkow@17589
   468
by (rule measure_induct [of length]) iprover
wenzelm@13114
   469
haftmann@37289
   470
lemma list_nonempty_induct [consumes 1, case_names single cons]:
haftmann@37289
   471
  assumes "xs \<noteq> []"
haftmann@37289
   472
  assumes single: "\<And>x. P [x]"
haftmann@37289
   473
  assumes cons: "\<And>x xs. xs \<noteq> [] \<Longrightarrow> P xs \<Longrightarrow> P (x # xs)"
haftmann@37289
   474
  shows "P xs"
haftmann@37289
   475
using `xs \<noteq> []` proof (induct xs)
haftmann@37289
   476
  case Nil then show ?case by simp
haftmann@37289
   477
next
haftmann@37289
   478
  case (Cons x xs) show ?case proof (cases xs)
haftmann@37289
   479
    case Nil with single show ?thesis by simp
haftmann@37289
   480
  next
haftmann@37289
   481
    case Cons then have "xs \<noteq> []" by simp
haftmann@37289
   482
    moreover with Cons.hyps have "P xs" .
haftmann@37289
   483
    ultimately show ?thesis by (rule cons)
haftmann@37289
   484
  qed
haftmann@37289
   485
qed
haftmann@37289
   486
wenzelm@13114
   487
haftmann@21061
   488
subsubsection {* @{const length} *}
wenzelm@13114
   489
wenzelm@13142
   490
text {*
haftmann@21061
   491
  Needs to come before @{text "@"} because of theorem @{text
haftmann@21061
   492
  append_eq_append_conv}.
wenzelm@13142
   493
*}
wenzelm@13114
   494
wenzelm@13142
   495
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
nipkow@13145
   496
by (induct xs) auto
wenzelm@13114
   497
wenzelm@13142
   498
lemma length_map [simp]: "length (map f xs) = length xs"
nipkow@13145
   499
by (induct xs) auto
wenzelm@13114
   500
wenzelm@13142
   501
lemma length_rev [simp]: "length (rev xs) = length xs"
nipkow@13145
   502
by (induct xs) auto
wenzelm@13114
   503
wenzelm@13142
   504
lemma length_tl [simp]: "length (tl xs) = length xs - 1"
nipkow@13145
   505
by (cases xs) auto
wenzelm@13114
   506
wenzelm@13142
   507
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
nipkow@13145
   508
by (induct xs) auto
wenzelm@13114
   509
wenzelm@13142
   510
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
nipkow@13145
   511
by (induct xs) auto
wenzelm@13114
   512
nipkow@23479
   513
lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0"
nipkow@23479
   514
by auto
nipkow@23479
   515
wenzelm@13114
   516
lemma length_Suc_conv:
nipkow@13145
   517
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
nipkow@13145
   518
by (induct xs) auto
wenzelm@13142
   519
nipkow@14025
   520
lemma Suc_length_conv:
nipkow@14025
   521
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
paulson@14208
   522
apply (induct xs, simp, simp)
nipkow@14025
   523
apply blast
nipkow@14025
   524
done
nipkow@14025
   525
wenzelm@25221
   526
lemma impossible_Cons: "length xs <= length ys ==> xs = x # ys = False"
wenzelm@25221
   527
  by (induct xs) auto
wenzelm@25221
   528
haftmann@26442
   529
lemma list_induct2 [consumes 1, case_names Nil Cons]:
haftmann@26442
   530
  "length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow>
haftmann@26442
   531
   (\<And>x xs y ys. length xs = length ys \<Longrightarrow> P xs ys \<Longrightarrow> P (x#xs) (y#ys))
haftmann@26442
   532
   \<Longrightarrow> P xs ys"
haftmann@26442
   533
proof (induct xs arbitrary: ys)
haftmann@26442
   534
  case Nil then show ?case by simp
haftmann@26442
   535
next
haftmann@26442
   536
  case (Cons x xs ys) then show ?case by (cases ys) simp_all
haftmann@26442
   537
qed
haftmann@26442
   538
haftmann@26442
   539
lemma list_induct3 [consumes 2, case_names Nil Cons]:
haftmann@26442
   540
  "length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P [] [] [] \<Longrightarrow>
haftmann@26442
   541
   (\<And>x xs y ys z zs. length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P xs ys zs \<Longrightarrow> P (x#xs) (y#ys) (z#zs))
haftmann@26442
   542
   \<Longrightarrow> P xs ys zs"
haftmann@26442
   543
proof (induct xs arbitrary: ys zs)
haftmann@26442
   544
  case Nil then show ?case by simp
haftmann@26442
   545
next
haftmann@26442
   546
  case (Cons x xs ys zs) then show ?case by (cases ys, simp_all)
haftmann@26442
   547
    (cases zs, simp_all)
haftmann@26442
   548
qed
wenzelm@13114
   549
kaliszyk@36154
   550
lemma list_induct4 [consumes 3, case_names Nil Cons]:
kaliszyk@36154
   551
  "length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> length zs = length ws \<Longrightarrow>
kaliszyk@36154
   552
   P [] [] [] [] \<Longrightarrow> (\<And>x xs y ys z zs w ws. length xs = length ys \<Longrightarrow>
kaliszyk@36154
   553
   length ys = length zs \<Longrightarrow> length zs = length ws \<Longrightarrow> P xs ys zs ws \<Longrightarrow>
kaliszyk@36154
   554
   P (x#xs) (y#ys) (z#zs) (w#ws)) \<Longrightarrow> P xs ys zs ws"
kaliszyk@36154
   555
proof (induct xs arbitrary: ys zs ws)
kaliszyk@36154
   556
  case Nil then show ?case by simp
kaliszyk@36154
   557
next
kaliszyk@36154
   558
  case (Cons x xs ys zs ws) then show ?case by ((cases ys, simp_all), (cases zs,simp_all)) (cases ws, simp_all)
kaliszyk@36154
   559
qed
kaliszyk@36154
   560
krauss@22493
   561
lemma list_induct2': 
krauss@22493
   562
  "\<lbrakk> P [] [];
krauss@22493
   563
  \<And>x xs. P (x#xs) [];
krauss@22493
   564
  \<And>y ys. P [] (y#ys);
krauss@22493
   565
   \<And>x xs y ys. P xs ys  \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk>
krauss@22493
   566
 \<Longrightarrow> P xs ys"
krauss@22493
   567
by (induct xs arbitrary: ys) (case_tac x, auto)+
krauss@22493
   568
nipkow@22143
   569
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False"
nipkow@24349
   570
by (rule Eq_FalseI) auto
wenzelm@24037
   571
wenzelm@24037
   572
simproc_setup list_neq ("(xs::'a list) = ys") = {*
nipkow@22143
   573
(*
nipkow@22143
   574
Reduces xs=ys to False if xs and ys cannot be of the same length.
nipkow@22143
   575
This is the case if the atomic sublists of one are a submultiset
nipkow@22143
   576
of those of the other list and there are fewer Cons's in one than the other.
nipkow@22143
   577
*)
wenzelm@24037
   578
wenzelm@24037
   579
let
nipkow@22143
   580
huffman@29856
   581
fun len (Const(@{const_name Nil},_)) acc = acc
huffman@29856
   582
  | len (Const(@{const_name Cons},_) $ _ $ xs) (ts,n) = len xs (ts,n+1)
huffman@29856
   583
  | len (Const(@{const_name append},_) $ xs $ ys) acc = len xs (len ys acc)
huffman@29856
   584
  | len (Const(@{const_name rev},_) $ xs) acc = len xs acc
huffman@29856
   585
  | len (Const(@{const_name map},_) $ _ $ xs) acc = len xs acc
nipkow@22143
   586
  | len t (ts,n) = (t::ts,n);
nipkow@22143
   587
wenzelm@24037
   588
fun list_neq _ ss ct =
nipkow@22143
   589
  let
wenzelm@24037
   590
    val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct;
nipkow@22143
   591
    val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0);
nipkow@22143
   592
    fun prove_neq() =
nipkow@22143
   593
      let
nipkow@22143
   594
        val Type(_,listT::_) = eqT;
haftmann@22994
   595
        val size = HOLogic.size_const listT;
nipkow@22143
   596
        val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs);
nipkow@22143
   597
        val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len);
nipkow@22143
   598
        val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len
haftmann@22633
   599
          (K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1));
haftmann@22633
   600
      in SOME (thm RS @{thm neq_if_length_neq}) end
nipkow@22143
   601
  in
wenzelm@23214
   602
    if m < n andalso submultiset (op aconv) (ls,rs) orelse
wenzelm@23214
   603
       n < m andalso submultiset (op aconv) (rs,ls)
nipkow@22143
   604
    then prove_neq() else NONE
nipkow@22143
   605
  end;
wenzelm@24037
   606
in list_neq end;
nipkow@22143
   607
*}
nipkow@22143
   608
nipkow@22143
   609
nipkow@15392
   610
subsubsection {* @{text "@"} -- append *}
wenzelm@13114
   611
wenzelm@13142
   612
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
nipkow@13145
   613
by (induct xs) auto
wenzelm@13114
   614
wenzelm@13142
   615
lemma append_Nil2 [simp]: "xs @ [] = xs"
nipkow@13145
   616
by (induct xs) auto
nipkow@3507
   617
wenzelm@13142
   618
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
nipkow@13145
   619
by (induct xs) auto
wenzelm@13114
   620
wenzelm@13142
   621
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
nipkow@13145
   622
by (induct xs) auto
wenzelm@13114
   623
wenzelm@13142
   624
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
nipkow@13145
   625
by (induct xs) auto
wenzelm@13114
   626
wenzelm@13142
   627
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
nipkow@13145
   628
by (induct xs) auto
wenzelm@13114
   629
blanchet@35828
   630
lemma append_eq_append_conv [simp, no_atp]:
nipkow@24526
   631
 "length xs = length ys \<or> length us = length vs
berghofe@13883
   632
 ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
nipkow@24526
   633
apply (induct xs arbitrary: ys)
paulson@14208
   634
 apply (case_tac ys, simp, force)
paulson@14208
   635
apply (case_tac ys, force, simp)
nipkow@13145
   636
done
wenzelm@13142
   637
nipkow@24526
   638
lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) =
nipkow@24526
   639
  (EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)"
nipkow@24526
   640
apply (induct xs arbitrary: ys zs ts)
nipkow@14495
   641
 apply fastsimp
nipkow@14495
   642
apply(case_tac zs)
nipkow@14495
   643
 apply simp
nipkow@14495
   644
apply fastsimp
nipkow@14495
   645
done
nipkow@14495
   646
berghofe@34910
   647
lemma same_append_eq [iff, induct_simp]: "(xs @ ys = xs @ zs) = (ys = zs)"
nipkow@13145
   648
by simp
wenzelm@13142
   649
wenzelm@13142
   650
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
nipkow@13145
   651
by simp
wenzelm@13114
   652
berghofe@34910
   653
lemma append_same_eq [iff, induct_simp]: "(ys @ xs = zs @ xs) = (ys = zs)"
nipkow@13145
   654
by simp
wenzelm@13114
   655
wenzelm@13142
   656
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
nipkow@13145
   657
using append_same_eq [of _ _ "[]"] by auto
nipkow@3507
   658
wenzelm@13142
   659
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
nipkow@13145
   660
using append_same_eq [of "[]"] by auto
wenzelm@13114
   661
blanchet@35828
   662
lemma hd_Cons_tl [simp,no_atp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
nipkow@13145
   663
by (induct xs) auto
wenzelm@13114
   664
wenzelm@13142
   665
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
nipkow@13145
   666
by (induct xs) auto
wenzelm@13114
   667
wenzelm@13142
   668
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
nipkow@13145
   669
by (simp add: hd_append split: list.split)
wenzelm@13114
   670
wenzelm@13142
   671
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
nipkow@13145
   672
by (simp split: list.split)
wenzelm@13114
   673
wenzelm@13142
   674
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
nipkow@13145
   675
by (simp add: tl_append split: list.split)
wenzelm@13114
   676
wenzelm@13114
   677
nipkow@14300
   678
lemma Cons_eq_append_conv: "x#xs = ys@zs =
nipkow@14300
   679
 (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))"
nipkow@14300
   680
by(cases ys) auto
nipkow@14300
   681
nipkow@15281
   682
lemma append_eq_Cons_conv: "(ys@zs = x#xs) =
nipkow@15281
   683
 (ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))"
nipkow@15281
   684
by(cases ys) auto
nipkow@15281
   685
nipkow@14300
   686
wenzelm@13142
   687
text {* Trivial rules for solving @{text "@"}-equations automatically. *}
wenzelm@13114
   688
wenzelm@13114
   689
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
nipkow@13145
   690
by simp
wenzelm@13114
   691
wenzelm@13142
   692
lemma Cons_eq_appendI:
nipkow@13145
   693
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
nipkow@13145
   694
by (drule sym) simp
wenzelm@13114
   695
wenzelm@13142
   696
lemma append_eq_appendI:
nipkow@13145
   697
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
nipkow@13145
   698
by (drule sym) simp
wenzelm@13114
   699
wenzelm@13114
   700
wenzelm@13142
   701
text {*
nipkow@13145
   702
Simplification procedure for all list equalities.
nipkow@13145
   703
Currently only tries to rearrange @{text "@"} to see if
nipkow@13145
   704
- both lists end in a singleton list,
nipkow@13145
   705
- or both lists end in the same list.
wenzelm@13142
   706
*}
wenzelm@13142
   707
wenzelm@26480
   708
ML {*
nipkow@3507
   709
local
nipkow@3507
   710
huffman@29856
   711
fun last (cons as Const(@{const_name Cons},_) $ _ $ xs) =
huffman@29856
   712
  (case xs of Const(@{const_name Nil},_) => cons | _ => last xs)
huffman@29856
   713
  | last (Const(@{const_name append},_) $ _ $ ys) = last ys
wenzelm@13462
   714
  | last t = t;
wenzelm@13114
   715
huffman@29856
   716
fun list1 (Const(@{const_name Cons},_) $ _ $ Const(@{const_name Nil},_)) = true
wenzelm@13462
   717
  | list1 _ = false;
wenzelm@13114
   718
huffman@29856
   719
fun butlast ((cons as Const(@{const_name Cons},_) $ x) $ xs) =
huffman@29856
   720
  (case xs of Const(@{const_name Nil},_) => xs | _ => cons $ butlast xs)
huffman@29856
   721
  | butlast ((app as Const(@{const_name append},_) $ xs) $ ys) = app $ butlast ys
huffman@29856
   722
  | butlast xs = Const(@{const_name Nil},fastype_of xs);
wenzelm@13114
   723
haftmann@22633
   724
val rearr_ss = HOL_basic_ss addsimps [@{thm append_assoc},
haftmann@22633
   725
  @{thm append_Nil}, @{thm append_Cons}];
wenzelm@16973
   726
wenzelm@20044
   727
fun list_eq ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
wenzelm@13462
   728
  let
wenzelm@13462
   729
    val lastl = last lhs and lastr = last rhs;
wenzelm@13462
   730
    fun rearr conv =
wenzelm@13462
   731
      let
wenzelm@13462
   732
        val lhs1 = butlast lhs and rhs1 = butlast rhs;
wenzelm@13462
   733
        val Type(_,listT::_) = eqT
wenzelm@13462
   734
        val appT = [listT,listT] ---> listT
huffman@29856
   735
        val app = Const(@{const_name append},appT)
wenzelm@13462
   736
        val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
wenzelm@13480
   737
        val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2));
wenzelm@20044
   738
        val thm = Goal.prove (Simplifier.the_context ss) [] [] eq
wenzelm@17877
   739
          (K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1));
skalberg@15531
   740
      in SOME ((conv RS (thm RS trans)) RS eq_reflection) end;
wenzelm@13114
   741
wenzelm@13462
   742
  in
haftmann@22633
   743
    if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv}
haftmann@22633
   744
    else if lastl aconv lastr then rearr @{thm append_same_eq}
skalberg@15531
   745
    else NONE
wenzelm@13462
   746
  end;
wenzelm@13462
   747
wenzelm@13114
   748
in
wenzelm@13462
   749
wenzelm@13462
   750
val list_eq_simproc =
wenzelm@38715
   751
  Simplifier.simproc_global @{theory} "list_eq" ["(xs::'a list) = ys"] (K list_eq);
wenzelm@13462
   752
wenzelm@13114
   753
end;
wenzelm@13114
   754
wenzelm@13114
   755
Addsimprocs [list_eq_simproc];
wenzelm@13114
   756
*}
wenzelm@13114
   757
wenzelm@13114
   758
nipkow@15392
   759
subsubsection {* @{text map} *}
wenzelm@13114
   760
haftmann@40210
   761
lemma hd_map:
haftmann@40210
   762
  "xs \<noteq> [] \<Longrightarrow> hd (map f xs) = f (hd xs)"
haftmann@40210
   763
  by (cases xs) simp_all
haftmann@40210
   764
haftmann@40210
   765
lemma map_tl:
haftmann@40210
   766
  "map f (tl xs) = tl (map f xs)"
haftmann@40210
   767
  by (cases xs) simp_all
haftmann@40210
   768
wenzelm@13142
   769
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
nipkow@13145
   770
by (induct xs) simp_all
wenzelm@13114
   771
wenzelm@13142
   772
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
nipkow@13145
   773
by (rule ext, induct_tac xs) auto
wenzelm@13114
   774
wenzelm@13142
   775
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
nipkow@13145
   776
by (induct xs) auto
wenzelm@13114
   777
hoelzl@33639
   778
lemma map_map [simp]: "map f (map g xs) = map (f \<circ> g) xs"
hoelzl@33639
   779
by (induct xs) auto
hoelzl@33639
   780
nipkow@35208
   781
lemma map_comp_map[simp]: "((map f) o (map g)) = map(f o g)"
nipkow@35208
   782
apply(rule ext)
nipkow@35208
   783
apply(simp)
nipkow@35208
   784
done
nipkow@35208
   785
wenzelm@13142
   786
lemma rev_map: "rev (map f xs) = map f (rev xs)"
nipkow@13145
   787
by (induct xs) auto
wenzelm@13114
   788
nipkow@13737
   789
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)"
nipkow@13737
   790
by (induct xs) auto
nipkow@13737
   791
krauss@19770
   792
lemma map_cong [fundef_cong, recdef_cong]:
haftmann@40122
   793
  "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g ys"
haftmann@40122
   794
  by simp
wenzelm@13114
   795
wenzelm@13142
   796
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
nipkow@13145
   797
by (cases xs) auto
wenzelm@13114
   798
wenzelm@13142
   799
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
nipkow@13145
   800
by (cases xs) auto
wenzelm@13114
   801
paulson@18447
   802
lemma map_eq_Cons_conv:
nipkow@14025
   803
 "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)"
nipkow@13145
   804
by (cases xs) auto
wenzelm@13114
   805
paulson@18447
   806
lemma Cons_eq_map_conv:
nipkow@14025
   807
 "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)"
nipkow@14025
   808
by (cases ys) auto
nipkow@14025
   809
paulson@18447
   810
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1]
paulson@18447
   811
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1]
paulson@18447
   812
declare map_eq_Cons_D [dest!]  Cons_eq_map_D [dest!]
paulson@18447
   813
nipkow@14111
   814
lemma ex_map_conv:
nipkow@14111
   815
  "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)"
paulson@18447
   816
by(induct ys, auto simp add: Cons_eq_map_conv)
nipkow@14111
   817
nipkow@15110
   818
lemma map_eq_imp_length_eq:
paulson@35510
   819
  assumes "map f xs = map g ys"
haftmann@26734
   820
  shows "length xs = length ys"
haftmann@26734
   821
using assms proof (induct ys arbitrary: xs)
haftmann@26734
   822
  case Nil then show ?case by simp
haftmann@26734
   823
next
haftmann@26734
   824
  case (Cons y ys) then obtain z zs where xs: "xs = z # zs" by auto
paulson@35510
   825
  from Cons xs have "map f zs = map g ys" by simp
haftmann@26734
   826
  moreover with Cons have "length zs = length ys" by blast
haftmann@26734
   827
  with xs show ?case by simp
haftmann@26734
   828
qed
haftmann@26734
   829
  
nipkow@15110
   830
lemma map_inj_on:
nipkow@15110
   831
 "[| map f xs = map f ys; inj_on f (set xs Un set ys) |]
nipkow@15110
   832
  ==> xs = ys"
nipkow@15110
   833
apply(frule map_eq_imp_length_eq)
nipkow@15110
   834
apply(rotate_tac -1)
nipkow@15110
   835
apply(induct rule:list_induct2)
nipkow@15110
   836
 apply simp
nipkow@15110
   837
apply(simp)
nipkow@15110
   838
apply (blast intro:sym)
nipkow@15110
   839
done
nipkow@15110
   840
nipkow@15110
   841
lemma inj_on_map_eq_map:
nipkow@15110
   842
 "inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@15110
   843
by(blast dest:map_inj_on)
nipkow@15110
   844
wenzelm@13114
   845
lemma map_injective:
nipkow@24526
   846
 "map f xs = map f ys ==> inj f ==> xs = ys"
nipkow@24526
   847
by (induct ys arbitrary: xs) (auto dest!:injD)
wenzelm@13114
   848
nipkow@14339
   849
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@14339
   850
by(blast dest:map_injective)
nipkow@14339
   851
wenzelm@13114
   852
lemma inj_mapI: "inj f ==> inj (map f)"
nipkow@17589
   853
by (iprover dest: map_injective injD intro: inj_onI)
wenzelm@13114
   854
wenzelm@13114
   855
lemma inj_mapD: "inj (map f) ==> inj f"
paulson@14208
   856
apply (unfold inj_on_def, clarify)
nipkow@13145
   857
apply (erule_tac x = "[x]" in ballE)
paulson@14208
   858
 apply (erule_tac x = "[y]" in ballE, simp, blast)
nipkow@13145
   859
apply blast
nipkow@13145
   860
done
wenzelm@13114
   861
nipkow@14339
   862
lemma inj_map[iff]: "inj (map f) = inj f"
nipkow@13145
   863
by (blast dest: inj_mapD intro: inj_mapI)
wenzelm@13114
   864
nipkow@15303
   865
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A"
nipkow@15303
   866
apply(rule inj_onI)
nipkow@15303
   867
apply(erule map_inj_on)
nipkow@15303
   868
apply(blast intro:inj_onI dest:inj_onD)
nipkow@15303
   869
done
nipkow@15303
   870
kleing@14343
   871
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs"
kleing@14343
   872
by (induct xs, auto)
wenzelm@13114
   873
nipkow@14402
   874
lemma map_fun_upd [simp]: "y \<notin> set xs \<Longrightarrow> map (f(y:=v)) xs = map f xs"
nipkow@14402
   875
by (induct xs) auto
nipkow@14402
   876
nipkow@15110
   877
lemma map_fst_zip[simp]:
nipkow@15110
   878
  "length xs = length ys \<Longrightarrow> map fst (zip xs ys) = xs"
nipkow@15110
   879
by (induct rule:list_induct2, simp_all)
nipkow@15110
   880
nipkow@15110
   881
lemma map_snd_zip[simp]:
nipkow@15110
   882
  "length xs = length ys \<Longrightarrow> map snd (zip xs ys) = ys"
nipkow@15110
   883
by (induct rule:list_induct2, simp_all)
nipkow@15110
   884
haftmann@41505
   885
enriched_type map: map
haftmann@41372
   886
  by (simp_all add: fun_eq_iff id_def)
haftmann@40608
   887
nipkow@15110
   888
nipkow@15392
   889
subsubsection {* @{text rev} *}
wenzelm@13114
   890
wenzelm@13142
   891
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
nipkow@13145
   892
by (induct xs) auto
wenzelm@13114
   893
wenzelm@13142
   894
lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
nipkow@13145
   895
by (induct xs) auto
wenzelm@13114
   896
kleing@15870
   897
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)"
kleing@15870
   898
by auto
kleing@15870
   899
wenzelm@13142
   900
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
nipkow@13145
   901
by (induct xs) auto
wenzelm@13114
   902
wenzelm@13142
   903
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
nipkow@13145
   904
by (induct xs) auto
wenzelm@13114
   905
kleing@15870
   906
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])"
kleing@15870
   907
by (cases xs) auto
kleing@15870
   908
kleing@15870
   909
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])"
kleing@15870
   910
by (cases xs) auto
kleing@15870
   911
haftmann@21061
   912
lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)"
haftmann@21061
   913
apply (induct xs arbitrary: ys, force)
paulson@14208
   914
apply (case_tac ys, simp, force)
nipkow@13145
   915
done
wenzelm@13114
   916
nipkow@15439
   917
lemma inj_on_rev[iff]: "inj_on rev A"
nipkow@15439
   918
by(simp add:inj_on_def)
nipkow@15439
   919
wenzelm@13366
   920
lemma rev_induct [case_names Nil snoc]:
wenzelm@13366
   921
  "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
berghofe@15489
   922
apply(simplesubst rev_rev_ident[symmetric])
nipkow@13145
   923
apply(rule_tac list = "rev xs" in list.induct, simp_all)
nipkow@13145
   924
done
wenzelm@13114
   925
wenzelm@13366
   926
lemma rev_exhaust [case_names Nil snoc]:
wenzelm@13366
   927
  "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"
nipkow@13145
   928
by (induct xs rule: rev_induct) auto
wenzelm@13114
   929
wenzelm@13366
   930
lemmas rev_cases = rev_exhaust
wenzelm@13366
   931
nipkow@18423
   932
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])"
nipkow@18423
   933
by(rule rev_cases[of xs]) auto
nipkow@18423
   934
wenzelm@13114
   935
nipkow@15392
   936
subsubsection {* @{text set} *}
wenzelm@13114
   937
wenzelm@13142
   938
lemma finite_set [iff]: "finite (set xs)"
nipkow@13145
   939
by (induct xs) auto
wenzelm@13114
   940
wenzelm@13142
   941
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
nipkow@13145
   942
by (induct xs) auto
wenzelm@13114
   943
nipkow@17830
   944
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs"
nipkow@17830
   945
by(cases xs) auto
oheimb@14099
   946
wenzelm@13142
   947
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
nipkow@13145
   948
by auto
wenzelm@13114
   949
oheimb@14099
   950
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" 
oheimb@14099
   951
by auto
oheimb@14099
   952
wenzelm@13142
   953
lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
nipkow@13145
   954
by (induct xs) auto
wenzelm@13114
   955
nipkow@15245
   956
lemma set_empty2[iff]: "({} = set xs) = (xs = [])"
nipkow@15245
   957
by(induct xs) auto
nipkow@15245
   958
wenzelm@13142
   959
lemma set_rev [simp]: "set (rev xs) = set xs"
nipkow@13145
   960
by (induct xs) auto
wenzelm@13114
   961
wenzelm@13142
   962
lemma set_map [simp]: "set (map f xs) = f`(set xs)"
nipkow@13145
   963
by (induct xs) auto
wenzelm@13114
   964
wenzelm@13142
   965
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
nipkow@13145
   966
by (induct xs) auto
wenzelm@13114
   967
nipkow@32417
   968
lemma set_upt [simp]: "set[i..<j] = {i..<j}"
bulwahn@41463
   969
by (induct j) auto
wenzelm@13114
   970
wenzelm@13142
   971
wenzelm@25221
   972
lemma split_list: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs"
nipkow@18049
   973
proof (induct xs)
nipkow@26073
   974
  case Nil thus ?case by simp
nipkow@26073
   975
next
nipkow@26073
   976
  case Cons thus ?case by (auto intro: Cons_eq_appendI)
nipkow@26073
   977
qed
nipkow@26073
   978
haftmann@26734
   979
lemma in_set_conv_decomp: "x \<in> set xs \<longleftrightarrow> (\<exists>ys zs. xs = ys @ x # zs)"
haftmann@26734
   980
  by (auto elim: split_list)
nipkow@26073
   981
nipkow@26073
   982
lemma split_list_first: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys"
nipkow@26073
   983
proof (induct xs)
nipkow@26073
   984
  case Nil thus ?case by simp
nipkow@18049
   985
next
nipkow@18049
   986
  case (Cons a xs)
nipkow@18049
   987
  show ?case
nipkow@18049
   988
  proof cases
wenzelm@25221
   989
    assume "x = a" thus ?case using Cons by fastsimp
nipkow@18049
   990
  next
nipkow@26073
   991
    assume "x \<noteq> a" thus ?case using Cons by(fastsimp intro!: Cons_eq_appendI)
nipkow@26073
   992
  qed
nipkow@26073
   993
qed
nipkow@26073
   994
nipkow@26073
   995
lemma in_set_conv_decomp_first:
nipkow@26073
   996
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)"
haftmann@26734
   997
  by (auto dest!: split_list_first)
nipkow@26073
   998
haftmann@40122
   999
lemma split_list_last: "x \<in> set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs"
haftmann@40122
  1000
proof (induct xs rule: rev_induct)
nipkow@26073
  1001
  case Nil thus ?case by simp
nipkow@26073
  1002
next
nipkow@26073
  1003
  case (snoc a xs)
nipkow@26073
  1004
  show ?case
nipkow@26073
  1005
  proof cases
haftmann@40122
  1006
    assume "x = a" thus ?case using snoc by (metis List.set.simps(1) emptyE)
nipkow@26073
  1007
  next
nipkow@26073
  1008
    assume "x \<noteq> a" thus ?case using snoc by fastsimp
nipkow@18049
  1009
  qed
nipkow@18049
  1010
qed
nipkow@18049
  1011
nipkow@26073
  1012
lemma in_set_conv_decomp_last:
nipkow@26073
  1013
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)"
haftmann@26734
  1014
  by (auto dest!: split_list_last)
nipkow@26073
  1015
nipkow@26073
  1016
lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs & P x"
nipkow@26073
  1017
proof (induct xs)
nipkow@26073
  1018
  case Nil thus ?case by simp
nipkow@26073
  1019
next
nipkow@26073
  1020
  case Cons thus ?case
nipkow@26073
  1021
    by(simp add:Bex_def)(metis append_Cons append.simps(1))
nipkow@26073
  1022
qed
nipkow@26073
  1023
nipkow@26073
  1024
lemma split_list_propE:
haftmann@26734
  1025
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
  1026
  obtains ys x zs where "xs = ys @ x # zs" and "P x"
haftmann@26734
  1027
using split_list_prop [OF assms] by blast
nipkow@26073
  1028
nipkow@26073
  1029
lemma split_list_first_prop:
nipkow@26073
  1030
  "\<exists>x \<in> set xs. P x \<Longrightarrow>
nipkow@26073
  1031
   \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)"
haftmann@26734
  1032
proof (induct xs)
nipkow@26073
  1033
  case Nil thus ?case by simp
nipkow@26073
  1034
next
nipkow@26073
  1035
  case (Cons x xs)
nipkow@26073
  1036
  show ?case
nipkow@26073
  1037
  proof cases
nipkow@26073
  1038
    assume "P x"
haftmann@40122
  1039
    thus ?thesis by simp (metis Un_upper1 contra_subsetD in_set_conv_decomp_first self_append_conv2 set_append)
nipkow@26073
  1040
  next
nipkow@26073
  1041
    assume "\<not> P x"
nipkow@26073
  1042
    hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp
nipkow@26073
  1043
    thus ?thesis using `\<not> P x` Cons(1) by (metis append_Cons set_ConsD)
nipkow@26073
  1044
  qed
nipkow@26073
  1045
qed
nipkow@26073
  1046
nipkow@26073
  1047
lemma split_list_first_propE:
haftmann@26734
  1048
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
  1049
  obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y"
haftmann@26734
  1050
using split_list_first_prop [OF assms] by blast
nipkow@26073
  1051
nipkow@26073
  1052
lemma split_list_first_prop_iff:
nipkow@26073
  1053
  "(\<exists>x \<in> set xs. P x) \<longleftrightarrow>
nipkow@26073
  1054
   (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))"
haftmann@26734
  1055
by (rule, erule split_list_first_prop) auto
nipkow@26073
  1056
nipkow@26073
  1057
lemma split_list_last_prop:
nipkow@26073
  1058
  "\<exists>x \<in> set xs. P x \<Longrightarrow>
nipkow@26073
  1059
   \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)"
nipkow@26073
  1060
proof(induct xs rule:rev_induct)
nipkow@26073
  1061
  case Nil thus ?case by simp
nipkow@26073
  1062
next
nipkow@26073
  1063
  case (snoc x xs)
nipkow@26073
  1064
  show ?case
nipkow@26073
  1065
  proof cases
nipkow@26073
  1066
    assume "P x" thus ?thesis by (metis emptyE set_empty)
nipkow@26073
  1067
  next
nipkow@26073
  1068
    assume "\<not> P x"
nipkow@26073
  1069
    hence "\<exists>x\<in>set xs. P x" using snoc(2) by simp
nipkow@26073
  1070
    thus ?thesis using `\<not> P x` snoc(1) by fastsimp
nipkow@26073
  1071
  qed
nipkow@26073
  1072
qed
nipkow@26073
  1073
nipkow@26073
  1074
lemma split_list_last_propE:
haftmann@26734
  1075
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
  1076
  obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z"
haftmann@26734
  1077
using split_list_last_prop [OF assms] by blast
nipkow@26073
  1078
nipkow@26073
  1079
lemma split_list_last_prop_iff:
nipkow@26073
  1080
  "(\<exists>x \<in> set xs. P x) \<longleftrightarrow>
nipkow@26073
  1081
   (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))"
haftmann@26734
  1082
by (metis split_list_last_prop [where P=P] in_set_conv_decomp)
nipkow@26073
  1083
nipkow@26073
  1084
lemma finite_list: "finite A ==> EX xs. set xs = A"
haftmann@26734
  1085
  by (erule finite_induct)
haftmann@26734
  1086
    (auto simp add: set.simps(2) [symmetric] simp del: set.simps(2))
paulson@13508
  1087
kleing@14388
  1088
lemma card_length: "card (set xs) \<le> length xs"
kleing@14388
  1089
by (induct xs) (auto simp add: card_insert_if)
wenzelm@13114
  1090
haftmann@26442
  1091
lemma set_minus_filter_out:
haftmann@26442
  1092
  "set xs - {y} = set (filter (\<lambda>x. \<not> (x = y)) xs)"
haftmann@26442
  1093
  by (induct xs) auto
paulson@15168
  1094
wenzelm@35115
  1095
nipkow@15392
  1096
subsubsection {* @{text filter} *}
wenzelm@13114
  1097
wenzelm@13142
  1098
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
nipkow@13145
  1099
by (induct xs) auto
wenzelm@13114
  1100
nipkow@15305
  1101
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)"
nipkow@15305
  1102
by (induct xs) simp_all
nipkow@15305
  1103
wenzelm@13142
  1104
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
nipkow@13145
  1105
by (induct xs) auto
wenzelm@13114
  1106
nipkow@16998
  1107
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs"
nipkow@16998
  1108
by (induct xs) (auto simp add: le_SucI)
nipkow@16998
  1109
nipkow@18423
  1110
lemma sum_length_filter_compl:
nipkow@18423
  1111
  "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs"
nipkow@18423
  1112
by(induct xs) simp_all
nipkow@18423
  1113
wenzelm@13142
  1114
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
nipkow@13145
  1115
by (induct xs) auto
wenzelm@13114
  1116
wenzelm@13142
  1117
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
nipkow@13145
  1118
by (induct xs) auto
wenzelm@13114
  1119
nipkow@16998
  1120
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" 
nipkow@24349
  1121
by (induct xs) simp_all
nipkow@16998
  1122
nipkow@16998
  1123
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)"
nipkow@16998
  1124
apply (induct xs)
nipkow@16998
  1125
 apply auto
nipkow@16998
  1126
apply(cut_tac P=P and xs=xs in length_filter_le)
nipkow@16998
  1127
apply simp
nipkow@16998
  1128
done
wenzelm@13114
  1129
nipkow@16965
  1130
lemma filter_map:
nipkow@16965
  1131
  "filter P (map f xs) = map f (filter (P o f) xs)"
nipkow@16965
  1132
by (induct xs) simp_all
nipkow@16965
  1133
nipkow@16965
  1134
lemma length_filter_map[simp]:
nipkow@16965
  1135
  "length (filter P (map f xs)) = length(filter (P o f) xs)"
nipkow@16965
  1136
by (simp add:filter_map)
nipkow@16965
  1137
wenzelm@13142
  1138
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
nipkow@13145
  1139
by auto
wenzelm@13114
  1140
nipkow@15246
  1141
lemma length_filter_less:
nipkow@15246
  1142
  "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs"
nipkow@15246
  1143
proof (induct xs)
nipkow@15246
  1144
  case Nil thus ?case by simp
nipkow@15246
  1145
next
nipkow@15246
  1146
  case (Cons x xs) thus ?case
nipkow@15246
  1147
    apply (auto split:split_if_asm)
nipkow@15246
  1148
    using length_filter_le[of P xs] apply arith
nipkow@15246
  1149
  done
nipkow@15246
  1150
qed
wenzelm@13114
  1151
nipkow@15281
  1152
lemma length_filter_conv_card:
nipkow@15281
  1153
 "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
nipkow@15281
  1154
proof (induct xs)
nipkow@15281
  1155
  case Nil thus ?case by simp
nipkow@15281
  1156
next
nipkow@15281
  1157
  case (Cons x xs)
nipkow@15281
  1158
  let ?S = "{i. i < length xs & p(xs!i)}"
nipkow@15281
  1159
  have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite)
nipkow@15281
  1160
  show ?case (is "?l = card ?S'")
nipkow@15281
  1161
  proof (cases)
nipkow@15281
  1162
    assume "p x"
nipkow@15281
  1163
    hence eq: "?S' = insert 0 (Suc ` ?S)"
nipkow@25162
  1164
      by(auto simp: image_def split:nat.split dest:gr0_implies_Suc)
nipkow@15281
  1165
    have "length (filter p (x # xs)) = Suc(card ?S)"
wenzelm@23388
  1166
      using Cons `p x` by simp
nipkow@15281
  1167
    also have "\<dots> = Suc(card(Suc ` ?S))" using fin
nipkow@15281
  1168
      by (simp add: card_image inj_Suc)
nipkow@15281
  1169
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
  1170
      by (simp add:card_insert_if) (simp add:image_def)
nipkow@15281
  1171
    finally show ?thesis .
nipkow@15281
  1172
  next
nipkow@15281
  1173
    assume "\<not> p x"
nipkow@15281
  1174
    hence eq: "?S' = Suc ` ?S"
nipkow@25162
  1175
      by(auto simp add: image_def split:nat.split elim:lessE)
nipkow@15281
  1176
    have "length (filter p (x # xs)) = card ?S"
wenzelm@23388
  1177
      using Cons `\<not> p x` by simp
nipkow@15281
  1178
    also have "\<dots> = card(Suc ` ?S)" using fin
nipkow@15281
  1179
      by (simp add: card_image inj_Suc)
nipkow@15281
  1180
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
  1181
      by (simp add:card_insert_if)
nipkow@15281
  1182
    finally show ?thesis .
nipkow@15281
  1183
  qed
nipkow@15281
  1184
qed
nipkow@15281
  1185
nipkow@17629
  1186
lemma Cons_eq_filterD:
nipkow@17629
  1187
 "x#xs = filter P ys \<Longrightarrow>
nipkow@17629
  1188
  \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs"
wenzelm@19585
  1189
  (is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs")
nipkow@17629
  1190
proof(induct ys)
nipkow@17629
  1191
  case Nil thus ?case by simp
nipkow@17629
  1192
next
nipkow@17629
  1193
  case (Cons y ys)
nipkow@17629
  1194
  show ?case (is "\<exists>x. ?Q x")
nipkow@17629
  1195
  proof cases
nipkow@17629
  1196
    assume Py: "P y"
nipkow@17629
  1197
    show ?thesis
nipkow@17629
  1198
    proof cases
wenzelm@25221
  1199
      assume "x = y"
wenzelm@25221
  1200
      with Py Cons.prems have "?Q []" by simp
wenzelm@25221
  1201
      then show ?thesis ..
nipkow@17629
  1202
    next
wenzelm@25221
  1203
      assume "x \<noteq> y"
wenzelm@25221
  1204
      with Py Cons.prems show ?thesis by simp
nipkow@17629
  1205
    qed
nipkow@17629
  1206
  next
wenzelm@25221
  1207
    assume "\<not> P y"
wenzelm@25221
  1208
    with Cons obtain us vs where "?P (y#ys) (y#us) vs" by fastsimp
wenzelm@25221
  1209
    then have "?Q (y#us)" by simp
wenzelm@25221
  1210
    then show ?thesis ..
nipkow@17629
  1211
  qed
nipkow@17629
  1212
qed
nipkow@17629
  1213
nipkow@17629
  1214
lemma filter_eq_ConsD:
nipkow@17629
  1215
 "filter P ys = x#xs \<Longrightarrow>
nipkow@17629
  1216
  \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs"
nipkow@17629
  1217
by(rule Cons_eq_filterD) simp
nipkow@17629
  1218
nipkow@17629
  1219
lemma filter_eq_Cons_iff:
nipkow@17629
  1220
 "(filter P ys = x#xs) =
nipkow@17629
  1221
  (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)"
nipkow@17629
  1222
by(auto dest:filter_eq_ConsD)
nipkow@17629
  1223
nipkow@17629
  1224
lemma Cons_eq_filter_iff:
nipkow@17629
  1225
 "(x#xs = filter P ys) =
nipkow@17629
  1226
  (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)"
nipkow@17629
  1227
by(auto dest:Cons_eq_filterD)
nipkow@17629
  1228
krauss@19770
  1229
lemma filter_cong[fundef_cong, recdef_cong]:
nipkow@17501
  1230
 "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys"
nipkow@17501
  1231
apply simp
nipkow@17501
  1232
apply(erule thin_rl)
nipkow@17501
  1233
by (induct ys) simp_all
nipkow@17501
  1234
nipkow@15281
  1235
haftmann@26442
  1236
subsubsection {* List partitioning *}
haftmann@26442
  1237
haftmann@26442
  1238
primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where
haftmann@26442
  1239
  "partition P [] = ([], [])"
haftmann@26442
  1240
  | "partition P (x # xs) = 
haftmann@26442
  1241
      (let (yes, no) = partition P xs
haftmann@26442
  1242
      in if P x then (x # yes, no) else (yes, x # no))"
haftmann@26442
  1243
haftmann@26442
  1244
lemma partition_filter1:
haftmann@26442
  1245
    "fst (partition P xs) = filter P xs"
haftmann@26442
  1246
by (induct xs) (auto simp add: Let_def split_def)
haftmann@26442
  1247
haftmann@26442
  1248
lemma partition_filter2:
haftmann@26442
  1249
    "snd (partition P xs) = filter (Not o P) xs"
haftmann@26442
  1250
by (induct xs) (auto simp add: Let_def split_def)
haftmann@26442
  1251
haftmann@26442
  1252
lemma partition_P:
haftmann@26442
  1253
  assumes "partition P xs = (yes, no)"
haftmann@26442
  1254
  shows "(\<forall>p \<in> set yes.  P p) \<and> (\<forall>p  \<in> set no. \<not> P p)"
haftmann@26442
  1255
proof -
haftmann@26442
  1256
  from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)"
haftmann@26442
  1257
    by simp_all
haftmann@26442
  1258
  then show ?thesis by (simp_all add: partition_filter1 partition_filter2)
haftmann@26442
  1259
qed
haftmann@26442
  1260
haftmann@26442
  1261
lemma partition_set:
haftmann@26442
  1262
  assumes "partition P xs = (yes, no)"
haftmann@26442
  1263
  shows "set yes \<union> set no = set xs"
haftmann@26442
  1264
proof -
haftmann@26442
  1265
  from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)"
haftmann@26442
  1266
    by simp_all
haftmann@26442
  1267
  then show ?thesis by (auto simp add: partition_filter1 partition_filter2) 
haftmann@26442
  1268
qed
haftmann@26442
  1269
hoelzl@33639
  1270
lemma partition_filter_conv[simp]:
hoelzl@33639
  1271
  "partition f xs = (filter f xs,filter (Not o f) xs)"
hoelzl@33639
  1272
unfolding partition_filter2[symmetric]
hoelzl@33639
  1273
unfolding partition_filter1[symmetric] by simp
hoelzl@33639
  1274
hoelzl@33639
  1275
declare partition.simps[simp del]
haftmann@26442
  1276
wenzelm@35115
  1277
nipkow@15392
  1278
subsubsection {* @{text concat} *}
wenzelm@13114
  1279
wenzelm@13142
  1280
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
nipkow@13145
  1281
by (induct xs) auto
wenzelm@13114
  1282
paulson@18447
  1283
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1284
by (induct xss) auto
wenzelm@13114
  1285
paulson@18447
  1286
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1287
by (induct xss) auto
wenzelm@13114
  1288
nipkow@24308
  1289
lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)"
nipkow@13145
  1290
by (induct xs) auto
wenzelm@13114
  1291
nipkow@24476
  1292
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs"
nipkow@24349
  1293
by (induct xs) auto
nipkow@24349
  1294
wenzelm@13142
  1295
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
nipkow@13145
  1296
by (induct xs) auto
wenzelm@13114
  1297
wenzelm@13142
  1298
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
nipkow@13145
  1299
by (induct xs) auto
wenzelm@13114
  1300
wenzelm@13142
  1301
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
nipkow@13145
  1302
by (induct xs) auto
wenzelm@13114
  1303
bulwahn@40365
  1304
lemma concat_eq_concat_iff: "\<forall>(x, y) \<in> set (zip xs ys). length x = length y ==> length xs = length ys ==> (concat xs = concat ys) = (xs = ys)"
bulwahn@40365
  1305
proof (induct xs arbitrary: ys)
bulwahn@40365
  1306
  case (Cons x xs ys)
bulwahn@40365
  1307
  thus ?case by (cases ys) auto
bulwahn@40365
  1308
qed (auto)
bulwahn@40365
  1309
bulwahn@40365
  1310
lemma concat_injective: "concat xs = concat ys ==> length xs = length ys ==> \<forall>(x, y) \<in> set (zip xs ys). length x = length y ==> xs = ys"
bulwahn@40365
  1311
by (simp add: concat_eq_concat_iff)
bulwahn@40365
  1312
wenzelm@13114
  1313
nipkow@15392
  1314
subsubsection {* @{text nth} *}
wenzelm@13114
  1315
haftmann@29827
  1316
lemma nth_Cons_0 [simp, code]: "(x # xs)!0 = x"
nipkow@13145
  1317
by auto
wenzelm@13114
  1318
haftmann@29827
  1319
lemma nth_Cons_Suc [simp, code]: "(x # xs)!(Suc n) = xs!n"
nipkow@13145
  1320
by auto
wenzelm@13114
  1321
wenzelm@13142
  1322
declare nth.simps [simp del]
wenzelm@13114
  1323
nipkow@41842
  1324
lemma nth_Cons_pos[simp]: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)"
nipkow@41842
  1325
by(auto simp: Nat.gr0_conv_Suc)
nipkow@41842
  1326
wenzelm@13114
  1327
lemma nth_append:
nipkow@24526
  1328
  "(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
nipkow@24526
  1329
apply (induct xs arbitrary: n, simp)
paulson@14208
  1330
apply (case_tac n, auto)
nipkow@13145
  1331
done
wenzelm@13114
  1332
nipkow@14402
  1333
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x"
wenzelm@25221
  1334
by (induct xs) auto
nipkow@14402
  1335
nipkow@14402
  1336
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n"
wenzelm@25221
  1337
by (induct xs) auto
nipkow@14402
  1338
nipkow@24526
  1339
lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)"
nipkow@24526
  1340
apply (induct xs arbitrary: n, simp)
paulson@14208
  1341
apply (case_tac n, auto)
nipkow@13145
  1342
done
wenzelm@13114
  1343
nipkow@18423
  1344
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"
nipkow@18423
  1345
by(cases xs) simp_all
nipkow@18423
  1346
nipkow@18049
  1347
nipkow@18049
  1348
lemma list_eq_iff_nth_eq:
nipkow@24526
  1349
 "(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))"
nipkow@24526
  1350
apply(induct xs arbitrary: ys)
paulson@24632
  1351
 apply force
nipkow@18049
  1352
apply(case_tac ys)
nipkow@18049
  1353
 apply simp
nipkow@18049
  1354
apply(simp add:nth_Cons split:nat.split)apply blast
nipkow@18049
  1355
done
nipkow@18049
  1356
wenzelm@13142
  1357
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
paulson@15251
  1358
apply (induct xs, simp, simp)
nipkow@13145
  1359
apply safe
paulson@24632
  1360
apply (metis nat_case_0 nth.simps zero_less_Suc)
paulson@24632
  1361
apply (metis less_Suc_eq_0_disj nth_Cons_Suc)
paulson@14208
  1362
apply (case_tac i, simp)
paulson@24632
  1363
apply (metis diff_Suc_Suc nat_case_Suc nth.simps zero_less_diff)
nipkow@13145
  1364
done
wenzelm@13114
  1365
nipkow@17501
  1366
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)"
nipkow@17501
  1367
by(auto simp:set_conv_nth)
nipkow@17501
  1368
nipkow@13145
  1369
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"
nipkow@13145
  1370
by (auto simp add: set_conv_nth)
wenzelm@13114
  1371
wenzelm@13142
  1372
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
nipkow@13145
  1373
by (auto simp add: set_conv_nth)
wenzelm@13114
  1374
wenzelm@13114
  1375
lemma all_nth_imp_all_set:
nipkow@13145
  1376
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x"
nipkow@13145
  1377
by (auto simp add: set_conv_nth)
wenzelm@13114
  1378
wenzelm@13114
  1379
lemma all_set_conv_all_nth:
nipkow@13145
  1380
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
nipkow@13145
  1381
by (auto simp add: set_conv_nth)
wenzelm@13114
  1382
kleing@25296
  1383
lemma rev_nth:
kleing@25296
  1384
  "n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)"
kleing@25296
  1385
proof (induct xs arbitrary: n)
kleing@25296
  1386
  case Nil thus ?case by simp
kleing@25296
  1387
next
kleing@25296
  1388
  case (Cons x xs)
kleing@25296
  1389
  hence n: "n < Suc (length xs)" by simp
kleing@25296
  1390
  moreover
kleing@25296
  1391
  { assume "n < length xs"
kleing@25296
  1392
    with n obtain n' where "length xs - n = Suc n'"
kleing@25296
  1393
      by (cases "length xs - n", auto)
kleing@25296
  1394
    moreover
kleing@25296
  1395
    then have "length xs - Suc n = n'" by simp
kleing@25296
  1396
    ultimately
kleing@25296
  1397
    have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp
kleing@25296
  1398
  }
kleing@25296
  1399
  ultimately
kleing@25296
  1400
  show ?case by (clarsimp simp add: Cons nth_append)
kleing@25296
  1401
qed
wenzelm@13114
  1402
nipkow@31159
  1403
lemma Skolem_list_nth:
nipkow@31159
  1404
  "(ALL i<k. EX x. P i x) = (EX xs. size xs = k & (ALL i<k. P i (xs!i)))"
nipkow@31159
  1405
  (is "_ = (EX xs. ?P k xs)")
nipkow@31159
  1406
proof(induct k)
nipkow@31159
  1407
  case 0 show ?case by simp
nipkow@31159
  1408
next
nipkow@31159
  1409
  case (Suc k)
nipkow@31159
  1410
  show ?case (is "?L = ?R" is "_ = (EX xs. ?P' xs)")
nipkow@31159
  1411
  proof
nipkow@31159
  1412
    assume "?R" thus "?L" using Suc by auto
nipkow@31159
  1413
  next
nipkow@31159
  1414
    assume "?L"
nipkow@31159
  1415
    with Suc obtain x xs where "?P k xs & P k x" by (metis less_Suc_eq)
nipkow@31159
  1416
    hence "?P'(xs@[x])" by(simp add:nth_append less_Suc_eq)
nipkow@31159
  1417
    thus "?R" ..
nipkow@31159
  1418
  qed
nipkow@31159
  1419
qed
nipkow@31159
  1420
nipkow@31159
  1421
nipkow@15392
  1422
subsubsection {* @{text list_update} *}
wenzelm@13114
  1423
nipkow@24526
  1424
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs"
nipkow@24526
  1425
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1426
wenzelm@13114
  1427
lemma nth_list_update:
nipkow@24526
  1428
"i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"
nipkow@24526
  1429
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1430
wenzelm@13142
  1431
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
nipkow@13145
  1432
by (simp add: nth_list_update)
wenzelm@13114
  1433
nipkow@24526
  1434
lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j"
nipkow@24526
  1435
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1436
nipkow@24526
  1437
lemma list_update_id[simp]: "xs[i := xs!i] = xs"
nipkow@24526
  1438
by (induct xs arbitrary: i) (simp_all split:nat.splits)
nipkow@24526
  1439
nipkow@24526
  1440
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs"
nipkow@24526
  1441
apply (induct xs arbitrary: i)
nipkow@17501
  1442
 apply simp
nipkow@17501
  1443
apply (case_tac i)
nipkow@17501
  1444
apply simp_all
nipkow@17501
  1445
done
nipkow@17501
  1446
nipkow@31077
  1447
lemma list_update_nonempty[simp]: "xs[k:=x] = [] \<longleftrightarrow> xs=[]"
nipkow@31077
  1448
by(metis length_0_conv length_list_update)
nipkow@31077
  1449
wenzelm@13114
  1450
lemma list_update_same_conv:
nipkow@24526
  1451
"i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
nipkow@24526
  1452
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1453
nipkow@14187
  1454
lemma list_update_append1:
nipkow@24526
  1455
 "i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"
nipkow@24526
  1456
apply (induct xs arbitrary: i, simp)
nipkow@14187
  1457
apply(simp split:nat.split)
nipkow@14187
  1458
done
nipkow@14187
  1459
kleing@15868
  1460
lemma list_update_append:
nipkow@24526
  1461
  "(xs @ ys) [n:= x] = 
kleing@15868
  1462
  (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))"
nipkow@24526
  1463
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1464
nipkow@14402
  1465
lemma list_update_length [simp]:
nipkow@14402
  1466
 "(xs @ x # ys)[length xs := y] = (xs @ y # ys)"
nipkow@14402
  1467
by (induct xs, auto)
nipkow@14402
  1468
nipkow@31264
  1469
lemma map_update: "map f (xs[k:= y]) = (map f xs)[k := f y]"
nipkow@31264
  1470
by(induct xs arbitrary: k)(auto split:nat.splits)
nipkow@31264
  1471
nipkow@31264
  1472
lemma rev_update:
nipkow@31264
  1473
  "k < length xs \<Longrightarrow> rev (xs[k:= y]) = (rev xs)[length xs - k - 1 := y]"
nipkow@31264
  1474
by (induct xs arbitrary: k) (auto simp: list_update_append split:nat.splits)
nipkow@31264
  1475
wenzelm@13114
  1476
lemma update_zip:
nipkow@31080
  1477
  "(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
nipkow@24526
  1478
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split)
nipkow@24526
  1479
nipkow@24526
  1480
lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)"
nipkow@24526
  1481
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1482
wenzelm@13114
  1483
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
nipkow@13145
  1484
by (blast dest!: set_update_subset_insert [THEN subsetD])
wenzelm@13114
  1485
nipkow@24526
  1486
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])"
nipkow@24526
  1487
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1488
nipkow@31077
  1489
lemma list_update_overwrite[simp]:
haftmann@24796
  1490
  "xs [i := x, i := y] = xs [i := y]"
nipkow@31077
  1491
apply (induct xs arbitrary: i) apply simp
nipkow@31077
  1492
apply (case_tac i, simp_all)
haftmann@24796
  1493
done
haftmann@24796
  1494
haftmann@24796
  1495
lemma list_update_swap:
haftmann@24796
  1496
  "i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]"
haftmann@24796
  1497
apply (induct xs arbitrary: i i')
haftmann@24796
  1498
apply simp
haftmann@24796
  1499
apply (case_tac i, case_tac i')
haftmann@24796
  1500
apply auto
haftmann@24796
  1501
apply (case_tac i')
haftmann@24796
  1502
apply auto
haftmann@24796
  1503
done
haftmann@24796
  1504
haftmann@29827
  1505
lemma list_update_code [code]:
haftmann@29827
  1506
  "[][i := y] = []"
haftmann@29827
  1507
  "(x # xs)[0 := y] = y # xs"
haftmann@29827
  1508
  "(x # xs)[Suc i := y] = x # xs[i := y]"
haftmann@29827
  1509
  by simp_all
haftmann@29827
  1510
wenzelm@13114
  1511
nipkow@15392
  1512
subsubsection {* @{text last} and @{text butlast} *}
wenzelm@13114
  1513
wenzelm@13142
  1514
lemma last_snoc [simp]: "last (xs @ [x]) = x"
nipkow@13145
  1515
by (induct xs) auto
wenzelm@13114
  1516
wenzelm@13142
  1517
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
nipkow@13145
  1518
by (induct xs) auto
wenzelm@13114
  1519
nipkow@14302
  1520
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x"
nipkow@14302
  1521
by(simp add:last.simps)
nipkow@14302
  1522
nipkow@14302
  1523
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs"
nipkow@14302
  1524
by(simp add:last.simps)
nipkow@14302
  1525
nipkow@14302
  1526
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)"
nipkow@14302
  1527
by (induct xs) (auto)
nipkow@14302
  1528
nipkow@14302
  1529
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs"
nipkow@14302
  1530
by(simp add:last_append)
nipkow@14302
  1531
nipkow@14302
  1532
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"
nipkow@14302
  1533
by(simp add:last_append)
nipkow@14302
  1534
nipkow@17762
  1535
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"
nipkow@17762
  1536
by(rule rev_exhaust[of xs]) simp_all
nipkow@17762
  1537
nipkow@17762
  1538
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"
nipkow@17762
  1539
by(cases xs) simp_all
nipkow@17762
  1540
nipkow@17765
  1541
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as"
nipkow@17765
  1542
by (induct as) auto
nipkow@17762
  1543
wenzelm@13142
  1544
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
nipkow@13145
  1545
by (induct xs rule: rev_induct) auto
wenzelm@13114
  1546
wenzelm@13114
  1547
lemma butlast_append:
nipkow@24526
  1548
  "butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
nipkow@24526
  1549
by (induct xs arbitrary: ys) auto
wenzelm@13114
  1550
wenzelm@13142
  1551
lemma append_butlast_last_id [simp]:
nipkow@13145
  1552
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
nipkow@13145
  1553
by (induct xs) auto
wenzelm@13114
  1554
wenzelm@13142
  1555
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
nipkow@13145
  1556
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1557
wenzelm@13114
  1558
lemma in_set_butlast_appendI:
nipkow@13145
  1559
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
nipkow@13145
  1560
by (auto dest: in_set_butlastD simp add: butlast_append)
wenzelm@13114
  1561
nipkow@24526
  1562
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs"
nipkow@24526
  1563
apply (induct xs arbitrary: n)
nipkow@17501
  1564
 apply simp
nipkow@17501
  1565
apply (auto split:nat.split)
nipkow@17501
  1566
done
nipkow@17501
  1567
huffman@30128
  1568
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"
nipkow@17589
  1569
by(induct xs)(auto simp:neq_Nil_conv)
nipkow@17589
  1570
huffman@30128
  1571
lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs"
huffman@26584
  1572
by (induct xs, simp, case_tac xs, simp_all)
huffman@26584
  1573
nipkow@31077
  1574
lemma last_list_update:
nipkow@31077
  1575
  "xs \<noteq> [] \<Longrightarrow> last(xs[k:=x]) = (if k = size xs - 1 then x else last xs)"
nipkow@31077
  1576
by (auto simp: last_conv_nth)
nipkow@31077
  1577
nipkow@31077
  1578
lemma butlast_list_update:
nipkow@31077
  1579
  "butlast(xs[k:=x]) =
nipkow@31077
  1580
 (if k = size xs - 1 then butlast xs else (butlast xs)[k:=x])"
nipkow@31077
  1581
apply(cases xs rule:rev_cases)
nipkow@31077
  1582
apply simp
nipkow@31077
  1583
apply(simp add:list_update_append split:nat.splits)
nipkow@31077
  1584
done
nipkow@31077
  1585
haftmann@36851
  1586
lemma last_map:
haftmann@36851
  1587
  "xs \<noteq> [] \<Longrightarrow> last (map f xs) = f (last xs)"
haftmann@36851
  1588
  by (cases xs rule: rev_cases) simp_all
haftmann@36851
  1589
haftmann@36851
  1590
lemma map_butlast:
haftmann@36851
  1591
  "map f (butlast xs) = butlast (map f xs)"
haftmann@36851
  1592
  by (induct xs) simp_all
haftmann@36851
  1593
nipkow@40230
  1594
lemma snoc_eq_iff_butlast:
nipkow@40230
  1595
  "xs @ [x] = ys \<longleftrightarrow> (ys \<noteq> [] & butlast ys = xs & last ys = x)"
nipkow@40230
  1596
by (metis append_butlast_last_id append_is_Nil_conv butlast_snoc last_snoc not_Cons_self)
nipkow@40230
  1597
haftmann@24796
  1598
nipkow@15392
  1599
subsubsection {* @{text take} and @{text drop} *}
wenzelm@13114
  1600
wenzelm@13142
  1601
lemma take_0 [simp]: "take 0 xs = []"
nipkow@13145
  1602
by (induct xs) auto
wenzelm@13114
  1603
wenzelm@13142
  1604
lemma drop_0 [simp]: "drop 0 xs = xs"
nipkow@13145
  1605
by (induct xs) auto
wenzelm@13114
  1606
wenzelm@13142
  1607
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
nipkow@13145
  1608
by simp
wenzelm@13114
  1609
wenzelm@13142
  1610
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
nipkow@13145
  1611
by simp
wenzelm@13114
  1612
wenzelm@13142
  1613
declare take_Cons [simp del] and drop_Cons [simp del]
wenzelm@13114
  1614
huffman@30128
  1615
lemma take_1_Cons [simp]: "take 1 (x # xs) = [x]"
huffman@30128
  1616
  unfolding One_nat_def by simp
huffman@30128
  1617
huffman@30128
  1618
lemma drop_1_Cons [simp]: "drop 1 (x # xs) = xs"
huffman@30128
  1619
  unfolding One_nat_def by simp
huffman@30128
  1620
nipkow@15110
  1621
lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)"
nipkow@15110
  1622
by(clarsimp simp add:neq_Nil_conv)
nipkow@15110
  1623
nipkow@14187
  1624
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"
nipkow@14187
  1625
by(cases xs, simp_all)
nipkow@14187
  1626
huffman@26584
  1627
lemma take_tl: "take n (tl xs) = tl (take (Suc n) xs)"
huffman@26584
  1628
by (induct xs arbitrary: n) simp_all
huffman@26584
  1629
nipkow@24526
  1630
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)"
nipkow@24526
  1631
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split)
nipkow@24526
  1632
huffman@26584
  1633
lemma tl_take: "tl (take n xs) = take (n - 1) (tl xs)"
huffman@26584
  1634
by (cases n, simp, cases xs, auto)
huffman@26584
  1635
huffman@26584
  1636
lemma tl_drop: "tl (drop n xs) = drop n (tl xs)"
huffman@26584
  1637
by (simp only: drop_tl)
huffman@26584
  1638
nipkow@24526
  1639
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y"
nipkow@24526
  1640
apply (induct xs arbitrary: n, simp)
nipkow@14187
  1641
apply(simp add:drop_Cons nth_Cons split:nat.splits)
nipkow@14187
  1642
done
nipkow@14187
  1643
nipkow@13913
  1644
lemma take_Suc_conv_app_nth:
nipkow@24526
  1645
  "i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"
nipkow@24526
  1646
apply (induct xs arbitrary: i, simp)
paulson@14208
  1647
apply (case_tac i, auto)
nipkow@13913
  1648
done
nipkow@13913
  1649
mehta@14591
  1650
lemma drop_Suc_conv_tl:
nipkow@24526
  1651
  "i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs"
nipkow@24526
  1652
apply (induct xs arbitrary: i, simp)
mehta@14591
  1653
apply (case_tac i, auto)
mehta@14591
  1654
done
mehta@14591
  1655
nipkow@24526
  1656
lemma length_take [simp]: "length (take n xs) = min (length xs) n"
nipkow@24526
  1657
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1658
nipkow@24526
  1659
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)"
nipkow@24526
  1660
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1661
nipkow@24526
  1662
lemma take_all [simp]: "length xs <= n ==> take n xs = xs"
nipkow@24526
  1663
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1664
nipkow@24526
  1665
lemma drop_all [simp]: "length xs <= n ==> drop n xs = []"
nipkow@24526
  1666
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1667
wenzelm@13142
  1668
lemma take_append [simp]:
nipkow@24526
  1669
  "take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
nipkow@24526
  1670
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1671
wenzelm@13142
  1672
lemma drop_append [simp]:
nipkow@24526
  1673
  "drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
nipkow@24526
  1674
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1675
nipkow@24526
  1676
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs"
nipkow@24526
  1677
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1678
apply (case_tac xs, auto)
nipkow@15236
  1679
apply (case_tac n, auto)
nipkow@13145
  1680
done
wenzelm@13114
  1681
nipkow@24526
  1682
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs"
nipkow@24526
  1683
apply (induct m arbitrary: xs, auto)
paulson@14208
  1684
apply (case_tac xs, auto)
nipkow@13145
  1685
done
wenzelm@13114
  1686
nipkow@24526
  1687
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)"
nipkow@24526
  1688
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1689
apply (case_tac xs, auto)
nipkow@13145
  1690
done
wenzelm@13114
  1691
nipkow@24526
  1692
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)"
nipkow@24526
  1693
apply(induct xs arbitrary: m n)
nipkow@14802
  1694
 apply simp
nipkow@14802
  1695
apply(simp add: take_Cons drop_Cons split:nat.split)
nipkow@14802
  1696
done
nipkow@14802
  1697
nipkow@24526
  1698
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs"
nipkow@24526
  1699
apply (induct n arbitrary: xs, auto)
paulson@14208
  1700
apply (case_tac xs, auto)
nipkow@13145
  1701
done
wenzelm@13114
  1702
nipkow@24526
  1703
lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])"
nipkow@24526
  1704
apply(induct xs arbitrary: n)
nipkow@15110
  1705
 apply simp
nipkow@15110
  1706
apply(simp add:take_Cons split:nat.split)
nipkow@15110
  1707
done
nipkow@15110
  1708
nipkow@24526
  1709
lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)"
nipkow@24526
  1710
apply(induct xs arbitrary: n)
nipkow@15110
  1711
apply simp
nipkow@15110
  1712
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1713
done
nipkow@15110
  1714
nipkow@24526
  1715
lemma take_map: "take n (map f xs) = map f (take n xs)"
nipkow@24526
  1716
apply (induct n arbitrary: xs, auto)
paulson@14208
  1717
apply (case_tac xs, auto)
nipkow@13145
  1718
done
wenzelm@13114
  1719
nipkow@24526
  1720
lemma drop_map: "drop n (map f xs) = map f (drop n xs)"
nipkow@24526
  1721
apply (induct n arbitrary: xs, auto)
paulson@14208
  1722
apply (case_tac xs, auto)
nipkow@13145
  1723
done
wenzelm@13114
  1724
nipkow@24526
  1725
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)"
nipkow@24526
  1726
apply (induct xs arbitrary: i, auto)
paulson@14208
  1727
apply (case_tac i, auto)
nipkow@13145
  1728
done
wenzelm@13114
  1729
nipkow@24526
  1730
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)"
nipkow@24526
  1731
apply (induct xs arbitrary: i, auto)
paulson@14208
  1732
apply (case_tac i, auto)
nipkow@13145
  1733
done
wenzelm@13114
  1734
nipkow@24526
  1735
lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i"
nipkow@24526
  1736
apply (induct xs arbitrary: i n, auto)
paulson@14208
  1737
apply (case_tac n, blast)
paulson@14208
  1738
apply (case_tac i, auto)
nipkow@13145
  1739
done
wenzelm@13114
  1740
wenzelm@13142
  1741
lemma nth_drop [simp]:
nipkow@24526
  1742
  "n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
nipkow@24526
  1743
apply (induct n arbitrary: xs i, auto)
paulson@14208
  1744
apply (case_tac xs, auto)
nipkow@13145
  1745
done
nipkow@3507
  1746
huffman@26584
  1747
lemma butlast_take:
huffman@30128
  1748
  "n <= length xs ==> butlast (take n xs) = take (n - 1) xs"
huffman@26584
  1749
by (simp add: butlast_conv_take min_max.inf_absorb1 min_max.inf_absorb2)
huffman@26584
  1750
huffman@26584
  1751
lemma butlast_drop: "butlast (drop n xs) = drop n (butlast xs)"
huffman@30128
  1752
by (simp add: butlast_conv_take drop_take add_ac)
huffman@26584
  1753
huffman@26584
  1754
lemma take_butlast: "n < length xs ==> take n (butlast xs) = take n xs"
huffman@26584
  1755
by (simp add: butlast_conv_take min_max.inf_absorb1)
huffman@26584
  1756
huffman@26584
  1757
lemma drop_butlast: "drop n (butlast xs) = butlast (drop n xs)"
huffman@30128
  1758
by (simp add: butlast_conv_take drop_take add_ac)
huffman@26584
  1759
nipkow@18423
  1760
lemma hd_drop_conv_nth: "\<lbrakk> xs \<noteq> []; n < length xs \<rbrakk> \<Longrightarrow> hd(drop n xs) = xs!n"
nipkow@18423
  1761
by(simp add: hd_conv_nth)
nipkow@18423
  1762
nipkow@35248
  1763
lemma set_take_subset_set_take:
nipkow@35248
  1764
  "m <= n \<Longrightarrow> set(take m xs) <= set(take n xs)"
bulwahn@41463
  1765
apply (induct xs arbitrary: m n)
bulwahn@41463
  1766
apply simp
bulwahn@41463
  1767
apply (case_tac n)
bulwahn@41463
  1768
apply (auto simp: take_Cons)
bulwahn@41463
  1769
done
nipkow@35248
  1770
nipkow@24526
  1771
lemma set_take_subset: "set(take n xs) \<subseteq> set xs"
nipkow@24526
  1772
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split)
nipkow@24526
  1773
nipkow@24526
  1774
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs"
nipkow@24526
  1775
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split)
nipkow@14025
  1776
nipkow@35248
  1777
lemma set_drop_subset_set_drop:
nipkow@35248
  1778
  "m >= n \<Longrightarrow> set(drop m xs) <= set(drop n xs)"
nipkow@35248
  1779
apply(induct xs arbitrary: m n)
nipkow@35248
  1780
apply(auto simp:drop_Cons split:nat.split)
nipkow@35248
  1781
apply (metis set_drop_subset subset_iff)
nipkow@35248
  1782
done
nipkow@35248
  1783
nipkow@14187
  1784
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  1785
using set_take_subset by fast
nipkow@14187
  1786
nipkow@14187
  1787
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  1788
using set_drop_subset by fast
nipkow@14187
  1789
wenzelm@13114
  1790
lemma append_eq_conv_conj:
nipkow@24526
  1791
  "(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
nipkow@24526
  1792
apply (induct xs arbitrary: zs, simp, clarsimp)
paulson@14208
  1793
apply (case_tac zs, auto)
nipkow@13145
  1794
done
wenzelm@13142
  1795
nipkow@24526
  1796
lemma take_add: 
nipkow@24526
  1797
  "i+j \<le> length(xs) \<Longrightarrow> take (i+j) xs = take i xs @ take j (drop i xs)"
nipkow@24526
  1798
apply (induct xs arbitrary: i, auto) 
nipkow@24526
  1799
apply (case_tac i, simp_all)
paulson@14050
  1800
done
paulson@14050
  1801
nipkow@14300
  1802
lemma append_eq_append_conv_if:
nipkow@24526
  1803
 "(xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) =
nipkow@14300
  1804
  (if size xs\<^isub>1 \<le> size ys\<^isub>1
nipkow@14300
  1805
   then xs\<^isub>1 = take (size xs\<^isub>1) ys\<^isub>1 \<and> xs\<^isub>2 = drop (size xs\<^isub>1) ys\<^isub>1 @ ys\<^isub>2
nipkow@14300
  1806
   else take (size ys\<^isub>1) xs\<^isub>1 = ys\<^isub>1 \<and> drop (size ys\<^isub>1) xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>2)"
nipkow@24526
  1807
apply(induct xs\<^isub>1 arbitrary: ys\<^isub>1)
nipkow@14300
  1808
 apply simp
nipkow@14300
  1809
apply(case_tac ys\<^isub>1)
nipkow@14300
  1810
apply simp_all
nipkow@14300
  1811
done
nipkow@14300
  1812
nipkow@15110
  1813
lemma take_hd_drop:
huffman@30079
  1814
  "n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (Suc n) xs"
nipkow@24526
  1815
apply(induct xs arbitrary: n)
nipkow@15110
  1816
apply simp
nipkow@15110
  1817
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1818
done
nipkow@15110
  1819
nipkow@17501
  1820
lemma id_take_nth_drop:
nipkow@17501
  1821
 "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" 
nipkow@17501
  1822
proof -
nipkow@17501
  1823
  assume si: "i < length xs"
nipkow@17501
  1824
  hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto
nipkow@17501
  1825
  moreover
nipkow@17501
  1826
  from si have "take (Suc i) xs = take i xs @ [xs!i]"
nipkow@17501
  1827
    apply (rule_tac take_Suc_conv_app_nth) by arith
nipkow@17501
  1828
  ultimately show ?thesis by auto
nipkow@17501
  1829
qed
nipkow@17501
  1830
  
nipkow@17501
  1831
lemma upd_conv_take_nth_drop:
nipkow@17501
  1832
 "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  1833
proof -
nipkow@17501
  1834
  assume i: "i < length xs"
nipkow@17501
  1835
  have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]"
nipkow@17501
  1836
    by(rule arg_cong[OF id_take_nth_drop[OF i]])
nipkow@17501
  1837
  also have "\<dots> = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  1838
    using i by (simp add: list_update_append)
nipkow@17501
  1839
  finally show ?thesis .
nipkow@17501
  1840
qed
nipkow@17501
  1841
haftmann@24796
  1842
lemma nth_drop':
haftmann@24796
  1843
  "i < length xs \<Longrightarrow> xs ! i # drop (Suc i) xs = drop i xs"
haftmann@24796
  1844
apply (induct i arbitrary: xs)
haftmann@24796
  1845
apply (simp add: neq_Nil_conv)
haftmann@24796
  1846
apply (erule exE)+
haftmann@24796
  1847
apply simp
haftmann@24796
  1848
apply (case_tac xs)
haftmann@24796
  1849
apply simp_all
haftmann@24796
  1850
done
haftmann@24796
  1851
wenzelm@13114
  1852
nipkow@15392
  1853
subsubsection {* @{text takeWhile} and @{text dropWhile} *}
wenzelm@13114
  1854
hoelzl@33639
  1855
lemma length_takeWhile_le: "length (takeWhile P xs) \<le> length xs"
hoelzl@33639
  1856
  by (induct xs) auto
hoelzl@33639
  1857
wenzelm@13142
  1858
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
nipkow@13145
  1859
by (induct xs) auto
wenzelm@13114
  1860
wenzelm@13142
  1861
lemma takeWhile_append1 [simp]:
nipkow@13145
  1862
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"
nipkow@13145
  1863
by (induct xs) auto
wenzelm@13114
  1864
wenzelm@13142
  1865
lemma takeWhile_append2 [simp]:
nipkow@13145
  1866
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
nipkow@13145
  1867
by (induct xs) auto
wenzelm@13114
  1868
wenzelm@13142
  1869
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
nipkow@13145
  1870
by (induct xs) auto
wenzelm@13114
  1871
hoelzl@33639
  1872
lemma takeWhile_nth: "j < length (takeWhile P xs) \<Longrightarrow> takeWhile P xs ! j = xs ! j"
hoelzl@33639
  1873
apply (subst (3) takeWhile_dropWhile_id[symmetric]) unfolding nth_append by auto
hoelzl@33639
  1874
hoelzl@33639
  1875
lemma dropWhile_nth: "j < length (dropWhile P xs) \<Longrightarrow> dropWhile P xs ! j = xs ! (j + length (takeWhile P xs))"
hoelzl@33639
  1876
apply (subst (3) takeWhile_dropWhile_id[symmetric]) unfolding nth_append by auto
hoelzl@33639
  1877
hoelzl@33639
  1878
lemma length_dropWhile_le: "length (dropWhile P xs) \<le> length xs"
hoelzl@33639
  1879
by (induct xs) auto
hoelzl@33639
  1880
wenzelm@13142
  1881
lemma dropWhile_append1 [simp]:
nipkow@13145
  1882
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
nipkow@13145
  1883
by (induct xs) auto
wenzelm@13114
  1884
wenzelm@13142
  1885
lemma dropWhile_append2 [simp]:
nipkow@13145
  1886
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
nipkow@13145
  1887
by (induct xs) auto
wenzelm@13114
  1888
krauss@23971
  1889
lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
nipkow@13145
  1890
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1891
nipkow@13913
  1892
lemma takeWhile_eq_all_conv[simp]:
nipkow@13913
  1893
 "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  1894
by(induct xs, auto)
nipkow@13913
  1895
nipkow@13913
  1896
lemma dropWhile_eq_Nil_conv[simp]:
nipkow@13913
  1897
 "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  1898
by(induct xs, auto)
nipkow@13913
  1899
nipkow@13913
  1900
lemma dropWhile_eq_Cons_conv:
nipkow@13913
  1901
 "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"
nipkow@13913
  1902
by(induct xs, auto)
nipkow@13913
  1903
nipkow@31077
  1904
lemma distinct_takeWhile[simp]: "distinct xs ==> distinct (takeWhile P xs)"
nipkow@31077
  1905
by (induct xs) (auto dest: set_takeWhileD)
nipkow@31077
  1906
nipkow@31077
  1907
lemma distinct_dropWhile[simp]: "distinct xs ==> distinct (dropWhile P xs)"
nipkow@31077
  1908
by (induct xs) auto
nipkow@31077
  1909
hoelzl@33639
  1910
lemma takeWhile_map: "takeWhile P (map f xs) = map f (takeWhile (P \<circ> f) xs)"
hoelzl@33639
  1911
by (induct xs) auto
hoelzl@33639
  1912
hoelzl@33639
  1913
lemma dropWhile_map: "dropWhile P (map f xs) = map f (dropWhile (P \<circ> f) xs)"
hoelzl@33639
  1914
by (induct xs) auto
hoelzl@33639
  1915
hoelzl@33639
  1916
lemma takeWhile_eq_take: "takeWhile P xs = take (length (takeWhile P xs)) xs"
hoelzl@33639
  1917
by (induct xs) auto
hoelzl@33639
  1918
hoelzl@33639
  1919
lemma dropWhile_eq_drop: "dropWhile P xs = drop (length (takeWhile P xs)) xs"
hoelzl@33639
  1920
by (induct xs) auto
hoelzl@33639
  1921
hoelzl@33639
  1922
lemma hd_dropWhile:
hoelzl@33639
  1923
  "dropWhile P xs \<noteq> [] \<Longrightarrow> \<not> P (hd (dropWhile P xs))"
hoelzl@33639
  1924
using assms by (induct xs) auto
hoelzl@33639
  1925
hoelzl@33639
  1926
lemma takeWhile_eq_filter:
hoelzl@33639
  1927
  assumes "\<And> x. x \<in> set (dropWhile P xs) \<Longrightarrow> \<not> P x"
hoelzl@33639
  1928
  shows "takeWhile P xs = filter P xs"
hoelzl@33639
  1929
proof -
hoelzl@33639
  1930
  have A: "filter P xs = filter P (takeWhile P xs @ dropWhile P xs)"
hoelzl@33639
  1931
    by simp
hoelzl@33639
  1932
  have B: "filter P (dropWhile P xs) = []"
hoelzl@33639
  1933
    unfolding filter_empty_conv using assms by blast
hoelzl@33639
  1934
  have "filter P xs = takeWhile P xs"
hoelzl@33639
  1935
    unfolding A filter_append B
hoelzl@33639
  1936
    by (auto simp add: filter_id_conv dest: set_takeWhileD)
hoelzl@33639
  1937
  thus ?thesis ..
hoelzl@33639
  1938
qed
hoelzl@33639
  1939
hoelzl@33639
  1940
lemma takeWhile_eq_take_P_nth:
hoelzl@33639
  1941
  "\<lbrakk> \<And> i. \<lbrakk> i < n ; i < length xs \<rbrakk> \<Longrightarrow> P (xs ! i) ; n < length xs \<Longrightarrow> \<not> P (xs ! n) \<rbrakk> \<Longrightarrow>
hoelzl@33639
  1942
  takeWhile P xs = take n xs"
hoelzl@33639
  1943
proof (induct xs arbitrary: n)
hoelzl@33639
  1944
  case (Cons x xs)
hoelzl@33639
  1945
  thus ?case
hoelzl@33639
  1946
  proof (cases n)
hoelzl@33639
  1947
    case (Suc n') note this[simp]
hoelzl@33639
  1948
    have "P x" using Cons.prems(1)[of 0] by simp
hoelzl@33639
  1949
    moreover have "takeWhile P xs = take n' xs"
hoelzl@33639
  1950
    proof (rule Cons.hyps)
hoelzl@33639
  1951
      case goal1 thus "P (xs ! i)" using Cons.prems(1)[of "Suc i"] by simp
hoelzl@33639
  1952
    next case goal2 thus ?case using Cons by auto
hoelzl@33639
  1953
    qed
hoelzl@33639
  1954
    ultimately show ?thesis by simp
hoelzl@33639
  1955
   qed simp
hoelzl@33639
  1956
qed simp
hoelzl@33639
  1957
hoelzl@33639
  1958
lemma nth_length_takeWhile:
hoelzl@33639
  1959
  "length (takeWhile P xs) < length xs \<Longrightarrow> \<not> P (xs ! length (takeWhile P xs))"
hoelzl@33639
  1960
by (induct xs) auto
hoelzl@33639
  1961
hoelzl@33639
  1962
lemma length_takeWhile_less_P_nth:
hoelzl@33639
  1963
  assumes all: "\<And> i. i < j \<Longrightarrow> P (xs ! i)" and "j \<le> length xs"
hoelzl@33639
  1964
  shows "j \<le> length (takeWhile P xs)"
hoelzl@33639
  1965
proof (rule classical)
hoelzl@33639
  1966
  assume "\<not> ?thesis"
hoelzl@33639
  1967
  hence "length (takeWhile P xs) < length xs" using assms by simp
hoelzl@33639
  1968
  thus ?thesis using all `\<not> ?thesis` nth_length_takeWhile[of P xs] by auto
hoelzl@33639
  1969
qed
nipkow@31077
  1970
nipkow@17501
  1971
text{* The following two lemmmas could be generalized to an arbitrary
nipkow@17501
  1972
property. *}
nipkow@17501
  1973
nipkow@17501
  1974
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  1975
 takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))"
nipkow@17501
  1976
by(induct xs) (auto simp: takeWhile_tail[where l="[]"])
nipkow@17501
  1977
nipkow@17501
  1978
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  1979
  dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)"
nipkow@17501
  1980
apply(induct xs)
nipkow@17501
  1981
 apply simp
nipkow@17501
  1982
apply auto
nipkow@17501
  1983
apply(subst dropWhile_append2)
nipkow@17501
  1984
apply auto
nipkow@17501
  1985
done
nipkow@17501
  1986
nipkow@18423
  1987
lemma takeWhile_not_last:
nipkow@18423
  1988
 "\<lbrakk> xs \<noteq> []; distinct xs\<rbrakk> \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs"
nipkow@18423
  1989
apply(induct xs)
nipkow@18423
  1990
 apply simp
nipkow@18423
  1991
apply(case_tac xs)
nipkow@18423
  1992
apply(auto)
nipkow@18423
  1993
done
nipkow@18423
  1994
krauss@19770
  1995
lemma takeWhile_cong [fundef_cong, recdef_cong]:
krauss@18336
  1996
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  1997
  ==> takeWhile P l = takeWhile Q k"
nipkow@24349
  1998
by (induct k arbitrary: l) (simp_all)
krauss@18336
  1999
krauss@19770
  2000
lemma dropWhile_cong [fundef_cong, recdef_cong]:
krauss@18336
  2001
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  2002
  ==> dropWhile P l = dropWhile Q k"
nipkow@24349
  2003
by (induct k arbitrary: l, simp_all)
krauss@18336
  2004
wenzelm@13114
  2005
nipkow@15392
  2006
subsubsection {* @{text zip} *}
wenzelm@13114
  2007
wenzelm@13142
  2008
lemma zip_Nil [simp]: "zip [] ys = []"
nipkow@13145
  2009
by (induct ys) auto
wenzelm@13114
  2010
wenzelm@13142
  2011
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
nipkow@13145
  2012
by simp
wenzelm@13114
  2013
wenzelm@13142
  2014
declare zip_Cons [simp del]
wenzelm@13114
  2015
haftmann@36198
  2016
lemma [code]:
haftmann@36198
  2017
  "zip [] ys = []"
haftmann@36198
  2018
  "zip xs [] = []"
haftmann@36198
  2019
  "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
haftmann@36198
  2020
  by (fact zip_Nil zip.simps(1) zip_Cons_Cons)+
haftmann@36198
  2021
nipkow@15281
  2022
lemma zip_Cons1:
nipkow@15281
  2023
 "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)"
nipkow@15281
  2024
by(auto split:list.split)
nipkow@15281
  2025
wenzelm@13142
  2026
lemma length_zip [simp]:
krauss@22493
  2027
"length (zip xs ys) = min (length xs) (length ys)"
krauss@22493
  2028
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  2029
haftmann@34978
  2030
lemma zip_obtain_same_length:
haftmann@34978
  2031
  assumes "\<And>zs ws n. length zs = length ws \<Longrightarrow> n = min (length xs) (length ys)
haftmann@34978
  2032
    \<Longrightarrow> zs = take n xs \<Longrightarrow> ws = take n ys \<Longrightarrow> P (zip zs ws)"
haftmann@34978
  2033
  shows "P (zip xs ys)"
haftmann@34978
  2034
proof -
haftmann@34978
  2035
  let ?n = "min (length xs) (length ys)"
haftmann@34978
  2036
  have "P (zip (take ?n xs) (take ?n ys))"
haftmann@34978
  2037
    by (rule assms) simp_all
haftmann@34978
  2038
  moreover have "zip xs ys = zip (take ?n xs) (take ?n ys)"
haftmann@34978
  2039
  proof (induct xs arbitrary: ys)
haftmann@34978
  2040
    case Nil then show ?case by simp
haftmann@34978
  2041
  next
haftmann@34978
  2042
    case (Cons x xs) then show ?case by (cases ys) simp_all
haftmann@34978
  2043
  qed
haftmann@34978
  2044
  ultimately show ?thesis by simp
haftmann@34978
  2045
qed
haftmann@34978
  2046
wenzelm@13114
  2047
lemma zip_append1:
krauss@22493
  2048
"zip (xs @ ys) zs =
nipkow@13145
  2049
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
krauss@22493
  2050
by (induct xs zs rule:list_induct2') auto
wenzelm@13114
  2051
wenzelm@13114
  2052
lemma zip_append2:
krauss@22493
  2053
"zip xs (ys @ zs) =
nipkow@13145
  2054
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
krauss@22493
  2055
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  2056
wenzelm@13142
  2057
lemma zip_append [simp]:
wenzelm@13142
  2058
 "[| length xs = length us; length ys = length vs |] ==>
nipkow@13145
  2059
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
nipkow@13145
  2060
by (simp add: zip_append1)
wenzelm@13114
  2061
wenzelm@13114
  2062
lemma zip_rev:
nipkow@14247
  2063
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
nipkow@14247
  2064
by (induct rule:list_induct2, simp_all)
wenzelm@13114
  2065
hoelzl@33639
  2066
lemma zip_map_map:
hoelzl@33639
  2067
  "zip (map f xs) (map g ys) = map (\<lambda> (x, y). (f x, g y)) (zip xs ys)"
hoelzl@33639
  2068
proof (induct xs arbitrary: ys)
hoelzl@33639
  2069
  case (Cons x xs) note Cons_x_xs = Cons.hyps
hoelzl@33639
  2070
  show ?case
hoelzl@33639
  2071
  proof (cases ys)
hoelzl@33639
  2072
    case (Cons y ys')
hoelzl@33639
  2073
    show ?thesis unfolding Cons using Cons_x_xs by simp
hoelzl@33639
  2074
  qed simp
hoelzl@33639
  2075
qed simp
hoelzl@33639
  2076
hoelzl@33639
  2077
lemma zip_map1:
hoelzl@33639
  2078
  "zip (map f xs) ys = map (\<lambda>(x, y). (f x, y)) (zip xs ys)"
hoelzl@33639
  2079
using zip_map_map[of f xs "\<lambda>x. x" ys] by simp
hoelzl@33639
  2080
hoelzl@33639
  2081
lemma zip_map2:
hoelzl@33639
  2082
  "zip xs (map f ys) = map (\<lambda>(x, y). (x, f y)) (zip xs ys)"
hoelzl@33639
  2083
using zip_map_map[of "\<lambda>x. x" xs f ys] by simp
hoelzl@33639
  2084
nipkow@23096
  2085
lemma map_zip_map:
hoelzl@33639
  2086
  "map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)"
hoelzl@33639
  2087
unfolding zip_map1 by auto
nipkow@23096
  2088
nipkow@23096
  2089
lemma map_zip_map2:
hoelzl@33639
  2090
  "map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)"
hoelzl@33639
  2091
unfolding zip_map2 by auto
nipkow@23096
  2092
nipkow@31080
  2093
text{* Courtesy of Andreas Lochbihler: *}
nipkow@31080
  2094
lemma zip_same_conv_map: "zip xs xs = map (\<lambda>x. (x, x)) xs"
nipkow@31080
  2095
by(induct xs) auto
nipkow@31080
  2096
wenzelm@13142
  2097
lemma nth_zip [simp]:
nipkow@24526
  2098
"[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"
nipkow@24526
  2099
apply (induct ys arbitrary: i xs, simp)
nipkow@13145
  2100
apply (case_tac xs)
nipkow@13145
  2101
 apply (simp_all add: nth.simps split: nat.split)
nipkow@13145
  2102
done
wenzelm@13114
  2103
wenzelm@13114
  2104
lemma set_zip:
nipkow@13145
  2105
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
nipkow@31080
  2106
by(simp add: set_conv_nth cong: rev_conj_cong)
wenzelm@13114
  2107
hoelzl@33639
  2108
lemma zip_same: "((a,b) \<in> set (zip xs xs)) = (a \<in> set xs \<and> a = b)"
hoelzl@33639
  2109
by(induct xs) auto
hoelzl@33639
  2110
wenzelm@13114
  2111
lemma zip_update:
nipkow@31080
  2112
  "zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
nipkow@31080
  2113
by(rule sym, simp add: update_zip)
wenzelm@13114
  2114
wenzelm@13142
  2115
lemma zip_replicate [simp]:
nipkow@24526
  2116
  "zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
nipkow@24526
  2117
apply (induct i arbitrary: j, auto)
paulson@14208
  2118
apply (case_tac j, auto)
nipkow@13145
  2119
done
wenzelm@13114
  2120
nipkow@19487
  2121
lemma take_zip:
nipkow@24526
  2122
  "take n (zip xs ys) = zip (take n xs) (take n ys)"
nipkow@24526
  2123
apply (induct n arbitrary: xs ys)
nipkow@19487
  2124
 apply simp
nipkow@19487
  2125
apply (case_tac xs, simp)
nipkow@19487
  2126
apply (case_tac ys, simp_all)
nipkow@19487
  2127
done
nipkow@19487
  2128
nipkow@19487
  2129
lemma drop_zip:
nipkow@24526
  2130
  "drop n (zip xs ys) = zip (drop n xs) (drop n ys)"
nipkow@24526
  2131
apply (induct n arbitrary: xs ys)
nipkow@19487
  2132
 apply simp
nipkow@19487
  2133
apply (case_tac xs, simp)
nipkow@19487
  2134
apply (case_tac ys, simp_all)
nipkow@19487
  2135
done
nipkow@19487
  2136
hoelzl@33639
  2137
lemma zip_takeWhile_fst: "zip (takeWhile P xs) ys = takeWhile (P \<circ> fst) (zip xs ys)"
hoelzl@33639
  2138
proof (induct xs arbitrary: ys)
hoelzl@33639
  2139
  case (Cons x xs) thus ?case by (cases ys) auto
hoelzl@33639
  2140
qed simp
hoelzl@33639
  2141
hoelzl@33639
  2142
lemma zip_takeWhile_snd: "zip xs (takeWhile P ys) = takeWhile (P \<circ> snd) (zip xs ys)"
hoelzl@33639
  2143
proof (induct xs arbitrary: ys)
hoelzl@33639
  2144
  case (Cons x xs) thus ?case by (cases ys) auto
hoelzl@33639
  2145
qed simp
hoelzl@33639
  2146
krauss@22493
  2147
lemma set_zip_leftD:
krauss@22493
  2148
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs"
krauss@22493
  2149
by (induct xs ys rule:list_induct2') auto
krauss@22493
  2150
krauss@22493
  2151
lemma set_zip_rightD:
krauss@22493
  2152
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys"
krauss@22493
  2153
by (induct xs ys rule:list_induct2') auto
wenzelm@13142
  2154
nipkow@23983
  2155
lemma in_set_zipE:
nipkow@23983
  2156
  "(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23983
  2157
by(blast dest: set_zip_leftD set_zip_rightD)
nipkow@23983
  2158
haftmann@29829
  2159
lemma zip_map_fst_snd:
haftmann@29829
  2160
  "zip (map fst zs) (map snd zs) = zs"
haftmann@29829
  2161
  by (induct zs) simp_all
haftmann@29829
  2162
haftmann@29829
  2163
lemma zip_eq_conv:
haftmann@29829
  2164
  "length xs = length ys \<Longrightarrow> zip xs ys = zs \<longleftrightarrow> map fst zs = xs \<and> map snd zs = ys"
haftmann@29829
  2165
  by (auto simp add: zip_map_fst_snd)
haftmann@29829
  2166
wenzelm@35115
  2167
nipkow@15392
  2168
subsubsection {* @{text list_all2} *}
wenzelm@13114
  2169
kleing@14316
  2170
lemma list_all2_lengthD [intro?]: 
kleing@14316
  2171
  "list_all2 P xs ys ==> length xs = length ys"
nipkow@24349
  2172
by (simp add: list_all2_def)
haftmann@19607
  2173
haftmann@19787
  2174
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])"
nipkow@24349
  2175
by (simp add: list_all2_def)
haftmann@19607
  2176
haftmann@19787
  2177
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])"
nipkow@24349
  2178
by (simp add: list_all2_def)
haftmann@19607
  2179
haftmann@19607
  2180
lemma list_all2_Cons [iff, code]:
haftmann@19607
  2181
  "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
nipkow@24349
  2182
by (auto simp add: list_all2_def)
wenzelm@13114
  2183
wenzelm@13114
  2184
lemma list_all2_Cons1:
nipkow@13145
  2185
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
nipkow@13145
  2186
by (cases ys) auto
wenzelm@13114
  2187
wenzelm@13114
  2188
lemma list_all2_Cons2:
nipkow@13145
  2189
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
nipkow@13145
  2190
by (cases xs) auto
wenzelm@13114
  2191
wenzelm@13142
  2192
lemma list_all2_rev [iff]:
nipkow@13145
  2193
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
nipkow@13145
  2194
by (simp add: list_all2_def zip_rev cong: conj_cong)
wenzelm@13114
  2195
kleing@13863
  2196
lemma list_all2_rev1:
kleing@13863
  2197
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"
kleing@13863
  2198
by (subst list_all2_rev [symmetric]) simp
kleing@13863
  2199
wenzelm@13114
  2200
lemma list_all2_append1:
nipkow@13145
  2201
"list_all2 P (xs @ ys) zs =
nipkow@13145
  2202
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
nipkow@13145
  2203
list_all2 P xs us \<and> list_all2 P ys vs)"
nipkow@13145
  2204
apply (simp add: list_all2_def zip_append1)
nipkow@13145
  2205
apply (rule iffI)
nipkow@13145
  2206
 apply (rule_tac x = "take (length xs) zs" in exI)
nipkow@13145
  2207
 apply (rule_tac x = "drop (length xs) zs" in exI)
paulson@14208
  2208
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  2209
apply (simp add: ball_Un)
nipkow@13145
  2210
done
wenzelm@13114
  2211
wenzelm@13114
  2212
lemma list_all2_append2:
nipkow@13145
  2213
"list_all2 P xs (ys @ zs) =
nipkow@13145
  2214
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
nipkow@13145
  2215
list_all2 P us ys \<and> list_all2 P vs zs)"
nipkow@13145
  2216
apply (simp add: list_all2_def zip_append2)
nipkow@13145
  2217
apply (rule iffI)
nipkow@13145
  2218
 apply (rule_tac x = "take (length ys) xs" in exI)
nipkow@13145
  2219
 apply (rule_tac x = "drop (length ys) xs" in exI)
paulson@14208
  2220
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  2221
apply (simp add: ball_Un)
nipkow@13145
  2222
done
wenzelm@13114
  2223
kleing@13863
  2224
lemma list_all2_append:
nipkow@14247
  2225
  "length xs = length ys \<Longrightarrow>
nipkow@14247
  2226
  list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"
nipkow@14247
  2227
by (induct rule:list_induct2, simp_all)
kleing@13863
  2228
kleing@13863
  2229
lemma list_all2_appendI [intro?, trans]:
kleing@13863
  2230
  "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"
nipkow@24349
  2231
by (simp add: list_all2_append list_all2_lengthD)
kleing@13863
  2232
wenzelm@13114
  2233
lemma list_all2_conv_all_nth:
nipkow@13145
  2234
"list_all2 P xs ys =
nipkow@13145
  2235
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
nipkow@13145
  2236
by (force simp add: list_all2_def set_zip)
wenzelm@13114
  2237
berghofe@13883
  2238
lemma list_all2_trans:
berghofe@13883
  2239
  assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"
berghofe@13883
  2240
  shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"
berghofe@13883
  2241
        (is "!!bs cs. PROP ?Q as bs cs")
berghofe@13883
  2242
proof (induct as)
berghofe@13883
  2243
  fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"
berghofe@13883
  2244
  show "!!cs. PROP ?Q (x # xs) bs cs"
berghofe@13883
  2245
  proof (induct bs)
berghofe@13883
  2246
    fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs"
berghofe@13883
  2247
    show "PROP ?Q (x # xs) (y # ys) cs"
berghofe@13883
  2248
      by (induct cs) (auto intro: tr I1 I2)
berghofe@13883
  2249
  qed simp
berghofe@13883
  2250
qed simp
berghofe@13883
  2251
kleing@13863
  2252
lemma list_all2_all_nthI [intro?]:
kleing@13863
  2253
  "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"
nipkow@24349
  2254
by (simp add: list_all2_conv_all_nth)
kleing@13863
  2255
paulson@14395
  2256
lemma list_all2I:
paulson@14395
  2257
  "\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b"
nipkow@24349
  2258
by (simp add: list_all2_def)
paulson@14395
  2259
kleing@14328
  2260
lemma list_all2_nthD:
kleing@13863
  2261
  "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  2262
by (simp add: list_all2_conv_all_nth)
kleing@13863
  2263
nipkow@14302
  2264
lemma list_all2_nthD2:
nipkow@14302
  2265
  "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  2266
by (frule list_all2_lengthD) (auto intro: list_all2_nthD)
nipkow@14302
  2267
kleing@13863
  2268
lemma list_all2_map1: 
kleing@13863
  2269
  "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"
nipkow@24349
  2270
by (simp add: list_all2_conv_all_nth)
kleing@13863
  2271
kleing@13863
  2272
lemma list_all2_map2: 
kleing@13863
  2273
  "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"
nipkow@24349
  2274
by (auto simp add: list_all2_conv_all_nth)
kleing@13863
  2275
kleing@14316
  2276
lemma list_all2_refl [intro?]:
kleing@13863
  2277
  "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"
nipkow@24349
  2278
by (simp add: list_all2_conv_all_nth)
kleing@13863
  2279
kleing@13863
  2280
lemma list_all2_update_cong:
kleing@13863
  2281
  "\<lbrakk> i<size xs; list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
nipkow@24349
  2282
by (simp add: list_all2_conv_all_nth nth_list_update)
kleing@13863
  2283
kleing@13863
  2284
lemma list_all2_update_cong2:
kleing@13863
  2285
  "\<lbrakk>list_all2 P xs ys; P x y; i < length ys\<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
nipkow@24349
  2286
by (simp add: list_all2_lengthD list_all2_update_cong)
kleing@13863
  2287
nipkow@14302
  2288
lemma list_all2_takeI [simp,intro?]:
nipkow@24526
  2289
  "list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)"
nipkow@24526
  2290
apply (induct xs arbitrary: n ys)
nipkow@24526
  2291
 apply simp
nipkow@24526
  2292
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  2293
apply (case_tac n)
nipkow@24526
  2294
apply auto
nipkow@24526
  2295
done
nipkow@14302
  2296
nipkow@14302
  2297
lemma list_all2_dropI [simp,intro?]:
nipkow@24526
  2298
  "list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)"
nipkow@24526
  2299
apply (induct as arbitrary: n bs, simp)
nipkow@24526
  2300
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  2301
apply (case_tac n, simp, simp)
nipkow@24526
  2302
done
kleing@13863
  2303
kleing@14327
  2304
lemma list_all2_mono [intro?]:
nipkow@24526
  2305
  "list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys"
nipkow@24526
  2306
apply (induct xs arbitrary: ys, simp)
nipkow@24526
  2307
apply (case_tac ys, auto)
nipkow@24526
  2308
done
kleing@13863
  2309
haftmann@22551
  2310
lemma list_all2_eq:
haftmann@22551
  2311
  "xs = ys \<longleftrightarrow> list_all2 (op =) xs ys"
nipkow@24349
  2312
by (induct xs ys rule: list_induct2') auto
haftmann@22551
  2313
nipkow@40230
  2314
lemma list_eq_iff_zip_eq:
nipkow@40230
  2315
  "xs = ys \<longleftrightarrow> length xs = length ys \<and> (\<forall>(x,y) \<in> set (zip xs ys). x = y)"
nipkow@40230
  2316
by(auto simp add: set_zip list_all2_eq list_all2_conv_all_nth cong: conj_cong)
nipkow@40230
  2317
wenzelm@13142
  2318
nipkow@15392
  2319
subsubsection {* @{text foldl} and @{text foldr} *}
wenzelm@13142
  2320
wenzelm@13142
  2321
lemma foldl_append [simp]:
nipkow@24526
  2322
  "foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
nipkow@24526
  2323
by (induct xs arbitrary: a) auto
wenzelm@13142
  2324
nipkow@14402
  2325
lemma foldr_append[simp]: "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)"
nipkow@14402
  2326
by (induct xs) auto
nipkow@14402
  2327
nipkow@23096
  2328
lemma foldr_map: "foldr g (map f xs) a = foldr (g o f) xs a"
nipkow@23096
  2329
by(induct xs) simp_all
nipkow@23096
  2330
nipkow@24449
  2331
text{* For efficient code generation: avoid intermediate list. *}
haftmann@31998
  2332
lemma foldl_map[code_unfold]:
nipkow@24449
  2333
  "foldl g a (map f xs) = foldl (%a x. g a (f x)) a xs"
nipkow@23096
  2334
by(induct xs arbitrary:a) simp_all
nipkow@23096
  2335
haftmann@34978
  2336
lemma foldl_apply:
haftmann@34978
  2337
  assumes "\<And>x. x \<in> set xs \<Longrightarrow> f x \<circ> h = h \<circ> g x"
haftmann@34978
  2338
  shows "foldl (\<lambda>s x. f x s) (h s) xs = h (foldl (\<lambda>s x. g x s) s xs)"
nipkow@39302
  2339
  by (rule sym, insert assms, induct xs arbitrary: s) (simp_all add: fun_eq_iff)
haftmann@31930
  2340
krauss@19770
  2341
lemma foldl_cong [fundef_cong, recdef_cong]:
krauss@18336
  2342
  "[| a = b; l = k; !!a x. x : set l ==> f a x = g a x |] 
krauss@18336
  2343
  ==> foldl f a l = foldl g b k"
nipkow@24349
  2344
by (induct k arbitrary: a b l) simp_all
krauss@18336
  2345
krauss@19770
  2346
lemma foldr_cong [fundef_cong, recdef_cong]:
krauss@18336
  2347
  "[| a = b; l = k; !!a x. x : set l ==> f x a = g x a |] 
krauss@18336
  2348
  ==> foldr f l a = foldr g k b"
nipkow@24349
  2349
by (induct k arbitrary: a b l) simp_all
krauss@18336
  2350
haftmann@35195
  2351
lemma foldl_fun_comm:
haftmann@35195
  2352
  assumes "\<And>x y s. f (f s x) y = f (f s y) x"
haftmann@35195
  2353
  shows "f (foldl f s xs) x = foldl f (f s x) xs"
haftmann@35195
  2354
  by (induct xs arbitrary: s)
haftmann@35195
  2355
    (simp_all add: assms)
haftmann@35195
  2356
nipkow@24449
  2357
lemma (in semigroup_add) foldl_assoc:
haftmann@25062
  2358
shows "foldl op+ (x+y) zs = x + (foldl op+ y zs)"
nipkow@24449
  2359
by (induct zs arbitrary: y) (simp_all add:add_assoc)
nipkow@24449
  2360
nipkow@24449
  2361
lemma (in monoid_add) foldl_absorb0:
haftmann@25062
  2362
shows "x + (foldl op+ 0 zs) = foldl op+ x zs"
nipkow@24449
  2363
by (induct zs) (simp_all add:foldl_assoc)
nipkow@24449
  2364
haftmann@35195
  2365
lemma foldl_rev:
haftmann@35195
  2366
  assumes "\<And>x y s. f (f s x) y = f (f s y) x"
haftmann@35195
  2367
  shows "foldl f s (rev xs) = foldl f s xs"
haftmann@35195
  2368
proof (induct xs arbitrary: s)
haftmann@35195
  2369
  case Nil then show ?case by simp
haftmann@35195
  2370
next
haftmann@35195
  2371
  case (Cons x xs) with assms show ?case by (simp add: foldl_fun_comm)
haftmann@35195
  2372
qed
haftmann@35195
  2373
haftmann@37605
  2374
lemma rev_foldl_cons [code]:
haftmann@37605
  2375
  "rev xs = foldl (\<lambda>xs x. x # xs) [] xs"
haftmann@37605
  2376
proof (induct xs)
haftmann@37605
  2377
  case Nil then show ?case by simp
haftmann@37605
  2378
next
haftmann@37605
  2379
  case Cons
haftmann@37605
  2380
  {
haftmann@37605
  2381
    fix x xs ys
haftmann@37605
  2382
    have "foldl (\<lambda>xs x. x # xs) ys xs @ [x]
haftmann@37605
  2383
      = foldl (\<lambda>xs x. x # xs) (ys @ [x]) xs"
haftmann@37605
  2384
    by (induct xs arbitrary: ys) auto
haftmann@37605
  2385
  }
haftmann@37605
  2386
  note aux = this
haftmann@37605
  2387
  show ?case by (induct xs) (auto simp add: Cons aux)
haftmann@37605
  2388
qed
haftmann@37605
  2389
nipkow@24449
  2390
haftmann@39774
  2391
text{* The ``Third Duality Theorem'' in Bird \& Wadler: *}
haftmann@39774
  2392
haftmann@39774
  2393
lemma foldr_foldl:
haftmann@39774
  2394
  "foldr f xs a = foldl (%x y. f y x) a (rev xs)"
haftmann@39774
  2395
  by (induct xs) auto
haftmann@39774
  2396
haftmann@39774
  2397
lemma foldl_foldr:
haftmann@39774
  2398
  "foldl f a xs = foldr (%x y. f y x) (rev xs) a"
haftmann@39774
  2399
  by (simp add: foldr_foldl [of "%x y. f y x" "rev xs"])
haftmann@39774
  2400
haftmann@39774
  2401
nipkow@23096
  2402
text{* The ``First Duality Theorem'' in Bird \& Wadler: *}
nipkow@23096
  2403
haftmann@39774
  2404
lemma (in monoid_add) foldl_foldr1_lemma:
haftmann@39774
  2405
  "foldl op + a xs = a + foldr op + xs 0"
haftmann@39774
  2406
  by (induct xs arbitrary: a) (auto simp: add_assoc)
haftmann@39774
  2407
haftmann@39774
  2408
corollary (in monoid_add) foldl_foldr1:
haftmann@39774
  2409
  "foldl op + 0 xs = foldr op + xs 0"
haftmann@39774
  2410
  by (simp add: foldl_foldr1_lemma)
haftmann@39774
  2411
haftmann@39774
  2412
lemma (in ab_semigroup_add) foldr_conv_foldl:
haftmann@39774
  2413
  "foldr op + xs a = foldl op + a xs"
haftmann@39774
  2414
  by (induct xs) (simp_all add: foldl_assoc add.commute)
chaieb@24471
  2415
wenzelm@13142
  2416
text {*
nipkow@13145
  2417
Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
nipkow@13145
  2418
difficult to use because it requires an additional transitivity step.
wenzelm@13142
  2419
*}
wenzelm@13142
  2420
nipkow@24526
  2421
lemma start_le_sum: "(m::nat) <= n ==> m <= foldl (op +) n ns"
nipkow@24526
  2422
by (induct ns arbitrary: n) auto
nipkow@24526
  2423
nipkow@24526
  2424
lemma elem_le_sum: "(n::nat) : set ns ==> n <= foldl (op +) 0 ns"
nipkow@13145
  2425
by (force intro: start_le_sum simp add: in_set_conv_decomp)
wenzelm@13142
  2426
wenzelm@13142
  2427
lemma sum_eq_0_conv [iff]:
nipkow@24526
  2428
  "(foldl (op +) (m::nat) ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
nipkow@24526
  2429
by (induct ns arbitrary: m) auto
wenzelm@13114
  2430
chaieb@24471
  2431
lemma foldr_invariant: 
chaieb@24471
  2432
  "\<lbrakk>Q x ; \<forall> x\<in> set xs. P x; \<forall> x y. P x \<and> Q y \<longrightarrow> Q (f x y) \<rbrakk> \<Longrightarrow> Q (foldr f xs x)"
chaieb@24471
  2433
  by (induct xs, simp_all)
chaieb@24471
  2434
chaieb@24471
  2435
lemma foldl_invariant: 
chaieb@24471
  2436
  "\<lbrakk>Q x ; \<forall> x\<in> set xs. P x; \<forall> x y. P x \<and> Q y \<longrightarrow> Q (f y x) \<rbrakk> \<Longrightarrow> Q (foldl f x xs)"
chaieb@24471
  2437
  by (induct xs arbitrary: x, simp_all)
chaieb@24471
  2438
haftmann@34978
  2439
lemma foldl_weak_invariant:
haftmann@34978
  2440
  assumes "P s"
haftmann@34978
  2441
    and "\<And>s x. x \<in> set xs \<Longrightarrow> P s \<Longrightarrow> P (f s x)"
haftmann@34978
  2442
  shows "P (foldl f s xs)"
haftmann@34978
  2443
  using assms by (induct xs arbitrary: s) simp_all
haftmann@34978
  2444
haftmann@31455
  2445
text {* @{const foldl} and @{const concat} *}
nipkow@24449
  2446
nipkow@24449
  2447
lemma foldl_conv_concat:
haftmann@29782
  2448
  "foldl (op @) xs xss = xs @ concat xss"
haftmann@29782
  2449
proof (induct xss arbitrary: xs)
haftmann@29782
  2450
  case Nil show ?case by simp
haftmann@29782
  2451
next
haftmann@35267
  2452
  interpret monoid_add "op @" "[]" proof qed simp_all
haftmann@29782
  2453
  case Cons then show ?case by (simp add: foldl_absorb0)
haftmann@29782
  2454
qed
haftmann@29782
  2455
haftmann@29782
  2456
lemma concat_conv_foldl: "concat xss = foldl (op @) [] xss"
haftmann@29782
  2457
  by (simp add: foldl_conv_concat)
haftmann@29782
  2458
haftmann@31455
  2459
text {* @{const Finite_Set.fold} and @{const foldl} *}
haftmann@31455
  2460
haftmann@35195
  2461
lemma (in fun_left_comm) fold_set_remdups:
haftmann@35195
  2462
  "fold f y (set xs) = foldl (\<lambda>y x. f x y) y (remdups xs)"
haftmann@35195
  2463
  by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_comm insert_absorb)
haftmann@35195
  2464
haftmann@31455
  2465
lemma (in fun_left_comm_idem) fold_set:
haftmann@31455
  2466
  "fold f y (set xs) = foldl (\<lambda>y x. f x y) y xs"
haftmann@31455
  2467
  by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_comm)
haftmann@31455
  2468
haftmann@32681
  2469
lemma (in ab_semigroup_idem_mult) fold1_set:
haftmann@32681
  2470
  assumes "xs \<noteq> []"
haftmann@32681
  2471
  shows "fold1 times (set xs) = foldl times (hd xs) (tl xs)"
haftmann@32681
  2472
proof -
haftmann@32681
  2473
  interpret fun_left_comm_idem times by (fact fun_left_comm_idem)
haftmann@32681
  2474
  from assms obtain y ys where xs: "xs = y # ys"
haftmann@32681
  2475
    by (cases xs) auto
haftmann@32681
  2476
  show ?thesis
haftmann@32681
  2477
  proof (cases "set ys = {}")
haftmann@32681
  2478
    case True with xs show ?thesis by simp
haftmann@32681
  2479
  next
haftmann@32681
  2480
    case False
haftmann@32681
  2481
    then have "fold1 times (insert y (set ys)) = fold times y (set ys)"
haftmann@32681
  2482
      by (simp only: finite_set fold1_eq_fold_idem)
haftmann@32681
  2483
    with xs show ?thesis by (simp add: fold_set mult_commute)
haftmann@32681
  2484
  qed
haftmann@32681
  2485
qed
haftmann@32681
  2486
haftmann@32681
  2487
lemma (in lattice) Inf_fin_set_fold [code_unfold]:
haftmann@32681
  2488
  "Inf_fin (set (x # xs)) = foldl inf x xs"
haftmann@32681
  2489
proof -
haftmann@32681
  2490
  interpret ab_semigroup_idem_mult "inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@32681
  2491
    by (fact ab_semigroup_idem_mult_inf)
haftmann@32681
  2492
  show ?thesis
haftmann@32681
  2493
    by (simp add: Inf_fin_def fold1_set del: set.simps)
haftmann@32681
  2494
qed
haftmann@32681
  2495
haftmann@32681
  2496
lemma (in lattice) Sup_fin_set_fold [code_unfold]:
haftmann@32681
  2497
  "Sup_fin (set (x # xs)) = foldl sup x xs"
haftmann@32681
  2498
proof -
haftmann@32681
  2499
  interpret ab_semigroup_idem_mult "sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@32681
  2500
    by (fact ab_semigroup_idem_mult_sup)
haftmann@32681
  2501
  show ?thesis
haftmann@32681
  2502
    by (simp add: Sup_fin_def fold1_set del: set.simps)
haftmann@32681
  2503
qed
haftmann@32681
  2504
haftmann@32681
  2505
lemma (in linorder) Min_fin_set_fold [code_unfold]:
haftmann@32681
  2506
  "Min (set (x # xs)) = foldl min x xs"
haftmann@32681
  2507
proof -
haftmann@32681
  2508
  interpret ab_semigroup_idem_mult "min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@32681
  2509
    by (fact ab_semigroup_idem_mult_min)
haftmann@32681
  2510
  show ?thesis
haftmann@32681
  2511
    by (simp add: Min_def fold1_set del: set.simps)
haftmann@32681
  2512
qed
haftmann@32681
  2513
haftmann@32681
  2514
lemma (in linorder) Max_fin_set_fold [code_unfold]:
haftmann@32681
  2515
  "Max (set (x # xs)) = foldl max x xs"
haftmann@32681
  2516
proof -
haftmann@32681
  2517
  interpret ab_semigroup_idem_mult "max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@32681
  2518
    by (fact ab_semigroup_idem_mult_max)
haftmann@32681
  2519
  show ?thesis
haftmann@32681
  2520
    by (simp add: Max_def fold1_set del: set.simps)
haftmann@32681
  2521
qed
haftmann@32681
  2522
haftmann@32681
  2523
lemma (in complete_lattice) Inf_set_fold [code_unfold]:
haftmann@32681
  2524
  "Inf (set xs) = foldl inf top xs"
haftmann@34007
  2525
proof -
haftmann@34007
  2526
  interpret fun_left_comm_idem "inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@34007
  2527
    by (fact fun_left_comm_idem_inf)
haftmann@34007
  2528
  show ?thesis by (simp add: Inf_fold_inf fold_set inf_commute)
haftmann@34007
  2529
qed
haftmann@32681
  2530
haftmann@32681
  2531
lemma (in complete_lattice) Sup_set_fold [code_unfold]:
haftmann@32681
  2532
  "Sup (set xs) = foldl sup bot xs"
haftmann@34007
  2533
proof -
haftmann@34007
  2534
  interpret fun_left_comm_idem "sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@34007
  2535
    by (fact fun_left_comm_idem_sup)
haftmann@34007
  2536
  show ?thesis by (simp add: Sup_fold_sup fold_set sup_commute)
haftmann@34007
  2537
qed
haftmann@34007
  2538
haftmann@34007
  2539
lemma (in complete_lattice) INFI_set_fold:
haftmann@34007
  2540
  "INFI (set xs) f = foldl (\<lambda>y x. inf (f x) y) top xs"
haftmann@34007
  2541
  unfolding INFI_def set_map [symmetric] Inf_set_fold foldl_map
haftmann@34007
  2542
    by (simp add: inf_commute)
haftmann@34007
  2543
haftmann@34007
  2544
lemma (in complete_lattice) SUPR_set_fold:
haftmann@34007
  2545
  "SUPR (set xs) f = foldl (\<lambda>y x. sup (f x) y) bot xs"
haftmann@34007
  2546
  unfolding SUPR_def set_map [symmetric] Sup_set_fold foldl_map
haftmann@34007
  2547
    by (simp add: sup_commute)
haftmann@31455
  2548
wenzelm@35115
  2549
nipkow@24645
  2550
subsubsection {* @{text upt} *}
wenzelm@13114
  2551
nipkow@17090
  2552
lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])"
nipkow@17090
  2553
-- {* simp does not terminate! *}
nipkow@13145
  2554
by (induct j) auto
wenzelm@13142
  2555
nipkow@32005
  2556
lemmas upt_rec_number_of[simp] = upt_rec[of "number_of m" "number_of n", standard]
nipkow@32005
  2557
nipkow@15425
  2558
lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []"
nipkow@13145
  2559
by (subst upt_rec) simp
wenzelm@13114
  2560
nipkow@15425
  2561
lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)"
nipkow@15281
  2562
by(induct j)simp_all
nipkow@15281
  2563
nipkow@15281
  2564
lemma upt_eq_Cons_conv:
nipkow@24526
  2565
 "([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)"
nipkow@24526
  2566
apply(induct j arbitrary: x xs)
nipkow@15281
  2567
 apply simp
nipkow@15281
  2568
apply(clarsimp simp add: append_eq_Cons_conv)
nipkow@15281
  2569
apply arith
nipkow@15281
  2570
done
nipkow@15281
  2571
nipkow@15425
  2572
lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]"
nipkow@13145
  2573
-- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
nipkow@13145
  2574
by simp
wenzelm@13114
  2575
nipkow@15425
  2576
lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]"
haftmann@26734
  2577
  by (simp add: upt_rec)
wenzelm@13114
  2578
nipkow@15425
  2579
lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]"
nipkow@13145
  2580
-- {* LOOPS as a simprule, since @{text "j <= j"}. *}
nipkow@13145
  2581
by (induct k) auto
wenzelm@13114
  2582
nipkow@15425
  2583
lemma length_upt [simp]: "length [i..<j] = j - i"
nipkow@13145
  2584
by (induct j) (auto simp add: Suc_diff_le)
wenzelm@13114
  2585
nipkow@15425
  2586
lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k"
nipkow@13145
  2587
apply (induct j)
nipkow@13145
  2588
apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
nipkow@13145
  2589
done
wenzelm@13114
  2590
nipkow@17906
  2591
nipkow@17906
  2592
lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i"
nipkow@17906
  2593
by(simp add:upt_conv_Cons)
nipkow@17906
  2594
nipkow@17906
  2595
lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1"
nipkow@17906
  2596
apply(cases j)
nipkow@17906
  2597
 apply simp
nipkow@17906
  2598
by(simp add:upt_Suc_append)
nipkow@17906
  2599
nipkow@24526
  2600
lemma take_upt [simp]: "i+m <= n ==> take m [i..<n] = [i..<i+m]"
nipkow@24526
  2601
apply (induct m arbitrary: i, simp)
nipkow@13145
  2602
apply (subst upt_rec)
nipkow@13145
  2603
apply (rule sym)
nipkow@13145
  2604
apply (subst upt_rec)
nipkow@13145
  2605
apply (simp del: upt.simps)
nipkow@13145
  2606
done
nipkow@3507
  2607
nipkow@17501
  2608
lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]"
nipkow@17501
  2609
apply(induct j)
nipkow@17501
  2610
apply auto
nipkow@17501
  2611
done
nipkow@17501
  2612
nipkow@24645
  2613
lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]"
nipkow@13145
  2614
by (induct n) auto
wenzelm@13114
  2615
nipkow@24526
  2616
lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)"
nipkow@24526
  2617
apply (induct n m  arbitrary: i rule: diff_induct)
nipkow@13145
  2618
prefer 3 apply (subst map_Suc_upt[symmetric])
nipkow@13145
  2619
apply (auto simp add: less_diff_conv nth_upt)
nipkow@13145
  2620
done
wenzelm@13114
  2621
berghofe@13883
  2622
lemma nth_take_lemma:
nipkow@24526
  2623
  "k <= length xs ==> k <= length ys ==>
berghofe@13883
  2624
     (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"
nipkow@24526
  2625
apply (atomize, induct k arbitrary: xs ys)
paulson@14208
  2626
apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)
nipkow@13145
  2627
txt {* Both lists must be non-empty *}
paulson@14208
  2628
apply (case_tac xs, simp)
paulson@14208
  2629
apply (case_tac ys, clarify)
nipkow@13145
  2630
 apply (simp (no_asm_use))
nipkow@13145
  2631
apply clarify
nipkow@13145
  2632
txt {* prenexing's needed, not miniscoping *}
nipkow@13145
  2633
apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
nipkow@13145
  2634
apply blast
nipkow@13145
  2635
done
wenzelm@13114
  2636
wenzelm@13114
  2637
lemma nth_equalityI:
wenzelm@13114
  2638
 "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
nipkow@13145
  2639
apply (frule nth_take_lemma [OF le_refl eq_imp_le])
nipkow@13145
  2640
apply (simp_all add: take_all)
nipkow@13145
  2641
done
wenzelm@13142
  2642
haftmann@24796
  2643
lemma map_nth:
haftmann@24796
  2644
  "map (\<lambda>i. xs ! i) [0..<length xs] = xs"
haftmann@24796
  2645
  by (rule nth_equalityI, auto)
haftmann@24796
  2646
kleing@13863
  2647
(* needs nth_equalityI *)
kleing@13863
  2648
lemma list_all2_antisym:
kleing@13863
  2649
  "\<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> 
kleing@13863
  2650
  \<Longrightarrow> xs = ys"
kleing@13863
  2651
  apply (simp add: list_all2_conv_all_nth) 
paulson@14208
  2652
  apply (rule nth_equalityI, blast, simp)
kleing@13863
  2653
  done
kleing@13863
  2654
wenzelm@13142
  2655
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
nipkow@13145
  2656
-- {* The famous take-lemma. *}
nipkow@13145
  2657
apply (drule_tac x = "max (length xs) (length ys)" in spec)
nipkow@13145
  2658
apply (simp add: le_max_iff_disj take_all)
nipkow@13145
  2659
done
wenzelm@13142
  2660
wenzelm@13142
  2661
nipkow@15302
  2662
lemma take_Cons':
nipkow@15302
  2663
     "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
nipkow@15302
  2664
by (cases n) simp_all
nipkow@15302
  2665
nipkow@15302
  2666
lemma drop_Cons':
nipkow@15302
  2667
     "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
nipkow@15302
  2668
by (cases n) simp_all
nipkow@15302
  2669
nipkow@15302
  2670
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"