src/HOL/List.thy
author wenzelm
Tue May 07 19:54:29 2002 +0200 (2002-05-07)
changeset 13114 f2b00262bdfc
parent 12887 d25b43743e10
child 13122 c63612ffb186
permissions -rw-r--r--
converted;
clasohm@923
     1
(*  Title:      HOL/List.thy
clasohm@923
     2
    ID:         $Id$
clasohm@923
     3
    Author:     Tobias Nipkow
clasohm@923
     4
    Copyright   1994 TU Muenchen
clasohm@923
     5
*)
clasohm@923
     6
wenzelm@13114
     7
header {* The datatype of finite lists *}
wenzelm@13114
     8
theory List1 = PreList:
clasohm@923
     9
wenzelm@13114
    10
datatype 'a list = Nil ("[]") | Cons 'a "'a list" (infixr "#" 65)
clasohm@923
    11
clasohm@923
    12
consts
wenzelm@13114
    13
  "@"         :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"            (infixr 65)
wenzelm@13114
    14
  filter      :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
wenzelm@13114
    15
  concat      :: "'a list list \<Rightarrow> 'a list"
wenzelm@13114
    16
  foldl       :: "('b \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b"
wenzelm@13114
    17
  foldr       :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b"
wenzelm@13114
    18
  hd          :: "'a list \<Rightarrow> 'a"
wenzelm@13114
    19
  tl          :: "'a list \<Rightarrow> 'a list"
wenzelm@13114
    20
  last        :: "'a list \<Rightarrow> 'a"
wenzelm@13114
    21
  butlast     :: "'a list \<Rightarrow> 'a list"
wenzelm@13114
    22
  set         :: "'a list \<Rightarrow> 'a set"
wenzelm@13114
    23
  list_all    :: "('a \<Rightarrow> bool) \<Rightarrow> ('a list \<Rightarrow> bool)"
wenzelm@13114
    24
  list_all2   :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> bool"
wenzelm@13114
    25
  map         :: "('a\<Rightarrow>'b) \<Rightarrow> ('a list \<Rightarrow> 'b list)"
wenzelm@13114
    26
  mem         :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"                    (infixl 55)
wenzelm@13114
    27
  nth         :: "'a list \<Rightarrow> nat \<Rightarrow> 'a"			  (infixl "!" 100)
wenzelm@13114
    28
  list_update :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a list"
wenzelm@13114
    29
  take        :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
wenzelm@13114
    30
  drop        :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
wenzelm@13114
    31
  takeWhile   :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
wenzelm@13114
    32
  dropWhile   :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
wenzelm@13114
    33
  rev         :: "'a list \<Rightarrow> 'a list"
wenzelm@13114
    34
  zip	      :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a * 'b) list"
wenzelm@13114
    35
  upt         :: "nat \<Rightarrow> nat \<Rightarrow> nat list"                   ("(1[_../_'(])")
wenzelm@13114
    36
  remdups     :: "'a list \<Rightarrow> 'a list"
wenzelm@13114
    37
  null        :: "'a list \<Rightarrow> bool"
wenzelm@13114
    38
  "distinct"  :: "'a list \<Rightarrow> bool"
wenzelm@13114
    39
  replicate   :: "nat \<Rightarrow> 'a \<Rightarrow> 'a list"
clasohm@923
    40
nipkow@5077
    41
nonterminals
nipkow@5077
    42
  lupdbinds  lupdbind
nipkow@5077
    43
clasohm@923
    44
syntax
clasohm@923
    45
  (* list Enumeration *)
wenzelm@13114
    46
  "@list"     :: "args \<Rightarrow> 'a list"                          ("[(_)]")
clasohm@923
    47
nipkow@2512
    48
  (* Special syntax for filter *)
wenzelm@13114
    49
  "@filter"   :: "[pttrn, 'a list, bool] \<Rightarrow> 'a list"        ("(1[_:_./ _])")
clasohm@923
    50
nipkow@5077
    51
  (* list update *)
wenzelm@13114
    52
  "_lupdbind"      :: "['a, 'a] \<Rightarrow> lupdbind"            ("(2_ :=/ _)")
wenzelm@13114
    53
  ""               :: "lupdbind \<Rightarrow> lupdbinds"           ("_")
wenzelm@13114
    54
  "_lupdbinds"     :: "[lupdbind, lupdbinds] \<Rightarrow> lupdbinds" ("_,/ _")
wenzelm@13114
    55
  "_LUpdate"       :: "['a, lupdbinds] \<Rightarrow> 'a"           ("_/[(_)]" [900,0] 900)
nipkow@5077
    56
wenzelm@13114
    57
  upto        :: "nat \<Rightarrow> nat \<Rightarrow> nat list"                   ("(1[_../_])")
nipkow@5427
    58
clasohm@923
    59
translations
clasohm@923
    60
  "[x, xs]"     == "x#[xs]"
clasohm@923
    61
  "[x]"         == "x#[]"
wenzelm@3842
    62
  "[x:xs . P]"  == "filter (%x. P) xs"
clasohm@923
    63
nipkow@5077
    64
  "_LUpdate xs (_lupdbinds b bs)"  == "_LUpdate (_LUpdate xs b) bs"
nipkow@5077
    65
  "xs[i:=x]"                       == "list_update xs i x"
nipkow@5077
    66
nipkow@5427
    67
  "[i..j]" == "[i..(Suc j)(]"
nipkow@5427
    68
nipkow@5427
    69
wenzelm@12114
    70
syntax (xsymbols)
wenzelm@13114
    71
  "@filter"   :: "[pttrn, 'a list, bool] \<Rightarrow> 'a list"        ("(1[_\<in>_ ./ _])")
wenzelm@2262
    72
wenzelm@2262
    73
paulson@3342
    74
consts
wenzelm@13114
    75
  lists        :: "'a set \<Rightarrow> 'a list set"
paulson@3342
    76
wenzelm@13114
    77
inductive "lists A"
wenzelm@13114
    78
intros
wenzelm@13114
    79
Nil:  "[]: lists A"
wenzelm@13114
    80
Cons: "\<lbrakk> a: A;  l: lists A \<rbrakk> \<Longrightarrow> a#l : lists A"
paulson@3342
    81
paulson@3342
    82
paulson@3437
    83
(*Function "size" is overloaded for all datatypes.  Users may refer to the
paulson@3437
    84
  list version as "length".*)
wenzelm@13114
    85
syntax   length :: "'a list \<Rightarrow> nat"
wenzelm@13114
    86
translations  "length"  =>  "size:: _ list \<Rightarrow> nat"
wenzelm@13114
    87
wenzelm@13114
    88
(* translating size::list -> length *)
wenzelm@13114
    89
