src/HOL/List.ML
author wenzelm
Thu Jun 22 23:04:34 2000 +0200 (2000-06-22)
changeset 9108 9fff97d29837
parent 9014 4382883421ec
child 9187 68ecc04785f1
permissions -rw-r--r--
bind_thm(s);
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
wenzelm@9108
    35
bind_thm ("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
paulson@9003
   184
AddIffs [same_append_eq, append1_eq_conv, append_same_eq];
nipkow@3571
   185
nipkow@4935
   186
Goal "(xs @ ys = ys) = (xs=[])";
wenzelm@5132
   187
by (cut_inst_tac [("zs","[]")] append_same_eq 1);
paulson@5316
   188
by Auto_tac;
nipkow@4647
   189
qed "append_self_conv2";
nipkow@4647
   190
nipkow@4935
   191
Goal "(ys = xs @ ys) = (xs=[])";
wenzelm@5132
   192
by (simp_tac (simpset() addsimps
nipkow@4647
   193
     [simplify (simpset()) (read_instantiate[("ys","[]")]append_same_eq)]) 1);
wenzelm@5132
   194
by (Blast_tac 1);
nipkow@4647
   195
qed "self_append_conv2";
nipkow@4647
   196
AddIffs [append_self_conv2,self_append_conv2];
nipkow@4647
   197
nipkow@4935
   198
Goal "xs ~= [] --> hd xs # tl xs = xs";
paulson@3457
   199
by (induct_tac "xs" 1);
paulson@5316
   200
by Auto_tac;
nipkow@2608
   201
qed_spec_mp "hd_Cons_tl";
nipkow@2608
   202
Addsimps [hd_Cons_tl];
clasohm@923
   203
nipkow@4935
   204
Goal "hd(xs@ys) = (if xs=[] then hd ys else hd xs)";
nipkow@3040
   205
by (induct_tac "xs" 1);
paulson@5316
   206
by Auto_tac;
nipkow@1327
   207
qed "hd_append";
clasohm@923
   208
nipkow@5043
   209
Goal "xs ~= [] ==> hd(xs @ ys) = hd xs";
wenzelm@4089
   210
by (asm_simp_tac (simpset() addsimps [hd_append]
berghofe@5183
   211
                           addsplits [list.split]) 1);
nipkow@3571
   212
qed "hd_append2";
nipkow@3571
   213
Addsimps [hd_append2];
nipkow@3571
   214
nipkow@4935
   215
Goal "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)";
berghofe@5183
   216
by (simp_tac (simpset() addsplits [list.split]) 1);
nipkow@2608
   217
qed "tl_append";
nipkow@2608
   218
nipkow@5043
   219
Goal "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys";
wenzelm@4089
   220
by (asm_simp_tac (simpset() addsimps [tl_append]
berghofe@5183
   221
                           addsplits [list.split]) 1);
nipkow@3571
   222
qed "tl_append2";
nipkow@3571
   223
Addsimps [tl_append2];
nipkow@3571
   224
nipkow@5272
   225
(* trivial rules for solving @-equations automatically *)
nipkow@5272
   226
nipkow@5272
   227
Goal "xs = ys ==> xs = [] @ ys";
paulson@5318
   228
by (Asm_simp_tac 1);
nipkow@5272
   229
qed "eq_Nil_appendI";
nipkow@5272
   230
nipkow@5272
   231
Goal "[| x#xs1 = ys; xs = xs1 @ zs |] ==> x#xs = ys@zs";
paulson@5318
   232
by (dtac sym 1);
paulson@5318
   233
by (Asm_simp_tac 1);
nipkow@5272
   234
qed "Cons_eq_appendI";
nipkow@5272
   235
nipkow@5272
   236
Goal "[| xs@xs1 = zs; ys = xs1 @ us |] ==> xs@ys = zs@us";
paulson@5318
   237
by (dtac sym 1);
paulson@5318
   238
by (Asm_simp_tac 1);
nipkow@5272
   239
qed "append_eq_appendI";
nipkow@5272
   240
nipkow@4830
   241
nipkow@5427
   242
(***
nipkow@5427
   243
Simplification procedure for all list equalities.
nipkow@5427
   244
Currently only tries to rearranges @ to see if
nipkow@5427
   245
- both lists end in a singleton list,
nipkow@5427
   246
- or both lists end in the same list.
nipkow@5427
   247
***)
nipkow@5427
   248
local
nipkow@5427
   249
nipkow@5427
   250
val list_eq_pattern =
wenzelm@6394
   251
  Thm.read_cterm (Theory.sign_of List.thy) ("(xs::'a list) = ys",HOLogic.boolT);
nipkow@5427
   252
wenzelm@7224
   253
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
wenzelm@7224
   254
      (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
nipkow@5427
   255
  | last (Const("List.op @",_) $ _ $ ys) = last ys
nipkow@5427
   256
  | last t = t;
nipkow@5427
   257
wenzelm@7224
   258
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
nipkow@5427
   259
  | list1 _ = false;
nipkow@5427
   260
wenzelm@7224
   261
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
wenzelm@7224
   262
      (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
nipkow@5427
   263
  | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
wenzelm@7224
   264
  | butlast xs = Const("List.list.Nil",fastype_of xs);
nipkow@5427
   265
nipkow@5427
   266
val rearr_tac =
nipkow@5427
   267
  simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons]);
nipkow@5427
   268
nipkow@5427
   269
fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
nipkow@5427
   270
  let
nipkow@5427
   271
    val lastl = last lhs and lastr = last rhs
nipkow@5427
   272
    fun rearr conv =
nipkow@5427
   273
      let val lhs1 = butlast lhs and rhs1 = butlast rhs
nipkow@5427
   274
          val Type(_,listT::_) = eqT
nipkow@5427
   275
          val appT = [listT,listT] ---> listT
nipkow@5427
   276
          val app = Const("List.op @",appT)
nipkow@5427
   277
          val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
nipkow@5427
   278
          val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2)))
nipkow@5427
   279
          val thm = prove_goalw_cterm [] ct (K [rearr_tac 1])
nipkow@5427
   280
            handle ERROR =>
nipkow@5427
   281
            error("The error(s) above occurred while trying to prove " ^
nipkow@5427
   282
                  string_of_cterm ct)
nipkow@5427
   283
      in Some((conv RS (thm RS trans)) RS eq_reflection) end
nipkow@5427
   284
nipkow@5427
   285
  in if list1 lastl andalso list1 lastr
nipkow@5427
   286
     then rearr append1_eq_conv
nipkow@5427
   287
     else
nipkow@5427
   288
     if lastl aconv lastr
nipkow@5427
   289
     then rearr append_same_eq
nipkow@5427
   290
     else None
nipkow@5427
   291
  end;
nipkow@5427
   292
in
nipkow@5427
   293
val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq;
nipkow@5427
   294
end;
nipkow@5427
   295
nipkow@5427
   296
Addsimprocs [list_eq_simproc];
nipkow@5427
   297
nipkow@5427
   298
nipkow@2608
   299
(** map **)
nipkow@2608
   300
nipkow@3467
   301
section "map";
nipkow@3467
   302
paulson@5278
   303
Goal "(!x. x : set xs --> f x = g x) --> map f xs = map g xs";
paulson@3457
   304
by (induct_tac "xs" 1);
paulson@5316
   305
by Auto_tac;
nipkow@2608
   306
bind_thm("map_ext", impI RS (allI RS (result() RS mp)));
nipkow@2608
   307
nipkow@4935
   308
Goal "map (%x. x) = (%xs. xs)";
nipkow@2608
   309
by (rtac ext 1);
nipkow@3040
   310
by (induct_tac "xs" 1);
paulson@5316
   311
by Auto_tac;
nipkow@2608
   312
qed "map_ident";
nipkow@2608
   313
Addsimps[map_ident];
nipkow@2608
   314
nipkow@4935
   315
Goal "map f (xs@ys) = map f xs @ map f ys";
nipkow@3040
   316
by (induct_tac "xs" 1);
paulson@5316
   317
by Auto_tac;
nipkow@2608
   318
qed "map_append";
nipkow@2608
   319
Addsimps[map_append];
nipkow@2608
   320
nipkow@4935
   321
Goalw [o_def] "map (f o g) xs = map f (map g xs)";
nipkow@3040
   322
by (induct_tac "xs" 1);
paulson@5316
   323
by Auto_tac;
nipkow@2608
   324
qed "map_compose";
nipkow@2608
   325
Addsimps[map_compose];
nipkow@2608
   326
nipkow@4935
   327
Goal "rev(map f xs) = map f (rev xs)";
nipkow@3040
   328
by (induct_tac "xs" 1);
paulson@5316
   329
by Auto_tac;
nipkow@2608
   330
qed "rev_map";
nipkow@2608
   331
nipkow@3589
   332
(* a congruence rule for map: *)
paulson@6451
   333
Goal "xs=ys ==> (!x. x : set ys --> f x = g x) --> map f xs = map g ys";
wenzelm@4423
   334
by (hyp_subst_tac 1);
wenzelm@4423
   335
by (induct_tac "ys" 1);
paulson@5316
   336
by Auto_tac;
paulson@6451
   337
bind_thm("map_cong", impI RSN (2,allI RSN (2, result() RS mp)));
nipkow@3589
   338
nipkow@4935
   339
Goal "(map f xs = []) = (xs = [])";
wenzelm@8442
   340
by (case_tac "xs" 1);
paulson@5316
   341
by Auto_tac;
nipkow@3860
   342
qed "map_is_Nil_conv";
nipkow@3860
   343
