src/HOL/List.thy
author wenzelm
Fri Dec 07 16:25:33 2012 +0100 (2012-12-07)
changeset 50422 ee729dbd1b7f
parent 50134 13211e07d931
child 50548 0aec55e63795
permissions -rw-r--r--
avoid ML_file in large theory files to improve performance of dependency discovery of main HOL (approx. 1s CPU time) -- relevant for any application using it, e.g. small paper sessions;
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@50422
   507
ML {*
wenzelm@50422
   508
(* Simproc for rewriting list comprehensions applied to List.set to set
wenzelm@50422
   509
   comprehension. *)
wenzelm@50422
   510
wenzelm@50422
   511
signature LIST_TO_SET_COMPREHENSION =
wenzelm@50422
   512
sig
wenzelm@50422
   513
  val simproc : simpset -> cterm -> thm option
wenzelm@50422
   514
end
wenzelm@50422
   515
wenzelm@50422
   516
structure List_to_Set_Comprehension : LIST_TO_SET_COMPREHENSION =
wenzelm@50422
   517
struct
wenzelm@50422
   518
wenzelm@50422
   519
(* conversion *)
wenzelm@50422
   520
wenzelm@50422
   521
fun all_exists_conv cv ctxt ct =
wenzelm@50422
   522
  (case Thm.term_of ct of
wenzelm@50422
   523
    Const (@{const_name HOL.Ex}, _) $ Abs _ =>
wenzelm@50422
   524
      Conv.arg_conv (Conv.abs_conv (all_exists_conv cv o #2) ctxt) ct
wenzelm@50422
   525
  | _ => cv ctxt ct)
wenzelm@50422
   526
wenzelm@50422
   527
fun all_but_last_exists_conv cv ctxt ct =
wenzelm@50422
   528
  (case Thm.term_of ct of
wenzelm@50422
   529
    Const (@{const_name HOL.Ex}, _) $ Abs (_, _, Const (@{const_name HOL.Ex}, _) $ _) =>
wenzelm@50422
   530
      Conv.arg_conv (Conv.abs_conv (all_but_last_exists_conv cv o #2) ctxt) ct
wenzelm@50422
   531
  | _ => cv ctxt ct)
wenzelm@50422
   532
wenzelm@50422
   533
fun Collect_conv cv ctxt ct =
wenzelm@50422
   534
  (case Thm.term_of ct of
wenzelm@50422
   535
    Const (@{const_name Set.Collect}, _) $ Abs _ => Conv.arg_conv (Conv.abs_conv cv ctxt) ct
wenzelm@50422
   536
  | _ => raise CTERM ("Collect_conv", [ct]))
wenzelm@50422
   537
wenzelm@50422
   538
fun Trueprop_conv cv ct =
wenzelm@50422
   539
  (case Thm.term_of ct of
wenzelm@50422
   540
    Const (@{const_name Trueprop}, _) $ _ => Conv.arg_conv cv ct
wenzelm@50422
   541
  | _ => raise CTERM ("Trueprop_conv", [ct]))
wenzelm@50422
   542
wenzelm@50422
   543
fun eq_conv cv1 cv2 ct =
wenzelm@50422
   544
  (case Thm.term_of ct of
wenzelm@50422
   545
    Const (@{const_name HOL.eq}, _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv1) cv2 ct
wenzelm@50422
   546
  | _ => raise CTERM ("eq_conv", [ct]))
wenzelm@50422
   547
wenzelm@50422
   548
fun conj_conv cv1 cv2 ct =
wenzelm@50422
   549
  (case Thm.term_of ct of
wenzelm@50422
   550
    Const (@{const_name HOL.conj}, _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv1) cv2 ct
wenzelm@50422
   551
  | _ => raise CTERM ("conj_conv", [ct]))
wenzelm@50422
   552
wenzelm@50422
   553
fun rewr_conv' th = Conv.rewr_conv (mk_meta_eq th)
wenzelm@50422
   554
wenzelm@50422
   555
fun conjunct_assoc_conv ct =
wenzelm@50422
   556
  Conv.try_conv
wenzelm@50422
   557
    (rewr_conv' @{thm conj_assoc} then_conv conj_conv Conv.all_conv conjunct_assoc_conv) ct
wenzelm@50422
   558
wenzelm@50422
   559
fun right_hand_set_comprehension_conv conv ctxt =
wenzelm@50422
   560
  Trueprop_conv (eq_conv Conv.all_conv
wenzelm@50422
   561
    (Collect_conv (all_exists_conv conv o #2) ctxt))
wenzelm@50422
   562
wenzelm@50422
   563
wenzelm@50422
   564
(* term abstraction of list comprehension patterns *)
wenzelm@50422
   565
wenzelm@50422
   566
datatype termlets = If | Case of (typ * int)
wenzelm@50422
   567
wenzelm@50422
   568
fun simproc ss redex =
wenzelm@50422
   569
  let
wenzelm@50422
   570
    val ctxt = Simplifier.the_context ss
wenzelm@50422
   571
    val thy = Proof_Context.theory_of ctxt
wenzelm@50422
   572
    val set_Nil_I = @{thm trans} OF [@{thm set.simps(1)}, @{thm empty_def}]
wenzelm@50422
   573
    val set_singleton = @{lemma "set [a] = {x. x = a}" by simp}
wenzelm@50422
   574
    val inst_Collect_mem_eq = @{lemma "set A = {x. x : set A}" by simp}
wenzelm@50422
   575
    val del_refl_eq = @{lemma "(t = t & P) == P" by simp}
wenzelm@50422
   576
    fun mk_set T = Const (@{const_name List.set}, HOLogic.listT T --> HOLogic.mk_setT T)
wenzelm@50422
   577
    fun dest_set (Const (@{const_name List.set}, _) $ xs) = xs
wenzelm@50422
   578
    fun dest_singleton_list (Const (@{const_name List.Cons}, _)
wenzelm@50422
   579
          $ t $ (Const (@{const_name List.Nil}, _))) = t
wenzelm@50422
   580
      | dest_singleton_list t = raise TERM ("dest_singleton_list", [t])
wenzelm@50422
   581
    (* We check that one case returns a singleton list and all other cases
wenzelm@50422
   582
       return [], and return the index of the one singleton list case *)
wenzelm@50422
   583
    fun possible_index_of_singleton_case cases =
wenzelm@50422
   584
      let
wenzelm@50422
   585
        fun check (i, case_t) s =
wenzelm@50422
   586
          (case strip_abs_body case_t of
wenzelm@50422
   587
            (Const (@{const_name List.Nil}, _)) => s
wenzelm@50422
   588
          | _ => (case s of NONE => SOME i | SOME _ => NONE))
wenzelm@50422
   589
      in
wenzelm@50422
   590
        fold_index check cases NONE
wenzelm@50422
   591
      end
wenzelm@50422
   592
    (* returns (case_expr type index chosen_case) option  *)
wenzelm@50422
   593
    fun dest_case case_term =
wenzelm@50422
   594
      let
wenzelm@50422
   595
        val (case_const, args) = strip_comb case_term
wenzelm@50422
   596
      in
wenzelm@50422
   597
        (case try dest_Const case_const of
wenzelm@50422
   598
          SOME (c, T) =>
wenzelm@50422
   599
            (case Datatype.info_of_case thy c of
wenzelm@50422
   600
              SOME _ =>
wenzelm@50422
   601
                (case possible_index_of_singleton_case (fst (split_last args)) of
wenzelm@50422
   602
                  SOME i =>
wenzelm@50422
   603
                    let
wenzelm@50422
   604
                      val (Ts, _) = strip_type T
wenzelm@50422
   605
                      val T' = List.last Ts
wenzelm@50422
   606
                    in SOME (List.last args, T', i, nth args i) end
wenzelm@50422
   607
                | NONE => NONE)
wenzelm@50422
   608
            | NONE => NONE)
wenzelm@50422
   609
        | NONE => NONE)
wenzelm@50422
   610
      end
wenzelm@50422
   611
    (* returns condition continuing term option *)
wenzelm@50422
   612
    fun dest_if (Const (@{const_name If}, _) $ cond $ then_t $ Const (@{const_name Nil}, _)) =
wenzelm@50422
   613
          SOME (cond, then_t)
wenzelm@50422
   614
      | dest_if _ = NONE
wenzelm@50422
   615
    fun tac _ [] = rtac set_singleton 1 ORELSE rtac inst_Collect_mem_eq 1
wenzelm@50422
   616
      | tac ctxt (If :: cont) =
wenzelm@50422
   617
          Splitter.split_tac [@{thm split_if}] 1
wenzelm@50422
   618
          THEN rtac @{thm conjI} 1
wenzelm@50422
   619
          THEN rtac @{thm impI} 1
wenzelm@50422
   620
          THEN Subgoal.FOCUS (fn {prems, context, ...} =>
wenzelm@50422
   621
            CONVERSION (right_hand_set_comprehension_conv (K
wenzelm@50422
   622
              (conj_conv (Conv.rewr_conv (List.last prems RS @{thm Eq_TrueI})) Conv.all_conv
wenzelm@50422
   623
               then_conv
wenzelm@50422
   624
               rewr_conv' @{lemma "(True & P) = P" by simp})) context) 1) ctxt 1
wenzelm@50422
   625
          THEN tac ctxt cont
wenzelm@50422
   626
          THEN rtac @{thm impI} 1
wenzelm@50422
   627
          THEN Subgoal.FOCUS (fn {prems, context, ...} =>
wenzelm@50422
   628
              CONVERSION (right_hand_set_comprehension_conv (K
wenzelm@50422
   629
                (conj_conv (Conv.rewr_conv (List.last prems RS @{thm Eq_FalseI})) Conv.all_conv
wenzelm@50422
   630
                 then_conv rewr_conv' @{lemma "(False & P) = False" by simp})) context) 1) ctxt 1
wenzelm@50422
   631
          THEN rtac set_Nil_I 1
wenzelm@50422
   632
      | tac ctxt (Case (T, i) :: cont) =
wenzelm@50422
   633
          let
wenzelm@50422
   634
            val info = Datatype.the_info thy (fst (dest_Type T))
wenzelm@50422
   635
          in
wenzelm@50422
   636
            (* do case distinction *)
wenzelm@50422
   637
            Splitter.split_tac [#split info] 1
wenzelm@50422
   638
            THEN EVERY (map_index (fn (i', _) =>
wenzelm@50422
   639
              (if i' < length (#case_rewrites info) - 1 then rtac @{thm conjI} 1 else all_tac)
wenzelm@50422
   640
              THEN REPEAT_DETERM (rtac @{thm allI} 1)
wenzelm@50422
   641
              THEN rtac @{thm impI} 1
wenzelm@50422
   642
              THEN (if i' = i then
wenzelm@50422
   643
                (* continue recursively *)
wenzelm@50422
   644
                Subgoal.FOCUS (fn {prems, context, ...} =>
wenzelm@50422
   645
                  CONVERSION (Thm.eta_conversion then_conv right_hand_set_comprehension_conv (K
wenzelm@50422
   646
                      ((conj_conv
wenzelm@50422
   647
                        (eq_conv Conv.all_conv (rewr_conv' (List.last prems)) then_conv
wenzelm@50422
   648
                          (Conv.try_conv (Conv.rewrs_conv (map mk_meta_eq (#inject info)))))
wenzelm@50422
   649
                        Conv.all_conv)
wenzelm@50422
   650
                        then_conv (Conv.try_conv (Conv.rewr_conv del_refl_eq))
wenzelm@50422
   651
                        then_conv conjunct_assoc_conv)) context
wenzelm@50422
   652
                    then_conv (Trueprop_conv (eq_conv Conv.all_conv (Collect_conv (fn (_, ctxt) =>
wenzelm@50422
   653
                      Conv.repeat_conv
wenzelm@50422
   654
                        (all_but_last_exists_conv
wenzelm@50422
   655
                          (K (rewr_conv'
wenzelm@50422
   656
                            @{lemma "(EX x. x = t & P x) = P t" by simp})) ctxt)) context)))) 1) ctxt 1
wenzelm@50422
   657
                THEN tac ctxt cont
wenzelm@50422
   658
              else
wenzelm@50422
   659
                Subgoal.FOCUS (fn {prems, context, ...} =>
wenzelm@50422
   660
                  CONVERSION
wenzelm@50422
   661
                    (right_hand_set_comprehension_conv (K
wenzelm@50422
   662
                      (conj_conv
wenzelm@50422
   663
                        ((eq_conv Conv.all_conv
wenzelm@50422
   664
                          (rewr_conv' (List.last prems))) then_conv
wenzelm@50422
   665
                          (Conv.rewrs_conv (map (fn th => th RS @{thm Eq_FalseI}) (#distinct info))))
wenzelm@50422
   666
                        Conv.all_conv then_conv
wenzelm@50422
   667
                        (rewr_conv' @{lemma "(False & P) = False" by simp}))) context then_conv
wenzelm@50422
   668
                      Trueprop_conv
wenzelm@50422
   669
                        (eq_conv Conv.all_conv
wenzelm@50422
   670
                          (Collect_conv (fn (_, ctxt) =>
wenzelm@50422
   671
                            Conv.repeat_conv
wenzelm@50422
   672
                              (Conv.bottom_conv
wenzelm@50422
   673
                                (K (rewr_conv'
wenzelm@50422
   674
                                  @{lemma "(EX x. P) = P" by simp})) ctxt)) context))) 1) ctxt 1
wenzelm@50422
   675
                THEN rtac set_Nil_I 1)) (#case_rewrites info))
wenzelm@50422
   676
          end
wenzelm@50422
   677
    fun make_inner_eqs bound_vs Tis eqs t =
wenzelm@50422
   678
      (case dest_case t of
wenzelm@50422
   679
        SOME (x, T, i, cont) =>
wenzelm@50422
   680
          let
wenzelm@50422
   681
            val (vs, body) = strip_abs (Pattern.eta_long (map snd bound_vs) cont)
wenzelm@50422
   682
            val x' = incr_boundvars (length vs) x
wenzelm@50422
   683
            val eqs' = map (incr_boundvars (length vs)) eqs
wenzelm@50422
   684
            val (constr_name, _) = nth (the (Datatype.get_constrs thy (fst (dest_Type T)))) i
wenzelm@50422
   685
            val constr_t =
wenzelm@50422
   686
              list_comb
wenzelm@50422
   687
                (Const (constr_name, map snd vs ---> T), map Bound (((length vs) - 1) downto 0))
wenzelm@50422
   688
            val constr_eq = Const (@{const_name HOL.eq}, T --> T --> @{typ bool}) $ constr_t $ x'
wenzelm@50422
   689
          in
wenzelm@50422
   690
            make_inner_eqs (rev vs @ bound_vs) (Case (T, i) :: Tis) (constr_eq :: eqs') body
wenzelm@50422
   691
          end
wenzelm@50422
   692
      | NONE =>
wenzelm@50422
   693
          (case dest_if t of
wenzelm@50422
   694
            SOME (condition, cont) => make_inner_eqs bound_vs (If :: Tis) (condition :: eqs) cont
wenzelm@50422
   695
          | NONE =>
wenzelm@50422
   696
            if eqs = [] then NONE (* no rewriting, nothing to be done *)
wenzelm@50422
   697
            else
wenzelm@50422
   698
              let
wenzelm@50422
   699
                val Type (@{type_name List.list}, [rT]) = fastype_of1 (map snd bound_vs, t)
wenzelm@50422
   700
                val pat_eq =
wenzelm@50422
   701
                  (case try dest_singleton_list t of
wenzelm@50422
   702
                    SOME t' =>
wenzelm@50422
   703
                      Const (@{const_name HOL.eq}, rT --> rT --> @{typ bool}) $
wenzelm@50422
   704
                        Bound (length bound_vs) $ t'
wenzelm@50422
   705
                  | NONE =>
wenzelm@50422
   706
                      Const (@{const_name Set.member}, rT --> HOLogic.mk_setT rT --> @{typ bool}) $
wenzelm@50422
   707
                        Bound (length bound_vs) $ (mk_set rT $ t))
wenzelm@50422
   708
                val reverse_bounds = curry subst_bounds
wenzelm@50422
   709
                  ((map Bound ((length bound_vs - 1) downto 0)) @ [Bound (length bound_vs)])
wenzelm@50422
   710
                val eqs' = map reverse_bounds eqs
wenzelm@50422
   711
                val pat_eq' = reverse_bounds pat_eq
wenzelm@50422
   712
                val inner_t =
wenzelm@50422
   713
                  fold (fn (_, T) => fn t => HOLogic.exists_const T $ absdummy T t)
wenzelm@50422
   714
                    (rev bound_vs) (fold (curry HOLogic.mk_conj) eqs' pat_eq')
wenzelm@50422
   715
                val lhs = term_of redex
wenzelm@50422
   716
                val rhs = HOLogic.mk_Collect ("x", rT, inner_t)
wenzelm@50422
   717
                val rewrite_rule_t = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
wenzelm@50422
   718
              in
wenzelm@50422
   719
                SOME
wenzelm@50422
   720
                  ((Goal.prove ctxt [] [] rewrite_rule_t
wenzelm@50422
   721
                    (fn {context, ...} => tac context (rev Tis))) RS @{thm eq_reflection})
wenzelm@50422
   722
              end))
wenzelm@50422
   723
  in
wenzelm@50422
   724
    make_inner_eqs [] [] [] (dest_set (term_of redex))
wenzelm@50422
   725
  end
wenzelm@50422
   726
wenzelm@50422
   727
end
wenzelm@50422
   728
*}
bulwahn@41463
   729
bulwahn@41463
   730
simproc_setup list_to_set_comprehension ("set xs") = {* K List_to_Set_Comprehension.simproc *}
bulwahn@41463
   731
haftmann@46133
   732
code_datatype set coset
haftmann@46133
   733
haftmann@46133
   734
hide_const (open) coset
wenzelm@35115
   735
haftmann@49948
   736
haftmann@21061
   737
subsubsection {* @{const Nil} and @{const Cons} *}
haftmann@21061
   738
haftmann@21061
   739
lemma not_Cons_self [simp]:
haftmann@21061
   740
  "xs \<noteq> x # xs"
nipkow@13145
   741
by (induct xs) auto
wenzelm@13114
   742
wenzelm@41697
   743
lemma not_Cons_self2 [simp]:
wenzelm@41697
   744
  "x # xs \<noteq> xs"
wenzelm@41697
   745
by (rule not_Cons_self [symmetric])
wenzelm@13114
   746
wenzelm@13142
   747
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
nipkow@13145
   748
by (induct xs) auto
wenzelm@13114
   749
wenzelm@13142
   750
lemma length_induct:
haftmann@21061
   751
  "(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs"
nipkow@17589
   752
by (rule measure_induct [of length]) iprover
wenzelm@13114
   753
haftmann@37289
   754
lemma list_nonempty_induct [consumes 1, case_names single cons]:
haftmann@37289
   755
  assumes "xs \<noteq> []"
haftmann@37289
   756
  assumes single: "\<And>x. P [x]"
haftmann@37289
   757
  assumes cons: "\<And>x xs. xs \<noteq> [] \<Longrightarrow> P xs \<Longrightarrow> P (x # xs)"
haftmann@37289
   758
  shows "P xs"
haftmann@37289
   759
using `xs \<noteq> []` proof (induct xs)
haftmann@37289
   760
  case Nil then show ?case by simp
haftmann@37289
   761
next
haftmann@37289
   762
  case (Cons x xs) show ?case proof (cases xs)
haftmann@37289
   763
    case Nil with single show ?thesis by simp
haftmann@37289
   764
  next
haftmann@37289
   765
    case Cons then have "xs \<noteq> []" by simp
haftmann@37289
   766
    moreover with Cons.hyps have "P xs" .
haftmann@37289
   767
    ultimately show ?thesis by (rule cons)
haftmann@37289
   768
  qed
haftmann@37289
   769
qed
haftmann@37289
   770
hoelzl@45714
   771
lemma inj_split_Cons: "inj_on (\<lambda>(xs, n). n#xs) X"
hoelzl@45714
   772
  by (auto intro!: inj_onI)
wenzelm@13114
   773
haftmann@49948
   774
haftmann@21061
   775
subsubsection {* @{const length} *}
wenzelm@13114
   776
wenzelm@13142
   777
text {*
haftmann@21061
   778
  Needs to come before @{text "@"} because of theorem @{text
haftmann@21061
   779
  append_eq_append_conv}.
wenzelm@13142
   780
*}
wenzelm@13114
   781
wenzelm@13142
   782
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
nipkow@13145
   783
by (induct xs) auto
wenzelm@13114
   784
wenzelm@13142
   785
lemma length_map [simp]: "length (map f xs) = length xs"
nipkow@13145
   786
by (induct xs) auto
wenzelm@13114
   787
wenzelm@13142
   788
lemma length_rev [simp]: "length (rev xs) = length xs"
nipkow@13145
   789
by (induct xs) auto
wenzelm@13114
   790
wenzelm@13142
   791
lemma length_tl [simp]: "length (tl xs) = length xs - 1"
nipkow@13145
   792
by (cases xs) auto
wenzelm@13114
   793
wenzelm@13142
   794
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
nipkow@13145
   795
by (induct xs) auto
wenzelm@13114
   796
wenzelm@13142
   797
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
nipkow@13145
   798
by (induct xs) auto
wenzelm@13114
   799
nipkow@23479
   800
lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0"
nipkow@23479
   801
by auto
nipkow@23479
   802
wenzelm@13114
   803
lemma length_Suc_conv:
nipkow@13145
   804
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
nipkow@13145
   805
by (induct xs) auto
wenzelm@13142
   806
nipkow@14025
   807
lemma Suc_length_conv:
nipkow@14025
   808
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
paulson@14208
   809
apply (induct xs, simp, simp)
nipkow@14025
   810
apply blast
nipkow@14025
   811
done
nipkow@14025
   812
wenzelm@25221
   813
lemma impossible_Cons: "length xs <= length ys ==> xs = x # ys = False"
wenzelm@25221
   814
  by (induct xs) auto
wenzelm@25221
   815
haftmann@26442
   816
lemma list_induct2 [consumes 1, case_names Nil Cons]:
haftmann@26442
   817
  "length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow>
haftmann@26442
   818
   (\<And>x xs y ys. length xs = length ys \<Longrightarrow> P xs ys \<Longrightarrow> P (x#xs) (y#ys))
haftmann@26442
   819
   \<Longrightarrow> P xs ys"
haftmann@26442
   820
proof (induct xs arbitrary: ys)
haftmann@26442
   821
  case Nil then show ?case by simp
haftmann@26442
   822
next
haftmann@26442
   823
  case (Cons x xs ys) then show ?case by (cases ys) simp_all
haftmann@26442
   824
qed
haftmann@26442
   825
haftmann@26442
   826
lemma list_induct3 [consumes 2, case_names Nil Cons]:
haftmann@26442
   827
  "length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P [] [] [] \<Longrightarrow>
haftmann@26442
   828
   (\<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
   829
   \<Longrightarrow> P xs ys zs"
haftmann@26442
   830
proof (induct xs arbitrary: ys zs)
haftmann@26442
   831
  case Nil then show ?case by simp
haftmann@26442
   832
next
haftmann@26442
   833
  case (Cons x xs ys zs) then show ?case by (cases ys, simp_all)
haftmann@26442
   834
    (cases zs, simp_all)
haftmann@26442
   835
qed
wenzelm@13114
   836
kaliszyk@36154
   837
lemma list_induct4 [consumes 3, case_names Nil Cons]:
kaliszyk@36154
   838
  "length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> length zs = length ws \<Longrightarrow>
kaliszyk@36154
   839
   P [] [] [] [] \<Longrightarrow> (\<And>x xs y ys z zs w ws. length xs = length ys \<Longrightarrow>
kaliszyk@36154
   840
   length ys = length zs \<Longrightarrow> length zs = length ws \<Longrightarrow> P xs ys zs ws \<Longrightarrow>
kaliszyk@36154
   841
   P (x#xs) (y#ys) (z#zs) (w#ws)) \<Longrightarrow> P xs ys zs ws"
kaliszyk@36154
   842
proof (induct xs arbitrary: ys zs ws)
kaliszyk@36154
   843
  case Nil then show ?case by simp
kaliszyk@36154
   844
next
kaliszyk@36154
   845
  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
   846
qed
kaliszyk@36154
   847
krauss@22493
   848
lemma list_induct2': 
krauss@22493
   849
  "\<lbrakk> P [] [];
krauss@22493
   850
  \<And>x xs. P (x#xs) [];
krauss@22493
   851
  \<And>y ys. P [] (y#ys);
krauss@22493
   852
   \<And>x xs y ys. P xs ys  \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk>
krauss@22493
   853
 \<Longrightarrow> P xs ys"
krauss@22493
   854
by (induct xs arbitrary: ys) (case_tac x, auto)+
krauss@22493
   855
nipkow@22143
   856
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False"
nipkow@24349
   857
by (rule Eq_FalseI) auto
wenzelm@24037
   858
wenzelm@24037
   859
simproc_setup list_neq ("(xs::'a list) = ys") = {*
nipkow@22143
   860
(*
nipkow@22143
   861
Reduces xs=ys to False if xs and ys cannot be of the same length.
nipkow@22143
   862
This is the case if the atomic sublists of one are a submultiset
nipkow@22143
   863
of those of the other list and there are fewer Cons's in one than the other.
nipkow@22143
   864
*)
wenzelm@24037
   865
wenzelm@24037
   866
let
nipkow@22143
   867
huffman@29856
   868
fun len (Const(@{const_name Nil},_)) acc = acc
huffman@29856
   869
  | len (Const(@{const_name Cons},_) $ _ $ xs) (ts,n) = len xs (ts,n+1)
huffman@29856
   870
  | len (Const(@{const_name append},_) $ xs $ ys) acc = len xs (len ys acc)
huffman@29856
   871
  | len (Const(@{const_name rev},_) $ xs) acc = len xs acc
huffman@29856
   872
  | len (Const(@{const_name map},_) $ _ $ xs) acc = len xs acc
nipkow@22143
   873
  | len t (ts,n) = (t::ts,n);
nipkow@22143
   874
wenzelm@24037
   875
fun list_neq _ ss ct =
nipkow@22143
   876
  let
wenzelm@24037
   877
    val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct;
nipkow@22143
   878
    val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0);
nipkow@22143
   879
    fun prove_neq() =
nipkow@22143
   880
      let
nipkow@22143
   881
        val Type(_,listT::_) = eqT;
haftmann@22994
   882
        val size = HOLogic.size_const listT;
nipkow@22143
   883
        val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs);
nipkow@22143
   884
        val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len);
nipkow@22143
   885
        val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len
haftmann@22633
   886
          (K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1));
haftmann@22633
   887
      in SOME (thm RS @{thm neq_if_length_neq}) end
nipkow@22143
   888
  in
wenzelm@23214
   889
    if m < n andalso submultiset (op aconv) (ls,rs) orelse
wenzelm@23214
   890
       n < m andalso submultiset (op aconv) (rs,ls)
nipkow@22143
   891
    then prove_neq() else NONE
nipkow@22143
   892
  end;
wenzelm@24037
   893
in list_neq end;
nipkow@22143
   894
*}
nipkow@22143
   895
nipkow@22143
   896
nipkow@15392
   897
subsubsection {* @{text "@"} -- append *}
wenzelm@13114
   898
wenzelm@13142
   899
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
nipkow@13145
   900
by (induct xs) auto
wenzelm@13114
   901
wenzelm@13142
   902
lemma append_Nil2 [simp]: "xs @ [] = xs"
nipkow@13145
   903
by (induct xs) auto
nipkow@3507
   904
wenzelm@13142
   905
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
nipkow@13145
   906
by (induct xs) auto
wenzelm@13114
   907
wenzelm@13142
   908
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
nipkow@13145
   909
by (induct xs) auto
wenzelm@13114
   910
wenzelm@13142
   911
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
nipkow@13145
   912
by (induct xs) auto
wenzelm@13114
   913
wenzelm@13142
   914
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
nipkow@13145
   915
by (induct xs) auto
wenzelm@13114
   916
blanchet@35828
   917
lemma append_eq_append_conv [simp, no_atp]:
nipkow@24526
   918
 "length xs = length ys \<or> length us = length vs
berghofe@13883
   919
 ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
nipkow@24526
   920
apply (induct xs arbitrary: ys)
paulson@14208
   921
 apply (case_tac ys, simp, force)
paulson@14208
   922
apply (case_tac ys, force, simp)
nipkow@13145
   923
done
wenzelm@13142
   924
nipkow@24526
   925
lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) =
nipkow@24526
   926
  (EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)"
nipkow@24526
   927
apply (induct xs arbitrary: ys zs ts)
nipkow@44890
   928
 apply fastforce
nipkow@14495
   929
apply(case_tac zs)
nipkow@14495
   930
 apply simp
nipkow@44890
   931
apply fastforce
nipkow@14495
   932
done
nipkow@14495
   933
berghofe@34910
   934
lemma same_append_eq [iff, induct_simp]: "(xs @ ys = xs @ zs) = (ys = zs)"
nipkow@13145
   935
by simp
wenzelm@13142
   936
wenzelm@13142
   937
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
nipkow@13145
   938
by simp
wenzelm@13114
   939
berghofe@34910
   940
lemma append_same_eq [iff, induct_simp]: "(ys @ xs = zs @ xs) = (ys = zs)"
nipkow@13145
   941
by simp
wenzelm@13114
   942
wenzelm@13142
   943
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
nipkow@13145
   944
using append_same_eq [of _ _ "[]"] by auto
nipkow@3507
   945
wenzelm@13142
   946
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
nipkow@13145
   947
using append_same_eq [of "[]"] by auto
wenzelm@13114
   948
blanchet@35828
   949
lemma hd_Cons_tl [simp,no_atp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
nipkow@13145
   950
by (induct xs) auto
wenzelm@13114
   951
wenzelm@13142
   952
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
nipkow@13145
   953
by (induct xs) auto
wenzelm@13114
   954
wenzelm@13142
   955
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
nipkow@13145
   956
by (simp add: hd_append split: list.split)
wenzelm@13114
   957
wenzelm@13142
   958
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
nipkow@13145
   959
by (simp split: list.split)
wenzelm@13114
   960
wenzelm@13142
   961
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
nipkow@13145
   962
by (simp add: tl_append split: list.split)
wenzelm@13114
   963
wenzelm@13114
   964
nipkow@14300
   965
lemma Cons_eq_append_conv: "x#xs = ys@zs =
nipkow@14300
   966
 (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))"
nipkow@14300
   967
by(cases ys) auto
nipkow@14300
   968
nipkow@15281
   969
lemma append_eq_Cons_conv: "(ys@zs = x#xs) =
nipkow@15281
   970
 (ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))"
nipkow@15281
   971
by(cases ys) auto
nipkow@15281
   972
nipkow@14300
   973
wenzelm@13142
   974
text {* Trivial rules for solving @{text "@"}-equations automatically. *}
wenzelm@13114
   975
wenzelm@13114
   976
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
nipkow@13145
   977
by simp
wenzelm@13114
   978
wenzelm@13142
   979
lemma Cons_eq_appendI:
nipkow@13145
   980
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
nipkow@13145
   981
by (drule sym) simp
wenzelm@13114
   982
wenzelm@13142
   983
lemma append_eq_appendI:
nipkow@13145
   984
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
nipkow@13145
   985
by (drule sym) simp
wenzelm@13114
   986
wenzelm@13114
   987
wenzelm@13142
   988
text {*
nipkow@13145
   989
Simplification procedure for all list equalities.
nipkow@13145
   990
Currently only tries to rearrange @{text "@"} to see if
nipkow@13145
   991
- both lists end in a singleton list,
nipkow@13145
   992
- or both lists end in the same list.
wenzelm@13142
   993
*}
wenzelm@13142
   994
wenzelm@43594
   995
simproc_setup list_eq ("(xs::'a list) = ys")  = {*
wenzelm@13462
   996
  let
wenzelm@43594
   997
    fun last (cons as Const (@{const_name Cons}, _) $ _ $ xs) =
wenzelm@43594
   998
          (case xs of Const (@{const_name Nil}, _) => cons | _ => last xs)
wenzelm@43594
   999
      | last (Const(@{const_name append},_) $ _ $ ys) = last ys
wenzelm@43594
  1000
      | last t = t;
wenzelm@43594
  1001
    
wenzelm@43594
  1002
    fun list1 (Const(@{const_name Cons},_) $ _ $ Const(@{const_name Nil},_)) = true
wenzelm@43594
  1003
      | list1 _ = false;
wenzelm@43594
  1004
    
wenzelm@43594
  1005
    fun butlast ((cons as Const(@{const_name Cons},_) $ x) $ xs) =
wenzelm@43594
  1006
          (case xs of Const (@{const_name Nil}, _) => xs | _ => cons $ butlast xs)
wenzelm@43594
  1007
      | butlast ((app as Const (@{const_name append}, _) $ xs) $ ys) = app $ butlast ys
wenzelm@43594
  1008
      | butlast xs = Const(@{const_name Nil}, fastype_of xs);
wenzelm@43594
  1009
    
wenzelm@43594
  1010
    val rearr_ss =
wenzelm@43594
  1011
      HOL_basic_ss addsimps [@{thm append_assoc}, @{thm append_Nil}, @{thm append_Cons}];
wenzelm@43594
  1012
    
wenzelm@43594
  1013
    fun list_eq ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
wenzelm@13462
  1014
      let
wenzelm@43594
  1015
        val lastl = last lhs and lastr = last rhs;
wenzelm@43594
  1016
        fun rearr conv =
wenzelm@43594
  1017
          let
wenzelm@43594
  1018
            val lhs1 = butlast lhs and rhs1 = butlast rhs;
wenzelm@43594
  1019
            val Type(_,listT::_) = eqT
wenzelm@43594
  1020
            val appT = [listT,listT] ---> listT
wenzelm@43594
  1021
            val app = Const(@{const_name append},appT)
wenzelm@43594
  1022
            val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
wenzelm@43594
  1023
            val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2));
wenzelm@43594
  1024
            val thm = Goal.prove (Simplifier.the_context ss) [] [] eq
wenzelm@43594
  1025
              (K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1));
wenzelm@43594
  1026
          in SOME ((conv RS (thm RS trans)) RS eq_reflection) end;
wenzelm@43594
  1027
      in
wenzelm@43594
  1028
        if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv}
wenzelm@43594
  1029
        else if lastl aconv lastr then rearr @{thm append_same_eq}
wenzelm@43594
  1030
        else NONE
wenzelm@43594
  1031
      end;
wenzelm@43594
  1032
  in fn _ => fn ss => fn ct => list_eq ss (term_of ct) end;
wenzelm@13114
  1033
*}
wenzelm@13114
  1034
wenzelm@13114
  1035
haftmann@49948
  1036
subsubsection {* @{const map} *}
wenzelm@13114
  1037
haftmann@40210
  1038
lemma hd_map:
haftmann@40210
  1039
  "xs \<noteq> [] \<Longrightarrow> hd (map f xs) = f (hd xs)"
haftmann@40210
  1040
  by (cases xs) simp_all
haftmann@40210
  1041
haftmann@40210
  1042
lemma map_tl:
haftmann@40210
  1043
  "map f (tl xs) = tl (map f xs)"
haftmann@40210
  1044
  by (cases xs) simp_all
haftmann@40210
  1045
wenzelm@13142
  1046
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
nipkow@13145
  1047
by (induct xs) simp_all
wenzelm@13114
  1048
wenzelm@13142
  1049
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
nipkow@13145
  1050
by (rule ext, induct_tac xs) auto
wenzelm@13114
  1051
wenzelm@13142
  1052
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
nipkow@13145
  1053
by (induct xs) auto
wenzelm@13114
  1054
hoelzl@33639
  1055
lemma map_map [simp]: "map f (map g xs) = map (f \<circ> g) xs"
hoelzl@33639
  1056
by (induct xs) auto
hoelzl@33639
  1057
nipkow@35208
  1058
lemma map_comp_map[simp]: "((map f) o (map g)) = map(f o g)"
nipkow@35208
  1059
apply(rule ext)
nipkow@35208
  1060
apply(simp)
nipkow@35208
  1061
done
nipkow@35208
  1062
wenzelm@13142
  1063
lemma rev_map: "rev (map f xs) = map f (rev xs)"
nipkow@13145
  1064
by (induct xs) auto
wenzelm@13114
  1065
nipkow@13737
  1066
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)"
nipkow@13737
  1067
by (induct xs) auto
nipkow@13737
  1068
krauss@44013
  1069
lemma map_cong [fundef_cong]:
haftmann@40122
  1070
  "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g ys"
haftmann@40122
  1071
  by simp
wenzelm@13114
  1072
wenzelm@13142
  1073
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
nipkow@13145
  1074
by (cases xs) auto
wenzelm@13114
  1075
wenzelm@13142
  1076
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
nipkow@13145
  1077
by (cases xs) auto
wenzelm@13114
  1078
paulson@18447
  1079
lemma map_eq_Cons_conv:
nipkow@14025
  1080
 "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)"
nipkow@13145
  1081
by (cases xs) auto
wenzelm@13114
  1082
paulson@18447
  1083
lemma Cons_eq_map_conv:
nipkow@14025
  1084
 "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)"
nipkow@14025
  1085
by (cases ys) auto
nipkow@14025
  1086
paulson@18447
  1087
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1]
paulson@18447
  1088
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1]
paulson@18447
  1089
declare map_eq_Cons_D [dest!]  Cons_eq_map_D [dest!]
paulson@18447
  1090
nipkow@14111
  1091
lemma ex_map_conv:
nipkow@14111
  1092
  "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)"
paulson@18447
  1093
by(induct ys, auto simp add: Cons_eq_map_conv)
nipkow@14111
  1094
nipkow@15110
  1095
lemma map_eq_imp_length_eq:
paulson@35510
  1096
  assumes "map f xs = map g ys"
haftmann@26734
  1097
  shows "length xs = length ys"
haftmann@26734
  1098
using assms proof (induct ys arbitrary: xs)
haftmann@26734
  1099
  case Nil then show ?case by simp
haftmann@26734
  1100
next
haftmann@26734
  1101
  case (Cons y ys) then obtain z zs where xs: "xs = z # zs" by auto
paulson@35510
  1102
  from Cons xs have "map f zs = map g ys" by simp
haftmann@26734
  1103
  moreover with Cons have "length zs = length ys" by blast
haftmann@26734
  1104
  with xs show ?case by simp
haftmann@26734
  1105
qed
haftmann@26734
  1106
  
nipkow@15110
  1107
lemma map_inj_on:
nipkow@15110
  1108
 "[| map f xs = map f ys; inj_on f (set xs Un set ys) |]
nipkow@15110
  1109
  ==> xs = ys"
nipkow@15110
  1110
apply(frule map_eq_imp_length_eq)
nipkow@15110
  1111
apply(rotate_tac -1)
nipkow@15110
  1112
apply(induct rule:list_induct2)
nipkow@15110
  1113
 apply simp
nipkow@15110
  1114
apply(simp)
nipkow@15110
  1115
apply (blast intro:sym)
nipkow@15110
  1116
done
nipkow@15110
  1117
nipkow@15110
  1118
lemma inj_on_map_eq_map:
nipkow@15110
  1119
 "inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@15110
  1120
by(blast dest:map_inj_on)
nipkow@15110
  1121
wenzelm@13114
  1122
lemma map_injective:
nipkow@24526
  1123
 "map f xs = map f ys ==> inj f ==> xs = ys"
nipkow@24526
  1124
by (induct ys arbitrary: xs) (auto dest!:injD)
wenzelm@13114
  1125
nipkow@14339
  1126
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@14339
  1127
by(blast dest:map_injective)
nipkow@14339
  1128
wenzelm@13114
  1129
lemma inj_mapI: "inj f ==> inj (map f)"
nipkow@17589
  1130
by (iprover dest: map_injective injD intro: inj_onI)
wenzelm@13114
  1131
wenzelm@13114
  1132
lemma inj_mapD: "inj (map f) ==> inj f"
paulson@14208
  1133
apply (unfold inj_on_def, clarify)
nipkow@13145
  1134
apply (erule_tac x = "[x]" in ballE)
paulson@14208
  1135
 apply (erule_tac x = "[y]" in ballE, simp, blast)
nipkow@13145
  1136
apply blast
nipkow@13145
  1137
done
wenzelm@13114
  1138
nipkow@14339
  1139
lemma inj_map[iff]: "inj (map f) = inj f"
nipkow@13145
  1140
by (blast dest: inj_mapD intro: inj_mapI)
wenzelm@13114
  1141
nipkow@15303
  1142
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A"
nipkow@15303
  1143
apply(rule inj_onI)
nipkow@15303
  1144
apply(erule map_inj_on)
nipkow@15303
  1145
apply(blast intro:inj_onI dest:inj_onD)
nipkow@15303
  1146
done
nipkow@15303
  1147
kleing@14343
  1148
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs"
kleing@14343
  1149
by (induct xs, auto)
wenzelm@13114
  1150
nipkow@14402
  1151
lemma map_fun_upd [simp]: "y \<notin> set xs \<Longrightarrow> map (f(y:=v)) xs = map f xs"
nipkow@14402
  1152
by (induct xs) auto
nipkow@14402
  1153
nipkow@15110
  1154
lemma map_fst_zip[simp]:
nipkow@15110
  1155
  "length xs = length ys \<Longrightarrow> map fst (zip xs ys) = xs"
nipkow@15110
  1156
by (induct rule:list_induct2, simp_all)
nipkow@15110
  1157
nipkow@15110
  1158
lemma map_snd_zip[simp]:
nipkow@15110
  1159
  "length xs = length ys \<Longrightarrow> map snd (zip xs ys) = ys"
nipkow@15110
  1160
by (induct rule:list_induct2, simp_all)
nipkow@15110
  1161
haftmann@41505
  1162
enriched_type map: map
nipkow@47122
  1163
by (simp_all add: id_def)
nipkow@47122
  1164
haftmann@49948
  1165
declare map.id [simp]
haftmann@49948
  1166
haftmann@49948
  1167
haftmann@49948
  1168
subsubsection {* @{const rev} *}
wenzelm@13114
  1169
wenzelm@13142
  1170
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
nipkow@13145
  1171
by (induct xs) auto
wenzelm@13114
  1172
wenzelm@13142
  1173
lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
nipkow@13145
  1174
by (induct xs) auto
wenzelm@13114
  1175
kleing@15870
  1176
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)"
kleing@15870
  1177
by auto
kleing@15870
  1178
wenzelm@13142
  1179
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
nipkow@13145
  1180
by (induct xs) auto
wenzelm@13114
  1181
wenzelm@13142
  1182
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
nipkow@13145
  1183
by (induct xs) auto
wenzelm@13114
  1184
kleing@15870
  1185
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])"
kleing@15870
  1186
by (cases xs) auto
kleing@15870
  1187
kleing@15870
  1188
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])"
kleing@15870
  1189
by (cases xs) auto
kleing@15870
  1190
blanchet@46439
  1191
lemma rev_is_rev_conv [iff, no_atp]: "(rev xs = rev ys) = (xs = ys)"
haftmann@21061
  1192
apply (induct xs arbitrary: ys, force)
paulson@14208
  1193
apply (case_tac ys, simp, force)
nipkow@13145
  1194
done
wenzelm@13114
  1195
nipkow@15439
  1196
lemma inj_on_rev[iff]: "inj_on rev A"
nipkow@15439
  1197
by(simp add:inj_on_def)
nipkow@15439
  1198
wenzelm@13366
  1199
lemma rev_induct [case_names Nil snoc]:
wenzelm@13366
  1200
  "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
berghofe@15489
  1201
apply(simplesubst rev_rev_ident[symmetric])
nipkow@13145
  1202
apply(rule_tac list = "rev xs" in list.induct, simp_all)
nipkow@13145
  1203
done
wenzelm@13114
  1204
wenzelm@13366
  1205
lemma rev_exhaust [case_names Nil snoc]:
wenzelm@13366
  1206
  "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"
nipkow@13145
  1207
by (induct xs rule: rev_induct) auto
wenzelm@13114
  1208
wenzelm@13366
  1209
lemmas rev_cases = rev_exhaust
wenzelm@13366
  1210
nipkow@18423
  1211
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])"
nipkow@18423
  1212
by(rule rev_cases[of xs]) auto
nipkow@18423
  1213
wenzelm@13114
  1214
haftmann@49948
  1215
subsubsection {* @{const set} *}
wenzelm@13114
  1216
nipkow@46698
  1217
declare set.simps [code_post]  --"pretty output"
nipkow@46698
  1218
wenzelm@13142
  1219
lemma finite_set [iff]: "finite (set xs)"
nipkow@13145
  1220
by (induct xs) auto
wenzelm@13114
  1221
wenzelm@13142
  1222
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
nipkow@13145
  1223
by (induct xs) auto
wenzelm@13114
  1224
nipkow@17830
  1225
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs"
nipkow@17830
  1226
by(cases xs) auto
oheimb@14099
  1227
wenzelm@13142
  1228
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
nipkow@13145
  1229
by auto
wenzelm@13114
  1230
oheimb@14099
  1231
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" 
oheimb@14099
  1232
by auto
oheimb@14099
  1233
wenzelm@13142
  1234
lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
nipkow@13145
  1235
by (induct xs) auto
wenzelm@13114
  1236
nipkow@15245
  1237
lemma set_empty2[iff]: "({} = set xs) = (xs = [])"
nipkow@15245
  1238
by(induct xs) auto
nipkow@15245
  1239
wenzelm@13142
  1240
lemma set_rev [simp]: "set (rev xs) = set xs"
nipkow@13145
  1241
by (induct xs) auto
wenzelm@13114
  1242
wenzelm@13142
  1243
lemma set_map [simp]: "set (map f xs) = f`(set xs)"
nipkow@13145
  1244
by (induct xs) auto
wenzelm@13114
  1245
wenzelm@13142
  1246
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
nipkow@13145
  1247
by (induct xs) auto
wenzelm@13114
  1248
nipkow@32417
  1249
lemma set_upt [simp]: "set[i..<j] = {i..<j}"
bulwahn@41463
  1250
by (induct j) auto
wenzelm@13114
  1251
wenzelm@13142
  1252
wenzelm@25221
  1253
lemma split_list: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs"
nipkow@18049
  1254
proof (induct xs)
nipkow@26073
  1255
  case Nil thus ?case by simp
nipkow@26073
  1256
next
nipkow@26073
  1257
  case Cons thus ?case by (auto intro: Cons_eq_appendI)
nipkow@26073
  1258
qed
nipkow@26073
  1259
haftmann@26734
  1260
lemma in_set_conv_decomp: "x \<in> set xs \<longleftrightarrow> (\<exists>ys zs. xs = ys @ x # zs)"
haftmann@26734
  1261
  by (auto elim: split_list)
nipkow@26073
  1262
nipkow@26073
  1263
lemma split_list_first: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys"
nipkow@26073
  1264
proof (induct xs)
nipkow@26073
  1265
  case Nil thus ?case by simp
nipkow@18049
  1266
next
nipkow@18049
  1267
  case (Cons a xs)
nipkow@18049
  1268
  show ?case
nipkow@18049
  1269
  proof cases
nipkow@44890
  1270
    assume "x = a" thus ?case using Cons by fastforce
nipkow@18049
  1271
  next
nipkow@44890
  1272
    assume "x \<noteq> a" thus ?case using Cons by(fastforce intro!: Cons_eq_appendI)
nipkow@26073
  1273
  qed
nipkow@26073
  1274
qed
nipkow@26073
  1275
nipkow@26073
  1276
lemma in_set_conv_decomp_first:
nipkow@26073
  1277
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)"
haftmann@26734
  1278
  by (auto dest!: split_list_first)
nipkow@26073
  1279
haftmann@40122
  1280
lemma split_list_last: "x \<in> set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs"
haftmann@40122
  1281
proof (induct xs rule: rev_induct)
nipkow@26073
  1282
  case Nil thus ?case by simp
nipkow@26073
  1283
next
nipkow@26073
  1284
  case (snoc a xs)
nipkow@26073
  1285
  show ?case
nipkow@26073
  1286
  proof cases
haftmann@40122
  1287
    assume "x = a" thus ?case using snoc by (metis List.set.simps(1) emptyE)
nipkow@26073
  1288
  next
nipkow@44890
  1289
    assume "x \<noteq> a" thus ?case using snoc by fastforce
nipkow@18049
  1290
  qed
nipkow@18049
  1291
qed
nipkow@18049
  1292
nipkow@26073
  1293
lemma in_set_conv_decomp_last:
nipkow@26073
  1294
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)"
haftmann@26734
  1295
  by (auto dest!: split_list_last)
nipkow@26073
  1296
nipkow@26073
  1297
lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs & P x"
nipkow@26073
  1298
proof (induct xs)
nipkow@26073
  1299
  case Nil thus ?case by simp
nipkow@26073
  1300
next
nipkow@26073
  1301
  case Cons thus ?case
nipkow@26073
  1302
    by(simp add:Bex_def)(metis append_Cons append.simps(1))
nipkow@26073
  1303
qed
nipkow@26073
  1304
nipkow@26073
  1305
lemma split_list_propE:
haftmann@26734
  1306
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
  1307
  obtains ys x zs where "xs = ys @ x # zs" and "P x"
haftmann@26734
  1308
using split_list_prop [OF assms] by blast
nipkow@26073
  1309
nipkow@26073
  1310
lemma split_list_first_prop:
nipkow@26073
  1311
  "\<exists>x \<in> set xs. P x \<Longrightarrow>
nipkow@26073
  1312
   \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)"
haftmann@26734
  1313
proof (induct xs)
nipkow@26073
  1314
  case Nil thus ?case by simp
nipkow@26073
  1315
next
nipkow@26073
  1316
  case (Cons x xs)
nipkow@26073
  1317
  show ?case
nipkow@26073
  1318
  proof cases
nipkow@26073
  1319
    assume "P x"
haftmann@40122
  1320
    thus ?thesis by simp (metis Un_upper1 contra_subsetD in_set_conv_decomp_first self_append_conv2 set_append)
nipkow@26073
  1321
  next
nipkow@26073
  1322
    assume "\<not> P x"
nipkow@26073
  1323
    hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp
nipkow@26073
  1324
    thus ?thesis using `\<not> P x` Cons(1) by (metis append_Cons set_ConsD)
nipkow@26073
  1325
  qed
nipkow@26073
  1326
qed
nipkow@26073
  1327
nipkow@26073
  1328
lemma split_list_first_propE:
haftmann@26734
  1329
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
  1330
  obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y"
haftmann@26734
  1331
using split_list_first_prop [OF assms] by blast
nipkow@26073
  1332
nipkow@26073
  1333
lemma split_list_first_prop_iff:
nipkow@26073
  1334
  "(\<exists>x \<in> set xs. P x) \<longleftrightarrow>
nipkow@26073
  1335
   (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))"
haftmann@26734
  1336
by (rule, erule split_list_first_prop) auto
nipkow@26073
  1337
nipkow@26073
  1338
lemma split_list_last_prop:
nipkow@26073
  1339
  "\<exists>x \<in> set xs. P x \<Longrightarrow>
nipkow@26073
  1340
   \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)"
nipkow@26073
  1341
proof(induct xs rule:rev_induct)
nipkow@26073
  1342
  case Nil thus ?case by simp
nipkow@26073
  1343
next
nipkow@26073
  1344
  case (snoc x xs)
nipkow@26073
  1345
  show ?case
nipkow@26073
  1346
  proof cases
nipkow@26073
  1347
    assume "P x" thus ?thesis by (metis emptyE set_empty)
nipkow@26073
  1348
  next
nipkow@26073
  1349
    assume "\<not> P x"
nipkow@26073
  1350
    hence "\<exists>x\<in>set xs. P x" using snoc(2) by simp
nipkow@44890
  1351
    thus ?thesis using `\<not> P x` snoc(1) by fastforce
nipkow@26073
  1352
  qed
nipkow@26073
  1353
qed
nipkow@26073
  1354
nipkow@26073
  1355
lemma split_list_last_propE:
haftmann@26734
  1356
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
  1357
  obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z"
haftmann@26734
  1358
using split_list_last_prop [OF assms] by blast
nipkow@26073
  1359
nipkow@26073
  1360
lemma split_list_last_prop_iff:
nipkow@26073
  1361
  "(\<exists>x \<in> set xs. P x) \<longleftrightarrow>
nipkow@26073
  1362
   (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))"
haftmann@26734
  1363
by (metis split_list_last_prop [where P=P] in_set_conv_decomp)
nipkow@26073
  1364
nipkow@26073
  1365
lemma finite_list: "finite A ==> EX xs. set xs = A"
haftmann@26734
  1366
  by (erule finite_induct)
haftmann@26734
  1367
    (auto simp add: set.simps(2) [symmetric] simp del: set.simps(2))
paulson@13508
  1368
kleing@14388
  1369
lemma card_length: "card (set xs) \<le> length xs"
kleing@14388
  1370
by (induct xs) (auto simp add: card_insert_if)
wenzelm@13114
  1371
haftmann@26442
  1372
lemma set_minus_filter_out:
haftmann@26442
  1373
  "set xs - {y} = set (filter (\<lambda>x. \<not> (x = y)) xs)"
haftmann@26442
  1374
  by (induct xs) auto
paulson@15168
  1375
wenzelm@35115
  1376
haftmann@49948
  1377
subsubsection {* @{const filter} *}
wenzelm@13114
  1378
wenzelm@13142
  1379
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
nipkow@13145
  1380
by (induct xs) auto
wenzelm@13114
  1381
nipkow@15305
  1382
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)"
nipkow@15305
  1383
by (induct xs) simp_all
nipkow@15305
  1384
wenzelm@13142
  1385
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
nipkow@13145
  1386
by (induct xs) auto
wenzelm@13114
  1387
nipkow@16998
  1388
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs"
nipkow@16998
  1389
by (induct xs) (auto simp add: le_SucI)
nipkow@16998
  1390
nipkow@18423
  1391
lemma sum_length_filter_compl:
nipkow@18423
  1392
  "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs"
nipkow@18423
  1393
by(induct xs) simp_all
nipkow@18423
  1394
wenzelm@13142
  1395
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
nipkow@13145
  1396
by (induct xs) auto
wenzelm@13114
  1397
wenzelm@13142
  1398
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
nipkow@13145
  1399
by (induct xs) auto
wenzelm@13114
  1400
nipkow@16998
  1401
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" 
nipkow@24349
  1402
by (induct xs) simp_all
nipkow@16998
  1403
nipkow@16998
  1404
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)"
nipkow@16998
  1405
apply (induct xs)
nipkow@16998
  1406
 apply auto
nipkow@16998
  1407
apply(cut_tac P=P and xs=xs in length_filter_le)
nipkow@16998
  1408
apply simp
nipkow@16998
  1409
done
wenzelm@13114
  1410
nipkow@16965
  1411
lemma filter_map:
nipkow@16965
  1412
  "filter P (map f xs) = map f (filter (P o f) xs)"
nipkow@16965
  1413
by (induct xs) simp_all
nipkow@16965
  1414
nipkow@16965
  1415
lemma length_filter_map[simp]:
nipkow@16965
  1416
  "length (filter P (map f xs)) = length(filter (P o f) xs)"
nipkow@16965
  1417
by (simp add:filter_map)
nipkow@16965
  1418
wenzelm@13142
  1419
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
nipkow@13145
  1420
by auto
wenzelm@13114
  1421
nipkow@15246
  1422
lemma length_filter_less:
nipkow@15246
  1423
  "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs"
nipkow@15246
  1424
proof (induct xs)
nipkow@15246
  1425
  case Nil thus ?case by simp
nipkow@15246
  1426
next
nipkow@15246
  1427
  case (Cons x xs) thus ?case
nipkow@15246
  1428
    apply (auto split:split_if_asm)
nipkow@15246
  1429
    using length_filter_le[of P xs] apply arith
nipkow@15246
  1430
  done
nipkow@15246
  1431
qed
wenzelm@13114
  1432
nipkow@15281
  1433
lemma length_filter_conv_card:
nipkow@15281
  1434
 "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
nipkow@15281
  1435
proof (induct xs)
nipkow@15281
  1436
  case Nil thus ?case by simp
nipkow@15281
  1437
next
nipkow@15281
  1438
  case (Cons x xs)
nipkow@15281
  1439
  let ?S = "{i. i < length xs & p(xs!i)}"
nipkow@15281
  1440
  have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite)
nipkow@15281
  1441
  show ?case (is "?l = card ?S'")
nipkow@15281
  1442
  proof (cases)
nipkow@15281
  1443
    assume "p x"
nipkow@15281
  1444
    hence eq: "?S' = insert 0 (Suc ` ?S)"
nipkow@25162
  1445
      by(auto simp: image_def split:nat.split dest:gr0_implies_Suc)
nipkow@15281
  1446
    have "length (filter p (x # xs)) = Suc(card ?S)"
wenzelm@23388
  1447
      using Cons `p x` by simp
nipkow@15281
  1448
    also have "\<dots> = Suc(card(Suc ` ?S))" using fin
huffman@44921
  1449
      by (simp add: card_image)
nipkow@15281
  1450
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
  1451
      by (simp add:card_insert_if) (simp add:image_def)
nipkow@15281
  1452
    finally show ?thesis .
nipkow@15281
  1453
  next
nipkow@15281
  1454
    assume "\<not> p x"
nipkow@15281
  1455
    hence eq: "?S' = Suc ` ?S"
nipkow@25162
  1456
      by(auto simp add: image_def split:nat.split elim:lessE)
nipkow@15281
  1457
    have "length (filter p (x # xs)) = card ?S"
wenzelm@23388
  1458
      using Cons `\<not> p x` by simp
nipkow@15281
  1459
    also have "\<dots> = card(Suc ` ?S)" using fin
huffman@44921
  1460
      by (simp add: card_image)
nipkow@15281
  1461
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
  1462
      by (simp add:card_insert_if)
nipkow@15281
  1463
    finally show ?thesis .
nipkow@15281
  1464
  qed
nipkow@15281
  1465
qed
nipkow@15281
  1466
nipkow@17629
  1467
lemma Cons_eq_filterD:
nipkow@17629
  1468
 "x#xs = filter P ys \<Longrightarrow>
nipkow@17629
  1469
  \<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
  1470
  (is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs")
nipkow@17629
  1471
proof(induct ys)
nipkow@17629
  1472
  case Nil thus ?case by simp
nipkow@17629
  1473
next
nipkow@17629
  1474
  case (Cons y ys)
nipkow@17629
  1475
  show ?case (is "\<exists>x. ?Q x")
nipkow@17629
  1476
  proof cases
nipkow@17629
  1477
    assume Py: "P y"
nipkow@17629
  1478
    show ?thesis
nipkow@17629
  1479
    proof cases
wenzelm@25221
  1480
      assume "x = y"
wenzelm@25221
  1481
      with Py Cons.prems have "?Q []" by simp
wenzelm@25221
  1482
      then show ?thesis ..
nipkow@17629
  1483
    next
wenzelm@25221
  1484
      assume "x \<noteq> y"
wenzelm@25221
  1485
      with Py Cons.prems show ?thesis by simp
nipkow@17629
  1486
    qed
nipkow@17629
  1487
  next
wenzelm@25221
  1488
    assume "\<not> P y"
nipkow@44890
  1489
    with Cons obtain us vs where "?P (y#ys) (y#us) vs" by fastforce
wenzelm@25221
  1490
    then have "?Q (y#us)" by simp
wenzelm@25221
  1491
    then show ?thesis ..
nipkow@17629
  1492
  qed
nipkow@17629
  1493
qed
nipkow@17629
  1494
nipkow@17629
  1495
lemma filter_eq_ConsD:
nipkow@17629
  1496
 "filter P ys = x#xs \<Longrightarrow>
nipkow@17629
  1497
  \<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
  1498
by(rule Cons_eq_filterD) simp
nipkow@17629
  1499
nipkow@17629
  1500
lemma filter_eq_Cons_iff:
nipkow@17629
  1501
 "(filter P ys = x#xs) =
nipkow@17629
  1502
  (\<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
  1503
by(auto dest:filter_eq_ConsD)
nipkow@17629
  1504
nipkow@17629
  1505
lemma Cons_eq_filter_iff:
nipkow@17629
  1506
 "(x#xs = filter P ys) =
nipkow@17629
  1507
  (\<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
  1508
by(auto dest:Cons_eq_filterD)
nipkow@17629
  1509
krauss@44013
  1510
lemma filter_cong[fundef_cong]:
nipkow@17501
  1511
 "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys"
nipkow@17501
  1512
apply simp
nipkow@17501
  1513
apply(erule thin_rl)
nipkow@17501
  1514
by (induct ys) simp_all
nipkow@17501
  1515
nipkow@15281
  1516
haftmann@26442
  1517
subsubsection {* List partitioning *}
haftmann@26442
  1518
haftmann@26442
  1519
primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where
haftmann@26442
  1520
  "partition P [] = ([], [])"
haftmann@26442
  1521
  | "partition P (x # xs) = 
haftmann@26442
  1522
      (let (yes, no) = partition P xs
haftmann@26442
  1523
      in if P x then (x # yes, no) else (yes, x # no))"
haftmann@26442
  1524
haftmann@26442
  1525
lemma partition_filter1:
haftmann@26442
  1526
    "fst (partition P xs) = filter P xs"
haftmann@26442
  1527
by (induct xs) (auto simp add: Let_def split_def)
haftmann@26442
  1528
haftmann@26442
  1529
lemma partition_filter2:
haftmann@26442
  1530
    "snd (partition P xs) = filter (Not o P) xs"
haftmann@26442
  1531
by (induct xs) (auto simp add: Let_def split_def)
haftmann@26442
  1532
haftmann@26442
  1533
lemma partition_P:
haftmann@26442
  1534
  assumes "partition P xs = (yes, no)"
haftmann@26442
  1535
  shows "(\<forall>p \<in> set yes.  P p) \<and> (\<forall>p  \<in> set no. \<not> P p)"
haftmann@26442
  1536
proof -
haftmann@26442
  1537
  from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)"
haftmann@26442
  1538
    by simp_all
haftmann@26442
  1539
  then show ?thesis by (simp_all add: partition_filter1 partition_filter2)
haftmann@26442
  1540
qed
haftmann@26442
  1541
haftmann@26442
  1542
lemma partition_set:
haftmann@26442
  1543
  assumes "partition P xs = (yes, no)"
haftmann@26442
  1544
  shows "set yes \<union> set no = set xs"
haftmann@26442
  1545
proof -
haftmann@26442
  1546
  from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)"
haftmann@26442
  1547
    by simp_all
haftmann@26442
  1548
  then show ?thesis by (auto simp add: partition_filter1 partition_filter2) 
haftmann@26442
  1549
qed
haftmann@26442
  1550
hoelzl@33639
  1551
lemma partition_filter_conv[simp]:
hoelzl@33639
  1552
  "partition f xs = (filter f xs,filter (Not o f) xs)"
hoelzl@33639
  1553
unfolding partition_filter2[symmetric]
hoelzl@33639
  1554
unfolding partition_filter1[symmetric] by simp
hoelzl@33639
  1555
hoelzl@33639
  1556
declare partition.simps[simp del]
haftmann@26442
  1557
wenzelm@35115
  1558
haftmann@49948
  1559
subsubsection {* @{const concat} *}
wenzelm@13114
  1560
wenzelm@13142
  1561
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
nipkow@13145
  1562
by (induct xs) auto
wenzelm@13114
  1563
paulson@18447
  1564
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1565
by (induct xss) auto
wenzelm@13114
  1566
paulson@18447
  1567
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1568
by (induct xss) auto
wenzelm@13114
  1569
nipkow@24308
  1570
lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)"
nipkow@13145
  1571
by (induct xs) auto
wenzelm@13114
  1572
nipkow@24476
  1573
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs"
nipkow@24349
  1574
by (induct xs) auto
nipkow@24349
  1575
wenzelm@13142
  1576
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
nipkow@13145
  1577
by (induct xs) auto
wenzelm@13114
  1578
wenzelm@13142
  1579
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
nipkow@13145
  1580
by (induct xs) auto
wenzelm@13114
  1581
wenzelm@13142
  1582
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
nipkow@13145
  1583
by (induct xs) auto
wenzelm@13114
  1584
bulwahn@40365
  1585
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
  1586
proof (induct xs arbitrary: ys)
bulwahn@40365
  1587
  case (Cons x xs ys)
bulwahn@40365
  1588
  thus ?case by (cases ys) auto
bulwahn@40365
  1589
qed (auto)
bulwahn@40365
  1590
bulwahn@40365
  1591
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
  1592
by (simp add: concat_eq_concat_iff)
bulwahn@40365
  1593
wenzelm@13114
  1594
haftmann@49948
  1595
subsubsection {* @{const nth} *}
wenzelm@13114
  1596
haftmann@29827
  1597
lemma nth_Cons_0 [simp, code]: "(x # xs)!0 = x"
nipkow@13145
  1598
by auto
wenzelm@13114
  1599
haftmann@29827
  1600
lemma nth_Cons_Suc [simp, code]: "(x # xs)!(Suc n) = xs!n"
nipkow@13145
  1601
by auto
wenzelm@13114
  1602
wenzelm@13142
  1603
declare nth.simps [simp del]
wenzelm@13114
  1604
nipkow@41842
  1605
lemma nth_Cons_pos[simp]: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)"
nipkow@41842
  1606
by(auto simp: Nat.gr0_conv_Suc)
nipkow@41842
  1607
wenzelm@13114
  1608
lemma nth_append:
nipkow@24526
  1609
  "(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
nipkow@24526
  1610
apply (induct xs arbitrary: n, simp)
paulson@14208
  1611
apply (case_tac n, auto)
nipkow@13145
  1612
done
wenzelm@13114
  1613
nipkow@14402
  1614
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x"
wenzelm@25221
  1615
by (induct xs) auto
nipkow@14402
  1616
nipkow@14402
  1617
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n"
wenzelm@25221
  1618
by (induct xs) auto
nipkow@14402
  1619
nipkow@24526
  1620
lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)"
nipkow@24526
  1621
apply (induct xs arbitrary: n, simp)
paulson@14208
  1622
apply (case_tac n, auto)
nipkow@13145
  1623
done
wenzelm@13114
  1624
noschinl@45841
  1625
lemma nth_tl:
noschinl@45841
  1626
  assumes "n < length (tl x)" shows "tl x ! n = x ! Suc n"
noschinl@45841
  1627
using assms by (induct x) auto
noschinl@45841
  1628
nipkow@18423
  1629
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"
nipkow@18423
  1630
by(cases xs) simp_all
nipkow@18423
  1631
nipkow@18049
  1632
nipkow@18049
  1633
lemma list_eq_iff_nth_eq:
nipkow@24526
  1634
 "(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))"
nipkow@24526
  1635
apply(induct xs arbitrary: ys)
paulson@24632
  1636
 apply force
nipkow@18049
  1637
apply(case_tac ys)
nipkow@18049
  1638
 apply simp
nipkow@18049
  1639
apply(simp add:nth_Cons split:nat.split)apply blast
nipkow@18049
  1640
done
nipkow@18049
  1641
wenzelm@13142
  1642
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
paulson@15251
  1643
apply (induct xs, simp, simp)
nipkow@13145
  1644
apply safe
paulson@24632
  1645
apply (metis nat_case_0 nth.simps zero_less_Suc)
paulson@24632
  1646
apply (metis less_Suc_eq_0_disj nth_Cons_Suc)
paulson@14208
  1647
apply (case_tac i, simp)
paulson@24632
  1648
apply (metis diff_Suc_Suc nat_case_Suc nth.simps zero_less_diff)
nipkow@13145
  1649
done
wenzelm@13114
  1650
nipkow@17501
  1651
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)"
nipkow@17501
  1652
by(auto simp:set_conv_nth)
nipkow@17501
  1653
nipkow@13145
  1654
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"
nipkow@13145
  1655
by (auto simp add: set_conv_nth)
wenzelm@13114
  1656
wenzelm@13142
  1657
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
nipkow@13145
  1658
by (auto simp add: set_conv_nth)
wenzelm@13114
  1659
wenzelm@13114
  1660
lemma all_nth_imp_all_set:
nipkow@13145
  1661
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x"
nipkow@13145
  1662
by (auto simp add: set_conv_nth)
wenzelm@13114
  1663
wenzelm@13114
  1664
lemma all_set_conv_all_nth:
nipkow@13145
  1665
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
nipkow@13145
  1666
by (auto simp add: set_conv_nth)
wenzelm@13114
  1667
kleing@25296
  1668
lemma rev_nth:
kleing@25296
  1669
  "n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)"
kleing@25296
  1670
proof (induct xs arbitrary: n)
kleing@25296
  1671
  case Nil thus ?case by simp
kleing@25296
  1672
next
kleing@25296
  1673
  case (Cons x xs)
kleing@25296
  1674
  hence n: "n < Suc (length xs)" by simp
kleing@25296
  1675
  moreover
kleing@25296
  1676
  { assume "n < length xs"
kleing@25296
  1677
    with n obtain n' where "length xs - n = Suc n'"
kleing@25296
  1678
      by (cases "length xs - n", auto)
kleing@25296
  1679
    moreover
kleing@25296
  1680
    then have "length xs - Suc n = n'" by simp
kleing@25296
  1681
    ultimately
kleing@25296
  1682
    have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp
kleing@25296
  1683
  }
kleing@25296
  1684
  ultimately
kleing@25296
  1685
  show ?case by (clarsimp simp add: Cons nth_append)
kleing@25296
  1686
qed
wenzelm@13114
  1687
nipkow@31159
  1688
lemma Skolem_list_nth:
nipkow@31159
  1689
  "(ALL i<k. EX x. P i x) = (EX xs. size xs = k & (ALL i<k. P i (xs!i)))"
nipkow@31159
  1690
  (is "_ = (EX xs. ?P k xs)")
nipkow@31159
  1691
proof(induct k)
nipkow@31159
  1692
  case 0 show ?case by simp
nipkow@31159
  1693
next
nipkow@31159
  1694
  case (Suc k)
nipkow@31159
  1695
  show ?case (is "?L = ?R" is "_ = (EX xs. ?P' xs)")
nipkow@31159
  1696
  proof
nipkow@31159
  1697
    assume "?R" thus "?L" using Suc by auto
nipkow@31159
  1698
  next
nipkow@31159
  1699
    assume "?L"
nipkow@31159
  1700
    with Suc obtain x xs where "?P k xs & P k x" by (metis less_Suc_eq)
nipkow@31159
  1701
    hence "?P'(xs@[x])" by(simp add:nth_append less_Suc_eq)
nipkow@31159
  1702
    thus "?R" ..
nipkow@31159
  1703
  qed
nipkow@31159
  1704
qed
nipkow@31159
  1705
nipkow@31159
  1706
haftmann@49948
  1707
subsubsection {* @{const list_update} *}
wenzelm@13114
  1708
nipkow@24526
  1709
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs"
nipkow@24526
  1710
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1711
wenzelm@13114
  1712
lemma nth_list_update:
nipkow@24526
  1713
"i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"
nipkow@24526
  1714
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1715
wenzelm@13142
  1716
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
nipkow@13145
  1717
by (simp add: nth_list_update)
wenzelm@13114
  1718
nipkow@24526
  1719
lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j"
nipkow@24526
  1720
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1721
nipkow@24526
  1722
lemma list_update_id[simp]: "xs[i := xs!i] = xs"
nipkow@24526
  1723
by (induct xs arbitrary: i) (simp_all split:nat.splits)
nipkow@24526
  1724
nipkow@24526
  1725
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs"
nipkow@24526
  1726
apply (induct xs arbitrary: i)
nipkow@17501
  1727
 apply simp
nipkow@17501
  1728
apply (case_tac i)
nipkow@17501
  1729
apply simp_all
nipkow@17501
  1730
done
nipkow@17501
  1731
nipkow@31077
  1732
lemma list_update_nonempty[simp]: "xs[k:=x] = [] \<longleftrightarrow> xs=[]"
nipkow@31077
  1733
by(metis length_0_conv length_list_update)
nipkow@31077
  1734
wenzelm@13114
  1735
lemma list_update_same_conv:
nipkow@24526
  1736
"i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
nipkow@24526
  1737
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1738
nipkow@14187
  1739
lemma list_update_append1:
nipkow@24526
  1740
 "i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"
nipkow@24526
  1741
apply (induct xs arbitrary: i, simp)
nipkow@14187
  1742
apply(simp split:nat.split)
nipkow@14187
  1743
done
nipkow@14187
  1744
kleing@15868
  1745
lemma list_update_append:
nipkow@24526
  1746
  "(xs @ ys) [n:= x] = 
kleing@15868
  1747
  (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))"
nipkow@24526
  1748
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1749
nipkow@14402
  1750
lemma list_update_length [simp]:
nipkow@14402
  1751
 "(xs @ x # ys)[length xs := y] = (xs @ y # ys)"
nipkow@14402
  1752
by (induct xs, auto)
nipkow@14402
  1753
nipkow@31264
  1754
lemma map_update: "map f (xs[k:= y]) = (map f xs)[k := f y]"
nipkow@31264
  1755
by(induct xs arbitrary: k)(auto split:nat.splits)
nipkow@31264
  1756
nipkow@31264
  1757
lemma rev_update:
nipkow@31264
  1758
  "k < length xs \<Longrightarrow> rev (xs[k:= y]) = (rev xs)[length xs - k - 1 := y]"
nipkow@31264
  1759
by (induct xs arbitrary: k) (auto simp: list_update_append split:nat.splits)
nipkow@31264
  1760
wenzelm@13114
  1761
lemma update_zip:
nipkow@31080
  1762
  "(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
nipkow@24526
  1763
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split)
nipkow@24526
  1764
nipkow@24526
  1765
lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)"
nipkow@24526
  1766
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1767
wenzelm@13114
  1768
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
nipkow@13145
  1769
by (blast dest!: set_update_subset_insert [THEN subsetD])
wenzelm@13114
  1770
nipkow@24526
  1771
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])"
nipkow@24526
  1772
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1773
nipkow@31077
  1774
lemma list_update_overwrite[simp]:
haftmann@24796
  1775
  "xs [i := x, i := y] = xs [i := y]"
nipkow@31077
  1776
apply (induct xs arbitrary: i) apply simp
nipkow@31077
  1777
apply (case_tac i, simp_all)
haftmann@24796
  1778
done
haftmann@24796
  1779
haftmann@24796
  1780
lemma list_update_swap:
haftmann@24796
  1781
  "i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]"
haftmann@24796
  1782
apply (induct xs arbitrary: i i')
haftmann@24796
  1783
apply simp
haftmann@24796
  1784
apply (case_tac i, case_tac i')
haftmann@24796
  1785
apply auto
haftmann@24796
  1786
apply (case_tac i')
haftmann@24796
  1787
apply auto
haftmann@24796
  1788
done
haftmann@24796
  1789
haftmann@29827
  1790
lemma list_update_code [code]:
haftmann@29827
  1791
  "[][i := y] = []"
haftmann@29827
  1792
  "(x # xs)[0 := y] = y # xs"
haftmann@29827
  1793
  "(x # xs)[Suc i := y] = x # xs[i := y]"
haftmann@29827
  1794
  by simp_all
haftmann@29827
  1795
wenzelm@13114
  1796
haftmann@49948
  1797
subsubsection {* @{const last} and @{const butlast} *}
wenzelm@13114
  1798
wenzelm@13142
  1799
lemma last_snoc [simp]: "last (xs @ [x]) = x"
nipkow@13145
  1800
by (induct xs) auto
wenzelm@13114
  1801
wenzelm@13142
  1802
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
nipkow@13145
  1803
by (induct xs) auto
wenzelm@13114
  1804
nipkow@14302
  1805
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x"
huffman@44921
  1806
  by simp
nipkow@14302
  1807
nipkow@14302
  1808
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs"
huffman@44921
  1809
  by simp
nipkow@14302
  1810
nipkow@14302
  1811
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)"
nipkow@14302
  1812
by (induct xs) (auto)
nipkow@14302
  1813
nipkow@14302
  1814
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs"
nipkow@14302
  1815
by(simp add:last_append)
nipkow@14302
  1816
nipkow@14302
  1817
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"
nipkow@14302
  1818
by(simp add:last_append)
nipkow@14302
  1819
noschinl@45841
  1820
lemma last_tl: "xs = [] \<or> tl xs \<noteq> [] \<Longrightarrow>last (tl xs) = last xs"
noschinl@45841
  1821
by (induct xs) simp_all
noschinl@45841
  1822
noschinl@45841
  1823
lemma butlast_tl: "butlast (tl xs) = tl (butlast xs)"
noschinl@45841
  1824
by (induct xs) simp_all
noschinl@45841
  1825
nipkow@17762
  1826
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"
nipkow@17762
  1827
by(rule rev_exhaust[of xs]) simp_all
nipkow@17762
  1828
nipkow@17762
  1829
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"
nipkow@17762
  1830
by(cases xs) simp_all
nipkow@17762
  1831
nipkow@17765
  1832
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as"
nipkow@17765
  1833
by (induct as) auto
nipkow@17762
  1834
wenzelm@13142
  1835
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
nipkow@13145
  1836
by (induct xs rule: rev_induct) auto
wenzelm@13114
  1837
wenzelm@13114
  1838
lemma butlast_append:
nipkow@24526
  1839
  "butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
nipkow@24526
  1840
by (induct xs arbitrary: ys) auto
wenzelm@13114
  1841
wenzelm@13142
  1842
lemma append_butlast_last_id [simp]:
nipkow@13145
  1843
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
nipkow@13145
  1844
by (induct xs) auto
wenzelm@13114
  1845
wenzelm@13142
  1846
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
nipkow@13145
  1847
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1848
wenzelm@13114
  1849
lemma in_set_butlast_appendI:
nipkow@13145
  1850
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
nipkow@13145
  1851
by (auto dest: in_set_butlastD simp add: butlast_append)
wenzelm@13114
  1852
nipkow@24526
  1853
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs"
nipkow@24526
  1854
apply (induct xs arbitrary: n)
nipkow@17501
  1855
 apply simp
nipkow@17501
  1856
apply (auto split:nat.split)
nipkow@17501
  1857
done
nipkow@17501
  1858
noschinl@45841
  1859
lemma nth_butlast:
noschinl@45841
  1860
  assumes "n < length (butlast xs)" shows "butlast xs ! n = xs ! n"
noschinl@45841
  1861
proof (cases xs)
noschinl@45841
  1862
  case (Cons y ys)
noschinl@45841
  1863
  moreover from assms have "butlast xs ! n = (butlast xs @ [last xs]) ! n"
noschinl@45841
  1864
    by (simp add: nth_append)
noschinl@45841
  1865
  ultimately show ?thesis using append_butlast_last_id by simp
noschinl@45841
  1866
qed simp
noschinl@45841
  1867
huffman@30128
  1868
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"
nipkow@17589
  1869
by(induct xs)(auto simp:neq_Nil_conv)
nipkow@17589
  1870
huffman@30128
  1871
lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs"
huffman@26584
  1872
by (induct xs, simp, case_tac xs, simp_all)
huffman@26584
  1873
nipkow@31077
  1874
lemma last_list_update:
nipkow@31077
  1875
  "xs \<noteq> [] \<Longrightarrow> last(xs[k:=x]) = (if k = size xs - 1 then x else last xs)"
nipkow@31077
  1876
by (auto simp: last_conv_nth)
nipkow@31077
  1877
nipkow@31077
  1878
lemma butlast_list_update:
nipkow@31077
  1879
  "butlast(xs[k:=x]) =
nipkow@31077
  1880
 (if k = size xs - 1 then butlast xs else (butlast xs)[k:=x])"
nipkow@31077
  1881
apply(cases xs rule:rev_cases)
nipkow@31077
  1882
apply simp
nipkow@31077
  1883
apply(simp add:list_update_append split:nat.splits)
nipkow@31077
  1884
done
nipkow@31077
  1885
haftmann@36851
  1886
lemma last_map:
haftmann@36851
  1887
  "xs \<noteq> [] \<Longrightarrow> last (map f xs) = f (last xs)"
haftmann@36851
  1888
  by (cases xs rule: rev_cases) simp_all
haftmann@36851
  1889
haftmann@36851
  1890
lemma map_butlast:
haftmann@36851
  1891
  "map f (butlast xs) = butlast (map f xs)"
haftmann@36851
  1892
  by (induct xs) simp_all
haftmann@36851
  1893
nipkow@40230
  1894
lemma snoc_eq_iff_butlast:
nipkow@40230
  1895
  "xs @ [x] = ys \<longleftrightarrow> (ys \<noteq> [] & butlast ys = xs & last ys = x)"
nipkow@40230
  1896
by (metis append_butlast_last_id append_is_Nil_conv butlast_snoc last_snoc not_Cons_self)
nipkow@40230
  1897
haftmann@24796
  1898
haftmann@49948
  1899
subsubsection {* @{const take} and @{const drop} *}
wenzelm@13114
  1900
wenzelm@13142
  1901
lemma take_0 [simp]: "take 0 xs = []"
nipkow@13145
  1902
by (induct xs) auto
wenzelm@13114
  1903
wenzelm@13142
  1904
lemma drop_0 [simp]: "drop 0 xs = xs"
nipkow@13145
  1905
by (induct xs) auto
wenzelm@13114
  1906
wenzelm@13142
  1907
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
nipkow@13145
  1908
by simp
wenzelm@13114
  1909
wenzelm@13142
  1910
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
nipkow@13145
  1911
by simp
wenzelm@13114
  1912
wenzelm@13142
  1913
declare take_Cons [simp del] and drop_Cons [simp del]
wenzelm@13114
  1914
huffman@30128
  1915
lemma take_1_Cons [simp]: "take 1 (x # xs) = [x]"
huffman@30128
  1916
  unfolding One_nat_def by simp
huffman@30128
  1917
huffman@30128
  1918
lemma drop_1_Cons [simp]: "drop 1 (x # xs) = xs"
huffman@30128
  1919
  unfolding One_nat_def by simp
huffman@30128
  1920
nipkow@15110
  1921
lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)"
nipkow@15110
  1922
by(clarsimp simp add:neq_Nil_conv)
nipkow@15110
  1923
nipkow@14187
  1924
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"
nipkow@14187
  1925
by(cases xs, simp_all)
nipkow@14187
  1926
huffman@26584
  1927
lemma take_tl: "take n (tl xs) = tl (take (Suc n) xs)"
huffman@26584
  1928
by (induct xs arbitrary: n) simp_all
huffman@26584
  1929
nipkow@24526
  1930
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)"
nipkow@24526
  1931
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split)
nipkow@24526
  1932
huffman@26584
  1933
lemma tl_take: "tl (take n xs) = take (n - 1) (tl xs)"
huffman@26584
  1934
by (cases n, simp, cases xs, auto)
huffman@26584
  1935
huffman@26584
  1936
lemma tl_drop: "tl (drop n xs) = drop n (tl xs)"
huffman@26584
  1937
by (simp only: drop_tl)
huffman@26584
  1938
nipkow@24526
  1939
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y"
nipkow@24526
  1940
apply (induct xs arbitrary: n, simp)
nipkow@14187
  1941
apply(simp add:drop_Cons nth_Cons split:nat.splits)
nipkow@14187
  1942
done
nipkow@14187
  1943
nipkow@13913
  1944
lemma take_Suc_conv_app_nth:
nipkow@24526
  1945
  "i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"
nipkow@24526
  1946
apply (induct xs arbitrary: i, simp)
paulson@14208
  1947
apply (case_tac i, auto)
nipkow@13913
  1948
done
nipkow@13913
  1949
mehta@14591
  1950
lemma drop_Suc_conv_tl:
nipkow@24526
  1951
  "i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs"
nipkow@24526
  1952
apply (induct xs arbitrary: i, simp)
mehta@14591
  1953
apply (case_tac i, auto)
mehta@14591
  1954
done
mehta@14591
  1955
nipkow@24526
  1956
lemma length_take [simp]: "length (take n xs) = min (length xs) n"
nipkow@24526
  1957
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1958
nipkow@24526
  1959
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)"
nipkow@24526
  1960
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1961
nipkow@24526
  1962
lemma take_all [simp]: "length xs <= n ==> take n xs = xs"
nipkow@24526
  1963
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1964
nipkow@24526
  1965
lemma drop_all [simp]: "length xs <= n ==> drop n xs = []"
nipkow@24526
  1966
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1967
wenzelm@13142
  1968
lemma take_append [simp]:
nipkow@24526
  1969
  "take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
nipkow@24526
  1970
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1971
wenzelm@13142
  1972
lemma drop_append [simp]:
nipkow@24526
  1973
  "drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
nipkow@24526
  1974
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1975
nipkow@24526
  1976
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs"
nipkow@24526
  1977
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1978
apply (case_tac xs, auto)
nipkow@15236
  1979
apply (case_tac n, auto)
nipkow@13145
  1980
done
wenzelm@13114
  1981
nipkow@24526
  1982
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs"
nipkow@24526
  1983
apply (induct m arbitrary: xs, auto)
paulson@14208
  1984
apply (case_tac xs, auto)
nipkow@13145
  1985
done
wenzelm@13114
  1986
nipkow@24526
  1987
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)"
nipkow@24526
  1988
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1989
apply (case_tac xs, auto)
nipkow@13145
  1990
done
wenzelm@13114
  1991
nipkow@24526
  1992
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)"
nipkow@24526
  1993
apply(induct xs arbitrary: m n)
nipkow@14802
  1994
 apply simp
nipkow@14802
  1995
apply(simp add: take_Cons drop_Cons split:nat.split)
nipkow@14802
  1996
done
nipkow@14802
  1997
nipkow@24526
  1998
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs"
nipkow@24526
  1999
apply (induct n arbitrary: xs, auto)
paulson@14208
  2000
apply (case_tac xs, auto)
nipkow@13145
  2001
done
wenzelm@13114
  2002
nipkow@24526
  2003
lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])"
nipkow@24526
  2004
apply(induct xs arbitrary: n)
nipkow@15110
  2005
 apply simp
nipkow@15110
  2006
apply(simp add:take_Cons split:nat.split)
nipkow@15110
  2007
done
nipkow@15110
  2008
nipkow@24526
  2009
lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)"
nipkow@24526
  2010
apply(induct xs arbitrary: n)
nipkow@15110
  2011
apply simp
nipkow@15110
  2012
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  2013
done
nipkow@15110
  2014
nipkow@24526
  2015
lemma take_map: "take n (map f xs) = map f (take n xs)"
nipkow@24526
  2016
apply (induct n arbitrary: xs, auto)
paulson@14208
  2017
apply (case_tac xs, auto)
nipkow@13145
  2018
done
wenzelm@13114
  2019
nipkow@24526
  2020
lemma drop_map: "drop n (map f xs) = map f (drop n xs)"
nipkow@24526
  2021
apply (induct n arbitrary: xs, auto)
paulson@14208
  2022
apply (case_tac xs, auto)
nipkow@13145
  2023
done
wenzelm@13114
  2024
nipkow@24526
  2025
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)"
nipkow@24526
  2026
apply (induct xs arbitrary: i, auto)
paulson@14208
  2027
apply (case_tac i, auto)
nipkow@13145
  2028
done
wenzelm@13114
  2029
nipkow@24526
  2030
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)"
nipkow@24526
  2031
apply (induct xs arbitrary: i, auto)
paulson@14208
  2032
apply (case_tac i, auto)
nipkow@13145
  2033
done
wenzelm@13114
  2034
nipkow@24526
  2035
lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i"
nipkow@24526
  2036
apply (induct xs arbitrary: i n, auto)
paulson@14208
  2037
apply (case_tac n, blast)
paulson@14208
  2038
apply (case_tac i, auto)
nipkow@13145
  2039
done
wenzelm@13114
  2040
wenzelm@13142
  2041
lemma nth_drop [simp]:
nipkow@24526
  2042
  "n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
nipkow@24526
  2043
apply (induct n arbitrary: xs i, auto)
paulson@14208
  2044
apply (case_tac xs, auto)
nipkow@13145
  2045
done
nipkow@3507
  2046
huffman@26584
  2047
lemma butlast_take:
huffman@30128
  2048
  "n <= length xs ==> butlast (take n xs) = take (n - 1) xs"
huffman@26584
  2049
by (simp add: butlast_conv_take min_max.inf_absorb1 min_max.inf_absorb2)
huffman@26584
  2050
huffman@26584
  2051
lemma butlast_drop: "butlast (drop n xs) = drop n (butlast xs)"
huffman@30128
  2052
by (simp add: butlast_conv_take drop_take add_ac)
huffman@26584
  2053
huffman@26584
  2054
lemma take_butlast: "n < length xs ==> take n (butlast xs) = take n xs"
huffman@26584
  2055
by (simp add: butlast_conv_take min_max.inf_absorb1)
huffman@26584
  2056
huffman@26584
  2057
lemma drop_butlast: "drop n (butlast xs) = butlast (drop n xs)"
huffman@30128
  2058
by (simp add: butlast_conv_take drop_take add_ac)
huffman@26584
  2059
bulwahn@46500
  2060
lemma hd_drop_conv_nth: "n < length xs \<Longrightarrow> hd(drop n xs) = xs!n"
nipkow@18423
  2061
by(simp add: hd_conv_nth)
nipkow@18423
  2062
nipkow@35248
  2063
lemma set_take_subset_set_take:
nipkow@35248
  2064
  "m <= n \<Longrightarrow> set(take m xs) <= set(take n xs)"
bulwahn@41463
  2065
apply (induct xs arbitrary: m n)
bulwahn@41463
  2066
apply simp
bulwahn@41463
  2067
apply (case_tac n)
bulwahn@41463
  2068
apply (auto simp: take_Cons)
bulwahn@41463
  2069
done
nipkow@35248
  2070
nipkow@24526
  2071
lemma set_take_subset: "set(take n xs) \<subseteq> set xs"
nipkow@24526
  2072
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split)
nipkow@24526
  2073
