src/HOL/List.ML
author wenzelm
Thu Dec 13 15:45:03 2001 +0100 (2001-12-13)
changeset 12486 0ed8bdd883e0
parent 11868 56db9f3a6b3e
child 12515 3fb416265ba9
permissions -rw-r--r--
isatool expandshort;
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@4935
   532
Goal "filter (%x. True) xs = xs";
nipkow@4605
   533
by (induct_tac "xs" 1);
paulson@5316
   534
by Auto_tac;
nipkow@4605
   535
qed "filter_True";
nipkow@4605
   536
Addsimps [filter_True];
nipkow@4605
   537
nipkow@4935
   538
Goal "filter (%x. False) xs = []";
nipkow@4605
   539
by (induct_tac "xs" 1);
paulson@5316
   540
by Auto_tac;
nipkow@4605
   541
qed "filter_False";
nipkow@4605
   542
Addsimps [filter_False];
nipkow@4605
   543
nipkow@4935
   544
Goal "length (filter P xs) <= length xs";
paulson@3457
   545
by (induct_tac "xs" 1);
paulson@5316
   546
by Auto_tac;
paulson@8741
   547
by (asm_simp_tac (simpset() addsimps [le_SucI]) 1);
nipkow@4605
   548
qed "length_filter";
oheimb@5443
   549
Addsimps[length_filter];
nipkow@2608
   550
oheimb@5443
   551
Goal "set (filter P xs) <= set xs";
oheimb@5443
   552
by Auto_tac;
oheimb@5443
   553
qed "filter_is_subset";
oheimb@5443
   554
Addsimps [filter_is_subset];
oheimb@5443
   555
nipkow@2608
   556
nipkow@3467
   557
section "concat";
nipkow@3467
   558
nipkow@4935
   559
Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
nipkow@3040
   560
by (induct_tac "xs" 1);
paulson@5316
   561
by Auto_tac;
nipkow@2608
   562
qed"concat_append";
nipkow@2608
   563
Addsimps [concat_append];
nipkow@2512
   564
nipkow@4935
   565
Goal "(concat xss = []) = (!xs:set xss. xs=[])";
wenzelm@4423
   566
by (induct_tac "xss" 1);
paulson@5316
   567
by Auto_tac;
nipkow@3896
   568
qed "concat_eq_Nil_conv";
nipkow@3896
   569
AddIffs [concat_eq_Nil_conv];
nipkow@3896
   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 "Nil_eq_concat_conv";
nipkow@3896
   575
AddIffs [Nil_eq_concat_conv];
nipkow@3896
   576
nipkow@10832
   577
Goal  "set(concat xs) = Union(set ` set xs)";
nipkow@3467
   578
by (induct_tac "xs" 1);
paulson@5316
   579
by Auto_tac;
paulson@3647
   580
qed"set_concat";
paulson@3647
   581
Addsimps [set_concat];
nipkow@3467
   582
nipkow@4935
   583
Goal "map f (concat xs) = concat (map (map f) xs)"; 
nipkow@3467
   584
by (induct_tac "xs" 1);
paulson@5316
   585
by Auto_tac;
nipkow@3467
   586
qed "map_concat";
nipkow@3467
   587
nipkow@4935
   588
Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
nipkow@3467
   589
by (induct_tac "xs" 1);
paulson@5316
   590
by Auto_tac;
nipkow@3467
   591
qed"filter_concat"; 
nipkow@3467
   592
nipkow@4935
   593
Goal "rev(concat xs) = concat (map rev (rev xs))";
nipkow@3467
   594
by (induct_tac "xs" 1);
paulson@5316
   595
by Auto_tac;
nipkow@2608
   596
qed "rev_concat";
clasohm@923
   597
clasohm@923
   598
(** nth **)
clasohm@923
   599
nipkow@3467
   600
section "nth";
nipkow@3467
   601
pusch@6408
   602
Goal "(x#xs)!0 = x";
pusch@6408
   603
by Auto_tac;
pusch@6408
   604
qed "nth_Cons_0";
pusch@6408
   605
Addsimps [nth_Cons_0];
nipkow@5644
   606
pusch@6408
   607
Goal "(x#xs)!(Suc n) = xs!n";
pusch@6408
   608
by Auto_tac;
pusch@6408
   609
qed "nth_Cons_Suc";
pusch@6408
   610
Addsimps [nth_Cons_Suc];
pusch@6408
   611
pusch@6408
   612
Delsimps (thms "nth.simps");
pusch@6408
   613
pusch@6408
   614
Goal "!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
pusch@6408
   615
by (induct_tac "xs" 1);
paulson@3457
   616
 by (Asm_simp_tac 1);
paulson@3457
   617
 by (rtac allI 1);
wenzelm@8442
   618
 by (case_tac "n" 1);
paulson@5316
   619
  by Auto_tac;
nipkow@2608
   620
qed_spec_mp "nth_append";
nipkow@2608
   621
nipkow@4935
   622
Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
nipkow@3040
   623
by (induct_tac "xs" 1);
nipkow@8118
   624
 by (Asm_full_simp_tac 1);
nipkow@1301
   625
by (rtac allI 1);
berghofe@5183
   626
by (induct_tac "n" 1);
paulson@5316
   627
by Auto_tac;
nipkow@1485
   628
qed_spec_mp "nth_map";
nipkow@1301
   629
Addsimps [nth_map];
nipkow@1301
   630
nipkow@8118
   631
Goal "set xs = {xs!i |i. i < length xs}";
nipkow@3040
   632
by (induct_tac "xs" 1);
nipkow@8118
   633
 by (Simp_tac 1);
paulson@8254
   634
by (Asm_simp_tac 1);
paulson@8254
   635
by Safe_tac;
paulson@8254
   636
  by (res_inst_tac [("x","0")] exI 1);
nipkow@8118
   637
  by (Simp_tac 1);
paulson@8254
   638
 by (res_inst_tac [("x","Suc i")] exI 1);
paulson@8254
   639
 by (Asm_simp_tac 1);
wenzelm@8442
   640
by (case_tac "i" 1);
paulson@8254
   641
 by (Asm_full_simp_tac 1);
paulson@8254
   642
by (rename_tac "j" 1);
paulson@8254
   643
 by (res_inst_tac [("x","j")] exI 1);
paulson@8254
   644