AddIffs [map_is_Nil_conv];
nipkow@3860
   344
nipkow@4935
   345
Goal "([] = map f xs) = (xs = [])";
wenzelm@8442
   346
by (case_tac "xs" 1);
paulson@5316
   347
by Auto_tac;
nipkow@3860
   348
qed "Nil_is_map_conv";
nipkow@3860
   349
AddIffs [Nil_is_map_conv];
nipkow@3860
   350
nipkow@8009
   351
Goal "(map f xs = y#ys) = (? x xs'. xs = x#xs' & f x = y & map f xs' = ys)";
wenzelm@8442
   352
by (case_tac "xs" 1);
nipkow@8009
   353
by (ALLGOALS Asm_simp_tac);
nipkow@8009
   354
qed "map_eq_Cons";
nipkow@8009
   355
nipkow@8009
   356
Goal "!xs. map f xs = map f ys --> (!x y. f x = f y --> x=y) --> xs=ys";
nipkow@8009
   357
by (induct_tac "ys" 1);
nipkow@8009
   358
 by (Asm_simp_tac 1);
nipkow@8009
   359
by (fast_tac (claset() addss (simpset() addsimps [map_eq_Cons])) 1);
nipkow@8009
   360
qed_spec_mp "map_injective";
nipkow@8009
   361
nipkow@8009
   362
Goal "inj f ==> inj (map f)";
paulson@8064
   363
by (blast_tac (claset() addDs [map_injective,injD] addIs [injI]) 1);
nipkow@8009
   364
qed "inj_mapI";
nipkow@8009
   365
nipkow@8009
   366
Goalw [inj_on_def] "inj (map f) ==> inj f";
paulson@8064
   367
by (Clarify_tac 1);
paulson@8064
   368
by (eres_inst_tac [("x","[x]")] ballE 1);
paulson@8064
   369
 by (eres_inst_tac [("x","[y]")] ballE 1);
paulson@8064
   370
  by (Asm_full_simp_tac 1);
paulson@8064
   371
 by (Blast_tac 1);
paulson@8064
   372
by (Blast_tac 1);
nipkow@8009
   373
qed "inj_mapD";
nipkow@8009
   374
nipkow@8009
   375
Goal "inj (map f) = inj f";
paulson@8064
   376
by (blast_tac (claset() addDs [inj_mapD] addIs [inj_mapI]) 1);
nipkow@8009
   377
qed "inj_map";
nipkow@3860
   378
lcp@1169
   379
(** rev **)
lcp@1169
   380
nipkow@3467
   381
section "rev";
nipkow@3467
   382
nipkow@4935
   383
Goal "rev(xs@ys) = rev(ys) @ rev(xs)";
nipkow@3040
   384
by (induct_tac "xs" 1);
paulson@5316
   385
by Auto_tac;
lcp@1169
   386
qed "rev_append";
nipkow@2512
   387
Addsimps[rev_append];
lcp@1169
   388
nipkow@4935
   389
Goal "rev(rev l) = l";
nipkow@3040
   390
by (induct_tac "l" 1);
paulson@5316
   391
by Auto_tac;
lcp@1169
   392
qed "rev_rev_ident";
nipkow@2512
   393
Addsimps[rev_rev_ident];
lcp@1169
   394
nipkow@4935
   395
Goal "(rev xs = []) = (xs = [])";
wenzelm@4423
   396
by (induct_tac "xs" 1);
paulson@5316
   397
by Auto_tac;
nipkow@3860
   398
qed "rev_is_Nil_conv";
nipkow@3860
   399
AddIffs [rev_is_Nil_conv];
nipkow@3860
   400
nipkow@4935
   401
Goal "([] = rev xs) = (xs = [])";
wenzelm@4423
   402
by (induct_tac "xs" 1);
paulson@5316
   403
by Auto_tac;
nipkow@3860
   404
qed "Nil_is_rev_conv";
nipkow@3860
   405
AddIffs [Nil_is_rev_conv];
nipkow@3860
   406
nipkow@6820
   407
Goal "!ys. (rev xs = rev ys) = (xs = ys)";
paulson@6831
   408
by (induct_tac "xs" 1);
nipkow@6820
   409
 by (Force_tac 1);
paulson@6831
   410
by (rtac allI 1);
wenzelm@8442
   411
by (case_tac "ys" 1);
nipkow@6820
   412
 by (Asm_simp_tac 1);
nipkow@6820
   413
by (Force_tac 1);
nipkow@6820
   414
qed_spec_mp "rev_is_rev_conv";
nipkow@6820
   415
AddIffs [rev_is_rev_conv];
nipkow@6820
   416
nipkow@4935
   417
val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
wenzelm@5132
   418
by (stac (rev_rev_ident RS sym) 1);
paulson@6162
   419
by (res_inst_tac [("list", "rev xs")] list.induct 1);
wenzelm@5132
   420
by (ALLGOALS Simp_tac);
wenzelm@5132
   421
by (resolve_tac prems 1);
wenzelm@5132
   422
by (eresolve_tac prems 1);
nipkow@4935
   423
qed "rev_induct";
nipkow@4935
   424
nipkow@5272
   425
fun rev_induct_tac xs = res_inst_tac [("xs",xs)] rev_induct;
nipkow@5272
   426
nipkow@4935
   427
Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
wenzelm@5132
   428
by (res_inst_tac [("xs","xs")] rev_induct 1);
paulson@5316
   429
by Auto_tac;
nipkow@4935
   430
bind_thm ("rev_exhaust",
nipkow@4935
   431
  impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp)))));
nipkow@4935
   432
nipkow@2608
   433
nipkow@3465
   434
(** set **)
paulson@1812
   435
nipkow@3467
   436
section "set";
nipkow@3467
   437
paulson@7032
   438
Goal "finite (set xs)";
paulson@7032
   439
by (induct_tac "xs" 1);
paulson@7032
   440
by Auto_tac;
paulson@7032
   441
qed "finite_set";
paulson@7032
   442
AddIffs [finite_set];
oheimb@5296
   443
nipkow@4935
   444
Goal "set (xs@ys) = (set xs Un set ys)";
nipkow@3040
   445
by (induct_tac "xs" 1);
paulson@5316
   446
by Auto_tac;
paulson@3647
   447
qed "set_append";
paulson@3647
   448
Addsimps[set_append];
paulson@1812
   449
nipkow@4935
   450
Goal "set l <= set (x#l)";
paulson@5316
   451
by Auto_tac;
paulson@3647
   452
qed "set_subset_Cons";
paulson@1936
   453
nipkow@4935
   454
Goal "(set xs = {}) = (xs = [])";
paulson@3457
   455
by (induct_tac "xs" 1);
paulson@5316
   456
by Auto_tac;
paulson@3647
   457
qed "set_empty";
paulson@3647
   458
Addsimps [set_empty];
nipkow@2608
   459
nipkow@4935
   460
Goal "set(rev xs) = set(xs)";
paulson@3457
   461
by (induct_tac "xs" 1);
paulson@5316
   462
by Auto_tac;
paulson@3647
   463
qed "set_rev";
paulson@3647
   464
Addsimps [set_rev];
nipkow@2608
   465
nipkow@4935
   466
Goal "set(map f xs) = f``(set xs)";
paulson@3457
   467
by (induct_tac "xs" 1);
paulson@5316
   468
by Auto_tac;
paulson@3647
   469
qed "set_map";
paulson@3647
   470
Addsimps [set_map];
nipkow@2608
   471
nipkow@6433
   472
Goal "set(filter P xs) = {x. x : set xs & P x}";
paulson@6813
   473
by (induct_tac "xs" 1);
paulson@6813
   474
by Auto_tac;
nipkow@6433
   475
qed "set_filter";
nipkow@6433
   476
Addsimps [set_filter];
nipkow@8009
   477
nipkow@6433
   478
Goal "set[i..j(] = {k. i <= k & k < j}";
paulson@6813
   479
by (induct_tac "j" 1);
paulson@6813
   480
by Auto_tac;
paulson@6813
   481
by (arith_tac 1);
nipkow@6433
   482
qed "set_upt";
nipkow@6433
   483
Addsimps [set_upt];
nipkow@6433
   484
nipkow@5272
   485
Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)";
paulson@5318
   486
by (induct_tac "xs" 1);
paulson@5318
   487
 by (Simp_tac 1);
paulson@5318
   488
by (Asm_simp_tac 1);
paulson@5318
   489
by (rtac iffI 1);
paulson@5318
   490
by (blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1);
paulson@5318
   491
by (REPEAT(etac exE 1));
wenzelm@8442
   492
by (case_tac "ys" 1);
paulson@5316
   493
by Auto_tac;
nipkow@5272
   494
qed "in_set_conv_decomp";
nipkow@5272
   495
nipkow@8009
   496
nipkow@5272
   497