nipkow@24526
  2074
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs"
nipkow@24526
  2075
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split)
nipkow@14025
  2076
nipkow@35248
  2077
lemma set_drop_subset_set_drop:
nipkow@35248
  2078
  "m >= n \<Longrightarrow> set(drop m xs) <= set(drop n xs)"
nipkow@35248
  2079
apply(induct xs arbitrary: m n)
nipkow@35248
  2080
apply(auto simp:drop_Cons split:nat.split)
nipkow@35248
  2081
apply (metis set_drop_subset subset_iff)
nipkow@35248
  2082
done
nipkow@35248
  2083
nipkow@14187
  2084
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  2085
using set_take_subset by fast
nipkow@14187
  2086
nipkow@14187
  2087
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  2088
using set_drop_subset by fast
nipkow@14187
  2089
wenzelm@13114
  2090
lemma append_eq_conv_conj:
nipkow@24526
  2091
  "(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
nipkow@24526
  2092
apply (induct xs arbitrary: zs, simp, clarsimp)
paulson@14208
  2093
apply (case_tac zs, auto)
nipkow@13145
  2094
done
wenzelm@13142
  2095
nipkow@24526
  2096
lemma take_add: 
noschinl@42713
  2097
  "take (i+j) xs = take i xs @ take j (drop i xs)"
