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