(* eliminate `lists' in favour of `set' *)
nipkow@5272
   498
nipkow@5272
   499
Goal "(xs : lists A) = (!x : set xs. x : A)";
paulson@5318
   500
by (induct_tac "xs" 1);
paulson@5316
   501
by Auto_tac;
nipkow@5272
   502
qed "in_lists_conv_set";
nipkow@5272
   503
nipkow@5272
   504
bind_thm("in_listsD",in_lists_conv_set RS iffD1);
nipkow@5272
   505
AddSDs [in_listsD];
nipkow@5272
   506
bind_thm("in_listsI",in_lists_conv_set RS iffD2);
nipkow@5272
   507
AddSIs [in_listsI];
paulson@1812
   508
oheimb@5518
   509
(** mem **)
oheimb@5518
   510
 
oheimb@5518
   511
section "mem";
oheimb@5518
   512
oheimb@5518
   513
Goal "(x mem xs) = (x: set xs)";
oheimb@5518
   514
by (induct_tac "xs" 1);
oheimb@5518
   515
by Auto_tac;
oheimb@5518
   516
qed "set_mem_eq";
oheimb@5518
   517
oheimb@5518
   518
clasohm@923
   519
(** list_all **)
clasohm@923
   520
nipkow@3467
   521
section "list_all";
nipkow@3467
   522
oheimb@5518
   523
Goal "list_all P xs = (!x:set xs. P x)";
oheimb@5518
   524
by (induct_tac "xs" 1);
oheimb@5518
   525
by Auto_tac;
oheimb@5518
   526
qed "list_all_conv";
oheimb@5518
   527
oheimb@5443
   528
Goal "list_all P (xs@ys) = (list_all P xs & list_all P ys)";
nipkow@3040
   529
by (induct_tac "xs" 1);
paulson@5316
   530
by Auto_tac;
nipkow@2512
   531
qed "list_all_append";
nipkow@2512
   532
Addsimps [list_all_append];
clasohm@923
   533
clasohm@923
   534
nipkow@2608
   535
(** filter **)
clasohm@923
   536
nipkow@3467
   537
section "filter";
nipkow@3467
   538
nipkow@4935
   539
Goal "filter P (xs@ys) = filter P xs @ filter P ys";
paulson@3457
   540
by (induct_tac "xs" 1);
paulson@5316
   541
by Auto_tac;
nipkow@2608
   542
qed "filter_append";
nipkow@2608
   543
Addsimps [filter_append];
nipkow@2608
   544
nipkow@4935
   545
Goal "filter (%x. True) xs = xs";
nipkow@4605
   546
by (induct_tac "xs" 1);
paulson@5316
   547
by Auto_tac;
nipkow@4605
   548
qed "filter_True";
nipkow@4605
   549
Addsimps [filter_True];
nipkow@4605
   550
nipkow@4935
   551
Goal "filter (%x. False) xs = []";
nipkow@4605
   552
by (induct_tac "xs" 1);
paulson@5316
   553
by Auto_tac;
nipkow@4605
   554
qed "filter_False";
nipkow@4605
   555
Addsimps [filter_False];
nipkow@4605
   556
nipkow@4935
   557
Goal "length (filter P xs) <= length xs";
paulson@3457
   558
by (induct_tac "xs" 1);
paulson@5316
   559
by Auto_tac;
paulson@8741
   560
by (asm_simp_tac (simpset() addsimps [le_SucI]) 1);
nipkow@4605
   561
qed "length_filter";
oheimb@5443
   562
Addsimps[length_filter];
nipkow@2608
   563
oheimb@5443
   564
Goal "set (filter P xs) <= set xs";
oheimb@5443
   565
by Auto_tac;
oheimb@5443
   566
qed "filter_is_subset";
oheimb@5443
   567
Addsimps [filter_is_subset];
oheimb@5443
   568
nipkow@2608
   569
nipkow@3467
   570
section "concat";
nipkow@3467
   571
nipkow@4935
   572
Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
nipkow@3040
   573
by (induct_tac "xs" 1);
paulson@5316
   574
by Auto_tac;
nipkow@2608
   575
qed"concat_append";
nipkow@2608
   576
Addsimps [concat_append];
nipkow@2512
   577
nipkow@4935
   578
Goal "(concat xss = []) = (!xs:set xss. xs=[])";
wenzelm@4423
   579
by (induct_tac "xss" 1);
paulson@5316
   580
by Auto_tac;
nipkow@3896
   581
qed "concat_eq_Nil_conv";
nipkow@3896
   582
AddIffs [concat_eq_Nil_conv];
nipkow@3896
   583
nipkow@4935
   584
Goal "([] = concat xss) = (!xs:set xss. xs=[])";
wenzelm@4423
   585
by (induct_tac "xss" 1);
paulson@5316
   586
by Auto_tac;
nipkow@3896
   587
qed "Nil_eq_concat_conv";
nipkow@3896
   588
AddIffs [Nil_eq_concat_conv];
nipkow@3896
   589
nipkow@4935
   590
Goal  "set(concat xs) = Union(set `` set xs)";
nipkow@3467
   591
by (induct_tac "xs" 1);
paulson@5316
   592
by Auto_tac;
paulson@3647
   593
qed"set_concat";
paulson@3647
   594
Addsimps [set_concat];
nipkow@3467
   595
nipkow@4935
   596
Goal "map f (concat xs) = concat (map (map f) xs)"; 
nipkow@3467
   597
by (induct_tac "xs" 1);
paulson@5316
   598
by Auto_tac;
nipkow@3467
   599
qed "map_concat";
nipkow@3467
   600
nipkow@4935
   601
Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
nipkow@3467
   602
by (induct_tac "xs" 1);
paulson@5316
   603
by Auto_tac;
nipkow@3467
   604
qed"filter_concat"; 
nipkow@3467
   605
nipkow@4935
   606
Goal "rev(concat xs) = concat (map rev (rev xs))";
nipkow@3467
   607
by (induct_tac "xs" 1);
paulson@5316
   608
by Auto_tac;
nipkow@2608
   609
qed "rev_concat";
clasohm@923
   610
clasohm@923
   611
(** nth **)
clasohm@923
   612
nipkow@3467
   613
section "nth";
nipkow@3467
   614
pusch@6408
   615
Goal "(x#xs)!0 = x";
pusch@6408
   616
by Auto_tac;
pusch@6408
   617
qed "nth_Cons_0";
pusch@6408
   618
Addsimps [nth_Cons_0];
nipkow@5644
   619
pusch@6408
   620
Goal "(x#xs)!(Suc n) = xs!n";
pusch@6408
   621
by Auto_tac;
pusch@6408
   622
qed "nth_Cons_Suc";
pusch@6408
   623
Addsimps [nth_Cons_Suc];
pusch@6408
   624
pusch@6408
   625
Delsimps (thms "nth.simps");
pusch@6408
   626
pusch@6408
   627
Goal "!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
pusch@6408
   628
by (induct_tac "xs" 1);
paulson@3457
   629
 by (Asm_simp_tac 1);
paulson@3457
   630
 by (rtac allI 1);
wenzelm@8442
   631
 by (case_tac "n" 1);
paulson@5316
   632
  by Auto_tac;
nipkow@2608
   633
qed_spec_mp "nth_append";
nipkow@2608
   634
nipkow@4935
   635
Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
nipkow@3040
   636
by (induct_tac "xs" 1);
nipkow@8118
   637
 by (Asm_full_simp_tac 1);
nipkow@1301
   638
by (rtac allI 1);
berghofe@5183
   639
by (induct_tac "n" 1);
paulson@5316
   640
by Auto_tac;
nipkow@1485
   641
qed_spec_mp "nth_map";
nipkow@1301
   642
Addsimps [nth_map];
nipkow@1301
   643
nipkow@8118
   644
Goal "set xs = {xs!i |i. i < length xs}";
nipkow@3040
   645
by (induct_tac "xs" 1);
nipkow@8118
   646
 by (Simp_tac 1);
paulson@8254
   647
by (Asm_simp_tac 1);
paulson@8254
   648
by Safe_tac;
paulson@8254
   649
  by (res_inst_tac [("x","0")] exI 1);
nipkow@8118
   650
  by (Simp_tac 1);
paulson@8254
   651
 by (res_inst_tac [("x","Suc i")] exI 1);
paulson@8254
   652
 by (Asm_simp_tac 1);
wenzelm@8442
   653
by (case_tac "i" 1);
paulson@8254
   654
 by (Asm_full_simp_tac 1);
paulson@8254
   655
by (rename_tac "j" 1);
paulson@8254
   656
 by (res_inst_tac [("x","j")] exI 1);
paulson@8254
   657
by (Asm_simp_tac 1);
nipkow@8118
   658
qed "set_conv_nth";
nipkow@8118
   659
nipkow@8118
   660
Goal "n < length xs ==> Ball (set xs) P --> P(xs!n)";
nipkow@8118
   661
by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
paulson@8254
   662
by (Blast_tac 1);
oheimb@5518
   663
qed_spec_mp "list_ball_nth";
nipkow@1301
   664
nipkow@8118
   665
Goal "n < length xs ==> xs!n : set xs";
nipkow@8118
   666
by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
paulson@8254
   667
by (Blast_tac 1);
nipkow@1485
   668
qed_spec_mp "nth_mem";
nipkow@1301
   669
Addsimps [nth_mem];
nipkow@1301
   670
nipkow@8009
   671
Goal "(!i. i < length xs --> P(xs!i)) --> (!x : set xs. P x)";
nipkow@8118
   672
by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
paulson@8254
   673
by (Blast_tac 1);
nipkow@8009
   674
qed_spec_mp "all_nth_imp_all_set";
nipkow@8009
   675
nipkow@8009
   676
Goal "(!x : set xs. P x) = (!i. i<length xs --> P (xs ! i))";
nipkow@8118
   677
by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
paulson@8254
   678
by (Blast_tac 1);
nipkow@8009
   679
qed_spec_mp "all_set_conv_all_nth";
nipkow@8009
   680
nipkow@8009
   681
nipkow@5077
   682
(** list update **)
nipkow@5077
   683
nipkow@5077
   684
section "list update";
nipkow@5077
   685
nipkow@5077
   686
Goal "!i. length(xs[i:=x]) = length xs";
nipkow@5077
   687
by (induct_tac "xs" 1);
nipkow@5077
   688
by (Simp_tac 1);
berghofe@5183
   689
by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@5077
   690
qed_spec_mp "length_list_update";
nipkow@5077
   691
Addsimps [length_list_update];
nipkow@5077
   692
nipkow@5644
   693
Goal "!i j. i < length xs  --> (xs[i:=x])!j = (if i=j then x else xs!j)";
paulson@6162
   694
by (induct_tac "xs" 1);
paulson@6162
   695
 by (Simp_tac 1);
paulson@6162
   696
by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
nipkow@5644
   697
qed_spec_mp "nth_list_update";
nipkow@5644
   698
nipkow@8144
   699
Goal "i < length xs  ==> (xs[i:=x])!i = x";
nipkow@8144
   700
by (asm_simp_tac (simpset() addsimps [nth_list_update]) 1);
nipkow@8144
   701
qed "nth_list_update_eq";
nipkow@8144
   702
Addsimps [nth_list_update_eq];
nipkow@8144
   703
nipkow@8144
   704
Goal "!i j. i ~= j --> xs[i:=x]!j = xs!j";
nipkow@8144
   705
by (induct_tac "xs" 1);
nipkow@8144
   706
 by (Simp_tac 1);
nipkow@8144
   707
by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
nipkow@8144
   708
qed_spec_mp "nth_list_update_neq";
nipkow@8144
   709
Addsimps [nth_list_update_neq];
nipkow@8144
   710
nipkow@6433
   711
Goal "!i. i < size xs --> xs[i:=x, i:=y] = xs[i:=y]";
paulson@6813
   712
by (induct_tac "xs" 1);
paulson@6813
   713
 by (Simp_tac 1);
paulson@6813
   714
by (asm_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@6433
   715
qed_spec_mp "list_update_overwrite";
nipkow@6433
   716
Addsimps [list_update_overwrite];
nipkow@6433
   717
nipkow@6433
   718
Goal "!i < length xs. (xs[i := x] = xs) = (xs!i = x)";
paulson@6813
   719
by (induct_tac "xs" 1);
paulson@6813
   720
 by (Simp_tac 1);
paulson@6813
   721
by (simp_tac (simpset() addsplits [nat.split]) 1);
paulson@6813
   722
by (Blast_tac 1);
nipkow@6433
   723
qed_spec_mp "list_update_same_conv";
nipkow@6433
   724
nipkow@8009
   725
Goal "!i xy xs. length xs = length ys --> \
nipkow@8009
   726
\     (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])";
nipkow@8009
   727
by (induct_tac "ys" 1);
nipkow@8009
   728
 by Auto_tac;
wenzelm@8442
   729
by (case_tac "xs" 1);
nipkow@8009
   730
 by (auto_tac (claset(), simpset() addsplits [nat.split]));
nipkow@8009
   731
qed_spec_mp "update_zip";
nipkow@8009
   732
nipkow@8009
   733
Goal "!i. set(xs[i:=x]) <= insert x (set xs)";
nipkow@8009
   734
by (induct_tac "xs" 1);
nipkow@8009
   735
 by (asm_full_simp_tac (simpset() addsimps []) 1);
nipkow@8009
   736
by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@8009
   737
by (Fast_tac  1);
nipkow@8287
   738
qed_spec_mp "set_update_subset_insert";
nipkow@8009
   739
nipkow@8287
   740
Goal "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A";
nipkow@8287
   741
by(fast_tac (claset() addSDs [set_update_subset_insert RS subsetD]) 1);
nipkow@8287
   742
qed "set_update_subsetI";
nipkow@5077
   743
nipkow@3896
   744
(** last & butlast **)
nipkow@1327
   745
nipkow@5644
   746
section "last / butlast";
nipkow@5644
   747
nipkow@4935
   748
Goal "last(xs@[x]) = x";
wenzelm@4423
   749
by (induct_tac "xs" 1);
paulson@5316
   750
by Auto_tac;
nipkow@3896
   751
qed "last_snoc";
nipkow@3896
   752
Addsimps [last_snoc];
nipkow@3896
   753
nipkow@4935
   754
Goal "butlast(xs@[x]) = xs";
wenzelm@4423
   755
by (induct_tac "xs" 1);
paulson@5316
   756
by Auto_tac;
nipkow@3896
   757
qed "butlast_snoc";
nipkow@3896
   758
Addsimps [butlast_snoc];
nipkow@3896
   759
nipkow@4935
   760
Goal "length(butlast xs) = length xs - 1";
nipkow@4935
   761
by (res_inst_tac [("xs","xs")] rev_induct 1);
paulson@5316
   762
by Auto_tac;
nipkow@4643
   763
qed "length_butlast";
nipkow@4643
   764
Addsimps [length_butlast];
nipkow@4643
   765
paulson@5278
   766
Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
wenzelm@4423
   767
by (induct_tac "xs" 1);
paulson@5316
   768
by Auto_tac;
nipkow@3896
   769
qed_spec_mp "butlast_append";
nipkow@3896
   770
nipkow@8118
   771
Goal "xs ~= [] --> butlast xs @ [last xs] = xs";
paulson@8254
   772
by (induct_tac "xs" 1);
paulson@8254
   773
by (ALLGOALS Asm_simp_tac);
nipkow@8118
   774
qed_spec_mp "append_butlast_last_id";
nipkow@8118
   775
Addsimps [append_butlast_last_id];
nipkow@8118
   776
nipkow@4935
   777
Goal "x:set(butlast xs) --> x:set xs";
wenzelm@4423
   778
by (induct_tac "xs" 1);
paulson@5316
   779
by Auto_tac;
nipkow@3896
   780
qed_spec_mp "in_set_butlastD";
nipkow@3896
   781
paulson@5448
   782
Goal "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))";
paulson@5448
   783
by (auto_tac (claset() addDs [in_set_butlastD],
paulson@5448
   784
	      simpset() addsimps [butlast_append]));
paulson@5448
   785
qed "in_set_butlast_appendI";
nipkow@3902
   786
nipkow@2608
   787
(** take  & drop **)
nipkow@2608
   788
section "take & drop";
nipkow@1327
   789
nipkow@4935
   790
Goal "take 0 xs = []";
nipkow@3040
   791
by (induct_tac "xs" 1);
paulson@5316
   792
by Auto_tac;
nipkow@1327
   793
qed "take_0";
nipkow@1327
   794
nipkow@4935
   795
Goal "drop 0 xs = xs";
nipkow@3040
   796
by (induct_tac "xs" 1);
paulson@5316
   797
by Auto_tac;
nipkow@2608
   798
qed "drop_0";
nipkow@2608
   799
nipkow@4935
   800
Goal "take (Suc n) (x#xs) = x # take n xs";
paulson@1552
   801
by (Simp_tac 1);
nipkow@1419
   802
qed "take_Suc_Cons";
nipkow@1327
   803
nipkow@4935
   804
Goal "drop (Suc n) (x#xs) = drop n xs";
nipkow@2608
   805
by (Simp_tac 1);
nipkow@2608
   806
qed "drop_Suc_Cons";
nipkow@2608
   807
nipkow@2608
   808
Delsimps [take_Cons,drop_Cons];
nipkow@2608
   809
Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
nipkow@2608
   810
nipkow@4935
   811
Goal "!xs. length(take n xs) = min (length xs) n";
berghofe@5183
   812
by (induct_tac "n" 1);
paulson@5316
   813
 by Auto_tac;
wenzelm@8442
   814
by (case_tac "xs" 1);
paulson@5316
   815
 by Auto_tac;
nipkow@2608
   816
qed_spec_mp "length_take";
nipkow@2608
   817
Addsimps [length_take];
clasohm@923
   818
nipkow@4935
   819
Goal "!xs. length(drop n xs) = (length xs - n)";
berghofe@5183
   820
by (induct_tac "n" 1);
paulson@5316
   821
 by Auto_tac;
wenzelm@8442
   822
by (case_tac "xs" 1);
paulson@5316
   823
 by Auto_tac;
nipkow@2608
   824
qed_spec_mp "length_drop";
nipkow@2608
   825
Addsimps [length_drop];
nipkow@2608
   826
nipkow@4935
   827
Goal "!xs. length xs <= n --> take n xs = xs";
berghofe@5183
   828
by (induct_tac "n" 1);
paulson@5316
   829
 by Auto_tac;
wenzelm@8442
   830
by (case_tac "xs" 1);
paulson@5316
   831
 by Auto_tac;
nipkow@2608
   832
qed_spec_mp "take_all";
nipkow@7246
   833
Addsimps [take_all];
clasohm@923
   834
nipkow@4935
   835
Goal "!xs. length xs <= n --> drop n xs = []";
berghofe@5183
   836
by (induct_tac "n" 1);
paulson@5316
   837
 by Auto_tac;
wenzelm@8442
   838
by (case_tac "xs" 1);
paulson@5316
   839
 by Auto_tac;
nipkow@2608
   840
qed_spec_mp "drop_all";
nipkow@7246
   841
Addsimps [drop_all];
nipkow@2608
   842
paulson@5278
   843
Goal "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
berghofe@5183
   844
by (induct_tac "n" 1);
paulson@5316
   845
 by Auto_tac;
wenzelm@8442
   846
by (case_tac "xs" 1);
paulson@5316
   847
 by Auto_tac;
nipkow@2608
   848
qed_spec_mp "take_append";
nipkow@2608
   849
Addsimps [take_append];
nipkow@2608
   850
nipkow@4935
   851
Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
berghofe@5183
   852
by (induct_tac "n" 1);
paulson@5316
   853
 by Auto_tac;
wenzelm@8442
   854
by (case_tac "xs" 1);
paulson@5316
   855
 by Auto_tac;
nipkow@2608
   856
qed_spec_mp "drop_append";
nipkow@2608
   857
Addsimps [drop_append];
nipkow@2608
   858
nipkow@4935
   859
Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
berghofe@5183
   860
by (induct_tac "m" 1);
paulson@5316
   861
 by Auto_tac;
wenzelm@8442
   862
by (case_tac "xs" 1);
paulson@5316
   863
 by Auto_tac;
wenzelm@8442
   864
by (case_tac "na" 1);
paulson@5316
   865
 by Auto_tac;
nipkow@2608
   866
qed_spec_mp "take_take";
nipkow@7570
   867
Addsimps [take_take];
nipkow@2608
   868
nipkow@4935
   869
Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
berghofe@5183
   870
by (induct_tac "m" 1);
paulson@5316
   871
 by Auto_tac;
wenzelm@8442
   872
by (case_tac "xs" 1);
paulson@5316
   873
 by Auto_tac;
nipkow@2608
   874
qed_spec_mp "drop_drop";
nipkow@7570
   875
Addsimps [drop_drop];
clasohm@923
   876
nipkow@4935
   877
Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
berghofe@5183
   878
by (induct_tac "m" 1);
paulson@5316
   879
 by Auto_tac;
wenzelm@8442
   880
by (case_tac "xs" 1);
paulson@5316
   881
 by Auto_tac;
nipkow@2608
   882
qed_spec_mp "take_drop";
nipkow@2608
   883
paulson@6813
   884
Goal "!xs. take n xs @ drop n xs = xs";
paulson@6813
   885
by (induct_tac "n" 1);
paulson@6813
   886
 by Auto_tac;
wenzelm@8442
   887
by (case_tac "xs" 1);
paulson@6813
   888
 by Auto_tac;
paulson@6813
   889
qed_spec_mp "append_take_drop_id";
nipkow@8118
   890
Addsimps [append_take_drop_id];
paulson@6813
   891
nipkow@4935
   892
Goal "!xs. take n (map f xs) = map f (take n xs)"; 
berghofe@5183
   893
by (induct_tac "n" 1);
paulson@5316
   894
 by Auto_tac;
wenzelm@8442
   895
by (case_tac "xs" 1);
paulson@5316
   896
 by Auto_tac;
nipkow@2608
   897
qed_spec_mp "take_map"; 
nipkow@2608
   898
nipkow@4935
   899
Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
berghofe@5183
   900
by (induct_tac "n" 1);
paulson@5316
   901
 by Auto_tac;
wenzelm@8442
   902
by (case_tac "xs" 1);
paulson@5316
   903
 by Auto_tac;
nipkow@2608
   904
qed_spec_mp "drop_map";
nipkow@2608
   905
nipkow@4935
   906
Goal "!n i. i < n --> (take n xs)!i = xs!i";
paulson@3457
   907
by (induct_tac "xs" 1);
paulson@5316
   908
 by Auto_tac;
wenzelm@8442
   909
by (case_tac "n" 1);
paulson@3457
   910
 by (Blast_tac 1);
wenzelm@8442
   911
by (case_tac "i" 1);
paulson@5316
   912
 by Auto_tac;
nipkow@2608
   913
qed_spec_mp "nth_take";
nipkow@2608
   914
Addsimps [nth_take];
clasohm@923
   915
nipkow@4935
   916
Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
berghofe@5183
   917
by (induct_tac "n" 1);
paulson@5316
   918
 by Auto_tac;
wenzelm@8442
   919
by (case_tac "xs" 1);
paulson@5316
   920
 by Auto_tac;
nipkow@2608
   921
qed_spec_mp "nth_drop";
nipkow@2608
   922
Addsimps [nth_drop];
nipkow@2608
   923
nipkow@8118
   924
nipkow@8118
   925
Goal
nipkow@8118
   926
 "!zs. (xs@ys = zs) = (xs = take (length xs) zs & ys = drop (length xs) zs)";
paulson@8254
   927
by (induct_tac "xs" 1);
paulson@8254
   928
 by (Simp_tac 1);
paulson@8254
   929
by (Asm_full_simp_tac 1);
paulson@8254
   930
by (Clarify_tac 1);
wenzelm@8442
   931
by (case_tac "zs" 1);
paulson@8254
   932
by (Auto_tac);
nipkow@8118
   933
qed_spec_mp "append_eq_conv_conj";
nipkow@8118
   934
nipkow@2608
   935
(** takeWhile & dropWhile **)
nipkow@2608
   936
nipkow@3467
   937
section "takeWhile & dropWhile";
nipkow@3467
   938
nipkow@4935
   939
Goal "takeWhile P xs @ dropWhile P xs = xs";
nipkow@3586
   940
by (induct_tac "xs" 1);
paulson@5316
   941
by Auto_tac;
nipkow@3586
   942
qed "takeWhile_dropWhile_id";
nipkow@3586
   943
Addsimps [takeWhile_dropWhile_id];
nipkow@3586
   944
nipkow@4935
   945
Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
paulson@3457
   946
by (induct_tac "xs" 1);
paulson@5316
   947
by Auto_tac;
nipkow@2608
   948
bind_thm("takeWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   949
Addsimps [takeWhile_append1];
clasohm@923
   950
nipkow@4935
   951
Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
paulson@3457
   952
by (induct_tac "xs" 1);
paulson@5316
   953
by Auto_tac;
nipkow@2608
   954
bind_thm("takeWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   955
Addsimps [takeWhile_append2];
lcp@1169
   956
nipkow@4935
   957
Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
paulson@3457
   958
by (induct_tac "xs" 1);
paulson@5316
   959
by Auto_tac;
nipkow@2608
   960
bind_thm("dropWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   961
Addsimps [dropWhile_append1];
nipkow@2608
   962
nipkow@4935
   963
Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
paulson@3457
   964
by (induct_tac "xs" 1);
paulson@5316
   965
by Auto_tac;
nipkow@2608
   966
bind_thm("dropWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   967
Addsimps [dropWhile_append2];
nipkow@2608
   968
nipkow@4935
   969
Goal "x:set(takeWhile P xs) --> x:set xs & P x";
paulson@3457
   970
by (induct_tac "xs" 1);
paulson@5316
   971
by Auto_tac;
paulson@3647
   972
qed_spec_mp"set_take_whileD";
nipkow@2608
   973
nipkow@6306
   974
(** zip **)
nipkow@6306
   975
section "zip";
nipkow@6306
   976
nipkow@6306
   977
Goal "zip [] ys = []";
paulson@6813
   978
by (induct_tac "ys" 1);
nipkow@6306
   979
by Auto_tac;
nipkow@6306
   980
qed "zip_Nil";
nipkow@6306
   981
Addsimps [zip_Nil];
nipkow@6306
   982
nipkow@6306
   983
Goal "zip (x#xs) (y#ys) = (x,y)#zip xs ys";
paulson@6813
   984
by (Simp_tac 1);
nipkow@6306
   985
qed "zip_Cons_Cons";
nipkow@6306
   986
Addsimps [zip_Cons_Cons];
nipkow@6306
   987
nipkow@6306
   988
Delsimps(tl (thms"zip.simps"));
nipkow@4605
   989
nipkow@8118
   990
Goal "!xs. length (zip xs ys) = min (length xs) (length ys)";
nipkow@8009
   991
by (induct_tac "ys" 1);
nipkow@8009
   992
 by (Simp_tac 1);
nipkow@8009
   993
by (Clarify_tac 1);
wenzelm@8442
   994
by (case_tac "xs" 1);
paulson@8064
   995
 by (Auto_tac);
nipkow@8009
   996
qed_spec_mp "length_zip";
nipkow@8009
   997
Addsimps [length_zip];
nipkow@8009
   998
nipkow@8009
   999
Goal
nipkow@8118
  1000
 "!xs. zip (xs@ys) zs = \
nipkow@8118
  1001
\      zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)";
paulson@8254
  1002
by (induct_tac "zs" 1);
paulson@8254
  1003
 by (Simp_tac 1);
paulson@8064
  1004
by (Clarify_tac 1);
wenzelm@8442
  1005
by (case_tac "xs" 1);
paulson@8254
  1006
 by (Asm_simp_tac 1);
paulson@8254
  1007
by (Asm_simp_tac 1);
nipkow@8118
  1008
qed_spec_mp "zip_append1";
nipkow@8118
  1009
nipkow@8118
  1010
Goal
nipkow@8118
  1011
 "!ys. zip xs (ys@zs) = \
nipkow@8118
  1012
\      zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs";
paulson@8254
  1013
by (induct_tac "xs" 1);
paulson@8254
  1014
 by (Simp_tac 1);
nipkow@8118
  1015
by (Clarify_tac 1);
wenzelm@8442
  1016
by (case_tac "ys" 1);
paulson@8254
  1017
 by (Asm_simp_tac 1);
paulson@8254
  1018
by (Asm_simp_tac 1);
nipkow@8118
  1019
qed_spec_mp "zip_append2";
nipkow@8118
  1020
nipkow@8118
  1021
Goal
nipkow@8118
  1022
 "[| length xs = length us; length ys = length vs |] ==> \
nipkow@8118
  1023
\ zip (xs@ys) (us@vs) = zip xs us @ zip ys vs";
paulson@8254
  1024
by (asm_simp_tac (simpset() addsimps [zip_append1]) 1);
nipkow@8009
  1025
qed_spec_mp "zip_append";
nipkow@8118
  1026
Addsimps [zip_append];
nipkow@8009
  1027
nipkow@8009
  1028
Goal "!xs. length xs = length ys --> zip (rev xs) (rev ys) = rev (zip xs ys)";
paulson@8064
  1029
by (induct_tac "ys" 1);
paulson@8064
  1030
 by (Asm_full_simp_tac 1);
paulson@8064
  1031
by (Asm_full_simp_tac 1);
paulson@8064
  1032
by (Clarify_tac 1);
wenzelm@8442
  1033
by (case_tac "xs" 1);
paulson@8064
  1034
 by (Auto_tac);
nipkow@8009
  1035
qed_spec_mp "zip_rev";
nipkow@8009
  1036
nipkow@8115
  1037
nipkow@8115
  1038
Goal
nipkow@8009
  1039
"!i xs. i < length xs --> i < length ys --> (zip xs ys)!i = (xs!i, ys!i)";
nipkow@8009
  1040
by (induct_tac "ys" 1);
nipkow@8009
  1041
 by (Simp_tac 1);
nipkow@8009
  1042
by (Clarify_tac 1);
wenzelm@8442
  1043
by (case_tac "xs" 1);
paulson@8064
  1044
 by (Auto_tac);
nipkow@8009
  1045
by (asm_full_simp_tac (simpset() addsimps (thms"nth.simps") addsplits [nat.split]) 1);
nipkow@8009
  1046
qed_spec_mp "nth_zip";
nipkow@8009
  1047
Addsimps [nth_zip];
nipkow@8009
  1048
nipkow@8118
  1049
Goal "set(zip xs ys) = {(xs!i,ys!i) |i. i < min (length xs) (length ys)}";
nipkow@8118
  1050
by (simp_tac (simpset() addsimps [set_conv_nth]addcongs [rev_conj_cong]) 1);
nipkow@8118
  1051
qed_spec_mp "set_zip";
nipkow@8118
  1052
nipkow@8009
  1053
Goal
nipkow@8009
  1054
 "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]";
paulson@8064
  1055
by (rtac sym 1);
paulson@8064
  1056
by (asm_simp_tac (simpset() addsimps [update_zip]) 1);
nipkow@8009
  1057
qed_spec_mp "zip_update";
nipkow@8009
  1058
nipkow@8009
  1059
Goal "!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)";
nipkow@8009
  1060
by (induct_tac "i" 1);
paulson@8064
  1061
 by (Auto_tac);
wenzelm@8442
  1062
by (case_tac "j" 1);
paulson@8064
  1063
 by (Auto_tac);
nipkow@8009
  1064
qed "zip_replicate";
nipkow@8009
  1065
Addsimps [zip_replicate];
nipkow@8009
  1066
nipkow@8115
  1067
(** list_all2 **)
nipkow@8115
  1068
section "list_all2";
nipkow@8115
  1069
nipkow@8115
  1070
Goalw [list_all2_def] "list_all2 P xs ys ==> length xs = length ys";
paulson@8254
  1071
by (Asm_simp_tac 1);
nipkow@8115
  1072
qed "list_all2_lengthD";
nipkow@8115
  1073
nipkow@8115
  1074
Goalw [list_all2_def] "list_all2 P [] ys = (ys=[])";
nipkow@8115
  1075
by (Simp_tac 1);
nipkow@8115
  1076
qed "list_all2_Nil";
nipkow@8115
  1077
AddIffs [list_all2_Nil];
nipkow@8115
  1078
nipkow@8115
  1079
Goalw [list_all2_def] "list_all2 P xs [] = (xs=[])";
nipkow@8115
  1080
by (Simp_tac 1);
nipkow@8115
  1081
qed "list_all2_Nil2";
nipkow@8115
  1082
AddIffs [list_all2_Nil2];
nipkow@8115
  1083
nipkow@8115
  1084
Goalw [list_all2_def]
nipkow@8115
  1085
 "list_all2 P (x#xs) (y#ys) = (P x y & list_all2 P xs ys)";
nipkow@8115
  1086
by (Auto_tac);
nipkow@8115
  1087
qed "list_all2_Cons";
nipkow@8115
  1088
AddIffs[list_all2_Cons];
nipkow@8115
  1089
nipkow@8115
  1090
Goalw [list_all2_def]
nipkow@8118
  1091
 "list_all2 P (x#xs) ys = (? z zs. ys = z#zs & P x z & list_all2 P xs zs)";
wenzelm@8442
  1092
by (case_tac "ys" 1);
paulson@8254
  1093
by (Auto_tac);
nipkow@8118
  1094
qed "list_all2_Cons1";
nipkow@8118
  1095
nipkow@8118
  1096
Goalw [list_all2_def]
nipkow@8118
  1097
 "list_all2 P xs (y#ys) = (? z zs. xs = z#zs & P z y & list_all2 P zs ys)";
wenzelm@8442
  1098
by (case_tac "xs" 1);
paulson@8254
  1099
by (Auto_tac);
nipkow@8118
  1100
qed "list_all2_Cons2";
nipkow@8118
  1101
nipkow@8118
  1102
Goalw [list_all2_def]
nipkow@8118
  1103
 "list_all2 P (xs@ys) zs = \
nipkow@8118
  1104
\ (EX us vs. zs = us@vs & length us = length xs & length vs = length ys & \
nipkow@8118
  1105
\            list_all2 P xs us & list_all2 P ys vs)";
paulson@8254
  1106
by (simp_tac (simpset() addsimps [zip_append1]) 1);
paulson@8254
  1107
by (rtac iffI 1);
paulson@8254
  1108
 by (res_inst_tac [("x","take (length xs) zs")] exI 1);
paulson@8254
  1109
 by (res_inst_tac [("x","drop (length xs) zs")] exI 1);
paulson@8254
  1110
 by (asm_full_simp_tac (simpset() addsimps [min_def,eq_sym_conv]) 1);
nipkow@8118
  1111
by (Clarify_tac 1);
paulson@8254
  1112
by (asm_full_simp_tac (simpset() addsimps [ball_Un]) 1);
nipkow@8118
  1113
qed "list_all2_append1";
nipkow@8118
  1114
nipkow@8118
  1115
Goalw [list_all2_def]
nipkow@8118
  1116
 "list_all2 P xs (ys@zs) = \
nipkow@8118
  1117
\ (EX us vs. xs = us@vs & length us = length ys & length vs = length zs & \
nipkow@8118
  1118
\            list_all2 P us ys & list_all2 P vs zs)";
paulson@8254
  1119
by (simp_tac (simpset() addsimps [zip_append2]) 1);
paulson@8254
  1120
by (rtac iffI 1);
paulson@8254
  1121
 by (res_inst_tac [("x","take (length ys) xs")] exI 1);
paulson@8254
  1122
 by (res_inst_tac [("x","drop (length ys) xs")] exI 1);
paulson@8254
  1123
 by (asm_full_simp_tac (simpset() addsimps [min_def,eq_sym_conv]) 1);
nipkow@8118
  1124
by (Clarify_tac 1);
paulson@8254
  1125
by (asm_full_simp_tac (simpset() addsimps [ball_Un]) 1);
nipkow@8118
  1126
qed "list_all2_append2";
nipkow@8118
  1127
nipkow@8118
  1128
Goalw [list_all2_def]
nipkow@8115
  1129
  "list_all2 P xs ys = \
nipkow@8115
  1130
\  (length xs = length ys & (!i<length xs. P (xs!i) (ys!i)))";
paulson@8254
  1131
by (force_tac (claset(), simpset() addsimps [set_zip]) 1);
nipkow@8115
  1132
qed "list_all2_conv_all_nth";
nipkow@5272
  1133
nipkow@5272
  1134
(** foldl **)
nipkow@5272
  1135
section "foldl";
nipkow@5272
  1136
nipkow@5272
  1137
Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys";
paulson@5318
  1138
by (induct_tac "xs" 1);
paulson@5316
  1139
by Auto_tac;
nipkow@5272
  1140
qed_spec_mp "foldl_append";
nipkow@5272
  1141
Addsimps [foldl_append];
nipkow@5272
  1142
nipkow@5272
  1143
(* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
nipkow@5272
  1144
   because it requires an additional transitivity step
nipkow@5272
  1145
*)
nipkow@5272
  1146
Goal "!n::nat. m <= n --> m <= foldl op+ n ns";
paulson@5318
  1147
by (induct_tac "ns" 1);
nipkow@6058
  1148
by Auto_tac;
nipkow@5272
  1149
qed_spec_mp "start_le_sum";
nipkow@5272
  1150
paulson@8935
  1151
Goal "!!n::nat. n : set ns ==> n <= foldl op+ 0 ns";
oheimb@5758
  1152
by (force_tac (claset() addIs [start_le_sum],
oheimb@5758
  1153
              simpset() addsimps [in_set_conv_decomp]) 1);
nipkow@5272
  1154
qed "elem_le_sum";
nipkow@5272
  1155
paulson@8935
  1156
Goal "!m::nat. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))";
paulson@5318
  1157
by (induct_tac "ns" 1);
paulson@5316
  1158
by Auto_tac;
nipkow@5272
  1159
qed_spec_mp "sum_eq_0_conv";
nipkow@5272
  1160
AddIffs [sum_eq_0_conv];
nipkow@5272
  1161
nipkow@5425
  1162
(** upto **)
nipkow@5425
  1163
nipkow@5427
  1164
(* Does not terminate! *)
nipkow@5427
  1165
Goal "[i..j(] = (if i<j then i#[Suc i..j(] else [])";
paulson@6162
  1166
by (induct_tac "j" 1);
nipkow@5427
  1167
by Auto_tac;
nipkow@5427
  1168
qed "upt_rec";
nipkow@5425
  1169
nipkow@5427
  1170
Goal "j<=i ==> [i..j(] = []";
paulson@6162
  1171
by (stac upt_rec 1);
paulson@6162
  1172
by (Asm_simp_tac 1);
nipkow@5427
  1173
qed "upt_conv_Nil";
nipkow@5427
  1174
Addsimps [upt_conv_Nil];
nipkow@5427
  1175
paulson@8982
  1176
(*Only needed if upt_Suc is deleted from the simpset*)
nipkow@5427
  1177
Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]";
nipkow@5427
  1178
by (Asm_simp_tac 1);
paulson@8982
  1179
qed "upt_Suc_append";
nipkow@5427
  1180
nipkow@5427
  1181
Goal "i<j ==> [i..j(] = i#[Suc i..j(]";
paulson@6162
  1182
by (rtac trans 1);
paulson@6162
  1183
by (stac upt_rec 1);
paulson@6162
  1184
by (rtac refl 2);
nipkow@5427
  1185
by (Asm_simp_tac 1);
nipkow@5427
  1186
qed "upt_conv_Cons";
nipkow@5427
  1187
paulson@9003
  1188
(*LOOPS as a simprule, since j<=j*)
paulson@9003
  1189
Goal "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]";
paulson@9003
  1190
by (induct_tac "k" 1);
paulson@9003
  1191
by Auto_tac;
paulson@9003
  1192
qed "upt_add_eq_append";
paulson@9003
  1193
nipkow@5427
  1194
Goal "length [i..j(] = j-i";
paulson@6162
  1195
by (induct_tac "j" 1);
nipkow@5427
  1196
 by (Simp_tac 1);
paulson@6162
  1197
by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
nipkow@5427
  1198
qed "length_upt";
nipkow@5427
  1199
Addsimps [length_upt];
nipkow@5425
  1200
nipkow@5427
  1201
Goal "i+k < j --> [i..j(] ! k = i+k";
paulson@6162
  1202
by (induct_tac "j" 1);
paulson@9014
  1203
 by (asm_simp_tac (simpset() addsimps [less_Suc_eq, nth_append] 
paulson@9014
  1204
                             addsplits [nat_diff_split]) 2);
paulson@9014
  1205
by (Simp_tac 1);
nipkow@5427
  1206
qed_spec_mp "nth_upt";
nipkow@5427
  1207
Addsimps [nth_upt];
nipkow@5425
  1208
nipkow@6433
  1209
Goal "!i. i+m <= n --> take m [i..n(] = [i..i+m(]";
paulson@6813
  1210
by (induct_tac "m" 1);
paulson@6813
  1211
 by (Simp_tac 1);
paulson@6813
  1212
by (Clarify_tac 1);
paulson@6813
  1213
by (stac upt_rec 1);
paulson@6813
  1214
by (rtac sym 1);
paulson@6813
  1215
by (stac upt_rec 1);
paulson@6813
  1216
by (asm_simp_tac (simpset() delsimps (thms"upt.simps")) 1);
nipkow@6433
  1217
qed_spec_mp "take_upt";
nipkow@6433
  1218
Addsimps [take_upt];
nipkow@6433
  1219
paulson@9003
  1220
Goal "map Suc [m..n(] = [Suc m..n]";
paulson@6813
  1221
by (induct_tac "n" 1);
paulson@9003
  1222
by Auto_tac;
paulson@9003
  1223
qed "map_Suc_upt";
paulson@9003
  1224
paulson@9003
  1225
Goal "ALL i. i < n-m --> (map f [m..n(]) ! i = f(m+i)";
paulson@9003
  1226
by (res_inst_tac [("m","n"),("n","m")] diff_induct 1);
paulson@9003
  1227
by (stac (map_Suc_upt RS sym) 3);
paulson@9003
  1228
by (auto_tac (claset(), simpset() addsimps [less_diff_conv, nth_upt]));
nipkow@6433
  1229
qed_spec_mp "nth_map_upt";
nipkow@6433
  1230
paulson@6813
  1231
Goal "ALL xs ys. k <= length xs --> k <= length ys -->  \
paulson@6813
  1232
\        (ALL i. i < k --> xs!i = ys!i)  \
paulson@6813
  1233
\     --> take k xs = take k ys";
paulson@6813
  1234
by (induct_tac "k" 1);
paulson@6813
  1235
by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq_0_disj, 
paulson@6813
  1236
						all_conj_distrib])));
