src/HOL/List.ML
author paulson
Thu Jun 10 10:41:36 1999 +0200 (1999-06-10)
changeset 6813 bf90f86502b2
parent 6794 ac367328b875
child 6820 41d9b7bbf968
permissions -rw-r--r--
many new lemmas about take & drop, incl the famous take-lemma
Ran expandshort
clasohm@1465
     1
(*  Title:      HOL/List
clasohm@923
     2
    ID:         $Id$
clasohm@1465
     3
    Author:     Tobias Nipkow
clasohm@923
     4
    Copyright   1994 TU Muenchen
clasohm@923
     5
clasohm@923
     6
List lemmas
clasohm@923
     7
*)
clasohm@923
     8
nipkow@4935
     9
Goal "!x. xs ~= x#xs";
nipkow@3040
    10
by (induct_tac "xs" 1);
paulson@5316
    11
by Auto_tac;
nipkow@2608
    12
qed_spec_mp "not_Cons_self";
nipkow@3574
    13
bind_thm("not_Cons_self2",not_Cons_self RS not_sym);
nipkow@3574
    14
Addsimps [not_Cons_self,not_Cons_self2];
clasohm@923
    15
nipkow@4935
    16
Goal "(xs ~= []) = (? y ys. xs = y#ys)";
nipkow@3040
    17
by (induct_tac "xs" 1);
paulson@5316
    18
by Auto_tac;
clasohm@923
    19
qed "neq_Nil_conv";
clasohm@923
    20
nipkow@4830
    21
(* Induction over the length of a list: *)
nipkow@4935
    22
val [prem] = Goal
nipkow@4911
    23
  "(!!xs. (!ys. length ys < length xs --> P ys) ==> P xs) ==> P(xs)";
wenzelm@5132
    24
by (rtac measure_induct 1 THEN etac prem 1);
nipkow@4911
    25
qed "length_induct";
nipkow@4911
    26
clasohm@923
    27
paulson@3468
    28
(** "lists": the list-forming operator over sets **)
paulson@3342
    29
nipkow@5043
    30
Goalw lists.defs "A<=B ==> lists A <= lists B";
paulson@3342
    31
by (rtac lfp_mono 1);
paulson@3342
    32
by (REPEAT (ares_tac basic_monos 1));
paulson@3342
    33
qed "lists_mono";
paulson@3196
    34
paulson@6141
    35
val listsE = lists.mk_cases "x#l : lists A";
paulson@3468
    36
AddSEs [listsE];
paulson@3468
    37
AddSIs lists.intrs;
paulson@3468
    38
nipkow@5043
    39
Goal "l: lists A ==> l: lists B --> l: lists (A Int B)";
paulson@3468
    40
by (etac lists.induct 1);
paulson@3468
    41
by (ALLGOALS Blast_tac);
paulson@3468
    42
qed_spec_mp "lists_IntI";
paulson@3468
    43
nipkow@4935
    44
Goal "lists (A Int B) = lists A Int lists B";
wenzelm@4423
    45
by (rtac (mono_Int RS equalityI) 1);
wenzelm@4089
    46
by (simp_tac (simpset() addsimps [mono_def, lists_mono]) 1);
wenzelm@4089
    47
by (blast_tac (claset() addSIs [lists_IntI]) 1);
paulson@3468
    48
qed "lists_Int_eq";
paulson@3468
    49
Addsimps [lists_Int_eq];
paulson@3468
    50
paulson@3196
    51
nipkow@4643
    52
(**  Case analysis **)
nipkow@4643
    53
section "Case analysis";
nipkow@2608
    54
nipkow@4935
    55
val prems = Goal "[| P([]); !!x xs. P(x#xs) |] ==> P(xs)";
paulson@3457
    56
by (induct_tac "xs" 1);
paulson@3457
    57
by (REPEAT(resolve_tac prems 1));
nipkow@2608
    58
qed "list_cases";
nipkow@2608
    59
nipkow@4935
    60
Goal "(xs=[] --> P([])) & (!y ys. xs=y#ys --> P(y#ys)) --> P(xs)";
nipkow@3040
    61
by (induct_tac "xs" 1);
paulson@2891
    62
by (Blast_tac 1);
paulson@2891
    63
by (Blast_tac 1);
nipkow@2608
    64
bind_thm("list_eq_cases",
nipkow@2608
    65
  impI RSN (2,allI RSN (2,allI RSN (2,impI RS (conjI RS (result() RS mp))))));
nipkow@2608
    66
nipkow@3860
    67
(** length **)
nipkow@3860
    68
(* needs to come before "@" because of thm append_eq_append_conv *)
nipkow@3860
    69
nipkow@3860
    70
section "length";
nipkow@3860
    71
nipkow@4935
    72
Goal "length(xs@ys) = length(xs)+length(ys)";
nipkow@3860
    73
by (induct_tac "xs" 1);
paulson@5316
    74
by Auto_tac;
nipkow@3860
    75
qed"length_append";
nipkow@3860
    76
Addsimps [length_append];
nipkow@3860
    77
nipkow@5129
    78
Goal "length (map f xs) = length xs";
nipkow@5129
    79
by (induct_tac "xs" 1);
paulson@5316
    80
by Auto_tac;
nipkow@3860
    81
qed "length_map";
nipkow@3860
    82
Addsimps [length_map];
nipkow@3860
    83
nipkow@4935
    84
Goal "length(rev xs) = length(xs)";
nipkow@3860
    85
by (induct_tac "xs" 1);
paulson@5316
    86
by Auto_tac;
nipkow@3860
    87
qed "length_rev";
nipkow@3860
    88
Addsimps [length_rev];
nipkow@3860
    89
nipkow@5043
    90
Goal "xs ~= [] ==> length(tl xs) = (length xs) - 1";
wenzelm@4423
    91
by (exhaust_tac "xs" 1);
paulson@5316
    92
by Auto_tac;
nipkow@3896
    93
qed "length_tl";
nipkow@3896
    94
Addsimps [length_tl];
nipkow@3896
    95
nipkow@4935
    96
Goal "(length xs = 0) = (xs = [])";
nipkow@3860
    97
by (induct_tac "xs" 1);
paulson@5316
    98
by Auto_tac;
nipkow@3860
    99
qed "length_0_conv";
nipkow@3860
   100
AddIffs [length_0_conv];
nipkow@3860
   101
nipkow@4935
   102
Goal "(0 = length xs) = (xs = [])";
nipkow@3860
   103
by (induct_tac "xs" 1);
paulson@5316
   104
by Auto_tac;
nipkow@3860
   105
qed "zero_length_conv";
nipkow@3860
   106
AddIffs [zero_length_conv];
nipkow@3860
   107
nipkow@4935
   108
Goal "(0 < length xs) = (xs ~= [])";
nipkow@3860
   109
by (induct_tac "xs" 1);
paulson@5316
   110
by Auto_tac;
nipkow@3860
   111
qed "length_greater_0_conv";
nipkow@3860
   112
AddIffs [length_greater_0_conv];
nipkow@3860
   113
oheimb@5296
   114
Goal "(length xs = Suc n) = (? y ys. xs = y#ys & length ys = n)";
oheimb@5296
   115
by (induct_tac "xs" 1);
paulson@6813
   116
by Auto_tac;
oheimb@5296
   117
qed "length_Suc_conv";
oheimb@5296
   118
clasohm@923
   119
(** @ - append **)
clasohm@923
   120
nipkow@3467
   121
section "@ - append";
nipkow@3467
   122
nipkow@4935
   123
Goal "(xs@ys)@zs = xs@(ys@zs)";
nipkow@3040
   124
by (induct_tac "xs" 1);
paulson@5316
   125
by Auto_tac;
clasohm@923
   126
qed "append_assoc";
nipkow@2512
   127
Addsimps [append_assoc];
clasohm@923
   128
nipkow@4935
   129
Goal "xs @ [] = xs";
nipkow@3040
   130
by (induct_tac "xs" 1);
paulson@5316
   131
by Auto_tac;
clasohm@923
   132
qed "append_Nil2";
nipkow@2512
   133
Addsimps [append_Nil2];
clasohm@923
   134
nipkow@4935
   135
Goal "(xs@ys = []) = (xs=[] & ys=[])";
nipkow@3040
   136
by (induct_tac "xs" 1);
paulson@5316
   137
by Auto_tac;
nipkow@2608
   138
qed "append_is_Nil_conv";
nipkow@2608
   139
AddIffs [append_is_Nil_conv];
nipkow@2608
   140
nipkow@4935
   141
Goal "([] = xs@ys) = (xs=[] & ys=[])";
nipkow@3040
   142
by (induct_tac "xs" 1);
paulson@5316
   143
by Auto_tac;
nipkow@2608
   144
qed "Nil_is_append_conv";
nipkow@2608
   145
