src/HOL/List.thy
author haftmann
Sat, 07 Feb 2009 09:57:07 +0100
changeset 29766 9acb915a62fa
parent 29764 c82b3e8a4daf
child 29793 984191be0357
permissions -rw-r--r--
code theorems for nth, list_update
wenzelm@13462
     1
(*  Title:      HOL/List.thy
wenzelm@13462
     2
    Author:     Tobias Nipkow
clasohm@923
     3
*)
clasohm@923
     4
wenzelm@13114
     5
header {* The datatype of finite lists *}
wenzelm@13122
     6
nipkow@15131
     7
theory List
haftmann@27368
     8
imports Plain Relation_Power Presburger Recdef ATP_Linkup
wenzelm@21754
     9
uses "Tools/string_syntax.ML"
nipkow@15131
    10
begin
clasohm@923
    11
wenzelm@13142
    12
datatype 'a list =
wenzelm@13366
    13
    Nil    ("[]")
wenzelm@13366
    14
  | Cons 'a  "'a list"    (infixr "#" 65)
clasohm@923
    15
nipkow@15392
    16
subsection{*Basic list processing functions*}
nipkow@15302
    17
clasohm@923
    18
consts
wenzelm@13366
    19
  filter:: "('a => bool) => 'a list => 'a list"
wenzelm@13366
    20
  concat:: "'a list list => 'a list"
wenzelm@13366
    21
  foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b"
wenzelm@13366
    22
  foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b"
wenzelm@13366
    23
  hd:: "'a list => 'a"
wenzelm@13366
    24
  tl:: "'a list => 'a list"
wenzelm@13366
    25
  last:: "'a list => 'a"
wenzelm@13366
    26
  butlast :: "'a list => 'a list"
wenzelm@13366
    27
  set :: "'a list => 'a set"
wenzelm@13366
    28
  map :: "('a=>'b) => ('a list => 'b list)"
nipkow@23096
    29
  listsum ::  "'a list => 'a::monoid_add"
wenzelm@13366
    30
  list_update :: "'a list => nat => 'a => 'a list"
wenzelm@13366
    31
  take:: "nat => 'a list => 'a list"
wenzelm@13366
    32
  drop:: "nat => 'a list => 'a list"
wenzelm@13366
    33
  takeWhile :: "('a => bool) => 'a list => 'a list"
wenzelm@13366
    34
  dropWhile :: "('a => bool) => 'a list => 'a list"
wenzelm@13366
    35
  rev :: "'a list => 'a list"
wenzelm@13366
    36
  zip :: "'a list => 'b list => ('a * 'b) list"
nipkow@15425
    37
  upt :: "nat => nat => nat list" ("(1[_..</_'])")
wenzelm@13366
    38
  remdups :: "'a list => 'a list"
nipkow@15110
    39
  remove1 :: "'a => 'a list => 'a list"
nipkow@27693
    40
  removeAll :: "'a => 'a list => 'a list"
wenzelm@13366
    41
  "distinct":: "'a list => bool"
wenzelm@13366
    42
  replicate :: "nat => 'a => 'a list"
nipkow@19390
    43
  splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
nipkow@15302
    44
clasohm@923
    45
nipkow@13146
    46
nonterminals lupdbinds lupdbind
nipkow@5077
    47
clasohm@923
    48
syntax
wenzelm@13366
    49
  -- {* list Enumeration *}
wenzelm@13366
    50
  "@list" :: "args => 'a list"    ("[(_)]")
clasohm@923
    51
wenzelm@13366
    52
  -- {* Special syntax for filter *}
nipkow@23279
    53
  "@filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_<-_./ _])")
clasohm@923
    54
wenzelm@13366
    55
  -- {* list update *}
wenzelm@13366
    56
  "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")
wenzelm@13366
    57
  "" :: "lupdbind => lupdbinds"    ("_")
wenzelm@13366
    58
  "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")
wenzelm@13366
    59
  "_LUpdate" :: "['a, lupdbinds] => 'a"    ("_/[(_)]" [900,0] 900)
nipkow@5077
    60
clasohm@923
    61
translations
wenzelm@13366
    62
  "[x, xs]" == "x#[xs]"
wenzelm@13366
    63
  "[x]" == "x#[]"
nipkow@23279
    64
  "[x<-xs . P]"== "filter (%x. P) xs"
clasohm@923
    65
wenzelm@13366
    66
  "_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs"
wenzelm@13366
    67
  "xs[i:=x]" == "list_update xs i x"
nipkow@5077
    68
nipkow@5427
    69
wenzelm@12114
    70
syntax (xsymbols)
nipkow@23279
    71
  "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
kleing@14565
    72
syntax (HTML output)
nipkow@23279
    73
  "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
wenzelm@2262
    74
wenzelm@2262
    75
wenzelm@13142
    76
text {*
wenzelm@14589
    77
  Function @{text size} is overloaded for all datatypes. Users may
wenzelm@13366
    78
  refer to the list version as @{text length}. *}
paulson@3342
    79
wenzelm@19363
    80
abbreviation
wenzelm@21404
    81
  length :: "'a list => nat" where
wenzelm@19363
    82
  "length == size"
nipkow@15302
    83
berghofe@5183
    84
primrec
paulson@15307
    85
  "hd(x#xs) = x"
paulson@15307
    86
berghofe@5183
    87
primrec
paulson@15307
    88
  "tl([]) = []"
paulson@15307
    89
  "tl(x#xs) = xs"
paulson@15307
    90
berghofe@5183
    91
primrec
paulson@15307
    92
  "last(x#xs) = (if xs=[] then x else last xs)"
paulson@15307
    93
berghofe@5183
    94
primrec
paulson@15307
    95
  "butlast []= []"
paulson@15307
    96
  "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
paulson@15307
    97
berghofe@5183
    98
primrec
paulson@15307
    99
  "set [] = {}"
paulson@15307
   100
  "set (x#xs) = insert x (set xs)"
paulson@15307
   101
berghofe@5183
   102
primrec
paulson@15307
   103
  "map f [] = []"
paulson@15307
   104
  "map f (x#xs) = f(x)#map f xs"
paulson@15307
   105
wenzelm@25221
   106
primrec
haftmann@25559
   107
  append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65)
haftmann@25559
   108
where
haftmann@25559
   109
  append_Nil:"[] @ ys = ys"
haftmann@25559
   110
  | append_Cons: "(x#xs) @ ys = x # xs @ ys"
paulson@15307
   111
berghofe@5183
   112
primrec
paulson@15307
   113
  "rev([]) = []"
paulson@15307
   114
  "rev(x#xs) = rev(xs) @ [x]"
paulson@15307
   115
berghofe@5183
   116
primrec
paulson@15307
   117
  "filter P [] = []"
paulson@15307
   118
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
paulson@15307
   119
berghofe@5183
   120
primrec
paulson@15307
   121
  foldl_Nil:"foldl f a [] = a"
paulson@15307
   122
  foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs"
paulson@15307
   123
berghofe@5183
   124
primrec
paulson@15307
   125
  "foldr f [] a = a"
paulson@15307
   126
  "foldr f (x#xs) a = f x (foldr f xs a)"
paulson@15307
   127
paulson@8000
   128
primrec
paulson@15307
   129
  "concat([]) = []"
paulson@15307
   130
  "concat(x#xs) = x @ concat(xs)"
paulson@15307
   131
berghofe@5183
   132
primrec
nipkow@23096
   133
"listsum [] = 0"
nipkow@23096
   134
"listsum (x # xs) = x + listsum xs"
nipkow@23096
   135
nipkow@23096
   136
primrec
paulson@15307
   137
  drop_Nil:"drop n [] = []"
paulson@15307
   138
  drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
paulson@15307
   139
  -- {*Warning: simpset does not contain this definition, but separate
paulson@15307
   140
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
paulson@15307
   141
berghofe@5183
   142
primrec
paulson@15307
   143
  take_Nil:"take n [] = []"
paulson@15307
   144
  take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
paulson@15307
   145
  -- {*Warning: simpset does not contain this definition, but separate
paulson@15307
   146
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
paulson@15307
   147
haftmann@29759
   148
primrec nth :: "'a list => nat => 'a" (infixl "!" 100) where
haftmann@29759
   149
  nth_Cons: "(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
paulson@15307
   150
  -- {*Warning: simpset does not contain this definition, but separate
paulson@15307
   151
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
paulson@15307
   152
wenzelm@13142
   153
primrec
paulson@15307
   154
  "[][i:=v] = []"
paulson@15307
   155
  "(x#xs)[i:=v] = (case i of 0 => v # xs | Suc j => x # xs[j:=v])"
paulson@15307
   156
berghofe@5183
   157
primrec
paulson@15307
   158
  "takeWhile P [] = []"
paulson@15307
   159
  "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
paulson@15307
   160
berghofe@5183
   161
primrec
paulson@15307
   162
  "dropWhile P [] = []"
paulson@15307
   163
  "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
paulson@15307
   164
berghofe@5183
   165
primrec
paulson@15307
   166
  "zip xs [] = []"
paulson@15307
   167
  zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"
paulson@15307
   168
  -- {*Warning: simpset does not contain this definition, but separate
paulson@15307
   169
       theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
paulson@15307
   170
nipkow@5427
   171
primrec
nipkow@15425
   172
  upt_0: "[i..<0] = []"
nipkow@15425
   173
  upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])"
paulson@15307
   174
berghofe@5183
   175
primrec
paulson@15307
   176
  "distinct [] = True"
paulson@15307
   177
  "distinct (x#xs) = (x ~: set xs \<and> distinct xs)"
paulson@15307
   178
berghofe@5183
   179
primrec
paulson@15307
   180
  "remdups [] = []"
paulson@15307
   181
  "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
paulson@15307
   182
berghofe@5183
   183
primrec
paulson@15307
   184
  "remove1 x [] = []"
paulson@15307
   185
  "remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)"
paulson@15307
   186
nipkow@15110
   187
primrec
nipkow@27693
   188
  "removeAll x [] = []"
nipkow@27693
   189
  "removeAll x (y#xs) = (if x=y then removeAll x xs else y # removeAll x xs)"
nipkow@27693
   190
nipkow@27693
   191
primrec
paulson@15307
   192
  replicate_0: "replicate 0 x = []"
paulson@15307
   193
  replicate_Suc: "replicate (Suc n) x = x # replicate n x"
paulson@15307
   194
haftmann@21061
   195
definition
wenzelm@21404
   196
  rotate1 :: "'a list \<Rightarrow> 'a list" where
wenzelm@21404
   197
  "rotate1 xs = (case xs of [] \<Rightarrow> [] | x#xs \<Rightarrow> xs @ [x])"
wenzelm@21404
   198
wenzelm@21404
   199
definition
wenzelm@21404
   200
  rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
wenzelm@21404
   201
  "rotate n = rotate1 ^ n"
wenzelm@21404
   202
wenzelm@21404
   203
definition
wenzelm@21404
   204
  list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" where
haftmann@28562
   205
  [code del]: "list_all2 P xs ys =
haftmann@21061
   206
    (length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y))"
wenzelm@21404
   207
wenzelm@21404
   208
definition
wenzelm@21404
   209
  sublist :: "'a list => nat set => 'a list" where
wenzelm@21404
   210
  "sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))"
nipkow@5281
   211
nipkow@17086
   212
primrec
haftmann@21061
   213
  "splice [] ys = ys"
haftmann@21061
   214
  "splice (x#xs) ys = (if ys=[] then x#xs else x # hd ys # splice xs (tl ys))"
haftmann@21061
   215
    -- {*Warning: simpset does not contain the second eqn but a derived one. *}
haftmann@21061
   216
nipkow@26771
   217
text{*
nipkow@26771
   218
\begin{figure}[htbp]
nipkow@26771
   219
\fbox{
nipkow@26771
   220
\begin{tabular}{l}
wenzelm@27381
   221
@{lemma "[a,b]@[c,d] = [a,b,c,d]" by simp}\\
wenzelm@27381
   222
@{lemma "length [a,b,c] = 3" by simp}\\
wenzelm@27381
   223
@{lemma "set [a,b,c] = {a,b,c}" by simp}\\
wenzelm@27381
   224
@{lemma "map f [a,b,c] = [f a, f b, f c]" by simp}\\
wenzelm@27381
   225
@{lemma "rev [a,b,c] = [c,b,a]" by simp}\\
wenzelm@27381
   226
@{lemma "hd [a,b,c,d] = a" by simp}\\
wenzelm@27381
   227
@{lemma "tl [a,b,c,d] = [b,c,d]" by simp}\\
wenzelm@27381
   228
@{lemma "last [a,b,c,d] = d" by simp}\\
wenzelm@27381
   229
@{lemma "butlast [a,b,c,d] = [a,b,c]" by simp}\\
wenzelm@27381
   230
@{lemma[source] "filter (\<lambda>n::nat. n<2) [0,2,1] = [0,1]" by simp}\\
wenzelm@27381
   231
@{lemma "concat [[a,b],[c,d,e],[],[f]] = [a,b,c,d,e,f]" by simp}\\
wenzelm@27381
   232
@{lemma "foldl f x [a,b,c] = f (f (f x a) b) c" by simp}\\
wenzelm@27381
   233
@{lemma "foldr f [a,b,c] x = f a (f b (f c x))" by simp}\\
wenzelm@27381
   234
@{lemma "zip [a,b,c] [x,y,z] = [(a,x),(b,y),(c,z)]" by simp}\\
wenzelm@27381
   235
@{lemma "zip [a,b] [x,y,z] = [(a,x),(b,y)]" by simp}\\
wenzelm@27381
   236
@{lemma "splice [a,b,c] [x,y,z] = [a,x,b,y,c,z]" by simp}\\
wenzelm@27381
   237
@{lemma "splice [a,b,c,d] [x,y] = [a,x,b,y,c,d]" by simp}\\
wenzelm@27381
   238
@{lemma "take 2 [a,b,c,d] = [a,b]" by simp}\\
wenzelm@27381
   239
@{lemma "take 6 [a,b,c,d] = [a,b,c,d]" by simp}\\
wenzelm@27381
   240
@{lemma "drop 2 [a,b,c,d] = [c,d]" by simp}\\
wenzelm@27381
   241
@{lemma "drop 6 [a,b,c,d] = []" by simp}\\
wenzelm@27381
   242
@{lemma "takeWhile (%n::nat. n<3) [1,2,3,0] = [1,2]" by simp}\\
wenzelm@27381
   243
@{lemma "dropWhile (%n::nat. n<3) [1,2,3,0] = [3,0]" by simp}\\
wenzelm@27381
   244
@{lemma "distinct [2,0,1::nat]" by simp}\\
wenzelm@27381
   245
@{lemma "remdups [2,0,2,1::nat,2] = [0,1,2]" by simp}\\
wenzelm@27381
   246
@{lemma "remove1 2 [2,0,2,1::nat,2] = [0,2,1,2]" by simp}\\
nipkow@27693
   247
@{lemma "removeAll 2 [2,0,2,1::nat,2] = [0,1]" by simp}\\
wenzelm@27381
   248
@{lemma "nth [a,b,c,d] 2 = c" by simp}\\
wenzelm@27381
   249
@{lemma "[a,b,c,d][2 := x] = [a,b,x,d]" by simp}\\
wenzelm@27381
   250
@{lemma "sublist [a,b,c,d,e] {0,2,3} = [a,c,d]" by (simp add:sublist_def)}\\
wenzelm@27381
   251
@{lemma "rotate1 [a,b,c,d] = [b,c,d,a]" by (simp add:rotate1_def)}\\
wenzelm@27381
   252
@{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate1_def rotate_def nat_number)}\\
wenzelm@27381
   253
@{lemma "replicate 4 a = [a,a,a,a]" by (simp add:nat_number)}\\
wenzelm@27381
   254
@{lemma "[2..<5] = [2,3,4]" by (simp add:nat_number)}\\
wenzelm@27381
   255
@{lemma "listsum [1,2,3::nat] = 6" by simp}
nipkow@26771
   256
\end{tabular}}
nipkow@26771
   257
\caption{Characteristic examples}
nipkow@26771
   258
\label{fig:Characteristic}
nipkow@26771
   259
\end{figure}
nipkow@26771
   260
Figure~\ref{fig:Characteristic} shows charachteristic examples
nipkow@26771
   261
that should give an intuitive understanding of the above functions.
nipkow@26771
   262
*}
nipkow@26771
   263
nipkow@24616
   264
text{* The following simple sort functions are intended for proofs,
nipkow@24616
   265
not for efficient implementations. *}
nipkow@24616
   266
wenzelm@25221
   267
context linorder
wenzelm@25221
   268
begin
wenzelm@25221
   269
wenzelm@25221
   270
fun sorted :: "'a list \<Rightarrow> bool" where
nipkow@24697
   271
"sorted [] \<longleftrightarrow> True" |
nipkow@24697
   272
"sorted [x] \<longleftrightarrow> True" |
haftmann@25062
   273
"sorted (x#y#zs) \<longleftrightarrow> x <= y \<and> sorted (y#zs)"
nipkow@24697
   274
haftmann@25559
   275
primrec insort :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@24697
   276
"insort x [] = [x]" |
haftmann@25062
   277
"insort x (y#ys) = (if x <= y then (x#y#ys) else y#(insort x ys))"
nipkow@24697
   278
haftmann@25559
   279
primrec sort :: "'a list \<Rightarrow> 'a list" where
nipkow@24697
   280
"sort [] = []" |
nipkow@24697
   281
"sort (x#xs) = insort x (sort xs)"
nipkow@24616
   282
wenzelm@25221
   283
end
wenzelm@25221
   284
nipkow@24616
   285
wenzelm@23388
   286
subsubsection {* List comprehension *}
nipkow@23192
   287
nipkow@24349
   288
text{* Input syntax for Haskell-like list comprehension notation.
nipkow@24349
   289
Typical example: @{text"[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]"},
nipkow@24349
   290
the list of all pairs of distinct elements from @{text xs} and @{text ys}.
nipkow@24349
   291
The syntax is as in Haskell, except that @{text"|"} becomes a dot
nipkow@24349
   292
(like in Isabelle's set comprehension): @{text"[e. x \<leftarrow> xs, \<dots>]"} rather than
nipkow@24349
   293
\verb![e| x <- xs, ...]!.
nipkow@24349
   294
nipkow@24349
   295
The qualifiers after the dot are
nipkow@24349
   296
\begin{description}
nipkow@24349
   297
\item[generators] @{text"p \<leftarrow> xs"},
nipkow@24476
   298
 where @{text p} is a pattern and @{text xs} an expression of list type, or
nipkow@24476
   299
\item[guards] @{text"b"}, where @{text b} is a boolean expression.
nipkow@24476
   300
%\item[local bindings] @ {text"let x = e"}.
nipkow@24349
   301
\end{description}
nipkow@23240
   302
nipkow@24476
   303
Just like in Haskell, list comprehension is just a shorthand. To avoid
nipkow@24476
   304
misunderstandings, the translation into desugared form is not reversed
nipkow@24476
   305
upon output. Note that the translation of @{text"[e. x \<leftarrow> xs]"} is
nipkow@24476
   306
optmized to @{term"map (%x. e) xs"}.
nipkow@23240
   307
nipkow@24349
   308
It is easy to write short list comprehensions which stand for complex
nipkow@24349
   309
expressions. During proofs, they may become unreadable (and
nipkow@24349
   310
mangled). In such cases it can be advisable to introduce separate
nipkow@24349
   311
definitions for the list comprehensions in question.  *}
nipkow@24349
   312
nipkow@23209
   313
(*
nipkow@23240
   314
Proper theorem proving support would be nice. For example, if
nipkow@23192
   315
@{text"set[f x y. x \<leftarrow> xs, y \<leftarrow> ys, P x y]"}
nipkow@23192
   316
produced something like
nipkow@23209
   317
@{term"{z. EX x: set xs. EX y:set ys. P x y \<and> z = f x y}"}.
nipkow@23209
   318
*)
nipkow@23209
   319
nipkow@23240
   320
nonterminals lc_qual lc_quals
nipkow@23192
   321
nipkow@23192
   322
syntax
nipkow@23240
   323
"_listcompr" :: "'a \<Rightarrow> lc_qual \<Rightarrow> lc_quals \<Rightarrow> 'a list"  ("[_ . __")
nipkow@24349
   324
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ <- _")
nipkow@23240
   325
"_lc_test" :: "bool \<Rightarrow> lc_qual" ("_")
nipkow@24476
   326
(*"_lc_let" :: "letbinds => lc_qual"  ("let _")*)
nipkow@23240
   327
"_lc_end" :: "lc_quals" ("]")
nipkow@23240
   328
"_lc_quals" :: "lc_qual \<Rightarrow> lc_quals \<Rightarrow> lc_quals" (", __")
nipkow@24349
   329
"_lc_abs" :: "'a => 'b list => 'b list"
nipkow@23192
   330
nipkow@24476
   331
(* These are easier than ML code but cannot express the optimized
nipkow@24476
   332
   translation of [e. p<-xs]
nipkow@23192
   333
translations
nipkow@24349
   334
"[e. p<-xs]" => "concat(map (_lc_abs p [e]) xs)"
nipkow@23240
   335
"_listcompr e (_lc_gen p xs) (_lc_quals Q Qs)"
nipkow@24349
   336
 => "concat (map (_lc_abs p (_listcompr e Q Qs)) xs)"
nipkow@23240
   337
"[e. P]" => "if P then [e] else []"
nipkow@23240
   338
"_listcompr e (_lc_test P) (_lc_quals Q Qs)"
nipkow@23240
   339
 => "if P then (_listcompr e Q Qs) else []"
nipkow@24349
   340
"_listcompr e (_lc_let b) (_lc_quals Q Qs)"
nipkow@24349
   341
 => "_Let b (_listcompr e Q Qs)"
nipkow@24476
   342
*)
nipkow@23240
   343
nipkow@23279
   344
syntax (xsymbols)
nipkow@24349
   345
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _")
nipkow@23279
   346
syntax (HTML output)
nipkow@24349
   347
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _")
nipkow@24349
   348
nipkow@24349
   349
parse_translation (advanced) {*
nipkow@24349
   350
let
nipkow@24476
   351
  val NilC = Syntax.const @{const_name Nil};
nipkow@24476
   352
  val ConsC = Syntax.const @{const_name Cons};
nipkow@24476
   353
  val mapC = Syntax.const @{const_name map};
nipkow@24476
   354
  val concatC = Syntax.const @{const_name concat};
nipkow@24476
   355
  val IfC = Syntax.const @{const_name If};
nipkow@24476
   356
  fun singl x = ConsC $ x $ NilC;
nipkow@24476
   357
nipkow@24476
   358
   fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *)
nipkow@24349
   359
    let
wenzelm@29281
   360
      val x = Free (Name.variant (fold Term.add_free_names [p, e] []) "x", dummyT);
nipkow@24476
   361
      val e = if opti then singl e else e;
nipkow@24476
   362
      val case1 = Syntax.const "_case1" $ p $ e;
nipkow@24349
   363
      val case2 = Syntax.const "_case1" $ Syntax.const Term.dummy_patternN
nipkow@24476
   364
                                        $ NilC;
nipkow@24349
   365
      val cs = Syntax.const "_case2" $ case1 $ case2
nipkow@24349
   366
      val ft = DatatypeCase.case_tr false DatatypePackage.datatype_of_constr
nipkow@24349
   367
                 ctxt [x, cs]
nipkow@24349
   368
    in lambda x ft end;
nipkow@24349
   369
nipkow@24476
   370
  fun abs_tr ctxt (p as Free(s,T)) e opti =
nipkow@24349
   371
        let val thy = ProofContext.theory_of ctxt;
nipkow@24349
   372
            val s' = Sign.intern_const thy s
nipkow@24476
   373
        in if Sign.declared_const thy s'
nipkow@24476
   374
           then (pat_tr ctxt p e opti, false)
nipkow@24476
   375
           else (lambda p e, true)
nipkow@24349
   376
        end
nipkow@24476
   377
    | abs_tr ctxt p e opti = (pat_tr ctxt p e opti, false);
nipkow@24476
   378
nipkow@24476
   379
  fun lc_tr ctxt [e, Const("_lc_test",_)$b, qs] =
nipkow@24476
   380
        let val res = case qs of Const("_lc_end",_) => singl e
nipkow@24476
   381
                      | Const("_lc_quals",_)$q$qs => lc_tr ctxt [e,q,qs];
nipkow@24476
   382
        in IfC $ b $ res $ NilC end
nipkow@24476
   383
    | lc_tr ctxt [e, Const("_lc_gen",_) $ p $ es, Const("_lc_end",_)] =
nipkow@24476
   384
        (case abs_tr ctxt p e true of
nipkow@24476
   385
           (f,true) => mapC $ f $ es
nipkow@24476
   386
         | (f, false) => concatC $ (mapC $ f $ es))
nipkow@24476
   387
    | lc_tr ctxt [e, Const("_lc_gen",_) $ p $ es, Const("_lc_quals",_)$q$qs] =
nipkow@24476
   388
        let val e' = lc_tr ctxt [e,q,qs];
nipkow@24476
   389
        in concatC $ (mapC $ (fst(abs_tr ctxt p e' false)) $ es) end
nipkow@24476
   390
nipkow@24476
   391
in [("_listcompr", lc_tr)] end
nipkow@24349
   392
*}
nipkow@23279
   393
nipkow@23240
   394
(*
nipkow@23240
   395
term "[(x,y,z). b]"
nipkow@24476
   396
term "[(x,y,z). x\<leftarrow>xs]"
nipkow@24476
   397
term "[e x y. x\<leftarrow>xs, y\<leftarrow>ys]"
nipkow@24476
   398
term "[(x,y,z). x<a, x>b]"
nipkow@24476
   399
term "[(x,y,z). x\<leftarrow>xs, x>b]"
nipkow@24476
   400
term "[(x,y,z). x<a, x\<leftarrow>xs]"
nipkow@24349
   401
term "[(x,y). Cons True x \<leftarrow> xs]"
nipkow@24349
   402
term "[(x,y,z). Cons x [] \<leftarrow> xs]"
nipkow@23240
   403
term "[(x,y,z). x<a, x>b, x=d]"
nipkow@23240
   404
term "[(x,y,z). x<a, x>b, y\<leftarrow>ys]"
nipkow@23240
   405
term "[(x,y,z). x<a, x\<leftarrow>xs,y>b]"
nipkow@23240
   406
term "[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]"
nipkow@23240
   407
term "[(x,y,z). x\<leftarrow>xs, x>b, y<a]"
nipkow@23240
   408
term "[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]"
nipkow@23240
   409
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x]"
nipkow@23240
   410
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]"
nipkow@24349
   411
term "[(x,y). x\<leftarrow>xs, let xx = x+x, y\<leftarrow>ys, y \<noteq> xx]"
nipkow@23192
   412
*)
nipkow@23192
   413
haftmann@21061
   414
subsubsection {* @{const Nil} and @{const Cons} *}
haftmann@21061
   415
haftmann@21061
   416
lemma not_Cons_self [simp]:
haftmann@21061
   417
  "xs \<noteq> x # xs"
nipkow@13145
   418
by (induct xs) auto
nipkow@3507
   419
wenzelm@13142
   420
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric]
wenzelm@13114
   421
wenzelm@13142
   422
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
nipkow@13145
   423
by (induct xs) auto
wenzelm@13114
   424
wenzelm@13142
   425
lemma length_induct:
haftmann@21061
   426
  "(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs"
nipkow@17589
   427
by (rule measure_induct [of length]) iprover
wenzelm@13114
   428
wenzelm@13114
   429
haftmann@21061
   430
subsubsection {* @{const length} *}
wenzelm@13114
   431
wenzelm@13142
   432
text {*
haftmann@21061
   433
  Needs to come before @{text "@"} because of theorem @{text
haftmann@21061
   434
  append_eq_append_conv}.
wenzelm@13142
   435
*}
wenzelm@13114
   436
wenzelm@13142
   437
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
nipkow@13145
   438
by (induct xs) auto
wenzelm@13114
   439
wenzelm@13142
   440
lemma length_map [simp]: "length (map f xs) = length xs"
nipkow@13145
   441
by (induct xs) auto
wenzelm@13114
   442
wenzelm@13142
   443
lemma length_rev [simp]: "length (rev xs) = length xs"
nipkow@13145
   444
by (induct xs) auto
wenzelm@13114
   445
wenzelm@13142
   446
lemma length_tl [simp]: "length (tl xs) = length xs - 1"
nipkow@13145
   447
by (cases xs) auto
wenzelm@13142
   448