paulson@6813
  1237
by (Clarify_tac 1);
paulson@6813
  1238
(*Both lists must be non-empty*)
wenzelm@8442
  1239
by (case_tac "xs" 1);
wenzelm@8442
  1240
by (case_tac "ys" 2);
paulson@6813
  1241
by (ALLGOALS Clarify_tac);
paulson@6813
  1242
(*prenexing's needed, not miniscoping*)
paulson@6813
  1243
by (ALLGOALS (full_simp_tac (simpset() addsimps (all_simps RL [sym])  
paulson@6813
  1244
                                       delsimps (all_simps))));
paulson@6813
  1245
by (Blast_tac 1);
paulson@6813
  1246
qed_spec_mp "nth_take_lemma";
paulson@6813
  1247
paulson@6813
  1248
Goal "[| length xs = length ys;  \
paulson@6813
  1249
\        ALL i. i < length xs --> xs!i = ys!i |]  \
paulson@6813
  1250
\     ==> xs = ys";
paulson@6813
  1251
by (forward_tac [[le_refl, eq_imp_le] MRS nth_take_lemma] 1);
paulson@6813
  1252
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [take_all])));
paulson@6813
  1253
qed_spec_mp "nth_equalityI";
paulson@6813
  1254
paulson@6813
  1255