typed_print_translation
wenzelm@13114
    90
{*
wenzelm@13114
    91
let
wenzelm@13114
    92
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
wenzelm@13114
    93
      Syntax.const "length" $ t
wenzelm@13114
    94
  | size_tr' _ _ _ = raise Match;
wenzelm@13114
    95
in [("size", size_tr')] end
wenzelm@13114
    96
*}
paulson@3437
    97
berghofe@5183
    98
primrec
berghofe@1898
    99
  "hd(x#xs) = x"
berghofe@5183
   100
primrec
paulson@8972
   101
  "tl([])   = []"
berghofe@1898
   102
  "tl(x#xs) = xs"
berghofe@5183
   103
primrec
paulson@8972
   104
  "null([])   = True"
paulson@8972
   105
  "null(x#xs) = False"
paulson@8972
   106
primrec
nipkow@3896
   107
  "last(x#xs) = (if xs=[] then x else last xs)"
berghofe@5183
   108
primrec
paulson@8972
   109
  "butlast []    = []"
nipkow@3896
   110
  "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
berghofe@5183
   111
primrec
paulson@8972
   112
  "x mem []     = False"
oheimb@5518
   113
  "x mem (y#ys) = (if y=x then True else x mem ys)"
oheimb@5518
   114
primrec
nipkow@3465
   115
  "set [] = {}"
nipkow@3465
   116
  "set (x#xs) = insert x (set xs)"
berghofe@5183
   117
primrec
wenzelm@13114
   118
  list_all_Nil:  "list_all P [] = True"
wenzelm@13114
   119
  list_all_Cons: "list_all P (x#xs) = (P(x) & list_all P xs)"
oheimb@5518
   120
primrec
paulson@8972
   121
  "map f []     = []"
berghofe@1898
   122
  "map f (x#xs) = f(x)#map f xs"
berghofe@5183
   123
primrec
wenzelm@13114
   124
  append_Nil:  "[]    @ys = ys"
wenzelm@13114
   125
  append_Cons: "(x#xs)@ys = x#(xs@ys)"
berghofe@5183
   126
primrec
paulson@8972
   127
  "rev([])   = []"
berghofe@1898
   128
  "rev(x#xs) = rev(xs) @ [x]"
berghofe@5183
   129
primrec
paulson@8972
   130
  "filter P []     = []"
berghofe@1898
   131
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
berghofe@5183
   132
primrec
wenzelm@13114
   133
  foldl_Nil:  "foldl f a [] = a"
wenzelm@13114
   134
  foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs"
berghofe@5183
   135
primrec
paulson@8972
   136
  "foldr f [] a     = a"
paulson@8000
   137
  "foldr f (x#xs) a = f x (foldr f xs a)"
paulson@8000
   138
primrec
paulson@8972
   139
  "concat([])   = []"
nipkow@2608
   140
  "concat(x#xs) = x @ concat(xs)"
berghofe@5183
   141
primrec
wenzelm@13114
   142
  drop_Nil:  "drop n [] = []"
wenzelm@13114
   143
  drop_Cons: "drop n (x#xs) = (case n of 0 \<Rightarrow> x#xs | Suc(m) \<Rightarrow> drop m xs)"
pusch@6408
   144
  (* Warning: simpset does not contain this definition but separate theorems 
pusch@6408
   145
     for n=0 / n=Suc k*)
berghofe@5183
   146
primrec
wenzelm@13114
   147
  take_Nil:  "take n [] = []"
wenzelm@13114
   148
  take_Cons: "take n (x#xs) = (case n of 0 \<Rightarrow> [] | Suc(m) \<Rightarrow> x # take m xs)"
pusch@6408
   149
  (* Warning: simpset does not contain this definition but separate theorems 
pusch@6408
   150
     for n=0 / n=Suc k*)
pusch@6408
   151
primrec 
wenzelm@13114
   152
  nth_Cons:  "(x#xs)!n = (case n of 0 \<Rightarrow> x | (Suc k) \<Rightarrow> xs!k)"
pusch@6408
   153
  (* Warning: simpset does not contain this definition but separate theorems 
pusch@6408
   154
     for n=0 / n=Suc k*)
berghofe@5183
   155
primrec
nipkow@5077
   156
 "    [][i:=v] = []"
wenzelm@13114
   157
 "(x#xs)[i:=v] = (case i of 0     \<Rightarrow> v # xs 
wenzelm@13114
   158
			  | Suc j \<Rightarrow> x # xs[j:=v])"
berghofe@5183
   159
primrec
paulson@8972
   160
  "takeWhile P []     = []"
nipkow@2608
   161
  "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
berghofe@5183
   162
primrec
paulson@8972
   163
  "dropWhile P []     = []"
nipkow@3584
   164
  "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
berghofe@5183
   165
primrec
oheimb@4132
   166
  "zip xs []     = []"
wenzelm@13114
   167
zip_Cons:
wenzelm@13114
   168
  "zip xs (y#ys) = (case xs of [] \<Rightarrow> [] | z#zs \<Rightarrow> (z,y)#zip zs ys)"
pusch@6408
   169
  (* Warning: simpset does not contain this definition but separate theorems 
pusch@6408
   170
     for xs=[] / xs=z#zs *)
nipkow@5427
   171
primrec
wenzelm@13114
   172
  upt_0:   "[i..0(] = []"
wenzelm@13114
   173
  upt_Suc: "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])"
berghofe@5183
   174
primrec
nipkow@12887
   175
  "distinct []     = True"
nipkow@12887
   176
  "distinct (x#xs) = (x ~: set xs & distinct xs)"
berghofe@5183
   177
primrec
nipkow@4605
   178
  "remdups [] = []"
nipkow@4605
   179
  "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
berghofe@5183
   180
primrec
wenzelm@13114
   181
  replicate_0:   "replicate  0      x = []"
wenzelm@13114
   182
  replicate_Suc: "replicate (Suc n) x = x # replicate n x"
nipkow@8115
   183
defs
wenzelm@13114
   184
 list_all2_def:
nipkow@8115
   185
 "list_all2 P xs ys == length xs = length ys & (!(x,y):set(zip xs ys). P x y)"
nipkow@8115
   186
paulson@3196
   187
pusch@6408
   188
(** Lexicographic orderings on lists **)
nipkow@5281
   189
nipkow@5281
   190
consts
wenzelm@13114
   191
 lexn :: "('a * 'a)set \<Rightarrow> nat \<Rightarrow> ('a list * 'a list)set"
nipkow@5281
   192
primrec
nipkow@5281
   193
"lexn r 0       = {}"
nipkow@10832
   194
"lexn r (Suc n) = (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int
nipkow@5281
   195
                  {(xs,ys). length xs = Suc n & length ys = Suc n}"
nipkow@5281
   196
nipkow@5281
   197
constdefs
wenzelm@13114
   198
  lex :: "('a * 'a)set \<Rightarrow> ('a list * 'a list)set"
paulson@9336
   199
    "lex r == UN n. lexn r n"
nipkow@5281
   200
wenzelm@13114
   201
  lexico :: "('a * 'a)set \<Rightarrow> ('a list * 'a list)set"
paulson@9336
   202
    "lexico r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"
paulson@9336
   203
wenzelm@13114
   204
  sublist :: "['a list, nat set] \<Rightarrow> 'a list"
paulson@9336
   205
    "sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..size xs(]))"
nipkow@5281
   206
wenzelm@13114
   207
wenzelm@13114
   208
lemma not_Cons_self[simp]: "\<And>x. xs ~= x#xs"
wenzelm@13114
   209
by(induct_tac "xs", auto)
wenzelm@13114
   210
wenzelm@13114
   211
lemmas not_Cons_self2[simp] = not_Cons_self[THEN not_sym]
wenzelm@13114
   212
wenzelm@13114
   213
lemma neq_Nil_conv: "(xs ~= []) = (? y ys. xs = y#ys)";
wenzelm@13114
   214
by(induct_tac "xs", auto)
wenzelm@13114
   215
wenzelm@13114
   216
(* Induction over the length of a list: *)
wenzelm@13114
   217
(* "(!!xs. (!ys. length ys < length xs --> P ys) ==> P xs) ==> P(xs)" *)
wenzelm@13114
   218
lemmas length_induct = measure_induct[of length]
wenzelm@13114
   219
wenzelm@13114
   220
wenzelm@13114
   221
(** "lists": the list-forming operator over sets **)
wenzelm@13114
   222
wenzelm@13114
   223
lemma lists_mono: "A<=B ==> lists A <= lists B"
wenzelm@13114
   224
apply(unfold lists.defs)
wenzelm@13114
   225
apply(blast intro!:lfp_mono)
wenzelm@13114
   226
done
wenzelm@13114
   227
wenzelm@13114
   228
inductive_cases listsE[elim!]: "x#l : lists A"
wenzelm@13114
   229
declare lists.intros[intro!]
wenzelm@13114
   230
wenzelm@13114
   231
lemma lists_IntI[rule_format]:
wenzelm@13114
   232
 "l: lists A ==> l: lists B --> l: lists (A Int B)";
wenzelm@13114
   233
apply(erule lists.induct)
wenzelm@13114
   234
apply blast+
wenzelm@13114
   235
done
wenzelm@13114
   236
wenzelm@13114
   237
lemma lists_Int_eq[simp]: "lists (A Int B) = lists A Int lists B"
wenzelm@13114
   238
apply(rule mono_Int[THEN equalityI])
wenzelm@13114
   239
apply(simp add:mono_def lists_mono)
wenzelm@13114
   240
apply(blast intro!: lists_IntI)
wenzelm@13114
   241
done
wenzelm@13114
   242
wenzelm@13114
   243
lemma append_in_lists_conv[iff]:
wenzelm@13114
   244
 "(xs@ys : lists A) = (xs : lists A & ys : lists A)"
wenzelm@13114
   245
by(induct_tac "xs", auto)
wenzelm@13114
   246
wenzelm@13114
   247
(** length **)
wenzelm@13114
   248
(* needs to come before "@" because of thm append_eq_append_conv *)
wenzelm@13114
   249
wenzelm@13114
   250
section "length"
wenzelm@13114
   251
wenzelm@13114
   252
lemma length_append[simp]: "length(xs@ys) = length(xs)+length(ys)"
wenzelm@13114
   253
by(induct_tac "xs", auto)
wenzelm@13114
   254
wenzelm@13114
   255
lemma length_map[simp]: "length (map f xs) = length xs"
wenzelm@13114
   256
by(induct_tac "xs", auto)
wenzelm@13114
   257
wenzelm@13114
   258
lemma length_rev[simp]: "length(rev xs) = length(xs)"
wenzelm@13114
   259
by(induct_tac "xs", auto)
wenzelm@13114
   260
wenzelm@13114
   261
lemma length_tl[simp]: "length(tl xs) = (length xs) - 1"
wenzelm@13114
   262
by(case_tac "xs", auto)
wenzelm@13114
   263
wenzelm@13114
   264
lemma length_0_conv[iff]: "(length xs = 0) = (xs = [])"
wenzelm@13114
   265
by(induct_tac "xs", auto)
wenzelm@13114
   266
wenzelm@13114
   267
lemma length_greater_0_conv[iff]: "(0 < length xs) = (xs ~= [])"
wenzelm@13114
   268
by(induct_tac xs, auto)
wenzelm@13114
   269
wenzelm@13114
   270
lemma length_Suc_conv:
wenzelm@13114
   271
 "(length xs = Suc n) = (? y ys. xs = y#ys & length ys = n)"
wenzelm@13114
   272
by(induct_tac "xs", auto)
wenzelm@13114
   273
wenzelm@13114
   274
(** @ - append **)
wenzelm@13114
   275
wenzelm@13114
   276
section "@ - append"
wenzelm@13114
   277
wenzelm@13114
   278
lemma append_assoc[simp]: "(xs@ys)@zs = xs@(ys@zs)"
wenzelm@13114
   279
by(induct_tac "xs", auto)
nipkow@3507
   280
wenzelm@13114
   281
lemma append_Nil2[simp]: "xs @ [] = xs"
wenzelm@13114
   282
by(induct_tac "xs", auto)
wenzelm@13114
   283
wenzelm@13114
   284
lemma append_is_Nil_conv[iff]: "(xs@ys = []) = (xs=[] & ys=[])"
wenzelm@13114
   285
by(induct_tac "xs", auto)
wenzelm@13114
   286
wenzelm@13114
   287
lemma Nil_is_append_conv[iff]: "([] = xs@ys) = (xs=[] & ys=[])"
wenzelm@13114
   288
by(induct_tac "xs", auto)
wenzelm@13114
   289
wenzelm@13114
   290
lemma append_self_conv[iff]: "(xs @ ys = xs) = (ys=[])"
wenzelm@13114
   291
by(induct_tac "xs", auto)
wenzelm@13114
   292
wenzelm@13114
   293
lemma self_append_conv[iff]: "(xs = xs @ ys) = (ys=[])"
wenzelm@13114
   294
by(induct_tac "xs", auto)
wenzelm@13114
   295
wenzelm@13114
   296
lemma append_eq_append_conv[rule_format,simp]:
wenzelm@13114
   297
 "!ys. length xs = length ys | length us = length vs
wenzelm@13114
   298
       --> (xs@us = ys@vs) = (xs=ys & us=vs)"
wenzelm@13114
   299
apply(induct_tac "xs")
wenzelm@13114
   300
 apply(rule allI)
wenzelm@13114
   301
 apply(case_tac "ys")
wenzelm@13114
   302
  apply simp
wenzelm@13114
   303
 apply force
wenzelm@13114
   304
apply(rule allI)
wenzelm@13114
   305
apply(case_tac "ys")
wenzelm@13114
   306
 apply force
wenzelm@13114
   307
apply simp
wenzelm@13114
   308
done
wenzelm@13114
   309
wenzelm@13114
   310
lemma same_append_eq[iff]: "(xs @ ys = xs @ zs) = (ys=zs)"
wenzelm@13114
   311
by simp
wenzelm@13114
   312
wenzelm@13114
   313
lemma append1_eq_conv[iff]: "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)" 
wenzelm@13114
   314
by simp
wenzelm@13114
   315
wenzelm@13114
   316
lemma append_same_eq[iff]: "(ys @ xs = zs @ xs) = (ys=zs)"
wenzelm@13114
   317
by simp
nipkow@3507
   318
wenzelm@13114
   319
lemma append_self_conv2[iff]: "(xs @ ys = ys) = (xs=[])"
wenzelm@13114
   320
by(insert append_same_eq[of _ _ "[]"], auto)
wenzelm@13114
   321
wenzelm@13114
   322
lemma self_append_conv2[iff]: "(ys = xs @ ys) = (xs=[])"
wenzelm@13114
   323
by(auto simp add: append_same_eq[of "[]", simplified])
wenzelm@13114
   324
wenzelm@13114
   325
lemma hd_Cons_tl[rule_format,simp]: "xs ~= [] --> hd xs # tl xs = xs"
wenzelm@13114
   326
by(induct_tac "xs", auto)
wenzelm@13114
   327
wenzelm@13114
   328
lemma hd_append: "hd(xs@ys) = (if xs=[] then hd ys else hd xs)"
wenzelm@13114
   329
by(induct_tac "xs", auto)
wenzelm@13114
   330
wenzelm@13114
   331
lemma hd_append2[simp]: "xs ~= [] ==> hd(xs @ ys) = hd xs"
wenzelm@13114
   332
by(simp add: hd_append split: list.split)
wenzelm@13114
   333
wenzelm@13114
   334
lemma tl_append: "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)"
wenzelm@13114
   335
by(simp split: list.split)
wenzelm@13114
   336
wenzelm@13114
   337
lemma tl_append2[simp]: "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys"
wenzelm@13114
   338
by(simp add: tl_append split: list.split)
wenzelm@13114
   339
wenzelm@13114
   340
(* trivial rules for solving @-equations automatically *)
wenzelm@13114
   341
wenzelm@13114
   342
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
wenzelm@13114
   343
by simp
wenzelm@13114
   344
wenzelm@13114
   345
lemma Cons_eq_appendI: "[| x#xs1 = ys; xs = xs1 @ zs |] ==> x#xs = ys@zs"
wenzelm@13114
   346
by(drule sym, simp)
wenzelm@13114
   347
wenzelm@13114
   348
lemma append_eq_appendI: "[| xs@xs1 = zs; ys = xs1 @ us |] ==> xs@ys = zs@us"
wenzelm@13114
   349
by(drule sym, simp)
wenzelm@13114
   350
wenzelm@13114
   351
wenzelm@13114
   352
(***
wenzelm@13114
   353
Simplification procedure for all list equalities.
wenzelm@13114
   354
Currently only tries to rearrange @ to see if
wenzelm@13114
   355
- both lists end in a singleton list,
wenzelm@13114
   356
- or both lists end in the same list.
wenzelm@13114
   357
***)
wenzelm@13114
   358
ML_setup{*
nipkow@3507
   359
local
nipkow@3507
   360
wenzelm@13114
   361
val list_eq_pattern =
wenzelm@13114
   362
  Thm.read_cterm (Theory.sign_of (the_context ())) ("(xs::'a list) = ys",HOLogic.boolT)
wenzelm@13114
   363
wenzelm@13114
   364
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
wenzelm@13114
   365
      (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
wenzelm@13114
   366
  | last (Const("List.op @",_) $ _ $ ys) = last ys
wenzelm@13114
   367
  | last t = t
wenzelm@13114
   368
wenzelm@13114
   369
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
wenzelm@13114
   370
  | list1 _ = false
wenzelm@13114
   371
wenzelm@13114
   372
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
wenzelm@13114
   373
      (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
wenzelm@13114
   374
  | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
wenzelm@13114
   375
  | butlast xs = Const("List.list.Nil",fastype_of xs)
wenzelm@13114
   376
wenzelm@13114
   377
val rearr_tac =
wenzelm@13114
   378
  simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons])
wenzelm@13114
   379
wenzelm@13114
   380
fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
wenzelm@13114
   381
  let
wenzelm@13114
   382
    val lastl = last lhs and lastr = last rhs
wenzelm@13114
   383
    fun rearr conv =
wenzelm@13114
   384
      let val lhs1 = butlast lhs and rhs1 = butlast rhs
wenzelm@13114
   385
          val Type(_,listT::_) = eqT
wenzelm@13114
   386
          val appT = [listT,listT] ---> listT
wenzelm@13114
   387
          val app = Const("List.op @",appT)
wenzelm@13114
   388
          val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
wenzelm@13114
   389
          val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2)))
wenzelm@13114
   390
          val thm = prove_goalw_cterm [] ct (K [rearr_tac 1])
wenzelm@13114
   391
            handle ERROR =>
wenzelm@13114
   392
            error("The error(s) above occurred while trying to prove " ^
wenzelm@13114
   393
                  string_of_cterm ct)
wenzelm@13114
   394
      in Some((conv RS (thm RS trans)) RS eq_reflection) end
wenzelm@13114
   395
wenzelm@13114
   396
  in if list1 lastl andalso list1 lastr
wenzelm@13114
   397
     then rearr append1_eq_conv
wenzelm@13114
   398
     else
wenzelm@13114
   399
     if lastl aconv lastr
wenzelm@13114
   400
     then rearr append_same_eq
wenzelm@13114
   401
     else None
wenzelm@13114
   402
  end
wenzelm@13114
   403
in
wenzelm@13114
   404
val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq
wenzelm@13114
   405
end;
wenzelm@13114
   406
wenzelm@13114
   407
Addsimprocs [list_eq_simproc];
wenzelm@13114
   408
*}
wenzelm@13114
   409