AddIffs [Nil_is_append_conv];
clasohm@923
   146
nipkow@4935
   147
Goal "(xs @ ys = xs) = (ys=[])";
nipkow@3574
   148
by (induct_tac "xs" 1);
paulson@5316
   149
by Auto_tac;
nipkow@3574
   150
qed "append_self_conv";
nipkow@3574
   151
nipkow@4935
   152
Goal "(xs = xs @ ys) = (ys=[])";
nipkow@3574
   153
by (induct_tac "xs" 1);
paulson@5316
   154
by Auto_tac;
nipkow@3574
   155
qed "self_append_conv";
nipkow@3574
   156
AddIffs [append_self_conv,self_append_conv];
nipkow@3574
   157
nipkow@4935
   158
Goal "!ys. length xs = length ys | length us = length vs \
nipkow@3860
   159
\              --> (xs@us = ys@vs) = (xs=ys & us=vs)";
wenzelm@4423
   160
by (induct_tac "xs" 1);
wenzelm@4423
   161
 by (rtac allI 1);
wenzelm@4423
   162
 by (exhaust_tac "ys" 1);
wenzelm@4423
   163
  by (Asm_simp_tac 1);
paulson@5641
   164
 by (Force_tac 1);
wenzelm@4423
   165
by (rtac allI 1);
wenzelm@4423
   166
by (exhaust_tac "ys" 1);
paulson@5641
   167
by (Force_tac 1);
wenzelm@4423
   168
by (Asm_simp_tac 1);
nipkow@3860
   169
qed_spec_mp "append_eq_append_conv";
nipkow@3860
   170
Addsimps [append_eq_append_conv];
nipkow@3860
   171
nipkow@4935
   172
Goal "(xs @ ys = xs @ zs) = (ys=zs)";
nipkow@3896
   173
by (Simp_tac 1);
nipkow@3896
   174
qed "same_append_eq";
nipkow@3860
   175
nipkow@4935
   176
Goal "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)"; 
nipkow@3896
   177
by (Simp_tac 1);
nipkow@3896
   178
qed "append1_eq_conv";
nipkow@2608
   179
nipkow@4935
   180
Goal "(ys @ xs = zs @ xs) = (ys=zs)";
nipkow@3896
   181
by (Simp_tac 1);
nipkow@3896
   182
qed "append_same_eq";
nipkow@2608
   183
nipkow@3896
   184
AddSIs
nipkow@3896
   185
 [same_append_eq RS iffD2, append1_eq_conv RS iffD2, append_same_eq RS iffD2];
nipkow@3896
   186
AddSDs
nipkow@3896
   187
 [same_append_eq RS iffD1, append1_eq_conv RS iffD1, append_same_eq RS iffD1];
nipkow@3571
   188
nipkow@4935
   189
Goal "(xs @ ys = ys) = (xs=[])";
wenzelm@5132
   190
by (cut_inst_tac [("zs","[]")] append_same_eq 1);
paulson@5316
   191
by Auto_tac;
nipkow@4647
   192
qed "append_self_conv2";
nipkow@4647
   193
nipkow@4935
   194
Goal "(ys = xs @ ys) = (xs=[])";
wenzelm@5132
   195
by (simp_tac (simpset() addsimps
nipkow@4647
   196
     [simplify (simpset()) (read_instantiate[("ys","[]")]append_same_eq)]) 1);
wenzelm@5132
   197
by (Blast_tac 1);
nipkow@4647
   198
qed "self_append_conv2";
nipkow@4647
   199
AddIffs [append_self_conv2,self_append_conv2];
nipkow@4647
   200
nipkow@4935
   201
Goal "xs ~= [] --> hd xs # tl xs = xs";
paulson@3457
   202
by (induct_tac "xs" 1);
paulson@5316
   203
by Auto_tac;
nipkow@2608
   204
qed_spec_mp "hd_Cons_tl";
nipkow@2608
   205
Addsimps [hd_Cons_tl];
clasohm@923
   206
nipkow@4935
   207
Goal "hd(xs@ys) = (if xs=[] then hd ys else hd xs)";
nipkow@3040
   208
by (induct_tac "xs" 1);
paulson@5316
   209
by Auto_tac;
nipkow@1327
   210
qed "hd_append";
clasohm@923
   211
nipkow@5043
   212
Goal "xs ~= [] ==> hd(xs @ ys) = hd xs";
wenzelm@4089
   213
by (asm_simp_tac (simpset() addsimps [hd_append]
berghofe@5183
   214
                           addsplits [list.split]) 1);
nipkow@3571
   215
qed "hd_append2";
nipkow@3571
   216
Addsimps [hd_append2];
nipkow@3571
   217
nipkow@4935
   218
Goal "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)";
berghofe@5183
   219
by (simp_tac (simpset() addsplits [list.split]) 1);
nipkow@2608
   220
qed "tl_append";
nipkow@2608
   221
nipkow@5043
   222
Goal "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys";
wenzelm@4089
   223
by (asm_simp_tac (simpset() addsimps [tl_append]
berghofe@5183
   224
                           addsplits [list.split]) 1);
nipkow@3571
   225
qed "tl_append2";
nipkow@3571
   226
Addsimps [tl_append2];
nipkow@3571
   227
nipkow@5272
   228
(* trivial rules for solving @-equations automatically *)
nipkow@5272
   229
nipkow@5272
   230
Goal "xs = ys ==> xs = [] @ ys";
paulson@5318
   231
by (Asm_simp_tac 1);
nipkow@5272
   232
qed "eq_Nil_appendI";
nipkow@5272
   233
nipkow@5272
   234
Goal "[| x#xs1 = ys; xs = xs1 @ zs |] ==> x#xs = ys@zs";
paulson@5318
   235
by (dtac sym 1);
paulson@5318
   236
by (Asm_simp_tac 1);
nipkow@5272
   237
qed "Cons_eq_appendI";
nipkow@5272
   238
nipkow@5272
   239
Goal "[| xs@xs1 = zs; ys = xs1 @ us |] ==> xs@ys = zs@us";
paulson@5318
   240
by (dtac sym 1);
paulson@5318
   241
by (Asm_simp_tac 1);
nipkow@5272
   242
qed "append_eq_appendI";
nipkow@5272
   243
nipkow@4830
   244
nipkow@5427
   245
(***
nipkow@5427
   246
Simplification procedure for all list equalities.
nipkow@5427
   247
Currently only tries to rearranges @ to see if
nipkow@5427
   248
- both lists end in a singleton list,
nipkow@5427
   249
- or both lists end in the same list.
nipkow@5427
   250
***)
nipkow@5427
   251
local
nipkow@5427
   252
nipkow@5427
   253
val list_eq_pattern =
wenzelm@6394
   254
  Thm.read_cterm (Theory.sign_of List.thy) ("(xs::'a list) = ys",HOLogic.boolT);
nipkow@5427
   255
nipkow@5427
   256
fun last (cons as Const("List.list.op #",_) $ _ $ xs) =
nipkow@5427
   257
      (case xs of Const("List.list.[]",_) => cons | _ => last xs)
nipkow@5427
   258
  | last (Const("List.op @",_) $ _ $ ys) = last ys
nipkow@5427
   259
  | last t = t;
nipkow@5427
   260
nipkow@5427
   261
fun list1 (Const("List.list.op #",_) $ _ $ Const("List.list.[]",_)) = true
nipkow@5427
   262
  | list1 _ = false;
nipkow@5427
   263
nipkow@5427
   264
fun butlast ((cons as Const("List.list.op #",_) $ x) $ xs) =
nipkow@5427
   265
      (case xs of Const("List.list.[]",_) => xs | _ => cons $ butlast xs)
nipkow@5427
   266
  | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
nipkow@5427
   267
  | butlast xs = Const("List.list.[]",fastype_of xs);
nipkow@5427
   268
nipkow@5427
   269
val rearr_tac =
nipkow@5427
   270
  simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons]);
nipkow@5427
   271
nipkow@5427
   272
fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
nipkow@5427
   273
  let
nipkow@5427
   274
    val lastl = last lhs and lastr = last rhs
nipkow@5427
   275
    fun rearr conv =
nipkow@5427
   276
      let val lhs1 = butlast lhs and rhs1 = butlast rhs
nipkow@5427
   277
          val Type(_,listT::_) = eqT
nipkow@5427
   278
          val appT = [listT,listT] ---> listT
nipkow@5427
   279
          val app = Const("List.op @",appT)
nipkow@5427
   280
          val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
nipkow@5427
   281
          val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2)))
nipkow@5427
   282
          val thm = prove_goalw_cterm [] ct (K [rearr_tac 1])
nipkow@5427
   283
            handle ERROR =>
nipkow@5427
   284
            error("The error(s) above occurred while trying to prove " ^
nipkow@5427
   285
                  string_of_cterm ct)
