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