wenzelm@13114
   410
wenzelm@13114
   411
(** map **)
wenzelm@13114
   412
wenzelm@13114
   413
section "map"
wenzelm@13114
   414
wenzelm@13114
   415
lemma map_ext: "(\<And>x. x : set xs \<longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g xs"
wenzelm@13114
   416
by (induct xs, simp_all)
wenzelm@13114
   417
wenzelm@13114
   418
lemma map_ident[simp]: "map (%x. x) = (%xs. xs)"
wenzelm@13114
   419
by(rule ext, induct_tac "xs", auto)
wenzelm@13114
   420
wenzelm@13114
   421
lemma map_append[simp]: "map f (xs@ys) = map f xs @ map f ys"
wenzelm@13114
   422
by(induct_tac "xs", auto)
wenzelm@13114
   423
wenzelm@13114
   424
lemma map_compose(*[simp]*): "map (f o g) xs = map f (map g xs)"
wenzelm@13114
   425
by(unfold o_def, induct_tac "xs", auto)
wenzelm@13114
   426
wenzelm@13114
   427
lemma rev_map: "rev(map f xs) = map f (rev xs)"
wenzelm@13114
   428
by(induct_tac xs, auto)
wenzelm@13114
   429
wenzelm@13114
   430
(* a congruence rule for map: *)
wenzelm@13114
   431
lemma map_cong:
wenzelm@13114
   432
 "xs=ys ==> (!!x. x : set ys \<Longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g ys"
wenzelm@13114
   433
by (clarify, induct ys, auto)
wenzelm@13114
   434
wenzelm@13114
   435
lemma map_is_Nil_conv[iff]: "(map f xs = []) = (xs = [])"
wenzelm@13114
   436
by(case_tac xs, auto)
wenzelm@13114
   437
wenzelm@13114
   438
lemma Nil_is_map_conv[iff]: "([] = map f xs) = (xs = [])"
wenzelm@13114
   439
by(case_tac xs, auto)
wenzelm@13114
   440
wenzelm@13114
   441
lemma map_eq_Cons:
wenzelm@13114
   442
 "(map f xs = y#ys) = (? x xs'. xs = x#xs' & f x = y & map f xs' = ys)"
wenzelm@13114
   443
by(case_tac xs, auto)
wenzelm@13114
   444
wenzelm@13114
   445
lemma map_injective:
wenzelm@13114
   446
 "\<And>xs. map f xs = map f ys \<Longrightarrow> (!x y. f x = f y --> x=y) \<Longrightarrow> xs=ys"
wenzelm@13114
   447
by(induct "ys", simp, fastsimp simp add:map_eq_Cons)
wenzelm@13114
   448
wenzelm@13114
   449
lemma inj_mapI: "inj f ==> inj (map f)"
wenzelm@13114
   450
by(blast dest:map_injective injD intro:injI)
wenzelm@13114
   451
wenzelm@13114
   452
lemma inj_mapD: "inj (map f) ==> inj f"
wenzelm@13114
   453
apply(unfold inj_on_def)
wenzelm@13114
   454
apply clarify
wenzelm@13114
   455
apply(erule_tac x = "[x]" in ballE)
wenzelm@13114
   456
 apply(erule_tac x = "[y]" in ballE)
wenzelm@13114
   457
  apply simp
wenzelm@13114
   458
 apply blast
wenzelm@13114
   459
apply blast
wenzelm@13114
   460
done
wenzelm@13114
   461
wenzelm@13114
   462
lemma inj_map: "inj (map f) = inj f"
wenzelm@13114
   463
by(blast dest:inj_mapD intro:inj_mapI)
wenzelm@13114
   464
wenzelm@13114
   465
(** rev **)
wenzelm@13114
   466
wenzelm@13114
   467
section "rev"
wenzelm@13114
   468
wenzelm@13114
   469
lemma rev_append[simp]: "rev(xs@ys) = rev(ys) @ rev(xs)"
wenzelm@13114
   470
by(induct_tac xs, auto)
wenzelm@13114
   471
wenzelm@13114
   472
lemma rev_rev_ident[simp]: "rev(rev xs) = xs"
wenzelm@13114
   473
by(induct_tac xs, auto)
wenzelm@13114
   474
wenzelm@13114
   475
lemma rev_is_Nil_conv[iff]: "(rev xs = []) = (xs = [])"
wenzelm@13114
   476
by(induct_tac xs, auto)
wenzelm@13114
   477
wenzelm@13114
   478
lemma Nil_is_rev_conv[iff]: "([] = rev xs) = (xs = [])"
wenzelm@13114
   479
by(induct_tac xs, auto)
wenzelm@13114
   480
wenzelm@13114
   481
lemma rev_is_rev_conv[iff]: "!!ys. (rev xs = rev ys) = (xs = ys)"
wenzelm@13114
   482
apply(induct "xs" )
wenzelm@13114
   483
 apply force
wenzelm@13114
   484
apply(case_tac ys)
wenzelm@13114
   485
 apply simp
wenzelm@13114
   486
apply force
wenzelm@13114
   487
done
wenzelm@13114
   488
wenzelm@13114
   489