nipkow@5427
   286
      in Some((conv RS (thm RS trans)) RS eq_reflection) end
nipkow@5427
   287
nipkow@5427
   288
  in if list1 lastl andalso list1 lastr
nipkow@5427
   289
     then rearr append1_eq_conv
nipkow@5427
   290
     else
nipkow@5427
   291
     if lastl aconv lastr
nipkow@5427
   292
     then rearr append_same_eq
nipkow@5427
   293
     else None
nipkow@5427
   294
  end;
nipkow@5427
   295
in
nipkow@5427
   296
val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq;
nipkow@5427
   297
end;
nipkow@5427
   298
nipkow@5427
   299
Addsimprocs [list_eq_simproc];
nipkow@5427
   300
nipkow@5427
   301
nipkow@2608
   302
(** map **)
nipkow@2608
   303
nipkow@3467
   304
section "map";
nipkow@3467
   305
paulson@5278
   306
Goal "(!x. x : set xs --> f x = g x) --> map f xs = map g xs";
paulson@3457
   307
by (induct_tac "xs" 1);
paulson@5316
   308
by Auto_tac;
nipkow@2608
   309
bind_thm("map_ext", impI RS (allI RS (result() RS mp)));
nipkow@2608
   310
nipkow@4935
   311
Goal "map (%x. x) = (%xs. xs)";
nipkow@2608
   312
by (rtac ext 1);
nipkow@3040
   313
by (induct_tac "xs" 1);
paulson@5316
   314
by Auto_tac;
nipkow@2608
   315
qed "map_ident";
nipkow@2608
   316
Addsimps[map_ident];
nipkow@2608
   317
nipkow@4935
   318
Goal "map f (xs@ys) = map f xs @ map f ys";
nipkow@3040
   319
by (induct_tac "xs" 1);
paulson@5316
   320
by Auto_tac;
nipkow@2608
   321
qed "map_append";
nipkow@2608
   322
Addsimps[map_append];
nipkow@2608
   323
nipkow@4935
   324
Goalw [o_def] "map (f o g) xs = map f (map g xs)";
nipkow@3040
   325
by (induct_tac "xs" 1);
paulson@5316
   326
by Auto_tac;
nipkow@2608
   327
qed "map_compose";
nipkow@2608
   328
Addsimps[map_compose];
nipkow@2608
   329
nipkow@4935
   330
Goal "rev(map f xs) = map f (rev xs)";
nipkow@3040
   331
by (induct_tac "xs" 1);
paulson@5316
   332
by Auto_tac;
nipkow@2608
   333
qed "rev_map";
nipkow@2608
   334
nipkow@3589
   335
(* a congruence rule for map: *)
paulson@6451
   336
Goal "xs=ys ==> (!x. x : set ys --> f x = g x) --> map f xs = map g ys";
wenzelm@4423
   337
by (hyp_subst_tac 1);
wenzelm@4423
   338
by (induct_tac "ys" 1);
paulson@5316
   339
by Auto_tac;
paulson@6451
   340
bind_thm("map_cong", impI RSN (2,allI RSN (2, result() RS mp)));
nipkow@3589
   341
nipkow@4935
   342
Goal "(map f xs = []) = (xs = [])";
wenzelm@4423
   343
by (induct_tac "xs" 1);
paulson@5316
   344
by Auto_tac;
nipkow@3860
   345
qed "map_is_Nil_conv";
nipkow@3860
   346
AddIffs [map_is_Nil_conv];
nipkow@3860
   347
nipkow@4935
   348
Goal "([] = map f xs) = (xs = [])";
wenzelm@4423
   349
by (induct_tac "xs" 1);
paulson@5316
   350
by Auto_tac;
nipkow@3860
   351
qed "Nil_is_map_conv";
nipkow@3860
   352
AddIffs [Nil_is_map_conv];
nipkow@3860
   353
nipkow@3860
   354
lcp@1169
   355
(** rev **)
lcp@1169
   356
nipkow@3467
   357
section "rev";
nipkow@3467
   358
nipkow@4935
   359
Goal "rev(xs@ys) = rev(ys) @ rev(xs)";
nipkow@3040
   360
by (induct_tac "xs" 1);
paulson@5316
   361
by Auto_tac;
lcp@1169
   362
qed "rev_append";
nipkow@2512
   363
Addsimps[rev_append];
lcp@1169
   364
nipkow@4935
   365
Goal "rev(rev l) = l";
nipkow@3040
   366
by (induct_tac "l" 1);
paulson@5316
   367
by Auto_tac;
lcp@1169
   368
qed "rev_rev_ident";
nipkow@2512
   369
Addsimps[rev_rev_ident];
lcp@1169
   370
nipkow@4935
   371
Goal "(rev xs = []) = (xs = [])";
wenzelm@4423
   372
by (induct_tac "xs" 1);
paulson@5316
   373
by Auto_tac;
nipkow@3860
   374
qed "rev_is_Nil_conv";
nipkow@3860
   375
AddIffs [rev_is_Nil_conv];
nipkow@3860
   376
nipkow@4935
   377
Goal "([] = rev xs) = (xs = [])";
wenzelm@4423
   378
by (induct_tac "xs" 1);
paulson@5316
   379
by Auto_tac;
nipkow@3860
   380
qed "Nil_is_rev_conv";
nipkow@3860
   381
AddIffs [Nil_is_rev_conv];
nipkow@3860
   382
nipkow@4935
   383
val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
wenzelm@5132
   384
by (stac (rev_rev_ident RS sym) 1);
paulson@6162
   385
by (res_inst_tac [("list", "rev xs")] list.induct 1);
wenzelm@5132
   386
by (ALLGOALS Simp_tac);
wenzelm@5132
   387
by (resolve_tac prems 1);
wenzelm@5132
   388
by (eresolve_tac prems 1);
nipkow@4935
   389
qed "rev_induct";
nipkow@4935
   390
nipkow@5272
   391
fun rev_induct_tac xs = res_inst_tac [("xs",xs)] rev_induct;
nipkow@5272
   392
nipkow@4935
   393
Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
wenzelm@5132
   394
by (res_inst_tac [("xs","xs")] rev_induct 1);
paulson@5316
   395
by Auto_tac;
nipkow@4935
   396
bind_thm ("rev_exhaust",
nipkow@4935
   397
  impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp)))));
nipkow@4935
   398
nipkow@2608
   399
nipkow@3465
   400
(** set **)
paulson@1812
   401
nipkow@3467
   402
section "set";
nipkow@3467
   403
oheimb@5296
   404
qed_goal "finite_set" thy "finite (set xs)" 
oheimb@5296
   405
	(K [induct_tac "xs" 1, Auto_tac]);
oheimb@5296
   406
Addsimps[finite_set];
oheimb@5296
   407
AddSIs[finite_set];
oheimb@5296
   408
nipkow@4935
   409
Goal "set (xs@ys) = (set xs Un set ys)";
nipkow@3040
   410
by (induct_tac "xs" 1);
paulson@5316
   411
by Auto_tac;
paulson@3647
   412
qed "set_append";
paulson@3647
   413
Addsimps[set_append];
paulson@1812
   414
nipkow@4935
   415
Goal "set l <= set (x#l)";
paulson@5316
   416
by Auto_tac;
paulson@3647
   417
qed "set_subset_Cons";
paulson@1936
   418
nipkow@4935
   419
Goal "(set xs = {}) = (xs = [])";
paulson@3457
   420
by (induct_tac "xs" 1);
paulson@5316
   421
by Auto_tac;
paulson@3647
   422
qed "set_empty";
paulson@3647
   423
Addsimps [set_empty];
nipkow@2608
   424
nipkow@4935
   425
Goal "set(rev xs) = set(xs)";
paulson@3457
   426
by (induct_tac "xs" 1);
paulson@5316
   427
by Auto_tac;
paulson@3647
   428
qed "set_rev";
paulson@3647
   429
Addsimps [set_rev];
nipkow@2608
   430
nipkow@4935
   431
Goal "set(map f xs) = f``(set xs)";
paulson@3457
   432
by (induct_tac "xs" 1);
paulson@5316
   433
by Auto_tac;
paulson@3647
   434
qed "set_map";
paulson@3647
   435
Addsimps [set_map];
nipkow@2608
   436
nipkow@6433
   437
Goal "set(filter P xs) = {x. x : set xs & P x}";
paulson@6813
   438
by (induct_tac "xs" 1);
paulson@6813
   439
by Auto_tac;
nipkow@6433
   440
qed "set_filter";
nipkow@6433
   441
Addsimps [set_filter];
nipkow@6433
   442
(*
oheimb@5443
   443
Goal "(x : set (filter P xs)) = (x : set xs & P x)";
nipkow@4605
   444
by (induct_tac "xs" 1);
paulson@5316
   445
by Auto_tac;
nipkow@4605
   446
qed "in_set_filter";
nipkow@4605
   447
Addsimps [in_set_filter];
nipkow@6433
   448
*)
nipkow@6433
   449
