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