src/HOL/List.thy
author nipkow
Tue Sep 04 15:30:31 2007 +0200 (2007-09-04)
changeset 24526 7fa202789bf6
parent 24476 f7ad9fbbeeaa
child 24566 2bfa0215904c
permissions -rw-r--r--
tuned lemma; replaced !! by arbitrary
wenzelm@13462
     1
(*  Title:      HOL/List.thy
wenzelm@13462
     2
    ID:         $Id$
wenzelm@13462
     3
    Author:     Tobias Nipkow
clasohm@923
     4
*)
clasohm@923
     5
wenzelm@13114
     6
header {* The datatype of finite lists *}
wenzelm@13122
     7
nipkow@15131
     8
theory List
haftmann@22844
     9
imports PreList
wenzelm@21754
    10
uses "Tools/string_syntax.ML"
wenzelm@23554
    11
  ("Tools/function_package/lexicographic_order.ML")
wenzelm@23554
    12
  ("Tools/function_package/fundef_datatype.ML")
nipkow@15131
    13
begin
clasohm@923
    14
wenzelm@13142
    15
datatype 'a list =
wenzelm@13366
    16
    Nil    ("[]")
wenzelm@13366
    17
  | Cons 'a  "'a list"    (infixr "#" 65)
clasohm@923
    18
nipkow@15392
    19
subsection{*Basic list processing functions*}
nipkow@15302
    20
clasohm@923
    21
consts
wenzelm@13366
    22
  filter:: "('a => bool) => 'a list => 'a list"
wenzelm@13366
    23
  concat:: "'a list list => 'a list"
wenzelm@13366
    24
  foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b"
wenzelm@13366
    25
  foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b"
wenzelm@13366
    26
  hd:: "'a list => 'a"
wenzelm@13366
    27
  tl:: "'a list => 'a list"
wenzelm@13366
    28
  last:: "'a list => 'a"
wenzelm@13366
    29
  butlast :: "'a list => 'a list"
wenzelm@13366
    30
  set :: "'a list => 'a set"
wenzelm@13366
    31
  map :: "('a=>'b) => ('a list => 'b list)"
nipkow@23096
    32
  listsum ::  "'a list => 'a::monoid_add"
wenzelm@13366
    33
  nth :: "'a list => nat => 'a"    (infixl "!" 100)
wenzelm@13366
    34
  list_update :: "'a list => nat => 'a => 'a list"
wenzelm@13366
    35
  take:: "nat => 'a list => 'a list"
wenzelm@13366
    36
  drop:: "nat => 'a list => 'a list"
wenzelm@13366
    37
  takeWhile :: "('a => bool) => 'a list => 'a list"
wenzelm@13366
    38
  dropWhile :: "('a => bool) => 'a list => 'a list"
wenzelm@13366
    39
  rev :: "'a list => 'a list"
wenzelm@13366
    40
  zip :: "'a list => 'b list => ('a * 'b) list"
nipkow@15425
    41
  upt :: "nat => nat => nat list" ("(1[_..</_'])")
wenzelm@13366
    42
  remdups :: "'a list => 'a list"
nipkow@15110
    43
  remove1 :: "'a => 'a list => 'a list"
wenzelm@13366
    44
  "distinct":: "'a list => bool"
wenzelm@13366
    45
  replicate :: "nat => 'a => 'a list"
nipkow@19390
    46
  splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
nipkow@15302
    47
wenzelm@19363
    48
abbreviation
wenzelm@21404
    49
  upto:: "nat => nat => nat list"  ("(1[_../_])") where
wenzelm@19363
    50
  "[i..j] == [i..<(Suc j)]"
wenzelm@19302
    51
clasohm@923
    52
nipkow@13146
    53
nonterminals lupdbinds lupdbind
nipkow@5077
    54
clasohm@923
    55
syntax
wenzelm@13366
    56
  -- {* list Enumeration *}
wenzelm@13366
    57
  "@list" :: "args => 'a list"    ("[(_)]")
clasohm@923
    58
wenzelm@13366
    59
  -- {* Special syntax for filter *}
nipkow@23279
    60
  "@filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_<-_./ _])")
clasohm@923
    61
wenzelm@13366
    62
  -- {* list update *}
wenzelm@13366
    63
  "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")
wenzelm@13366
    64
  "" :: "lupdbind => lupdbinds"    ("_")
wenzelm@13366
    65
  "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")
wenzelm@13366
    66
  "_LUpdate" :: "['a, lupdbinds] => 'a"    ("_/[(_)]" [900,0] 900)
nipkow@5077
    67
clasohm@923
    68
translations
wenzelm@13366
    69
  "[x, xs]" == "x#[xs]"
wenzelm@13366
    70
  "[x]" == "x#[]"
nipkow@23279
    71
  "[x<-xs . P]"== "filter (%x. P) xs"
clasohm@923
    72
wenzelm@13366
    73
  "_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs"
wenzelm@13366
    74
  "xs[i:=x]" == "list_update xs i x"
nipkow@5077
    75
nipkow@5427
    76
wenzelm@12114
    77
syntax (xsymbols)
nipkow@23279
    78
  "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
kleing@14565
    79
syntax (HTML output)
nipkow@23279
    80
  "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
paulson@3342
    81
paulson@3342
    82
wenzelm@13142
    83
text {*
wenzelm@14589
    84
  Function @{text size} is overloaded for all datatypes. Users may
wenzelm@13366
    85
  refer to the list version as @{text length}. *}
wenzelm@13142
    86
wenzelm@19363
    87
abbreviation
wenzelm@21404
    88
  length :: "'a list => nat" where
wenzelm@19363
    89
  "length == size"
nipkow@15302
    90
berghofe@5183
    91
primrec
paulson@15307
    92
  "hd(x#xs) = x"
paulson@15307
    93
berghofe@5183
    94
primrec
paulson@15307
    95
  "tl([]) = []"
paulson@15307
    96
  "tl(x#xs) = xs"
paulson@15307
    97
berghofe@5183
    98
primrec
paulson@15307
    99
  "last(x#xs) = (if xs=[] then x else last xs)"
paulson@15307
   100
berghofe@5183
   101
primrec
paulson@15307
   102
  "butlast []= []"
paulson@15307
   103
  "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
paulson@15307
   104
berghofe@5183
   105
primrec
paulson@15307
   106
  "set [] = {}"
paulson@15307
   107
  "set (x#xs) = insert x (set xs)"
paulson@15307
   108
berghofe@5183
   109
primrec
paulson@15307
   110
  "map f [] = []"
paulson@15307
   111
  "map f (x#xs) = f(x)#map f xs"
paulson@15307
   112
haftmann@23235
   113
function (*authentic syntax for append -- revert to primrec
haftmann@23235
   114
  as soon as "authentic" primrec is available*)
haftmann@23235
   115
  append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65)
haftmann@23235
   116
where
haftmann@23235
   117
  append_Nil: "[] @ ys = ys"
haftmann@23235
   118
  | append_Cons: "(x # xs) @ ys = x # (xs @ ys)"
haftmann@23235
   119
by (auto, case_tac a, auto)
haftmann@23235
   120
termination by (relation "measure (size o fst)") auto
paulson@15307
   121
berghofe@5183
   122
primrec
paulson@15307
   123
  "rev([]) = []"
paulson@15307
   124
  "rev(x#xs) = rev(xs) @ [x]"
paulson@15307
   125
berghofe@5183
   126
primrec
paulson@15307
   127
  "filter P [] = []"
paulson@15307
   128
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
paulson@15307
   129
berghofe@5183
   130
primrec
paulson@15307
   131
  foldl_Nil:"foldl f a [] = a"
paulson@15307
   132
  foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs"
paulson@15307
   133
paulson@8000
   134
primrec
paulson@15307
   135
  "foldr f [] a = a"
paulson@15307
   136
  "foldr f (x#xs) a = f x (foldr f xs a)"
paulson@15307
   137
berghofe@5183
   138
primrec
paulson@15307
   139
  "concat([]) = []"
paulson@15307
   140
  "concat(x#xs) = x @ concat(xs)"
paulson@15307
   141
berghofe@5183
   142
primrec
nipkow@23096
   143
"listsum [] = 0"
nipkow@23096
   144
"listsum (x # xs) = x + listsum xs"
nipkow@23096
   145
nipkow@23096
   146
primrec
paulson@15307
   147
  drop_Nil:"drop n [] = []"
paulson@15307
   148
  drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
paulson@15307
   149
  -- {*Warning: simpset does not contain this definition, but separate
paulson@15307
   150
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
paulson@15307
   151
berghofe@5183
   152
primrec
paulson@15307
   153
  take_Nil:"take n [] = []"
paulson@15307
   154
  take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
paulson@15307
   155
  -- {*Warning: simpset does not contain this definition, but separate
paulson@15307
   156
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
paulson@15307
   157
wenzelm@13142
   158
primrec
paulson@15307
   159
  nth_Cons:"(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
paulson@15307
   160
  -- {*Warning: simpset does not contain this definition, but separate
paulson@15307
   161
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
paulson@15307
   162
berghofe@5183
   163
primrec
paulson@15307
   164
  "[][i:=v] = []"
paulson@15307
   165
  "(x#xs)[i:=v] = (case i of 0 => v # xs | Suc j => x # xs[j:=v])"
paulson@15307
   166
paulson@15307
   167
primrec
paulson@15307
   168
  "takeWhile P [] = []"
paulson@15307
   169
  "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
paulson@15307
   170
berghofe@5183
   171
primrec
paulson@15307
   172
  "dropWhile P [] = []"
paulson@15307
   173
  "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
paulson@15307
   174
berghofe@5183
   175
primrec
paulson@15307
   176
  "zip xs [] = []"
paulson@15307
   177
  zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"
paulson@15307
   178
  -- {*Warning: simpset does not contain this definition, but separate
paulson@15307
   179
       theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
paulson@15307
   180
nipkow@5427
   181
primrec
nipkow@15425
   182
  upt_0: "[i..<0] = []"
nipkow@15425
   183
  upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])"
paulson@15307
   184
berghofe@5183
   185
primrec
paulson@15307
   186
  "distinct [] = True"
paulson@15307
   187
  "distinct (x#xs) = (x ~: set xs \<and> distinct xs)"
paulson@15307
   188
berghofe@5183
   189
primrec
paulson@15307
   190
  "remdups [] = []"
paulson@15307
   191
  "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
paulson@15307
   192
berghofe@5183
   193
primrec
paulson@15307
   194
  "remove1 x [] = []"
paulson@15307
   195
  "remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)"
paulson@15307
   196
nipkow@15110
   197
primrec
paulson@15307
   198
  replicate_0: "replicate 0 x = []"
paulson@15307
   199
  replicate_Suc: "replicate (Suc n) x = x # replicate n x"
paulson@15307
   200
haftmann@21061
   201
definition
wenzelm@21404
   202
  rotate1 :: "'a list \<Rightarrow> 'a list" where
wenzelm@21404
   203
  "rotate1 xs = (case xs of [] \<Rightarrow> [] | x#xs \<Rightarrow> xs @ [x])"
wenzelm@21404
   204
wenzelm@21404
   205
definition
wenzelm@21404
   206
  rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
wenzelm@21404
   207
  "rotate n = rotate1 ^ n"
wenzelm@21404
   208
wenzelm@21404
   209
definition
wenzelm@21404
   210
  list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" where
wenzelm@21404
   211
  "list_all2 P xs ys =
haftmann@21061
   212
    (length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y))"
wenzelm@21404
   213
wenzelm@21404
   214
definition
wenzelm@21404
   215
  sublist :: "'a list => nat set => 'a list" where
wenzelm@21404
   216
  "sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))"
nipkow@17086
   217
nipkow@17086
   218
primrec
haftmann@21061
   219
  "splice [] ys = ys"
haftmann@21061
   220
  "splice (x#xs) ys = (if ys=[] then x#xs else x # hd ys # splice xs (tl ys))"
haftmann@21061
   221
    -- {*Warning: simpset does not contain the second eqn but a derived one. *}
haftmann@21061
   222
wenzelm@23388
   223
subsubsection {* List comprehension *}
nipkow@23192
   224
nipkow@24349
   225
text{* Input syntax for Haskell-like list comprehension notation.
nipkow@24349
   226
Typical example: @{text"[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]"},
nipkow@24349
   227
the list of all pairs of distinct elements from @{text xs} and @{text ys}.
nipkow@24349
   228
The syntax is as in Haskell, except that @{text"|"} becomes a dot
nipkow@24349
   229
(like in Isabelle's set comprehension): @{text"[e. x \<leftarrow> xs, \<dots>]"} rather than
nipkow@24349
   230
\verb![e| x <- xs, ...]!.
nipkow@24349
   231
nipkow@24349
   232
The qualifiers after the dot are
nipkow@24349
   233
\begin{description}
nipkow@24349
   234
\item[generators] @{text"p \<leftarrow> xs"},
nipkow@24476
   235
 where @{text p} is a pattern and @{text xs} an expression of list type, or
nipkow@24476
   236
\item[guards] @{text"b"}, where @{text b} is a boolean expression.
nipkow@24476
   237
%\item[local bindings] @ {text"let x = e"}.
nipkow@24349
   238
\end{description}
nipkow@23240
   239
nipkow@24476
   240
Just like in Haskell, list comprehension is just a shorthand. To avoid
nipkow@24476
   241
misunderstandings, the translation into desugared form is not reversed
nipkow@24476
   242
upon output. Note that the translation of @{text"[e. x \<leftarrow> xs]"} is
nipkow@24476
   243
optmized to @{term"map (%x. e) xs"}.
nipkow@23240
   244
nipkow@24349
   245
It is easy to write short list comprehensions which stand for complex
nipkow@24349
   246
expressions. During proofs, they may become unreadable (and
nipkow@24349
   247
mangled). In such cases it can be advisable to introduce separate
nipkow@24349
   248
definitions for the list comprehensions in question.  *}
nipkow@24349
   249
nipkow@23209
   250
(*
nipkow@23240
   251
Proper theorem proving support would be nice. For example, if
nipkow@23192
   252
@{text"set[f x y. x \<leftarrow> xs, y \<leftarrow> ys, P x y]"}
nipkow@23192
   253
produced something like
nipkow@23209
   254
@{term"{z. EX x: set xs. EX y:set ys. P x y \<and> z = f x y}"}.
nipkow@23209
   255
*)
nipkow@23209
   256
nipkow@23240
   257
nonterminals lc_qual lc_quals
nipkow@23192
   258
nipkow@23192
   259
syntax
nipkow@23240
   260
"_listcompr" :: "'a \<Rightarrow> lc_qual \<Rightarrow> lc_quals \<Rightarrow> 'a list"  ("[_ . __")
nipkow@24349
   261
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ <- _")
nipkow@23240
   262
"_lc_test" :: "bool \<Rightarrow> lc_qual" ("_")
nipkow@24476
   263
(*"_lc_let" :: "letbinds => lc_qual"  ("let _")*)
nipkow@23240
   264
"_lc_end" :: "lc_quals" ("]")
nipkow@23240
   265
"_lc_quals" :: "lc_qual \<Rightarrow> lc_quals \<Rightarrow> lc_quals" (", __")
nipkow@24349
   266
"_lc_abs" :: "'a => 'b list => 'b list"
nipkow@23192
   267
nipkow@24476
   268
(* These are easier than ML code but cannot express the optimized
nipkow@24476
   269
   translation of [e. p<-xs]
nipkow@23192
   270
translations
nipkow@24349
   271
"[e. p<-xs]" => "concat(map (_lc_abs p [e]) xs)"
nipkow@23240
   272
"_listcompr e (_lc_gen p xs) (_lc_quals Q Qs)"
nipkow@24349
   273
 => "concat (map (_lc_abs p (_listcompr e Q Qs)) xs)"
nipkow@23240
   274
"[e. P]" => "if P then [e] else []"
nipkow@23240
   275
"_listcompr e (_lc_test P) (_lc_quals Q Qs)"
nipkow@23240
   276
 => "if P then (_listcompr e Q Qs) else []"
nipkow@24349
   277
"_listcompr e (_lc_let b) (_lc_quals Q Qs)"
nipkow@24349
   278
 => "_Let b (_listcompr e Q Qs)"
nipkow@24476
   279
*)
nipkow@23240
   280
nipkow@23279
   281
syntax (xsymbols)
nipkow@24349
   282
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _")
nipkow@23279
   283
syntax (HTML output)
nipkow@24349
   284
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _")
nipkow@24349
   285
nipkow@24349
   286
parse_translation (advanced) {*
nipkow@24349
   287
let
nipkow@24476
   288
  val NilC = Syntax.const @{const_name Nil};
nipkow@24476
   289
  val ConsC = Syntax.const @{const_name Cons};
nipkow@24476
   290
  val mapC = Syntax.const @{const_name map};
nipkow@24476
   291
  val concatC = Syntax.const @{const_name concat};
nipkow@24476
   292
  val IfC = Syntax.const @{const_name If};
nipkow@24476
   293
  fun singl x = ConsC $ x $ NilC;
nipkow@24476
   294
nipkow@24476
   295
   fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *)
nipkow@24349
   296
    let
nipkow@24476
   297
      val x = Free (Name.variant (add_term_free_names (p$e, [])) "x", dummyT);
nipkow@24476
   298
      val e = if opti then singl e else e;
nipkow@24476
   299
      val case1 = Syntax.const "_case1" $ p $ e;
nipkow@24349
   300
      val case2 = Syntax.const "_case1" $ Syntax.const Term.dummy_patternN
nipkow@24476
   301
                                        $ NilC;
nipkow@24349
   302
      val cs = Syntax.const "_case2" $ case1 $ case2
nipkow@24349
   303
      val ft = DatatypeCase.case_tr false DatatypePackage.datatype_of_constr
nipkow@24349
   304
                 ctxt [x, cs]
nipkow@24349
   305
    in lambda x ft end;
nipkow@24349
   306
nipkow@24476
   307
  fun abs_tr ctxt (p as Free(s,T)) e opti =
nipkow@24349
   308
        let val thy = ProofContext.theory_of ctxt;
nipkow@24349
   309
            val s' = Sign.intern_const thy s
nipkow@24476
   310
        in if Sign.declared_const thy s'
nipkow@24476
   311
           then (pat_tr ctxt p e opti, false)
nipkow@24476
   312
           else (lambda p e, true)
nipkow@24349
   313
        end
nipkow@24476
   314
    | abs_tr ctxt p e opti = (pat_tr ctxt p e opti, false);
nipkow@24476
   315
nipkow@24476
   316
  fun lc_tr ctxt [e, Const("_lc_test",_)$b, qs] =
nipkow@24476
   317
        let val res = case qs of Const("_lc_end",_) => singl e
nipkow@24476
   318
                      | Const("_lc_quals",_)$q$qs => lc_tr ctxt [e,q,qs];
nipkow@24476
   319
        in IfC $ b $ res $ NilC end
nipkow@24476
   320
    | lc_tr ctxt [e, Const("_lc_gen",_) $ p $ es, Const("_lc_end",_)] =
nipkow@24476
   321
        (case abs_tr ctxt p e true of
nipkow@24476
   322
           (f,true) => mapC $ f $ es
nipkow@24476
   323
         | (f, false) => concatC $ (mapC $ f $ es))
nipkow@24476
   324
    | lc_tr ctxt [e, Const("_lc_gen",_) $ p $ es, Const("_lc_quals",_)$q$qs] =
nipkow@24476
   325
        let val e' = lc_tr ctxt [e,q,qs];
nipkow@24476
   326
        in concatC $ (mapC $ (fst(abs_tr ctxt p e' false)) $ es) end
nipkow@24476
   327
nipkow@24476
   328
in [("_listcompr", lc_tr)] end
nipkow@24349
   329
*}
nipkow@23279
   330
nipkow@23240
   331
(*
nipkow@23240
   332
term "[(x,y,z). b]"
nipkow@24476
   333
term "[(x,y,z). x\<leftarrow>xs]"
nipkow@24476
   334
term "[e x y. x\<leftarrow>xs, y\<leftarrow>ys]"
nipkow@24476
   335
term "[(x,y,z). x<a, x>b]"
nipkow@24476
   336
term "[(x,y,z). x\<leftarrow>xs, x>b]"
nipkow@24476
   337
term "[(x,y,z). x<a, x\<leftarrow>xs]"
nipkow@24349
   338
term "[(x,y). Cons True x \<leftarrow> xs]"
nipkow@24349
   339
term "[(x,y,z). Cons x [] \<leftarrow> xs]"
nipkow@23240
   340
term "[(x,y,z). x<a, x>b, x=d]"
nipkow@23240
   341
term "[(x,y,z). x<a, x>b, y\<leftarrow>ys]"
nipkow@23240
   342
term "[(x,y,z). x<a, x\<leftarrow>xs,y>b]"
nipkow@23240
   343
term "[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]"
nipkow@23240
   344
term "[(x,y,z). x\<leftarrow>xs, x>b, y<a]"
nipkow@23240
   345
term "[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]"
nipkow@23240
   346
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x]"
nipkow@23240
   347
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]"
nipkow@24349
   348
term "[(x,y). x\<leftarrow>xs, let xx = x+x, y\<leftarrow>ys, y \<noteq> xx]"
nipkow@23192
   349
*)
nipkow@23192
   350
haftmann@21061
   351
subsubsection {* @{const Nil} and @{const Cons} *}
haftmann@21061
   352
haftmann@21061
   353
lemma not_Cons_self [simp]:
haftmann@21061
   354
  "xs \<noteq> x # xs"
nipkow@13145
   355
by (induct xs) auto
wenzelm@13114
   356
wenzelm@13142
   357
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric]
wenzelm@13114
   358
wenzelm@13142
   359
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
nipkow@13145
   360
by (induct xs) auto
wenzelm@13114
   361
wenzelm@13142
   362
lemma length_induct:
haftmann@21061
   363
  "(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs"
nipkow@17589
   364
by (rule measure_induct [of length]) iprover
wenzelm@13114
   365
wenzelm@13114
   366
haftmann@21061
   367
subsubsection {* @{const length} *}
wenzelm@13114
   368
wenzelm@13142
   369
text {*
haftmann@21061
   370
  Needs to come before @{text "@"} because of theorem @{text
haftmann@21061
   371
  append_eq_append_conv}.
wenzelm@13142
   372
*}
wenzelm@13114
   373
wenzelm@13142
   374
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
nipkow@13145
   375
by (induct xs) auto
wenzelm@13114
   376
wenzelm@13142
   377
lemma length_map [simp]: "length (map f xs) = length xs"
nipkow@13145
   378
by (induct xs) auto
wenzelm@13114
   379
wenzelm@13142
   380
lemma length_rev [simp]: "length (rev xs) = length xs"
nipkow@13145
   381
by (induct xs) auto
wenzelm@13114
   382
wenzelm@13142
   383
lemma length_tl [simp]: "length (tl xs) = length xs - 1"
nipkow@13145
   384
by (cases xs) auto
wenzelm@13114
   385
wenzelm@13142
   386
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
nipkow@13145
   387
by (induct xs) auto
wenzelm@13114
   388
wenzelm@13142
   389
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
nipkow@13145
   390
by (induct xs) auto
wenzelm@13114
   391
nipkow@23479
   392
lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0"
nipkow@23479
   393
by auto
nipkow@23479
   394
wenzelm@13114
   395
lemma length_Suc_conv:
nipkow@13145
   396
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
nipkow@13145
   397
by (induct xs) auto
wenzelm@13142
   398
nipkow@14025
   399
lemma Suc_length_conv:
nipkow@14025
   400
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
paulson@14208
   401
apply (induct xs, simp, simp)
nipkow@14025
   402
apply blast
nipkow@14025
   403
done
nipkow@14025
   404
oheimb@14099
   405
lemma impossible_Cons [rule_format]: 
oheimb@14099
   406
  "length xs <= length ys --> xs = x # ys = False"
wenzelm@20503
   407
apply (induct xs)
wenzelm@20503
   408
apply auto
oheimb@14099
   409
done
oheimb@14099
   410
nipkow@24526
   411
lemma list_induct2[consumes 1]:
nipkow@24526
   412
  "\<lbrakk> length xs = length ys;
nipkow@14247
   413
   P [] [];
nipkow@14247
   414
   \<And>x xs y ys. \<lbrakk> length xs = length ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk>
nipkow@14247
   415
 \<Longrightarrow> P xs ys"
nipkow@24526
   416
apply(induct xs arbitrary: ys)
nipkow@14247
   417
 apply simp
nipkow@14247
   418
apply(case_tac ys)
nipkow@14247
   419
 apply simp
nipkow@14247
   420
apply(simp)
nipkow@14247
   421
done
wenzelm@13114
   422
krauss@22493
   423
lemma list_induct2': 
krauss@22493
   424
  "\<lbrakk> P [] [];
krauss@22493
   425
  \<And>x xs. P (x#xs) [];
krauss@22493
   426
  \<And>y ys. P [] (y#ys);
krauss@22493
   427
   \<And>x xs y ys. P xs ys  \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk>
krauss@22493
   428
 \<Longrightarrow> P xs ys"
krauss@22493
   429
by (induct xs arbitrary: ys) (case_tac x, auto)+
krauss@22493
   430
nipkow@22143
   431
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False"
nipkow@24349
   432
by (rule Eq_FalseI) auto
wenzelm@24037
   433
wenzelm@24037
   434
simproc_setup list_neq ("(xs::'a list) = ys") = {*
nipkow@22143
   435
(*
nipkow@22143
   436
Reduces xs=ys to False if xs and ys cannot be of the same length.
nipkow@22143
   437
This is the case if the atomic sublists of one are a submultiset
nipkow@22143
   438
of those of the other list and there are fewer Cons's in one than the other.
nipkow@22143
   439
*)
wenzelm@24037
   440
wenzelm@24037
   441
let
nipkow@22143
   442
nipkow@22143
   443
fun len (Const("List.list.Nil",_)) acc = acc
nipkow@22143
   444
  | len (Const("List.list.Cons",_) $ _ $ xs) (ts,n) = len xs (ts,n+1)
haftmann@23029
   445
  | len (Const("List.append",_) $ xs $ ys) acc = len xs (len ys acc)
nipkow@22143
   446
  | len (Const("List.rev",_) $ xs) acc = len xs acc
nipkow@22143
   447
  | len (Const("List.map",_) $ _ $ xs) acc = len xs acc
nipkow@22143
   448
  | len t (ts,n) = (t::ts,n);
nipkow@22143
   449
wenzelm@24037
   450
fun list_neq _ ss ct =
nipkow@22143
   451
  let
wenzelm@24037
   452
    val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct;
nipkow@22143
   453
    val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0);
nipkow@22143
   454
    fun prove_neq() =
nipkow@22143
   455
      let
nipkow@22143
   456
        val Type(_,listT::_) = eqT;
haftmann@22994
   457
        val size = HOLogic.size_const listT;
nipkow@22143
   458
        val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs);
nipkow@22143
   459
        val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len);
nipkow@22143
   460
        val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len
haftmann@22633
   461
          (K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1));