lemma rev_induct: "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs"
wenzelm@13114
   490
apply(subst rev_rev_ident[symmetric])
wenzelm@13114
   491
apply(rule_tac list = "rev xs" in list.induct, simp_all)
wenzelm@13114
   492
done
wenzelm@13114
   493
wenzelm@13114
   494
(* Instead of (rev_induct_tac xs) use (induct_tac xs rule: rev_induct) *)
wenzelm@13114
   495
wenzelm@13114
   496
lemma rev_exhaust: "(xs = [] \<Longrightarrow> P) \<Longrightarrow>  (!!ys y. xs = ys@[y] \<Longrightarrow> P) \<Longrightarrow> P"
wenzelm@13114
   497
by(induct xs rule: rev_induct, auto)
wenzelm@13114
   498
wenzelm@13114
   499
wenzelm@13114
   500
(** set **)
wenzelm@13114
   501
wenzelm@13114
   502
section "set"
wenzelm@13114
   503
wenzelm@13114
   504
lemma finite_set[iff]: "finite (set xs)"
wenzelm@13114
   505
by(induct_tac xs, auto)
wenzelm@13114
   506
wenzelm@13114
   507
lemma set_append[simp]: "set (xs@ys) = (set xs Un set ys)"
wenzelm@13114
   508
by(induct_tac xs, auto)
wenzelm@13114
   509
wenzelm@13114
   510
lemma set_subset_Cons: "set xs \<subseteq> set (x#xs)"
wenzelm@13114
   511
by auto
wenzelm@13114
   512
wenzelm@13114
   513
lemma set_empty[iff]: "(set xs = {}) = (xs = [])"
wenzelm@13114
   514
by(induct_tac xs, auto)
wenzelm@13114
   515
wenzelm@13114
   516
lemma set_rev[simp]: "set(rev xs) = set(xs)"
wenzelm@13114
   517
by(induct_tac xs, auto)
wenzelm@13114
   518
wenzelm@13114
   519
lemma set_map[simp]: "set(map f xs) = f`(set xs)"
wenzelm@13114
   520
by(induct_tac xs, auto)
wenzelm@13114
   521
wenzelm@13114
   522
lemma set_filter[simp]: "set(filter P xs) = {x. x : set xs & P x}"
wenzelm@13114
   523
by(induct_tac xs, auto)
wenzelm@13114
   524
wenzelm@13114
   525
lemma set_upt[simp]: "set[i..j(] = {k. i <= k & k < j}"
wenzelm@13114
   526
apply(induct_tac j)
wenzelm@13114
   527
 apply simp_all
wenzelm@13114
   528
apply(erule ssubst)
wenzelm@13114
   529
apply auto
wenzelm@13114
   530
apply arith
wenzelm@13114
   531
done
wenzelm@13114
   532
wenzelm@13114
   533
lemma in_set_conv_decomp: "(x : set xs) = (? ys zs. xs = ys@x#zs)"
wenzelm@13114
   534
apply(induct_tac "xs")
wenzelm@13114
   535
 apply simp
wenzelm@13114
   536
apply simp
wenzelm@13114
   537
apply(rule iffI)
wenzelm@13114
   538
 apply(blast intro: eq_Nil_appendI Cons_eq_appendI)
wenzelm@13114
   539
apply(erule exE)+
wenzelm@13114
   540
apply(case_tac "ys")
wenzelm@13114
   541
apply auto
wenzelm@13114
   542
done
wenzelm@13114
   543
wenzelm@13114
   544
wenzelm@13114
   545