by (Asm_simp_tac 1);
nipkow@8118
   645
qed "set_conv_nth";
nipkow@8118
   646
nipkow@8118
   647
Goal "n < length xs ==> Ball (set xs) P --> P(xs!n)";
nipkow@8118
   648
by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
paulson@8254
   649
by (Blast_tac 1);
oheimb@5518
   650
qed_spec_mp "list_ball_nth";
nipkow@1301
   651
nipkow@8118
   652
Goal "n < length xs ==> xs!n : set xs";
nipkow@8118
   653
by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
paulson@8254
   654
by (Blast_tac 1);
nipkow@1485
   655
qed_spec_mp "nth_mem";
nipkow@1301
   656
Addsimps [nth_mem];
nipkow@1301
   657
nipkow@8009
   658
Goal "(!i. i < length xs --> P(xs!i)) --> (!x : set xs. P x)";
nipkow@8118
   659
by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
paulson@8254
   660
by (Blast_tac 1);
nipkow@8009
   661
qed_spec_mp "all_nth_imp_all_set";
nipkow@8009
   662
nipkow@8009
   663
Goal "(!x : set xs. P x) = (!i. i<length xs --> P (xs ! i))";
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_set_conv_all_nth";
nipkow@8009
   667
nipkow@8009
   668
nipkow@5077
   669
(** list update **)
nipkow@5077
   670
nipkow@5077
   671
section "list update";
nipkow@5077
   672
nipkow@5077
   673
Goal "!i. length(xs[i:=x]) = length xs";
nipkow@5077
   674
by (induct_tac "xs" 1);
nipkow@5077
   675
by (Simp_tac 1);
berghofe@5183
   676
by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@5077
   677
qed_spec_mp "length_list_update";
nipkow@5077
   678
Addsimps [length_list_update];
nipkow@5077
   679
nipkow@5644
   680
Goal "!i j. i < length xs  --> (xs[i:=x])!j = (if i=j then x else xs!j)";
paulson@6162
   681
by (induct_tac "xs" 1);
paulson@6162
   682
 by (Simp_tac 1);
paulson@6162
   683
by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
nipkow@5644
   684
qed_spec_mp "nth_list_update";
nipkow@5644
   685
nipkow@8144
   686
Goal "i < length xs  ==> (xs[i:=x])!i = x";
nipkow@8144
   687
by (asm_simp_tac (simpset() addsimps [nth_list_update]) 1);
nipkow@8144
   688
qed "nth_list_update_eq";
nipkow@8144
   689
Addsimps [nth_list_update_eq];
nipkow@8144
   690
nipkow@8144
   691
Goal "!i j. i ~= j --> xs[i:=x]!j = xs!j";
nipkow@8144
   692
by (induct_tac "xs" 1);
nipkow@8144
   693
 by (Simp_tac 1);
nipkow@8144
   694
by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
nipkow@8144
   695
qed_spec_mp "nth_list_update_neq";
nipkow@8144
   696
Addsimps [nth_list_update_neq];
nipkow@8144
   697
nipkow@6433
   698
Goal "!i. i < size xs --> xs[i:=x, i:=y] = xs[i:=y]";
paulson@6813
   699
by (induct_tac "xs" 1);
paulson@6813
   700
 by (Simp_tac 1);
paulson@6813
   701
by (asm_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@6433
   702
qed_spec_mp "list_update_overwrite";
nipkow@6433
   703
Addsimps [list_update_overwrite];
nipkow@6433
   704
nipkow@6433
   705
Goal "!i < length xs. (xs[i := x] = xs) = (xs!i = x)";
paulson@6813
   706
by (induct_tac "xs" 1);
paulson@6813
   707
 by (Simp_tac 1);
paulson@6813
   708
by (simp_tac (simpset() addsplits [nat.split]) 1);
paulson@6813
   709
by (Blast_tac 1);
nipkow@6433
   710
qed_spec_mp "list_update_same_conv";
nipkow@6433
   711
nipkow@8009
   712
Goal "!i xy xs. length xs = length ys --> \
nipkow@8009
   713
\     (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])";
nipkow@8009
   714
by (induct_tac "ys" 1);
nipkow@8009
   715
 by Auto_tac;
wenzelm@8442
   716
by (case_tac "xs" 1);
nipkow@8009
   717
 by (auto_tac (claset(), simpset() addsplits [nat.split]));
nipkow@8009
   718
qed_spec_mp "update_zip";
nipkow@8009
   719
nipkow@8009
   720
Goal "!i. set(xs[i:=x]) <= insert x (set xs)";
nipkow@8009
   721
by (induct_tac "xs" 1);
nipkow@8009
   722
 by (asm_full_simp_tac (simpset() addsimps []) 1);
nipkow@8009
   723
by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
nipkow@8009
   724
by (Fast_tac  1);
nipkow@8287
   725
qed_spec_mp "set_update_subset_insert";
nipkow@8009
   726
nipkow@8287
   727
Goal "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A";
wenzelm@12486
   728
by (fast_tac (claset() addSDs [set_update_subset_insert RS subsetD]) 1);
nipkow@8287
   729
qed "set_update_subsetI";
nipkow@5077
   730
nipkow@3896
   731
(** last & butlast **)
nipkow@1327
   732
nipkow@5644
   733
section "last / butlast";
nipkow@5644
   734
nipkow@4935
   735
Goal "last(xs@[x]) = x";
wenzelm@4423
   736
by (induct_tac "xs" 1);
paulson@5316
   737
by Auto_tac;
nipkow@3896
   738
qed "last_snoc";
nipkow@3896
   739
Addsimps [last_snoc];
nipkow@3896
   740
nipkow@4935
   741
Goal "butlast(xs@[x]) = xs";
wenzelm@4423
   742
by (induct_tac "xs" 1);
paulson@5316
   743
by Auto_tac;
nipkow@3896
   744
qed "butlast_snoc";
nipkow@3896
   745
Addsimps [butlast_snoc];
nipkow@3896
   746
nipkow@4935
   747
Goal "length(butlast xs) = length xs - 1";
nipkow@9747
   748
by (rev_induct_tac "xs" 1);
paulson@5316
   749
by Auto_tac;
nipkow@4643
   750
qed "length_butlast";
nipkow@4643
   751
Addsimps [length_butlast];
nipkow@4643
   752