haftmann@22633
   462
      in SOME (thm RS @{thm neq_if_length_neq}) end
nipkow@22143
   463
  in
wenzelm@23214
   464
    if m < n andalso submultiset (op aconv) (ls,rs) orelse
wenzelm@23214
   465
       n < m andalso submultiset (op aconv) (rs,ls)
nipkow@22143
   466
    then prove_neq() else NONE
nipkow@22143
   467
  end;
wenzelm@24037
   468
in list_neq end;
nipkow@22143
   469
*}
nipkow@22143
   470
nipkow@22143
   471
nipkow@15392
   472
subsubsection {* @{text "@"} -- append *}
wenzelm@13114
   473
wenzelm@13142
   474
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
nipkow@13145
   475
by (induct xs) auto
wenzelm@13114
   476
wenzelm@13142
   477
lemma append_Nil2 [simp]: "xs @ [] = xs"
nipkow@13145
   478
by (induct xs) auto
nipkow@3507
   479
nipkow@24449
   480
interpretation semigroup_append: semigroup_add ["op @"]
nipkow@24449
   481
by unfold_locales simp
nipkow@24449
   482
interpretation monoid_append: monoid_add ["[]" "op @"]
nipkow@24449
   483
by unfold_locales (simp+)
nipkow@24449
   484
wenzelm@13142
   485
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
nipkow@13145
   486
by (induct xs) auto
wenzelm@13114
   487
wenzelm@13142
   488
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
nipkow@13145
   489
by (induct xs) auto
wenzelm@13114
   490
wenzelm@13142
   491
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
nipkow@13145
   492
by (induct xs) auto
wenzelm@13114
   493
wenzelm@13142
   494
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
nipkow@13145
   495
by (induct xs) auto
wenzelm@13114
   496
paulson@24286
   497
lemma append_eq_append_conv [simp,noatp]:
nipkow@24526
   498
 "length xs = length ys \<or> length us = length vs
berghofe@13883
   499
 ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
nipkow@24526
   500
apply (induct xs arbitrary: ys)
paulson@14208
   501
 apply (case_tac ys, simp, force)
paulson@14208
   502
apply (case_tac ys, force, simp)
nipkow@13145
   503
done
wenzelm@13142
   504
nipkow@24526
   505
lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) =
nipkow@24526
   506
  (EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)"
nipkow@24526
   507
apply (induct xs arbitrary: ys zs ts)
nipkow@14495
   508
 apply fastsimp
nipkow@14495
   509
apply(case_tac zs)
nipkow@14495
   510
 apply simp
nipkow@14495
   511
apply fastsimp
nipkow@14495
   512
done
nipkow@14495
   513
wenzelm@13142
   514
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)"
nipkow@13145
   515
by simp
wenzelm@13142
   516
wenzelm@13142
   517
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
nipkow@13145
   518
by simp
wenzelm@13114
   519
wenzelm@13142
   520
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)"
nipkow@13145
   521
by simp
wenzelm@13114
   522
wenzelm@13142
   523
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
nipkow@13145
   524
using append_same_eq [of _ _ "[]"] by auto
nipkow@3507
   525
wenzelm@13142
   526
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
nipkow@13145
   527
using append_same_eq [of "[]"] by auto
wenzelm@13114
   528
paulson@24286
   529
lemma hd_Cons_tl [simp,noatp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
nipkow@13145
   530
by (induct xs) auto
wenzelm@13114
   531
wenzelm@13142
   532
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
nipkow@13145
   533
by (induct xs) auto
wenzelm@13114
   534
wenzelm@13142
   535
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
nipkow@13145
   536
by (simp add: hd_append split: list.split)
wenzelm@13114
   537
wenzelm@13142
   538
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
nipkow@13145
   539
by (simp split: list.split)
wenzelm@13114
   540
wenzelm@13142
   541
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
nipkow@13145
   542
by (simp add: tl_append split: list.split)
wenzelm@13114
   543
wenzelm@13114
   544
nipkow@14300
   545
lemma Cons_eq_append_conv: "x#xs = ys@zs =
nipkow@14300
   546
 (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))"
nipkow@14300
   547
by(cases ys) auto
nipkow@14300
   548
nipkow@15281
   549
lemma append_eq_Cons_conv: "(ys@zs = x#xs) =
nipkow@15281
   550
 (ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))"
nipkow@15281
   551
by(cases ys) auto
nipkow@15281
   552
nipkow@14300
   553
wenzelm@13142
   554
text {* Trivial rules for solving @{text "@"}-equations automatically. *}
wenzelm@13114
   555
wenzelm@13114
   556
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
nipkow@13145
   557
by simp
wenzelm@13114
   558
wenzelm@13142
   559
lemma Cons_eq_appendI:
nipkow@13145
   560
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
nipkow@13145
   561
by (drule sym) simp
wenzelm@13114
   562
wenzelm@13142
   563
lemma append_eq_appendI:
nipkow@13145
   564
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
nipkow@13145
   565
by (drule sym) simp
wenzelm@13114
   566
wenzelm@13114
   567
wenzelm@13142
   568
text {*
nipkow@13145
   569
Simplification procedure for all list equalities.
nipkow@13145
   570
Currently only tries to rearrange @{text "@"} to see if
nipkow@13145
   571
- both lists end in a singleton list,
nipkow@13145
   572
- or both lists end in the same list.
wenzelm@13142
   573
*}
wenzelm@13142
   574
wenzelm@13142
   575
ML_setup {*
nipkow@3507
   576
local
nipkow@3507
   577
wenzelm@13114
   578
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
wenzelm@13462
   579
  (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
haftmann@23029
   580
  | last (Const("List.append",_) $ _ $ ys) = last ys
wenzelm@13462
   581
  | last t = t;
wenzelm@13114
   582
wenzelm@13114
   583
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
wenzelm@13462
   584
  | list1 _ = false;
wenzelm@13114
   585
wenzelm@13114
   586
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
wenzelm@13462
   587
  (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
haftmann@23029
   588
  | butlast ((app as Const("List.append",_) $ xs) $ ys) = app $ butlast ys
wenzelm@13462
   589
  | butlast xs = Const("List.list.Nil",fastype_of xs);
wenzelm@13114
   590
haftmann@22633
   591
val rearr_ss = HOL_basic_ss addsimps [@{thm append_assoc},
haftmann@22633
   592
  @{thm append_Nil}, @{thm append_Cons}];
wenzelm@16973
   593
wenzelm@20044
   594
fun list_eq ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
wenzelm@13462
   595
  let
wenzelm@13462
   596
    val lastl = last lhs and lastr = last rhs;
wenzelm@13462
   597
    fun rearr conv =
wenzelm@13462
   598
      let
wenzelm@13462
   599
        val lhs1 = butlast lhs and rhs1 = butlast rhs;
wenzelm@13462
   600
        val Type(_,listT::_) = eqT
wenzelm@13462
   601
        val appT = [listT,listT] ---> listT
haftmann@23029
   602
        val app = Const("List.append",appT)
wenzelm@13462
   603
        val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
wenzelm@13480
   604
        val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2));
wenzelm@20044
   605
        val thm = Goal.prove (Simplifier.the_context ss) [] [] eq
wenzelm@17877
   606
          (K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1));
skalberg@15531
   607
      in SOME ((conv RS (thm RS trans)) RS eq_reflection) end;
wenzelm@13114
   608
wenzelm@13462
   609
  in
haftmann@22633
   610
    if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv}
haftmann@22633
   611
    else if lastl aconv lastr then rearr @{thm append_same_eq}
skalberg@15531
   612
    else NONE
wenzelm@13462
   613
  end;
wenzelm@13462
   614
wenzelm@13114
   615
in
wenzelm@13462
   616
wenzelm@13462
   617
val list_eq_simproc =
haftmann@22633
   618
  Simplifier.simproc @{theory} "list_eq" ["(xs::'a list) = ys"] (K list_eq);
wenzelm@13462
   619
wenzelm@13114
   620
end;
wenzelm@13114
   621
wenzelm@13114
   622
Addsimprocs [list_eq_simproc];
wenzelm@13114
   623
*}
wenzelm@13114
   624
wenzelm@13114
   625
nipkow@15392
   626
subsubsection {* @{text map} *}
wenzelm@13114
   627
wenzelm@13142
   628
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
nipkow@13145
   629
by (induct xs) simp_all
wenzelm@13114
   630
wenzelm@13142
   631
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
nipkow@13145
   632
by (rule ext, induct_tac xs) auto
wenzelm@13114
   633
wenzelm@13142
   634
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
nipkow@13145
   635
by (induct xs) auto
wenzelm@13114
   636
wenzelm@13142
   637
lemma map_compose: "map (f o g) xs = map f (map g xs)"
nipkow@13145
   638
by (induct xs) (auto simp add: o_def)
wenzelm@13114
   639
wenzelm@13142
   640
lemma rev_map: "rev (map f xs) = map f (rev xs)"
nipkow@13145
   641
by (induct xs) auto
wenzelm@13114
   642
nipkow@13737
   643
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)"
nipkow@13737
   644
by (induct xs) auto
nipkow@13737
   645
krauss@19770
   646
lemma map_cong [fundef_cong, recdef_cong]:
nipkow@13145
   647
"xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys"
nipkow@13145
   648
-- {* a congruence rule for @{text map} *}
nipkow@13737
   649
by simp
wenzelm@13114
   650
wenzelm@13142
   651
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
nipkow@13145
   652
by (cases xs) auto
wenzelm@13114
   653
wenzelm@13142
   654
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
nipkow@13145
   655
by (cases xs) auto
wenzelm@13114
   656
paulson@18447
   657
lemma map_eq_Cons_conv:
nipkow@14025
   658
 "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)"
nipkow@13145
   659
by (cases xs) auto
wenzelm@13114
   660
paulson@18447
   661
lemma Cons_eq_map_conv:
nipkow@14025
   662
 "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)"
nipkow@14025
   663
by (cases ys) auto
nipkow@14025
   664
paulson@18447
   665
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1]
paulson@18447
   666
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1]
paulson@18447
   667
declare map_eq_Cons_D [dest!]  Cons_eq_map_D [dest!]
paulson@18447
   668
nipkow@14111
   669
lemma ex_map_conv:
nipkow@14111
   670
  "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)"
paulson@18447
   671
by(induct ys, auto simp add: Cons_eq_map_conv)
nipkow@14111
   672
nipkow@15110
   673