Goal "set[i..j(] = {k. i <= k & k < j}";
paulson@6813
   450
by (induct_tac "j" 1);
paulson@6813
   451
by Auto_tac;
paulson@6813
   452
by (arith_tac 1);
nipkow@6433
   453
qed "set_upt";
nipkow@6433
   454
Addsimps [set_upt];
nipkow@6433
   455
nipkow@6433
   456
Goal "!i < size xs. set(xs[i := x]) <= insert x (set xs)";
paulson@6813
   457
by (induct_tac "xs" 1);
paulson@6813
   458
 by (Simp_tac 1);
paulson@6813
   459
by (asm_simp_tac (simpset() addsplits [nat.split]) 1);
paulson@6813
   460
by (Blast_tac 1);
nipkow@6433
   461
qed_spec_mp "set_list_update_subset";
nipkow@4605
   462
nipkow@5272
   463
Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)";
paulson@5318
   464
by (induct_tac "xs" 1);
paulson@5318
   465
 by (Simp_tac 1);
paulson@5318
   466
by (Asm_simp_tac 1);
paulson@5318
   467
by (rtac iffI 1);
paulson@5318
   468
by (blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1);
paulson@5318
   469
by (REPEAT(etac exE 1));
paulson@5318
   470
by (exhaust_tac "ys" 1);
paulson@5316
   471
by Auto_tac;
nipkow@5272
   472
qed "in_set_conv_decomp";
nipkow@5272
   473
nipkow@5272
   474
