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