src/HOL/List.ML
author pusch
Thu Apr 01 18:42:48 1999 +0200 (1999-04-01)
changeset 6408 5b443d6331ed
parent 6394 3d9fd50fcc43
child 6433 228237ec56e5
permissions -rw-r--r--
new definition for nth.
added warnings, if primrec definition is deleted from simpset.
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);
oheimb@5296
   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@5278
   336
Goal "(xs=ys) --> (!x. x : set ys --> f x = g x) --> map f xs = map g ys";
wenzelm@4423
   337
by (rtac impI 1);
wenzelm@4423
   338
by (hyp_subst_tac 1);
wenzelm@4423
   339
by (induct_tac "ys" 1);
paulson@5316
   340
by Auto_tac;
nipkow@3589
   341
val lemma = result();
nipkow@3589
   342
bind_thm("map_cong",impI RSN (2,allI RSN (2,lemma RS mp RS mp)));
nipkow@3589
   343
nipkow@4935
   344
Goal "(map f xs = []) = (xs = [])";
wenzelm@4423
   345
by (induct_tac "xs" 1);
paulson@5316
   346
by Auto_tac;
nipkow@3860
   347
qed "map_is_Nil_conv";
nipkow@3860
   348
AddIffs [map_is_Nil_conv];
nipkow@3860
   349
nipkow@4935
   350
Goal "([] = map f xs) = (xs = [])";
wenzelm@4423
   351
by (induct_tac "xs" 1);
paulson@5316
   352
by Auto_tac;
nipkow@3860
   353
qed "Nil_is_map_conv";
nipkow@3860
   354
AddIffs [Nil_is_map_conv];
nipkow@3860
   355
nipkow@3860
   356
lcp@1169
   357
(** rev **)
lcp@1169
   358
nipkow@3467
   359
section "rev";
nipkow@3467
   360
nipkow@4935
   361
Goal "rev(xs@ys) = rev(ys) @ rev(xs)";
nipkow@3040
   362
by (induct_tac "xs" 1);
paulson@5316
   363
by Auto_tac;
lcp@1169
   364
qed "rev_append";
nipkow@2512
   365
Addsimps[rev_append];
lcp@1169
   366
nipkow@4935
   367
Goal "rev(rev l) = l";
nipkow@3040
   368
by (induct_tac "l" 1);
paulson@5316
   369
by Auto_tac;
lcp@1169
   370
qed "rev_rev_ident";
nipkow@2512
   371
Addsimps[rev_rev_ident];
lcp@1169
   372
nipkow@4935
   373
Goal "(rev xs = []) = (xs = [])";
wenzelm@4423
   374
by (induct_tac "xs" 1);
paulson@5316
   375
by Auto_tac;
nipkow@3860
   376
qed "rev_is_Nil_conv";
nipkow@3860
   377
AddIffs [rev_is_Nil_conv];
nipkow@3860
   378
nipkow@4935
   379
Goal "([] = rev xs) = (xs = [])";
wenzelm@4423
   380
by (induct_tac "xs" 1);
paulson@5316
   381
by Auto_tac;
nipkow@3860
   382
qed "Nil_is_rev_conv";
nipkow@3860
   383
AddIffs [Nil_is_rev_conv];
nipkow@3860
   384
nipkow@4935
   385
val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
wenzelm@5132
   386
by (stac (rev_rev_ident RS sym) 1);
paulson@6162
   387
by (res_inst_tac [("list", "rev xs")] list.induct 1);
wenzelm@5132
   388
by (ALLGOALS Simp_tac);
wenzelm@5132
   389
by (resolve_tac prems 1);
wenzelm@5132
   390
by (eresolve_tac prems 1);
nipkow@4935
   391
qed "rev_induct";
nipkow@4935
   392
nipkow@5272
   393
fun rev_induct_tac xs = res_inst_tac [("xs",xs)] rev_induct;
nipkow@5272
   394
nipkow@4935
   395
Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
wenzelm@5132
   396
by (res_inst_tac [("xs","xs")] rev_induct 1);
paulson@5316
   397
by Auto_tac;
nipkow@4935
   398
bind_thm ("rev_exhaust",
nipkow@4935
   399
  impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp)))));
nipkow@4935
   400
nipkow@2608
   401
nipkow@3465
   402
(** set **)
paulson@1812
   403
nipkow@3467
   404
section "set";
nipkow@3467
   405
oheimb@5296
   406
qed_goal "finite_set" thy "finite (set xs)" 
oheimb@5296
   407
	(K [induct_tac "xs" 1, Auto_tac]);
oheimb@5296
   408
Addsimps[finite_set];
oheimb@5296
   409
AddSIs[finite_set];
oheimb@5296
   410
nipkow@4935
   411
Goal "set (xs@ys) = (set xs Un set ys)";
nipkow@3040
   412
by (induct_tac "xs" 1);
paulson@5316
   413
by Auto_tac;
paulson@3647
   414
qed "set_append";
paulson@3647
   415
Addsimps[set_append];
paulson@1812
   416