wenzelm@13142
   449
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
nipkow@13145
   450
by (induct xs) auto
wenzelm@13142
   451
wenzelm@13142
   452
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
nipkow@13145
   453
by (induct xs) auto
wenzelm@13114
   454
nipkow@23479
   455
lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0"
nipkow@23479
   456
by auto
nipkow@23479
   457
wenzelm@13114
   458
lemma length_Suc_conv:
nipkow@13145
   459
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
nipkow@13145
   460
by (induct xs) auto
wenzelm@13114
   461
nipkow@14025
   462
lemma Suc_length_conv:
nipkow@14025
   463
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
paulson@14208
   464
apply (induct xs, simp, simp)
nipkow@14025
   465
apply blast
nipkow@14025
   466
done
nipkow@14025
   467
wenzelm@25221
   468
lemma impossible_Cons: "length xs <= length ys ==> xs = x # ys = False"
wenzelm@25221
   469
  by (induct xs) auto
wenzelm@25221
   470
haftmann@26442
   471
lemma list_induct2 [consumes 1, case_names Nil Cons]:
haftmann@26442
   472
  "length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow>
haftmann@26442
   473
   (\<And>x xs y ys. length xs = length ys \<Longrightarrow> P xs ys \<Longrightarrow> P (x#xs) (y#ys))
haftmann@26442
   474
   \<Longrightarrow> P xs ys"
haftmann@26442
   475
proof (induct xs arbitrary: ys)
haftmann@26442
   476
  case Nil then show ?case by simp
haftmann@26442
   477
next
haftmann@26442
   478
  case (Cons x xs ys) then show ?case by (cases ys) simp_all
haftmann@26442
   479
qed
haftmann@26442
   480
haftmann@26442
   481
lemma list_induct3 [consumes 2, case_names Nil Cons]:
haftmann@26442
   482
  "length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P [] [] [] \<Longrightarrow>
haftmann@26442
   483
   (\<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
   484
   \<Longrightarrow> P xs ys zs"
haftmann@26442
   485
proof (induct xs arbitrary: ys zs)
haftmann@26442
   486
  case Nil then show ?case by simp
haftmann@26442
   487
next
haftmann@26442
   488
  case (Cons x xs ys zs) then show ?case by (cases ys, simp_all)
haftmann@26442
   489
    (cases zs, simp_all)
haftmann@26442
   490
qed
wenzelm@13114
   491
krauss@22493
   492
lemma list_induct2': 
krauss@22493
   493
  "\<lbrakk> P [] [];
krauss@22493
   494
  \<And>x xs. P (x#xs) [];
krauss@22493
   495
  \<And>y ys. P [] (y#ys);
krauss@22493
   496
   \<And>x xs y ys. P xs ys  \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk>
krauss@22493
   497
 \<Longrightarrow> P xs ys"
krauss@22493
   498
by (induct xs arbitrary: ys) (case_tac x, auto)+
krauss@22493
   499
nipkow@22143
   500
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False"
nipkow@24349
   501
by (rule Eq_FalseI) auto
wenzelm@24037
   502
wenzelm@24037
   503
simproc_setup list_neq ("(xs::'a list) = ys") = {*
nipkow@22143
   504
(*
nipkow@22143
   505
Reduces xs=ys to False if xs and ys cannot be of the same length.
nipkow@22143
   506
This is the case if the atomic sublists of one are a submultiset
nipkow@22143
   507
of those of the other list and there are fewer Cons's in one than the other.
nipkow@22143
   508
*)
wenzelm@24037
   509
wenzelm@24037
   510
let
nipkow@22143
   511
nipkow@22143
   512
fun len (Const("List.list.Nil",_)) acc = acc
nipkow@22143
   513
  | len (Const("List.list.Cons",_) $ _ $ xs) (ts,n) = len xs (ts,n+1)
haftmann@23029
   514
  | len (Const("List.append",_) $ xs $ ys) acc = len xs (len ys acc)
nipkow@22143
   515
  | len (Const("List.rev",_) $ xs) acc = len xs acc
nipkow@22143
   516
  | len (Const("List.map",_) $ _ $ xs) acc = len xs acc
nipkow@22143
   517
  | len t (ts,n) = (t::ts,n);
nipkow@22143
   518
wenzelm@24037
   519
fun list_neq _ ss ct =
nipkow@22143
   520
  let
wenzelm@24037
   521
    val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct;
nipkow@22143
   522
    val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0);
nipkow@22143
   523
    fun prove_neq() =
nipkow@22143
   524
      let
nipkow@22143
   525
        val Type(_,listT::_) = eqT;
haftmann@22994
   526
        val size = HOLogic.size_const listT;
nipkow@22143
   527
        val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs);
nipkow@22143
   528
        val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len);
nipkow@22143
   529
        val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len
haftmann@22633
   530
          (K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1));
haftmann@22633
   531
      in SOME (thm RS @{thm neq_if_length_neq}) end
nipkow@22143
   532
  in
wenzelm@23214
   533
    if m < n andalso submultiset (op aconv) (ls,rs) orelse
wenzelm@23214
   534
       n < m andalso submultiset (op aconv) (rs,ls)
nipkow@22143
   535
    then prove_neq() else NONE
nipkow@22143
   536
  end;
wenzelm@24037
   537
in list_neq end;
nipkow@22143
   538
*}
nipkow@22143
   539
nipkow@22143
   540
nipkow@15392
   541
subsubsection {* @{text "@"} -- append *}
wenzelm@13114
   542
wenzelm@13142
   543
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
nipkow@13145
   544
by (induct xs) auto
wenzelm@13114
   545
wenzelm@13142
   546
lemma append_Nil2 [simp]: "xs @ [] = xs"
nipkow@13145
   547
by (induct xs) auto
wenzelm@13114
   548
wenzelm@13142
   549
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
nipkow@13145
   550
by (induct xs) auto
wenzelm@13114
   551
wenzelm@13142
   552
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
nipkow@13145
   553
by (induct xs) auto
wenzelm@13114
   554
wenzelm@13142
   555
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
nipkow@13145
   556
by (induct xs) auto
wenzelm@13114
   557
wenzelm@13142
   558
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
nipkow@13145
   559
by (induct xs) auto
wenzelm@13114
   560
wenzelm@25221
   561
lemma append_eq_append_conv [simp, noatp]:
nipkow@24526
   562
 "length xs = length ys \<or> length us = length vs
berghofe@13883
   563
 ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
nipkow@24526
   564
apply (induct xs arbitrary: ys)
paulson@14208
   565
 apply (case_tac ys, simp, force)
paulson@14208
   566
apply (case_tac ys, force, simp)
nipkow@13145
   567
done
wenzelm@13114
   568
nipkow@24526
   569
lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) =
nipkow@24526
   570
  (EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)"
nipkow@24526
   571
apply (induct xs arbitrary: ys zs ts)
nipkow@14495
   572
 apply fastsimp
nipkow@14495
   573
apply(case_tac zs)
nipkow@14495
   574
 apply simp
nipkow@14495
   575
apply fastsimp
nipkow@14495
   576
done
nipkow@14495
   577
wenzelm@13142
   578
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)"
nipkow@13145
   579
by simp
wenzelm@13114
   580
wenzelm@13142
   581
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
nipkow@13145
   582
by simp
wenzelm@13114
   583
wenzelm@13142
   584
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)"
nipkow@13145
   585
by simp
wenzelm@13114
   586
wenzelm@13142
   587
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
nipkow@13145
   588
using append_same_eq [of _ _ "[]"] by auto
wenzelm@13114
   589
wenzelm@13142
   590
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
nipkow@13145
   591
using append_same_eq [of "[]"] by auto
wenzelm@13114
   592
paulson@24286
   593
lemma hd_Cons_tl [simp,noatp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
nipkow@13145
   594
by (induct xs) auto
wenzelm@13114
   595
wenzelm@13142
   596
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
nipkow@13145
   597
by (induct xs) auto
wenzelm@13114
   598
wenzelm@13142
   599
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
nipkow@13145
   600
by (simp add: hd_append split: list.split)
wenzelm@13114
   601
wenzelm@13142
   602
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
nipkow@13145
   603
by (simp split: list.split)
wenzelm@13114
   604
wenzelm@13142
   605
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
nipkow@13145
   606
by (simp add: tl_append split: list.split)
wenzelm@13114
   607
wenzelm@13142
   608
nipkow@14300
   609
lemma Cons_eq_append_conv: "x#xs = ys@zs =
nipkow@14300
   610
 (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))"
nipkow@14300
   611
by(cases ys) auto
nipkow@14300
   612
nipkow@15281
   613
lemma append_eq_Cons_conv: "(ys@zs = x#xs) =
nipkow@15281
   614
 (ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))"
nipkow@15281
   615
by(cases ys) auto
nipkow@15281
   616
nipkow@14300
   617
wenzelm@13142
   618
text {* Trivial rules for solving @{text "@"}-equations automatically. *}
wenzelm@13114
   619
wenzelm@13114
   620
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
nipkow@13145
   621
by simp
wenzelm@13114
   622
wenzelm@13142
   623
lemma Cons_eq_appendI:
nipkow@13145
   624
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
nipkow@13145
   625
by (drule sym) simp
wenzelm@13114
   626
wenzelm@13142
   627
lemma append_eq_appendI:
nipkow@13145
   628
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
nipkow@13145
   629
by (drule sym) simp
wenzelm@13114
   630
wenzelm@13114
   631
wenzelm@13142
   632
text {*
nipkow@13145
   633
Simplification procedure for all list equalities.
nipkow@13145
   634
Currently only tries to rearrange @{text "@"} to see if
nipkow@13145
   635
- both lists end in a singleton list,
nipkow@13145
   636
- or both lists end in the same list.
wenzelm@13142
   637
*}
wenzelm@13142
   638
wenzelm@26480
   639
ML {*
nipkow@3507
   640
local
nipkow@3507
   641
wenzelm@13114
   642
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
wenzelm@13462
   643
  (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
haftmann@23029
   644
  | last (Const("List.append",_) $ _ $ ys) = last ys
wenzelm@13462
   645
  | last t = t;
nipkow@3507
   646
wenzelm@13114
   647
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
wenzelm@13462
   648
  | list1 _ = false;
wenzelm@13114
   649
wenzelm@13114
   650
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
wenzelm@13462
   651
  (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
haftmann@23029
   652
  | butlast ((app as Const("List.append",_) $ xs) $ ys) = app $ butlast ys
wenzelm@13462
   653
  | butlast xs = Const("List.list.Nil",fastype_of xs);
wenzelm@13114
   654
haftmann@22633
   655
val rearr_ss = HOL_basic_ss addsimps [@{thm append_assoc},
haftmann@22633
   656
  @{thm append_Nil}, @{thm append_Cons}];
wenzelm@16973
   657
wenzelm@20044
   658
fun list_eq ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
wenzelm@13462
   659
  let
wenzelm@13462
   660
    val lastl = last lhs and lastr = last rhs;
wenzelm@13462
   661
    fun rearr conv =
wenzelm@13462
   662
      let
wenzelm@13462
   663
        val lhs1 = butlast lhs and rhs1 = butlast rhs;
wenzelm@13462
   664
        val Type(_,listT::_) = eqT
wenzelm@13462
   665
        val appT = [listT,listT] ---> listT
haftmann@23029
   666
        val app = Const("List.append",appT)
wenzelm@13462
   667
        val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
wenzelm@13480
   668
        val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2));
wenzelm@20044
   669
        val thm = Goal.prove (Simplifier.the_context ss) [] [] eq
wenzelm@17877
   670
          (K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1));
skalberg@15531
   671
      in SOME ((conv RS (thm RS trans)) RS eq_reflection) end;
wenzelm@13114
   672
wenzelm@13462
   673
  in
haftmann@22633
   674
    if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv}
haftmann@22633
   675
    else if lastl aconv lastr then rearr @{thm append_same_eq}
skalberg@15531
   676
    else NONE
wenzelm@13462
   677
  end;
wenzelm@13462
   678
nipkow@3507
   679
in
wenzelm@13462
   680
wenzelm@13462
   681
val list_eq_simproc =
wenzelm@28262
   682
  Simplifier.simproc (the_context ()) "list_eq" ["(xs::'a list) = ys"] (K list_eq);
wenzelm@13462
   683
wenzelm@13114
   684
end;
nipkow@3507
   685
wenzelm@13114
   686
Addsimprocs [list_eq_simproc];
wenzelm@13114
   687
*}
wenzelm@13114
   688
wenzelm@13114
   689
nipkow@15392
   690
subsubsection {* @{text map} *}
wenzelm@13114
   691
wenzelm@13142
   692
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
nipkow@13145
   693
by (induct xs) simp_all
wenzelm@13114
   694
wenzelm@13142
   695
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
nipkow@13145
   696
by (rule ext, induct_tac xs) auto
wenzelm@13114
   697
wenzelm@13142
   698
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
nipkow@13145
   699
by (induct xs) auto
wenzelm@13114
   700
wenzelm@13142
   701
lemma map_compose: "map (f o g) xs = map f (map g xs)"
nipkow@13145
   702
by (induct xs) (auto simp add: o_def)
wenzelm@13114
   703
wenzelm@13142
   704
lemma rev_map: "rev (map f xs) = map f (rev xs)"
nipkow@13145
   705
by (induct xs) auto
wenzelm@13114
   706
nipkow@13737
   707
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)"
nipkow@13737
   708
by (induct xs) auto
nipkow@13737
   709
krauss@19770
   710
lemma map_cong [fundef_cong, recdef_cong]:
nipkow@13145
   711
"xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys"
nipkow@13145
   712
-- {* a congruence rule for @{text map} *}
nipkow@13737
   713
by simp
wenzelm@13114
   714
wenzelm@13142
   715
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
nipkow@13145
   716
by (cases xs) auto
wenzelm@13114
   717
wenzelm@13142
   718
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
nipkow@13145
   719
by (cases xs) auto
wenzelm@13114
   720
paulson@18447
   721
lemma map_eq_Cons_conv:
nipkow@14025
   722
 "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)"
nipkow@13145
   723
by (cases xs) auto
wenzelm@13114
   724
paulson@18447
   725
lemma Cons_eq_map_conv:
nipkow@14025
   726
 "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)"
nipkow@14025
   727
by (cases ys) auto
nipkow@14025
   728
paulson@18447
   729
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1]
paulson@18447
   730
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1]
paulson@18447
   731
declare map_eq_Cons_D [dest!]  Cons_eq_map_D [dest!]
paulson@18447
   732
nipkow@14111
   733
lemma ex_map_conv:
nipkow@14111
   734
  "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)"
paulson@18447
   735
by(induct ys, auto simp add: Cons_eq_map_conv)
nipkow@14111
   736
nipkow@15110
   737
lemma map_eq_imp_length_eq:
haftmann@26734
   738
  assumes "map f xs = map f ys"
haftmann@26734
   739
  shows "length xs = length ys"
haftmann@26734
   740
using assms proof (induct ys arbitrary: xs)
haftmann@26734
   741
  case Nil then show ?case by simp
haftmann@26734
   742
next
haftmann@26734
   743
  case (Cons y ys) then obtain z zs where xs: "xs = z # zs" by auto
haftmann@26734
   744
  from Cons xs have "map f zs = map f ys" by simp
haftmann@26734
   745
  moreover with Cons have "length zs = length ys" by blast
haftmann@26734
   746
  with xs show ?case by simp
haftmann@26734
   747
qed
haftmann@26734
   748
  
nipkow@15110
   749
lemma map_inj_on:
nipkow@15110
   750
 "[| map f xs = map f ys; inj_on f (set xs Un set ys) |]
nipkow@15110
   751
  ==> xs = ys"
nipkow@15110
   752
apply(frule map_eq_imp_length_eq)
nipkow@15110
   753
apply(rotate_tac -1)
nipkow@15110
   754
apply(induct rule:list_induct2)
nipkow@15110
   755
 apply simp
nipkow@15110
   756
apply(simp)
nipkow@15110
   757
apply (blast intro:sym)
nipkow@15110
   758
done
nipkow@15110
   759
nipkow@15110
   760
lemma inj_on_map_eq_map:
nipkow@15110
   761
 "inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@15110
   762
by(blast dest:map_inj_on)
nipkow@15110
   763
wenzelm@13114
   764
lemma map_injective:
nipkow@24526
   765
 "map f xs = map f ys ==> inj f ==> xs = ys"
nipkow@24526
   766
by (induct ys arbitrary: xs) (auto dest!:injD)
wenzelm@13114
   767
nipkow@14339
   768
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@14339
   769
by(blast dest:map_injective)
nipkow@14339
   770
wenzelm@13114
   771
lemma inj_mapI: "inj f ==> inj (map f)"
nipkow@17589
   772
by (iprover dest: map_injective injD intro: inj_onI)
wenzelm@13114
   773
wenzelm@13114
   774
lemma inj_mapD: "inj (map f) ==> inj f"
paulson@14208
   775
apply (unfold inj_on_def, clarify)
nipkow@13145
   776
apply (erule_tac x = "[x]" in ballE)
paulson@14208
   777
 apply (erule_tac x = "[y]" in ballE, simp, blast)
nipkow@13145
   778
apply blast
nipkow@13145
   779
done
wenzelm@13114
   780
nipkow@14339
   781
lemma inj_map[iff]: "inj (map f) = inj f"
nipkow@13145
   782
by (blast dest: inj_mapD intro: inj_mapI)
wenzelm@13114
   783
nipkow@15303
   784
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A"
nipkow@15303
   785
apply(rule inj_onI)
nipkow@15303
   786
apply(erule map_inj_on)
nipkow@15303
   787
apply(blast intro:inj_onI dest:inj_onD)
nipkow@15303
   788
done
nipkow@15303
   789
kleing@14343
   790
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs"
kleing@14343
   791
by (induct xs, auto)
wenzelm@13114
   792
nipkow@14402
   793
lemma map_fun_upd [simp]: "y \<notin> set xs \<Longrightarrow> map (f(y:=v)) xs = map f xs"
nipkow@14402
   794
by (induct xs) auto
nipkow@14402
   795
nipkow@15110
   796
lemma map_fst_zip[simp]:
nipkow@15110
   797
  "length xs = length ys \<Longrightarrow> map fst (zip xs ys) = xs"
nipkow@15110
   798
by (induct rule:list_induct2, simp_all)
nipkow@15110
   799
nipkow@15110
   800
lemma map_snd_zip[simp]:
nipkow@15110
   801
  "length xs = length ys \<Longrightarrow> map snd (zip xs ys) = ys"
nipkow@15110
   802
by (induct rule:list_induct2, simp_all)
nipkow@15110
   803
nipkow@15110
   804
nipkow@15392
   805
subsubsection {* @{text rev} *}
wenzelm@13114
   806
wenzelm@13142
   807
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
nipkow@13145
   808
by (induct xs) auto
wenzelm@13114
   809
wenzelm@13142
   810
lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
nipkow@13145
   811
by (induct xs) auto
wenzelm@13114
   812
kleing@15870
   813
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)"
kleing@15870
   814
by auto
kleing@15870
   815
wenzelm@13142
   816
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
nipkow@13145
   817
by (induct xs) auto
wenzelm@13114
   818
wenzelm@13142
   819
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
nipkow@13145
   820
by (induct xs) auto
wenzelm@13114
   821
kleing@15870
   822
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])"
kleing@15870
   823
by (cases xs) auto
kleing@15870
   824
kleing@15870
   825
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])"
kleing@15870
   826
by (cases xs) auto
kleing@15870
   827
haftmann@21061
   828
lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)"
haftmann@21061
   829
apply (induct xs arbitrary: ys, force)
paulson@14208
   830
apply (case_tac ys, simp, force)
nipkow@13145
   831
done
wenzelm@13114
   832
nipkow@15439
   833
lemma inj_on_rev[iff]: "inj_on rev A"
nipkow@15439
   834
by(simp add:inj_on_def)
nipkow@15439
   835
wenzelm@13366
   836
lemma rev_induct [case_names Nil snoc]:
wenzelm@13366
   837
  "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
berghofe@15489
   838
apply(simplesubst rev_rev_ident[symmetric])
nipkow@13145
   839
apply(rule_tac list = "rev xs" in list.induct, simp_all)
nipkow@13145
   840
done
wenzelm@13114
   841
wenzelm@13366
   842
lemma rev_exhaust [case_names Nil snoc]:
wenzelm@13366
   843
  "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"
nipkow@13145
   844
by (induct xs rule: rev_induct) auto
wenzelm@13114
   845
wenzelm@13366
   846
lemmas rev_cases = rev_exhaust
wenzelm@13366
   847
nipkow@18423
   848
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])"
nipkow@18423
   849
by(rule rev_cases[of xs]) auto
nipkow@18423
   850
wenzelm@13114
   851
nipkow@15392
   852
subsubsection {* @{text set} *}
wenzelm@13114
   853
wenzelm@13142
   854
lemma finite_set [iff]: "finite (set xs)"
nipkow@13145
   855
by (induct xs) auto
wenzelm@13114
   856
wenzelm@13142
   857
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
nipkow@13145
   858
by (induct xs) auto
wenzelm@13114
   859
nipkow@17830
   860
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs"
nipkow@17830
   861
by(cases xs) auto
oheimb@14099
   862
wenzelm@13142
   863
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
nipkow@13145
   864
by auto
wenzelm@13114
   865
oheimb@14099
   866
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" 
oheimb@14099
   867
by auto
oheimb@14099
   868
wenzelm@13142
   869
lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
nipkow@13145
   870
by (induct xs) auto
wenzelm@13114
   871
nipkow@15245
   872
lemma set_empty2[iff]: "({} = set xs) = (xs = [])"
nipkow@15245
   873
by(induct xs) auto
nipkow@15245
   874
wenzelm@13142
   875
lemma set_rev [simp]: "set (rev xs) = set xs"
nipkow@13145
   876
by (induct xs) auto
wenzelm@13114
   877
wenzelm@13142
   878
lemma set_map [simp]: "set (map f xs) = f`(set xs)"
nipkow@13145
   879
by (induct xs) auto
wenzelm@13114
   880
wenzelm@13142
   881
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
nipkow@13145
   882
by (induct xs) auto
wenzelm@13114
   883
nipkow@15425
   884
lemma set_upt [simp]: "set[i..<j] = {k. i \<le> k \<and> k < j}"
paulson@14208
   885
apply (induct j, simp_all)
paulson@14208
   886
apply (erule ssubst, auto)
nipkow@13145
   887
done
wenzelm@13114
   888
nipkow@26073
   889
nipkow@26073
   890
lemma split_list: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs"
nipkow@26073
   891
proof (induct xs)
nipkow@26073
   892
  case Nil thus ?case by simp
nipkow@26073
   893
next
nipkow@26073
   894
  case Cons thus ?case by (auto intro: Cons_eq_appendI)
nipkow@26073
   895
qed
nipkow@26073
   896
haftmann@26734
   897
lemma in_set_conv_decomp: "x \<in> set xs \<longleftrightarrow> (\<exists>ys zs. xs = ys @ x # zs)"
haftmann@26734
   898
  by (auto elim: split_list)
nipkow@26073
   899
nipkow@26073
   900
lemma split_list_first: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys"
paulson@15113
   901
proof (induct xs)
nipkow@26073
   902
  case Nil thus ?case by simp
nipkow@18049
   903
next
nipkow@18049
   904
  case (Cons a xs)
nipkow@18049
   905
  show ?case
nipkow@18049
   906
  proof cases
wenzelm@25221
   907
    assume "x = a" thus ?case using Cons by fastsimp
nipkow@18049
   908
  next
nipkow@26073
   909
    assume "x \<noteq> a" thus ?case using Cons by(fastsimp intro!: Cons_eq_appendI)
nipkow@18049
   910
  qed
nipkow@18049
   911
qed
nipkow@18049
   912
nipkow@26073
   913
lemma in_set_conv_decomp_first:
nipkow@26073
   914
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)"
haftmann@26734
   915
  by (auto dest!: split_list_first)
nipkow@26073
   916
nipkow@26073
   917
lemma split_list_last: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs"
nipkow@26073
   918
proof (induct xs rule:rev_induct)
nipkow@26073
   919
  case Nil thus ?case by simp
nipkow@26073
   920
next
nipkow@26073
   921
  case (snoc a xs)
nipkow@26073
   922
  show ?case
nipkow@26073
   923
  proof cases
nipkow@26073
   924
    assume "x = a" thus ?case using snoc by simp (metis ex_in_conv set_empty2)
nipkow@26073
   925
  next
nipkow@26073
   926
    assume "x \<noteq> a" thus ?case using snoc by fastsimp
nipkow@26073
   927
  qed
nipkow@26073
   928
qed
nipkow@26073
   929
nipkow@26073
   930
lemma in_set_conv_decomp_last:
nipkow@26073
   931
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)"
haftmann@26734
   932
  by (auto dest!: split_list_last)
nipkow@26073
   933
nipkow@26073
   934
lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs & P x"
nipkow@26073
   935
proof (induct xs)
nipkow@26073
   936
  case Nil thus ?case by simp
nipkow@26073
   937
next
nipkow@26073
   938
  case Cons thus ?case
nipkow@26073
   939
    by(simp add:Bex_def)(metis append_Cons append.simps(1))
nipkow@26073
   940
qed
nipkow@26073
   941
nipkow@26073
   942
lemma split_list_propE:
haftmann@26734
   943
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
   944
  obtains ys x zs where "xs = ys @ x # zs" and "P x"
haftmann@26734
   945
using split_list_prop [OF assms] by blast
nipkow@26073
   946
nipkow@26073
   947
lemma split_list_first_prop:
nipkow@26073
   948
  "\<exists>x \<in> set xs. P x \<Longrightarrow>
nipkow@26073
   949
   \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)"
haftmann@26734
   950
proof (induct xs)
nipkow@26073
   951
  case Nil thus ?case by simp
nipkow@26073
   952
next
nipkow@26073
   953
  case (Cons x xs)
nipkow@26073
   954
  show ?case
nipkow@26073
   955
  proof cases
nipkow@26073
   956
    assume "P x"
haftmann@26734
   957
    thus ?thesis by simp
haftmann@26734
   958
      (metis Un_upper1 contra_subsetD in_set_conv_decomp_first self_append_conv2 set_append)
nipkow@26073
   959
  next
nipkow@26073
   960
    assume "\<not> P x"
nipkow@26073
   961
    hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp
nipkow@26073
   962
    thus ?thesis using `\<not> P x` Cons(1) by (metis append_Cons set_ConsD)
nipkow@26073
   963
  qed
nipkow@26073
   964
qed
nipkow@26073
   965
nipkow@26073
   966
lemma split_list_first_propE:
haftmann@26734
   967
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
   968
  obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y"
haftmann@26734
   969
using split_list_first_prop [OF assms] by blast
nipkow@26073
   970
nipkow@26073
   971
lemma split_list_first_prop_iff:
nipkow@26073
   972
  "(\<exists>x \<in> set xs. P x) \<longleftrightarrow>
nipkow@26073
   973
   (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))"
haftmann@26734
   974
by (rule, erule split_list_first_prop) auto
nipkow@26073
   975
nipkow@26073
   976
lemma split_list_last_prop:
nipkow@26073
   977
  "\<exists>x \<in> set xs. P x \<Longrightarrow>
nipkow@26073
   978
   \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)"
nipkow@26073
   979
proof(induct xs rule:rev_induct)
nipkow@26073
   980
  case Nil thus ?case by simp
nipkow@26073
   981
next
nipkow@26073
   982
  case (snoc x xs)
nipkow@26073
   983
  show ?case
nipkow@26073
   984
  proof cases
nipkow@26073
   985
    assume "P x" thus ?thesis by (metis emptyE set_empty)
nipkow@26073
   986
  next
nipkow@26073
   987
    assume "\<not> P x"
nipkow@26073
   988
    hence "\<exists>x\<in>set xs. P x" using snoc(2) by simp
nipkow@26073
   989
    thus ?thesis using `\<not> P x` snoc(1) by fastsimp
nipkow@26073
   990
  qed
nipkow@26073
   991
qed
nipkow@26073
   992
nipkow@26073
   993
lemma split_list_last_propE:
haftmann@26734
   994
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
   995
  obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z"
haftmann@26734
   996
using split_list_last_prop [OF assms] by blast
nipkow@26073
   997
nipkow@26073
   998
lemma split_list_last_prop_iff:
nipkow@26073
   999
  "(\<exists>x \<in> set xs. P x) \<longleftrightarrow>
nipkow@26073
  1000
   (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))"
haftmann@26734
  1001