lemma map_eq_imp_length_eq:
nipkow@24526
   674
  "map f xs = map f ys ==> length xs = length ys"
nipkow@24526
   675
apply (induct ys arbitrary: xs)
nipkow@15110
   676
 apply simp
nipkow@15110
   677
apply(simp (no_asm_use))
nipkow@15110
   678
apply clarify
nipkow@15110
   679
apply(simp (no_asm_use))
nipkow@15110
   680
apply fast
nipkow@15110
   681
done
nipkow@15110
   682
nipkow@15110
   683
lemma map_inj_on:
nipkow@15110
   684
 "[| map f xs = map f ys; inj_on f (set xs Un set ys) |]
nipkow@15110
   685
  ==> xs = ys"
nipkow@15110
   686
apply(frule map_eq_imp_length_eq)
nipkow@15110
   687
apply(rotate_tac -1)
nipkow@15110
   688
apply(induct rule:list_induct2)
nipkow@15110
   689
 apply simp
nipkow@15110
   690
apply(simp)
nipkow@15110
   691
apply (blast intro:sym)
nipkow@15110
   692
done
nipkow@15110
   693
nipkow@15110
   694
lemma inj_on_map_eq_map:
nipkow@15110
   695
 "inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@15110
   696
by(blast dest:map_inj_on)
nipkow@15110
   697
wenzelm@13114
   698
lemma map_injective:
nipkow@24526
   699
 "map f xs = map f ys ==> inj f ==> xs = ys"
nipkow@24526
   700
by (induct ys arbitrary: xs) (auto dest!:injD)
wenzelm@13114
   701
nipkow@14339
   702
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@14339
   703
by(blast dest:map_injective)
nipkow@14339
   704
wenzelm@13114
   705
lemma inj_mapI: "inj f ==> inj (map f)"
nipkow@17589
   706
by (iprover dest: map_injective injD intro: inj_onI)
wenzelm@13114
   707
wenzelm@13114
   708
lemma inj_mapD: "inj (map f) ==> inj f"
paulson@14208
   709
apply (unfold inj_on_def, clarify)
nipkow@13145
   710
apply (erule_tac x = "[x]" in ballE)
paulson@14208
   711
 apply (erule_tac x = "[y]" in ballE, simp, blast)
nipkow@13145
   712
apply blast
nipkow@13145
   713
done
wenzelm@13114
   714
nipkow@14339
   715
lemma inj_map[iff]: "inj (map f) = inj f"
nipkow@13145
   716
by (blast dest: inj_mapD intro: inj_mapI)
wenzelm@13114
   717
nipkow@15303
   718
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A"
nipkow@15303
   719
apply(rule inj_onI)
nipkow@15303
   720
apply(erule map_inj_on)
nipkow@15303
   721
apply(blast intro:inj_onI dest:inj_onD)
nipkow@15303
   722
done
nipkow@15303
   723
kleing@14343
   724
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs"
kleing@14343
   725
by (induct xs, auto)
wenzelm@13114
   726
nipkow@14402
   727
lemma map_fun_upd [simp]: "y \<notin> set xs \<Longrightarrow> map (f(y:=v)) xs = map f xs"
nipkow@14402
   728
by (induct xs) auto
nipkow@14402
   729
nipkow@15110
   730
lemma map_fst_zip[simp]:
nipkow@15110
   731
  "length xs = length ys \<Longrightarrow> map fst (zip xs ys) = xs"
nipkow@15110
   732
by (induct rule:list_induct2, simp_all)
nipkow@15110
   733
nipkow@15110
   734
lemma map_snd_zip[simp]:
nipkow@15110
   735
  "length xs = length ys \<Longrightarrow> map snd (zip xs ys) = ys"
nipkow@15110
   736
by (induct rule:list_induct2, simp_all)
nipkow@15110
   737
nipkow@15110
   738
nipkow@15392
   739
subsubsection {* @{text rev} *}
wenzelm@13114
   740
wenzelm@13142
   741
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
nipkow@13145
   742
by (induct xs) auto
wenzelm@13114
   743
wenzelm@13142
   744
lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
nipkow@13145
   745
by (induct xs) auto
wenzelm@13114
   746
kleing@15870
   747
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)"
kleing@15870
   748
by auto
kleing@15870
   749
wenzelm@13142
   750
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
nipkow@13145
   751
by (induct xs) auto
wenzelm@13114
   752
wenzelm@13142
   753
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
nipkow@13145
   754
by (induct xs) auto
wenzelm@13114
   755
kleing@15870
   756
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])"
kleing@15870
   757
by (cases xs) auto
kleing@15870
   758
kleing@15870
   759
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])"
kleing@15870
   760
by (cases xs) auto
kleing@15870
   761
haftmann@21061
   762
lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)"
haftmann@21061
   763
apply (induct xs arbitrary: ys, force)
paulson@14208
   764
apply (case_tac ys, simp, force)
nipkow@13145
   765
done
wenzelm@13114
   766
nipkow@15439
   767
lemma inj_on_rev[iff]: "inj_on rev A"
nipkow@15439
   768
by(simp add:inj_on_def)
nipkow@15439
   769
wenzelm@13366
   770
lemma rev_induct [case_names Nil snoc]:
wenzelm@13366
   771
  "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
berghofe@15489
   772
apply(simplesubst rev_rev_ident[symmetric])
nipkow@13145
   773
apply(rule_tac list = "rev xs" in list.induct, simp_all)
nipkow@13145
   774
done
wenzelm@13114
   775
nipkow@13145
   776
ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}-- "compatibility"
wenzelm@13114
   777
wenzelm@13366
   778
lemma rev_exhaust [case_names Nil snoc]:
wenzelm@13366
   779
  "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"
nipkow@13145
   780
by (induct xs rule: rev_induct) auto
wenzelm@13114
   781
wenzelm@13366
   782
lemmas rev_cases = rev_exhaust
wenzelm@13366
   783
nipkow@18423
   784
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])"
nipkow@18423
   785
by(rule rev_cases[of xs]) auto
nipkow@18423
   786
wenzelm@13114
   787
nipkow@15392
   788
subsubsection {* @{text set} *}
wenzelm@13114
   789
wenzelm@13142
   790
lemma finite_set [iff]: "finite (set xs)"
nipkow@13145
   791
by (induct xs) auto
wenzelm@13114
   792
wenzelm@13142
   793
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
nipkow@13145
   794
by (induct xs) auto
wenzelm@13114
   795
nipkow@17830
   796
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs"
nipkow@17830
   797
by(cases xs) auto
oheimb@14099
   798
wenzelm@13142
   799
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
nipkow@13145
   800
by auto
wenzelm@13114
   801
oheimb@14099
   802
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" 
oheimb@14099
   803
by auto
oheimb@14099
   804
wenzelm@13142
   805
lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
nipkow@13145
   806
by (induct xs) auto
wenzelm@13114
   807
nipkow@15245
   808
lemma set_empty2[iff]: "({} = set xs) = (xs = [])"
nipkow@15245
   809
by(induct xs) auto
nipkow@15245
   810
wenzelm@13142
   811
lemma set_rev [simp]: "set (rev xs) = set xs"
nipkow@13145
   812
by (induct xs) auto
wenzelm@13114
   813
wenzelm@13142
   814
lemma set_map [simp]: "set (map f xs) = f`(set xs)"
nipkow@13145
   815
by (induct xs) auto
wenzelm@13114
   816
wenzelm@13142
   817
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
nipkow@13145
   818
by (induct xs) auto
wenzelm@13114
   819
nipkow@15425
   820
lemma set_upt [simp]: "set[i..<j] = {k. i \<le> k \<and> k < j}"
paulson@14208
   821
apply (induct j, simp_all)
paulson@14208
   822
apply (erule ssubst, auto)
nipkow@13145
   823
done
wenzelm@13114
   824
wenzelm@13142
   825
lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)"
paulson@15113
   826
proof (induct xs)
paulson@15113
   827
  case Nil show ?case by simp
paulson@15113
   828
  case (Cons a xs)
paulson@15113
   829
  show ?case
paulson@15113
   830
  proof 
paulson@15113
   831
    assume "x \<in> set (a # xs)"
paulson@15113
   832
    with prems show "\<exists>ys zs. a # xs = ys @ x # zs"
paulson@15113
   833
      by (simp, blast intro: Cons_eq_appendI)
paulson@15113
   834
  next
paulson@15113
   835
    assume "\<exists>ys zs. a # xs = ys @ x # zs"
paulson@15113
   836
    then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast
paulson@15113
   837
    show "x \<in> set (a # xs)" 
paulson@15113
   838
      by (cases ys, auto simp add: eq)
paulson@15113
   839
  qed
paulson@15113
   840
qed
wenzelm@13142
   841
nipkow@18049
   842
lemma in_set_conv_decomp_first:
nipkow@18049
   843
 "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)"
nipkow@18049
   844
proof (induct xs)
nipkow@18049
   845
  case Nil show ?case by simp
nipkow@18049
   846
next
nipkow@18049
   847
  case (Cons a xs)
nipkow@18049
   848
  show ?case
nipkow@18049
   849
  proof cases
nipkow@18049
   850
    assume "x = a" thus ?case using Cons by force
nipkow@18049
   851
  next
nipkow@18049
   852
    assume "x \<noteq> a"
nipkow@18049
   853
    show ?case
nipkow@18049
   854
    proof
nipkow@18049
   855
      assume "x \<in> set (a # xs)"
nipkow@18049
   856
      from prems show "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys"
nipkow@18049
   857
	by(fastsimp intro!: Cons_eq_appendI)
nipkow@18049
   858
    next
nipkow@18049
   859
      assume "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys"
nipkow@18049
   860
      then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast
nipkow@18049
   861
      show "x \<in> set (a # xs)" by (cases ys, auto simp add: eq)
nipkow@18049
   862
    qed
nipkow@18049
   863
  qed
nipkow@18049
   864
qed
nipkow@18049
   865
nipkow@18049
   866
lemmas split_list       = in_set_conv_decomp[THEN iffD1, standard]
nipkow@18049
   867
lemmas split_list_first = in_set_conv_decomp_first[THEN iffD1, standard]
nipkow@18049
   868
nipkow@18049
   869
paulson@13508
   870
lemma finite_list: "finite A ==> EX l. set l = A"
paulson@13508
   871
apply (erule finite_induct, auto)
paulson@13508
   872
apply (rule_tac x="x#l" in exI, auto)
paulson@13508
   873
done
paulson@13508
   874
kleing@14388
   875
lemma card_length: "card (set xs) \<le> length xs"
kleing@14388
   876
by (induct xs) (auto simp add: card_insert_if)
wenzelm@13114
   877
paulson@15168
   878
nipkow@15392
   879
subsubsection {* @{text filter} *}
wenzelm@13114
   880
wenzelm@13142
   881
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
nipkow@13145
   882
by (induct xs) auto
wenzelm@13114
   883
nipkow@15305
   884
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)"
nipkow@15305
   885
by (induct xs) simp_all
nipkow@15305
   886
wenzelm@13142
   887
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
nipkow@13145
   888
by (induct xs) auto
wenzelm@13114
   889
nipkow@16998
   890
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs"
nipkow@16998
   891
by (induct xs) (auto simp add: le_SucI)
nipkow@16998
   892
nipkow@18423
   893
lemma sum_length_filter_compl:
nipkow@18423
   894
  "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs"
nipkow@18423
   895
by(induct xs) simp_all
nipkow@18423
   896
wenzelm@13142
   897
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
nipkow@13145
   898
by (induct xs) auto
wenzelm@13114
   899
wenzelm@13142
   900
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
nipkow@13145
   901
by (induct xs) auto
wenzelm@13114
   902
nipkow@16998
   903
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" 
nipkow@24349
   904
by (induct xs) simp_all
nipkow@16998
   905
nipkow@16998
   906
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)"
nipkow@16998
   907
apply (induct xs)
nipkow@16998
   908
 apply auto
nipkow@16998
   909
apply(cut_tac P=P and xs=xs in length_filter_le)
nipkow@16998
   910
apply simp
nipkow@16998
   911
done
wenzelm@13114
   912
nipkow@16965
   913
lemma filter_map:
nipkow@16965
   914
  "filter P (map f xs) = map f (filter (P o f) xs)"
nipkow@16965
   915
by (induct xs) simp_all
nipkow@16965
   916
nipkow@16965
   917
lemma length_filter_map[simp]:
nipkow@16965
   918
  "length (filter P (map f xs)) = length(filter (P o f) xs)"
nipkow@16965
   919
by (simp add:filter_map)
nipkow@16965
   920
wenzelm@13142
   921
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
nipkow@13145
   922
by auto
wenzelm@13114
   923
nipkow@15246
   924
lemma length_filter_less:
nipkow@15246
   925
  "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs"
nipkow@15246
   926
proof (induct xs)
nipkow@15246
   927
  case Nil thus ?case by simp
nipkow@15246
   928
next
nipkow@15246
   929
  case (Cons x xs) thus ?case
nipkow@15246
   930
    apply (auto split:split_if_asm)
nipkow@15246
   931
    using length_filter_le[of P xs] apply arith
nipkow@15246
   932
  done
nipkow@15246
   933
qed
wenzelm@13114
   934
nipkow@15281
   935
lemma length_filter_conv_card:
nipkow@15281
   936
 "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
nipkow@15281
   937
proof (induct xs)
nipkow@15281
   938
  case Nil thus ?case by simp
nipkow@15281
   939
next
nipkow@15281
   940
  case (Cons x xs)
nipkow@15281
   941
  let ?S = "{i. i < length xs & p(xs!i)}"
nipkow@15281
   942
  have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite)
nipkow@15281
   943
  show ?case (is "?l = card ?S'")
nipkow@15281
   944
  proof (cases)
nipkow@15281
   945
    assume "p x"
nipkow@15281
   946
    hence eq: "?S' = insert 0 (Suc ` ?S)"
nipkow@15281
   947
      by(auto simp add: nth_Cons image_def split:nat.split elim:lessE)
nipkow@15281
   948
    have "length (filter p (x # xs)) = Suc(card ?S)"
wenzelm@23388
   949
      using Cons `p x` by simp
nipkow@15281
   950
    also have "\<dots> = Suc(card(Suc ` ?S))" using fin
nipkow@15281
   951
      by (simp add: card_image inj_Suc)
nipkow@15281
   952
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
   953
      by (simp add:card_insert_if) (simp add:image_def)
nipkow@15281
   954
    finally show ?thesis .
nipkow@15281
   955
  next
nipkow@15281
   956
    assume "\<not> p x"
nipkow@15281
   957
    hence eq: "?S' = Suc ` ?S"
nipkow@15281
   958
      by(auto simp add: nth_Cons image_def split:nat.split elim:lessE)
nipkow@15281
   959
    have "length (filter p (x # xs)) = card ?S"
wenzelm@23388
   960
      using Cons `\<not> p x` by simp
nipkow@15281
   961
    also have "\<dots> = card(Suc ` ?S)" using fin
nipkow@15281
   962
      by (simp add: card_image inj_Suc)
nipkow@15281
   963
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
   964
      by (simp add:card_insert_if)
nipkow@15281
   965
    finally show ?thesis .
nipkow@15281
   966
  qed
nipkow@15281
   967
qed
nipkow@15281
   968
nipkow@17629
   969
lemma Cons_eq_filterD:
nipkow@17629
   970
 "x#xs = filter P ys \<Longrightarrow>
nipkow@17629
   971
  \<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
   972
  (is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs")
nipkow@17629
   973
proof(induct ys)
nipkow@17629
   974
  case Nil thus ?case by simp
nipkow@17629
   975
next
nipkow@17629
   976
  case (Cons y ys)
nipkow@17629
   977
  show ?case (is "\<exists>x. ?Q x")
nipkow@17629
   978
  proof cases
nipkow@17629
   979
    assume Py: "P y"
nipkow@17629
   980
    show ?thesis
nipkow@17629
   981
    proof cases
nipkow@17629
   982
      assume xy: "x = y"
nipkow@17629
   983
      show ?thesis
nipkow@17629
   984
      proof from Py xy Cons(2) show "?Q []" by simp qed
nipkow@17629
   985
    next
nipkow@17629
   986
      assume "x \<noteq> y" with Py Cons(2) show ?thesis by simp
nipkow@17629
   987
    qed
nipkow@17629
   988
  next
nipkow@17629
   989
    assume Py: "\<not> P y"
nipkow@17629
   990
    with Cons obtain us vs where 1 : "?P (y#ys) (y#us) vs" by fastsimp
nipkow@17629
   991
    show ?thesis (is "? us. ?Q us")
nipkow@17629
   992
    proof show "?Q (y#us)" using 1 by simp qed
nipkow@17629
   993
  qed
nipkow@17629
   994
qed
nipkow@17629
   995
nipkow@17629
   996
lemma filter_eq_ConsD:
nipkow@17629
   997
 "filter P ys = x#xs \<Longrightarrow>
nipkow@17629
   998
  \<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
   999
by(rule Cons_eq_filterD) simp
nipkow@17629
  1000
nipkow@17629
  1001
lemma filter_eq_Cons_iff:
nipkow@17629
  1002
 "(filter P ys = x#xs) =
nipkow@17629
  1003
  (\<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
  1004
by(auto dest:filter_eq_ConsD)
nipkow@17629
  1005
nipkow@17629
  1006
lemma Cons_eq_filter_iff:
nipkow@17629
  1007
 "(x#xs = filter P ys) =
nipkow@17629
  1008
  (\<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
  1009
by(auto dest:Cons_eq_filterD)
nipkow@17629
  1010
krauss@19770
  1011
lemma filter_cong[fundef_cong, recdef_cong]:
nipkow@17501
  1012
 "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys"
nipkow@17501
  1013
apply simp
nipkow@17501
  1014
apply(erule thin_rl)
nipkow@17501
  1015
by (induct ys) simp_all
nipkow@17501
  1016
nipkow@15281
  1017
nipkow@15392
  1018
subsubsection {* @{text concat} *}
wenzelm@13114
  1019
wenzelm@13142
  1020
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
nipkow@13145
  1021
by (induct xs) auto
wenzelm@13114
  1022
paulson@18447
  1023
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1024
by (induct xss) auto
wenzelm@13114
  1025
paulson@18447
  1026
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1027
by (induct xss) auto
wenzelm@13114
  1028
nipkow@24308
  1029
lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)"
nipkow@13145
  1030
by (induct xs) auto
wenzelm@13114
  1031
nipkow@24476
  1032
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs"
nipkow@24349
  1033
by (induct xs) auto
nipkow@24349
  1034
wenzelm@13142
  1035
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
nipkow@13145
  1036
by (induct xs) auto
wenzelm@13114
  1037
wenzelm@13142
  1038
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
nipkow@13145
  1039
by (induct xs) auto
wenzelm@13114
  1040
wenzelm@13142
  1041
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
nipkow@13145
  1042
by (induct xs) auto
wenzelm@13114
  1043
wenzelm@13114
  1044
nipkow@15392
  1045
subsubsection {* @{text nth} *}
wenzelm@13114
  1046
wenzelm@13142
  1047
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x"
nipkow@13145
  1048
by auto
wenzelm@13114
  1049
wenzelm@13142
  1050
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n"
nipkow@13145
  1051
by auto
wenzelm@13114
  1052
wenzelm@13142
  1053
declare nth.simps [simp del]
wenzelm@13114
  1054
wenzelm@13114
  1055
lemma nth_append:
nipkow@24526
  1056
  "(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
nipkow@24526
  1057
apply (induct xs arbitrary: n, simp)
paulson@14208
  1058
apply (case_tac n, auto)
nipkow@13145
  1059
done
wenzelm@13114
  1060
nipkow@14402
  1061
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x"
nipkow@14402
  1062
by (induct "xs") auto
nipkow@14402
  1063
nipkow@14402
  1064
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n"
nipkow@14402
  1065
by (induct "xs") auto
nipkow@14402
  1066
nipkow@24526
  1067
lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)"
nipkow@24526
  1068
apply (induct xs arbitrary: n, simp)
paulson@14208
  1069
apply (case_tac n, auto)
nipkow@13145
  1070
done
wenzelm@13114
  1071
nipkow@18423
  1072
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"
nipkow@18423
  1073
by(cases xs) simp_all
nipkow@18423
  1074
nipkow@18049
  1075
nipkow@18049
  1076
lemma list_eq_iff_nth_eq:
nipkow@24526
  1077
 "(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))"
nipkow@24526
  1078
apply(induct xs arbitrary: ys)
nipkow@18049
  1079
 apply simp apply blast
nipkow@18049
  1080
apply(case_tac ys)
nipkow@18049
  1081
 apply simp
nipkow@18049
  1082
apply(simp add:nth_Cons split:nat.split)apply blast
nipkow@18049
  1083
done
nipkow@18049
  1084
wenzelm@13142
  1085
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
paulson@15251
  1086
apply (induct xs, simp, simp)
nipkow@13145
  1087
apply safe
paulson@14208
  1088
apply (rule_tac x = 0 in exI, simp)
paulson@14208
  1089
 apply (rule_tac x = "Suc i" in exI, simp)
paulson@14208
  1090
apply (case_tac i, simp)
nipkow@13145
  1091
apply (rename_tac j)
paulson@14208
  1092
apply (rule_tac x = j in exI, simp)
nipkow@13145
  1093
done
wenzelm@13114
  1094
nipkow@17501
  1095
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)"
nipkow@17501
  1096