(* eliminate `lists' in favour of `set' *)
wenzelm@13114
   546
wenzelm@13114
   547
lemma in_lists_conv_set: "(xs : lists A) = (!x : set xs. x : A)"
wenzelm@13114
   548
by(induct_tac xs, auto)
wenzelm@13114
   549
wenzelm@13114
   550
lemmas in_listsD[dest!] = in_lists_conv_set[THEN iffD1]
wenzelm@13114
   551
lemmas in_listsI[intro!] = in_lists_conv_set[THEN iffD2]
wenzelm@13114
   552
wenzelm@13114
   553
wenzelm@13114
   554
(** mem **)
wenzelm@13114
   555
 
wenzelm@13114
   556
section "mem"
wenzelm@13114
   557
wenzelm@13114
   558
lemma set_mem_eq: "(x mem xs) = (x : set xs)"
wenzelm@13114
   559
by(induct_tac xs, auto)
wenzelm@13114
   560
wenzelm@13114
   561
wenzelm@13114
   562
(** list_all **)
wenzelm@13114
   563
wenzelm@13114
   564
section "list_all"
wenzelm@13114
   565
wenzelm@13114
   566
lemma list_all_conv: "list_all P xs = (!x:set xs. P x)"
wenzelm@13114
   567
by(induct_tac xs, auto)
wenzelm@13114
   568
wenzelm@13114
   569
lemma list_all_append[simp]:
wenzelm@13114
   570
 "list_all P (xs@ys) = (list_all P xs & list_all P ys)"
wenzelm@13114
   571
by(induct_tac xs, auto)
wenzelm@13114
   572
wenzelm@13114
   573
wenzelm@13114
   574
(** filter **)
wenzelm@13114
   575
wenzelm@13114
   576
section "filter"
wenzelm@13114
   577
wenzelm@13114
   578
lemma filter_append[simp]: "filter P (xs@ys) = filter P xs @ filter P ys"
wenzelm@13114
   579
by(induct_tac xs, auto)
wenzelm@13114
   580
wenzelm@13114
   581
lemma filter_filter[simp]: "filter P (filter Q xs) = filter (%x. Q x & P x) xs"
wenzelm@13114
   582
by(induct_tac xs, auto)
wenzelm@13114
   583
wenzelm@13114
   584
lemma filter_True[simp]: "!x : set xs. P x \<Longrightarrow> filter P xs = xs"
wenzelm@13114
   585
by(induct xs, auto)
wenzelm@13114
   586
wenzelm@13114
   587
lemma filter_False[simp]: "!x : set xs. ~P x \<Longrightarrow> filter P xs = []"
wenzelm@13114
   588
by(induct xs, auto)
wenzelm@13114
   589
wenzelm@13114
   590
lemma length_filter[simp]: "length (filter P xs) <= length xs"
wenzelm@13114
   591
by(induct xs, auto simp add: le_SucI)
wenzelm@13114
   592
wenzelm@13114
   593
lemma filter_is_subset[simp]: "set (filter P xs) <= set xs"
wenzelm@13114
   594
by auto
wenzelm@13114
   595
wenzelm@13114
   596
wenzelm@13114
   597
section "concat"
wenzelm@13114
   598
wenzelm@13114
   599
lemma concat_append[simp]: "concat(xs@ys) = concat(xs)@concat(ys)"
wenzelm@13114
   600
by(induct xs, auto)
wenzelm@13114
   601
wenzelm@13114
   602
lemma concat_eq_Nil_conv[iff]: "(concat xss = []) = (!xs:set xss. xs=[])"
wenzelm@13114
   603
by(induct xss, auto)
wenzelm@13114
   604
wenzelm@13114
   605
lemma Nil_eq_concat_conv[iff]: "([] = concat xss) = (!xs:set xss. xs=[])"
wenzelm@13114
   606
by(induct xss, auto)
wenzelm@13114
   607
wenzelm@13114
   608
lemma set_concat[simp]: "set(concat xs) = Union(set ` set xs)"
wenzelm@13114
   609
by(induct xs, auto)
wenzelm@13114
   610
wenzelm@13114
   611
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" 
wenzelm@13114
   612
by(induct xs, auto)
wenzelm@13114
   613
wenzelm@13114
   614
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" 
wenzelm@13114
   615
by(induct xs, auto)
wenzelm@13114
   616
wenzelm@13114
   617
lemma rev_concat: "rev(concat xs) = concat (map rev (rev xs))"
wenzelm@13114
   618
by(induct xs, auto)
wenzelm@13114
   619
wenzelm@13114
   620
(** nth **)
wenzelm@13114
   621
wenzelm@13114
   622
section "nth"
wenzelm@13114
   623
wenzelm@13114
   624
lemma nth_Cons_0[simp]: "(x#xs)!0 = x"
wenzelm@13114
   625
by auto
wenzelm@13114
   626
wenzelm@13114
   627
lemma nth_Cons_Suc[simp]: "(x#xs)!(Suc n) = xs!n"
wenzelm@13114
   628
by auto
wenzelm@13114
   629
wenzelm@13114
   630
declare nth.simps[simp del]
wenzelm@13114
   631
wenzelm@13114
   632
lemma nth_append:
wenzelm@13114
   633
 "!!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
wenzelm@13114
   634
apply(induct "xs")
wenzelm@13114
   635
 apply simp
wenzelm@13114
   636
apply(case_tac "n" )
wenzelm@13114
   637
 apply auto
wenzelm@13114
   638
done
wenzelm@13114
   639
wenzelm@13114
   640
lemma nth_map[simp]: "!!n. n < length xs \<Longrightarrow> (map f xs)!n = f(xs!n)"
wenzelm@13114
   641
apply(induct "xs" )
wenzelm@13114
   642
 apply simp
wenzelm@13114
   643
apply(case_tac "n")
wenzelm@13114
   644
 apply auto
wenzelm@13114
   645
done
wenzelm@13114
   646
wenzelm@13114
   647
lemma set_conv_nth: "set xs = {xs!i |i. i < length xs}"
wenzelm@13114
   648
apply(induct_tac "xs")
wenzelm@13114
   649
 apply simp
wenzelm@13114
   650
apply simp
wenzelm@13114
   651
apply safe
wenzelm@13114
   652
  apply(rule_tac x = 0 in exI)
wenzelm@13114
   653
  apply simp
wenzelm@13114
   654
 apply(rule_tac x = "Suc i" in exI)
wenzelm@13114
   655
 apply simp
wenzelm@13114
   656
apply(case_tac "i")
wenzelm@13114
   657
 apply simp
wenzelm@13114
   658
apply(rename_tac "j")
wenzelm@13114
   659
apply(rule_tac x = "j" in exI)
wenzelm@13114
   660
apply simp
wenzelm@13114
   661
done
wenzelm@13114
   662
wenzelm@13114
   663
lemma list_ball_nth: "\<lbrakk> n < length xs; !x : set xs. P x \<rbrakk> \<Longrightarrow> P(xs!n)"
wenzelm@13114
   664
by(simp add:set_conv_nth, blast)
wenzelm@13114
   665
wenzelm@13114
   666
lemma nth_mem[simp]: "n < length xs ==> xs!n : set xs"
wenzelm@13114
   667
by(simp add:set_conv_nth, blast)
wenzelm@13114
   668
wenzelm@13114
   669
lemma all_nth_imp_all_set:
wenzelm@13114
   670
 "\<lbrakk> !i < length xs. P(xs!i); x : set xs \<rbrakk> \<Longrightarrow> P x"
wenzelm@13114
   671
by(simp add:set_conv_nth, blast)
wenzelm@13114
   672
wenzelm@13114
   673
lemma all_set_conv_all_nth:
wenzelm@13114
   674
 "(!x : set xs. P x) = (!i. i<length xs --> P (xs ! i))"
wenzelm@13114
   675
by(simp add:set_conv_nth, blast)
wenzelm@13114
   676
wenzelm@13114
   677
wenzelm@13114
   678
(** list update **)
wenzelm@13114
   679
wenzelm@13114
   680
section "list update"
wenzelm@13114
   681
wenzelm@13114
   682
lemma length_list_update[simp]: "!!i. length(xs[i:=x]) = length xs"
wenzelm@13114
   683
by(induct xs, simp, simp split:nat.split)
wenzelm@13114
   684
wenzelm@13114
   685
lemma nth_list_update:
wenzelm@13114
   686
 "!!i j. i < length xs  \<Longrightarrow> (xs[i:=x])!j = (if i=j then x else xs!j)"
wenzelm@13114
   687
by(induct xs, simp, auto simp add:nth_Cons split:nat.split)
wenzelm@13114
   688
wenzelm@13114
   689
lemma nth_list_update_eq[simp]: "i < length xs  ==> (xs[i:=x])!i = x"
wenzelm@13114
   690
by(simp add:nth_list_update)
wenzelm@13114
   691
wenzelm@13114
   692
lemma nth_list_update_neq[simp]: "!!i j. i ~= j \<Longrightarrow> xs[i:=x]!j = xs!j"
wenzelm@13114
   693
by(induct xs, simp, auto simp add:nth_Cons split:nat.split)
wenzelm@13114
   694
wenzelm@13114
   695
lemma list_update_overwrite[simp]:
wenzelm@13114
   696
 "!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"
wenzelm@13114
   697
by(induct xs, simp, simp split:nat.split)
wenzelm@13114
   698
wenzelm@13114
   699
lemma list_update_same_conv:
wenzelm@13114
   700
 "!!i. i < length xs \<Longrightarrow> (xs[i := x] = xs) = (xs!i = x)"
wenzelm@13114
   701
by(induct xs, simp, simp split:nat.split, blast)
wenzelm@13114
   702
wenzelm@13114
   703
lemma update_zip:
wenzelm@13114
   704
"!!i xy xs. length xs = length ys \<Longrightarrow>
wenzelm@13114
   705
    (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
wenzelm@13114
   706
by(induct ys, auto, case_tac xs, auto split:nat.split)
wenzelm@13114
   707
wenzelm@13114
   708
lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)"
wenzelm@13114
   709
by(induct xs, simp, simp split:nat.split, fast)
wenzelm@13114
   710
wenzelm@13114
   711
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
wenzelm@13114
   712
by(fast dest!:set_update_subset_insert[THEN subsetD])
wenzelm@13114
   713
wenzelm@13114
   714
wenzelm@13114
   715
(** last & butlast **)
wenzelm@13114
   716
wenzelm@13114
   717
section "last / butlast"
wenzelm@13114
   718
wenzelm@13114
   719
lemma last_snoc[simp]: "last(xs@[x]) = x"
wenzelm@13114
   720
by(induct xs, auto)
wenzelm@13114
   721
wenzelm@13114
   722
lemma butlast_snoc[simp]:"butlast(xs@[x]) = xs"
wenzelm@13114
   723
by(induct xs, auto)
wenzelm@13114
   724
wenzelm@13114
   725
lemma length_butlast[simp]: "length(butlast xs) = length xs - 1"
wenzelm@13114
   726
by(induct xs rule:rev_induct, auto)
wenzelm@13114
   727
wenzelm@13114
   728
lemma butlast_append:
wenzelm@13114
   729
 "!!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)"
wenzelm@13114
   730
by(induct xs, auto)
wenzelm@13114
   731
wenzelm@13114
   732
lemma append_butlast_last_id[simp]:
wenzelm@13114
   733
 "xs ~= [] --> butlast xs @ [last xs] = xs"
wenzelm@13114
   734
by(induct xs, auto)
wenzelm@13114
   735
wenzelm@13114
   736
lemma in_set_butlastD: "x:set(butlast xs) ==> x:set xs"
wenzelm@13114
   737
by(induct xs, auto split:split_if_asm)
wenzelm@13114
   738
wenzelm@13114
   739
lemma in_set_butlast_appendI:
wenzelm@13114
   740
 "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))"
wenzelm@13114
   741
by(auto dest:in_set_butlastD simp add:butlast_append)
wenzelm@13114
   742
wenzelm@13114
   743
(** take  & drop **)
wenzelm@13114
   744
section "take & drop"
wenzelm@13114
   745
wenzelm@13114
   746
lemma take_0[simp]: "take 0 xs = []"
wenzelm@13114
   747
by(induct xs, auto)
wenzelm@13114
   748
wenzelm@13114
   749
lemma drop_0[simp]: "drop 0 xs = xs"
wenzelm@13114
   750
by(induct xs, auto)
wenzelm@13114
   751
wenzelm@13114
   752
lemma take_Suc_Cons[simp]: "take (Suc n) (x#xs) = x # take n xs"
wenzelm@13114
   753
by simp
wenzelm@13114
   754
wenzelm@13114
   755
lemma drop_Suc_Cons[simp]: "drop (Suc n) (x#xs) = drop n xs"
wenzelm@13114
   756
by simp
wenzelm@13114
   757
wenzelm@13114
   758
declare take_Cons[simp del] drop_Cons[simp del]
wenzelm@13114
   759
wenzelm@13114
   760
lemma length_take[simp]: "!!xs. length(take n xs) = min (length xs) n"
wenzelm@13114
   761
by(induct n, auto, case_tac xs, auto)
wenzelm@13114
   762
wenzelm@13114
   763
lemma length_drop[simp]: "!!xs. length(drop n xs) = (length xs - n)"
wenzelm@13114
   764
by(induct n, auto, case_tac xs, auto)
wenzelm@13114
   765
wenzelm@13114
   766
lemma take_all[simp]: "!!xs. length xs <= n ==> take n xs = xs"
wenzelm@13114
   767
by(induct n, auto, case_tac xs, auto)
wenzelm@13114
   768
wenzelm@13114
   769
lemma drop_all[simp]: "!!xs. length xs <= n ==> drop n xs = []"
wenzelm@13114
   770
by(induct n, auto, case_tac xs, auto)
wenzelm@13114
   771
wenzelm@13114
   772
lemma take_append[simp]:
wenzelm@13114
   773
 "!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
wenzelm@13114
   774
by(induct n, auto, case_tac xs, auto)
wenzelm@13114
   775
wenzelm@13114
   776
lemma drop_append[simp]:
wenzelm@13114
   777
 "!!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys" 
wenzelm@13114
   778
by(induct n, auto, case_tac xs, auto)
wenzelm@13114
   779
wenzelm@13114
   780
lemma take_take[simp]: "!!xs n. take n (take m xs) = take (min n m) xs"
wenzelm@13114
   781
apply(induct m)
wenzelm@13114
   782
 apply auto
wenzelm@13114
   783
apply(case_tac xs)
wenzelm@13114
   784
 apply auto
wenzelm@13114
   785
apply(case_tac na)
wenzelm@13114
   786
 apply auto
wenzelm@13114
   787
done
wenzelm@13114
   788
wenzelm@13114
   789
lemma drop_drop[simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs" 
wenzelm@13114
   790
apply(induct m)
wenzelm@13114
   791
 apply auto
wenzelm@13114
   792
apply(case_tac xs)
wenzelm@13114
   793
 apply auto
wenzelm@13114
   794
done
wenzelm@13114
   795
wenzelm@13114
   796
lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)"
wenzelm@13114
   797
apply(induct m)
wenzelm@13114
   798
 apply auto
wenzelm@13114
   799
apply(case_tac xs)
wenzelm@13114
   800
 apply auto
wenzelm@13114
   801
done
wenzelm@13114
   802
wenzelm@13114
   803
lemma append_take_drop_id[simp]: "!!xs. take n xs @ drop n xs = xs"
wenzelm@13114
   804
apply(induct n)
wenzelm@13114
   805
 apply auto
wenzelm@13114
   806
apply(case_tac xs)
wenzelm@13114
   807
 apply auto
wenzelm@13114
   808
done
wenzelm@13114
   809
wenzelm@13114
   810
lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)"
wenzelm@13114
   811
apply(induct n)
wenzelm@13114
   812
 apply auto
wenzelm@13114
   813
apply(case_tac xs)
wenzelm@13114
   814
 apply auto
wenzelm@13114
   815
done
wenzelm@13114
   816
wenzelm@13114
   817
lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)" 
wenzelm@13114
   818
apply(induct n)
wenzelm@13114
   819
 apply auto
wenzelm@13114
   820
apply(case_tac xs)
wenzelm@13114
   821
 apply auto
wenzelm@13114
   822
done
wenzelm@13114
   823
wenzelm@13114
   824
lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)"
wenzelm@13114
   825
apply(induct xs)
wenzelm@13114
   826
 apply auto
wenzelm@13114
   827
apply(case_tac i)
wenzelm@13114
   828
 apply auto
wenzelm@13114
   829
done
wenzelm@13114
   830
wenzelm@13114
   831
lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)"
wenzelm@13114
   832
apply(induct xs)
wenzelm@13114
   833
 apply auto
wenzelm@13114
   834
apply(case_tac i)
wenzelm@13114
   835
 apply auto
wenzelm@13114
   836
done
wenzelm@13114
   837
wenzelm@13114
   838
lemma nth_take[simp]: "!!n i. i < n ==> (take n xs)!i = xs!i"
wenzelm@13114
   839
apply(induct xs)
wenzelm@13114
   840
 apply auto
wenzelm@13114
   841
apply(case_tac n)
wenzelm@13114
   842
 apply(blast )
wenzelm@13114
   843
apply(case_tac i)
wenzelm@13114
   844
 apply auto
wenzelm@13114
   845
done
wenzelm@13114
   846
