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