(*The famous take-lemma*)
paulson@6813
  1256
Goal "(ALL i. take i xs = take i ys) ==> xs = ys";
paulson@6813
  1257
by (dres_inst_tac [("x", "max (length xs) (length ys)")] spec 1);
paulson@6813
  1258
by (full_simp_tac (simpset() addsimps [le_max_iff_disj, take_all]) 1);
paulson@6813
  1259
qed_spec_mp "take_equalityI";
paulson@6813
  1260
nipkow@5272
  1261
nipkow@4605
  1262
(** nodups & remdups **)
nipkow@4605
  1263
section "nodups & remdups";
nipkow@4605
  1264
nipkow@4935
  1265
Goal "set(remdups xs) = set xs";
nipkow@4605
  1266
by (induct_tac "xs" 1);
nipkow@4605
  1267
 by (Simp_tac 1);
nipkow@4686
  1268
by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
nipkow@4605
  1269
qed "set_remdups";
nipkow@4605
  1270
Addsimps [set_remdups];
nipkow@4605
  1271
nipkow@4935
  1272
Goal "nodups(remdups xs)";
nipkow@4605
  1273
by (induct_tac "xs" 1);
paulson@5316
  1274
by Auto_tac;
nipkow@4605
  1275
qed "nodups_remdups";
nipkow@4605
  1276