by (metis split_list_last_prop [where P=P] in_set_conv_decomp)
nipkow@26073
  1002
nipkow@26073
  1003
lemma finite_list: "finite A ==> EX xs. set xs = A"
haftmann@26734
  1004
  by (erule finite_induct)
haftmann@26734
  1005
    (auto simp add: set.simps(2) [symmetric] simp del: set.simps(2))
paulson@13508
  1006
kleing@14388
  1007
lemma card_length: "card (set xs) \<le> length xs"
kleing@14388
  1008
by (induct xs) (auto simp add: card_insert_if)
wenzelm@13114
  1009
haftmann@26442
  1010
lemma set_minus_filter_out:
haftmann@26442
  1011
  "set xs - {y} = set (filter (\<lambda>x. \<not> (x = y)) xs)"
haftmann@26442
  1012
  by (induct xs) auto
paulson@15168
  1013
nipkow@15392
  1014
subsubsection {* @{text filter} *}
wenzelm@13114
  1015
wenzelm@13142
  1016
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
nipkow@13145
  1017
by (induct xs) auto
wenzelm@13114
  1018
nipkow@15305
  1019
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)"
nipkow@15305
  1020
by (induct xs) simp_all
nipkow@15305
  1021
wenzelm@13142
  1022
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
nipkow@13145
  1023
by (induct xs) auto
wenzelm@13114
  1024
nipkow@16998
  1025
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs"
nipkow@16998
  1026
by (induct xs) (auto simp add: le_SucI)
nipkow@16998
  1027
nipkow@18423
  1028
lemma sum_length_filter_compl:
nipkow@18423
  1029
  "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs"
nipkow@18423
  1030
by(induct xs) simp_all
nipkow@18423
  1031
wenzelm@13142
  1032
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
nipkow@13145
  1033
by (induct xs) auto
wenzelm@13114
  1034
wenzelm@13142
  1035
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
nipkow@13145
  1036
by (induct xs) auto
wenzelm@13114
  1037
nipkow@16998
  1038
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" 
nipkow@24349
  1039
by (induct xs) simp_all
nipkow@16998
  1040
nipkow@16998
  1041
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)"
nipkow@16998
  1042
apply (induct xs)
nipkow@16998
  1043
 apply auto
nipkow@16998
  1044
apply(cut_tac P=P and xs=xs in length_filter_le)
nipkow@16998
  1045
apply simp
nipkow@16998
  1046
done
wenzelm@13114
  1047
nipkow@16965
  1048
lemma filter_map:
nipkow@16965
  1049
  "filter P (map f xs) = map f (filter (P o f) xs)"
nipkow@16965
  1050
by (induct xs) simp_all
nipkow@16965
  1051
nipkow@16965
  1052
lemma length_filter_map[simp]:
nipkow@16965
  1053
  "length (filter P (map f xs)) = length(filter (P o f) xs)"
nipkow@16965
  1054
by (simp add:filter_map)
nipkow@16965
  1055
wenzelm@13142
  1056
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
nipkow@13145
  1057
by auto
wenzelm@13114
  1058
nipkow@15246
  1059
lemma length_filter_less:
nipkow@15246
  1060
  "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs"
nipkow@15246
  1061
proof (induct xs)
nipkow@15246
  1062
  case Nil thus ?case by simp
nipkow@15246
  1063
next
nipkow@15246
  1064
  case (Cons x xs) thus ?case
nipkow@15246
  1065
    apply (auto split:split_if_asm)
nipkow@15246
  1066
    using length_filter_le[of P xs] apply arith
nipkow@15246
  1067
  done
nipkow@15246
  1068
qed
wenzelm@13114
  1069
nipkow@15281
  1070
lemma length_filter_conv_card:
nipkow@15281
  1071
 "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
nipkow@15281
  1072
proof (induct xs)
nipkow@15281
  1073
  case Nil thus ?case by simp
nipkow@15281
  1074
next
nipkow@15281
  1075
  case (Cons x xs)
nipkow@15281
  1076
  let ?S = "{i. i < length xs & p(xs!i)}"
nipkow@15281
  1077
  have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite)
nipkow@15281
  1078
  show ?case (is "?l = card ?S'")
nipkow@15281
  1079
  proof (cases)
nipkow@15281
  1080
    assume "p x"
nipkow@15281
  1081
    hence eq: "?S' = insert 0 (Suc ` ?S)"
nipkow@25162
  1082
      by(auto simp: image_def split:nat.split dest:gr0_implies_Suc)
nipkow@15281
  1083
    have "length (filter p (x # xs)) = Suc(card ?S)"
wenzelm@23388
  1084
      using Cons `p x` by simp
nipkow@15281
  1085
    also have "\<dots> = Suc(card(Suc ` ?S))" using fin
nipkow@15281
  1086
      by (simp add: card_image inj_Suc)
nipkow@15281
  1087
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
  1088
      by (simp add:card_insert_if) (simp add:image_def)
nipkow@15281
  1089
    finally show ?thesis .
nipkow@15281
  1090
  next
nipkow@15281
  1091
    assume "\<not> p x"
nipkow@15281
  1092
    hence eq: "?S' = Suc ` ?S"
nipkow@25162
  1093
      by(auto simp add: image_def split:nat.split elim:lessE)
nipkow@15281
  1094
    have "length (filter p (x # xs)) = card ?S"
wenzelm@23388
  1095
      using Cons `\<not> p x` by simp
nipkow@15281
  1096
    also have "\<dots> = card(Suc ` ?S)" using fin
nipkow@15281
  1097
      by (simp add: card_image inj_Suc)
nipkow@15281
  1098
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
  1099
      by (simp add:card_insert_if)
nipkow@15281
  1100
    finally show ?thesis .
nipkow@15281
  1101
  qed
nipkow@15281
  1102
qed
nipkow@15281
  1103
nipkow@17629
  1104
lemma Cons_eq_filterD:
nipkow@17629
  1105
 "x#xs = filter P ys \<Longrightarrow>
nipkow@17629
  1106
  \<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
  1107
  (is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs")
nipkow@17629
  1108
proof(induct ys)
nipkow@17629
  1109
  case Nil thus ?case by simp
nipkow@17629
  1110
next
nipkow@17629
  1111
  case (Cons y ys)
nipkow@17629
  1112
  show ?case (is "\<exists>x. ?Q x")
nipkow@17629
  1113
  proof cases
nipkow@17629
  1114
    assume Py: "P y"
nipkow@17629
  1115
    show ?thesis
nipkow@17629
  1116
    proof cases
wenzelm@25221
  1117
      assume "x = y"
wenzelm@25221
  1118
      with Py Cons.prems have "?Q []" by simp
wenzelm@25221
  1119
      then show ?thesis ..
nipkow@17629
  1120
    next
wenzelm@25221
  1121
      assume "x \<noteq> y"
wenzelm@25221
  1122
      with Py Cons.prems show ?thesis by simp
nipkow@17629
  1123
    qed
nipkow@17629
  1124
  next
wenzelm@25221
  1125
    assume "\<not> P y"
wenzelm@25221
  1126
    with Cons obtain us vs where "?P (y#ys) (y#us) vs" by fastsimp
wenzelm@25221
  1127
    then have "?Q (y#us)" by simp
wenzelm@25221
  1128
    then show ?thesis ..
nipkow@17629
  1129
  qed
nipkow@17629
  1130
qed
nipkow@17629
  1131
nipkow@17629
  1132
lemma filter_eq_ConsD:
nipkow@17629
  1133
 "filter P ys = x#xs \<Longrightarrow>
nipkow@17629
  1134
  \<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
  1135
by(rule Cons_eq_filterD) simp
nipkow@17629
  1136
nipkow@17629
  1137
lemma filter_eq_Cons_iff:
nipkow@17629
  1138
 "(filter P ys = x#xs) =
nipkow@17629
  1139
  (\<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
  1140
by(auto dest:filter_eq_ConsD)
nipkow@17629
  1141
nipkow@17629
  1142
lemma Cons_eq_filter_iff:
nipkow@17629
  1143
 "(x#xs = filter P ys) =
nipkow@17629
  1144
  (\<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
  1145
by(auto dest:Cons_eq_filterD)
nipkow@17629
  1146
krauss@19770
  1147
lemma filter_cong[fundef_cong, recdef_cong]:
nipkow@17501
  1148
 "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys"
nipkow@17501
  1149
apply simp
nipkow@17501
  1150
apply(erule thin_rl)
nipkow@17501
  1151
by (induct ys) simp_all
nipkow@17501
  1152
nipkow@15281
  1153
haftmann@26442
  1154
subsubsection {* List partitioning *}
haftmann@26442
  1155
haftmann@26442
  1156
primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where
haftmann@26442
  1157
  "partition P [] = ([], [])"
haftmann@26442
  1158
  | "partition P (x # xs) = 
haftmann@26442
  1159
      (let (yes, no) = partition P xs
haftmann@26442
  1160
      in if P x then (x # yes, no) else (yes, x # no))"
haftmann@26442
  1161
haftmann@26442
  1162
lemma partition_filter1:
haftmann@26442
  1163
    "fst (partition P xs) = filter P xs"
haftmann@26442
  1164
by (induct xs) (auto simp add: Let_def split_def)
haftmann@26442
  1165
haftmann@26442
  1166
lemma partition_filter2:
haftmann@26442
  1167
    "snd (partition P xs) = filter (Not o P) xs"
haftmann@26442
  1168
by (induct xs) (auto simp add: Let_def split_def)
haftmann@26442
  1169
haftmann@26442
  1170
lemma partition_P:
haftmann@26442
  1171
  assumes "partition P xs = (yes, no)"
haftmann@26442
  1172
  shows "(\<forall>p \<in> set yes.  P p) \<and> (\<forall>p  \<in> set no. \<not> P p)"
haftmann@26442
  1173
proof -
haftmann@26442
  1174
  from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)"
haftmann@26442
  1175
    by simp_all
haftmann@26442
  1176
  then show ?thesis by (simp_all add: partition_filter1 partition_filter2)
haftmann@26442
  1177
qed
haftmann@26442
  1178
haftmann@26442
  1179
lemma partition_set:
haftmann@26442
  1180
  assumes "partition P xs = (yes, no)"
haftmann@26442
  1181
  shows "set yes \<union> set no = set xs"
haftmann@26442
  1182
proof -
haftmann@26442
  1183
  from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)"
haftmann@26442
  1184
    by simp_all
haftmann@26442
  1185
  then show ?thesis by (auto simp add: partition_filter1 partition_filter2) 
haftmann@26442
  1186
qed
haftmann@26442
  1187
haftmann@26442
  1188
nipkow@15392
  1189
subsubsection {* @{text concat} *}
wenzelm@13114
  1190
wenzelm@13142
  1191
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
nipkow@13145
  1192
by (induct xs) auto
wenzelm@13114
  1193
paulson@18447
  1194
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1195
by (induct xss) auto
wenzelm@13114
  1196
paulson@18447
  1197
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1198
by (induct xss) auto
wenzelm@13114
  1199
nipkow@24308
  1200
lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)"
nipkow@13145
  1201
by (induct xs) auto
wenzelm@13114
  1202
nipkow@24476
  1203
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs"
nipkow@24349
  1204
by (induct xs) auto
nipkow@24349
  1205
wenzelm@13142
  1206
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
nipkow@13145
  1207
by (induct xs) auto
wenzelm@13114
  1208
wenzelm@13142
  1209
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
nipkow@13145
  1210
by (induct xs) auto
wenzelm@13114
  1211
wenzelm@13142
  1212
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
nipkow@13145
  1213
by (induct xs) auto
wenzelm@13114
  1214
wenzelm@13114
  1215
nipkow@15392
  1216
subsubsection {* @{text nth} *}
wenzelm@13114
  1217
haftmann@29764
  1218
lemma nth_Cons_0 [simp, code]: "(x # xs)!0 = x"
nipkow@13145
  1219
by auto
wenzelm@13114
  1220
haftmann@29764
  1221
lemma nth_Cons_Suc [simp, code]: "(x # xs)!(Suc n) = xs!n"
nipkow@13145
  1222
by auto
wenzelm@13114
  1223
wenzelm@13142
  1224
declare nth.simps [simp del]
wenzelm@13114
  1225
wenzelm@13114
  1226
lemma nth_append:
nipkow@24526
  1227
  "(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
nipkow@24526
  1228
apply (induct xs arbitrary: n, simp)
paulson@14208
  1229
apply (case_tac n, auto)
nipkow@13145
  1230
done
wenzelm@13114
  1231
nipkow@14402
  1232
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x"
wenzelm@25221
  1233
by (induct xs) auto
nipkow@14402
  1234
nipkow@14402
  1235
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n"
wenzelm@25221
  1236
by (induct xs) auto
nipkow@14402
  1237
nipkow@24526
  1238
lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)"
nipkow@24526
  1239
apply (induct xs arbitrary: n, simp)
paulson@14208
  1240
apply (case_tac n, auto)
nipkow@13145
  1241
done
wenzelm@13114
  1242
nipkow@18423
  1243
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"
nipkow@18423
  1244
by(cases xs) simp_all
nipkow@18423
  1245
nipkow@18049
  1246
nipkow@18049
  1247
lemma list_eq_iff_nth_eq:
nipkow@24526
  1248
 "(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))"
nipkow@24526
  1249
apply(induct xs arbitrary: ys)
paulson@24632
  1250
 apply force
nipkow@18049
  1251
apply(case_tac ys)
nipkow@18049
  1252
 apply simp
nipkow@18049
  1253
apply(simp add:nth_Cons split:nat.split)apply blast
nipkow@18049
  1254
done
nipkow@18049
  1255
wenzelm@13142
  1256
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
paulson@15251
  1257
apply (induct xs, simp, simp)
nipkow@13145
  1258
apply safe
paulson@24632
  1259
apply (metis nat_case_0 nth.simps zero_less_Suc)
paulson@24632
  1260
apply (metis less_Suc_eq_0_disj nth_Cons_Suc)
paulson@14208
  1261
apply (case_tac i, simp)
paulson@24632
  1262
apply (metis diff_Suc_Suc nat_case_Suc nth.simps zero_less_diff)
nipkow@13145
  1263
done
wenzelm@13114
  1264
nipkow@17501
  1265
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)"
nipkow@17501
  1266
by(auto simp:set_conv_nth)
nipkow@17501
  1267
nipkow@13145
  1268
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"
nipkow@13145
  1269
by (auto simp add: set_conv_nth)
wenzelm@13114
  1270
wenzelm@13142
  1271
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
nipkow@13145
  1272
by (auto simp add: set_conv_nth)
wenzelm@13114
  1273
wenzelm@13114
  1274
lemma all_nth_imp_all_set:
nipkow@13145
  1275
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x"
nipkow@13145
  1276
by (auto simp add: set_conv_nth)
wenzelm@13114
  1277
wenzelm@13114
  1278
lemma all_set_conv_all_nth:
nipkow@13145
  1279
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
nipkow@13145
  1280
by (auto simp add: set_conv_nth)
wenzelm@13114
  1281
kleing@25296
  1282
lemma rev_nth:
kleing@25296
  1283
  "n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)"
kleing@25296
  1284
proof (induct xs arbitrary: n)
kleing@25296
  1285
  case Nil thus ?case by simp
kleing@25296
  1286
next
kleing@25296
  1287
  case (Cons x xs)
kleing@25296
  1288
  hence n: "n < Suc (length xs)" by simp
kleing@25296
  1289
  moreover
kleing@25296
  1290
  { assume "n < length xs"
kleing@25296
  1291
    with n obtain n' where "length xs - n = Suc n'"
kleing@25296
  1292
      by (cases "length xs - n", auto)
kleing@25296
  1293
    moreover
kleing@25296
  1294
    then have "length xs - Suc n = n'" by simp
kleing@25296
  1295
    ultimately
kleing@25296
  1296
    have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp
kleing@25296
  1297
  }
kleing@25296
  1298
  ultimately
kleing@25296
  1299
  show ?case by (clarsimp simp add: Cons nth_append)
kleing@25296
  1300
qed
wenzelm@13114
  1301
nipkow@15392
  1302
subsubsection {* @{text list_update} *}
wenzelm@13114
  1303
nipkow@24526
  1304
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs"
nipkow@24526
  1305
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1306
wenzelm@13114
  1307
lemma nth_list_update:
nipkow@24526
  1308
"i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"
nipkow@24526
  1309
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1310
wenzelm@13142
  1311
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
nipkow@13145
  1312
by (simp add: nth_list_update)
wenzelm@13114
  1313
nipkow@24526
  1314
lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j"
nipkow@24526
  1315
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1316
nipkow@24526
  1317
lemma list_update_id[simp]: "xs[i := xs!i] = xs"
nipkow@24526
  1318
by (induct xs arbitrary: i) (simp_all split:nat.splits)
nipkow@24526
  1319
nipkow@24526
  1320
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs"
nipkow@24526
  1321
apply (induct xs arbitrary: i)
nipkow@17501
  1322
 apply simp
nipkow@17501
  1323
apply (case_tac i)
nipkow@17501
  1324
apply simp_all
nipkow@17501
  1325
done
nipkow@17501
  1326
wenzelm@13114
  1327
lemma list_update_same_conv:
nipkow@24526
  1328
"i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
nipkow@24526
  1329
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1330
nipkow@14187
  1331
lemma list_update_append1:
nipkow@24526
  1332
 "i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"
nipkow@24526
  1333
apply (induct xs arbitrary: i, simp)
nipkow@14187
  1334
apply(simp split:nat.split)
nipkow@14187
  1335
done
nipkow@14187
  1336
kleing@15868
  1337
lemma list_update_append:
nipkow@24526
  1338
  "(xs @ ys) [n:= x] = 
kleing@15868
  1339
  (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))"
nipkow@24526
  1340
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1341
nipkow@14402
  1342
lemma list_update_length [simp]:
nipkow@14402
  1343
 "(xs @ x # ys)[length xs := y] = (xs @ y # ys)"
nipkow@14402
  1344
by (induct xs, auto)
nipkow@14402
  1345
wenzelm@13114
  1346
lemma update_zip:
nipkow@24526
  1347
  "length xs = length ys ==>
nipkow@24526
  1348
  (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
nipkow@24526
  1349
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split)
nipkow@24526
  1350
nipkow@24526
  1351
lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)"
nipkow@24526
  1352
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1353
wenzelm@13114
  1354
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
nipkow@13145
  1355
by (blast dest!: set_update_subset_insert [THEN subsetD])
wenzelm@13114
  1356
nipkow@24526
  1357
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])"
nipkow@24526
  1358
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1359
haftmann@24796
  1360
lemma list_update_overwrite:
haftmann@24796
  1361
  "xs [i := x, i := y] = xs [i := y]"
haftmann@24796
  1362
apply (induct xs arbitrary: i)
haftmann@24796
  1363
apply simp
haftmann@24796
  1364
apply (case_tac i)
haftmann@24796
  1365
apply simp_all
haftmann@24796
  1366
done
haftmann@24796
  1367
haftmann@24796
  1368
lemma list_update_swap:
haftmann@24796
  1369
  "i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]"
haftmann@24796
  1370
apply (induct xs arbitrary: i i')
haftmann@24796
  1371
apply simp
haftmann@24796
  1372
apply (case_tac i, case_tac i')
haftmann@24796
  1373
apply auto
haftmann@24796
  1374
apply (case_tac i')
haftmann@24796
  1375
apply auto
haftmann@24796
  1376
done
haftmann@24796
  1377
haftmann@29764
  1378
lemma list_update_code [code]:
haftmann@29764
  1379
  "[][i := y] = []"
haftmann@29764
  1380
  "(x # xs)[0 := y] = y # xs"
haftmann@29764
  1381
  "(x # xs)[Suc i := y] = x # xs[i := y]"
haftmann@29764
  1382
  by simp_all
haftmann@29764
  1383
wenzelm@13114
  1384
nipkow@15392
  1385
subsubsection {* @{text last} and @{text butlast} *}
wenzelm@13114
  1386
wenzelm@13142
  1387
lemma last_snoc [simp]: "last (xs @ [x]) = x"
nipkow@13145
  1388
by (induct xs) auto
wenzelm@13114
  1389
wenzelm@13142
  1390
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
nipkow@13145
  1391
by (induct xs) auto
wenzelm@13114
  1392
nipkow@14302
  1393
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x"
nipkow@14302
  1394
by(simp add:last.simps)
nipkow@14302
  1395
nipkow@14302
  1396
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs"
nipkow@14302
  1397
by(simp add:last.simps)
nipkow@14302
  1398
nipkow@14302
  1399
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)"
nipkow@14302
  1400
by (induct xs) (auto)
nipkow@14302
  1401
nipkow@14302
  1402
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs"
nipkow@14302
  1403
by(simp add:last_append)
nipkow@14302
  1404
nipkow@14302
  1405
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"
nipkow@14302
  1406
by(simp add:last_append)
nipkow@14302
  1407
nipkow@17762
  1408
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"
nipkow@17762
  1409
by(rule rev_exhaust[of xs]) simp_all
nipkow@17762
  1410
nipkow@17762
  1411
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"
nipkow@17762
  1412
by(cases xs) simp_all
nipkow@17762
  1413
nipkow@17765
  1414
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as"
nipkow@17765
  1415
by (induct as) auto
nipkow@17762
  1416
wenzelm@13142
  1417
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
nipkow@13145
  1418
by (induct xs rule: rev_induct) auto
wenzelm@13114
  1419
wenzelm@13114
  1420
lemma butlast_append:
nipkow@24526
  1421
  "butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
nipkow@24526
  1422
by (induct xs arbitrary: ys) auto
wenzelm@13114
  1423
wenzelm@13142
  1424
lemma append_butlast_last_id [simp]:
nipkow@13145
  1425
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
nipkow@13145
  1426
by (induct xs) auto
wenzelm@13114
  1427
wenzelm@13142
  1428
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
nipkow@13145
  1429
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1430
wenzelm@13114
  1431
lemma in_set_butlast_appendI:
nipkow@13145
  1432
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
nipkow@13145
  1433
by (auto dest: in_set_butlastD simp add: butlast_append)
wenzelm@13114
  1434
nipkow@24526
  1435
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs"
nipkow@24526
  1436
apply (induct xs arbitrary: n)
nipkow@17501
  1437
 apply simp
nipkow@17501
  1438
apply (auto split:nat.split)
nipkow@17501
  1439
done
nipkow@17501
  1440
nipkow@17589
  1441
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"
nipkow@17589
  1442
by(induct xs)(auto simp:neq_Nil_conv)
nipkow@17589
  1443
huffman@26584
  1444
lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs"
huffman@26584
  1445
by (induct xs, simp, case_tac xs, simp_all)
huffman@26584
  1446
haftmann@24796
  1447
nipkow@15392
  1448
subsubsection {* @{text take} and @{text drop} *}
wenzelm@13114
  1449
wenzelm@13142
  1450
lemma take_0 [simp]: "take 0 xs = []"
nipkow@13145
  1451
by (induct xs) auto
wenzelm@13114
  1452
wenzelm@13142
  1453
lemma drop_0 [simp]: "drop 0 xs = xs"
nipkow@13145
  1454
by (induct xs) auto
wenzelm@13114
  1455
wenzelm@13142
  1456
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
nipkow@13145
  1457
by simp
wenzelm@13114
  1458
wenzelm@13142
  1459
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
nipkow@13145
  1460
by simp
wenzelm@13114
  1461
wenzelm@13142
  1462
declare take_Cons [simp del] and drop_Cons [simp del]
wenzelm@13114
  1463
nipkow@15110
  1464
lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)"
nipkow@15110
  1465
by(clarsimp simp add:neq_Nil_conv)
nipkow@15110
  1466
nipkow@14187
  1467
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"
nipkow@14187
  1468
by(cases xs, simp_all)
nipkow@14187
  1469
huffman@26584
  1470
lemma take_tl: "take n (tl xs) = tl (take (Suc n) xs)"
huffman@26584
  1471
by (induct xs arbitrary: n) simp_all
huffman@26584
  1472
nipkow@24526
  1473
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)"
nipkow@24526
  1474
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split)
nipkow@24526
  1475
huffman@26584
  1476
lemma tl_take: "tl (take n xs) = take (n - 1) (tl xs)"
huffman@26584
  1477
by (cases n, simp, cases xs, auto)
huffman@26584
  1478
huffman@26584
  1479
lemma tl_drop: "tl (drop n xs) = drop n (tl xs)"
huffman@26584
  1480
by (simp only: drop_tl)
huffman@26584
  1481
nipkow@24526
  1482
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y"
nipkow@24526
  1483
apply (induct xs arbitrary: n, simp)
nipkow@14187
  1484
apply(simp add:drop_Cons nth_Cons split:nat.splits)
nipkow@14187
  1485
done
nipkow@14187
  1486
nipkow@13913
  1487
lemma take_Suc_conv_app_nth:
nipkow@24526
  1488
  "i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"
nipkow@24526
  1489
apply (induct xs arbitrary: i, simp)
paulson@14208
  1490
apply (case_tac i, auto)
nipkow@13913
  1491
done
nipkow@13913
  1492
mehta@14591
  1493
lemma drop_Suc_conv_tl:
nipkow@24526
  1494
  "i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs"
nipkow@24526
  1495
apply (induct xs arbitrary: i, simp)
mehta@14591
  1496
apply (case_tac i, auto)
mehta@14591
  1497
done
mehta@14591
  1498
nipkow@24526
  1499
lemma length_take [simp]: "length (take n xs) = min (length xs) n"
nipkow@24526
  1500
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1501
nipkow@24526
  1502
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)"
nipkow@24526
  1503
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1504
nipkow@24526
  1505
lemma take_all [simp]: "length xs <= n ==> take n xs = xs"
nipkow@24526
  1506
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1507
nipkow@24526
  1508
lemma drop_all [simp]: "length xs <= n ==> drop n xs = []"
nipkow@24526
  1509
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1510
wenzelm@13142
  1511
lemma take_append [simp]:
nipkow@24526
  1512
  "take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
nipkow@24526
  1513
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1514
wenzelm@13142
  1515
lemma drop_append [simp]:
nipkow@24526
  1516
  "drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
nipkow@24526
  1517
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1518
nipkow@24526
  1519
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs"
nipkow@24526
  1520
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1521
apply (case_tac xs, auto)
nipkow@15236
  1522
apply (case_tac n, auto)
nipkow@13145
  1523
done
wenzelm@13142
  1524
nipkow@24526
  1525
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs"
nipkow@24526
  1526
apply (induct m arbitrary: xs, auto)
paulson@14208
  1527
apply (case_tac xs, auto)
nipkow@13145
  1528
done
wenzelm@13114
  1529
nipkow@24526
  1530
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)"
nipkow@24526
  1531
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1532
apply (case_tac xs, auto)
nipkow@13145
  1533
done
wenzelm@13114
  1534
nipkow@24526
  1535
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)"
nipkow@24526
  1536
apply(induct xs arbitrary: m n)
nipkow@14802
  1537
 apply simp
nipkow@14802
  1538
apply(simp add: take_Cons drop_Cons split:nat.split)
nipkow@14802
  1539
done
nipkow@14802
  1540
nipkow@24526
  1541
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs"
nipkow@24526
  1542
apply (induct n arbitrary: xs, auto)
paulson@14208
  1543
apply (case_tac xs, auto)
nipkow@13145
  1544
done
wenzelm@13114
  1545
nipkow@24526
  1546
lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])"
nipkow@24526
  1547
apply(induct xs arbitrary: n)
nipkow@15110
  1548
 apply simp
nipkow@15110
  1549
apply(simp add:take_Cons split:nat.split)
nipkow@15110
  1550
done
nipkow@15110
  1551
nipkow@24526
  1552
lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)"
nipkow@24526
  1553
apply(induct xs arbitrary: n)
nipkow@15110
  1554
apply simp
nipkow@15110
  1555
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1556
done
nipkow@15110
  1557
nipkow@24526
  1558
lemma take_map: "take n (map f xs) = map f (take n xs)"
nipkow@24526
  1559
apply (induct n arbitrary: xs, auto)
paulson@14208
  1560
apply (case_tac xs, auto)
nipkow@13145
  1561
done
wenzelm@13114
  1562
nipkow@24526
  1563
lemma drop_map: "drop n (map f xs) = map f (drop n xs)"
nipkow@24526
  1564
apply (induct n arbitrary: xs, auto)
paulson@14208
  1565
apply (case_tac xs, auto)
nipkow@13145
  1566
done
wenzelm@13114
  1567
nipkow@24526
  1568
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)"
nipkow@24526
  1569
