src/HOL/List.ML
author nipkow
Fri Sep 04 11:01:59 1998 +0200 (1998-09-04)
changeset 5427 26c9a7c0b36b
parent 5425 157c6663dedd
child 5443 e2459d18ff47
permissions -rw-r--r--
Arith: less_diff_conv
List: [i..j]
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@3468
    35
val listsE = lists.mk_cases list.simps  "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
AddIffs [length_Suc_conv];
oheimb@5296
   119
clasohm@923
   120
(** @ - append **)
clasohm@923
   121
nipkow@3467
   122
section "@ - append";
nipkow@3467
   123
nipkow@4935
   124
Goal "(xs@ys)@zs = xs@(ys@zs)";
nipkow@3040
   125
by (induct_tac "xs" 1);
paulson@5316
   126
by Auto_tac;
clasohm@923
   127
qed "append_assoc";
nipkow@2512
   128
Addsimps [append_assoc];
clasohm@923
   129
nipkow@4935
   130
Goal "xs @ [] = xs";
nipkow@3040
   131
by (induct_tac "xs" 1);
paulson@5316
   132
by Auto_tac;
clasohm@923
   133
qed "append_Nil2";
nipkow@2512
   134
Addsimps [append_Nil2];
clasohm@923
   135
nipkow@4935
   136
Goal "(xs@ys = []) = (xs=[] & ys=[])";
nipkow@3040
   137
by (induct_tac "xs" 1);
paulson@5316
   138
by Auto_tac;
nipkow@2608
   139
qed "append_is_Nil_conv";
nipkow@2608
   140
AddIffs [append_is_Nil_conv];
nipkow@2608
   141
nipkow@4935
   142
Goal "([] = xs@ys) = (xs=[] & ys=[])";
nipkow@3040
   143
by (induct_tac "xs" 1);
paulson@5316
   144
by Auto_tac;
nipkow@2608
   145
qed "Nil_is_append_conv";
nipkow@2608
   146
AddIffs [Nil_is_append_conv];
clasohm@923
   147
nipkow@4935
   148
Goal "(xs @ ys = xs) = (ys=[])";
nipkow@3574
   149
by (induct_tac "xs" 1);
paulson@5316
   150
by Auto_tac;
nipkow@3574
   151
qed "append_self_conv";
nipkow@3574
   152
nipkow@4935
   153
Goal "(xs = xs @ ys) = (ys=[])";
nipkow@3574
   154
by (induct_tac "xs" 1);
paulson@5316
   155
by Auto_tac;
nipkow@3574
   156
qed "self_append_conv";
nipkow@3574
   157
AddIffs [append_self_conv,self_append_conv];
nipkow@3574
   158
nipkow@4935
   159
Goal "!ys. length xs = length ys | length us = length vs \
nipkow@3860
   160
\              --> (xs@us = ys@vs) = (xs=ys & us=vs)";
wenzelm@4423
   161
by (induct_tac "xs" 1);
wenzelm@4423
   162
 by (rtac allI 1);
wenzelm@4423
   163
 by (exhaust_tac "ys" 1);
wenzelm@4423
   164
  by (Asm_simp_tac 1);
wenzelm@4423
   165
 by (fast_tac (claset() addIs [less_add_Suc2] addss simpset()
nipkow@3860
   166
                      addEs [less_not_refl2 RSN (2,rev_notE)]) 1);
wenzelm@4423
   167
by (rtac allI 1);
wenzelm@4423
   168
by (exhaust_tac "ys" 1);
oheimb@5296
   169
by (fast_tac (claset() addIs [less_add_Suc2] 
oheimb@5296
   170
		       addss (simpset() delsimps [length_Suc_conv])
paulson@5355
   171
                       addEs [(less_not_refl3) RSN (2,rev_notE)]) 1);
wenzelm@4423
   172
by (Asm_simp_tac 1);
nipkow@3860
   173
qed_spec_mp "append_eq_append_conv";
nipkow@3860
   174
Addsimps [append_eq_append_conv];
nipkow@3860
   175
nipkow@4935
   176
Goal "(xs @ ys = xs @ zs) = (ys=zs)";
nipkow@3896
   177
by (Simp_tac 1);
nipkow@3896
   178
qed "same_append_eq";
nipkow@3860
   179
nipkow@4935
   180
Goal "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)"; 
nipkow@3896
   181
by (Simp_tac 1);
nipkow@3896
   182
qed "append1_eq_conv";
nipkow@2608
   183
nipkow@4935
   184
Goal "(ys @ xs = zs @ xs) = (ys=zs)";
nipkow@3896
   185
by (Simp_tac 1);
nipkow@3896
   186
qed "append_same_eq";
nipkow@2608
   187
nipkow@3896
   188
AddSIs
nipkow@3896
   189
 [same_append_eq RS iffD2, append1_eq_conv RS iffD2, append_same_eq RS iffD2];
nipkow@3896
   190
AddSDs
nipkow@3896
   191
 [same_append_eq RS iffD1, append1_eq_conv RS iffD1, append_same_eq RS iffD1];
nipkow@3571
   192
nipkow@4935
   193
Goal "(xs @ ys = ys) = (xs=[])";
wenzelm@5132
   194
by (cut_inst_tac [("zs","[]")] append_same_eq 1);
paulson@5316
   195
by Auto_tac;
nipkow@4647
   196
qed "append_self_conv2";
nipkow@4647
   197
nipkow@4935
   198
Goal "(ys = xs @ ys) = (xs=[])";
wenzelm@5132
   199
by (simp_tac (simpset() addsimps
nipkow@4647
   200
     [simplify (simpset()) (read_instantiate[("ys","[]")]append_same_eq)]) 1);
wenzelm@5132
   201
by (Blast_tac 1);
nipkow@4647
   202
qed "self_append_conv2";
nipkow@4647
   203
AddIffs [append_self_conv2,self_append_conv2];
nipkow@4647
   204
nipkow@4935
   205
Goal "xs ~= [] --> hd xs # tl xs = xs";
paulson@3457
   206
by (induct_tac "xs" 1);
paulson@5316
   207
by Auto_tac;
nipkow@2608
   208
qed_spec_mp "hd_Cons_tl";
nipkow@2608
   209
Addsimps [hd_Cons_tl];
clasohm@923
   210
nipkow@4935
   211
Goal "hd(xs@ys) = (if xs=[] then hd ys else hd xs)";
nipkow@3040
   212
by (induct_tac "xs" 1);
paulson@5316
   213
by Auto_tac;
nipkow@1327
   214
qed "hd_append";
clasohm@923
   215
nipkow@5043
   216
Goal "xs ~= [] ==> hd(xs @ ys) = hd xs";
wenzelm@4089
   217