nipkow@4935
  1277
Goal "nodups xs --> nodups (filter P xs)";
nipkow@4605
  1278
by (induct_tac "xs" 1);
paulson@5316
  1279
by Auto_tac;
nipkow@4605
  1280
qed_spec_mp "nodups_filter";
nipkow@4605
  1281
nipkow@3589
  1282
(** replicate **)
nipkow@3589
  1283
section "replicate";
nipkow@3589
  1284
nipkow@6794
  1285
Goal "length(replicate n x) = n";
paulson@6813
  1286
by (induct_tac "n" 1);
paulson@6813
  1287
by Auto_tac;
nipkow@6794
  1288
qed "length_replicate";
nipkow@6794
  1289
Addsimps [length_replicate];
nipkow@6794
  1290
nipkow@6794
  1291
Goal "map f (replicate n x) = replicate n (f x)";
nipkow@6794
  1292
by (induct_tac "n" 1);
paulson@6813
  1293
by Auto_tac;
nipkow@6794
  1294
qed "map_replicate";
nipkow@6794
  1295
Addsimps [map_replicate];
nipkow@6794
  1296
nipkow@6794
  1297
Goal "(replicate n x) @ (x#xs) = x # replicate n x @ xs";
nipkow@6794
  1298
by (induct_tac "n" 1);
paulson@6813
  1299