apply (induct xs arbitrary: i, auto)
paulson@14208
  1570
apply (case_tac i, auto)
nipkow@13145
  1571
done
wenzelm@13114
  1572
nipkow@24526
  1573
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)"
nipkow@24526
  1574
apply (induct xs arbitrary: i, auto)
paulson@14208
  1575
apply (case_tac i, auto)
nipkow@13145
  1576
done
wenzelm@13114
  1577
nipkow@24526
  1578
lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i"
nipkow@24526
  1579
apply (induct xs arbitrary: i n, auto)
paulson@14208
  1580
apply (case_tac n, blast)
paulson@14208
  1581
apply (case_tac i, auto)
nipkow@13145
  1582
done
wenzelm@13114
  1583
wenzelm@13142
  1584
lemma nth_drop [simp]:
nipkow@24526
  1585
  "n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
nipkow@24526
  1586
apply (induct n arbitrary: xs i, auto)
paulson@14208
  1587
apply (case_tac xs, auto)
nipkow@13145
  1588
done
wenzelm@13114
  1589
huffman@26584
  1590
lemma butlast_take:
huffman@26584
  1591
  "n <= length xs ==> butlast (take n xs) = take (n - 1) xs"
huffman@26584
  1592
by (simp add: butlast_conv_take min_max.inf_absorb1 min_max.inf_absorb2)
huffman@26584
  1593
huffman@26584
  1594
lemma butlast_drop: "butlast (drop n xs) = drop n (butlast xs)"
huffman@26584
  1595
by (simp add: butlast_conv_take drop_take)
huffman@26584
  1596
huffman@26584
  1597
lemma take_butlast: "n < length xs ==> take n (butlast xs) = take n xs"
huffman@26584
  1598
by (simp add: butlast_conv_take min_max.inf_absorb1)
huffman@26584
  1599
huffman@26584
  1600
lemma drop_butlast: "drop n (butlast xs) = butlast (drop n xs)"
huffman@26584
  1601
by (simp add: butlast_conv_take drop_take)
huffman@26584
  1602
nipkow@18423
  1603
lemma hd_drop_conv_nth: "\<lbrakk> xs \<noteq> []; n < length xs \<rbrakk> \<Longrightarrow> hd(drop n xs) = xs!n"
nipkow@18423
  1604
by(simp add: hd_conv_nth)
nipkow@18423
  1605
nipkow@24526
  1606
lemma set_take_subset: "set(take n xs) \<subseteq> set xs"
nipkow@24526
  1607
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split)
nipkow@24526
  1608
nipkow@24526
  1609
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs"
nipkow@24526
  1610
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split)
nipkow@14025
  1611
nipkow@14187
  1612
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  1613
using set_take_subset by fast
nipkow@14187
  1614
nipkow@14187
  1615
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  1616
using set_drop_subset by fast
nipkow@14187
  1617
wenzelm@13114
  1618
lemma append_eq_conv_conj:
nipkow@24526
  1619
  "(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
nipkow@24526
  1620
apply (induct xs arbitrary: zs, simp, clarsimp)
paulson@14208
  1621
apply (case_tac zs, auto)
nipkow@13145
  1622
done
wenzelm@13114
  1623
nipkow@24526
  1624
lemma take_add: 
nipkow@24526
  1625
  "i+j \<le> length(xs) \<Longrightarrow> take (i+j) xs = take i xs @ take j (drop i xs)"
nipkow@24526
  1626
apply (induct xs arbitrary: i, auto) 
nipkow@24526
  1627
apply (case_tac i, simp_all)
paulson@14050
  1628
done
paulson@14050
  1629
nipkow@14300
  1630
lemma append_eq_append_conv_if:
nipkow@24526
  1631
 "(xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) =
nipkow@14300
  1632
  (if size xs\<^isub>1 \<le> size ys\<^isub>1
nipkow@14300
  1633
   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
  1634
   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
  1635
apply(induct xs\<^isub>1 arbitrary: ys\<^isub>1)
nipkow@14300
  1636
 apply simp
nipkow@14300
  1637
apply(case_tac ys\<^isub>1)
nipkow@14300
  1638
apply simp_all
nipkow@14300
  1639
done
nipkow@14300
  1640
nipkow@15110
  1641
lemma take_hd_drop:
nipkow@24526
  1642
  "n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (n+1) xs"
nipkow@24526
  1643
apply(induct xs arbitrary: n)
nipkow@15110
  1644
apply simp
nipkow@15110
  1645
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1646
done
nipkow@15110
  1647
nipkow@17501
  1648
lemma id_take_nth_drop:
nipkow@17501
  1649
 "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" 
nipkow@17501
  1650
proof -
nipkow@17501
  1651
  assume si: "i < length xs"
nipkow@17501
  1652
  hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto
nipkow@17501
  1653
  moreover
nipkow@17501
  1654
  from si have "take (Suc i) xs = take i xs @ [xs!i]"
nipkow@17501
  1655
    apply (rule_tac take_Suc_conv_app_nth) by arith
nipkow@17501
  1656
  ultimately show ?thesis by auto
nipkow@17501
  1657
qed
nipkow@17501
  1658
  
nipkow@17501
  1659
lemma upd_conv_take_nth_drop:
nipkow@17501
  1660
 "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  1661
proof -
nipkow@17501
  1662
  assume i: "i < length xs"
nipkow@17501
  1663
  have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]"
nipkow@17501
  1664
    by(rule arg_cong[OF id_take_nth_drop[OF i]])
nipkow@17501
  1665
  also have "\<dots> = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  1666
    using i by (simp add: list_update_append)
nipkow@17501
  1667
  finally show ?thesis .
nipkow@17501
  1668
qed
nipkow@17501
  1669
haftmann@24796
  1670
lemma nth_drop':
haftmann@24796
  1671
  "i < length xs \<Longrightarrow> xs ! i # drop (Suc i) xs = drop i xs"
haftmann@24796
  1672
apply (induct i arbitrary: xs)
haftmann@24796
  1673
apply (simp add: neq_Nil_conv)
haftmann@24796
  1674
apply (erule exE)+
haftmann@24796
  1675
apply simp
haftmann@24796
  1676
apply (case_tac xs)
haftmann@24796
  1677
apply simp_all
haftmann@24796
  1678
done
haftmann@24796
  1679
wenzelm@13114
  1680
nipkow@15392
  1681
subsubsection {* @{text takeWhile} and @{text dropWhile} *}
wenzelm@13114
  1682
wenzelm@13142
  1683
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
nipkow@13145
  1684
by (induct xs) auto
wenzelm@13114
  1685
wenzelm@13142
  1686
lemma takeWhile_append1 [simp]:
nipkow@13145
  1687
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"
nipkow@13145
  1688
by (induct xs) auto
wenzelm@13114
  1689
wenzelm@13142
  1690
lemma takeWhile_append2 [simp]:
nipkow@13145
  1691
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
nipkow@13145
  1692
by (induct xs) auto
wenzelm@13114
  1693
wenzelm@13142
  1694
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
nipkow@13145
  1695
by (induct xs) auto
wenzelm@13114
  1696
wenzelm@13142
  1697
lemma dropWhile_append1 [simp]:
nipkow@13145
  1698
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
nipkow@13145
  1699
by (induct xs) auto
wenzelm@13114
  1700
wenzelm@13142
  1701
lemma dropWhile_append2 [simp]:
nipkow@13145
  1702
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
nipkow@13145
  1703
by (induct xs) auto
wenzelm@13114
  1704
krauss@23971
  1705
lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
nipkow@13145
  1706
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1707
nipkow@13913
  1708
lemma takeWhile_eq_all_conv[simp]:
nipkow@13913
  1709
 "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  1710
by(induct xs, auto)
nipkow@13913
  1711
nipkow@13913
  1712
lemma dropWhile_eq_Nil_conv[simp]:
nipkow@13913
  1713
 "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  1714
by(induct xs, auto)
nipkow@13913
  1715
nipkow@13913
  1716
lemma dropWhile_eq_Cons_conv:
nipkow@13913
  1717
 "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"
nipkow@13913
  1718
by(induct xs, auto)
nipkow@13913
  1719
nipkow@17501
  1720
text{* The following two lemmmas could be generalized to an arbitrary
nipkow@17501
  1721
property. *}
nipkow@17501
  1722
nipkow@17501
  1723
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  1724
 takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))"
nipkow@17501
  1725
by(induct xs) (auto simp: takeWhile_tail[where l="[]"])
nipkow@17501
  1726
nipkow@17501
  1727
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  1728
  dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)"
nipkow@17501
  1729
apply(induct xs)
nipkow@17501
  1730
 apply simp
nipkow@17501
  1731
apply auto
nipkow@17501
  1732
apply(subst dropWhile_append2)
nipkow@17501
  1733
apply auto
nipkow@17501
  1734
done
nipkow@17501
  1735
nipkow@18423
  1736
lemma takeWhile_not_last:
nipkow@18423
  1737
 "\<lbrakk> xs \<noteq> []; distinct xs\<rbrakk> \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs"
nipkow@18423
  1738
apply(induct xs)
nipkow@18423
  1739
 apply simp
nipkow@18423
  1740
apply(case_tac xs)
nipkow@18423
  1741
apply(auto)
nipkow@18423
  1742
done
nipkow@18423
  1743
krauss@19770
  1744
lemma takeWhile_cong [fundef_cong, recdef_cong]:
krauss@18336
  1745
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  1746
  ==> takeWhile P l = takeWhile Q k"
nipkow@24349
  1747
by (induct k arbitrary: l) (simp_all)
krauss@18336
  1748
krauss@19770
  1749
lemma dropWhile_cong [fundef_cong, recdef_cong]:
krauss@18336
  1750
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  1751
  ==> dropWhile P l = dropWhile Q k"
nipkow@24349
  1752
by (induct k arbitrary: l, simp_all)
krauss@18336
  1753
wenzelm@13114
  1754
nipkow@15392
  1755
subsubsection {* @{text zip} *}
wenzelm@13114
  1756
wenzelm@13142
  1757
lemma zip_Nil [simp]: "zip [] ys = []"
nipkow@13145
  1758
by (induct ys) auto
wenzelm@13114
  1759
wenzelm@13142
  1760
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
nipkow@13145
  1761
by simp
wenzelm@13114
  1762
wenzelm@13142
  1763
declare zip_Cons [simp del]
wenzelm@13114
  1764
nipkow@15281
  1765
lemma zip_Cons1:
nipkow@15281
  1766
 "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)"
nipkow@15281
  1767
by(auto split:list.split)
nipkow@15281
  1768
wenzelm@13142
  1769
lemma length_zip [simp]:
krauss@22493
  1770
"length (zip xs ys) = min (length xs) (length ys)"
krauss@22493
  1771
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  1772
wenzelm@13114
  1773
lemma zip_append1:
krauss@22493
  1774
"zip (xs @ ys) zs =
nipkow@13145
  1775
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
krauss@22493
  1776
by (induct xs zs rule:list_induct2') auto
wenzelm@13114
  1777
wenzelm@13114
  1778
lemma zip_append2:
krauss@22493
  1779
"zip xs (ys @ zs) =
nipkow@13145
  1780
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
krauss@22493
  1781
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  1782
wenzelm@13142
  1783
lemma zip_append [simp]:
wenzelm@13142
  1784
 "[| length xs = length us; length ys = length vs |] ==>
nipkow@13145
  1785
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
nipkow@13145
  1786
by (simp add: zip_append1)
wenzelm@13114
  1787
wenzelm@13114
  1788
lemma zip_rev:
nipkow@14247
  1789
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
nipkow@14247
  1790
by (induct rule:list_induct2, simp_all)
wenzelm@13114
  1791
nipkow@23096
  1792
lemma map_zip_map:
nipkow@23096
  1793
 "map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)"
nipkow@23096
  1794
apply(induct xs arbitrary:ys) apply simp
nipkow@23096
  1795
apply(case_tac ys)
nipkow@23096
  1796
apply simp_all
nipkow@23096
  1797
done
nipkow@23096
  1798
nipkow@23096
  1799
lemma map_zip_map2:
nipkow@23096
  1800
 "map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)"
nipkow@23096
  1801
apply(induct xs arbitrary:ys) apply simp
nipkow@23096
  1802
apply(case_tac ys)
nipkow@23096
  1803
apply simp_all
nipkow@23096
  1804
done
nipkow@23096
  1805
wenzelm@13142
  1806
lemma nth_zip [simp]:
nipkow@24526
  1807
"[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"
nipkow@24526
  1808
apply (induct ys arbitrary: i xs, simp)
nipkow@13145
  1809
apply (case_tac xs)
nipkow@13145
  1810
 apply (simp_all add: nth.simps split: nat.split)
nipkow@13145
  1811
done
wenzelm@13114
  1812
wenzelm@13114
  1813
lemma set_zip:
nipkow@13145
  1814
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
nipkow@13145
  1815
by (simp add: set_conv_nth cong: rev_conj_cong)
wenzelm@13114
  1816
wenzelm@13114
  1817
lemma zip_update:
nipkow@13145
  1818
"length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
nipkow@13145
  1819
by (rule sym, simp add: update_zip)
wenzelm@13114
  1820
wenzelm@13142
  1821
lemma zip_replicate [simp]:
nipkow@24526
  1822
  "zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
nipkow@24526
  1823
apply (induct i arbitrary: j, auto)
paulson@14208
  1824
apply (case_tac j, auto)
nipkow@13145
  1825
done
wenzelm@13114
  1826
nipkow@19487
  1827
lemma take_zip:
nipkow@24526
  1828
  "take n (zip xs ys) = zip (take n xs) (take n ys)"
nipkow@24526
  1829
apply (induct n arbitrary: xs ys)
nipkow@19487
  1830
 apply simp
nipkow@19487
  1831
apply (case_tac xs, simp)
nipkow@19487
  1832
apply (case_tac ys, simp_all)
nipkow@19487
  1833
done
nipkow@19487
  1834
nipkow@19487
  1835
lemma drop_zip:
nipkow@24526
  1836
  "drop n (zip xs ys) = zip (drop n xs) (drop n ys)"
nipkow@24526
  1837
apply (induct n arbitrary: xs ys)
nipkow@19487
  1838
 apply simp
nipkow@19487
  1839
apply (case_tac xs, simp)
nipkow@19487
  1840
apply (case_tac ys, simp_all)
nipkow@19487
  1841
done
nipkow@19487
  1842
krauss@22493
  1843
lemma set_zip_leftD:
krauss@22493
  1844
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs"
krauss@22493
  1845
by (induct xs ys rule:list_induct2') auto
krauss@22493
  1846
krauss@22493
  1847
lemma set_zip_rightD:
krauss@22493
  1848
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys"
krauss@22493
  1849
by (induct xs ys rule:list_induct2') auto
wenzelm@13142
  1850
nipkow@23983
  1851
lemma in_set_zipE:
nipkow@23983
  1852
  "(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23983
  1853
by(blast dest: set_zip_leftD set_zip_rightD)
nipkow@23983
  1854
haftmann@29766
  1855
lemma zip_map_fst_snd:
haftmann@29766
  1856
  "zip (map fst zs) (map snd zs) = zs"
haftmann@29766
  1857
  by (induct zs) simp_all
haftmann@29766
  1858
haftmann@29766
  1859
lemma zip_eq_conv:
haftmann@29766
  1860
  "length xs = length ys \<Longrightarrow> zip xs ys = zs \<longleftrightarrow> map fst zs = xs \<and> map snd zs = ys"
haftmann@29766
  1861
  by (auto simp add: zip_map_fst_snd)
haftmann@29766
  1862
haftmann@29766
  1863
nipkow@15392
  1864
subsubsection {* @{text list_all2} *}
wenzelm@13114
  1865
kleing@14316
  1866
lemma list_all2_lengthD [intro?]: 
kleing@14316
  1867
  "list_all2 P xs ys ==> length xs = length ys"
nipkow@24349
  1868
by (simp add: list_all2_def)
haftmann@19607
  1869
haftmann@19787
  1870
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])"
nipkow@24349
  1871
by (simp add: list_all2_def)
haftmann@19607
  1872
haftmann@19787
  1873
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])"
nipkow@24349
  1874
by (simp add: list_all2_def)
haftmann@19607
  1875
haftmann@19607
  1876
lemma list_all2_Cons [iff, code]:
haftmann@19607
  1877
  "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
nipkow@24349
  1878
by (auto simp add: list_all2_def)
wenzelm@13114
  1879
wenzelm@13114
  1880
lemma list_all2_Cons1:
nipkow@13145
  1881
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
nipkow@13145
  1882
by (cases ys) auto
wenzelm@13114
  1883
wenzelm@13114
  1884
lemma list_all2_Cons2:
nipkow@13145
  1885
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
nipkow@13145
  1886
by (cases xs) auto
wenzelm@13114
  1887
wenzelm@13142
  1888
lemma list_all2_rev [iff]:
nipkow@13145
  1889
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
nipkow@13145
  1890
by (simp add: list_all2_def zip_rev cong: conj_cong)
wenzelm@13114
  1891
kleing@13863
  1892
lemma list_all2_rev1:
kleing@13863
  1893
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"
kleing@13863
  1894
by (subst list_all2_rev [symmetric]) simp
kleing@13863
  1895
wenzelm@13114
  1896
lemma list_all2_append1:
nipkow@13145
  1897
"list_all2 P (xs @ ys) zs =
nipkow@13145
  1898
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
nipkow@13145
  1899
list_all2 P xs us \<and> list_all2 P ys vs)"
nipkow@13145
  1900
apply (simp add: list_all2_def zip_append1)
nipkow@13145
  1901
apply (rule iffI)
nipkow@13145
  1902
 apply (rule_tac x = "take (length xs) zs" in exI)
nipkow@13145
  1903
 apply (rule_tac x = "drop (length xs) zs" in exI)
paulson@14208
  1904
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  1905
apply (simp add: ball_Un)
nipkow@13145
  1906
done
wenzelm@13114
  1907
wenzelm@13114
  1908
lemma list_all2_append2:
nipkow@13145
  1909
"list_all2 P xs (ys @ zs) =
nipkow@13145
  1910
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
nipkow@13145
  1911
list_all2 P us ys \<and> list_all2 P vs zs)"
nipkow@13145
  1912
apply (simp add: list_all2_def zip_append2)
nipkow@13145
  1913
apply (rule iffI)
nipkow@13145
  1914
 apply (rule_tac x = "take (length ys) xs" in exI)
nipkow@13145
  1915
 apply (rule_tac x = "drop (length ys) xs" in exI)
paulson@14208
  1916
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  1917
apply (simp add: ball_Un)
nipkow@13145
  1918
done
wenzelm@13114
  1919
kleing@13863
  1920
lemma list_all2_append:
nipkow@14247
  1921
  "length xs = length ys \<Longrightarrow>
nipkow@14247
  1922
  list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"
nipkow@14247
  1923
by (induct rule:list_induct2, simp_all)
kleing@13863
  1924
kleing@13863
  1925
lemma list_all2_appendI [intro?, trans]:
kleing@13863
  1926
  "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"
nipkow@24349
  1927
by (simp add: list_all2_append list_all2_lengthD)
kleing@13863
  1928
wenzelm@13114
  1929
lemma list_all2_conv_all_nth:
nipkow@13145
  1930
"list_all2 P xs ys =
nipkow@13145
  1931
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
nipkow@13145
  1932
by (force simp add: list_all2_def set_zip)
wenzelm@13114
  1933
berghofe@13883
  1934
lemma list_all2_trans:
berghofe@13883
  1935
  assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"
berghofe@13883
  1936
  shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"
berghofe@13883
  1937
        (is "!!bs cs. PROP ?Q as bs cs")
berghofe@13883
  1938
proof (induct as)
berghofe@13883
  1939
  fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"
berghofe@13883
  1940
  show "!!cs. PROP ?Q (x # xs) bs cs"
berghofe@13883
  1941
  proof (induct bs)
berghofe@13883
  1942
    fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs"
berghofe@13883
  1943
    show "PROP ?Q (x # xs) (y # ys) cs"
berghofe@13883
  1944
      by (induct cs) (auto intro: tr I1 I2)
berghofe@13883
  1945
  qed simp
berghofe@13883
  1946
qed simp
berghofe@13883
  1947
kleing@13863
  1948
lemma list_all2_all_nthI [intro?]:
kleing@13863
  1949
  "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"
nipkow@24349
  1950
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1951
paulson@14395
  1952
lemma list_all2I:
paulson@14395
  1953
  "\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b"
nipkow@24349
  1954
by (simp add: list_all2_def)
paulson@14395
  1955
kleing@14328
  1956
lemma list_all2_nthD:
kleing@13863
  1957
  "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  1958
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1959
nipkow@14302
  1960
lemma list_all2_nthD2:
nipkow@14302
  1961
  "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  1962
by (frule list_all2_lengthD) (auto intro: list_all2_nthD)
nipkow@14302
  1963
kleing@13863
  1964
lemma list_all2_map1: 
kleing@13863
  1965
  "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"
nipkow@24349
  1966
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1967
kleing@13863
  1968
lemma list_all2_map2: 
kleing@13863
  1969
  "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"
nipkow@24349
  1970
by (auto simp add: list_all2_conv_all_nth)
kleing@13863
  1971
kleing@14316
  1972
lemma list_all2_refl [intro?]:
kleing@13863
  1973
  "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"
nipkow@24349
  1974
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1975
kleing@13863
  1976
lemma list_all2_update_cong:
kleing@13863
  1977
  "\<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
  1978
by (simp add: list_all2_conv_all_nth nth_list_update)
kleing@13863
  1979
kleing@13863
  1980
lemma list_all2_update_cong2:
kleing@13863
  1981
  "\<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
  1982
by (simp add: list_all2_lengthD list_all2_update_cong)
kleing@13863
  1983
nipkow@14302
  1984
lemma list_all2_takeI [simp,intro?]:
nipkow@24526
  1985
  "list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)"
nipkow@24526
  1986
apply (induct xs arbitrary: n ys)
nipkow@24526
  1987
 apply simp
nipkow@24526
  1988
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  1989
apply (case_tac n)
nipkow@24526
  1990
apply auto
nipkow@24526
  1991
done
nipkow@14302
  1992
nipkow@14302
  1993
lemma list_all2_dropI [simp,intro?]:
nipkow@24526
  1994
  "list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)"
nipkow@24526
  1995
apply (induct as arbitrary: n bs, simp)
nipkow@24526
  1996
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  1997
apply (case_tac n, simp, simp)
nipkow@24526
  1998
done
kleing@13863
  1999
kleing@14327
  2000
lemma list_all2_mono [intro?]:
nipkow@24526
  2001
  "list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys"
nipkow@24526
  2002
apply (induct xs arbitrary: ys, simp)
nipkow@24526
  2003
apply (case_tac ys, auto)
nipkow@24526
  2004
done
kleing@13863
  2005
haftmann@22551
  2006
lemma list_all2_eq:
haftmann@22551
  2007
  "xs = ys \<longleftrightarrow> list_all2 (op =) xs ys"
nipkow@24349
  2008
by (induct xs ys rule: list_induct2') auto
haftmann@22551
  2009
wenzelm@13114
  2010
nipkow@15392
  2011
subsubsection {* @{text foldl} and @{text foldr} *}
wenzelm@13114
  2012
wenzelm@13142
  2013
lemma foldl_append [simp]:
nipkow@24526
  2014
  "foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
nipkow@24526
  2015
by (induct xs arbitrary: a) auto
wenzelm@13114
  2016
nipkow@14402
  2017
lemma foldr_append[simp]: "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)"
nipkow@14402
  2018
by (induct xs) auto
nipkow@14402
  2019
nipkow@23096
  2020
lemma foldr_map: "foldr g (map f xs) a = foldr (g o f) xs a"
nipkow@23096
  2021
by(induct xs) simp_all
nipkow@23096
  2022
nipkow@24449
  2023
text{* For efficient code generation: avoid intermediate list. *}
nipkow@24449
  2024
lemma foldl_map[code unfold]:
nipkow@24449
  2025
  "foldl g a (map f xs) = foldl (%a x. g a (f x)) a xs"
nipkow@23096
  2026
by(induct xs arbitrary:a) simp_all
nipkow@23096
  2027
krauss@19770
  2028
lemma foldl_cong [fundef_cong, recdef_cong]:
krauss@18336
  2029
  "[| a = b; l = k; !!a x. x : set l ==> f a x = g a x |] 
krauss@18336
  2030
  ==> foldl f a l = foldl g b k"
nipkow@24349
  2031
by (induct k arbitrary: a b l) simp_all
krauss@18336
  2032
krauss@19770
  2033
lemma foldr_cong [fundef_cong, recdef_cong]:
krauss@18336
  2034
  "[| a = b; l = k; !!a x. x : set l ==> f x a = g x a |] 
krauss@18336
  2035
  ==> foldr f l a = foldr g k b"
nipkow@24349
  2036
by (induct k arbitrary: a b l) simp_all
krauss@18336
  2037
nipkow@24449
  2038
lemma (in semigroup_add) foldl_assoc:
haftmann@25062
  2039
shows "foldl op+ (x+y) zs = x + (foldl op+ y zs)"
nipkow@24449
  2040
by (induct zs arbitrary: y) (simp_all add:add_assoc)
nipkow@24449
  2041
nipkow@24449
  2042
lemma (in monoid_add) foldl_absorb0:
haftmann@25062
  2043
shows "x + (foldl op+ 0 zs) = foldl op+ x zs"
nipkow@24449
  2044
by (induct zs) (simp_all add:foldl_assoc)
nipkow@24449
  2045
nipkow@24449
  2046
nipkow@23096
  2047
text{* The ``First Duality Theorem'' in Bird \& Wadler: *}
nipkow@23096
  2048
nipkow@23096
  2049
lemma foldl_foldr1_lemma:
nipkow@23096
  2050
 "foldl op + a xs = a + foldr op + xs (0\<Colon>'a::monoid_add)"
nipkow@23096
  2051
by (induct xs arbitrary: a) (auto simp:add_assoc)
nipkow@23096
  2052
nipkow@23096
  2053
corollary foldl_foldr1:
nipkow@23096
  2054
 "foldl op + 0 xs = foldr op + xs (0\<Colon>'a::monoid_add)"
nipkow@23096
  2055
by (simp add:foldl_foldr1_lemma)
nipkow@23096
  2056
nipkow@23096
  2057
nipkow@23096
  2058
text{* The ``Third Duality Theorem'' in Bird \& Wadler: *}
nipkow@23096
  2059
nipkow@14402
  2060
lemma foldr_foldl: "foldr f xs a = foldl (%x y. f y x) a (rev xs)"
nipkow@14402
  2061
by (induct xs) auto
nipkow@14402
  2062
nipkow@14402
  2063
lemma foldl_foldr: "foldl f a xs = foldr (%x y. f y x) (rev xs) a"
nipkow@14402
  2064
by (simp add: foldr_foldl [of "%x y. f y x" "rev xs"])
nipkow@14402
  2065
haftmann@25062
  2066
lemma (in ab_semigroup_add) foldr_conv_foldl: "foldr op + xs a = foldl op + a xs"
chaieb@24471
  2067
  by (induct xs, auto simp add: foldl_assoc add_commute)
chaieb@24471
  2068
wenzelm@13142
  2069
text {*
nipkow@13145
  2070
Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
nipkow@13145
  2071
difficult to use because it requires an additional transitivity step.
wenzelm@13142
  2072
*}
wenzelm@13114
  2073
nipkow@24526
  2074
lemma start_le_sum: "(m::nat) <= n ==> m <= foldl (op +) n ns"
nipkow@24526
  2075
by (induct ns arbitrary: n) auto
nipkow@24526
  2076
nipkow@24526
  2077
lemma elem_le_sum: "(n::nat) : set ns ==> n <= foldl (op +) 0 ns"
nipkow@13145
  2078
by (force intro: start_le_sum simp add: in_set_conv_decomp)
wenzelm@13114
  2079
wenzelm@13142
  2080
lemma sum_eq_0_conv [iff]:
nipkow@24526
  2081
  "(foldl (op +) (m::nat) ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
nipkow@24526
  2082
by (induct ns arbitrary: m) auto
wenzelm@13114
  2083
chaieb@24471
  2084
lemma foldr_invariant: 
chaieb@24471
  2085
  "\<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
  2086
  by (induct xs, simp_all)
chaieb@24471
  2087
chaieb@24471
  2088
lemma foldl_invariant: 
chaieb@24471
  2089
  "\<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
  2090
  by (induct xs arbitrary: x, simp_all)
chaieb@24471
  2091
nipkow@24449
  2092
text{* @{const foldl} and @{text concat} *}
nipkow@24449
  2093
nipkow@24449
  2094
lemma foldl_conv_concat:
haftmann@29719
  2095
  "foldl (op @) xs xss = xs @ concat xss"
haftmann@29719
  2096
proof (induct xss arbitrary: xs)
haftmann@29719
  2097
  case Nil show ?case by simp
haftmann@29719
  2098
next
haftmann@29719
  2099
  interpret monoid_add "[]" "op @" proof qed simp_all
haftmann@29719
  2100
  case Cons then show ?case by (simp add: foldl_absorb0)
haftmann@29719
  2101
qed
haftmann@29719
  2102
haftmann@29719
  2103
lemma concat_conv_foldl: "concat xss = foldl (op @) [] xss"
haftmann@29719
  2104
  by (simp add: foldl_conv_concat)
haftmann@29719
  2105
nipkow@24449
  2106
nipkow@23096
  2107
subsubsection {* List summation: @{const listsum} and @{text"\<Sum>"}*}
nipkow@23096
  2108
haftmann@26442
  2109
lemma listsum_append [simp]: "listsum (xs @ ys) = listsum xs + listsum ys"
nipkow@24449
  2110
by (induct xs) (simp_all add:add_assoc)
nipkow@24449
  2111
haftmann@26442
  2112
lemma listsum_rev [simp]:
haftmann@26442
  2113
  fixes xs :: "'a\<Colon>comm_monoid_add list"
haftmann@26442
  2114
  shows "listsum (rev xs) = listsum xs"
nipkow@24449
  2115
by (induct xs) (simp_all add:add_ac)
nipkow@24449
  2116
haftmann@26442
  2117
lemma listsum_foldr: "listsum xs = foldr (op +) xs 0"
haftmann@26442
  2118
by (induct xs) auto
haftmann@26442
  2119
haftmann@26442
  2120
lemma length_concat: "length (concat xss) = listsum (map length xss)"
haftmann@26442
  2121
by (induct xss) simp_all
nipkow@23096
  2122
nipkow@24449
  2123
text{* For efficient code generation ---
nipkow@24449
  2124
       @{const listsum} is not tail recursive but @{const foldl} is. *}
nipkow@24449
  2125
lemma listsum[code unfold]: "listsum xs = foldl (op +) 0 xs"
nipkow@23096
  2126
by(simp add:listsum_foldr foldl_foldr1)
nipkow@23096
  2127
nipkow@24449
  2128
nipkow@23096
  2129
text{* Some syntactic sugar for summing a function over a list: *}
nipkow@23096
  2130
nipkow@23096
  2131
syntax
nipkow@23096
  2132
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3SUM _<-_. _)" [0, 51, 10] 10)
nipkow@23096
  2133
