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