nipkow@4935
   417
Goal "set l <= set (x#l)";
paulson@5316
   418
by Auto_tac;
paulson@3647
   419
qed "set_subset_Cons";
paulson@1936
   420
nipkow@4935
   421
Goal "(set xs = {}) = (xs = [])";
paulson@3457
   422
by (induct_tac "xs" 1);
paulson@5316
   423
by Auto_tac;
paulson@3647
   424
qed "set_empty";
paulson@3647
   425
Addsimps [set_empty];
nipkow@2608
   426
nipkow@4935
   427
Goal "set(rev xs) = set(xs)";
paulson@3457
   428
by (induct_tac "xs" 1);
paulson@5316
   429
by Auto_tac;
paulson@3647
   430
qed "set_rev";
paulson@3647
   431
Addsimps [set_rev];
nipkow@2608
   432
nipkow@4935
   433
Goal "set(map f xs) = f``(set xs)";
paulson@3457
   434
by (induct_tac "xs" 1);
paulson@5316
   435
by Auto_tac;
paulson@3647
   436
qed "set_map";
paulson@3647
   437
Addsimps [set_map];
nipkow@2608
   438
oheimb@5443
   439
Goal "(x : set (filter P xs)) = (x : set xs & P x)";
nipkow@4605
   440
by (induct_tac "xs" 1);
paulson@5316
   441
by Auto_tac;
nipkow@4605
   442
qed "in_set_filter";
nipkow@4605
   443
Addsimps [in_set_filter];
nipkow@4605
   444
nipkow@5272
   445
Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)";
paulson@5318
   446
by (induct_tac "xs" 1);
paulson@5318
   447
 by (Simp_tac 1);
paulson@5318
   448
by (Asm_simp_tac 1);
paulson@5318
   449
by (rtac iffI 1);
paulson@5318
   450
by (blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1);
paulson@5318
   451
by (REPEAT(etac exE 1));
paulson@5318
   452
by (exhaust_tac "ys" 1);
paulson@5316
   453
by Auto_tac;
nipkow@5272
   454
qed "in_set_conv_decomp";
nipkow@5272
   455
nipkow@5272
   456