syntax (xsymbols)
nipkow@23096
  2134
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
nipkow@23096
  2135
syntax (HTML output)
nipkow@23096
  2136
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
nipkow@23096
  2137
nipkow@23096
  2138
translations -- {* Beware of argument permutation! *}
nipkow@23096
  2139
  "SUM x<-xs. b" == "CONST listsum (map (%x. b) xs)"
nipkow@23096
  2140
  "\<Sum>x\<leftarrow>xs. b" == "CONST listsum (map (%x. b) xs)"
nipkow@23096
  2141
haftmann@26442
  2142
lemma listsum_triv: "(\<Sum>x\<leftarrow>xs. r) = of_nat (length xs) * r"
haftmann@26442
  2143
  by (induct xs) (simp_all add: left_distrib)
haftmann@26442
  2144
nipkow@23096
  2145
lemma listsum_0 [simp]: "(\<Sum>x\<leftarrow>xs. 0) = 0"
haftmann@26442
  2146
  by (induct xs) (simp_all add: left_distrib)
nipkow@23096
  2147
nipkow@23096
  2148
text{* For non-Abelian groups @{text xs} needs to be reversed on one side: *}
nipkow@23096
  2149
lemma uminus_listsum_map:
haftmann@26442
  2150
  fixes f :: "'a \<Rightarrow> 'b\<Colon>ab_group_add"
haftmann@26442
  2151
  shows "- listsum (map f xs) = (listsum (map (uminus o f) xs))"
haftmann@26442
  2152
by (induct xs) simp_all
nipkow@23096
  2153
wenzelm@13142
  2154
nipkow@24645
  2155
subsubsection {* @{text upt} *}
wenzelm@13142
  2156
nipkow@17090
  2157
lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])"
nipkow@17090
  2158
-- {* simp does not terminate! *}
nipkow@13145
  2159
by (induct j) auto
wenzelm@13114
  2160
nipkow@15425
  2161
lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []"
nipkow@13145
  2162
by (subst upt_rec) simp
wenzelm@13114
  2163
nipkow@15425
  2164
lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)"
nipkow@15281
  2165
by(induct j)simp_all
nipkow@15281
  2166
nipkow@15281
  2167
lemma upt_eq_Cons_conv:
nipkow@24526
  2168
 "([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)"
nipkow@24526
  2169
apply(induct j arbitrary: x xs)
nipkow@15281
  2170
 apply simp
nipkow@15281
  2171
apply(clarsimp simp add: append_eq_Cons_conv)
nipkow@15281
  2172
apply arith
nipkow@15281
  2173
done
nipkow@15281
  2174
nipkow@15425
  2175
lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]"
nipkow@13145
  2176
-- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
nipkow@13145
  2177
by simp
wenzelm@13114
  2178
nipkow@15425
  2179
lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]"
haftmann@26734
  2180
  by (simp add: upt_rec)
wenzelm@13114
  2181
nipkow@15425
  2182
lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]"
nipkow@13145
  2183
-- {* LOOPS as a simprule, since @{text "j <= j"}. *}
nipkow@13145
  2184
by (induct k) auto
wenzelm@13114
  2185
nipkow@15425
  2186
lemma length_upt [simp]: "length [i..<j] = j - i"
nipkow@13145
  2187
by (induct j) (auto simp add: Suc_diff_le)
wenzelm@13114
  2188
nipkow@15425
  2189
lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k"
nipkow@13145
  2190
apply (induct j)
nipkow@13145
  2191
apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
nipkow@13145
  2192
done
wenzelm@13114
  2193
nipkow@17906
  2194
nipkow@17906
  2195
lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i"
nipkow@17906
  2196
by(simp add:upt_conv_Cons)
nipkow@17906
  2197
nipkow@17906
  2198
lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1"
nipkow@17906
  2199
apply(cases j)
nipkow@17906
  2200
 apply simp
nipkow@17906
  2201
by(simp add:upt_Suc_append)
nipkow@17906
  2202
nipkow@24526
  2203
lemma take_upt [simp]: "i+m <= n ==> take m [i..<n] = [i..<i+m]"
nipkow@24526
  2204
apply (induct m arbitrary: i, simp)
nipkow@13145
  2205
apply (subst upt_rec)
nipkow@13145
  2206
apply (rule sym)
nipkow@13145
  2207
apply (subst upt_rec)
nipkow@13145
  2208
apply (simp del: upt.simps)
nipkow@13145
  2209
done
wenzelm@13114
  2210
nipkow@17501
  2211
lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]"
nipkow@17501
  2212
apply(induct j)
nipkow@17501
  2213
apply auto
nipkow@17501
  2214
done
nipkow@17501
  2215
nipkow@24645
  2216
lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]"
nipkow@13145
  2217
by (induct n) auto
wenzelm@13114
  2218
nipkow@24526
  2219
lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)"
nipkow@24526
  2220
apply (induct n m  arbitrary: i rule: diff_induct)
nipkow@13145
  2221
prefer 3 apply (subst map_Suc_upt[symmetric])
nipkow@13145
  2222
apply (auto simp add: less_diff_conv nth_upt)
nipkow@13145
  2223
done
wenzelm@13114
  2224
berghofe@13883
  2225
lemma nth_take_lemma:
nipkow@24526
  2226
  "k <= length xs ==> k <= length ys ==>
berghofe@13883
  2227
     (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"
nipkow@24526
  2228
apply (atomize, induct k arbitrary: xs ys)
paulson@14208
  2229
apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)
nipkow@13145
  2230
txt {* Both lists must be non-empty *}
paulson@14208
  2231
apply (case_tac xs, simp)
paulson@14208
  2232
apply (case_tac ys, clarify)
nipkow@13145
  2233
 apply (simp (no_asm_use))
nipkow@13145
  2234
apply clarify
nipkow@13145
  2235
txt {* prenexing's needed, not miniscoping *}
nipkow@13145
  2236
apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
nipkow@13145
  2237
apply blast
nipkow@13145
  2238
done
wenzelm@13114
  2239
wenzelm@13114
  2240
lemma nth_equalityI:
wenzelm@13114
  2241
 "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
nipkow@13145
  2242
apply (frule nth_take_lemma [OF le_refl eq_imp_le])
nipkow@13145
  2243
apply (simp_all add: take_all)
nipkow@13145
  2244
done
wenzelm@13114
  2245
haftmann@24796
  2246
lemma map_nth:
haftmann@24796
  2247
  "map (\<lambda>i. xs ! i) [0..<length xs] = xs"
haftmann@24796
  2248
  by (rule nth_equalityI, auto)
haftmann@24796
  2249
kleing@13863
  2250
(* needs nth_equalityI *)
kleing@13863
  2251
lemma list_all2_antisym:
kleing@13863
  2252
  "\<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
  2253
  \<Longrightarrow> xs = ys"
kleing@13863
  2254
  apply (simp add: list_all2_conv_all_nth) 
paulson@14208
  2255
  apply (rule nth_equalityI, blast, simp)
kleing@13863
  2256
  done
kleing@13863
  2257
wenzelm@13142
  2258
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
nipkow@13145
  2259
-- {* The famous take-lemma. *}
nipkow@13145
  2260
apply (drule_tac x = "max (length xs) (length ys)" in spec)
nipkow@13145
  2261
apply (simp add: le_max_iff_disj take_all)
nipkow@13145
  2262
done
wenzelm@13114
  2263
wenzelm@13114
  2264
nipkow@15302
  2265
lemma take_Cons':
nipkow@15302
  2266
     "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
nipkow@15302
  2267
by (cases n) simp_all
nipkow@15302
  2268
nipkow@15302
  2269
lemma drop_Cons':
nipkow@15302
  2270
     "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
nipkow@15302
  2271
by (cases n) simp_all
nipkow@15302
  2272
nipkow@15302
  2273
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
nipkow@15302
  2274
by (cases n) simp_all
nipkow@15302
  2275
paulson@18622
  2276
lemmas take_Cons_number_of = take_Cons'[of "number_of v",standard]
paulson@18622
  2277
lemmas drop_Cons_number_of = drop_Cons'[of "number_of v",standard]
paulson@18622
  2278
lemmas nth_Cons_number_of = nth_Cons'[of _ _ "number_of v",standard]
paulson@18622
  2279
paulson@18622
  2280
declare take_Cons_number_of [simp] 
paulson@18622
  2281
        drop_Cons_number_of [simp] 
paulson@18622
  2282
        nth_Cons_number_of [simp] 
nipkow@15302
  2283
nipkow@15302
  2284
nipkow@15392
  2285
subsubsection {* @{text "distinct"} and @{text remdups} *}
wenzelm@13114
  2286
wenzelm@13142
  2287
lemma distinct_append [simp]:
nipkow@13145
  2288
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
nipkow@13145
  2289
by (induct xs) auto
wenzelm@13114
  2290
nipkow@15305
  2291
lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs"
nipkow@15305
  2292
by(induct xs) auto
nipkow@15305
  2293
wenzelm@13142
  2294
lemma set_remdups [simp]: "set (remdups xs) = set xs"
nipkow@13145
  2295
by (induct xs) (auto simp add: insert_absorb)
wenzelm@13114
  2296
wenzelm@13142
  2297
lemma distinct_remdups [iff]: "distinct (remdups xs)"
nipkow@13145
  2298
by (induct xs) auto
wenzelm@13114
  2299
nipkow@25287
  2300
lemma distinct_remdups_id: "distinct xs ==> remdups xs = xs"
nipkow@25287
  2301
by (induct xs, auto)
nipkow@25287
  2302
haftmann@26734
  2303
lemma remdups_id_iff_distinct [simp]: "remdups xs = xs \<longleftrightarrow> distinct xs"
haftmann@26734
  2304
by (metis distinct_remdups distinct_remdups_id)
nipkow@25287
  2305
nipkow@24566
  2306
lemma finite_distinct_list: "finite A \<Longrightarrow> EX xs. set xs = A & distinct xs"
paulson@24632
  2307
by (metis distinct_remdups finite_list set_remdups)
nipkow@24566
  2308
paulson@15072
  2309
lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])"
nipkow@24349
  2310
by (induct x, auto) 
paulson@15072
  2311
paulson@15072
  2312
lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])"
nipkow@24349
  2313
by (induct x, auto)
paulson@15072
  2314
nipkow@15245
  2315
lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs"
nipkow@15245
  2316
by (induct xs) auto
nipkow@15245
  2317
nipkow@15245
  2318
lemma length_remdups_eq[iff]:
nipkow@15245
  2319
  "(length (remdups xs) = length xs) = (remdups xs = xs)"
nipkow@15245
  2320
apply(induct xs)
nipkow@15245
  2321
 apply auto
nipkow@15245
  2322
apply(subgoal_tac "length (remdups xs) <= length xs")
nipkow@15245
  2323
 apply arith
nipkow@15245
  2324
apply(rule length_remdups_leq)
nipkow@15245
  2325
done
nipkow@15245
  2326
nipkow@18490
  2327
nipkow@18490
  2328
lemma distinct_map:
nipkow@18490
  2329
  "distinct(map f xs) = (distinct xs & inj_on f (set xs))"
nipkow@18490
  2330
by (induct xs) auto
nipkow@18490
  2331
nipkow@18490
  2332
wenzelm@13142
  2333
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"
nipkow@13145
  2334
by (induct xs) auto
wenzelm@13114
  2335
nipkow@17501
  2336
lemma distinct_upt[simp]: "distinct[i..<j]"
nipkow@17501
  2337
by (induct j) auto
nipkow@17501
  2338
nipkow@24526
  2339
lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)"
nipkow@24526
  2340
apply(induct xs arbitrary: i)
nipkow@17501
  2341
 apply simp
nipkow@17501
  2342
apply (case_tac i)
nipkow@17501
  2343
 apply simp_all
nipkow@17501
  2344
apply(blast dest:in_set_takeD)
nipkow@17501
  2345
done
nipkow@17501
  2346
nipkow@24526
  2347
lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)"
nipkow@24526
  2348
apply(induct xs arbitrary: i)
nipkow@17501
  2349
 apply simp
nipkow@17501
  2350
apply (case_tac i)
nipkow@17501
  2351
 apply simp_all
nipkow@17501
  2352
done
nipkow@17501
  2353
nipkow@17501
  2354
lemma distinct_list_update:
nipkow@17501
  2355
assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}"
nipkow@17501
  2356
shows "distinct (xs[i:=a])"
nipkow@17501
  2357
proof (cases "i < length xs")
nipkow@17501
  2358
  case True
nipkow@17501
  2359
  with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}"
nipkow@17501
  2360
    apply (drule_tac id_take_nth_drop) by simp
nipkow@17501
  2361
  with d True show ?thesis
nipkow@17501
  2362
    apply (simp add: upd_conv_take_nth_drop)
nipkow@17501
  2363
    apply (drule subst [OF id_take_nth_drop]) apply assumption
nipkow@17501
  2364
    apply simp apply (cases "a = xs!i") apply simp by blast
nipkow@17501
  2365
next
nipkow@17501
  2366
  case False with d show ?thesis by auto
nipkow@17501
  2367
qed
nipkow@17501
  2368
nipkow@17501
  2369
nipkow@17501
  2370
text {* It is best to avoid this indexed version of distinct, but
nipkow@17501
  2371
sometimes it is useful. *}
nipkow@17501
  2372
nipkow@13124
  2373
lemma distinct_conv_nth:
nipkow@17501
  2374
"distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)"
paulson@15251
  2375
apply (induct xs, simp, simp)
paulson@14208
  2376
apply (rule iffI, clarsimp)
nipkow@13145
  2377
 apply (case_tac i)
paulson@14208
  2378
apply (case_tac j, simp)
nipkow@13145
  2379
apply (simp add: set_conv_nth)
nipkow@13145
  2380
 apply (case_tac j)
paulson@24648
  2381
apply (clarsimp simp add: set_conv_nth, simp) 
nipkow@13145
  2382
apply (rule conjI)
paulson@24648
  2383
(*TOO SLOW
paulson@24632
  2384
apply (metis Zero_neq_Suc gr0_conv_Suc in_set_conv_nth lessI less_trans_Suc nth_Cons' nth_Cons_Suc)
paulson@24648
  2385
*)
paulson@24648
  2386
 apply (clarsimp simp add: set_conv_nth)
paulson@24648
  2387
 apply (erule_tac x = 0 in allE, simp)
paulson@24648
  2388
 apply (erule_tac x = "Suc i" in allE, simp, clarsimp)
wenzelm@25130
  2389
(*TOO SLOW
paulson@24632
  2390
apply (metis Suc_Suc_eq lessI less_trans_Suc nth_Cons_Suc)
wenzelm@25130
  2391
*)
wenzelm@25130
  2392
apply (erule_tac x = "Suc i" in allE, simp)
wenzelm@25130
  2393
apply (erule_tac x = "Suc j" in allE, simp)
nipkow@13145
  2394
done
nipkow@13124
  2395
nipkow@18490
  2396
lemma nth_eq_iff_index_eq:
nipkow@18490
  2397
 "\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)"
nipkow@18490
  2398
by(auto simp: distinct_conv_nth)
nipkow@18490
  2399
nipkow@15110
  2400
lemma distinct_card: "distinct xs ==> card (set xs) = size xs"
nipkow@24349
  2401
by (induct xs) auto
kleing@14388
  2402
nipkow@15110
  2403
lemma card_distinct: "card (set xs) = size xs ==> distinct xs"
kleing@14388
  2404
proof (induct xs)
kleing@14388
  2405
  case Nil thus ?case by simp
kleing@14388
  2406
next
kleing@14388
  2407
  case (Cons x xs)
kleing@14388
  2408
  show ?case
kleing@14388
  2409
  proof (cases "x \<in> set xs")
kleing@14388
  2410
    case False with Cons show ?thesis by simp
kleing@14388
  2411
  next
kleing@14388
  2412
    case True with Cons.prems
kleing@14388
  2413
    have "card (set xs) = Suc (length xs)" 
kleing@14388
  2414
      by (simp add: card_insert_if split: split_if_asm)
kleing@14388
  2415
    moreover have "card (set xs) \<le> length xs" by (rule card_length)
kleing@14388
  2416
    ultimately have False by simp
kleing@14388
  2417
    thus ?thesis ..
kleing@14388
  2418
  qed
kleing@14388
  2419
qed
kleing@14388
  2420
nipkow@25287
  2421
lemma not_distinct_decomp: "~ distinct ws ==> EX xs ys zs y. ws = xs@[y]@ys@[y]@zs"
nipkow@25287
  2422
apply (induct n == "length ws" arbitrary:ws) apply simp
nipkow@25287
  2423
apply(case_tac ws) apply simp
nipkow@25287
  2424
apply (simp split:split_if_asm)
nipkow@25287
  2425
apply (metis Cons_eq_appendI eq_Nil_appendI split_list)
nipkow@25287
  2426
done
nipkow@18490
  2427
nipkow@18490
  2428
lemma length_remdups_concat:
nipkow@18490
  2429
 "length(remdups(concat xss)) = card(\<Union>xs \<in> set xss. set xs)"
nipkow@24308
  2430
by(simp add: set_concat distinct_card[symmetric])
nipkow@17906
  2431
nipkow@17906
  2432
nipkow@15392
  2433
subsubsection {* @{text remove1} *}
nipkow@15110
  2434
nipkow@18049
  2435
lemma remove1_append:
nipkow@18049
  2436
  "remove1 x (xs @ ys) =
nipkow@18049
  2437
  (if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)"
nipkow@18049
  2438
by (induct xs) auto
nipkow@18049
  2439
nipkow@23479
  2440
lemma in_set_remove1[simp]:
nipkow@23479
  2441
  "a \<noteq> b \<Longrightarrow> a : set(remove1 b xs) = (a : set xs)"
nipkow@23479
  2442
apply (induct xs)
nipkow@23479
  2443
apply auto
nipkow@23479
  2444
done
nipkow@23479
  2445
nipkow@15110
  2446
lemma set_remove1_subset: "set(remove1 x xs) <= set xs"
nipkow@15110
  2447
apply(induct xs)
nipkow@15110
  2448
 apply simp
nipkow@15110
  2449
apply simp
nipkow@15110
  2450
apply blast
nipkow@15110
  2451
done
nipkow@15110
  2452
paulson@17724
  2453
lemma set_remove1_eq [simp]: "distinct xs ==> set(remove1 x xs) = set xs - {x}"
nipkow@15110
  2454
apply(induct xs)
nipkow@15110
  2455
 apply simp
nipkow@15110
  2456
apply simp
nipkow@15110
  2457
apply blast
nipkow@15110
  2458
done
nipkow@15110
  2459
nipkow@23479
  2460
lemma length_remove1:
nipkow@23479
  2461
  "length(remove1 x xs) = (if x : set xs then length xs - 1 else length xs)"
nipkow@23479
  2462
apply (induct xs)
nipkow@23479
  2463
 apply (auto dest!:length_pos_if_in_set)
nipkow@23479
  2464
done
nipkow@23479
  2465
nipkow@18049
  2466
lemma remove1_filter_not[simp]:
nipkow@18049
  2467
  "\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs"
nipkow@18049
  2468
by(induct xs) auto
nipkow@18049
  2469
nipkow@15110
  2470
lemma notin_set_remove1[simp]: "x ~: set xs ==> x ~: set(remove1 y xs)"
nipkow@15110
  2471
apply(insert set_remove1_subset)
nipkow@15110
  2472
apply fast
nipkow@15110
  2473
done
nipkow@15110
  2474
nipkow@15110
  2475
lemma distinct_remove1[simp]: "distinct xs ==> distinct(remove1 x xs)"
nipkow@15110
  2476
by (induct xs) simp_all
nipkow@15110
  2477
wenzelm@13114
  2478
nipkow@27693
  2479
subsubsection {* @{text removeAll} *}
nipkow@27693
  2480
nipkow@27693
  2481
lemma removeAll_append[simp]:
nipkow@27693
  2482
  "removeAll x (xs @ ys) = removeAll x xs @ removeAll x ys"
nipkow@27693
  2483
by (induct xs) auto
nipkow@27693
  2484
nipkow@27693
  2485
lemma set_removeAll[simp]: "set(removeAll x xs) = set xs - {x}"
nipkow@27693
  2486
by (induct xs) auto
nipkow@27693
  2487
nipkow@27693
  2488
lemma removeAll_id[simp]: "x \<notin> set xs \<Longrightarrow> removeAll x xs = xs"
nipkow@27693
  2489
by (induct xs) auto
nipkow@27693
  2490
nipkow@27693
  2491
(* Needs count:: 'a \<Rightarrow> a' list \<Rightarrow> nat
nipkow@27693
  2492
lemma length_removeAll:
nipkow@27693
  2493
  "length(removeAll x xs) = length xs - count x xs"
nipkow@27693
  2494
*)
nipkow@27693
  2495
nipkow@27693
  2496
lemma removeAll_filter_not[simp]:
nipkow@27693
  2497
  "\<not> P x \<Longrightarrow> removeAll x (filter P xs) = filter P xs"
nipkow@27693
  2498
by(induct xs) auto
nipkow@27693
  2499
nipkow@27693
  2500
nipkow@27693
  2501
lemma distinct_remove1_removeAll:
nipkow@27693
  2502
  "distinct xs ==> remove1 x xs = removeAll x xs"
nipkow@27693
  2503
by (induct xs) simp_all
nipkow@27693
  2504
nipkow@27693
  2505
lemma map_removeAll_inj_on: "inj_on f (insert x (set xs)) \<Longrightarrow>
nipkow@27693
  2506
  map f (removeAll x xs) = removeAll (f x) (map f xs)"
nipkow@27693
  2507
by (induct xs) (simp_all add:inj_on_def)
nipkow@27693
  2508
nipkow@27693
  2509
lemma map_removeAll_inj: "inj f \<Longrightarrow>
nipkow@27693
  2510
  map f (removeAll x xs) = removeAll (f x) (map f xs)"
nipkow@27693
  2511
by(metis map_removeAll_inj_on subset_inj_on subset_UNIV)
nipkow@27693
  2512
nipkow@27693
  2513
nipkow@15392
  2514
subsubsection {* @{text replicate} *}
wenzelm@13114
  2515
wenzelm@13142
  2516
lemma length_replicate [simp]: "length (replicate n x) = n"
nipkow@13145
  2517
by (induct n) auto
wenzelm@13142
  2518
wenzelm@13142
  2519
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
nipkow@13145
  2520
by (induct n) auto
wenzelm@13114
  2521
wenzelm@13114
  2522
lemma replicate_app_Cons_same:
nipkow@13145
  2523
"(replicate n x) @ (x # xs) = x # replicate n x @ xs"
nipkow@13145
  2524
by (induct n) auto
wenzelm@13114
  2525
wenzelm@13142
  2526
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
paulson@14208
  2527
apply (induct n, simp)
nipkow@13145
  2528
apply (simp add: replicate_app_Cons_same)
nipkow@13145
  2529
done
wenzelm@13114
  2530
wenzelm@13142
  2531
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
nipkow@13145
  2532
by (induct n) auto
wenzelm@13114
  2533
nipkow@16397
  2534
text{* Courtesy of Matthias Daum: *}
nipkow@16397
  2535
lemma append_replicate_commute:
nipkow@16397
  2536
  "replicate n x @ replicate k x = replicate k x @ replicate n x"
nipkow@16397
  2537
apply (simp add: replicate_add [THEN sym])
nipkow@16397
  2538
apply (simp add: add_commute)
nipkow@16397
  2539
done
nipkow@16397
  2540
wenzelm@13142
  2541
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"
nipkow@13145
  2542
by (induct n) auto
wenzelm@13114
  2543
wenzelm@13142
  2544
lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"
nipkow@13145
  2545
by (induct n) auto
wenzelm@13114
  2546
wenzelm@13142
  2547
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"
nipkow@13145
  2548
by (atomize (full), induct n) auto
wenzelm@13114
  2549
nipkow@24526
  2550
lemma nth_replicate[simp]: "i < n ==> (replicate n x)!i = x"
nipkow@24526
  2551
apply (induct n arbitrary: i, simp)
nipkow@13145
  2552
apply (simp add: nth_Cons split: nat.split)
nipkow@13145
  2553
done
wenzelm@13114
  2554
nipkow@16397
  2555
text{* Courtesy of Matthias Daum (2 lemmas): *}
nipkow@16397
  2556
lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x"
nipkow@16397
  2557
apply (case_tac "k \<le> i")
nipkow@16397
  2558
 apply  (simp add: min_def)
nipkow@16397
  2559
apply (drule not_leE)
nipkow@16397
  2560
apply (simp add: min_def)
nipkow@16397
  2561
apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x")
nipkow@16397
  2562
 apply  simp
nipkow@16397
  2563
apply (simp add: replicate_add [symmetric])
nipkow@16397
  2564
done
nipkow@16397
  2565
nipkow@24526
  2566
lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x"
nipkow@24526
  2567
apply (induct k arbitrary: i)
nipkow@16397
  2568
 apply simp
nipkow@16397
  2569
apply clarsimp
nipkow@16397
  2570
apply (case_tac i)
nipkow@16397
  2571
 apply simp
nipkow@16397
  2572
apply clarsimp
nipkow@16397
  2573
done
nipkow@16397
  2574
nipkow@16397
  2575
wenzelm@13142
  2576
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
nipkow@13145
  2577
by (induct n) auto
wenzelm@13114
  2578
wenzelm@13142
  2579
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
nipkow@13145
  2580
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
wenzelm@13114
  2581
wenzelm@13142
  2582
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
nipkow@13145
  2583
by auto
wenzelm@13114
  2584
wenzelm@13142
  2585
lemma in_set_replicateD: "x : set (replicate n y) ==> x = y"
nipkow@13145
  2586
by (simp add: set_replicate_conv_if split: split_if_asm)
wenzelm@13114
  2587
haftmann@24796
  2588
lemma replicate_append_same:
haftmann@24796
  2589
  "replicate i x @ [x] = x # replicate i x"
haftmann@24796
  2590
  by (induct i) simp_all
haftmann@24796
  2591
haftmann@24796
  2592
lemma map_replicate_trivial:
haftmann@24796
  2593
  "map (\<lambda>i. x) [0..<i] = replicate i x"
haftmann@24796
  2594
  by (induct i) (simp_all add: replicate_append_same)
haftmann@24796
  2595
wenzelm@13114
  2596
nipkow@28642
  2597
lemma replicate_empty[simp]: "(replicate n x = []) \<longleftrightarrow> n=0"
nipkow@28642
  2598
by (induct n) auto
nipkow@28642
  2599
nipkow@28642
  2600
lemma empty_replicate[simp]: "([] = replicate n x) \<longleftrightarrow> n=0"
nipkow@28642
  2601
by (induct n) auto
nipkow@28642
  2602
nipkow@28642
  2603
lemma replicate_eq_replicate[simp]:
nipkow@28642
  2604
  "(replicate m x = replicate n y) \<longleftrightarrow> (m=n & (m\<noteq>0 \<longrightarrow> x=y))"
nipkow@28642
  2605
apply(induct m arbitrary: n)
nipkow@28642
  2606
 apply simp
nipkow@28642
  2607
apply(induct_tac n)
nipkow@28642
  2608
apply auto
nipkow@28642
  2609
done
nipkow@28642
  2610
nipkow@28642
  2611
nipkow@15392
  2612
subsubsection{*@{text rotate1} and @{text rotate}*}
nipkow@15302
  2613
nipkow@15302
  2614
lemma rotate_simps[simp]: "rotate1 [] = [] \<and> rotate1 (x#xs) = xs @ [x]"
nipkow@15302
  2615
by(simp add:rotate1_def)
nipkow@15302
  2616
nipkow@15302
  2617
lemma rotate0[simp]: "rotate 0 = id"
nipkow@15302
  2618
by(simp add:rotate_def)
nipkow@15302
  2619
nipkow@15302
  2620
lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)"
nipkow@15302
  2621