nipkow@24526
  2098
apply (induct xs arbitrary: i, auto) 
nipkow@24526
  2099
apply (case_tac i, simp_all)
paulson@14050
  2100
done
paulson@14050
  2101
nipkow@14300
  2102
lemma append_eq_append_conv_if:
nipkow@24526
  2103
 "(xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) =
nipkow@14300
  2104
  (if size xs\<^isub>1 \<le> size ys\<^isub>1
nipkow@14300
  2105
   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
  2106
   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
  2107
apply(induct xs\<^isub>1 arbitrary: ys\<^isub>1)
nipkow@14300
  2108
 apply simp
nipkow@14300
  2109
apply(case_tac ys\<^isub>1)
nipkow@14300
  2110
apply simp_all
nipkow@14300
  2111
done
nipkow@14300
  2112
nipkow@15110
  2113
lemma take_hd_drop:
huffman@30079
  2114
  "n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (Suc n) xs"
nipkow@24526
  2115
apply(induct xs arbitrary: n)
nipkow@15110
  2116
apply simp
nipkow@15110
  2117
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  2118
done
nipkow@15110
  2119
nipkow@17501
  2120
lemma id_take_nth_drop:
nipkow@17501
  2121
 "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" 
nipkow@17501
  2122
proof -
nipkow@17501
  2123
  assume si: "i < length xs"
nipkow@17501
  2124
  hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto
nipkow@17501
  2125
  moreover
nipkow@17501
  2126
  from si have "take (Suc i) xs = take i xs @ [xs!i]"
nipkow@17501
  2127
    apply (rule_tac take_Suc_conv_app_nth) by arith
nipkow@17501
  2128
  ultimately show ?thesis by auto
nipkow@17501
  2129
qed
nipkow@17501
  2130
  
nipkow@17501
  2131
lemma upd_conv_take_nth_drop:
nipkow@17501
  2132
 "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  2133
proof -
nipkow@17501
  2134
  assume i: "i < length xs"
nipkow@17501
  2135
  have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]"