wenzelm@13114
   847
lemma nth_drop[simp]: "!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n+i)"
wenzelm@13114
   848
apply(induct n)
wenzelm@13114
   849
 apply auto
wenzelm@13114
   850
apply(case_tac xs)
wenzelm@13114
   851
 apply auto
wenzelm@13114
   852
done
nipkow@3507
   853
wenzelm@13114
   854
lemma append_eq_conv_conj:
wenzelm@13114
   855
 "!!zs. (xs@ys = zs) = (xs = take (length xs) zs & ys = drop (length xs) zs)"
wenzelm@13114
   856
apply(induct xs)
wenzelm@13114
   857
 apply simp
wenzelm@13114
   858
apply clarsimp
wenzelm@13114
   859
apply(case_tac zs)
wenzelm@13114
   860
apply auto
wenzelm@13114
   861
done
wenzelm@13114
   862
wenzelm@13114
   863
(** takeWhile & dropWhile **)
wenzelm@13114
   864
wenzelm@13114
   865
section "takeWhile & dropWhile"
wenzelm@13114
   866
wenzelm@13114
   867
lemma takeWhile_dropWhile_id[simp]: "takeWhile P xs @ dropWhile P xs = xs"
wenzelm@13114
   868
by(induct xs, auto)
wenzelm@13114
   869
wenzelm@13114
   870
lemma  takeWhile_append1[simp]:
wenzelm@13114
   871
 "\<lbrakk> x:set xs; ~P(x) \<rbrakk> \<Longrightarrow> takeWhile P (xs @ ys) = takeWhile P xs"
wenzelm@13114
   872
by(induct xs, auto)
wenzelm@13114
   873
wenzelm@13114
   874
lemma takeWhile_append2[simp]:
wenzelm@13114
   875
 "(!!x. x : set xs \<Longrightarrow> P(x)) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
wenzelm@13114
   876
by(induct xs, auto)
wenzelm@13114
   877
wenzelm@13114
   878
lemma takeWhile_tail: "~P(x) ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
wenzelm@13114
   879
by(induct xs, auto)
wenzelm@13114
   880
wenzelm@13114
   881
lemma dropWhile_append1[simp]:
wenzelm@13114
   882
 "\<lbrakk> x : set xs; ~P(x) \<rbrakk> \<Longrightarrow> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
wenzelm@13114
   883
by(induct xs, auto)
wenzelm@13114
   884
wenzelm@13114
   885
lemma dropWhile_append2[simp]:
wenzelm@13114
   886
 "(!!x. x:set xs \<Longrightarrow> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
wenzelm@13114
   887
by(induct xs, auto)
wenzelm@13114
   888
wenzelm@13114
   889
lemma set_take_whileD: "x:set(takeWhile P xs) ==> x:set xs & P x"
wenzelm@13114
   890
by(induct xs, auto split:split_if_asm)
wenzelm@13114
   891
wenzelm@13114
   892
wenzelm@13114
   893
(** zip **)
wenzelm@13114
   894
section "zip"
wenzelm@13114
   895
wenzelm@13114
   896
lemma zip_Nil[simp]: "zip [] ys = []"
wenzelm@13114
   897
by(induct ys, auto)
wenzelm@13114
   898
wenzelm@13114
   899
lemma zip_Cons_Cons[simp]: "zip (x#xs) (y#ys) = (x,y)#zip xs ys"
wenzelm@13114
   900
by simp
wenzelm@13114
   901
wenzelm@13114
   902
declare zip_Cons[simp del]
wenzelm@13114
   903
wenzelm@13114
   904
lemma length_zip[simp]:
wenzelm@13114
   905
 "!!xs. length (zip xs ys) = min (length xs) (length ys)"
wenzelm@13114
   906
apply(induct ys)
wenzelm@13114
   907
 apply simp
wenzelm@13114
   908
apply(case_tac xs)
wenzelm@13114
   909
 apply auto
wenzelm@13114
   910
done
wenzelm@13114
   911
wenzelm@13114
   912
lemma zip_append1:
wenzelm@13114
   913
 "!!xs. zip (xs@ys) zs =
wenzelm@13114
   914
        zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
wenzelm@13114
   915
apply(induct zs)
wenzelm@13114
   916
 apply simp
wenzelm@13114
   917
apply(case_tac xs)
wenzelm@13114
   918
 apply simp_all
wenzelm@13114
   919
done
wenzelm@13114
   920
wenzelm@13114
   921
lemma zip_append2:
wenzelm@13114
   922
 "!!ys. zip xs (ys@zs) =
wenzelm@13114
   923
        zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
wenzelm@13114
   924
apply(induct xs)
wenzelm@13114
   925
 apply simp
wenzelm@13114
   926
apply(case_tac ys)
wenzelm@13114
   927
 apply simp_all
wenzelm@13114
   928
done
wenzelm@13114
   929
wenzelm@13114
   930
lemma zip_append[simp]:
wenzelm@13114
   931
 "[| length xs = length us; length ys = length vs |] ==> \
wenzelm@13114
   932
\ zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
wenzelm@13114
   933
by(simp add: zip_append1)
wenzelm@13114
   934
wenzelm@13114
   935
lemma zip_rev:
wenzelm@13114
   936
 "!!xs. length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
wenzelm@13114
   937
apply(induct ys)
wenzelm@13114
   938
 apply simp
wenzelm@13114
   939
apply(case_tac xs)
wenzelm@13114
   940
 apply simp_all
wenzelm@13114
   941
done
wenzelm@13114
   942
wenzelm@13114
   943
lemma nth_zip[simp]:
wenzelm@13114
   944
"!!i xs. \<lbrakk> i < length xs; i < length ys \<rbrakk> \<Longrightarrow> (zip xs ys)!i = (xs!i, ys!i)"
wenzelm@13114
   945
apply(induct ys)
wenzelm@13114
   946
 apply simp
wenzelm@13114
   947
apply(case_tac xs)
wenzelm@13114
   948
 apply (simp_all add: nth.simps split:nat.split)
wenzelm@13114
   949
done
wenzelm@13114
   950
wenzelm@13114
   951
lemma set_zip:
wenzelm@13114
   952
 "set(zip xs ys) = {(xs!i,ys!i) |i. i < min (length xs) (length ys)}"
wenzelm@13114
   953
by(simp add: set_conv_nth cong: rev_conj_cong)
wenzelm@13114
   954
wenzelm@13114
   955
lemma zip_update:
wenzelm@13114
   956
 "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
wenzelm@13114
   957
by(rule sym, simp add: update_zip)
wenzelm@13114
   958
wenzelm@13114
   959
lemma zip_replicate[simp]:
wenzelm@13114
   960
 "!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
wenzelm@13114
   961
apply(induct i)
wenzelm@13114
   962
 apply auto
wenzelm@13114
   963
apply(case_tac j)
wenzelm@13114
   964
 apply auto
wenzelm@13114
   965
done
wenzelm@13114
   966
wenzelm@13114
   967
(** list_all2 **)
wenzelm@13114
   968
section "list_all2"
wenzelm@13114
   969
wenzelm@13114
   970
lemma list_all2_lengthD: "list_all2 P xs ys ==> length xs = length ys"
wenzelm@13114
   971
by(simp add:list_all2_def)
wenzelm@13114
   972
wenzelm@13114
   973
lemma list_all2_Nil[iff]: "list_all2 P [] ys = (ys=[])"
wenzelm@13114
   974
by(simp add:list_all2_def)
wenzelm@13114
   975
wenzelm@13114
   976
lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs=[])"
wenzelm@13114
   977
by(simp add:list_all2_def)
wenzelm@13114
   978
wenzelm@13114
   979
lemma list_all2_Cons[iff]:
wenzelm@13114
   980
 "list_all2 P (x#xs) (y#ys) = (P x y & list_all2 P xs ys)"
wenzelm@13114
   981
by(auto simp add:list_all2_def)
wenzelm@13114
   982
wenzelm@13114
   983
lemma list_all2_Cons1:
wenzelm@13114
   984
 "list_all2 P (x#xs) ys = (? z zs. ys = z#zs & P x z & list_all2 P xs zs)"
wenzelm@13114
   985
by(case_tac ys, auto)
wenzelm@13114
   986
wenzelm@13114
   987
lemma list_all2_Cons2:
wenzelm@13114
   988
 "list_all2 P xs (y#ys) = (? z zs. xs = z#zs & P z y & list_all2 P zs ys)"
wenzelm@13114
   989
by(case_tac xs, auto)
wenzelm@13114
   990
wenzelm@13114
   991
lemma list_all2_rev[iff]:
wenzelm@13114
   992
 "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
wenzelm@13114
   993
by(simp add:list_all2_def zip_rev cong:conj_cong)
wenzelm@13114
   994
wenzelm@13114
   995
lemma list_all2_append1:
wenzelm@13114
   996
 "list_all2 P (xs@ys) zs =
wenzelm@13114
   997
  (EX us vs. zs = us@vs & length us = length xs & length vs = length ys &
wenzelm@13114
   998
             list_all2 P xs us & list_all2 P ys vs)"
wenzelm@13114
   999
apply(simp add:list_all2_def zip_append1)
wenzelm@13114
  1000
apply(rule iffI)
wenzelm@13114
  1001
 apply(rule_tac x = "take (length xs) zs" in exI)
wenzelm@13114
  1002
 apply(rule_tac x = "drop (length xs) zs" in exI)
wenzelm@13114
  1003
 apply(force split: nat_diff_split simp add:min_def)
wenzelm@13114
  1004
apply clarify
wenzelm@13114
  1005
apply(simp add: ball_Un)
wenzelm@13114
  1006
done
wenzelm@13114
  1007
wenzelm@13114
  1008
lemma list_all2_append2:
wenzelm@13114
  1009
 "list_all2 P xs (ys@zs) =
wenzelm@13114
  1010
  (EX us vs. xs = us@vs & length us = length ys & length vs = length zs &
wenzelm@13114
  1011
             list_all2 P us ys & list_all2 P vs zs)"
wenzelm@13114
  1012
apply(simp add:list_all2_def zip_append2)
wenzelm@13114
  1013
apply(rule iffI)
wenzelm@13114
  1014
 apply(rule_tac x = "take (length ys) xs" in exI)
wenzelm@13114
  1015
 apply(rule_tac x = "drop (length ys) xs" in exI)
wenzelm@13114
  1016
 apply(force split: nat_diff_split simp add:min_def)
wenzelm@13114
  1017
apply clarify
wenzelm@13114
  1018
