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