nipkow@17501
  2136
    by(rule arg_cong[OF id_take_nth_drop[OF i]])
nipkow@17501
  2137
  also have "\<dots> = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  2138
    using i by (simp add: list_update_append)
nipkow@17501
  2139
  finally show ?thesis .
nipkow@17501
  2140
qed
nipkow@17501
  2141
haftmann@24796
  2142
lemma nth_drop':
haftmann@24796
  2143
  "i < length xs \<Longrightarrow> xs ! i # drop (Suc i) xs = drop i xs"
haftmann@24796
  2144
apply (induct i arbitrary: xs)
haftmann@24796
  2145
apply (simp add: neq_Nil_conv)
haftmann@24796
  2146
apply (erule exE)+
haftmann@24796
  2147
apply simp
haftmann@24796
  2148
apply (case_tac xs)
haftmann@24796
  2149
apply simp_all
haftmann@24796
  2150
done
haftmann@24796
  2151
wenzelm@13114
  2152
haftmann@49948
  2153
subsubsection {* @{const takeWhile} and @{const dropWhile} *}
wenzelm@13114
  2154
hoelzl@33639
  2155
lemma length_takeWhile_le: "length (takeWhile P xs) \<le> length xs"
hoelzl@33639
  2156
  by (induct xs) auto