by (asm_simp_tac (simpset() addsimps [hd_append]
berghofe@5183
   218
                           addsplits [list.split]) 1);
nipkow@3571
   219
qed "hd_append2";
nipkow@3571
   220
Addsimps [hd_append2];
nipkow@3571
   221
nipkow@4935
   222
Goal "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)";
berghofe@5183
   223
by (simp_tac (simpset() addsplits [list.split]) 1);
nipkow@2608
   224
qed "tl_append";
nipkow@2608
   225
nipkow@5043
   226
Goal "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys";
wenzelm@4089
   227
by (asm_simp_tac (simpset() addsimps [tl_append]
berghofe@5183
   228
                           addsplits [list.split]) 1);
nipkow@3571
   229
qed "tl_append2";
nipkow@3571
   230
Addsimps [tl_append2];
nipkow@3571
   231
nipkow@5272
   232
(* trivial rules for solving @-equations automatically *)
nipkow@5272
   233
nipkow@5272
   234
Goal "xs = ys ==> xs = [] @ ys";
paulson@5318
   235
by (Asm_simp_tac 1);
nipkow@5272
   236
qed "eq_Nil_appendI";
nipkow@5272
   237
nipkow@5272
   238
Goal "[| x#xs1 = ys; xs = xs1 @ zs |] ==> x#xs = ys@zs";
paulson@5318
   239
by (dtac sym 1);
paulson@5318
   240
by (Asm_simp_tac 1);
nipkow@5272
   241
qed "Cons_eq_appendI";
nipkow@5272
   242
nipkow@5272
   243
Goal "[| xs@xs1 = zs; ys = xs1 @ us |] ==> xs@ys = zs@us";
paulson@5318
   244
by (dtac sym 1);
paulson@5318
   245
by (Asm_simp_tac 1);
nipkow@5272
   246
qed "append_eq_appendI";
nipkow@5272
   247
nipkow@4830
   248
nipkow@5427
   249
(***
nipkow@5427
   250
Simplification procedure for all list equalities.
nipkow@5427
   251
Currently only tries to rearranges @ to see if
nipkow@5427
   252
- both lists end in a singleton list,
nipkow@5427
   253
- or both lists end in the same list.
nipkow@5427
   254
***)
nipkow@5427
   255
local
nipkow@5427
   256
nipkow@5427
   257
val list_eq_pattern =
nipkow@5427
   258
  read_cterm (sign_of List.thy) ("(xs::'a list) = ys",HOLogic.boolT);
nipkow@5427
   259
nipkow@5427
   260
fun last (cons as Const("List.list.op #",_) $ _ $ xs) =
nipkow@5427
   261
      (case xs of Const("List.list.[]",_) => cons | _ => last xs)
nipkow@5427
   262
  | last (Const("List.op @",_) $ _ $ ys) = last ys
nipkow@5427
   263
  | last t = t;
nipkow@5427
   264
nipkow@5427
   265
fun list1 (Const("List.list.op #",_) $ _ $ Const("List.list.[]",_)) = true
nipkow@5427
   266
  | list1 _ = false;
nipkow@5427
   267
nipkow@5427
   268
fun butlast ((cons as Const("List.list.op #",_) $ x) $ xs) =
nipkow@5427
   269
      (case xs of Const("List.list.[]",_) => xs | _ => cons $ butlast xs)
nipkow@5427
   270
  | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
nipkow@5427
   271
  | butlast xs = Const("List.list.[]",fastype_of xs);
nipkow@5427
   272
nipkow@5427
   273
val rearr_tac =
nipkow@5427
   274
  simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons]);
nipkow@5427
   275
nipkow@5427
   276
fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
nipkow@5427
   277
  let
nipkow@5427
   278
    val lastl = last lhs and lastr = last rhs
nipkow@5427
   279
    fun rearr conv =
nipkow@5427
   280
      let val lhs1 = butlast lhs and rhs1 = butlast rhs
nipkow@5427
   281
          val Type(_,listT::_) = eqT
nipkow@5427
   282
          val appT = [listT,listT] ---> listT
nipkow@5427
   283
          val app = Const("List.op @",appT)
nipkow@5427
   284
          val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
nipkow@5427
   285
          val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2)))
nipkow@5427
   286
          val thm = prove_goalw_cterm [] ct (K [rearr_tac 1])
nipkow@5427
   287
            handle ERROR =>
nipkow@5427
   288
            error("The error(s) above occurred while trying to prove " ^
nipkow@5427
   289
                  string_of_cterm ct)
nipkow@5427
   290
      in Some((conv RS (thm RS trans)) RS eq_reflection) end
nipkow@5427
   291
nipkow@5427
   292
  in if list1 lastl andalso list1 lastr
nipkow@5427
   293
     then rearr append1_eq_conv
nipkow@5427
   294
     else
nipkow@5427
   295
     if lastl aconv lastr
nipkow@5427
   296
     then rearr append_same_eq
nipkow@5427
   297
     else None
nipkow@5427
   298
  end;
nipkow@5427
   299
in
nipkow@5427
   300
val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq;
nipkow@5427
   301
end;
nipkow@5427
   302
nipkow@5427
   303
Addsimprocs [list_eq_simproc];
nipkow@5427
   304
nipkow@5427
   305
nipkow@2608
   306
(** map **)
nipkow@2608
   307
nipkow@3467
   308
section "map";
nipkow@3467
   309
paulson@5278
   310
Goal "(!x. x : set xs --> f x = g x) --> map f xs = map g xs";
paulson@3457
   311
by (induct_tac "xs" 1);
paulson@5316
   312
by Auto_tac;
nipkow@2608
   313
bind_thm("map_ext", impI RS (allI RS (result() RS mp)));
nipkow@2608
   314
nipkow@4935
   315
Goal "map (%x. x) = (%xs. xs)";
nipkow@2608
   316
by (rtac ext 1);
nipkow@3040
   317
by (induct_tac "xs" 1);
paulson@5316
   318
by Auto_tac;
nipkow@2608
   319
qed "map_ident";
nipkow@2608
   320
Addsimps[map_ident];
nipkow@2608
   321
nipkow@4935
   322
Goal "map f (xs@ys) = map f xs @ map f ys";
nipkow@3040
   323
by (induct_tac "xs" 1);
paulson@5316
   324
by Auto_tac;
nipkow@2608
   325
qed "map_append";
nipkow@2608
   326
Addsimps[map_append];
nipkow@2608
   327
nipkow@4935
   328
Goalw [o_def] "map (f o g) xs = map f (map g xs)";
nipkow@3040
   329
by (induct_tac "xs" 1);
paulson@5316
   330
by Auto_tac;
nipkow@2608
   331
qed "map_compose";
nipkow@2608
   332
Addsimps[map_compose];
nipkow@2608
   333
nipkow@4935
   334
