src/HOL/List.ML
author nipkow
Fri Jun 11 17:14:00 1999 +0200 (1999-06-11)
changeset 6820 41d9b7bbf968
parent 6813 bf90f86502b2
child 6831 799859f2e657
permissions -rw-r--r--
rev=rev lemma.
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@6820
   383
Goal "!ys. (rev xs = rev ys) = (xs = ys)";
nipkow@6820
   384
by(induct_tac "xs" 1);
nipkow@6820
   385
 by (Force_tac 1);
nipkow@6820
   386
br allI 1;
nipkow@6820
   387
by(exhaust_tac "ys" 1);
nipkow@6820
   388
 by (Asm_simp_tac 1);
nipkow@6820
   389
by (Force_tac 1);
nipkow@6820
   390
qed_spec_mp "rev_is_rev_conv";
nipkow@6820
   391
AddIffs [rev_is_rev_conv];
nipkow@6820
   392
nipkow@4935
   393
val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
wenzelm@5132
   394
by (stac (rev_rev_ident RS sym) 1);
paulson@6162
   395
by (res_inst_tac [("list", "rev xs")] list.induct 1);
wenzelm@5132
   396
by (ALLGOALS Simp_tac);
wenzelm@5132
   397
by (resolve_tac prems 1);
wenzelm@5132
   398
by (eresolve_tac prems 1);
nipkow@4935
   399
qed "rev_induct";
nipkow@4935
   400
nipkow@5272
   401
fun rev_induct_tac xs = res_inst_tac [("xs",xs)] rev_induct;
nipkow@5272
   402
nipkow@4935
   403
Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
wenzelm@5132
   404
by (res_inst_tac [("xs","xs")] rev_induct 1);
paulson@5316
   405
by Auto_tac;
nipkow@4935
   406
bind_thm ("rev_exhaust",
nipkow@4935
   407
  impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp)))));
nipkow@4935
   408
nipkow@2608
   409
nipkow@3465
   410
(** set **)
paulson@1812
   411
nipkow@3467
   412
section "set";
nipkow@3467
   413
oheimb@5296
   414
qed_goal "finite_set" thy "finite (set xs)" 
oheimb@5296
   415
	(K [induct_tac "xs" 1, Auto_tac]);
oheimb@5296
   416
Addsimps[finite_set];
oheimb@5296
   417
AddSIs[finite_set];
oheimb@5296
   418
nipkow@4935
   419
Goal "set (xs@ys) = (set xs Un set ys)";
nipkow@3040
   420
by (induct_tac "xs" 1);
paulson@5316
   421
by Auto_tac;
paulson@3647
   422
qed "set_append";
paulson@3647
   423
Addsimps[set_append];
paulson@1812
   424
nipkow@4935
   425
Goal "set l <= set (x#l)";
paulson@5316
   426
by Auto_tac;
paulson@3647
   427
qed "set_subset_Cons";
paulson@1936
   428
nipkow@4935
   429
Goal "(set xs = {}) = (xs = [])";
paulson@3457
   430
by (induct_tac "xs" 1);
paulson@5316
   431
by Auto_tac;
paulson@3647
   432
qed "set_empty";
paulson@3647
   433
Addsimps [set_empty];
nipkow@2608
   434
nipkow@4935
   435
Goal "set(rev xs) = set(xs)";
paulson@3457
   436
by (induct_tac "xs" 1);
paulson@5316
   437
by Auto_tac;
paulson@3647
   438
qed "set_rev";
paulson@3647
   439
Addsimps [set_rev];
nipkow@2608
   440
nipkow@4935
   441
Goal "set(map f xs) = f``(set xs)";
paulson@3457
   442
by (induct_tac "xs" 1);
paulson@5316
   443
by Auto_tac;
paulson@3647
   444
qed "set_map";
paulson@3647
   445
Addsimps [set_map];
nipkow@2608
   446
nipkow@6433
   447
Goal "set(filter P xs) = {x. x : set xs & P x}";
paulson@6813
   448
by (induct_tac "xs" 1);
paulson@6813
   449
by Auto_tac;
nipkow@6433
   450
qed "set_filter";
nipkow@6433
   451
Addsimps [set_filter];
nipkow@6433
   452
(*
oheimb@5443
   453
Goal "(x : set (filter P xs)) = (x : set xs & P x)";
nipkow@4605
   454
by (induct_tac "xs" 1);
paulson@5316
   455
by Auto_tac;
nipkow@4605
   456
qed "in_set_filter";
nipkow@4605
   457
Addsimps [in_set_filter];
nipkow@6433
   458
*)
nipkow@6433
   459
Goal "set[i..j(] = {k. i <= k & k < j}";
paulson@6813
   460
by (induct_tac "j" 1);
paulson@6813
   461
by Auto_tac;
paulson@6813
   462
by (arith_tac 1);
nipkow@6433
   463
qed "set_upt";
nipkow@6433
   464
Addsimps [set_upt];
nipkow@6433
   465
nipkow@6433
   466
Goal "!i < size xs. set(xs[i := x]) <= insert x (set xs)";
paulson@6813
   467
by (induct_tac "xs" 1);
paulson@6813
   468
 by (Simp_tac 1);
paulson@6813
   469
by (asm_simp_tac (simpset() addsplits [nat.split]) 1);
paulson@6813
   470
by (Blast_tac 1);
nipkow@6433
   471
qed_spec_mp "set_list_update_subset";
nipkow@4605
   472
nipkow@5272
   473
Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)";
paulson@5318
   474
by (induct_tac "xs" 1);
paulson@5318
   475
 by (Simp_tac 1);
paulson@5318
   476
by (Asm_simp_tac 1);
paulson@5318
   477
by (rtac iffI 1);
paulson@5318
   478
by (blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1);
paulson@5318
   479
by (REPEAT(etac exE 1));
paulson@5318
   480
by (exhaust_tac "ys" 1);
paulson@5316
   481
by Auto_tac;
nipkow@5272
   482
qed "in_set_conv_decomp";
nipkow@5272
   483
nipkow@5272
   484