paulson@5278
   753
Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
wenzelm@4423
   754
by (induct_tac "xs" 1);
paulson@5316
   755
by Auto_tac;
nipkow@3896
   756
qed_spec_mp "butlast_append";
nipkow@3896
   757
nipkow@8118
   758
Goal "xs ~= [] --> butlast xs @ [last xs] = xs";
paulson@8254
   759
by (induct_tac "xs" 1);
paulson@8254
   760
by (ALLGOALS Asm_simp_tac);
nipkow@8118
   761
qed_spec_mp "append_butlast_last_id";
nipkow@8118
   762
Addsimps [append_butlast_last_id];
nipkow@8118
   763
nipkow@4935
   764
Goal "x:set(butlast xs) --> x:set xs";
wenzelm@4423
   765
by (induct_tac "xs" 1);
paulson@5316
   766
by Auto_tac;
nipkow@3896
   767
qed_spec_mp "in_set_butlastD";
nipkow@3896
   768
paulson@5448
   769
Goal "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))";
paulson@5448
   770
by (auto_tac (claset() addDs [in_set_butlastD],
paulson@5448
   771
	      simpset() addsimps [butlast_append]));
paulson@5448
   772
qed "in_set_butlast_appendI";
nipkow@3902
   773
nipkow@2608
   774
(** take  & drop **)
nipkow@2608
   775
section "take & drop";
nipkow@1327
   776
nipkow@4935
   777
Goal "take 0 xs = []";
nipkow@3040
   778
by (induct_tac "xs" 1);
paulson@5316
   779
by Auto_tac;
nipkow@1327
   780
qed "take_0";
nipkow@1327
   781
nipkow@4935
   782
Goal "drop 0 xs = xs";
nipkow@3040
   783
by (induct_tac "xs" 1);
paulson@5316
   784
by Auto_tac;
nipkow@2608
   785
qed "drop_0";
nipkow@2608
   786
nipkow@4935
   787
Goal "take (Suc n) (x#xs) = x # take n xs";
paulson@1552
   788
by (Simp_tac 1);
nipkow@1419
   789
qed "take_Suc_Cons";
nipkow@1327
   790
nipkow@4935
   791
Goal "drop (Suc n) (x#xs) = drop n xs";
nipkow@2608
   792
by (Simp_tac 1);
nipkow@2608
   793
qed "drop_Suc_Cons";
nipkow@2608
   794
nipkow@2608
   795
Delsimps [take_Cons,drop_Cons];
nipkow@2608
   796
Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
nipkow@2608
   797
nipkow@4935
   798
Goal "!xs. length(take n xs) = min (length xs) n";
berghofe@5183
   799
by (induct_tac "n" 1);
paulson@5316
   800
 by Auto_tac;
wenzelm@8442
   801
by (case_tac "xs" 1);
paulson@5316
   802
 by Auto_tac;
nipkow@2608
   803
qed_spec_mp "length_take";
nipkow@2608
   804
Addsimps [length_take];
clasohm@923
   805
nipkow@4935
   806
Goal "!xs. length(drop n xs) = (length xs - n)";
berghofe@5183
   807
by (induct_tac "n" 1);
paulson@5316
   808
 by Auto_tac;
wenzelm@8442
   809
by (case_tac "xs" 1);
paulson@5316
   810
 by Auto_tac;
nipkow@2608
   811
qed_spec_mp "length_drop";
nipkow@2608
   812
Addsimps [length_drop];
nipkow@2608
   813
nipkow@4935
   814
Goal "!xs. length xs <= n --> take n xs = xs";
berghofe@5183
   815
by (induct_tac "n" 1);
paulson@5316
   816
 by Auto_tac;
wenzelm@8442
   817
by (case_tac "xs" 1);
paulson@5316
   818
 by Auto_tac;
nipkow@2608
   819
qed_spec_mp "take_all";
nipkow@7246
   820
Addsimps [take_all];
clasohm@923
   821
nipkow@4935
   822
Goal "!xs. length xs <= n --> drop n xs = []";
berghofe@5183
   823
by (induct_tac "n" 1);
paulson@5316
   824
 by Auto_tac;
wenzelm@8442
   825
by (case_tac "xs" 1);
paulson@5316
   826
 by Auto_tac;
nipkow@2608
   827
qed_spec_mp "drop_all";
nipkow@7246
   828
Addsimps [drop_all];
nipkow@2608
   829
paulson@5278
   830
Goal "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
berghofe@5183
   831
by (induct_tac "n" 1);
paulson@5316
   832
 by Auto_tac;
wenzelm@8442
   833
by (case_tac "xs" 1);
paulson@5316
   834
 by Auto_tac;
nipkow@2608
   835
qed_spec_mp "take_append";
nipkow@2608
   836
Addsimps [take_append];
nipkow@2608
   837
nipkow@4935
   838
Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
berghofe@5183
   839
by (induct_tac "n" 1);
paulson@5316
   840
 by Auto_tac;
wenzelm@8442
   841
by (case_tac "xs" 1);
paulson@5316
   842
 by Auto_tac;
nipkow@2608
   843
qed_spec_mp "drop_append";
nipkow@2608
   844
Addsimps [drop_append];
nipkow@2608
   845
nipkow@4935
   846
Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
berghofe@5183
   847
by (induct_tac "m" 1);
paulson@5316
   848
 by Auto_tac;
wenzelm@8442
   849
by (case_tac "xs" 1);
paulson@5316
   850
 by Auto_tac;
wenzelm@8442
   851
by (case_tac "na" 1);
paulson@5316
   852
 by Auto_tac;
nipkow@2608
   853
qed_spec_mp "take_take";
nipkow@7570
   854
Addsimps [take_take];
nipkow@2608
   855
nipkow@4935
   856
Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
berghofe@5183
   857
by (induct_tac "m" 1);
paulson@5316
   858
 by Auto_tac;
wenzelm@8442
   859
by (case_tac "xs" 1);
paulson@5316
   860
 by Auto_tac;
nipkow@2608
   861
qed_spec_mp "drop_drop";
nipkow@7570
   862
Addsimps [drop_drop];
clasohm@923
   863
nipkow@4935
   864
Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
berghofe@5183
   865
by (induct_tac "m" 1);
paulson@5316
   866
 by Auto_tac;
wenzelm@8442
   867
