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