apply(simp add: ball_Un)
wenzelm@13114
  1019
done
wenzelm@13114
  1020
wenzelm@13114
  1021
lemma list_all2_conv_all_nth:
wenzelm@13114
  1022
  "list_all2 P xs ys =
wenzelm@13114
  1023
   (length xs = length ys & (!i<length xs. P (xs!i) (ys!i)))"
wenzelm@13114
  1024
by(force simp add:list_all2_def set_zip)
wenzelm@13114
  1025
wenzelm@13114
  1026
lemma list_all2_trans[rule_format]:
wenzelm@13114
  1027
 "ALL a b c. P1 a b --> P2 b c --> P3 a c ==>
wenzelm@13114
  1028
  ALL bs cs. list_all2 P1 as bs --> list_all2 P2 bs cs --> list_all2 P3 as cs"
wenzelm@13114
  1029
apply(induct_tac as)
wenzelm@13114
  1030
 apply simp
wenzelm@13114
  1031
apply(rule allI)
wenzelm@13114
  1032
apply(induct_tac bs)
wenzelm@13114
  1033
 apply simp
wenzelm@13114
  1034
apply(rule allI)
wenzelm@13114
  1035
apply(induct_tac cs)
wenzelm@13114
  1036
 apply auto
wenzelm@13114
  1037
done
wenzelm@13114
  1038
wenzelm@13114
  1039
wenzelm@13114
  1040
section "foldl"
wenzelm@13114
  1041
wenzelm@13114
  1042
lemma foldl_append[simp]:
wenzelm@13114
  1043
 "!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
wenzelm@13114
  1044
by(induct xs, auto)
wenzelm@13114
  1045
wenzelm@13114
  1046
(* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
wenzelm@13114
  1047
   because it requires an additional transitivity step
wenzelm@13114
  1048
*)
wenzelm@13114
  1049
lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl op+ n ns"
wenzelm@13114
  1050
by(induct ns, auto)
wenzelm@13114
  1051
wenzelm@13114
  1052
lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl op+ 0 ns"
wenzelm@13114
  1053
by(force intro: start_le_sum simp add:in_set_conv_decomp)
wenzelm@13114
  1054
wenzelm@13114
  1055
lemma sum_eq_0_conv[iff]:
wenzelm@13114
  1056
 "!!m::nat. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))"
wenzelm@13114
  1057
by(induct ns, auto)
wenzelm@13114
  1058
wenzelm@13114
  1059
(** upto **)
wenzelm@13114
  1060
wenzelm@13114
  1061
(* Does not terminate! *)
wenzelm@13114
  1062
lemma upt_rec: "[i..j(] = (if i<j then i#[Suc i..j(] else [])"
wenzelm@13114
  1063
by(induct j, auto)
wenzelm@13114
  1064
wenzelm@13114
  1065
lemma upt_conv_Nil[simp]: "j<=i ==> [i..j(] = []"
wenzelm@13114
  1066
by(subst upt_rec, simp)
wenzelm@13114
  1067
wenzelm@13114
  1068
(*Only needed if upt_Suc is deleted from the simpset*)
wenzelm@13114
  1069
lemma upt_Suc_append: "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]"
wenzelm@13114
  1070
by simp
wenzelm@13114
  1071
wenzelm@13114
  1072
lemma upt_conv_Cons: "i<j ==> [i..j(] = i#[Suc i..j(]"
wenzelm@13114
  1073
apply(rule trans)
wenzelm@13114
  1074
apply(subst upt_rec)
wenzelm@13114
  1075
 prefer 2 apply(rule refl)
wenzelm@13114
  1076
apply simp
wenzelm@13114
  1077
done
wenzelm@13114
  1078
wenzelm@13114
  1079
(*LOOPS as a simprule, since j<=j*)
wenzelm@13114
  1080
lemma upt_add_eq_append: "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]"
wenzelm@13114
  1081
by(induct_tac "k", auto)
wenzelm@13114
  1082
wenzelm@13114
  1083
lemma length_upt[simp]: "length [i..j(] = j-i"
wenzelm@13114
  1084
by(induct_tac j, simp, simp add: Suc_diff_le)
wenzelm@13114
  1085
wenzelm@13114
  1086
lemma nth_upt[simp]: "i+k < j ==> [i..j(] ! k = i+k"
wenzelm@13114
  1087
apply(induct j)
wenzelm@13114
  1088
apply(auto simp add: less_Suc_eq nth_append split:nat_diff_split)
wenzelm@13114
  1089
done
wenzelm@13114
  1090
wenzelm@13114
  1091
lemma take_upt[simp]: "!!i. i+m <= n ==> take m [i..n(] = [i..i+m(]"
wenzelm@13114
  1092
apply(induct m)
wenzelm@13114
  1093
 apply simp
wenzelm@13114
  1094
apply(subst upt_rec)
wenzelm@13114
  1095
apply(rule sym)
wenzelm@13114
  1096
apply(subst upt_rec)
wenzelm@13114
  1097
apply(simp del: upt.simps)
wenzelm@13114
  1098
done
nipkow@3507
  1099
wenzelm@13114
  1100
lemma map_Suc_upt: "map Suc [m..n(] = [Suc m..n]"
wenzelm@13114
  1101
by(induct n, auto)
wenzelm@13114
  1102
wenzelm@13114
  1103
lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..n(]) ! i = f(m+i)"
wenzelm@13114
  1104
thm diff_induct
wenzelm@13114
  1105
apply(induct n m rule: diff_induct)
wenzelm@13114
  1106
prefer 3 apply(subst map_Suc_upt[symmetric])
wenzelm@13114
  1107
apply(auto simp add: less_diff_conv nth_upt)
wenzelm@13114
  1108
done
wenzelm@13114
  1109
wenzelm@13114
  1110
lemma nth_take_lemma[rule_format]:
wenzelm@13114
  1111
 "ALL xs ys. k <= length xs --> k <= length ys
wenzelm@13114
  1112
             --> (ALL i. i < k --> xs!i = ys!i)
wenzelm@13114
  1113
             --> take k xs = take k ys"
wenzelm@13114
  1114
apply(induct_tac k)
wenzelm@13114
  1115
apply(simp_all add: less_Suc_eq_0_disj all_conj_distrib)
wenzelm@13114
  1116
apply clarify
wenzelm@13114
  1117
(*Both lists must be non-empty*)
wenzelm@13114
  1118
apply(case_tac xs)
wenzelm@13114
  1119
 apply simp
wenzelm@13114
  1120
apply(case_tac ys)
wenzelm@13114
  1121
 apply clarify
wenzelm@13114
  1122
 apply(simp (no_asm_use))
wenzelm@13114
  1123
apply clarify
wenzelm@13114
  1124