by (case_tac "xs" 1);
paulson@5316
   868
 by Auto_tac;
nipkow@2608
   869
qed_spec_mp "take_drop";
nipkow@2608
   870
paulson@6813
   871
Goal "!xs. take n xs @ drop n xs = xs";
paulson@6813
   872
by (induct_tac "n" 1);
paulson@6813
   873
 by Auto_tac;
wenzelm@8442
   874
by (case_tac "xs" 1);
paulson@6813
   875
 by Auto_tac;
paulson@6813
   876
qed_spec_mp "append_take_drop_id";
nipkow@8118
   877
Addsimps [append_take_drop_id];
paulson@6813
   878
nipkow@4935
   879
Goal "!xs. take n (map f xs) = map f (take n xs)"; 
berghofe@5183
   880
by (induct_tac "n" 1);
paulson@5316
   881
 by Auto_tac;
wenzelm@8442
   882
by (case_tac "xs" 1);
paulson@5316
   883
 by Auto_tac;
nipkow@2608
   884
qed_spec_mp "take_map"; 
nipkow@2608
   885
nipkow@4935
   886
Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
berghofe@5183
   887
by (induct_tac "n" 1);
paulson@5316
   888
 by Auto_tac;
wenzelm@8442
   889
by (case_tac "xs" 1);
paulson@5316
   890
 by Auto_tac;
nipkow@2608
   891
qed_spec_mp "drop_map";
nipkow@2608
   892
nipkow@4935
   893
Goal "!n i. i < n --> (take n xs)!i = xs!i";
paulson@3457
   894
by (induct_tac "xs" 1);
paulson@5316
   895
 by Auto_tac;
wenzelm@8442
   896
by (case_tac "n" 1);
paulson@3457
   897
 by (Blast_tac 1);
wenzelm@8442
   898
by (case_tac "i" 1);
paulson@5316
   899
 by Auto_tac;
nipkow@2608
   900
qed_spec_mp "nth_take";
nipkow@2608
   901
Addsimps [nth_take];
clasohm@923
   902
nipkow@4935
   903
Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
berghofe@5183
   904
by (induct_tac "n" 1);
paulson@5316
   905
 by Auto_tac;
wenzelm@8442
   906
by (case_tac "xs" 1);
paulson@5316
   907
 by Auto_tac;
nipkow@2608
   908
qed_spec_mp "nth_drop";
nipkow@2608
   909
Addsimps [nth_drop];
nipkow@2608
   910
nipkow@8118
   911
nipkow@8118
   912
Goal
nipkow@8118
   913
 "!zs. (xs@ys = zs) = (xs = take (length xs) zs & ys = drop (length xs) zs)";
paulson@8254
   914
by (induct_tac "xs" 1);
paulson@8254
   915
 by (Simp_tac 1);
paulson@8254
   916
by (Asm_full_simp_tac 1);
paulson@8254
   917
by (Clarify_tac 1);
wenzelm@8442
   918
by (case_tac "zs" 1);
paulson@8254
   919
by (Auto_tac);
nipkow@8118
   920
qed_spec_mp "append_eq_conv_conj";
nipkow@8118
   921
nipkow@2608
   922
(** takeWhile & dropWhile **)
nipkow@2608
   923
nipkow@3467
   924
section "takeWhile & dropWhile";
nipkow@3467
   925
nipkow@4935
   926
Goal "takeWhile P xs @ dropWhile P xs = xs";
nipkow@3586
   927
by (induct_tac "xs" 1);
paulson@5316
   928
by Auto_tac;
nipkow@3586
   929
qed "takeWhile_dropWhile_id";
nipkow@3586
   930
Addsimps [takeWhile_dropWhile_id];
nipkow@3586
   931
nipkow@4935
   932
Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
paulson@3457
   933
by (induct_tac "xs" 1);
paulson@5316
   934
by Auto_tac;
nipkow@2608
   935