hoelzl@33639
  2157
wenzelm@13142
  2158
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
nipkow@13145
  2159
by (induct xs) auto
wenzelm@13114
  2160
wenzelm@13142
  2161
lemma takeWhile_append1 [simp]:
nipkow@13145
  2162
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"
nipkow@13145
  2163
by (induct xs) auto
wenzelm@13114
  2164
wenzelm@13142
  2165
lemma takeWhile_append2 [simp]:
nipkow@13145
  2166
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
nipkow@13145
  2167
by (induct xs) auto
wenzelm@13114
  2168
wenzelm@13142
  2169
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
nipkow@13145
  2170
by (induct xs) auto
wenzelm@13114
  2171
hoelzl@33639
  2172
lemma takeWhile_nth: "j < length (takeWhile P xs) \<Longrightarrow> takeWhile P xs ! j = xs ! j"
hoelzl@33639
  2173
apply (subst (3) takeWhile_dropWhile_id[symmetric]) unfolding nth_append by auto
hoelzl@33639
  2174
hoelzl@33639
  2175
lemma dropWhile_nth: "j < length (dropWhile P xs) \<Longrightarrow> dropWhile P xs ! j = xs ! (j + length (takeWhile P xs))"
hoelzl@33639
  2176
apply (subst (3) takeWhile_dropWhile_id[symmetric]) unfolding nth_append by auto
hoelzl@33639
  2177
