src/HOL/List.ML
author paulson
Wed Apr 19 11:09:59 2000 +0200 (2000-04-19)
changeset 8741 61bc5ed22b62
parent 8442 96023903c2df
child 8935 548901d05a0e
permissions -rw-r--r--
removal of less_SucI, le_SucI from default simpset
clasohm@1465
     1
(*  Title:      HOL/List
clasohm@923
     2
    ID:         $Id$
clasohm@1465
     3
    Author:     Tobias Nipkow
clasohm@923
     4
    Copyright   1994 TU Muenchen
clasohm@923
     5
clasohm@923
     6
List lemmas
clasohm@923
     7
*)
clasohm@923
     8
nipkow@4935
     9
Goal "!x. xs ~= x#xs";
nipkow@3040
    10
by (induct_tac "xs" 1);
paulson@5316
    11
by Auto_tac;
nipkow@2608
    12
qed_spec_mp "not_Cons_self";
nipkow@3574
    13
bind_thm("not_Cons_self2",not_Cons_self RS not_sym);
nipkow@3574
    14
Addsimps [not_Cons_self,not_Cons_self2];
clasohm@923
    15
nipkow@4935
    16
Goal "(xs ~= []) = (? y ys. xs = y#ys)";
nipkow@3040
    17
by (induct_tac "xs" 1);
paulson@5316
    18
by Auto_tac;
clasohm@923
    19
qed "neq_Nil_conv";
clasohm@923
    20
nipkow@4830
    21
(* Induction over the length of a list: *)
nipkow@4935
    22
val [prem] = Goal
nipkow@4911
    23
  "(!!xs. (!ys. length ys < length xs --> P ys) ==> P xs) ==> P(xs)";
wenzelm@5132
    24
by (rtac measure_induct 1 THEN etac prem 1);
nipkow@4911
    25
qed "length_induct";
nipkow@4911
    26
clasohm@923
    27
paulson@3468
    28
(** "lists": the list-forming operator over sets **)
paulson@3342
    29
nipkow@5043
    30
Goalw lists.defs "A<=B ==> lists A <= lists B";
paulson@3342
    31
by (rtac lfp_mono 1);
paulson@3342
    32
by (REPEAT (ares_tac basic_monos 1));
paulson@3342
    33
qed "lists_mono";
paulson@3196
    34
paulson@6141
    35
val listsE = lists.mk_cases "x#l : lists A";
paulson@3468
    36
AddSEs [listsE];
paulson@3468
    37
AddSIs lists.intrs;
paulson@3468
    38
nipkow@5043
    39
Goal "l: lists A ==> l: lists B --> l: lists (A Int B)";
paulson@3468
    40
by (etac lists.induct 1);
paulson@3468
    41
by (ALLGOALS Blast_tac);
paulson@3468
    42
qed_spec_mp "lists_IntI";
paulson@3468
    43
nipkow@4935
    44
Goal "lists (A Int B) = lists A Int lists B";
wenzelm@4423
    45
by (rtac (mono_Int RS equalityI) 1);
wenzelm@4089
    46
by (simp_tac (simpset() addsimps [mono_def, lists_mono]) 1);
wenzelm@4089
    47
by (blast_tac (claset() addSIs [lists_IntI]) 1);
paulson@3468
    48
qed "lists_Int_eq";
paulson@3468
    49
Addsimps [lists_Int_eq];
paulson@3468
    50
paulson@3196
    51
nipkow@4643
    52
(**  Case analysis **)
nipkow@4643
    53
section "Case analysis";
nipkow@2608
    54
nipkow@4935
    55
val prems = Goal "[| P([]); !!x xs. P(x#xs) |] ==> P(xs)";
paulson@3457
    56
by (induct_tac "xs" 1);
paulson@3457
    57
by (REPEAT(resolve_tac prems 1));
nipkow@2608
    58
qed "list_cases";
nipkow@2608
    59
nipkow@4935
    60
Goal "(xs=[] --> P([])) & (!y ys. xs=y#ys --> P(y#ys)) --> P(xs)";
nipkow@3040
    61
by (induct_tac "xs" 1);
paulson@2891
    62
by (Blast_tac 1);
paulson@2891
    63
by (Blast_tac 1);
nipkow@2608
    64
bind_thm("list_eq_cases",
nipkow@2608
    65
  impI RSN (2,allI RSN (2,allI RSN (2,impI RS (conjI RS (result() RS mp))))));
nipkow@2608
    66
nipkow@3860
    67
(** length **)
nipkow@3860
    68
(* needs to come before "@" because of thm append_eq_append_conv *)
nipkow@3860
    69
nipkow@3860
    70
section "length";
nipkow@3860
    71
nipkow@4935
    72
Goal "length(xs@ys) = length(xs)+length(ys)";
nipkow@3860
    73
by (induct_tac "xs" 1);
paulson@5316
    74
by Auto_tac;
nipkow@3860
    75
qed"length_append";
nipkow@3860
    76
Addsimps [length_append];
nipkow@3860
    77
nipkow@5129
    78
Goal "length (map f xs) = length xs";
nipkow@5129
    79
by (induct_tac "xs" 1);
paulson@5316
    80
by Auto_tac;
nipkow@3860
    81
qed "length_map";
nipkow@3860
    82
Addsimps [length_map];
nipkow@3860
    83
nipkow@4935
    84
Goal "length(rev xs) = length(xs)";
nipkow@3860
    85
by (induct_tac "xs" 1);
paulson@5316
    86
by Auto_tac;
nipkow@3860
    87
qed "length_rev";
nipkow@3860
    88
Addsimps [length_rev];
nipkow@3860
    89
nipkow@7028
    90
Goal "length(tl xs) = (length xs) - 1";
wenzelm@8442
    91