bind_thm("takeWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   936
Addsimps [takeWhile_append1];
clasohm@923
   937
nipkow@4935
   938
Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
paulson@3457
   939
by (induct_tac "xs" 1);
paulson@5316
   940
by Auto_tac;
nipkow@2608
   941
bind_thm("takeWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   942
Addsimps [takeWhile_append2];
lcp@1169
   943
paulson@11289
   944
Goal "~P(x) ==> takeWhile P (xs @ (x#l)) = takeWhile P xs";
paulson@11289
   945
by (induct_tac "xs" 1);
paulson@11289
   946
by Auto_tac;
paulson@11289
   947
qed "takeWhile_tail";
paulson@11289
   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@10709
  1102
 by (force_tac (claset(),
paulson@10709
  1103
		simpset() addsplits [nat_diff_split] addsimps [min_def]) 1);
nipkow@8118
  1104
by (Clarify_tac 1);
paulson@8254
  1105
by (asm_full_simp_tac (simpset() addsimps [ball_Un]) 1);
nipkow@8118
  1106
qed "list_all2_append1";
nipkow@8118
  1107
nipkow@8118
  1108
Goalw [list_all2_def]
nipkow@8118
  1109
 "list_all2 P xs (ys@zs) = \
nipkow@8118
  1110
\ (EX us vs. xs = us@vs & length us = length ys & length vs = length zs & \
nipkow@8118
  1111
\            list_all2 P us ys & list_all2 P vs zs)";
paulson@8254
  1112
by (simp_tac (simpset() addsimps [zip_append2]) 1);
paulson@8254
  1113
by (rtac iffI 1);
paulson@8254
  1114
 by (res_inst_tac [("x","take (length ys) xs")] exI 1);
paulson@8254
  1115
 by (res_inst_tac [("x","drop (length ys) xs")] exI 1);
paulson@10709
  1116
 by (force_tac (claset(),
paulson@10709
  1117
		simpset() addsplits [nat_diff_split] addsimps [min_def]) 1);
nipkow@8118
  1118
by (Clarify_tac 1);
paulson@8254
  1119
by (asm_full_simp_tac (simpset() addsimps [ball_Un]) 1);
nipkow@8118
  1120
qed "list_all2_append2";
nipkow@8118
  1121
nipkow@8118
  1122
Goalw [list_all2_def]
nipkow@8115
  1123
  "list_all2 P xs ys = \
nipkow@8115
  1124
\  (length xs = length ys & (!i<length xs. P (xs!i) (ys!i)))";
paulson@8254
  1125
by (force_tac (claset(), simpset() addsimps [set_zip]) 1);
nipkow@8115
  1126
qed "list_all2_conv_all_nth";
nipkow@5272
  1127
oheimb@11336
  1128
Goal "ALL a b c. P1 a b --> P2 b c --> P3 a c ==> \
oheimb@11336
  1129
\ ALL bs cs. list_all2 P1 as bs --> list_all2 P2 bs cs --> list_all2 P3 as cs";
oheimb@11336
  1130
by (induct_tac "as" 1);
oheimb@11336
  1131
by  (Simp_tac 1);
oheimb@11336
  1132
by (rtac allI 1);
oheimb@11336
  1133
by (induct_tac "bs" 1);
oheimb@11336
  1134
by  (Simp_tac 1);
oheimb@11336
  1135
by (rtac allI 1);
oheimb@11336
  1136
by (induct_tac "cs" 1);
oheimb@11336
  1137
by Auto_tac;
oheimb@11336
  1138
qed_spec_mp "list_all2_trans";
oheimb@11336
  1139
oheimb@11336
  1140
nipkow@5272
  1141
section "foldl";
nipkow@5272
  1142
nipkow@5272
  1143
Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys";
paulson@5318
  1144
by (induct_tac "xs" 1);
paulson@5316
  1145
by Auto_tac;
nipkow@5272
  1146
qed_spec_mp "foldl_append";
nipkow@5272
  1147
Addsimps [foldl_append];
nipkow@5272
  1148
nipkow@5272
  1149
(* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
nipkow@5272
  1150
   because it requires an additional transitivity step
nipkow@5272
  1151
*)
nipkow@5272
  1152
Goal "!n::nat. m <= n --> m <= foldl op+ n ns";
paulson@5318
  1153
by (induct_tac "ns" 1);
nipkow@6058
  1154
by Auto_tac;
nipkow@5272
  1155
qed_spec_mp "start_le_sum";
nipkow@5272
  1156
paulson@8935
  1157
Goal "!!n::nat. n : set ns ==> n <= foldl op+ 0 ns";
oheimb@5758
  1158
by (force_tac (claset() addIs [start_le_sum],
oheimb@5758
  1159
              simpset() addsimps [in_set_conv_decomp]) 1);
nipkow@5272
  1160
qed "elem_le_sum";
nipkow@5272
  1161
paulson@8935
  1162
Goal "!m::nat. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))";
paulson@5318
  1163
by (induct_tac "ns" 1);
paulson@5316
  1164
by Auto_tac;
nipkow@5272
  1165
qed_spec_mp "sum_eq_0_conv";
nipkow@5272
  1166
AddIffs [sum_eq_0_conv];
nipkow@5272
  1167
nipkow@5425
  1168
(** upto **)
nipkow@5425
  1169
nipkow@5427
  1170
(* Does not terminate! *)
nipkow@5427
  1171
Goal "[i..j(] = (if i<j then i#[Suc i..j(] else [])";
paulson@6162
  1172
by (induct_tac "j" 1);
nipkow@5427
  1173
by Auto_tac;
nipkow@5427
  1174
qed "upt_rec";
nipkow@5425
  1175
nipkow@5427
  1176
Goal "j<=i ==> [i..j(] = []";
paulson@6162
  1177
by (stac upt_rec 1);
paulson@6162
  1178
by (Asm_simp_tac 1);
nipkow@5427
  1179
qed "upt_conv_Nil";
nipkow@5427
  1180
Addsimps [upt_conv_Nil];
nipkow@5427
  1181
paulson@8982
  1182
(*Only needed if upt_Suc is deleted from the simpset*)
nipkow@5427
  1183
Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]";
nipkow@5427
  1184
by (Asm_simp_tac 1);
paulson@8982
  1185
qed "upt_Suc_append";
nipkow@5427
  1186
nipkow@5427
  1187
Goal "i<j ==> [i..j(] = i#[Suc i..j(]";
paulson@6162
  1188
by (rtac trans 1);
paulson@6162
  1189
by (stac upt_rec 1);
paulson@6162
  1190
by (rtac refl 2);
nipkow@5427
  1191
by (Asm_simp_tac 1);
nipkow@5427
  1192
qed "upt_conv_Cons";
nipkow@5427
  1193
paulson@9003
  1194
(*LOOPS as a simprule, since j<=j*)
paulson@9003
  1195
Goal "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]";
paulson@9003
  1196
by (induct_tac "k" 1);
paulson@9003
  1197
by Auto_tac;
paulson@9003
  1198
qed "upt_add_eq_append";
paulson@9003
  1199
nipkow@5427
  1200
Goal "length [i..j(] = j-i";
paulson@6162
  1201
by (induct_tac "j" 1);
nipkow@5427
  1202
 by (Simp_tac 1);
paulson@6162
  1203
by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
nipkow@5427
  1204
qed "length_upt";
nipkow@5427
  1205
Addsimps [length_upt];
nipkow@5425
  1206
nipkow@5427
  1207
Goal "i+k < j --> [i..j(] ! k = i+k";
paulson@6162
  1208
by (induct_tac "j" 1);
paulson@9014
  1209
 by (asm_simp_tac (simpset() addsimps [less_Suc_eq, nth_append] 
paulson@9014
  1210
                             addsplits [nat_diff_split]) 2);
paulson@9014
  1211
by (Simp_tac 1);
nipkow@5427
  1212
qed_spec_mp "nth_upt";
nipkow@5427
  1213
Addsimps [nth_upt];
nipkow@5425
  1214
nipkow@6433
  1215
Goal "!i. i+m <= n --> take m [i..n(] = [i..i+m(]";
paulson@6813
  1216
by (induct_tac "m" 1);
paulson@6813
  1217
 by (Simp_tac 1);
paulson@6813
  1218
by (Clarify_tac 1);
paulson@6813
  1219
by (stac upt_rec 1);
paulson@6813
  1220
by (rtac sym 1);
paulson@6813
  1221
by (stac upt_rec 1);
paulson@6813
  1222
by (asm_simp_tac (simpset() delsimps (thms"upt.simps")) 1);
nipkow@6433
  1223
qed_spec_mp "take_upt";
nipkow@6433
  1224
Addsimps [take_upt];
nipkow@6433
  1225
paulson@9003
  1226
Goal "map Suc [m..n(] = [Suc m..n]";
paulson@6813
  1227
by (induct_tac "n" 1);
paulson@9003
  1228
by Auto_tac;
paulson@9003
  1229
qed "map_Suc_upt";
paulson@9003
  1230
paulson@9003
  1231
Goal "ALL i. i < n-m --> (map f [m..n(]) ! i = f(m+i)";
nipkow@9747
  1232
by (induct_thm_tac diff_induct "n m" 1);
paulson@9003
  1233
by (stac (map_Suc_upt RS sym) 3);
paulson@9003
  1234
by (auto_tac (claset(), simpset() addsimps [less_diff_conv, nth_upt]));
nipkow@6433
  1235
qed_spec_mp "nth_map_upt";
nipkow@6433
  1236
paulson@6813
  1237
Goal "ALL xs ys. k <= length xs --> k <= length ys -->  \
paulson@6813
  1238
\        (ALL i. i < k --> xs!i = ys!i)  \
paulson@6813
  1239
\     --> take k xs = take k ys";
paulson@6813
  1240
by (induct_tac "k" 1);
paulson@6813
  1241
by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq_0_disj, 
paulson@6813
  1242
						all_conj_distrib])));
