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