by(auto simp:set_conv_nth)
nipkow@17501
  1097
nipkow@13145
  1098
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"
nipkow@13145
  1099
by (auto simp add: set_conv_nth)
wenzelm@13114
  1100
wenzelm@13142
  1101
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
nipkow@13145
  1102
by (auto simp add: set_conv_nth)
wenzelm@13114
  1103
wenzelm@13114
  1104
lemma all_nth_imp_all_set:
nipkow@13145
  1105
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x"
nipkow@13145
  1106
by (auto simp add: set_conv_nth)
wenzelm@13114
  1107
wenzelm@13114
  1108
lemma all_set_conv_all_nth:
nipkow@13145
  1109
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
nipkow@13145
  1110
by (auto simp add: set_conv_nth)
wenzelm@13114
  1111
wenzelm@13114
  1112
nipkow@15392
  1113
subsubsection {* @{text list_update} *}
wenzelm@13114
  1114
nipkow@24526
  1115
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs"
nipkow@24526
  1116
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1117
wenzelm@13114
  1118
lemma nth_list_update:
nipkow@24526
  1119
"i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"
nipkow@24526
  1120
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1121
wenzelm@13142
  1122
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
nipkow@13145
  1123
by (simp add: nth_list_update)
wenzelm@13114
  1124
nipkow@24526
  1125
lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j"
nipkow@24526
  1126
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1127
wenzelm@13142
  1128
lemma list_update_overwrite [simp]:
nipkow@24526
  1129
"i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"
nipkow@24526
  1130
by (induct xs arbitrary: i) (auto split: nat.split)
nipkow@24526
  1131
nipkow@24526
  1132
lemma list_update_id[simp]: "xs[i := xs!i] = xs"
nipkow@24526
  1133
by (induct xs arbitrary: i) (simp_all split:nat.splits)
nipkow@24526
  1134
nipkow@24526
  1135
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs"
nipkow@24526
  1136
apply (induct xs arbitrary: i)
nipkow@17501
  1137
 apply simp
nipkow@17501
  1138
apply (case_tac i)
nipkow@17501
  1139
apply simp_all
nipkow@17501
  1140
done
nipkow@17501
  1141
wenzelm@13114
  1142
lemma list_update_same_conv:
nipkow@24526
  1143
"i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
nipkow@24526
  1144
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1145
nipkow@14187
  1146
lemma list_update_append1:
nipkow@24526
  1147
 "i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"
nipkow@24526
  1148
apply (induct xs arbitrary: i, simp)
nipkow@14187
  1149
apply(simp split:nat.split)
nipkow@14187
  1150
done
nipkow@14187
  1151
kleing@15868
  1152
lemma list_update_append:
nipkow@24526
  1153
  "(xs @ ys) [n:= x] = 
kleing@15868
  1154
  (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))"
nipkow@24526
  1155
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1156
nipkow@14402
  1157
lemma list_update_length [simp]:
nipkow@14402
  1158
 "(xs @ x # ys)[length xs := y] = (xs @ y # ys)"
nipkow@14402
  1159
by (induct xs, auto)
nipkow@14402
  1160
wenzelm@13114
  1161
lemma update_zip:
nipkow@24526
  1162
  "length xs = length ys ==>
nipkow@24526
  1163
  (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
nipkow@24526
  1164
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split)
nipkow@24526
  1165
nipkow@24526
  1166
lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)"
nipkow@24526
  1167
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1168
wenzelm@13114
  1169
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
nipkow@13145
  1170
by (blast dest!: set_update_subset_insert [THEN subsetD])
wenzelm@13114
  1171
nipkow@24526
  1172
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])"
nipkow@24526
  1173
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1174
wenzelm@13114
  1175
nipkow@15392
  1176
subsubsection {* @{text last} and @{text butlast} *}
wenzelm@13114
  1177
wenzelm@13142
  1178
lemma last_snoc [simp]: "last (xs @ [x]) = x"
nipkow@13145
  1179
by (induct xs) auto
wenzelm@13114
  1180
wenzelm@13142
  1181
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
nipkow@13145
  1182
by (induct xs) auto
wenzelm@13114
  1183
nipkow@14302
  1184
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x"
nipkow@14302
  1185
by(simp add:last.simps)
nipkow@14302
  1186
nipkow@14302
  1187
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs"
nipkow@14302
  1188
by(simp add:last.simps)
nipkow@14302
  1189
nipkow@14302
  1190
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)"
nipkow@14302
  1191
by (induct xs) (auto)
nipkow@14302
  1192
nipkow@14302
  1193
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs"
nipkow@14302
  1194
by(simp add:last_append)
nipkow@14302
  1195
nipkow@14302
  1196
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"
nipkow@14302
  1197
by(simp add:last_append)
nipkow@14302
  1198
nipkow@17762
  1199
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"
nipkow@17762
  1200
by(rule rev_exhaust[of xs]) simp_all
nipkow@17762
  1201
nipkow@17762
  1202
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"
nipkow@17762
  1203
by(cases xs) simp_all
nipkow@17762
  1204
nipkow@17765
  1205
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as"
nipkow@17765
  1206
by (induct as) auto
nipkow@17762
  1207
wenzelm@13142
  1208
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
nipkow@13145
  1209
by (induct xs rule: rev_induct) auto
wenzelm@13114
  1210
wenzelm@13114
  1211
lemma butlast_append:
nipkow@24526
  1212
  "butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
nipkow@24526
  1213
by (induct xs arbitrary: ys) auto
wenzelm@13114
  1214
wenzelm@13142
  1215
lemma append_butlast_last_id [simp]:
nipkow@13145
  1216
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
nipkow@13145
  1217
by (induct xs) auto
wenzelm@13114
  1218
wenzelm@13142
  1219
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
nipkow@13145
  1220
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1221
wenzelm@13114
  1222
lemma in_set_butlast_appendI:
nipkow@13145
  1223
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
nipkow@13145
  1224
by (auto dest: in_set_butlastD simp add: butlast_append)
wenzelm@13114
  1225
nipkow@24526
  1226
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs"
nipkow@24526
  1227
apply (induct xs arbitrary: n)
nipkow@17501
  1228
 apply simp
nipkow@17501
  1229
apply (auto split:nat.split)
nipkow@17501
  1230
done
nipkow@17501
  1231
nipkow@17589
  1232
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"
nipkow@17589
  1233
by(induct xs)(auto simp:neq_Nil_conv)
nipkow@17589
  1234
nipkow@15392
  1235
subsubsection {* @{text take} and @{text drop} *}
wenzelm@13114
  1236
wenzelm@13142
  1237
lemma take_0 [simp]: "take 0 xs = []"
nipkow@13145
  1238
by (induct xs) auto
wenzelm@13114
  1239
wenzelm@13142
  1240
lemma drop_0 [simp]: "drop 0 xs = xs"
nipkow@13145
  1241
by (induct xs) auto
wenzelm@13114
  1242
wenzelm@13142
  1243
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
nipkow@13145
  1244
by simp
wenzelm@13114
  1245
wenzelm@13142
  1246
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
nipkow@13145
  1247
by simp
wenzelm@13114
  1248
wenzelm@13142
  1249
declare take_Cons [simp del] and drop_Cons [simp del]
wenzelm@13114
  1250
nipkow@15110
  1251
lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)"
nipkow@15110
  1252
by(clarsimp simp add:neq_Nil_conv)
nipkow@15110
  1253
nipkow@14187
  1254
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"
nipkow@14187
  1255
by(cases xs, simp_all)
nipkow@14187
  1256
nipkow@24526
  1257
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)"
nipkow@24526
  1258
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split)
nipkow@24526
  1259
nipkow@24526
  1260
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y"
nipkow@24526
  1261
apply (induct xs arbitrary: n, simp)
nipkow@14187
  1262
apply(simp add:drop_Cons nth_Cons split:nat.splits)
nipkow@14187
  1263
done
nipkow@14187
  1264
nipkow@13913
  1265
lemma take_Suc_conv_app_nth:
nipkow@24526
  1266
  "i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"
nipkow@24526
  1267
apply (induct xs arbitrary: i, simp)
paulson@14208
  1268
apply (case_tac i, auto)
nipkow@13913
  1269
done
nipkow@13913
  1270
mehta@14591
  1271
lemma drop_Suc_conv_tl:
nipkow@24526
  1272
  "i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs"
nipkow@24526
  1273
apply (induct xs arbitrary: i, simp)
mehta@14591
  1274
apply (case_tac i, auto)
mehta@14591
  1275
done
mehta@14591
  1276
nipkow@24526
  1277
lemma length_take [simp]: "length (take n xs) = min (length xs) n"
nipkow@24526
  1278
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1279
nipkow@24526
  1280
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)"
nipkow@24526
  1281
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1282
nipkow@24526
  1283
lemma take_all [simp]: "length xs <= n ==> take n xs = xs"
nipkow@24526
  1284
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1285
nipkow@24526
  1286
lemma drop_all [simp]: "length xs <= n ==> drop n xs = []"
nipkow@24526
  1287
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1288
wenzelm@13142
  1289
lemma take_append [simp]:
nipkow@24526
  1290
  "take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
nipkow@24526
  1291
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1292
wenzelm@13142
  1293
lemma drop_append [simp]:
nipkow@24526
  1294
  "drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
nipkow@24526
  1295
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1296
nipkow@24526
  1297
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs"
nipkow@24526
  1298
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1299
apply (case_tac xs, auto)
nipkow@15236
  1300
apply (case_tac n, auto)
nipkow@13145
  1301
done
wenzelm@13114
  1302
nipkow@24526
  1303
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs"
nipkow@24526
  1304
apply (induct m arbitrary: xs, auto)
paulson@14208
  1305
apply (case_tac xs, auto)
nipkow@13145
  1306
done
wenzelm@13114
  1307
nipkow@24526
  1308
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)"
nipkow@24526
  1309
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1310
apply (case_tac xs, auto)
nipkow@13145
  1311
done
wenzelm@13114
  1312
nipkow@24526
  1313
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)"
nipkow@24526
  1314
apply(induct xs arbitrary: m n)
nipkow@14802
  1315
 apply simp
nipkow@14802
  1316
apply(simp add: take_Cons drop_Cons split:nat.split)
nipkow@14802
  1317
done
nipkow@14802
  1318
nipkow@24526
  1319
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs"
nipkow@24526
  1320
apply (induct n arbitrary: xs, auto)
paulson@14208
  1321
apply (case_tac xs, auto)
nipkow@13145
  1322
done
wenzelm@13114
  1323
nipkow@24526
  1324
lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])"
nipkow@24526
  1325
apply(induct xs arbitrary: n)
nipkow@15110
  1326
 apply simp
nipkow@15110
  1327
apply(simp add:take_Cons split:nat.split)
nipkow@15110
  1328
done
nipkow@15110
  1329
nipkow@24526
  1330
lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)"
nipkow@24526
  1331
apply(induct xs arbitrary: n)
nipkow@15110
  1332
apply simp
nipkow@15110
  1333
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1334
done
nipkow@15110
  1335
nipkow@24526
  1336
lemma take_map: "take n (map f xs) = map f (take n xs)"
nipkow@24526
  1337
apply (induct n arbitrary: xs, auto)
paulson@14208
  1338
apply (case_tac xs, auto)
nipkow@13145
  1339
done
wenzelm@13114
  1340
nipkow@24526
  1341
lemma drop_map: "drop n (map f xs) = map f (drop n xs)"
nipkow@24526
  1342
apply (induct n arbitrary: xs, auto)
paulson@14208
  1343
apply (case_tac xs, auto)
nipkow@13145
  1344
done
wenzelm@13114
  1345
nipkow@24526
  1346
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)"
nipkow@24526
  1347
apply (induct xs arbitrary: i, auto)
paulson@14208
  1348
apply (case_tac i, auto)
nipkow@13145
  1349
done
wenzelm@13114
  1350
nipkow@24526
  1351
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)"
nipkow@24526
  1352
apply (induct xs arbitrary: i, auto)
paulson@14208
  1353
apply (case_tac i, auto)
nipkow@13145
  1354
done
wenzelm@13114
  1355
nipkow@24526
  1356
lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i"
nipkow@24526
  1357
apply (induct xs arbitrary: i n, auto)
paulson@14208
  1358
apply (case_tac n, blast)
paulson@14208
  1359
apply (case_tac i, auto)
nipkow@13145
  1360
done
wenzelm@13114
  1361
wenzelm@13142
  1362
lemma nth_drop [simp]:
nipkow@24526
  1363
  "n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
nipkow@24526
  1364
apply (induct n arbitrary: xs i, auto)
paulson@14208
  1365
apply (case_tac xs, auto)
nipkow@13145
  1366
done
nipkow@3507
  1367
nipkow@18423
  1368
lemma hd_drop_conv_nth: "\<lbrakk> xs \<noteq> []; n < length xs \<rbrakk> \<Longrightarrow> hd(drop n xs) = xs!n"
nipkow@18423
  1369
by(simp add: hd_conv_nth)
nipkow@18423
  1370
nipkow@24526
  1371
lemma set_take_subset: "set(take n xs) \<subseteq> set xs"
nipkow@24526
  1372
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split)
nipkow@24526
  1373
nipkow@24526
  1374
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs"
nipkow@24526
  1375
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split)
nipkow@14025
  1376
nipkow@14187
  1377
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  1378
using set_take_subset by fast
nipkow@14187
  1379
nipkow@14187
  1380
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  1381
using set_drop_subset by fast
nipkow@14187
  1382
wenzelm@13114
  1383
lemma append_eq_conv_conj:
nipkow@24526
  1384
  "(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
nipkow@24526
  1385
apply (induct xs arbitrary: zs, simp, clarsimp)
paulson@14208
  1386
apply (case_tac zs, auto)
nipkow@13145
  1387
done
wenzelm@13142
  1388
nipkow@24526
  1389
lemma take_add: 
nipkow@24526
  1390
  "i+j \<le> length(xs) \<Longrightarrow> take (i+j) xs = take i xs @ take j (drop i xs)"
nipkow@24526
  1391
apply (induct xs arbitrary: i, auto) 
nipkow@24526
  1392
apply (case_tac i, simp_all)
paulson@14050
  1393
done
paulson@14050
  1394
nipkow@14300
  1395
lemma append_eq_append_conv_if:
nipkow@24526
  1396
 "(xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) =
nipkow@14300
  1397
  (if size xs\<^isub>1 \<le> size ys\<^isub>1
nipkow@14300
  1398
   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
  1399
   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
  1400
apply(induct xs\<^isub>1 arbitrary: ys\<^isub>1)
nipkow@14300
  1401
 apply simp
nipkow@14300
  1402
apply(case_tac ys\<^isub>1)
nipkow@14300
  1403
apply simp_all
nipkow@14300
  1404
done
nipkow@14300
  1405
nipkow@15110
  1406
lemma take_hd_drop:
nipkow@24526
  1407
  "n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (n+1) xs"
nipkow@24526
  1408
apply(induct xs arbitrary: n)
nipkow@15110
  1409
apply simp
nipkow@15110
  1410
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1411
done
nipkow@15110
  1412
nipkow@17501
  1413
lemma id_take_nth_drop:
nipkow@17501
  1414
 "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" 
nipkow@17501
  1415
proof -
nipkow@17501
  1416
  assume si: "i < length xs"
nipkow@17501
  1417
  hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto
nipkow@17501
  1418
  moreover
nipkow@17501
  1419
  from si have "take (Suc i) xs = take i xs @ [xs!i]"
nipkow@17501
  1420
    apply (rule_tac take_Suc_conv_app_nth) by arith
nipkow@17501
  1421
  ultimately show ?thesis by auto
nipkow@17501
  1422
qed
nipkow@17501
  1423
  
nipkow@17501
  1424
lemma upd_conv_take_nth_drop:
nipkow@17501
  1425
 "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  1426
proof -
nipkow@17501
  1427
  assume i: "i < length xs"
nipkow@17501
  1428
  have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]"
nipkow@17501
  1429
    by(rule arg_cong[OF id_take_nth_drop[OF i]])