paulson@6813
  1243
by (Clarify_tac 1);
paulson@6813
  1244
(*Both lists must be non-empty*)
wenzelm@8442
  1245
by (case_tac "xs" 1);
wenzelm@8442
  1246
by (case_tac "ys" 2);
paulson@6813
  1247
by (ALLGOALS Clarify_tac);
paulson@6813
  1248
(*prenexing's needed, not miniscoping*)
paulson@6813
  1249
by (ALLGOALS (full_simp_tac (simpset() addsimps (all_simps RL [sym])  
paulson@6813
  1250
                                       delsimps (all_simps))));
paulson@6813
  1251
by (Blast_tac 1);
paulson@6813
  1252
qed_spec_mp "nth_take_lemma";
paulson@6813
  1253
paulson@6813
  1254
Goal "[| length xs = length ys;  \
paulson@6813
  1255
\        ALL i. i < length xs --> xs!i = ys!i |]  \
paulson@6813
  1256
\     ==> xs = ys";
paulson@6813
  1257
by (forward_tac [[le_refl, eq_imp_le] MRS nth_take_lemma] 1);
paulson@6813
  1258
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [take_all])));
paulson@6813
  1259
qed_spec_mp "nth_equalityI";
paulson@6813
  1260
paulson@6813
  1261
(*The famous take-lemma*)
paulson@6813
  1262
Goal "(ALL i. take i xs = take i ys) ==> xs = ys";
paulson@6813
  1263
by (dres_inst_tac [("x", "max (length xs) (length ys)")] spec 1);
paulson@6813
  1264
by (full_simp_tac (simpset() addsimps [le_max_iff_disj, take_all]) 1);
paulson@6813
  1265
qed_spec_mp "take_equalityI";
paulson@6813
  1266
nipkow@5272
  1267
nipkow@4605
  1268
(** nodups & remdups **)
nipkow@4605
  1269
section "nodups & remdups";
nipkow@4605
  1270
nipkow@4935
  1271
Goal "set(remdups xs) = set xs";
nipkow@4605
  1272
by (induct_tac "xs" 1);
nipkow@4605
  1273
 by (Simp_tac 1);
nipkow@4686
  1274
by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
nipkow@4605
  1275
qed "set_remdups";
nipkow@4605
  1276
Addsimps [set_remdups];
nipkow@4605
  1277
nipkow@4935
  1278
Goal "nodups(remdups xs)";
nipkow@4605
  1279
by (induct_tac "xs" 1);
paulson@5316
  1280
by Auto_tac;
nipkow@4605
  1281
qed "nodups_remdups";
nipkow@4605
  1282
nipkow@4935
  1283
Goal "nodups xs --> nodups (filter P xs)";
nipkow@4605
  1284
by (induct_tac "xs" 1);
paulson@5316
  1285
by Auto_tac;
nipkow@4605
  1286
qed_spec_mp "nodups_filter";
nipkow@4605
  1287
nipkow@3589
  1288
(** replicate **)
nipkow@3589
  1289
section "replicate";
nipkow@3589
  1290
nipkow@6794
  1291
Goal "length(replicate n x) = n";
paulson@6813
  1292
by (induct_tac "n" 1);
paulson@6813
  1293
by Auto_tac;
nipkow@6794
  1294
qed "length_replicate";
nipkow@6794
  1295
Addsimps [length_replicate];
nipkow@6794
  1296
nipkow@6794
  1297
Goal "map f (replicate n x) = replicate n (f x)";
nipkow@6794
  1298
by (induct_tac "n" 1);
paulson@6813
  1299
by Auto_tac;
nipkow@6794
  1300
qed "map_replicate";
nipkow@6794
  1301
Addsimps [map_replicate];
nipkow@6794
  1302
nipkow@6794
  1303
Goal "(replicate n x) @ (x#xs) = x # replicate n x @ xs";
nipkow@6794
  1304
by (induct_tac "n" 1);
paulson@6813
  1305
by Auto_tac;
nipkow@6794
  1306
qed "replicate_app_Cons_same";
nipkow@6794
  1307
nipkow@6794
  1308
Goal "rev(replicate n x) = replicate n x";
nipkow@6794
  1309
by (induct_tac "n" 1);
paulson@6813
  1310
 by (Simp_tac 1);
nipkow@6794
  1311
by (asm_simp_tac (simpset() addsimps [replicate_app_Cons_same]) 1);
nipkow@6794
  1312
qed "rev_replicate";
nipkow@6794
  1313
Addsimps [rev_replicate];
nipkow@6794
  1314
nipkow@8009
  1315
Goal "replicate (n+m) x = replicate n x @ replicate m x";
nipkow@8009
  1316
by (induct_tac "n" 1);
nipkow@8009
  1317
by Auto_tac;
nipkow@8009
  1318
qed "replicate_add";
nipkow@8009
  1319
nipkow@6794
  1320
Goal"n ~= 0 --> hd(replicate n x) = x";
nipkow@6794
  1321
by (induct_tac "n" 1);
paulson@6813
  1322