(* eliminate `lists' in favour of `set' *)
nipkow@5272
   475
nipkow@5272
   476
Goal "(xs : lists A) = (!x : set xs. x : A)";
paulson@5318
   477
by (induct_tac "xs" 1);
paulson@5316
   478
by Auto_tac;
nipkow@5272
   479
qed "in_lists_conv_set";
nipkow@5272
   480
nipkow@5272
   481
bind_thm("in_listsD",in_lists_conv_set RS iffD1);
nipkow@5272
   482
AddSDs [in_listsD];
nipkow@5272
   483
bind_thm("in_listsI",in_lists_conv_set RS iffD2);
nipkow@5272
   484
AddSIs [in_listsI];
paulson@1812
   485
oheimb@5518
   486
(** mem **)
oheimb@5518
   487
 
oheimb@5518
   488
section "mem";
oheimb@5518
   489
oheimb@5518
   490
Goal "(x mem xs) = (x: set xs)";
oheimb@5518
   491
by (induct_tac "xs" 1);
oheimb@5518
   492
by Auto_tac;
oheimb@5518
   493
qed "set_mem_eq";
oheimb@5518
   494
oheimb@5518
   495
clasohm@923
   496
(** list_all **)
clasohm@923
   497
nipkow@3467
   498
section "list_all";
nipkow@3467
   499
oheimb@5518
   500
Goal "list_all P xs = (!x:set xs. P x)";
oheimb@5518
   501
by (induct_tac "xs" 1);
oheimb@5518
   502
by Auto_tac;
oheimb@5518
   503
qed "list_all_conv";
oheimb@5518
   504
oheimb@5443
   505
Goal "list_all P (xs@ys) = (list_all P xs & list_all P ys)";
nipkow@3040
   506
by (induct_tac "xs" 1);
paulson@5316
   507
by Auto_tac;
nipkow@2512
   508
qed "list_all_append";
nipkow@2512
   509
Addsimps [list_all_append];
clasohm@923
   510
clasohm@923
   511
nipkow@2608
   512
(** filter **)
clasohm@923
   513
nipkow@3467
   514
section "filter";
nipkow@3467
   515
nipkow@4935
   516
Goal "filter P (xs@ys) = filter P xs @ filter P ys";
paulson@3457
   517
by (induct_tac "xs" 1);
paulson@5316
   518
by Auto_tac;
nipkow@2608
   519
qed "filter_append";
nipkow@2608
   520
Addsimps [filter_append];
nipkow@2608
   521
nipkow@4935
   522
Goal "filter (%x. True) xs = xs";
nipkow@4605
   523
by (induct_tac "xs" 1);
paulson@5316
   524
by Auto_tac;
nipkow@4605
   525
qed "filter_True";
nipkow@4605
   526
Addsimps [filter_True];
nipkow@4605
   527
nipkow@4935
   528
Goal "filter (%x. False) xs = []";
nipkow@4605
   529
by (induct_tac "xs" 1);
paulson@5316
   530
by Auto_tac;
nipkow@4605
   531
qed "filter_False";
nipkow@4605
   532
Addsimps [filter_False];
nipkow@4605
   533
nipkow@4935
   534
Goal "length (filter P xs) <= length xs";
paulson@3457
   535
by (induct_tac "xs" 1);
paulson@5316
   536
by Auto_tac;
nipkow@4605
   537
qed "length_filter";
oheimb@5443
   538
Addsimps[length_filter];
nipkow@2608
   539
oheimb@5443
   540
Goal "set (filter P xs) <= set xs";
oheimb@5443
   541
by Auto_tac;
oheimb@5443
   542
qed "filter_is_subset";
oheimb@5443
   543
Addsimps [filter_is_subset];
oheimb@5443
   544
nipkow@2608
   545
nipkow@3467
   546
section "concat";
nipkow@3467
   547
nipkow@4935
   548
Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
nipkow@3040
   549
by (induct_tac "xs" 1);
paulson@5316
   550
by Auto_tac;
nipkow@2608
   551
qed"concat_append";
nipkow@2608
   552
Addsimps [concat_append];
nipkow@2512
   553
nipkow@4935
   554
Goal "(concat xss = []) = (!xs:set xss. xs=[])";
wenzelm@4423
   555
by (induct_tac "xss" 1);
paulson@5316
   556
by Auto_tac;
nipkow@3896
   557
qed "concat_eq_Nil_conv";
nipkow@3896
   558
AddIffs [concat_eq_Nil_conv];
nipkow@3896
   559
nipkow@4935
   560
Goal "([] = concat xss) = (!xs:set xss. xs=[])";
wenzelm@4423
   561
by (induct_tac "xss" 1);
paulson@5316
   562
by Auto_tac;
nipkow@3896
   563
qed "Nil_eq_concat_conv";
nipkow@3896
   564
AddIffs [Nil_eq_concat_conv];
nipkow@3896
   565
nipkow@4935
   566
Goal  "set(concat xs) = Union(set `` set xs)";
nipkow@3467
   567
by (induct_tac "xs" 1);
paulson@5316
   568
by Auto_tac;
paulson@3647
   569
qed"set_concat";
paulson@3647
   570
Addsimps [set_concat];
nipkow@3467
   571
nipkow@4935
   572
Goal "map f (concat xs) = concat (map (map f) xs)"; 
nipkow@3467
   573
by (induct_tac "xs" 1);
paulson@5316
   574
by Auto_tac;
nipkow@3467
   575
qed "map_concat";
nipkow@3467
   576
nipkow@4935
   577
Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
nipkow@3467
   578
by (induct_tac "xs" 1);
paulson@5316
   579
by Auto_tac;
nipkow@3467
   580
qed"filter_concat"; 
nipkow@3467
   581
nipkow@4935
   582
Goal "rev(concat xs) = concat (map rev (rev xs))";
nipkow@3467
   583
by (induct_tac "xs" 1);
paulson@5316
   584
by Auto_tac;
nipkow@2608
   585
qed "rev_concat";
clasohm@923
   586
clasohm@923
   587
(** nth **)
clasohm@923
   588
nipkow@3467
   589
section "nth";
nipkow@3467
   590
pusch@6408
   591
Goal "(x#xs)!0 = x";
pusch@6408
   592
by Auto_tac;
pusch@6408
   593
qed "nth_Cons_0";
pusch@6408
   594
Addsimps [nth_Cons_0];
nipkow@5644
   595
pusch@6408
   596
Goal "(x#xs)!(Suc n) = xs!n";
pusch@6408
   597
by Auto_tac;
pusch@6408
   598
qed "nth_Cons_Suc";
pusch@6408
   599
Addsimps [nth_Cons_Suc];
pusch@6408
   600
pusch@6408
   601
Delsimps (thms "nth.simps");
pusch@6408
   602
pusch@6408
   603
Goal "!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
pusch@6408
   604
by (induct_tac "xs" 1);
paulson@3457
   605
 by (Asm_simp_tac 1);
paulson@3457
   606
 by (rtac allI 1);
pusch@6408
   607
 by (exhaust_tac "n" 1);
paulson@5316
   608
  by Auto_tac;
nipkow@2608
   609
qed_spec_mp "nth_append";
nipkow@2608
   610
nipkow@4935
   611
Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
nipkow@3040
   612
by (induct_tac "xs" 1);
nipkow@1301
   613
(* case [] *)
nipkow@1301
   614
by (Asm_full_simp_tac 1);
nipkow@1301
   615
(* case x#xl *)
nipkow@1301
   616
by (rtac allI 1);
berghofe@5183
   617
by (induct_tac "n" 1);
paulson@5316
   618
by Auto_tac;
nipkow@1485
   619
qed_spec_mp "nth_map";
nipkow@1301
   620
Addsimps [nth_map];
nipkow@1301
   621
oheimb@5518
   622
Goal "!n. n < length xs --> Ball (set xs) P --> P(xs!n)";
nipkow@3040
   623
by (induct_tac "xs" 1);
nipkow@1301
   624
(* case [] *)
nipkow@1301
   625
by (Simp_tac 1);
nipkow@1301
   626
(* case x#xl *)
nipkow@1301
   627
by (rtac allI 1);
berghofe@5183
   628
by (induct_tac "n" 1);
paulson@5316
   629
by Auto_tac;
oheimb@5518
   630
qed_spec_mp "list_ball_nth";
nipkow@1301
   631
oheimb@5518
   632
Goal "!n. n < length xs --> xs!n : set xs";
nipkow@3040
   633
by (induct_tac "xs" 1);
nipkow@1301
   634
(* case [] *)
nipkow@1301
   635
by (Simp_tac 1);
nipkow@1301
   636
(* case x#xl *)
nipkow@1301
   637
by (rtac allI 1);
berghofe@5183
   638
by (induct_tac "n" 1);
nipkow@1301
   639
(* case 0 *)
nipkow@1301
   640
by (Asm_full_simp_tac 1);
nipkow@1301
   641
(* case Suc x *)
nipkow@4686
   642
by (Asm_full_simp_tac 1);
nipkow@1485
   643
qed_spec_mp "nth_mem";
nipkow@1301
   644
Addsimps [nth_mem];
nipkow@1301
   645
oheimb@5518
   646
nipkow@5077
   647
(** list update **)
nipkow@5077
   648
nipkow@5077
   649
section "list update";
nipkow@5077
   650
nipkow@5077
   651
Goal "!i. length(xs[i:=x]) = length xs";
nipkow@5077
   652
by (induct_tac "xs" 1);
nipkow@5077
   653
by (Simp_tac 1);
berghofe@5183
   654
by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@5077
   655
qed_spec_mp "length_list_update";
nipkow@5077
   656
Addsimps [length_list_update];
nipkow@5077
   657
nipkow@5644
   658
Goal "!i j. i < length xs  --> (xs[i:=x])!j = (if i=j then x else xs!j)";
paulson@6162
   659
by (induct_tac "xs" 1);
paulson@6162
   660
 by (Simp_tac 1);
paulson@6162
   661
by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
nipkow@5644
   662
qed_spec_mp "nth_list_update";
nipkow@5644
   663
nipkow@6433
   664
Goal "!i. i < size xs --> xs[i:=x, i:=y] = xs[i:=y]";
paulson@6813
   665
by (induct_tac "xs" 1);
paulson@6813
   666
 by (Simp_tac 1);
paulson@6813
   667
by (asm_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@6433
   668
qed_spec_mp "list_update_overwrite";
nipkow@6433
   669
Addsimps [list_update_overwrite];
nipkow@6433
   670
nipkow@6433
   671
Goal "!i < length xs. (xs[i := x] = xs) = (xs!i = x)";
paulson@6813
   672
by (induct_tac "xs" 1);
paulson@6813
   673
 by (Simp_tac 1);
paulson@6813
   674
by (simp_tac (simpset() addsplits [nat.split]) 1);
paulson@6813
   675
by (Blast_tac 1);
nipkow@6433
   676
qed_spec_mp "list_update_same_conv";
nipkow@6433
   677
nipkow@5077
   678
nipkow@3896
   679
(** last & butlast **)
nipkow@1327
   680
nipkow@5644
   681
section "last / butlast";
nipkow@5644
   682
nipkow@4935
   683
Goal "last(xs@[x]) = x";
wenzelm@4423
   684
by (induct_tac "xs" 1);
paulson@5316
   685
by Auto_tac;
nipkow@3896
   686
qed "last_snoc";
nipkow@3896
   687
Addsimps [last_snoc];
nipkow@3896
   688
nipkow@4935
   689
Goal "butlast(xs@[x]) = xs";
wenzelm@4423
   690
by (induct_tac "xs" 1);
paulson@5316
   691
by Auto_tac;
nipkow@3896
   692
qed "butlast_snoc";
nipkow@3896
   693
Addsimps [butlast_snoc];
nipkow@3896
   694
nipkow@4935
   695
Goal "length(butlast xs) = length xs - 1";
nipkow@4935
   696
by (res_inst_tac [("xs","xs")] rev_induct 1);
paulson@5316
   697
by Auto_tac;
nipkow@4643
   698
qed "length_butlast";
nipkow@4643
   699
Addsimps [length_butlast];
nipkow@4643
   700
paulson@5278
   701
Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
wenzelm@4423
   702
by (induct_tac "xs" 1);
paulson@5316
   703
by Auto_tac;
nipkow@3896
   704
qed_spec_mp "butlast_append";
nipkow@3896
   705
nipkow@4935
   706
Goal "x:set(butlast xs) --> x:set xs";
wenzelm@4423
   707
by (induct_tac "xs" 1);
paulson@5316
   708
by Auto_tac;
nipkow@3896
   709
qed_spec_mp "in_set_butlastD";
nipkow@3896
   710
paulson@5448
   711
Goal "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))";
paulson@5448
   712
by (auto_tac (claset() addDs [in_set_butlastD],
paulson@5448
   713
	      simpset() addsimps [butlast_append]));
paulson@5448
   714
qed "in_set_butlast_appendI";
nipkow@3902
   715
nipkow@2608
   716
(** take  & drop **)
nipkow@2608
   717
section "take & drop";
nipkow@1327
   718
nipkow@4935
   719
Goal "take 0 xs = []";
nipkow@3040
   720
by (induct_tac "xs" 1);
paulson@5316
   721
by Auto_tac;
nipkow@1327
   722
qed "take_0";
nipkow@1327
   723
nipkow@4935
   724
Goal "drop 0 xs = xs";
nipkow@3040
   725
by (induct_tac "xs" 1);
paulson@5316
   726
by Auto_tac;
nipkow@2608
   727
qed "drop_0";
nipkow@2608
   728
nipkow@4935
   729
Goal "take (Suc n) (x#xs) = x # take n xs";
paulson@1552
   730
by (Simp_tac 1);
nipkow@1419
   731
qed "take_Suc_Cons";
nipkow@1327
   732
nipkow@4935
   733
Goal "drop (Suc n) (x#xs) = drop n xs";
nipkow@2608
   734
by (Simp_tac 1);
nipkow@2608
   735
qed "drop_Suc_Cons";
nipkow@2608
   736
nipkow@2608
   737
Delsimps [take_Cons,drop_Cons];
nipkow@2608
   738
Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
nipkow@2608
   739
nipkow@4935
   740
Goal "!xs. length(take n xs) = min (length xs) n";
berghofe@5183
   741
by (induct_tac "n" 1);
paulson@5316
   742
 by Auto_tac;
paulson@3457
   743
by (exhaust_tac "xs" 1);
paulson@5316
   744
 by Auto_tac;
nipkow@2608
   745
qed_spec_mp "length_take";
nipkow@2608
   746
Addsimps [length_take];
clasohm@923
   747
nipkow@4935
   748
Goal "!xs. length(drop n xs) = (length xs - n)";
berghofe@5183
   749
by (induct_tac "n" 1);
paulson@5316
   750
 by Auto_tac;
paulson@3457
   751
by (exhaust_tac "xs" 1);
paulson@5316
   752
 by Auto_tac;
nipkow@2608
   753
qed_spec_mp "length_drop";
nipkow@2608
   754
Addsimps [length_drop];
nipkow@2608
   755
nipkow@4935
   756
Goal "!xs. length xs <= n --> take n xs = xs";
berghofe@5183
   757
by (induct_tac "n" 1);
paulson@5316
   758
 by Auto_tac;
paulson@3457
   759
by (exhaust_tac "xs" 1);
paulson@5316
   760
 by Auto_tac;
nipkow@2608
   761
qed_spec_mp "take_all";
clasohm@923
   762
nipkow@4935
   763
Goal "!xs. length xs <= n --> drop n xs = []";
berghofe@5183
   764
by (induct_tac "n" 1);
paulson@5316
   765
 by Auto_tac;
paulson@3457
   766
by (exhaust_tac "xs" 1);
paulson@5316
   767
 by Auto_tac;
nipkow@2608
   768
qed_spec_mp "drop_all";
nipkow@2608
   769
paulson@5278
   770
Goal "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
berghofe@5183
   771
by (induct_tac "n" 1);
paulson@5316
   772
 by Auto_tac;
paulson@3457
   773
by (exhaust_tac "xs" 1);
paulson@5316
   774
 by Auto_tac;
nipkow@2608
   775
qed_spec_mp "take_append";
nipkow@2608
   776
Addsimps [take_append];
nipkow@2608
   777
nipkow@4935
   778
Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
berghofe@5183
   779
by (induct_tac "n" 1);
paulson@5316
   780
 by Auto_tac;
paulson@3457
   781
by (exhaust_tac "xs" 1);
paulson@5316
   782
 by Auto_tac;
nipkow@2608
   783
qed_spec_mp "drop_append";
nipkow@2608
   784
Addsimps [drop_append];
nipkow@2608
   785
nipkow@4935
   786
Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
berghofe@5183
   787
by (induct_tac "m" 1);
paulson@5316
   788
 by Auto_tac;
paulson@3457
   789
by (exhaust_tac "xs" 1);
paulson@5316
   790
 by Auto_tac;
berghofe@5183
   791
by (exhaust_tac "na" 1);
paulson@5316
   792
 by Auto_tac;
nipkow@2608
   793
qed_spec_mp "take_take";
nipkow@2608
   794
nipkow@4935
   795
Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
berghofe@5183
   796
by (induct_tac "m" 1);
paulson@5316
   797
 by Auto_tac;
paulson@3457
   798
by (exhaust_tac "xs" 1);
paulson@5316
   799
 by Auto_tac;
nipkow@2608
   800
qed_spec_mp "drop_drop";
clasohm@923
   801
nipkow@4935
   802
Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
berghofe@5183
   803
by (induct_tac "m" 1);
paulson@5316
   804
 by Auto_tac;
paulson@3457
   805
by (exhaust_tac "xs" 1);
paulson@5316
   806
 by Auto_tac;
nipkow@2608
   807
qed_spec_mp "take_drop";
nipkow@2608
   808
paulson@6813
   809
Goal "!xs. take n xs @ drop n xs = xs";
paulson@6813
   810
by (induct_tac "n" 1);
paulson@6813
   811
 by Auto_tac;
paulson@6813
   812
by (exhaust_tac "xs" 1);
paulson@6813
   813
 by Auto_tac;
paulson@6813
   814
qed_spec_mp "append_take_drop_id";
paulson@6813
   815
nipkow@4935
   816
Goal "!xs. take n (map f xs) = map f (take n xs)"; 
berghofe@5183
   817
by (induct_tac "n" 1);
paulson@5316
   818
 by Auto_tac;
paulson@3457
   819
by (exhaust_tac "xs" 1);
paulson@5316
   820
 by Auto_tac;
nipkow@2608
   821
qed_spec_mp "take_map"; 
nipkow@2608
   822
nipkow@4935
   823
Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
berghofe@5183
   824
by (induct_tac "n" 1);
paulson@5316
   825
 by Auto_tac;
paulson@3457
   826
by (exhaust_tac "xs" 1);
paulson@5316
   827
 by Auto_tac;
nipkow@2608
   828
qed_spec_mp "drop_map";
nipkow@2608
   829
nipkow@4935
   830
Goal "!n i. i < n --> (take n xs)!i = xs!i";
paulson@3457
   831
by (induct_tac "xs" 1);
paulson@5316
   832
 by Auto_tac;
paulson@3457
   833
by (exhaust_tac "n" 1);
paulson@3457
   834
 by (Blast_tac 1);
paulson@3457
   835
by (exhaust_tac "i" 1);
paulson@5316
   836
 by Auto_tac;
nipkow@2608
   837
qed_spec_mp "nth_take";
nipkow@2608
   838
Addsimps [nth_take];
clasohm@923
   839
nipkow@4935
   840
Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
berghofe@5183
   841
by (induct_tac "n" 1);
paulson@5316
   842
 by Auto_tac;
paulson@3457
   843
by (exhaust_tac "xs" 1);
paulson@5316
   844
 by Auto_tac;
nipkow@2608
   845
qed_spec_mp "nth_drop";
nipkow@2608
   846
Addsimps [nth_drop];
nipkow@2608
   847
nipkow@2608
   848
(** takeWhile & dropWhile **)
nipkow@2608
   849
nipkow@3467
   850
section "takeWhile & dropWhile";
nipkow@3467
   851
nipkow@4935
   852
Goal "takeWhile P xs @ dropWhile P xs = xs";
nipkow@3586
   853
by (induct_tac "xs" 1);
paulson@5316
   854
by Auto_tac;
nipkow@3586
   855
qed "takeWhile_dropWhile_id";
nipkow@3586
   856
Addsimps [takeWhile_dropWhile_id];
nipkow@3586
   857
nipkow@4935
   858
Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
paulson@3457
   859
by (induct_tac "xs" 1);
paulson@5316
   860
by Auto_tac;
nipkow@2608
   861
bind_thm("takeWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   862
Addsimps [takeWhile_append1];
clasohm@923
   863
nipkow@4935
   864
Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
paulson@3457
   865
by (induct_tac "xs" 1);
paulson@5316
   866
by Auto_tac;
nipkow@2608
   867
bind_thm("takeWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   868
Addsimps [takeWhile_append2];
lcp@1169
   869
nipkow@4935
   870
Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
paulson@3457
   871
by (induct_tac "xs" 1);
paulson@5316
   872
by Auto_tac;
nipkow@2608
   873
bind_thm("dropWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   874
Addsimps [dropWhile_append1];
nipkow@2608
   875
nipkow@4935
   876
Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
paulson@3457
   877
by (induct_tac "xs" 1);
paulson@5316
   878
by Auto_tac;
nipkow@2608
   879
bind_thm("dropWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   880
Addsimps [dropWhile_append2];
nipkow@2608
   881
nipkow@4935
   882
Goal "x:set(takeWhile P xs) --> x:set xs & P x";
paulson@3457
   883
by (induct_tac "xs" 1);
paulson@5316
   884
by Auto_tac;
paulson@3647
   885
qed_spec_mp"set_take_whileD";
nipkow@2608
   886
nipkow@6306
   887
(** zip **)
nipkow@6306
   888
section "zip";
nipkow@6306
   889
nipkow@6306
   890
Goal "zip [] ys = []";
paulson@6813
   891
by (induct_tac "ys" 1);
nipkow@6306
   892
by Auto_tac;
nipkow@6306
   893
qed "zip_Nil";
nipkow@6306
   894
Addsimps [zip_Nil];
nipkow@6306
   895
nipkow@6306
   896
Goal "zip (x#xs) (y#ys) = (x,y)#zip xs ys";
paulson@6813
   897
by (Simp_tac 1);
nipkow@6306
   898
qed "zip_Cons_Cons";
nipkow@6306
   899
Addsimps [zip_Cons_Cons];
nipkow@6306
   900
nipkow@6306
   901
Delsimps(tl (thms"zip.simps"));
nipkow@4605
   902
nipkow@5272
   903
nipkow@5272
   904
(** foldl **)
nipkow@5272
   905
section "foldl";
nipkow@5272
   906
nipkow@5272
   907
Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys";
paulson@5318
   908
by (induct_tac "xs" 1);
paulson@5316
   909
by Auto_tac;
nipkow@5272
   910
qed_spec_mp "foldl_append";
nipkow@5272
   911
Addsimps [foldl_append];
nipkow@5272
   912
nipkow@5272
   913
(* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
nipkow@5272
   914
   because it requires an additional transitivity step
nipkow@5272
   915
*)
nipkow@5272
   916
Goal "!n::nat. m <= n --> m <= foldl op+ n ns";
paulson@5318
   917
by (induct_tac "ns" 1);
nipkow@6058
   918
by Auto_tac;
nipkow@5272
   919
qed_spec_mp "start_le_sum";
nipkow@5272
   920
nipkow@5272
   921
Goal "n : set ns ==> n <= foldl op+ 0 ns";
oheimb@5758
   922
by (force_tac (claset() addIs [start_le_sum],
oheimb@5758
   923
              simpset() addsimps [in_set_conv_decomp]) 1);
nipkow@5272
   924
qed "elem_le_sum";
nipkow@5272
   925
nipkow@5272
   926
Goal "!m. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))";
paulson@5318
   927
by (induct_tac "ns" 1);
paulson@5316
   928
by Auto_tac;
nipkow@5272
   929
qed_spec_mp "sum_eq_0_conv";
nipkow@5272
   930
AddIffs [sum_eq_0_conv];
nipkow@5272
   931
nipkow@5425
   932
(** upto **)
nipkow@5425
   933
nipkow@5427
   934
(* Does not terminate! *)
nipkow@5427
   935
Goal "[i..j(] = (if i<j then i#[Suc i..j(] else [])";
paulson@6162
   936
by (induct_tac "j" 1);
nipkow@5427
   937
by Auto_tac;
nipkow@5427
   938
qed "upt_rec";
nipkow@5425
   939
nipkow@5427
   940
Goal "j<=i ==> [i..j(] = []";
paulson@6162
   941
by (stac upt_rec 1);
paulson@6162
   942
by (Asm_simp_tac 1);
nipkow@5427
   943
qed "upt_conv_Nil";
nipkow@5427
   944
Addsimps [upt_conv_Nil];
nipkow@5427
   945
nipkow@5427
   946
Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]";
nipkow@5427
   947
by (Asm_simp_tac 1);
nipkow@5427
   948
qed "upt_Suc";
nipkow@5427
   949
nipkow@5427
   950
Goal "i<j ==> [i..j(] = i#[Suc i..j(]";
paulson@6162
   951
by (rtac trans 1);
paulson@6162
   952
by (stac upt_rec 1);
paulson@6162
   953
by (rtac refl 2);
nipkow@5427
   954
by (Asm_simp_tac 1);
nipkow@5427
   955
qed "upt_conv_Cons";
nipkow@5427
   956
nipkow@5427
   957
Goal "length [i..j(] = j-i";
paulson@6162
   958
by (induct_tac "j" 1);
nipkow@5427
   959
 by (Simp_tac 1);
paulson@6162
   960
by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
nipkow@5427
   961
qed "length_upt";
nipkow@5427
   962
Addsimps [length_upt];
nipkow@5425
   963
nipkow@5427
   964
Goal "i+k < j --> [i..j(] ! k = i+k";
paulson@6162
   965
by (induct_tac "j" 1);
paulson@6162
   966
 by (Simp_tac 1);
paulson@6162
   967
by (asm_simp_tac (simpset() addsimps [nth_append,less_diff_conv]@add_ac) 1);
paulson@6162
   968
by (Clarify_tac 1);
paulson@6162
   969
by (subgoal_tac "n=i+k" 1);
paulson@6162
   970
 by (Asm_simp_tac 2);
paulson@6162
   971
by (Asm_simp_tac 1);
nipkow@5427
   972
qed_spec_mp "nth_upt";
nipkow@5427
   973
Addsimps [nth_upt];
nipkow@5425
   974
nipkow@6433
   975
Goal "!i. i+m <= n --> take m [i..n(] = [i..i+m(]";
paulson@6813
   976
by (induct_tac "m" 1);
paulson@6813
   977
 by (Simp_tac 1);
paulson@6813
   978
by (Clarify_tac 1);
paulson@6813
   979
by (stac upt_rec 1);
paulson@6813
   980
by (rtac sym 1);
paulson@6813
   981
by (stac upt_rec 1);
paulson@6813
   982
by (asm_simp_tac (simpset() delsimps (thms"upt.simps")) 1);
nipkow@6433
   983
qed_spec_mp "take_upt";
nipkow@6433
   984
Addsimps [take_upt];
nipkow@6433
   985
nipkow@6433
   986
Goal "!m i. i < n-m --> (map f [m..n(]) ! i = f(m+i)";
paulson@6813
   987
by (induct_tac "n" 1);
paulson@6813
   988
 by (Simp_tac 1);
paulson@6813
   989
by (Clarify_tac 1);
paulson@6813
   990
by (subgoal_tac "m < Suc n" 1);
paulson@6813
   991
 by (arith_tac 2);
paulson@6813
   992
by (stac upt_rec 1);
paulson@6813
   993
by (asm_simp_tac (simpset() delsplits [split_if]) 1);
paulson@6813
   994
by (split_tac [split_if] 1);
paulson@6813
   995
by (rtac conjI 1);
paulson@6813
   996
 by (simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
paulson@6813
   997
 by (simp_tac (simpset() addsimps [nth_append] addsplits [nat.split]) 1);
paulson@6813
   998
 by (Clarify_tac 1);
paulson@6813
   999
 by (rtac conjI 1);
paulson@6813
  1000
  by (Clarify_tac 1);
paulson@6813
  1001
  by (subgoal_tac "Suc(m+nat) < n" 1);
paulson@6813
  1002
   by (arith_tac 2);
paulson@6813
  1003
  by (Asm_simp_tac 1);
paulson@6813
  1004
 by (Clarify_tac 1);
paulson@6813
  1005
 by (subgoal_tac "n = Suc(m+nat)" 1);
paulson@6813
  1006
  by (arith_tac 2);
paulson@6813
  1007
 by (Asm_simp_tac 1);
paulson@6813
  1008
by (simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
paulson@6813
  1009
by (arith_tac 1);
nipkow@6433
  1010
qed_spec_mp "nth_map_upt";
nipkow@6433
  1011
paulson@6813
  1012
Goal "ALL xs ys. k <= length xs --> k <= length ys -->  \
paulson@6813
  1013
\        (ALL i. i < k --> xs!i = ys!i)  \
paulson@6813
  1014
\     --> take k xs = take k ys";
paulson@6813
  1015
by (induct_tac "k" 1);
paulson@6813
  1016
by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq_0_disj, 
paulson@6813
  1017
						all_conj_distrib])));
paulson@6813
  1018
by (Clarify_tac 1);
paulson@6813
  1019
(*Both lists must be non-empty*)
paulson@6813
  1020
by (exhaust_tac "xs" 1);
paulson@6813
  1021
by (exhaust_tac "ys" 2);
paulson@6813
  1022
by (ALLGOALS Clarify_tac);
paulson@6813
  1023
(*prenexing's needed, not miniscoping*)
paulson@6813
  1024
by (ALLGOALS (full_simp_tac (simpset() addsimps (all_simps RL [sym])  
paulson@6813
  1025
                                       delsimps (all_simps))));
paulson@6813
  1026
by (Blast_tac 1);
paulson@6813
  1027
qed_spec_mp "nth_take_lemma";
paulson@6813
  1028
paulson@6813
  1029
Goal "[| length xs = length ys;  \
paulson@6813
  1030
\        ALL i. i < length xs --> xs!i = ys!i |]  \
paulson@6813
  1031
\     ==> xs = ys";
paulson@6813
  1032
by (forward_tac [[le_refl, eq_imp_le] MRS nth_take_lemma] 1);
paulson@6813
  1033
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [take_all])));
paulson@6813
  1034
qed_spec_mp "nth_equalityI";
paulson@6813
  1035
paulson@6813
  1036
(*The famous take-lemma*)
paulson@6813
  1037
Goal "(ALL i. take i xs = take i ys) ==> xs = ys";
paulson@6813
  1038
by (dres_inst_tac [("x", "max (length xs) (length ys)")] spec 1);
paulson@6813
  1039
by (full_simp_tac (simpset() addsimps [le_max_iff_disj, take_all]) 1);
paulson@6813
  1040
qed_spec_mp "take_equalityI";
paulson@6813
  1041
nipkow@5272
  1042
nipkow@4605
  1043
(** nodups & remdups **)
nipkow@4605
  1044
section "nodups & remdups";
nipkow@4605
  1045
nipkow@4935
  1046
Goal "set(remdups xs) = set xs";
nipkow@4605
  1047
by (induct_tac "xs" 1);
nipkow@4605
  1048
 by (Simp_tac 1);
nipkow@4686
  1049
by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
nipkow@4605
  1050
qed "set_remdups";
nipkow@4605
  1051
Addsimps [set_remdups];
nipkow@4605
  1052
nipkow@4935
  1053
Goal "nodups(remdups xs)";
nipkow@4605
  1054
by (induct_tac "xs" 1);
paulson@5316
  1055
by Auto_tac;
nipkow@4605
  1056
qed "nodups_remdups";
nipkow@4605
  1057
nipkow@4935
  1058
Goal "nodups xs --> nodups (filter P xs)";
nipkow@4605
  1059
by (induct_tac "xs" 1);
paulson@5316
  1060
by Auto_tac;
nipkow@4605
  1061
qed_spec_mp "nodups_filter";
nipkow@4605
  1062
nipkow@3589
  1063
(** replicate **)
nipkow@3589
  1064
section "replicate";
nipkow@3589
  1065
nipkow@6794
  1066
Goal "length(replicate n x) = n";
paulson@6813
  1067
by (induct_tac "n" 1);
paulson@6813
  1068
by Auto_tac;
nipkow@6794
  1069
qed "length_replicate";
nipkow@6794
  1070
Addsimps [length_replicate];
nipkow@6794
  1071
nipkow@6794
  1072
Goal "map f (replicate n x) = replicate n (f x)";
nipkow@6794
  1073
by (induct_tac "n" 1);
paulson@6813
  1074
by Auto_tac;
nipkow@6794
  1075
qed "map_replicate";
nipkow@6794
  1076
Addsimps [map_replicate];
nipkow@6794
  1077
nipkow@6794
  1078
Goal "(replicate n x) @ (x#xs) = x # replicate n x @ xs";
nipkow@6794
  1079
by (induct_tac "n" 1);
paulson@6813
  1080
by Auto_tac;
nipkow@6794
  1081
qed "replicate_app_Cons_same";
nipkow@6794
  1082
nipkow@6794
  1083
Goal "rev(replicate n x) = replicate n x";
nipkow@6794
  1084
by (induct_tac "n" 1);
paulson@6813
  1085
 by (Simp_tac 1);
nipkow@6794
  1086
by (asm_simp_tac (simpset() addsimps [replicate_app_Cons_same]) 1);
nipkow@6794
  1087
qed "rev_replicate";
nipkow@6794
  1088
Addsimps [rev_replicate];
nipkow@6794
  1089
nipkow@6794
  1090
Goal"n ~= 0 --> hd(replicate n x) = x";
nipkow@6794
  1091
by (induct_tac "n" 1);
paulson@6813
  1092
by Auto_tac;
nipkow@6794
  1093
qed_spec_mp "hd_replicate";
nipkow@6794
  1094
Addsimps [hd_replicate];
nipkow@6794
  1095
nipkow@6794
  1096
Goal "n ~= 0 --> tl(replicate n x) = replicate (n-1) x";
nipkow@6794
  1097
by (induct_tac "n" 1);
paulson@6813
  1098
by Auto_tac;
nipkow@6794
  1099
qed_spec_mp "tl_replicate";
nipkow@6794
  1100
Addsimps [tl_replicate];
nipkow@6794
  1101
nipkow@6794
  1102
Goal "n ~= 0 --> last(replicate n x) = x";
nipkow@6794
  1103
by (induct_tac "n" 1);
paulson@6813
  1104
by Auto_tac;
nipkow@6794
  1105
qed_spec_mp "last_replicate";
nipkow@6794
  1106
Addsimps [last_replicate];
nipkow@6794
  1107
nipkow@6794
  1108
Goal "!i. i<n --> (replicate n x)!i = x";
paulson@6813
  1109
by (induct_tac "n" 1);
paulson@6813
  1110
 by (Simp_tac 1);
paulson@6813
  1111
by (asm_simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
nipkow@6794
  1112
qed_spec_mp "nth_replicate";
nipkow@6794
  1113
Addsimps [nth_replicate];
nipkow@6794
  1114
nipkow@4935
  1115
Goal "set(replicate (Suc n) x) = {x}";
wenzelm@4423
  1116
by (induct_tac "n" 1);
paulson@5316
  1117
by Auto_tac;
nipkow@3589
  1118
val lemma = result();
nipkow@3589
  1119
nipkow@5043
  1120
Goal "n ~= 0 ==> set(replicate n x) = {x}";
wenzelm@4423
  1121
by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
nipkow@3589
  1122
qed "set_replicate";
nipkow@3589
  1123
Addsimps [set_replicate];
nipkow@5162
  1124
nipkow@6794
  1125
Goal "replicate (n+m) x = replicate n x @ replicate m x";
nipkow@6794
  1126
by (induct_tac "n" 1);
nipkow@6794
  1127
by Auto_tac;
nipkow@6794
  1128
qed "replicate_add";
nipkow@5162
  1129
nipkow@5281
  1130
(*** Lexcicographic orderings on lists ***)
nipkow@5281
  1131
section"Lexcicographic orderings on lists";
nipkow@5281
  1132
nipkow@5281
  1133
Goal "wf r ==> wf(lexn r n)";
paulson@5318
  1134
by (induct_tac "n" 1);
paulson@5318
  1135
by (Simp_tac 1);
paulson@5318
  1136
by (Simp_tac 1);
paulson@5318
  1137
by (rtac wf_subset 1);
paulson@5318
  1138
by (rtac Int_lower1 2);
paulson@5318
  1139
by (rtac wf_prod_fun_image 1);
paulson@5318
  1140
by (rtac injI 2);
paulson@6813
  1141
by Auto_tac;
nipkow@5281
  1142
qed "wf_lexn";
nipkow@5281
  1143
nipkow@5281
  1144
Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n";
paulson@5318
  1145
by (induct_tac "n" 1);
paulson@6813
  1146
by Auto_tac;
nipkow@5281
  1147
qed_spec_mp "lexn_length";
nipkow@5281
  1148
nipkow@5281
  1149
Goalw [lex_def] "wf r ==> wf(lex r)";
paulson@5318
  1150
by (rtac wf_UN 1);
paulson@5318
  1151
by (blast_tac (claset() addIs [wf_lexn]) 1);
paulson@5318
  1152
by (Clarify_tac 1);
paulson@5318
  1153
by (rename_tac "m n" 1);
paulson@5318
  1154
by (subgoal_tac "m ~= n" 1);
paulson@5318
  1155
 by (Blast_tac 2);
paulson@5318
  1156
by (blast_tac (claset() addDs [lexn_length,not_sym]) 1);
nipkow@5281
  1157
qed "wf_lex";
nipkow@5281
  1158
AddSIs [wf_lex];
nipkow@5281
  1159
nipkow@5281
  1160
Goal
nipkow@5281
  1161
 "lexn r n = \
nipkow@5281
  1162
\ {(xs,ys). length xs = n & length ys = n & \
nipkow@5281
  1163
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5318
  1164
by (induct_tac "n" 1);
paulson@5318
  1165
 by (Simp_tac 1);
paulson@5318
  1166
 by (Blast_tac 1);
paulson@5641
  1167
by (asm_full_simp_tac (simpset() 
oheimb@5296
  1168
				addsimps [lex_prod_def]) 1);
paulson@5641
  1169
by (auto_tac (claset(), simpset()));
paulson@5318
  1170
  by (Blast_tac 1);
paulson@5318
  1171
 by (rename_tac "a xys x xs' y ys'" 1);
paulson@5318
  1172
 by (res_inst_tac [("x","a#xys")] exI 1);
paulson@5318
  1173
 by (Simp_tac 1);
paulson@5318
  1174
by (exhaust_tac "xys" 1);
paulson@5641
  1175
 by (ALLGOALS (asm_full_simp_tac (simpset())));
paulson@5318
  1176
by (Blast_tac 1);
nipkow@5281
  1177
qed "lexn_conv";
nipkow@5281
  1178
nipkow@5281
  1179
Goalw [lex_def]
nipkow@5281
  1180
 "lex r = \
nipkow@5281
  1181
\ {(xs,ys). length xs = length ys & \
nipkow@5281
  1182
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5641
  1183
by (force_tac (claset(), simpset() addsimps [lexn_conv]) 1);
nipkow@5281
  1184
qed "lex_conv";
nipkow@5281
  1185
nipkow@5281
  1186
Goalw [lexico_def] "wf r ==> wf(lexico r)";
paulson@5318
  1187
by (Blast_tac 1);
nipkow@5281
  1188
qed "wf_lexico";
nipkow@5281
  1189
AddSIs [wf_lexico];
nipkow@5281
  1190
nipkow@5281
  1191
Goalw
nipkow@5281
  1192
 [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def]
nipkow@5281
  1193
"lexico r = {(xs,ys). length xs < length ys | \
nipkow@5281
  1194
\                     length xs = length ys & (xs,ys) : lex r}";
paulson@5318
  1195
by (Simp_tac 1);
nipkow@5281
  1196
qed "lexico_conv";
nipkow@5281
  1197
nipkow@5283
  1198
Goal "([],ys) ~: lex r";
paulson@5318
  1199
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1200
qed "Nil_notin_lex";
nipkow@5283
  1201
nipkow@5283
  1202
Goal "(xs,[]) ~: lex r";
paulson@5318
  1203
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1204
qed "Nil2_notin_lex";
nipkow@5283
  1205
nipkow@5283
  1206
AddIffs [Nil_notin_lex,Nil2_notin_lex];
nipkow@5283
  1207
nipkow@5283
  1208
Goal "((x#xs,y#ys) : lex r) = \
nipkow@5283
  1209
\     ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)";
paulson@5318
  1210
by (simp_tac (simpset() addsimps [lex_conv]) 1);
paulson@5318
  1211
by (rtac iffI 1);
paulson@5318
  1212
 by (blast_tac (claset() addIs [Cons_eq_appendI]) 2);
paulson@5318
  1213
by (REPEAT(eresolve_tac [conjE, exE] 1));
paulson@5318
  1214
by (exhaust_tac "xys" 1);
paulson@5318
  1215
by (Asm_full_simp_tac 1);
paulson@5318
  1216
by (Asm_full_simp_tac 1);
paulson@5318
  1217
by (Blast_tac 1);
nipkow@5283
  1218
qed "Cons_in_lex";
nipkow@5283
  1219
AddIffs [Cons_in_lex];