nipkow@17501
  1430
  also have "\<dots> = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  1431
    using i by (simp add: list_update_append)
nipkow@17501
  1432
  finally show ?thesis .
nipkow@17501
  1433
qed
nipkow@17501
  1434
wenzelm@13114
  1435
nipkow@15392
  1436
subsubsection {* @{text takeWhile} and @{text dropWhile} *}
wenzelm@13114
  1437
wenzelm@13142
  1438
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
nipkow@13145
  1439
by (induct xs) auto
wenzelm@13114
  1440
wenzelm@13142
  1441
lemma takeWhile_append1 [simp]:
nipkow@13145
  1442
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"
nipkow@13145
  1443
by (induct xs) auto
wenzelm@13114
  1444
wenzelm@13142
  1445
lemma takeWhile_append2 [simp]:
nipkow@13145
  1446
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
nipkow@13145
  1447
by (induct xs) auto
wenzelm@13114
  1448
wenzelm@13142
  1449
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
nipkow@13145
  1450
by (induct xs) auto
wenzelm@13114
  1451
wenzelm@13142
  1452
lemma dropWhile_append1 [simp]:
nipkow@13145
  1453
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
nipkow@13145
  1454
by (induct xs) auto
wenzelm@13114
  1455
wenzelm@13142
  1456
lemma dropWhile_append2 [simp]:
nipkow@13145
  1457
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
nipkow@13145
  1458
by (induct xs) auto
wenzelm@13114
  1459
krauss@23971
  1460
lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
nipkow@13145
  1461
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1462
nipkow@13913
  1463
lemma takeWhile_eq_all_conv[simp]:
nipkow@13913
  1464
 "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  1465
by(induct xs, auto)
nipkow@13913
  1466
nipkow@13913
  1467
lemma dropWhile_eq_Nil_conv[simp]:
nipkow@13913
  1468
 "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  1469
by(induct xs, auto)
nipkow@13913
  1470
nipkow@13913
  1471
lemma dropWhile_eq_Cons_conv:
nipkow@13913
  1472
 "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"
nipkow@13913
  1473
by(induct xs, auto)
nipkow@13913
  1474
nipkow@17501
  1475
text{* The following two lemmmas could be generalized to an arbitrary
nipkow@17501
  1476
property. *}
nipkow@17501
  1477
nipkow@17501
  1478
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  1479
 takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))"
nipkow@17501
  1480
by(induct xs) (auto simp: takeWhile_tail[where l="[]"])
nipkow@17501
  1481
nipkow@17501
  1482
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  1483
  dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)"
nipkow@17501
  1484
apply(induct xs)
nipkow@17501
  1485
 apply simp
nipkow@17501
  1486
apply auto
nipkow@17501
  1487
apply(subst dropWhile_append2)
nipkow@17501
  1488
apply auto
nipkow@17501
  1489
done
nipkow@17501
  1490
nipkow@18423
  1491
lemma takeWhile_not_last:
nipkow@18423
  1492
 "\<lbrakk> xs \<noteq> []; distinct xs\<rbrakk> \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs"
nipkow@18423
  1493
apply(induct xs)
nipkow@18423
  1494
 apply simp
nipkow@18423
  1495
apply(case_tac xs)
nipkow@18423
  1496
apply(auto)
nipkow@18423
  1497
done
nipkow@18423
  1498
krauss@19770
  1499
lemma takeWhile_cong [fundef_cong, recdef_cong]:
krauss@18336
  1500
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  1501
  ==> takeWhile P l = takeWhile Q k"
nipkow@24349
  1502
by (induct k arbitrary: l) (simp_all)
krauss@18336
  1503
krauss@19770
  1504
lemma dropWhile_cong [fundef_cong, recdef_cong]:
krauss@18336
  1505
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  1506
  ==> dropWhile P l = dropWhile Q k"
nipkow@24349
  1507
by (induct k arbitrary: l, simp_all)
krauss@18336
  1508
wenzelm@13114
  1509
nipkow@15392
  1510
subsubsection {* @{text zip} *}
wenzelm@13114
  1511
wenzelm@13142
  1512
lemma zip_Nil [simp]: "zip [] ys = []"
nipkow@13145
  1513
by (induct ys) auto
wenzelm@13114
  1514
wenzelm@13142
  1515
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
nipkow@13145
  1516
by simp
wenzelm@13114
  1517
wenzelm@13142
  1518
declare zip_Cons [simp del]
wenzelm@13114
  1519
nipkow@15281
  1520
lemma zip_Cons1:
nipkow@15281
  1521
 "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)"
nipkow@15281
  1522
by(auto split:list.split)
nipkow@15281
  1523
wenzelm@13142
  1524
lemma length_zip [simp]:
krauss@22493
  1525
"length (zip xs ys) = min (length xs) (length ys)"
krauss@22493
  1526
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  1527
wenzelm@13114
  1528
lemma zip_append1:
krauss@22493
  1529
"zip (xs @ ys) zs =
nipkow@13145
  1530
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
krauss@22493
  1531
by (induct xs zs rule:list_induct2') auto
wenzelm@13114
  1532
wenzelm@13114
  1533
lemma zip_append2:
krauss@22493
  1534
"zip xs (ys @ zs) =
nipkow@13145
  1535
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
krauss@22493
  1536
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  1537
wenzelm@13142
  1538
lemma zip_append [simp]:
wenzelm@13142
  1539
 "[| length xs = length us; length ys = length vs |] ==>
nipkow@13145
  1540
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
nipkow@13145
  1541
by (simp add: zip_append1)
wenzelm@13114
  1542
wenzelm@13114
  1543
lemma zip_rev:
nipkow@14247
  1544
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
nipkow@14247
  1545
by (induct rule:list_induct2, simp_all)
wenzelm@13114
  1546
nipkow@23096
  1547
lemma map_zip_map:
nipkow@23096
  1548
 "map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)"
nipkow@23096
  1549
apply(induct xs arbitrary:ys) apply simp
nipkow@23096
  1550
apply(case_tac ys)
nipkow@23096
  1551
apply simp_all
nipkow@23096
  1552
done
nipkow@23096
  1553
nipkow@23096
  1554
lemma map_zip_map2:
nipkow@23096
  1555
 "map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)"
nipkow@23096
  1556
apply(induct xs arbitrary:ys) apply simp
nipkow@23096
  1557
apply(case_tac ys)
nipkow@23096
  1558
apply simp_all
nipkow@23096
  1559
done
nipkow@23096
  1560
wenzelm@13142
  1561
lemma nth_zip [simp]:
nipkow@24526
  1562
"[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"
nipkow@24526
  1563
apply (induct ys arbitrary: i xs, simp)
nipkow@13145
  1564
apply (case_tac xs)
nipkow@13145
  1565
 apply (simp_all add: nth.simps split: nat.split)
nipkow@13145
  1566
done
wenzelm@13114
  1567
wenzelm@13114
  1568
lemma set_zip:
nipkow@13145
  1569
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
nipkow@13145
  1570
by (simp add: set_conv_nth cong: rev_conj_cong)
wenzelm@13114
  1571
wenzelm@13114
  1572
lemma zip_update:
nipkow@13145
  1573
"length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
nipkow@13145
  1574
by (rule sym, simp add: update_zip)
wenzelm@13114
  1575
wenzelm@13142
  1576
lemma zip_replicate [simp]:
nipkow@24526
  1577
  "zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
nipkow@24526
  1578
apply (induct i arbitrary: j, auto)
paulson@14208
  1579
apply (case_tac j, auto)
nipkow@13145
  1580
done
wenzelm@13114
  1581
nipkow@19487
  1582
lemma take_zip:
nipkow@24526
  1583
  "take n (zip xs ys) = zip (take n xs) (take n ys)"
nipkow@24526
  1584
apply (induct n arbitrary: xs ys)
nipkow@19487
  1585
 apply simp
nipkow@19487
  1586
apply (case_tac xs, simp)
nipkow@19487
  1587
apply (case_tac ys, simp_all)
nipkow@19487
  1588
done
nipkow@19487
  1589
nipkow@19487
  1590
lemma drop_zip:
nipkow@24526
  1591
  "drop n (zip xs ys) = zip (drop n xs) (drop n ys)"
nipkow@24526
  1592
apply (induct n arbitrary: xs ys)
nipkow@19487
  1593
 apply simp
nipkow@19487
  1594
apply (case_tac xs, simp)
nipkow@19487
  1595
apply (case_tac ys, simp_all)
nipkow@19487
  1596
done
nipkow@19487
  1597
krauss@22493
  1598
lemma set_zip_leftD:
krauss@22493
  1599
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs"
krauss@22493
  1600
by (induct xs ys rule:list_induct2') auto
krauss@22493
  1601
krauss@22493
  1602
lemma set_zip_rightD:
krauss@22493
  1603
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys"
krauss@22493
  1604
by (induct xs ys rule:list_induct2') auto
wenzelm@13142
  1605
nipkow@23983
  1606
lemma in_set_zipE:
nipkow@23983
  1607
  "(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23983
  1608
by(blast dest: set_zip_leftD set_zip_rightD)
nipkow@23983
  1609
nipkow@15392
  1610
subsubsection {* @{text list_all2} *}
wenzelm@13114
  1611
kleing@14316
  1612
lemma list_all2_lengthD [intro?]: 
kleing@14316
  1613
  "list_all2 P xs ys ==> length xs = length ys"
nipkow@24349
  1614
by (simp add: list_all2_def)
haftmann@19607
  1615
haftmann@19787
  1616
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])"
nipkow@24349
  1617
by (simp add: list_all2_def)
haftmann@19607
  1618
haftmann@19787
  1619
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])"
nipkow@24349
  1620
by (simp add: list_all2_def)
haftmann@19607
  1621
haftmann@19607
  1622
lemma list_all2_Cons [iff, code]:
haftmann@19607
  1623
  "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
nipkow@24349
  1624
by (auto simp add: list_all2_def)
wenzelm@13114
  1625
wenzelm@13114
  1626
lemma list_all2_Cons1:
nipkow@13145
  1627
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
nipkow@13145
  1628
by (cases ys) auto
wenzelm@13114
  1629
wenzelm@13114
  1630
lemma list_all2_Cons2:
nipkow@13145
  1631
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
nipkow@13145
  1632
by (cases xs) auto
wenzelm@13114
  1633
wenzelm@13142
  1634
lemma list_all2_rev [iff]:
nipkow@13145
  1635
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
nipkow@13145
  1636
by (simp add: list_all2_def zip_rev cong: conj_cong)
wenzelm@13114
  1637
kleing@13863
  1638
lemma list_all2_rev1:
kleing@13863
  1639
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"
kleing@13863
  1640
by (subst list_all2_rev [symmetric]) simp
kleing@13863
  1641
wenzelm@13114
  1642
lemma list_all2_append1:
nipkow@13145
  1643
"list_all2 P (xs @ ys) zs =
nipkow@13145
  1644
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
nipkow@13145
  1645
list_all2 P xs us \<and> list_all2 P ys vs)"
nipkow@13145
  1646
apply (simp add: list_all2_def zip_append1)
nipkow@13145
  1647
apply (rule iffI)
nipkow@13145
  1648
 apply (rule_tac x = "take (length xs) zs" in exI)
nipkow@13145
  1649
 apply (rule_tac x = "drop (length xs) zs" in exI)
paulson@14208
  1650
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  1651
apply (simp add: ball_Un)
nipkow@13145
  1652
done
wenzelm@13114
  1653
wenzelm@13114
  1654
lemma list_all2_append2:
nipkow@13145
  1655
"list_all2 P xs (ys @ zs) =
nipkow@13145
  1656
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
nipkow@13145
  1657
list_all2 P us ys \<and> list_all2 P vs zs)"
nipkow@13145
  1658
apply (simp add: list_all2_def zip_append2)
nipkow@13145
  1659
apply (rule iffI)
nipkow@13145
  1660
 apply (rule_tac x = "take (length ys) xs" in exI)
nipkow@13145
  1661
 apply (rule_tac x = "drop (length ys) xs" in exI)
paulson@14208
  1662
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  1663
apply (simp add: ball_Un)
nipkow@13145
  1664
done
wenzelm@13114
  1665
kleing@13863
  1666
lemma list_all2_append:
nipkow@14247
  1667
  "length xs = length ys \<Longrightarrow>
nipkow@14247
  1668
  list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"
nipkow@14247
  1669
by (induct rule:list_induct2, simp_all)
kleing@13863
  1670
kleing@13863
  1671
lemma list_all2_appendI [intro?, trans]:
kleing@13863
  1672
  "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"
nipkow@24349
  1673
by (simp add: list_all2_append list_all2_lengthD)
kleing@13863
  1674
wenzelm@13114
  1675
lemma list_all2_conv_all_nth:
nipkow@13145
  1676
"list_all2 P xs ys =
nipkow@13145
  1677
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
nipkow@13145
  1678
by (force simp add: list_all2_def set_zip)
wenzelm@13114
  1679
berghofe@13883
  1680
lemma list_all2_trans:
berghofe@13883
  1681
  assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"
berghofe@13883
  1682
  shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"
berghofe@13883
  1683
        (is "!!bs cs. PROP ?Q as bs cs")
berghofe@13883
  1684
proof (induct as)
berghofe@13883
  1685
  fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"
berghofe@13883
  1686
  show "!!cs. PROP ?Q (x # xs) bs cs"
berghofe@13883
  1687
  proof (induct bs)
berghofe@13883
  1688
    fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs"
berghofe@13883
  1689
    show "PROP ?Q (x # xs) (y # ys) cs"
berghofe@13883
  1690
      by (induct cs) (auto intro: tr I1 I2)
berghofe@13883
  1691
  qed simp
berghofe@13883
  1692
qed simp
berghofe@13883
  1693
kleing@13863
  1694
lemma list_all2_all_nthI [intro?]:
kleing@13863
  1695
  "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"
nipkow@24349
  1696
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1697
paulson@14395
  1698
lemma list_all2I:
paulson@14395
  1699
  "\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b"
nipkow@24349
  1700
by (simp add: list_all2_def)
paulson@14395
  1701
kleing@14328
  1702
lemma list_all2_nthD:
kleing@13863
  1703
  "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  1704
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1705
nipkow@14302
  1706
lemma list_all2_nthD2:
nipkow@14302
  1707
  "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  1708
by (frule list_all2_lengthD) (auto intro: list_all2_nthD)
nipkow@14302
  1709
kleing@13863
  1710
lemma list_all2_map1: 
kleing@13863
  1711
  "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"
nipkow@24349
  1712
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1713
kleing@13863
  1714
lemma list_all2_map2: 
kleing@13863
  1715
  "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"
nipkow@24349
  1716
by (auto simp add: list_all2_conv_all_nth)
kleing@13863
  1717
kleing@14316
  1718
lemma list_all2_refl [intro?]:
kleing@13863
  1719
  "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"
nipkow@24349
  1720
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1721
kleing@13863
  1722
lemma list_all2_update_cong:
kleing@13863
  1723
  "\<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
  1724
by (simp add: list_all2_conv_all_nth nth_list_update)
kleing@13863
  1725
kleing@13863
  1726
lemma list_all2_update_cong2:
kleing@13863
  1727
  "\<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
  1728
by (simp add: list_all2_lengthD list_all2_update_cong)
kleing@13863
  1729
nipkow@14302
  1730
lemma list_all2_takeI [simp,intro?]:
nipkow@24526
  1731
  "list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)"
nipkow@24526
  1732
apply (induct xs arbitrary: n ys)
nipkow@24526
  1733
 apply simp
nipkow@24526
  1734
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  1735
apply (case_tac n)
nipkow@24526
  1736
apply auto
nipkow@24526
  1737
done
nipkow@14302
  1738
nipkow@14302
  1739
lemma list_all2_dropI [simp,intro?]:
nipkow@24526
  1740
  "list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)"
nipkow@24526
  1741
apply (induct as arbitrary: n bs, simp)
nipkow@24526
  1742
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  1743
apply (case_tac n, simp, simp)
nipkow@24526
  1744
done
kleing@13863
  1745
kleing@14327
  1746
lemma list_all2_mono [intro?]:
nipkow@24526
  1747
  "list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys"
nipkow@24526
  1748
apply (induct xs arbitrary: ys, simp)
nipkow@24526
  1749
apply (case_tac ys, auto)
nipkow@24526
  1750
done
kleing@13863
  1751
haftmann@22551
  1752
lemma list_all2_eq:
haftmann@22551
  1753
  "xs = ys \<longleftrightarrow> list_all2 (op =) xs ys"
nipkow@24349
  1754