hoelzl@33639
  2178
lemma length_dropWhile_le: "length (dropWhile P xs) \<le> length xs"
hoelzl@33639
  2179
by (induct xs) auto
hoelzl@33639
  2180
wenzelm@13142
  2181
lemma dropWhile_append1 [simp]:
nipkow@13145
  2182
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
nipkow@13145
  2183
by (induct xs) auto
wenzelm@13114
  2184
wenzelm@13142
  2185
lemma dropWhile_append2 [simp]:
nipkow@13145
  2186
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
nipkow@13145
  2187
by (induct xs) auto
wenzelm@13114
  2188
noschinl@45841
  2189
lemma dropWhile_append3:
noschinl@45841
  2190
  "\<not> P y \<Longrightarrow>dropWhile P (xs @ y # ys) = dropWhile P xs @ y # ys"
noschinl@45841
  2191
by (induct xs) auto
noschinl@45841
  2192
noschinl@45841
  2193
lemma dropWhile_last:
noschinl@45841
  2194
  "x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> last (dropWhile P xs) = last xs"
noschinl@45841
  2195
by (auto simp add: dropWhile_append3 in_set_conv_decomp)
noschinl@45841
  2196
noschinl@45841
  2197
lemma set_dropWhileD: "x \<in> set (dropWhile P xs) \<Longrightarrow> x \<in> set xs"
noschinl@45841
  2198
