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