(* eliminate `lists' in favour of `set' *)
nipkow@5272
   485
nipkow@5272
   486
Goal "(xs : lists A) = (!x : set xs. x : A)";
paulson@5318
   487
by (induct_tac "xs" 1);
paulson@5316
   488
by Auto_tac;
nipkow@5272
   489
qed "in_lists_conv_set";
nipkow@5272
   490
nipkow@5272
   491
bind_thm("in_listsD",in_lists_conv_set RS iffD1);
nipkow@5272
   492
AddSDs [in_listsD];
nipkow@5272
   493
bind_thm("in_listsI",in_lists_conv_set RS iffD2);
nipkow@5272
   494
AddSIs [in_listsI];
paulson@1812
   495
oheimb@5518
   496
(** mem **)
oheimb@5518
   497
 
oheimb@5518
   498
section "mem";
oheimb@5518
   499
oheimb@5518
   500
Goal "(x mem xs) = (x: set xs)";
oheimb@5518
   501
by (induct_tac "xs" 1);
oheimb@5518
   502
by Auto_tac;
oheimb@5518
   503
qed "set_mem_eq";
oheimb@5518
   504
oheimb@5518
   505
clasohm@923
   506
(** list_all **)
clasohm@923
   507
nipkow@3467
   508
section "list_all";
nipkow@3467
   509
oheimb@5518
   510
Goal "list_all P xs = (!x:set xs. P x)";
oheimb@5518
   511
by (induct_tac "xs" 1);
oheimb@5518
   512
by Auto_tac;
oheimb@5518
   513
qed "list_all_conv";
oheimb@5518
   514
oheimb@5443
   515
Goal "list_all P (xs@ys) = (list_all P xs & list_all P ys)";
nipkow@3040
   516
by (induct_tac "xs" 1);
paulson@5316
   517
by Auto_tac;
nipkow@2512
   518
qed "list_all_append";
nipkow@2512
   519
Addsimps [list_all_append];
clasohm@923
   520
clasohm@923
   521
nipkow@2608
   522
(** filter **)
clasohm@923
   523
nipkow@3467
   524
section "filter";
nipkow@3467
   525
nipkow@4935
   526
Goal "filter P (xs@ys) = filter P xs @ filter P ys";
paulson@3457
   527
by (induct_tac "xs" 1);
paulson@5316
   528
by Auto_tac;
nipkow@2608
   529
qed "filter_append";
nipkow@2608
   530
Addsimps [filter_append];
nipkow@2608
   531
nipkow@4935
   532
Goal "filter (%x. True) xs = xs";
nipkow@4605
   533
by (induct_tac "xs" 1);
paulson@5316
   534
by Auto_tac;
nipkow@4605
   535
qed "filter_True";
nipkow@4605
   536
Addsimps [filter_True];
nipkow@4605
   537
nipkow@4935
   538
Goal "filter (%x. False) xs = []";
nipkow@4605
   539
by (induct_tac "xs" 1);
paulson@5316
   540
by Auto_tac;
nipkow@4605
   541
qed "filter_False";
nipkow@4605
   542
Addsimps [filter_False];
nipkow@4605
   543
nipkow@4935
   544
Goal "length (filter P xs) <= length xs";
paulson@3457
   545
by (induct_tac "xs" 1);
paulson@5316
   546
by Auto_tac;
nipkow@4605
   547
qed "length_filter";
oheimb@5443
   548
Addsimps[length_filter];
nipkow@2608
   549
oheimb@5443
   550
Goal "set (filter P xs) <= set xs";
oheimb@5443
   551
by Auto_tac;
oheimb@5443
   552
qed "filter_is_subset";
oheimb@5443
   553
Addsimps [filter_is_subset];
oheimb@5443
   554
nipkow@2608
   555
nipkow@3467
   556
section "concat";
nipkow@3467
   557
nipkow@4935
   558
Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
nipkow@3040
   559
by (induct_tac "xs" 1);
paulson@5316
   560
by Auto_tac;
nipkow@2608
   561
qed"concat_append";
nipkow@2608
   562
Addsimps [concat_append];
nipkow@2512
   563
nipkow@4935
   564
Goal "(concat xss = []) = (!xs:set xss. xs=[])";
wenzelm@4423
   565
by (induct_tac "xss" 1);
paulson@5316
   566
by Auto_tac;
nipkow@3896
   567
qed "concat_eq_Nil_conv";
nipkow@3896
   568
AddIffs [concat_eq_Nil_conv];
nipkow@3896
   569
nipkow@4935
   570
Goal "([] = concat xss) = (!xs:set xss. xs=[])";
wenzelm@4423
   571
by (induct_tac "xss" 1);
paulson@5316
   572
by Auto_tac;
nipkow@3896
   573
qed "Nil_eq_concat_conv";
nipkow@3896
   574
AddIffs [Nil_eq_concat_conv];
nipkow@3896
   575
nipkow@4935
   576
Goal  "set(concat xs) = Union(set `` set xs)";
nipkow@3467
   577
by (induct_tac "xs" 1);
paulson@5316
   578
by Auto_tac;
paulson@3647
   579
qed"set_concat";
paulson@3647
   580
Addsimps [set_concat];
nipkow@3467
   581
nipkow@4935
   582
Goal "map f (concat xs) = concat (map (map f) xs)"; 
nipkow@3467
   583
by (induct_tac "xs" 1);
paulson@5316
   584
by Auto_tac;
nipkow@3467
   585
qed "map_concat";
nipkow@3467
   586
nipkow@4935
   587
Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
nipkow@3467
   588
by (induct_tac "xs" 1);
paulson@5316
   589
by Auto_tac;
nipkow@3467
   590
qed"filter_concat"; 
nipkow@3467
   591
nipkow@4935
   592
Goal "rev(concat xs) = concat (map rev (rev xs))";
nipkow@3467
   593
by (induct_tac "xs" 1);
paulson@5316
   594
by Auto_tac;
nipkow@2608
   595
qed "rev_concat";
clasohm@923
   596
clasohm@923
   597
(** nth **)
clasohm@923
   598
nipkow@3467
   599
section "nth";
nipkow@3467
   600
pusch@6408
   601
Goal "(x#xs)!0 = x";
pusch@6408
   602
by Auto_tac;
pusch@6408
   603
qed "nth_Cons_0";
pusch@6408
   604
Addsimps [nth_Cons_0];
nipkow@5644
   605
pusch@6408
   606
Goal "(x#xs)!(Suc n) = xs!n";
pusch@6408
   607
by Auto_tac;
pusch@6408
   608
qed "nth_Cons_Suc";
pusch@6408
   609
Addsimps [nth_Cons_Suc];
pusch@6408
   610
pusch@6408
   611
Delsimps (thms "nth.simps");
pusch@6408
   612
pusch@6408
   613
Goal "!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
pusch@6408
   614
by (induct_tac "xs" 1);
paulson@3457
   615
 by (Asm_simp_tac 1);
paulson@3457
   616
 by (rtac allI 1);
pusch@6408
   617
 by (exhaust_tac "n" 1);
paulson@5316
   618
  by Auto_tac;
nipkow@2608
   619
qed_spec_mp "nth_append";
nipkow@2608
   620
nipkow@4935
   621
Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
nipkow@3040
   622
by (induct_tac "xs" 1);
nipkow@1301
   623
(* case [] *)
nipkow@1301
   624
by (Asm_full_simp_tac 1);
nipkow@1301
   625
(* case x#xl *)
nipkow@1301
   626
by (rtac allI 1);
berghofe@5183
   627
by (induct_tac "n" 1);
paulson@5316
   628
by Auto_tac;
nipkow@1485
   629
qed_spec_mp "nth_map";
nipkow@1301
   630
Addsimps [nth_map];
nipkow@1301
   631
oheimb@5518
   632
Goal "!n. n < length xs --> Ball (set xs) P --> P(xs!n)";
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);
paulson@5316
   639
by Auto_tac;
oheimb@5518
   640
qed_spec_mp "list_ball_nth";
nipkow@1301
   641
oheimb@5518
   642
Goal "!n. n < length xs --> xs!n : set xs";
nipkow@3040
   643
by (induct_tac "xs" 1);
nipkow@1301
   644
(* case [] *)
nipkow@1301
   645
by (Simp_tac 1);
nipkow@1301
   646
(* case x#xl *)
nipkow@1301
   647
by (rtac allI 1);
berghofe@5183
   648
by (induct_tac "n" 1);
nipkow@1301
   649
(* case 0 *)
nipkow@1301
   650
by (Asm_full_simp_tac 1);
nipkow@1301
   651
(* case Suc x *)
nipkow@4686
   652
by (Asm_full_simp_tac 1);
nipkow@1485
   653
qed_spec_mp "nth_mem";
nipkow@1301
   654
Addsimps [nth_mem];
nipkow@1301
   655
oheimb@5518
   656
nipkow@5077
   657
(** list update **)
nipkow@5077
   658
nipkow@5077
   659
section "list update";
nipkow@5077
   660
nipkow@5077
   661
Goal "!i. length(xs[i:=x]) = length xs";
nipkow@5077
   662
by (induct_tac "xs" 1);
nipkow@5077
   663
by (Simp_tac 1);
berghofe@5183
   664
by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@5077
   665
qed_spec_mp "length_list_update";
nipkow@5077
   666
Addsimps [length_list_update];
nipkow@5077
   667
nipkow@5644
   668
Goal "!i j. i < length xs  --> (xs[i:=x])!j = (if i=j then x else xs!j)";
paulson@6162
   669
by (induct_tac "xs" 1);
paulson@6162
   670
 by (Simp_tac 1);
paulson@6162
   671
by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
nipkow@5644
   672
qed_spec_mp "nth_list_update";
nipkow@5644
   673
nipkow@6433
   674
Goal "!i. i < size xs --> xs[i:=x, i:=y] = xs[i:=y]";
paulson@6813
   675
by (induct_tac "xs" 1);
paulson@6813
   676
 by (Simp_tac 1);
paulson@6813
   677
by (asm_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@6433
   678
qed_spec_mp "list_update_overwrite";
nipkow@6433
   679
Addsimps [list_update_overwrite];
nipkow@6433
   680
nipkow@6433
   681
Goal "!i < length xs. (xs[i := x] = xs) = (xs!i = x)";
paulson@6813
   682
by (induct_tac "xs" 1);
paulson@6813
   683
 by (Simp_tac 1);
paulson@6813
   684
by (simp_tac (simpset() addsplits [nat.split]) 1);
paulson@6813
   685
by (Blast_tac 1);
nipkow@6433
   686
qed_spec_mp "list_update_same_conv";
nipkow@6433
   687
nipkow@5077
   688
nipkow@3896
   689
(** last & butlast **)
nipkow@1327
   690
nipkow@5644
   691
section "last / butlast";
nipkow@5644
   692
nipkow@4935
   693
Goal "last(xs@[x]) = x";
wenzelm@4423
   694
by (induct_tac "xs" 1);
paulson@5316
   695
by Auto_tac;
nipkow@3896
   696
qed "last_snoc";
nipkow@3896
   697
Addsimps [last_snoc];
nipkow@3896
   698
nipkow@4935
   699
Goal "butlast(xs@[x]) = xs";
wenzelm@4423
   700
by (induct_tac "xs" 1);
paulson@5316
   701
by Auto_tac;
nipkow@3896
   702
qed "butlast_snoc";
nipkow@3896
   703
Addsimps [butlast_snoc];
nipkow@3896
   704
nipkow@4935
   705
Goal "length(butlast xs) = length xs - 1";
nipkow@4935
   706
by (res_inst_tac [("xs","xs")] rev_induct 1);
paulson@5316
   707
by Auto_tac;
nipkow@4643
   708
qed "length_butlast";
nipkow@4643
   709
Addsimps [length_butlast];
nipkow@4643
   710
paulson@5278
   711
Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
wenzelm@4423
   712
by (induct_tac "xs" 1);
paulson@5316
   713
by Auto_tac;
nipkow@3896
   714
qed_spec_mp "butlast_append";
nipkow@3896
   715
nipkow@4935
   716
Goal "x:set(butlast xs) --> x:set xs";
wenzelm@4423
   717
by (induct_tac "xs" 1);
paulson@5316
   718
by Auto_tac;
nipkow@3896
   719
qed_spec_mp "in_set_butlastD";
nipkow@3896
   720
paulson@5448
   721
Goal "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))";
paulson@5448
   722
by (auto_tac (claset() addDs [in_set_butlastD],
paulson@5448
   723
	      simpset() addsimps [butlast_append]));
paulson@5448
   724
qed "in_set_butlast_appendI";
nipkow@3902
   725
nipkow@2608
   726
(** take  & drop **)
nipkow@2608
   727
section "take & drop";
nipkow@1327
   728
nipkow@4935
   729
Goal "take 0 xs = []";
nipkow@3040
   730
by (induct_tac "xs" 1);
paulson@5316
   731
by Auto_tac;
nipkow@1327
   732
qed "take_0";
nipkow@1327
   733
nipkow@4935
   734
Goal "drop 0 xs = xs";
nipkow@3040
   735
by (induct_tac "xs" 1);
paulson@5316
   736
by Auto_tac;
nipkow@2608
   737
qed "drop_0";
nipkow@2608
   738
nipkow@4935
   739
Goal "take (Suc n) (x#xs) = x # take n xs";
paulson@1552
   740
by (Simp_tac 1);
nipkow@1419
   741
qed "take_Suc_Cons";
nipkow@1327
   742
nipkow@4935
   743
Goal "drop (Suc n) (x#xs) = drop n xs";
nipkow@2608
   744
by (Simp_tac 1);
nipkow@2608
   745
qed "drop_Suc_Cons";
nipkow@2608
   746
nipkow@2608
   747
Delsimps [take_Cons,drop_Cons];
nipkow@2608
   748
Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
nipkow@2608
   749
nipkow@4935
   750
Goal "!xs. length(take n xs) = min (length xs) n";
berghofe@5183
   751
by (induct_tac "n" 1);
paulson@5316
   752
 by Auto_tac;
paulson@3457
   753
by (exhaust_tac "xs" 1);
paulson@5316
   754
 by Auto_tac;
nipkow@2608
   755
qed_spec_mp "length_take";
nipkow@2608
   756
Addsimps [length_take];
clasohm@923
   757
nipkow@4935
   758
Goal "!xs. length(drop n xs) = (length xs - n)";
berghofe@5183
   759
by (induct_tac "n" 1);
paulson@5316
   760
 by Auto_tac;
paulson@3457
   761
by (exhaust_tac "xs" 1);
paulson@5316
   762
 by Auto_tac;
nipkow@2608
   763
qed_spec_mp "length_drop";
nipkow@2608
   764
Addsimps [length_drop];
nipkow@2608
   765
nipkow@4935
   766
Goal "!xs. length xs <= n --> take n xs = xs";
berghofe@5183
   767
by (induct_tac "n" 1);
paulson@5316
   768
 by Auto_tac;
paulson@3457
   769
by (exhaust_tac "xs" 1);
paulson@5316
   770
 by Auto_tac;
nipkow@2608
   771
qed_spec_mp "take_all";
clasohm@923
   772
nipkow@4935
   773
Goal "!xs. length xs <= n --> drop n xs = []";
berghofe@5183
   774
by (induct_tac "n" 1);
paulson@5316
   775
 by Auto_tac;
paulson@3457
   776
by (exhaust_tac "xs" 1);
paulson@5316
   777
 by Auto_tac;
nipkow@2608
   778
qed_spec_mp "drop_all";
nipkow@2608
   779
paulson@5278
   780
Goal "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
berghofe@5183
   781
by (induct_tac "n" 1);
paulson@5316
   782
 by Auto_tac;
paulson@3457
   783
by (exhaust_tac "xs" 1);
paulson@5316
   784
 by Auto_tac;
nipkow@2608
   785
qed_spec_mp "take_append";
nipkow@2608
   786
Addsimps [take_append];
nipkow@2608
   787
nipkow@4935
   788
Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
berghofe@5183
   789
by (induct_tac "n" 1);
paulson@5316
   790
 by Auto_tac;
paulson@3457
   791
by (exhaust_tac "xs" 1);
paulson@5316
   792
 by Auto_tac;
nipkow@2608
   793
qed_spec_mp "drop_append";
nipkow@2608
   794
Addsimps [drop_append];
nipkow@2608
   795
nipkow@4935
   796
Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
berghofe@5183
   797
by (induct_tac "m" 1);
paulson@5316
   798
 by Auto_tac;
paulson@3457
   799
by (exhaust_tac "xs" 1);
paulson@5316
   800
 by Auto_tac;
berghofe@5183
   801
by (exhaust_tac "na" 1);
paulson@5316
   802
 by Auto_tac;
nipkow@2608
   803
qed_spec_mp "take_take";
nipkow@2608
   804
nipkow@4935
   805
Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
berghofe@5183
   806
by (induct_tac "m" 1);
paulson@5316
   807
 by Auto_tac;
paulson@3457
   808
by (exhaust_tac "xs" 1);
paulson@5316
   809
 by Auto_tac;
nipkow@2608
   810
qed_spec_mp "drop_drop";
clasohm@923
   811
nipkow@4935
   812
Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
berghofe@5183
   813
by (induct_tac "m" 1);
paulson@5316
   814
 by Auto_tac;
paulson@3457
   815
by (exhaust_tac "xs" 1);
paulson@5316
   816
 by Auto_tac;
nipkow@2608
   817
qed_spec_mp "take_drop";
nipkow@2608
   818
paulson@6813
   819
Goal "!xs. take n xs @ drop n xs = xs";
paulson@6813
   820
by (induct_tac "n" 1);
paulson@6813
   821
 by Auto_tac;
paulson@6813
   822
by (exhaust_tac "xs" 1);
paulson@6813
   823
 by Auto_tac;
paulson@6813
   824
qed_spec_mp "append_take_drop_id";
paulson@6813
   825
nipkow@4935
   826
Goal "!xs. take n (map f xs) = map f (take n xs)"; 
berghofe@5183
   827
by (induct_tac "n" 1);
paulson@5316
   828
 by Auto_tac;
paulson@3457
   829
by (exhaust_tac "xs" 1);
paulson@5316
   830
 by Auto_tac;
nipkow@2608
   831
qed_spec_mp "take_map"; 
nipkow@2608
   832
nipkow@4935
   833
Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
berghofe@5183
   834
by (induct_tac "n" 1);
paulson@5316
   835
 by Auto_tac;
paulson@3457
   836
by (exhaust_tac "xs" 1);
paulson@5316
   837
 by Auto_tac;
nipkow@2608
   838
qed_spec_mp "drop_map";
nipkow@2608
   839
nipkow@4935
   840
Goal "!n i. i < n --> (take n xs)!i = xs!i";
paulson@3457
   841
by (induct_tac "xs" 1);
paulson@5316
   842
 by Auto_tac;
paulson@3457
   843
by (exhaust_tac "n" 1);
paulson@3457
   844
 by (Blast_tac 1);
paulson@3457
   845
by (exhaust_tac "i" 1);
paulson@5316
   846
 by Auto_tac;
nipkow@2608
   847
qed_spec_mp "nth_take";
nipkow@2608
   848
Addsimps [nth_take];
clasohm@923
   849
nipkow@4935
   850
Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
berghofe@5183
   851
by (induct_tac "n" 1);
paulson@5316
   852
 by Auto_tac;
paulson@3457
   853
by (exhaust_tac "xs" 1);
paulson@5316
   854
 by Auto_tac;
nipkow@2608
   855
qed_spec_mp "nth_drop";
nipkow@2608
   856
Addsimps [nth_drop];
nipkow@2608
   857
nipkow@2608
   858
(** takeWhile & dropWhile **)
nipkow@2608
   859
nipkow@3467
   860
section "takeWhile & dropWhile";
nipkow@3467
   861
nipkow@4935
   862
Goal "takeWhile P xs @ dropWhile P xs = xs";
nipkow@3586
   863
by (induct_tac "xs" 1);
paulson@5316
   864
by Auto_tac;
nipkow@3586
   865
qed "takeWhile_dropWhile_id";
nipkow@3586
   866
Addsimps [takeWhile_dropWhile_id];
nipkow@3586
   867
nipkow@4935
   868
Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
paulson@3457
   869
by (induct_tac "xs" 1);
paulson@5316
   870
by Auto_tac;
nipkow@2608
   871
bind_thm("takeWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   872
Addsimps [takeWhile_append1];
clasohm@923
   873
nipkow@4935
   874
Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
paulson@3457
   875
by (induct_tac "xs" 1);
paulson@5316
   876
by Auto_tac;
nipkow@2608
   877
bind_thm("takeWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   878
Addsimps [takeWhile_append2];
lcp@1169
   879
nipkow@4935
   880
Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
paulson@3457
   881
by (induct_tac "xs" 1);
paulson@5316
   882
by Auto_tac;
nipkow@2608
   883
bind_thm("dropWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   884
Addsimps [dropWhile_append1];
nipkow@2608
   885
nipkow@4935
   886
Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
paulson@3457
   887
by (induct_tac "xs" 1);
paulson@5316
   888
by Auto_tac;
nipkow@2608
   889
bind_thm("dropWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   890
Addsimps [dropWhile_append2];
nipkow@2608
   891
nipkow@4935
   892
Goal "x:set(takeWhile P xs) --> x:set xs & P x";
paulson@3457
   893
by (induct_tac "xs" 1);
paulson@5316
   894
by Auto_tac;
paulson@3647
   895
qed_spec_mp"set_take_whileD";
nipkow@2608
   896
nipkow@6306
   897
(** zip **)
nipkow@6306
   898
section "zip";
nipkow@6306
   899
nipkow@6306
   900
Goal "zip [] ys = []";
paulson@6813
   901
by (induct_tac "ys" 1);
nipkow@6306
   902
by Auto_tac;
nipkow@6306
   903
qed "zip_Nil";
nipkow@6306
   904
Addsimps [zip_Nil];
nipkow@6306
   905
nipkow@6306
   906
Goal "zip (x#xs) (y#ys) = (x,y)#zip xs ys";
paulson@6813
   907
by (Simp_tac 1);
nipkow@6306
   908
qed "zip_Cons_Cons";
nipkow@6306
   909
Addsimps [zip_Cons_Cons];
nipkow@6306
   910
nipkow@6306
   911
Delsimps(tl (thms"zip.simps"));
nipkow@4605
   912
nipkow@5272
   913
nipkow@5272
   914
(** foldl **)
nipkow@5272
   915
section "foldl";
nipkow@5272
   916
nipkow@5272
   917
Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys";
paulson@5318
   918
by (induct_tac "xs" 1);
paulson@5316
   919
by Auto_tac;
nipkow@5272
   920
qed_spec_mp "foldl_append";
nipkow@5272
   921
Addsimps [foldl_append];
nipkow@5272
   922
nipkow@5272
   923
(* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
nipkow@5272
   924
   because it requires an additional transitivity step
nipkow@5272
   925
*)
nipkow@5272
   926
Goal "!n::nat. m <= n --> m <= foldl op+ n ns";
paulson@5318
   927
by (induct_tac "ns" 1);
nipkow@6058
   928
by Auto_tac;
nipkow@5272
   929
qed_spec_mp "start_le_sum";
nipkow@5272
   930
nipkow@5272
   931
Goal "n : set ns ==> n <= foldl op+ 0 ns";
oheimb@5758
   932
by (force_tac (claset() addIs [start_le_sum],
oheimb@5758
   933
              simpset() addsimps [in_set_conv_decomp]) 1);
nipkow@5272
   934
qed "elem_le_sum";
nipkow@5272
   935
nipkow@5272
   936
Goal "!m. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))";
paulson@5318
   937
by (induct_tac "ns" 1);
paulson@5316
   938
by Auto_tac;
nipkow@5272
   939
qed_spec_mp "sum_eq_0_conv";
nipkow@5272
   940
AddIffs [sum_eq_0_conv];
nipkow@5272
   941
nipkow@5425
   942
(** upto **)
nipkow@5425
   943
nipkow@5427
   944
(* Does not terminate! *)
nipkow@5427
   945
Goal "[i..j(] = (if i<j then i#[Suc i..j(] else [])";
paulson@6162
   946
by (induct_tac "j" 1);
nipkow@5427
   947
by Auto_tac;
nipkow@5427
   948
qed "upt_rec";
nipkow@5425
   949
nipkow@5427
   950
Goal "j<=i ==> [i..j(] = []";
paulson@6162
   951
by (stac upt_rec 1);
paulson@6162
   952
by (Asm_simp_tac 1);
nipkow@5427
   953
qed "upt_conv_Nil";
nipkow@5427
   954
Addsimps [upt_conv_Nil];
nipkow@5427
   955
nipkow@5427
   956
Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]";
nipkow@5427
   957
by (Asm_simp_tac 1);
nipkow@5427
   958
qed "upt_Suc";
nipkow@5427
   959
nipkow@5427
   960
Goal "i<j ==> [i..j(] = i#[Suc i..j(]";
paulson@6162
   961
by (rtac trans 1);
paulson@6162
   962
by (stac upt_rec 1);
paulson@6162
   963
by (rtac refl 2);
nipkow@5427
   964
by (Asm_simp_tac 1);
nipkow@5427
   965
qed "upt_conv_Cons";
nipkow@5427
   966
nipkow@5427
   967
Goal "length [i..j(] = j-i";
paulson@6162
   968
by (induct_tac "j" 1);
nipkow@5427
   969
 by (Simp_tac 1);
paulson@6162
   970
by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
nipkow@5427
   971
qed "length_upt";
nipkow@5427
   972
Addsimps [length_upt];
nipkow@5425
   973
nipkow@5427
   974
Goal "i+k < j --> [i..j(] ! k = i+k";
paulson@6162
   975
by (induct_tac "j" 1);
paulson@6162
   976
 by (Simp_tac 1);
paulson@6162
   977
by (asm_simp_tac (simpset() addsimps [nth_append,less_diff_conv]@add_ac) 1);
paulson@6162
   978
by (Clarify_tac 1);
paulson@6162
   979
by (subgoal_tac "n=i+k" 1);
paulson@6162
   980
 by (Asm_simp_tac 2);
paulson@6162
   981
by (Asm_simp_tac 1);
nipkow@5427
   982
qed_spec_mp "nth_upt";
nipkow@5427
   983
Addsimps [nth_upt];
nipkow@5425
   984
nipkow@6433
   985
Goal "!i. i+m <= n --> take m [i..n(] = [i..i+m(]";
paulson@6813
   986
by (induct_tac "m" 1);
paulson@6813
   987
 by (Simp_tac 1);
paulson@6813
   988
by (Clarify_tac 1);
paulson@6813
   989
by (stac upt_rec 1);
paulson@6813
   990
by (rtac sym 1);
paulson@6813
   991
by (stac upt_rec 1);
paulson@6813
   992
by (asm_simp_tac (simpset() delsimps (thms"upt.simps")) 1);
nipkow@6433
   993
qed_spec_mp "take_upt";
nipkow@6433
   994
Addsimps [take_upt];
nipkow@6433
   995
nipkow@6433
   996
Goal "!m i. i < n-m --> (map f [m..n(]) ! i = f(m+i)";
paulson@6813
   997
by (induct_tac "n" 1);
paulson@6813
   998
 by (Simp_tac 1);
paulson@6813
   999
by (Clarify_tac 1);
paulson@6813
  1000
by (subgoal_tac "m < Suc n" 1);
paulson@6813
  1001
 by (arith_tac 2);
paulson@6813
  1002
by (stac upt_rec 1);
paulson@6813
  1003
by (asm_simp_tac (simpset() delsplits [split_if]) 1);
paulson@6813
  1004
by (split_tac [split_if] 1);
paulson@6813
  1005
by (rtac conjI 1);
paulson@6813
  1006
 by (simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
paulson@6813
  1007
 by (simp_tac (simpset() addsimps [nth_append] addsplits [nat.split]) 1);
paulson@6813
  1008
 by (Clarify_tac 1);
paulson@6813
  1009
 by (rtac conjI 1);
paulson@6813
  1010
  by (Clarify_tac 1);
paulson@6813
  1011
  by (subgoal_tac "Suc(m+nat) < n" 1);
paulson@6813
  1012
   by (arith_tac 2);
paulson@6813
  1013
  by (Asm_simp_tac 1);
paulson@6813
  1014
 by (Clarify_tac 1);
paulson@6813
  1015
 by (subgoal_tac "n = Suc(m+nat)" 1);
paulson@6813
  1016
  by (arith_tac 2);
paulson@6813
  1017
 by (Asm_simp_tac 1);
paulson@6813
  1018
by (simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
paulson@6813
  1019
by (arith_tac 1);
nipkow@6433
  1020
qed_spec_mp "nth_map_upt";
nipkow@6433
  1021
paulson@6813
  1022
Goal "ALL xs ys. k <= length xs --> k <= length ys -->  \
paulson@6813
  1023
\        (ALL i. i < k --> xs!i = ys!i)  \
paulson@6813
  1024
\     --> take k xs = take k ys";
paulson@6813
  1025
by (induct_tac "k" 1);
paulson@6813
  1026
by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq_0_disj, 
paulson@6813
  1027
						all_conj_distrib])));
paulson@6813
  1028
by (Clarify_tac 1);
paulson@6813
  1029
(*Both lists must be non-empty*)
paulson@6813
  1030
by (exhaust_tac "xs" 1);
paulson@6813
  1031
by (exhaust_tac "ys" 2);
paulson@6813
  1032
by (ALLGOALS Clarify_tac);
paulson@6813
  1033
(*prenexing's needed, not miniscoping*)
paulson@6813
  1034
by (ALLGOALS (full_simp_tac (simpset() addsimps (all_simps RL [sym])  
paulson@6813
  1035
                                       delsimps (all_simps))));
paulson@6813
  1036
by (Blast_tac 1);
paulson@6813
  1037
qed_spec_mp "nth_take_lemma";
paulson@6813
  1038
paulson@6813
  1039
Goal "[| length xs = length ys;  \
paulson@6813
  1040
\        ALL i. i < length xs --> xs!i = ys!i |]  \
paulson@6813
  1041
\     ==> xs = ys";
paulson@6813
  1042
by (forward_tac [[le_refl, eq_imp_le] MRS nth_take_lemma] 1);
paulson@6813
  1043
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [take_all])));
paulson@6813
  1044
qed_spec_mp "nth_equalityI";
paulson@6813
  1045
paulson@6813
  1046
(*The famous take-lemma*)
paulson@6813
  1047
Goal "(ALL i. take i xs = take i ys) ==> xs = ys";
paulson@6813
  1048
by (dres_inst_tac [("x", "max (length xs) (length ys)")] spec 1);
paulson@6813
  1049
by (full_simp_tac (simpset() addsimps [le_max_iff_disj, take_all]) 1);
paulson@6813
  1050
qed_spec_mp "take_equalityI";
paulson@6813
  1051
nipkow@5272
  1052
nipkow@4605
  1053
(** nodups & remdups **)
nipkow@4605
  1054
section "nodups & remdups";
nipkow@4605
  1055
nipkow@4935
  1056
Goal "set(remdups xs) = set xs";
nipkow@4605
  1057
by (induct_tac "xs" 1);
nipkow@4605
  1058
 by (Simp_tac 1);
nipkow@4686
  1059
by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
nipkow@4605
  1060
qed "set_remdups";
nipkow@4605
  1061
Addsimps [set_remdups];
nipkow@4605
  1062
nipkow@4935
  1063
Goal "nodups(remdups xs)";
nipkow@4605
  1064
by (induct_tac "xs" 1);
paulson@5316
  1065
by Auto_tac;
nipkow@4605
  1066
qed "nodups_remdups";
nipkow@4605
  1067
nipkow@4935
  1068
Goal "nodups xs --> nodups (filter P xs)";
nipkow@4605
  1069
by (induct_tac "xs" 1);
paulson@5316
  1070
by Auto_tac;
nipkow@4605
  1071
qed_spec_mp "nodups_filter";
nipkow@4605
  1072
nipkow@3589
  1073
(** replicate **)
nipkow@3589
  1074
section "replicate";
nipkow@3589
  1075
nipkow@6794
  1076
Goal "length(replicate n x) = n";
paulson@6813
  1077
by (induct_tac "n" 1);
paulson@6813
  1078
by Auto_tac;
nipkow@6794
  1079
qed "length_replicate";
nipkow@6794
  1080
Addsimps [length_replicate];
nipkow@6794
  1081
nipkow@6794
  1082
Goal "map f (replicate n x) = replicate n (f x)";
nipkow@6794
  1083
by (induct_tac "n" 1);
paulson@6813
  1084
by Auto_tac;
nipkow@6794
  1085
qed "map_replicate";
nipkow@6794
  1086
Addsimps [map_replicate];
nipkow@6794
  1087
nipkow@6794
  1088
Goal "(replicate n x) @ (x#xs) = x # replicate n x @ xs";
nipkow@6794
  1089
by (induct_tac "n" 1);
paulson@6813
  1090
by Auto_tac;
nipkow@6794
  1091
qed "replicate_app_Cons_same";
nipkow@6794
  1092
nipkow@6794
  1093
Goal "rev(replicate n x) = replicate n x";
nipkow@6794
  1094
by (induct_tac "n" 1);
paulson@6813
  1095
 by (Simp_tac 1);
nipkow@6794
  1096
by (asm_simp_tac (simpset() addsimps [replicate_app_Cons_same]) 1);
nipkow@6794
  1097
qed "rev_replicate";
nipkow@6794
  1098
Addsimps [rev_replicate];
nipkow@6794
  1099
nipkow@6794
  1100
Goal"n ~= 0 --> hd(replicate n x) = x";
nipkow@6794
  1101
by (induct_tac "n" 1);
paulson@6813
  1102
by Auto_tac;
nipkow@6794
  1103
qed_spec_mp "hd_replicate";
nipkow@6794
  1104
Addsimps [hd_replicate];
nipkow@6794
  1105
nipkow@6794
  1106
Goal "n ~= 0 --> tl(replicate n x) = replicate (n-1) x";
nipkow@6794
  1107
by (induct_tac "n" 1);
paulson@6813
  1108
by Auto_tac;
nipkow@6794
  1109
qed_spec_mp "tl_replicate";
nipkow@6794
  1110
Addsimps [tl_replicate];
nipkow@6794
  1111
nipkow@6794
  1112
Goal "n ~= 0 --> last(replicate n x) = x";
nipkow@6794
  1113
by (induct_tac "n" 1);
paulson@6813
  1114
by Auto_tac;
nipkow@6794
  1115
qed_spec_mp "last_replicate";
nipkow@6794
  1116
Addsimps [last_replicate];
nipkow@6794
  1117
nipkow@6794
  1118
Goal "!i. i<n --> (replicate n x)!i = x";
paulson@6813
  1119
by (induct_tac "n" 1);
paulson@6813
  1120
 by (Simp_tac 1);
paulson@6813
  1121
by (asm_simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
nipkow@6794
  1122
qed_spec_mp "nth_replicate";
nipkow@6794
  1123
Addsimps [nth_replicate];
nipkow@6794
  1124
nipkow@4935
  1125
Goal "set(replicate (Suc n) x) = {x}";
wenzelm@4423
  1126
by (induct_tac "n" 1);
paulson@5316
  1127
by Auto_tac;
nipkow@3589
  1128
val lemma = result();
nipkow@3589
  1129
nipkow@5043
  1130
Goal "n ~= 0 ==> set(replicate n x) = {x}";
wenzelm@4423
  1131
by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
nipkow@3589
  1132
qed "set_replicate";
nipkow@3589
  1133
Addsimps [set_replicate];
nipkow@5162
  1134
nipkow@6794
  1135
Goal "replicate (n+m) x = replicate n x @ replicate m x";
nipkow@6794
  1136
by (induct_tac "n" 1);
nipkow@6794
  1137
by Auto_tac;
nipkow@6794
  1138
qed "replicate_add";
nipkow@5162
  1139
nipkow@5281
  1140
(*** Lexcicographic orderings on lists ***)
nipkow@5281
  1141
section"Lexcicographic orderings on lists";
nipkow@5281
  1142
nipkow@5281
  1143
Goal "wf r ==> wf(lexn r n)";
paulson@5318
  1144
by (induct_tac "n" 1);
paulson@5318
  1145
by (Simp_tac 1);
paulson@5318
  1146
by (Simp_tac 1);
paulson@5318
  1147
by (rtac wf_subset 1);
paulson@5318
  1148
by (rtac Int_lower1 2);
paulson@5318
  1149
by (rtac wf_prod_fun_image 1);
paulson@5318
  1150
by (rtac injI 2);
paulson@6813
  1151
by Auto_tac;
nipkow@5281
  1152
qed "wf_lexn";
nipkow@5281
  1153
nipkow@5281
  1154
Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n";
paulson@5318
  1155
by (induct_tac "n" 1);
paulson@6813
  1156
by Auto_tac;
nipkow@5281
  1157
qed_spec_mp "lexn_length";
nipkow@5281
  1158
nipkow@5281
  1159
Goalw [lex_def] "wf r ==> wf(lex r)";
paulson@5318
  1160
by (rtac wf_UN 1);
paulson@5318
  1161
by (blast_tac (claset() addIs [wf_lexn]) 1);
paulson@5318
  1162
by (Clarify_tac 1);
paulson@5318
  1163
by (rename_tac "m n" 1);
paulson@5318
  1164
by (subgoal_tac "m ~= n" 1);
paulson@5318
  1165
 by (Blast_tac 2);
paulson@5318
  1166
by (blast_tac (claset() addDs [lexn_length,not_sym]) 1);
nipkow@5281
  1167
qed "wf_lex";
nipkow@5281
  1168
AddSIs [wf_lex];
nipkow@5281
  1169
nipkow@5281
  1170
Goal
nipkow@5281
  1171
 "lexn r n = \
nipkow@5281
  1172
\ {(xs,ys). length xs = n & length ys = n & \
nipkow@5281
  1173
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5318
  1174
by (induct_tac "n" 1);
paulson@5318
  1175
 by (Simp_tac 1);
paulson@5318
  1176
 by (Blast_tac 1);
paulson@5641
  1177
by (asm_full_simp_tac (simpset() 
oheimb@5296
  1178
				addsimps [lex_prod_def]) 1);
paulson@5641
  1179
by (auto_tac (claset(), simpset()));
paulson@5318
  1180
  by (Blast_tac 1);
paulson@5318
  1181
 by (rename_tac "a xys x xs' y ys'" 1);
paulson@5318
  1182
 by (res_inst_tac [("x","a#xys")] exI 1);
paulson@5318
  1183
 by (Simp_tac 1);
paulson@5318
  1184
by (exhaust_tac "xys" 1);
paulson@5641
  1185
 by (ALLGOALS (asm_full_simp_tac (simpset())));
paulson@5318
  1186
by (Blast_tac 1);
nipkow@5281
  1187
qed "lexn_conv";
nipkow@5281
  1188
nipkow@5281
  1189
Goalw [lex_def]
nipkow@5281
  1190
 "lex r = \
nipkow@5281
  1191
\ {(xs,ys). length xs = length ys & \
nipkow@5281
  1192
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5641
  1193
by (force_tac (claset(), simpset() addsimps [lexn_conv]) 1);
nipkow@5281
  1194
qed "lex_conv";
nipkow@5281
  1195
nipkow@5281
  1196
Goalw [lexico_def] "wf r ==> wf(lexico r)";
paulson@5318
  1197
by (Blast_tac 1);
nipkow@5281
  1198
qed "wf_lexico";
nipkow@5281
  1199
AddSIs [wf_lexico];
nipkow@5281
  1200
nipkow@5281
  1201
Goalw
nipkow@5281
  1202
 [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def]
nipkow@5281
  1203
"lexico r = {(xs,ys). length xs < length ys | \
nipkow@5281
  1204
\                     length xs = length ys & (xs,ys) : lex r}";
paulson@5318
  1205
by (Simp_tac 1);
nipkow@5281
  1206
qed "lexico_conv";
nipkow@5281
  1207
nipkow@5283
  1208
Goal "([],ys) ~: lex r";
paulson@5318
  1209
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1210
qed "Nil_notin_lex";
nipkow@5283
  1211
nipkow@5283
  1212
Goal "(xs,[]) ~: lex r";
paulson@5318
  1213
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1214
qed "Nil2_notin_lex";
nipkow@5283
  1215
nipkow@5283
  1216
AddIffs [Nil_notin_lex,Nil2_notin_lex];
nipkow@5283
  1217
nipkow@5283
  1218
Goal "((x#xs,y#ys) : lex r) = \
nipkow@5283
  1219
\     ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)";
paulson@5318
  1220
by (simp_tac (simpset() addsimps [lex_conv]) 1);
paulson@5318
  1221
by (rtac iffI 1);
paulson@5318
  1222
 by (blast_tac (claset() addIs [Cons_eq_appendI]) 2);
paulson@5318
  1223
by (REPEAT(eresolve_tac [conjE, exE] 1));
paulson@5318
  1224
by (exhaust_tac "xys" 1);
paulson@5318
  1225
by (Asm_full_simp_tac 1);
paulson@5318
  1226
by (Asm_full_simp_tac 1);
paulson@5318
  1227
by (Blast_tac 1);
nipkow@5283
  1228
qed "Cons_in_lex";
nipkow@5283
  1229
AddIffs [Cons_in_lex];