by (induct xs ys rule: list_induct2') auto
haftmann@22551
  1755
wenzelm@13142
  1756
nipkow@15392
  1757
subsubsection {* @{text foldl} and @{text foldr} *}
wenzelm@13142
  1758
wenzelm@13142
  1759
lemma foldl_append [simp]:
nipkow@24526
  1760
  "foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
nipkow@24526
  1761
by (induct xs arbitrary: a) auto
wenzelm@13142
  1762
nipkow@14402
  1763
lemma foldr_append[simp]: "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)"
nipkow@14402
  1764
by (induct xs) auto
nipkow@14402
  1765
nipkow@23096
  1766
lemma foldr_map: "foldr g (map f xs) a = foldr (g o f) xs a"
nipkow@23096
  1767
by(induct xs) simp_all
nipkow@23096
  1768
nipkow@24449
  1769
text{* For efficient code generation: avoid intermediate list. *}
nipkow@24449
  1770
lemma foldl_map[code unfold]:
nipkow@24449
  1771
  "foldl g a (map f xs) = foldl (%a x. g a (f x)) a xs"
nipkow@23096
  1772
by(induct xs arbitrary:a) simp_all
nipkow@23096
  1773
krauss@19770
  1774
lemma foldl_cong [fundef_cong, recdef_cong]:
krauss@18336
  1775
  "[| a = b; l = k; !!a x. x : set l ==> f a x = g a x |] 
krauss@18336
  1776
  ==> foldl f a l = foldl g b k"
nipkow@24349
  1777
by (induct k arbitrary: a b l) simp_all
krauss@18336
  1778
krauss@19770
  1779
lemma foldr_cong [fundef_cong, recdef_cong]:
krauss@18336
  1780
  "[| a = b; l = k; !!a x. x : set l ==> f x a = g x a |] 
krauss@18336
  1781
  ==> foldr f l a = foldr g k b"
nipkow@24349
  1782
by (induct k arbitrary: a b l) simp_all
krauss@18336
  1783
nipkow@24449
  1784
lemma (in semigroup_add) foldl_assoc:
nipkow@24449
  1785
shows "foldl op\<^loc>+ (x\<^loc>+y) zs = x \<^loc>+ (foldl op\<^loc>+ y zs)"
nipkow@24449
  1786
by (induct zs arbitrary: y) (simp_all add:add_assoc)
nipkow@24449
  1787
nipkow@24449
  1788
lemma (in monoid_add) foldl_absorb0:
nipkow@24449
  1789
shows "x \<^loc>+ (foldl op\<^loc>+ \<^loc>0 zs) = foldl op\<^loc>+ x zs"
nipkow@24449
  1790
by (induct zs) (simp_all add:foldl_assoc)
nipkow@24449
  1791
nipkow@24449
  1792
nipkow@23096
  1793
text{* The ``First Duality Theorem'' in Bird \& Wadler: *}
nipkow@23096
  1794
nipkow@23096
  1795
lemma foldl_foldr1_lemma:
nipkow@23096
  1796
 "foldl op + a xs = a + foldr op + xs (0\<Colon>'a::monoid_add)"
nipkow@23096
  1797
by (induct xs arbitrary: a) (auto simp:add_assoc)
nipkow@23096
  1798
nipkow@23096
  1799
corollary foldl_foldr1:
nipkow@23096
  1800
 "foldl op + 0 xs = foldr op + xs (0\<Colon>'a::monoid_add)"
nipkow@23096
  1801
by (simp add:foldl_foldr1_lemma)
nipkow@23096
  1802
nipkow@23096
  1803
nipkow@23096
  1804
text{* The ``Third Duality Theorem'' in Bird \& Wadler: *}
nipkow@23096
  1805
nipkow@14402
  1806
lemma foldr_foldl: "foldr f xs a = foldl (%x y. f y x) a (rev xs)"
nipkow@14402
  1807
by (induct xs) auto
nipkow@14402
  1808
nipkow@14402
  1809
lemma foldl_foldr: "foldl f a xs = foldr (%x y. f y x) (rev xs) a"
nipkow@14402
  1810
by (simp add: foldr_foldl [of "%x y. f y x" "rev xs"])
nipkow@14402
  1811
chaieb@24471
  1812
lemma (in ab_semigroup_add) foldr_conv_foldl: "foldr op \<^loc>+ xs a = foldl op \<^loc>+ a xs"
chaieb@24471
  1813
  by (induct xs, auto simp add: foldl_assoc add_commute)
chaieb@24471
  1814
wenzelm@13142
  1815
text {*
nipkow@13145
  1816
Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
nipkow@13145
  1817
difficult to use because it requires an additional transitivity step.
wenzelm@13142
  1818
*}
wenzelm@13142
  1819
nipkow@24526
  1820
lemma start_le_sum: "(m::nat) <= n ==> m <= foldl (op +) n ns"
nipkow@24526
  1821
by (induct ns arbitrary: n) auto
nipkow@24526
  1822
nipkow@24526
  1823
lemma elem_le_sum: "(n::nat) : set ns ==> n <= foldl (op +) 0 ns"
nipkow@13145
  1824
by (force intro: start_le_sum simp add: in_set_conv_decomp)
wenzelm@13142
  1825
wenzelm@13142
  1826
lemma sum_eq_0_conv [iff]:
nipkow@24526
  1827
  "(foldl (op +) (m::nat) ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
nipkow@24526
  1828
by (induct ns arbitrary: m) auto
wenzelm@13114
  1829
chaieb@24471
  1830
lemma foldr_invariant: 
chaieb@24471
  1831
  "\<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
  1832
  by (induct xs, simp_all)
chaieb@24471
  1833
chaieb@24471
  1834
lemma foldl_invariant: 
chaieb@24471
  1835
  "\<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
  1836
  by (induct xs arbitrary: x, simp_all)
chaieb@24471
  1837
nipkow@24449
  1838
text{* @{const foldl} and @{text concat} *}
nipkow@24449
  1839
nipkow@24449
  1840
lemma concat_conv_foldl: "concat xss = foldl op@ [] xss"
nipkow@24449
  1841
by (induct xss) (simp_all add:monoid_append.foldl_absorb0)
nipkow@24449
  1842
nipkow@24449
  1843
lemma foldl_conv_concat:
nipkow@24449
  1844
  "foldl (op @) xs xxs = xs @ (concat xxs)"
nipkow@24449
  1845
by(simp add:concat_conv_foldl monoid_append.foldl_absorb0)
nipkow@24449
  1846
nipkow@23096
  1847
subsubsection {* List summation: @{const listsum} and @{text"\<Sum>"}*}
nipkow@23096
  1848
nipkow@24449
  1849
lemma listsum_append[simp]: "listsum (xs @ ys) = listsum xs + listsum ys"
nipkow@24449
  1850
by (induct xs) (simp_all add:add_assoc)
nipkow@24449
  1851
nipkow@24449
  1852
lemma listsum_rev[simp]:
nipkow@24449
  1853
fixes xs :: "'a::comm_monoid_add list"
nipkow@24449
  1854
shows "listsum (rev xs) = listsum xs"
nipkow@24449
  1855
by (induct xs) (simp_all add:add_ac)
nipkow@24449
  1856
nipkow@23096
  1857
lemma listsum_foldr:
nipkow@23096
  1858
 "listsum xs = foldr (op +) xs 0"
nipkow@23096
  1859
by(induct xs) auto
nipkow@23096
  1860
nipkow@24449
  1861
text{* For efficient code generation ---
nipkow@24449
  1862
       @{const listsum} is not tail recursive but @{const foldl} is. *}
nipkow@24449
  1863
lemma listsum[code unfold]: "listsum xs = foldl (op +) 0 xs"
nipkow@23096
  1864
by(simp add:listsum_foldr foldl_foldr1)
nipkow@23096
  1865
nipkow@24449
  1866
nipkow@23096
  1867
text{* Some syntactic sugar for summing a function over a list: *}
nipkow@23096
  1868
nipkow@23096
  1869
syntax
nipkow@23096
  1870
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3SUM _<-_. _)" [0, 51, 10] 10)
nipkow@23096
  1871
syntax (xsymbols)
nipkow@23096
  1872
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
nipkow@23096
  1873
syntax (HTML output)
nipkow@23096
  1874
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
nipkow@23096
  1875
nipkow@23096
  1876
translations -- {* Beware of argument permutation! *}
nipkow@23096
  1877
  "SUM x<-xs. b" == "CONST listsum (map (%x. b) xs)"
nipkow@23096
  1878
  "\<Sum>x\<leftarrow>xs. b" == "CONST listsum (map (%x. b) xs)"
nipkow@23096
  1879
nipkow@23096
  1880
lemma listsum_0 [simp]: "(\<Sum>x\<leftarrow>xs. 0) = 0"
nipkow@23096
  1881
by (induct xs) simp_all
nipkow@23096
  1882
nipkow@23096
  1883
text{* For non-Abelian groups @{text xs} needs to be reversed on one side: *}
nipkow@23096
  1884
lemma uminus_listsum_map:
nipkow@23096
  1885
 "- listsum (map f xs) = (listsum (map (uminus o f) xs) :: 'a::ab_group_add)"
nipkow@23096
  1886
by(induct xs) simp_all
nipkow@23096
  1887
wenzelm@13114
  1888
nipkow@15392
  1889
subsubsection {* @{text upto} *}
wenzelm@13114
  1890
nipkow@17090
  1891
lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])"
nipkow@17090
  1892
-- {* simp does not terminate! *}
nipkow@13145
  1893
by (induct j) auto
wenzelm@13142
  1894
nipkow@15425
  1895
lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []"
nipkow@13145
  1896
by (subst upt_rec) simp
wenzelm@13114
  1897
nipkow@15425
  1898
lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)"
nipkow@15281
  1899
by(induct j)simp_all
nipkow@15281
  1900
nipkow@15281
  1901
lemma upt_eq_Cons_conv:
nipkow@24526
  1902
 "([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)"
nipkow@24526
  1903
apply(induct j arbitrary: x xs)
nipkow@15281
  1904
 apply simp
nipkow@15281
  1905
apply(clarsimp simp add: append_eq_Cons_conv)
nipkow@15281
  1906
apply arith
nipkow@15281
  1907
done
nipkow@15281
  1908
nipkow@15425
  1909
lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]"
nipkow@13145
  1910
-- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
nipkow@13145
  1911
by simp
wenzelm@13114
  1912
nipkow@15425
  1913
lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]"
nipkow@13145
  1914
apply(rule trans)
nipkow@13145
  1915
apply(subst upt_rec)
paulson@14208
  1916
 prefer 2 apply (rule refl, simp)
nipkow@13145
  1917
done
wenzelm@13114
  1918
nipkow@15425
  1919
lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]"
nipkow@13145
  1920
-- {* LOOPS as a simprule, since @{text "j <= j"}. *}
nipkow@13145
  1921
by (induct k) auto
wenzelm@13114
  1922
nipkow@15425
  1923
lemma length_upt [simp]: "length [i..<j] = j - i"
nipkow@13145
  1924
by (induct j) (auto simp add: Suc_diff_le)
wenzelm@13114
  1925
nipkow@15425
  1926
lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k"
nipkow@13145
  1927
apply (induct j)
nipkow@13145
  1928
apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
nipkow@13145
  1929
done
wenzelm@13114
  1930
nipkow@17906
  1931
nipkow@17906
  1932
lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i"
nipkow@17906
  1933
by(simp add:upt_conv_Cons)
nipkow@17906
  1934
nipkow@17906
  1935
lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1"
nipkow@17906
  1936
apply(cases j)
nipkow@17906
  1937
 apply simp
nipkow@17906
  1938
by(simp add:upt_Suc_append)
nipkow@17906
  1939
nipkow@24526
  1940
lemma take_upt [simp]: "i+m <= n ==> take m [i..<n] = [i..<i+m]"
nipkow@24526
  1941
apply (induct m arbitrary: i, simp)
nipkow@13145
  1942
apply (subst upt_rec)
nipkow@13145
  1943
apply (rule sym)
nipkow@13145
  1944
apply (subst upt_rec)
nipkow@13145
  1945
apply (simp del: upt.simps)
nipkow@13145
  1946
done
nipkow@3507
  1947
nipkow@17501
  1948
lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]"
nipkow@17501
  1949
apply(induct j)
nipkow@17501
  1950
apply auto
nipkow@17501
  1951
done
nipkow@17501
  1952
nipkow@15425
  1953
lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..n]"
nipkow@13145
  1954
by (induct n) auto
wenzelm@13114
  1955
nipkow@24526
  1956
lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)"
nipkow@24526
  1957
apply (induct n m  arbitrary: i rule: diff_induct)
nipkow@13145
  1958
prefer 3 apply (subst map_Suc_upt[symmetric])
nipkow@13145
  1959
apply (auto simp add: less_diff_conv nth_upt)
nipkow@13145
  1960
done
wenzelm@13114
  1961
berghofe@13883
  1962
lemma nth_take_lemma:
nipkow@24526
  1963
  "k <= length xs ==> k <= length ys ==>
berghofe@13883
  1964
     (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"
nipkow@24526
  1965
apply (atomize, induct k arbitrary: xs ys)
paulson@14208
  1966
apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)
nipkow@13145
  1967
txt {* Both lists must be non-empty *}
paulson@14208
  1968
apply (case_tac xs, simp)
paulson@14208
  1969
apply (case_tac ys, clarify)
nipkow@13145
  1970
 apply (simp (no_asm_use))
nipkow@13145
  1971
apply clarify
nipkow@13145
  1972
txt {* prenexing's needed, not miniscoping *}
nipkow@13145
  1973
apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
nipkow@13145
  1974
apply blast
nipkow@13145
  1975
done
wenzelm@13114
  1976
wenzelm@13114
  1977
lemma nth_equalityI:
wenzelm@13114
  1978
 "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
nipkow@13145
  1979
apply (frule nth_take_lemma [OF le_refl eq_imp_le])
nipkow@13145
  1980
apply (simp_all add: take_all)
nipkow@13145
  1981
done
wenzelm@13142
  1982
kleing@13863
  1983
(* needs nth_equalityI *)
kleing@13863
  1984
lemma list_all2_antisym:
kleing@13863
  1985
  "\<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
  1986
  \<Longrightarrow> xs = ys"
kleing@13863
  1987
  apply (simp add: list_all2_conv_all_nth) 
paulson@14208
  1988
  apply (rule nth_equalityI, blast, simp)
kleing@13863
  1989
  done
kleing@13863
  1990
wenzelm@13142
  1991
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
nipkow@13145
  1992
-- {* The famous take-lemma. *}
nipkow@13145
  1993
apply (drule_tac x = "max (length xs) (length ys)" in spec)
nipkow@13145
  1994
apply (simp add: le_max_iff_disj take_all)
nipkow@13145
  1995
done
wenzelm@13142
  1996
wenzelm@13142
  1997
nipkow@15302
  1998
lemma take_Cons':
nipkow@15302
  1999
     "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
nipkow@15302
  2000
by (cases n) simp_all
nipkow@15302
  2001
nipkow@15302
  2002
lemma drop_Cons':
nipkow@15302
  2003
     "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
nipkow@15302
  2004
by (cases n) simp_all
nipkow@15302
  2005
nipkow@15302
  2006
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
nipkow@15302
  2007
by (cases n) simp_all
nipkow@15302
  2008
paulson@18622
  2009
lemmas take_Cons_number_of = take_Cons'[of "number_of v",standard]
paulson@18622
  2010
lemmas drop_Cons_number_of = drop_Cons'[of "number_of v",standard]
paulson@18622
  2011
lemmas nth_Cons_number_of = nth_Cons'[of _ _ "number_of v",standard]
paulson@18622
  2012
paulson@18622
  2013
declare take_Cons_number_of [simp] 
paulson@18622
  2014
        drop_Cons_number_of [simp] 
paulson@18622
  2015
        nth_Cons_number_of [simp] 
nipkow@15302
  2016
nipkow@15302
  2017
nipkow@15392
  2018
subsubsection {* @{text "distinct"} and @{text remdups} *}
wenzelm@13142
  2019
wenzelm@13142
  2020
lemma distinct_append [simp]:
nipkow@13145
  2021
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
nipkow@13145
  2022
by (induct xs) auto
wenzelm@13142
  2023
nipkow@15305
  2024
lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs"
nipkow@15305
  2025
by(induct xs) auto
nipkow@15305
  2026
wenzelm@13142
  2027
lemma set_remdups [simp]: "set (remdups xs) = set xs"
nipkow@13145
  2028
by (induct xs) (auto simp add: insert_absorb)
wenzelm@13142
  2029
wenzelm@13142
  2030
lemma distinct_remdups [iff]: "distinct (remdups xs)"
nipkow@13145
  2031
by (induct xs) auto
wenzelm@13142
  2032
paulson@15072
  2033
lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])"
nipkow@24349
  2034
by (induct x, auto) 
paulson@15072
  2035
paulson@15072
  2036
lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])"
nipkow@24349
  2037
by (induct x, auto)
paulson@15072
  2038
nipkow@15245
  2039
lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs"
nipkow@15245
  2040
by (induct xs) auto
nipkow@15245
  2041
nipkow@15245
  2042
lemma length_remdups_eq[iff]:
nipkow@15245
  2043
  "(length (remdups xs) = length xs) = (remdups xs = xs)"
nipkow@15245
  2044
apply(induct xs)
nipkow@15245
  2045
 apply auto
nipkow@15245
  2046
apply(subgoal_tac "length (remdups xs) <= length xs")
nipkow@15245
  2047
 apply arith
nipkow@15245
  2048
apply(rule length_remdups_leq)
nipkow@15245
  2049
done
nipkow@15245
  2050
nipkow@18490
  2051
nipkow@18490
  2052
lemma distinct_map:
nipkow@18490
  2053
  "distinct(map f xs) = (distinct xs & inj_on f (set xs))"
nipkow@18490
  2054
by (induct xs) auto
nipkow@18490
  2055
nipkow@18490
  2056
wenzelm@13142
  2057
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"
nipkow@13145
  2058
by (induct xs) auto
wenzelm@13114
  2059
nipkow@17501
  2060
lemma distinct_upt[simp]: "distinct[i..<j]"
nipkow@17501
  2061
by (induct j) auto
nipkow@17501
  2062
nipkow@24526
  2063
lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)"
nipkow@24526
  2064
apply(induct xs arbitrary: i)
nipkow@17501
  2065
 apply simp
nipkow@17501
  2066
apply (case_tac i)
nipkow@17501
  2067
 apply simp_all
nipkow@17501
  2068
apply(blast dest:in_set_takeD)
nipkow@17501
  2069
done
nipkow@17501
  2070
nipkow@24526
  2071
lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)"
nipkow@24526
  2072
apply(induct xs arbitrary: i)
nipkow@17501
  2073
 apply simp
nipkow@17501
  2074
apply (case_tac i)
nipkow@17501
  2075
 apply simp_all
nipkow@17501
  2076
done
nipkow@17501
  2077
nipkow@17501
  2078
lemma distinct_list_update:
nipkow@17501
  2079
assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}"
nipkow@17501
  2080
shows "distinct (xs[i:=a])"
nipkow@17501
  2081
proof (cases "i < length xs")
nipkow@17501
  2082
  case True
nipkow@17501
  2083
  with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}"
nipkow@17501
  2084
    apply (drule_tac id_take_nth_drop) by simp
nipkow@17501
  2085
  with d True show ?thesis
nipkow@17501
  2086
    apply (simp add: upd_conv_take_nth_drop)
nipkow@17501
  2087
    apply (drule subst [OF id_take_nth_drop]) apply assumption
nipkow@17501
  2088
    apply simp apply (cases "a = xs!i") apply simp by blast
nipkow@17501
  2089
next
nipkow@17501
  2090
  case False with d show ?thesis by auto
nipkow@17501
  2091
qed
nipkow@17501
  2092
nipkow@17501
  2093
nipkow@17501
  2094
text {* It is best to avoid this indexed version of distinct, but
nipkow@17501
  2095
sometimes it is useful. *}
nipkow@17501
  2096
wenzelm@13142
  2097
lemma distinct_conv_nth:
nipkow@17501
  2098
"distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)"
paulson@15251
  2099
apply (induct xs, simp, simp)
paulson@14208
  2100
apply (rule iffI, clarsimp)
nipkow@13145
  2101
 apply (case_tac i)
paulson@14208
  2102
apply (case_tac j, simp)
nipkow@13145
  2103
apply (simp add: set_conv_nth)
nipkow@13145
  2104
 apply (case_tac j)
paulson@14208
  2105
apply (clarsimp simp add: set_conv_nth, simp)
nipkow@13145
  2106
apply (rule conjI)
nipkow@13145
  2107
 apply (clarsimp simp add: set_conv_nth)
nipkow@17501
  2108
 apply (erule_tac x = 0 in allE, simp)