by (induct xs) (auto split: split_if_asm)
noschinl@45841
  2199
krauss@23971
  2200
lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
nipkow@13145
  2201
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  2202
nipkow@13913
  2203
lemma takeWhile_eq_all_conv[simp]:
nipkow@13913
  2204
 "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  2205
by(induct xs, auto)
nipkow@13913
  2206
nipkow@13913
  2207
lemma dropWhile_eq_Nil_conv[simp]:
nipkow@13913
  2208
 "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  2209
by(induct xs, auto)
nipkow@13913
  2210
nipkow@13913
  2211
lemma dropWhile_eq_Cons_conv:
nipkow@13913
  2212
 "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"
nipkow@13913
  2213
by(induct xs, auto)
nipkow@13913
  2214
nipkow@31077
  2215
lemma distinct_takeWhile[simp]: "distinct xs ==> distinct (takeWhile P xs)"
nipkow@31077
  2216
by (induct xs) (auto dest: set_takeWhileD)
nipkow@31077
  2217
nipkow@31077
  2218
lemma distinct_dropWhile[simp]: "distinct xs ==> distinct (dropWhile P xs)"
nipkow@31077
  2219
by (induct xs) auto
nipkow@31077
  2220
hoelzl@33639
  2221
lemma takeWhile_map: "takeWhile P (map f xs) = map f (takeWhile (P \<circ> f) xs)"
hoelzl@33639
  2222
by (induct xs) auto
hoelzl@33639
  2223
hoelzl@33639
  2224
lemma dropWhile_map: "dropWhile P (map f xs) = map f (dropWhile (P \<circ> f) xs)"
hoelzl@33639
  2225
by (induct xs) auto
hoelzl@33639
  2226
hoelzl@33639
  2227
lemma takeWhile_eq_take: "takeWhile P xs = take (length (takeWhile P xs)) xs"
hoelzl@33639
  2228
by (induct xs) auto
hoelzl@33639
  2229
hoelzl@33639
  2230
lemma dropWhile_eq_drop: "dropWhile P xs = drop (length (takeWhile P xs)) xs"
hoelzl@33639
  2231
by (induct xs) auto
hoelzl@33639
  2232
hoelzl@33639
  2233
lemma hd_dropWhile:
hoelzl@33639
  2234
  "dropWhile P xs \<noteq> [] \<Longrightarrow> \<not> P (hd (dropWhile P xs))"
hoelzl@33639
  2235
using assms by (induct xs) auto
hoelzl@33639
  2236
hoelzl@33639
  2237
lemma takeWhile_eq_filter:
hoelzl@33639
  2238
  assumes "\<And> x. x \<in> set (dropWhile P xs) \<Longrightarrow> \<not> P x"
hoelzl@33639
  2239
  shows "takeWhile P xs = filter P xs"
hoelzl@33639
  2240
proof -
hoelzl@33639
  2241
  have A: "filter P xs = filter P (takeWhile P xs @ dropWhile P xs)"
hoelzl@33639
  2242
    by simp
hoelzl@33639
  2243
  have B: "filter P (dropWhile P xs) = []"
hoelzl@33639
  2244
    unfolding filter_empty_conv using assms by blast
hoelzl@33639
  2245
  have "filter P xs = takeWhile P xs"
hoelzl@33639
  2246
    unfolding A filter_append B
hoelzl@33639
  2247
    by (auto simp add: filter_id_conv dest: set_takeWhileD)
hoelzl@33639
  2248
  thus ?thesis ..
hoelzl@33639
  2249
qed
hoelzl@33639
  2250
hoelzl@33639
  2251
lemma takeWhile_eq_take_P_nth:
hoelzl@33639
  2252
  "\<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
  2253
  takeWhile P xs = take n xs"
hoelzl@33639
  2254
proof (induct xs arbitrary: n)
hoelzl@33639
  2255
  case (Cons x xs)
hoelzl@33639
  2256
  thus ?case
hoelzl@33639
  2257
  proof (cases n)
hoelzl@33639
  2258
    case (Suc n') note this[simp]
hoelzl@33639
  2259
    have "P x" using Cons.prems(1)[of 0] by simp
hoelzl@33639
  2260
    moreover have "takeWhile P xs = take n' xs"
hoelzl@33639
  2261
    proof (rule Cons.hyps)
hoelzl@33639
  2262
      case goal1 thus "P (xs ! i)" using Cons.prems(1)[of "Suc i"] by simp
hoelzl@33639
  2263
    next case goal2 thus ?case using Cons by auto
hoelzl@33639
  2264
    qed
hoelzl@33639
  2265
    ultimately show ?thesis by simp
hoelzl@33639
  2266
   qed simp
hoelzl@33639
  2267
qed simp
hoelzl@33639
  2268
hoelzl@33639
  2269
lemma nth_length_takeWhile:
hoelzl@33639
  2270
  "length (takeWhile P xs) < length xs \<Longrightarrow> \<not> P (xs ! length (takeWhile P xs))"
hoelzl@33639
  2271
by (induct xs) auto
hoelzl@33639
  2272
hoelzl@33639
  2273
lemma length_takeWhile_less_P_nth:
hoelzl@33639
  2274
  assumes all: "\<And> i. i < j \<Longrightarrow> P (xs ! i)" and "j \<le> length xs"
hoelzl@33639
  2275
  shows "j \<le> length (takeWhile P xs)"
hoelzl@33639
  2276
proof (rule classical)
hoelzl@33639
  2277
  assume "\<not> ?thesis"
hoelzl@33639
  2278
  hence "length (takeWhile P xs) < length xs" using assms by simp
hoelzl@33639
  2279
  thus ?thesis using all `\<not> ?thesis` nth_length_takeWhile[of P xs] by auto
hoelzl@33639
  2280
qed
nipkow@31077
  2281
nipkow@17501
  2282
text{* The following two lemmmas could be generalized to an arbitrary
nipkow@17501
  2283
property. *}
nipkow@17501
  2284
nipkow@17501
  2285
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  2286
 takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))"
nipkow@17501
  2287
by(induct xs) (auto simp: takeWhile_tail[where l="[]"])
nipkow@17501
  2288
nipkow@17501
  2289
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  2290
  dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)"
nipkow@17501
  2291
apply(induct xs)
nipkow@17501
  2292
 apply simp
nipkow@17501
  2293
apply auto
nipkow@17501
  2294
apply(subst dropWhile_append2)
nipkow@17501
  2295
apply auto
nipkow@17501
  2296
done
nipkow@17501
  2297
nipkow@18423
  2298
lemma takeWhile_not_last:
bulwahn@46500
  2299
 "distinct xs \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs"
nipkow@18423
  2300
apply(induct xs)
nipkow@18423
  2301
 apply simp
nipkow@18423
  2302
apply(case_tac xs)
nipkow@18423
  2303
apply(auto)
nipkow@18423
  2304
done
nipkow@18423
  2305
krauss@44013
  2306
lemma takeWhile_cong [fundef_cong]:
krauss@18336
  2307
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  2308
  ==> takeWhile P l = takeWhile Q k"
nipkow@24349
  2309
by (induct k arbitrary: l) (simp_all)
krauss@18336
  2310
krauss@44013
  2311
lemma dropWhile_cong [fundef_cong]:
krauss@18336
  2312
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  2313
  ==> dropWhile P l = dropWhile Q k"
nipkow@24349
  2314