by Auto_tac;
nipkow@6794
  1300
qed "replicate_app_Cons_same";
nipkow@6794
  1301
nipkow@6794
  1302
Goal "rev(replicate n x) = replicate n x";
nipkow@6794
  1303
by (induct_tac "n" 1);
paulson@6813
  1304
 by (Simp_tac 1);
nipkow@6794
  1305
by (asm_simp_tac (simpset() addsimps [replicate_app_Cons_same]) 1);
nipkow@6794
  1306
qed "rev_replicate";
nipkow@6794
  1307
Addsimps [rev_replicate];
nipkow@6794
  1308
nipkow@8009
  1309
Goal "replicate (n+m) x = replicate n x @ replicate m x";
nipkow@8009
  1310
by (induct_tac "n" 1);
nipkow@8009
  1311
by Auto_tac;
nipkow@8009
  1312
qed "replicate_add";
nipkow@8009
  1313
nipkow@6794
  1314
Goal"n ~= 0 --> hd(replicate n x) = x";
nipkow@6794
  1315
by (induct_tac "n" 1);
paulson@6813
  1316
by Auto_tac;
nipkow@6794
  1317
qed_spec_mp "hd_replicate";
nipkow@6794
  1318
Addsimps [hd_replicate];
nipkow@6794
  1319
