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