(* eliminate `lists' in favour of `set' *)
nipkow@5272
   457
nipkow@5272
   458
Goal "(xs : lists A) = (!x : set xs. x : A)";
paulson@5318
   459
by (induct_tac "xs" 1);
paulson@5316
   460
by Auto_tac;
nipkow@5272
   461
qed "in_lists_conv_set";
nipkow@5272
   462
nipkow@5272
   463
bind_thm("in_listsD",in_lists_conv_set RS iffD1);
nipkow@5272
   464
AddSDs [in_listsD];
nipkow@5272
   465
bind_thm("in_listsI",in_lists_conv_set RS iffD2);
nipkow@5272
   466
AddSIs [in_listsI];
paulson@1812
   467
oheimb@5518
   468
(** mem **)
oheimb@5518
   469
 
oheimb@5518
   470
section "mem";
oheimb@5518
   471
oheimb@5518
   472
Goal "(x mem xs) = (x: set xs)";
oheimb@5518
   473
by (induct_tac "xs" 1);
oheimb@5518
   474
by Auto_tac;
oheimb@5518
   475
qed "set_mem_eq";
oheimb@5518
   476
oheimb@5518
   477
clasohm@923
   478
(** list_all **)
clasohm@923
   479
nipkow@3467
   480
section "list_all";
nipkow@3467
   481
oheimb@5518
   482
Goal "list_all P xs = (!x:set xs. P x)";
oheimb@5518
   483
by (induct_tac "xs" 1);
oheimb@5518
   484
by Auto_tac;
oheimb@5518
   485
qed "list_all_conv";
oheimb@5518
   486
oheimb@5443
   487
Goal "list_all P (xs@ys) = (list_all P xs & list_all P ys)";
nipkow@3040
   488
by (induct_tac "xs" 1);
paulson@5316
   489
by Auto_tac;
nipkow@2512
   490
qed "list_all_append";
nipkow@2512
   491
Addsimps [list_all_append];
clasohm@923
   492
clasohm@923
   493
nipkow@2608
   494
(** filter **)
clasohm@923
   495
nipkow@3467
   496
section "filter";
nipkow@3467
   497
nipkow@4935
   498
Goal "filter P (xs@ys) = filter P xs @ filter P ys";
paulson@3457
   499
by (induct_tac "xs" 1);
paulson@5316
   500
by Auto_tac;
nipkow@2608
   501
qed "filter_append";
nipkow@2608
   502
Addsimps [filter_append];
nipkow@2608
   503
nipkow@4935
   504
Goal "filter (%x. True) xs = xs";
nipkow@4605
   505
by (induct_tac "xs" 1);
paulson@5316
   506
by Auto_tac;
nipkow@4605
   507
qed "filter_True";
nipkow@4605
   508
Addsimps [filter_True];
nipkow@4605
   509
nipkow@4935
   510
Goal "filter (%x. False) xs = []";
nipkow@4605
   511
by (induct_tac "xs" 1);
paulson@5316
   512
by Auto_tac;
nipkow@4605
   513
qed "filter_False";
nipkow@4605
   514
Addsimps [filter_False];
nipkow@4605
   515
nipkow@4935
   516
Goal "length (filter P xs) <= length xs";
paulson@3457
   517
by (induct_tac "xs" 1);
paulson@5316
   518
by Auto_tac;
nipkow@4605
   519
qed "length_filter";
oheimb@5443
   520
Addsimps[length_filter];
nipkow@2608
   521
oheimb@5443
   522
Goal "set (filter P xs) <= set xs";
oheimb@5443
   523
by Auto_tac;
oheimb@5443
   524
qed "filter_is_subset";
oheimb@5443
   525
Addsimps [filter_is_subset];
oheimb@5443
   526
nipkow@2608
   527
nipkow@3467
   528
section "concat";
nipkow@3467
   529
nipkow@4935
   530
Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
nipkow@3040
   531
by (induct_tac "xs" 1);
paulson@5316
   532
by Auto_tac;
nipkow@2608
   533
qed"concat_append";
nipkow@2608
   534
Addsimps [concat_append];
nipkow@2512
   535
nipkow@4935
   536
Goal "(concat xss = []) = (!xs:set xss. xs=[])";
wenzelm@4423
   537
by (induct_tac "xss" 1);
paulson@5316
   538
by Auto_tac;
nipkow@3896
   539
qed "concat_eq_Nil_conv";
nipkow@3896
   540
AddIffs [concat_eq_Nil_conv];
nipkow@3896
   541
nipkow@4935
   542
Goal "([] = concat xss) = (!xs:set xss. xs=[])";
wenzelm@4423
   543
by (induct_tac "xss" 1);
paulson@5316
   544
by Auto_tac;
nipkow@3896
   545
qed "Nil_eq_concat_conv";
nipkow@3896
   546
AddIffs [Nil_eq_concat_conv];
nipkow@3896
   547
nipkow@4935
   548
Goal  "set(concat xs) = Union(set `` set xs)";
nipkow@3467
   549
by (induct_tac "xs" 1);
paulson@5316
   550
by Auto_tac;
paulson@3647
   551
qed"set_concat";
paulson@3647
   552
Addsimps [set_concat];
nipkow@3467
   553
nipkow@4935
   554
Goal "map f (concat xs) = concat (map (map f) xs)"; 
nipkow@3467
   555
by (induct_tac "xs" 1);
paulson@5316
   556
by Auto_tac;
nipkow@3467
   557
qed "map_concat";
nipkow@3467
   558
nipkow@4935
   559
Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
nipkow@3467
   560
by (induct_tac "xs" 1);
paulson@5316
   561
by Auto_tac;
nipkow@3467
   562
qed"filter_concat"; 
nipkow@3467
   563
nipkow@4935
   564
Goal "rev(concat xs) = concat (map rev (rev xs))";
nipkow@3467
   565
by (induct_tac "xs" 1);
paulson@5316
   566
by Auto_tac;
nipkow@2608
   567
qed "rev_concat";
clasohm@923
   568
clasohm@923
   569
(** nth **)
clasohm@923
   570
nipkow@3467
   571
section "nth";
nipkow@3467
   572
pusch@6408
   573
Goal "(x#xs)!0 = x";
pusch@6408
   574
by Auto_tac;
pusch@6408
   575
qed "nth_Cons_0";
pusch@6408
   576
Addsimps [nth_Cons_0];
nipkow@5644
   577
pusch@6408
   578
Goal "(x#xs)!(Suc n) = xs!n";
pusch@6408
   579
by Auto_tac;
pusch@6408
   580
qed "nth_Cons_Suc";
pusch@6408
   581
Addsimps [nth_Cons_Suc];
pusch@6408
   582
pusch@6408
   583
Delsimps (thms "nth.simps");
pusch@6408
   584
pusch@6408
   585
Goal "!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
pusch@6408
   586
by (induct_tac "xs" 1);
paulson@3457
   587
 by (Asm_simp_tac 1);
paulson@3457
   588
 by (rtac allI 1);
pusch@6408
   589
 by (exhaust_tac "n" 1);
paulson@5316
   590
  by Auto_tac;
nipkow@2608
   591
qed_spec_mp "nth_append";
nipkow@2608
   592
nipkow@4935
   593
Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
nipkow@3040
   594
by (induct_tac "xs" 1);
nipkow@1301
   595
(* case [] *)
nipkow@1301
   596
by (Asm_full_simp_tac 1);
nipkow@1301
   597
(* case x#xl *)
nipkow@1301
   598
by (rtac allI 1);
berghofe@5183
   599
by (induct_tac "n" 1);
paulson@5316
   600
by Auto_tac;
nipkow@1485
   601
qed_spec_mp "nth_map";
nipkow@1301
   602
Addsimps [nth_map];
nipkow@1301
   603
oheimb@5518
   604
Goal "!n. n < length xs --> Ball (set xs) P --> P(xs!n)";
nipkow@3040
   605
by (induct_tac "xs" 1);
nipkow@1301
   606
(* case [] *)
nipkow@1301
   607
by (Simp_tac 1);
nipkow@1301
   608
(* case x#xl *)
nipkow@1301
   609
by (rtac allI 1);
berghofe@5183
   610
by (induct_tac "n" 1);
paulson@5316
   611
by Auto_tac;
oheimb@5518
   612
qed_spec_mp "list_ball_nth";
nipkow@1301
   613
oheimb@5518
   614
Goal "!n. n < length xs --> xs!n : set xs";
nipkow@3040
   615
by (induct_tac "xs" 1);
nipkow@1301
   616
(* case [] *)
nipkow@1301
   617
by (Simp_tac 1);
nipkow@1301
   618
(* case x#xl *)
nipkow@1301
   619
by (rtac allI 1);
berghofe@5183
   620
by (induct_tac "n" 1);
nipkow@1301
   621
(* case 0 *)
nipkow@1301
   622
by (Asm_full_simp_tac 1);
nipkow@1301
   623
(* case Suc x *)
nipkow@4686
   624
by (Asm_full_simp_tac 1);
nipkow@1485
   625
qed_spec_mp "nth_mem";
nipkow@1301
   626
Addsimps [nth_mem];
nipkow@1301
   627
oheimb@5518
   628
nipkow@5077
   629
(** list update **)
nipkow@5077
   630
nipkow@5077
   631
section "list update";
nipkow@5077
   632
nipkow@5077
   633
Goal "!i. length(xs[i:=x]) = length xs";
nipkow@5077
   634
by (induct_tac "xs" 1);
nipkow@5077
   635
by (Simp_tac 1);
berghofe@5183
   636
by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@5077
   637
qed_spec_mp "length_list_update";
nipkow@5077
   638
Addsimps [length_list_update];
nipkow@5077
   639
nipkow@5644
   640
Goal "!i j. i < length xs  --> (xs[i:=x])!j = (if i=j then x else xs!j)";
paulson@6162
   641
by (induct_tac "xs" 1);
paulson@6162
   642
 by (Simp_tac 1);
paulson@6162
   643
by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
nipkow@5644
   644
qed_spec_mp "nth_list_update";
nipkow@5644
   645
nipkow@5077
   646
nipkow@3896
   647
(** last & butlast **)
nipkow@1327
   648
nipkow@5644
   649
section "last / butlast";
nipkow@5644
   650
nipkow@4935
   651
Goal "last(xs@[x]) = x";
wenzelm@4423
   652
by (induct_tac "xs" 1);
paulson@5316
   653
by Auto_tac;
nipkow@3896
   654
qed "last_snoc";
nipkow@3896
   655
Addsimps [last_snoc];
nipkow@3896
   656
nipkow@4935
   657
Goal "butlast(xs@[x]) = xs";
wenzelm@4423
   658
by (induct_tac "xs" 1);
paulson@5316
   659
by Auto_tac;
nipkow@3896
   660
qed "butlast_snoc";
nipkow@3896
   661
Addsimps [butlast_snoc];
nipkow@3896
   662
nipkow@4935
   663
Goal "length(butlast xs) = length xs - 1";
nipkow@4935
   664
by (res_inst_tac [("xs","xs")] rev_induct 1);
paulson@5316
   665
by Auto_tac;
nipkow@4643
   666
qed "length_butlast";
nipkow@4643
   667
Addsimps [length_butlast];
nipkow@4643
   668
paulson@5278
   669
Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
wenzelm@4423
   670
by (induct_tac "xs" 1);
paulson@5316
   671
by Auto_tac;
nipkow@3896
   672
qed_spec_mp "butlast_append";
nipkow@3896
   673
nipkow@4935
   674
Goal "x:set(butlast xs) --> x:set xs";
wenzelm@4423
   675
by (induct_tac "xs" 1);
paulson@5316
   676
by Auto_tac;
nipkow@3896
   677
qed_spec_mp "in_set_butlastD";
nipkow@3896
   678
paulson@5448
   679
Goal "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))";
paulson@5448
   680
by (auto_tac (claset() addDs [in_set_butlastD],
paulson@5448
   681
	      simpset() addsimps [butlast_append]));
paulson@5448
   682
qed "in_set_butlast_appendI";
nipkow@3902
   683
nipkow@2608
   684
(** take  & drop **)
nipkow@2608
   685
section "take & drop";
nipkow@1327
   686
nipkow@4935
   687
Goal "take 0 xs = []";
nipkow@3040
   688
by (induct_tac "xs" 1);
paulson@5316
   689
by Auto_tac;
nipkow@1327
   690
qed "take_0";
nipkow@1327
   691
nipkow@4935
   692
Goal "drop 0 xs = xs";
nipkow@3040
   693
by (induct_tac "xs" 1);
paulson@5316
   694
by Auto_tac;
nipkow@2608
   695
qed "drop_0";
nipkow@2608
   696
nipkow@4935
   697
Goal "take (Suc n) (x#xs) = x # take n xs";
paulson@1552
   698
by (Simp_tac 1);
nipkow@1419
   699
qed "take_Suc_Cons";
nipkow@1327
   700
nipkow@4935
   701
Goal "drop (Suc n) (x#xs) = drop n xs";
nipkow@2608
   702
by (Simp_tac 1);
nipkow@2608
   703
qed "drop_Suc_Cons";
nipkow@2608
   704
nipkow@2608
   705
Delsimps [take_Cons,drop_Cons];
nipkow@2608
   706
Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
nipkow@2608
   707
nipkow@4935
   708
Goal "!xs. length(take n xs) = min (length xs) n";
berghofe@5183
   709
by (induct_tac "n" 1);
paulson@5316
   710
 by Auto_tac;
paulson@3457
   711
by (exhaust_tac "xs" 1);
paulson@5316
   712
 by Auto_tac;
nipkow@2608
   713
qed_spec_mp "length_take";
nipkow@2608
   714
Addsimps [length_take];
clasohm@923
   715
nipkow@4935
   716
Goal "!xs. length(drop n xs) = (length xs - n)";
berghofe@5183
   717
by (induct_tac "n" 1);
paulson@5316
   718
 by Auto_tac;
paulson@3457
   719
by (exhaust_tac "xs" 1);
paulson@5316
   720
 by Auto_tac;
nipkow@2608
   721
qed_spec_mp "length_drop";
nipkow@2608
   722
Addsimps [length_drop];
nipkow@2608
   723
nipkow@4935
   724
Goal "!xs. length xs <= n --> take n xs = xs";
berghofe@5183
   725
by (induct_tac "n" 1);
paulson@5316
   726
 by Auto_tac;
paulson@3457
   727
by (exhaust_tac "xs" 1);
paulson@5316
   728
 by Auto_tac;
nipkow@2608
   729
qed_spec_mp "take_all";
clasohm@923
   730
nipkow@4935
   731
Goal "!xs. length xs <= n --> drop n xs = []";
berghofe@5183
   732
by (induct_tac "n" 1);
paulson@5316
   733
 by Auto_tac;
paulson@3457
   734
by (exhaust_tac "xs" 1);
paulson@5316
   735
 by Auto_tac;
nipkow@2608
   736
qed_spec_mp "drop_all";
nipkow@2608
   737
paulson@5278
   738
Goal "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
berghofe@5183
   739
by (induct_tac "n" 1);
paulson@5316
   740
 by Auto_tac;
paulson@3457
   741
by (exhaust_tac "xs" 1);
paulson@5316
   742
 by Auto_tac;
nipkow@2608
   743
qed_spec_mp "take_append";
nipkow@2608
   744
Addsimps [take_append];
nipkow@2608
   745
nipkow@4935
   746
Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
berghofe@5183
   747
by (induct_tac "n" 1);
paulson@5316
   748
 by Auto_tac;
paulson@3457
   749
by (exhaust_tac "xs" 1);
paulson@5316
   750
 by Auto_tac;
nipkow@2608
   751
qed_spec_mp "drop_append";
nipkow@2608
   752
Addsimps [drop_append];
nipkow@2608
   753
nipkow@4935
   754
Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
berghofe@5183
   755
by (induct_tac "m" 1);
paulson@5316
   756
 by Auto_tac;
paulson@3457
   757
by (exhaust_tac "xs" 1);
paulson@5316
   758
 by Auto_tac;
berghofe@5183
   759
by (exhaust_tac "na" 1);
paulson@5316
   760
 by Auto_tac;
nipkow@2608
   761
qed_spec_mp "take_take";
nipkow@2608
   762
nipkow@4935
   763
Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
berghofe@5183
   764
by (induct_tac "m" 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_drop";
clasohm@923
   769
nipkow@4935
   770
Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
berghofe@5183
   771
by (induct_tac "m" 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_drop";
nipkow@2608
   776
nipkow@4935
   777
Goal "!xs. take n (map f xs) = map f (take n xs)"; 
berghofe@5183
   778
by (induct_tac "n" 1);
paulson@5316
   779
 by Auto_tac;
paulson@3457
   780
by (exhaust_tac "xs" 1);
paulson@5316
   781
 by Auto_tac;
nipkow@2608
   782
qed_spec_mp "take_map"; 
nipkow@2608
   783
nipkow@4935
   784
Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
berghofe@5183
   785
by (induct_tac "n" 1);
paulson@5316
   786
 by Auto_tac;
paulson@3457
   787
by (exhaust_tac "xs" 1);
paulson@5316
   788
 by Auto_tac;
nipkow@2608
   789
qed_spec_mp "drop_map";
nipkow@2608
   790
nipkow@4935
   791
Goal "!n i. i < n --> (take n xs)!i = xs!i";
paulson@3457
   792
by (induct_tac "xs" 1);
paulson@5316
   793
 by Auto_tac;
paulson@3457
   794
by (exhaust_tac "n" 1);
paulson@3457
   795
 by (Blast_tac 1);
paulson@3457
   796
by (exhaust_tac "i" 1);
paulson@5316
   797
 by Auto_tac;
nipkow@2608
   798
qed_spec_mp "nth_take";
nipkow@2608
   799
Addsimps [nth_take];
clasohm@923
   800
nipkow@4935
   801
Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
berghofe@5183
   802
by (induct_tac "n" 1);
paulson@5316
   803
 by Auto_tac;
paulson@3457
   804
by (exhaust_tac "xs" 1);
paulson@5316
   805
 by Auto_tac;
nipkow@2608
   806
qed_spec_mp "nth_drop";
nipkow@2608
   807
Addsimps [nth_drop];
nipkow@2608
   808
nipkow@2608
   809
(** takeWhile & dropWhile **)
nipkow@2608
   810
nipkow@3467
   811
section "takeWhile & dropWhile";
nipkow@3467
   812
nipkow@4935
   813
Goal "takeWhile P xs @ dropWhile P xs = xs";
nipkow@3586
   814
by (induct_tac "xs" 1);
paulson@5316
   815
by Auto_tac;
nipkow@3586
   816
qed "takeWhile_dropWhile_id";
nipkow@3586
   817
Addsimps [takeWhile_dropWhile_id];
nipkow@3586
   818
nipkow@4935
   819
Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
paulson@3457
   820
by (induct_tac "xs" 1);
paulson@5316
   821
by Auto_tac;
nipkow@2608
   822
bind_thm("takeWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   823
Addsimps [takeWhile_append1];
clasohm@923
   824
nipkow@4935
   825
Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
paulson@3457
   826
by (induct_tac "xs" 1);
paulson@5316
   827
by Auto_tac;
nipkow@2608
   828
bind_thm("takeWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   829
Addsimps [takeWhile_append2];
lcp@1169
   830
nipkow@4935
   831
Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
paulson@3457
   832
by (induct_tac "xs" 1);
paulson@5316
   833
by Auto_tac;
nipkow@2608
   834
bind_thm("dropWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   835
Addsimps [dropWhile_append1];
nipkow@2608
   836
nipkow@4935
   837
Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
paulson@3457
   838
by (induct_tac "xs" 1);
paulson@5316
   839
by Auto_tac;
nipkow@2608
   840
bind_thm("dropWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   841
Addsimps [dropWhile_append2];
nipkow@2608
   842
nipkow@4935
   843
Goal "x:set(takeWhile P xs) --> x:set xs & P x";
paulson@3457
   844
by (induct_tac "xs" 1);
paulson@5316
   845
by Auto_tac;
paulson@3647
   846
qed_spec_mp"set_take_whileD";
nipkow@2608
   847
nipkow@6306
   848
(** zip **)
nipkow@6306
   849
section "zip";
nipkow@6306
   850
nipkow@6306
   851
Goal "zip [] ys = []";
nipkow@6306
   852
by(induct_tac "ys" 1);
nipkow@6306
   853
by Auto_tac;
nipkow@6306
   854
qed "zip_Nil";
nipkow@6306
   855
Addsimps [zip_Nil];
nipkow@6306
   856
nipkow@6306
   857
Goal "zip (x#xs) (y#ys) = (x,y)#zip xs ys";
nipkow@6306
   858
by(Simp_tac 1);
nipkow@6306
   859
qed "zip_Cons_Cons";
nipkow@6306
   860
Addsimps [zip_Cons_Cons];
nipkow@6306
   861
nipkow@6306
   862
Delsimps(tl (thms"zip.simps"));
nipkow@4605
   863
nipkow@5272
   864
nipkow@5272
   865
(** foldl **)
nipkow@5272
   866
section "foldl";
nipkow@5272
   867
nipkow@5272
   868
Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys";
paulson@5318
   869
by (induct_tac "xs" 1);
paulson@5316
   870
by Auto_tac;
nipkow@5272
   871
qed_spec_mp "foldl_append";
nipkow@5272
   872
Addsimps [foldl_append];
nipkow@5272
   873
nipkow@5272
   874
(* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
nipkow@5272
   875
   because it requires an additional transitivity step
nipkow@5272
   876
*)
nipkow@5272
   877
Goal "!n::nat. m <= n --> m <= foldl op+ n ns";
paulson@5318
   878
by (induct_tac "ns" 1);
nipkow@6058
   879
by Auto_tac;
nipkow@5272
   880
qed_spec_mp "start_le_sum";
nipkow@5272
   881
nipkow@5272
   882
Goal "n : set ns ==> n <= foldl op+ 0 ns";
oheimb@5758
   883
by (force_tac (claset() addIs [start_le_sum],
oheimb@5758
   884
              simpset() addsimps [in_set_conv_decomp]) 1);
nipkow@5272
   885
qed "elem_le_sum";
nipkow@5272
   886
nipkow@5272
   887
Goal "!m. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))";
paulson@5318
   888
by (induct_tac "ns" 1);
paulson@5316
   889
by Auto_tac;
nipkow@5272
   890
qed_spec_mp "sum_eq_0_conv";
nipkow@5272
   891
AddIffs [sum_eq_0_conv];
nipkow@5272
   892
nipkow@5425
   893
(** upto **)
nipkow@5425
   894
nipkow@5427
   895
(* Does not terminate! *)
nipkow@5427
   896
Goal "[i..j(] = (if i<j then i#[Suc i..j(] else [])";
paulson@6162
   897
by (induct_tac "j" 1);
nipkow@5427
   898
by Auto_tac;
nipkow@5427
   899
qed "upt_rec";
nipkow@5425
   900
nipkow@5427
   901
Goal "j<=i ==> [i..j(] = []";
paulson@6162
   902
by (stac upt_rec 1);
paulson@6162
   903
by (Asm_simp_tac 1);
nipkow@5427
   904
qed "upt_conv_Nil";
nipkow@5427
   905
Addsimps [upt_conv_Nil];
nipkow@5427
   906
nipkow@5427
   907
Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]";
nipkow@5427
   908
by (Asm_simp_tac 1);
nipkow@5427
   909
qed "upt_Suc";
nipkow@5427
   910
nipkow@5427
   911
Goal "i<j ==> [i..j(] = i#[Suc i..j(]";
paulson@6162
   912
by (rtac trans 1);
paulson@6162
   913
by (stac upt_rec 1);
paulson@6162
   914
by (rtac refl 2);
nipkow@5427
   915
by (Asm_simp_tac 1);
nipkow@5427
   916
qed "upt_conv_Cons";
nipkow@5427
   917
nipkow@5427
   918
Goal "length [i..j(] = j-i";
paulson@6162
   919
by (induct_tac "j" 1);
nipkow@5427
   920
 by (Simp_tac 1);
paulson@6162
   921
by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
nipkow@5427
   922
qed "length_upt";
nipkow@5427
   923
Addsimps [length_upt];
nipkow@5425
   924
nipkow@5427
   925
Goal "i+k < j --> [i..j(] ! k = i+k";
paulson@6162
   926
by (induct_tac "j" 1);
paulson@6162
   927
 by (Simp_tac 1);
paulson@6162
   928
by (asm_simp_tac (simpset() addsimps [nth_append,less_diff_conv]@add_ac) 1);
paulson@6162
   929
by (Clarify_tac 1);
paulson@6162
   930
by (subgoal_tac "n=i+k" 1);
paulson@6162
   931
 by (Asm_simp_tac 2);
paulson@6162
   932
by (Asm_simp_tac 1);
nipkow@5427
   933
qed_spec_mp "nth_upt";
nipkow@5427
   934
Addsimps [nth_upt];
nipkow@5425
   935
nipkow@5272
   936
nipkow@4605
   937
(** nodups & remdups **)
nipkow@4605
   938
section "nodups & remdups";
nipkow@4605
   939
nipkow@4935
   940
Goal "set(remdups xs) = set xs";
nipkow@4605
   941
by (induct_tac "xs" 1);
nipkow@4605
   942
 by (Simp_tac 1);
nipkow@4686
   943
by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
nipkow@4605
   944
qed "set_remdups";
nipkow@4605
   945
Addsimps [set_remdups];
nipkow@4605
   946
nipkow@4935
   947
Goal "nodups(remdups xs)";
nipkow@4605
   948
by (induct_tac "xs" 1);
paulson@5316
   949
by Auto_tac;
nipkow@4605
   950
qed "nodups_remdups";
nipkow@4605
   951
nipkow@4935
   952
Goal "nodups xs --> nodups (filter P xs)";
nipkow@4605
   953
by (induct_tac "xs" 1);
paulson@5316
   954
by Auto_tac;
nipkow@4605
   955
qed_spec_mp "nodups_filter";
nipkow@4605
   956
nipkow@3589
   957
(** replicate **)
nipkow@3589
   958
section "replicate";
nipkow@3589
   959
nipkow@4935
   960
Goal "set(replicate (Suc n) x) = {x}";
wenzelm@4423
   961
by (induct_tac "n" 1);
paulson@5316
   962
by Auto_tac;
nipkow@3589
   963
val lemma = result();
nipkow@3589
   964
nipkow@5043
   965
Goal "n ~= 0 ==> set(replicate n x) = {x}";
wenzelm@4423
   966
by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
nipkow@3589
   967
qed "set_replicate";
nipkow@3589
   968
Addsimps [set_replicate];
nipkow@5162
   969
nipkow@5162
   970
nipkow@5281
   971
(*** Lexcicographic orderings on lists ***)
nipkow@5281
   972
section"Lexcicographic orderings on lists";
nipkow@5281
   973
nipkow@5281
   974
Goal "wf r ==> wf(lexn r n)";
paulson@5318
   975
by (induct_tac "n" 1);
paulson@5318
   976
by (Simp_tac 1);
paulson@5318
   977
by (Simp_tac 1);
paulson@5318
   978
by (rtac wf_subset 1);
paulson@5318
   979
by (rtac Int_lower1 2);
paulson@5318
   980
by (rtac wf_prod_fun_image 1);
paulson@5318
   981
by (rtac injI 2);
paulson@5318
   982
by (Auto_tac);
nipkow@5281
   983
qed "wf_lexn";
nipkow@5281
   984
nipkow@5281
   985
Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n";
paulson@5318
   986
by (induct_tac "n" 1);
paulson@5318
   987
by (Auto_tac);
nipkow@5281
   988
qed_spec_mp "lexn_length";
nipkow@5281
   989
nipkow@5281
   990
Goalw [lex_def] "wf r ==> wf(lex r)";
paulson@5318
   991
by (rtac wf_UN 1);
paulson@5318
   992
by (blast_tac (claset() addIs [wf_lexn]) 1);
paulson@5318
   993
by (Clarify_tac 1);
paulson@5318
   994
by (rename_tac "m n" 1);
paulson@5318
   995
by (subgoal_tac "m ~= n" 1);
paulson@5318
   996
 by (Blast_tac 2);
paulson@5318
   997
by (blast_tac (claset() addDs [lexn_length,not_sym]) 1);
nipkow@5281
   998
qed "wf_lex";
nipkow@5281
   999
AddSIs [wf_lex];
nipkow@5281
  1000
nipkow@5281
  1001
Goal
nipkow@5281
  1002
 "lexn r n = \
nipkow@5281
  1003
\ {(xs,ys). length xs = n & length ys = n & \
nipkow@5281
  1004
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5318
  1005
by (induct_tac "n" 1);
paulson@5318
  1006
 by (Simp_tac 1);
paulson@5318
  1007
 by (Blast_tac 1);
paulson@5641
  1008
by (asm_full_simp_tac (simpset() 
oheimb@5296
  1009
				addsimps [lex_prod_def]) 1);
paulson@5641
  1010
by (auto_tac (claset(), simpset()));
paulson@5318
  1011
  by (Blast_tac 1);
paulson@5318
  1012
 by (rename_tac "a xys x xs' y ys'" 1);
paulson@5318
  1013
 by (res_inst_tac [("x","a#xys")] exI 1);
paulson@5318
  1014
 by (Simp_tac 1);
paulson@5318
  1015
by (exhaust_tac "xys" 1);
paulson@5641
  1016
 by (ALLGOALS (asm_full_simp_tac (simpset())));
paulson@5318
  1017
by (Blast_tac 1);
nipkow@5281
  1018
qed "lexn_conv";
nipkow@5281
  1019
nipkow@5281
  1020
Goalw [lex_def]
nipkow@5281
  1021
 "lex r = \
nipkow@5281
  1022
\ {(xs,ys). length xs = length ys & \
nipkow@5281
  1023
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5641
  1024
by (force_tac (claset(), simpset() addsimps [lexn_conv]) 1);
nipkow@5281
  1025
qed "lex_conv";
nipkow@5281
  1026
nipkow@5281
  1027
Goalw [lexico_def] "wf r ==> wf(lexico r)";
paulson@5318
  1028
by (Blast_tac 1);
nipkow@5281
  1029
qed "wf_lexico";
nipkow@5281
  1030
AddSIs [wf_lexico];
nipkow@5281
  1031
nipkow@5281
  1032
Goalw
nipkow@5281
  1033
 [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def]
nipkow@5281
  1034
"lexico r = {(xs,ys). length xs < length ys | \
nipkow@5281
  1035
\                     length xs = length ys & (xs,ys) : lex r}";
paulson@5318
  1036
by (Simp_tac 1);
nipkow@5281
  1037
qed "lexico_conv";
nipkow@5281
  1038
nipkow@5283
  1039
Goal "([],ys) ~: lex r";
paulson@5318
  1040
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1041
qed "Nil_notin_lex";
nipkow@5283
  1042
nipkow@5283
  1043
Goal "(xs,[]) ~: lex r";
paulson@5318
  1044
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1045
qed "Nil2_notin_lex";
nipkow@5283
  1046
nipkow@5283
  1047
AddIffs [Nil_notin_lex,Nil2_notin_lex];
nipkow@5283
  1048
nipkow@5283
  1049
Goal "((x#xs,y#ys) : lex r) = \
nipkow@5283
  1050
\     ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)";
paulson@5318
  1051
by (simp_tac (simpset() addsimps [lex_conv]) 1);
paulson@5318
  1052
by (rtac iffI 1);
paulson@5318
  1053
 by (blast_tac (claset() addIs [Cons_eq_appendI]) 2);
paulson@5318
  1054
by (REPEAT(eresolve_tac [conjE, exE] 1));
paulson@5318
  1055
by (exhaust_tac "xys" 1);
paulson@5318
  1056
by (Asm_full_simp_tac 1);
paulson@5318
  1057
by (Asm_full_simp_tac 1);
paulson@5318
  1058
by (Blast_tac 1);
nipkow@5283
  1059
qed "Cons_in_lex";
nipkow@5283
  1060
AddIffs [Cons_in_lex];