by Auto_tac;
nipkow@6794
  1323
qed_spec_mp "hd_replicate";
nipkow@6794
  1324
Addsimps [hd_replicate];
nipkow@6794
  1325
wenzelm@11701
  1326
Goal "n ~= 0 --> tl(replicate n x) = replicate (n - 1) x";
nipkow@6794
  1327
by (induct_tac "n" 1);
paulson@6813
  1328
by Auto_tac;
nipkow@6794
  1329
qed_spec_mp "tl_replicate";
nipkow@6794
  1330
Addsimps [tl_replicate];
nipkow@6794
  1331
nipkow@6794
  1332
Goal "n ~= 0 --> last(replicate n x) = x";
nipkow@6794
  1333
by (induct_tac "n" 1);
paulson@6813
  1334
by Auto_tac;
nipkow@6794
  1335
qed_spec_mp "last_replicate";
nipkow@6794
  1336
Addsimps [last_replicate];
nipkow@6794
  1337
nipkow@6794
  1338
Goal "!i. i<n --> (replicate n x)!i = x";
paulson@6813
  1339
by (induct_tac "n" 1);
paulson@6813
  1340
 by (Simp_tac 1);
paulson@6813
  1341
by (asm_simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
nipkow@6794
  1342
qed_spec_mp "nth_replicate";
nipkow@6794
  1343
Addsimps [nth_replicate];
nipkow@6794
  1344
nipkow@4935
  1345
Goal "set(replicate (Suc n) x) = {x}";
wenzelm@4423
  1346
by (induct_tac "n" 1);
paulson@5316
  1347
by Auto_tac;
nipkow@3589
  1348
val lemma = result();
nipkow@3589
  1349
nipkow@5043
  1350
Goal "n ~= 0 ==> set(replicate n x) = {x}";
wenzelm@4423
  1351
by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
nipkow@3589
  1352
qed "set_replicate";
nipkow@3589
  1353
Addsimps [set_replicate];
nipkow@5162
  1354
nipkow@8009
  1355
Goal "set(replicate n x) = (if n=0 then {} else {x})";
paulson@8064
  1356
by (Auto_tac);
nipkow@8009
  1357
qed "set_replicate_conv_if";
nipkow@8009
  1358
nipkow@8009
  1359
Goal "x : set(replicate n y) --> x=y";
paulson@8064
  1360
by (asm_simp_tac (simpset() addsimps [set_replicate_conv_if]) 1);
nipkow@8009
  1361
qed_spec_mp "in_set_replicateD";
nipkow@8009
  1362
nipkow@5162
  1363
nipkow@5281
  1364
(*** Lexcicographic orderings on lists ***)
nipkow@5281
  1365
section"Lexcicographic orderings on lists";
nipkow@5281
  1366
nipkow@5281
  1367
Goal "wf r ==> wf(lexn r n)";
paulson@5318
  1368
by (induct_tac "n" 1);
paulson@5318
  1369
by (Simp_tac 1);
paulson@5318
  1370
by (Simp_tac 1);
paulson@5318
  1371
by (rtac wf_subset 1);
paulson@5318
  1372
by (rtac Int_lower1 2);
paulson@5318
  1373
by (rtac wf_prod_fun_image 1);
paulson@5318
  1374
by (rtac injI 2);
paulson@6813
  1375
by Auto_tac;
nipkow@5281
  1376
qed "wf_lexn";
nipkow@5281
  1377
nipkow@5281
  1378
Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n";
paulson@5318
  1379
by (induct_tac "n" 1);
paulson@6813
  1380
by Auto_tac;
nipkow@5281
  1381
qed_spec_mp "lexn_length";
nipkow@5281
  1382
nipkow@5281
  1383
Goalw [lex_def] "wf r ==> wf(lex r)";
paulson@5318
  1384
by (rtac wf_UN 1);
paulson@5318
  1385
by (blast_tac (claset() addIs [wf_lexn]) 1);
paulson@5318
  1386
by (Clarify_tac 1);
paulson@5318
  1387
by (rename_tac "m n" 1);
paulson@5318
  1388
by (subgoal_tac "m ~= n" 1);
paulson@5318
  1389
 by (Blast_tac 2);
paulson@5318
  1390
by (blast_tac (claset() addDs [lexn_length,not_sym]) 1);
nipkow@5281
  1391
qed "wf_lex";
nipkow@5281
  1392
AddSIs [wf_lex];
nipkow@5281
  1393
paulson@11265
  1394
nipkow@5281
  1395
Goal
nipkow@5281
  1396
 "lexn r n = \
nipkow@5281
  1397
\ {(xs,ys). length xs = n & length ys = n & \
nipkow@5281
  1398
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5318
  1399
by (induct_tac "n" 1);
paulson@5318
  1400
 by (Simp_tac 1);
paulson@5318
  1401
 by (Blast_tac 1);
paulson@11265
  1402
by (asm_full_simp_tac (simpset() addsimps [image_Collect, lex_prod_def]) 1);
paulson@11265
  1403
by Auto_tac;
paulson@5318
  1404
  by (Blast_tac 1);
paulson@5318
  1405
 by (rename_tac "a xys x xs' y ys'" 1);
paulson@5318
  1406
 by (res_inst_tac [("x","a#xys")] exI 1);
paulson@5318
  1407
 by (Simp_tac 1);
wenzelm@8442
  1408
by (case_tac "xys" 1);
paulson@11265
  1409
 by (ALLGOALS Asm_full_simp_tac);
paulson@5318
  1410
by (Blast_tac 1);
nipkow@5281
  1411
qed "lexn_conv";
nipkow@5281
  1412
nipkow@5281
  1413
Goalw [lex_def]
nipkow@5281
  1414
 "lex r = \
nipkow@5281
  1415
\ {(xs,ys). length xs = length ys & \
nipkow@5281
  1416
\           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
paulson@5641
  1417
by (force_tac (claset(), simpset() addsimps [lexn_conv]) 1);
nipkow@5281
  1418
qed "lex_conv";
nipkow@5281
  1419
nipkow@5281
  1420
Goalw [lexico_def] "wf r ==> wf(lexico r)";
paulson@5318
  1421
by (Blast_tac 1);
nipkow@5281
  1422
qed "wf_lexico";
nipkow@5281
  1423
AddSIs [wf_lexico];
nipkow@5281
  1424
paulson@10709
  1425
Goalw [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def]
nipkow@5281
  1426
"lexico r = {(xs,ys). length xs < length ys | \
nipkow@5281
  1427
\                     length xs = length ys & (xs,ys) : lex r}";
paulson@5318
  1428
by (Simp_tac 1);
nipkow@5281
  1429
qed "lexico_conv";
nipkow@5281
  1430
nipkow@5283
  1431
Goal "([],ys) ~: lex r";
paulson@5318
  1432
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1433
qed "Nil_notin_lex";
nipkow@5283
  1434
nipkow@5283
  1435
Goal "(xs,[]) ~: lex r";
paulson@5318
  1436
by (simp_tac (simpset() addsimps [lex_conv]) 1);
nipkow@5283
  1437
qed "Nil2_notin_lex";
nipkow@5283
  1438
nipkow@5283
  1439
AddIffs [Nil_notin_lex,Nil2_notin_lex];
nipkow@5283
  1440
nipkow@5283
  1441
Goal "((x#xs,y#ys) : lex r) = \
nipkow@5283
  1442
\     ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)";
paulson@5318
  1443
by (simp_tac (simpset() addsimps [lex_conv]) 1);
paulson@5318
  1444
by (rtac iffI 1);
paulson@5318
  1445
 by (blast_tac (claset() addIs [Cons_eq_appendI]) 2);
paulson@10709
  1446
by (Clarify_tac 1);
wenzelm@8442
  1447
by (case_tac "xys" 1);
paulson@5318
  1448
by (Asm_full_simp_tac 1);
paulson@5318
  1449
by (Asm_full_simp_tac 1);
paulson@5318
  1450
by (Blast_tac 1);
nipkow@5283
  1451
qed "Cons_in_lex";
nipkow@5283
  1452
AddIffs [Cons_in_lex];
paulson@7032
  1453
paulson@7032
  1454
paulson@9336
  1455
(*** sublist (a generalization of nth to sets) ***)
paulson@9336
  1456
paulson@9336
  1457
Goalw [sublist_def] "sublist l {} = []";
paulson@9336
  1458
by Auto_tac;
paulson@9336
  1459
qed "sublist_empty";
paulson@9336
  1460
paulson@9336
  1461
Goalw [sublist_def] "sublist [] A = []";
paulson@9336
  1462
by Auto_tac;
paulson@9336
  1463
qed "sublist_nil";
paulson@9336
  1464
paulson@9336
  1465
Goal "map fst [p:zip xs [i..i + length xs(] . snd p : A] =     \
paulson@9336
  1466
\     map fst [p:zip xs [0..length xs(] . snd p + i : A]";
nipkow@9747
  1467
by (rev_induct_tac "xs" 1);
paulson@9336
  1468
 by (asm_simp_tac (simpset() addsimps [add_commute]) 2);
paulson@9336
  1469
by (Simp_tac 1);
paulson@9336
  1470
qed "sublist_shift_lemma";
paulson@9336
  1471
paulson@9336
  1472
Goalw [sublist_def]
paulson@9336
  1473
     "sublist (l@l') A = sublist l A @ sublist l' {j. j + length l : A}";
nipkow@9747
  1474
by (rev_induct_tac "l'" 1);
paulson@9336
  1475
by (Simp_tac 1);
paulson@9336
  1476
by (asm_simp_tac (simpset() addsimps [inst "i" "0" upt_add_eq_append, 
paulson@9336
  1477
	                              zip_append, sublist_shift_lemma]) 1);
paulson@9336
  1478
by (asm_simp_tac (simpset() addsimps [add_commute]) 1);
paulson@9336
  1479
qed "sublist_append";
paulson@9336
  1480
paulson@9336
  1481
Addsimps [sublist_empty, sublist_nil];
paulson@9336
  1482
paulson@9336
  1483
Goal "sublist (x#l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}";
nipkow@9747
  1484
by (rev_induct_tac "l" 1);
paulson@9336
  1485
 by (asm_simp_tac (simpset() delsimps [append_Cons]
paulson@9336
  1486
	 		     addsimps [append_Cons RS sym, sublist_append]) 2);
paulson@9336
  1487
by (simp_tac (simpset() addsimps [sublist_def]) 1);
paulson@9336
  1488
qed "sublist_Cons";
paulson@9336
  1489
paulson@9336
  1490
Goal "sublist [x] A = (if 0 : A then [x] else [])";
paulson@9336
  1491
by (simp_tac (simpset() addsimps [sublist_Cons]) 1);
paulson@9336
  1492
qed "sublist_singleton";
paulson@9336
  1493
Addsimps [sublist_singleton];
paulson@9336
  1494
paulson@9336
  1495
Goal "sublist l {..n(} = take n l";
nipkow@9747
  1496
by (rev_induct_tac "l" 1);
paulson@9336
  1497
 by (asm_simp_tac (simpset() addsplits [nat_diff_split]
paulson@9336
  1498
                             addsimps [sublist_append]) 2);
paulson@9336
  1499
by (Simp_tac 1);
paulson@9336
  1500
qed "sublist_upt_eq_take";
paulson@9336
  1501
Addsimps [sublist_upt_eq_take];
paulson@9336
  1502
paulson@9336
  1503
paulson@11868
  1504
Goal "take n (x#xs) = (if n=0 then [] else x # take (n - 1) xs)";
wenzelm@8442
  1505
by (case_tac "n" 1);
paulson@7032
  1506
by (ALLGOALS 
paulson@7032
  1507
    (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
paulson@7032
  1508
qed "take_Cons'";
paulson@7032
  1509
paulson@11868
  1510
Goal "drop n (x#xs) = (if n=0 then x#xs else drop (n - 1) xs)";
wenzelm@8442
  1511
by (case_tac "n" 1);
paulson@7032
  1512
by (ALLGOALS
paulson@7032
  1513
    (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
paulson@7032
  1514
qed "drop_Cons'";
paulson@7032
  1515
paulson@11868
  1516
Goal "(x#xs)!n = (if n=0 then x else xs!(n - 1))";
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 "nth_Cons'";
paulson@7032
  1521
paulson@7032
  1522
Addsimps (map (inst "n" "number_of ?v") [take_Cons', drop_Cons', nth_Cons']);
paulson@7032
  1523