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