(*prenexing's needed, not miniscoping*)
wenzelm@13114
  1125
apply(simp (no_asm_use) add: all_simps[symmetric] del: all_simps)
wenzelm@13114
  1126
apply blast
wenzelm@13114
  1127
(*prenexing's needed, not miniscoping*)
wenzelm@13114
  1128
done
wenzelm@13114
  1129
wenzelm@13114
  1130
lemma nth_equalityI:
wenzelm@13114
  1131
 "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
wenzelm@13114
  1132
apply(frule nth_take_lemma[OF le_refl eq_imp_le])
wenzelm@13114
  1133
apply(simp_all add: take_all)
wenzelm@13114
  1134
done
wenzelm@13114
  1135
wenzelm@13114
  1136
(*The famous take-lemma*)
wenzelm@13114
  1137
lemma take_equalityI: "(ALL i. take i xs = take i ys) ==> xs = ys"
wenzelm@13114
  1138
apply(drule_tac x = "max (length xs) (length ys)" in spec)
wenzelm@13114
  1139
apply(simp add: le_max_iff_disj take_all)
wenzelm@13114
  1140
done
wenzelm@13114
  1141
wenzelm@13114
  1142
wenzelm@13114
  1143
(** distinct & remdups **)
wenzelm@13114
  1144
section "distinct & remdups"
wenzelm@13114
  1145
wenzelm@13114
  1146
lemma distinct_append[simp]:
wenzelm@13114
  1147
 "distinct(xs@ys) = (distinct xs & distinct ys & set xs Int set ys = {})"
wenzelm@13114
  1148
by(induct xs, auto)
wenzelm@13114
  1149
wenzelm@13114
  1150
lemma set_remdups[simp]: "set(remdups xs) = set xs"
wenzelm@13114
  1151
by(induct xs, simp, simp add:insert_absorb)
wenzelm@13114
  1152
wenzelm@13114
  1153
lemma distinct_remdups[iff]: "distinct(remdups xs)"
wenzelm@13114
  1154
by(induct xs, auto)
wenzelm@13114
  1155
wenzelm@13114
  1156
lemma distinct_filter[simp]: "distinct xs ==> distinct (filter P xs)"
wenzelm@13114
  1157
by(induct xs, auto)
wenzelm@13114
  1158
wenzelm@13114
  1159
(** replicate **)
wenzelm@13114
  1160
section "replicate"
wenzelm@13114
  1161
wenzelm@13114
  1162
lemma length_replicate[simp]: "length(replicate n x) = n"
wenzelm@13114
  1163
by(induct n, auto)
wenzelm@13114
  1164
wenzelm@13114
  1165
lemma map_replicate[simp]: "map f (replicate n x) = replicate n (f x)"
wenzelm@13114
  1166
by(induct n, auto)
wenzelm@13114
  1167
wenzelm@13114
  1168
lemma replicate_app_Cons_same:
wenzelm@13114
  1169
 "(replicate n x) @ (x#xs) = x # replicate n x @ xs"
wenzelm@13114
  1170
by(induct n, auto)
wenzelm@13114
  1171
wenzelm@13114
  1172
lemma rev_replicate[simp]: "rev(replicate n x) = replicate n x"
wenzelm@13114
  1173
apply(induct n)
wenzelm@13114
  1174
 apply simp
wenzelm@13114
  1175
apply(simp add: replicate_app_Cons_same)
wenzelm@13114
  1176
done
wenzelm@13114
  1177
wenzelm@13114
  1178
lemma replicate_add: "replicate (n+m) x = replicate n x @ replicate m x"
wenzelm@13114
  1179
by(induct n, auto)
wenzelm@13114
  1180
wenzelm@13114
  1181
lemma hd_replicate[simp]: "n ~= 0 ==> hd(replicate n x) = x"
wenzelm@13114
  1182
by(induct n, auto)
wenzelm@13114
  1183
wenzelm@13114
  1184
lemma tl_replicate[simp]: "n ~= 0 ==> tl(replicate n x) = replicate (n - 1) x"
wenzelm@13114
  1185
by(induct n, auto)
wenzelm@13114
  1186
wenzelm@13114
  1187
lemma last_replicate[rule_format,simp]:
wenzelm@13114
  1188
 "n ~= 0 --> last(replicate n x) = x"
wenzelm@13114
  1189
by(induct_tac n, auto)
wenzelm@13114
  1190
wenzelm@13114
  1191
lemma nth_replicate[simp]: "!!i. i<n ==> (replicate n x)!i = x"
wenzelm@13114
  1192
apply(induct n)
wenzelm@13114
  1193
 apply simp
wenzelm@13114
  1194
apply(simp add: nth_Cons split:nat.split)
wenzelm@13114
  1195
done
wenzelm@13114
  1196
wenzelm@13114
  1197
lemma set_replicate_Suc: "set(replicate (Suc n) x) = {x}"
wenzelm@13114
  1198
by(induct n, auto)
wenzelm@13114
  1199
wenzelm@13114
  1200
lemma set_replicate[simp]: "n ~= 0 ==> set(replicate n x) = {x}"
wenzelm@13114
  1201
by(fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
wenzelm@13114
  1202
wenzelm@13114
  1203
lemma set_replicate_conv_if: "set(replicate n x) = (if n=0 then {} else {x})"
wenzelm@13114
  1204
by auto
wenzelm@13114
  1205
wenzelm@13114
  1206
lemma in_set_replicateD: "x : set(replicate n y) ==> x=y"
wenzelm@13114
  1207
by(simp add: set_replicate_conv_if split:split_if_asm)
wenzelm@13114
  1208
wenzelm@13114
  1209
wenzelm@13114
  1210
(*** Lexcicographic orderings on lists ***)
wenzelm@13114
  1211
section"Lexcicographic orderings on lists"
nipkow@3507
  1212
wenzelm@13114
  1213
lemma wf_lexn: "wf r ==> wf(lexn r n)"
wenzelm@13114
  1214
apply(induct_tac n)
wenzelm@13114
  1215
 apply simp
wenzelm@13114
  1216
apply simp
wenzelm@13114
  1217
apply(rule wf_subset)
wenzelm@13114
  1218
 prefer 2 apply(rule Int_lower1)
wenzelm@13114
  1219
apply(rule wf_prod_fun_image)
wenzelm@13114
  1220
 prefer 2 apply(rule injI)
wenzelm@13114
  1221
apply auto
wenzelm@13114
  1222
done
wenzelm@13114
  1223
wenzelm@13114
  1224
lemma lexn_length:
wenzelm@13114
  1225
 "!!xs ys. (xs,ys) : lexn r n ==> length xs = n & length ys = n"
wenzelm@13114
  1226
by(induct n, auto)
wenzelm@13114
  1227
wenzelm@13114
  1228
lemma wf_lex[intro!]: "wf r ==> wf(lex r)"
wenzelm@13114
  1229
apply(unfold lex_def)
wenzelm@13114
  1230
apply(rule wf_UN)
wenzelm@13114
  1231
apply(blast intro: wf_lexn)
wenzelm@13114
  1232
apply clarify
wenzelm@13114
  1233
apply(rename_tac m n)
wenzelm@13114
  1234
apply(subgoal_tac "m ~= n")
wenzelm@13114
  1235
 prefer 2 apply blast
wenzelm@13114
  1236
apply(blast dest: lexn_length not_sym)
wenzelm@13114
  1237
done
wenzelm@13114
  1238
wenzelm@13114
  1239
wenzelm@13114
  1240
lemma lexn_conv:
wenzelm@13114
  1241
 "lexn r n =
wenzelm@13114
  1242
  {(xs,ys). length xs = n & length ys = n &
wenzelm@13114
  1243
            (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}"
wenzelm@13114
  1244
apply(induct_tac n)
wenzelm@13114
  1245
 apply simp
wenzelm@13114
  1246
 apply blast
wenzelm@13114
  1247
apply(simp add: image_Collect lex_prod_def)
wenzelm@13114
  1248
apply auto
wenzelm@13114
  1249
  apply blast
wenzelm@13114
  1250
 apply(rename_tac a xys x xs' y ys')
wenzelm@13114
  1251
 apply(rule_tac x = "a#xys" in exI)
wenzelm@13114
  1252
 apply simp
wenzelm@13114
  1253
apply(case_tac xys)
wenzelm@13114
  1254
 apply simp_all
wenzelm@13114
  1255
apply blast
wenzelm@13114
  1256
done
wenzelm@13114
  1257
wenzelm@13114
  1258
lemma lex_conv:
wenzelm@13114
  1259
 "lex r =
wenzelm@13114
  1260
  {(xs,ys). length xs = length ys &
wenzelm@13114
  1261
            (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}"
wenzelm@13114
  1262
by(force simp add: lex_def lexn_conv)
wenzelm@13114
  1263
wenzelm@13114
  1264
lemma wf_lexico[intro!]: "wf r ==> wf(lexico r)"
wenzelm@13114
  1265
by(unfold lexico_def, blast)
wenzelm@13114
  1266
wenzelm@13114
  1267
lemma lexico_conv:
wenzelm@13114
  1268
"lexico r = {(xs,ys). length xs < length ys |
wenzelm@13114
  1269
                      length xs = length ys & (xs,ys) : lex r}"
wenzelm@13114
  1270
by(simp add: lexico_def diag_def lex_prod_def measure_def inv_image_def)
wenzelm@13114
  1271
wenzelm@13114
  1272
lemma Nil_notin_lex[iff]: "([],ys) ~: lex r"
wenzelm@13114
  1273
by(simp add:lex_conv)
wenzelm@13114
  1274
wenzelm@13114
  1275
lemma Nil2_notin_lex[iff]: "(xs,[]) ~: lex r"
wenzelm@13114
  1276
by(simp add:lex_conv)
wenzelm@13114
  1277
wenzelm@13114
  1278
lemma Cons_in_lex[iff]:
wenzelm@13114
  1279
 "((x#xs,y#ys) : lex r) =
wenzelm@13114
  1280
  ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)"
wenzelm@13114
  1281
apply(simp add:lex_conv)
wenzelm@13114
  1282
apply(rule iffI)
wenzelm@13114
  1283
 prefer 2 apply(blast intro: Cons_eq_appendI)
wenzelm@13114
  1284
apply clarify
wenzelm@13114
  1285
apply(case_tac xys)
wenzelm@13114
  1286
 apply simp
wenzelm@13114
  1287
apply simp
wenzelm@13114
  1288
apply blast
wenzelm@13114
  1289
done
wenzelm@13114
  1290
wenzelm@13114
  1291
wenzelm@13114
  1292
(*** sublist (a generalization of nth to sets) ***)
wenzelm@13114
  1293
wenzelm@13114
  1294
lemma sublist_empty[simp]: "sublist xs {} = []"
wenzelm@13114
  1295
by(auto simp add:sublist_def)
wenzelm@13114
  1296
wenzelm@13114
  1297
lemma sublist_nil[simp]: "sublist [] A = []"
wenzelm@13114
  1298
by(auto simp add:sublist_def)
wenzelm@13114
  1299
wenzelm@13114
  1300
lemma sublist_shift_lemma:
wenzelm@13114
  1301
 "map fst [p:zip xs [i..i + length xs(] . snd p : A] =
wenzelm@13114
  1302
  map fst [p:zip xs [0..length xs(] . snd p + i : A]"
wenzelm@13114
  1303
apply(induct_tac xs rule: rev_induct)
wenzelm@13114
  1304
 apply simp
wenzelm@13114
  1305
apply(simp add:add_commute)
wenzelm@13114
  1306
done
wenzelm@13114
  1307
wenzelm@13114
  1308
lemma sublist_append:
wenzelm@13114
  1309
 "sublist (l@l') A = sublist l A @ sublist l' {j. j + length l : A}"
wenzelm@13114
  1310
apply(unfold sublist_def)
wenzelm@13114
  1311
apply(induct_tac l' rule: rev_induct)
wenzelm@13114
  1312
 apply simp
wenzelm@13114
  1313
apply(simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
wenzelm@13114
  1314
apply(simp add:add_commute)
wenzelm@13114
  1315
done
wenzelm@13114
  1316
wenzelm@13114
  1317
lemma sublist_Cons:
wenzelm@13114
  1318
 "sublist (x#l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
wenzelm@13114
  1319
apply(induct_tac l rule: rev_induct)
wenzelm@13114
  1320
 apply(simp add:sublist_def)
wenzelm@13114
  1321
apply(simp del: append_Cons add: append_Cons[symmetric] sublist_append)
wenzelm@13114
  1322
done
wenzelm@13114
  1323
wenzelm@13114
  1324
lemma sublist_singleton[simp]: "sublist [x] A = (if 0 : A then [x] else [])"
wenzelm@13114
  1325
by(simp add:sublist_Cons)
wenzelm@13114
  1326
wenzelm@13114
  1327
lemma sublist_upt_eq_take[simp]: "sublist l {..n(} = take n l"
wenzelm@13114
  1328
apply(induct_tac l rule: rev_induct)
wenzelm@13114
  1329
 apply simp
wenzelm@13114
  1330
apply(simp split:nat_diff_split add:sublist_append)
wenzelm@13114
  1331
done
wenzelm@13114
  1332
wenzelm@13114
  1333
wenzelm@13114
  1334
lemma take_Cons': "take n (x#xs) = (if n=0 then [] else x # take (n - 1) xs)"
wenzelm@13114
  1335
by(case_tac n, simp_all)
wenzelm@13114
  1336
wenzelm@13114
  1337
lemma drop_Cons': "drop n (x#xs) = (if n=0 then x#xs else drop (n - 1) xs)"
wenzelm@13114
  1338
by(case_tac n, simp_all)
wenzelm@13114
  1339
wenzelm@13114
  1340
lemma nth_Cons': "(x#xs)!n = (if n=0 then x else xs!(n - 1))"
wenzelm@13114
  1341
by(case_tac n, simp_all)
wenzelm@13114
  1342
wenzelm@13114
  1343
lemmas [simp] = take_Cons'[of "number_of v",standard]
wenzelm@13114
  1344
                drop_Cons'[of "number_of v",standard]
wenzelm@13114
  1345
                nth_Cons'[of "number_of v",standard]
nipkow@3507
  1346
nipkow@3507
  1347
end;