paulson@14208
  2109
 apply (erule_tac x = "Suc i" in allE, simp, clarsimp)
nipkow@17501
  2110
apply (erule_tac x = "Suc i" in allE, simp)
paulson@14208
  2111
apply (erule_tac x = "Suc j" in allE, simp)
nipkow@13145
  2112
done
wenzelm@13114
  2113
nipkow@18490
  2114
lemma nth_eq_iff_index_eq:
nipkow@18490
  2115
 "\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)"
nipkow@18490
  2116
by(auto simp: distinct_conv_nth)
nipkow@18490
  2117
nipkow@15110
  2118
lemma distinct_card: "distinct xs ==> card (set xs) = size xs"
nipkow@24349
  2119
by (induct xs) auto
kleing@14388
  2120
nipkow@15110
  2121
lemma card_distinct: "card (set xs) = size xs ==> distinct xs"
kleing@14388
  2122
proof (induct xs)
kleing@14388
  2123
  case Nil thus ?case by simp
kleing@14388
  2124
next
kleing@14388
  2125
  case (Cons x xs)
kleing@14388
  2126
  show ?case
kleing@14388
  2127
  proof (cases "x \<in> set xs")
kleing@14388
  2128
    case False with Cons show ?thesis by simp
kleing@14388
  2129
  next
kleing@14388
  2130
    case True with Cons.prems
kleing@14388
  2131
    have "card (set xs) = Suc (length xs)" 
kleing@14388
  2132
      by (simp add: card_insert_if split: split_if_asm)
kleing@14388
  2133
    moreover have "card (set xs) \<le> length xs" by (rule card_length)
kleing@14388
  2134
    ultimately have False by simp
kleing@14388
  2135
    thus ?thesis ..
kleing@14388
  2136
  qed
kleing@14388
  2137
qed
kleing@14388
  2138
nipkow@18490
  2139
nipkow@18490
  2140
lemma length_remdups_concat:
nipkow@18490
  2141
 "length(remdups(concat xss)) = card(\<Union>xs \<in> set xss. set xs)"
nipkow@24308
  2142
by(simp add: set_concat distinct_card[symmetric])
nipkow@17906
  2143
nipkow@17906
  2144
nipkow@15392
  2145
subsubsection {* @{text remove1} *}
nipkow@15110
  2146
nipkow@18049
  2147
lemma remove1_append:
nipkow@18049
  2148
  "remove1 x (xs @ ys) =
nipkow@18049
  2149
  (if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)"
nipkow@18049
  2150
by (induct xs) auto
nipkow@18049
  2151
nipkow@23479
  2152
lemma in_set_remove1[simp]:
nipkow@23479
  2153
  "a \<noteq> b \<Longrightarrow> a : set(remove1 b xs) = (a : set xs)"
nipkow@23479
  2154
apply (induct xs)
nipkow@23479
  2155
apply auto
nipkow@23479
  2156
done
nipkow@23479
  2157
nipkow@15110
  2158
lemma set_remove1_subset: "set(remove1 x xs) <= set xs"
nipkow@15110
  2159
apply(induct xs)
nipkow@15110
  2160
 apply simp
nipkow@15110
  2161
apply simp
nipkow@15110
  2162
apply blast
nipkow@15110
  2163
done
nipkow@15110
  2164
paulson@17724
  2165
lemma set_remove1_eq [simp]: "distinct xs ==> set(remove1 x xs) = set xs - {x}"
nipkow@15110
  2166
apply(induct xs)
nipkow@15110
  2167
 apply simp
nipkow@15110
  2168
apply simp
nipkow@15110
  2169
apply blast
nipkow@15110
  2170
done
nipkow@15110
  2171
nipkow@23479
  2172
lemma length_remove1:
nipkow@23479
  2173
  "length(remove1 x xs) = (if x : set xs then length xs - 1 else length xs)"
nipkow@23479
  2174
apply (induct xs)
nipkow@23479
  2175
 apply (auto dest!:length_pos_if_in_set)
nipkow@23479
  2176
done
nipkow@23479
  2177
nipkow@18049
  2178
lemma remove1_filter_not[simp]:
nipkow@18049
  2179
  "\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs"
nipkow@18049
  2180
by(induct xs) auto
nipkow@18049
  2181
nipkow@15110
  2182
lemma notin_set_remove1[simp]: "x ~: set xs ==> x ~: set(remove1 y xs)"
nipkow@15110
  2183
apply(insert set_remove1_subset)
nipkow@15110
  2184
apply fast
nipkow@15110
  2185
done
nipkow@15110
  2186
nipkow@15110
  2187
lemma distinct_remove1[simp]: "distinct xs ==> distinct(remove1 x xs)"
nipkow@15110
  2188
by (induct xs) simp_all
nipkow@15110
  2189
wenzelm@13114
  2190
nipkow@15392
  2191
subsubsection {* @{text replicate} *}
wenzelm@13114
  2192
wenzelm@13142
  2193
lemma length_replicate [simp]: "length (replicate n x) = n"
nipkow@13145
  2194
by (induct n) auto
nipkow@13124
  2195
wenzelm@13142
  2196
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
nipkow@13145
  2197
by (induct n) auto
wenzelm@13114
  2198
wenzelm@13114
  2199
lemma replicate_app_Cons_same:
nipkow@13145
  2200
"(replicate n x) @ (x # xs) = x # replicate n x @ xs"
nipkow@13145
  2201
by (induct n) auto
wenzelm@13114
  2202
wenzelm@13142
  2203
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
paulson@14208
  2204
apply (induct n, simp)
nipkow@13145
  2205
apply (simp add: replicate_app_Cons_same)
nipkow@13145
  2206
done
wenzelm@13114
  2207
wenzelm@13142
  2208
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
nipkow@13145
  2209
by (induct n) auto
wenzelm@13114
  2210
nipkow@16397
  2211
text{* Courtesy of Matthias Daum: *}
nipkow@16397
  2212
lemma append_replicate_commute:
nipkow@16397
  2213
  "replicate n x @ replicate k x = replicate k x @ replicate n x"
nipkow@16397
  2214
apply (simp add: replicate_add [THEN sym])
nipkow@16397
  2215
apply (simp add: add_commute)
nipkow@16397
  2216
done
nipkow@16397
  2217
wenzelm@13142
  2218
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"
nipkow@13145
  2219
by (induct n) auto
wenzelm@13114
  2220
wenzelm@13142
  2221
lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"
nipkow@13145
  2222
by (induct n) auto
wenzelm@13114
  2223
wenzelm@13142
  2224
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"
nipkow@13145
  2225
by (atomize (full), induct n) auto
wenzelm@13114
  2226
nipkow@24526
  2227
lemma nth_replicate[simp]: "i < n ==> (replicate n x)!i = x"
nipkow@24526
  2228
apply (induct n arbitrary: i, simp)
nipkow@13145
  2229
apply (simp add: nth_Cons split: nat.split)
nipkow@13145
  2230
done
wenzelm@13114
  2231
nipkow@16397
  2232
text{* Courtesy of Matthias Daum (2 lemmas): *}
nipkow@16397
  2233
lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x"
nipkow@16397
  2234
apply (case_tac "k \<le> i")
nipkow@16397
  2235
 apply  (simp add: min_def)
nipkow@16397
  2236
apply (drule not_leE)
nipkow@16397
  2237
apply (simp add: min_def)
nipkow@16397
  2238
apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x")
nipkow@16397
  2239
 apply  simp
nipkow@16397
  2240
apply (simp add: replicate_add [symmetric])
nipkow@16397
  2241
done
nipkow@16397
  2242
nipkow@24526
  2243
lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x"
nipkow@24526
  2244
apply (induct k arbitrary: i)
nipkow@16397
  2245
 apply simp
nipkow@16397
  2246
apply clarsimp
nipkow@16397
  2247
apply (case_tac i)
nipkow@16397
  2248
 apply simp
nipkow@16397
  2249
apply clarsimp
nipkow@16397
  2250
done
nipkow@16397
  2251
nipkow@16397
  2252
wenzelm@13142
  2253
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
nipkow@13145
  2254
by (induct n) auto
wenzelm@13114
  2255
wenzelm@13142
  2256
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
nipkow@13145
  2257
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
wenzelm@13114
  2258
wenzelm@13142
  2259
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
nipkow@13145
  2260
by auto
wenzelm@13114
  2261
wenzelm@13142
  2262
lemma in_set_replicateD: "x : set (replicate n y) ==> x = y"
nipkow@13145
  2263
by (simp add: set_replicate_conv_if split: split_if_asm)
wenzelm@13114
  2264
wenzelm@13114
  2265
nipkow@15392
  2266
subsubsection{*@{text rotate1} and @{text rotate}*}
nipkow@15302
  2267
nipkow@15302
  2268
lemma rotate_simps[simp]: "rotate1 [] = [] \<and> rotate1 (x#xs) = xs @ [x]"
nipkow@15302
  2269
by(simp add:rotate1_def)
nipkow@15302
  2270
nipkow@15302
  2271
lemma rotate0[simp]: "rotate 0 = id"
nipkow@15302
  2272
by(simp add:rotate_def)
nipkow@15302
  2273
nipkow@15302
  2274
lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)"
nipkow@15302
  2275
by(simp add:rotate_def)
nipkow@15302
  2276
nipkow@15302
  2277
lemma rotate_add:
nipkow@15302
  2278
  "rotate (m+n) = rotate m o rotate n"
nipkow@15302
  2279
by(simp add:rotate_def funpow_add)
nipkow@15302
  2280
nipkow@15302
  2281
lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs"
nipkow@15302
  2282
by(simp add:rotate_add)
nipkow@15302
  2283
nipkow@18049
  2284
lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)"
nipkow@18049
  2285
by(simp add:rotate_def funpow_swap1)
nipkow@18049
  2286
nipkow@15302
  2287
lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs"
nipkow@15302
  2288
by(cases xs) simp_all
nipkow@15302
  2289
nipkow@15302
  2290
lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs"
nipkow@15302
  2291
apply(induct n)
nipkow@15302
  2292
 apply simp
nipkow@15302
  2293
apply (simp add:rotate_def)
nipkow@13145
  2294
done
wenzelm@13114
  2295
nipkow@15302
  2296
lemma rotate1_hd_tl: "xs \<noteq> [] \<Longrightarrow> rotate1 xs = tl xs @ [hd xs]"
nipkow@15302
  2297
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2298
nipkow@15302
  2299
lemma rotate_drop_take:
nipkow@15302
  2300
  "rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs"
nipkow@15302
  2301
apply(induct n)
nipkow@15302
  2302
 apply simp
nipkow@15302
  2303
apply(simp add:rotate_def)
nipkow@15302
  2304
apply(cases "xs = []")
nipkow@15302
  2305
 apply (simp)
nipkow@15302
  2306
apply(case_tac "n mod length xs = 0")
nipkow@15302
  2307
 apply(simp add:mod_Suc)
nipkow@15302
  2308
 apply(simp add: rotate1_hd_tl drop_Suc take_Suc)
nipkow@15302
  2309
apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric]
nipkow@15302
  2310
                take_hd_drop linorder_not_le)
nipkow@13145
  2311
done
wenzelm@13114
  2312
nipkow@15302
  2313
lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs"
nipkow@15302
  2314
by(simp add:rotate_drop_take)
nipkow@15302
  2315
nipkow@15302
  2316
lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs"
nipkow@15302
  2317
by(simp add:rotate_drop_take)
nipkow@15302
  2318
nipkow@15302
  2319
lemma length_rotate1[simp]: "length(rotate1 xs) = length xs"
nipkow@15302
  2320
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2321
nipkow@24526
  2322
lemma length_rotate[simp]: "length(rotate n xs) = length xs"
nipkow@24526
  2323
by (induct n arbitrary: xs) (simp_all add:rotate_def)
nipkow@15302
  2324
nipkow@15302
  2325
lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs"
nipkow@15302
  2326
by(simp add:rotate1_def split:list.split) blast
nipkow@15302
  2327
nipkow@15302
  2328
lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs"
nipkow@15302
  2329
by (induct n) (simp_all add:rotate_def)
nipkow@15302
  2330
nipkow@15302
  2331
lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)"
nipkow@15302
  2332
by(simp add:rotate_drop_take take_map drop_map)
nipkow@15302
  2333
nipkow@15302
  2334
lemma set_rotate1[simp]: "set(rotate1 xs) = set xs"
nipkow@15302
  2335
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2336
nipkow@15302
  2337
lemma set_rotate[simp]: "set(rotate n xs) = set xs"
nipkow@15302
  2338
by (induct n) (simp_all add:rotate_def)
nipkow@15302
  2339
nipkow@15302
  2340
lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])"
nipkow@15302
  2341
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2342
nipkow@15302
  2343
lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])"
nipkow@15302
  2344
by (induct n) (simp_all add:rotate_def)
wenzelm@13114
  2345
nipkow@15439
  2346
lemma rotate_rev:
nipkow@15439
  2347
  "rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)"
nipkow@15439
  2348
apply(simp add:rotate_drop_take rev_drop rev_take)
nipkow@15439
  2349
apply(cases "length xs = 0")
nipkow@15439
  2350
 apply simp
nipkow@15439
  2351
apply(cases "n mod length xs = 0")
nipkow@15439
  2352
 apply simp
nipkow@15439
  2353
apply(simp add:rotate_drop_take rev_drop rev_take)
nipkow@15439
  2354
done
nipkow@15439
  2355
nipkow@18423
  2356
lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)"
nipkow@18423
  2357
apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth)
nipkow@18423
  2358
apply(subgoal_tac "length xs \<noteq> 0")
nipkow@18423
  2359
 prefer 2 apply simp
nipkow@18423
  2360
using mod_less_divisor[of "length xs" n] by arith
nipkow@18423
  2361
wenzelm@13114
  2362
nipkow@15392
  2363
subsubsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
wenzelm@13114
  2364
wenzelm@13142
  2365
lemma sublist_empty [simp]: "sublist xs {} = []"
nipkow@13145
  2366
by (auto simp add: sublist_def)
wenzelm@13114
  2367
wenzelm@13142
  2368
lemma sublist_nil [simp]: "sublist [] A = []"
nipkow@13145
  2369
by (auto simp add: sublist_def)
wenzelm@13114
  2370
nipkow@15281
  2371
lemma length_sublist:
nipkow@15281
  2372
  "length(sublist xs I) = card{i. i < length xs \<and> i : I}"
nipkow@15281
  2373
by(simp add: sublist_def length_filter_conv_card cong:conj_cong)
nipkow@15281
  2374
nipkow@15281
  2375
lemma sublist_shift_lemma_Suc:
nipkow@24526
  2376
  "map fst (filter (%p. P(Suc(snd p))) (zip xs is)) =
nipkow@24526
  2377
   map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))"
nipkow@24526
  2378
apply(induct xs arbitrary: "is")
nipkow@15281
  2379
 apply simp
nipkow@15281
  2380
apply (case_tac "is")
nipkow@15281
  2381
 apply simp
nipkow@15281
  2382
apply simp
nipkow@15281
  2383
done
nipkow@15281
  2384
wenzelm@13114
  2385
lemma sublist_shift_lemma:
nipkow@23279
  2386
     "map fst [p<-zip xs [i..<i + length xs] . snd p : A] =
nipkow@23279
  2387
      map fst [p<-zip xs [0..<length xs] . snd p + i : A]"
nipkow@13145
  2388
by (induct xs rule: rev_induct) (simp_all add: add_commute)
wenzelm@13114
  2389
wenzelm@13114
  2390
lemma sublist_append:
paulson@15168
  2391
     "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
nipkow@13145
  2392
apply (unfold sublist_def)
paulson@14208
  2393
apply (induct l' rule: rev_induct, simp)
nipkow@13145
  2394
apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
nipkow@13145
  2395
apply (simp add: add_commute)
nipkow@13145
  2396
done
wenzelm@13114
  2397
wenzelm@13114
  2398
lemma sublist_Cons:
nipkow@13145
  2399
"sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
nipkow@13145
  2400
apply (induct l rule: rev_induct)
nipkow@13145
  2401
 apply (simp add: sublist_def)
nipkow@13145
  2402
apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)
nipkow@13145
  2403
done
wenzelm@13114
  2404
nipkow@24526
  2405
lemma set_sublist: "set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}"
nipkow@24526
  2406
apply(induct xs arbitrary: I)
nipkow@15281
  2407
 apply simp
nipkow@15281
  2408
apply(auto simp add:sublist_Cons nth_Cons split:nat.split elim: lessE)
nipkow@15281
  2409
 apply(erule lessE)
nipkow@15281
  2410
  apply auto
nipkow@15281
  2411
apply(erule lessE)
nipkow@15281
  2412
apply auto
nipkow@15281
  2413
done
nipkow@15281
  2414
nipkow@15281
  2415
lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs"
nipkow@15281
  2416
by(auto simp add:set_sublist)
nipkow@15281
  2417
nipkow@15281
  2418
lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)"
nipkow@15281
  2419
by(auto simp add:set_sublist)
nipkow@15281
  2420
nipkow@15281
  2421
lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs"
nipkow@15281
  2422
by(auto simp add:set_sublist)
nipkow@15281
  2423
wenzelm@13142
  2424
lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"
nipkow@13145
  2425
by (simp add: sublist_Cons)
wenzelm@13114
  2426
nipkow@15281
  2427
nipkow@24526
  2428
lemma distinct_sublistI[simp]: "distinct xs \<Longrightarrow> distinct(sublist xs I)"
nipkow@24526
  2429
apply(induct xs arbitrary: I)
nipkow@15281
  2430
 apply simp
nipkow@15281
  2431
apply(auto simp add:sublist_Cons)
nipkow@15281
  2432
done
nipkow@15281
  2433
nipkow@15281
  2434
nipkow@15045
  2435
lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l"
paulson@14208
  2436
apply (induct l rule: rev_induct, simp)
nipkow@13145
  2437
apply (simp split: nat_diff_split add: sublist_append)
nipkow@13145
  2438
done
wenzelm@13114
  2439
nipkow@24526
  2440
lemma filter_in_sublist:
nipkow@24526
  2441
 "distinct xs \<Longrightarrow> filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s"
nipkow@24526
  2442
proof (induct xs arbitrary: s)
nipkow@17501
  2443
  case Nil thus ?case by simp
nipkow@17501
  2444
next
nipkow@17501
  2445
  case (Cons a xs)
nipkow@17501
  2446
  moreover hence "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto
nipkow@17501
  2447
  ultimately show ?case by(simp add: sublist_Cons cong:filter_cong)
nipkow@17501
  2448
qed
nipkow@17501
  2449
wenzelm@13114
  2450
nipkow@19390
  2451
subsubsection {* @{const splice} *}
nipkow@19390
  2452
