src/HOL/List.thy
author nipkow
Sun, 28 Nov 2010 15:20:51 +0100
changeset 41030 0a54cfc9add3
parent 40900 7bdfc1d6b143
child 41214 a6fcd305f7dc
permissions -rw-r--r--
gave more standard finite set rules simp and intro attribute
wenzelm@13462
     1
(*  Title:      HOL/List.thy
wenzelm@13462
     2
    Author:     Tobias Nipkow
clasohm@923
     3
*)
clasohm@923
     4
wenzelm@13114
     5
header {* The datatype of finite lists *}
wenzelm@13122
     6
nipkow@15131
     7
theory List
haftmann@40436
     8
imports Plain Presburger Recdef Code_Numeral Quotient ATP
haftmann@31055
     9
uses ("Tools/list_code.ML")
nipkow@15131
    10
begin
clasohm@923
    11
wenzelm@13142
    12
datatype 'a list =
wenzelm@13366
    13
    Nil    ("[]")
wenzelm@13366
    14
  | Cons 'a  "'a list"    (infixr "#" 65)
clasohm@923
    15
clasohm@923
    16
syntax
wenzelm@13366
    17
  -- {* list Enumeration *}
wenzelm@35118
    18
  "_list" :: "args => 'a list"    ("[(_)]")
clasohm@923
    19
haftmann@34928
    20
translations
haftmann@34928
    21
  "[x, xs]" == "x#[xs]"
haftmann@34928
    22
  "[x]" == "x#[]"
haftmann@34928
    23
wenzelm@35118
    24
wenzelm@35118
    25
subsection {* Basic list processing functions *}
haftmann@34928
    26
haftmann@34928
    27
primrec
haftmann@34928
    28
  hd :: "'a list \<Rightarrow> 'a" where
haftmann@34928
    29
  "hd (x # xs) = x"
haftmann@34928
    30
haftmann@34928
    31
primrec
haftmann@34928
    32
  tl :: "'a list \<Rightarrow> 'a list" where
haftmann@34928
    33
    "tl [] = []"
haftmann@34928
    34
  | "tl (x # xs) = xs"
haftmann@34928
    35
haftmann@34928
    36
primrec
haftmann@34928
    37
  last :: "'a list \<Rightarrow> 'a" where
haftmann@34928
    38
  "last (x # xs) = (if xs = [] then x else last xs)"
haftmann@34928
    39
haftmann@34928
    40
primrec
haftmann@34928
    41
  butlast :: "'a list \<Rightarrow> 'a list" where
haftmann@34928
    42
    "butlast []= []"
haftmann@34928
    43
  | "butlast (x # xs) = (if xs = [] then [] else x # butlast xs)"
haftmann@34928
    44
haftmann@34928
    45
primrec
haftmann@34928
    46
  set :: "'a list \<Rightarrow> 'a set" where
haftmann@34928
    47
    "set [] = {}"
haftmann@34928
    48
  | "set (x # xs) = insert x (set xs)"
haftmann@34928
    49
haftmann@34928
    50
primrec
haftmann@34928
    51
  map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list" where
haftmann@34928
    52
    "map f [] = []"
haftmann@34928
    53
  | "map f (x # xs) = f x # map f xs"
haftmann@34928
    54
haftmann@34928
    55
primrec
haftmann@34928
    56
  append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65) where
haftmann@34928
    57
    append_Nil:"[] @ ys = ys"
haftmann@34928
    58
  | append_Cons: "(x#xs) @ ys = x # xs @ ys"
haftmann@34928
    59
haftmann@34928
    60
primrec
haftmann@34928
    61
  rev :: "'a list \<Rightarrow> 'a list" where
haftmann@34928
    62
    "rev [] = []"
haftmann@34928
    63
  | "rev (x # xs) = rev xs @ [x]"
haftmann@34928
    64
haftmann@34928
    65
primrec
haftmann@34928
    66
  filter:: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34928
    67
    "filter P [] = []"
haftmann@34928
    68
  | "filter P (x # xs) = (if P x then x # filter P xs else filter P xs)"
haftmann@34928
    69
haftmann@34928
    70
syntax
wenzelm@13366
    71
  -- {* Special syntax for filter *}
wenzelm@35118
    72
  "_filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_<-_./ _])")
clasohm@923
    73
haftmann@34928
    74
translations
haftmann@34928
    75
  "[x<-xs . P]"== "CONST filter (%x. P) xs"
haftmann@34928
    76
haftmann@34928
    77
syntax (xsymbols)
wenzelm@35118
    78
  "_filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
haftmann@34928
    79
syntax (HTML output)
wenzelm@35118
    80
  "_filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
haftmann@34928
    81
haftmann@34928
    82
primrec
haftmann@34928
    83
  foldl :: "('b \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b" where
haftmann@34928
    84
    foldl_Nil: "foldl f a [] = a"
haftmann@34928
    85
  | foldl_Cons: "foldl f a (x # xs) = foldl f (f a x) xs"
haftmann@34928
    86
haftmann@34928
    87
primrec
haftmann@34928
    88
  foldr :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
haftmann@34928
    89
    "foldr f [] a = a"
haftmann@34928
    90
  | "foldr f (x # xs) a = f x (foldr f xs a)"
haftmann@34928
    91
haftmann@34928
    92
primrec
haftmann@34928
    93
  concat:: "'a list list \<Rightarrow> 'a list" where
haftmann@34928
    94
    "concat [] = []"
haftmann@34928
    95
  | "concat (x # xs) = x @ concat xs"
haftmann@34928
    96
haftmann@40007
    97
definition (in monoid_add)
haftmann@34928
    98
  listsum :: "'a list \<Rightarrow> 'a" where
haftmann@40007
    99
  "listsum xs = foldr plus xs 0"
haftmann@34928
   100
haftmann@34928
   101
primrec
haftmann@34928
   102
  drop:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34928
   103
    drop_Nil: "drop n [] = []"
haftmann@34928
   104
  | drop_Cons: "drop n (x # xs) = (case n of 0 \<Rightarrow> x # xs | Suc m \<Rightarrow> drop m xs)"
haftmann@34928
   105
  -- {*Warning: simpset does not contain this definition, but separate
haftmann@34928
   106
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
haftmann@34928
   107
haftmann@34928
   108
primrec
haftmann@34928
   109
  take:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34928
   110
    take_Nil:"take n [] = []"
haftmann@34928
   111
  | take_Cons: "take n (x # xs) = (case n of 0 \<Rightarrow> [] | Suc m \<Rightarrow> x # take m xs)"
haftmann@34928
   112
  -- {*Warning: simpset does not contain this definition, but separate
haftmann@34928
   113
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
haftmann@34928
   114
haftmann@34928
   115
primrec
haftmann@34928
   116
  nth :: "'a list => nat => 'a" (infixl "!" 100) where
haftmann@34928
   117
  nth_Cons: "(x # xs) ! n = (case n of 0 \<Rightarrow> x | Suc k \<Rightarrow> xs ! k)"
haftmann@34928
   118
  -- {*Warning: simpset does not contain this definition, but separate
haftmann@34928
   119
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
haftmann@34928
   120
haftmann@34928
   121
primrec
haftmann@34928
   122
  list_update :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a list" where
haftmann@34928
   123
    "list_update [] i v = []"
haftmann@34928
   124
  | "list_update (x # xs) i v = (case i of 0 \<Rightarrow> v # xs | Suc j \<Rightarrow> x # list_update xs j v)"
haftmann@34928
   125
haftmann@34928
   126
nonterminals lupdbinds lupdbind
haftmann@34928
   127
haftmann@34928
   128
syntax
wenzelm@13366
   129
  "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")
wenzelm@13366
   130
  "" :: "lupdbind => lupdbinds"    ("_")
wenzelm@13366
   131
  "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")
wenzelm@13366
   132
  "_LUpdate" :: "['a, lupdbinds] => 'a"    ("_/[(_)]" [900,0] 900)
nipkow@5077
   133
clasohm@923
   134
translations
wenzelm@35118
   135
  "_LUpdate xs (_lupdbinds b bs)" == "_LUpdate (_LUpdate xs b) bs"
haftmann@34928
   136
  "xs[i:=x]" == "CONST list_update xs i x"
haftmann@34928
   137
haftmann@34928
   138
primrec
haftmann@34928
   139
  takeWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34928
   140
    "takeWhile P [] = []"
haftmann@34928
   141
  | "takeWhile P (x # xs) = (if P x then x # takeWhile P xs else [])"
haftmann@34928
   142
haftmann@34928
   143
primrec
haftmann@34928
   144
  dropWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@34928
   145
    "dropWhile P [] = []"
haftmann@34928
   146
  | "dropWhile P (x # xs) = (if P x then dropWhile P xs else x # xs)"
haftmann@34928
   147
haftmann@34928
   148
primrec
haftmann@34928
   149
  zip :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
haftmann@34928
   150
    "zip xs [] = []"
haftmann@34928
   151
  | zip_Cons: "zip xs (y # ys) = (case xs of [] => [] | z # zs => (z, y) # zip zs ys)"
haftmann@34928
   152
  -- {*Warning: simpset does not contain this definition, but separate
haftmann@34928
   153
       theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
haftmann@34928
   154
haftmann@34928
   155
primrec 
haftmann@34928
   156
  upt :: "nat \<Rightarrow> nat \<Rightarrow> nat list" ("(1[_..</_'])") where
haftmann@34928
   157
    upt_0: "[i..<0] = []"
haftmann@34928
   158
  | upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])"
haftmann@34928
   159
haftmann@40096
   160
definition
haftmann@40096
   161
  insert :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@40096
   162
  "insert x xs = (if x \<in> set xs then xs else x # xs)"
haftmann@40096
   163
haftmann@40096
   164
hide_const (open) insert
haftmann@40096
   165
hide_fact (open) insert_def
haftmann@40096
   166
haftmann@34928
   167
primrec
haftmann@40096
   168
  remove1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@40096
   169
    "remove1 x [] = []"
haftmann@40096
   170
  | "remove1 x (y # xs) = (if x = y then xs else y # remove1 x xs)"
haftmann@40096
   171
haftmann@40096
   172
primrec
haftmann@40096
   173
  removeAll :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@40096
   174
    "removeAll x [] = []"
haftmann@40096
   175
  | "removeAll x (y # xs) = (if x = y then removeAll x xs else y # removeAll x xs)"
haftmann@40096
   176
haftmann@40366
   177
primrec
haftmann@34928
   178
  distinct :: "'a list \<Rightarrow> bool" where
haftmann@40366
   179
    "distinct [] \<longleftrightarrow> True"
haftmann@40366
   180
  | "distinct (x # xs) \<longleftrightarrow> x \<notin> set xs \<and> distinct xs"
haftmann@34928
   181
haftmann@34928
   182
primrec
haftmann@34928
   183
  remdups :: "'a list \<Rightarrow> 'a list" where
haftmann@34928
   184
    "remdups [] = []"
haftmann@34928
   185
  | "remdups (x # xs) = (if x \<in> set xs then remdups xs else x # remdups xs)"
haftmann@34928
   186
haftmann@34928
   187
primrec
haftmann@34928
   188
  replicate :: "nat \<Rightarrow> 'a \<Rightarrow> 'a list" where
haftmann@34928
   189
    replicate_0: "replicate 0 x = []"
haftmann@34928
   190
  | replicate_Suc: "replicate (Suc n) x = x # replicate n x"
wenzelm@2262
   191
wenzelm@13142
   192
text {*
wenzelm@14589
   193
  Function @{text size} is overloaded for all datatypes. Users may
wenzelm@13366
   194
  refer to the list version as @{text length}. *}
paulson@3342
   195
wenzelm@19363
   196
abbreviation
haftmann@34928
   197
  length :: "'a list \<Rightarrow> nat" where
haftmann@34928
   198
  "length \<equiv> size"
paulson@15307
   199
haftmann@21061
   200
definition
wenzelm@21404
   201
  rotate1 :: "'a list \<Rightarrow> 'a list" where
wenzelm@21404
   202
  "rotate1 xs = (case xs of [] \<Rightarrow> [] | x#xs \<Rightarrow> xs @ [x])"
wenzelm@21404
   203
wenzelm@21404
   204
definition
wenzelm@21404
   205
  rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@30971
   206
  "rotate n = rotate1 ^^ n"
wenzelm@21404
   207
wenzelm@21404
   208
definition
wenzelm@21404
   209
  list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" where
haftmann@37767
   210
  "list_all2 P xs ys =
haftmann@21061
   211
    (length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y))"
wenzelm@21404
   212
wenzelm@21404
   213
definition
wenzelm@21404
   214
  sublist :: "'a list => nat set => 'a list" where
wenzelm@21404
   215
  "sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))"
nipkow@5281
   216
nipkow@40841
   217
fun splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@40841
   218
"splice [] ys = ys" |
nipkow@40841
   219
"splice xs [] = xs" |
nipkow@40841
   220
"splice (x#xs) (y#ys) = x # y # splice xs ys"
haftmann@21061
   221
nipkow@26771
   222
text{*
nipkow@26771
   223
\begin{figure}[htbp]
nipkow@26771
   224
\fbox{
nipkow@26771
   225
\begin{tabular}{l}
wenzelm@27381
   226
@{lemma "[a,b]@[c,d] = [a,b,c,d]" by simp}\\
wenzelm@27381
   227
@{lemma "length [a,b,c] = 3" by simp}\\
wenzelm@27381
   228
@{lemma "set [a,b,c] = {a,b,c}" by simp}\\
wenzelm@27381
   229
@{lemma "map f [a,b,c] = [f a, f b, f c]" by simp}\\
wenzelm@27381
   230
@{lemma "rev [a,b,c] = [c,b,a]" by simp}\\
wenzelm@27381
   231
@{lemma "hd [a,b,c,d] = a" by simp}\\
wenzelm@27381
   232
@{lemma "tl [a,b,c,d] = [b,c,d]" by simp}\\
wenzelm@27381
   233
@{lemma "last [a,b,c,d] = d" by simp}\\
wenzelm@27381
   234
@{lemma "butlast [a,b,c,d] = [a,b,c]" by simp}\\
wenzelm@27381
   235
@{lemma[source] "filter (\<lambda>n::nat. n<2) [0,2,1] = [0,1]" by simp}\\
wenzelm@27381
   236
@{lemma "concat [[a,b],[c,d,e],[],[f]] = [a,b,c,d,e,f]" by simp}\\
wenzelm@27381
   237
@{lemma "foldl f x [a,b,c] = f (f (f x a) b) c" by simp}\\
wenzelm@27381
   238
@{lemma "foldr f [a,b,c] x = f a (f b (f c x))" by simp}\\
wenzelm@27381
   239
@{lemma "zip [a,b,c] [x,y,z] = [(a,x),(b,y),(c,z)]" by simp}\\
wenzelm@27381
   240
@{lemma "zip [a,b] [x,y,z] = [(a,x),(b,y)]" by simp}\\
wenzelm@27381
   241
@{lemma "splice [a,b,c] [x,y,z] = [a,x,b,y,c,z]" by simp}\\
wenzelm@27381
   242
@{lemma "splice [a,b,c,d] [x,y] = [a,x,b,y,c,d]" by simp}\\
wenzelm@27381
   243
@{lemma "take 2 [a,b,c,d] = [a,b]" by simp}\\
wenzelm@27381
   244
@{lemma "take 6 [a,b,c,d] = [a,b,c,d]" by simp}\\
wenzelm@27381
   245
@{lemma "drop 2 [a,b,c,d] = [c,d]" by simp}\\
wenzelm@27381
   246
@{lemma "drop 6 [a,b,c,d] = []" by simp}\\
wenzelm@27381
   247
@{lemma "takeWhile (%n::nat. n<3) [1,2,3,0] = [1,2]" by simp}\\
wenzelm@27381
   248
@{lemma "dropWhile (%n::nat. n<3) [1,2,3,0] = [3,0]" by simp}\\
wenzelm@27381
   249
@{lemma "distinct [2,0,1::nat]" by simp}\\
wenzelm@27381
   250
@{lemma "remdups [2,0,2,1::nat,2] = [0,1,2]" by simp}\\
haftmann@34965
   251
@{lemma "List.insert 2 [0::nat,1,2] = [0,1,2]" by (simp add: List.insert_def)}\\
haftmann@35295
   252
@{lemma "List.insert 3 [0::nat,1,2] = [3,0,1,2]" by (simp add: List.insert_def)}\\
wenzelm@27381
   253
@{lemma "remove1 2 [2,0,2,1::nat,2] = [0,2,1,2]" by simp}\\
nipkow@27693
   254
@{lemma "removeAll 2 [2,0,2,1::nat,2] = [0,1]" by simp}\\
wenzelm@27381
   255
@{lemma "nth [a,b,c,d] 2 = c" by simp}\\
wenzelm@27381
   256
@{lemma "[a,b,c,d][2 := x] = [a,b,x,d]" by simp}\\
wenzelm@27381
   257
@{lemma "sublist [a,b,c,d,e] {0,2,3} = [a,c,d]" by (simp add:sublist_def)}\\
wenzelm@27381
   258
@{lemma "rotate1 [a,b,c,d] = [b,c,d,a]" by (simp add:rotate1_def)}\\
nipkow@40258
   259
@{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate1_def rotate_def eval_nat_numeral)}\\
nipkow@40258
   260
@{lemma "replicate 4 a = [a,a,a,a]" by (simp add:eval_nat_numeral)}\\
nipkow@40258
   261
@{lemma "[2..<5] = [2,3,4]" by (simp add:eval_nat_numeral)}\\
haftmann@40007
   262
@{lemma "listsum [1,2,3::nat] = 6" by (simp add: listsum_def)}
nipkow@26771
   263
\end{tabular}}
nipkow@26771
   264
\caption{Characteristic examples}
nipkow@26771
   265
\label{fig:Characteristic}
nipkow@26771
   266
\end{figure}
blanchet@29864
   267
Figure~\ref{fig:Characteristic} shows characteristic examples
nipkow@26771
   268
that should give an intuitive understanding of the above functions.
nipkow@26771
   269
*}
nipkow@26771
   270
nipkow@24616
   271
text{* The following simple sort functions are intended for proofs,
nipkow@24616
   272
not for efficient implementations. *}
nipkow@24616
   273
wenzelm@25221
   274
context linorder
wenzelm@25221
   275
begin
wenzelm@25221
   276
haftmann@40096
   277
inductive sorted :: "'a list \<Rightarrow> bool" where
haftmann@40096
   278
  Nil [iff]: "sorted []"
haftmann@40096
   279
| Cons: "\<forall>y\<in>set xs. x \<le> y \<Longrightarrow> sorted xs \<Longrightarrow> sorted (x # xs)"
haftmann@40096
   280
haftmann@40096
   281
lemma sorted_single [iff]:
haftmann@40096
   282
  "sorted [x]"
haftmann@40096
   283
  by (rule sorted.Cons) auto
haftmann@40096
   284
haftmann@40096
   285
lemma sorted_many:
haftmann@40096
   286
  "x \<le> y \<Longrightarrow> sorted (y # zs) \<Longrightarrow> sorted (x # y # zs)"
haftmann@40096
   287
  by (rule sorted.Cons) (cases "y # zs" rule: sorted.cases, auto)
haftmann@40096
   288
haftmann@40096
   289
lemma sorted_many_eq [simp, code]:
haftmann@40096
   290
  "sorted (x # y # zs) \<longleftrightarrow> x \<le> y \<and> sorted (y # zs)"
haftmann@40096
   291
  by (auto intro: sorted_many elim: sorted.cases)
haftmann@40096
   292
haftmann@40096
   293
lemma [code]:
haftmann@40096
   294
  "sorted [] \<longleftrightarrow> True"
haftmann@40096
   295
  "sorted [x] \<longleftrightarrow> True"
haftmann@40096
   296
  by simp_all
nipkow@24697
   297
hoelzl@33639
   298
primrec insort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
hoelzl@33639
   299
"insort_key f x [] = [x]" |
hoelzl@33639
   300
"insort_key f x (y#ys) = (if f x \<le> f y then (x#y#ys) else y#(insort_key f x ys))"
hoelzl@33639
   301
haftmann@35195
   302
definition sort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b list \<Rightarrow> 'b list" where
haftmann@35195
   303
"sort_key f xs = foldr (insort_key f) xs []"
hoelzl@33639
   304
haftmann@40451
   305
definition insort_insert_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
haftmann@40451
   306
  "insort_insert_key f x xs = (if f x \<in> f ` set xs then xs else insort_key f x xs)"
haftmann@40451
   307
hoelzl@33639
   308
abbreviation "sort \<equiv> sort_key (\<lambda>x. x)"
hoelzl@33639
   309
abbreviation "insort \<equiv> insort_key (\<lambda>x. x)"
haftmann@40451
   310
abbreviation "insort_insert \<equiv> insort_insert_key (\<lambda>x. x)"
haftmann@35608
   311
wenzelm@25221
   312
end
wenzelm@25221
   313
nipkow@24616
   314
wenzelm@23388
   315
subsubsection {* List comprehension *}
nipkow@23192
   316
nipkow@24349
   317
text{* Input syntax for Haskell-like list comprehension notation.
nipkow@24349
   318
Typical example: @{text"[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]"},
nipkow@24349
   319
the list of all pairs of distinct elements from @{text xs} and @{text ys}.
nipkow@24349
   320
The syntax is as in Haskell, except that @{text"|"} becomes a dot
nipkow@24349
   321
(like in Isabelle's set comprehension): @{text"[e. x \<leftarrow> xs, \<dots>]"} rather than
nipkow@24349
   322
\verb![e| x <- xs, ...]!.
nipkow@24349
   323
nipkow@24349
   324
The qualifiers after the dot are
nipkow@24349
   325
\begin{description}
nipkow@24349
   326
\item[generators] @{text"p \<leftarrow> xs"},
nipkow@24476
   327
 where @{text p} is a pattern and @{text xs} an expression of list type, or
nipkow@24476
   328
\item[guards] @{text"b"}, where @{text b} is a boolean expression.
nipkow@24476
   329
%\item[local bindings] @ {text"let x = e"}.
nipkow@24349
   330
\end{description}
nipkow@23240
   331
nipkow@24476
   332
Just like in Haskell, list comprehension is just a shorthand. To avoid
nipkow@24476
   333
misunderstandings, the translation into desugared form is not reversed
nipkow@24476
   334
upon output. Note that the translation of @{text"[e. x \<leftarrow> xs]"} is
nipkow@24476
   335
optmized to @{term"map (%x. e) xs"}.
nipkow@23240
   336
nipkow@24349
   337
It is easy to write short list comprehensions which stand for complex
nipkow@24349
   338
expressions. During proofs, they may become unreadable (and
nipkow@24349
   339
mangled). In such cases it can be advisable to introduce separate
nipkow@24349
   340
definitions for the list comprehensions in question.  *}
nipkow@24349
   341
nipkow@23209
   342
(*
nipkow@23240
   343
Proper theorem proving support would be nice. For example, if
nipkow@23192
   344
@{text"set[f x y. x \<leftarrow> xs, y \<leftarrow> ys, P x y]"}
nipkow@23192
   345
produced something like
nipkow@23209
   346
@{term"{z. EX x: set xs. EX y:set ys. P x y \<and> z = f x y}"}.
nipkow@23209
   347
*)
nipkow@23209
   348
nipkow@23240
   349
nonterminals lc_qual lc_quals
nipkow@23192
   350
nipkow@23192
   351
syntax
nipkow@23240
   352
"_listcompr" :: "'a \<Rightarrow> lc_qual \<Rightarrow> lc_quals \<Rightarrow> 'a list"  ("[_ . __")
nipkow@24349
   353
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ <- _")
nipkow@23240
   354
"_lc_test" :: "bool \<Rightarrow> lc_qual" ("_")
nipkow@24476
   355
(*"_lc_let" :: "letbinds => lc_qual"  ("let _")*)
nipkow@23240
   356
"_lc_end" :: "lc_quals" ("]")
nipkow@23240
   357
"_lc_quals" :: "lc_qual \<Rightarrow> lc_quals \<Rightarrow> lc_quals" (", __")
nipkow@24349
   358
"_lc_abs" :: "'a => 'b list => 'b list"
nipkow@23192
   359
nipkow@24476
   360
(* These are easier than ML code but cannot express the optimized
nipkow@24476
   361
   translation of [e. p<-xs]
nipkow@23192
   362
translations
nipkow@24349
   363
"[e. p<-xs]" => "concat(map (_lc_abs p [e]) xs)"
nipkow@23240
   364
"_listcompr e (_lc_gen p xs) (_lc_quals Q Qs)"
nipkow@24349
   365
 => "concat (map (_lc_abs p (_listcompr e Q Qs)) xs)"
nipkow@23240
   366
"[e. P]" => "if P then [e] else []"
nipkow@23240
   367
"_listcompr e (_lc_test P) (_lc_quals Q Qs)"
nipkow@23240
   368
 => "if P then (_listcompr e Q Qs) else []"
nipkow@24349
   369
"_listcompr e (_lc_let b) (_lc_quals Q Qs)"
nipkow@24349
   370
 => "_Let b (_listcompr e Q Qs)"
nipkow@24476
   371
*)
nipkow@23240
   372
nipkow@23279
   373
syntax (xsymbols)
nipkow@24349
   374
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _")
nipkow@23279
   375
syntax (HTML output)
nipkow@24349
   376
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _")
nipkow@24349
   377
nipkow@24349
   378
parse_translation (advanced) {*
nipkow@24349
   379
let
wenzelm@35256
   380
  val NilC = Syntax.const @{const_syntax Nil};
wenzelm@35256
   381
  val ConsC = Syntax.const @{const_syntax Cons};
wenzelm@35256
   382
  val mapC = Syntax.const @{const_syntax map};
wenzelm@35256
   383
  val concatC = Syntax.const @{const_syntax concat};
wenzelm@35256
   384
  val IfC = Syntax.const @{const_syntax If};
wenzelm@35118
   385
nipkow@24476
   386
  fun singl x = ConsC $ x $ NilC;
nipkow@24476
   387
wenzelm@35118
   388
  fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *)
nipkow@24349
   389
    let
wenzelm@29281
   390
      val x = Free (Name.variant (fold Term.add_free_names [p, e] []) "x", dummyT);
nipkow@24476
   391
      val e = if opti then singl e else e;
wenzelm@35118
   392
      val case1 = Syntax.const @{syntax_const "_case1"} $ p $ e;
wenzelm@35256
   393
      val case2 =
wenzelm@35256
   394
        Syntax.const @{syntax_const "_case1"} $
wenzelm@35256
   395
          Syntax.const @{const_syntax dummy_pattern} $ NilC;
wenzelm@35118
   396
      val cs = Syntax.const @{syntax_const "_case2"} $ case1 $ case2;
wenzelm@35118
   397
      val ft = Datatype_Case.case_tr false Datatype.info_of_constr ctxt [x, cs];
nipkow@24349
   398
    in lambda x ft end;
nipkow@24349
   399
wenzelm@35256
   400
  fun abs_tr ctxt (p as Free (s, T)) e opti =
wenzelm@35118
   401
        let
wenzelm@35118
   402
          val thy = ProofContext.theory_of ctxt;
wenzelm@35118
   403
          val s' = Sign.intern_const thy s;
wenzelm@35118
   404
        in
wenzelm@35118
   405
          if Sign.declared_const thy s'
wenzelm@35118
   406
          then (pat_tr ctxt p e opti, false)
wenzelm@35118
   407
          else (lambda p e, true)
nipkow@24349
   408
        end
nipkow@24476
   409
    | abs_tr ctxt p e opti = (pat_tr ctxt p e opti, false);
nipkow@24476
   410
wenzelm@35118
   411
  fun lc_tr ctxt [e, Const (@{syntax_const "_lc_test"}, _) $ b, qs] =
wenzelm@35118
   412
        let
wenzelm@35118
   413
          val res =
wenzelm@35118
   414
            (case qs of
wenzelm@35118
   415
              Const (@{syntax_const "_lc_end"}, _) => singl e
wenzelm@35118
   416
            | Const (@{syntax_const "_lc_quals"}, _) $ q $ qs => lc_tr ctxt [e, q, qs]);
nipkow@24476
   417
        in IfC $ b $ res $ NilC end
wenzelm@35118
   418
    | lc_tr ctxt
wenzelm@35118
   419
          [e, Const (@{syntax_const "_lc_gen"}, _) $ p $ es,
wenzelm@35118
   420
            Const(@{syntax_const "_lc_end"}, _)] =
nipkow@24476
   421
        (case abs_tr ctxt p e true of
wenzelm@35118
   422
          (f, true) => mapC $ f $ es
wenzelm@35118
   423
        | (f, false) => concatC $ (mapC $ f $ es))
wenzelm@35118
   424
    | lc_tr ctxt
wenzelm@35118
   425
          [e, Const (@{syntax_const "_lc_gen"}, _) $ p $ es,
wenzelm@35118
   426
            Const (@{syntax_const "_lc_quals"}, _) $ q $ qs] =
wenzelm@35118
   427
        let val e' = lc_tr ctxt [e, q, qs];
wenzelm@35118
   428
        in concatC $ (mapC $ (fst (abs_tr ctxt p e' false)) $ es) end;
wenzelm@35118
   429
wenzelm@35118
   430
in [(@{syntax_const "_listcompr"}, lc_tr)] end
nipkow@24349
   431
*}
nipkow@23279
   432
nipkow@23240
   433
term "[(x,y,z). b]"
nipkow@24476
   434
term "[(x,y,z). x\<leftarrow>xs]"
nipkow@24476
   435
term "[e x y. x\<leftarrow>xs, y\<leftarrow>ys]"
nipkow@24476
   436
term "[(x,y,z). x<a, x>b]"
nipkow@24476
   437
term "[(x,y,z). x\<leftarrow>xs, x>b]"
nipkow@24476
   438
term "[(x,y,z). x<a, x\<leftarrow>xs]"
nipkow@24349
   439
term "[(x,y). Cons True x \<leftarrow> xs]"
nipkow@24349
   440
term "[(x,y,z). Cons x [] \<leftarrow> xs]"
nipkow@23240
   441
term "[(x,y,z). x<a, x>b, x=d]"
nipkow@23240
   442
term "[(x,y,z). x<a, x>b, y\<leftarrow>ys]"
nipkow@23240
   443
term "[(x,y,z). x<a, x\<leftarrow>xs,y>b]"
nipkow@23240
   444
term "[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]"
nipkow@23240
   445
term "[(x,y,z). x\<leftarrow>xs, x>b, y<a]"
nipkow@23240
   446
term "[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]"
nipkow@23240
   447
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x]"
nipkow@23240
   448
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]"
wenzelm@35118
   449
(*
nipkow@24349
   450
term "[(x,y). x\<leftarrow>xs, let xx = x+x, y\<leftarrow>ys, y \<noteq> xx]"
nipkow@23192
   451
*)
nipkow@23192
   452
wenzelm@35118
   453
haftmann@21061
   454
subsubsection {* @{const Nil} and @{const Cons} *}
haftmann@21061
   455
haftmann@21061
   456
lemma not_Cons_self [simp]:
haftmann@21061
   457
  "xs \<noteq> x # xs"
nipkow@13145
   458
by (induct xs) auto
nipkow@3507
   459
wenzelm@13142
   460
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric]
wenzelm@13114
   461
wenzelm@13142
   462
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
nipkow@13145
   463
by (induct xs) auto
wenzelm@13114
   464
wenzelm@13142
   465
lemma length_induct:
haftmann@21061
   466
  "(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs"
nipkow@17589
   467
by (rule measure_induct [of length]) iprover
wenzelm@13114
   468
haftmann@37289
   469
lemma list_nonempty_induct [consumes 1, case_names single cons]:
haftmann@37289
   470
  assumes "xs \<noteq> []"
haftmann@37289
   471
  assumes single: "\<And>x. P [x]"
haftmann@37289
   472
  assumes cons: "\<And>x xs. xs \<noteq> [] \<Longrightarrow> P xs \<Longrightarrow> P (x # xs)"
haftmann@37289
   473
  shows "P xs"
haftmann@37289
   474
using `xs \<noteq> []` proof (induct xs)
haftmann@37289
   475
  case Nil then show ?case by simp
haftmann@37289
   476
next
haftmann@37289
   477
  case (Cons x xs) show ?case proof (cases xs)
haftmann@37289
   478
    case Nil with single show ?thesis by simp
haftmann@37289
   479
  next
haftmann@37289
   480
    case Cons then have "xs \<noteq> []" by simp
haftmann@37289
   481
    moreover with Cons.hyps have "P xs" .
haftmann@37289
   482
    ultimately show ?thesis by (rule cons)
haftmann@37289
   483
  qed
haftmann@37289
   484
qed
haftmann@37289
   485
wenzelm@13114
   486
haftmann@21061
   487
subsubsection {* @{const length} *}
wenzelm@13114
   488
wenzelm@13142
   489
text {*
haftmann@21061
   490
  Needs to come before @{text "@"} because of theorem @{text
haftmann@21061
   491
  append_eq_append_conv}.
wenzelm@13142
   492
*}
wenzelm@13114
   493
wenzelm@13142
   494
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
nipkow@13145
   495
by (induct xs) auto
wenzelm@13114
   496
wenzelm@13142
   497
lemma length_map [simp]: "length (map f xs) = length xs"
nipkow@13145
   498
by (induct xs) auto
wenzelm@13114
   499
wenzelm@13142
   500
lemma length_rev [simp]: "length (rev xs) = length xs"
nipkow@13145
   501
by (induct xs) auto
wenzelm@13114
   502
wenzelm@13142
   503
lemma length_tl [simp]: "length (tl xs) = length xs - 1"
nipkow@13145
   504
by (cases xs) auto
wenzelm@13142
   505
wenzelm@13142
   506
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
nipkow@13145
   507
by (induct xs) auto
wenzelm@13142
   508
wenzelm@13142
   509
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
nipkow@13145
   510
by (induct xs) auto
wenzelm@13114
   511
nipkow@23479
   512
lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0"
nipkow@23479
   513
by auto
nipkow@23479
   514
wenzelm@13114
   515
lemma length_Suc_conv:
nipkow@13145
   516
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
nipkow@13145
   517
by (induct xs) auto
wenzelm@13114
   518
nipkow@14025
   519
lemma Suc_length_conv:
nipkow@14025
   520
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
paulson@14208
   521
apply (induct xs, simp, simp)
nipkow@14025
   522
apply blast
nipkow@14025
   523
done
nipkow@14025
   524
wenzelm@25221
   525
lemma impossible_Cons: "length xs <= length ys ==> xs = x # ys = False"
wenzelm@25221
   526
  by (induct xs) auto
wenzelm@25221
   527
haftmann@26442
   528
lemma list_induct2 [consumes 1, case_names Nil Cons]:
haftmann@26442
   529
  "length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow>
haftmann@26442
   530
   (\<And>x xs y ys. length xs = length ys \<Longrightarrow> P xs ys \<Longrightarrow> P (x#xs) (y#ys))
haftmann@26442
   531
   \<Longrightarrow> P xs ys"
haftmann@26442
   532
proof (induct xs arbitrary: ys)
haftmann@26442
   533
  case Nil then show ?case by simp
haftmann@26442
   534
next
haftmann@26442
   535
  case (Cons x xs ys) then show ?case by (cases ys) simp_all
haftmann@26442
   536
qed
haftmann@26442
   537
haftmann@26442
   538
lemma list_induct3 [consumes 2, case_names Nil Cons]:
haftmann@26442
   539
  "length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P [] [] [] \<Longrightarrow>
haftmann@26442
   540
   (\<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
   541
   \<Longrightarrow> P xs ys zs"
haftmann@26442
   542
proof (induct xs arbitrary: ys zs)
haftmann@26442
   543
  case Nil then show ?case by simp
haftmann@26442
   544
next
haftmann@26442
   545
  case (Cons x xs ys zs) then show ?case by (cases ys, simp_all)
haftmann@26442
   546
    (cases zs, simp_all)
haftmann@26442
   547
qed
wenzelm@13114
   548
kaliszyk@36148
   549
lemma list_induct4 [consumes 3, case_names Nil Cons]:
kaliszyk@36148
   550
  "length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> length zs = length ws \<Longrightarrow>
kaliszyk@36148
   551
   P [] [] [] [] \<Longrightarrow> (\<And>x xs y ys z zs w ws. length xs = length ys \<Longrightarrow>
kaliszyk@36148
   552
   length ys = length zs \<Longrightarrow> length zs = length ws \<Longrightarrow> P xs ys zs ws \<Longrightarrow>
kaliszyk@36148
   553
   P (x#xs) (y#ys) (z#zs) (w#ws)) \<Longrightarrow> P xs ys zs ws"
kaliszyk@36148
   554
proof (induct xs arbitrary: ys zs ws)
kaliszyk@36148
   555
  case Nil then show ?case by simp
kaliszyk@36148
   556
next
kaliszyk@36148
   557
  case (Cons x xs ys zs ws) then show ?case by ((cases ys, simp_all), (cases zs,simp_all)) (cases ws, simp_all)
kaliszyk@36148
   558
qed
kaliszyk@36148
   559
krauss@22493
   560
lemma list_induct2': 
krauss@22493
   561
  "\<lbrakk> P [] [];
krauss@22493
   562
  \<And>x xs. P (x#xs) [];
krauss@22493
   563
  \<And>y ys. P [] (y#ys);
krauss@22493
   564
   \<And>x xs y ys. P xs ys  \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk>
krauss@22493
   565
 \<Longrightarrow> P xs ys"
krauss@22493
   566
by (induct xs arbitrary: ys) (case_tac x, auto)+
krauss@22493
   567
nipkow@22143
   568
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False"
nipkow@24349
   569
by (rule Eq_FalseI) auto
wenzelm@24037
   570
wenzelm@24037
   571
simproc_setup list_neq ("(xs::'a list) = ys") = {*
nipkow@22143
   572
(*
nipkow@22143
   573
Reduces xs=ys to False if xs and ys cannot be of the same length.
nipkow@22143
   574
This is the case if the atomic sublists of one are a submultiset
nipkow@22143
   575
of those of the other list and there are fewer Cons's in one than the other.
nipkow@22143
   576
*)
wenzelm@24037
   577
wenzelm@24037
   578
let
nipkow@22143
   579
huffman@29793
   580
fun len (Const(@{const_name Nil},_)) acc = acc
huffman@29793
   581
  | len (Const(@{const_name Cons},_) $ _ $ xs) (ts,n) = len xs (ts,n+1)
huffman@29793
   582
  | len (Const(@{const_name append},_) $ xs $ ys) acc = len xs (len ys acc)
huffman@29793
   583
  | len (Const(@{const_name rev},_) $ xs) acc = len xs acc
huffman@29793
   584
  | len (Const(@{const_name map},_) $ _ $ xs) acc = len xs acc
nipkow@22143
   585
  | len t (ts,n) = (t::ts,n);
nipkow@22143
   586
wenzelm@24037
   587
fun list_neq _ ss ct =
nipkow@22143
   588
  let
wenzelm@24037
   589
    val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct;
nipkow@22143
   590
    val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0);
nipkow@22143
   591
    fun prove_neq() =
nipkow@22143
   592
      let
nipkow@22143
   593
        val Type(_,listT::_) = eqT;
haftmann@22994
   594
        val size = HOLogic.size_const listT;
nipkow@22143
   595
        val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs);
nipkow@22143
   596
        val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len);
nipkow@22143
   597
        val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len
haftmann@22633
   598
          (K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1));
haftmann@22633
   599
      in SOME (thm RS @{thm neq_if_length_neq}) end
nipkow@22143
   600
  in
wenzelm@23214
   601
    if m < n andalso submultiset (op aconv) (ls,rs) orelse
wenzelm@23214
   602
       n < m andalso submultiset (op aconv) (rs,ls)
nipkow@22143
   603
    then prove_neq() else NONE
nipkow@22143
   604
  end;
wenzelm@24037
   605
in list_neq end;
nipkow@22143
   606
*}
nipkow@22143
   607
nipkow@22143
   608
nipkow@15392
   609
subsubsection {* @{text "@"} -- append *}
wenzelm@13114
   610
wenzelm@13142
   611
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
nipkow@13145
   612
by (induct xs) auto
wenzelm@13114
   613
wenzelm@13142
   614
lemma append_Nil2 [simp]: "xs @ [] = xs"
nipkow@13145
   615
by (induct xs) auto
wenzelm@13114
   616
wenzelm@13142
   617
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
nipkow@13145
   618
by (induct xs) auto
wenzelm@13114
   619
wenzelm@13142
   620
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
nipkow@13145
   621
by (induct xs) auto
wenzelm@13114
   622
wenzelm@13142
   623
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
nipkow@13145
   624
by (induct xs) auto
wenzelm@13114
   625
wenzelm@13142
   626
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
nipkow@13145
   627
by (induct xs) auto
wenzelm@13114
   628
blanchet@35828
   629
lemma append_eq_append_conv [simp, no_atp]:
nipkow@24526
   630
 "length xs = length ys \<or> length us = length vs
berghofe@13883
   631
 ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
nipkow@24526
   632
apply (induct xs arbitrary: ys)
paulson@14208
   633
 apply (case_tac ys, simp, force)
paulson@14208
   634
apply (case_tac ys, force, simp)
nipkow@13145
   635
done
wenzelm@13114
   636
nipkow@24526
   637
lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) =
nipkow@24526
   638
  (EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)"
nipkow@24526
   639
apply (induct xs arbitrary: ys zs ts)
nipkow@14495
   640
 apply fastsimp
nipkow@14495
   641
apply(case_tac zs)
nipkow@14495
   642
 apply simp
nipkow@14495
   643
apply fastsimp
nipkow@14495
   644
done
nipkow@14495
   645
berghofe@34910
   646
lemma same_append_eq [iff, induct_simp]: "(xs @ ys = xs @ zs) = (ys = zs)"
nipkow@13145
   647
by simp
wenzelm@13114
   648
wenzelm@13142
   649
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
nipkow@13145
   650
by simp
wenzelm@13114
   651
berghofe@34910
   652
lemma append_same_eq [iff, induct_simp]: "(ys @ xs = zs @ xs) = (ys = zs)"
nipkow@13145
   653
by simp
wenzelm@13114
   654
wenzelm@13142
   655
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
nipkow@13145
   656
using append_same_eq [of _ _ "[]"] by auto
wenzelm@13114
   657
wenzelm@13142
   658
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
nipkow@13145
   659
using append_same_eq [of "[]"] by auto
wenzelm@13114
   660
blanchet@35828
   661
lemma hd_Cons_tl [simp,no_atp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
nipkow@13145
   662
by (induct xs) auto
wenzelm@13114
   663
wenzelm@13142
   664
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
nipkow@13145
   665
by (induct xs) auto
wenzelm@13114
   666
wenzelm@13142
   667
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
nipkow@13145
   668
by (simp add: hd_append split: list.split)
wenzelm@13114
   669
wenzelm@13142
   670
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
nipkow@13145
   671
by (simp split: list.split)
wenzelm@13114
   672
wenzelm@13142
   673
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
nipkow@13145
   674
by (simp add: tl_append split: list.split)
wenzelm@13114
   675
wenzelm@13142
   676
nipkow@14300
   677
lemma Cons_eq_append_conv: "x#xs = ys@zs =
nipkow@14300
   678
 (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))"
nipkow@14300
   679
by(cases ys) auto
nipkow@14300
   680
nipkow@15281
   681
lemma append_eq_Cons_conv: "(ys@zs = x#xs) =
nipkow@15281
   682
 (ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))"
nipkow@15281
   683
by(cases ys) auto
nipkow@15281
   684
nipkow@14300
   685
wenzelm@13142
   686
text {* Trivial rules for solving @{text "@"}-equations automatically. *}
wenzelm@13114
   687
wenzelm@13114
   688
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
nipkow@13145
   689
by simp
wenzelm@13114
   690
wenzelm@13142
   691
lemma Cons_eq_appendI:
nipkow@13145
   692
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
nipkow@13145
   693
by (drule sym) simp
wenzelm@13114
   694
wenzelm@13142
   695
lemma append_eq_appendI:
nipkow@13145
   696
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
nipkow@13145
   697
by (drule sym) simp
wenzelm@13114
   698
wenzelm@13114
   699
wenzelm@13142
   700
text {*
nipkow@13145
   701
Simplification procedure for all list equalities.
nipkow@13145
   702
Currently only tries to rearrange @{text "@"} to see if
nipkow@13145
   703
- both lists end in a singleton list,
nipkow@13145
   704
- or both lists end in the same list.
wenzelm@13142
   705
*}
wenzelm@13142
   706
wenzelm@26480
   707
ML {*
nipkow@3507
   708
local
nipkow@3507
   709
huffman@29793
   710
fun last (cons as Const(@{const_name Cons},_) $ _ $ xs) =
huffman@29793
   711
  (case xs of Const(@{const_name Nil},_) => cons | _ => last xs)
huffman@29793
   712
  | last (Const(@{const_name append},_) $ _ $ ys) = last ys
wenzelm@13462
   713
  | last t = t;
nipkow@3507
   714
huffman@29793
   715
fun list1 (Const(@{const_name Cons},_) $ _ $ Const(@{const_name Nil},_)) = true
wenzelm@13462
   716
  | list1 _ = false;
wenzelm@13114
   717
huffman@29793
   718
fun butlast ((cons as Const(@{const_name Cons},_) $ x) $ xs) =
huffman@29793
   719
  (case xs of Const(@{const_name Nil},_) => xs | _ => cons $ butlast xs)
huffman@29793
   720
  | butlast ((app as Const(@{const_name append},_) $ xs) $ ys) = app $ butlast ys
huffman@29793
   721
  | butlast xs = Const(@{const_name Nil},fastype_of xs);
wenzelm@13114
   722
haftmann@22633
   723
val rearr_ss = HOL_basic_ss addsimps [@{thm append_assoc},
haftmann@22633
   724
  @{thm append_Nil}, @{thm append_Cons}];
wenzelm@16973
   725
wenzelm@20044
   726
fun list_eq ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
wenzelm@13462
   727
  let
wenzelm@13462
   728
    val lastl = last lhs and lastr = last rhs;
wenzelm@13462
   729
    fun rearr conv =
wenzelm@13462
   730
      let
wenzelm@13462
   731
        val lhs1 = butlast lhs and rhs1 = butlast rhs;
wenzelm@13462
   732
        val Type(_,listT::_) = eqT
wenzelm@13462
   733
        val appT = [listT,listT] ---> listT
huffman@29793
   734
        val app = Const(@{const_name append},appT)
wenzelm@13462
   735
        val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
wenzelm@13480
   736
        val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2));
wenzelm@20044
   737
        val thm = Goal.prove (Simplifier.the_context ss) [] [] eq
wenzelm@17877
   738
          (K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1));
skalberg@15531
   739
      in SOME ((conv RS (thm RS trans)) RS eq_reflection) end;
wenzelm@13114
   740
wenzelm@13462
   741
  in
haftmann@22633
   742
    if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv}
haftmann@22633
   743
    else if lastl aconv lastr then rearr @{thm append_same_eq}
skalberg@15531
   744
    else NONE
wenzelm@13462
   745
  end;
wenzelm@13462
   746
nipkow@3507
   747
in
wenzelm@13462
   748
wenzelm@13462
   749
val list_eq_simproc =
wenzelm@38963
   750
  Simplifier.simproc_global @{theory} "list_eq" ["(xs::'a list) = ys"] (K list_eq);
wenzelm@13462
   751
wenzelm@13114
   752
end;
nipkow@3507
   753
wenzelm@13114
   754
Addsimprocs [list_eq_simproc];
wenzelm@13114
   755
*}
wenzelm@13114
   756
wenzelm@13114
   757
nipkow@15392
   758
subsubsection {* @{text map} *}
wenzelm@13114
   759
haftmann@40451
   760
lemma hd_map:
haftmann@40451
   761
  "xs \<noteq> [] \<Longrightarrow> hd (map f xs) = f (hd xs)"
haftmann@40451
   762
  by (cases xs) simp_all
haftmann@40451
   763
haftmann@40451
   764
lemma map_tl:
haftmann@40451
   765
  "map f (tl xs) = tl (map f xs)"
haftmann@40451
   766
  by (cases xs) simp_all
haftmann@40451
   767
wenzelm@13142
   768
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
nipkow@13145
   769
by (induct xs) simp_all
wenzelm@13114
   770
wenzelm@13142
   771
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
nipkow@13145
   772
by (rule ext, induct_tac xs) auto
wenzelm@13114
   773
wenzelm@13142
   774
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
nipkow@13145
   775
by (induct xs) auto
wenzelm@13114
   776
hoelzl@33639
   777
lemma map_map [simp]: "map f (map g xs) = map (f \<circ> g) xs"
hoelzl@33639
   778
by (induct xs) auto
hoelzl@33639
   779
nipkow@35200
   780
lemma map_comp_map[simp]: "((map f) o (map g)) = map(f o g)"
nipkow@35200
   781
apply(rule ext)
nipkow@35200
   782
apply(simp)
nipkow@35200
   783
done
nipkow@35200
   784
wenzelm@13142
   785
lemma rev_map: "rev (map f xs) = map f (rev xs)"
nipkow@13145
   786
by (induct xs) auto
wenzelm@13114
   787
nipkow@13737
   788
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)"
nipkow@13737
   789
by (induct xs) auto
nipkow@13737
   790
krauss@19770
   791
lemma map_cong [fundef_cong, recdef_cong]:
haftmann@40366
   792
  "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g ys"
haftmann@40366
   793
  by simp
wenzelm@13114
   794
wenzelm@13142
   795
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
nipkow@13145
   796
by (cases xs) auto
wenzelm@13114
   797
wenzelm@13142
   798
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
nipkow@13145
   799
by (cases xs) auto
wenzelm@13114
   800
paulson@18447
   801
lemma map_eq_Cons_conv:
nipkow@14025
   802
 "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)"
nipkow@13145
   803
by (cases xs) auto
wenzelm@13114
   804
paulson@18447
   805
lemma Cons_eq_map_conv:
nipkow@14025
   806
 "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)"
nipkow@14025
   807
by (cases ys) auto
nipkow@14025
   808
paulson@18447
   809
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1]
paulson@18447
   810
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1]
paulson@18447
   811
declare map_eq_Cons_D [dest!]  Cons_eq_map_D [dest!]
paulson@18447
   812
nipkow@14111
   813
lemma ex_map_conv:
nipkow@14111
   814
  "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)"
paulson@18447
   815
by(induct ys, auto simp add: Cons_eq_map_conv)
nipkow@14111
   816
nipkow@15110
   817
lemma map_eq_imp_length_eq:
paulson@35496
   818
  assumes "map f xs = map g ys"
haftmann@26734
   819
  shows "length xs = length ys"
haftmann@26734
   820
using assms proof (induct ys arbitrary: xs)
haftmann@26734
   821
  case Nil then show ?case by simp
haftmann@26734
   822
next
haftmann@26734
   823
  case (Cons y ys) then obtain z zs where xs: "xs = z # zs" by auto
paulson@35496
   824
  from Cons xs have "map f zs = map g ys" by simp
haftmann@26734
   825
  moreover with Cons have "length zs = length ys" by blast
haftmann@26734
   826
  with xs show ?case by simp
haftmann@26734
   827
qed
haftmann@26734
   828
  
nipkow@15110
   829
lemma map_inj_on:
nipkow@15110
   830
 "[| map f xs = map f ys; inj_on f (set xs Un set ys) |]
nipkow@15110
   831
  ==> xs = ys"
nipkow@15110
   832
apply(frule map_eq_imp_length_eq)
nipkow@15110
   833
apply(rotate_tac -1)
nipkow@15110
   834
apply(induct rule:list_induct2)
nipkow@15110
   835
 apply simp
nipkow@15110
   836
apply(simp)
nipkow@15110
   837
apply (blast intro:sym)
nipkow@15110
   838
done
nipkow@15110
   839
nipkow@15110
   840
lemma inj_on_map_eq_map:
nipkow@15110
   841
 "inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@15110
   842
by(blast dest:map_inj_on)
nipkow@15110
   843
wenzelm@13114
   844
lemma map_injective:
nipkow@24526
   845
 "map f xs = map f ys ==> inj f ==> xs = ys"
nipkow@24526
   846
by (induct ys arbitrary: xs) (auto dest!:injD)
wenzelm@13114
   847
nipkow@14339
   848
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@14339
   849
by(blast dest:map_injective)
nipkow@14339
   850
wenzelm@13114
   851
lemma inj_mapI: "inj f ==> inj (map f)"
nipkow@17589
   852
by (iprover dest: map_injective injD intro: inj_onI)
wenzelm@13114
   853
wenzelm@13114
   854
lemma inj_mapD: "inj (map f) ==> inj f"
paulson@14208
   855
apply (unfold inj_on_def, clarify)
nipkow@13145
   856
apply (erule_tac x = "[x]" in ballE)
paulson@14208
   857
 apply (erule_tac x = "[y]" in ballE, simp, blast)
nipkow@13145
   858
apply blast
nipkow@13145
   859
done
wenzelm@13114
   860
nipkow@14339
   861
lemma inj_map[iff]: "inj (map f) = inj f"
nipkow@13145
   862
by (blast dest: inj_mapD intro: inj_mapI)
wenzelm@13114
   863
nipkow@15303
   864
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A"
nipkow@15303
   865
apply(rule inj_onI)
nipkow@15303
   866
apply(erule map_inj_on)
nipkow@15303
   867
apply(blast intro:inj_onI dest:inj_onD)
nipkow@15303
   868
done
nipkow@15303
   869
kleing@14343
   870
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs"
kleing@14343
   871
by (induct xs, auto)
wenzelm@13114
   872
nipkow@14402
   873
lemma map_fun_upd [simp]: "y \<notin> set xs \<Longrightarrow> map (f(y:=v)) xs = map f xs"
nipkow@14402
   874
by (induct xs) auto
nipkow@14402
   875
nipkow@15110
   876
lemma map_fst_zip[simp]:
nipkow@15110
   877
  "length xs = length ys \<Longrightarrow> map fst (zip xs ys) = xs"
nipkow@15110
   878
by (induct rule:list_induct2, simp_all)
nipkow@15110
   879
nipkow@15110
   880
lemma map_snd_zip[simp]:
nipkow@15110
   881
  "length xs = length ys \<Longrightarrow> map snd (zip xs ys) = ys"
nipkow@15110
   882
by (induct rule:list_induct2, simp_all)
nipkow@15110
   883
haftmann@40856
   884
type_mapper map
haftmann@40856
   885
  by simp_all
haftmann@40856
   886
nipkow@15110
   887
nipkow@15392
   888
subsubsection {* @{text rev} *}
wenzelm@13114
   889
wenzelm@13142
   890
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
nipkow@13145
   891
by (induct xs) auto
wenzelm@13114
   892
wenzelm@13142
   893
lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
nipkow@13145
   894
by (induct xs) auto
wenzelm@13114
   895
kleing@15870
   896
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)"
kleing@15870
   897
by auto
kleing@15870
   898
wenzelm@13142
   899
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
nipkow@13145
   900
by (induct xs) auto
wenzelm@13114
   901
wenzelm@13142
   902
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
nipkow@13145
   903
by (induct xs) auto
wenzelm@13114
   904
kleing@15870
   905
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])"
kleing@15870
   906
by (cases xs) auto
kleing@15870
   907
kleing@15870
   908
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])"
kleing@15870
   909
by (cases xs) auto
kleing@15870
   910
haftmann@21061
   911
lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)"
haftmann@21061
   912
apply (induct xs arbitrary: ys, force)
paulson@14208
   913
apply (case_tac ys, simp, force)
nipkow@13145
   914
done
wenzelm@13114
   915
nipkow@15439
   916
lemma inj_on_rev[iff]: "inj_on rev A"
nipkow@15439
   917
by(simp add:inj_on_def)
nipkow@15439
   918
wenzelm@13366
   919
lemma rev_induct [case_names Nil snoc]:
wenzelm@13366
   920
  "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
berghofe@15489
   921
apply(simplesubst rev_rev_ident[symmetric])
nipkow@13145
   922
apply(rule_tac list = "rev xs" in list.induct, simp_all)
nipkow@13145
   923
done
wenzelm@13114
   924
wenzelm@13366
   925
lemma rev_exhaust [case_names Nil snoc]:
wenzelm@13366
   926
  "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"
nipkow@13145
   927
by (induct xs rule: rev_induct) auto
wenzelm@13114
   928
wenzelm@13366
   929
lemmas rev_cases = rev_exhaust
wenzelm@13366
   930
nipkow@18423
   931
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])"
nipkow@18423
   932
by(rule rev_cases[of xs]) auto
nipkow@18423
   933
wenzelm@13114
   934
nipkow@15392
   935
subsubsection {* @{text set} *}
wenzelm@13114
   936
wenzelm@13142
   937
lemma finite_set [iff]: "finite (set xs)"
nipkow@13145
   938
by (induct xs) auto
wenzelm@13114
   939
wenzelm@13142
   940
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
nipkow@13145
   941
by (induct xs) auto
wenzelm@13114
   942
nipkow@17830
   943
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs"
nipkow@17830
   944
by(cases xs) auto
oheimb@14099
   945
wenzelm@13142
   946
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
nipkow@13145
   947
by auto
wenzelm@13114
   948
oheimb@14099
   949
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" 
oheimb@14099
   950
by auto
oheimb@14099
   951
wenzelm@13142
   952
lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
nipkow@13145
   953
by (induct xs) auto
wenzelm@13114
   954
nipkow@15245
   955
lemma set_empty2[iff]: "({} = set xs) = (xs = [])"
nipkow@15245
   956
by(induct xs) auto
nipkow@15245
   957
wenzelm@13142
   958
lemma set_rev [simp]: "set (rev xs) = set xs"
nipkow@13145
   959
by (induct xs) auto
wenzelm@13114
   960
wenzelm@13142
   961
lemma set_map [simp]: "set (map f xs) = f`(set xs)"
nipkow@13145
   962
by (induct xs) auto
wenzelm@13114
   963
wenzelm@13142
   964
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
nipkow@13145
   965
by (induct xs) auto
wenzelm@13114
   966
nipkow@32417
   967
lemma set_upt [simp]: "set[i..<j] = {i..<j}"
nipkow@32417
   968
by (induct j) (simp_all add: atLeastLessThanSuc)
wenzelm@13114
   969
nipkow@26073
   970
nipkow@26073
   971
lemma split_list: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs"
nipkow@26073
   972
proof (induct xs)
nipkow@26073
   973
  case Nil thus ?case by simp
nipkow@26073
   974
next
nipkow@26073
   975
  case Cons thus ?case by (auto intro: Cons_eq_appendI)
nipkow@26073
   976
qed
nipkow@26073
   977
haftmann@26734
   978
lemma in_set_conv_decomp: "x \<in> set xs \<longleftrightarrow> (\<exists>ys zs. xs = ys @ x # zs)"
haftmann@26734
   979
  by (auto elim: split_list)
nipkow@26073
   980
nipkow@26073
   981
lemma split_list_first: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys"
paulson@15113
   982
proof (induct xs)
nipkow@26073
   983
  case Nil thus ?case by simp
nipkow@18049
   984
next
nipkow@18049
   985
  case (Cons a xs)
nipkow@18049
   986
  show ?case
nipkow@18049
   987
  proof cases
wenzelm@25221
   988
    assume "x = a" thus ?case using Cons by fastsimp
nipkow@18049
   989
  next
nipkow@26073
   990
    assume "x \<noteq> a" thus ?case using Cons by(fastsimp intro!: Cons_eq_appendI)
nipkow@18049
   991
  qed
nipkow@18049
   992
qed
nipkow@18049
   993
nipkow@26073
   994
lemma in_set_conv_decomp_first:
nipkow@26073
   995
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)"
haftmann@26734
   996
  by (auto dest!: split_list_first)
nipkow@26073
   997
haftmann@40366
   998
lemma split_list_last: "x \<in> set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs"
haftmann@40366
   999
proof (induct xs rule: rev_induct)
nipkow@26073
  1000
  case Nil thus ?case by simp
nipkow@26073
  1001
next
nipkow@26073
  1002
  case (snoc a xs)
nipkow@26073
  1003
  show ?case
nipkow@26073
  1004
  proof cases
haftmann@40366
  1005
    assume "x = a" thus ?case using snoc by (metis List.set.simps(1) emptyE)
nipkow@26073
  1006
  next
nipkow@26073
  1007
    assume "x \<noteq> a" thus ?case using snoc by fastsimp
nipkow@26073
  1008
  qed
nipkow@26073
  1009
qed
nipkow@26073
  1010
nipkow@26073
  1011
lemma in_set_conv_decomp_last:
nipkow@26073
  1012
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)"
haftmann@26734
  1013
  by (auto dest!: split_list_last)
nipkow@26073
  1014
nipkow@26073
  1015
lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs & P x"
nipkow@26073
  1016
proof (induct xs)
nipkow@26073
  1017
  case Nil thus ?case by simp
nipkow@26073
  1018
next
nipkow@26073
  1019
  case Cons thus ?case
nipkow@26073
  1020
    by(simp add:Bex_def)(metis append_Cons append.simps(1))
nipkow@26073
  1021
qed
nipkow@26073
  1022
nipkow@26073
  1023
lemma split_list_propE:
haftmann@26734
  1024
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
  1025
  obtains ys x zs where "xs = ys @ x # zs" and "P x"
haftmann@26734
  1026
using split_list_prop [OF assms] by blast
nipkow@26073
  1027
nipkow@26073
  1028
lemma split_list_first_prop:
nipkow@26073
  1029
  "\<exists>x \<in> set xs. P x \<Longrightarrow>
nipkow@26073
  1030
   \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)"
haftmann@26734
  1031
proof (induct xs)
nipkow@26073
  1032
  case Nil thus ?case by simp
nipkow@26073
  1033
next
nipkow@26073
  1034
  case (Cons x xs)
nipkow@26073
  1035
  show ?case
nipkow@26073
  1036
  proof cases
nipkow@26073
  1037
    assume "P x"
haftmann@40366
  1038
    thus ?thesis by simp (metis Un_upper1 contra_subsetD in_set_conv_decomp_first self_append_conv2 set_append)
nipkow@26073
  1039
  next
nipkow@26073
  1040
    assume "\<not> P x"
nipkow@26073
  1041
    hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp
nipkow@26073
  1042
    thus ?thesis using `\<not> P x` Cons(1) by (metis append_Cons set_ConsD)
nipkow@26073
  1043
  qed
nipkow@26073
  1044
qed
nipkow@26073
  1045
nipkow@26073
  1046
lemma split_list_first_propE:
haftmann@26734
  1047
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
  1048
  obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y"
haftmann@26734
  1049
using split_list_first_prop [OF assms] by blast
nipkow@26073
  1050
nipkow@26073
  1051
lemma split_list_first_prop_iff:
nipkow@26073
  1052
  "(\<exists>x \<in> set xs. P x) \<longleftrightarrow>
nipkow@26073
  1053
   (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))"
haftmann@26734
  1054
by (rule, erule split_list_first_prop) auto
nipkow@26073
  1055
nipkow@26073
  1056
lemma split_list_last_prop:
nipkow@26073
  1057
  "\<exists>x \<in> set xs. P x \<Longrightarrow>
nipkow@26073
  1058
   \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)"
nipkow@26073
  1059
proof(induct xs rule:rev_induct)
nipkow@26073
  1060
  case Nil thus ?case by simp
nipkow@26073
  1061
next
nipkow@26073
  1062
  case (snoc x xs)
nipkow@26073
  1063
  show ?case
nipkow@26073
  1064
  proof cases
nipkow@26073
  1065
    assume "P x" thus ?thesis by (metis emptyE set_empty)
nipkow@26073
  1066
  next
nipkow@26073
  1067
    assume "\<not> P x"
nipkow@26073
  1068
    hence "\<exists>x\<in>set xs. P x" using snoc(2) by simp
nipkow@26073
  1069
    thus ?thesis using `\<not> P x` snoc(1) by fastsimp
nipkow@26073
  1070
  qed
nipkow@26073
  1071
qed
nipkow@26073
  1072
nipkow@26073
  1073
lemma split_list_last_propE:
haftmann@26734
  1074
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
  1075
  obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z"
haftmann@26734
  1076
using split_list_last_prop [OF assms] by blast
nipkow@26073
  1077
nipkow@26073
  1078
lemma split_list_last_prop_iff:
nipkow@26073
  1079
  "(\<exists>x \<in> set xs. P x) \<longleftrightarrow>
nipkow@26073
  1080
   (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))"
haftmann@26734
  1081
by (metis split_list_last_prop [where P=P] in_set_conv_decomp)
nipkow@26073
  1082
nipkow@26073
  1083
lemma finite_list: "finite A ==> EX xs. set xs = A"
haftmann@26734
  1084
  by (erule finite_induct)
haftmann@26734
  1085
    (auto simp add: set.simps(2) [symmetric] simp del: set.simps(2))
paulson@13508
  1086
kleing@14388
  1087
lemma card_length: "card (set xs) \<le> length xs"
kleing@14388
  1088
by (induct xs) (auto simp add: card_insert_if)
wenzelm@13114
  1089
haftmann@26442
  1090
lemma set_minus_filter_out:
haftmann@26442
  1091
  "set xs - {y} = set (filter (\<lambda>x. \<not> (x = y)) xs)"
haftmann@26442
  1092
  by (induct xs) auto
paulson@15168
  1093
wenzelm@35118
  1094
nipkow@15392
  1095
subsubsection {* @{text filter} *}
wenzelm@13114
  1096
wenzelm@13142
  1097
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
nipkow@13145
  1098
by (induct xs) auto
wenzelm@13114
  1099
nipkow@15305
  1100
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)"
nipkow@15305
  1101
by (induct xs) simp_all
nipkow@15305
  1102
wenzelm@13142
  1103
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
nipkow@13145
  1104
by (induct xs) auto
wenzelm@13114
  1105
nipkow@16998
  1106
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs"
nipkow@16998
  1107
by (induct xs) (auto simp add: le_SucI)
nipkow@16998
  1108
nipkow@18423
  1109
lemma sum_length_filter_compl:
nipkow@18423
  1110
  "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs"
nipkow@18423
  1111
by(induct xs) simp_all
nipkow@18423
  1112
wenzelm@13142
  1113
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
nipkow@13145
  1114
by (induct xs) auto
wenzelm@13114
  1115
wenzelm@13142
  1116
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
nipkow@13145
  1117
by (induct xs) auto
wenzelm@13114
  1118
nipkow@16998
  1119
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" 
nipkow@24349
  1120
by (induct xs) simp_all
nipkow@16998
  1121
nipkow@16998
  1122
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)"
nipkow@16998
  1123
apply (induct xs)
nipkow@16998
  1124
 apply auto
nipkow@16998
  1125
apply(cut_tac P=P and xs=xs in length_filter_le)
nipkow@16998
  1126
apply simp
nipkow@16998
  1127
done
wenzelm@13114
  1128
nipkow@16965
  1129
lemma filter_map:
nipkow@16965
  1130
  "filter P (map f xs) = map f (filter (P o f) xs)"
nipkow@16965
  1131
by (induct xs) simp_all
nipkow@16965
  1132
nipkow@16965
  1133
lemma length_filter_map[simp]:
nipkow@16965
  1134
  "length (filter P (map f xs)) = length(filter (P o f) xs)"
nipkow@16965
  1135
by (simp add:filter_map)
nipkow@16965
  1136
wenzelm@13142
  1137
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
nipkow@13145
  1138
by auto
wenzelm@13114
  1139
nipkow@15246
  1140
lemma length_filter_less:
nipkow@15246
  1141
  "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs"
nipkow@15246
  1142
proof (induct xs)
nipkow@15246
  1143
  case Nil thus ?case by simp
nipkow@15246
  1144
next
nipkow@15246
  1145
  case (Cons x xs) thus ?case
nipkow@15246
  1146
    apply (auto split:split_if_asm)
nipkow@15246
  1147
    using length_filter_le[of P xs] apply arith
nipkow@15246
  1148
  done
nipkow@15246
  1149
qed
wenzelm@13114
  1150
nipkow@15281
  1151
lemma length_filter_conv_card:
nipkow@15281
  1152
 "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
nipkow@15281
  1153
proof (induct xs)
nipkow@15281
  1154
  case Nil thus ?case by simp
nipkow@15281
  1155
next
nipkow@15281
  1156
  case (Cons x xs)
nipkow@15281
  1157
  let ?S = "{i. i < length xs & p(xs!i)}"
nipkow@15281
  1158
  have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite)
nipkow@15281
  1159
  show ?case (is "?l = card ?S'")
nipkow@15281
  1160
  proof (cases)
nipkow@15281
  1161
    assume "p x"
nipkow@15281
  1162
    hence eq: "?S' = insert 0 (Suc ` ?S)"
nipkow@25162
  1163
      by(auto simp: image_def split:nat.split dest:gr0_implies_Suc)
nipkow@15281
  1164
    have "length (filter p (x # xs)) = Suc(card ?S)"
wenzelm@23388
  1165
      using Cons `p x` by simp
nipkow@15281
  1166
    also have "\<dots> = Suc(card(Suc ` ?S))" using fin
nipkow@15281
  1167
      by (simp add: card_image inj_Suc)
nipkow@15281
  1168
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
  1169
      by (simp add:card_insert_if) (simp add:image_def)
nipkow@15281
  1170
    finally show ?thesis .
nipkow@15281
  1171
  next
nipkow@15281
  1172
    assume "\<not> p x"
nipkow@15281
  1173
    hence eq: "?S' = Suc ` ?S"
nipkow@25162
  1174
      by(auto simp add: image_def split:nat.split elim:lessE)
nipkow@15281
  1175
    have "length (filter p (x # xs)) = card ?S"
wenzelm@23388
  1176
      using Cons `\<not> p x` by simp
nipkow@15281
  1177
    also have "\<dots> = card(Suc ` ?S)" using fin
nipkow@15281
  1178
      by (simp add: card_image inj_Suc)
nipkow@15281
  1179
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
  1180
      by (simp add:card_insert_if)
nipkow@15281
  1181
    finally show ?thesis .
nipkow@15281
  1182
  qed
nipkow@15281
  1183
qed
nipkow@15281
  1184
nipkow@17629
  1185
lemma Cons_eq_filterD:
nipkow@17629
  1186
 "x#xs = filter P ys \<Longrightarrow>
nipkow@17629
  1187
  \<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
  1188
  (is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs")
nipkow@17629
  1189
proof(induct ys)
nipkow@17629
  1190
  case Nil thus ?case by simp
nipkow@17629
  1191
next
nipkow@17629
  1192
  case (Cons y ys)
nipkow@17629
  1193
  show ?case (is "\<exists>x. ?Q x")
nipkow@17629
  1194
  proof cases
nipkow@17629
  1195
    assume Py: "P y"
nipkow@17629
  1196
    show ?thesis
nipkow@17629
  1197
    proof cases
wenzelm@25221
  1198
      assume "x = y"
wenzelm@25221
  1199
      with Py Cons.prems have "?Q []" by simp
wenzelm@25221
  1200
      then show ?thesis ..
nipkow@17629
  1201
    next
wenzelm@25221
  1202
      assume "x \<noteq> y"
wenzelm@25221
  1203
      with Py Cons.prems show ?thesis by simp
nipkow@17629
  1204
    qed
nipkow@17629
  1205
  next
wenzelm@25221
  1206
    assume "\<not> P y"
wenzelm@25221
  1207
    with Cons obtain us vs where "?P (y#ys) (y#us) vs" by fastsimp
wenzelm@25221
  1208
    then have "?Q (y#us)" by simp
wenzelm@25221
  1209
    then show ?thesis ..
nipkow@17629
  1210
  qed
nipkow@17629
  1211
qed
nipkow@17629
  1212
nipkow@17629
  1213
lemma filter_eq_ConsD:
nipkow@17629
  1214
 "filter P ys = x#xs \<Longrightarrow>
nipkow@17629
  1215
  \<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
  1216
by(rule Cons_eq_filterD) simp
nipkow@17629
  1217
nipkow@17629
  1218
lemma filter_eq_Cons_iff:
nipkow@17629
  1219
 "(filter P ys = x#xs) =
nipkow@17629
  1220
  (\<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
  1221
by(auto dest:filter_eq_ConsD)
nipkow@17629
  1222
nipkow@17629
  1223
lemma Cons_eq_filter_iff:
nipkow@17629
  1224
 "(x#xs = filter P ys) =
nipkow@17629
  1225
  (\<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
  1226
by(auto dest:Cons_eq_filterD)
nipkow@17629
  1227
krauss@19770
  1228
lemma filter_cong[fundef_cong, recdef_cong]:
nipkow@17501
  1229
 "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys"
nipkow@17501
  1230
apply simp
nipkow@17501
  1231
apply(erule thin_rl)
nipkow@17501
  1232
by (induct ys) simp_all
nipkow@17501
  1233
nipkow@15281
  1234
haftmann@26442
  1235
subsubsection {* List partitioning *}
haftmann@26442
  1236
haftmann@26442
  1237
primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where
haftmann@26442
  1238
  "partition P [] = ([], [])"
haftmann@26442
  1239
  | "partition P (x # xs) = 
haftmann@26442
  1240
      (let (yes, no) = partition P xs
haftmann@26442
  1241
      in if P x then (x # yes, no) else (yes, x # no))"
haftmann@26442
  1242
haftmann@26442
  1243
lemma partition_filter1:
haftmann@26442
  1244
    "fst (partition P xs) = filter P xs"
haftmann@26442
  1245
by (induct xs) (auto simp add: Let_def split_def)
haftmann@26442
  1246
haftmann@26442
  1247
lemma partition_filter2:
haftmann@26442
  1248
    "snd (partition P xs) = filter (Not o P) xs"
haftmann@26442
  1249
by (induct xs) (auto simp add: Let_def split_def)
haftmann@26442
  1250
haftmann@26442
  1251
lemma partition_P:
haftmann@26442
  1252
  assumes "partition P xs = (yes, no)"
haftmann@26442
  1253
  shows "(\<forall>p \<in> set yes.  P p) \<and> (\<forall>p  \<in> set no. \<not> P p)"
haftmann@26442
  1254
proof -
haftmann@26442
  1255
  from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)"
haftmann@26442
  1256
    by simp_all
haftmann@26442
  1257
  then show ?thesis by (simp_all add: partition_filter1 partition_filter2)
haftmann@26442
  1258
qed
haftmann@26442
  1259
haftmann@26442
  1260
lemma partition_set:
haftmann@26442
  1261
  assumes "partition P xs = (yes, no)"
haftmann@26442
  1262
  shows "set yes \<union> set no = set xs"
haftmann@26442
  1263
proof -
haftmann@26442
  1264
  from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)"
haftmann@26442
  1265
    by simp_all
haftmann@26442
  1266
  then show ?thesis by (auto simp add: partition_filter1 partition_filter2) 
haftmann@26442
  1267
qed
haftmann@26442
  1268
hoelzl@33639
  1269
lemma partition_filter_conv[simp]:
hoelzl@33639
  1270
  "partition f xs = (filter f xs,filter (Not o f) xs)"
hoelzl@33639
  1271
unfolding partition_filter2[symmetric]
hoelzl@33639
  1272
unfolding partition_filter1[symmetric] by simp
hoelzl@33639
  1273
hoelzl@33639
  1274
declare partition.simps[simp del]
haftmann@26442
  1275
wenzelm@35118
  1276
nipkow@15392
  1277
subsubsection {* @{text concat} *}
wenzelm@13114
  1278
wenzelm@13142
  1279
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
nipkow@13145
  1280
by (induct xs) auto
wenzelm@13114
  1281
paulson@18447
  1282
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1283
by (induct xss) auto
wenzelm@13114
  1284
paulson@18447
  1285
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1286
by (induct xss) auto
wenzelm@13114
  1287
nipkow@24308
  1288
lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)"
nipkow@13145
  1289
by (induct xs) auto
wenzelm@13114
  1290
nipkow@24476
  1291
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs"
nipkow@24349
  1292
by (induct xs) auto
nipkow@24349
  1293
wenzelm@13142
  1294
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
nipkow@13145
  1295
by (induct xs) auto
wenzelm@13114
  1296
wenzelm@13142
  1297
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
nipkow@13145
  1298
by (induct xs) auto
wenzelm@13114
  1299
wenzelm@13142
  1300
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
nipkow@13145
  1301
by (induct xs) auto
wenzelm@13114
  1302
bulwahn@40611
  1303
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@40611
  1304
proof (induct xs arbitrary: ys)
bulwahn@40611
  1305
  case (Cons x xs ys)
bulwahn@40611
  1306
  thus ?case by (cases ys) auto
bulwahn@40611
  1307
qed (auto)
bulwahn@40611
  1308
bulwahn@40611
  1309
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@40611
  1310
by (simp add: concat_eq_concat_iff)
bulwahn@40611
  1311
wenzelm@13114
  1312
nipkow@15392
  1313
subsubsection {* @{text nth} *}
wenzelm@13114
  1314
haftmann@29764
  1315
lemma nth_Cons_0 [simp, code]: "(x # xs)!0 = x"
nipkow@13145
  1316
by auto
wenzelm@13114
  1317
haftmann@29764
  1318
lemma nth_Cons_Suc [simp, code]: "(x # xs)!(Suc n) = xs!n"
nipkow@13145
  1319
by auto
wenzelm@13114
  1320
wenzelm@13142
  1321
declare nth.simps [simp del]
wenzelm@13114
  1322
wenzelm@13114
  1323
lemma nth_append:
nipkow@24526
  1324
  "(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
nipkow@24526
  1325
apply (induct xs arbitrary: n, simp)
paulson@14208
  1326
apply (case_tac n, auto)
nipkow@13145
  1327
done
wenzelm@13114
  1328
nipkow@14402
  1329
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x"
wenzelm@25221
  1330
by (induct xs) auto
nipkow@14402
  1331
nipkow@14402
  1332
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n"
wenzelm@25221
  1333
by (induct xs) auto
nipkow@14402
  1334
nipkow@24526
  1335
lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)"
nipkow@24526
  1336
apply (induct xs arbitrary: n, simp)
paulson@14208
  1337
apply (case_tac n, auto)
nipkow@13145
  1338
done
wenzelm@13114
  1339
nipkow@18423
  1340
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"
nipkow@18423
  1341
by(cases xs) simp_all
nipkow@18423
  1342
nipkow@18049
  1343
nipkow@18049
  1344
lemma list_eq_iff_nth_eq:
nipkow@24526
  1345
 "(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))"
nipkow@24526
  1346
apply(induct xs arbitrary: ys)
paulson@24632
  1347
 apply force
nipkow@18049
  1348
apply(case_tac ys)
nipkow@18049
  1349
 apply simp
nipkow@18049
  1350
apply(simp add:nth_Cons split:nat.split)apply blast
nipkow@18049
  1351
done
nipkow@18049
  1352
wenzelm@13142
  1353
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
paulson@15251
  1354
apply (induct xs, simp, simp)
nipkow@13145
  1355
apply safe
paulson@24632
  1356
apply (metis nat_case_0 nth.simps zero_less_Suc)
paulson@24632
  1357
apply (metis less_Suc_eq_0_disj nth_Cons_Suc)
paulson@14208
  1358
apply (case_tac i, simp)
paulson@24632
  1359
apply (metis diff_Suc_Suc nat_case_Suc nth.simps zero_less_diff)
nipkow@13145
  1360
done
wenzelm@13114
  1361
nipkow@17501
  1362
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)"
nipkow@17501
  1363
by(auto simp:set_conv_nth)
nipkow@17501
  1364
nipkow@13145
  1365
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"
nipkow@13145
  1366
by (auto simp add: set_conv_nth)
wenzelm@13114
  1367
wenzelm@13142
  1368
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
nipkow@13145
  1369
by (auto simp add: set_conv_nth)
wenzelm@13114
  1370
wenzelm@13114
  1371
lemma all_nth_imp_all_set:
nipkow@13145
  1372
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x"
nipkow@13145
  1373
by (auto simp add: set_conv_nth)
wenzelm@13114
  1374
wenzelm@13114
  1375
lemma all_set_conv_all_nth:
nipkow@13145
  1376
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
nipkow@13145
  1377
by (auto simp add: set_conv_nth)
wenzelm@13114
  1378
kleing@25296
  1379
lemma rev_nth:
kleing@25296
  1380
  "n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)"
kleing@25296
  1381
proof (induct xs arbitrary: n)
kleing@25296
  1382
  case Nil thus ?case by simp
kleing@25296
  1383
next
kleing@25296
  1384
  case (Cons x xs)
kleing@25296
  1385
  hence n: "n < Suc (length xs)" by simp
kleing@25296
  1386
  moreover
kleing@25296
  1387
  { assume "n < length xs"
kleing@25296
  1388
    with n obtain n' where "length xs - n = Suc n'"
kleing@25296
  1389
      by (cases "length xs - n", auto)
kleing@25296
  1390
    moreover
kleing@25296
  1391
    then have "length xs - Suc n = n'" by simp
kleing@25296
  1392
    ultimately
kleing@25296
  1393
    have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp
kleing@25296
  1394
  }
kleing@25296
  1395
  ultimately
kleing@25296
  1396
  show ?case by (clarsimp simp add: Cons nth_append)
kleing@25296
  1397
qed
wenzelm@13114
  1398
nipkow@31159
  1399
lemma Skolem_list_nth:
nipkow@31159
  1400
  "(ALL i<k. EX x. P i x) = (EX xs. size xs = k & (ALL i<k. P i (xs!i)))"
nipkow@31159
  1401
  (is "_ = (EX xs. ?P k xs)")
nipkow@31159
  1402
proof(induct k)
nipkow@31159
  1403
  case 0 show ?case by simp
nipkow@31159
  1404
next
nipkow@31159
  1405
  case (Suc k)
nipkow@31159
  1406
  show ?case (is "?L = ?R" is "_ = (EX xs. ?P' xs)")
nipkow@31159
  1407
  proof
nipkow@31159
  1408
    assume "?R" thus "?L" using Suc by auto
nipkow@31159
  1409
  next
nipkow@31159
  1410
    assume "?L"
nipkow@31159
  1411
    with Suc obtain x xs where "?P k xs & P k x" by (metis less_Suc_eq)
nipkow@31159
  1412
    hence "?P'(xs@[x])" by(simp add:nth_append less_Suc_eq)
nipkow@31159
  1413
    thus "?R" ..
nipkow@31159
  1414
  qed
nipkow@31159
  1415
qed
nipkow@31159
  1416
nipkow@31159
  1417
nipkow@15392
  1418
subsubsection {* @{text list_update} *}
wenzelm@13114
  1419
nipkow@24526
  1420
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs"
nipkow@24526
  1421
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1422
wenzelm@13114
  1423
lemma nth_list_update:
nipkow@24526
  1424
"i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"
nipkow@24526
  1425
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1426
wenzelm@13142
  1427
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
nipkow@13145
  1428
by (simp add: nth_list_update)
wenzelm@13114
  1429
nipkow@24526
  1430
lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j"
nipkow@24526
  1431
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1432
nipkow@24526
  1433
lemma list_update_id[simp]: "xs[i := xs!i] = xs"
nipkow@24526
  1434
by (induct xs arbitrary: i) (simp_all split:nat.splits)
nipkow@24526
  1435
nipkow@24526
  1436
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs"
nipkow@24526
  1437
apply (induct xs arbitrary: i)
nipkow@17501
  1438
 apply simp
nipkow@17501
  1439
apply (case_tac i)
nipkow@17501
  1440
apply simp_all
nipkow@17501
  1441
done
nipkow@17501
  1442
nipkow@31077
  1443
lemma list_update_nonempty[simp]: "xs[k:=x] = [] \<longleftrightarrow> xs=[]"
nipkow@31077
  1444
by(metis length_0_conv length_list_update)
nipkow@31077
  1445
wenzelm@13114
  1446
lemma list_update_same_conv:
nipkow@24526
  1447
"i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
nipkow@24526
  1448
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1449
nipkow@14187
  1450
lemma list_update_append1:
nipkow@24526
  1451
 "i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"
nipkow@24526
  1452
apply (induct xs arbitrary: i, simp)
nipkow@14187
  1453
apply(simp split:nat.split)
nipkow@14187
  1454
done
nipkow@14187
  1455
kleing@15868
  1456
lemma list_update_append:
nipkow@24526
  1457
  "(xs @ ys) [n:= x] = 
kleing@15868
  1458
  (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))"
nipkow@24526
  1459
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1460
nipkow@14402
  1461
lemma list_update_length [simp]:
nipkow@14402
  1462
 "(xs @ x # ys)[length xs := y] = (xs @ y # ys)"
nipkow@14402
  1463
by (induct xs, auto)
nipkow@14402
  1464
nipkow@31258
  1465
lemma map_update: "map f (xs[k:= y]) = (map f xs)[k := f y]"
nipkow@31258
  1466
by(induct xs arbitrary: k)(auto split:nat.splits)
nipkow@31258
  1467
nipkow@31258
  1468
lemma rev_update:
nipkow@31258
  1469
  "k < length xs \<Longrightarrow> rev (xs[k:= y]) = (rev xs)[length xs - k - 1 := y]"
nipkow@31258
  1470
by (induct xs arbitrary: k) (auto simp: list_update_append split:nat.splits)
nipkow@31258
  1471
wenzelm@13114
  1472
lemma update_zip:
nipkow@31080
  1473
  "(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
nipkow@24526
  1474
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split)
nipkow@24526
  1475
nipkow@24526
  1476
lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)"
nipkow@24526
  1477
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1478
wenzelm@13114
  1479
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
nipkow@13145
  1480
by (blast dest!: set_update_subset_insert [THEN subsetD])
wenzelm@13114
  1481
nipkow@24526
  1482
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])"
nipkow@24526
  1483
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1484
nipkow@31077
  1485
lemma list_update_overwrite[simp]:
haftmann@24796
  1486
  "xs [i := x, i := y] = xs [i := y]"
nipkow@31077
  1487
apply (induct xs arbitrary: i) apply simp
nipkow@31077
  1488
apply (case_tac i, simp_all)
haftmann@24796
  1489
done
haftmann@24796
  1490
haftmann@24796
  1491
lemma list_update_swap:
haftmann@24796
  1492
  "i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]"
haftmann@24796
  1493
apply (induct xs arbitrary: i i')
haftmann@24796
  1494
apply simp
haftmann@24796
  1495
apply (case_tac i, case_tac i')
haftmann@24796
  1496
apply auto
haftmann@24796
  1497
apply (case_tac i')
haftmann@24796
  1498
apply auto
haftmann@24796
  1499
done
haftmann@24796
  1500
haftmann@29764
  1501
lemma list_update_code [code]:
haftmann@29764
  1502
  "[][i := y] = []"
haftmann@29764
  1503
  "(x # xs)[0 := y] = y # xs"
haftmann@29764
  1504
  "(x # xs)[Suc i := y] = x # xs[i := y]"
haftmann@29764
  1505
  by simp_all
haftmann@29764
  1506
wenzelm@13114
  1507
nipkow@15392
  1508
subsubsection {* @{text last} and @{text butlast} *}
wenzelm@13114
  1509
wenzelm@13142
  1510
lemma last_snoc [simp]: "last (xs @ [x]) = x"
nipkow@13145
  1511
by (induct xs) auto
wenzelm@13114
  1512
wenzelm@13142
  1513
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
nipkow@13145
  1514
by (induct xs) auto
wenzelm@13114
  1515
nipkow@14302
  1516
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x"
nipkow@14302
  1517
by(simp add:last.simps)
nipkow@14302
  1518
nipkow@14302
  1519
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs"
nipkow@14302
  1520
by(simp add:last.simps)
nipkow@14302
  1521
nipkow@14302
  1522
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)"
nipkow@14302
  1523
by (induct xs) (auto)
nipkow@14302
  1524
nipkow@14302
  1525
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs"
nipkow@14302
  1526
by(simp add:last_append)
nipkow@14302
  1527
nipkow@14302
  1528
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"
nipkow@14302
  1529
by(simp add:last_append)
nipkow@14302
  1530
nipkow@17762
  1531
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"
nipkow@17762
  1532
by(rule rev_exhaust[of xs]) simp_all
nipkow@17762
  1533
nipkow@17762
  1534
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"
nipkow@17762
  1535
by(cases xs) simp_all
nipkow@17762
  1536
nipkow@17765
  1537
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as"
nipkow@17765
  1538
by (induct as) auto
nipkow@17762
  1539
wenzelm@13142
  1540
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
nipkow@13145
  1541
by (induct xs rule: rev_induct) auto
wenzelm@13114
  1542
wenzelm@13114
  1543
lemma butlast_append:
nipkow@24526
  1544
  "butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
nipkow@24526
  1545
by (induct xs arbitrary: ys) auto
wenzelm@13114
  1546
wenzelm@13142
  1547
lemma append_butlast_last_id [simp]:
nipkow@13145
  1548
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
nipkow@13145
  1549
by (induct xs) auto
wenzelm@13114
  1550
wenzelm@13142
  1551
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
nipkow@13145
  1552
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1553
wenzelm@13114
  1554
lemma in_set_butlast_appendI:
nipkow@13145
  1555
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
nipkow@13145
  1556
by (auto dest: in_set_butlastD simp add: butlast_append)
wenzelm@13114
  1557
nipkow@24526
  1558
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs"
nipkow@24526
  1559
apply (induct xs arbitrary: n)
nipkow@17501
  1560
 apply simp
nipkow@17501
  1561
apply (auto split:nat.split)
nipkow@17501
  1562
done
nipkow@17501
  1563
huffman@30128
  1564
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"
nipkow@17589
  1565
by(induct xs)(auto simp:neq_Nil_conv)
nipkow@17589
  1566
huffman@30128
  1567
lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs"
huffman@26584
  1568
by (induct xs, simp, case_tac xs, simp_all)
huffman@26584
  1569
nipkow@31077
  1570
lemma last_list_update:
nipkow@31077
  1571
  "xs \<noteq> [] \<Longrightarrow> last(xs[k:=x]) = (if k = size xs - 1 then x else last xs)"
nipkow@31077
  1572
by (auto simp: last_conv_nth)
nipkow@31077
  1573
nipkow@31077
  1574
lemma butlast_list_update:
nipkow@31077
  1575
  "butlast(xs[k:=x]) =
nipkow@31077
  1576
 (if k = size xs - 1 then butlast xs else (butlast xs)[k:=x])"
nipkow@31077
  1577
apply(cases xs rule:rev_cases)
nipkow@31077
  1578
apply simp
nipkow@31077
  1579
apply(simp add:list_update_append split:nat.splits)
nipkow@31077
  1580
done
nipkow@31077
  1581
haftmann@36846
  1582
lemma last_map:
haftmann@36846
  1583
  "xs \<noteq> [] \<Longrightarrow> last (map f xs) = f (last xs)"
haftmann@36846
  1584
  by (cases xs rule: rev_cases) simp_all
haftmann@36846
  1585
haftmann@36846
  1586
lemma map_butlast:
haftmann@36846
  1587
  "map f (butlast xs) = butlast (map f xs)"
haftmann@36846
  1588
  by (induct xs) simp_all
haftmann@36846
  1589
nipkow@40476
  1590
lemma snoc_eq_iff_butlast:
nipkow@40476
  1591
  "xs @ [x] = ys \<longleftrightarrow> (ys \<noteq> [] & butlast ys = xs & last ys = x)"
nipkow@40476
  1592
by (metis append_butlast_last_id append_is_Nil_conv butlast_snoc last_snoc not_Cons_self)
nipkow@40476
  1593
haftmann@24796
  1594
nipkow@15392
  1595
subsubsection {* @{text take} and @{text drop} *}
wenzelm@13114
  1596
wenzelm@13142
  1597
lemma take_0 [simp]: "take 0 xs = []"
nipkow@13145
  1598
by (induct xs) auto
wenzelm@13114
  1599
wenzelm@13142
  1600
lemma drop_0 [simp]: "drop 0 xs = xs"
nipkow@13145
  1601
by (induct xs) auto
wenzelm@13114
  1602
wenzelm@13142
  1603
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
nipkow@13145
  1604
by simp
wenzelm@13114
  1605
wenzelm@13142
  1606
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
nipkow@13145
  1607
by simp
wenzelm@13114
  1608
wenzelm@13142
  1609
declare take_Cons [simp del] and drop_Cons [simp del]
wenzelm@13114
  1610
huffman@30128
  1611
lemma take_1_Cons [simp]: "take 1 (x # xs) = [x]"
huffman@30128
  1612
  unfolding One_nat_def by simp
huffman@30128
  1613
huffman@30128
  1614
lemma drop_1_Cons [simp]: "drop 1 (x # xs) = xs"
huffman@30128
  1615
  unfolding One_nat_def by simp
huffman@30128
  1616
nipkow@15110
  1617
lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)"
nipkow@15110
  1618
by(clarsimp simp add:neq_Nil_conv)
nipkow@15110
  1619
nipkow@14187
  1620
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"
nipkow@14187
  1621
by(cases xs, simp_all)
nipkow@14187
  1622
huffman@26584
  1623
lemma take_tl: "take n (tl xs) = tl (take (Suc n) xs)"
huffman@26584
  1624
by (induct xs arbitrary: n) simp_all
huffman@26584
  1625
nipkow@24526
  1626
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)"
nipkow@24526
  1627
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split)
nipkow@24526
  1628
huffman@26584
  1629
lemma tl_take: "tl (take n xs) = take (n - 1) (tl xs)"
huffman@26584
  1630
by (cases n, simp, cases xs, auto)
huffman@26584
  1631
huffman@26584
  1632
lemma tl_drop: "tl (drop n xs) = drop n (tl xs)"
huffman@26584
  1633
by (simp only: drop_tl)
huffman@26584
  1634
nipkow@24526
  1635
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y"
nipkow@24526
  1636
apply (induct xs arbitrary: n, simp)
nipkow@14187
  1637
apply(simp add:drop_Cons nth_Cons split:nat.splits)
nipkow@14187
  1638
done
nipkow@14187
  1639
nipkow@13913
  1640
lemma take_Suc_conv_app_nth:
nipkow@24526
  1641
  "i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"
nipkow@24526
  1642
apply (induct xs arbitrary: i, simp)
paulson@14208
  1643
apply (case_tac i, auto)
nipkow@13913
  1644
done
nipkow@13913
  1645
mehta@14591
  1646
lemma drop_Suc_conv_tl:
nipkow@24526
  1647
  "i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs"
nipkow@24526
  1648
apply (induct xs arbitrary: i, simp)
mehta@14591
  1649
apply (case_tac i, auto)
mehta@14591
  1650
done
mehta@14591
  1651
nipkow@24526
  1652
lemma length_take [simp]: "length (take n xs) = min (length xs) n"
nipkow@24526
  1653
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1654
nipkow@24526
  1655
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)"
nipkow@24526
  1656
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1657
nipkow@24526
  1658
lemma take_all [simp]: "length xs <= n ==> take n xs = xs"
nipkow@24526
  1659
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1660
nipkow@24526
  1661
lemma drop_all [simp]: "length xs <= n ==> drop n xs = []"
nipkow@24526
  1662
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1663
wenzelm@13142
  1664
lemma take_append [simp]:
nipkow@24526
  1665
  "take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
nipkow@24526
  1666
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1667
wenzelm@13142
  1668
lemma drop_append [simp]:
nipkow@24526
  1669
  "drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
nipkow@24526
  1670
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1671
nipkow@24526
  1672
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs"
nipkow@24526
  1673
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1674
apply (case_tac xs, auto)
nipkow@15236
  1675
apply (case_tac n, auto)
nipkow@13145
  1676
done
wenzelm@13142
  1677
nipkow@24526
  1678
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs"
nipkow@24526
  1679
apply (induct m arbitrary: xs, auto)
paulson@14208
  1680
apply (case_tac xs, auto)
nipkow@13145
  1681
done
wenzelm@13114
  1682
nipkow@24526
  1683
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)"
nipkow@24526
  1684
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1685
apply (case_tac xs, auto)
nipkow@13145
  1686
done
wenzelm@13114
  1687
nipkow@24526
  1688
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)"
nipkow@24526
  1689
apply(induct xs arbitrary: m n)
nipkow@14802
  1690
 apply simp
nipkow@14802
  1691
apply(simp add: take_Cons drop_Cons split:nat.split)
nipkow@14802
  1692
done
nipkow@14802
  1693
nipkow@24526
  1694
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs"
nipkow@24526
  1695
apply (induct n arbitrary: xs, auto)
paulson@14208
  1696
apply (case_tac xs, auto)
nipkow@13145
  1697
done
wenzelm@13114
  1698
nipkow@24526
  1699
lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])"
nipkow@24526
  1700
apply(induct xs arbitrary: n)
nipkow@15110
  1701
 apply simp
nipkow@15110
  1702
apply(simp add:take_Cons split:nat.split)
nipkow@15110
  1703
done
nipkow@15110
  1704
nipkow@24526
  1705
lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)"
nipkow@24526
  1706
apply(induct xs arbitrary: n)
nipkow@15110
  1707
apply simp
nipkow@15110
  1708
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1709
done
nipkow@15110
  1710
nipkow@24526
  1711
lemma take_map: "take n (map f xs) = map f (take n xs)"
nipkow@24526
  1712
apply (induct n arbitrary: xs, auto)
paulson@14208
  1713
apply (case_tac xs, auto)
nipkow@13145
  1714
done
wenzelm@13114
  1715
nipkow@24526
  1716
lemma drop_map: "drop n (map f xs) = map f (drop n xs)"
nipkow@24526
  1717
apply (induct n arbitrary: xs, auto)
paulson@14208
  1718
apply (case_tac xs, auto)
nipkow@13145
  1719
done
wenzelm@13114
  1720
nipkow@24526
  1721
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)"
nipkow@24526
  1722
apply (induct xs arbitrary: i, auto)
paulson@14208
  1723
apply (case_tac i, auto)
nipkow@13145
  1724
done
wenzelm@13114
  1725
nipkow@24526
  1726
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)"
nipkow@24526
  1727
apply (induct xs arbitrary: i, auto)
paulson@14208
  1728
apply (case_tac i, auto)
nipkow@13145
  1729
done
wenzelm@13114
  1730
nipkow@24526
  1731
lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i"
nipkow@24526
  1732
apply (induct xs arbitrary: i n, auto)
paulson@14208
  1733
apply (case_tac n, blast)
paulson@14208
  1734
apply (case_tac i, auto)
nipkow@13145
  1735
done
wenzelm@13114
  1736
wenzelm@13142
  1737
lemma nth_drop [simp]:
nipkow@24526
  1738
  "n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
nipkow@24526
  1739
apply (induct n arbitrary: xs i, auto)
paulson@14208
  1740
apply (case_tac xs, auto)
nipkow@13145
  1741
done
wenzelm@13114
  1742
huffman@26584
  1743
lemma butlast_take:
huffman@30128
  1744
  "n <= length xs ==> butlast (take n xs) = take (n - 1) xs"
huffman@26584
  1745
by (simp add: butlast_conv_take min_max.inf_absorb1 min_max.inf_absorb2)
huffman@26584
  1746
huffman@26584
  1747
lemma butlast_drop: "butlast (drop n xs) = drop n (butlast xs)"
huffman@30128
  1748
by (simp add: butlast_conv_take drop_take add_ac)
huffman@26584
  1749
huffman@26584
  1750
lemma take_butlast: "n < length xs ==> take n (butlast xs) = take n xs"
huffman@26584
  1751
by (simp add: butlast_conv_take min_max.inf_absorb1)
huffman@26584
  1752
huffman@26584
  1753
lemma drop_butlast: "drop n (butlast xs) = butlast (drop n xs)"
huffman@30128
  1754
by (simp add: butlast_conv_take drop_take add_ac)
huffman@26584
  1755
nipkow@18423
  1756
lemma hd_drop_conv_nth: "\<lbrakk> xs \<noteq> []; n < length xs \<rbrakk> \<Longrightarrow> hd(drop n xs) = xs!n"
nipkow@18423
  1757
by(simp add: hd_conv_nth)
nipkow@18423
  1758
nipkow@35248
  1759
lemma set_take_subset_set_take:
nipkow@35248
  1760
  "m <= n \<Longrightarrow> set(take m xs) <= set(take n xs)"
nipkow@35248
  1761
by(induct xs arbitrary: m n)(auto simp:take_Cons split:nat.split)
nipkow@35248
  1762
nipkow@24526
  1763
lemma set_take_subset: "set(take n xs) \<subseteq> set xs"
nipkow@24526
  1764
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split)
nipkow@24526
  1765
nipkow@24526
  1766
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs"
nipkow@24526
  1767
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split)
nipkow@14025
  1768
nipkow@35248
  1769
lemma set_drop_subset_set_drop:
nipkow@35248
  1770
  "m >= n \<Longrightarrow> set(drop m xs) <= set(drop n xs)"
nipkow@35248
  1771
apply(induct xs arbitrary: m n)
nipkow@35248
  1772
apply(auto simp:drop_Cons split:nat.split)
nipkow@35248
  1773
apply (metis set_drop_subset subset_iff)
nipkow@35248
  1774
done
nipkow@35248
  1775
nipkow@14187
  1776
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  1777
using set_take_subset by fast
nipkow@14187
  1778
nipkow@14187
  1779
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  1780
using set_drop_subset by fast
nipkow@14187
  1781
wenzelm@13114
  1782
lemma append_eq_conv_conj:
nipkow@24526
  1783
  "(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
nipkow@24526
  1784
apply (induct xs arbitrary: zs, simp, clarsimp)
paulson@14208
  1785
apply (case_tac zs, auto)
nipkow@13145
  1786
done
wenzelm@13114
  1787
nipkow@24526
  1788
lemma take_add: 
nipkow@24526
  1789
  "i+j \<le> length(xs) \<Longrightarrow> take (i+j) xs = take i xs @ take j (drop i xs)"
nipkow@24526
  1790
apply (induct xs arbitrary: i, auto) 
nipkow@24526
  1791
apply (case_tac i, simp_all)
paulson@14050
  1792
done
paulson@14050
  1793
nipkow@14300
  1794
lemma append_eq_append_conv_if:
nipkow@24526
  1795
 "(xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) =
nipkow@14300
  1796
  (if size xs\<^isub>1 \<le> size ys\<^isub>1
nipkow@14300
  1797
   then xs\<^isub>1 = take (size xs\<^isub>1) ys\<^isub>1 \<and> xs\<^isub>2 = drop (size xs\<^isub>1) ys\<^isub>1 @ ys\<^isub>2
nipkow@14300
  1798
   else take (size ys\<^isub>1) xs\<^isub>1 = ys\<^isub>1 \<and> drop (size ys\<^isub>1) xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>2)"
nipkow@24526
  1799
apply(induct xs\<^isub>1 arbitrary: ys\<^isub>1)
nipkow@14300
  1800
 apply simp
nipkow@14300
  1801
apply(case_tac ys\<^isub>1)
nipkow@14300
  1802
apply simp_all
nipkow@14300
  1803
done
nipkow@14300
  1804
nipkow@15110
  1805
lemma take_hd_drop:
huffman@30016
  1806
  "n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (Suc n) xs"
nipkow@24526
  1807
apply(induct xs arbitrary: n)
nipkow@15110
  1808
apply simp
nipkow@15110
  1809
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1810
done
nipkow@15110
  1811
nipkow@17501
  1812
lemma id_take_nth_drop:
nipkow@17501
  1813
 "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" 
nipkow@17501
  1814
proof -
nipkow@17501
  1815
  assume si: "i < length xs"
nipkow@17501
  1816
  hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto
nipkow@17501
  1817
  moreover
nipkow@17501
  1818
  from si have "take (Suc i) xs = take i xs @ [xs!i]"
nipkow@17501
  1819
    apply (rule_tac take_Suc_conv_app_nth) by arith
nipkow@17501
  1820
  ultimately show ?thesis by auto
nipkow@17501
  1821
qed
nipkow@17501
  1822
  
nipkow@17501
  1823
lemma upd_conv_take_nth_drop:
nipkow@17501
  1824
 "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  1825
proof -
nipkow@17501
  1826
  assume i: "i < length xs"
nipkow@17501
  1827
  have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]"
nipkow@17501
  1828
    by(rule arg_cong[OF id_take_nth_drop[OF i]])
nipkow@17501
  1829
  also have "\<dots> = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  1830
    using i by (simp add: list_update_append)
nipkow@17501
  1831
  finally show ?thesis .
nipkow@17501
  1832
qed
nipkow@17501
  1833
haftmann@24796
  1834
lemma nth_drop':
haftmann@24796
  1835
  "i < length xs \<Longrightarrow> xs ! i # drop (Suc i) xs = drop i xs"
haftmann@24796
  1836
apply (induct i arbitrary: xs)
haftmann@24796
  1837
apply (simp add: neq_Nil_conv)
haftmann@24796
  1838
apply (erule exE)+
haftmann@24796
  1839
apply simp
haftmann@24796
  1840
apply (case_tac xs)
haftmann@24796
  1841
apply simp_all
haftmann@24796
  1842
done
haftmann@24796
  1843
wenzelm@13114
  1844
nipkow@15392
  1845
subsubsection {* @{text takeWhile} and @{text dropWhile} *}
wenzelm@13114
  1846
hoelzl@33639
  1847
lemma length_takeWhile_le: "length (takeWhile P xs) \<le> length xs"
hoelzl@33639
  1848
  by (induct xs) auto
hoelzl@33639
  1849
wenzelm@13142
  1850
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
nipkow@13145
  1851
by (induct xs) auto
wenzelm@13114
  1852
wenzelm@13142
  1853
lemma takeWhile_append1 [simp]:
nipkow@13145
  1854
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"
nipkow@13145
  1855
by (induct xs) auto
wenzelm@13114
  1856
wenzelm@13142
  1857
lemma takeWhile_append2 [simp]:
nipkow@13145
  1858
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
nipkow@13145
  1859
by (induct xs) auto
wenzelm@13114
  1860
wenzelm@13142
  1861
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
nipkow@13145
  1862
by (induct xs) auto
wenzelm@13114
  1863
hoelzl@33639
  1864
lemma takeWhile_nth: "j < length (takeWhile P xs) \<Longrightarrow> takeWhile P xs ! j = xs ! j"
hoelzl@33639
  1865
apply (subst (3) takeWhile_dropWhile_id[symmetric]) unfolding nth_append by auto
hoelzl@33639
  1866
hoelzl@33639
  1867
lemma dropWhile_nth: "j < length (dropWhile P xs) \<Longrightarrow> dropWhile P xs ! j = xs ! (j + length (takeWhile P xs))"
hoelzl@33639
  1868
apply (subst (3) takeWhile_dropWhile_id[symmetric]) unfolding nth_append by auto
hoelzl@33639
  1869
hoelzl@33639
  1870
lemma length_dropWhile_le: "length (dropWhile P xs) \<le> length xs"
hoelzl@33639
  1871
by (induct xs) auto
hoelzl@33639
  1872
wenzelm@13142
  1873
lemma dropWhile_append1 [simp]:
nipkow@13145
  1874
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
nipkow@13145
  1875
by (induct xs) auto
wenzelm@13114
  1876
wenzelm@13142
  1877
lemma dropWhile_append2 [simp]:
nipkow@13145
  1878
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
nipkow@13145
  1879
by (induct xs) auto
wenzelm@13114
  1880
krauss@23971
  1881
lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
nipkow@13145
  1882
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1883
nipkow@13913
  1884
lemma takeWhile_eq_all_conv[simp]:
nipkow@13913
  1885
 "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  1886
by(induct xs, auto)
nipkow@13913
  1887
nipkow@13913
  1888
lemma dropWhile_eq_Nil_conv[simp]:
nipkow@13913
  1889
 "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  1890
by(induct xs, auto)
nipkow@13913
  1891
nipkow@13913
  1892
lemma dropWhile_eq_Cons_conv:
nipkow@13913
  1893
 "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"
nipkow@13913
  1894
by(induct xs, auto)
nipkow@13913
  1895
nipkow@31077
  1896
lemma distinct_takeWhile[simp]: "distinct xs ==> distinct (takeWhile P xs)"
nipkow@31077
  1897
by (induct xs) (auto dest: set_takeWhileD)
nipkow@31077
  1898
nipkow@31077
  1899
lemma distinct_dropWhile[simp]: "distinct xs ==> distinct (dropWhile P xs)"
nipkow@31077
  1900
by (induct xs) auto
nipkow@31077
  1901
hoelzl@33639
  1902
lemma takeWhile_map: "takeWhile P (map f xs) = map f (takeWhile (P \<circ> f) xs)"
hoelzl@33639
  1903
by (induct xs) auto
hoelzl@33639
  1904
hoelzl@33639
  1905
lemma dropWhile_map: "dropWhile P (map f xs) = map f (dropWhile (P \<circ> f) xs)"
hoelzl@33639
  1906
by (induct xs) auto
hoelzl@33639
  1907
hoelzl@33639
  1908
lemma takeWhile_eq_take: "takeWhile P xs = take (length (takeWhile P xs)) xs"
hoelzl@33639
  1909
by (induct xs) auto
hoelzl@33639
  1910
hoelzl@33639
  1911
lemma dropWhile_eq_drop: "dropWhile P xs = drop (length (takeWhile P xs)) xs"
hoelzl@33639
  1912
by (induct xs) auto
hoelzl@33639
  1913
hoelzl@33639
  1914
lemma hd_dropWhile:
hoelzl@33639
  1915
  "dropWhile P xs \<noteq> [] \<Longrightarrow> \<not> P (hd (dropWhile P xs))"
hoelzl@33639
  1916
using assms by (induct xs) auto
hoelzl@33639
  1917
hoelzl@33639
  1918
lemma takeWhile_eq_filter:
hoelzl@33639
  1919
  assumes "\<And> x. x \<in> set (dropWhile P xs) \<Longrightarrow> \<not> P x"
hoelzl@33639
  1920
  shows "takeWhile P xs = filter P xs"
hoelzl@33639
  1921
proof -
hoelzl@33639
  1922
  have A: "filter P xs = filter P (takeWhile P xs @ dropWhile P xs)"
hoelzl@33639
  1923
    by simp
hoelzl@33639
  1924
  have B: "filter P (dropWhile P xs) = []"
hoelzl@33639
  1925
    unfolding filter_empty_conv using assms by blast
hoelzl@33639
  1926
  have "filter P xs = takeWhile P xs"
hoelzl@33639
  1927
    unfolding A filter_append B
hoelzl@33639
  1928
    by (auto simp add: filter_id_conv dest: set_takeWhileD)
hoelzl@33639
  1929
  thus ?thesis ..
hoelzl@33639
  1930
qed
hoelzl@33639
  1931
hoelzl@33639
  1932
lemma takeWhile_eq_take_P_nth:
hoelzl@33639
  1933
  "\<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
  1934
  takeWhile P xs = take n xs"
hoelzl@33639
  1935
proof (induct xs arbitrary: n)
hoelzl@33639
  1936
  case (Cons x xs)
hoelzl@33639
  1937
  thus ?case
hoelzl@33639
  1938
  proof (cases n)
hoelzl@33639
  1939
    case (Suc n') note this[simp]
hoelzl@33639
  1940
    have "P x" using Cons.prems(1)[of 0] by simp
hoelzl@33639
  1941
    moreover have "takeWhile P xs = take n' xs"
hoelzl@33639
  1942
    proof (rule Cons.hyps)
hoelzl@33639
  1943
      case goal1 thus "P (xs ! i)" using Cons.prems(1)[of "Suc i"] by simp
hoelzl@33639
  1944
    next case goal2 thus ?case using Cons by auto
hoelzl@33639
  1945
    qed
hoelzl@33639
  1946
    ultimately show ?thesis by simp
hoelzl@33639
  1947
   qed simp
hoelzl@33639
  1948
qed simp
hoelzl@33639
  1949
hoelzl@33639
  1950
lemma nth_length_takeWhile:
hoelzl@33639
  1951
  "length (takeWhile P xs) < length xs \<Longrightarrow> \<not> P (xs ! length (takeWhile P xs))"
hoelzl@33639
  1952
by (induct xs) auto
hoelzl@33639
  1953
hoelzl@33639
  1954
lemma length_takeWhile_less_P_nth:
hoelzl@33639
  1955
  assumes all: "\<And> i. i < j \<Longrightarrow> P (xs ! i)" and "j \<le> length xs"
hoelzl@33639
  1956
  shows "j \<le> length (takeWhile P xs)"
hoelzl@33639
  1957
proof (rule classical)
hoelzl@33639
  1958
  assume "\<not> ?thesis"
hoelzl@33639
  1959
  hence "length (takeWhile P xs) < length xs" using assms by simp
hoelzl@33639
  1960
  thus ?thesis using all `\<not> ?thesis` nth_length_takeWhile[of P xs] by auto
hoelzl@33639
  1961
qed
nipkow@31077
  1962
nipkow@17501
  1963
text{* The following two lemmmas could be generalized to an arbitrary
nipkow@17501
  1964
property. *}
nipkow@17501
  1965
nipkow@17501
  1966
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  1967
 takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))"
nipkow@17501
  1968
by(induct xs) (auto simp: takeWhile_tail[where l="[]"])
nipkow@17501
  1969
nipkow@17501
  1970
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  1971
  dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)"
nipkow@17501
  1972
apply(induct xs)
nipkow@17501
  1973
 apply simp
nipkow@17501
  1974
apply auto
nipkow@17501
  1975
apply(subst dropWhile_append2)
nipkow@17501
  1976
apply auto
nipkow@17501
  1977
done
nipkow@17501
  1978
nipkow@18423
  1979
lemma takeWhile_not_last:
nipkow@18423
  1980
 "\<lbrakk> xs \<noteq> []; distinct xs\<rbrakk> \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs"
nipkow@18423
  1981
apply(induct xs)
nipkow@18423
  1982
 apply simp
nipkow@18423
  1983
apply(case_tac xs)
nipkow@18423
  1984
apply(auto)
nipkow@18423
  1985
done
nipkow@18423
  1986
krauss@19770
  1987
lemma takeWhile_cong [fundef_cong, recdef_cong]:
krauss@18336
  1988
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  1989
  ==> takeWhile P l = takeWhile Q k"
nipkow@24349
  1990
by (induct k arbitrary: l) (simp_all)
krauss@18336
  1991
krauss@19770
  1992
lemma dropWhile_cong [fundef_cong, recdef_cong]:
krauss@18336
  1993
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  1994
  ==> dropWhile P l = dropWhile Q k"
nipkow@24349
  1995
by (induct k arbitrary: l, simp_all)
krauss@18336
  1996
wenzelm@13114
  1997
nipkow@15392
  1998
subsubsection {* @{text zip} *}
wenzelm@13114
  1999
wenzelm@13142
  2000
lemma zip_Nil [simp]: "zip [] ys = []"
nipkow@13145
  2001
by (induct ys) auto
wenzelm@13114
  2002
wenzelm@13142
  2003
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
nipkow@13145
  2004
by simp
wenzelm@13114
  2005
wenzelm@13142
  2006
declare zip_Cons [simp del]
wenzelm@13114
  2007
haftmann@36198
  2008
lemma [code]:
haftmann@36198
  2009
  "zip [] ys = []"
haftmann@36198
  2010
  "zip xs [] = []"
haftmann@36198
  2011
  "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
haftmann@36198
  2012
  by (fact zip_Nil zip.simps(1) zip_Cons_Cons)+
haftmann@36198
  2013
nipkow@15281
  2014
lemma zip_Cons1:
nipkow@15281
  2015
 "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)"
nipkow@15281
  2016
by(auto split:list.split)
nipkow@15281
  2017
wenzelm@13142
  2018
lemma length_zip [simp]:
krauss@22493
  2019
"length (zip xs ys) = min (length xs) (length ys)"
krauss@22493
  2020
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  2021
haftmann@34965
  2022
lemma zip_obtain_same_length:
haftmann@34965
  2023
  assumes "\<And>zs ws n. length zs = length ws \<Longrightarrow> n = min (length xs) (length ys)
haftmann@34965
  2024
    \<Longrightarrow> zs = take n xs \<Longrightarrow> ws = take n ys \<Longrightarrow> P (zip zs ws)"
haftmann@34965
  2025
  shows "P (zip xs ys)"
haftmann@34965
  2026
proof -
haftmann@34965
  2027
  let ?n = "min (length xs) (length ys)"
haftmann@34965
  2028
  have "P (zip (take ?n xs) (take ?n ys))"
haftmann@34965
  2029
    by (rule assms) simp_all
haftmann@34965
  2030
  moreover have "zip xs ys = zip (take ?n xs) (take ?n ys)"
haftmann@34965
  2031
  proof (induct xs arbitrary: ys)
haftmann@34965
  2032
    case Nil then show ?case by simp
haftmann@34965
  2033
  next
haftmann@34965
  2034
    case (Cons x xs) then show ?case by (cases ys) simp_all
haftmann@34965
  2035
  qed
haftmann@34965
  2036
  ultimately show ?thesis by simp
haftmann@34965
  2037
qed
haftmann@34965
  2038
wenzelm@13114
  2039
lemma zip_append1:
krauss@22493
  2040
"zip (xs @ ys) zs =
nipkow@13145
  2041
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
krauss@22493
  2042
by (induct xs zs rule:list_induct2') auto
wenzelm@13114
  2043
wenzelm@13114
  2044
lemma zip_append2:
krauss@22493
  2045
"zip xs (ys @ zs) =
nipkow@13145
  2046
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
krauss@22493
  2047
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  2048
wenzelm@13142
  2049
lemma zip_append [simp]:
wenzelm@13142
  2050
 "[| length xs = length us; length ys = length vs |] ==>
nipkow@13145
  2051
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
nipkow@13145
  2052
by (simp add: zip_append1)
wenzelm@13114
  2053
wenzelm@13114
  2054
lemma zip_rev:
nipkow@14247
  2055
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
nipkow@14247
  2056
by (induct rule:list_induct2, simp_all)
wenzelm@13114
  2057
hoelzl@33639
  2058
lemma zip_map_map:
hoelzl@33639
  2059
  "zip (map f xs) (map g ys) = map (\<lambda> (x, y). (f x, g y)) (zip xs ys)"
hoelzl@33639
  2060
proof (induct xs arbitrary: ys)
hoelzl@33639
  2061
  case (Cons x xs) note Cons_x_xs = Cons.hyps
hoelzl@33639
  2062
  show ?case
hoelzl@33639
  2063
  proof (cases ys)
hoelzl@33639
  2064
    case (Cons y ys')
hoelzl@33639
  2065
    show ?thesis unfolding Cons using Cons_x_xs by simp
hoelzl@33639
  2066
  qed simp
hoelzl@33639
  2067
qed simp
hoelzl@33639
  2068
hoelzl@33639
  2069
lemma zip_map1:
hoelzl@33639
  2070
  "zip (map f xs) ys = map (\<lambda>(x, y). (f x, y)) (zip xs ys)"
hoelzl@33639
  2071
using zip_map_map[of f xs "\<lambda>x. x" ys] by simp
hoelzl@33639
  2072
hoelzl@33639
  2073
lemma zip_map2:
hoelzl@33639
  2074
  "zip xs (map f ys) = map (\<lambda>(x, y). (x, f y)) (zip xs ys)"
hoelzl@33639
  2075
using zip_map_map[of "\<lambda>x. x" xs f ys] by simp
hoelzl@33639
  2076
nipkow@23096
  2077
lemma map_zip_map:
hoelzl@33639
  2078
  "map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)"
hoelzl@33639
  2079
unfolding zip_map1 by auto
nipkow@23096
  2080
nipkow@23096
  2081
lemma map_zip_map2:
hoelzl@33639
  2082
  "map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)"
hoelzl@33639
  2083
unfolding zip_map2 by auto
nipkow@23096
  2084
nipkow@31080
  2085
text{* Courtesy of Andreas Lochbihler: *}
nipkow@31080
  2086
lemma zip_same_conv_map: "zip xs xs = map (\<lambda>x. (x, x)) xs"
nipkow@31080
  2087
by(induct xs) auto
nipkow@31080
  2088
wenzelm@13142
  2089
lemma nth_zip [simp]:
nipkow@24526
  2090
"[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"
nipkow@24526
  2091
apply (induct ys arbitrary: i xs, simp)
nipkow@13145
  2092
apply (case_tac xs)
nipkow@13145
  2093
 apply (simp_all add: nth.simps split: nat.split)
nipkow@13145
  2094
done
wenzelm@13114
  2095
wenzelm@13114
  2096
lemma set_zip:
nipkow@13145
  2097
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
nipkow@31080
  2098
by(simp add: set_conv_nth cong: rev_conj_cong)
wenzelm@13114
  2099
hoelzl@33639
  2100
lemma zip_same: "((a,b) \<in> set (zip xs xs)) = (a \<in> set xs \<and> a = b)"
hoelzl@33639
  2101
by(induct xs) auto
hoelzl@33639
  2102
wenzelm@13114
  2103
lemma zip_update:
nipkow@31080
  2104
  "zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
nipkow@31080
  2105
by(rule sym, simp add: update_zip)
wenzelm@13114
  2106
wenzelm@13142
  2107
lemma zip_replicate [simp]:
nipkow@24526
  2108
  "zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
nipkow@24526
  2109
apply (induct i arbitrary: j, auto)
paulson@14208
  2110
apply (case_tac j, auto)
nipkow@13145
  2111
done
wenzelm@13114
  2112
nipkow@19487
  2113
lemma take_zip:
nipkow@24526
  2114
  "take n (zip xs ys) = zip (take n xs) (take n ys)"
nipkow@24526
  2115
apply (induct n arbitrary: xs ys)
nipkow@19487
  2116
 apply simp
nipkow@19487
  2117
apply (case_tac xs, simp)
nipkow@19487
  2118
apply (case_tac ys, simp_all)
nipkow@19487
  2119
done
nipkow@19487
  2120
nipkow@19487
  2121
lemma drop_zip:
nipkow@24526
  2122
  "drop n (zip xs ys) = zip (drop n xs) (drop n ys)"
nipkow@24526
  2123
apply (induct n arbitrary: xs ys)
nipkow@19487
  2124
 apply simp
nipkow@19487
  2125
apply (case_tac xs, simp)
nipkow@19487
  2126
apply (case_tac ys, simp_all)
nipkow@19487
  2127
done
nipkow@19487
  2128
hoelzl@33639
  2129
lemma zip_takeWhile_fst: "zip (takeWhile P xs) ys = takeWhile (P \<circ> fst) (zip xs ys)"
hoelzl@33639
  2130
proof (induct xs arbitrary: ys)
hoelzl@33639
  2131
  case (Cons x xs) thus ?case by (cases ys) auto
hoelzl@33639
  2132
qed simp
hoelzl@33639
  2133
hoelzl@33639
  2134
lemma zip_takeWhile_snd: "zip xs (takeWhile P ys) = takeWhile (P \<circ> snd) (zip xs ys)"
hoelzl@33639
  2135
proof (induct xs arbitrary: ys)
hoelzl@33639
  2136
  case (Cons x xs) thus ?case by (cases ys) auto
hoelzl@33639
  2137
qed simp
hoelzl@33639
  2138
krauss@22493
  2139
lemma set_zip_leftD:
krauss@22493
  2140
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs"
krauss@22493
  2141
by (induct xs ys rule:list_induct2') auto
krauss@22493
  2142
krauss@22493
  2143
lemma set_zip_rightD:
krauss@22493
  2144
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys"
krauss@22493
  2145
by (induct xs ys rule:list_induct2') auto
wenzelm@13142
  2146
nipkow@23983
  2147
lemma in_set_zipE:
nipkow@23983
  2148
  "(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23983
  2149
by(blast dest: set_zip_leftD set_zip_rightD)
nipkow@23983
  2150
haftmann@29766
  2151
lemma zip_map_fst_snd:
haftmann@29766
  2152
  "zip (map fst zs) (map snd zs) = zs"
haftmann@29766
  2153
  by (induct zs) simp_all
haftmann@29766
  2154
haftmann@29766
  2155
lemma zip_eq_conv:
haftmann@29766
  2156
  "length xs = length ys \<Longrightarrow> zip xs ys = zs \<longleftrightarrow> map fst zs = xs \<and> map snd zs = ys"
haftmann@29766
  2157
  by (auto simp add: zip_map_fst_snd)
haftmann@29766
  2158
wenzelm@35118
  2159
nipkow@15392
  2160
subsubsection {* @{text list_all2} *}
wenzelm@13114
  2161
kleing@14316
  2162
lemma list_all2_lengthD [intro?]: 
kleing@14316
  2163
  "list_all2 P xs ys ==> length xs = length ys"
nipkow@24349
  2164
by (simp add: list_all2_def)
haftmann@19607
  2165
haftmann@19787
  2166
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])"
nipkow@24349
  2167
by (simp add: list_all2_def)
haftmann@19607
  2168
haftmann@19787
  2169
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])"
nipkow@24349
  2170
by (simp add: list_all2_def)
haftmann@19607
  2171
haftmann@19607
  2172
lemma list_all2_Cons [iff, code]:
haftmann@19607
  2173
  "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
nipkow@24349
  2174
by (auto simp add: list_all2_def)
wenzelm@13114
  2175
wenzelm@13114
  2176
lemma list_all2_Cons1:
nipkow@13145
  2177
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
nipkow@13145
  2178
by (cases ys) auto
wenzelm@13114
  2179
wenzelm@13114
  2180
lemma list_all2_Cons2:
nipkow@13145
  2181
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
nipkow@13145
  2182
by (cases xs) auto
wenzelm@13114
  2183
wenzelm@13142
  2184
lemma list_all2_rev [iff]:
nipkow@13145
  2185
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
nipkow@13145
  2186
by (simp add: list_all2_def zip_rev cong: conj_cong)
wenzelm@13114
  2187
kleing@13863
  2188
lemma list_all2_rev1:
kleing@13863
  2189
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"
kleing@13863
  2190
by (subst list_all2_rev [symmetric]) simp
kleing@13863
  2191
wenzelm@13114
  2192
lemma list_all2_append1:
nipkow@13145
  2193
"list_all2 P (xs @ ys) zs =
nipkow@13145
  2194
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
nipkow@13145
  2195
list_all2 P xs us \<and> list_all2 P ys vs)"
nipkow@13145
  2196
apply (simp add: list_all2_def zip_append1)
nipkow@13145
  2197
apply (rule iffI)
nipkow@13145
  2198
 apply (rule_tac x = "take (length xs) zs" in exI)
nipkow@13145
  2199
 apply (rule_tac x = "drop (length xs) zs" in exI)
paulson@14208
  2200
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  2201
apply (simp add: ball_Un)
nipkow@13145
  2202
done
wenzelm@13114
  2203
wenzelm@13114
  2204
lemma list_all2_append2:
nipkow@13145
  2205
"list_all2 P xs (ys @ zs) =
nipkow@13145
  2206
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
nipkow@13145
  2207
list_all2 P us ys \<and> list_all2 P vs zs)"
nipkow@13145
  2208
apply (simp add: list_all2_def zip_append2)
nipkow@13145
  2209
apply (rule iffI)
nipkow@13145
  2210
 apply (rule_tac x = "take (length ys) xs" in exI)
nipkow@13145
  2211
 apply (rule_tac x = "drop (length ys) xs" in exI)
paulson@14208
  2212
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  2213
apply (simp add: ball_Un)
nipkow@13145
  2214
done
wenzelm@13114
  2215
kleing@13863
  2216
lemma list_all2_append:
nipkow@14247
  2217
  "length xs = length ys \<Longrightarrow>
nipkow@14247
  2218
  list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"
nipkow@14247
  2219
by (induct rule:list_induct2, simp_all)
kleing@13863
  2220
kleing@13863
  2221
lemma list_all2_appendI [intro?, trans]:
kleing@13863
  2222
  "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"
nipkow@24349
  2223
by (simp add: list_all2_append list_all2_lengthD)
kleing@13863
  2224
wenzelm@13114
  2225
lemma list_all2_conv_all_nth:
nipkow@13145
  2226
"list_all2 P xs ys =
nipkow@13145
  2227
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
nipkow@13145
  2228
by (force simp add: list_all2_def set_zip)
wenzelm@13114
  2229
berghofe@13883
  2230
lemma list_all2_trans:
berghofe@13883
  2231
  assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"
berghofe@13883
  2232
  shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"
berghofe@13883
  2233
        (is "!!bs cs. PROP ?Q as bs cs")
berghofe@13883
  2234
proof (induct as)
berghofe@13883
  2235
  fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"
berghofe@13883
  2236
  show "!!cs. PROP ?Q (x # xs) bs cs"
berghofe@13883
  2237
  proof (induct bs)
berghofe@13883
  2238
    fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs"
berghofe@13883
  2239
    show "PROP ?Q (x # xs) (y # ys) cs"
berghofe@13883
  2240
      by (induct cs) (auto intro: tr I1 I2)
berghofe@13883
  2241
  qed simp
berghofe@13883
  2242
qed simp
berghofe@13883
  2243
kleing@13863
  2244
lemma list_all2_all_nthI [intro?]:
kleing@13863
  2245
  "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"
nipkow@24349
  2246
by (simp add: list_all2_conv_all_nth)
kleing@13863
  2247
paulson@14395
  2248
lemma list_all2I:
paulson@14395
  2249
  "\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b"
nipkow@24349
  2250
by (simp add: list_all2_def)
paulson@14395
  2251
kleing@14328
  2252
lemma list_all2_nthD:
kleing@13863
  2253
  "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  2254
by (simp add: list_all2_conv_all_nth)
kleing@13863
  2255
nipkow@14302
  2256
lemma list_all2_nthD2:
nipkow@14302
  2257
  "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  2258
by (frule list_all2_lengthD) (auto intro: list_all2_nthD)
nipkow@14302
  2259
kleing@13863
  2260
lemma list_all2_map1: 
kleing@13863
  2261
  "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"
nipkow@24349
  2262
by (simp add: list_all2_conv_all_nth)
kleing@13863
  2263
kleing@13863
  2264
lemma list_all2_map2: 
kleing@13863
  2265
  "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"
nipkow@24349
  2266
by (auto simp add: list_all2_conv_all_nth)
kleing@13863
  2267
kleing@14316
  2268
lemma list_all2_refl [intro?]:
kleing@13863
  2269
  "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"
nipkow@24349
  2270
by (simp add: list_all2_conv_all_nth)
kleing@13863
  2271
kleing@13863
  2272
lemma list_all2_update_cong:
kleing@13863
  2273
  "\<lbrakk> i<size xs; list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
nipkow@24349
  2274
by (simp add: list_all2_conv_all_nth nth_list_update)
kleing@13863
  2275
kleing@13863
  2276
lemma list_all2_update_cong2:
kleing@13863
  2277
  "\<lbrakk>list_all2 P xs ys; P x y; i < length ys\<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
nipkow@24349
  2278
by (simp add: list_all2_lengthD list_all2_update_cong)
kleing@13863
  2279
nipkow@14302
  2280
lemma list_all2_takeI [simp,intro?]:
nipkow@24526
  2281
  "list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)"
nipkow@24526
  2282
apply (induct xs arbitrary: n ys)
nipkow@24526
  2283
 apply simp
nipkow@24526
  2284
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  2285
apply (case_tac n)
nipkow@24526
  2286
apply auto
nipkow@24526
  2287
done
nipkow@14302
  2288
nipkow@14302
  2289
lemma list_all2_dropI [simp,intro?]:
nipkow@24526
  2290
  "list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)"
nipkow@24526
  2291
apply (induct as arbitrary: n bs, simp)
nipkow@24526
  2292
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  2293
apply (case_tac n, simp, simp)
nipkow@24526
  2294
done
kleing@13863
  2295
kleing@14327
  2296
lemma list_all2_mono [intro?]:
nipkow@24526
  2297
  "list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys"
nipkow@24526
  2298
apply (induct xs arbitrary: ys, simp)
nipkow@24526
  2299
apply (case_tac ys, auto)
nipkow@24526
  2300
done
kleing@13863
  2301
haftmann@22551
  2302
lemma list_all2_eq:
haftmann@22551
  2303
  "xs = ys \<longleftrightarrow> list_all2 (op =) xs ys"
nipkow@24349
  2304
by (induct xs ys rule: list_induct2') auto
haftmann@22551
  2305
nipkow@40476
  2306
lemma list_eq_iff_zip_eq:
nipkow@40476
  2307
  "xs = ys \<longleftrightarrow> length xs = length ys \<and> (\<forall>(x,y) \<in> set (zip xs ys). x = y)"
nipkow@40476
  2308
by(auto simp add: set_zip list_all2_eq list_all2_conv_all_nth cong: conj_cong)
nipkow@40476
  2309
wenzelm@13114
  2310
nipkow@15392
  2311
subsubsection {* @{text foldl} and @{text foldr} *}
wenzelm@13114
  2312
wenzelm@13142
  2313
lemma foldl_append [simp]:
nipkow@24526
  2314
  "foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
nipkow@24526
  2315
by (induct xs arbitrary: a) auto
wenzelm@13114
  2316
nipkow@14402
  2317
lemma foldr_append[simp]: "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)"
nipkow@14402
  2318
by (induct xs) auto
nipkow@14402
  2319
nipkow@23096
  2320
lemma foldr_map: "foldr g (map f xs) a = foldr (g o f) xs a"
nipkow@23096
  2321
by(induct xs) simp_all
nipkow@23096
  2322
nipkow@24449
  2323
text{* For efficient code generation: avoid intermediate list. *}
haftmann@31998
  2324
lemma foldl_map[code_unfold]:
nipkow@24449
  2325
  "foldl g a (map f xs) = foldl (%a x. g a (f x)) a xs"
nipkow@23096
  2326
by(induct xs arbitrary:a) simp_all
nipkow@23096
  2327
haftmann@34965
  2328
lemma foldl_apply:
haftmann@34965
  2329
  assumes "\<And>x. x \<in> set xs \<Longrightarrow> f x \<circ> h = h \<circ> g x"
haftmann@34965
  2330
  shows "foldl (\<lambda>s x. f x s) (h s) xs = h (foldl (\<lambda>s x. g x s) s xs)"
nipkow@39535
  2331
  by (rule sym, insert assms, induct xs arbitrary: s) (simp_all add: fun_eq_iff)
haftmann@31929
  2332
krauss@19770
  2333
lemma foldl_cong [fundef_cong, recdef_cong]:
krauss@18336
  2334
  "[| a = b; l = k; !!a x. x : set l ==> f a x = g a x |] 
krauss@18336
  2335
  ==> foldl f a l = foldl g b k"
nipkow@24349
  2336
by (induct k arbitrary: a b l) simp_all
krauss@18336
  2337
krauss@19770
  2338
lemma foldr_cong [fundef_cong, recdef_cong]:
krauss@18336
  2339
  "[| a = b; l = k; !!a x. x : set l ==> f x a = g x a |] 
krauss@18336
  2340
  ==> foldr f l a = foldr g k b"
nipkow@24349
  2341
by (induct k arbitrary: a b l) simp_all
krauss@18336
  2342
haftmann@35195
  2343
lemma foldl_fun_comm:
haftmann@35195
  2344
  assumes "\<And>x y s. f (f s x) y = f (f s y) x"
haftmann@35195
  2345
  shows "f (foldl f s xs) x = foldl f (f s x) xs"
haftmann@35195
  2346
  by (induct xs arbitrary: s)
haftmann@35195
  2347
    (simp_all add: assms)
haftmann@35195
  2348
nipkow@24449
  2349
lemma (in semigroup_add) foldl_assoc:
haftmann@25062
  2350
shows "foldl op+ (x+y) zs = x + (foldl op+ y zs)"
nipkow@24449
  2351
by (induct zs arbitrary: y) (simp_all add:add_assoc)
nipkow@24449
  2352
nipkow@24449
  2353
lemma (in monoid_add) foldl_absorb0:
haftmann@25062
  2354
shows "x + (foldl op+ 0 zs) = foldl op+ x zs"
nipkow@24449
  2355
by (induct zs) (simp_all add:foldl_assoc)
nipkow@24449
  2356
haftmann@35195
  2357
lemma foldl_rev:
haftmann@35195
  2358
  assumes "\<And>x y s. f (f s x) y = f (f s y) x"
haftmann@35195
  2359
  shows "foldl f s (rev xs) = foldl f s xs"
haftmann@35195
  2360
proof (induct xs arbitrary: s)
haftmann@35195
  2361
  case Nil then show ?case by simp
haftmann@35195
  2362
next
haftmann@35195
  2363
  case (Cons x xs) with assms show ?case by (simp add: foldl_fun_comm)
haftmann@35195
  2364
qed
haftmann@35195
  2365
haftmann@37605
  2366
lemma rev_foldl_cons [code]:
haftmann@37605
  2367
  "rev xs = foldl (\<lambda>xs x. x # xs) [] xs"
haftmann@37605
  2368
proof (induct xs)
haftmann@37605
  2369
  case Nil then show ?case by simp
haftmann@37605
  2370
next
haftmann@37605
  2371
  case Cons
haftmann@37605
  2372
  {
haftmann@37605
  2373
    fix x xs ys
haftmann@37605
  2374
    have "foldl (\<lambda>xs x. x # xs) ys xs @ [x]
haftmann@37605
  2375
      = foldl (\<lambda>xs x. x # xs) (ys @ [x]) xs"
haftmann@37605
  2376
    by (induct xs arbitrary: ys) auto
haftmann@37605
  2377
  }
haftmann@37605
  2378
  note aux = this
haftmann@37605
  2379
  show ?case by (induct xs) (auto simp add: Cons aux)
haftmann@37605
  2380
qed
haftmann@37605
  2381
nipkow@24449
  2382
haftmann@40007
  2383
text{* The ``Third Duality Theorem'' in Bird \& Wadler: *}
haftmann@40007
  2384
haftmann@40007
  2385
lemma foldr_foldl:
haftmann@40007
  2386
  "foldr f xs a = foldl (%x y. f y x) a (rev xs)"
haftmann@40007
  2387
  by (induct xs) auto
haftmann@40007
  2388
haftmann@40007
  2389
lemma foldl_foldr:
haftmann@40007
  2390
  "foldl f a xs = foldr (%x y. f y x) (rev xs) a"
haftmann@40007
  2391
  by (simp add: foldr_foldl [of "%x y. f y x" "rev xs"])
haftmann@40007
  2392
haftmann@40007
  2393
nipkow@23096
  2394
text{* The ``First Duality Theorem'' in Bird \& Wadler: *}
nipkow@23096
  2395
haftmann@40007
  2396
lemma (in monoid_add) foldl_foldr1_lemma:
haftmann@40007
  2397
  "foldl op + a xs = a + foldr op + xs 0"
haftmann@40007
  2398
  by (induct xs arbitrary: a) (auto simp: add_assoc)
haftmann@40007
  2399
haftmann@40007
  2400
corollary (in monoid_add) foldl_foldr1:
haftmann@40007
  2401
  "foldl op + 0 xs = foldr op + xs 0"
haftmann@40007
  2402
  by (simp add: foldl_foldr1_lemma)
haftmann@40007
  2403
haftmann@40007
  2404
lemma (in ab_semigroup_add) foldr_conv_foldl:
haftmann@40007
  2405
  "foldr op + xs a = foldl op + a xs"
haftmann@40007
  2406
  by (induct xs) (simp_all add: foldl_assoc add.commute)
chaieb@24471
  2407
wenzelm@13142
  2408
text {*
nipkow@13145
  2409
Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
nipkow@13145
  2410
difficult to use because it requires an additional transitivity step.
wenzelm@13142
  2411
*}
wenzelm@13114
  2412
nipkow@24526
  2413
lemma start_le_sum: "(m::nat) <= n ==> m <= foldl (op +) n ns"
nipkow@24526
  2414
by (induct ns arbitrary: n) auto
nipkow@24526
  2415
nipkow@24526
  2416
lemma elem_le_sum: "(n::nat) : set ns ==> n <= foldl (op +) 0 ns"
nipkow@13145
  2417
by (force intro: start_le_sum simp add: in_set_conv_decomp)
wenzelm@13114
  2418
wenzelm@13142
  2419
lemma sum_eq_0_conv [iff]:
nipkow@24526
  2420
  "(foldl (op +) (m::nat) ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
nipkow@24526
  2421
by (induct ns arbitrary: m) auto
wenzelm@13114
  2422
chaieb@24471
  2423
lemma foldr_invariant: 
chaieb@24471
  2424
  "\<lbrakk>Q x ; \<forall> x\<in> set xs. P x; \<forall> x y. P x \<and> Q y \<longrightarrow> Q (f x y) \<rbrakk> \<Longrightarrow> Q (foldr f xs x)"
chaieb@24471
  2425
  by (induct xs, simp_all)
chaieb@24471
  2426
chaieb@24471
  2427
lemma foldl_invariant: 
chaieb@24471
  2428
  "\<lbrakk>Q x ; \<forall> x\<in> set xs. P x; \<forall> x y. P x \<and> Q y \<longrightarrow> Q (f y x) \<rbrakk> \<Longrightarrow> Q (foldl f x xs)"
chaieb@24471
  2429
  by (induct xs arbitrary: x, simp_all)
chaieb@24471
  2430
haftmann@34965
  2431
lemma foldl_weak_invariant:
haftmann@34965
  2432
  assumes "P s"
haftmann@34965
  2433
    and "\<And>s x. x \<in> set xs \<Longrightarrow> P s \<Longrightarrow> P (f s x)"
haftmann@34965
  2434
  shows "P (foldl f s xs)"
haftmann@34965
  2435
  using assms by (induct xs arbitrary: s) simp_all
haftmann@34965
  2436
haftmann@31455
  2437
text {* @{const foldl} and @{const concat} *}
nipkow@24449
  2438
nipkow@24449
  2439
lemma foldl_conv_concat:
haftmann@29719
  2440
  "foldl (op @) xs xss = xs @ concat xss"
haftmann@29719
  2441
proof (induct xss arbitrary: xs)
haftmann@29719
  2442
  case Nil show ?case by simp
haftmann@29719
  2443
next
haftmann@35267
  2444
  interpret monoid_add "op @" "[]" proof qed simp_all
haftmann@29719
  2445
  case Cons then show ?case by (simp add: foldl_absorb0)
haftmann@29719
  2446
qed
haftmann@29719
  2447
haftmann@29719
  2448
lemma concat_conv_foldl: "concat xss = foldl (op @) [] xss"
haftmann@29719
  2449
  by (simp add: foldl_conv_concat)
haftmann@29719
  2450
haftmann@31455
  2451
text {* @{const Finite_Set.fold} and @{const foldl} *}
haftmann@31455
  2452
haftmann@35195
  2453
lemma (in fun_left_comm) fold_set_remdups:
haftmann@35195
  2454
  "fold f y (set xs) = foldl (\<lambda>y x. f x y) y (remdups xs)"
haftmann@35195
  2455
  by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_comm insert_absorb)
haftmann@35195
  2456
haftmann@31455
  2457
lemma (in fun_left_comm_idem) fold_set:
haftmann@31455
  2458
  "fold f y (set xs) = foldl (\<lambda>y x. f x y) y xs"
haftmann@31455
  2459
  by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_comm)
haftmann@31455
  2460
haftmann@32681
  2461
lemma (in ab_semigroup_idem_mult) fold1_set:
haftmann@32681
  2462
  assumes "xs \<noteq> []"
haftmann@32681
  2463
  shows "fold1 times (set xs) = foldl times (hd xs) (tl xs)"
haftmann@32681
  2464
proof -
haftmann@32681
  2465
  interpret fun_left_comm_idem times by (fact fun_left_comm_idem)
haftmann@32681
  2466
  from assms obtain y ys where xs: "xs = y # ys"
haftmann@32681
  2467
    by (cases xs) auto
haftmann@32681
  2468
  show ?thesis
haftmann@32681
  2469
  proof (cases "set ys = {}")
haftmann@32681
  2470
    case True with xs show ?thesis by simp
haftmann@32681
  2471
  next
haftmann@32681
  2472
    case False
haftmann@32681
  2473
    then have "fold1 times (insert y (set ys)) = fold times y (set ys)"
haftmann@32681
  2474
      by (simp only: finite_set fold1_eq_fold_idem)
haftmann@32681
  2475
    with xs show ?thesis by (simp add: fold_set mult_commute)
haftmann@32681
  2476
  qed
haftmann@32681
  2477
qed
haftmann@32681
  2478
haftmann@32681
  2479
lemma (in lattice) Inf_fin_set_fold [code_unfold]:
haftmann@32681
  2480
  "Inf_fin (set (x # xs)) = foldl inf x xs"
haftmann@32681
  2481
proof -
haftmann@32681
  2482
  interpret ab_semigroup_idem_mult "inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@32681
  2483
    by (fact ab_semigroup_idem_mult_inf)
haftmann@32681
  2484
  show ?thesis
haftmann@32681
  2485
    by (simp add: Inf_fin_def fold1_set del: set.simps)
haftmann@32681
  2486
qed
haftmann@32681
  2487
haftmann@32681
  2488
lemma (in lattice) Sup_fin_set_fold [code_unfold]:
haftmann@32681
  2489
  "Sup_fin (set (x # xs)) = foldl sup x xs"
haftmann@32681
  2490
proof -
haftmann@32681
  2491
  interpret ab_semigroup_idem_mult "sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@32681
  2492
    by (fact ab_semigroup_idem_mult_sup)
haftmann@32681
  2493
  show ?thesis
haftmann@32681
  2494
    by (simp add: Sup_fin_def fold1_set del: set.simps)
haftmann@32681
  2495
qed
haftmann@32681
  2496
haftmann@32681
  2497
lemma (in linorder) Min_fin_set_fold [code_unfold]:
haftmann@32681
  2498
  "Min (set (x # xs)) = foldl min x xs"
haftmann@32681
  2499
proof -
haftmann@32681
  2500
  interpret ab_semigroup_idem_mult "min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@32681
  2501
    by (fact ab_semigroup_idem_mult_min)
haftmann@32681
  2502
  show ?thesis
haftmann@32681
  2503
    by (simp add: Min_def fold1_set del: set.simps)
haftmann@32681
  2504
qed
haftmann@32681
  2505
haftmann@32681
  2506
lemma (in linorder) Max_fin_set_fold [code_unfold]:
haftmann@32681
  2507
  "Max (set (x # xs)) = foldl max x xs"
haftmann@32681
  2508
proof -
haftmann@32681
  2509
  interpret ab_semigroup_idem_mult "max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@32681
  2510
    by (fact ab_semigroup_idem_mult_max)
haftmann@32681
  2511
  show ?thesis
haftmann@32681
  2512
    by (simp add: Max_def fold1_set del: set.simps)
haftmann@32681
  2513
qed
haftmann@32681
  2514
haftmann@32681
  2515
lemma (in complete_lattice) Inf_set_fold [code_unfold]:
haftmann@32681
  2516
  "Inf (set xs) = foldl inf top xs"
haftmann@33998
  2517
proof -
haftmann@33998
  2518
  interpret fun_left_comm_idem "inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@33998
  2519
    by (fact fun_left_comm_idem_inf)
haftmann@33998
  2520
  show ?thesis by (simp add: Inf_fold_inf fold_set inf_commute)
haftmann@33998
  2521
qed
haftmann@32681
  2522
haftmann@32681
  2523
lemma (in complete_lattice) Sup_set_fold [code_unfold]:
haftmann@32681
  2524
  "Sup (set xs) = foldl sup bot xs"
haftmann@33998
  2525
proof -
haftmann@33998
  2526
  interpret fun_left_comm_idem "sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@33998
  2527
    by (fact fun_left_comm_idem_sup)
haftmann@33998
  2528
  show ?thesis by (simp add: Sup_fold_sup fold_set sup_commute)
haftmann@33998
  2529
qed
haftmann@33998
  2530
haftmann@33998
  2531
lemma (in complete_lattice) INFI_set_fold:
haftmann@33998
  2532
  "INFI (set xs) f = foldl (\<lambda>y x. inf (f x) y) top xs"
haftmann@33998
  2533
  unfolding INFI_def set_map [symmetric] Inf_set_fold foldl_map
haftmann@33998
  2534
    by (simp add: inf_commute)
haftmann@33998
  2535
haftmann@33998
  2536
lemma (in complete_lattice) SUPR_set_fold:
haftmann@33998
  2537
  "SUPR (set xs) f = foldl (\<lambda>y x. sup (f x) y) bot xs"
haftmann@33998
  2538
  unfolding SUPR_def set_map [symmetric] Sup_set_fold foldl_map
haftmann@33998
  2539
    by (simp add: sup_commute)
haftmann@31455
  2540
wenzelm@35118
  2541
nipkow@24645
  2542
subsubsection {* @{text upt} *}
wenzelm@13142
  2543
nipkow@17090
  2544
lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])"
nipkow@17090
  2545
-- {* simp does not terminate! *}
nipkow@13145
  2546
by (induct j) auto
wenzelm@13114
  2547
nipkow@32005
  2548
lemmas upt_rec_number_of[simp] = upt_rec[of "number_of m" "number_of n", standard]
nipkow@32005
  2549
nipkow@15425
  2550
lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []"
nipkow@13145
  2551
by (subst upt_rec) simp
wenzelm@13114
  2552
nipkow@15425
  2553
lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)"
nipkow@15281
  2554
by(induct j)simp_all
nipkow@15281
  2555
nipkow@15281
  2556
lemma upt_eq_Cons_conv:
nipkow@24526
  2557
 "([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)"
nipkow@24526
  2558
apply(induct j arbitrary: x xs)
nipkow@15281
  2559
 apply simp
nipkow@15281
  2560
apply(clarsimp simp add: append_eq_Cons_conv)
nipkow@15281
  2561
apply arith
nipkow@15281
  2562
done
nipkow@15281
  2563
nipkow@15425
  2564
lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]"
nipkow@13145
  2565
-- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
nipkow@13145
  2566
by simp
wenzelm@13114
  2567
nipkow@15425
  2568
lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]"
haftmann@26734
  2569
  by (simp add: upt_rec)
wenzelm@13114
  2570
nipkow@15425
  2571
lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]"
nipkow@13145
  2572
-- {* LOOPS as a simprule, since @{text "j <= j"}. *}
nipkow@13145
  2573
by (induct k) auto
wenzelm@13114
  2574
nipkow@15425
  2575
lemma length_upt [simp]: "length [i..<j] = j - i"
nipkow@13145
  2576
by (induct j) (auto simp add: Suc_diff_le)
wenzelm@13114
  2577
nipkow@15425
  2578
lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k"
nipkow@13145
  2579
apply (induct j)
nipkow@13145
  2580
apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
nipkow@13145
  2581
done
wenzelm@13114
  2582
nipkow@17906
  2583
nipkow@17906
  2584
lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i"
nipkow@17906
  2585
by(simp add:upt_conv_Cons)
nipkow@17906
  2586
nipkow@17906
  2587
lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1"
nipkow@17906
  2588
apply(cases j)
nipkow@17906
  2589
 apply simp
nipkow@17906
  2590
by(simp add:upt_Suc_append)
nipkow@17906
  2591
nipkow@24526
  2592
lemma take_upt [simp]: "i+m <= n ==> take m [i..<n] = [i..<i+m]"
nipkow@24526
  2593
apply (induct m arbitrary: i, simp)
nipkow@13145
  2594
apply (subst upt_rec)
nipkow@13145
  2595
apply (rule sym)
nipkow@13145
  2596
apply (subst upt_rec)
nipkow@13145
  2597
apply (simp del: upt.simps)
nipkow@13145
  2598
done
wenzelm@13114
  2599
nipkow@17501
  2600
lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]"
nipkow@17501
  2601
apply(induct j)
nipkow@17501
  2602
apply auto
nipkow@17501
  2603
done
nipkow@17501
  2604
nipkow@24645
  2605
lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]"
nipkow@13145
  2606
by (induct n) auto
wenzelm@13114
  2607
nipkow@24526
  2608
lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)"
nipkow@24526
  2609
apply (induct n m  arbitrary: i rule: diff_induct)
nipkow@13145
  2610
prefer 3 apply (subst map_Suc_upt[symmetric])
nipkow@13145
  2611
apply (auto simp add: less_diff_conv nth_upt)
nipkow@13145
  2612
done
wenzelm@13114
  2613
berghofe@13883
  2614
lemma nth_take_lemma:
nipkow@24526
  2615
  "k <= length xs ==> k <= length ys ==>
berghofe@13883
  2616
     (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"
nipkow@24526
  2617
apply (atomize, induct k arbitrary: xs ys)
paulson@14208
  2618
apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)
nipkow@13145
  2619
txt {* Both lists must be non-empty *}
paulson@14208
  2620
apply (case_tac xs, simp)
paulson@14208
  2621
apply (case_tac ys, clarify)
nipkow@13145
  2622
 apply (simp (no_asm_use))
nipkow@13145
  2623
apply clarify
nipkow@13145
  2624
txt {* prenexing's needed, not miniscoping *}
nipkow@13145
  2625
apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
nipkow@13145
  2626
apply blast
nipkow@13145
  2627
done
wenzelm@13114
  2628
wenzelm@13114
  2629
lemma nth_equalityI:
wenzelm@13114
  2630
 "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
nipkow@13145
  2631
apply (frule nth_take_lemma [OF le_refl eq_imp_le])
nipkow@13145
  2632
apply (simp_all add: take_all)
nipkow@13145
  2633
done
wenzelm@13114
  2634
haftmann@24796
  2635
lemma map_nth:
haftmann@24796
  2636
  "map (\<lambda>i. xs ! i) [0..<length xs] = xs"
haftmann@24796
  2637
  by (rule nth_equalityI, auto)
haftmann@24796
  2638
kleing@13863
  2639
(* needs nth_equalityI *)
kleing@13863
  2640
lemma list_all2_antisym:
kleing@13863
  2641
  "\<lbrakk> (\<And>x y. \<lbrakk>P x y; Q y x\<rbrakk> \<Longrightarrow> x = y); list_all2 P xs ys; list_all2 Q ys xs \<rbrakk> 
kleing@13863
  2642
  \<Longrightarrow> xs = ys"
kleing@13863
  2643
  apply (simp add: list_all2_conv_all_nth) 
paulson@14208
  2644
  apply (rule nth_equalityI, blast, simp)
kleing@13863
  2645
  done
kleing@13863
  2646
wenzelm@13142
  2647
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
nipkow@13145
  2648
-- {* The famous take-lemma. *}
nipkow@13145
  2649
apply (drule_tac x = "max (length xs) (length ys)" in spec)
nipkow@13145
  2650
apply (simp add: le_max_iff_disj take_all)
nipkow@13145
  2651
done
wenzelm@13114
  2652
wenzelm@13114
  2653
nipkow@15302
  2654
lemma take_Cons':
nipkow@15302
  2655
     "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
nipkow@15302
  2656
by (cases n) simp_all
nipkow@15302
  2657
nipkow@15302
  2658
lemma drop_Cons':
nipkow@15302
  2659
     "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
nipkow@15302
  2660
by (cases n) simp_all
nipkow@15302
  2661
nipkow@15302
  2662
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
nipkow@15302
  2663
by (cases n) simp_all
nipkow@15302
  2664
paulson@18622
  2665
lemmas take_Cons_number_of = take_Cons'[of "number_of v",standard]
paulson@18622
  2666
lemmas drop_Cons_number_of = drop_Cons'[of "number_of v",standard]
paulson@18622
  2667
lemmas nth_Cons_number_of = nth_Cons'[of _ _ "number_of v",standard]
paulson@18622
  2668
paulson@18622
  2669
declare take_Cons_number_of [simp] 
paulson@18622
  2670
        drop_Cons_number_of [simp] 
paulson@18622
  2671
        nth_Cons_number_of [simp] 
nipkow@15302
  2672
nipkow@15302
  2673
nipkow@32415
  2674
subsubsection {* @{text upto}: interval-list on @{typ int} *}
nipkow@32415
  2675
nipkow@32415
  2676
(* FIXME make upto tail recursive? *)
nipkow@32415
  2677
nipkow@32415
  2678
function upto :: "int \<Rightarrow> int \<Rightarrow> int list" ("(1[_../_])") where
nipkow@32415
  2679
"upto i j = (if i \<le> j then i # [i+1..j] else [])"
nipkow@32415
  2680
by auto
nipkow@32415
  2681
termination
nipkow@32415
  2682
by(relation "measure(%(i::int,j). nat(j - i + 1))") auto
nipkow@32415
  2683
nipkow@32415
  2684
declare upto.simps[code, simp del]
nipkow@32415
  2685
nipkow@32415
  2686
lemmas upto_rec_number_of[simp] =
nipkow@32415
  2687
  upto.simps[of "number_of m" "number_of n", standard]
nipkow@32415
  2688
nipkow@32415
  2689
lemma upto_empty[simp]: "j < i \<Longrightarrow> [i..j] = []"
nipkow@32415
  2690
by(simp add: upto.simps)
nipkow@32415
  2691
nipkow@32415
  2692
lemma set_upto[simp]: "set[i..j] = {i..j}"
nipkow@32415
  2693
apply(induct i j rule:upto.induct)
nipkow@32415
  2694
apply(simp add: upto.simps simp_from_to)
nipkow@32415
  2695
done
nipkow@32415
  2696
nipkow@32415
  2697
nipkow@15392
  2698
subsubsection {* @{text "distinct"} and @{text remdups} *}
wenzelm@13114
  2699
haftmann@40451
  2700
lemma distinct_tl:
haftmann@40451
  2701
  "distinct xs \<Longrightarrow> distinct (tl xs)"
haftmann@40451
  2702
  by (cases xs) simp_all
haftmann@40451
  2703
wenzelm@13142
  2704
lemma distinct_append [simp]:
nipkow@13145
  2705
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
nipkow@13145
  2706
by (induct xs) auto
wenzelm@13114
  2707
nipkow@15305
  2708
lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs"
nipkow@15305
  2709
by(induct xs) auto
nipkow@15305
  2710
wenzelm@13142
  2711
lemma set_remdups [simp]: "set (remdups xs) = set xs"
nipkow@13145
  2712
by (induct xs) (auto simp add: insert_absorb)
wenzelm@13114
  2713
wenzelm@13142
  2714
lemma distinct_remdups [iff]: "distinct (remdups xs)"
nipkow@13145
  2715
by (induct xs) auto
wenzelm@13114
  2716
nipkow@25287
  2717
lemma distinct_remdups_id: "distinct xs ==> remdups xs = xs"
nipkow@25287
  2718
by (induct xs, auto)
nipkow@25287
  2719
haftmann@26734
  2720
lemma remdups_id_iff_distinct [simp]: "remdups xs = xs \<longleftrightarrow> distinct xs"
haftmann@26734
  2721
by (metis distinct_remdups distinct_remdups_id)
nipkow@25287
  2722
nipkow@24566
  2723
lemma finite_distinct_list: "finite A \<Longrightarrow> EX xs. set xs = A & distinct xs"
paulson@24632
  2724
by (metis distinct_remdups finite_list set_remdups)
nipkow@24566
  2725
paulson@15072
  2726
lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])"
nipkow@24349
  2727
by (induct x, auto) 
paulson@15072
  2728
paulson@15072
  2729
lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])"
nipkow@24349
  2730
by (induct x, auto)
paulson@15072
  2731
nipkow@15245
  2732
lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs"
nipkow@15245
  2733
by (induct xs) auto
nipkow@15245
  2734
nipkow@15245
  2735
lemma length_remdups_eq[iff]:
nipkow@15245
  2736
  "(length (remdups xs) = length xs) = (remdups xs = xs)"
nipkow@15245
  2737
apply(induct xs)
nipkow@15245
  2738
 apply auto
nipkow@15245
  2739
apply(subgoal_tac "length (remdups xs) <= length xs")
nipkow@15245
  2740
 apply arith
nipkow@15245
  2741
apply(rule length_remdups_leq)
nipkow@15245
  2742
done
nipkow@15245
  2743
nipkow@33911
  2744
lemma remdups_filter: "remdups(filter P xs) = filter P (remdups xs)"
nipkow@33911
  2745
apply(induct xs)
nipkow@33911
  2746
apply auto
nipkow@33911
  2747
done
nipkow@18490
  2748
nipkow@18490
  2749
lemma distinct_map:
nipkow@18490
  2750
  "distinct(map f xs) = (distinct xs & inj_on f (set xs))"
nipkow@18490
  2751
by (induct xs) auto
nipkow@18490
  2752
nipkow@18490
  2753
wenzelm@13142
  2754
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"
nipkow@13145
  2755
by (induct xs) auto
wenzelm@13114
  2756
nipkow@17501
  2757
lemma distinct_upt[simp]: "distinct[i..<j]"
nipkow@17501
  2758
by (induct j) auto
nipkow@17501
  2759
nipkow@32415
  2760
lemma distinct_upto[simp]: "distinct[i..j]"
nipkow@32415
  2761
apply(induct i j rule:upto.induct)
nipkow@32415
  2762
apply(subst upto.simps)
nipkow@32415
  2763
apply(simp)
nipkow@32415
  2764
done
nipkow@32415
  2765
nipkow@24526
  2766
lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)"
nipkow@24526
  2767
apply(induct xs arbitrary: i)
nipkow@17501
  2768
 apply simp
nipkow@17501
  2769
apply (case_tac i)
nipkow@17501
  2770
 apply simp_all
nipkow@17501
  2771
apply(blast dest:in_set_takeD)
nipkow@17501
  2772
done
nipkow@17501
  2773
nipkow@24526
  2774
lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)"
nipkow@24526
  2775
apply(induct xs arbitrary: i)
nipkow@17501
  2776
 apply simp
nipkow@17501
  2777
apply (case_tac i)
nipkow@17501
  2778
 apply simp_all
nipkow@17501
  2779
done
nipkow@17501
  2780
nipkow@17501
  2781
lemma distinct_list_update:
nipkow@17501
  2782
assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}"
nipkow@17501
  2783
shows "distinct (xs[i:=a])"
nipkow@17501
  2784
proof (cases "i < length xs")
nipkow@17501
  2785
  case True
nipkow@17501
  2786
  with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}"
nipkow@17501
  2787
    apply (drule_tac id_take_nth_drop) by simp
nipkow@17501
  2788
  with d True show ?thesis
nipkow@17501
  2789
    apply (simp add: upd_conv_take_nth_drop)
nipkow@17501
  2790
    apply (drule subst [OF id_take_nth_drop]) apply assumption
nipkow@17501
  2791
    apply simp apply (cases "a = xs!i") apply simp by blast
nipkow@17501
  2792
next
nipkow@17501
  2793
  case False with d show ?thesis by auto
nipkow@17501
  2794
qed
nipkow@17501
  2795
hoelzl@31350
  2796
lemma distinct_concat:
hoelzl@31350
  2797
  assumes "distinct xs"
hoelzl@31350
  2798
  and "\<And> ys. ys \<in> set xs \<Longrightarrow> distinct ys"
hoelzl@31350
  2799
  and "\<And> ys zs. \<lbrakk> ys \<in> set xs ; zs \<in> set xs ; ys \<noteq> zs \<rbrakk> \<Longrightarrow> set ys \<inter> set zs = {}"
hoelzl@31350
  2800
  shows "distinct (concat xs)"
hoelzl@31350
  2801
  using assms by (induct xs) auto
nipkow@17501
  2802
nipkow@17501
  2803
text {* It is best to avoid this indexed version of distinct, but
nipkow@17501
  2804
sometimes it is useful. *}
nipkow@17501
  2805
nipkow@13124
  2806
lemma distinct_conv_nth:
nipkow@17501
  2807
"distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)"
paulson@15251
  2808
apply (induct xs, simp, simp)
paulson@14208
  2809
apply (rule iffI, clarsimp)
nipkow@13145
  2810
 apply (case_tac i)
paulson@14208
  2811
apply (case_tac j, simp)
nipkow@13145
  2812
apply (simp add: set_conv_nth)
nipkow@13145
  2813
 apply (case_tac j)
paulson@24648
  2814
apply (clarsimp simp add: set_conv_nth, simp) 
nipkow@13145
  2815
apply (rule conjI)
paulson@24648
  2816
(*TOO SLOW
paulson@24632
  2817
apply (metis Zero_neq_Suc gr0_conv_Suc in_set_conv_nth lessI less_trans_Suc nth_Cons' nth_Cons_Suc)
paulson@24648
  2818
*)
paulson@24648
  2819
 apply (clarsimp simp add: set_conv_nth)
paulson@24648
  2820
 apply (erule_tac x = 0 in allE, simp)
paulson@24648
  2821
 apply (erule_tac x = "Suc i" in allE, simp, clarsimp)
wenzelm@25130
  2822
(*TOO SLOW
paulson@24632
  2823
apply (metis Suc_Suc_eq lessI less_trans_Suc nth_Cons_Suc)
wenzelm@25130
  2824
*)
wenzelm@25130
  2825
apply (erule_tac x = "Suc i" in allE, simp)
wenzelm@25130
  2826
apply (erule_tac x = "Suc j" in allE, simp)
nipkow@13145
  2827
done
nipkow@13124
  2828
nipkow@18490
  2829
lemma nth_eq_iff_index_eq:
nipkow@18490
  2830
 "\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)"
nipkow@18490
  2831
by(auto simp: distinct_conv_nth)
nipkow@18490
  2832
nipkow@15110
  2833
lemma distinct_card: "distinct xs ==> card (set xs) = size xs"
nipkow@24349
  2834
by (induct xs) auto
kleing@14388
  2835
nipkow@15110
  2836
lemma card_distinct: "card (set xs) = size xs ==> distinct xs"
kleing@14388
  2837
proof (induct xs)
kleing@14388
  2838
  case Nil thus ?case by simp
kleing@14388
  2839
next
kleing@14388
  2840
  case (Cons x xs)
kleing@14388
  2841
  show ?case
kleing@14388
  2842
  proof (cases "x \<in> set xs")
kleing@14388
  2843
    case False with Cons show ?thesis by simp
kleing@14388
  2844
  next
kleing@14388
  2845
    case True with Cons.prems
kleing@14388
  2846
    have "card (set xs) = Suc (length xs)" 
kleing@14388
  2847
      by (simp add: card_insert_if split: split_if_asm)
kleing@14388
  2848
    moreover have "card (set xs) \<le> length xs" by (rule card_length)
kleing@14388
  2849
    ultimately have False by simp
kleing@14388
  2850
    thus ?thesis ..
kleing@14388
  2851
  qed
kleing@14388
  2852
qed
kleing@14388
  2853
nipkow@25287
  2854
lemma not_distinct_decomp: "~ distinct ws ==> EX xs ys zs y. ws = xs@[y]@ys@[y]@zs"
nipkow@25287
  2855
apply (induct n == "length ws" arbitrary:ws) apply simp
nipkow@25287
  2856
apply(case_tac ws) apply simp
nipkow@25287
  2857
apply (simp split:split_if_asm)
nipkow@25287
  2858
apply (metis Cons_eq_appendI eq_Nil_appendI split_list)
nipkow@25287
  2859
done
nipkow@18490
  2860
nipkow@18490
  2861
lemma length_remdups_concat:
nipkow@18490
  2862
 "length(remdups(concat xss)) = card(\<Union>xs \<in> set xss. set xs)"
nipkow@24308
  2863
by(simp add: set_concat distinct_card[symmetric])
nipkow@17906
  2864
hoelzl@33639
  2865
lemma length_remdups_card_conv: "length(remdups xs) = card(set xs)"
hoelzl@33639
  2866
proof -
hoelzl@33639
  2867
  have xs: "concat[xs] = xs" by simp
hoelzl@33639
  2868
  from length_remdups_concat[of "[xs]"] show ?thesis unfolding xs by simp
hoelzl@33639
  2869
qed
nipkow@17906
  2870
haftmann@36275
  2871
lemma remdups_remdups:
haftmann@36275
  2872
  "remdups (remdups xs) = remdups xs"
haftmann@36275
  2873
  by (induct xs) simp_all
haftmann@36275
  2874
haftmann@36846
  2875
lemma distinct_butlast:
haftmann@36846
  2876
  assumes "xs \<noteq> []" and "distinct xs"
haftmann@36846
  2877
  shows "distinct (butlast xs)"
haftmann@36846
  2878
proof -
haftmann@36846
  2879
  from `xs \<noteq> []` obtain ys y where "xs = ys @ [y]" by (cases xs rule: rev_cases) auto
haftmann@36846
  2880
  with `distinct xs` show ?thesis by simp
haftmann@36846
  2881
qed
haftmann@36846
  2882
haftmann@39960
  2883
lemma remdups_map_remdups:
haftmann@39960
  2884
  "remdups (map f (remdups xs)) = remdups (map f xs)"
haftmann@39960
  2885
  by (induct xs) simp_all
haftmann@39960
  2886
haftmann@40096
  2887
lemma distinct_zipI1:
haftmann@40096
  2888
  assumes "distinct xs"
haftmann@40096
  2889
  shows "distinct (zip xs ys)"
haftmann@40096
  2890
proof (rule zip_obtain_same_length)
haftmann@40096
  2891
  fix xs' :: "'a list" and ys' :: "'b list" and n
haftmann@40096
  2892
  assume "length xs' = length ys'"
haftmann@40096
  2893
  assume "xs' = take n xs"
haftmann@40096
  2894
  with assms have "distinct xs'" by simp
haftmann@40096
  2895
  with `length xs' = length ys'` show "distinct (zip xs' ys')"
haftmann@40096
  2896
    by (induct xs' ys' rule: list_induct2) (auto elim: in_set_zipE)
haftmann@40096
  2897
qed
haftmann@40096
  2898
haftmann@40096
  2899
lemma distinct_zipI2:
haftmann@40096
  2900
  assumes "distinct ys"
haftmann@40096
  2901
  shows "distinct (zip xs ys)"
haftmann@40096
  2902
proof (rule zip_obtain_same_length)
haftmann@40096
  2903
  fix xs' :: "'b list" and ys' :: "'a list" and n
haftmann@40096
  2904
  assume "length xs' = length ys'"
haftmann@40096
  2905
  assume "ys' = take n ys"
haftmann@40096
  2906
  with assms have "distinct ys'" by simp
haftmann@40096
  2907
  with `length xs' = length ys'` show "distinct (zip xs' ys')"
haftmann@40096
  2908
    by (induct xs' ys' rule: list_induct2) (auto elim: in_set_zipE)
haftmann@40096
  2909
qed
haftmann@40096
  2910
wenzelm@35118
  2911
haftmann@37605
  2912
subsubsection {* List summation: @{const listsum} and @{text"\<Sum>"}*}
haftmann@37605
  2913
haftmann@40007
  2914
lemma (in monoid_add) listsum_foldl [code]:
haftmann@40007
  2915
  "listsum = foldl (op +) 0"
haftmann@40007
  2916
  by (simp add: listsum_def foldl_foldr1 fun_eq_iff)
haftmann@40007
  2917
haftmann@40007
  2918
lemma (in monoid_add) listsum_simps [simp]:
haftmann@40007
  2919
  "listsum [] = 0"
haftmann@40007
  2920
  "listsum (x#xs) = x + listsum xs"
haftmann@40007
  2921
  by (simp_all add: listsum_def)
haftmann@40007
  2922
haftmann@40007
  2923
lemma (in monoid_add) listsum_append [simp]:
haftmann@40007
  2924
  "listsum (xs @ ys) = listsum xs + listsum ys"
haftmann@40007
  2925
  by (induct xs) (simp_all add: add.assoc)
haftmann@40007
  2926
haftmann@40007
  2927
lemma (in comm_monoid_add) listsum_rev [simp]:
haftmann@40007
  2928
  "listsum (rev xs) = listsum xs"
haftmann@40007
  2929
  by (simp add: listsum_def [of "rev xs"]) (simp add: listsum_foldl foldr_foldl add.commute)
haftmann@40007
  2930
haftmann@40007
  2931
lemma (in comm_monoid_add) listsum_map_remove1:
haftmann@40007
  2932
  "x \<in> set xs \<Longrightarrow> listsum (map f xs) = f x + listsum (map f (remove1 x xs))"
haftmann@40007
  2933
  by (induct xs) (auto simp add: ac_simps)
haftmann@40007
  2934
haftmann@40007
  2935
lemma (in monoid_add) list_size_conv_listsum:
haftmann@37605
  2936
  "list_size f xs = listsum (map f xs) + size xs"
haftmann@40007
  2937
  by (induct xs) auto
haftmann@40007
  2938
haftmann@40007
  2939
lemma (in monoid_add) length_concat:
haftmann@40007
  2940
  "length (concat xss) = listsum (map length xss)"
haftmann@40007
  2941
  by (induct xss) simp_all
haftmann@40007
  2942
haftmann@40007
  2943
lemma (in monoid_add) listsum_map_filter:
haftmann@40007
  2944
  assumes "\<And>x. x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> f x = 0"
haftmann@37605
  2945
  shows "listsum (map f (filter P xs)) = listsum (map f xs)"
haftmann@40007
  2946
  using assms by (induct xs) auto
haftmann@40007
  2947
haftmann@40007
  2948
lemma (in monoid_add) distinct_listsum_conv_Setsum:
haftmann@40007
  2949
  "distinct xs \<Longrightarrow> listsum xs = Setsum (set xs)"
haftmann@40007
  2950
  by (induct xs) simp_all
haftmann@40007
  2951
haftmann@40007
  2952
lemma listsum_eq_0_nat_iff_nat [simp]:
haftmann@40007
  2953
  "listsum ns = (0::nat) \<longleftrightarrow> (\<forall>n \<in> set ns. n = 0)"
haftmann@40007
  2954
  by (simp add: listsum_foldl)
haftmann@40007
  2955
haftmann@40007
  2956
lemma elem_le_listsum_nat:
haftmann@40007
  2957
  "k < size ns \<Longrightarrow> ns ! k \<le> listsum (ns::nat list)"
haftmann@37605
  2958
apply(induct ns arbitrary: k)
haftmann@37605
  2959
 apply simp
haftmann@37605
  2960
apply(fastsimp simp add:nth_Cons split: nat.split)
haftmann@37605
  2961
done
haftmann@37605
  2962
haftmann@40007
  2963
lemma listsum_update_nat:
haftmann@40007
  2964
  "k<size ns \<Longrightarrow> listsum (ns[k := (n::nat)]) = listsum ns + n - ns ! k"
haftmann@37605
  2965
apply(induct ns arbitrary:k)
haftmann@37605
  2966
 apply (auto split:nat.split)
haftmann@37605
  2967
apply(drule elem_le_listsum_nat)
haftmann@37605
  2968
apply arith
haftmann@37605
  2969
done
haftmann@37605
  2970
haftmann@37605
  2971
text{* Some syntactic sugar for summing a function over a list: *}
haftmann@37605
  2972
haftmann@37605
  2973
syntax
haftmann@37605
  2974
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3SUM _<-_. _)" [0, 51, 10] 10)
haftmann@37605
  2975
syntax (xsymbols)
haftmann@37605
  2976
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
haftmann@37605
  2977
syntax (HTML output)
haftmann@37605
  2978
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
haftmann@37605
  2979
haftmann@37605
  2980
translations -- {* Beware of argument permutation! *}
haftmann@37605
  2981
  "SUM x<-xs. b" == "CONST listsum (CONST map (%x. b) xs)"
haftmann@37605
  2982
  "\<Sum>x\<leftarrow>xs. b" == "CONST listsum (CONST map (%x. b) xs)"
haftmann@37605
  2983
haftmann@40007
  2984
lemma (in monoid_add) listsum_triv:
haftmann@40007
  2985
  "(\<Sum>x\<leftarrow>xs. r) = of_nat (length xs) * r"
haftmann@37605
  2986
  by (induct xs) (simp_all add: left_distrib)
haftmann@37605
  2987
haftmann@40007
  2988
lemma (in monoid_add) listsum_0 [simp]:
haftmann@40007
  2989
  "(\<Sum>x\<leftarrow>xs. 0) = 0"
haftmann@37605
  2990
  by (induct xs) (simp_all add: left_distrib)
haftmann@37605
  2991
haftmann@37605
  2992
text{* For non-Abelian groups @{text xs} needs to be reversed on one side: *}
haftmann@40007
  2993
lemma (in ab_group_add) uminus_listsum_map:
haftmann@40007
  2994
  "- listsum (map f xs) = listsum (map (uminus \<circ> f) xs)"
haftmann@40007
  2995
  by (induct xs) simp_all
haftmann@40007
  2996
haftmann@40007
  2997
lemma (in comm_monoid_add) listsum_addf:
haftmann@40007
  2998
  "(\<Sum>x\<leftarrow>xs. f x + g x) = listsum (map f xs) + listsum (map g xs)"
haftmann@40007
  2999
  by (induct xs) (simp_all add: algebra_simps)
haftmann@40007
  3000
haftmann@40007
  3001
lemma (in ab_group_add) listsum_subtractf:
haftmann@40007
  3002
  "(\<Sum>x\<leftarrow>xs. f x - g x) = listsum (map f xs) - listsum (map g xs)"
haftmann@40007
  3003
  by (induct xs) (simp_all add: algebra_simps)
haftmann@40007
  3004
haftmann@40007
  3005
lemma (in semiring_0) listsum_const_mult:
haftmann@40007
  3006
  "(\<Sum>x\<leftarrow>xs. c * f x) = c * (\<Sum>x\<leftarrow>xs. f x)"
haftmann@40007
  3007
  by (induct xs) (simp_all add: algebra_simps)
haftmann@40007
  3008
haftmann@40007
  3009
lemma (in semiring_0) listsum_mult_const:
haftmann@40007
  3010
  "(\<Sum>x\<leftarrow>xs. f x * c) = (\<Sum>x\<leftarrow>xs. f x) * c"
haftmann@40007
  3011
  by (induct xs) (simp_all add: algebra_simps)
haftmann@40007
  3012
haftmann@40007
  3013
lemma (in ordered_ab_group_add_abs) listsum_abs:
haftmann@40007
  3014
  "\<bar>listsum xs\<bar> \<le> listsum (map abs xs)"
haftmann@40007
  3015
  by (induct xs) (simp_all add: order_trans [OF abs_triangle_ineq])
haftmann@37605
  3016
haftmann@37605
  3017
lemma listsum_mono:
haftmann@40007
  3018
  fixes f g :: "'a \<Rightarrow> 'b::{monoid_add, ordered_ab_semigroup_add}"
haftmann@37605
  3019
  shows "(\<And>x. x \<in> set xs \<Longrightarrow> f x \<le> g x) \<Longrightarrow> (\<Sum>x\<leftarrow>xs. f x) \<le> (\<Sum>x\<leftarrow>xs. g x)"
haftmann@40007
  3020
  by (induct xs) (simp, simp add: add_mono)
haftmann@40007
  3021
haftmann@40007
  3022
lemma (in monoid_add) listsum_distinct_conv_setsum_set:
haftmann@37605
  3023
  "distinct xs \<Longrightarrow> listsum (map f xs) = setsum f (set xs)"
haftmann@37605
  3024
  by (induct xs) simp_all
haftmann@37605
  3025
haftmann@40007
  3026
lemma (in monoid_add) interv_listsum_conv_setsum_set_nat:
haftmann@37605
  3027
  "listsum (map f [m..<n]) = setsum f (set [m..<n])"
haftmann@37605
  3028
  by (simp add: listsum_distinct_conv_setsum_set)
haftmann@37605
  3029
haftmann@40007
  3030
lemma (in monoid_add) interv_listsum_conv_setsum_set_int:
haftmann@37605
  3031
  "listsum (map f [k..l]) = setsum f (set [k..l])"
haftmann@37605
  3032
  by (simp add: listsum_distinct_conv_setsum_set)
haftmann@37605
  3033
haftmann@37605
  3034
text {* General equivalence between @{const listsum} and @{const setsum} *}
haftmann@40007
  3035
lemma (in monoid_add) listsum_setsum_nth:
haftmann@37605
  3036
  "listsum xs = (\<Sum> i = 0 ..< length xs. xs ! i)"
haftmann@37605
  3037
  using interv_listsum_conv_setsum_set_nat [of "op ! xs" 0 "length xs"] by (simp add: map_nth)
haftmann@37605
  3038
haftmann@37605
  3039
haftmann@34965
  3040
subsubsection {* @{const insert} *}
haftmann@34965
  3041
haftmann@34965
  3042
lemma in_set_insert [simp]:
haftmann@34965
  3043
  "x \<in> set xs \<Longrightarrow> List.insert x xs = xs"
haftmann@34965
  3044
  by (simp add: List.insert_def)
haftmann@34965
  3045
haftmann@34965
  3046
lemma not_in_set_insert [simp]:
haftmann@34965
  3047
  "x \<notin> set xs \<Longrightarrow> List.insert x xs = x # xs"
haftmann@34965
  3048
  by (simp add: List.insert_def)
haftmann@34965
  3049
haftmann@34965
  3050
lemma insert_Nil [simp]:
haftmann@34965
  3051
  "List.insert x [] = [x]"
haftmann@34965
  3052
  by simp
haftmann@34965
  3053
haftmann@35295
  3054
lemma set_insert [simp]:
haftmann@34965
  3055
  "set (List.insert x xs) = insert x (set xs)"
haftmann@34965
  3056
  by (auto simp add: List.insert_def)
haftmann@34965
  3057
haftmann@35295
  3058
lemma distinct_insert [simp]:
haftmann@35295
  3059
  "distinct xs \<Longrightarrow> distinct (List.insert x xs)"
haftmann@35295
  3060
  by (simp add: List.insert_def)
haftmann@35295
  3061
haftmann@36275
  3062
lemma insert_remdups:
haftmann@36275
  3063
  "List.insert x (remdups xs) = remdups (List.insert x xs)"
haftmann@36275
  3064
  by (simp add: List.insert_def)
haftmann@36275
  3065
haftmann@34965
  3066
nipkow@15392
  3067
subsubsection {* @{text remove1} *}
nipkow@15110
  3068
nipkow@18049
  3069
lemma remove1_append:
nipkow@18049
  3070
  "remove1 x (xs @ ys) =
nipkow@18049
  3071
  (if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)"
nipkow@18049
  3072
by (induct xs) auto
nipkow@18049
  3073
nipkow@36895
  3074
lemma remove1_commute: "remove1 x (remove1 y zs) = remove1 y (remove1 x zs)"
nipkow@36895
  3075
by (induct zs) auto
nipkow@36895
  3076
nipkow@23479
  3077
lemma in_set_remove1[simp]:
nipkow@23479
  3078
  "a \<noteq> b \<Longrightarrow> a : set(remove1 b xs) = (a : set xs)"
nipkow@23479
  3079
apply (induct xs)
nipkow@23479
  3080
apply auto
nipkow@23479
  3081
done
nipkow@23479
  3082
nipkow@15110
  3083
lemma set_remove1_subset: "set(remove1 x xs) <= set xs"
nipkow@15110
  3084
apply(induct xs)
nipkow@15110
  3085
 apply simp
nipkow@15110
  3086
apply simp
nipkow@15110
  3087
apply blast
nipkow@15110
  3088
done
nipkow@15110
  3089
paulson@17724
  3090
lemma set_remove1_eq [simp]: "distinct xs ==> set(remove1 x xs) = set xs - {x}"
nipkow@15110
  3091
apply(induct xs)
nipkow@15110
  3092
 apply simp
nipkow@15110
  3093
apply simp
nipkow@15110
  3094
apply blast
nipkow@15110
  3095
done
nipkow@15110
  3096
nipkow@23479
  3097
lemma length_remove1:
huffman@30128
  3098
  "length(remove1 x xs) = (if x : set xs then length xs - 1 else length xs)"
nipkow@23479
  3099
apply (induct xs)
nipkow@23479
  3100
 apply (auto dest!:length_pos_if_in_set)
nipkow@23479
  3101
done
nipkow@23479
  3102
nipkow@18049
  3103
lemma remove1_filter_not[simp]:
nipkow@18049
  3104
  "\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs"
nipkow@18049
  3105
by(induct xs) auto
nipkow@18049
  3106
hoelzl@39307
  3107
lemma filter_remove1:
hoelzl@39307
  3108
  "filter Q (remove1 x xs) = remove1 x (filter Q xs)"
hoelzl@39307
  3109
by (induct xs) auto
hoelzl@39307
  3110
nipkow@15110
  3111
lemma notin_set_remove1[simp]: "x ~: set xs ==> x ~: set(remove1 y xs)"
nipkow@15110
  3112
apply(insert set_remove1_subset)
nipkow@15110
  3113
apply fast
nipkow@15110
  3114
done
nipkow@15110
  3115
nipkow@15110
  3116
lemma distinct_remove1[simp]: "distinct xs ==> distinct(remove1 x xs)"
nipkow@15110
  3117
by (induct xs) simp_all
nipkow@15110
  3118
haftmann@36275
  3119
lemma remove1_remdups:
haftmann@36275
  3120
  "distinct xs \<Longrightarrow> remove1 x (remdups xs) = remdups (remove1 x xs)"
haftmann@36275
  3121
  by (induct xs) simp_all
haftmann@36275
  3122
haftmann@37091
  3123
lemma remove1_idem:
haftmann@37091
  3124
  assumes "x \<notin> set xs"
haftmann@37091
  3125
  shows "remove1 x xs = xs"
haftmann@37091
  3126
  using assms by (induct xs) simp_all
haftmann@37091
  3127
wenzelm@13114
  3128
nipkow@27693
  3129
subsubsection {* @{text removeAll} *}
nipkow@27693
  3130
haftmann@34965
  3131
lemma removeAll_filter_not_eq:
haftmann@34965
  3132
  "removeAll x = filter (\<lambda>y. x \<noteq> y)"
haftmann@34965
  3133
proof
haftmann@34965
  3134
  fix xs
haftmann@34965
  3135
  show "removeAll x xs = filter (\<lambda>y. x \<noteq> y) xs"
haftmann@34965
  3136
    by (induct xs) auto
haftmann@34965
  3137
qed
haftmann@34965
  3138
nipkow@27693
  3139
lemma removeAll_append[simp]:
nipkow@27693
  3140
  "removeAll x (xs @ ys) = removeAll x xs @ removeAll x ys"
nipkow@27693
  3141
by (induct xs) auto
nipkow@27693
  3142
nipkow@27693
  3143
lemma set_removeAll[simp]: "set(removeAll x xs) = set xs - {x}"
nipkow@27693
  3144
by (induct xs) auto
nipkow@27693
  3145
nipkow@27693
  3146
lemma removeAll_id[simp]: "x \<notin> set xs \<Longrightarrow> removeAll x xs = xs"
nipkow@27693
  3147
by (induct xs) auto
nipkow@27693
  3148
nipkow@27693
  3149
(* Needs count:: 'a \<Rightarrow> a' list \<Rightarrow> nat
nipkow@27693
  3150
lemma length_removeAll:
nipkow@27693
  3151
  "length(removeAll x xs) = length xs - count x xs"
nipkow@27693
  3152
*)
nipkow@27693
  3153
nipkow@27693
  3154
lemma removeAll_filter_not[simp]:
nipkow@27693
  3155
  "\<not> P x \<Longrightarrow> removeAll x (filter P xs) = filter P xs"
nipkow@27693
  3156
by(induct xs) auto
nipkow@27693
  3157
haftmann@34965
  3158
lemma distinct_removeAll:
haftmann@34965
  3159
  "distinct xs \<Longrightarrow> distinct (removeAll x xs)"
haftmann@34965
  3160
  by (simp add: removeAll_filter_not_eq)
nipkow@27693
  3161
nipkow@27693
  3162
lemma distinct_remove1_removeAll:
nipkow@27693
  3163
  "distinct xs ==> remove1 x xs = removeAll x xs"
nipkow@27693
  3164
by (induct xs) simp_all
nipkow@27693
  3165
nipkow@27693
  3166
lemma map_removeAll_inj_on: "inj_on f (insert x (set xs)) \<Longrightarrow>
nipkow@27693
  3167
  map f (removeAll x xs) = removeAll (f x) (map f xs)"
nipkow@27693
  3168
by (induct xs) (simp_all add:inj_on_def)
nipkow@27693
  3169
nipkow@27693
  3170
lemma map_removeAll_inj: "inj f \<Longrightarrow>
nipkow@27693
  3171
  map f (removeAll x xs) = removeAll (f x) (map f xs)"
nipkow@27693
  3172
by(metis map_removeAll_inj_on subset_inj_on subset_UNIV)
nipkow@27693
  3173
nipkow@27693
  3174
nipkow@15392
  3175
subsubsection {* @{text replicate} *}
wenzelm@13114
  3176
wenzelm@13142
  3177
lemma length_replicate [simp]: "length (replicate n x) = n"
nipkow@13145
  3178
by (induct n) auto
wenzelm@13142
  3179
hoelzl@36610
  3180
lemma Ex_list_of_length: "\<exists>xs. length xs = n"
hoelzl@36610
  3181
by (rule exI[of _ "replicate n undefined"]) simp
hoelzl@36610
  3182
wenzelm@13142
  3183
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
nipkow@13145
  3184
by (induct n) auto
wenzelm@13114
  3185
hoelzl@31350
  3186
lemma map_replicate_const:
hoelzl@31350
  3187
  "map (\<lambda> x. k) lst = replicate (length lst) k"
hoelzl@31350
  3188
  by (induct lst) auto
hoelzl@31350
  3189
wenzelm@13114
  3190
lemma replicate_app_Cons_same:
nipkow@13145
  3191
"(replicate n x) @ (x # xs) = x # replicate n x @ xs"
nipkow@13145
  3192
by (induct n) auto
wenzelm@13114
  3193
wenzelm@13142
  3194
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
paulson@14208
  3195
apply (induct n, simp)
nipkow@13145
  3196
apply (simp add: replicate_app_Cons_same)
nipkow@13145
  3197
done
wenzelm@13114
  3198
wenzelm@13142
  3199
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
nipkow@13145
  3200
by (induct n) auto
wenzelm@13114
  3201
nipkow@16397
  3202
text{* Courtesy of Matthias Daum: *}
nipkow@16397
  3203
lemma append_replicate_commute:
nipkow@16397
  3204
  "replicate n x @ replicate k x = replicate k x @ replicate n x"
nipkow@16397
  3205
apply (simp add: replicate_add [THEN sym])
nipkow@16397
  3206
apply (simp add: add_commute)
nipkow@16397
  3207
done
nipkow@16397
  3208
nipkow@31080
  3209
text{* Courtesy of Andreas Lochbihler: *}
nipkow@31080
  3210
lemma filter_replicate:
nipkow@31080
  3211
  "filter P (replicate n x) = (if P x then replicate n x else [])"
nipkow@31080
  3212
by(induct n) auto
nipkow@31080
  3213
wenzelm@13142
  3214
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"
nipkow@13145
  3215
by (induct n) auto
wenzelm@13114
  3216
wenzelm@13142
  3217
lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"
nipkow@13145
  3218
by (induct n) auto
wenzelm@13114
  3219
wenzelm@13142
  3220
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"
nipkow@13145
  3221
by (atomize (full), induct n) auto
wenzelm@13114
  3222
nipkow@24526
  3223
lemma nth_replicate[simp]: "i < n ==> (replicate n x)!i = x"
nipkow@24526
  3224
apply (induct n arbitrary: i, simp)
nipkow@13145
  3225
apply (simp add: nth_Cons split: nat.split)
nipkow@13145
  3226
done
wenzelm@13114
  3227
nipkow@16397
  3228
text{* Courtesy of Matthias Daum (2 lemmas): *}
nipkow@16397
  3229
lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x"
nipkow@16397
  3230
apply (case_tac "k \<le> i")
nipkow@16397
  3231
 apply  (simp add: min_def)
nipkow@16397
  3232
apply (drule not_leE)
nipkow@16397
  3233
apply (simp add: min_def)
nipkow@16397
  3234
apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x")
nipkow@16397
  3235
 apply  simp
nipkow@16397
  3236
apply (simp add: replicate_add [symmetric])
nipkow@16397
  3237
done
nipkow@16397
  3238
nipkow@24526
  3239
lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x"
nipkow@24526
  3240
apply (induct k arbitrary: i)
nipkow@16397
  3241
 apply simp
nipkow@16397
  3242
apply clarsimp
nipkow@16397
  3243
apply (case_tac i)
nipkow@16397
  3244
 apply simp
nipkow@16397
  3245
apply clarsimp
nipkow@16397
  3246
done
nipkow@16397
  3247
nipkow@16397
  3248
wenzelm@13142
  3249
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
nipkow@13145
  3250
by (induct n) auto
wenzelm@13114
  3251
wenzelm@13142
  3252
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
nipkow@13145
  3253
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
wenzelm@13114
  3254
wenzelm@13142
  3255
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
nipkow@13145
  3256
by auto
wenzelm@13114
  3257
nipkow@37431
  3258
lemma in_set_replicate[simp]: "(x : set (replicate n y)) = (x = y & n \<noteq> 0)"
nipkow@37431
  3259
by (simp add: set_replicate_conv_if)
nipkow@37431
  3260
nipkow@37429
  3261
lemma Ball_set_replicate[simp]:
nipkow@37429
  3262
  "(ALL x : set(replicate n a). P x) = (P a | n=0)"
nipkow@37429
  3263
by(simp add: set_replicate_conv_if)
nipkow@37429
  3264
nipkow@37429
  3265
lemma Bex_set_replicate[simp]:
nipkow@37429
  3266
  "(EX x : set(replicate n a). P x) = (P a & n\<noteq>0)"
nipkow@37429
  3267
by(simp add: set_replicate_conv_if)
wenzelm@13114
  3268
haftmann@24796
  3269
lemma replicate_append_same:
haftmann@24796
  3270
  "replicate i x @ [x] = x # replicate i x"
haftmann@24796
  3271
  by (induct i) simp_all
haftmann@24796
  3272
haftmann@24796
  3273
lemma map_replicate_trivial:
haftmann@24796
  3274
  "map (\<lambda>i. x) [0..<i] = replicate i x"
haftmann@24796
  3275
  by (induct i) (simp_all add: replicate_append_same)
haftmann@24796
  3276
hoelzl@31350
  3277
lemma concat_replicate_trivial[simp]:
hoelzl@31350
  3278
  "concat (replicate i []) = []"
hoelzl@31350
  3279
  by (induct i) (auto simp add: map_replicate_const)
wenzelm@13114
  3280
nipkow@28642
  3281
lemma replicate_empty[simp]: "(replicate n x = []) \<longleftrightarrow> n=0"
nipkow@28642
  3282
by (induct n) auto
nipkow@28642
  3283
nipkow@28642
  3284
lemma empty_replicate[simp]: "([] = replicate n x) \<longleftrightarrow> n=0"
nipkow@28642
  3285
by (induct n) auto
nipkow@28642
  3286
nipkow@28642
  3287
lemma replicate_eq_replicate[simp]:
nipkow@28642
  3288
  "(replicate m x = replicate n y) \<longleftrightarrow> (m=n & (m\<noteq>0 \<longrightarrow> x=y))"
nipkow@28642
  3289
apply(induct m arbitrary: n)
nipkow@28642
  3290
 apply simp
nipkow@28642
  3291
apply(induct_tac n)
nipkow@28642
  3292
apply auto
nipkow@28642
  3293
done
nipkow@28642
  3294
haftmann@39756
  3295
lemma replicate_length_filter:
haftmann@39756
  3296
  "replicate (length (filter (\<lambda>y. x = y) xs)) x = filter (\<lambda>y. x = y) xs"
haftmann@39756
  3297
  by (induct xs) auto
haftmann@39756
  3298
nipkow@28642
  3299
nipkow@15392
  3300
subsubsection{*@{text rotate1} and @{text rotate}*}
nipkow@15302
  3301
nipkow@15302
  3302
lemma rotate_simps[simp]: "rotate1 [] = [] \<and> rotate1 (x#xs) = xs @ [x]"
nipkow@15302
  3303
by(simp add:rotate1_def)
nipkow@15302
  3304
nipkow@15302
  3305
lemma rotate0[simp]: "rotate 0 = id"
nipkow@15302
  3306
by(simp add:rotate_def)
nipkow@15302
  3307
nipkow@15302
  3308
lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)"
nipkow@15302
  3309
by(simp add:rotate_def)
nipkow@15302
  3310
nipkow@15302
  3311
lemma rotate_add:
nipkow@15302
  3312
  "rotate (m+n) = rotate m o rotate n"
nipkow@15302
  3313
by(simp add:rotate_def funpow_add)
nipkow@15302
  3314
nipkow@15302
  3315
lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs"
nipkow@15302
  3316
by(simp add:rotate_add)
nipkow@15302
  3317
nipkow@18049
  3318
lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)"
nipkow@18049
  3319
by(simp add:rotate_def funpow_swap1)
nipkow@18049
  3320
nipkow@15302
  3321
lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs"
nipkow@15302
  3322
by(cases xs) simp_all
nipkow@15302
  3323
nipkow@15302
  3324
lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs"
nipkow@15302
  3325
apply(induct n)
nipkow@15302
  3326
 apply simp
nipkow@15302
  3327
apply (simp add:rotate_def)
nipkow@15302
  3328
done
nipkow@15302
  3329
nipkow@15302
  3330
lemma rotate1_hd_tl: "xs \<noteq> [] \<Longrightarrow> rotate1 xs = tl xs @ [hd xs]"
nipkow@15302
  3331
by(simp add:rotate1_def split:list.split)
nipkow@15302
  3332
nipkow@15302
  3333
lemma rotate_drop_take:
nipkow@15302
  3334
  "rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs"
nipkow@15302
  3335
apply(induct n)
nipkow@15302
  3336
 apply simp
nipkow@15302
  3337
apply(simp add:rotate_def)
nipkow@15302
  3338
apply(cases "xs = []")
nipkow@15302
  3339
 apply (simp)
nipkow@15302
  3340
apply(case_tac "n mod length xs = 0")
nipkow@15302
  3341
 apply(simp add:mod_Suc)
nipkow@15302
  3342
 apply(simp add: rotate1_hd_tl drop_Suc take_Suc)
nipkow@15302
  3343
apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric]
nipkow@15302
  3344
                take_hd_drop linorder_not_le)
nipkow@15302
  3345
done
nipkow@15302
  3346
nipkow@15302
  3347
lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs"
nipkow@15302
  3348
by(simp add:rotate_drop_take)
nipkow@15302
  3349
nipkow@15302
  3350
lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs"
nipkow@15302
  3351
by(simp add:rotate_drop_take)
nipkow@15302
  3352
nipkow@15302
  3353
lemma length_rotate1[simp]: "length(rotate1 xs) = length xs"
nipkow@15302
  3354
by(simp add:rotate1_def split:list.split)
nipkow@15302
  3355
nipkow@24526
  3356
lemma length_rotate[simp]: "length(rotate n xs) = length xs"
nipkow@24526
  3357
by (induct n arbitrary: xs) (simp_all add:rotate_def)
nipkow@15302
  3358
nipkow@15302
  3359
lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs"
nipkow@15302
  3360
by(simp add:rotate1_def split:list.split) blast
nipkow@15302
  3361
nipkow@15302
  3362
lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs"
nipkow@15302
  3363
by (induct n) (simp_all add:rotate_def)
nipkow@15302
  3364
nipkow@15302
  3365
lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)"
nipkow@15302
  3366
by(simp add:rotate_drop_take take_map drop_map)
nipkow@15302
  3367
nipkow@15302
  3368
lemma set_rotate1[simp]: "set(rotate1 xs) = set xs"
nipkow@15302
  3369
by(simp add:rotate1_def split:list.split)
nipkow@15302
  3370
nipkow@15302
  3371
lemma set_rotate[simp]: "set(rotate n xs) = set xs"
nipkow@15302
  3372
by (induct n) (simp_all add:rotate_def)
nipkow@15302
  3373
nipkow@15302
  3374
lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])"
nipkow@15302
  3375
by(simp add:rotate1_def split:list.split)
nipkow@15302
  3376
nipkow@15302
  3377
lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])"
nipkow@15302
  3378
by (induct n) (simp_all add:rotate_def)
nipkow@15302
  3379
nipkow@15439
  3380
lemma rotate_rev:
nipkow@15439
  3381
  "rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)"
nipkow@15439
  3382
apply(simp add:rotate_drop_take rev_drop rev_take)
nipkow@15439
  3383
apply(cases "length xs = 0")
nipkow@15439
  3384
 apply simp
nipkow@15439
  3385
apply(cases "n mod length xs = 0")
nipkow@15439
  3386
 apply simp
nipkow@15439
  3387
apply(simp add:rotate_drop_take rev_drop rev_take)
nipkow@15439
  3388
done
nipkow@15439
  3389
nipkow@18423
  3390
lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)"
nipkow@18423
  3391
apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth)
nipkow@18423
  3392
apply(subgoal_tac "length xs \<noteq> 0")
nipkow@18423
  3393
 prefer 2 apply simp
nipkow@18423
  3394
using mod_less_divisor[of "length xs" n] by arith
nipkow@18423
  3395
nipkow@15302
  3396
nipkow@15392
  3397
subsubsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
nipkow@15302
  3398
nipkow@15302
  3399
lemma sublist_empty [simp]: "sublist xs {} = []"
nipkow@15302
  3400
by (auto simp add: sublist_def)
nipkow@15302
  3401
nipkow@15302
  3402
lemma sublist_nil [simp]: "sublist [] A = []"
nipkow@15302
  3403
by (auto simp add: sublist_def)
nipkow@15302
  3404
nipkow@15302
  3405
lemma length_sublist:
nipkow@15302
  3406
  "length(sublist xs I) = card{i. i < length xs \<and> i : I}"
nipkow@15302
  3407
by(simp add: sublist_def length_filter_conv_card cong:conj_cong)
nipkow@15302
  3408
nipkow@15302
  3409
lemma sublist_shift_lemma_Suc:
nipkow@24526
  3410
  "map fst (filter (%p. P(Suc(snd p))) (zip xs is)) =
nipkow@24526
  3411
   map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))"
nipkow@24526
  3412
apply(induct xs arbitrary: "is")
nipkow@15302
  3413
 apply simp
nipkow@15302
  3414
apply (case_tac "is")
nipkow@15302
  3415
 apply simp
nipkow@15302
  3416
apply simp
nipkow@15302
  3417
done
nipkow@15302
  3418
nipkow@15302
  3419
lemma sublist_shift_lemma:
nipkow@23279
  3420
     "map fst [p<-zip xs [i..<i + length xs] . snd p : A] =
nipkow@23279
  3421
      map fst [p<-zip xs [0..<length xs] . snd p + i : A]"
nipkow@15302
  3422
by (induct xs rule: rev_induct) (simp_all add: add_commute)
nipkow@15302
  3423
nipkow@15302
  3424
lemma sublist_append:
nipkow@15302
  3425
     "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
nipkow@15302
  3426
apply (unfold sublist_def)
nipkow@15302
  3427
apply (induct l' rule: rev_induct, simp)
nipkow@15302
  3428
apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
nipkow@15302
  3429
apply (simp add: add_commute)
nipkow@15302
  3430
done
nipkow@15302
  3431
nipkow@15302
  3432
lemma sublist_Cons:
nipkow@15302
  3433
"sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
nipkow@15302
  3434
apply (induct l rule: rev_induct)
nipkow@15302
  3435
 apply (simp add: sublist_def)
nipkow@15302
  3436
apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)
nipkow@15302
  3437
done
nipkow@15302
  3438
nipkow@24526
  3439
lemma set_sublist: "set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}"
nipkow@24526
  3440
apply(induct xs arbitrary: I)
nipkow@25162
  3441
apply(auto simp: sublist_Cons nth_Cons split:nat.split dest!: gr0_implies_Suc)
nipkow@15302
  3442
done
nipkow@15302
  3443
nipkow@15302
  3444
lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs"
nipkow@15302
  3445
by(auto simp add:set_sublist)
nipkow@15302
  3446
nipkow@15302
  3447
lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)"
nipkow@15302
  3448
by(auto simp add:set_sublist)
nipkow@15302
  3449
nipkow@15302
  3450
lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs"
nipkow@15302
  3451
by(auto simp add:set_sublist)
nipkow@15302
  3452
nipkow@15302
  3453
lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"
nipkow@15302
  3454
by (simp add: sublist_Cons)
nipkow@15302
  3455
nipkow@15302
  3456
nipkow@24526
  3457
lemma distinct_sublistI[simp]: "distinct xs \<Longrightarrow> distinct(sublist xs I)"
nipkow@24526
  3458
apply(induct xs arbitrary: I)
nipkow@15302
  3459
 apply simp
nipkow@15302
  3460
apply(auto simp add:sublist_Cons)
nipkow@15302
  3461
done
nipkow@15302
  3462
nipkow@15302
  3463
nipkow@15302
  3464
lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l"
nipkow@15302
  3465
apply (induct l rule: rev_induct, simp)
nipkow@15302
  3466
apply (simp split: nat_diff_split add: sublist_append)
nipkow@15302
  3467
done
nipkow@15302
  3468
nipkow@24526
  3469
lemma filter_in_sublist:
nipkow@24526
  3470
 "distinct xs \<Longrightarrow> filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s"
nipkow@24526
  3471
proof (induct xs arbitrary: s)
nipkow@17501
  3472
  case Nil thus ?case by simp
nipkow@17501
  3473
next
nipkow@17501
  3474
  case (Cons a xs)
nipkow@17501
  3475
  moreover hence "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto
nipkow@17501
  3476
  ultimately show ?case by(simp add: sublist_Cons cong:filter_cong)
nipkow@17501
  3477
qed
nipkow@17501
  3478
nipkow@15302
  3479
nipkow@19390
  3480
subsubsection {* @{const splice} *}
nipkow@19390
  3481
nipkow@40841
  3482
lemma splice_Nil2 [simp, code]: "splice xs [] = xs"
nipkow@19390
  3483
by (cases xs) simp_all
nipkow@19390
  3484
nipkow@40841
  3485
declare splice.simps(1,3)[code]
nipkow@40841
  3486
declare splice.simps(2)[simp del]
nipkow@19390
  3487
nipkow@24526
  3488
lemma length_splice[simp]: "length(splice xs ys) = length xs + length ys"
nipkow@40841
  3489
by (induct xs ys rule: splice.induct) auto
nipkow@22793
  3490
wenzelm@35118
  3491
wenzelm@35118
  3492
subsubsection {* Transpose *}
hoelzl@34920
  3493
hoelzl@34920
  3494
function transpose where
hoelzl@34920
  3495
"transpose []             = []" |
hoelzl@34920
  3496
"transpose ([]     # xss) = transpose xss" |
hoelzl@34920
  3497
"transpose ((x#xs) # xss) =
hoelzl@34920
  3498
  (x # [h. (h#t) \<leftarrow> xss]) # transpose (xs # [t. (h#t) \<leftarrow> xss])"
hoelzl@34920
  3499
by pat_completeness auto
hoelzl@34920
  3500
hoelzl@34920
  3501
lemma transpose_aux_filter_head:
hoelzl@34920
  3502
  "concat (map (list_case [] (\<lambda>h t. [h])) xss) =
hoelzl@34920
  3503
  map (\<lambda>xs. hd xs) [ys\<leftarrow>xss . ys \<noteq> []]"
hoelzl@34920
  3504
  by (induct xss) (auto split: list.split)
hoelzl@34920
  3505
hoelzl@34920
  3506
lemma transpose_aux_filter_tail:
hoelzl@34920
  3507
  "concat (map (list_case [] (\<lambda>h t. [t])) xss) =
hoelzl@34920
  3508
  map (\<lambda>xs. tl xs) [ys\<leftarrow>xss . ys \<noteq> []]"
hoelzl@34920
  3509
  by (induct xss) (auto split: list.split)
hoelzl@34920
  3510
hoelzl@34920
  3511
lemma transpose_aux_max:
hoelzl@34920
  3512
  "max (Suc (length xs)) (foldr (\<lambda>xs. max (length xs)) xss 0) =
hoelzl@34920
  3513
  Suc (max (length xs) (foldr (\<lambda>x. max (length x - Suc 0)) [ys\<leftarrow>xss . ys\<noteq>[]] 0))"
hoelzl@34920
  3514
  (is "max _ ?foldB = Suc (max _ ?foldA)")
hoelzl@34920
  3515
proof (cases "[ys\<leftarrow>xss . ys\<noteq>[]] = []")
hoelzl@34920
  3516
  case True
hoelzl@34920
  3517
  hence "foldr (\<lambda>xs. max (length xs)) xss 0 = 0"
hoelzl@34920
  3518
  proof (induct xss)
hoelzl@34920
  3519
    case (Cons x xs)
hoelzl@34920
  3520
    moreover hence "x = []" by (cases x) auto
hoelzl@34920
  3521
    ultimately show ?case by auto
hoelzl@34920
  3522
  qed simp
hoelzl@34920
  3523
  thus ?thesis using True by simp
hoelzl@34920
  3524
next
hoelzl@34920
  3525
  case False
hoelzl@34920
  3526
hoelzl@34920
  3527
  have foldA: "?foldA = foldr (\<lambda>x. max (length x)) [ys\<leftarrow>xss . ys \<noteq> []] 0 - 1"
hoelzl@34920
  3528
    by (induct xss) auto
hoelzl@34920
  3529
  have foldB: "?foldB = foldr (\<lambda>x. max (length x)) [ys\<leftarrow>xss . ys \<noteq> []] 0"
hoelzl@34920
  3530
    by (induct xss) auto
hoelzl@34920
  3531
hoelzl@34920
  3532
  have "0 < ?foldB"
hoelzl@34920
  3533
  proof -
hoelzl@34920
  3534
    from False
hoelzl@34920
  3535
    obtain z zs where zs: "[ys\<leftarrow>xss . ys \<noteq> []] = z#zs" by (auto simp: neq_Nil_conv)
hoelzl@34920
  3536
    hence "z \<in> set ([ys\<leftarrow>xss . ys \<noteq> []])" by auto
hoelzl@34920
  3537
    hence "z \<noteq> []" by auto
hoelzl@34920
  3538
    thus ?thesis
hoelzl@34920
  3539
      unfolding foldB zs
hoelzl@34920
  3540
      by (auto simp: max_def intro: less_le_trans)
hoelzl@34920
  3541
  qed
hoelzl@34920
  3542
  thus ?thesis
hoelzl@34920
  3543
    unfolding foldA foldB max_Suc_Suc[symmetric]
hoelzl@34920
  3544
    by simp
hoelzl@34920
  3545
qed
hoelzl@34920
  3546
hoelzl@34920
  3547
termination transpose
hoelzl@34920
  3548
  by (relation "measure (\<lambda>xs. foldr (\<lambda>xs. max (length xs)) xs 0 + length xs)")
hoelzl@34920
  3549
     (auto simp: transpose_aux_filter_tail foldr_map comp_def transpose_aux_max less_Suc_eq_le)
hoelzl@34920
  3550
hoelzl@34920
  3551
lemma transpose_empty: "(transpose xs = []) \<longleftrightarrow> (\<forall>x \<in> set xs. x = [])"
hoelzl@34920
  3552
  by (induct rule: transpose.induct) simp_all
hoelzl@34920
  3553
hoelzl@34920
  3554
lemma length_transpose:
hoelzl@34920
  3555
  fixes xs :: "'a list list"
hoelzl@34920
  3556
  shows "length (transpose xs) = foldr (\<lambda>xs. max (length xs)) xs 0"
hoelzl@34920
  3557
  by (induct rule: transpose.induct)
hoelzl@34920
  3558
    (auto simp: transpose_aux_filter_tail foldr_map comp_def transpose_aux_max
hoelzl@34920
  3559
                max_Suc_Suc[symmetric] simp del: max_Suc_Suc)
hoelzl@34920
  3560
hoelzl@34920
  3561
lemma nth_transpose:
hoelzl@34920
  3562
  fixes xs :: "'a list list"
hoelzl@34920
  3563
  assumes "i < length (transpose xs)"
hoelzl@34920
  3564
  shows "transpose xs ! i = map (\<lambda>xs. xs ! i) [ys \<leftarrow> xs. i < length ys]"
hoelzl@34920
  3565
using assms proof (induct arbitrary: i rule: transpose.induct)
hoelzl@34920
  3566
  case (3 x xs xss)
hoelzl@34920
  3567
  def XS == "(x # xs) # xss"
hoelzl@34920
  3568
  hence [simp]: "XS \<noteq> []" by auto
hoelzl@34920
  3569
  thus ?case
hoelzl@34920
  3570
  proof (cases i)
hoelzl@34920
  3571
    case 0
hoelzl@34920
  3572
    thus ?thesis by (simp add: transpose_aux_filter_head hd_conv_nth)
hoelzl@34920
  3573
  next
hoelzl@34920
  3574
    case (Suc j)
hoelzl@34920
  3575
    have *: "\<And>xss. xs # map tl xss = map tl ((x#xs)#xss)" by simp
hoelzl@34920
  3576
    have **: "\<And>xss. (x#xs) # filter (\<lambda>ys. ys \<noteq> []) xss = filter (\<lambda>ys. ys \<noteq> []) ((x#xs)#xss)" by simp
hoelzl@34920
  3577
    { fix x have "Suc j < length x \<longleftrightarrow> x \<noteq> [] \<and> j < length x - Suc 0"
hoelzl@34920
  3578
      by (cases x) simp_all
hoelzl@34920
  3579
    } note *** = this
hoelzl@34920
  3580
hoelzl@34920
  3581
    have j_less: "j < length (transpose (xs # concat (map (list_case [] (\<lambda>h t. [t])) xss)))"
hoelzl@34920
  3582
      using "3.prems" by (simp add: transpose_aux_filter_tail length_transpose Suc)
hoelzl@34920
  3583
hoelzl@34920
  3584
    show ?thesis
hoelzl@34920
  3585
      unfolding transpose.simps `i = Suc j` nth_Cons_Suc "3.hyps"[OF j_less]
hoelzl@34920
  3586
      apply (auto simp: transpose_aux_filter_tail filter_map comp_def length_transpose * ** *** XS_def[symmetric])
hoelzl@34920
  3587
      apply (rule_tac y=x in list.exhaust)
hoelzl@34920
  3588
      by auto
hoelzl@34920
  3589
  qed
hoelzl@34920
  3590
qed simp_all
hoelzl@34920
  3591
hoelzl@34920
  3592
lemma transpose_map_map:
hoelzl@34920
  3593
  "transpose (map (map f) xs) = map (map f) (transpose xs)"
hoelzl@34920
  3594
proof (rule nth_equalityI, safe)
hoelzl@34920
  3595
  have [simp]: "length (transpose (map (map f) xs)) = length (transpose xs)"
hoelzl@34920
  3596
    by (simp add: length_transpose foldr_map comp_def)
hoelzl@34920
  3597
  show "length (transpose (map (map f) xs)) = length (map (map f) (transpose xs))" by simp
hoelzl@34920
  3598
hoelzl@34920
  3599
  fix i assume "i < length (transpose (map (map f) xs))"
hoelzl@34920
  3600
  thus "transpose (map (map f) xs) ! i = map (map f) (transpose xs) ! i"
hoelzl@34920
  3601
    by (simp add: nth_transpose filter_map comp_def)
hoelzl@34920
  3602
qed
nipkow@24616
  3603
wenzelm@35118
  3604
nipkow@31557
  3605
subsubsection {* (In)finiteness *}
nipkow@28642
  3606
nipkow@28642
  3607
lemma finite_maxlen:
nipkow@28642
  3608
  "finite (M::'a list set) ==> EX n. ALL s:M. size s < n"
nipkow@28642
  3609
proof (induct rule: finite.induct)
nipkow@28642
  3610
  case emptyI show ?case by simp
nipkow@28642
  3611
next
nipkow@28642
  3612
  case (insertI M xs)
nipkow@28642
  3613
  then obtain n where "\<forall>s\<in>M. length s < n" by blast
nipkow@28642
  3614
  hence "ALL s:insert xs M. size s < max n (size xs) + 1" by auto
nipkow@28642
  3615
  thus ?case ..
nipkow@28642
  3616
qed
nipkow@28642
  3617
nipkow@31557
  3618
lemma finite_lists_length_eq:
nipkow@31557
  3619
assumes "finite A"
nipkow@31557
  3620
shows "finite {xs. set xs \<subseteq> A \<and> length xs = n}" (is "finite (?S n)")
nipkow@31557
  3621
proof(induct n)
nipkow@31557
  3622
  case 0 show ?case by simp
nipkow@31557
  3623
next
nipkow@31557
  3624
  case (Suc n)
nipkow@31557
  3625
  have "?S (Suc n) = (\<Union>x\<in>A. (\<lambda>xs. x#xs) ` ?S n)"
nipkow@31557
  3626
    by (auto simp:length_Suc_conv)
nipkow@31557
  3627
  then show ?case using `finite A`
nipkow@41030
  3628
    by (auto intro: Suc) (* FIXME metis? *)
nipkow@31557
  3629
qed
nipkow@31557
  3630
nipkow@31557
  3631
lemma finite_lists_length_le:
nipkow@31557
  3632
  assumes "finite A" shows "finite {xs. set xs \<subseteq> A \<and> length xs \<le> n}"
nipkow@31557
  3633
 (is "finite ?S")
nipkow@31557
  3634
proof-
nipkow@31557
  3635
  have "?S = (\<Union>n\<in>{0..n}. {xs. set xs \<subseteq> A \<and> length xs = n})" by auto
nipkow@31557
  3636
  thus ?thesis by (auto intro: finite_lists_length_eq[OF `finite A`])
nipkow@31557
  3637
qed
nipkow@31557
  3638
nipkow@28642
  3639
lemma infinite_UNIV_listI: "~ finite(UNIV::'a list set)"
nipkow@28642
  3640
apply(rule notI)
nipkow@28642
  3641
apply(drule finite_maxlen)
nipkow@28642
  3642
apply (metis UNIV_I length_replicate less_not_refl)
nipkow@28642
  3643
done
nipkow@28642
  3644
nipkow@28642
  3645
wenzelm@35118
  3646
subsection {* Sorting *}
nipkow@24616
  3647
nipkow@24617
  3648
text{* Currently it is not shown that @{const sort} returns a
nipkow@24617
  3649
permutation of its input because the nicest proof is via multisets,
nipkow@24617
  3650
which are not yet available. Alternatively one could define a function
nipkow@24617
  3651
that counts the number of occurrences of an element in a list and use
nipkow@24617
  3652
that instead of multisets to state the correctness property. *}
nipkow@24617
  3653
nipkow@24616
  3654
context linorder
nipkow@24616
  3655
begin
nipkow@24616
  3656
haftmann@40451
  3657
lemma length_insort [simp]:
haftmann@40451
  3658
  "length (insort_key f x xs) = Suc (length xs)"
haftmann@40451
  3659
  by (induct xs) simp_all
haftmann@40451
  3660
haftmann@40451
  3661
lemma insort_key_left_comm:
haftmann@40451
  3662
  assumes "f x \<noteq> f y"
haftmann@40451
  3663
  shows "insort_key f y (insort_key f x xs) = insort_key f x (insort_key f y xs)"
haftmann@40451
  3664
  by (induct xs) (auto simp add: assms dest: antisym)
hoelzl@33639
  3665
haftmann@35195
  3666
lemma insort_left_comm:
haftmann@35195
  3667
  "insort x (insort y xs) = insort y (insort x xs)"
haftmann@40451
  3668
  by (cases "x = y") (auto intro: insort_key_left_comm)
haftmann@35195
  3669
haftmann@35195
  3670
lemma fun_left_comm_insort:
haftmann@35195
  3671
  "fun_left_comm insort"
haftmann@35195
  3672
proof
haftmann@35195
  3673
qed (fact insort_left_comm)
haftmann@35195
  3674
haftmann@35195
  3675
lemma sort_key_simps [simp]:
haftmann@35195
  3676
  "sort_key f [] = []"
haftmann@35195
  3677
  "sort_key f (x#xs) = insort_key f x (sort_key f xs)"
haftmann@35195
  3678
  by (simp_all add: sort_key_def)
haftmann@35195
  3679
haftmann@35195
  3680
lemma sort_foldl_insort:
haftmann@35195
  3681
  "sort xs = foldl (\<lambda>ys x. insort x ys) [] xs"
haftmann@35195
  3682
  by (simp add: sort_key_def foldr_foldl foldl_rev insort_left_comm)
haftmann@35195
  3683
hoelzl@33639
  3684
lemma length_sort[simp]: "length (sort_key f xs) = length xs"
hoelzl@33639
  3685
by (induct xs, auto)
hoelzl@33639
  3686
haftmann@25062
  3687
lemma sorted_Cons: "sorted (x#xs) = (sorted xs & (ALL y:set xs. x <= y))"
nipkow@24616
  3688
apply(induct xs arbitrary: x) apply simp
nipkow@24616
  3689
by simp (blast intro: order_trans)
nipkow@24616
  3690
haftmann@40451
  3691
lemma sorted_tl:
haftmann@40451
  3692
  "sorted xs \<Longrightarrow> sorted (tl xs)"
haftmann@40451
  3693
  by (cases xs) (simp_all add: sorted_Cons)
haftmann@40451
  3694
nipkow@24616
  3695
lemma sorted_append:
haftmann@25062
  3696
  "sorted (xs@ys) = (sorted xs & sorted ys & (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<le>y))"
nipkow@24616
  3697
by (induct xs) (auto simp add:sorted_Cons)
nipkow@24616
  3698
nipkow@31201
  3699
lemma sorted_nth_mono:
hoelzl@33639
  3700
  "sorted xs \<Longrightarrow> i \<le> j \<Longrightarrow> j < length xs \<Longrightarrow> xs!i \<le> xs!j"
nipkow@31201
  3701
by (induct xs arbitrary: i j) (auto simp:nth_Cons' sorted_Cons)
nipkow@31201
  3702
hoelzl@33639
  3703
lemma sorted_rev_nth_mono:
hoelzl@33639
  3704
  "sorted (rev xs) \<Longrightarrow> i \<le> j \<Longrightarrow> j < length xs \<Longrightarrow> xs!j \<le> xs!i"
hoelzl@33639
  3705
using sorted_nth_mono[ of "rev xs" "length xs - j - 1" "length xs - i - 1"]
hoelzl@33639
  3706
      rev_nth[of "length xs - i - 1" "xs"] rev_nth[of "length xs - j - 1" "xs"]
hoelzl@33639
  3707
by auto
hoelzl@33639
  3708
hoelzl@33639
  3709
lemma sorted_nth_monoI:
hoelzl@33639
  3710
  "(\<And> i j. \<lbrakk> i \<le> j ; j < length xs \<rbrakk> \<Longrightarrow> xs ! i \<le> xs ! j) \<Longrightarrow> sorted xs"
hoelzl@33639
  3711
proof (induct xs)
hoelzl@33639
  3712
  case (Cons x xs)
hoelzl@33639
  3713
  have "sorted xs"
hoelzl@33639
  3714
  proof (rule Cons.hyps)
hoelzl@33639
  3715
    fix i j assume "i \<le> j" and "j < length xs"
hoelzl@33639
  3716
    with Cons.prems[of "Suc i" "Suc j"]
hoelzl@33639
  3717
    show "xs ! i \<le> xs ! j" by auto
hoelzl@33639
  3718
  qed
hoelzl@33639
  3719
  moreover
hoelzl@33639
  3720
  {
hoelzl@33639
  3721
    fix y assume "y \<in> set xs"
hoelzl@33639
  3722
    then obtain j where "j < length xs" and "xs ! j = y"
hoelzl@33639
  3723
      unfolding in_set_conv_nth by blast
hoelzl@33639
  3724
    with Cons.prems[of 0 "Suc j"]
hoelzl@33639
  3725
    have "x \<le> y"
hoelzl@33639
  3726
      by auto
hoelzl@33639
  3727
  }
hoelzl@33639
  3728
  ultimately
hoelzl@33639
  3729
  show ?case
hoelzl@33639
  3730
    unfolding sorted_Cons by auto
hoelzl@33639
  3731
qed simp
hoelzl@33639
  3732
hoelzl@33639
  3733
lemma sorted_equals_nth_mono:
hoelzl@33639
  3734
  "sorted xs = (\<forall>j < length xs. \<forall>i \<le> j. xs ! i \<le> xs ! j)"
hoelzl@33639
  3735
by (auto intro: sorted_nth_monoI sorted_nth_mono)
hoelzl@33639
  3736
hoelzl@33639
  3737
lemma set_insort: "set(insort_key f x xs) = insert x (set xs)"
nipkow@24616
  3738
by (induct xs) auto
nipkow@24616
  3739
hoelzl@33639
  3740
lemma set_sort[simp]: "set(sort_key f xs) = set xs"
nipkow@24616
  3741
by (induct xs) (simp_all add:set_insort)
nipkow@24616
  3742
hoelzl@33639
  3743
lemma distinct_insort: "distinct (insort_key f x xs) = (x \<notin> set xs \<and> distinct xs)"
nipkow@24616
  3744
by(induct xs)(auto simp:set_insort)
nipkow@24616
  3745
hoelzl@33639
  3746
lemma distinct_sort[simp]: "distinct (sort_key f xs) = distinct xs"
nipkow@24616
  3747
by(induct xs)(simp_all add:distinct_insort set_sort)
nipkow@24616
  3748
hoelzl@33639
  3749
lemma sorted_insort_key: "sorted (map f (insort_key f x xs)) = sorted (map f xs)"
haftmann@40451
  3750
  by (induct xs) (auto simp:sorted_Cons set_insort)
hoelzl@33639
  3751
nipkow@24616
  3752
lemma sorted_insort: "sorted (insort x xs) = sorted xs"
haftmann@40451
  3753
  using sorted_insort_key [where f="\<lambda>x. x"] by simp
haftmann@40451
  3754
haftmann@40451
  3755
theorem sorted_sort_key [simp]: "sorted (map f (sort_key f xs))"
haftmann@40451
  3756
  by (induct xs) (auto simp:sorted_insort_key)
haftmann@40451
  3757
haftmann@40451
  3758
theorem sorted_sort [simp]: "sorted (sort xs)"
haftmann@40451
  3759
  using sorted_sort_key [where f="\<lambda>x. x"] by simp
hoelzl@33639
  3760
haftmann@36846
  3761
lemma sorted_butlast:
haftmann@36846
  3762
  assumes "xs \<noteq> []" and "sorted xs"
haftmann@36846
  3763
  shows "sorted (butlast xs)"
haftmann@36846
  3764
proof -
haftmann@36846
  3765
  from `xs \<noteq> []` obtain ys y where "xs = ys @ [y]" by (cases xs rule: rev_cases) auto
haftmann@36846
  3766
  with `sorted xs` show ?thesis by (simp add: sorted_append)
haftmann@36846
  3767
qed
haftmann@36846
  3768
  
haftmann@36846
  3769
lemma insort_not_Nil [simp]:
haftmann@36846
  3770
  "insort_key f a xs \<noteq> []"
haftmann@36846
  3771
  by (induct xs) simp_all
haftmann@36846
  3772
hoelzl@33639
  3773
lemma insort_is_Cons: "\<forall>x\<in>set xs. f a \<le> f x \<Longrightarrow> insort_key f a xs = a # xs"
bulwahn@26143
  3774
by (cases xs) auto
bulwahn@26143
  3775
haftmann@39756
  3776
lemma sorted_map_remove1:
haftmann@39756
  3777
  "sorted (map f xs) \<Longrightarrow> sorted (map f (remove1 x xs))"
haftmann@39756
  3778
  by (induct xs) (auto simp add: sorted_Cons)
haftmann@39756
  3779
bulwahn@26143
  3780
lemma sorted_remove1: "sorted xs \<Longrightarrow> sorted (remove1 a xs)"
haftmann@39756
  3781
  using sorted_map_remove1 [of "\<lambda>x. x"] by simp
haftmann@39756
  3782
haftmann@39756
  3783
lemma insort_key_remove1:
haftmann@39756
  3784
  assumes "a \<in> set xs" and "sorted (map f xs)" and "hd (filter (\<lambda>x. f a = f x) xs) = a"
haftmann@39756
  3785
  shows "insort_key f a (remove1 a xs) = xs"
haftmann@39756
  3786
using assms proof (induct xs)
hoelzl@33639
  3787
  case (Cons x xs)
haftmann@39756
  3788
  then show ?case
hoelzl@33639
  3789
  proof (cases "x = a")
hoelzl@33639
  3790
    case False
haftmann@39756
  3791
    then have "f x \<noteq> f a" using Cons.prems by auto
haftmann@39756
  3792
    then have "f x < f a" using Cons.prems by (auto simp: sorted_Cons)
haftmann@39756
  3793
    with `f x \<noteq> f a` show ?thesis using Cons by (auto simp: sorted_Cons insort_is_Cons)
hoelzl@33639
  3794
  qed (auto simp: sorted_Cons insort_is_Cons)
hoelzl@33639
  3795
qed simp
bulwahn@26143
  3796
haftmann@39756
  3797
lemma insort_remove1:
haftmann@39756
  3798
  assumes "a \<in> set xs" and "sorted xs"
haftmann@39756
  3799
  shows "insort a (remove1 a xs) = xs"
haftmann@39756
  3800
proof (rule insort_key_remove1)
haftmann@39756
  3801
  from `a \<in> set xs` show "a \<in> set xs" .
haftmann@39756
  3802
  from `sorted xs` show "sorted (map (\<lambda>x. x) xs)" by simp
haftmann@39756
  3803
  from `a \<in> set xs` have "a \<in> set (filter (op = a) xs)" by auto
haftmann@39756
  3804
  then have "set (filter (op = a) xs) \<noteq> {}" by auto
haftmann@39756
  3805
  then have "filter (op = a) xs \<noteq> []" by (auto simp only: set_empty)
haftmann@39756
  3806
  then have "length (filter (op = a) xs) > 0" by simp
haftmann@39756
  3807
  then obtain n where n: "Suc n = length (filter (op = a) xs)"
haftmann@39756
  3808
    by (cases "length (filter (op = a) xs)") simp_all
haftmann@39756
  3809
  moreover have "replicate (Suc n) a = a # replicate n a"
haftmann@39756
  3810
    by simp
haftmann@39756
  3811
  ultimately show "hd (filter (op = a) xs) = a" by (simp add: replicate_length_filter)
haftmann@39756
  3812
qed
bulwahn@26143
  3813
bulwahn@26143
  3814
lemma sorted_remdups[simp]:
bulwahn@26143
  3815
  "sorted l \<Longrightarrow> sorted (remdups l)"
bulwahn@26143
  3816
by (induct l) (auto simp: sorted_Cons)
bulwahn@26143
  3817
nipkow@24645
  3818
lemma sorted_distinct_set_unique:
nipkow@24645
  3819
assumes "sorted xs" "distinct xs" "sorted ys" "distinct ys" "set xs = set ys"
nipkow@24645
  3820
shows "xs = ys"
nipkow@24645
  3821
proof -
haftmann@26734
  3822
  from assms have 1: "length xs = length ys" by (auto dest!: distinct_card)
nipkow@24645
  3823
  from assms show ?thesis
nipkow@24645
  3824
  proof(induct rule:list_induct2[OF 1])
nipkow@24645
  3825
    case 1 show ?case by simp
nipkow@24645
  3826
  next
nipkow@24645
  3827
    case 2 thus ?case by (simp add:sorted_Cons)
nipkow@24645
  3828
       (metis Diff_insert_absorb antisym insertE insert_iff)
nipkow@24645
  3829
  qed
nipkow@24645
  3830
qed
nipkow@24645
  3831
haftmann@35603
  3832
lemma map_sorted_distinct_set_unique:
haftmann@35603
  3833
  assumes "inj_on f (set xs \<union> set ys)"
haftmann@35603
  3834
  assumes "sorted (map f xs)" "distinct (map f xs)"
haftmann@35603
  3835
    "sorted (map f ys)" "distinct (map f ys)"
haftmann@35603
  3836
  assumes "set xs = set ys"
haftmann@35603
  3837
  shows "xs = ys"
haftmann@35603
  3838
proof -
haftmann@35603
  3839
  from assms have "map f xs = map f ys"
haftmann@35603
  3840
    by (simp add: sorted_distinct_set_unique)
haftmann@35603
  3841
  moreover with `inj_on f (set xs \<union> set ys)` show "xs = ys"
haftmann@35603
  3842
    by (blast intro: map_inj_on)
haftmann@35603
  3843
qed
haftmann@35603
  3844
nipkow@24645
  3845
lemma finite_sorted_distinct_unique:
nipkow@24645
  3846
shows "finite A \<Longrightarrow> EX! xs. set xs = A & sorted xs & distinct xs"
nipkow@24645
  3847
apply(drule finite_distinct_list)
nipkow@24645
  3848
apply clarify
nipkow@24645
  3849
apply(rule_tac a="sort xs" in ex1I)
nipkow@24645
  3850
apply (auto simp: sorted_distinct_set_unique)
nipkow@24645
  3851
done
nipkow@24645
  3852
haftmann@40096
  3853
lemma
haftmann@40096
  3854
  assumes "sorted xs"
haftmann@40096
  3855
  shows sorted_take: "sorted (take n xs)"
haftmann@40096
  3856
  and sorted_drop: "sorted (drop n xs)"
haftmann@40096
  3857
proof -
haftmann@40096
  3858
  from assms have "sorted (take n xs @ drop n xs)" by simp
haftmann@40096
  3859
  then show "sorted (take n xs)" and "sorted (drop n xs)"
haftmann@40096
  3860
    unfolding sorted_append by simp_all
haftmann@29626
  3861
qed
haftmann@29626
  3862
hoelzl@33639
  3863
lemma sorted_dropWhile: "sorted xs \<Longrightarrow> sorted (dropWhile P xs)"
haftmann@40096
  3864
  by (auto dest: sorted_drop simp add: dropWhile_eq_drop)
hoelzl@33639
  3865
hoelzl@33639
  3866
lemma sorted_takeWhile: "sorted xs \<Longrightarrow> sorted (takeWhile P xs)"
haftmann@40096
  3867
  by (subst takeWhile_eq_take) (auto dest: sorted_take)
haftmann@29626
  3868
hoelzl@34920
  3869
lemma sorted_filter:
hoelzl@34920
  3870
  "sorted (map f xs) \<Longrightarrow> sorted (map f (filter P xs))"
hoelzl@34920
  3871
  by (induct xs) (simp_all add: sorted_Cons)
hoelzl@34920
  3872
hoelzl@34920
  3873
lemma foldr_max_sorted:
hoelzl@34920
  3874
  assumes "sorted (rev xs)"
hoelzl@34920
  3875
  shows "foldr max xs y = (if xs = [] then y else max (xs ! 0) y)"
hoelzl@34920
  3876
using assms proof (induct xs)
hoelzl@34920
  3877
  case (Cons x xs)
hoelzl@34920
  3878
  moreover hence "sorted (rev xs)" using sorted_append by auto
hoelzl@34920
  3879
  ultimately show ?case
hoelzl@34920
  3880
    by (cases xs, auto simp add: sorted_append max_def)
hoelzl@34920
  3881
qed simp
hoelzl@34920
  3882
hoelzl@34920
  3883
lemma filter_equals_takeWhile_sorted_rev:
hoelzl@34920
  3884
  assumes sorted: "sorted (rev (map f xs))"
hoelzl@34920
  3885
  shows "[x \<leftarrow> xs. t < f x] = takeWhile (\<lambda> x. t < f x) xs"
hoelzl@34920
  3886
    (is "filter ?P xs = ?tW")
hoelzl@34920
  3887
proof (rule takeWhile_eq_filter[symmetric])
hoelzl@34920
  3888
  let "?dW" = "dropWhile ?P xs"
hoelzl@34920
  3889
  fix x assume "x \<in> set ?dW"
hoelzl@34920
  3890
  then obtain i where i: "i < length ?dW" and nth_i: "x = ?dW ! i"
hoelzl@34920
  3891
    unfolding in_set_conv_nth by auto
hoelzl@34920
  3892
  hence "length ?tW + i < length (?tW @ ?dW)"
hoelzl@34920
  3893
    unfolding length_append by simp
hoelzl@34920
  3894
  hence i': "length (map f ?tW) + i < length (map f xs)" by simp
hoelzl@34920
  3895
  have "(map f ?tW @ map f ?dW) ! (length (map f ?tW) + i) \<le>
hoelzl@34920
  3896
        (map f ?tW @ map f ?dW) ! (length (map f ?tW) + 0)"
hoelzl@34920
  3897
    using sorted_rev_nth_mono[OF sorted _ i', of "length ?tW"]
hoelzl@34920
  3898
    unfolding map_append[symmetric] by simp
hoelzl@34920
  3899
  hence "f x \<le> f (?dW ! 0)"
hoelzl@34920
  3900
    unfolding nth_append_length_plus nth_i
hoelzl@34920
  3901
    using i preorder_class.le_less_trans[OF le0 i] by simp
hoelzl@34920
  3902
  also have "... \<le> t"
hoelzl@34920
  3903
    using hd_dropWhile[of "?P" xs] le0[THEN preorder_class.le_less_trans, OF i]
hoelzl@34920
  3904
    using hd_conv_nth[of "?dW"] by simp
hoelzl@34920
  3905
  finally show "\<not> t < f x" by simp
hoelzl@34920
  3906
qed
hoelzl@34920
  3907
haftmann@40451
  3908
lemma insort_insert_key_triv:
haftmann@40451
  3909
  "f x \<in> f ` set xs \<Longrightarrow> insort_insert_key f x xs = xs"
haftmann@40451
  3910
  by (simp add: insort_insert_key_def)
haftmann@40451
  3911
haftmann@40451
  3912
lemma insort_insert_triv:
haftmann@40451
  3913
  "x \<in> set xs \<Longrightarrow> insort_insert x xs = xs"
haftmann@40451
  3914
  using insort_insert_key_triv [of "\<lambda>x. x"] by simp
haftmann@40451
  3915
haftmann@40451
  3916
lemma insort_insert_insort_key:
haftmann@40451
  3917
  "f x \<notin> f ` set xs \<Longrightarrow> insort_insert_key f x xs = insort_key f x xs"
haftmann@40451
  3918
  by (simp add: insort_insert_key_def)
haftmann@40451
  3919
haftmann@40451
  3920
lemma insort_insert_insort:
haftmann@40451
  3921
  "x \<notin> set xs \<Longrightarrow> insort_insert x xs = insort x xs"
haftmann@40451
  3922
  using insort_insert_insort_key [of "\<lambda>x. x"] by simp
haftmann@40451
  3923
haftmann@35608
  3924
lemma set_insort_insert:
haftmann@35608
  3925
  "set (insort_insert x xs) = insert x (set xs)"
haftmann@40451
  3926
  by (auto simp add: insort_insert_key_def set_insort)
haftmann@35608
  3927
haftmann@35608
  3928
lemma distinct_insort_insert:
haftmann@35608
  3929
  assumes "distinct xs"
haftmann@40451
  3930
  shows "distinct (insort_insert_key f x xs)"
haftmann@40451
  3931
  using assms by (induct xs) (auto simp add: insort_insert_key_def set_insort)
haftmann@40451
  3932
haftmann@40451
  3933
lemma sorted_insort_insert_key:
haftmann@40451
  3934
  assumes "sorted (map f xs)"
haftmann@40451
  3935
  shows "sorted (map f (insort_insert_key f x xs))"
haftmann@40451
  3936
  using assms by (simp add: insort_insert_key_def sorted_insort_key)
haftmann@35608
  3937
haftmann@35608
  3938
lemma sorted_insort_insert:
haftmann@35608
  3939
  assumes "sorted xs"
haftmann@35608
  3940
  shows "sorted (insort_insert x xs)"
haftmann@40451
  3941
  using assms sorted_insort_insert_key [of "\<lambda>x. x"] by simp
haftmann@40451
  3942
haftmann@40451
  3943
lemma filter_insort_triv:
haftmann@37091
  3944
  "\<not> P x \<Longrightarrow> filter P (insort_key f x xs) = filter P xs"
haftmann@37091
  3945
  by (induct xs) simp_all
haftmann@37091
  3946
haftmann@40451
  3947
lemma filter_insort:
haftmann@37091
  3948
  "sorted (map f xs) \<Longrightarrow> P x \<Longrightarrow> filter P (insort_key f x xs) = insort_key f x (filter P xs)"
haftmann@37091
  3949
  using assms by (induct xs)
haftmann@37091
  3950
    (auto simp add: sorted_Cons, subst insort_is_Cons, auto)
haftmann@37091
  3951
haftmann@40451
  3952
lemma filter_sort:
haftmann@37091
  3953
  "filter P (sort_key f xs) = sort_key f (filter P xs)"
haftmann@40451
  3954
  by (induct xs) (simp_all add: filter_insort_triv filter_insort)
haftmann@37091
  3955
haftmann@40547
  3956
lemma sorted_map_same:
haftmann@40547
  3957
  "sorted (map f [x\<leftarrow>xs. f x = g xs])"
haftmann@40547
  3958
proof (induct xs arbitrary: g)
haftmann@37091
  3959
  case Nil then show ?case by simp
haftmann@37091
  3960
next
haftmann@37091
  3961
  case (Cons x xs)
haftmann@40547
  3962
  then have "sorted (map f [y\<leftarrow>xs . f y = (\<lambda>xs. f x) xs])" .
haftmann@40547
  3963
  moreover from Cons have "sorted (map f [y\<leftarrow>xs . f y = (g \<circ> Cons x) xs])" .
haftmann@37091
  3964
  ultimately show ?case by (simp_all add: sorted_Cons)
haftmann@37091
  3965
qed
haftmann@37091
  3966
haftmann@40547
  3967
lemma sorted_same:
haftmann@40547
  3968
  "sorted [x\<leftarrow>xs. x = g xs]"
haftmann@40547
  3969
  using sorted_map_same [of "\<lambda>x. x"] by simp
haftmann@40547
  3970
haftmann@37091
  3971
lemma remove1_insort [simp]:
haftmann@37091
  3972
  "remove1 x (insort x xs) = xs"
haftmann@37091
  3973
  by (induct xs) simp_all
haftmann@37091
  3974
nipkow@24616
  3975
end
nipkow@24616
  3976
nipkow@25277
  3977
lemma sorted_upt[simp]: "sorted[i..<j]"
nipkow@25277
  3978
by (induct j) (simp_all add:sorted_append)
nipkow@25277
  3979
nipkow@32415
  3980
lemma sorted_upto[simp]: "sorted[i..j]"
nipkow@32415
  3981
apply(induct i j rule:upto.induct)
nipkow@32415
  3982
apply(subst upto.simps)
nipkow@32415
  3983
apply(simp add:sorted_Cons)
nipkow@32415
  3984
done
nipkow@32415
  3985
wenzelm@35118
  3986
wenzelm@35118
  3987
subsubsection {* @{const transpose} on sorted lists *}
hoelzl@34920
  3988
hoelzl@34920
  3989
lemma sorted_transpose[simp]:
hoelzl@34920
  3990
  shows "sorted (rev (map length (transpose xs)))"
hoelzl@34920
  3991
  by (auto simp: sorted_equals_nth_mono rev_nth nth_transpose
hoelzl@34920
  3992
    length_filter_conv_card intro: card_mono)
hoelzl@34920
  3993
hoelzl@34920
  3994
lemma transpose_max_length:
hoelzl@34920
  3995
  "foldr (\<lambda>xs. max (length xs)) (transpose xs) 0 = length [x \<leftarrow> xs. x \<noteq> []]"
hoelzl@34920
  3996
  (is "?L = ?R")
hoelzl@34920
  3997
proof (cases "transpose xs = []")
hoelzl@34920
  3998
  case False
hoelzl@34920
  3999
  have "?L = foldr max (map length (transpose xs)) 0"
hoelzl@34920
  4000
    by (simp add: foldr_map comp_def)
hoelzl@34920
  4001
  also have "... = length (transpose xs ! 0)"
hoelzl@34920
  4002
    using False sorted_transpose by (simp add: foldr_max_sorted)
hoelzl@34920
  4003
  finally show ?thesis
hoelzl@34920
  4004
    using False by (simp add: nth_transpose)
hoelzl@34920
  4005
next
hoelzl@34920
  4006
  case True
hoelzl@34920
  4007
  hence "[x \<leftarrow> xs. x \<noteq> []] = []"
hoelzl@34920
  4008
    by (auto intro!: filter_False simp: transpose_empty)
hoelzl@34920
  4009
  thus ?thesis by (simp add: transpose_empty True)
hoelzl@34920
  4010
qed
hoelzl@34920
  4011
hoelzl@34920
  4012
lemma length_transpose_sorted:
hoelzl@34920
  4013
  fixes xs :: "'a list list"
hoelzl@34920
  4014
  assumes sorted: "sorted (rev (map length xs))"
hoelzl@34920
  4015
  shows "length (transpose xs) = (if xs = [] then 0 else length (xs ! 0))"
hoelzl@34920
  4016
proof (cases "xs = []")
hoelzl@34920
  4017
  case False
hoelzl@34920
  4018
  thus ?thesis
hoelzl@34920
  4019
    using foldr_max_sorted[OF sorted] False
hoelzl@34920
  4020
    unfolding length_transpose foldr_map comp_def
hoelzl@34920
  4021
    by simp
hoelzl@34920
  4022
qed simp
hoelzl@34920
  4023
hoelzl@34920
  4024
lemma nth_nth_transpose_sorted[simp]:
hoelzl@34920
  4025
  fixes xs :: "'a list list"
hoelzl@34920
  4026
  assumes sorted: "sorted (rev (map length xs))"
hoelzl@34920
  4027
  and i: "i < length (transpose xs)"
hoelzl@34920
  4028
  and j: "j < length [ys \<leftarrow> xs. i < length ys]"
hoelzl@34920
  4029
  shows "transpose xs ! i ! j = xs ! j  ! i"
hoelzl@34920
  4030
  using j filter_equals_takeWhile_sorted_rev[OF sorted, of i]
hoelzl@34920
  4031
    nth_transpose[OF i] nth_map[OF j]
hoelzl@34920
  4032
  by (simp add: takeWhile_nth)
hoelzl@34920
  4033
hoelzl@34920
  4034
lemma transpose_column_length:
hoelzl@34920
  4035
  fixes xs :: "'a list list"
hoelzl@34920
  4036
  assumes sorted: "sorted (rev (map length xs))" and "i < length xs"
hoelzl@34920
  4037
  shows "length (filter (\<lambda>ys. i < length ys) (transpose xs)) = length (xs ! i)"
hoelzl@34920
  4038
proof -
hoelzl@34920
  4039
  have "xs \<noteq> []" using `i < length xs` by auto
hoelzl@34920
  4040
  note filter_equals_takeWhile_sorted_rev[OF sorted, simp]
hoelzl@34920
  4041
  { fix j assume "j \<le> i"
hoelzl@34920
  4042
    note sorted_rev_nth_mono[OF sorted, of j i, simplified, OF this `i < length xs`]
hoelzl@34920
  4043
  } note sortedE = this[consumes 1]
hoelzl@34920
  4044
hoelzl@34920
  4045
  have "{j. j < length (transpose xs) \<and> i < length (transpose xs ! j)}
hoelzl@34920
  4046
    = {..< length (xs ! i)}"
hoelzl@34920
  4047
  proof safe
hoelzl@34920
  4048
    fix j
hoelzl@34920
  4049
    assume "j < length (transpose xs)" and "i < length (transpose xs ! j)"
hoelzl@34920
  4050
    with this(2) nth_transpose[OF this(1)]
hoelzl@34920
  4051
    have "i < length (takeWhile (\<lambda>ys. j < length ys) xs)" by simp
hoelzl@34920
  4052
    from nth_mem[OF this] takeWhile_nth[OF this]
hoelzl@34920
  4053
    show "j < length (xs ! i)" by (auto dest: set_takeWhileD)
hoelzl@34920
  4054
  next
hoelzl@34920
  4055
    fix j assume "j < length (xs ! i)"
hoelzl@34920
  4056
    thus "j < length (transpose xs)"
hoelzl@34920
  4057
      using foldr_max_sorted[OF sorted] `xs \<noteq> []` sortedE[OF le0]
hoelzl@34920
  4058
      by (auto simp: length_transpose comp_def foldr_map)
hoelzl@34920
  4059
hoelzl@34920
  4060
    have "Suc i \<le> length (takeWhile (\<lambda>ys. j < length ys) xs)"
hoelzl@34920
  4061
      using `i < length xs` `j < length (xs ! i)` less_Suc_eq_le
hoelzl@34920
  4062
      by (auto intro!: length_takeWhile_less_P_nth dest!: sortedE)
hoelzl@34920
  4063
    with nth_transpose[OF `j < length (transpose xs)`]
hoelzl@34920
  4064
    show "i < length (transpose xs ! j)" by simp
hoelzl@34920
  4065
  qed
hoelzl@34920
  4066
  thus ?thesis by (simp add: length_filter_conv_card)
hoelzl@34920
  4067
qed
hoelzl@34920
  4068
hoelzl@34920
  4069
lemma transpose_column:
hoelzl@34920
  4070
  fixes xs :: "'a list list"
hoelzl@34920
  4071
  assumes sorted: "sorted (rev (map length xs))" and "i < length xs"
hoelzl@34920
  4072
  shows "map (\<lambda>ys. ys ! i) (filter (\<lambda>ys. i < length ys) (transpose xs))
hoelzl@34920
  4073
    = xs ! i" (is "?R = _")
hoelzl@34920
  4074
proof (rule nth_equalityI, safe)
hoelzl@34920
  4075
  show length: "length ?R = length (xs ! i)"
hoelzl@34920
  4076
    using transpose_column_length[OF assms] by simp
hoelzl@34920
  4077
hoelzl@34920
  4078
  fix j assume j: "j < length ?R"
hoelzl@34920
  4079
  note * = less_le_trans[OF this, unfolded length_map, OF length_filter_le]
hoelzl@34920
  4080
  from j have j_less: "j < length (xs ! i)" using length by simp
hoelzl@34920
  4081
  have i_less_tW: "Suc i \<le> length (takeWhile (\<lambda>ys. Suc j \<le> length ys) xs)"
hoelzl@34920
  4082
  proof (rule length_takeWhile_less_P_nth)
hoelzl@34920
  4083
    show "Suc i \<le> length xs" using `i < length xs` by simp
hoelzl@34920
  4084
    fix k assume "k < Suc i"
hoelzl@34920
  4085
    hence "k \<le> i" by auto
hoelzl@34920
  4086
    with sorted_rev_nth_mono[OF sorted this] `i < length xs`
hoelzl@34920
  4087
    have "length (xs ! i) \<le> length (xs ! k)" by simp
hoelzl@34920
  4088
    thus "Suc j \<le> length (xs ! k)" using j_less by simp
hoelzl@34920
  4089
  qed
hoelzl@34920
  4090
  have i_less_filter: "i < length [ys\<leftarrow>xs . j < length ys]"
hoelzl@34920
  4091
    unfolding filter_equals_takeWhile_sorted_rev[OF sorted, of j]
hoelzl@34920
  4092
    using i_less_tW by (simp_all add: Suc_le_eq)
hoelzl@34920
  4093
  from j show "?R ! j = xs ! i ! j"
hoelzl@34920
  4094
    unfolding filter_equals_takeWhile_sorted_rev[OF sorted_transpose, of i]
hoelzl@34920
  4095
    by (simp add: takeWhile_nth nth_nth_transpose_sorted[OF sorted * i_less_filter])
hoelzl@34920
  4096
qed
hoelzl@34920
  4097
hoelzl@34920
  4098
lemma transpose_transpose:
hoelzl@34920
  4099
  fixes xs :: "'a list list"
hoelzl@34920
  4100
  assumes sorted: "sorted (rev (map length xs))"
hoelzl@34920
  4101
  shows "transpose (transpose xs) = takeWhile (\<lambda>x. x \<noteq> []) xs" (is "?L = ?R")
hoelzl@34920
  4102
proof -
hoelzl@34920
  4103
  have len: "length ?L = length ?R"
hoelzl@34920
  4104
    unfolding length_transpose transpose_max_length
hoelzl@34920
  4105
    using filter_equals_takeWhile_sorted_rev[OF sorted, of 0]
hoelzl@34920
  4106
    by simp
hoelzl@34920
  4107
hoelzl@34920
  4108
  { fix i assume "i < length ?R"
hoelzl@34920
  4109
    with less_le_trans[OF _ length_takeWhile_le[of _ xs]]
hoelzl@34920
  4110
    have "i < length xs" by simp
hoelzl@34920
  4111
  } note * = this
hoelzl@34920
  4112
  show ?thesis
hoelzl@34920
  4113
    by (rule nth_equalityI)
hoelzl@34920
  4114
       (simp_all add: len nth_transpose transpose_column[OF sorted] * takeWhile_nth)
hoelzl@34920
  4115
qed
haftmann@34064
  4116
hoelzl@34921
  4117
theorem transpose_rectangle:
hoelzl@34921
  4118
  assumes "xs = [] \<Longrightarrow> n = 0"
hoelzl@34921
  4119
  assumes rect: "\<And> i. i < length xs \<Longrightarrow> length (xs ! i) = n"
hoelzl@34921
  4120
  shows "transpose xs = map (\<lambda> i. map (\<lambda> j. xs ! j ! i) [0..<length xs]) [0..<n]"
hoelzl@34921
  4121
    (is "?trans = ?map")
hoelzl@34921
  4122
proof (rule nth_equalityI)
hoelzl@34921
  4123
  have "sorted (rev (map length xs))"
hoelzl@34921
  4124
    by (auto simp: rev_nth rect intro!: sorted_nth_monoI)
hoelzl@34921
  4125
  from foldr_max_sorted[OF this] assms
hoelzl@34921
  4126
  show len: "length ?trans = length ?map"
hoelzl@34921
  4127
    by (simp_all add: length_transpose foldr_map comp_def)
hoelzl@34921
  4128
  moreover
hoelzl@34921
  4129
  { fix i assume "i < n" hence "[ys\<leftarrow>xs . i < length ys] = xs"
hoelzl@34921
  4130
      using rect by (auto simp: in_set_conv_nth intro!: filter_True) }
hoelzl@34921
  4131
  ultimately show "\<forall>i < length ?trans. ?trans ! i = ?map ! i"
hoelzl@34921
  4132
    by (auto simp: nth_transpose intro: nth_equalityI)
hoelzl@34921
  4133
qed
nipkow@24616
  4134
wenzelm@35118
  4135
nipkow@25069
  4136
subsubsection {* @{text sorted_list_of_set} *}
nipkow@25069
  4137
nipkow@25069
  4138
text{* This function maps (finite) linearly ordered sets to sorted
nipkow@25069
  4139
lists. Warning: in most cases it is not a good idea to convert from
nipkow@25069
  4140
sets to lists but one should convert in the other direction (via
nipkow@25069
  4141
@{const set}). *}
nipkow@25069
  4142
nipkow@25069
  4143
context linorder
nipkow@25069
  4144
begin
nipkow@25069
  4145
haftmann@35195
  4146
definition sorted_list_of_set :: "'a set \<Rightarrow> 'a list" where
haftmann@35195
  4147
  "sorted_list_of_set = Finite_Set.fold insort []"
haftmann@35195
  4148
haftmann@35195
  4149
lemma sorted_list_of_set_empty [simp]:
haftmann@35195
  4150
  "sorted_list_of_set {} = []"
haftmann@35195
  4151
  by (simp add: sorted_list_of_set_def)
haftmann@35195
  4152
haftmann@35195
  4153
lemma sorted_list_of_set_insert [simp]:
haftmann@35195
  4154
  assumes "finite A"
haftmann@35195
  4155
  shows "sorted_list_of_set (insert x A) = insort x (sorted_list_of_set (A - {x}))"
haftmann@35195
  4156
proof -
haftmann@35195
  4157
  interpret fun_left_comm insort by (fact fun_left_comm_insort)
haftmann@35195
  4158
  with assms show ?thesis by (simp add: sorted_list_of_set_def fold_insert_remove)
haftmann@35195
  4159
qed
haftmann@35195
  4160
haftmann@35195
  4161
lemma sorted_list_of_set [simp]:
haftmann@35195
  4162
  "finite A \<Longrightarrow> set (sorted_list_of_set A) = A \<and> sorted (sorted_list_of_set A) 
haftmann@35195
  4163
    \<and> distinct (sorted_list_of_set A)"
haftmann@35195
  4164
  by (induct A rule: finite_induct) (simp_all add: set_insort sorted_insort distinct_insort)
haftmann@35195
  4165
haftmann@35195
  4166
lemma sorted_list_of_set_sort_remdups:
haftmann@35195
  4167
  "sorted_list_of_set (set xs) = sort (remdups xs)"
haftmann@35195
  4168
proof -
haftmann@35195
  4169
  interpret fun_left_comm insort by (fact fun_left_comm_insort)
haftmann@35195
  4170
  show ?thesis by (simp add: sort_foldl_insort sorted_list_of_set_def fold_set_remdups)
haftmann@35195
  4171
qed
nipkow@25069
  4172
haftmann@37091
  4173
lemma sorted_list_of_set_remove:
haftmann@37091
  4174
  assumes "finite A"
haftmann@37091
  4175
  shows "sorted_list_of_set (A - {x}) = remove1 x (sorted_list_of_set A)"
haftmann@37091
  4176
proof (cases "x \<in> A")
haftmann@37091
  4177
  case False with assms have "x \<notin> set (sorted_list_of_set A)" by simp
haftmann@37091
  4178
  with False show ?thesis by (simp add: remove1_idem)
haftmann@37091
  4179
next
haftmann@37091
  4180
  case True then obtain B where A: "A = insert x B" by (rule Set.set_insert)
haftmann@37091
  4181
  with assms show ?thesis by simp
haftmann@37091
  4182
qed
haftmann@37091
  4183
nipkow@25069
  4184
end
nipkow@25069
  4185
haftmann@37091
  4186
lemma sorted_list_of_set_range [simp]:
haftmann@37091
  4187
  "sorted_list_of_set {m..<n} = [m..<n]"
haftmann@37091
  4188
  by (rule sorted_distinct_set_unique) simp_all
haftmann@37091
  4189
haftmann@37091
  4190
nipkow@15392
  4191
subsubsection {* @{text lists}: the list-forming operator over sets *}
nipkow@15302
  4192
berghofe@23740
  4193
inductive_set
berghofe@23740
  4194
  lists :: "'a set => 'a list set"
berghofe@23740
  4195
  for A :: "'a set"
berghofe@22262
  4196
where
nipkow@39859
  4197
    Nil [intro!, simp]: "[]: lists A"
nipkow@39859
  4198
  | Cons [intro!, simp, no_atp]: "[| a: A; l: lists A|] ==> a#l : lists A"
blanchet@35828
  4199
blanchet@35828
  4200
inductive_cases listsE [elim!,no_atp]: "x#l : lists A"
blanchet@35828
  4201
inductive_cases listspE [elim!,no_atp]: "listsp A (x # l)"
berghofe@23740
  4202
berghofe@23740
  4203
lemma listsp_mono [mono]: "A \<le> B ==> listsp A \<le> listsp B"
haftmann@34064
  4204
by (rule predicate1I, erule listsp.induct, (blast dest: predicate1D)+)
berghofe@26795
  4205
berghofe@26795
  4206
lemmas lists_mono = listsp_mono [to_set pred_subset_eq]
berghofe@22262
  4207
haftmann@22422
  4208
lemma listsp_infI:
haftmann@22422
  4209
  assumes l: "listsp A l" shows "listsp B l ==> listsp (inf A B) l" using l
nipkow@24349
  4210
by induct blast+
nipkow@15302
  4211
haftmann@22422
  4212
lemmas lists_IntI = listsp_infI [to_set]
haftmann@22422
  4213
haftmann@22422
  4214
lemma listsp_inf_eq [simp]: "listsp (inf A B) = inf (listsp A) (listsp B)"
haftmann@22422
  4215
proof (rule mono_inf [where f=listsp, THEN order_antisym])
berghofe@22262
  4216
  show "mono listsp" by (simp add: mono_def listsp_mono)
berghofe@26795
  4217
  show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro!: listsp_infI predicate1I)
nipkow@15302
  4218
qed
nipkow@15302
  4219
haftmann@22422
  4220
lemmas listsp_conj_eq [simp] = listsp_inf_eq [simplified inf_fun_eq inf_bool_eq]
haftmann@22422
  4221
berghofe@26795
  4222
lemmas lists_Int_eq [simp] = listsp_inf_eq [to_set pred_equals_eq]
berghofe@22262
  4223
nipkow@39859
  4224
lemma Cons_in_lists_iff[simp]: "x#xs : lists A \<longleftrightarrow> x:A \<and> xs : lists A"
nipkow@39859
  4225
by auto
nipkow@39859
  4226
berghofe@22262
  4227
lemma append_in_listsp_conv [iff]:
berghofe@22262
  4228
     "(listsp A (xs @ ys)) = (listsp A xs \<and> listsp A ys)"
nipkow@15302
  4229
by (induct xs) auto
nipkow@15302
  4230
berghofe@22262
  4231
lemmas append_in_lists_conv [iff] = append_in_listsp_conv [to_set]
berghofe@22262
  4232
berghofe@22262
  4233
lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)"
berghofe@22262
  4234
-- {* eliminate @{text listsp} in favour of @{text set} *}
nipkow@15302
  4235
by (induct xs) auto
nipkow@15302
  4236
berghofe@22262
  4237
lemmas in_lists_conv_set = in_listsp_conv_set [to_set]
berghofe@22262
  4238
blanchet@35828
  4239
lemma in_listspD [dest!,no_atp]: "listsp A xs ==> \<forall>x\<in>set xs. A x"
berghofe@22262
  4240
by (rule in_listsp_conv_set [THEN iffD1])
berghofe@22262
  4241
blanchet@35828
  4242
lemmas in_listsD [dest!,no_atp] = in_listspD [to_set]
blanchet@35828
  4243
blanchet@35828
  4244
lemma in_listspI [intro!,no_atp]: "\<forall>x\<in>set xs. A x ==> listsp A xs"
berghofe@22262
  4245
by (rule in_listsp_conv_set [THEN iffD2])
berghofe@22262
  4246
blanchet@35828
  4247
lemmas in_listsI [intro!,no_atp] = in_listspI [to_set]
nipkow@15302
  4248
nipkow@39821
  4249
lemma lists_eq_set: "lists A = {xs. set xs <= A}"
nipkow@39821
  4250
by auto
nipkow@39821
  4251
nipkow@39859
  4252
lemma lists_empty [simp]: "lists {} = {[]}"
nipkow@39859
  4253
by auto
nipkow@39859
  4254
nipkow@15302
  4255
lemma lists_UNIV [simp]: "lists UNIV = UNIV"
nipkow@15302
  4256
by auto
nipkow@15302
  4257
nipkow@17086
  4258
wenzelm@35118
  4259
subsubsection {* Inductive definition for membership *}
nipkow@17086
  4260
berghofe@23740
  4261
inductive ListMem :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"
berghofe@22262
  4262
where
berghofe@22262
  4263
    elem:  "ListMem x (x # xs)"
berghofe@22262
  4264
  | insert:  "ListMem x xs \<Longrightarrow> ListMem x (y # xs)"
berghofe@22262
  4265
berghofe@22262
  4266
lemma ListMem_iff: "(ListMem x xs) = (x \<in> set xs)"
nipkow@17086
  4267
apply (rule iffI)
nipkow@17086
  4268
 apply (induct set: ListMem)
nipkow@17086
  4269
  apply auto
nipkow@17086
  4270
apply (induct xs)
nipkow@17086
  4271
 apply (auto intro: ListMem.intros)
nipkow@17086
  4272
done
nipkow@17086
  4273
nipkow@17086
  4274
wenzelm@35118
  4275
subsubsection {* Lists as Cartesian products *}
nipkow@15302
  4276
nipkow@15302
  4277
text{*@{text"set_Cons A Xs"}: the set of lists with head drawn from
nipkow@15302
  4278
@{term A} and tail drawn from @{term Xs}.*}
nipkow@15302
  4279
haftmann@34928
  4280
definition
haftmann@34928
  4281
  set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set" where
haftmann@37767
  4282
  "set_Cons A XS = {z. \<exists>x xs. z = x # xs \<and> x \<in> A \<and> xs \<in> XS}"
nipkow@15302
  4283
paulson@17724
  4284
lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A"
nipkow@15302
  4285
by (auto simp add: set_Cons_def)
nipkow@15302
  4286
nipkow@15302
  4287
text{*Yields the set of lists, all of the same length as the argument and
nipkow@15302
  4288
with elements drawn from the corresponding element of the argument.*}
nipkow@15302
  4289
nipkow@15302
  4290
primrec
haftmann@34928
  4291
  listset :: "'a set list \<Rightarrow> 'a list set" where
haftmann@34928
  4292
     "listset [] = {[]}"
haftmann@34928
  4293
  |  "listset (A # As) = set_Cons A (listset As)"
nipkow@15302
  4294
nipkow@15302
  4295
wenzelm@35118
  4296
subsection {* Relations on Lists *}
paulson@15656
  4297
paulson@15656
  4298
subsubsection {* Length Lexicographic Ordering *}
paulson@15656
  4299
paulson@15656
  4300
text{*These orderings preserve well-foundedness: shorter lists 
paulson@15656
  4301
  precede longer lists. These ordering are not used in dictionaries.*}
haftmann@34928
  4302
        
haftmann@34928
  4303
primrec -- {*The lexicographic ordering for lists of the specified length*}
haftmann@34928
  4304
  lexn :: "('a \<times> 'a) set \<Rightarrow> nat \<Rightarrow> ('a list \<times> 'a list) set" where
haftmann@37767
  4305
    "lexn r 0 = {}"
haftmann@40856
  4306
  | "lexn r (Suc n) = (map_pair (%(x, xs). x#xs) (%(x, xs). x#xs) ` (r <*lex*> lexn r n)) Int
haftmann@34928
  4307
      {(xs, ys). length xs = Suc n \<and> length ys = Suc n}"
haftmann@34928
  4308
haftmann@34928
  4309
definition
haftmann@34928
  4310
  lex :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
haftmann@37767
  4311
  "lex r = (\<Union>n. lexn r n)" -- {*Holds only between lists of the same length*}
haftmann@34928
  4312
haftmann@34928
  4313
definition
haftmann@34928
  4314
  lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" where
haftmann@37767
  4315
  "lenlex r = inv_image (less_than <*lex*> lex r) (\<lambda>xs. (length xs, xs))"
haftmann@34928
  4316
        -- {*Compares lists by their length and then lexicographically*}
nipkow@15302
  4317
wenzelm@13142
  4318
lemma wf_lexn: "wf r ==> wf (lexn r n)"
paulson@15251
  4319
apply (induct n, simp, simp)
nipkow@13145
  4320
apply(rule wf_subset)
nipkow@13145
  4321
 prefer 2 apply (rule Int_lower1)
haftmann@40856
  4322
apply(rule wf_map_pair_image)
paulson@14208
  4323
 prefer 2 apply (rule inj_onI, auto)
nipkow@13145
  4324
done
wenzelm@13114
  4325
wenzelm@13114
  4326
lemma lexn_length:
nipkow@24526
  4327
  "(xs, ys) : lexn r n ==> length xs = n \<and> length ys = n"
nipkow@24526
  4328
by (induct n arbitrary: xs ys) auto
wenzelm@13114
  4329
wenzelm@13142
  4330
lemma wf_lex [intro!]: "wf r ==> wf (lex r)"
nipkow@13145
  4331
apply (unfold lex_def)
nipkow@13145
  4332
apply (rule wf_UN)
paulson@14208
  4333
apply (blast intro: wf_lexn, clarify)
nipkow@13145
  4334
apply (rename_tac m n)
nipkow@13145
  4335
apply (subgoal_tac "m \<noteq> n")
nipkow@13145
  4336
 prefer 2 apply blast
nipkow@13145
  4337
apply (blast dest: lexn_length not_sym)
nipkow@13145
  4338
done
wenzelm@13114
  4339
wenzelm@13114
  4340
lemma lexn_conv:
paulson@15656
  4341
  "lexn r n =
paulson@15656
  4342
    {(xs,ys). length xs = n \<and> length ys = n \<and>
paulson@15656
  4343
    (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}"
nipkow@18423
  4344
apply (induct n, simp)
paulson@14208
  4345
apply (simp add: image_Collect lex_prod_def, safe, blast)
paulson@14208
  4346
 apply (rule_tac x = "ab # xys" in exI, simp)
paulson@14208
  4347
apply (case_tac xys, simp_all, blast)
nipkow@13145
  4348
done
wenzelm@13114
  4349
wenzelm@13114
  4350
lemma lex_conv:
paulson@15656
  4351
  "lex r =
paulson@15656
  4352
    {(xs,ys). length xs = length ys \<and>
paulson@15656
  4353
    (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}"
nipkow@13145
  4354
by (force simp add: lex_def lexn_conv)
wenzelm@13114
  4355
nipkow@15693
  4356
lemma wf_lenlex [intro!]: "wf r ==> wf (lenlex r)"
nipkow@15693
  4357
by (unfold lenlex_def) blast
nipkow@15693
  4358
nipkow@15693
  4359
lemma lenlex_conv:
nipkow@15693
  4360
    "lenlex r = {(xs,ys). length xs < length ys |
paulson@15656
  4361
                 length xs = length ys \<and> (xs, ys) : lex r}"
nipkow@30198
  4362
by (simp add: lenlex_def Id_on_def lex_prod_def inv_image_def)
wenzelm@13114
  4363
wenzelm@13142
  4364
lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r"
nipkow@13145
  4365
by (simp add: lex_conv)
wenzelm@13114
  4366
wenzelm@13142
  4367
lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r"
nipkow@13145
  4368
by (simp add:lex_conv)
wenzelm@13114
  4369
paulson@18447
  4370
lemma Cons_in_lex [simp]:
paulson@15656
  4371
    "((x # xs, y # ys) : lex r) =
paulson@15656
  4372
      ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)"
nipkow@13145
  4373
apply (simp add: lex_conv)
nipkow@13145
  4374
apply (rule iffI)
paulson@14208
  4375
 prefer 2 apply (blast intro: Cons_eq_appendI, clarify)
paulson@14208
  4376
apply (case_tac xys, simp, simp)
nipkow@13145
  4377
apply blast
nipkow@13145
  4378
done
wenzelm@13114
  4379
wenzelm@13114
  4380
paulson@15656
  4381
subsubsection {* Lexicographic Ordering *}
paulson@15656
  4382
paulson@15656
  4383
text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b".
paulson@15656
  4384
    This ordering does \emph{not} preserve well-foundedness.
nipkow@17090
  4385
     Author: N. Voelker, March 2005. *} 
paulson@15656
  4386
haftmann@34928
  4387
definition
haftmann@34928
  4388
  lexord :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
haftmann@37767
  4389
  "lexord r = {(x,y ). \<exists> a v. y = x @ a # v \<or>
paulson@15656
  4390
            (\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}"
paulson@15656
  4391
paulson@15656
  4392
lemma lexord_Nil_left[simp]:  "([],y) \<in> lexord r = (\<exists> a x. y = a # x)"
nipkow@24349
  4393
by (unfold lexord_def, induct_tac y, auto) 
paulson@15656
  4394
paulson@15656
  4395
lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r"
nipkow@24349
  4396
by (unfold lexord_def, induct_tac x, auto)
paulson@15656
  4397
paulson@15656
  4398
lemma lexord_cons_cons[simp]:
paulson@15656
  4399
     "((a # x, b # y) \<in> lexord r) = ((a,b)\<in> r | (a = b & (x,y)\<in> lexord r))"
paulson@15656
  4400
  apply (unfold lexord_def, safe, simp_all)
paulson@15656
  4401
  apply (case_tac u, simp, simp)
paulson@15656
  4402
  apply (case_tac u, simp, clarsimp, blast, blast, clarsimp)
paulson@15656
  4403
  apply (erule_tac x="b # u" in allE)
paulson@15656
  4404
  by force
paulson@15656
  4405
paulson@15656
  4406
lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons
paulson@15656
  4407
paulson@15656
  4408
lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r"
nipkow@24349
  4409
by (induct_tac x, auto)  
paulson@15656
  4410
paulson@15656
  4411
lemma lexord_append_left_rightI:
paulson@15656
  4412
     "(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r"
nipkow@24349
  4413
by (induct_tac u, auto)
paulson@15656
  4414
paulson@15656
  4415
lemma lexord_append_leftI: " (u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r"
nipkow@24349
  4416
by (induct x, auto)
paulson@15656
  4417
paulson@15656
  4418
lemma lexord_append_leftD:
paulson@15656
  4419
     "\<lbrakk> (x @ u, x @ v) \<in> lexord r; (! a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r"
nipkow@24349
  4420
by (erule rev_mp, induct_tac x, auto)
paulson@15656
  4421
paulson@15656
  4422
lemma lexord_take_index_conv: 
paulson@15656
  4423
   "((x,y) : lexord r) = 
paulson@15656
  4424
    ((length x < length y \<and> take (length x) y = x) \<or> 
paulson@15656
  4425
     (\<exists>i. i < min(length x)(length y) & take i x = take i y & (x!i,y!i) \<in> r))"
paulson@15656
  4426
  apply (unfold lexord_def Let_def, clarsimp) 
paulson@15656
  4427
  apply (rule_tac f = "(% a b. a \<or> b)" in arg_cong2)
paulson@15656
  4428
  apply auto 
paulson@15656
  4429
  apply (rule_tac x="hd (drop (length x) y)" in exI)
paulson@15656
  4430
  apply (rule_tac x="tl (drop (length x) y)" in exI)
paulson@15656
  4431
  apply (erule subst, simp add: min_def) 
paulson@15656
  4432
  apply (rule_tac x ="length u" in exI, simp) 
paulson@15656
  4433
  apply (rule_tac x ="take i x" in exI) 
paulson@15656
  4434
  apply (rule_tac x ="x ! i" in exI) 
paulson@15656
  4435
  apply (rule_tac x ="y ! i" in exI, safe) 
paulson@15656
  4436
  apply (rule_tac x="drop (Suc i) x" in exI)
paulson@15656
  4437
  apply (drule sym, simp add: drop_Suc_conv_tl) 
paulson@15656
  4438
  apply (rule_tac x="drop (Suc i) y" in exI)
paulson@15656
  4439
  by (simp add: drop_Suc_conv_tl) 
paulson@15656
  4440
paulson@15656
  4441
-- {* lexord is extension of partial ordering List.lex *} 
paulson@15656
  4442
lemma lexord_lex: " (x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)"
paulson@15656
  4443
  apply (rule_tac x = y in spec) 
paulson@15656
  4444
  apply (induct_tac x, clarsimp) 
paulson@15656
  4445
  by (clarify, case_tac x, simp, force)
paulson@15656
  4446
paulson@15656
  4447
lemma lexord_irreflexive: "(! x. (x,x) \<notin> r) \<Longrightarrow> (y,y) \<notin> lexord r"
paulson@15656
  4448
  by (induct y, auto)
paulson@15656
  4449
paulson@15656
  4450
lemma lexord_trans: 
paulson@15656
  4451
    "\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r"
paulson@15656
  4452
   apply (erule rev_mp)+
paulson@15656
  4453
   apply (rule_tac x = x in spec) 
paulson@15656
  4454
  apply (rule_tac x = z in spec) 
paulson@15656
  4455
  apply ( induct_tac y, simp, clarify)
paulson@15656
  4456
  apply (case_tac xa, erule ssubst) 
paulson@15656
  4457
  apply (erule allE, erule allE) -- {* avoid simp recursion *} 
paulson@15656
  4458
  apply (case_tac x, simp, simp) 
paulson@24632
  4459
  apply (case_tac x, erule allE, erule allE, simp)
paulson@15656
  4460
  apply (erule_tac x = listb in allE) 
paulson@15656
  4461
  apply (erule_tac x = lista in allE, simp)
paulson@15656
  4462
  apply (unfold trans_def)
paulson@15656
  4463
  by blast
paulson@15656
  4464
paulson@15656
  4465
lemma lexord_transI:  "trans r \<Longrightarrow> trans (lexord r)"
nipkow@24349
  4466
by (rule transI, drule lexord_trans, blast) 
paulson@15656
  4467
paulson@15656
  4468
lemma lexord_linear: "(! a b. (a,b)\<in> r | a = b | (b,a) \<in> r) \<Longrightarrow> (x,y) : lexord r | x = y | (y,x) : lexord r"
paulson@15656
  4469
  apply (rule_tac x = y in spec) 
paulson@15656
  4470
  apply (induct_tac x, rule allI) 
paulson@15656
  4471
  apply (case_tac x, simp, simp) 
paulson@15656
  4472
  apply (rule allI, case_tac x, simp, simp) 
paulson@15656
  4473
  by blast
paulson@15656
  4474
paulson@15656
  4475
nipkow@40476
  4476
subsubsection {* Lexicographic combination of measure functions *}
krauss@21103
  4477
krauss@21103
  4478
text {* These are useful for termination proofs *}
krauss@21103
  4479
krauss@21103
  4480
definition
krauss@21103
  4481
  "measures fs = inv_image (lex less_than) (%a. map (%f. f a) fs)"
krauss@21103
  4482
krauss@21106
  4483
lemma wf_measures[recdef_wf, simp]: "wf (measures fs)"
nipkow@24349
  4484
unfolding measures_def
nipkow@24349
  4485
by blast
krauss@21103
  4486
krauss@21103
  4487
lemma in_measures[simp]: 
krauss@21103
  4488
  "(x, y) \<in> measures [] = False"
krauss@21103
  4489
  "(x, y) \<in> measures (f # fs)
krauss@21103
  4490
         = (f x < f y \<or> (f x = f y \<and> (x, y) \<in> measures fs))"  
nipkow@24349
  4491
unfolding measures_def
nipkow@24349
  4492
by auto
krauss@21103
  4493
krauss@21103
  4494
lemma measures_less: "f x < f y ==> (x, y) \<in> measures (f#fs)"
nipkow@24349
  4495
by simp
krauss@21103
  4496
krauss@21103
  4497
lemma measures_lesseq: "f x <= f y ==> (x, y) \<in> measures fs ==> (x, y) \<in> measures (f#fs)"
nipkow@24349
  4498
by auto
krauss@21103
  4499
krauss@21103
  4500
nipkow@40476
  4501
subsubsection {* Lifting Relations to Lists: one element *}
nipkow@40476
  4502
nipkow@40476
  4503
definition listrel1 :: "('a \<times> 'a) set \<Rightarrow> ('a list \<times> 'a list) set" where
nipkow@40476
  4504
"listrel1 r = {(xs,ys).
nipkow@40476
  4505
   \<exists>us z z' vs. xs = us @ z # vs \<and> (z,z') \<in> r \<and> ys = us @ z' # vs}"
nipkow@40476
  4506
nipkow@40476
  4507
lemma listrel1I:
nipkow@40476
  4508
  "\<lbrakk> (x, y) \<in> r;  xs = us @ x # vs;  ys = us @ y # vs \<rbrakk> \<Longrightarrow>
nipkow@40476
  4509
  (xs, ys) \<in> listrel1 r"
nipkow@40476
  4510
unfolding listrel1_def by auto
nipkow@40476
  4511
nipkow@40476
  4512
lemma listrel1E:
nipkow@40476
  4513
  "\<lbrakk> (xs, ys) \<in> listrel1 r;
nipkow@40476
  4514
     !!x y us vs. \<lbrakk> (x, y) \<in> r;  xs = us @ x # vs;  ys = us @ y # vs \<rbrakk> \<Longrightarrow> P
nipkow@40476
  4515
   \<rbrakk> \<Longrightarrow> P"
nipkow@40476
  4516
unfolding listrel1_def by auto
nipkow@40476
  4517
nipkow@40476
  4518
lemma not_Nil_listrel1 [iff]: "([], xs) \<notin> listrel1 r"
nipkow@40476
  4519
unfolding listrel1_def by blast
nipkow@40476
  4520
nipkow@40476
  4521
lemma not_listrel1_Nil [iff]: "(xs, []) \<notin> listrel1 r"
nipkow@40476
  4522
unfolding listrel1_def by blast
nipkow@40476
  4523
nipkow@40476
  4524
lemma Cons_listrel1_Cons [iff]:
nipkow@40476
  4525
  "(x # xs, y # ys) \<in> listrel1 r \<longleftrightarrow>
nipkow@40476
  4526
   (x,y) \<in> r \<and> xs = ys \<or> x = y \<and> (xs, ys) \<in> listrel1 r"
nipkow@40476
  4527
by (simp add: listrel1_def Cons_eq_append_conv) (blast)
nipkow@40476
  4528
nipkow@40476
  4529
lemma listrel1I1: "(x,y) \<in> r \<Longrightarrow> (x # xs, y # xs) \<in> listrel1 r"
nipkow@40476
  4530
by (metis Cons_listrel1_Cons)
nipkow@40476
  4531
nipkow@40476
  4532
lemma listrel1I2: "(xs, ys) \<in> listrel1 r \<Longrightarrow> (x # xs, x # ys) \<in> listrel1 r"
nipkow@40476
  4533
by (metis Cons_listrel1_Cons)
nipkow@40476
  4534
nipkow@40476
  4535
lemma append_listrel1I:
nipkow@40476
  4536
  "(xs, ys) \<in> listrel1 r \<and> us = vs \<or> xs = ys \<and> (us, vs) \<in> listrel1 r
nipkow@40476
  4537
    \<Longrightarrow> (xs @ us, ys @ vs) \<in> listrel1 r"
nipkow@40476
  4538
unfolding listrel1_def
nipkow@40476
  4539
by auto (blast intro: append_eq_appendI)+
nipkow@40476
  4540
nipkow@40476
  4541
lemma Cons_listrel1E1[elim!]:
nipkow@40476
  4542
  assumes "(x # xs, ys) \<in> listrel1 r"
nipkow@40476
  4543
    and "\<And>y. ys = y # xs \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R"
nipkow@40476
  4544
    and "\<And>zs. ys = x # zs \<Longrightarrow> (xs, zs) \<in> listrel1 r \<Longrightarrow> R"
nipkow@40476
  4545
  shows R
nipkow@40476
  4546
using assms by (cases ys) blast+
nipkow@40476
  4547
nipkow@40476
  4548
lemma Cons_listrel1E2[elim!]:
nipkow@40476
  4549
  assumes "(xs, y # ys) \<in> listrel1 r"
nipkow@40476
  4550
    and "\<And>x. xs = x # ys \<Longrightarrow> (x, y) \<in> r \<Longrightarrow> R"
nipkow@40476
  4551
    and "\<And>zs. xs = y # zs \<Longrightarrow> (zs, ys) \<in> listrel1 r \<Longrightarrow> R"
nipkow@40476
  4552
  shows R
nipkow@40476
  4553
using assms by (cases xs) blast+
nipkow@40476
  4554
nipkow@40476
  4555
lemma snoc_listrel1_snoc_iff:
nipkow@40476
  4556
  "(xs @ [x], ys @ [y]) \<in> listrel1 r
nipkow@40476
  4557
    \<longleftrightarrow> (xs, ys) \<in> listrel1 r \<and> x = y \<or> xs = ys \<and> (x,y) \<in> r" (is "?L \<longleftrightarrow> ?R")
nipkow@40476
  4558
proof
nipkow@40476
  4559
  assume ?L thus ?R
nipkow@40476
  4560
    by (fastsimp simp: listrel1_def snoc_eq_iff_butlast butlast_append)
nipkow@40476
  4561
next
nipkow@40476
  4562
  assume ?R then show ?L unfolding listrel1_def by force
nipkow@40476
  4563
qed
nipkow@40476
  4564
nipkow@40476
  4565
lemma listrel1_eq_len: "(xs,ys) \<in> listrel1 r \<Longrightarrow> length xs = length ys"
nipkow@40476
  4566
unfolding listrel1_def by auto
nipkow@40476
  4567
nipkow@40476
  4568
lemma listrel1_mono:
nipkow@40476
  4569
  "r \<subseteq> s \<Longrightarrow> listrel1 r \<subseteq> listrel1 s"
nipkow@40476
  4570
unfolding listrel1_def by blast
nipkow@40476
  4571
nipkow@40476
  4572
nipkow@40476
  4573
lemma listrel1_converse: "listrel1 (r^-1) = (listrel1 r)^-1"
nipkow@40476
  4574
unfolding listrel1_def by blast
nipkow@40476
  4575
nipkow@40476
  4576
lemma in_listrel1_converse:
nipkow@40476
  4577
  "(x,y) : listrel1 (r^-1) \<longleftrightarrow> (x,y) : (listrel1 r)^-1"
nipkow@40476
  4578
unfolding listrel1_def by blast
nipkow@40476
  4579
nipkow@40476
  4580
lemma listrel1_iff_update:
nipkow@40476
  4581
  "(xs,ys) \<in> (listrel1 r)
nipkow@40476
  4582
   \<longleftrightarrow> (\<exists>y n. (xs ! n, y) \<in> r \<and> n < length xs \<and> ys = xs[n:=y])" (is "?L \<longleftrightarrow> ?R")
nipkow@40476
  4583
proof
nipkow@40476
  4584
  assume "?L"
nipkow@40476
  4585
  then obtain x y u v where "xs = u @ x # v"  "ys = u @ y # v"  "(x,y) \<in> r"
nipkow@40476
  4586
    unfolding listrel1_def by auto
nipkow@40476
  4587
  then have "ys = xs[length u := y]" and "length u < length xs"
nipkow@40476
  4588
    and "(xs ! length u, y) \<in> r" by auto
nipkow@40476
  4589
  then show "?R" by auto
nipkow@40476
  4590
next
nipkow@40476
  4591
  assume "?R"
nipkow@40476
  4592
  then obtain x y n where "(xs!n, y) \<in> r" "n < size xs" "ys = xs[n:=y]" "x = xs!n"
nipkow@40476
  4593
    by auto
nipkow@40476
  4594
  then obtain u v where "xs = u @ x # v" and "ys = u @ y # v" and "(x, y) \<in> r"
nipkow@40476
  4595
    by (auto intro: upd_conv_take_nth_drop id_take_nth_drop)
nipkow@40476
  4596
  then show "?L" by (auto simp: listrel1_def)
nipkow@40476
  4597
qed
nipkow@40476
  4598
nipkow@40476
  4599
nipkow@40476
  4600
text{* Accessible part of @{term listrel1} relations: *}
nipkow@40476
  4601
nipkow@40476
  4602
lemma Cons_acc_listrel1I [intro!]:
nipkow@40476
  4603
  "x \<in> acc r \<Longrightarrow> xs \<in> acc (listrel1 r) \<Longrightarrow> (x # xs) \<in> acc (listrel1 r)"
nipkow@40476
  4604
apply (induct arbitrary: xs set: acc)
nipkow@40476
  4605
apply (erule thin_rl)
nipkow@40476
  4606
apply (erule acc_induct)
nipkow@40476
  4607
apply (rule accI)
nipkow@40476
  4608
apply (blast)
nipkow@40476
  4609
done
nipkow@40476
  4610
nipkow@40476
  4611
lemma lists_accD: "xs \<in> lists (acc r) \<Longrightarrow> xs \<in> acc (listrel1 r)"
nipkow@40476
  4612
apply (induct set: lists)
nipkow@40476
  4613
 apply (rule accI)
nipkow@40476
  4614
 apply simp
nipkow@40476
  4615
apply (rule accI)
nipkow@40476
  4616
apply (fast dest: acc_downward)
nipkow@40476
  4617
done
nipkow@40476
  4618
nipkow@40476
  4619
lemma lists_accI: "xs \<in> acc (listrel1 r) \<Longrightarrow> xs \<in> lists (acc r)"
nipkow@40476
  4620
apply (induct set: acc)
nipkow@40476
  4621
apply clarify
nipkow@40476
  4622
apply (rule accI)
nipkow@40476
  4623
apply (fastsimp dest!: in_set_conv_decomp[THEN iffD1] simp: listrel1_def)
nipkow@40476
  4624
done
nipkow@40476
  4625
nipkow@40476
  4626
nipkow@40476
  4627
subsubsection {* Lifting Relations to Lists: all elements *}
nipkow@15302
  4628
berghofe@23740
  4629
inductive_set
berghofe@23740
  4630
  listrel :: "('a * 'a)set => ('a list * 'a list)set"
berghofe@23740
  4631
  for r :: "('a * 'a)set"
berghofe@22262
  4632
where
berghofe@23740
  4633
    Nil:  "([],[]) \<in> listrel r"
berghofe@23740
  4634
  | Cons: "[| (x,y) \<in> r; (xs,ys) \<in> listrel r |] ==> (x#xs, y#ys) \<in> listrel r"
berghofe@23740
  4635
berghofe@23740
  4636
inductive_cases listrel_Nil1 [elim!]: "([],xs) \<in> listrel r"
berghofe@23740
  4637
inductive_cases listrel_Nil2 [elim!]: "(xs,[]) \<in> listrel r"
berghofe@23740
  4638
inductive_cases listrel_Cons1 [elim!]: "(y#ys,xs) \<in> listrel r"
berghofe@23740
  4639
inductive_cases listrel_Cons2 [elim!]: "(xs,y#ys) \<in> listrel r"
nipkow@15302
  4640
nipkow@15302
  4641
nipkow@40476
  4642
lemma listrel_eq_len:  "(xs, ys) \<in> listrel r \<Longrightarrow> length xs = length ys"
nipkow@40476
  4643
by(induct rule: listrel.induct) auto
nipkow@40476
  4644
nipkow@40476
  4645
lemma listrel_iff_zip: "(xs,ys) : listrel r \<longleftrightarrow>
nipkow@40476
  4646
  length xs = length ys & (\<forall>(x,y) \<in> set(zip xs ys). (x,y) \<in> r)" (is "?L \<longleftrightarrow> ?R")
nipkow@40476
  4647
proof
nipkow@40476
  4648
  assume ?L thus ?R by induct (auto intro: listrel_eq_len)
nipkow@40476
  4649
next
nipkow@40476
  4650
  assume ?R thus ?L
nipkow@40476
  4651
    apply (clarify)
nipkow@40476
  4652
    by (induct rule: list_induct2) (auto intro: listrel.intros)
nipkow@40476
  4653
qed
nipkow@40476
  4654
nipkow@40476
  4655
lemma listrel_iff_nth: "(xs,ys) : listrel r \<longleftrightarrow>
nipkow@40476
  4656
  length xs = length ys & (\<forall>n < length xs. (xs!n, ys!n) \<in> r)" (is "?L \<longleftrightarrow> ?R")
nipkow@40476
  4657
by (auto simp add: all_set_conv_all_nth listrel_iff_zip)
nipkow@40476
  4658
nipkow@40476
  4659
nipkow@15302
  4660
lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s"
nipkow@15302
  4661
apply clarify  
berghofe@23740
  4662
apply (erule listrel.induct)
berghofe@23740
  4663
apply (blast intro: listrel.intros)+
nipkow@15281
  4664
done
nipkow@15281
  4665
nipkow@15302
  4666
lemma listrel_subset: "r \<subseteq> A \<times> A \<Longrightarrow> listrel r \<subseteq> lists A \<times> lists A"
nipkow@15302
  4667
apply clarify 
berghofe@23740
  4668
apply (erule listrel.induct, auto) 
nipkow@13145
  4669
done
wenzelm@13114
  4670
nipkow@30198
  4671
lemma listrel_refl_on: "refl_on A r \<Longrightarrow> refl_on (lists A) (listrel r)" 
nipkow@30198
  4672
apply (simp add: refl_on_def listrel_subset Ball_def)
nipkow@15302
  4673
apply (rule allI) 
nipkow@15302
  4674
apply (induct_tac x) 
berghofe@23740
  4675
apply (auto intro: listrel.intros)
nipkow@13145
  4676
done
wenzelm@13114
  4677
nipkow@15302
  4678
lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)" 
nipkow@15302
  4679
apply (auto simp add: sym_def)
berghofe@23740
  4680
apply (erule listrel.induct) 
berghofe@23740
  4681
apply (blast intro: listrel.intros)+
nipkow@15281
  4682
done
nipkow@15281
  4683
nipkow@15302
  4684
lemma listrel_trans: "trans r \<Longrightarrow> trans (listrel r)" 
nipkow@15302
  4685
apply (simp add: trans_def)
nipkow@15302
  4686
apply (intro allI) 
nipkow@15302
  4687
apply (rule impI) 
berghofe@23740
  4688
apply (erule listrel.induct) 
berghofe@23740
  4689
apply (blast intro: listrel.intros)+
nipkow@15281
  4690
done
nipkow@15281
  4691
nipkow@15302
  4692
theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)"
nipkow@30198
  4693
by (simp add: equiv_def listrel_refl_on listrel_sym listrel_trans) 
nipkow@15302
  4694
nipkow@40476
  4695
lemma listrel_rtrancl_refl[iff]: "(xs,xs) : listrel(r^*)"
nipkow@40476
  4696
using listrel_refl_on[of UNIV, OF refl_rtrancl]
nipkow@40476
  4697
by(auto simp: refl_on_def)
nipkow@40476
  4698
nipkow@40476
  4699
lemma listrel_rtrancl_trans:
nipkow@40476
  4700
  "\<lbrakk> (xs,ys) : listrel(r^*);  (ys,zs) : listrel(r^*) \<rbrakk>
nipkow@40476
  4701
  \<Longrightarrow> (xs,zs) : listrel(r^*)"
nipkow@40476
  4702
by (metis listrel_trans trans_def trans_rtrancl)
nipkow@40476
  4703
nipkow@40476
  4704
nipkow@15302
  4705
lemma listrel_Nil [simp]: "listrel r `` {[]} = {[]}"
berghofe@23740
  4706
by (blast intro: listrel.intros)
nipkow@15302
  4707
nipkow@15302
  4708
lemma listrel_Cons:
haftmann@33301
  4709
     "listrel r `` {x#xs} = set_Cons (r``{x}) (listrel r `` {xs})"
haftmann@33301
  4710
by (auto simp add: set_Cons_def intro: listrel.intros)
nipkow@15302
  4711
nipkow@40476
  4712
text {* Relating @{term listrel1}, @{term listrel} and closures: *}
nipkow@40476
  4713
nipkow@40476
  4714
lemma listrel1_rtrancl_subset_rtrancl_listrel1:
nipkow@40476
  4715
  "listrel1 (r^*) \<subseteq> (listrel1 r)^*"
nipkow@40476
  4716
proof (rule subrelI)
nipkow@40476
  4717
  fix xs ys assume 1: "(xs,ys) \<in> listrel1 (r^*)"
nipkow@40476
  4718
  { fix x y us vs
nipkow@40476
  4719
    have "(x,y) : r^* \<Longrightarrow> (us @ x # vs, us @ y # vs) : (listrel1 r)^*"
nipkow@40476
  4720
    proof(induct rule: rtrancl.induct)
nipkow@40476
  4721
      case rtrancl_refl show ?case by simp
nipkow@40476
  4722
    next
nipkow@40476
  4723
      case rtrancl_into_rtrancl thus ?case
nipkow@40476
  4724
        by (metis listrel1I rtrancl.rtrancl_into_rtrancl)
nipkow@40476
  4725
    qed }
nipkow@40476
  4726
  thus "(xs,ys) \<in> (listrel1 r)^*" using 1 by(blast elim: listrel1E)
nipkow@40476
  4727
qed
nipkow@40476
  4728
nipkow@40476
  4729
lemma rtrancl_listrel1_eq_len: "(x,y) \<in> (listrel1 r)^* \<Longrightarrow> length x = length y"
nipkow@40476
  4730
by (induct rule: rtrancl.induct) (auto intro: listrel1_eq_len)
nipkow@40476
  4731
nipkow@40476
  4732
lemma rtrancl_listrel1_ConsI1:
nipkow@40476
  4733
  "(xs,ys) : (listrel1 r)^* \<Longrightarrow> (x#xs,x#ys) : (listrel1 r)^*"
nipkow@40476
  4734
apply(induct rule: rtrancl.induct)
nipkow@40476
  4735
 apply simp
nipkow@40476
  4736
by (metis listrel1I2 rtrancl.rtrancl_into_rtrancl)
nipkow@40476
  4737
nipkow@40476
  4738
lemma rtrancl_listrel1_ConsI2:
nipkow@40476
  4739
  "(x,y) \<in> r^* \<Longrightarrow> (xs, ys) \<in> (listrel1 r)^*
nipkow@40476
  4740
  \<Longrightarrow> (x # xs, y # ys) \<in> (listrel1 r)^*"
nipkow@40476
  4741
  by (blast intro: rtrancl_trans rtrancl_listrel1_ConsI1 
nipkow@40476
  4742
    subsetD[OF listrel1_rtrancl_subset_rtrancl_listrel1 listrel1I1])
nipkow@40476
  4743
nipkow@40476
  4744
lemma listrel1_subset_listrel:
nipkow@40476
  4745
  "r \<subseteq> r' \<Longrightarrow> refl r' \<Longrightarrow> listrel1 r \<subseteq> listrel(r')"
nipkow@40476
  4746
by(auto elim!: listrel1E simp add: listrel_iff_zip set_zip refl_on_def)
nipkow@40476
  4747
nipkow@40476
  4748
lemma listrel_reflcl_if_listrel1:
nipkow@40476
  4749
  "(xs,ys) : listrel1 r \<Longrightarrow> (xs,ys) : listrel(r^*)"
nipkow@40476
  4750
by(erule listrel1E)(auto simp add: listrel_iff_zip set_zip)
nipkow@40476
  4751
nipkow@40476
  4752
lemma listrel_rtrancl_eq_rtrancl_listrel1: "listrel (r^*) = (listrel1 r)^*"
nipkow@40476
  4753
proof
nipkow@40476
  4754
  { fix x y assume "(x,y) \<in> listrel (r^*)"
nipkow@40476
  4755
    then have "(x,y) \<in> (listrel1 r)^*"
nipkow@40476
  4756
    by induct (auto intro: rtrancl_listrel1_ConsI2) }
nipkow@40476
  4757
  then show "listrel (r^*) \<subseteq> (listrel1 r)^*"
nipkow@40476
  4758
    by (rule subrelI)
nipkow@40476
  4759
next
nipkow@40476
  4760
  show "listrel (r^*) \<supseteq> (listrel1 r)^*"
nipkow@40476
  4761
  proof(rule subrelI)
nipkow@40476
  4762
    fix xs ys assume "(xs,ys) \<in> (listrel1 r)^*"
nipkow@40476
  4763
    then show "(xs,ys) \<in> listrel (r^*)"
nipkow@40476
  4764
    proof induct
nipkow@40476
  4765
      case base show ?case by(auto simp add: listrel_iff_zip set_zip)
nipkow@40476
  4766
    next
nipkow@40476
  4767
      case (step ys zs)
nipkow@40476
  4768
      thus ?case  by (metis listrel_reflcl_if_listrel1 listrel_rtrancl_trans)
nipkow@40476
  4769
    qed
nipkow@40476
  4770
  qed
nipkow@40476
  4771
qed
nipkow@40476
  4772
nipkow@40476
  4773
lemma rtrancl_listrel1_if_listrel:
nipkow@40476
  4774
  "(xs,ys) : listrel r \<Longrightarrow> (xs,ys) : (listrel1 r)^*"
nipkow@40476
  4775
by(metis listrel_rtrancl_eq_rtrancl_listrel1 subsetD[OF listrel_mono] r_into_rtrancl subsetI)
nipkow@40476
  4776
nipkow@40476
  4777
lemma listrel_subset_rtrancl_listrel1: "listrel r \<subseteq> (listrel1 r)^*"
nipkow@40476
  4778
by(fast intro:rtrancl_listrel1_if_listrel)
nipkow@40476
  4779
nipkow@15302
  4780
krauss@26749
  4781
subsection {* Size function *}
krauss@26749
  4782
krauss@26875
  4783
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (list_size f)"
krauss@26875
  4784
by (rule is_measure_trivial)
krauss@26875
  4785
krauss@26875
  4786
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (option_size f)"
krauss@26875
  4787
by (rule is_measure_trivial)
krauss@26875
  4788
krauss@26875
  4789
lemma list_size_estimation[termination_simp]: 
krauss@26875
  4790
  "x \<in> set xs \<Longrightarrow> y < f x \<Longrightarrow> y < list_size f xs"
krauss@26749
  4791
by (induct xs) auto
krauss@26749
  4792
krauss@26875
  4793
lemma list_size_estimation'[termination_simp]: 
krauss@26875
  4794
  "x \<in> set xs \<Longrightarrow> y \<le> f x \<Longrightarrow> y \<le> list_size f xs"
krauss@26875
  4795
by (induct xs) auto
krauss@26875
  4796
krauss@26875
  4797
lemma list_size_map[simp]: "list_size f (map g xs) = list_size (f o g) xs"
krauss@26875
  4798
by (induct xs) auto
krauss@26875
  4799
krauss@26875
  4800
lemma list_size_pointwise[termination_simp]: 
krauss@26875
  4801
  "(\<And>x. x \<in> set xs \<Longrightarrow> f x < g x) \<Longrightarrow> list_size f xs \<le> list_size g xs"
krauss@26875
  4802
by (induct xs) force+
krauss@26749
  4803
haftmann@31048
  4804
haftmann@33301
  4805
subsection {* Transfer *}
haftmann@33301
  4806
haftmann@33301
  4807
definition
haftmann@33301
  4808
  embed_list :: "nat list \<Rightarrow> int list"
haftmann@33301
  4809
where
haftmann@33301
  4810
  "embed_list l = map int l"
haftmann@33301
  4811
haftmann@33301
  4812
definition
haftmann@33301
  4813
  nat_list :: "int list \<Rightarrow> bool"
haftmann@33301
  4814
where
haftmann@33301
  4815
  "nat_list l = nat_set (set l)"
haftmann@33301
  4816
haftmann@33301
  4817
definition
haftmann@33301
  4818
  return_list :: "int list \<Rightarrow> nat list"
haftmann@33301
  4819
where
haftmann@33301
  4820
  "return_list l = map nat l"
haftmann@33301
  4821
haftmann@33301
  4822
lemma transfer_nat_int_list_return_embed: "nat_list l \<longrightarrow>
haftmann@33301
  4823
    embed_list (return_list l) = l"
haftmann@33301
  4824
  unfolding embed_list_def return_list_def nat_list_def nat_set_def
haftmann@33301
  4825
  apply (induct l)
haftmann@33301
  4826
  apply auto
haftmann@33301
  4827
done
haftmann@33301
  4828
haftmann@33301
  4829
lemma transfer_nat_int_list_functions:
haftmann@33301
  4830
  "l @ m = return_list (embed_list l @ embed_list m)"
haftmann@33301
  4831
  "[] = return_list []"
haftmann@33301
  4832
  unfolding return_list_def embed_list_def
haftmann@33301
  4833
  apply auto
haftmann@33301
  4834
  apply (induct l, auto)
haftmann@33301
  4835
  apply (induct m, auto)
haftmann@33301
  4836
done
haftmann@33301
  4837
haftmann@33301
  4838
(*
haftmann@33301
  4839
lemma transfer_nat_int_fold1: "fold f l x =
haftmann@33301
  4840
    fold (%x. f (nat x)) (embed_list l) x";
haftmann@33301
  4841
*)
haftmann@33301
  4842
haftmann@33301
  4843
haftmann@37605
  4844
subsection {* Code generation *}
haftmann@37605
  4845
haftmann@37605
  4846
subsubsection {* Counterparts for set-related operations *}
haftmann@37605
  4847
haftmann@37605
  4848
definition member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool" where
haftmann@37605
  4849
  [code_post]: "member xs x \<longleftrightarrow> x \<in> set xs"
haftmann@37605
  4850
haftmann@37605
  4851
text {*
haftmann@37605
  4852
  Only use @{text member} for generating executable code.  Otherwise use
haftmann@37605
  4853
  @{prop "x \<in> set xs"} instead --- it is much easier to reason about.
haftmann@37605
  4854
*}
haftmann@37605
  4855
haftmann@37605
  4856
lemma member_set:
haftmann@37605
  4857
  "member = set"
nipkow@39535
  4858
  by (simp add: fun_eq_iff member_def mem_def)
haftmann@37605
  4859
haftmann@37605
  4860
lemma member_rec [code]:
haftmann@37605
  4861
  "member (x # xs) y \<longleftrightarrow> x = y \<or> member xs y"
haftmann@37605
  4862
  "member [] y \<longleftrightarrow> False"
haftmann@37605
  4863
  by (auto simp add: member_def)
haftmann@37605
  4864
haftmann@37605
  4865
lemma in_set_member [code_unfold]:
haftmann@37605
  4866
  "x \<in> set xs \<longleftrightarrow> member xs x"
haftmann@37605
  4867
  by (simp add: member_def)
haftmann@37605
  4868
haftmann@37605
  4869
declare INFI_def [code_unfold]
haftmann@37605
  4870
declare SUPR_def [code_unfold]
haftmann@37605
  4871
haftmann@37605
  4872
declare set_map [symmetric, code_unfold]
haftmann@37605
  4873
haftmann@37605
  4874
definition list_all :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
haftmann@37605
  4875
  list_all_iff [code_post]: "list_all P xs \<longleftrightarrow> (\<forall>x \<in> set xs. P x)"
haftmann@37605
  4876
haftmann@37605
  4877
definition list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" where
haftmann@37605
  4878
  list_ex_iff [code_post]: "list_ex P xs \<longleftrightarrow> (\<exists>x \<in> set xs. P x)"
haftmann@37605
  4879
bulwahn@40900
  4880
definition list_ex1
bulwahn@40900
  4881
where
bulwahn@40900
  4882
  list_ex1_iff: "list_ex1 P xs \<longleftrightarrow> (\<exists>! x. x \<in> set xs \<and> P x)"
bulwahn@40900
  4883
haftmann@37605
  4884
text {*
haftmann@37605
  4885
  Usually you should prefer @{text "\<forall>x\<in>set xs"} and @{text "\<exists>x\<in>set xs"}
haftmann@37605
  4886
  over @{const list_all} and @{const list_ex} in specifications.
haftmann@37605
  4887
*}
haftmann@37605
  4888
haftmann@37605
  4889
lemma list_all_simps [simp, code]:
haftmann@37605
  4890
  "list_all P (x # xs) \<longleftrightarrow> P x \<and> list_all P xs"
haftmann@37605
  4891
  "list_all P [] \<longleftrightarrow> True"
haftmann@37605
  4892
  by (simp_all add: list_all_iff)
haftmann@37605
  4893
haftmann@37605
  4894
lemma list_ex_simps [simp, code]:
haftmann@37605
  4895
  "list_ex P (x # xs) \<longleftrightarrow> P x \<or> list_ex P xs"
haftmann@37605
  4896
  "list_ex P [] \<longleftrightarrow> False"
haftmann@37605
  4897
  by (simp_all add: list_ex_iff)
haftmann@37605
  4898
bulwahn@40900
  4899
lemma list_ex1_simps [simp, code]:
bulwahn@40900
  4900
  "list_ex1 P [] = False"
bulwahn@40900
  4901
  "list_ex1 P (x # xs) = (if P x then list_all (\<lambda>y. \<not> P y \<or> x = y) xs else list_ex1 P xs)"
bulwahn@40900
  4902
unfolding list_ex1_iff list_all_iff by auto
bulwahn@40900
  4903
haftmann@37605
  4904
lemma Ball_set_list_all [code_unfold]:
haftmann@37605
  4905
  "Ball (set xs) P \<longleftrightarrow> list_all P xs"
haftmann@37605
  4906
  by (simp add: list_all_iff)
haftmann@37605
  4907
haftmann@37605
  4908
lemma Bex_set_list_ex [code_unfold]:
haftmann@37605
  4909
  "Bex (set xs) P \<longleftrightarrow> list_ex P xs"
haftmann@37605
  4910
  by (simp add: list_ex_iff)
haftmann@37605
  4911
haftmann@37605
  4912
lemma list_all_append [simp]:
haftmann@37605
  4913
  "list_all P (xs @ ys) \<longleftrightarrow> list_all P xs \<and> list_all P ys"
haftmann@37605
  4914
  by (auto simp add: list_all_iff)
haftmann@37605
  4915
haftmann@37605
  4916
lemma list_ex_append [simp]:
haftmann@37605
  4917
  "list_ex P (xs @ ys) \<longleftrightarrow> list_ex P xs \<or> list_ex P ys"
haftmann@37605
  4918
  by (auto simp add: list_ex_iff)
haftmann@37605
  4919
haftmann@37605
  4920
lemma list_all_rev [simp]:
haftmann@37605
  4921
  "list_all P (rev xs) \<longleftrightarrow> list_all P xs"
haftmann@37605
  4922
  by (simp add: list_all_iff)
haftmann@37605
  4923
haftmann@37605
  4924
lemma list_ex_rev [simp]:
haftmann@37605
  4925
  "list_ex P (rev xs) \<longleftrightarrow> list_ex P xs"
haftmann@37605
  4926
  by (simp add: list_ex_iff)
haftmann@37605
  4927
haftmann@37605
  4928
lemma list_all_length:
haftmann@37605
  4929
  "list_all P xs \<longleftrightarrow> (\<forall>n < length xs. P (xs ! n))"
haftmann@37605
  4930
  by (auto simp add: list_all_iff set_conv_nth)
haftmann@37605
  4931
haftmann@37605
  4932
lemma list_ex_length:
haftmann@37605
  4933
  "list_ex P xs \<longleftrightarrow> (\<exists>n < length xs. P (xs ! n))"
haftmann@37605
  4934
  by (auto simp add: list_ex_iff set_conv_nth)
haftmann@37605
  4935
haftmann@37605
  4936
lemma list_all_cong [fundef_cong]:
haftmann@37605
  4937
  "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> list_all f xs = list_all g ys"
haftmann@37605
  4938
  by (simp add: list_all_iff)
haftmann@37605
  4939
haftmann@37605
  4940
lemma list_any_cong [fundef_cong]:
haftmann@37605
  4941
  "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> list_ex f xs = list_ex g ys"
haftmann@37605
  4942
  by (simp add: list_ex_iff)
haftmann@37605
  4943
haftmann@37605
  4944
text {* Bounded quantification and summation over nats. *}
haftmann@37605
  4945
haftmann@37605
  4946
lemma atMost_upto [code_unfold]:
haftmann@37605
  4947
  "{..n} = set [0..<Suc n]"
haftmann@37605
  4948
  by auto
haftmann@37605
  4949
haftmann@37605
  4950
lemma atLeast_upt [code_unfold]:
haftmann@37605
  4951
  "{..<n} = set [0..<n]"
haftmann@37605
  4952
  by auto
haftmann@37605
  4953
haftmann@37605
  4954
lemma greaterThanLessThan_upt [code_unfold]:
haftmann@37605
  4955
  "{n<..<m} = set [Suc n..<m]"
haftmann@37605
  4956
  by auto
haftmann@37605
  4957
haftmann@37605
  4958
lemmas atLeastLessThan_upt [code_unfold] = set_upt [symmetric]
haftmann@37605
  4959
haftmann@37605
  4960
lemma greaterThanAtMost_upt [code_unfold]:
haftmann@37605
  4961
  "{n<..m} = set [Suc n..<Suc m]"
haftmann@37605
  4962
  by auto
haftmann@37605
  4963
haftmann@37605
  4964
lemma atLeastAtMost_upt [code_unfold]:
haftmann@37605
  4965
  "{n..m} = set [n..<Suc m]"
haftmann@37605
  4966
  by auto
haftmann@37605
  4967
haftmann@37605
  4968
lemma all_nat_less_eq [code_unfold]:
haftmann@37605
  4969
  "(\<forall>m<n\<Colon>nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..<n}. P m)"
haftmann@37605
  4970
  by auto
haftmann@37605
  4971
haftmann@37605
  4972
lemma ex_nat_less_eq [code_unfold]:
haftmann@37605
  4973
  "(\<exists>m<n\<Colon>nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..<n}. P m)"
haftmann@37605
  4974
  by auto
haftmann@37605
  4975
haftmann@37605
  4976
lemma all_nat_less [code_unfold]:
haftmann@37605
  4977
  "(\<forall>m\<le>n\<Colon>nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..n}. P m)"
haftmann@37605
  4978
  by auto
haftmann@37605
  4979
haftmann@37605
  4980
lemma ex_nat_less [code_unfold]:
haftmann@37605
  4981
  "(\<exists>m\<le>n\<Colon>nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..n}. P m)"
haftmann@37605
  4982
  by auto
haftmann@37605
  4983
haftmann@37605
  4984
lemma setsum_set_upt_conv_listsum_nat [code_unfold]:
haftmann@37605
  4985
  "setsum f (set [m..<n]) = listsum (map f [m..<n])"
haftmann@37605
  4986
  by (simp add: interv_listsum_conv_setsum_set_nat)
haftmann@37605
  4987
haftmann@37605
  4988
text {* Summation over ints. *}
haftmann@37605
  4989
haftmann@37605
  4990
lemma greaterThanLessThan_upto [code_unfold]:
haftmann@37605
  4991
  "{i<..<j::int} = set [i+1..j - 1]"
haftmann@37605
  4992
by auto
haftmann@37605
  4993
haftmann@37605
  4994
lemma atLeastLessThan_upto [code_unfold]:
haftmann@37605
  4995
  "{i..<j::int} = set [i..j - 1]"
haftmann@37605
  4996
by auto
haftmann@37605
  4997
haftmann@37605
  4998
lemma greaterThanAtMost_upto [code_unfold]:
haftmann@37605
  4999
  "{i<..j::int} = set [i+1..j]"
haftmann@37605
  5000
by auto
haftmann@37605
  5001
haftmann@37605
  5002
lemmas atLeastAtMost_upto [code_unfold] = set_upto [symmetric]
haftmann@37605
  5003
haftmann@37605
  5004
lemma setsum_set_upto_conv_listsum_int [code_unfold]:
haftmann@37605
  5005
  "setsum f (set [i..j::int]) = listsum (map f [i..j])"
haftmann@37605
  5006
  by (simp add: interv_listsum_conv_setsum_set_int)
haftmann@37605
  5007
haftmann@37605
  5008
haftmann@37605
  5009
subsubsection {* Optimizing by rewriting *}
haftmann@37605
  5010
haftmann@37605
  5011
definition null :: "'a list \<Rightarrow> bool" where
haftmann@37605
  5012
  [code_post]: "null xs \<longleftrightarrow> xs = []"
haftmann@37605
  5013
haftmann@37605
  5014
text {*
haftmann@37605
  5015
  Efficient emptyness check is implemented by @{const null}.
haftmann@37605
  5016
*}
haftmann@37605
  5017
haftmann@37605
  5018
lemma null_rec [code]:
haftmann@37605
  5019
  "null (x # xs) \<longleftrightarrow> False"
haftmann@37605
  5020
  "null [] \<longleftrightarrow> True"
haftmann@37605
  5021
  by (simp_all add: null_def)
haftmann@37605
  5022
haftmann@37605
  5023
lemma eq_Nil_null [code_unfold]:
haftmann@37605
  5024
  "xs = [] \<longleftrightarrow> null xs"
haftmann@37605
  5025
  by (simp add: null_def)
haftmann@37605
  5026
haftmann@37605
  5027
lemma equal_Nil_null [code_unfold]:
haftmann@39086
  5028
  "HOL.equal xs [] \<longleftrightarrow> null xs"
haftmann@39086
  5029
  by (simp add: equal eq_Nil_null)
haftmann@37605
  5030
haftmann@37605
  5031
definition maps :: "('a \<Rightarrow> 'b list) \<Rightarrow> 'a list \<Rightarrow> 'b list" where
haftmann@37605
  5032
  [code_post]: "maps f xs = concat (map f xs)"
haftmann@37605
  5033
haftmann@37605
  5034
definition map_filter :: "('a \<Rightarrow> 'b option) \<Rightarrow> 'a list \<Rightarrow> 'b list" where
haftmann@37605
  5035
  [code_post]: "map_filter f xs = map (the \<circ> f) (filter (\<lambda>x. f x \<noteq> None) xs)"
haftmann@37605
  5036
haftmann@37605
  5037
text {*
haftmann@37605
  5038
  Operations @{const maps} and @{const map_filter} avoid
haftmann@37605
  5039
  intermediate lists on execution -- do not use for proving.
haftmann@37605
  5040
*}
haftmann@37605
  5041
haftmann@37605
  5042
lemma maps_simps [code]:
haftmann@37605
  5043
  "maps f (x # xs) = f x @ maps f xs"
haftmann@37605
  5044
  "maps f [] = []"
haftmann@37605
  5045
  by (simp_all add: maps_def)
haftmann@37605
  5046
haftmann@37605
  5047
lemma map_filter_simps [code]:
haftmann@37605
  5048
  "map_filter f (x # xs) = (case f x of None \<Rightarrow> map_filter f xs | Some y \<Rightarrow> y # map_filter f xs)"
haftmann@37605
  5049
  "map_filter f [] = []"
haftmann@37605
  5050
  by (simp_all add: map_filter_def split: option.split)
haftmann@37605
  5051
haftmann@37605
  5052
lemma concat_map_maps [code_unfold]:
haftmann@37605
  5053
  "concat (map f xs) = maps f xs"
haftmann@37605
  5054
  by (simp add: maps_def)
haftmann@37605
  5055
haftmann@37605
  5056
lemma map_filter_map_filter [code_unfold]:
haftmann@37605
  5057
  "map f (filter P xs) = map_filter (\<lambda>x. if P x then Some (f x) else None) xs"
haftmann@37605
  5058
  by (simp add: map_filter_def)
haftmann@37605
  5059
haftmann@37605
  5060
text {* Optimized code for @{text"\<forall>i\<in>{a..b::int}"} and @{text"\<forall>n:{a..<b::nat}"}
haftmann@37605
  5061
and similiarly for @{text"\<exists>"}. *}
haftmann@37605
  5062
haftmann@37605
  5063
definition all_interval_nat :: "(nat \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where
haftmann@37605
  5064
  "all_interval_nat P i j \<longleftrightarrow> (\<forall>n \<in> {i..<j}. P n)"
haftmann@37605
  5065
haftmann@37605
  5066
lemma [code]:
haftmann@37605
  5067
  "all_interval_nat P i j \<longleftrightarrow> i \<ge> j \<or> P i \<and> all_interval_nat P (Suc i) j"
haftmann@37605
  5068
proof -
haftmann@37605
  5069
  have *: "\<And>n. P i \<Longrightarrow> \<forall>n\<in>{Suc i..<j}. P n \<Longrightarrow> i \<le> n \<Longrightarrow> n < j \<Longrightarrow> P n"
haftmann@37605
  5070
  proof -
haftmann@37605
  5071
    fix n
haftmann@37605
  5072
    assume "P i" "\<forall>n\<in>{Suc i..<j}. P n" "i \<le> n" "n < j"
haftmann@37605
  5073
    then show "P n" by (cases "n = i") simp_all
haftmann@37605
  5074
  qed
haftmann@37605
  5075
  show ?thesis by (auto simp add: all_interval_nat_def intro: *)
haftmann@37605
  5076
qed
haftmann@37605
  5077
haftmann@37605
  5078
lemma list_all_iff_all_interval_nat [code_unfold]:
haftmann@37605
  5079
  "list_all P [i..<j] \<longleftrightarrow> all_interval_nat P i j"
haftmann@37605
  5080
  by (simp add: list_all_iff all_interval_nat_def)
haftmann@37605
  5081
haftmann@37605
  5082
lemma list_ex_iff_not_all_inverval_nat [code_unfold]:
haftmann@37605
  5083
  "list_ex P [i..<j] \<longleftrightarrow> \<not> (all_interval_nat (Not \<circ> P) i j)"
haftmann@37605
  5084
  by (simp add: list_ex_iff all_interval_nat_def)
haftmann@37605
  5085
haftmann@37605
  5086
definition all_interval_int :: "(int \<Rightarrow> bool) \<Rightarrow> int \<Rightarrow> int \<Rightarrow> bool" where
haftmann@37605
  5087
  "all_interval_int P i j \<longleftrightarrow> (\<forall>k \<in> {i..j}. P k)"
haftmann@37605
  5088
haftmann@37605
  5089
lemma [code]:
haftmann@37605
  5090
  "all_interval_int P i j \<longleftrightarrow> i > j \<or> P i \<and> all_interval_int P (i + 1) j"
haftmann@37605
  5091
proof -
haftmann@37605
  5092
  have *: "\<And>k. P i \<Longrightarrow> \<forall>k\<in>{i+1..j}. P k \<Longrightarrow> i \<le> k \<Longrightarrow> k \<le> j \<Longrightarrow> P k"
haftmann@37605
  5093
  proof -
haftmann@37605
  5094
    fix k
haftmann@37605
  5095
    assume "P i" "\<forall>k\<in>{i+1..j}. P k" "i \<le> k" "k \<le> j"
haftmann@37605
  5096
    then show "P k" by (cases "k = i") simp_all
haftmann@37605
  5097
  qed
haftmann@37605
  5098
  show ?thesis by (auto simp add: all_interval_int_def intro: *)
haftmann@37605
  5099
qed
haftmann@37605
  5100
haftmann@37605
  5101
lemma list_all_iff_all_interval_int [code_unfold]:
haftmann@37605
  5102
  "list_all P [i..j] \<longleftrightarrow> all_interval_int P i j"
haftmann@37605
  5103
  by (simp add: list_all_iff all_interval_int_def)
haftmann@37605
  5104
haftmann@37605
  5105
lemma list_ex_iff_not_all_inverval_int [code_unfold]:
haftmann@37605
  5106
  "list_ex P [i..j] \<longleftrightarrow> \<not> (all_interval_int (Not \<circ> P) i j)"
haftmann@37605
  5107
  by (simp add: list_ex_iff all_interval_int_def)
haftmann@37605
  5108
haftmann@37605
  5109
hide_const (open) member null maps map_filter all_interval_nat all_interval_int
haftmann@37605
  5110
haftmann@37605
  5111
haftmann@37605
  5112
subsubsection {* Pretty lists *}
berghofe@15064
  5113
haftmann@31055
  5114
use "Tools/list_code.ML"
haftmann@31055
  5115
haftmann@31048
  5116
code_type list
haftmann@31048
  5117
  (SML "_ list")
haftmann@31048
  5118
  (OCaml "_ list")
haftmann@34886
  5119
  (Haskell "![(_)]")
haftmann@34886
  5120
  (Scala "List[(_)]")
haftmann@31048
  5121
haftmann@31048
  5122
code_const Nil
haftmann@31048
  5123
  (SML "[]")
haftmann@31048
  5124
  (OCaml "[]")
haftmann@31048
  5125
  (Haskell "[]")
haftmann@37853
  5126
  (Scala "!Nil")
haftmann@31048
  5127
haftmann@39086
  5128
code_instance list :: equal
haftmann@31048
  5129
  (Haskell -)
haftmann@31048
  5130
haftmann@39086
  5131
code_const "HOL.equal \<Colon> 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
haftmann@39499
  5132
  (Haskell infix 4 "==")
haftmann@31048
  5133
haftmann@31048
  5134
code_reserved SML
haftmann@31048
  5135
  list
haftmann@31048
  5136
haftmann@31048
  5137
code_reserved OCaml
haftmann@31048
  5138
  list
haftmann@31048
  5139
berghofe@16770
  5140
types_code
berghofe@16770
  5141
  "list" ("_ list")
berghofe@16770
  5142
attach (term_of) {*
wenzelm@21760
  5143
fun term_of_list f T = HOLogic.mk_list T o map f;
berghofe@16770
  5144
*}
berghofe@16770
  5145
attach (test) {*
berghofe@25885
  5146
fun gen_list' aG aT i j = frequency
berghofe@25885
  5147
  [(i, fn () =>
berghofe@25885
  5148
      let
berghofe@25885
  5149
        val (x, t) = aG j;
berghofe@25885
  5150
        val (xs, ts) = gen_list' aG aT (i-1) j
berghofe@25885
  5151
      in (x :: xs, fn () => HOLogic.cons_const aT $ t () $ ts ()) end),
berghofe@25885
  5152
   (1, fn () => ([], fn () => HOLogic.nil_const aT))] ()
berghofe@25885
  5153
and gen_list aG aT i = gen_list' aG aT i i;
berghofe@16770
  5154
*}
haftmann@31048
  5155
haftmann@31048
  5156
consts_code Cons ("(_ ::/ _)")
haftmann@20588
  5157
haftmann@20453
  5158
setup {*
haftmann@20453
  5159
let
haftmann@31055
  5160
  fun list_codegen thy defs dep thyname b t gr =
haftmann@31055
  5161
    let
haftmann@31055
  5162
      val ts = HOLogic.dest_list t;
haftmann@31055
  5163
      val (_, gr') = Codegen.invoke_tycodegen thy defs dep thyname false
haftmann@31055
  5164
        (fastype_of t) gr;
haftmann@31055
  5165
      val (ps, gr'') = fold_map
haftmann@31055
  5166
        (Codegen.invoke_codegen thy defs dep thyname false) ts gr'
haftmann@31055
  5167
    in SOME (Pretty.list "[" "]" ps, gr'') end handle TERM _ => NONE;
haftmann@31055
  5168
in
haftmann@34886
  5169
  fold (List_Code.add_literal_list) ["SML", "OCaml", "Haskell", "Scala"]
haftmann@31055
  5170
  #> Codegen.add_codegen "list_codegen" list_codegen
haftmann@31055
  5171
end
haftmann@20453
  5172
*}
berghofe@15064
  5173
haftmann@21061
  5174
haftmann@37399
  5175
subsubsection {* Use convenient predefined operations *}
haftmann@37399
  5176
haftmann@37399
  5177
code_const "op @"
haftmann@37399
  5178
  (SML infixr 7 "@")
haftmann@37399
  5179
  (OCaml infixr 6 "@")
haftmann@37399
  5180
  (Haskell infixr 5 "++")
haftmann@37399
  5181
  (Scala infixl 7 "++")
haftmann@37399
  5182
haftmann@37399
  5183
code_const map
haftmann@37399
  5184
  (Haskell "map")
haftmann@37399
  5185
haftmann@37399
  5186
code_const filter
haftmann@37399
  5187
  (Haskell "filter")
haftmann@37399
  5188
haftmann@37399
  5189
code_const concat
haftmann@37399
  5190
  (Haskell "concat")
haftmann@37399
  5191
haftmann@37605
  5192
code_const List.maps
haftmann@37605
  5193
  (Haskell "concatMap")
haftmann@37605
  5194
haftmann@37399
  5195
code_const rev
haftmann@37426
  5196
  (Haskell "reverse")
haftmann@37399
  5197
haftmann@37399
  5198
code_const zip
haftmann@37399
  5199
  (Haskell "zip")
haftmann@37399
  5200
haftmann@37605
  5201
code_const List.null
haftmann@37605
  5202
  (Haskell "null")
haftmann@37605
  5203
haftmann@37399
  5204
code_const takeWhile
haftmann@37399
  5205
  (Haskell "takeWhile")
haftmann@37399
  5206
haftmann@37399
  5207
code_const dropWhile
haftmann@37399
  5208
  (Haskell "dropWhile")
haftmann@37399
  5209
haftmann@37399
  5210
code_const hd
haftmann@37399
  5211
  (Haskell "head")
haftmann@37399
  5212
haftmann@37399
  5213
code_const last
haftmann@37399
  5214
  (Haskell "last")
haftmann@37399
  5215
haftmann@37605
  5216
code_const list_all
haftmann@37605
  5217
  (Haskell "all")
haftmann@37605
  5218
haftmann@37605
  5219
code_const list_ex
haftmann@37605
  5220
  (Haskell "any")
haftmann@37605
  5221
wenzelm@23388
  5222
end