by (induct k arbitrary: l, simp_all)
krauss@18336
  2315
wenzelm@13114
  2316
haftmann@49948
  2317
subsubsection {* @{const zip} *}
wenzelm@13114
  2318
wenzelm@13142
  2319
lemma zip_Nil [simp]: "zip [] ys = []"
nipkow@13145
  2320
by (induct ys) auto
wenzelm@13114
  2321
wenzelm@13142
  2322
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
nipkow@13145
  2323
by simp
wenzelm@13114
  2324
wenzelm@13142
  2325
declare zip_Cons [simp del]
wenzelm@13114
  2326
haftmann@36198
  2327
lemma [code]:
haftmann@36198
  2328
  "zip [] ys = []"
haftmann@36198
  2329
  "zip xs [] = []"
haftmann@36198
  2330
  "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
haftmann@36198
  2331
  by (fact zip_Nil zip.simps(1) zip_Cons_Cons)+
haftmann@36198
  2332
nipkow@15281
  2333
lemma zip_Cons1:
nipkow@15281
  2334
 "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)"
nipkow@15281
  2335
by(auto split:list.split)
nipkow@15281
  2336
wenzelm@13142
  2337
lemma length_zip [simp]:
krauss@22493
  2338
"length (zip xs ys) = min (length xs) (length ys)"
krauss@22493
  2339
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  2340
haftmann@34978
  2341
lemma zip_obtain_same_length:
haftmann@34978
  2342
  assumes "\<And>zs ws n. length zs = length ws \<Longrightarrow> n = min (length xs) (length ys)
haftmann@34978
  2343
    \<Longrightarrow> zs = take n xs \<Longrightarrow> ws = take n ys \<Longrightarrow> P (zip zs ws)"
haftmann@34978
  2344
  shows "P (zip xs ys)"
haftmann@34978
  2345
proof -
haftmann@34978
  2346
  let ?n = "min (length xs) (length ys)"
haftmann@34978
  2347
  have "P (zip (take ?n xs) (take ?n ys))"
haftmann@34978
  2348
    by (rule assms) simp_all
haftmann@34978
  2349
  moreover have "zip xs ys = zip (take ?n xs) (take ?n ys)"
haftmann@34978
  2350
  proof (induct xs arbitrary: ys)
haftmann@34978
  2351
    case Nil then show ?case by simp
haftmann@34978
  2352
  next
haftmann@34978
  2353
    case (Cons x xs) then show ?case by (cases ys) simp_all
haftmann@34978
  2354
  qed
haftmann@34978
  2355
  ultimately show ?thesis by simp
haftmann@34978
  2356
qed
haftmann@34978
  2357
wenzelm@13114
  2358
lemma zip_append1:
krauss@22493
  2359
"zip (xs @ ys) zs =
nipkow@13145
  2360
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
krauss@22493
  2361
by (induct xs zs rule:list_induct2') auto
wenzelm@13114
  2362
wenzelm@13114
  2363
lemma zip_append2:
krauss@22493
  2364
"zip xs (ys @ zs) =
nipkow@13145
  2365
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
krauss@22493
  2366
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  2367
wenzelm@13142
  2368
lemma zip_append [simp]:
bulwahn@46500
  2369
 "[| length xs = length us |] ==>
nipkow@13145
  2370
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
nipkow@13145
  2371
by (simp add: zip_append1)
wenzelm@13114
  2372
wenzelm@13114
  2373
lemma zip_rev:
nipkow@14247
  2374
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
nipkow@14247
  2375
by (induct rule:list_induct2, simp_all)
wenzelm@13114
  2376
hoelzl@33639
  2377
lemma zip_map_map:
hoelzl@33639
  2378
  "zip (map f xs) (map g ys) = map (\<lambda> (x, y). (f x, g y)) (zip xs ys)"
hoelzl@33639
  2379
proof (induct xs arbitrary: ys)
hoelzl@33639
  2380
  case (Cons x xs) note Cons_x_xs = Cons.hyps
hoelzl@33639
  2381
  show ?case
hoelzl@33639
  2382
  proof (cases ys)
hoelzl@33639
  2383
    case (Cons y ys')
hoelzl@33639
  2384
    show ?thesis unfolding Cons using Cons_x_xs by simp
hoelzl@33639
  2385
  qed simp
hoelzl@33639
  2386
qed simp
hoelzl@33639
  2387
hoelzl@33639
  2388
lemma zip_map1:
hoelzl@33639
  2389
  "zip (map f xs) ys = map (\<lambda>(x, y). (f x, y)) (zip xs ys)"
hoelzl@33639
  2390
using zip_map_map[of f xs "\<lambda>x. x" ys] by simp
hoelzl@33639
  2391
hoelzl@33639
  2392
lemma zip_map2:
hoelzl@33639
  2393
  "zip xs (map f ys) = map (\<lambda>(x, y). (x, f y)) (zip xs ys)"
hoelzl@33639
  2394
using zip_map_map[of "\<lambda>x. x" xs f ys] by simp
hoelzl@33639
  2395
nipkow@23096
  2396
lemma map_zip_map:
hoelzl@33639
  2397
  "map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)"
hoelzl@33639
  2398
unfolding zip_map1 by auto
nipkow@23096
  2399
nipkow@23096
  2400
lemma map_zip_map2:
hoelzl@33639
  2401
  "map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)"
hoelzl@33639
  2402
unfolding zip_map2 by auto
nipkow@23096
  2403
nipkow@31080
  2404
text{* Courtesy of Andreas Lochbihler: *}
nipkow@31080
  2405
lemma zip_same_conv_map: "zip xs xs = map (\<lambda>x. (x, x)) xs"
nipkow@31080
  2406
by(induct xs) auto
nipkow@31080
  2407
wenzelm@13142
  2408
lemma nth_zip [simp]:
nipkow@24526
  2409
"[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"
nipkow@24526
  2410
apply (induct ys arbitrary: i xs, simp)
nipkow@13145
  2411
apply (case_tac xs)
nipkow@13145
  2412
 apply (simp_all add: nth.simps split: nat.split)
nipkow@13145
  2413
done
wenzelm@13114
  2414
wenzelm@13114
  2415
lemma set_zip:
nipkow@13145
  2416
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
nipkow@31080
  2417
by(simp add: set_conv_nth cong: rev_conj_cong)
wenzelm@13114
  2418
hoelzl@33639
  2419
lemma zip_same: "((a,b) \<in> set (zip xs xs)) = (a \<in> set xs \<and> a = b)"
hoelzl@33639
  2420
by(induct xs) auto
hoelzl@33639
  2421
wenzelm@13114
  2422
lemma zip_update:
nipkow@31080
  2423
  "zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
nipkow@31080
  2424
by(rule sym, simp add: update_zip)
wenzelm@13114
  2425
wenzelm@13142
  2426
lemma zip_replicate [simp]:
nipkow@24526
  2427
  "zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
nipkow@24526
  2428
apply (induct i arbitrary: j, auto)
paulson@14208
  2429
apply (case_tac j, auto)
nipkow@13145
  2430
done
wenzelm@13114
  2431
nipkow@19487
  2432
lemma take_zip:
nipkow@24526
  2433
  "take n (zip xs ys) = zip (take n xs) (take n ys)"
nipkow@24526
  2434
apply (induct n arbitrary: xs ys)
nipkow@19487
  2435
 apply simp
nipkow@19487
  2436
apply (case_tac xs, simp)
nipkow@19487
  2437
apply (case_tac ys, simp_all)
nipkow@19487
  2438
done
nipkow@19487
  2439
nipkow@19487
  2440
lemma drop_zip:
nipkow@24526
  2441
  "drop n (zip xs ys) = zip (drop n xs) (drop n ys)"
nipkow@24526
  2442
apply (induct n arbitrary: xs ys)
nipkow@19487
  2443
 apply simp
nipkow@19487
  2444
apply (case_tac xs, simp)
nipkow@19487
  2445
apply (case_tac ys, simp_all)
nipkow@19487
  2446
done
nipkow@19487
  2447
hoelzl@33639
  2448
lemma zip_takeWhile_fst: "zip (takeWhile P xs) ys = takeWhile (P \<circ> fst) (zip xs ys)"
hoelzl@33639
  2449
proof (induct xs arbitrary: ys)
hoelzl@33639
  2450
  case (Cons x xs) thus ?case by (cases ys) auto
hoelzl@33639
  2451
qed simp
hoelzl@33639
  2452
hoelzl@33639
  2453
lemma zip_takeWhile_snd: "zip xs (takeWhile P ys) = takeWhile (P \<circ> snd) (zip xs ys)"
hoelzl@33639
  2454
proof (induct xs arbitrary: ys)
hoelzl@33639
  2455
  case (Cons x xs) thus ?case by (cases ys) auto
hoelzl@33639
  2456
qed simp
hoelzl@33639
  2457
krauss@22493
  2458
lemma set_zip_leftD:
krauss@22493
  2459
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs"
krauss@22493
  2460
by (induct xs ys rule:list_induct2') auto
krauss@22493
  2461
krauss@22493
  2462
lemma set_zip_rightD:
krauss@22493
  2463
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys"
krauss@22493
  2464
by (induct xs ys rule:list_induct2') auto
wenzelm@13142
  2465
nipkow@23983
  2466
lemma in_set_zipE:
nipkow@23983
  2467
  "(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23983
  2468
by(blast dest: set_zip_leftD set_zip_rightD)
nipkow@23983
  2469
haftmann@29829
  2470
lemma zip_map_fst_snd:
haftmann@29829
  2471
  "zip (map fst zs) (map snd zs) = zs"
haftmann@29829
  2472
  by (induct zs) simp_all
haftmann@29829
  2473
haftmann@29829
  2474
lemma zip_eq_conv:
haftmann@29829
  2475
  "length xs = length ys \<Longrightarrow> zip xs ys = zs \<longleftrightarrow> map fst zs = xs \<and> map snd zs = ys"
haftmann@29829
  2476
  by (auto simp add: zip_map_fst_snd)
haftmann@29829
  2477
wenzelm@35115
  2478
haftmann@49948
  2479
subsubsection {* @{const list_all2} *}
wenzelm@13114
  2480
kleing@14316
  2481
lemma list_all2_lengthD [intro?]: 
kleing@14316
  2482
  "list_all2 P xs ys ==> length xs = length ys"
nipkow@24349
  2483
by (simp add: list_all2_def)
haftmann@19607
  2484
haftmann@19787
  2485
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])"
nipkow@24349
  2486
by (simp add: list_all2_def)
haftmann@19607
  2487
haftmann@19787
  2488
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])"
nipkow@24349
  2489
by (simp add: list_all2_def)
haftmann@19607
  2490
haftmann@19607
  2491
lemma list_all2_Cons [iff, code]:
haftmann@19607
  2492
  "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
nipkow@24349
  2493
by (auto simp add: list_all2_def)
wenzelm@13114
  2494
wenzelm@13114
  2495
lemma list_all2_Cons1:
nipkow@13145
  2496
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
nipkow@13145
  2497
by (cases ys) auto
wenzelm@13114
  2498
wenzelm@13114
  2499
lemma list_all2_Cons2:
nipkow@13145
  2500
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
nipkow@13145
  2501
by (cases xs) auto
wenzelm@13114
  2502
huffman@45794
  2503
lemma list_all2_induct
huffman@45794
  2504
  [consumes 1, case_names Nil Cons, induct set: list_all2]:
huffman@45794
  2505
  assumes P: "list_all2 P xs ys"
huffman@45794
  2506
  assumes Nil: "R [] []"
huffman@47640
  2507
  assumes Cons: "\<And>x xs y ys.
huffman@47640
  2508
    \<lbrakk>P x y; list_all2 P xs ys; R xs ys\<rbrakk> \<Longrightarrow> R (x # xs) (y # ys)"
huffman@45794
  2509
  shows "R xs ys"
huffman@45794
  2510
using P
huffman@45794
  2511
by (induct xs arbitrary: ys) (auto simp add: list_all2_Cons1 Nil Cons)
huffman@45794
  2512
wenzelm@13142
  2513
lemma list_all2_rev [iff]:
nipkow@13145
  2514
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
nipkow@13145
  2515
by (simp add: list_all2_def zip_rev cong: conj_cong)
wenzelm@13114
  2516
kleing@13863
  2517
lemma list_all2_rev1:
kleing@13863
  2518
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"
kleing@13863
  2519
by (subst list_all2_rev [symmetric]) simp
kleing@13863
  2520
wenzelm@13114
  2521
lemma list_all2_append1:
nipkow@13145
  2522
"list_all2 P (xs @ ys) zs =
nipkow@13145
  2523
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
nipkow@13145
  2524
list_all2 P xs us \<and> list_all2 P ys vs)"
nipkow@13145
  2525
apply (simp add: list_all2_def zip_append1)
nipkow@13145
  2526
apply (rule iffI)
nipkow@13145
  2527
 apply (rule_tac x = "take (length xs) zs" in exI)
nipkow@13145
  2528
 apply (rule_tac x = "drop (length xs) zs" in exI)
paulson@14208
  2529
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  2530
apply (simp add: ball_Un)
nipkow@13145
  2531
done
wenzelm@13114
  2532
wenzelm@13114
  2533
lemma list_all2_append2:
nipkow@13145
  2534
"list_all2 P xs (ys @ zs) =
nipkow@13145
  2535
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
nipkow@13145
  2536
list_all2 P us ys \<and> list_all2 P vs zs)"
nipkow@13145
  2537
apply (simp add: list_all2_def zip_append2)
nipkow@13145
  2538
apply (rule iffI)
nipkow@13145
  2539
 apply (rule_tac x = "take (length ys) xs" in exI)
nipkow@13145
  2540
 apply (rule_tac x = "drop (length ys) xs" in exI)
paulson@14208
  2541
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  2542
apply (simp add: ball_Un)
nipkow@13145
  2543
done
wenzelm@13114
  2544
kleing@13863
  2545
lemma list_all2_append:
nipkow@14247
  2546
  "length xs = length ys \<Longrightarrow>
nipkow@14247
  2547
  list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"
nipkow@14247
  2548
by (induct rule:list_induct2, simp_all)
kleing@13863
  2549
kleing@13863
  2550
lemma list_all2_appendI [intro?, trans]:
kleing@13863
  2551
  "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"
nipkow@24349
  2552
by (simp add: list_all2_append list_all2_lengthD)
kleing@13863
  2553
wenzelm@13114
  2554
lemma list_all2_conv_all_nth:
nipkow@13145
  2555
"list_all2 P xs ys =
nipkow@13145
  2556
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
nipkow@13145
  2557
by (force simp add: list_all2_def set_zip)
wenzelm@13114
  2558
berghofe@13883
  2559
lemma list_all2_trans:
berghofe@13883
  2560
  assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"
berghofe@13883
  2561
  shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"
berghofe@13883
  2562
        (is "!!bs cs. PROP ?Q as bs cs")
berghofe@13883
  2563
proof (induct as)
berghofe@13883
  2564
  fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"
berghofe@13883
  2565
  show "!!cs. PROP ?Q (x # xs) bs cs"
berghofe@13883
  2566
  proof (induct bs)