by (case_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@8442
   162
 by (case_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@8442
   166
by (case_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
wenzelm@7224
   256
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
wenzelm@7224
   257
      (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
nipkow@5427
   258
  | last (Const("List.op @",_) $ _ $ ys) = last ys
nipkow@5427
   259
  | last t = t;
nipkow@5427
   260
wenzelm@7224
   261
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
nipkow@5427
   262
  | list1 _ = false;
nipkow@5427
   263
wenzelm@7224
   264
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
wenzelm@7224
   265
      (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
nipkow@5427
   266
  | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
wenzelm@7224
   267
  | butlast xs = Const("List.list.Nil",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@8442
   343
by (case_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@8442
   349
by (case_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@8009
   354
Goal "(map f xs = y#ys) = (? x xs'. xs = x#xs' & f x = y & map f xs' = ys)";
wenzelm@8442
   355
by (case_tac "xs" 1);
nipkow@8009
   356
by (ALLGOALS Asm_simp_tac);
nipkow@8009
   357
qed "map_eq_Cons";
nipkow@8009
   358
nipkow@8009
   359
Goal "!xs. map f xs = map f ys --> (!x y. f x = f y --> x=y) --> xs=ys";
nipkow@8009
   360
by (induct_tac "ys" 1);
nipkow@8009
   361
 by (Asm_simp_tac 1);
nipkow@8009
   362
by (fast_tac (claset() addss (simpset() addsimps [map_eq_Cons])) 1);
nipkow@8009
   363
qed_spec_mp "map_injective";
nipkow@8009
   364
nipkow@8009
   365
Goal "inj f ==> inj (map f)";
paulson@8064
   366
by (blast_tac (claset() addDs [map_injective,injD] addIs [injI]) 1);
nipkow@8009
   367
qed "inj_mapI";
nipkow@8009
   368
nipkow@8009
   369
Goalw [inj_on_def] "inj (map f) ==> inj f";
paulson@8064
   370
by (Clarify_tac 1);
paulson@8064
   371
by (eres_inst_tac [("x","[x]")] ballE 1);
paulson@8064
   372
 by (eres_inst_tac [("x","[y]")] ballE 1);
paulson@8064
   373
  by (Asm_full_simp_tac 1);
paulson@8064
   374
 by (Blast_tac 1);
paulson@8064
   375
by (Blast_tac 1);
nipkow@8009
   376
qed "inj_mapD";
nipkow@8009
   377
nipkow@8009
   378
Goal "inj (map f) = inj f";
paulson@8064
   379
by (blast_tac (claset() addDs [inj_mapD] addIs [inj_mapI]) 1);
nipkow@8009
   380
qed "inj_map";
nipkow@3860
   381
lcp@1169
   382
(** rev **)
lcp@1169
   383
nipkow@3467
   384
section "rev";
nipkow@3467
   385
nipkow@4935
   386
Goal "rev(xs@ys) = rev(ys) @ rev(xs)";
nipkow@3040
   387
by (induct_tac "xs" 1);
paulson@5316
   388
by Auto_tac;
lcp@1169
   389
qed "rev_append";
nipkow@2512
   390
Addsimps[rev_append];
lcp@1169
   391
nipkow@4935
   392
Goal "rev(rev l) = l";
nipkow@3040
   393
by (induct_tac "l" 1);
paulson@5316
   394
by Auto_tac;
lcp@1169
   395
qed "rev_rev_ident";
nipkow@2512
   396
Addsimps[rev_rev_ident];
lcp@1169
   397
nipkow@4935
   398
Goal "(rev xs = []) = (xs = [])";
wenzelm@4423
   399
by (induct_tac "xs" 1);
paulson@5316
   400
by Auto_tac;
nipkow@3860
   401
qed "rev_is_Nil_conv";
nipkow@3860
   402
AddIffs [rev_is_Nil_conv];
nipkow@3860
   403
nipkow@4935
   404
Goal "([] = rev xs) = (xs = [])";
wenzelm@4423
   405
by (induct_tac "xs" 1);
paulson@5316
   406
by Auto_tac;
nipkow@3860
   407
qed "Nil_is_rev_conv";
nipkow@3860
   408
AddIffs [Nil_is_rev_conv];
nipkow@3860
   409
nipkow@6820
   410
Goal "!ys. (rev xs = rev ys) = (xs = ys)";
paulson@6831
   411
by (induct_tac "xs" 1);
nipkow@6820
   412
 by (Force_tac 1);
paulson@6831
   413
by (rtac allI 1);
wenzelm@8442
   414
by (case_tac "ys" 1);
nipkow@6820
   415
 by (Asm_simp_tac 1);
nipkow@6820
   416
by (Force_tac 1);
nipkow@6820
   417
qed_spec_mp "rev_is_rev_conv";
nipkow@6820
   418
AddIffs [rev_is_rev_conv];
nipkow@6820
   419
nipkow@4935
   420
val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
wenzelm@5132
   421
by (stac (rev_rev_ident RS sym) 1);
paulson@6162
   422
by (res_inst_tac [("list", "rev xs")] list.induct 1);
wenzelm@5132
   423
by (ALLGOALS Simp_tac);
wenzelm@5132
   424
by (resolve_tac prems 1);
wenzelm@5132
   425
by (eresolve_tac prems 1);
nipkow@4935
   426
qed "rev_induct";
nipkow@4935
   427
nipkow@5272
   428
fun rev_induct_tac xs = res_inst_tac [("xs",xs)] rev_induct;
nipkow@5272
   429
nipkow@4935
   430
Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
wenzelm@5132
   431
by (res_inst_tac [("xs","xs")] rev_induct 1);
paulson@5316
   432
by Auto_tac;
nipkow@4935
   433
bind_thm ("rev_exhaust",
nipkow@4935
   434
  impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp)))));
nipkow@4935
   435
nipkow@2608
   436
nipkow@3465
   437
(** set **)
paulson@1812
   438
nipkow@3467
   439
section "set";
nipkow@3467
   440
paulson@7032
   441
Goal "finite (set xs)";
paulson@7032
   442
by (induct_tac "xs" 1);
paulson@7032
   443
by Auto_tac;
paulson@7032
   444
qed "finite_set";
paulson@7032
   445
AddIffs [finite_set];
oheimb@5296
   446
nipkow@4935
   447
Goal "set (xs@ys) = (set xs Un set ys)";
nipkow@3040
   448
by (induct_tac "xs" 1);
paulson@5316
   449
by Auto_tac;
paulson@3647
   450
qed "set_append";
paulson@3647
   451
Addsimps[set_append];
paulson@1812
   452
nipkow@4935
   453
Goal "set l <= set (x#l)";
paulson@5316
   454
by Auto_tac;
paulson@3647
   455
qed "set_subset_Cons";
paulson@1936
   456
nipkow@4935
   457
Goal "(set xs = {}) = (xs = [])";
paulson@3457
   458
by (induct_tac "xs" 1);
paulson@5316
   459
by Auto_tac;
paulson@3647
   460
qed "set_empty";
paulson@3647
   461
Addsimps [set_empty];
nipkow@2608
   462
nipkow@4935
   463
Goal "set(rev xs) = set(xs)";
paulson@3457
   464
by (induct_tac "xs" 1);
paulson@5316
   465
by Auto_tac;
paulson@3647
   466
qed "set_rev";
paulson@3647
   467
Addsimps [set_rev];
nipkow@2608
   468
nipkow@4935
   469
Goal "set(map f xs) = f``(set xs)";
paulson@3457
   470
by (induct_tac "xs" 1);
paulson@5316
   471
by Auto_tac;
paulson@3647
   472
qed "set_map";
paulson@3647
   473
Addsimps [set_map];
nipkow@2608
   474
nipkow@6433
   475
Goal "set(filter P xs) = {x. x : set xs & P x}";
paulson@6813
   476
by (induct_tac "xs" 1);
paulson@6813
   477
by Auto_tac;
nipkow@6433
   478
qed "set_filter";
nipkow@6433
   479
Addsimps [set_filter];
nipkow@8009
   480
nipkow@6433
   481
Goal "set[i..j(] = {k. i <= k & k < j}";
paulson@6813
   482
by (induct_tac "j" 1);
paulson@6813
   483
by Auto_tac;
paulson@6813
   484
by (arith_tac 1);
nipkow@6433
   485
qed "set_upt";
nipkow@6433
   486
Addsimps [set_upt];
nipkow@6433
   487
nipkow@5272
   488
Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)";
paulson@5318
   489
by (induct_tac "xs" 1);
paulson@5318
   490
 by (Simp_tac 1);
paulson@5318
   491
by (Asm_simp_tac 1);
paulson@5318
   492
by (rtac iffI 1);
paulson@5318
   493
by (blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1);
paulson@5318
   494
by (REPEAT(etac exE 1));
wenzelm@8442
   495
by (case_tac "ys" 1);
paulson@5316
   496
by Auto_tac;
nipkow@5272
   497
qed "in_set_conv_decomp";
nipkow@5272
   498
nipkow@8009
   499
nipkow@5272
   500
(* eliminate `lists' in favour of `set' *)
nipkow@5272
   501
nipkow@5272
   502
Goal "(xs : lists A) = (!x : set xs. x : A)";
paulson@5318
   503
by (induct_tac "xs" 1);
paulson@5316
   504
by Auto_tac;
nipkow@5272
   505
qed "in_lists_conv_set";
nipkow@5272
   506
nipkow@5272
   507
bind_thm("in_listsD",in_lists_conv_set RS iffD1);
nipkow@5272
   508
AddSDs [in_listsD];
nipkow@5272
   509
bind_thm("in_listsI",in_lists_conv_set RS iffD2);
nipkow@5272
   510
AddSIs [in_listsI];
paulson@1812
   511
oheimb@5518
   512
(** mem **)
oheimb@5518
   513
 
oheimb@5518
   514
section "mem";
oheimb@5518
   515
oheimb@5518
   516
Goal "(x mem xs) = (x: set xs)";
oheimb@5518
   517
by (induct_tac "xs" 1);
oheimb@5518
   518
by Auto_tac;
oheimb@5518
   519
qed "set_mem_eq";
oheimb@5518
   520
oheimb@5518
   521
clasohm@923
   522
(** list_all **)
clasohm@923
   523
nipkow@3467
   524
section "list_all";
nipkow@3467
   525
oheimb@5518
   526
Goal "list_all P xs = (!x:set xs. P x)";
oheimb@5518
   527
by (induct_tac "xs" 1);
oheimb@5518
   528
by Auto_tac;
oheimb@5518
   529
qed "list_all_conv";
oheimb@5518
   530
oheimb@5443
   531
Goal "list_all P (xs@ys) = (list_all P xs & list_all P ys)";
nipkow@3040
   532
by (induct_tac "xs" 1);
paulson@5316
   533
by Auto_tac;
nipkow@2512
   534
qed "list_all_append";
nipkow@2512
   535
Addsimps [list_all_append];
clasohm@923
   536
clasohm@923
   537
nipkow@2608
   538
(** filter **)
clasohm@923
   539
nipkow@3467
   540
section "filter";
nipkow@3467
   541
nipkow@4935
   542
Goal "filter P (xs@ys) = filter P xs @ filter P ys";
paulson@3457
   543
by (induct_tac "xs" 1);
paulson@5316
   544
by Auto_tac;
nipkow@2608
   545
qed "filter_append";
nipkow@2608
   546
Addsimps [filter_append];
nipkow@2608
   547
nipkow@4935
   548
Goal "filter (%x. True) xs = xs";
nipkow@4605
   549
by (induct_tac "xs" 1);
paulson@5316
   550
by Auto_tac;
nipkow@4605
   551
qed "filter_True";
nipkow@4605
   552
Addsimps [filter_True];
nipkow@4605
   553
nipkow@4935
   554
Goal "filter (%x. False) xs = []";
nipkow@4605
   555
by (induct_tac "xs" 1);
paulson@5316
   556
by Auto_tac;
nipkow@4605
   557
qed "filter_False";
nipkow@4605
   558
Addsimps [filter_False];
nipkow@4605
   559
nipkow@4935
   560
Goal "length (filter P xs) <= length xs";
paulson@3457
   561
by (induct_tac "xs" 1);
paulson@5316
   562
by Auto_tac;
paulson@8741
   563
by (asm_simp_tac (simpset() addsimps [le_SucI]) 1);
nipkow@4605
   564
qed "length_filter";
oheimb@5443
   565
Addsimps[length_filter];
nipkow@2608
   566
oheimb@5443
   567
Goal "set (filter P xs) <= set xs";
oheimb@5443
   568
by Auto_tac;
oheimb@5443
   569
qed "filter_is_subset";
oheimb@5443
   570
Addsimps [filter_is_subset];
oheimb@5443
   571
nipkow@2608
   572
nipkow@3467
   573
section "concat";
nipkow@3467
   574
nipkow@4935
   575
Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
nipkow@3040
   576
by (induct_tac "xs" 1);
paulson@5316
   577
by Auto_tac;
nipkow@2608
   578
qed"concat_append";
nipkow@2608
   579
Addsimps [concat_append];
nipkow@2512
   580
nipkow@4935
   581
Goal "(concat xss = []) = (!xs:set xss. xs=[])";
wenzelm@4423
   582
by (induct_tac "xss" 1);
paulson@5316
   583
by Auto_tac;
nipkow@3896
   584
qed "concat_eq_Nil_conv";
nipkow@3896
   585
AddIffs [concat_eq_Nil_conv];
nipkow@3896
   586
nipkow@4935
   587
Goal "([] = concat xss) = (!xs:set xss. xs=[])";
wenzelm@4423
   588
by (induct_tac "xss" 1);
paulson@5316
   589
by Auto_tac;
nipkow@3896
   590
qed "Nil_eq_concat_conv";
nipkow@3896
   591
AddIffs [Nil_eq_concat_conv];
nipkow@3896
   592
nipkow@4935
   593
Goal  "set(concat xs) = Union(set `` set xs)";
nipkow@3467
   594
by (induct_tac "xs" 1);
paulson@5316
   595
by Auto_tac;
paulson@3647
   596
qed"set_concat";
paulson@3647
   597
Addsimps [set_concat];
nipkow@3467
   598
nipkow@4935
   599
Goal "map f (concat xs) = concat (map (map f) xs)"; 
nipkow@3467
   600
by (induct_tac "xs" 1);
paulson@5316
   601
by Auto_tac;
nipkow@3467
   602
qed "map_concat";
nipkow@3467
   603
nipkow@4935
   604
Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
nipkow@3467
   605
by (induct_tac "xs" 1);
paulson@5316
   606
by Auto_tac;
nipkow@3467
   607
qed"filter_concat"; 
nipkow@3467
   608
nipkow@4935
   609
Goal "rev(concat xs) = concat (map rev (rev xs))";
nipkow@3467
   610
by (induct_tac "xs" 1);
paulson@5316
   611
by Auto_tac;
nipkow@2608
   612
qed "rev_concat";
clasohm@923
   613
clasohm@923
   614
(** nth **)
clasohm@923
   615
nipkow@3467
   616
section "nth";
nipkow@3467
   617
pusch@6408
   618
Goal "(x#xs)!0 = x";
pusch@6408
   619
by Auto_tac;
pusch@6408
   620
qed "nth_Cons_0";
pusch@6408
   621
Addsimps [nth_Cons_0];
nipkow@5644
   622
pusch@6408
   623
Goal "(x#xs)!(Suc n) = xs!n";
pusch@6408
   624
by Auto_tac;
pusch@6408
   625
qed "nth_Cons_Suc";
pusch@6408
   626
Addsimps [nth_Cons_Suc];
pusch@6408
   627
pusch@6408
   628
Delsimps (thms "nth.simps");
pusch@6408
   629
pusch@6408
   630
Goal "!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
pusch@6408
   631
by (induct_tac "xs" 1);
paulson@3457
   632
 by (Asm_simp_tac 1);
paulson@3457
   633
 by (rtac allI 1);
wenzelm@8442
   634
 by (case_tac "n" 1);
paulson@5316
   635
  by Auto_tac;
nipkow@2608
   636
qed_spec_mp "nth_append";
nipkow@2608
   637
nipkow@4935
   638
Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
nipkow@3040
   639
by (induct_tac "xs" 1);
nipkow@8118
   640
 by (Asm_full_simp_tac 1);
nipkow@1301
   641
by (rtac allI 1);
berghofe@5183
   642
by (induct_tac "n" 1);
paulson@5316
   643
by Auto_tac;
nipkow@1485
   644
qed_spec_mp "nth_map";
nipkow@1301
   645
Addsimps [nth_map];
nipkow@1301
   646
nipkow@8118
   647
Goal "set xs = {xs!i |i. i < length xs}";
nipkow@3040
   648
by (induct_tac "xs" 1);
nipkow@8118
   649
 by (Simp_tac 1);
paulson@8254
   650
by (Asm_simp_tac 1);
paulson@8254
   651
by Safe_tac;
paulson@8254
   652
  by (res_inst_tac [("x","0")] exI 1);
nipkow@8118
   653
  by (Simp_tac 1);
paulson@8254
   654
 by (res_inst_tac [("x","Suc i")] exI 1);
paulson@8254
   655
 by (Asm_simp_tac 1);
wenzelm@8442
   656
by (case_tac "i" 1);
paulson@8254
   657
 by (Asm_full_simp_tac 1);
paulson@8254
   658
by (rename_tac "j" 1);
paulson@8254
   659
 by (res_inst_tac [("x","j")] exI 1);
paulson@8254
   660
by (Asm_simp_tac 1);
nipkow@8118
   661
qed "set_conv_nth";
nipkow@8118
   662
nipkow@8118
   663
Goal "n < length xs ==> Ball (set xs) P --> P(xs!n)";
nipkow@8118
   664
by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
paulson@8254
   665
by (Blast_tac 1);
oheimb@5518
   666
qed_spec_mp "list_ball_nth";
nipkow@1301
   667
nipkow@8118
   668
Goal "n < length xs ==> xs!n : set xs";
nipkow@8118
   669
by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
paulson@8254
   670
by (Blast_tac 1);
nipkow@1485
   671
qed_spec_mp "nth_mem";
nipkow@1301
   672
Addsimps [nth_mem];
nipkow@1301
   673
nipkow@8009
   674
Goal "(!i. i < length xs --> P(xs!i)) --> (!x : set xs. P x)";
nipkow@8118
   675
by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
paulson@8254
   676
by (Blast_tac 1);
nipkow@8009
   677
qed_spec_mp "all_nth_imp_all_set";
nipkow@8009
   678
nipkow@8009
   679
Goal "(!x : set xs. P x) = (!i. i<length xs --> P (xs ! i))";
nipkow@8118
   680
by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
paulson@8254
   681
by (Blast_tac 1);
nipkow@8009
   682
qed_spec_mp "all_set_conv_all_nth";
nipkow@8009
   683
nipkow@8009
   684
nipkow@5077
   685
(** list update **)
nipkow@5077
   686
nipkow@5077
   687
section "list update";
nipkow@5077
   688
nipkow@5077
   689
Goal "!i. length(xs[i:=x]) = length xs";
nipkow@5077
   690
by (induct_tac "xs" 1);
nipkow@5077
   691
by (Simp_tac 1);
berghofe@5183
   692
by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@5077
   693
qed_spec_mp "length_list_update";
nipkow@5077
   694
Addsimps [length_list_update];
nipkow@5077
   695
nipkow@5644
   696
Goal "!i j. i < length xs  --> (xs[i:=x])!j = (if i=j then x else xs!j)";
paulson@6162
   697
by (induct_tac "xs" 1);
paulson@6162
   698
 by (Simp_tac 1);
paulson@6162
   699
by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
nipkow@5644
   700
qed_spec_mp "nth_list_update";
nipkow@5644
   701
nipkow@8144
   702
Goal "i < length xs  ==> (xs[i:=x])!i = x";
nipkow@8144
   703
by (asm_simp_tac (simpset() addsimps [nth_list_update]) 1);
nipkow@8144
   704
qed "nth_list_update_eq";
nipkow@8144
   705
Addsimps [nth_list_update_eq];
nipkow@8144
   706
nipkow@8144
   707
Goal "!i j. i ~= j --> xs[i:=x]!j = xs!j";
nipkow@8144
   708
by (induct_tac "xs" 1);
nipkow@8144
   709
 by (Simp_tac 1);
nipkow@8144
   710
by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
nipkow@8144
   711
qed_spec_mp "nth_list_update_neq";
nipkow@8144
   712
Addsimps [nth_list_update_neq];
nipkow@8144
   713
nipkow@6433
   714
Goal "!i. i < size xs --> xs[i:=x, i:=y] = xs[i:=y]";
paulson@6813
   715
by (induct_tac "xs" 1);
paulson@6813
   716
 by (Simp_tac 1);
paulson@6813
   717
by (asm_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@6433
   718
qed_spec_mp "list_update_overwrite";
nipkow@6433
   719
Addsimps [list_update_overwrite];
nipkow@6433
   720
nipkow@6433
   721
Goal "!i < length xs. (xs[i := x] = xs) = (xs!i = x)";
paulson@6813
   722
by (induct_tac "xs" 1);
paulson@6813
   723
 by (Simp_tac 1);
paulson@6813
   724
by (simp_tac (simpset() addsplits [nat.split]) 1);
paulson@6813
   725
by (Blast_tac 1);
nipkow@6433
   726
qed_spec_mp "list_update_same_conv";
nipkow@6433
   727
nipkow@8009
   728
Goal "!i xy xs. length xs = length ys --> \
nipkow@8009
   729
\     (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])";
nipkow@8009
   730
by (induct_tac "ys" 1);
nipkow@8009
   731
 by Auto_tac;
wenzelm@8442
   732
by (case_tac "xs" 1);
nipkow@8009
   733
 by (auto_tac (claset(), simpset() addsplits [nat.split]));
nipkow@8009
   734
qed_spec_mp "update_zip";
nipkow@8009
   735
nipkow@8009
   736
Goal "!i. set(xs[i:=x]) <= insert x (set xs)";
nipkow@8009
   737
by (induct_tac "xs" 1);
nipkow@8009
   738
 by (asm_full_simp_tac (simpset() addsimps []) 1);
nipkow@8009
   739
by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@8009
   740
by (Fast_tac  1);
nipkow@8287
   741
qed_spec_mp "set_update_subset_insert";
nipkow@8009
   742
nipkow@8287
   743
Goal "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A";
nipkow@8287
   744
by(fast_tac (claset() addSDs [set_update_subset_insert RS subsetD]) 1);
nipkow@8287
   745
qed "set_update_subsetI";
nipkow@5077
   746
nipkow@3896
   747
(** last & butlast **)
nipkow@1327
   748
nipkow@5644
   749
section "last / butlast";
nipkow@5644
   750
nipkow@4935
   751
Goal "last(xs@[x]) = x";
wenzelm@4423
   752
by (induct_tac "xs" 1);
paulson@5316
   753
by Auto_tac;
nipkow@3896
   754
qed "last_snoc";
nipkow@3896
   755
Addsimps [last_snoc];
nipkow@3896
   756
nipkow@4935
   757
Goal "butlast(xs@[x]) = xs";
wenzelm@4423
   758
by (induct_tac "xs" 1);
paulson@5316
   759
by Auto_tac;
nipkow@3896
   760
qed "butlast_snoc";
nipkow@3896
   761
Addsimps [butlast_snoc];
nipkow@3896
   762
nipkow@4935
   763
Goal "length(butlast xs) = length xs - 1";
nipkow@4935
   764
by (res_inst_tac [("xs","xs")] rev_induct 1);
paulson@5316
   765
by Auto_tac;
nipkow@4643
   766
qed "length_butlast";
nipkow@4643
   767
Addsimps [length_butlast];
nipkow@4643
   768
paulson@5278
   769
Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
wenzelm@4423
   770
by (induct_tac "xs" 1);
paulson@5316
   771
by Auto_tac;
nipkow@3896
   772
qed_spec_mp "butlast_append";
nipkow@3896
   773
nipkow@8118
   774
Goal "xs ~= [] --> butlast xs @ [last xs] = xs";
paulson@8254
   775
by (induct_tac "xs" 1);
paulson@8254
   776
by (ALLGOALS Asm_simp_tac);
nipkow@8118
   777
qed_spec_mp "append_butlast_last_id";
nipkow@8118
   778
Addsimps [append_butlast_last_id];
nipkow@8118
   779
nipkow@4935
   780
Goal "x:set(butlast xs) --> x:set xs";
wenzelm@4423
   781
by (induct_tac "xs" 1);
paulson@5316
   782
by Auto_tac;
nipkow@3896
   783
qed_spec_mp "in_set_butlastD";
nipkow@3896
   784
paulson@5448
   785
Goal "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))";
paulson@5448
   786
by (auto_tac (claset() addDs [in_set_butlastD],
paulson@5448
   787
	      simpset() addsimps [butlast_append]));
paulson@5448
   788
qed "in_set_butlast_appendI";
nipkow@3902
   789
nipkow@2608
   790
(** take  & drop **)
nipkow@2608
   791
section "take & drop";
nipkow@1327
   792
nipkow@4935
   793
Goal "take 0 xs = []";
nipkow@3040
   794
by (induct_tac "xs" 1);
paulson@5316
   795
by Auto_tac;
nipkow@1327
   796
qed "take_0";
nipkow@1327
   797
nipkow@4935
   798
Goal "drop 0 xs = xs";
nipkow@3040
   799
by (induct_tac "xs" 1);
paulson@5316
   800
by Auto_tac;
nipkow@2608
   801
qed "drop_0";
nipkow@2608
   802
nipkow@4935
   803
Goal "take (Suc n) (x#xs) = x # take n xs";
paulson@1552
   804
by (Simp_tac 1);
nipkow@1419
   805
qed "take_Suc_Cons";
nipkow@1327
   806
nipkow@4935
   807
Goal "drop (Suc n) (x#xs) = drop n xs";
nipkow@2608
   808
by (Simp_tac 1);
nipkow@2608
   809
qed "drop_Suc_Cons";
nipkow@2608
   810
nipkow@2608
   811
Delsimps [take_Cons,drop_Cons];
nipkow@2608
   812
Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
nipkow@2608
   813
nipkow@4935
   814
Goal "!xs. length(take n xs) = min (length xs) n";
berghofe@5183
   815
by (induct_tac "n" 1);
paulson@5316
   816
 by Auto_tac;
wenzelm@8442
   817
by (case_tac "xs" 1);
paulson@5316
   818
 by Auto_tac;
nipkow@2608
   819
qed_spec_mp "length_take";
nipkow@2608
   820
Addsimps [length_take];
clasohm@923
   821
nipkow@4935
   822
Goal "!xs. length(drop n xs) = (length xs - n)";
berghofe@5183
   823
by (induct_tac "n" 1);
paulson@5316
   824
 by Auto_tac;
wenzelm@8442
   825
by (case_tac "xs" 1);
paulson@5316
   826
 by Auto_tac;
nipkow@2608
   827
qed_spec_mp "length_drop";
nipkow@2608
   828
Addsimps [length_drop];
nipkow@2608
   829
nipkow@4935
   830
Goal "!xs. length xs <= n --> take n xs = xs";
berghofe@5183
   831
by (induct_tac "n" 1);
paulson@5316
   832
 by Auto_tac;
wenzelm@8442
   833
by (case_tac "xs" 1);
paulson@5316
   834
 by Auto_tac;
nipkow@2608
   835
qed_spec_mp "take_all";
nipkow@7246
   836
Addsimps [take_all];
clasohm@923
   837
nipkow@4935
   838
Goal "!xs. length xs <= n --> drop n xs = []";
berghofe@5183
   839
by (induct_tac "n" 1);
paulson@5316
   840
 by Auto_tac;
wenzelm@8442
   841
by (case_tac "xs" 1);
paulson@5316
   842
 by Auto_tac;
nipkow@2608
   843
qed_spec_mp "drop_all";
nipkow@7246
   844
Addsimps [drop_all];
nipkow@2608
   845
paulson@5278
   846
Goal "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
berghofe@5183
   847
by (induct_tac "n" 1);
paulson@5316
   848
 by Auto_tac;
wenzelm@8442
   849
by (case_tac "xs" 1);
paulson@5316
   850
 by Auto_tac;
nipkow@2608
   851
qed_spec_mp "take_append";
nipkow@2608
   852
Addsimps [take_append];
nipkow@2608
   853
nipkow@4935
   854
Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
berghofe@5183
   855
by (induct_tac "n" 1);
paulson@5316
   856
 by Auto_tac;
wenzelm@8442
   857
by (case_tac "xs" 1);
paulson@5316
   858
 by Auto_tac;
nipkow@2608
   859
qed_spec_mp "drop_append";
nipkow@2608
   860
Addsimps [drop_append];
nipkow@2608
   861
nipkow@4935
   862
Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
berghofe@5183
   863
by (induct_tac "m" 1);
paulson@5316
   864
 by Auto_tac;
wenzelm@8442
   865
by (case_tac "xs" 1);
paulson@5316
   866
 by Auto_tac;
wenzelm@8442
   867
by (case_tac "na" 1);
paulson@5316
   868
 by Auto_tac;
nipkow@2608
   869
qed_spec_mp "take_take";
nipkow@7570
   870
Addsimps [take_take];
nipkow@2608
   871
nipkow@4935
   872
Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
berghofe@5183
   873
by (induct_tac "m" 1);
paulson@5316
   874
 by Auto_tac;
wenzelm@8442
   875
by (case_tac "xs" 1);
paulson@5316
   876
 by Auto_tac;
nipkow@2608
   877
qed_spec_mp "drop_drop";
nipkow@7570
   878
Addsimps [drop_drop];
clasohm@923
   879
nipkow@4935
   880
Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
berghofe@5183
   881
by (induct_tac "m" 1);
paulson@5316
   882
 by Auto_tac;
wenzelm@8442
   883
by (case_tac "xs" 1);
paulson@5316
   884
 by Auto_tac;
nipkow@2608
   885
qed_spec_mp "take_drop";
nipkow@2608
   886
paulson@6813
   887
Goal "!xs. take n xs @ drop n xs = xs";
paulson@6813
   888
by (induct_tac "n" 1);
paulson@6813
   889
 by Auto_tac;
wenzelm@8442
   890
by (case_tac "xs" 1);
paulson@6813
   891
 by Auto_tac;
paulson@6813
   892
qed_spec_mp "append_take_drop_id";
nipkow@8118
   893
Addsimps [append_take_drop_id];
paulson@6813
   894
nipkow@4935
   895
Goal "!xs. take n (map f xs) = map f (take n xs)"; 
berghofe@5183
   896
by (induct_tac "n" 1);
paulson@5316
   897
 by Auto_tac;
wenzelm@8442
   898
by (case_tac "xs" 1);
paulson@5316
   899
 by Auto_tac;
nipkow@2608
   900
qed_spec_mp "take_map"; 
nipkow@2608
   901
nipkow@4935
   902
Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
berghofe@5183
   903
by (induct_tac "n" 1);
paulson@5316
   904
 by Auto_tac;
wenzelm@8442
   905
by (case_tac "xs" 1);
paulson@5316
   906
 by Auto_tac;
nipkow@2608
   907
qed_spec_mp "drop_map";
nipkow@2608
   908
nipkow@4935
   909
Goal "!n i. i < n --> (take n xs)!i = xs!i";
paulson@3457
   910
by (induct_tac "xs" 1);
paulson@5316
   911
 by Auto_tac;
wenzelm@8442
   912
by (case_tac "n" 1);
paulson@3457
   913
 by (Blast_tac 1);
wenzelm@8442
   914
by (case_tac "i" 1);
paulson@5316
   915
 by Auto_tac;
nipkow@2608
   916
qed_spec_mp "nth_take";
nipkow@2608
   917
Addsimps [nth_take];
clasohm@923
   918
nipkow@4935
   919
Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
berghofe@5183
   920
by (induct_tac "n" 1);
paulson@5316
   921
 by Auto_tac;
wenzelm@8442
   922
by (case_tac "xs" 1);
paulson@5316
   923
 by Auto_tac;
nipkow@2608
   924
qed_spec_mp "nth_drop";
nipkow@2608
   925
Addsimps [nth_drop];
nipkow@2608
   926
nipkow@8118
   927
nipkow@8118
   928
Goal
nipkow@8118
   929
 "!zs. (xs@ys = zs) = (xs = take (length xs) zs & ys = drop (length xs) zs)";
paulson@8254
   930
by (induct_tac "xs" 1);
paulson@8254
   931
 by (Simp_tac 1);
paulson@8254
   932
by (Asm_full_simp_tac 1);
paulson@8254
   933
by (Clarify_tac 1);
wenzelm@8442
   934
by (case_tac "zs" 1);
paulson@8254
   935
by (Auto_tac);
nipkow@8118
   936
qed_spec_mp "append_eq_conv_conj";
nipkow@8118
   937
nipkow@2608
   938
(** takeWhile & dropWhile **)
nipkow@2608
   939
nipkow@3467
   940
section "takeWhile & dropWhile";
nipkow@3467
   941
nipkow@4935
   942
Goal "takeWhile P xs @ dropWhile P xs = xs";
nipkow@3586
   943
by (induct_tac "xs" 1);
paulson@5316
   944
by Auto_tac;
nipkow@3586
   945
qed "takeWhile_dropWhile_id";
nipkow@3586
   946
Addsimps [takeWhile_dropWhile_id];
nipkow@3586
   947
nipkow@4935
   948
Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
paulson@3457
   949
by (induct_tac "xs" 1);
paulson@5316
   950
by Auto_tac;
nipkow@2608
   951
bind_thm("takeWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   952
Addsimps [takeWhile_append1];
clasohm@923
   953
nipkow@4935
   954
Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
paulson@3457
   955
by (induct_tac "xs" 1);
paulson@5316
   956
by Auto_tac;
nipkow@2608
   957
bind_thm("takeWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   958
Addsimps [takeWhile_append2];
lcp@1169
   959
nipkow@4935
   960
Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
paulson@3457
   961
by (induct_tac "xs" 1);
paulson@5316
   962
by Auto_tac;
nipkow@2608
   963
bind_thm("dropWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   964
Addsimps [dropWhile_append1];
nipkow@2608
   965
nipkow@4935
   966
Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
paulson@3457
   967
by (induct_tac "xs" 1);
paulson@5316
   968
by Auto_tac;
nipkow@2608
   969
bind_thm("dropWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   970
Addsimps [dropWhile_append2];
nipkow@2608
   971
nipkow@4935
   972
Goal "x:set(takeWhile P xs) --> x:set xs & P x";
paulson@3457
   973
by (induct_tac "xs" 1);
paulson@5316
   974
by Auto_tac;
paulson@3647
   975
qed_spec_mp"set_take_whileD";
nipkow@2608
   976
nipkow@6306
   977
(** zip **)
nipkow@6306
   978
section "zip";
nipkow@6306
   979
nipkow@6306
   980
Goal "zip [] ys = []";
paulson@6813
   981
by (induct_tac "ys" 1);
nipkow@6306
   982
by Auto_tac;
nipkow@6306
   983
qed "zip_Nil";
nipkow@6306
   984
Addsimps [zip_Nil];
nipkow@6306
   985
nipkow@6306
   986
Goal "zip (x#xs) (y#ys) = (x,y)#zip xs ys";
paulson@6813
   987
by (Simp_tac 1);
nipkow@6306
   988
qed "zip_Cons_Cons";
nipkow@6306
   989
Addsimps [zip_Cons_Cons];
nipkow@6306
   990
nipkow@6306
   991
Delsimps(tl (thms"zip.simps"));
nipkow@4605
   992
nipkow@8118
   993
Goal "!xs. length (zip xs ys) = min (length xs) (length ys)";
nipkow@8009
   994
by (induct_tac "ys" 1);
nipkow@8009
   995
 by (Simp_tac 1);
nipkow@8009
   996
by (Clarify_tac 1);
wenzelm@8442
   997
by (case_tac "xs" 1);
paulson@8064
   998
 by (Auto_tac);
nipkow@8009
   999
qed_spec_mp "length_zip";
nipkow@8009
  1000
Addsimps [length_zip];
nipkow@8009
  1001
nipkow@8009
  1002
Goal
nipkow@8118
  1003
 "!xs. zip (xs@ys) zs = \
nipkow@8118
  1004
\      zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)";
paulson@8254
  1005
by (induct_tac "zs" 1);
paulson@8254
  1006
 by (Simp_tac 1);
paulson@8064
  1007
by (Clarify_tac 1);
wenzelm@8442
  1008
by (case_tac "xs" 1);
paulson@8254
  1009
 by (Asm_simp_tac 1);
paulson@8254
  1010
by (Asm_simp_tac 1);
nipkow@8118
  1011
qed_spec_mp "zip_append1";
nipkow@8118
  1012
nipkow@8118
  1013
Goal
nipkow@8118
  1014
 "!ys. zip xs (ys@zs) = \
nipkow@8118
  1015
\      zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs";
paulson@8254
  1016
by (induct_tac "xs" 1);
paulson@8254
  1017
 by (Simp_tac 1);
nipkow@8118
  1018
by (Clarify_tac 1);
wenzelm@8442
  1019
by (case_tac "ys" 1);
paulson@8254
  1020
 by (Asm_simp_tac 1);
paulson@8254
  1021
by (Asm_simp_tac 1);
nipkow@8118
  1022
qed_spec_mp "zip_append2";
nipkow@8118
  1023
nipkow@8118
  1024
Goal
nipkow@8118
  1025
 "[| length xs = length us; length ys = length vs |] ==> \
nipkow@8118
  1026
\ zip (xs@ys) (us@vs) = zip xs us @ zip ys vs";
paulson@8254
  1027
by (asm_simp_tac (simpset() addsimps [zip_append1]) 1);
nipkow@8009
  1028
qed_spec_mp "zip_append";
nipkow@8118
  1029
Addsimps [zip_append];
nipkow@8009
  1030
nipkow@8009
  1031
Goal "!xs. length xs = length ys --> zip (rev xs) (rev ys) = rev (zip xs ys)";
paulson@8064
  1032
by (induct_tac "ys" 1);
paulson@8064
  1033
 by (Asm_full_simp_tac 1);
paulson@8064
  1034
by (Asm_full_simp_tac 1);
paulson@8064
  1035
by (Clarify_tac 1);
wenzelm@8442
  1036
by (case_tac "xs" 1);
paulson@8064
  1037
 by (Auto_tac);
nipkow@8009
  1038
qed_spec_mp "zip_rev";
nipkow@8009
  1039
nipkow@8115
  1040
nipkow@8115
  1041
Goal
nipkow@8009
  1042
"!i xs. i < length xs --> i < length ys --> (zip xs ys)!i = (xs!i, ys!i)";
nipkow@8009
  1043
by (induct_tac "ys" 1);
nipkow@8009
  1044
 by (Simp_tac 1);
nipkow@8009
  1045
by (Clarify_tac 1);
wenzelm@8442
  1046
by (case_tac "xs" 1);
paulson@8064
  1047
 by (Auto_tac);
nipkow@8009
  1048
by (asm_full_simp_tac (simpset() addsimps (thms"nth.simps") addsplits [nat.split]) 1);
nipkow@8009
  1049
qed_spec_mp "nth_zip";
nipkow@8009
  1050
Addsimps [nth_zip];
nipkow@8009
  1051
nipkow@8118
  1052
Goal "set(zip xs ys) = {(xs!i,ys!i) |i. i < min (length xs) (length ys)}";
nipkow@8118
  1053
by (simp_tac (simpset() addsimps [set_conv_nth]addcongs [rev_conj_cong]) 1);
nipkow@8118
  1054
qed_spec_mp "set_zip";
nipkow@8118
  1055
nipkow@8009
  1056
Goal
nipkow@8009
  1057
 "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]";
paulson@8064
  1058
by (rtac sym 1);
paulson@8064
  1059
by (asm_simp_tac (simpset() addsimps [update_zip]) 1);
nipkow@8009
  1060
qed_spec_mp "zip_update";
nipkow@8009
  1061
nipkow@8009
  1062
Goal "!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)";
nipkow@8009
  1063
by (induct_tac "i" 1);
paulson@8064
  1064
 by (Auto_tac);
wenzelm@8442
  1065
by (case_tac "j" 1);
paulson@8064
  1066
 by (Auto_tac);
nipkow@8009
  1067
qed "zip_replicate";
nipkow@8009
  1068
Addsimps [zip_replicate];
nipkow@8009
  1069
nipkow@8115
  1070
(** list_all2 **)
nipkow@8115
  1071
section "list_all2";
nipkow@8115
  1072
nipkow@8115
  1073
Goalw [list_all2_def] "list_all2 P xs ys ==> length xs = length ys";
paulson@8254
  1074
by (Asm_simp_tac 1);
nipkow@8115
  1075
qed "list_all2_lengthD";
nipkow@8115
  1076
nipkow@8115
  1077
Goalw [list_all2_def] "list_all2 P [] ys = (ys=[])";
nipkow@8115
  1078
by (Simp_tac 1);
nipkow@8115
  1079
qed "list_all2_Nil";
nipkow@8115
  1080
AddIffs [list_all2_Nil];
nipkow@8115
  1081
nipkow@8115
  1082
Goalw [list_all2_def] "list_all2 P xs [] = (xs=[])";
nipkow@8115
  1083
by (Simp_tac 1);
nipkow@8115
  1084
qed "list_all2_Nil2";
nipkow@8115
  1085
AddIffs [list_all2_Nil2];
nipkow@8115
  1086
nipkow@8115
  1087
Goalw [list_all2_def]
nipkow@8115
  1088
 "list_all2 P (x#xs) (y#ys) = (P x y & list_all2 P xs ys)";
nipkow@8115
  1089
by (Auto_tac);
nipkow@8115
  1090
qed "list_all2_Cons";
nipkow@8115
  1091
AddIffs[list_all2_Cons];
nipkow@8115
  1092
nipkow@8115
  1093
Goalw [list_all2_def]
nipkow@8118
  1094
 "list_all2 P (x#xs) ys = (? z zs. ys = z#zs & P x z & list_all2 P xs zs)";
wenzelm@8442
  1095
by (case_tac "ys" 1);
paulson@8254
  1096
by (Auto_tac);
nipkow@8118
  1097
qed "list_all2_Cons1";
nipkow@8118
  1098
nipkow@8118
  1099
Goalw [list_all2_def]
nipkow@8118
  1100
 "list_all2 P xs (y#ys) = (? z zs. xs = z#zs & P z y & list_all2 P zs ys)";
wenzelm@8442
  1101
by (case_tac "xs" 1);
paulson@8254
  1102
by (Auto_tac);
nipkow@8118
  1103
qed "list_all2_Cons2";
nipkow@8118
  1104
nipkow@8118
  1105
Goalw [list_all2_def]
nipkow@8118
  1106
 "list_all2 P (xs@ys) zs = \
nipkow@8118
  1107
\ (EX us vs. zs = us@vs & length us = length xs & length vs = length ys & \
nipkow@8118
  1108
\            list_all2 P xs us & list_all2 P ys vs)";
paulson@8254
  1109
by (simp_tac (simpset() addsimps [zip_append1]) 1);
paulson@8254
  1110
by (rtac iffI 1);
paulson@8254
  1111
 by (res_inst_tac [("x","take (length xs) zs")] exI 1);
paulson@8254
  1112
 by (res_inst_tac [("x","drop (length xs) zs")] exI 1);
paulson@8254
  1113
 by (asm_full_simp_tac (simpset() addsimps [min_def,eq_sym_conv]) 1);
nipkow@8118
  1114
by (Clarify_tac 1);
paulson@8254
  1115
by (asm_full_simp_tac (simpset() addsimps [ball_Un]) 1);
nipkow@8118
  1116
qed "list_all2_append1";
nipkow@8118
  1117
nipkow@8118
  1118
Goalw [list_all2_def]
nipkow@8118
  1119
 "list_all2 P xs (ys@zs) = \
nipkow@8118
  1120
\ (EX us vs. xs = us@vs & length us = length ys & length vs = length zs & \
nipkow@8118
  1121
\            list_all2 P us ys & list_all2 P vs zs)";
paulson@8254
  1122
by (simp_tac (simpset() addsimps [zip_append2]) 1);
paulson@8254
  1123
by (rtac iffI 1);
paulson@8254
  1124
 by (res_inst_tac [("x","take (length ys) xs")] exI 1);
paulson@8254
  1125
 by (res_inst_tac [("x","drop (length ys) xs")] exI 1);
paulson@8254
  1126
 by (asm_full_simp_tac (simpset() addsimps [min_def,eq_sym_conv]) 1);
nipkow@8118
  1127
by (Clarify_tac 1);
paulson@8254
  1128
by (asm_full_simp_tac (simpset() addsimps [ball_Un]) 1);
nipkow@8118
  1129
qed "list_all2_append2";
nipkow@8118
  1130
nipkow@8118
  1131
Goalw [list_all2_def]
nipkow@8115
  1132
  "list_all2 P xs ys = \
nipkow@8115
  1133
\  (length xs = length ys & (!i<length xs. P (xs!i) (ys!i)))";
paulson@8254
  1134
by (force_tac (claset(), simpset() addsimps [set_zip]) 1);
nipkow@8115
  1135
qed "list_all2_conv_all_nth";
nipkow@5272
  1136
nipkow@5272
  1137
(** foldl **)
nipkow@5272
  1138
section "foldl";
nipkow@5272
  1139
nipkow@5272
  1140
Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys";
paulson@5318
  1141
by (induct_tac "xs" 1);
paulson@5316
  1142
by Auto_tac;
nipkow@5272
  1143
qed_spec_mp "foldl_append";
nipkow@5272
  1144
Addsimps [foldl_append];
nipkow@5272
  1145
nipkow@5272
  1146
(* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
nipkow@5272
  1147
   because it requires an additional transitivity step
nipkow@5272
  1148
*)
nipkow@5272
  1149
Goal "!n::nat. m <= n --> m <= foldl op+ n ns";
paulson@5318
  1150
by (induct_tac "ns" 1);
nipkow@6058
  1151
by Auto_tac;
nipkow@5272
  1152
qed_spec_mp "start_le_sum";
nipkow@5272
  1153
nipkow@5272
  1154
Goal "n : set ns ==> n <= foldl op+ 0 ns";
oheimb@5758
  1155
by (force_tac (claset() addIs [start_le_sum],
oheimb@5758
  1156
              simpset() addsimps [in_set_conv_decomp]) 1);
nipkow@5272
  1157
qed "elem_le_sum";
nipkow@5272
  1158
nipkow@5272
  1159
Goal "!m. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))";
paulson@5318
  1160
by (induct_tac "ns" 1);
paulson@5316
  1161
by Auto_tac;
nipkow@5272
  1162
qed_spec_mp "sum_eq_0_conv";
nipkow@5272
  1163
AddIffs [sum_eq_0_conv];
nipkow@5272
  1164
nipkow@5425
  1165
(** upto **)
nipkow@5425
  1166
nipkow@5427
  1167
(* Does not terminate! *)
nipkow@5427
  1168
Goal "[i..j(] = (if i<j then i#[Suc i..j(] else [])";
paulson@6162
  1169
by (induct_tac "j" 1);
nipkow@5427
  1170
by Auto_tac;
nipkow@5427
  1171
qed "upt_rec";
nipkow@5425
  1172
nipkow@5427
  1173
Goal "j<=i ==> [i..j(] = []";
paulson@6162
  1174
by (stac upt_rec 1);
paulson@6162
  1175
by (Asm_simp_tac 1);
nipkow@5427
  1176
qed "upt_conv_Nil";
nipkow@5427
  1177
Addsimps [upt_conv_Nil];
nipkow@5427
  1178
nipkow@5427
  1179
Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]";
nipkow@5427
  1180
by (Asm_simp_tac 1);
nipkow@5427
  1181
qed "upt_Suc";
nipkow@5427
  1182
nipkow@5427
  1183
Goal "i<j ==> [i..j(] = i#[Suc i..j(]";
paulson@6162
  1184
by (rtac trans 1);
paulson@6162
  1185
by (stac upt_rec 1);
paulson@6162
  1186
by (rtac refl 2);
nipkow@5427
  1187
by (Asm_simp_tac 1);
nipkow@5427
  1188
qed "upt_conv_Cons";
nipkow@5427
  1189
nipkow@5427
  1190
Goal "length [i..j(] = j-i";
paulson@6162
  1191
by (induct_tac "j" 1);
nipkow@5427
  1192
 by (Simp_tac 1);
paulson@6162
  1193
by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
nipkow@5427
  1194
qed "length_upt";
nipkow@5427
  1195
Addsimps [length_upt];
nipkow@5425
  1196
nipkow@5427
  1197
Goal "i+k < j --> [i..j(] ! k = i+k";
paulson@6162
  1198
by (induct_tac "j" 1);
paulson@6162
  1199
 by (Simp_tac 1);
paulson@6162
  1200
by (asm_simp_tac (simpset() addsimps [nth_append,less_diff_conv]@add_ac) 1);
paulson@6162
  1201
by (Clarify_tac 1);
paulson@6162
  1202
by (subgoal_tac "n=i+k" 1);
paulson@6162
  1203
 by (Asm_simp_tac 2);
paulson@6162
  1204
by (Asm_simp_tac 1);
nipkow@5427
  1205
qed_spec_mp "nth_upt";
nipkow@5427
  1206
Addsimps [nth_upt];
nipkow@5425
  1207
nipkow@6433
  1208
Goal "!i. i+m <= n --> take m [i..n(] = [i..i+m(]";
paulson@6813
  1209
by (induct_tac "m" 1);
paulson@6813
  1210
 by (Simp_tac 1);
paulson@6813
  1211
by (Clarify_tac 1);
paulson@6813
  1212
by (stac upt_rec 1);
paulson@6813
  1213
by (rtac sym 1);
paulson@6813
  1214
by (stac upt_rec 1);
paulson@6813
  1215
by (asm_simp_tac (simpset() delsimps (thms"upt.simps")) 1);
nipkow@6433
  1216
qed_spec_mp "take_upt";
nipkow@6433
  1217
Addsimps [take_upt];
nipkow@6433
  1218
nipkow@6433
  1219
Goal "!m i. i < n-m --> (map f [m..n(]) ! i = f(m+i)";
paulson@6813
  1220
by (induct_tac "n" 1);
paulson@6813
  1221
 by (Simp_tac 1);
paulson@6813
  1222
by (Clarify_tac 1);
paulson@6813
  1223
by (subgoal_tac "m < Suc n" 1);
paulson@6813
  1224
 by (arith_tac 2);
paulson@6813
  1225
by (stac upt_rec 1);
paulson@6813
  1226
by (asm_simp_tac (simpset() delsplits [split_if]) 1);
paulson@6813
  1227
by (split_tac [split_if] 1);
paulson@6813
  1228
by (rtac conjI 1);
paulson@6813
  1229
 by (simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
paulson@6813
  1230
 by (simp_tac (simpset() addsimps [nth_append] addsplits [nat.split]) 1);
paulson@6813
  1231
 by (Clarify_tac 1);
paulson@6813
  1232
 by (rtac conjI 1);
paulson@6813
  1233
  by (Clarify_tac 1);
paulson@6813
  1234
  by (subgoal_tac "Suc(m+nat) < n" 1);
paulson@6813
  1235
   by (arith_tac 2);
paulson@6813
  1236
  by (Asm_simp_tac 1);
paulson@6813
  1237
 by (Clarify_tac 1);
paulson@6813
  1238
 by (subgoal_tac "n = Suc(m+nat)" 1);
paulson@6813
  1239
  by (arith_tac 2);
paulson@6813
  1240
 by (Asm_simp_tac 1);
paulson@6813
  1241
by (simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
paulson@6813
  1242
by (arith_tac 1);
nipkow@6433
  1243
qed_spec_mp "nth_map_upt";
nipkow@6433
  1244
paulson@6813
  1245
Goal "ALL xs ys. k <= length xs --> k <= length ys -->  \
paulson@6813
  1246
\        (ALL i. i < k --> xs!i = ys!i)  \
paulson@6813
  1247
\     --> take k xs = take k ys";
paulson@6813
  1248
by (induct_tac "k" 1);
paulson@6813
  1249
by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq_0_disj, 
paulson@6813
  1250
						all_conj_distrib])));
paulson@6813
  1251
by (Clarify_tac 1);
paulson@6813
  1252
(*Both lists must be non-empty*)
wenzelm@8442
  1253
by (case_tac "xs" 1);
wenzelm@8442
  1254
by (case_tac "ys" 2);
paulson@6813
  1255
by (ALLGOALS Clarify_tac);
paulson@6813
  1256
(*prenexing's needed, not miniscoping*)
paulson@6813
  1257
by (ALLGOALS (full_simp_tac (simpset() addsimps (all_simps RL [sym])  
paulson@6813
  1258
                                       delsimps (all_simps))));
paulson@6813
  1259
by (Blast_tac 1);
paulson@6813
  1260
qed_spec_mp "nth_take_lemma";
paulson@6813
  1261
paulson@6813
  1262
Goal "[| length xs = length ys;  \
paulson@6813
  1263
\        ALL i. i < length xs --> xs!i = ys!i |]  \
paulson@6813
  1264
\     ==> xs = ys";
paulson@6813
  1265
by (forward_tac [[le_refl, eq_imp_le] MRS nth_take_lemma] 1);
paulson@6813
  1266
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [take_all])));
paulson@6813
  1267
qed_spec_mp "nth_equalityI";
paulson@6813
  1268
paulson@6813
  1269
(*The famous take-lemma*)
paulson@6813
  1270
Goal "(ALL i. take i xs = take i ys) ==> xs = ys";
paulson@6813
  1271
by (dres_inst_tac [("x", "max (length xs) (length ys)")] spec 1);
paulson@6813
  1272
by (full_simp_tac (simpset() addsimps [le_max_iff_disj, take_all]) 1);
paulson@6813
  1273
qed_spec_mp "take_equalityI";
paulson@6813
  1274
nipkow@5272
  1275
nipkow@4605
  1276
(** nodups & remdups **)
nipkow@4605
  1277
section "nodups & remdups";
nipkow@4605
  1278
nipkow@4935
  1279
Goal "set(remdups xs) = set xs";
nipkow@4605
  1280
by (induct_tac "xs" 1);
nipkow@4605
  1281
 by (Simp_tac 1);
nipkow@4686
  1282
by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
nipkow@4605
  1283
qed "set_remdups";
nipkow@4605
  1284
Addsimps [set_remdups];
nipkow@4605
  1285
nipkow@4935
  1286
Goal "nodups(remdups xs)";
nipkow@4605
  1287
by (induct_tac "xs" 1);
paulson@5316
  1288
by Auto_tac;
nipkow@4605
  1289
qed "nodups_remdups";
nipkow@4605
  1290
nipkow@4935
  1291
Goal "nodups xs --> nodups (filter P xs)";
nipkow@4605
  1292
by (induct_tac "xs" 1);
paulson@5316
  1293
by Auto_tac;
nipkow@4605
  1294
qed_spec_mp "nodups_filter";
nipkow@4605
  1295
nipkow@3589
  1296
(** replicate **)
nipkow@3589
  1297
section "replicate";
nipkow@3589
  1298
nipkow@6794
  1299
Goal "length(replicate n x) = n";
paulson@6813
  1300
by (induct_tac "n" 1);
paulson@6813
  1301
by Auto_tac;
nipkow@6794
  1302
qed "length_replicate";
nipkow@6794
  1303
Addsimps [length_replicate];
nipkow@6794
  1304
nipkow@6794
  1305
Goal "map f (replicate n x) = replicate n (f x)";
nipkow@6794
  1306
by (induct_tac "n" 1);
paulson@6813
  1307
by Auto_tac;
nipkow@6794
  1308
qed "map_replicate";
nipkow@6794
  1309
Addsimps [map_replicate];
nipkow@6794
  1310
nipkow@6794
  1311
Goal "(replicate n x) @ (x#xs) = x # replicate n x @ xs";
nipkow@6794
  1312
by (induct_tac "n" 1);
paulson@6813
  1313
by Auto_tac;
nipkow@6794
  1314
qed "replicate_app_Cons_same";
nipkow@6794
  1315
nipkow@6794
  1316
Goal "rev(replicate n x) = replicate n x";
nipkow@6794
  1317
by (induct_tac "n" 1);
paulson@6813
  1318
 by (Simp_tac 1);
nipkow@6794
  1319
by (asm_simp_tac (simpset() addsimps [replicate_app_Cons_same]) 1);
nipkow@6794
  1320
qed "rev_replicate";
nipkow@6794
  1321
Addsimps [rev_replicate];
nipkow@6794
  1322
nipkow@8009
  1323
Goal "replicate (n+m) x = replicate n x @ replicate m x";
nipkow@8009
  1324
by (induct_tac "n" 1);
nipkow@8009
  1325
by Auto_tac;
nipkow@8009
  1326
qed "replicate_add";
nipkow@8009
  1327
nipkow@6794
  1328
Goal"n ~= 0 --> hd(replicate n x) = x";
nipkow@6794
  1329
by (induct_tac "n" 1);
paulson@6813
  1330
by Auto_tac;
nipkow@6794
  1331
qed_spec_mp "hd_replicate";
nipkow@6794
  1332
Addsimps [hd_replicate];
nipkow@6794
  1333
nipkow@6794
  1334
Goal "n ~= 0 --> tl(replicate n x) = replicate (n-1) x";
nipkow@6794
  1335
by (induct_tac "n" 1);
paulson@6813
  1336
by Auto_tac;
nipkow@6794
  1337
qed_spec_mp "tl_replicate";
nipkow@6794
  1338
Addsimps [tl_replicate];
nipkow@6794
  1339
nipkow@6794
  1340
Goal "n ~= 0 --> last(replicate n x) = x";
nipkow@6794
  1341
by (induct_tac "n" 1);
paulson@6813
  1342
by Auto_tac;
nipkow@6794
  1343
qed_spec_mp "last_replicate";
nipkow@6794
  1344
Addsimps [last_replicate];
nipkow@6794
  1345
nipkow@6794
  1346
Goal "!i. i<n --> (replicate n x)!i = x";
paulson@6813
  1347
by (induct_tac "n" 1);
paulson@6813
  1348
 by (Simp_tac 1);
paulson@6813
  1349
by (asm_simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
nipkow@6794
  1350
qed_spec_mp "nth_replicate";
nipkow@6794
  1351
Addsimps [nth_replicate];
nipkow@6794
  1352
nipkow@4935
  1353
Goal "set(replicate (Suc n) x) = {x}";
wenzelm@4423
  1354
by (induct_tac "n" 1);
paulson@5316
  1355
by Auto_tac;
nipkow@3589
  1356
val lemma = result();
nipkow@3589
  1357
nipkow@5043
  1358
Goal "n ~= 0 ==> set(replicate n x) = {x}";
wenzelm@4423
  1359
by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
nipkow@3589
  1360
qed "set_replicate";
nipkow@3589
  1361
Addsimps [set_replicate];
nipkow@5162
  1362
nipkow@8009
  1363
Goal "set(replicate n x) = (if n=0 then {} else {x})";
paulson@8064
  1364
by (Auto_tac);
nipkow@8009
  1365
qed "set_replicate_conv_if";
nipkow@8009
  1366
nipkow@8009
  1367
Goal "x : set(replicate n y) --> x=y";
paulson@8064
  1368
by (asm_simp_tac (simpset() addsimps [set_replicate_conv_if]) 1);
nipkow@8009
  1369
qed_spec_mp "in_set_replicateD";
nipkow@8009
  1370
nipkow@5162
  1371
nipkow@5281
  1372
(*** Lexcicographic orderings on lists ***)
nipkow@5281
  1373
section"Lexcicographic orderings on lists";
nipkow@5281
  1374
nipkow@5281
  1375
Goal "wf r ==> wf(lexn r n)";
paulson@5318
  1376
by (induct_tac "n" 1);
paulson@5318
  1377
by (Simp_tac 1);
paulson@5318
  1378
by (Simp_tac 1);
paulson@5318
  1379
by (rtac wf_subset 1);
paulson@5318
  1380
by (rtac Int_lower1 2);
paulson@5318
  1381
by (rtac wf_prod_fun_image 1);
paulson@5318
  1382
by (rtac injI 2);
paulson@6813
  1383
by Auto_tac;
nipkow@5281
  1384
qed "wf_lexn";
nipkow@5281
  1385
nipkow@5281
  1386
Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n";
paulson@5318
  1387
by (induct_tac "n" 1);
paulson@6813
  1388
by Auto_tac;
nipkow@5281
  1389
qed_spec_mp "lexn_length";
nipkow@5281
  1390
nipkow@5281
  1391
Goalw [lex_def] "wf r ==> wf(lex r)";
paulson@5318
  1392
by (rtac wf_UN 1);
paulson@5318
  1393
by (blast_tac (claset() addIs [wf_lexn]) 1);
paulson@5318
  1394
by (Clarify_tac 1);
paulson@5318
  1395
by (rename_tac "m n" 1);
paulson@5318
  1396
by (subgoal_tac "m ~= n" 1);
paulson@5318
  1397
 by (Blast_tac 2);
paulson@5318
  1398
by (blast_tac (claset() addDs [lexn_length,not_sym]) 1);
nipkow@5281
  1399
qed "wf_lex";
nipkow@5281
  1400
AddSIs [wf_lex];
nipkow@5281
  1401
nipkow@5281
  1402
Goal
nipkow@5281
  1403
 "lexn r n = \
nipkow@5281
  1404
\ {(xs,ys). length xs = n & length ys = n & \
nipkow@5281
  1405
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5318
  1406
by (induct_tac "n" 1);
paulson@5318
  1407
 by (Simp_tac 1);
paulson@5318
  1408
 by (Blast_tac 1);
paulson@5641
  1409
by (asm_full_simp_tac (simpset() 
oheimb@5296
  1410
				addsimps [lex_prod_def]) 1);
paulson@5641
  1411
by (auto_tac (claset(), simpset()));
paulson@5318
  1412
  by (Blast_tac 1);
paulson@5318
  1413
 by (rename_tac "a xys x xs' y ys'" 1);
paulson@5318
  1414
 by (res_inst_tac [("x","a#xys")] exI 1);
paulson@5318
  1415
 by (Simp_tac 1);
wenzelm@8442
  1416
by (case_tac "xys" 1);
paulson@5641
  1417
 by (ALLGOALS (asm_full_simp_tac (simpset())));
paulson@5318
  1418
by (Blast_tac 1);
nipkow@5281
  1419
qed "lexn_conv";
nipkow@5281
  1420
nipkow@5281
  1421
Goalw [lex_def]
nipkow@5281
  1422
 "lex r = \
nipkow@5281
  1423
\ {(xs,ys). length xs = length ys & \
nipkow@5281
  1424
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5641
  1425
by (force_tac (claset(), simpset() addsimps [lexn_conv]) 1);
nipkow@5281
  1426
qed "lex_conv";
nipkow@5281
  1427
nipkow@5281
  1428
Goalw [lexico_def] "wf r ==> wf(lexico r)";
paulson@5318
  1429
by (Blast_tac 1);
nipkow@5281
  1430
qed "wf_lexico";
nipkow@5281
  1431
AddSIs [wf_lexico];
nipkow@5281
  1432
nipkow@5281
  1433
Goalw
nipkow@5281
  1434
 [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def]
nipkow@5281
  1435
"lexico r = {(xs,ys). length xs < length ys | \
nipkow@5281
  1436
\                     length xs = length ys & (xs,ys) : lex r}";
paulson@5318
  1437
by (Simp_tac 1);
nipkow@5281
  1438
qed "lexico_conv";
nipkow@5281
  1439
nipkow@5283
  1440
Goal "([],ys) ~: lex r";
paulson@5318
  1441
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1442
qed "Nil_notin_lex";
nipkow@5283
  1443
nipkow@5283
  1444
Goal "(xs,[]) ~: lex r";
paulson@5318
  1445
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1446
qed "Nil2_notin_lex";
nipkow@5283
  1447
nipkow@5283
  1448
AddIffs [Nil_notin_lex,Nil2_notin_lex];
nipkow@5283
  1449
nipkow@5283
  1450
Goal "((x#xs,y#ys) : lex r) = \
nipkow@5283
  1451
\     ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)";
paulson@5318
  1452
by (simp_tac (simpset() addsimps [lex_conv]) 1);
paulson@5318
  1453
by (rtac iffI 1);
paulson@5318
  1454
 by (blast_tac (claset() addIs [Cons_eq_appendI]) 2);
paulson@5318
  1455
by (REPEAT(eresolve_tac [conjE, exE] 1));
wenzelm@8442
  1456
by (case_tac "xys" 1);
paulson@5318
  1457
by (Asm_full_simp_tac 1);
paulson@5318
  1458
by (Asm_full_simp_tac 1);
paulson@5318
  1459
by (Blast_tac 1);
nipkow@5283
  1460
qed "Cons_in_lex";
nipkow@5283
  1461
AddIffs [Cons_in_lex];
paulson@7032
  1462
paulson@7032
  1463
paulson@7032
  1464
(*** Versions of some theorems above using binary numerals ***)
paulson@7032
  1465
paulson@7032
  1466
AddIffs (map (rename_numerals thy) 
paulson@7032
  1467
	  [length_0_conv, zero_length_conv, length_greater_0_conv,
paulson@7032
  1468
	   sum_eq_0_conv]);
paulson@7032
  1469
paulson@7032
  1470
Goal "take n (x#xs) = (if n = #0 then [] else x # take (n-#1) xs)";
wenzelm@8442
  1471
by (case_tac "n" 1);
paulson@7032
  1472
by (ALLGOALS 
paulson@7032
  1473
    (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
paulson@7032
  1474
qed "take_Cons'";
paulson@7032
  1475
paulson@7032
  1476
Goal "drop n (x#xs) = (if n = #0 then x#xs else drop (n-#1) xs)";
wenzelm@8442
  1477
by (case_tac "n" 1);
paulson@7032
  1478
by (ALLGOALS
paulson@7032
  1479
    (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
paulson@7032
  1480
qed "drop_Cons'";
paulson@7032
  1481
paulson@7032
  1482
Goal "(x#xs)!n = (if n = #0 then x else xs!(n-#1))";
wenzelm@8442
  1483
by (case_tac "n" 1);
paulson@7032
  1484
by (ALLGOALS
paulson@7032
  1485
    (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
paulson@7032
  1486
qed "nth_Cons'";
paulson@7032
  1487
paulson@7032
  1488
Addsimps (map (inst "n" "number_of ?v") [take_Cons', drop_Cons', nth_Cons']);
paulson@7032
  1489