Goal "rev(map f xs) = map f (rev xs)";
nipkow@3040
   335
by (induct_tac "xs" 1);
paulson@5316
   336
by Auto_tac;
nipkow@2608
   337
qed "rev_map";
nipkow@2608
   338
nipkow@3589
   339
(* a congruence rule for map: *)
paulson@5278
   340
Goal "(xs=ys) --> (!x. x : set ys --> f x = g x) --> map f xs = map g ys";
wenzelm@4423
   341
by (rtac impI 1);
wenzelm@4423
   342
by (hyp_subst_tac 1);
wenzelm@4423
   343
by (induct_tac "ys" 1);
paulson@5316
   344
by Auto_tac;
nipkow@3589
   345
val lemma = result();
nipkow@3589
   346
bind_thm("map_cong",impI RSN (2,allI RSN (2,lemma RS mp RS mp)));
nipkow@3589
   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 "map_is_Nil_conv";
nipkow@3860
   352
AddIffs [map_is_Nil_conv];
nipkow@3860
   353
nipkow@4935
   354
Goal "([] = map f xs) = (xs = [])";
wenzelm@4423
   355
by (induct_tac "xs" 1);
paulson@5316
   356
by Auto_tac;
nipkow@3860
   357
qed "Nil_is_map_conv";
nipkow@3860
   358
AddIffs [Nil_is_map_conv];
nipkow@3860
   359
nipkow@3860
   360
lcp@1169
   361
(** rev **)
lcp@1169
   362
nipkow@3467
   363
section "rev";
nipkow@3467
   364
nipkow@4935
   365
Goal "rev(xs@ys) = rev(ys) @ rev(xs)";
nipkow@3040
   366
by (induct_tac "xs" 1);
paulson@5316
   367
by Auto_tac;
lcp@1169
   368
qed "rev_append";
nipkow@2512
   369
Addsimps[rev_append];
lcp@1169
   370
nipkow@4935
   371
Goal "rev(rev l) = l";
nipkow@3040
   372
by (induct_tac "l" 1);
paulson@5316
   373
by Auto_tac;
lcp@1169
   374
qed "rev_rev_ident";
nipkow@2512
   375
Addsimps[rev_rev_ident];
lcp@1169
   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 "rev_is_Nil_conv";
nipkow@3860
   381
AddIffs [rev_is_Nil_conv];
nipkow@3860
   382
nipkow@4935
   383
Goal "([] = rev xs) = (xs = [])";
wenzelm@4423
   384
by (induct_tac "xs" 1);
paulson@5316
   385
by Auto_tac;
nipkow@3860
   386
qed "Nil_is_rev_conv";
nipkow@3860
   387
AddIffs [Nil_is_rev_conv];
nipkow@3860
   388
nipkow@4935
   389
val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
wenzelm@5132
   390
by (stac (rev_rev_ident RS sym) 1);
nipkow@4935
   391
br(read_instantiate [("P","%xs. ?P(rev xs)")]list.induct)1;
wenzelm@5132
   392
by (ALLGOALS Simp_tac);
wenzelm@5132
   393
by (resolve_tac prems 1);
wenzelm@5132
   394
by (eresolve_tac prems 1);
nipkow@4935
   395
qed "rev_induct";
nipkow@4935
   396
nipkow@5272
   397
fun rev_induct_tac xs = res_inst_tac [("xs",xs)] rev_induct;
nipkow@5272
   398
nipkow@4935
   399
Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
wenzelm@5132
   400
by (res_inst_tac [("xs","xs")] rev_induct 1);
paulson@5316
   401
by Auto_tac;
nipkow@4935
   402
bind_thm ("rev_exhaust",
nipkow@4935
   403
  impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp)))));
nipkow@4935
   404
nipkow@2608
   405
clasohm@923
   406
(** mem **)
clasohm@923
   407
nipkow@3467
   408
section "mem";
nipkow@3467
   409
nipkow@4935
   410
Goal "x mem (xs@ys) = (x mem xs | x mem ys)";
nipkow@3040
   411
by (induct_tac "xs" 1);
paulson@5316
   412
by Auto_tac;
clasohm@923
   413
qed "mem_append";
nipkow@2512
   414
Addsimps[mem_append];
clasohm@923
   415
nipkow@4935
   416
Goal "x mem [x:xs. P(x)] = (x mem xs & P(x))";
nipkow@3040
   417
by (induct_tac "xs" 1);
paulson@5316
   418
by Auto_tac;
clasohm@923
   419
qed "mem_filter";
nipkow@2512
   420
Addsimps[mem_filter];
clasohm@923
   421
nipkow@3465
   422
(** set **)
paulson@1812
   423
nipkow@3467
   424
section "set";
nipkow@3467
   425
oheimb@5296
   426
qed_goal "finite_set" thy "finite (set xs)" 
oheimb@5296
   427
	(K [induct_tac "xs" 1, Auto_tac]);
oheimb@5296
   428
Addsimps[finite_set];
oheimb@5296
   429
AddSIs[finite_set];
oheimb@5296
   430
nipkow@4935
   431
Goal "set (xs@ys) = (set xs Un set ys)";
nipkow@3040
   432
by (induct_tac "xs" 1);
paulson@5316
   433
by Auto_tac;
paulson@3647
   434
qed "set_append";
paulson@3647
   435
Addsimps[set_append];
paulson@1812
   436
nipkow@4935
   437
Goal "(x mem xs) = (x: set xs)";
nipkow@3040
   438
by (induct_tac "xs" 1);
paulson@5316
   439
by Auto_tac;
paulson@3647
   440
qed "set_mem_eq";
paulson@1812
   441
nipkow@4935
   442
Goal "set l <= set (x#l)";
paulson@5316
   443
by Auto_tac;
paulson@3647
   444
qed "set_subset_Cons";
paulson@1936
   445
nipkow@4935
   446
Goal "(set xs = {}) = (xs = [])";
paulson@3457
   447
by (induct_tac "xs" 1);
paulson@5316
   448
by Auto_tac;
paulson@3647
   449
qed "set_empty";
paulson@3647
   450
Addsimps [set_empty];
nipkow@2608
   451
nipkow@4935
   452
Goal "set(rev xs) = set(xs)";
paulson@3457
   453
by (induct_tac "xs" 1);
paulson@5316
   454
by Auto_tac;
paulson@3647
   455
qed "set_rev";
paulson@3647
   456
Addsimps [set_rev];
nipkow@2608
   457
nipkow@4935
   458
Goal "set(map f xs) = f``(set xs)";
paulson@3457
   459
by (induct_tac "xs" 1);
paulson@5316
   460
by Auto_tac;
paulson@3647
   461
qed "set_map";
paulson@3647
   462
Addsimps [set_map];
nipkow@2608
   463
nipkow@4935
   464