haftmann@19607
  2453
lemma splice_Nil2 [simp, code]:
nipkow@19390
  2454
 "splice xs [] = xs"
nipkow@19390
  2455
by (cases xs) simp_all
nipkow@19390
  2456
haftmann@19607
  2457
lemma splice_Cons_Cons [simp, code]:
nipkow@19390
  2458
 "splice (x#xs) (y#ys) = x # y # splice xs ys"
nipkow@19390
  2459
by simp
nipkow@19390
  2460
haftmann@19607
  2461
declare splice.simps(2) [simp del, code del]
nipkow@19390
  2462
nipkow@24526
  2463
lemma length_splice[simp]: "length(splice xs ys) = length xs + length ys"
nipkow@24526
  2464
apply(induct xs arbitrary: ys) apply simp
nipkow@22793
  2465
apply(case_tac ys)
nipkow@22793
  2466
 apply auto
nipkow@22793
  2467
done
nipkow@22793
  2468
nipkow@15392
  2469
subsubsection {* @{text lists}: the list-forming operator over sets *}
nipkow@15302
  2470
berghofe@23740
  2471
inductive_set
berghofe@22262
  2472
  lists :: "'a set => 'a list set"
berghofe@23740
  2473
  for A :: "'a set"
berghofe@23740
  2474
where
berghofe@23740
  2475
    Nil [intro!]: "[]: lists A"
paulson@24286
  2476
  | Cons [intro!,noatp]: "[| a: A;l: lists A|] ==> a#l : lists A"
paulson@24286
  2477
paulson@24286
  2478
inductive_cases listsE [elim!,noatp]: "x#l : lists A"
paulson@24286
  2479
inductive_cases listspE [elim!,noatp]: "listsp A (x # l)"
berghofe@23740
  2480
berghofe@23740
  2481
lemma listsp_mono [mono]: "A \<le> B ==> listsp A \<le> listsp B"
nipkow@24349
  2482
by (clarify, erule listsp.induct, blast+)
berghofe@22262
  2483
berghofe@23740
  2484
lemmas lists_mono = listsp_mono [to_set]
berghofe@22262
  2485
haftmann@22422
  2486
lemma listsp_infI:
haftmann@22422
  2487
  assumes l: "listsp A l" shows "listsp B l ==> listsp (inf A B) l" using l
nipkow@24349
  2488
by induct blast+
nipkow@15302
  2489
haftmann@22422
  2490
lemmas lists_IntI = listsp_infI [to_set]
haftmann@22422
  2491
haftmann@22422
  2492
lemma listsp_inf_eq [simp]: "listsp (inf A B) = inf (listsp A) (listsp B)"
haftmann@22422
  2493
proof (rule mono_inf [where f=listsp, THEN order_antisym])
berghofe@22262
  2494
  show "mono listsp" by (simp add: mono_def listsp_mono)
haftmann@22422
  2495
  show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro: listsp_infI)
kleing@14388
  2496
qed
kleing@14388
  2497
haftmann@22422
  2498
lemmas listsp_conj_eq [simp] = listsp_inf_eq [simplified inf_fun_eq inf_bool_eq]
haftmann@22422
  2499
haftmann@22422
  2500
lemmas lists_Int_eq [simp] = listsp_inf_eq [to_set]
berghofe@22262
  2501
berghofe@22262
  2502
lemma append_in_listsp_conv [iff]:
berghofe@22262
  2503
     "(listsp A (xs @ ys)) = (listsp A xs \<and> listsp A ys)"
nipkow@15302
  2504
by (induct xs) auto
nipkow@15302
  2505
berghofe@22262
  2506
lemmas append_in_lists_conv [iff] = append_in_listsp_conv [to_set]
berghofe@22262
  2507
berghofe@22262
  2508
lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)"
berghofe@22262
  2509
-- {* eliminate @{text listsp} in favour of @{text set} *}
nipkow@15302
  2510
by (induct xs) auto
nipkow@15302
  2511
berghofe@22262
  2512
lemmas in_lists_conv_set = in_listsp_conv_set [to_set]
berghofe@22262
  2513
paulson@24286
  2514
lemma in_listspD [dest!,noatp]: "listsp A xs ==> \<forall>x\<in>set xs. A x"
berghofe@22262
  2515
by (rule in_listsp_conv_set [THEN iffD1])
berghofe@22262
  2516
paulson@24286
  2517
lemmas in_listsD [dest!,noatp] = in_listspD [to_set]
paulson@24286
  2518
paulson@24286
  2519
lemma in_listspI [intro!,noatp]: "\<forall>x\<in>set xs. A x ==> listsp A xs"
berghofe@22262
  2520
by (rule in_listsp_conv_set [THEN iffD2])
berghofe@22262
  2521
paulson@24286
  2522
lemmas in_listsI [intro!,noatp] = in_listspI [to_set]
nipkow@15302
  2523
nipkow@15302
  2524
lemma lists_UNIV [simp]: "lists UNIV = UNIV"
nipkow@15302
  2525
by auto
nipkow@15302
  2526
nipkow@17086
  2527
nipkow@17086
  2528
nipkow@17086
  2529
subsubsection{* Inductive definition for membership *}
nipkow@17086
  2530
berghofe@23740
  2531
inductive ListMem :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"
berghofe@22262
  2532
where
berghofe@22262
  2533
    elem:  "ListMem x (x # xs)"
berghofe@22262
  2534
  | insert:  "ListMem x xs \<Longrightarrow> ListMem x (y # xs)"
berghofe@22262
  2535
berghofe@22262
  2536
lemma ListMem_iff: "(ListMem x xs) = (x \<in> set xs)"
nipkow@17086
  2537
apply (rule iffI)
nipkow@17086
  2538
 apply (induct set: ListMem)
nipkow@17086
  2539
  apply auto
nipkow@17086
  2540
apply (induct xs)
nipkow@17086
  2541
 apply (auto intro: ListMem.intros)
nipkow@17086
  2542
done
nipkow@17086
  2543
nipkow@17086
  2544
nipkow@17086
  2545
nipkow@15392
  2546
subsubsection{*Lists as Cartesian products*}
nipkow@15302
  2547
nipkow@15302
  2548
text{*@{text"set_Cons A Xs"}: the set of lists with head drawn from
nipkow@15302
  2549
@{term A} and tail drawn from @{term Xs}.*}
nipkow@15302
  2550
nipkow@15302
  2551
constdefs
nipkow@15302
  2552
  set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set"
nipkow@15302
  2553
  "set_Cons A XS == {z. \<exists>x xs. z = x#xs & x \<in> A & xs \<in> XS}"
nipkow@15302
  2554
paulson@17724
  2555
lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A"
nipkow@15302
  2556
by (auto simp add: set_Cons_def)
nipkow@15302
  2557
nipkow@15302
  2558
text{*Yields the set of lists, all of the same length as the argument and
nipkow@15302
  2559
with elements drawn from the corresponding element of the argument.*}
nipkow@15302
  2560
nipkow@15302
  2561
consts  listset :: "'a set list \<Rightarrow> 'a list set"
nipkow@15302
  2562
primrec
nipkow@15302
  2563
   "listset []    = {[]}"
nipkow@15302
  2564
   "listset(A#As) = set_Cons A (listset As)"
nipkow@15302
  2565
nipkow@15302
  2566
paulson@15656
  2567
subsection{*Relations on Lists*}
paulson@15656
  2568
paulson@15656
  2569
subsubsection {* Length Lexicographic Ordering *}
paulson@15656
  2570
paulson@15656
  2571
text{*These orderings preserve well-foundedness: shorter lists 
paulson@15656
  2572
  precede longer lists. These ordering are not used in dictionaries.*}
paulson@15656
  2573
paulson@15656
  2574
consts lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
paulson@15656
  2575
        --{*The lexicographic ordering for lists of the specified length*}
nipkow@15302
  2576
primrec
paulson@15656
  2577
  "lexn r 0 = {}"
paulson@15656
  2578
  "lexn r (Suc n) =
paulson@15656
  2579
    (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int
paulson@15656
  2580
    {(xs,ys). length xs = Suc n \<and> length ys = Suc n}"
nipkow@15302
  2581
nipkow@15302
  2582
constdefs
paulson@15656
  2583
  lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
paulson@15656
  2584
    "lex r == \<Union>n. lexn r n"
paulson@15656
  2585
        --{*Holds only between lists of the same length*}
paulson@15656
  2586
nipkow@15693
  2587
  lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
nipkow@15693
  2588
    "lenlex r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"
paulson@15656
  2589
        --{*Compares lists by their length and then lexicographically*}
nipkow@15302
  2590
nipkow@15302
  2591
nipkow@15302
  2592
lemma wf_lexn: "wf r ==> wf (lexn r n)"
nipkow@15302
  2593
apply (induct n, simp, simp)
nipkow@15302
  2594
apply(rule wf_subset)
nipkow@15302
  2595
 prefer 2 apply (rule Int_lower1)
nipkow@15302
  2596
apply(rule wf_prod_fun_image)
nipkow@15302
  2597
 prefer 2 apply (rule inj_onI, auto)
nipkow@15302
  2598
done
nipkow@15302
  2599
nipkow@15302
  2600
lemma lexn_length:
nipkow@24526
  2601
  "(xs, ys) : lexn r n ==> length xs = n \<and> length ys = n"
nipkow@24526
  2602
by (induct n arbitrary: xs ys) auto
nipkow@15302
  2603
nipkow@15302
  2604
lemma wf_lex [intro!]: "wf r ==> wf (lex r)"
nipkow@15302
  2605
apply (unfold lex_def)
nipkow@15302
  2606
apply (rule wf_UN)
nipkow@15302
  2607
apply (blast intro: wf_lexn, clarify)
nipkow@15302
  2608
apply (rename_tac m n)
nipkow@15302
  2609
apply (subgoal_tac "m \<noteq> n")
nipkow@15302
  2610
 prefer 2 apply blast
nipkow@15302
  2611
apply (blast dest: lexn_length not_sym)
nipkow@15302
  2612
done
nipkow@15302
  2613
nipkow@15302
  2614
lemma lexn_conv:
paulson@15656
  2615
  "lexn r n =
paulson@15656
  2616
    {(xs,ys). length xs = n \<and> length ys = n \<and>
paulson@15656
  2617
    (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}"
nipkow@18423
  2618
apply (induct n, simp)
nipkow@15302
  2619
apply (simp add: image_Collect lex_prod_def, safe, blast)
nipkow@15302
  2620
 apply (rule_tac x = "ab # xys" in exI, simp)
nipkow@15302
  2621
apply (case_tac xys, simp_all, blast)
nipkow@15302
  2622
done
nipkow@15302
  2623
nipkow@15302
  2624
lemma lex_conv:
paulson@15656
  2625
  "lex r =
paulson@15656
  2626
    {(xs,ys). length xs = length ys \<and>
paulson@15656
  2627
    (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}"
nipkow@15302
  2628
by (force simp add: lex_def lexn_conv)
nipkow@15302
  2629
nipkow@15693
  2630
lemma wf_lenlex [intro!]: "wf r ==> wf (lenlex r)"
nipkow@15693
  2631
by (unfold lenlex_def) blast
nipkow@15693
  2632
nipkow@15693
  2633
lemma lenlex_conv:
nipkow@15693
  2634
    "lenlex r = {(xs,ys). length xs < length ys |
paulson@15656
  2635
                 length xs = length ys \<and> (xs, ys) : lex r}"
nipkow@19623
  2636
by (simp add: lenlex_def diag_def lex_prod_def inv_image_def)
nipkow@15302
  2637
nipkow@15302
  2638
lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r"
nipkow@15302
  2639
by (simp add: lex_conv)
nipkow@15302
  2640
nipkow@15302
  2641
lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r"
nipkow@15302
  2642
by (simp add:lex_conv)
nipkow@15302
  2643
paulson@18447
  2644
lemma Cons_in_lex [simp]:
paulson@15656
  2645
    "((x # xs, y # ys) : lex r) =
paulson@15656
  2646
      ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)"
nipkow@15302
  2647
apply (simp add: lex_conv)
nipkow@15302
  2648
apply (rule iffI)
nipkow@15302
  2649
 prefer 2 apply (blast intro: Cons_eq_appendI, clarify)
nipkow@15302
  2650
apply (case_tac xys, simp, simp)
nipkow@15302
  2651
apply blast
nipkow@15302
  2652
done
nipkow@15302
  2653
nipkow@15302
  2654
paulson@15656
  2655
subsubsection {* Lexicographic Ordering *}
paulson@15656
  2656
paulson@15656
  2657
text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b".
paulson@15656
  2658
    This ordering does \emph{not} preserve well-foundedness.
nipkow@17090
  2659
     Author: N. Voelker, March 2005. *} 
paulson@15656
  2660
paulson@15656
  2661
constdefs 
paulson@15656
  2662
  lexord :: "('a * 'a)set \<Rightarrow> ('a list * 'a list) set" 
paulson@15656
  2663
  "lexord  r == {(x,y). \<exists> a v. y = x @ a # v \<or> 
paulson@15656
  2664
            (\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}"
paulson@15656
  2665
paulson@15656
  2666
lemma lexord_Nil_left[simp]:  "([],y) \<in> lexord r = (\<exists> a x. y = a # x)"
nipkow@24349
  2667
by (unfold lexord_def, induct_tac y, auto) 
paulson@15656
  2668
paulson@15656
  2669
lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r"
nipkow@24349
  2670
by (unfold lexord_def, induct_tac x, auto)
paulson@15656
  2671
paulson@15656
  2672
lemma lexord_cons_cons[simp]:
paulson@15656
  2673
     "((a # x, b # y) \<in> lexord r) = ((a,b)\<in> r | (a = b & (x,y)\<in> lexord r))"
paulson@15656
  2674
  apply (unfold lexord_def, safe, simp_all)
paulson@15656
  2675
  apply (case_tac u, simp, simp)
paulson@15656
  2676
  apply (case_tac u, simp, clarsimp, blast, blast, clarsimp)
paulson@15656
  2677
  apply (erule_tac x="b # u" in allE)
paulson@15656
  2678
  by force
paulson@15656
  2679
paulson@15656
  2680
lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons
paulson@15656
  2681
paulson@15656
  2682
lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r"
nipkow@24349
  2683
by (induct_tac x, auto)  
paulson@15656
  2684
paulson@15656
  2685
lemma lexord_append_left_rightI:
paulson@15656
  2686
     "(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r"
nipkow@24349
  2687
by (induct_tac u, auto)
paulson@15656
  2688
paulson@15656
  2689
lemma lexord_append_leftI: " (u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r"
nipkow@24349
  2690
by (induct x, auto)
paulson@15656
  2691
paulson@15656
  2692
lemma lexord_append_leftD:
paulson@15656
  2693
     "\<lbrakk> (x @ u, x @ v) \<in> lexord r; (! a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r"
nipkow@24349
  2694
by (erule rev_mp, induct_tac x, auto)
paulson@15656
  2695
paulson@15656
  2696
lemma lexord_take_index_conv: 
paulson@15656
  2697
   "((x,y) : lexord r) = 
paulson@15656
  2698
    ((length x < length y \<and> take (length x) y = x) \<or> 
paulson@15656
  2699
     (\<exists>i. i < min(length x)(length y) & take i x = take i y & (x!i,y!i) \<in> r))"
paulson@15656
  2700
  apply (unfold lexord_def Let_def, clarsimp) 
paulson@15656
  2701
  apply (rule_tac f = "(% a b. a \<or> b)" in arg_cong2)
paulson@15656
  2702
  apply auto 
paulson@15656
  2703
  apply (rule_tac x="hd (drop (length x) y)" in exI)
paulson@15656
  2704
  apply (rule_tac x="tl (drop (length x) y)" in exI)
paulson@15656
  2705
  apply (erule subst, simp add: min_def) 
paulson@15656
  2706
  apply (rule_tac x ="length u" in exI, simp) 
paulson@15656
  2707
  apply (rule_tac x ="take i x" in exI) 
paulson@15656
  2708
  apply (rule_tac x ="x ! i" in exI) 
paulson@15656
  2709
  apply (rule_tac x ="y ! i" in exI, safe) 
paulson@15656
  2710
  apply (rule_tac x="drop (Suc i) x" in exI)
paulson@15656
  2711
  apply (drule sym, simp add: drop_Suc_conv_tl) 
paulson@15656
  2712
  apply (rule_tac x="drop (Suc i) y" in exI)
paulson@15656
  2713
  by (simp add: drop_Suc_conv_tl) 
paulson@15656
  2714
paulson@15656
  2715
-- {* lexord is extension of partial ordering List.lex *} 
paulson@15656
  2716
lemma lexord_lex: " (x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)"
paulson@15656
  2717
  apply (rule_tac x = y in spec) 
paulson@15656
  2718
  apply (induct_tac x, clarsimp) 
paulson@15656
  2719
  by (clarify, case_tac x, simp, force)
paulson@15656
  2720
paulson@15656
  2721
lemma lexord_irreflexive: "(! x. (x,x) \<notin> r) \<Longrightarrow> (y,y) \<notin> lexord r"
paulson@15656
  2722
  by (induct y, auto)
paulson@15656
  2723
paulson@15656
  2724
lemma lexord_trans: 
paulson@15656
  2725
    "\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r"
paulson@15656
  2726
   apply (erule rev_mp)+
paulson@15656
  2727
   apply (rule_tac x = x in spec) 
paulson@15656
  2728
  apply (rule_tac x = z in spec) 
paulson@15656
  2729
  apply ( induct_tac y, simp, clarify)
paulson@15656
  2730
  apply (case_tac xa, erule ssubst) 
paulson@15656
  2731
  apply (erule allE, erule allE) -- {* avoid simp recursion *} 
paulson@15656
  2732
  apply (case_tac x, simp, simp) 
paulson@15656
  2733
  apply (case_tac x, erule allE, erule allE, simp) 
paulson@15656
  2734
  apply (erule_tac x = listb in allE) 
paulson@15656
  2735
  apply (erule_tac x = lista in allE, simp)
paulson@15656
  2736
  apply (unfold trans_def)
paulson@15656
  2737
  by blast
paulson@15656
  2738
paulson@15656
  2739
lemma lexord_transI:  "trans r \<Longrightarrow> trans (lexord r)"
nipkow@24349
  2740
by (rule transI, drule lexord_trans, blast) 
paulson@15656
  2741
paulson@15656
  2742
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
  2743
  apply (rule_tac x = y in spec) 
paulson@15656
  2744
  apply (induct_tac x, rule allI) 
paulson@15656
  2745
  apply (case_tac x, simp, simp) 
paulson@15656
  2746
  apply (rule allI, case_tac x, simp, simp) 
paulson@15656
  2747
  by blast
paulson@15656
  2748