nipkow@6794
  1320
Goal "n ~= 0 --> tl(replicate n x) = replicate (n-1) x";
nipkow@6794
  1321
by (induct_tac "n" 1);
paulson@6813
  1322
by Auto_tac;
nipkow@6794
  1323
qed_spec_mp "tl_replicate";
nipkow@6794
  1324
Addsimps [tl_replicate];
nipkow@6794
  1325
nipkow@6794
  1326
Goal "n ~= 0 --> last(replicate n x) = x";
nipkow@6794
  1327
by (induct_tac "n" 1);
paulson@6813
  1328
by Auto_tac;
nipkow@6794
  1329
qed_spec_mp "last_replicate";
nipkow@6794
  1330
Addsimps [last_replicate];
nipkow@6794
  1331
nipkow@6794
  1332
Goal "!i. i<n --> (replicate n x)!i = x";
paulson@6813
  1333
by (induct_tac "n" 1);
paulson@6813
  1334
 by (Simp_tac 1);
paulson@6813
  1335
by (asm_simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
nipkow@6794
  1336
qed_spec_mp "nth_replicate";
nipkow@6794
  1337
Addsimps [nth_replicate];
nipkow@6794
  1338
nipkow@4935
  1339
Goal "set(replicate (Suc n) x) = {x}";
wenzelm@4423
  1340
by (induct_tac "n" 1);
paulson@5316
  1341
by Auto_tac;
nipkow@3589
  1342
val lemma = result();
nipkow@3589
  1343
nipkow@5043
  1344
Goal "n ~= 0 ==> set(replicate n x) = {x}";
wenzelm@4423
  1345
by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
nipkow@3589
  1346
qed "set_replicate";
nipkow@3589
  1347
Addsimps [set_replicate];
nipkow@5162
  1348
nipkow@8009
  1349
Goal "set(replicate n x) = (if n=0 then {} else {x})";
paulson@8064
  1350
by (Auto_tac);
nipkow@8009
  1351
qed "set_replicate_conv_if";
nipkow@8009
  1352
nipkow@8009
  1353
Goal "x : set(replicate n y) --> x=y";
paulson@8064
  1354
by (asm_simp_tac (simpset() addsimps [set_replicate_conv_if]) 1);
nipkow@8009
  1355
qed_spec_mp "in_set_replicateD";
nipkow@8009
  1356
nipkow@5162
  1357
nipkow@5281
  1358
(*** Lexcicographic orderings on lists ***)
nipkow@5281
  1359
section"Lexcicographic orderings on lists";
nipkow@5281
  1360
nipkow@5281
  1361
Goal "wf r ==> wf(lexn r n)";
paulson@5318
  1362
by (induct_tac "n" 1);
paulson@5318
  1363
by (Simp_tac 1);
paulson@5318
  1364
by (Simp_tac 1);
paulson@5318
  1365
by (rtac wf_subset 1);
paulson@5318
  1366
by (rtac Int_lower1 2);
paulson@5318
  1367
by (rtac wf_prod_fun_image 1);
paulson@5318
  1368
by (rtac injI 2);
paulson@6813
  1369
by Auto_tac;
nipkow@5281
  1370
qed "wf_lexn";
nipkow@5281
  1371
nipkow@5281
  1372
Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n";
paulson@5318
  1373
by (induct_tac "n" 1);
paulson@6813
  1374
by Auto_tac;
nipkow@5281
  1375
qed_spec_mp "lexn_length";
nipkow@5281
  1376
nipkow@5281
  1377
Goalw [lex_def] "wf r ==> wf(lex r)";
paulson@5318
  1378
by (rtac wf_UN 1);
paulson@5318
  1379
by (blast_tac (claset() addIs [wf_lexn]) 1);
paulson@5318
  1380
by (Clarify_tac 1);
paulson@5318
  1381
by (rename_tac "m n" 1);
paulson@5318
  1382
by (subgoal_tac "m ~= n" 1);
paulson@5318
  1383
 by (Blast_tac 2);
paulson@5318
  1384
by (blast_tac (claset() addDs [lexn_length,not_sym]) 1);
nipkow@5281
  1385
qed "wf_lex";
nipkow@5281
  1386
AddSIs [wf_lex];
nipkow@5281
  1387
nipkow@5281
  1388
Goal
nipkow@5281
  1389
 "lexn r n = \
nipkow@5281
  1390
\ {(xs,ys). length xs = n & length ys = n & \
nipkow@5281
  1391
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5318
  1392
by (induct_tac "n" 1);
paulson@5318
  1393
 by (Simp_tac 1);
paulson@5318
  1394
 by (Blast_tac 1);
paulson@5641
  1395
by (asm_full_simp_tac (simpset() 
oheimb@5296
  1396
				addsimps [lex_prod_def]) 1);
paulson@5641
  1397
by (auto_tac (claset(), simpset()));
paulson@5318
  1398
  by (Blast_tac 1);
paulson@5318
  1399
 by (rename_tac "a xys x xs' y ys'" 1);
paulson@5318
  1400
 by (res_inst_tac [("x","a#xys")] exI 1);
paulson@5318
  1401
 by (Simp_tac 1);
wenzelm@8442
  1402
by (case_tac "xys" 1);
paulson@5641
  1403
 by (ALLGOALS (asm_full_simp_tac (simpset())));
paulson@5318
  1404
by (Blast_tac 1);
nipkow@5281
  1405
qed "lexn_conv";
nipkow@5281
  1406
nipkow@5281
  1407
Goalw [lex_def]
nipkow@5281
  1408
 "lex r = \
nipkow@5281
  1409
\ {(xs,ys). length xs = length ys & \
nipkow@5281
  1410
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5641
  1411
by (force_tac (claset(), simpset() addsimps [lexn_conv]) 1);
nipkow@5281
  1412
qed "lex_conv";
nipkow@5281
  1413
nipkow@5281
  1414
Goalw [lexico_def] "wf r ==> wf(lexico r)";
paulson@5318
  1415
by (Blast_tac 1);
nipkow@5281
  1416
qed "wf_lexico";
nipkow@5281
  1417
AddSIs [wf_lexico];
nipkow@5281
  1418
nipkow@5281
  1419
Goalw
nipkow@5281
  1420
 [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def]
nipkow@5281
  1421
"lexico r = {(xs,ys). length xs < length ys | \
nipkow@5281
  1422
\                     length xs = length ys & (xs,ys) : lex r}";
paulson@5318
  1423
by (Simp_tac 1);
nipkow@5281
  1424
qed "lexico_conv";
nipkow@5281
  1425
nipkow@5283
  1426
Goal "([],ys) ~: lex r";
paulson@5318
  1427
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1428
qed "Nil_notin_lex";
nipkow@5283
  1429
nipkow@5283
  1430
Goal "(xs,[]) ~: lex r";
paulson@5318
  1431
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1432
qed "Nil2_notin_lex";
nipkow@5283
  1433
nipkow@5283
  1434
AddIffs [Nil_notin_lex,Nil2_notin_lex];
nipkow@5283
  1435
nipkow@5283
  1436
Goal "((x#xs,y#ys) : lex r) = \
nipkow@5283
  1437
\     ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)";
paulson@5318
  1438
by (simp_tac (simpset() addsimps [lex_conv]) 1);
paulson@5318
  1439
by (rtac iffI 1);
paulson@5318
  1440
 by (blast_tac (claset() addIs [Cons_eq_appendI]) 2);
paulson@5318
  1441
by (REPEAT(eresolve_tac [conjE, exE] 1));
wenzelm@8442
  1442
by (case_tac "xys" 1);
paulson@5318
  1443
by (Asm_full_simp_tac 1);
paulson@5318
  1444
by (Asm_full_simp_tac 1);
paulson@5318
  1445
by (Blast_tac 1);
nipkow@5283
  1446
qed "Cons_in_lex";
nipkow@5283
  1447
AddIffs [Cons_in_lex];
paulson@7032
  1448
paulson@7032
  1449
paulson@7032
  1450
(*** Versions of some theorems above using binary numerals ***)
paulson@7032
  1451
paulson@7032
  1452
AddIffs (map (rename_numerals thy) 
paulson@7032
  1453
	  [length_0_conv, zero_length_conv, length_greater_0_conv,
paulson@7032
  1454
	   sum_eq_0_conv]);
paulson@7032
  1455
paulson@7032
  1456
Goal "take n (x#xs) = (if n = #0 then [] else x # take (n-#1) xs)";
wenzelm@8442
  1457
by (case_tac "n" 1);
paulson@7032
  1458
by (ALLGOALS 
paulson@7032
  1459
    (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
paulson@7032
  1460
qed "take_Cons'";
paulson@7032
  1461
paulson@7032
  1462
Goal "drop n (x#xs) = (if n = #0 then x#xs else drop (n-#1) xs)";
wenzelm@8442
  1463
by (case_tac "n" 1);
paulson@7032
  1464
by (ALLGOALS
paulson@7032
  1465
    (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
paulson@7032
  1466
qed "drop_Cons'";
paulson@7032
  1467
paulson@7032
  1468
Goal "(x#xs)!n = (if n = #0 then x else xs!(n-#1))";
wenzelm@8442
  1469
by (case_tac "n" 1);
paulson@7032
  1470
by (ALLGOALS
paulson@7032
  1471
    (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
paulson@7032
  1472
qed "nth_Cons'";
paulson@7032
  1473
paulson@7032
  1474
Addsimps (map (inst "n" "number_of ?v") [take_Cons', drop_Cons', nth_Cons']);
paulson@7032
  1475