by(simp add:rotate_def)
nipkow@15302
  2622
nipkow@15302
  2623
lemma rotate_add:
nipkow@15302
  2624
  "rotate (m+n) = rotate m o rotate n"
nipkow@15302
  2625
by(simp add:rotate_def funpow_add)
nipkow@15302
  2626
nipkow@15302
  2627
lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs"
nipkow@15302
  2628
by(simp add:rotate_add)
nipkow@15302
  2629
nipkow@18049
  2630
lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)"
nipkow@18049
  2631
by(simp add:rotate_def funpow_swap1)
nipkow@18049
  2632
nipkow@15302
  2633
lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs"
nipkow@15302
  2634
by(cases xs) simp_all
nipkow@15302
  2635
nipkow@15302
  2636
lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs"
nipkow@15302
  2637
apply(induct n)
nipkow@15302
  2638
 apply simp
nipkow@15302
  2639
apply (simp add:rotate_def)
nipkow@15302
  2640
done
nipkow@15302
  2641
nipkow@15302
  2642
lemma rotate1_hd_tl: "xs \<noteq> [] \<Longrightarrow> rotate1 xs = tl xs @ [hd xs]"
nipkow@15302
  2643
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2644
nipkow@15302
  2645
lemma rotate_drop_take:
nipkow@15302
  2646
  "rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs"
nipkow@15302
  2647
apply(induct n)
nipkow@15302
  2648
 apply simp
nipkow@15302
  2649
apply(simp add:rotate_def)
nipkow@15302
  2650
apply(cases "xs = []")
nipkow@15302
  2651
 apply (simp)
nipkow@15302
  2652
apply(case_tac "n mod length xs = 0")
nipkow@15302
  2653
 apply(simp add:mod_Suc)
nipkow@15302
  2654
 apply(simp add: rotate1_hd_tl drop_Suc take_Suc)
nipkow@15302
  2655
apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric]
nipkow@15302
  2656
                take_hd_drop linorder_not_le)
nipkow@15302
  2657
done
nipkow@15302
  2658
nipkow@15302
  2659
lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs"
nipkow@15302
  2660
by(simp add:rotate_drop_take)
nipkow@15302
  2661
nipkow@15302
  2662
lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs"
nipkow@15302
  2663
by(simp add:rotate_drop_take)
nipkow@15302
  2664
nipkow@15302
  2665
lemma length_rotate1[simp]: "length(rotate1 xs) = length xs"
nipkow@15302
  2666
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2667
nipkow@24526
  2668
lemma length_rotate[simp]: "length(rotate n xs) = length xs"
nipkow@24526
  2669
by (induct n arbitrary: xs) (simp_all add:rotate_def)
nipkow@15302
  2670
nipkow@15302
  2671
lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs"
nipkow@15302
  2672
by(simp add:rotate1_def split:list.split) blast
nipkow@15302
  2673
nipkow@15302
  2674
lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs"
nipkow@15302
  2675
by (induct n) (simp_all add:rotate_def)
nipkow@15302
  2676
nipkow@15302
  2677
lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)"
nipkow@15302
  2678
by(simp add:rotate_drop_take take_map drop_map)
nipkow@15302
  2679
nipkow@15302
  2680
lemma set_rotate1[simp]: "set(rotate1 xs) = set xs"
nipkow@15302
  2681
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2682
nipkow@15302
  2683
lemma set_rotate[simp]: "set(rotate n xs) = set xs"
nipkow@15302
  2684
by (induct n) (simp_all add:rotate_def)
nipkow@15302
  2685
nipkow@15302
  2686
lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])"
nipkow@15302
  2687
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2688
nipkow@15302
  2689
lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])"
nipkow@15302
  2690
by (induct n) (simp_all add:rotate_def)
nipkow@15302
  2691
nipkow@15439
  2692
lemma rotate_rev:
nipkow@15439
  2693
  "rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)"
nipkow@15439
  2694
apply(simp add:rotate_drop_take rev_drop rev_take)
nipkow@15439
  2695
apply(cases "length xs = 0")
nipkow@15439
  2696
 apply simp
nipkow@15439
  2697
apply(cases "n mod length xs = 0")
nipkow@15439
  2698
 apply simp
nipkow@15439
  2699
apply(simp add:rotate_drop_take rev_drop rev_take)
nipkow@15439
  2700
done
nipkow@15439
  2701
nipkow@18423
  2702
lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)"
nipkow@18423
  2703
apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth)
nipkow@18423
  2704
apply(subgoal_tac "length xs \<noteq> 0")
nipkow@18423
  2705
 prefer 2 apply simp
nipkow@18423
  2706
using mod_less_divisor[of "length xs" n] by arith
nipkow@18423
  2707
nipkow@15302
  2708
nipkow@15392
  2709
subsubsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
nipkow@15302
  2710
nipkow@15302
  2711
lemma sublist_empty [simp]: "sublist xs {} = []"
nipkow@15302
  2712
by (auto simp add: sublist_def)
nipkow@15302
  2713
nipkow@15302
  2714
lemma sublist_nil [simp]: "sublist [] A = []"
nipkow@15302
  2715
by (auto simp add: sublist_def)
nipkow@15302
  2716
nipkow@15302
  2717
lemma length_sublist:
nipkow@15302
  2718
  "length(sublist xs I) = card{i. i < length xs \<and> i : I}"
nipkow@15302
  2719
by(simp add: sublist_def length_filter_conv_card cong:conj_cong)
nipkow@15302
  2720
nipkow@15302
  2721
lemma sublist_shift_lemma_Suc:
nipkow@24526
  2722
  "map fst (filter (%p. P(Suc(snd p))) (zip xs is)) =
nipkow@24526
  2723
   map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))"
nipkow@24526
  2724
apply(induct xs arbitrary: "is")
nipkow@15302
  2725
 apply simp
nipkow@15302
  2726
apply (case_tac "is")
nipkow@15302
  2727
 apply simp
nipkow@15302
  2728
apply simp
nipkow@15302
  2729
done
nipkow@15302
  2730
nipkow@15302
  2731
lemma sublist_shift_lemma:
nipkow@23279
  2732
     "map fst [p<-zip xs [i..<i + length xs] . snd p : A] =
nipkow@23279
  2733
      map fst [p<-zip xs [0..<length xs] . snd p + i : A]"
nipkow@15302
  2734
by (induct xs rule: rev_induct) (simp_all add: add_commute)
nipkow@15302
  2735
nipkow@15302
  2736
lemma sublist_append:
nipkow@15302
  2737
     "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
nipkow@15302
  2738
apply (unfold sublist_def)
nipkow@15302
  2739
apply (induct l' rule: rev_induct, simp)
nipkow@15302
  2740
apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
nipkow@15302
  2741
apply (simp add: add_commute)
nipkow@15302
  2742
done
nipkow@15302
  2743
nipkow@15302
  2744
lemma sublist_Cons:
nipkow@15302
  2745
"sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
nipkow@15302
  2746
apply (induct l rule: rev_induct)
nipkow@15302
  2747
 apply (simp add: sublist_def)
nipkow@15302
  2748
apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)
nipkow@15302
  2749
done
nipkow@15302
  2750
nipkow@24526
  2751
lemma set_sublist: "set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}"
nipkow@24526
  2752
apply(induct xs arbitrary: I)
nipkow@25162
  2753
apply(auto simp: sublist_Cons nth_Cons split:nat.split dest!: gr0_implies_Suc)
nipkow@15302
  2754
done
nipkow@15302
  2755
nipkow@15302
  2756
lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs"
nipkow@15302
  2757
by(auto simp add:set_sublist)
nipkow@15302
  2758
nipkow@15302
  2759
lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)"
nipkow@15302
  2760
by(auto simp add:set_sublist)
nipkow@15302
  2761
nipkow@15302
  2762
lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs"
nipkow@15302
  2763
by(auto simp add:set_sublist)
nipkow@15302
  2764
nipkow@15302
  2765
lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"
nipkow@15302
  2766
by (simp add: sublist_Cons)
nipkow@15302
  2767
nipkow@15302
  2768
nipkow@24526
  2769
lemma distinct_sublistI[simp]: "distinct xs \<Longrightarrow> distinct(sublist xs I)"
nipkow@24526
  2770
apply(induct xs arbitrary: I)
nipkow@15302
  2771
 apply simp
nipkow@15302
  2772
apply(auto simp add:sublist_Cons)
nipkow@15302
  2773
done
nipkow@15302
  2774
nipkow@15302
  2775
nipkow@15302
  2776
lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l"
nipkow@15302
  2777
apply (induct l rule: rev_induct, simp)
nipkow@15302
  2778
apply (simp split: nat_diff_split add: sublist_append)
nipkow@15302
  2779
done
nipkow@15302
  2780
nipkow@24526
  2781
lemma filter_in_sublist:
nipkow@24526
  2782
 "distinct xs \<Longrightarrow> filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s"
nipkow@24526
  2783
proof (induct xs arbitrary: s)
nipkow@17501
  2784
  case Nil thus ?case by simp
nipkow@17501
  2785
next
nipkow@17501
  2786
  case (Cons a xs)
nipkow@17501
  2787
  moreover hence "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto
nipkow@17501
  2788
  ultimately show ?case by(simp add: sublist_Cons cong:filter_cong)
nipkow@17501
  2789
qed
nipkow@17501
  2790
nipkow@15302
  2791
nipkow@19390
  2792
subsubsection {* @{const splice} *}
nipkow@19390
  2793
haftmann@19607
  2794
lemma splice_Nil2 [simp, code]:
nipkow@19390
  2795
 "splice xs [] = xs"
nipkow@19390
  2796
by (cases xs) simp_all
nipkow@19390
  2797
haftmann@19607
  2798
lemma splice_Cons_Cons [simp, code]:
nipkow@19390
  2799
 "splice (x#xs) (y#ys) = x # y # splice xs ys"
nipkow@19390
  2800
by simp
nipkow@19390
  2801
haftmann@19607
  2802
declare splice.simps(2) [simp del, code del]
nipkow@19390
  2803
nipkow@24526
  2804
lemma length_splice[simp]: "length(splice xs ys) = length xs + length ys"
nipkow@24526
  2805
apply(induct xs arbitrary: ys) apply simp
nipkow@22793
  2806
apply(case_tac ys)
nipkow@22793
  2807
 apply auto
nipkow@22793
  2808
done
nipkow@22793
  2809
nipkow@24616
  2810
nipkow@28642
  2811
subsubsection {* Infiniteness *}
nipkow@28642
  2812
nipkow@28642
  2813
lemma finite_maxlen:
nipkow@28642
  2814
  "finite (M::'a list set) ==> EX n. ALL s:M. size s < n"
nipkow@28642
  2815
proof (induct rule: finite.induct)
nipkow@28642
  2816
  case emptyI show ?case by simp
nipkow@28642
  2817
next
nipkow@28642
  2818
  case (insertI M xs)
nipkow@28642
  2819
  then obtain n where "\<forall>s\<in>M. length s < n" by blast
nipkow@28642
  2820
  hence "ALL s:insert xs M. size s < max n (size xs) + 1" by auto
nipkow@28642
  2821
  thus ?case ..
nipkow@28642
  2822
qed
nipkow@28642
  2823
nipkow@28642
  2824
lemma infinite_UNIV_listI: "~ finite(UNIV::'a list set)"
nipkow@28642
  2825
apply(rule notI)
nipkow@28642
  2826
apply(drule finite_maxlen)
nipkow@28642
  2827
apply (metis UNIV_I length_replicate less_not_refl)
nipkow@28642
  2828
done
nipkow@28642
  2829
nipkow@28642
  2830
nipkow@24616
  2831
subsection {*Sorting*}
nipkow@24616
  2832
nipkow@24617
  2833
text{* Currently it is not shown that @{const sort} returns a
nipkow@24617
  2834
permutation of its input because the nicest proof is via multisets,
nipkow@24617
  2835
which are not yet available. Alternatively one could define a function
nipkow@24617
  2836
that counts the number of occurrences of an element in a list and use
nipkow@24617
  2837
that instead of multisets to state the correctness property. *}
nipkow@24617
  2838
nipkow@24616
  2839
context linorder
nipkow@24616
  2840
begin
nipkow@24616
  2841
haftmann@25062
  2842
lemma sorted_Cons: "sorted (x#xs) = (sorted xs & (ALL y:set xs. x <= y))"
nipkow@24616
  2843
apply(induct xs arbitrary: x) apply simp
nipkow@24616
  2844
by simp (blast intro: order_trans)
nipkow@24616
  2845
nipkow@24616
  2846
lemma sorted_append:
haftmann@25062
  2847
  "sorted (xs@ys) = (sorted xs & sorted ys & (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<le>y))"
nipkow@24616
  2848
by (induct xs) (auto simp add:sorted_Cons)
nipkow@24616
  2849
nipkow@24616
  2850
lemma set_insort: "set(insort x xs) = insert x (set xs)"
nipkow@24616
  2851
by (induct xs) auto
nipkow@24616
  2852
nipkow@24617
  2853
lemma set_sort[simp]: "set(sort xs) = set xs"
nipkow@24616
  2854
by (induct xs) (simp_all add:set_insort)
nipkow@24616
  2855
nipkow@24616
  2856
lemma distinct_insort: "distinct (insort x xs) = (x \<notin> set xs \<and> distinct xs)"
nipkow@24616
  2857
by(induct xs)(auto simp:set_insort)
nipkow@24616
  2858
nipkow@24617
  2859
lemma distinct_sort[simp]: "distinct (sort xs) = distinct xs"
nipkow@24616
  2860
by(induct xs)(simp_all add:distinct_insort set_sort)
nipkow@24616
  2861
nipkow@24616
  2862
lemma sorted_insort: "sorted (insort x xs) = sorted xs"
nipkow@24616
  2863
apply (induct xs)
nipkow@24650
  2864
 apply(auto simp:sorted_Cons set_insort)
nipkow@24616
  2865
done
nipkow@24616
  2866
nipkow@24616
  2867
theorem sorted_sort[simp]: "sorted (sort xs)"
nipkow@24616
  2868
by (induct xs) (auto simp:sorted_insort)
nipkow@24616
  2869
bulwahn@26143
  2870
lemma insort_is_Cons: "\<forall>x\<in>set xs. a \<le> x \<Longrightarrow> insort a xs = a # xs"
bulwahn@26143
  2871
by (cases xs) auto
bulwahn@26143
  2872
bulwahn@26143
  2873
lemma sorted_remove1: "sorted xs \<Longrightarrow> sorted (remove1 a xs)"
bulwahn@26143
  2874
by (induct xs, auto simp add: sorted_Cons)
bulwahn@26143
  2875
bulwahn@26143
  2876
lemma insort_remove1: "\<lbrakk> a \<in> set xs; sorted xs \<rbrakk> \<Longrightarrow> insort a (remove1 a xs) = xs"
bulwahn@26143
  2877
by (induct xs, auto simp add: sorted_Cons insort_is_Cons)
bulwahn@26143
  2878
bulwahn@26143
  2879
lemma sorted_remdups[simp]:
bulwahn@26143
  2880
  "sorted l \<Longrightarrow> sorted (remdups l)"
bulwahn@26143
  2881
by (induct l) (auto simp: sorted_Cons)
bulwahn@26143
  2882
nipkow@24645
  2883
lemma sorted_distinct_set_unique:
nipkow@24645
  2884
assumes "sorted xs" "distinct xs" "sorted ys" "distinct ys" "set xs = set ys"
nipkow@24645
  2885
shows "xs = ys"
nipkow@24645
  2886
proof -
haftmann@26734
  2887
  from assms have 1: "length xs = length ys" by (auto dest!: distinct_card)
nipkow@24645
  2888
  from assms show ?thesis
nipkow@24645
  2889
  proof(induct rule:list_induct2[OF 1])
nipkow@24645
  2890
    case 1 show ?case by simp
nipkow@24645
  2891
  next
nipkow@24645
  2892
    case 2 thus ?case by (simp add:sorted_Cons)
nipkow@24645
  2893
       (metis Diff_insert_absorb antisym insertE insert_iff)
nipkow@24645
  2894
  qed
nipkow@24645
  2895
qed
nipkow@24645
  2896
nipkow@24645
  2897
lemma finite_sorted_distinct_unique:
nipkow@24645
  2898
shows "finite A \<Longrightarrow> EX! xs. set xs = A & sorted xs & distinct xs"
nipkow@24645
  2899
apply(drule finite_distinct_list)
nipkow@24645
  2900
apply clarify
nipkow@24645
  2901
apply(rule_tac a="sort xs" in ex1I)
nipkow@24645
  2902
apply (auto simp: sorted_distinct_set_unique)
nipkow@24645
  2903
done
nipkow@24645
  2904
haftmann@29626
  2905
lemma sorted_take:
haftmann@29626
  2906
  "sorted xs \<Longrightarrow> sorted (take n xs)"
haftmann@29626
  2907
proof (induct xs arbitrary: n rule: sorted.induct)
haftmann@29626
  2908
  case 1 show ?case by simp
haftmann@29626
  2909
next
haftmann@29626
  2910
  case 2 show ?case by (cases n) simp_all
haftmann@29626
  2911
next
haftmann@29626
  2912
  case (3 x y xs)
haftmann@29626
  2913
  then have "x \<le> y" by simp
haftmann@29626
  2914
  show ?case proof (cases n)
haftmann@29626
  2915
    case 0 then show ?thesis by simp
haftmann@29626
  2916
  next
haftmann@29626
  2917
    case (Suc m) 
haftmann@29626
  2918
    with 3 have "sorted (take m (y # xs))" by simp
haftmann@29626
  2919
    with Suc  `x \<le> y` show ?thesis by (cases m) simp_all
haftmann@29626
  2920
  qed
haftmann@29626
  2921
qed
haftmann@29626
  2922
haftmann@29626
  2923
lemma sorted_drop:
haftmann@29626
  2924
  "sorted xs \<Longrightarrow> sorted (drop n xs)"
haftmann@29626
  2925
proof (induct xs arbitrary: n rule: sorted.induct)
haftmann@29626
  2926
  case 1 show ?case by simp
haftmann@29626
  2927
next
haftmann@29626
  2928
  case 2 show ?case by (cases n) simp_all
haftmann@29626
  2929
next
haftmann@29626
  2930
  case 3 then show ?case by (cases n) simp_all
haftmann@29626
  2931
qed
haftmann@29626
  2932
haftmann@29626
  2933
nipkow@24616
  2934
end
nipkow@24616
  2935
nipkow@25277
  2936
lemma sorted_upt[simp]: "sorted[i..<j]"
nipkow@25277
  2937
by (induct j) (simp_all add:sorted_append)
nipkow@25277
  2938
nipkow@24616
  2939
nipkow@25069
  2940
subsubsection {* @{text sorted_list_of_set} *}
nipkow@25069
  2941
nipkow@25069
  2942
text{* This function maps (finite) linearly ordered sets to sorted
nipkow@25069
  2943
lists. Warning: in most cases it is not a good idea to convert from
nipkow@25069
  2944
sets to lists but one should convert in the other direction (via
nipkow@25069
  2945
@{const set}). *}
nipkow@25069
  2946
nipkow@25069
  2947
nipkow@25069
  2948
context linorder
nipkow@25069
  2949
begin
nipkow@25069
  2950
nipkow@25069
  2951
definition
nipkow@25069
  2952
 sorted_list_of_set :: "'a set \<Rightarrow> 'a list" where
haftmann@28562
  2953
 [code del]: "sorted_list_of_set A == THE xs. set xs = A & sorted xs & distinct xs"
nipkow@25069
  2954
nipkow@25069
  2955
lemma sorted_list_of_set[simp]: "finite A \<Longrightarrow>
nipkow@25069
  2956
  set(sorted_list_of_set A) = A &
nipkow@25069
  2957
  sorted(sorted_list_of_set A) & distinct(sorted_list_of_set A)"
nipkow@25069
  2958
apply(simp add:sorted_list_of_set_def)
nipkow@25069
  2959
apply(rule the1I2)
nipkow@25069
  2960
 apply(simp_all add: finite_sorted_distinct_unique)
nipkow@25069
  2961
done
nipkow@25069
  2962
nipkow@25069
  2963
lemma sorted_list_of_empty[simp]: "sorted_list_of_set {} = []"
nipkow@25069
  2964
unfolding sorted_list_of_set_def
nipkow@25069
  2965
apply(subst the_equality[of _ "[]"])
nipkow@25069
  2966
apply simp_all
nipkow@25069
  2967
done
nipkow@25069
  2968
nipkow@25069
  2969
end
nipkow@25069
  2970
nipkow@25069
  2971
nipkow@24645
  2972
subsubsection {* @{text upto}: the generic interval-list *}
nipkow@24645
  2973
nipkow@24697
  2974
class finite_intvl_succ = linorder +
nipkow@24697
  2975
fixes successor :: "'a \<Rightarrow> 'a"
nipkow@25069
  2976
assumes finite_intvl: "finite{a..b}"
haftmann@25062
  2977
and successor_incr: "a < successor a"
haftmann@25062
  2978
and ord_discrete: "\<not>(\<exists>x. a < x & x < successor a)"
nipkow@24697
  2979
nipkow@24697
  2980
context finite_intvl_succ
nipkow@24697
  2981
begin
nipkow@24697
  2982
nipkow@24697
  2983
definition
haftmann@25062
  2984
 upto :: "'a \<Rightarrow> 'a \<Rightarrow> 'a list" ("(1[_../_])") where
nipkow@25069
  2985
"upto i j == sorted_list_of_set {i..j}"
nipkow@25069
  2986
nipkow@25069
  2987
lemma upto[simp]: "set[a..b] = {a..b} & sorted[a..b] & distinct[a..b]"
nipkow@25069
  2988
by(simp add:upto_def finite_intvl)
nipkow@24645
  2989
haftmann@25062
  2990
lemma insert_intvl: "i \<le> j \<Longrightarrow> insert i {successor i..j} = {i..j}"
nipkow@24697
  2991
apply(insert successor_incr[of i])
nipkow@24697
  2992
apply(auto simp: atLeastAtMost_def atLeast_def atMost_def)
haftmann@26734
  2993
apply(metis ord_discrete less_le not_le)
nipkow@24645
  2994
done
nipkow@24645
  2995
nipkow@25069
  2996
lemma sorted_list_of_set_rec: "i \<le> j \<Longrightarrow>
nipkow@25069
  2997
  sorted_list_of_set {i..j} = i # sorted_list_of_set {successor i..j}"
nipkow@25069
  2998
apply(simp add:sorted_list_of_set_def upto_def)
nipkow@25069
  2999
apply (rule the1_equality[OF finite_sorted_distinct_unique])
nipkow@25069
  3000
 apply (simp add:finite_intvl)
nipkow@25069
  3001
apply(rule the1I2[OF finite_sorted_distinct_unique])
nipkow@25069
  3002
 apply (simp add:finite_intvl)
nipkow@25069
  3003
apply (simp add: sorted_Cons insert_intvl Ball_def)
nipkow@25069
  3004
apply (metis successor_incr leD less_imp_le order_trans)
nipkow@25069
  3005
done
nipkow@25069
  3006
nipkow@27715
  3007
lemma sorted_list_of_set_rec2: "i \<le> j \<Longrightarrow>
nipkow@27715
  3008
  sorted_list_of_set {i..successor j} =
nipkow@27715
  3009
  sorted_list_of_set {i..j} @ [successor j]"
nipkow@27715
  3010
apply(simp add:sorted_list_of_set_def upto_def)
nipkow@27715
  3011
apply (rule the1_equality[OF finite_sorted_distinct_unique])
nipkow@27715
  3012
 apply (simp add:finite_intvl)
nipkow@27715
  3013
apply(rule the1I2[OF finite_sorted_distinct_unique])
nipkow@27715
  3014
 apply (simp add:finite_intvl)
nipkow@27715
  3015
apply (simp add: sorted_append Ball_def expand_set_eq)
nipkow@27715
  3016
apply(rule conjI)
nipkow@27715
  3017
apply (metis eq_iff leD linear not_leE ord_discrete order_trans successor_incr)
nipkow@27715
  3018
apply (metis leD linear order_trans successor_incr)
nipkow@27715
  3019
done
nipkow@27715
  3020
haftmann@25062
  3021
lemma upto_rec[code]: "[i..j] = (if i \<le> j then i # [successor i..j] else [])"
nipkow@25069
  3022
by(simp add: upto_def sorted_list_of_set_rec)
nipkow@24697
  3023
nipkow@27715
  3024
lemma upto_empty[simp]: "j < i \<Longrightarrow> [i..j] = []"
nipkow@27715
  3025
by(simp add: upto_rec)
nipkow@27715
  3026
nipkow@27715
  3027
lemma upto_rec2: "i \<le> j \<Longrightarrow> [i..successor j] = [i..j] @ [successor j]"
nipkow@27715
  3028
by(simp add: upto_def sorted_list_of_set_rec2)
nipkow@27715
  3029
nipkow@24697
  3030
end
nipkow@24697
  3031
nipkow@24697
  3032
text{* The integers are an instance of the above class: *}
nipkow@24697
  3033
haftmann@25571
  3034
instantiation int:: finite_intvl_succ
haftmann@25571
  3035
begin
haftmann@25571
  3036
haftmann@25571
  3037
definition
nipkow@27715
  3038
successor_int_def: "successor = (%i\<Colon>int. i+1)"
haftmann@25571
  3039
haftmann@25571
  3040
instance
nipkow@27715
  3041
by intro_classes (simp_all add: successor_int_def)
haftmann@25571
  3042
haftmann@25571
  3043
end
nipkow@24697
  3044
nipkow@24697
  3045
text{* Now @{term"[i..j::int]"} is defined for integers. *}
nipkow@24697
  3046
nipkow@24698
  3047
hide (open) const successor
nipkow@24698
  3048
nipkow@27715
  3049
lemma upto_rec2_int: "(i::int) \<le> j \<Longrightarrow> [i..j+1] = [i..j] @ [j+1]"
nipkow@27715
  3050
