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