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