by(rule upto_rec2[where 'a = int, simplified successor_int_def])
nipkow@27715
  3051
nipkow@24645
  3052
nipkow@15392
  3053
subsubsection {* @{text lists}: the list-forming operator over sets *}
nipkow@15302
  3054
berghofe@23740
  3055
inductive_set
berghofe@23740
  3056
  lists :: "'a set => 'a list set"
berghofe@23740
  3057
  for A :: "'a set"
berghofe@22262
  3058
where
berghofe@23740
  3059
    Nil [intro!]: "[]: lists A"
nipkow@27715
  3060
  | Cons [intro!,noatp]: "[| a: A; l: lists A|] ==> a#l : lists A"
paulson@24286
  3061
paulson@24286
  3062
inductive_cases listsE [elim!,noatp]: "x#l : lists A"
paulson@24286
  3063
inductive_cases listspE [elim!,noatp]: "listsp A (x # l)"
berghofe@23740
  3064
berghofe@23740
  3065
lemma listsp_mono [mono]: "A \<le> B ==> listsp A \<le> listsp B"
berghofe@26795
  3066
by (rule predicate1I, erule listsp.induct, blast+)
berghofe@26795
  3067
berghofe@26795
  3068
lemmas lists_mono = listsp_mono [to_set pred_subset_eq]
berghofe@22262
  3069
haftmann@22422
  3070
lemma listsp_infI:
haftmann@22422
  3071
  assumes l: "listsp A l" shows "listsp B l ==> listsp (inf A B) l" using l
nipkow@24349
  3072
by induct blast+
nipkow@15302
  3073
haftmann@22422
  3074
lemmas lists_IntI = listsp_infI [to_set]
haftmann@22422
  3075
haftmann@22422
  3076
lemma listsp_inf_eq [simp]: "listsp (inf A B) = inf (listsp A) (listsp B)"
haftmann@22422
  3077
proof (rule mono_inf [where f=listsp, THEN order_antisym])
berghofe@22262
  3078
  show "mono listsp" by (simp add: mono_def listsp_mono)
berghofe@26795
  3079
  show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro!: listsp_infI predicate1I)
nipkow@15302
  3080
qed
nipkow@15302
  3081
haftmann@22422
  3082
lemmas listsp_conj_eq [simp] = listsp_inf_eq [simplified inf_fun_eq inf_bool_eq]
haftmann@22422
  3083
berghofe@26795
  3084
lemmas lists_Int_eq [simp] = listsp_inf_eq [to_set pred_equals_eq]
berghofe@22262
  3085
berghofe@22262
  3086
lemma append_in_listsp_conv [iff]:
berghofe@22262
  3087
     "(listsp A (xs @ ys)) = (listsp A xs \<and> listsp A ys)"
nipkow@15302
  3088
by (induct xs) auto
nipkow@15302
  3089
berghofe@22262
  3090
lemmas append_in_lists_conv [iff] = append_in_listsp_conv [to_set]
berghofe@22262
  3091
berghofe@22262
  3092
lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)"
berghofe@22262
  3093
-- {* eliminate @{text listsp} in favour of @{text set} *}
nipkow@15302
  3094
by (induct xs) auto
nipkow@15302
  3095
berghofe@22262
  3096
lemmas in_lists_conv_set = in_listsp_conv_set [to_set]
berghofe@22262
  3097
paulson@24286
  3098
lemma in_listspD [dest!,noatp]: "listsp A xs ==> \<forall>x\<in>set xs. A x"
berghofe@22262
  3099
by (rule in_listsp_conv_set [THEN iffD1])
berghofe@22262
  3100
paulson@24286
  3101
lemmas in_listsD [dest!,noatp] = in_listspD [to_set]
paulson@24286
  3102
paulson@24286
  3103
lemma in_listspI [intro!,noatp]: "\<forall>x\<in>set xs. A x ==> listsp A xs"
berghofe@22262
  3104
by (rule in_listsp_conv_set [THEN iffD2])
berghofe@22262
  3105
paulson@24286
  3106
lemmas in_listsI [intro!,noatp] = in_listspI [to_set]
nipkow@15302
  3107
nipkow@15302
  3108
lemma lists_UNIV [simp]: "lists UNIV = UNIV"
nipkow@15302
  3109
by auto
nipkow@15302
  3110
nipkow@17086
  3111
nipkow@17086
  3112
nipkow@17086
  3113
subsubsection{* Inductive definition for membership *}
nipkow@17086
  3114
berghofe@23740
  3115
inductive ListMem :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"
berghofe@22262
  3116
where
berghofe@22262
  3117
    elem:  "ListMem x (x # xs)"
berghofe@22262
  3118
  | insert:  "ListMem x xs \<Longrightarrow> ListMem x (y # xs)"
berghofe@22262
  3119
berghofe@22262
  3120
lemma ListMem_iff: "(ListMem x xs) = (x \<in> set xs)"
nipkow@17086
  3121
apply (rule iffI)
nipkow@17086
  3122
 apply (induct set: ListMem)
nipkow@17086
  3123
  apply auto
nipkow@17086
  3124
apply (induct xs)
nipkow@17086
  3125
 apply (auto intro: ListMem.intros)
nipkow@17086
  3126
done
nipkow@17086
  3127
nipkow@17086
  3128
nipkow@17086
  3129
nipkow@15392
  3130
subsubsection{*Lists as Cartesian products*}
nipkow@15302
  3131
nipkow@15302
  3132
text{*@{text"set_Cons A Xs"}: the set of lists with head drawn from
nipkow@15302
  3133
@{term A} and tail drawn from @{term Xs}.*}
nipkow@15302
  3134
nipkow@15302
  3135
constdefs
nipkow@15302
  3136
  set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set"
nipkow@15302
  3137
  "set_Cons A XS == {z. \<exists>x xs. z = x#xs & x \<in> A & xs \<in> XS}"
haftmann@28562
  3138
declare set_Cons_def [code del]
nipkow@15302
  3139
paulson@17724
  3140
lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A"
nipkow@15302
  3141
by (auto simp add: set_Cons_def)
nipkow@15302
  3142
nipkow@15302
  3143
text{*Yields the set of lists, all of the same length as the argument and
nipkow@15302
  3144
with elements drawn from the corresponding element of the argument.*}
nipkow@15302
  3145
nipkow@15302
  3146
consts  listset :: "'a set list \<Rightarrow> 'a list set"
nipkow@15302
  3147
primrec
nipkow@15302
  3148
   "listset []    = {[]}"
nipkow@15302
  3149
   "listset(A#As) = set_Cons A (listset As)"
nipkow@15302
  3150
nipkow@15302
  3151
paulson@15656
  3152
subsection{*Relations on Lists*}
paulson@15656
  3153
paulson@15656
  3154
subsubsection {* Length Lexicographic Ordering *}
paulson@15656
  3155
paulson@15656
  3156
text{*These orderings preserve well-foundedness: shorter lists 
paulson@15656
  3157
  precede longer lists. These ordering are not used in dictionaries.*}
paulson@15656
  3158
paulson@15656
  3159
consts lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
paulson@15656
  3160
        --{*The lexicographic ordering for lists of the specified length*}
nipkow@15302
  3161
primrec
paulson@15656
  3162
  "lexn r 0 = {}"
paulson@15656
  3163
  "lexn r (Suc n) =
paulson@15656
  3164
    (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int
paulson@15656
  3165
    {(xs,ys). length xs = Suc n \<and> length ys = Suc n}"
nipkow@15302
  3166
nipkow@15302
  3167
constdefs
paulson@15656
  3168
  lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
paulson@15656
  3169
    "lex r == \<Union>n. lexn r n"
paulson@15656
  3170
        --{*Holds only between lists of the same length*}
paulson@15656
  3171
nipkow@15693
  3172
  lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
nipkow@15693
  3173
    "lenlex r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"
paulson@15656
  3174
        --{*Compares lists by their length and then lexicographically*}
nipkow@15302
  3175
haftmann@28562
  3176
declare lex_def [code del]
haftmann@27106
  3177
nipkow@15302
  3178
wenzelm@13142
  3179
lemma wf_lexn: "wf r ==> wf (lexn r n)"
paulson@15251
  3180
apply (induct n, simp, simp)
nipkow@13145
  3181
apply(rule wf_subset)
nipkow@13145
  3182
 prefer 2 apply (rule Int_lower1)
nipkow@13145
  3183
apply(rule wf_prod_fun_image)
paulson@14208
  3184
 prefer 2 apply (rule inj_onI, auto)
nipkow@13145
  3185
done
wenzelm@13114
  3186
wenzelm@13114
  3187
lemma lexn_length:
nipkow@24526
  3188
  "(xs, ys) : lexn r n ==> length xs = n \<and> length ys = n"
nipkow@24526
  3189
by (induct n arbitrary: xs ys) auto
wenzelm@13114
  3190
wenzelm@13142
  3191
lemma wf_lex [intro!]: "wf r ==> wf (lex r)"
nipkow@13145
  3192
apply (unfold lex_def)
nipkow@13145
  3193
apply (rule wf_UN)
paulson@14208
  3194
apply (blast intro: wf_lexn, clarify)
nipkow@13145
  3195
apply (rename_tac m n)
nipkow@13145
  3196
apply (subgoal_tac "m \<noteq> n")
nipkow@13145
  3197
 prefer 2 apply blast
nipkow@13145
  3198
apply (blast dest: lexn_length not_sym)
nipkow@13145
  3199
done
wenzelm@13114
  3200
wenzelm@13114
  3201
lemma lexn_conv:
paulson@15656
  3202
  "lexn r n =
paulson@15656
  3203
    {(xs,ys). length xs = n \<and> length ys = n \<and>
paulson@15656
  3204
    (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}"
nipkow@18423
  3205
apply (induct n, simp)
paulson@14208
  3206
apply (simp add: image_Collect lex_prod_def, safe, blast)
paulson@14208
  3207
 apply (rule_tac x = "ab # xys" in exI, simp)
paulson@14208
  3208
apply (case_tac xys, simp_all, blast)
nipkow@13145
  3209
done
wenzelm@13114
  3210
wenzelm@13114
  3211
lemma lex_conv:
paulson@15656
  3212
  "lex r =
paulson@15656
  3213
    {(xs,ys). length xs = length ys \<and>
paulson@15656
  3214
    (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}"
nipkow@13145
  3215
by (force simp add: lex_def lexn_conv)
wenzelm@13114
  3216
nipkow@15693
  3217
lemma wf_lenlex [intro!]: "wf r ==> wf (lenlex r)"
nipkow@15693
  3218
by (unfold lenlex_def) blast
nipkow@15693
  3219
nipkow@15693
  3220
lemma lenlex_conv:
nipkow@15693
  3221
    "lenlex r = {(xs,ys). length xs < length ys |
paulson@15656
  3222
                 length xs = length ys \<and> (xs, ys) : lex r}"
nipkow@19623
  3223
by (simp add: lenlex_def diag_def lex_prod_def inv_image_def)
wenzelm@13114
  3224
wenzelm@13142
  3225
lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r"
nipkow@13145
  3226
by (simp add: lex_conv)
wenzelm@13114
  3227
wenzelm@13142
  3228
lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r"
nipkow@13145
  3229
by (simp add:lex_conv)
wenzelm@13114
  3230
paulson@18447
  3231
lemma Cons_in_lex [simp]:
paulson@15656
  3232
    "((x # xs, y # ys) : lex r) =
paulson@15656
  3233
      ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)"
nipkow@13145
  3234
apply (simp add: lex_conv)
nipkow@13145
  3235
apply (rule iffI)
paulson@14208
  3236
 prefer 2 apply (blast intro: Cons_eq_appendI, clarify)
paulson@14208
  3237
apply (case_tac xys, simp, simp)
nipkow@13145
  3238
apply blast
nipkow@13145
  3239
done
wenzelm@13114
  3240
wenzelm@13114
  3241
paulson@15656
  3242
subsubsection {* Lexicographic Ordering *}
paulson@15656
  3243
paulson@15656
  3244
text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b".
paulson@15656
  3245
    This ordering does \emph{not} preserve well-foundedness.
nipkow@17090
  3246
     Author: N. Voelker, March 2005. *} 
paulson@15656
  3247
paulson@15656
  3248
constdefs 
paulson@15656
  3249
  lexord :: "('a * 'a)set \<Rightarrow> ('a list * 'a list) set" 
paulson@15656
  3250
  "lexord  r == {(x,y). \<exists> a v. y = x @ a # v \<or> 
paulson@15656
  3251
            (\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}"
haftmann@28562
  3252
declare lexord_def [code del]
paulson@15656
  3253
paulson@15656
  3254
lemma lexord_Nil_left[simp]:  "([],y) \<in> lexord r = (\<exists> a x. y = a # x)"
nipkow@24349
  3255
by (unfold lexord_def, induct_tac y, auto) 
paulson@15656
  3256
paulson@15656
  3257
lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r"
nipkow@24349
  3258
by (unfold lexord_def, induct_tac x, auto)
paulson@15656
  3259
paulson@15656
  3260
lemma lexord_cons_cons[simp]:
paulson@15656
  3261
     "((a # x, b # y) \<in> lexord r) = ((a,b)\<in> r | (a = b & (x,y)\<in> lexord r))"
paulson@15656
  3262
  apply (unfold lexord_def, safe, simp_all)
paulson@15656
  3263
  apply (case_tac u, simp, simp)
paulson@15656
  3264
  apply (case_tac u, simp, clarsimp, blast, blast, clarsimp)
paulson@15656
  3265
  apply (erule_tac x="b # u" in allE)
paulson@15656
  3266
  by force
paulson@15656
  3267
paulson@15656
  3268
lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons
paulson@15656
  3269
paulson@15656
  3270
lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r"
nipkow@24349
  3271
by (induct_tac x, auto)  
paulson@15656
  3272
paulson@15656
  3273
lemma lexord_append_left_rightI:
paulson@15656
  3274
     "(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r"
nipkow@24349
  3275
by (induct_tac u, auto)
paulson@15656
  3276
paulson@15656
  3277
lemma lexord_append_leftI: " (u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r"
nipkow@24349
  3278
by (induct x, auto)
paulson@15656
  3279
paulson@15656
  3280
lemma lexord_append_leftD:
paulson@15656
  3281
     "\<lbrakk> (x @ u, x @ v) \<in> lexord r; (! a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r"
nipkow@24349
  3282
by (erule rev_mp, induct_tac x, auto)
paulson@15656
  3283
paulson@15656
  3284
lemma lexord_take_index_conv: 
paulson@15656
  3285
   "((x,y) : lexord r) = 
paulson@15656
  3286
    ((length x < length y \<and> take (length x) y = x) \<or> 
paulson@15656
  3287
     (\<exists>i. i < min(length x)(length y) & take i x = take i y & (x!i,y!i) \<in> r))"
paulson@15656
  3288
  apply (unfold lexord_def Let_def, clarsimp) 
paulson@15656
  3289
  apply (rule_tac f = "(% a b. a \<or> b)" in arg_cong2)
paulson@15656
  3290
  apply auto 
paulson@15656
  3291
  apply (rule_tac x="hd (drop (length x) y)" in exI)
paulson@15656
  3292
  apply (rule_tac x="tl (drop (length x) y)" in exI)
paulson@15656
  3293
  apply (erule subst, simp add: min_def) 
paulson@15656
  3294
  apply (rule_tac x ="length u" in exI, simp) 
paulson@15656
  3295
  apply (rule_tac x ="take i x" in exI) 
paulson@15656
  3296
  apply (rule_tac x ="x ! i" in exI) 
paulson@15656
  3297
  apply (rule_tac x ="y ! i" in exI, safe) 
paulson@15656
  3298
  apply (rule_tac x="drop (Suc i) x" in exI)
paulson@15656
  3299
  apply (drule sym, simp add: drop_Suc_conv_tl) 
paulson@15656
  3300
  apply (rule_tac x="drop (Suc i) y" in exI)
paulson@15656
  3301
  by (simp add: drop_Suc_conv_tl) 
paulson@15656
  3302
paulson@15656
  3303
-- {* lexord is extension of partial ordering List.lex *} 
paulson@15656
  3304
lemma lexord_lex: " (x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)"
paulson@15656
  3305
  apply (rule_tac x = y in spec) 
paulson@15656
  3306
  apply (induct_tac x, clarsimp) 
paulson@15656
  3307
  by (clarify, case_tac x, simp, force)
paulson@15656
  3308
paulson@15656
  3309
lemma lexord_irreflexive: "(! x. (x,x) \<notin> r) \<Longrightarrow> (y,y) \<notin> lexord r"
paulson@15656
  3310
  by (induct y, auto)
paulson@15656
  3311
paulson@15656
  3312
lemma lexord_trans: 
paulson@15656
  3313
    "\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r"
paulson@15656
  3314
   apply (erule rev_mp)+
paulson@15656
  3315
   apply (rule_tac x = x in spec) 
paulson@15656
  3316
  apply (rule_tac x = z in spec) 
paulson@15656
  3317
  apply ( induct_tac y, simp, clarify)
paulson@15656
  3318
  apply (case_tac xa, erule ssubst) 
paulson@15656
  3319
  apply (erule allE, erule allE) -- {* avoid simp recursion *} 
paulson@15656
  3320
  apply (case_tac x, simp, simp) 
paulson@24632
  3321
  apply (case_tac x, erule allE, erule allE, simp)
paulson@15656
  3322
  apply (erule_tac x = listb in allE) 
paulson@15656
  3323
  apply (erule_tac x = lista in allE, simp)
paulson@15656
  3324
  apply (unfold trans_def)
paulson@15656
  3325
  by blast
paulson@15656
  3326
paulson@15656
  3327
lemma lexord_transI:  "trans r \<Longrightarrow> trans (lexord r)"
nipkow@24349
  3328
by (rule transI, drule lexord_trans, blast) 
paulson@15656
  3329
paulson@15656
  3330
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
  3331
  apply (rule_tac x = y in spec) 
paulson@15656
  3332
  apply (induct_tac x, rule allI) 
paulson@15656
  3333
  apply (case_tac x, simp, simp) 
paulson@15656
  3334
  apply (rule allI, case_tac x, simp, simp) 
paulson@15656
  3335
  by blast
paulson@15656
  3336
paulson@15656
  3337
krauss@21103
  3338
subsection {* Lexicographic combination of measure functions *}
krauss@21103
  3339
krauss@21103
  3340
text {* These are useful for termination proofs *}
krauss@21103
  3341
krauss@21103
  3342
definition
krauss@21103
  3343
  "measures fs = inv_image (lex less_than) (%a. map (%f. f a) fs)"
krauss@21103
  3344
krauss@21106
  3345
lemma wf_measures[recdef_wf, simp]: "wf (measures fs)"
nipkow@24349
  3346
unfolding measures_def
nipkow@24349
  3347
by blast
krauss@21103
  3348
krauss@21103
  3349
lemma in_measures[simp]: 
krauss@21103
  3350
  "(x, y) \<in> measures [] = False"
krauss@21103
  3351
  "(x, y) \<in> measures (f # fs)
krauss@21103
  3352
         = (f x < f y \<or> (f x = f y \<and> (x, y) \<in> measures fs))"  
nipkow@24349
  3353
unfolding measures_def
nipkow@24349
  3354
by auto
krauss@21103
  3355
krauss@21103
  3356
lemma measures_less: "f x < f y ==> (x, y) \<in> measures (f#fs)"
nipkow@24349
  3357
by simp
krauss@21103
  3358
krauss@21103
  3359
lemma measures_lesseq: "f x <= f y ==> (x, y) \<in> measures fs ==> (x, y) \<in> measures (f#fs)"
nipkow@24349
  3360
by auto
krauss@21103
  3361
krauss@21103
  3362
nipkow@15392
  3363
subsubsection{*Lifting a Relation on List Elements to the Lists*}
nipkow@15302
  3364
berghofe@23740
  3365
inductive_set
berghofe@23740
  3366
  listrel :: "('a * 'a)set => ('a list * 'a list)set"
berghofe@23740
  3367
  for r :: "('a * 'a)set"
berghofe@22262
  3368
where
berghofe@23740
  3369
    Nil:  "([],[]) \<in> listrel r"
berghofe@23740
  3370
  | Cons: "[| (x,y) \<in> r; (xs,ys) \<in> listrel r |] ==> (x#xs, y#ys) \<in> listrel r"
berghofe@23740
  3371
berghofe@23740
  3372
inductive_cases listrel_Nil1 [elim!]: "([],xs) \<in> listrel r"
berghofe@23740
  3373
inductive_cases listrel_Nil2 [elim!]: "(xs,[]) \<in> listrel r"
berghofe@23740
  3374
inductive_cases listrel_Cons1 [elim!]: "(y#ys,xs) \<in> listrel r"
berghofe@23740
  3375
inductive_cases listrel_Cons2 [elim!]: "(xs,y#ys) \<in> listrel r"
nipkow@15302
  3376
nipkow@15302
  3377
nipkow@15302
  3378
lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s"
nipkow@15302
  3379
apply clarify  
berghofe@23740
  3380
apply (erule listrel.induct)
berghofe@23740
  3381
apply (blast intro: listrel.intros)+
nipkow@15281
  3382
done
nipkow@15281
  3383
nipkow@15302
  3384
lemma listrel_subset: "r \<subseteq> A \<times> A \<Longrightarrow> listrel r \<subseteq> lists A \<times> lists A"
nipkow@15302
  3385
apply clarify 
berghofe@23740
  3386
apply (erule listrel.induct, auto) 
nipkow@13145
  3387
done
wenzelm@13114
  3388
nipkow@15302
  3389
lemma listrel_refl: "refl A r \<Longrightarrow> refl (lists A) (listrel r)" 
nipkow@15302
  3390
apply (simp add: refl_def listrel_subset Ball_def)
nipkow@15302
  3391
apply (rule allI) 
nipkow@15302
  3392
apply (induct_tac x) 
berghofe@23740
  3393
apply (auto intro: listrel.intros)
nipkow@13145
  3394
done
wenzelm@13114
  3395
nipkow@15302
  3396
lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)" 
nipkow@15302
  3397
apply (auto simp add: sym_def)
berghofe@23740
  3398
apply (erule listrel.induct) 
berghofe@23740
  3399
apply (blast intro: listrel.intros)+
nipkow@15281
  3400
done
nipkow@15281
  3401
nipkow@15302
  3402
lemma listrel_trans: "trans r \<Longrightarrow> trans (listrel r)" 
nipkow@15302
  3403
apply (simp add: trans_def)
nipkow@15302
  3404
apply (intro allI) 
nipkow@15302
  3405
apply (rule impI) 
berghofe@23740
  3406
apply (erule listrel.induct) 
berghofe@23740
  3407
apply (blast intro: listrel.intros)+
nipkow@15281
  3408
done
nipkow@15281
  3409
nipkow@15302
  3410
theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)"
nipkow@15302
  3411
by (simp add: equiv_def listrel_refl listrel_sym listrel_trans) 
nipkow@15302
  3412
nipkow@15302
  3413
lemma listrel_Nil [simp]: "listrel r `` {[]} = {[]}"
berghofe@23740
  3414
by (blast intro: listrel.intros)
nipkow@15302
  3415
nipkow@15302
  3416
lemma listrel_Cons:
nipkow@15302
  3417
     "listrel r `` {x#xs} = set_Cons (r``{x}) (listrel r `` {xs})";
berghofe@23740
  3418
by (auto simp add: set_Cons_def intro: listrel.intros) 
nipkow@15302
  3419
nipkow@15302
  3420
nipkow@15392
  3421
subsection{*Miscellany*}
nipkow@15392
  3422
nipkow@15392
  3423
subsubsection {* Characters and strings *}
wenzelm@13366
  3424
wenzelm@13366
  3425
datatype nibble =
wenzelm@13366
  3426
    Nibble0 | Nibble1 | Nibble2 | Nibble3 | Nibble4 | Nibble5 | Nibble6 | Nibble7
wenzelm@13366
  3427
  | Nibble8 | Nibble9 | NibbleA | NibbleB | NibbleC | NibbleD | NibbleE | NibbleF
wenzelm@13366
  3428
haftmann@26148
  3429
lemma UNIV_nibble:
haftmann@26148
  3430
  "UNIV = {Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
haftmann@26148
  3431
    Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF}" (is "_ = ?A")
haftmann@26148
  3432
proof (rule UNIV_eq_I)
haftmann@26148
  3433
  fix x show "x \<in> ?A" by (cases x) simp_all
haftmann@26148
  3434
qed
haftmann@26148
  3435
haftmann@26148
  3436
instance nibble :: finite
haftmann@26148
  3437
  by default (simp add: UNIV_nibble)
haftmann@26148
  3438
wenzelm@13366
  3439
datatype char = Char nibble nibble
wenzelm@13366
  3440
  -- "Note: canonical order of character encoding coincides with standard term ordering"
wenzelm@13366
  3441
haftmann@26148
  3442
lemma UNIV_char:
haftmann@26148
  3443
  "UNIV = image (split Char) (UNIV \<times> UNIV)"
haftmann@26148
  3444
proof (rule UNIV_eq_I)
haftmann@26148
  3445
  fix x show "x \<in> image (split Char) (UNIV \<times> UNIV)" by (cases x) auto
haftmann@26148
  3446
qed
haftmann@26148
  3447
haftmann@26148
  3448
instance char :: finite
haftmann@26148
  3449
  by default (simp add: UNIV_char)
haftmann@26148
  3450
haftmann@28230
  3451
lemma size_char [code, simp]:
haftmann@28230
  3452
  "size (c::char) = 0" by (cases c) simp
haftmann@28230
  3453
haftmann@28230
  3454
lemma char_size [code, simp]:
haftmann@28230
  3455
  "char_size (c::char) = 0" by (cases c) simp
haftmann@28230
  3456
haftmann@28244
  3457
primrec nibble_pair_of_char :: "char \<Rightarrow> nibble \<times> nibble" where
haftmann@28244
  3458
  "nibble_pair_of_char (Char n m) = (n, m)"
haftmann@28244
  3459
haftmann@28562
  3460
declare nibble_pair_of_char.simps [code del]
haftmann@28244
  3461
haftmann@28244
  3462