Goal "(x : set(filter P xs)) = (x : set xs & P x)";
nipkow@4605
   465
by (induct_tac "xs" 1);
paulson@5316
   466
by Auto_tac;
nipkow@4605
   467
qed "in_set_filter";
nipkow@4605
   468
Addsimps [in_set_filter];
nipkow@4605
   469
nipkow@5272
   470
Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)";
paulson@5318
   471
by (induct_tac "xs" 1);
paulson@5318
   472
 by (Simp_tac 1);
paulson@5318
   473
by (Asm_simp_tac 1);
paulson@5318
   474
by (rtac iffI 1);
paulson@5318
   475
by (blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1);
paulson@5318
   476
by (REPEAT(etac exE 1));
paulson@5318
   477
by (exhaust_tac "ys" 1);
paulson@5316
   478
by Auto_tac;
nipkow@5272
   479
qed "in_set_conv_decomp";
nipkow@5272
   480
nipkow@5272
   481
(* eliminate `lists' in favour of `set' *)
nipkow@5272
   482
nipkow@5272
   483
Goal "(xs : lists A) = (!x : set xs. x : A)";
paulson@5318
   484
by (induct_tac "xs" 1);
paulson@5316
   485
by Auto_tac;
nipkow@5272
   486
qed "in_lists_conv_set";
nipkow@5272
   487
nipkow@5272
   488
bind_thm("in_listsD",in_lists_conv_set RS iffD1);
nipkow@5272
   489
AddSDs [in_listsD];
nipkow@5272
   490
bind_thm("in_listsI",in_lists_conv_set RS iffD2);
nipkow@5272
   491
AddSIs [in_listsI];
paulson@1812
   492
clasohm@923
   493
(** list_all **)
clasohm@923
   494
nipkow@3467
   495
section "list_all";
nipkow@3467
   496
nipkow@4935
   497
Goal "list_all (%x. True) xs = True";
nipkow@3040
   498
by (induct_tac "xs" 1);
paulson@5316
   499
by Auto_tac;
clasohm@923
   500
qed "list_all_True";
nipkow@2512
   501
Addsimps [list_all_True];
clasohm@923
   502
nipkow@4935
   503
Goal "list_all p (xs@ys) = (list_all p xs & list_all p ys)";
nipkow@3040
   504
by (induct_tac "xs" 1);
paulson@5316
   505
by Auto_tac;
nipkow@2512
   506
qed "list_all_append";
nipkow@2512
   507
Addsimps [list_all_append];
clasohm@923
   508
nipkow@4935
   509
Goal "list_all P xs = (!x. x mem xs --> P(x))";
nipkow@3040
   510
by (induct_tac "xs" 1);
paulson@5316
   511
by Auto_tac;
clasohm@923
   512
qed "list_all_mem_conv";
clasohm@923
   513
clasohm@923
   514
nipkow@2608
   515
(** filter **)
clasohm@923
   516
nipkow@3467
   517
section "filter";
nipkow@3467
   518
nipkow@4935
   519
Goal "filter P (xs@ys) = filter P xs @ filter P ys";
paulson@3457
   520
by (induct_tac "xs" 1);
paulson@5316
   521
by Auto_tac;
nipkow@2608
   522
qed "filter_append";
nipkow@2608
   523
Addsimps [filter_append];
nipkow@2608
   524
nipkow@4935
   525
Goal "filter (%x. True) xs = xs";
nipkow@4605
   526
by (induct_tac "xs" 1);
paulson@5316
   527
by Auto_tac;
nipkow@4605
   528
qed "filter_True";
nipkow@4605
   529
Addsimps [filter_True];
nipkow@4605
   530
nipkow@4935
   531
Goal "filter (%x. False) xs = []";
nipkow@4605
   532
by (induct_tac "xs" 1);
paulson@5316
   533
by Auto_tac;
nipkow@4605
   534
qed "filter_False";
nipkow@4605
   535
Addsimps [filter_False];
nipkow@4605
   536
nipkow@4935
   537
Goal "length (filter P xs) <= length xs";
paulson@3457
   538
by (induct_tac "xs" 1);
paulson@5316
   539
by Auto_tac;
nipkow@4605
   540
qed "length_filter";
paulson@3383
   541
nipkow@2608
   542
nipkow@2608
   543
(** concat **)
nipkow@2608
   544
nipkow@3467
   545
section "concat";
nipkow@3467
   546
nipkow@4935
   547
Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
nipkow@3040
   548
by (induct_tac "xs" 1);
paulson@5316
   549
by Auto_tac;
nipkow@2608
   550
qed"concat_append";
nipkow@2608
   551
Addsimps [concat_append];
nipkow@2512
   552
nipkow@4935
   553
Goal "(concat xss = []) = (!xs:set xss. xs=[])";
wenzelm@4423
   554
by (induct_tac "xss" 1);
paulson@5316
   555
by Auto_tac;
nipkow@3896
   556
qed "concat_eq_Nil_conv";
nipkow@3896
   557
AddIffs [concat_eq_Nil_conv];
nipkow@3896
   558
nipkow@4935
   559
Goal "([] = concat xss) = (!xs:set xss. xs=[])";
wenzelm@4423
   560
by (induct_tac "xss" 1);
paulson@5316
   561
by Auto_tac;
nipkow@3896
   562
qed "Nil_eq_concat_conv";
nipkow@3896
   563
AddIffs [Nil_eq_concat_conv];
nipkow@3896
   564
nipkow@4935
   565
Goal  "set(concat xs) = Union(set `` set xs)";
nipkow@3467
   566
by (induct_tac "xs" 1);
paulson@5316
   567
by Auto_tac;
paulson@3647
   568
qed"set_concat";
paulson@3647
   569
Addsimps [set_concat];
nipkow@3467
   570
nipkow@4935
   571
Goal "map f (concat xs) = concat (map (map f) xs)"; 
nipkow@3467
   572
by (induct_tac "xs" 1);
paulson@5316
   573
by Auto_tac;
nipkow@3467
   574
qed "map_concat";
nipkow@3467
   575
nipkow@4935
   576
Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
nipkow@3467
   577
by (induct_tac "xs" 1);
paulson@5316
   578
by Auto_tac;
nipkow@3467
   579
qed"filter_concat"; 
nipkow@3467
   580
nipkow@4935
   581
Goal "rev(concat xs) = concat (map rev (rev xs))";
nipkow@3467
   582
by (induct_tac "xs" 1);
paulson@5316
   583
by Auto_tac;
nipkow@2608
   584
qed "rev_concat";
clasohm@923
   585
clasohm@923
   586
(** nth **)
clasohm@923
   587
nipkow@3467
   588
section "nth";
nipkow@3467
   589
paulson@5278
   590
Goal "!xs. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
berghofe@5183
   591
by (induct_tac "n" 1);
paulson@3457
   592
 by (Asm_simp_tac 1);
paulson@3457
   593
 by (rtac allI 1);
paulson@3457
   594
 by (exhaust_tac "xs" 1);
paulson@5316
   595
  by Auto_tac;
nipkow@2608
   596
qed_spec_mp "nth_append";
nipkow@2608
   597
nipkow@4935
   598
Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
nipkow@3040
   599
by (induct_tac "xs" 1);
nipkow@1301
   600
(* case [] *)
nipkow@1301
   601
by (Asm_full_simp_tac 1);
nipkow@1301
   602
(* case x#xl *)
nipkow@1301
   603
by (rtac allI 1);
berghofe@5183
   604
by (induct_tac "n" 1);
paulson@5316
   605
by Auto_tac;
nipkow@1485
   606
qed_spec_mp "nth_map";
nipkow@1301
   607
Addsimps [nth_map];
nipkow@1301
   608
nipkow@4935
   609
Goal "!n. n < length xs --> list_all P xs --> P(xs!n)";
nipkow@3040
   610
by (induct_tac "xs" 1);
nipkow@1301
   611
(* case [] *)
nipkow@1301
   612
by (Simp_tac 1);
nipkow@1301
   613
(* case x#xl *)
nipkow@1301
   614
by (rtac allI 1);
berghofe@5183
   615
by (induct_tac "n" 1);
paulson@5316
   616
by Auto_tac;
nipkow@1485
   617
qed_spec_mp "list_all_nth";
nipkow@1301
   618
nipkow@4935
   619
Goal "!n. n < length xs --> xs!n mem xs";
nipkow@3040
   620
by (induct_tac "xs" 1);
nipkow@1301
   621
(* case [] *)
nipkow@1301
   622
by (Simp_tac 1);
nipkow@1301
   623
(* case x#xl *)
nipkow@1301
   624
by (rtac allI 1);
berghofe@5183
   625
by (induct_tac "n" 1);
nipkow@1301
   626
(* case 0 *)
nipkow@1301
   627
by (Asm_full_simp_tac 1);
nipkow@1301
   628
(* case Suc x *)
nipkow@4686
   629
by (Asm_full_simp_tac 1);
nipkow@1485
   630
qed_spec_mp "nth_mem";
nipkow@1301
   631
Addsimps [nth_mem];
nipkow@1301
   632
nipkow@5077
   633
(** list update **)
nipkow@5077
   634
nipkow@5077
   635
section "list update";
nipkow@5077
   636
nipkow@5077
   637
Goal "!i. length(xs[i:=x]) = length xs";
nipkow@5077
   638
by (induct_tac "xs" 1);
nipkow@5077
   639
by (Simp_tac 1);
berghofe@5183
   640
by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@5077
   641
qed_spec_mp "length_list_update";
nipkow@5077
   642
Addsimps [length_list_update];
nipkow@5077
   643
nipkow@5077
   644
nipkow@3896
   645
(** last & butlast **)
nipkow@1327
   646
nipkow@4935
   647
Goal "last(xs@[x]) = x";
wenzelm@4423
   648
by (induct_tac "xs" 1);
paulson@5316
   649
by Auto_tac;
nipkow@3896
   650
qed "last_snoc";
nipkow@3896
   651
Addsimps [last_snoc];
nipkow@3896
   652
nipkow@4935
   653
Goal "butlast(xs@[x]) = xs";
wenzelm@4423
   654
by (induct_tac "xs" 1);
paulson@5316
   655
by Auto_tac;
nipkow@3896
   656
qed "butlast_snoc";
nipkow@3896
   657
Addsimps [butlast_snoc];
nipkow@3896
   658
nipkow@4935
   659
Goal "length(butlast xs) = length xs - 1";
nipkow@4935
   660
by (res_inst_tac [("xs","xs")] rev_induct 1);
paulson@5316
   661
by Auto_tac;
nipkow@4643
   662
qed "length_butlast";
nipkow@4643
   663
Addsimps [length_butlast];
nipkow@4643
   664
paulson@5278
   665
Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
wenzelm@4423
   666
by (induct_tac "xs" 1);
paulson@5316
   667
by Auto_tac;
nipkow@3896
   668
qed_spec_mp "butlast_append";
nipkow@3896
   669
nipkow@4935
   670
Goal "x:set(butlast xs) --> x:set xs";
wenzelm@4423
   671
by (induct_tac "xs" 1);
paulson@5316
   672
by Auto_tac;
nipkow@3896
   673
qed_spec_mp "in_set_butlastD";
nipkow@3896
   674
nipkow@5043
   675
Goal "x:set(butlast xs) ==> x:set(butlast(xs@ys))";
nipkow@4686
   676
by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
wenzelm@4423
   677
by (blast_tac (claset() addDs [in_set_butlastD]) 1);
nipkow@3896
   678
qed "in_set_butlast_appendI1";
nipkow@3896
   679
nipkow@5043
   680
Goal "x:set(butlast ys) ==> x:set(butlast(xs@ys))";
nipkow@4686
   681
by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
wenzelm@4423
   682
by (Clarify_tac 1);
wenzelm@4423
   683
by (Full_simp_tac 1);
nipkow@3896
   684
qed "in_set_butlast_appendI2";
nipkow@3902
   685
nipkow@2608
   686
(** take  & drop **)
nipkow@2608
   687
section "take & drop";
nipkow@1327
   688
nipkow@4935
   689
Goal "take 0 xs = []";
nipkow@3040
   690
by (induct_tac "xs" 1);
paulson@5316
   691
by Auto_tac;
nipkow@1327
   692
qed "take_0";
nipkow@1327
   693
nipkow@4935
   694
Goal "drop 0 xs = xs";
nipkow@3040
   695
by (induct_tac "xs" 1);
paulson@5316
   696
by Auto_tac;
nipkow@2608
   697
qed "drop_0";
nipkow@2608
   698
nipkow@4935
   699
Goal "take (Suc n) (x#xs) = x # take n xs";
paulson@1552
   700
by (Simp_tac 1);
nipkow@1419
   701
qed "take_Suc_Cons";
nipkow@1327
   702
nipkow@4935
   703
Goal "drop (Suc n) (x#xs) = drop n xs";
nipkow@2608
   704
by (Simp_tac 1);
nipkow@2608
   705
qed "drop_Suc_Cons";
nipkow@2608
   706
nipkow@2608
   707
Delsimps [take_Cons,drop_Cons];
nipkow@2608
   708
Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
nipkow@2608
   709
nipkow@4935
   710
Goal "!xs. length(take n xs) = min (length xs) n";
berghofe@5183
   711
by (induct_tac "n" 1);
paulson@5316
   712
 by Auto_tac;
paulson@3457
   713
by (exhaust_tac "xs" 1);
paulson@5316
   714
 by Auto_tac;
nipkow@2608
   715
qed_spec_mp "length_take";
nipkow@2608
   716
Addsimps [length_take];
clasohm@923
   717
nipkow@4935
   718
Goal "!xs. length(drop n xs) = (length xs - n)";
berghofe@5183
   719
by (induct_tac "n" 1);
paulson@5316
   720
 by Auto_tac;
paulson@3457
   721
by (exhaust_tac "xs" 1);
paulson@5316
   722
 by Auto_tac;
nipkow@2608
   723
qed_spec_mp "length_drop";
nipkow@2608
   724
Addsimps [length_drop];
nipkow@2608
   725
nipkow@4935
   726
Goal "!xs. length xs <= n --> take n xs = xs";
berghofe@5183
   727
by (induct_tac "n" 1);
paulson@5316
   728
 by Auto_tac;
paulson@3457
   729
by (exhaust_tac "xs" 1);
paulson@5316
   730
 by Auto_tac;
nipkow@2608
   731
qed_spec_mp "take_all";
clasohm@923
   732
nipkow@4935
   733
Goal "!xs. length xs <= n --> drop n xs = []";
berghofe@5183
   734
by (induct_tac "n" 1);
paulson@5316
   735
 by Auto_tac;
paulson@3457
   736
by (exhaust_tac "xs" 1);
paulson@5316
   737
 by Auto_tac;
nipkow@2608
   738
qed_spec_mp "drop_all";
nipkow@2608
   739
paulson@5278
   740
Goal "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
berghofe@5183
   741
by (induct_tac "n" 1);
paulson@5316
   742
 by Auto_tac;
paulson@3457
   743
by (exhaust_tac "xs" 1);
paulson@5316
   744
 by Auto_tac;
nipkow@2608
   745
qed_spec_mp "take_append";
nipkow@2608
   746
Addsimps [take_append];
nipkow@2608
   747
nipkow@4935
   748
Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
berghofe@5183
   749
by (induct_tac "n" 1);
paulson@5316
   750
 by Auto_tac;
paulson@3457
   751
by (exhaust_tac "xs" 1);
paulson@5316
   752
 by Auto_tac;
nipkow@2608
   753
qed_spec_mp "drop_append";
nipkow@2608
   754
Addsimps [drop_append];
nipkow@2608
   755
nipkow@4935
   756
Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
berghofe@5183
   757
by (induct_tac "m" 1);
paulson@5316
   758
 by Auto_tac;
paulson@3457
   759
by (exhaust_tac "xs" 1);
paulson@5316
   760
 by Auto_tac;
berghofe@5183
   761
by (exhaust_tac "na" 1);
paulson@5316
   762
 by Auto_tac;
nipkow@2608
   763
qed_spec_mp "take_take";
nipkow@2608
   764
nipkow@4935
   765
Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
berghofe@5183
   766
by (induct_tac "m" 1);
paulson@5316
   767
 by Auto_tac;
paulson@3457
   768
by (exhaust_tac "xs" 1);
paulson@5316
   769
 by Auto_tac;
nipkow@2608
   770
qed_spec_mp "drop_drop";
clasohm@923
   771
nipkow@4935
   772
Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
berghofe@5183
   773
by (induct_tac "m" 1);
paulson@5316
   774
 by Auto_tac;
paulson@3457
   775
by (exhaust_tac "xs" 1);
paulson@5316
   776
 by Auto_tac;
nipkow@2608
   777
qed_spec_mp "take_drop";
nipkow@2608
   778
nipkow@4935
   779
Goal "!xs. take n (map f xs) = map f (take n xs)"; 
berghofe@5183
   780
by (induct_tac "n" 1);
paulson@5316
   781
 by Auto_tac;
paulson@3457
   782
by (exhaust_tac "xs" 1);
paulson@5316
   783
 by Auto_tac;
nipkow@2608
   784
qed_spec_mp "take_map"; 
nipkow@2608
   785
nipkow@4935
   786
Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
berghofe@5183
   787
by (induct_tac "n" 1);
paulson@5316
   788
 by Auto_tac;
paulson@3457
   789
by (exhaust_tac "xs" 1);
paulson@5316
   790
 by Auto_tac;
nipkow@2608
   791
qed_spec_mp "drop_map";
nipkow@2608
   792
nipkow@4935
   793
Goal "!n i. i < n --> (take n xs)!i = xs!i";
paulson@3457
   794
by (induct_tac "xs" 1);
paulson@5316
   795
 by Auto_tac;
paulson@3457
   796
by (exhaust_tac "n" 1);
paulson@3457
   797
 by (Blast_tac 1);
paulson@3457
   798
by (exhaust_tac "i" 1);
paulson@5316
   799
 by Auto_tac;
nipkow@2608
   800
qed_spec_mp "nth_take";
nipkow@2608
   801
Addsimps [nth_take];
clasohm@923
   802
nipkow@4935
   803
Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
berghofe@5183
   804
by (induct_tac "n" 1);
paulson@5316
   805
 by Auto_tac;
paulson@3457
   806
by (exhaust_tac "xs" 1);
paulson@5316
   807
 by Auto_tac;
nipkow@2608
   808
qed_spec_mp "nth_drop";
nipkow@2608
   809
Addsimps [nth_drop];
nipkow@2608
   810
nipkow@2608
   811
(** takeWhile & dropWhile **)
nipkow@2608
   812
nipkow@3467
   813
section "takeWhile & dropWhile";
nipkow@3467
   814
nipkow@4935
   815
Goal "takeWhile P xs @ dropWhile P xs = xs";
nipkow@3586
   816
by (induct_tac "xs" 1);
paulson@5316
   817
by Auto_tac;
nipkow@3586
   818
qed "takeWhile_dropWhile_id";
nipkow@3586
   819
Addsimps [takeWhile_dropWhile_id];
nipkow@3586
   820
nipkow@4935
   821
Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
paulson@3457
   822
by (induct_tac "xs" 1);
paulson@5316
   823
by Auto_tac;
nipkow@2608
   824
bind_thm("takeWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   825
Addsimps [takeWhile_append1];
clasohm@923
   826
nipkow@4935
   827
Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
paulson@3457
   828
by (induct_tac "xs" 1);
paulson@5316
   829
by Auto_tac;
nipkow@2608
   830
bind_thm("takeWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   831
Addsimps [takeWhile_append2];
lcp@1169
   832
nipkow@4935
   833
Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
paulson@3457
   834
by (induct_tac "xs" 1);
paulson@5316
   835
by Auto_tac;
nipkow@2608
   836
bind_thm("dropWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   837
Addsimps [dropWhile_append1];
nipkow@2608
   838
nipkow@4935
   839
Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
paulson@3457
   840
by (induct_tac "xs" 1);
paulson@5316
   841
by Auto_tac;
nipkow@2608
   842
bind_thm("dropWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   843
Addsimps [dropWhile_append2];
nipkow@2608
   844
nipkow@4935
   845
Goal "x:set(takeWhile P xs) --> x:set xs & P x";
paulson@3457
   846
by (induct_tac "xs" 1);
paulson@5316
   847
by Auto_tac;
paulson@3647
   848
qed_spec_mp"set_take_whileD";
nipkow@2608
   849
oheimb@4132
   850
qed_goal "zip_Nil_Nil"   thy "zip []     []     = []" (K [Simp_tac 1]);
oheimb@4132
   851
qed_goal "zip_Cons_Cons" thy "zip (x#xs) (y#ys) = (x,y)#zip xs ys" 
oheimb@4132
   852
						      (K [Simp_tac 1]);
nipkow@4605
   853
nipkow@5272
   854
nipkow@5272
   855
(** foldl **)
nipkow@5272
   856
section "foldl";
nipkow@5272
   857
nipkow@5272
   858
Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys";
paulson@5318
   859
by (induct_tac "xs" 1);
paulson@5316
   860
by Auto_tac;
nipkow@5272
   861
qed_spec_mp "foldl_append";
nipkow@5272
   862
Addsimps [foldl_append];
nipkow@5272
   863
nipkow@5272
   864
(* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
nipkow@5272
   865
   because it requires an additional transitivity step
nipkow@5272
   866
*)
nipkow@5272
   867
Goal "!n::nat. m <= n --> m <= foldl op+ n ns";
paulson@5318
   868
by (induct_tac "ns" 1);
paulson@5318
   869
 by (Simp_tac 1);
paulson@5318
   870
by (Asm_full_simp_tac 1);
paulson@5318
   871
by (blast_tac (claset() addIs [trans_le_add1]) 1);
nipkow@5272
   872
qed_spec_mp "start_le_sum";
nipkow@5272
   873
nipkow@5272
   874
Goal "n : set ns ==> n <= foldl op+ 0 ns";
paulson@5318
   875
by (auto_tac (claset() addIs [start_le_sum],
nipkow@5272
   876
             simpset() addsimps [in_set_conv_decomp]));
nipkow@5272
   877
qed "elem_le_sum";
nipkow@5272
   878
nipkow@5272
   879
Goal "!m. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))";
paulson@5318
   880
by (induct_tac "ns" 1);
paulson@5316
   881
by Auto_tac;
nipkow@5272
   882
qed_spec_mp "sum_eq_0_conv";
nipkow@5272
   883
AddIffs [sum_eq_0_conv];
nipkow@5272
   884
nipkow@5425
   885
(** upto **)
nipkow@5425
   886
nipkow@5427
   887
(* Does not terminate! *)
nipkow@5427
   888
Goal "[i..j(] = (if i<j then i#[Suc i..j(] else [])";
nipkow@5427
   889
by(induct_tac "j" 1);
nipkow@5427
   890
by Auto_tac;
nipkow@5427
   891
by(REPEAT(trans_tac 1));
nipkow@5427
   892
qed "upt_rec";
nipkow@5425
   893
nipkow@5427
   894
Goal "j<=i ==> [i..j(] = []";
nipkow@5427
   895
by(stac upt_rec 1);
nipkow@5427
   896
by(asm_simp_tac (simpset() addSolver cut_trans_tac) 1);
nipkow@5427
   897
qed "upt_conv_Nil";
nipkow@5427
   898
Addsimps [upt_conv_Nil];
nipkow@5427
   899
nipkow@5427
   900
Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]";
nipkow@5427
   901
by (Asm_simp_tac 1);
nipkow@5427
   902
qed "upt_Suc";
nipkow@5427
   903
nipkow@5427
   904
Goal "i<j ==> [i..j(] = i#[Suc i..j(]";
nipkow@5425
   905
br trans 1;
nipkow@5427
   906
by(stac upt_rec 1);
nipkow@5427
   907
br refl 2;
nipkow@5427
   908
by (Asm_simp_tac 1);
nipkow@5427
   909
qed "upt_conv_Cons";
nipkow@5427
   910
nipkow@5427
   911
Goal "length [i..j(] = j-i";
nipkow@5427
   912
by(induct_tac "j" 1);
nipkow@5427
   913
 by (Simp_tac 1);
nipkow@5427
   914
by(asm_simp_tac (simpset() addsimps [Suc_diff_le] addSolver cut_trans_tac) 1);
nipkow@5427
   915
qed "length_upt";
nipkow@5427
   916
Addsimps [length_upt];
nipkow@5425
   917
nipkow@5427
   918
Goal "i+k < j --> [i..j(] ! k = i+k";
nipkow@5427
   919
by(induct_tac "j" 1);
nipkow@5427
   920
 by(Simp_tac 1);
nipkow@5427
   921
by(asm_simp_tac (simpset() addsimps ([nth_append,less_diff_conv]@add_ac)
nipkow@5427
   922
                           addSolver cut_trans_tac) 1);
nipkow@5427
   923
br conjI 1;
nipkow@5427
   924
 by(Clarify_tac 1);
nipkow@5427
   925
 bd add_lessD1 1;
nipkow@5427
   926
 by(trans_tac 1);
nipkow@5427
   927
by(Clarify_tac 1);
nipkow@5427
   928
br conjI 1;
nipkow@5427
   929
 by(Clarify_tac 1);
nipkow@5427
   930
 by(subgoal_tac "n=i+k" 1);
nipkow@5427
   931
  by(Asm_full_simp_tac 1);
nipkow@5427
   932
 by(trans_tac 1);
nipkow@5427
   933
by(Clarify_tac 1);
nipkow@5427
   934
by(subgoal_tac "n=i+k" 1);
nipkow@5427
   935
 by(Asm_full_simp_tac 1);
nipkow@5427
   936
by(trans_tac 1);
nipkow@5427
   937
qed_spec_mp "nth_upt";
nipkow@5427
   938
Addsimps [nth_upt];
nipkow@5425
   939
nipkow@5272
   940
nipkow@4605
   941
(** nodups & remdups **)
nipkow@4605
   942
section "nodups & remdups";
nipkow@4605
   943
nipkow@4935
   944
Goal "set(remdups xs) = set xs";
nipkow@4605
   945
by (induct_tac "xs" 1);
nipkow@4605
   946
 by (Simp_tac 1);
nipkow@4686
   947
by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
nipkow@4605
   948
qed "set_remdups";
nipkow@4605
   949
Addsimps [set_remdups];
nipkow@4605
   950
nipkow@4935
   951
Goal "nodups(remdups xs)";
nipkow@4605
   952
by (induct_tac "xs" 1);
paulson@5316
   953
by Auto_tac;
nipkow@4605
   954
qed "nodups_remdups";
nipkow@4605
   955
nipkow@4935
   956
Goal "nodups xs --> nodups (filter P xs)";
nipkow@4605
   957
by (induct_tac "xs" 1);
paulson@5316
   958
by Auto_tac;
nipkow@4605
   959
qed_spec_mp "nodups_filter";
nipkow@4605
   960
nipkow@3589
   961
(** replicate **)
nipkow@3589
   962
section "replicate";
nipkow@3589
   963
nipkow@4935
   964
Goal "set(replicate (Suc n) x) = {x}";
wenzelm@4423
   965
by (induct_tac "n" 1);
paulson@5316
   966
by Auto_tac;
nipkow@3589
   967
val lemma = result();
nipkow@3589
   968
nipkow@5043
   969
Goal "n ~= 0 ==> set(replicate n x) = {x}";
wenzelm@4423
   970
by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
nipkow@3589
   971
qed "set_replicate";
nipkow@3589
   972
Addsimps [set_replicate];
nipkow@5162
   973
nipkow@5162
   974
nipkow@5281
   975
(*** Lexcicographic orderings on lists ***)
nipkow@5281
   976
section"Lexcicographic orderings on lists";
nipkow@5281
   977
nipkow@5281
   978
Goal "wf r ==> wf(lexn r n)";
paulson@5318
   979
by (induct_tac "n" 1);
paulson@5318
   980
by (Simp_tac 1);
paulson@5318
   981
by (Simp_tac 1);
paulson@5318
   982
by (rtac wf_subset 1);
paulson@5318
   983
by (rtac Int_lower1 2);
paulson@5318
   984
by (rtac wf_prod_fun_image 1);
paulson@5318
   985
by (rtac injI 2);
paulson@5318
   986
by (Auto_tac);
nipkow@5281
   987
qed "wf_lexn";
nipkow@5281
   988
nipkow@5281
   989
Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n";
paulson@5318
   990
by (induct_tac "n" 1);
paulson@5318
   991
by (Auto_tac);
nipkow@5281
   992
qed_spec_mp "lexn_length";
nipkow@5281
   993
nipkow@5281
   994
Goalw [lex_def] "wf r ==> wf(lex r)";
paulson@5318
   995
by (rtac wf_UN 1);
paulson@5318
   996
by (blast_tac (claset() addIs [wf_lexn]) 1);
paulson@5318
   997
by (Clarify_tac 1);
paulson@5318
   998
by (rename_tac "m n" 1);
paulson@5318
   999
by (subgoal_tac "m ~= n" 1);
paulson@5318
  1000
 by (Blast_tac 2);
paulson@5318
  1001
by (blast_tac (claset() addDs [lexn_length,not_sym]) 1);
nipkow@5281
  1002
qed "wf_lex";
nipkow@5281
  1003
AddSIs [wf_lex];
nipkow@5281
  1004
nipkow@5281
  1005
Goal
nipkow@5281
  1006
 "lexn r n = \
nipkow@5281
  1007
\ {(xs,ys). length xs = n & length ys = n & \
nipkow@5281
  1008
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5318
  1009
by (induct_tac "n" 1);
paulson@5318
  1010
 by (Simp_tac 1);
paulson@5318
  1011
 by (Blast_tac 1);
paulson@5318
  1012
by (asm_full_simp_tac (simpset() delsimps [length_Suc_conv] 
oheimb@5296
  1013
				addsimps [lex_prod_def]) 1);
paulson@5318
  1014
by (auto_tac (claset(), simpset() delsimps [length_Suc_conv]));
paulson@5318
  1015
  by (Blast_tac 1);
paulson@5318
  1016
 by (rename_tac "a xys x xs' y ys'" 1);
paulson@5318
  1017
 by (res_inst_tac [("x","a#xys")] exI 1);
paulson@5318
  1018
 by (Simp_tac 1);
paulson@5318
  1019
by (exhaust_tac "xys" 1);
paulson@5318
  1020
 by (ALLGOALS (asm_full_simp_tac (simpset() delsimps [length_Suc_conv])));
paulson@5318
  1021
by (Blast_tac 1);
nipkow@5281
  1022
qed "lexn_conv";
nipkow@5281
  1023
nipkow@5281
  1024
Goalw [lex_def]
nipkow@5281
  1025
 "lex r = \
nipkow@5281
  1026
\ {(xs,ys). length xs = length ys & \
nipkow@5281
  1027
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5318
  1028
by (force_tac (claset(), simpset() delsimps [length_Suc_conv] addsimps [lexn_conv]) 1);
nipkow@5281
  1029
qed "lex_conv";
nipkow@5281
  1030
nipkow@5281
  1031
Goalw [lexico_def] "wf r ==> wf(lexico r)";
paulson@5318
  1032
by (Blast_tac 1);
nipkow@5281
  1033
qed "wf_lexico";
nipkow@5281
  1034
AddSIs [wf_lexico];
nipkow@5281
  1035
nipkow@5281
  1036
Goalw
nipkow@5281
  1037
 [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def]
nipkow@5281
  1038
"lexico r = {(xs,ys). length xs < length ys | \
nipkow@5281
  1039
\                     length xs = length ys & (xs,ys) : lex r}";
paulson@5318
  1040
by (Simp_tac 1);
nipkow@5281
  1041
qed "lexico_conv";
nipkow@5281
  1042
nipkow@5283
  1043
Goal "([],ys) ~: lex r";
paulson@5318
  1044
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1045
qed "Nil_notin_lex";
nipkow@5283
  1046
nipkow@5283
  1047
Goal "(xs,[]) ~: lex r";
paulson@5318
  1048
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1049
qed "Nil2_notin_lex";
nipkow@5283
  1050
nipkow@5283
  1051
AddIffs [Nil_notin_lex,Nil2_notin_lex];
nipkow@5283
  1052
nipkow@5283
  1053
Goal "((x#xs,y#ys) : lex r) = \
nipkow@5283
  1054
\     ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)";
paulson@5318
  1055
by (simp_tac (simpset() addsimps [lex_conv]) 1);
paulson@5318
  1056
by (rtac iffI 1);
paulson@5318
  1057
 by (blast_tac (claset() addIs [Cons_eq_appendI]) 2);
paulson@5318
  1058
by (REPEAT(eresolve_tac [conjE, exE] 1));
paulson@5318
  1059
by (exhaust_tac "xys" 1);
paulson@5318
  1060
by (Asm_full_simp_tac 1);
paulson@5318
  1061
by (Asm_full_simp_tac 1);
paulson@5318
  1062
by (Blast_tac 1);
nipkow@5283
  1063
qed "Cons_in_lex";
nipkow@5283
  1064
AddIffs [Cons_in_lex];