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