setup {*
haftmann@28244
  3463
let
haftmann@28244
  3464
  val nibbles = map (Thm.cterm_of @{theory} o HOLogic.mk_nibble) (0 upto 15);
haftmann@28244
  3465
  val thms = map_product
haftmann@28244
  3466
   (fn n => fn m => Drule.instantiate' [] [SOME n, SOME m] @{thm nibble_pair_of_char.simps})
haftmann@28244
  3467
      nibbles nibbles;
haftmann@28244
  3468
in
haftmann@28965
  3469
  PureThy.note_thmss Thm.lemmaK [((Binding.name "nibble_pair_of_char_simps", []), [(thms, [])])]
haftmann@28370
  3470
  #-> (fn [(_, thms)] => fold_rev Code.add_eqn thms)
haftmann@28244
  3471
end
haftmann@28244
  3472
*}
haftmann@28244
  3473
haftmann@28562
  3474
lemma char_case_nibble_pair [code, code inline]:
haftmann@28244
  3475
  "char_case f = split f o nibble_pair_of_char"
haftmann@28244
  3476
  by (simp add: expand_fun_eq split: char.split)
haftmann@28244
  3477
haftmann@28562
  3478
lemma char_rec_nibble_pair [code, code inline]:
haftmann@28244
  3479
  "char_rec f = split f o nibble_pair_of_char"
haftmann@28244
  3480
  unfolding char_case_nibble_pair [symmetric]
haftmann@28244
  3481
  by (simp add: expand_fun_eq split: char.split)
haftmann@28244
  3482
wenzelm@13366
  3483
types string = "char list"
wenzelm@13366
  3484
wenzelm@13366
  3485
syntax
wenzelm@13366
  3486
  "_Char" :: "xstr => char"    ("CHR _")
wenzelm@13366
  3487
  "_String" :: "xstr => string"    ("_")
wenzelm@13366
  3488
wenzelm@21754
  3489
setup StringSyntax.setup
wenzelm@13366
  3490
haftmann@20453
  3491
krauss@26749
  3492
subsection {* Size function *}
krauss@26749
  3493
krauss@26875
  3494
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (list_size f)"
krauss@26875
  3495
by (rule is_measure_trivial)
krauss@26875
  3496
krauss@26875
  3497
lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (option_size f)"
krauss@26875
  3498
by (rule is_measure_trivial)
krauss@26875
  3499
krauss@26875
  3500
lemma list_size_estimation[termination_simp]: 
krauss@26875
  3501
  "x \<in> set xs \<Longrightarrow> y < f x \<Longrightarrow> y < list_size f xs"
krauss@26749
  3502
by (induct xs) auto
krauss@26749
  3503
krauss@26875
  3504
lemma list_size_estimation'[termination_simp]: 
krauss@26875
  3505
  "x \<in> set xs \<Longrightarrow> y \<le> f x \<Longrightarrow> y \<le> list_size f xs"
krauss@26875
  3506
by (induct xs) auto
krauss@26875
  3507
krauss@26875
  3508
lemma list_size_map[simp]: "list_size f (map g xs) = list_size (f o g) xs"
krauss@26875
  3509
by (induct xs) auto
krauss@26875
  3510
krauss@26875
  3511
lemma list_size_pointwise[termination_simp]: 
krauss@26875
  3512
  "(\<And>x. x \<in> set xs \<Longrightarrow> f x < g x) \<Longrightarrow> list_size f xs \<le> list_size g xs"
krauss@26875
  3513
by (induct xs) force+
krauss@26749
  3514
haftmann@21061
  3515
subsection {* Code generator *}
haftmann@21061
  3516
haftmann@21061
  3517
subsubsection {* Setup *}
berghofe@15064
  3518
berghofe@16770
  3519
types_code
berghofe@16770
  3520
  "list" ("_ list")
berghofe@16770
  3521
attach (term_of) {*
wenzelm@21760
  3522
fun term_of_list f T = HOLogic.mk_list T o map f;
berghofe@16770
  3523
*}
berghofe@16770
  3524
attach (test) {*
berghofe@25885
  3525
fun gen_list' aG aT i j = frequency
berghofe@25885
  3526
  [(i, fn () =>
berghofe@25885
  3527
      let
berghofe@25885
  3528
        val (x, t) = aG j;
berghofe@25885
  3529
        val (xs, ts) = gen_list' aG aT (i-1) j
berghofe@25885
  3530
      in (x :: xs, fn () => HOLogic.cons_const aT $ t () $ ts ()) end),
berghofe@25885
  3531
   (1, fn () => ([], fn () => HOLogic.nil_const aT))] ()
berghofe@25885
  3532
and gen_list aG aT i = gen_list' aG aT i i;
berghofe@16770
  3533
*}
berghofe@16770
  3534
  "char" ("string")
berghofe@16770
  3535
attach (term_of) {*
berghofe@24130
  3536
val term_of_char = HOLogic.mk_char o ord;
berghofe@16770
  3537
*}
berghofe@16770
  3538
attach (test) {*
berghofe@25885
  3539
fun gen_char i =
berghofe@25885
  3540
  let val j = random_range (ord "a") (Int.min (ord "a" + i, ord "z"))
berghofe@25885
  3541
  in (chr j, fn () => HOLogic.mk_char j) end;
berghofe@15064
  3542
*}
berghofe@15064
  3543
berghofe@15064
  3544
consts_code "Cons" ("(_ ::/ _)")
berghofe@15064
  3545
haftmann@20453
  3546
code_type list
haftmann@20453
  3547
  (SML "_ list")
haftmann@21911
  3548
  (OCaml "_ list")
haftmann@21113
  3549
  (Haskell "![_]")
haftmann@20453
  3550
haftmann@22799
  3551
code_reserved SML
haftmann@22799
  3552
  list
haftmann@22799
  3553
haftmann@22799
  3554
code_reserved OCaml
haftmann@22799
  3555
  list
haftmann@22799
  3556
haftmann@20453
  3557
code_const Nil
haftmann@21113
  3558
  (SML "[]")
haftmann@21911
  3559
  (OCaml "[]")
haftmann@21113
  3560
  (Haskell "[]")
haftmann@20453
  3561
haftmann@28090
  3562
ML {*
haftmann@28090
  3563
local
haftmann@28090
  3564
haftmann@28090
  3565
open Basic_Code_Thingol;
haftmann@28663
  3566
haftmann@28663
  3567
fun implode_list (nil', cons') t =
haftmann@28090
  3568
  let
haftmann@28090
  3569
    fun dest_cons (IConst (c, _) `$ t1 `$ t2) =
haftmann@28090
  3570
          if c = cons'
haftmann@28090
  3571
          then SOME (t1, t2)
haftmann@28090
  3572
          else NONE
haftmann@28090
  3573
      | dest_cons _ = NONE;
haftmann@28090
  3574
    val (ts, t') = Code_Thingol.unfoldr dest_cons t;
haftmann@28090
  3575
  in case t'
haftmann@28090
  3576
   of IConst (c, _) => if c = nil' then SOME ts else NONE
haftmann@28090
  3577
    | _ => NONE
haftmann@28090
  3578
  end;
haftmann@28090
  3579
haftmann@28663
  3580
fun decode_char nibbles' (IConst (c1, _), IConst (c2, _)) =
haftmann@28090
  3581
      let
haftmann@28090
  3582
        fun idx c = find_index (curry (op =) c) nibbles';
haftmann@28090
  3583
        fun decode ~1 _ = NONE
haftmann@28090
  3584
          | decode _ ~1 = NONE
haftmann@28090
  3585
          | decode n m = SOME (chr (n * 16 + m));
haftmann@28090
  3586
      in decode (idx c1) (idx c2) end
haftmann@28663
  3587
  | decode_char _ _ = NONE;
haftmann@28663
  3588
haftmann@28663
  3589
fun implode_string (char', nibbles') mk_char mk_string ts =
haftmann@28090
  3590
  let
haftmann@28090
  3591
    fun implode_char (IConst (c, _) `$ t1 `$ t2) =
haftmann@28663
  3592
          if c = char' then decode_char nibbles' (t1, t2) else NONE
haftmann@28090
  3593
      | implode_char _ = NONE;
haftmann@28090
  3594
    val ts' = map implode_char ts;
haftmann@28090
  3595
  in if forall is_some ts'
haftmann@28090
  3596
    then (SOME o Code_Printer.str o mk_string o implode o map_filter I) ts'
haftmann@28090
  3597
    else NONE
haftmann@28090
  3598
  end;
haftmann@28090
  3599
haftmann@28663
  3600
fun list_names naming = pairself (the o Code_Thingol.lookup_const naming)
haftmann@28663
  3601
  (@{const_name Nil}, @{const_name Cons});
haftmann@28663
  3602
fun char_name naming = (the o Code_Thingol.lookup_const naming)
haftmann@28663
  3603
  @{const_name Char}
haftmann@28663
  3604
fun nibble_names naming = map (the o Code_Thingol.lookup_const naming)
haftmann@28663
  3605
  [@{const_name Nibble0}, @{const_name Nibble1},
haftmann@28663
  3606
   @{const_name Nibble2}, @{const_name Nibble3},
haftmann@28663
  3607
   @{const_name Nibble4}, @{const_name Nibble5},
haftmann@28663
  3608
   @{const_name Nibble6}, @{const_name Nibble7},
haftmann@28663
  3609
   @{const_name Nibble8}, @{const_name Nibble9},
haftmann@28663
  3610
   @{const_name NibbleA}, @{const_name NibbleB},
haftmann@28663
  3611
   @{const_name NibbleC}, @{const_name NibbleD},
haftmann@28663
  3612
   @{const_name NibbleE}, @{const_name NibbleF}];
haftmann@28663
  3613
haftmann@28090
  3614
fun default_list (target_fxy, target_cons) pr fxy t1 t2 =
haftmann@28090
  3615
  Code_Printer.brackify_infix (target_fxy, Code_Printer.R) fxy [
haftmann@28090
  3616
    pr (Code_Printer.INFX (target_fxy, Code_Printer.X)) t1,
haftmann@28090
  3617
    Code_Printer.str target_cons,
haftmann@28090
  3618
    pr (Code_Printer.INFX (target_fxy, Code_Printer.R)) t2
haftmann@28090
  3619
  ];
haftmann@28090
  3620
haftmann@28090
  3621
fun pretty_list literals =
haftmann@28090
  3622
  let
haftmann@28090
  3623
    val mk_list = Code_Printer.literal_list literals;
haftmann@28708
  3624
    fun pretty pr naming thm vars fxy [(t1, _), (t2, _)] =
haftmann@28663
  3625
      case Option.map (cons t1) (implode_list (list_names naming) t2)
haftmann@28090
  3626
       of SOME ts => mk_list (map (pr vars Code_Printer.NOBR) ts)
haftmann@28090
  3627
        | NONE => default_list (Code_Printer.infix_cons literals) (pr vars) fxy t1 t2;
haftmann@28090
  3628
  in (2, pretty) end;
haftmann@28090
  3629
haftmann@28090
  3630
fun pretty_list_string literals =
haftmann@28090
  3631
  let
haftmann@28090
  3632
    val mk_list = Code_Printer.literal_list literals;
haftmann@28090
  3633
    val mk_char = Code_Printer.literal_char literals;
haftmann@28090
  3634
    val mk_string = Code_Printer.literal_string literals;
haftmann@28708
  3635
    fun pretty pr naming thm vars fxy [(t1, _), (t2, _)] =
haftmann@28663
  3636
      case Option.map (cons t1) (implode_list (list_names naming) t2)
haftmann@28663
  3637
       of SOME ts => (case implode_string (char_name naming, nibble_names naming) mk_char mk_string ts
haftmann@28090
  3638
           of SOME p => p
haftmann@28090
  3639
            | NONE => mk_list (map (pr vars Code_Printer.NOBR) ts))
haftmann@28090
  3640
        | NONE => default_list (Code_Printer.infix_cons literals) (pr vars) fxy t1 t2;
haftmann@28090
  3641
  in (2, pretty) end;
haftmann@28090
  3642
haftmann@28090
  3643
fun pretty_char literals =
haftmann@28090
  3644
  let
haftmann@28090
  3645
    val mk_char = Code_Printer.literal_char literals;
haftmann@28708
  3646
    fun pretty _ naming thm _ _ [(t1, _), (t2, _)] =
haftmann@28663
  3647
      case decode_char (nibble_names naming) (t1, t2)
haftmann@28090
  3648
       of SOME c => (Code_Printer.str o mk_char) c
haftmann@28090
  3649
        | NONE => Code_Printer.nerror thm "Illegal character expression";
haftmann@28090
  3650
  in (2, pretty) end;
haftmann@28090
  3651
haftmann@28090
  3652
fun pretty_message literals =
haftmann@28090
  3653
  let
haftmann@28090
  3654
    val mk_char = Code_Printer.literal_char literals;
haftmann@28090
  3655
    val mk_string = Code_Printer.literal_string literals;
haftmann@28708
  3656
    fun pretty _ naming thm _ _ [(t, _)] =
haftmann@28663
  3657
      case implode_list (list_names naming) t
haftmann@28663
  3658
       of SOME ts => (case implode_string (char_name naming, nibble_names naming) mk_char mk_string ts
haftmann@28090
  3659
           of SOME p => p
haftmann@28090
  3660
            | NONE => Code_Printer.nerror thm "Illegal message expression")
haftmann@28090
  3661
        | NONE => Code_Printer.nerror thm "Illegal message expression";
haftmann@28090
  3662
  in (1, pretty) end;
haftmann@28090
  3663
haftmann@28090
  3664
in
haftmann@28090
  3665
haftmann@28090
  3666
fun add_literal_list target thy =
haftmann@28090
  3667
  let
haftmann@28090
  3668
    val pr = pretty_list (Code_Target.the_literals thy target);
haftmann@28090
  3669
  in
haftmann@28090
  3670
    thy
haftmann@28090
  3671
    |> Code_Target.add_syntax_const target @{const_name Cons} (SOME pr)
haftmann@28090
  3672
  end;
haftmann@28090
  3673
haftmann@28090
  3674
fun add_literal_list_string target thy =
haftmann@28090
  3675
  let
haftmann@28090
  3676
    val pr = pretty_list_string (Code_Target.the_literals thy target);
haftmann@28090
  3677
  in
haftmann@28090
  3678
    thy
haftmann@28090
  3679
    |> Code_Target.add_syntax_const target @{const_name Cons} (SOME pr)
haftmann@28090
  3680
  end;
haftmann@28090
  3681
haftmann@28090
  3682
fun add_literal_char target thy =
haftmann@28090
  3683
  let
haftmann@28090
  3684
    val pr = pretty_char (Code_Target.the_literals thy target);
haftmann@28090
  3685
  in
haftmann@28090
  3686
    thy
haftmann@28090
  3687
    |> Code_Target.add_syntax_const target @{const_name Char} (SOME pr)
haftmann@28090
  3688
  end;
haftmann@28090
  3689
haftmann@28090
  3690
fun add_literal_message str target thy =
haftmann@28090
  3691
  let
haftmann@28090
  3692
    val pr = pretty_message (Code_Target.the_literals thy target);
haftmann@28090
  3693
  in
haftmann@28090
  3694
    thy
haftmann@28090
  3695
    |> Code_Target.add_syntax_const target str (SOME pr)
haftmann@28090
  3696
  end;
haftmann@28090
  3697
haftmann@28090
  3698
end;
haftmann@28090
  3699
*}
haftmann@28090
  3700
haftmann@21911
  3701
setup {*
haftmann@28090
  3702
  fold (fn target => add_literal_list target) ["SML", "OCaml", "Haskell"]
haftmann@21911
  3703
*}
haftmann@21911
  3704
haftmann@22799
  3705
code_instance list :: eq
haftmann@22799
  3706
  (Haskell -)
haftmann@20588
  3707
haftmann@28346
  3708
code_const "eq_class.eq \<Colon> 'a\<Colon>eq list \<Rightarrow> 'a list \<Rightarrow> bool"
haftmann@20588
  3709
  (Haskell infixl 4 "==")
haftmann@20588
  3710
haftmann@20453
  3711
setup {*
haftmann@20453
  3712
let
haftmann@20453
  3713
haftmann@28537
  3714
fun list_codegen thy defs dep thyname b t gr =
berghofe@24902
  3715
  let
berghofe@24902
  3716
    val ts = HOLogic.dest_list t;
haftmann@28537
  3717
    val (_, gr') = Codegen.invoke_tycodegen thy defs dep thyname false
haftmann@28537
  3718
      (fastype_of t) gr;
haftmann@28537
  3719
    val (ps, gr'') = fold_map
haftmann@28537
  3720
      (Codegen.invoke_codegen thy defs dep thyname false) ts gr'
haftmann@28537
  3721
  in SOME (Pretty.list "[" "]" ps, gr'') end handle TERM _ => NONE;
haftmann@28537
  3722
haftmann@28537
  3723
fun char_codegen thy defs dep thyname b t gr =
berghofe@24902
  3724
  let
berghofe@24902
  3725
    val i = HOLogic.dest_char t;
haftmann@28537
  3726
    val (_, gr') = Codegen.invoke_tycodegen thy defs dep thyname false
haftmann@28537
  3727
      (fastype_of t) gr;
haftmann@28537
  3728
  in SOME (Codegen.str (ML_Syntax.print_string (chr i)), gr')
berghofe@24902
  3729
  end handle TERM _ => NONE;
haftmann@20453
  3730
haftmann@20453
  3731
in
haftmann@20453
  3732
  Codegen.add_codegen "list_codegen" list_codegen
haftmann@20453
  3733
  #> Codegen.add_codegen "char_codegen" char_codegen
haftmann@20453
  3734
end;
haftmann@20453
  3735
*}
berghofe@15064
  3736
haftmann@21061
  3737
haftmann@21061
  3738
subsubsection {* Generation of efficient code *}
haftmann@21061
  3739
wenzelm@25221
  3740
primrec
haftmann@25559
  3741
  member :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" (infixl "mem" 55)
haftmann@25559
  3742
where 
haftmann@25559
  3743
  "x mem [] \<longleftrightarrow> False"
haftmann@28515
  3744
  | "x mem (y#ys) \<longleftrightarrow> x = y \<or> x mem ys"
haftmann@21061
  3745
haftmann@21061
  3746
primrec
haftmann@26442
  3747
  null:: "'a list \<Rightarrow> bool"
haftmann@26442
  3748
where
haftmann@21061
  3749
  "null [] = True"
haftmann@26442
  3750
  | "null (x#xs) = False"
haftmann@21061
  3751
haftmann@21061
  3752
primrec
haftmann@26442
  3753
  list_inter :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
haftmann@26442
  3754
where
haftmann@21061
  3755
  "list_inter [] bs = []"
haftmann@26442
  3756
  | "list_inter (a#as) bs =
haftmann@21061
  3757
     (if a \<in> set bs then a # list_inter as bs else list_inter as bs)"
haftmann@21061
  3758
haftmann@21061
  3759
primrec
haftmann@26442
  3760
  list_all :: "('a \<Rightarrow> bool) \<Rightarrow> ('a list \<Rightarrow> bool)"
haftmann@26442
  3761
where
haftmann@21061
  3762
  "list_all P [] = True"
haftmann@26442
  3763
  | "list_all P (x#xs) = (P x \<and> list_all P xs)"
haftmann@21061
  3764
haftmann@21061
  3765
primrec
haftmann@26442
  3766
  list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool"
haftmann@26442
  3767
where
haftmann@21061
  3768
  "list_ex P [] = False"
haftmann@26442
  3769
  | "list_ex P (x#xs) = (P x \<or> list_ex P xs)"
haftmann@21061
  3770
haftmann@21061
  3771
primrec
haftmann@26442
  3772
  filtermap :: "('a \<Rightarrow> 'b option) \<Rightarrow> 'a list \<Rightarrow> 'b list"
haftmann@26442
  3773
where
haftmann@21061
  3774
  "filtermap f [] = []"
haftmann@26442
  3775
  | "filtermap f (x#xs) =
haftmann@21061
  3776
     (case f x of None \<Rightarrow> filtermap f xs
haftmann@21061
  3777
      | Some y \<Rightarrow> y # filtermap f xs)"
haftmann@21061
  3778
haftmann@21061
  3779
primrec
haftmann@26442
  3780
  map_filter :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list"
haftmann@26442
  3781
where
haftmann@21061
  3782
  "map_filter f P [] = []"
haftmann@26442
  3783
  | "map_filter f P (x#xs) =
haftmann@21061
  3784
     (if P x then f x # map_filter f P xs else map_filter f P xs)"
haftmann@21061
  3785
haftmann@28789
  3786
primrec
haftmann@28789
  3787
  length_unique :: "'a list \<Rightarrow> nat"
haftmann@28789
  3788
where
haftmann@28789
  3789
  "length_unique [] = 0"
haftmann@28789
  3790
  | "length_unique (x#xs) =
haftmann@28789
  3791
      (if x \<in> set xs then length_unique xs else Suc (length_unique xs))"
haftmann@28789
  3792
haftmann@21061
  3793
text {*
wenzelm@21754
  3794
  Only use @{text mem} for generating executable code.  Otherwise use
wenzelm@21754
  3795
  @{prop "x : set xs"} instead --- it is much easier to reason about.
haftmann@21061
  3796
  The same is true for @{const list_all} and @{const list_ex}: write
haftmann@21061
  3797
  @{text "\<forall>x\<in>set xs"} and @{text "\<exists>x\<in>set xs"} instead because the HOL
wenzelm@21754
  3798
  quantifiers are aleady known to the automatic provers. In fact, the
wenzelm@21754
  3799
  declarations in the code subsection make sure that @{text "\<in>"},
wenzelm@21754
  3800
  @{text "\<forall>x\<in>set xs"} and @{text "\<exists>x\<in>set xs"} are implemented
wenzelm@21754
  3801
  efficiently.
haftmann@21061
  3802
haftmann@21061
  3803
  Efficient emptyness check is implemented by @{const null}.
haftmann@21061
  3804
haftmann@23060
  3805
  The functions @{const filtermap} and @{const map_filter} are just
haftmann@23060
  3806
  there to generate efficient code. Do not use
wenzelm@21754
  3807
  them for modelling and proving.
haftmann@21061
  3808
*}
haftmann@21061
  3809
haftmann@23060
  3810
lemma rev_foldl_cons [code]:
haftmann@23060
  3811
  "rev xs = foldl (\<lambda>xs x. x # xs) [] xs"
haftmann@23060
  3812
proof (induct xs)
haftmann@23060
  3813
  case Nil then show ?case by simp
haftmann@23060
  3814
next
haftmann@23060
  3815
  case Cons
haftmann@23060
  3816
  {
haftmann@23060
  3817
    fix x xs ys
haftmann@23060
  3818
    have "foldl (\<lambda>xs x. x # xs) ys xs @ [x]
haftmann@23060
  3819
      = foldl (\<lambda>xs x. x # xs) (ys @ [x]) xs"
haftmann@23060
  3820
    by (induct xs arbitrary: ys) auto
haftmann@23060
  3821
  }
haftmann@23060
  3822
  note aux = this
haftmann@23060
  3823
  show ?case by (induct xs) (auto simp add: Cons aux)
haftmann@23060
  3824
qed
haftmann@23060
  3825
haftmann@24166
  3826
lemma mem_iff [code post]:
haftmann@22422
  3827
  "x mem xs \<longleftrightarrow> x \<in> set xs"
nipkow@24349
  3828
by (induct xs) auto
haftmann@21061
  3829
haftmann@22799
  3830
lemmas in_set_code [code unfold] = mem_iff [symmetric]
haftmann@21061
  3831
haftmann@21061
  3832
lemma empty_null [code inline]:
haftmann@22422
  3833
  "xs = [] \<longleftrightarrow> null xs"
nipkow@24349
  3834
by (cases xs) simp_all
haftmann@21061
  3835
haftmann@24166
  3836
lemmas null_empty [code post] =
haftmann@21061
  3837
  empty_null [symmetric]
haftmann@21061
  3838
haftmann@21061
  3839
lemma list_inter_conv:
haftmann@21061
  3840
  "set (list_inter xs ys) = set xs \<inter> set ys"
nipkow@24349
  3841
by (induct xs) auto
haftmann@21061
  3842
haftmann@24166
  3843
lemma list_all_iff [code post]:
haftmann@22422
  3844
  "list_all P xs \<longleftrightarrow> (\<forall>x \<in> set xs. P x)"
nipkow@24349
  3845
by (induct xs) auto
haftmann@21061
  3846
haftmann@22799
  3847
lemmas list_ball_code [code unfold] = list_all_iff [symmetric]
haftmann@21061
  3848
haftmann@21061
  3849
lemma list_all_append [simp]:
haftmann@22422
  3850
  "list_all P (xs @ ys) \<longleftrightarrow> (list_all P xs \<and> list_all P ys)"
nipkow@24349
  3851
by (induct xs) auto
haftmann@21061
  3852
haftmann@21061
  3853
lemma list_all_rev [simp]:
haftmann@22422
  3854
  "list_all P (rev xs) \<longleftrightarrow> list_all P xs"
nipkow@24349
  3855
by (simp add: list_all_iff)
haftmann@21061
  3856
haftmann@22506
  3857
lemma list_all_length:
haftmann@22506
  3858
  "list_all P xs \<longleftrightarrow> (\<forall>n < length xs. P (xs ! n))"
haftmann@22506
  3859
  unfolding list_all_iff by (auto intro: all_nth_imp_all_set)
haftmann@22506
  3860
haftmann@24166
  3861
lemma list_ex_iff [code post]:
haftmann@22422
  3862
  "list_ex P xs \<longleftrightarrow> (\<exists>x \<in> set xs. P x)"
nipkow@24349
  3863
by (induct xs) simp_all
haftmann@21061
  3864
haftmann@21061
  3865
lemmas list_bex_code [code unfold] =
haftmann@22799
  3866
  list_ex_iff [symmetric]
haftmann@21061
  3867
haftmann@22506
  3868
lemma list_ex_length:
haftmann@22506
  3869
  "list_ex P xs \<longleftrightarrow> (\<exists>n < length xs. P (xs ! n))"
haftmann@22506
  3870
  unfolding list_ex_iff set_conv_nth by auto
haftmann@22506
  3871
haftmann@21061
  3872
lemma filtermap_conv:
haftmann@21061
  3873
   "filtermap f xs = map (\<lambda>x. the (f x)) (filter (\<lambda>x. f x \<noteq> None) xs)"
nipkow@24349
  3874
by (induct xs) (simp_all split: option.split) 
haftmann@21061
  3875
haftmann@21061
  3876
lemma map_filter_conv [simp]:
haftmann@21061
  3877
  "map_filter f P xs = map f (filter P xs)"
nipkow@24349
  3878
by (induct xs) auto
haftmann@21061
  3879
haftmann@28823
  3880
lemma length_remdups_length_unique [code inline]:
haftmann@28789
  3881
  "length (remdups xs) = length_unique xs"
haftmann@28789
  3882
  by (induct xs) simp_all
haftmann@28789
  3883
haftmann@28789
  3884
hide (open) const length_unique
haftmann@28789
  3885
nipkow@24449
  3886
nipkow@24449
  3887
text {* Code for bounded quantification and summation over nats. *}
haftmann@21891
  3888
nipkow@28072
  3889
lemma atMost_upto [code unfold]:
nipkow@28072
  3890
  "{..n} = set [0..<Suc n]"
nipkow@28072
  3891
by auto
nipkow@28072
  3892
nipkow@28072
  3893
lemma atLeast_upt [code unfold]:
nipkow@28072
  3894
  "{..<n} = set [0..<n]"
nipkow@28072
  3895
by auto
nipkow@28072
  3896
nipkow@24449
  3897
lemma greaterThanLessThan_upt [code unfold]:
haftmann@21891
  3898
  "{n<..<m} = set [Suc n..<m]"
nipkow@24349
  3899
by auto
haftmann@22799
  3900
nipkow@24449
  3901
lemma atLeastLessThan_upt [code unfold]:
haftmann@21891
  3902
  "{n..<m} = set [n..<m]"
nipkow@24349
  3903
by auto
haftmann@22799
  3904
nipkow@27715
  3905
lemma greaterThanAtMost_upt [code unfold]:
nipkow@24645
  3906
  "{n<..m} = set [Suc n..<Suc m]"
nipkow@24349
  3907
by auto
haftmann@22799
  3908
nipkow@27715
  3909
lemma atLeastAtMost_upt [code unfold]:
nipkow@24645
  3910
  "{n..m} = set [n..<Suc m]"
nipkow@24349
  3911
by auto
haftmann@22799
  3912
haftmann@22799
  3913
lemma all_nat_less_eq [code unfold]:
haftmann@21891
  3914
  "(\<forall>m<n\<Colon>nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..<n}. P m)"
nipkow@24349
  3915
by auto
haftmann@22799
  3916
haftmann@22799
  3917
lemma ex_nat_less_eq [code unfold]:
haftmann@21891
  3918
  "(\<exists>m<n\<Colon>nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..<n}. P m)"
nipkow@24349
  3919
by auto
haftmann@22799
  3920
haftmann@22799
  3921
lemma all_nat_less [code unfold]:
haftmann@21891
  3922
  "(\<forall>m\<le>n\<Colon>nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..n}. P m)"
nipkow@24349
  3923
by auto
haftmann@22799
  3924
haftmann@22799
  3925
lemma ex_nat_less [code unfold]:
haftmann@21891
  3926
  "(\<exists>m\<le>n\<Colon>nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..n}. P m)"
nipkow@24349
  3927
by auto
haftmann@22799
  3928
nipkow@27715
  3929
lemma setsum_set_distinct_conv_listsum:
nipkow@27715
  3930
  "distinct xs \<Longrightarrow> setsum f (set xs) = listsum (map f xs)"
nipkow@27715
  3931
by (induct xs) simp_all
nipkow@27715
  3932
haftmann@26442
  3933
lemma setsum_set_upt_conv_listsum [code unfold]:
nipkow@27715
  3934
  "setsum f (set [m..<n]) = listsum (map f [m..<n])"
nipkow@27715
  3935
by (rule setsum_set_distinct_conv_listsum) simp
nipkow@27715
  3936
nipkow@27715
  3937
nipkow@27715
  3938
text {* Code for summation over ints. *}
nipkow@27715
  3939
nipkow@27715
  3940
lemma greaterThanLessThan_upto [code unfold]:
nipkow@27715
  3941
  "{i<..<j::int} = set [i+1..j - 1]"
nipkow@27715
  3942
by auto
nipkow@27715
  3943
nipkow@27715
  3944
lemma atLeastLessThan_upto [code unfold]:
nipkow@27715
  3945
  "{i..<j::int} = set [i..j - 1]"
nipkow@27715
  3946
by auto
nipkow@27715
  3947
nipkow@27715
  3948
lemma greaterThanAtMost_upto [code unfold]:
nipkow@27715
  3949
  "{i<..j::int} = set [i+1..j]"
nipkow@27715
  3950
by auto
nipkow@27715
  3951
nipkow@27715
  3952
lemma atLeastAtMost_upto [code unfold]:
nipkow@27715
  3953
  "{i..j::int} = set [i..j]"
nipkow@27715
  3954
by auto
nipkow@27715
  3955
nipkow@27715
  3956
lemma setsum_set_upto_conv_listsum [code unfold]:
nipkow@27715
  3957
  "setsum f (set [i..j::int]) = listsum (map f [i..j])"
nipkow@27715
  3958
by (rule setsum_set_distinct_conv_listsum) simp
nipkow@24449
  3959
wenzelm@23388
  3960
end