src/HOL/List.ML
author wenzelm
Fri Jul 03 17:34:24 1998 +0200 (1998-07-03)
changeset 5122 229190f9f303
parent 5077 71043526295f
child 5129 99ffd3dfb180
permissions -rw-r--r--
removed duplicate thms;
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);
clasohm@1264
    11
by (ALLGOALS Asm_simp_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);
clasohm@1264
    18
by (Simp_tac 1);
clasohm@1264
    19
by (Asm_simp_tac 1);
clasohm@923
    20
qed "neq_Nil_conv";
clasohm@923
    21
nipkow@4830
    22
(* Induction over the length of a list: *)
nipkow@4935
    23
val [prem] = Goal
nipkow@4911
    24
  "(!!xs. (!ys. length ys < length xs --> P ys) ==> P xs) ==> P(xs)";
nipkow@4911
    25
by(rtac measure_induct 1 THEN etac prem 1);
nipkow@4911
    26
qed "length_induct";
nipkow@4911
    27
clasohm@923
    28
paulson@3468
    29
(** "lists": the list-forming operator over sets **)
paulson@3342
    30
nipkow@5043
    31
Goalw lists.defs "A<=B ==> lists A <= lists B";
paulson@3342
    32
by (rtac lfp_mono 1);
paulson@3342
    33
by (REPEAT (ares_tac basic_monos 1));
paulson@3342
    34
qed "lists_mono";
paulson@3196
    35
paulson@3468
    36
val listsE = lists.mk_cases list.simps  "x#l : lists A";
paulson@3468
    37
AddSEs [listsE];
paulson@3468
    38
AddSIs lists.intrs;
paulson@3468
    39
nipkow@5043
    40
Goal "l: lists A ==> l: lists B --> l: lists (A Int B)";
paulson@3468
    41
by (etac lists.induct 1);
paulson@3468
    42
by (ALLGOALS Blast_tac);
paulson@3468
    43
qed_spec_mp "lists_IntI";
paulson@3468
    44
nipkow@4935
    45
Goal "lists (A Int B) = lists A Int lists B";
wenzelm@4423
    46
by (rtac (mono_Int RS equalityI) 1);
wenzelm@4089
    47
by (simp_tac (simpset() addsimps [mono_def, lists_mono]) 1);
wenzelm@4089
    48
by (blast_tac (claset() addSIs [lists_IntI]) 1);
paulson@3468
    49
qed "lists_Int_eq";
paulson@3468
    50
Addsimps [lists_Int_eq];
paulson@3468
    51
paulson@3196
    52
nipkow@4643
    53
(**  Case analysis **)
nipkow@4643
    54
section "Case analysis";
nipkow@2608
    55
nipkow@4935
    56
val prems = Goal "[| P([]); !!x xs. P(x#xs) |] ==> P(xs)";
paulson@3457
    57
by (induct_tac "xs" 1);
paulson@3457
    58
by (REPEAT(resolve_tac prems 1));
nipkow@2608
    59
qed "list_cases";
nipkow@2608
    60
nipkow@4935
    61
Goal "(xs=[] --> P([])) & (!y ys. xs=y#ys --> P(y#ys)) --> P(xs)";
nipkow@3040
    62
by (induct_tac "xs" 1);
paulson@2891
    63
by (Blast_tac 1);
paulson@2891
    64
by (Blast_tac 1);
nipkow@2608
    65
bind_thm("list_eq_cases",
nipkow@2608
    66
  impI RSN (2,allI RSN (2,allI RSN (2,impI RS (conjI RS (result() RS mp))))));
nipkow@2608
    67
nipkow@3860
    68
(** length **)
nipkow@3860
    69
(* needs to come before "@" because of thm append_eq_append_conv *)
nipkow@3860
    70
nipkow@3860
    71
section "length";
nipkow@3860
    72
nipkow@4935
    73
Goal "length(xs@ys) = length(xs)+length(ys)";
nipkow@3860
    74
by (induct_tac "xs" 1);
nipkow@3860
    75
by (ALLGOALS Asm_simp_tac);
nipkow@3860
    76
qed"length_append";
nipkow@3860
    77
Addsimps [length_append];
nipkow@3860
    78
nipkow@4935
    79
Goal "length (map f l) = length l";
nipkow@3860
    80
by (induct_tac "l" 1);
nipkow@3860
    81
by (ALLGOALS Simp_tac);
nipkow@3860
    82
qed "length_map";
nipkow@3860
    83
Addsimps [length_map];
nipkow@3860
    84
nipkow@4935
    85
Goal "length(rev xs) = length(xs)";
nipkow@3860
    86
by (induct_tac "xs" 1);
nipkow@3860
    87
by (ALLGOALS Asm_simp_tac);
nipkow@3860
    88
qed "length_rev";
nipkow@3860
    89
Addsimps [length_rev];
nipkow@3860
    90
nipkow@5043
    91
Goal "xs ~= [] ==> length(tl xs) = (length xs) - 1";
wenzelm@4423
    92
by (exhaust_tac "xs" 1);
wenzelm@4423
    93
by (ALLGOALS Asm_full_simp_tac);
nipkow@3896
    94
qed "length_tl";
nipkow@3896
    95
Addsimps [length_tl];
nipkow@3896
    96
nipkow@4935
    97
Goal "(length xs = 0) = (xs = [])";
nipkow@3860
    98
by (induct_tac "xs" 1);
nipkow@3860
    99
by (ALLGOALS Asm_simp_tac);
nipkow@3860
   100
qed "length_0_conv";
nipkow@3860
   101
AddIffs [length_0_conv];
nipkow@3860
   102
nipkow@4935
   103
Goal "(0 = length xs) = (xs = [])";
nipkow@3860
   104
by (induct_tac "xs" 1);
nipkow@3860
   105
by (ALLGOALS Asm_simp_tac);
nipkow@3860
   106
qed "zero_length_conv";
nipkow@3860
   107
AddIffs [zero_length_conv];
nipkow@3860
   108
nipkow@4935
   109
Goal "(0 < length xs) = (xs ~= [])";
nipkow@3860
   110
by (induct_tac "xs" 1);
nipkow@3860
   111
by (ALLGOALS Asm_simp_tac);
nipkow@3860
   112
qed "length_greater_0_conv";
nipkow@3860
   113
AddIffs [length_greater_0_conv];
nipkow@3860
   114
clasohm@923
   115
(** @ - append **)
clasohm@923
   116
nipkow@3467
   117
section "@ - append";
nipkow@3467
   118
nipkow@4935
   119
Goal "(xs@ys)@zs = xs@(ys@zs)";
nipkow@3040
   120
by (induct_tac "xs" 1);
clasohm@1264
   121
by (ALLGOALS Asm_simp_tac);
clasohm@923
   122
qed "append_assoc";
nipkow@2512
   123
Addsimps [append_assoc];
clasohm@923
   124
nipkow@4935
   125
Goal "xs @ [] = xs";
nipkow@3040
   126
by (induct_tac "xs" 1);
clasohm@1264
   127
by (ALLGOALS Asm_simp_tac);
clasohm@923
   128
qed "append_Nil2";
nipkow@2512
   129
Addsimps [append_Nil2];
clasohm@923
   130
nipkow@4935
   131
Goal "(xs@ys = []) = (xs=[] & ys=[])";
nipkow@3040
   132
by (induct_tac "xs" 1);
clasohm@1264
   133
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   134
qed "append_is_Nil_conv";
nipkow@2608
   135
AddIffs [append_is_Nil_conv];
nipkow@2608
   136
nipkow@4935
   137
Goal "([] = xs@ys) = (xs=[] & ys=[])";
nipkow@3040
   138
by (induct_tac "xs" 1);
nipkow@2608
   139
by (ALLGOALS Asm_simp_tac);
paulson@3457
   140
by (Blast_tac 1);
nipkow@2608
   141
qed "Nil_is_append_conv";
nipkow@2608
   142
AddIffs [Nil_is_append_conv];
clasohm@923
   143
nipkow@4935
   144
Goal "(xs @ ys = xs) = (ys=[])";
nipkow@3574
   145
by (induct_tac "xs" 1);
nipkow@3574
   146
by (ALLGOALS Asm_simp_tac);
nipkow@3574
   147
qed "append_self_conv";
nipkow@3574
   148
nipkow@4935
   149
Goal "(xs = xs @ ys) = (ys=[])";
nipkow@3574
   150
by (induct_tac "xs" 1);
nipkow@3574
   151
by (ALLGOALS Asm_simp_tac);
nipkow@3574
   152
by (Blast_tac 1);
nipkow@3574
   153
qed "self_append_conv";
nipkow@3574
   154
AddIffs [append_self_conv,self_append_conv];
nipkow@3574
   155
nipkow@4935
   156
Goal "!ys. length xs = length ys | length us = length vs \
nipkow@3860
   157
\              --> (xs@us = ys@vs) = (xs=ys & us=vs)";
wenzelm@4423
   158
by (induct_tac "xs" 1);
wenzelm@4423
   159
 by (rtac allI 1);
wenzelm@4423
   160
 by (exhaust_tac "ys" 1);
wenzelm@4423
   161
  by (Asm_simp_tac 1);
wenzelm@4423
   162
 by (fast_tac (claset() addIs [less_add_Suc2] addss simpset()
nipkow@3860
   163
                      addEs [less_not_refl2 RSN (2,rev_notE)]) 1);
wenzelm@4423
   164
by (rtac allI 1);
wenzelm@4423
   165
by (exhaust_tac "ys" 1);
wenzelm@4423
   166
 by (fast_tac (claset() addIs [less_add_Suc2] addss simpset()
nipkow@3860
   167
                      addEs [(less_not_refl2 RS not_sym) RSN (2,rev_notE)]) 1);
wenzelm@4423
   168
by (Asm_simp_tac 1);
nipkow@3860
   169
qed_spec_mp "append_eq_append_conv";
nipkow@3860
   170
Addsimps [append_eq_append_conv];
nipkow@3860
   171
nipkow@4935
   172
Goal "(xs @ ys = xs @ zs) = (ys=zs)";
nipkow@3896
   173
by (Simp_tac 1);
nipkow@3896
   174
qed "same_append_eq";
nipkow@3860
   175
nipkow@4935
   176
Goal "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)"; 
nipkow@3896
   177
by (Simp_tac 1);
nipkow@3896
   178
qed "append1_eq_conv";
nipkow@2608
   179
nipkow@4935
   180
Goal "(ys @ xs = zs @ xs) = (ys=zs)";
nipkow@3896
   181
by (Simp_tac 1);
nipkow@3896
   182
qed "append_same_eq";
nipkow@2608
   183
nipkow@3896
   184
AddSIs
nipkow@3896
   185
 [same_append_eq RS iffD2, append1_eq_conv RS iffD2, append_same_eq RS iffD2];
nipkow@3896
   186
AddSDs
nipkow@3896
   187
 [same_append_eq RS iffD1, append1_eq_conv RS iffD1, append_same_eq RS iffD1];
nipkow@3571
   188
nipkow@4935
   189
Goal "(xs @ ys = ys) = (xs=[])";
nipkow@4647
   190
by(cut_inst_tac [("zs","[]")] append_same_eq 1);
nipkow@4647
   191
by(Asm_full_simp_tac 1);
nipkow@4647
   192
qed "append_self_conv2";
nipkow@4647
   193
nipkow@4935
   194
Goal "(ys = xs @ ys) = (xs=[])";
nipkow@4647
   195
by(simp_tac (simpset() addsimps
nipkow@4647
   196
     [simplify (simpset()) (read_instantiate[("ys","[]")]append_same_eq)]) 1);
nipkow@4647
   197
by(Blast_tac 1);
nipkow@4647
   198
qed "self_append_conv2";
nipkow@4647
   199
AddIffs [append_self_conv2,self_append_conv2];
nipkow@4647
   200
nipkow@4935
   201
Goal "xs ~= [] --> hd xs # tl xs = xs";
paulson@3457
   202
by (induct_tac "xs" 1);
paulson@3457
   203
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   204
qed_spec_mp "hd_Cons_tl";
nipkow@2608
   205
Addsimps [hd_Cons_tl];
clasohm@923
   206
nipkow@4935
   207
Goal "hd(xs@ys) = (if xs=[] then hd ys else hd xs)";
nipkow@3040
   208
by (induct_tac "xs" 1);
nipkow@1327
   209
by (ALLGOALS Asm_simp_tac);
nipkow@1327
   210
qed "hd_append";
clasohm@923
   211
nipkow@5043
   212
Goal "xs ~= [] ==> hd(xs @ ys) = hd xs";
wenzelm@4089
   213
by (asm_simp_tac (simpset() addsimps [hd_append]
nipkow@4069
   214
                           addsplits [split_list_case]) 1);
nipkow@3571
   215
qed "hd_append2";
nipkow@3571
   216
Addsimps [hd_append2];
nipkow@3571
   217
nipkow@4935
   218
Goal "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)";
wenzelm@4089
   219
by (simp_tac (simpset() addsplits [split_list_case]) 1);
nipkow@2608
   220
qed "tl_append";
nipkow@2608
   221
nipkow@5043
   222
Goal "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys";
wenzelm@4089
   223
by (asm_simp_tac (simpset() addsimps [tl_append]
nipkow@4069
   224
                           addsplits [split_list_case]) 1);
nipkow@3571
   225
qed "tl_append2";
nipkow@3571
   226
Addsimps [tl_append2];
nipkow@3571
   227
nipkow@4830
   228
nipkow@2608
   229
(** map **)
nipkow@2608
   230
nipkow@3467
   231
section "map";
nipkow@3467
   232
nipkow@4935
   233
Goal
nipkow@3465
   234
  "(!x. x : set xs --> f x = g x) --> map f xs = map g xs";
paulson@3457
   235
by (induct_tac "xs" 1);
paulson@3457
   236
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   237
bind_thm("map_ext", impI RS (allI RS (result() RS mp)));
nipkow@2608
   238
nipkow@4935
   239
Goal "map (%x. x) = (%xs. xs)";
nipkow@2608
   240
by (rtac ext 1);
nipkow@3040
   241
by (induct_tac "xs" 1);
nipkow@2608
   242
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   243
qed "map_ident";
nipkow@2608
   244
Addsimps[map_ident];
nipkow@2608
   245
nipkow@4935
   246
Goal "map f (xs@ys) = map f xs @ map f ys";
nipkow@3040
   247
by (induct_tac "xs" 1);
nipkow@2608
   248
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   249
qed "map_append";
nipkow@2608
   250
Addsimps[map_append];
nipkow@2608
   251
nipkow@4935
   252
Goalw [o_def] "map (f o g) xs = map f (map g xs)";
nipkow@3040
   253
by (induct_tac "xs" 1);
nipkow@2608
   254
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   255
qed "map_compose";
nipkow@2608
   256
Addsimps[map_compose];
nipkow@2608
   257
nipkow@4935
   258
Goal "rev(map f xs) = map f (rev xs)";
nipkow@3040
   259
by (induct_tac "xs" 1);
nipkow@2608
   260
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   261
qed "rev_map";
nipkow@2608
   262
nipkow@3589
   263
(* a congruence rule for map: *)
nipkow@4935
   264
Goal
nipkow@3589
   265
 "(xs=ys) --> (!x. x : set ys --> f x = g x) --> map f xs = map g ys";
wenzelm@4423
   266
by (rtac impI 1);
wenzelm@4423
   267
by (hyp_subst_tac 1);
wenzelm@4423
   268
by (induct_tac "ys" 1);
wenzelm@4423
   269
by (ALLGOALS Asm_simp_tac);
nipkow@3589
   270
val lemma = result();
nipkow@3589
   271
bind_thm("map_cong",impI RSN (2,allI RSN (2,lemma RS mp RS mp)));
nipkow@3589
   272
nipkow@4935
   273
Goal "(map f xs = []) = (xs = [])";
wenzelm@4423
   274
by (induct_tac "xs" 1);
wenzelm@4423
   275
by (ALLGOALS Asm_simp_tac);
nipkow@3860
   276
qed "map_is_Nil_conv";
nipkow@3860
   277
AddIffs [map_is_Nil_conv];
nipkow@3860
   278
nipkow@4935
   279
Goal "([] = map f xs) = (xs = [])";
wenzelm@4423
   280
by (induct_tac "xs" 1);
wenzelm@4423
   281
by (ALLGOALS Asm_simp_tac);
nipkow@3860
   282
qed "Nil_is_map_conv";
nipkow@3860
   283
AddIffs [Nil_is_map_conv];
nipkow@3860
   284
nipkow@3860
   285
lcp@1169
   286
(** rev **)
lcp@1169
   287
nipkow@3467
   288
section "rev";
nipkow@3467
   289
nipkow@4935
   290
Goal "rev(xs@ys) = rev(ys) @ rev(xs)";
nipkow@3040
   291
by (induct_tac "xs" 1);
nipkow@2512
   292
by (ALLGOALS Asm_simp_tac);
lcp@1169
   293
qed "rev_append";
nipkow@2512
   294
Addsimps[rev_append];
lcp@1169
   295
nipkow@4935
   296
Goal "rev(rev l) = l";
nipkow@3040
   297
by (induct_tac "l" 1);
nipkow@2512
   298
by (ALLGOALS Asm_simp_tac);
lcp@1169
   299
qed "rev_rev_ident";
nipkow@2512
   300
Addsimps[rev_rev_ident];
lcp@1169
   301
nipkow@4935
   302
Goal "(rev xs = []) = (xs = [])";
wenzelm@4423
   303
by (induct_tac "xs" 1);
wenzelm@4423
   304
by (ALLGOALS Asm_simp_tac);
nipkow@3860
   305
qed "rev_is_Nil_conv";
nipkow@3860
   306
AddIffs [rev_is_Nil_conv];
nipkow@3860
   307
nipkow@4935
   308
Goal "([] = rev xs) = (xs = [])";
wenzelm@4423
   309
by (induct_tac "xs" 1);
wenzelm@4423
   310
by (ALLGOALS Asm_simp_tac);
nipkow@3860
   311
qed "Nil_is_rev_conv";
nipkow@3860
   312
AddIffs [Nil_is_rev_conv];
nipkow@3860
   313
nipkow@4935
   314
val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
nipkow@4935
   315
by(stac (rev_rev_ident RS sym) 1);
nipkow@4935
   316
br(read_instantiate [("P","%xs. ?P(rev xs)")]list.induct)1;
nipkow@4935
   317
by(ALLGOALS Simp_tac);
nipkow@4935
   318
brs prems 1;
nipkow@4935
   319
bes prems 1;
nipkow@4935
   320
qed "rev_induct";
nipkow@4935
   321
nipkow@4935
   322
Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
nipkow@4935
   323
by(res_inst_tac [("xs","xs")] rev_induct 1);
nipkow@4935
   324
by(ALLGOALS Asm_simp_tac);
nipkow@4935
   325
bind_thm ("rev_exhaust",
nipkow@4935
   326
  impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp)))));
nipkow@4935
   327
nipkow@2608
   328
clasohm@923
   329
(** mem **)
clasohm@923
   330
nipkow@3467
   331
section "mem";
nipkow@3467
   332
nipkow@4935
   333
Goal "x mem (xs@ys) = (x mem xs | x mem ys)";
nipkow@3040
   334
by (induct_tac "xs" 1);
nipkow@4686
   335
by (ALLGOALS Asm_simp_tac);
clasohm@923
   336
qed "mem_append";
nipkow@2512
   337
Addsimps[mem_append];
clasohm@923
   338
nipkow@4935
   339
Goal "x mem [x:xs. P(x)] = (x mem xs & P(x))";
nipkow@3040
   340
by (induct_tac "xs" 1);
nipkow@4686
   341
by (ALLGOALS Asm_simp_tac);
clasohm@923
   342
qed "mem_filter";
nipkow@2512
   343
Addsimps[mem_filter];
clasohm@923
   344
nipkow@3465
   345
(** set **)
paulson@1812
   346
nipkow@3467
   347
section "set";
nipkow@3467
   348
nipkow@4935
   349
Goal "set (xs@ys) = (set xs Un set ys)";
nipkow@3040
   350
by (induct_tac "xs" 1);
paulson@1812
   351
by (ALLGOALS Asm_simp_tac);
paulson@3647
   352
qed "set_append";
paulson@3647
   353
Addsimps[set_append];
paulson@1812
   354
nipkow@4935
   355
Goal "(x mem xs) = (x: set xs)";
nipkow@3040
   356
by (induct_tac "xs" 1);
nipkow@4686
   357
by (ALLGOALS Asm_simp_tac);
paulson@2891
   358
by (Blast_tac 1);
paulson@3647
   359
qed "set_mem_eq";
paulson@1812
   360
nipkow@4935
   361
Goal "set l <= set (x#l)";
paulson@1936
   362
by (Simp_tac 1);
paulson@2891
   363
by (Blast_tac 1);
paulson@3647
   364
qed "set_subset_Cons";
paulson@1936
   365
nipkow@4935
   366
Goal "(set xs = {}) = (xs = [])";
paulson@3457
   367
by (induct_tac "xs" 1);
paulson@3457
   368
by (ALLGOALS Asm_simp_tac);
paulson@3647
   369
qed "set_empty";
paulson@3647
   370
Addsimps [set_empty];
nipkow@2608
   371
nipkow@4935
   372
Goal "set(rev xs) = set(xs)";
paulson@3457
   373
by (induct_tac "xs" 1);
paulson@3457
   374
by (ALLGOALS Asm_simp_tac);
paulson@3647
   375
qed "set_rev";
paulson@3647
   376
Addsimps [set_rev];
nipkow@2608
   377
nipkow@4935
   378
Goal "set(map f xs) = f``(set xs)";
paulson@3457
   379
by (induct_tac "xs" 1);
paulson@3457
   380
by (ALLGOALS Asm_simp_tac);
paulson@3647
   381
qed "set_map";
paulson@3647
   382
Addsimps [set_map];
nipkow@2608
   383
nipkow@4935
   384
Goal "(x : set(filter P xs)) = (x : set xs & P x)";
nipkow@4605
   385
by (induct_tac "xs" 1);
nipkow@4686
   386
by (ALLGOALS Asm_simp_tac);
nipkow@4605
   387
by(Blast_tac 1);
nipkow@4605
   388
qed "in_set_filter";
nipkow@4605
   389
Addsimps [in_set_filter];
nipkow@4605
   390
paulson@1812
   391
clasohm@923
   392
(** list_all **)
clasohm@923
   393
nipkow@3467
   394
section "list_all";
nipkow@3467
   395
nipkow@4935
   396
Goal "list_all (%x. True) xs = True";
nipkow@3040
   397
by (induct_tac "xs" 1);
clasohm@1264
   398
by (ALLGOALS Asm_simp_tac);
clasohm@923
   399
qed "list_all_True";
nipkow@2512
   400
Addsimps [list_all_True];
clasohm@923
   401
nipkow@4935
   402
Goal "list_all p (xs@ys) = (list_all p xs & list_all p ys)";
nipkow@3040
   403
by (induct_tac "xs" 1);
clasohm@1264
   404
by (ALLGOALS Asm_simp_tac);
nipkow@2512
   405
qed "list_all_append";
nipkow@2512
   406
Addsimps [list_all_append];
clasohm@923
   407
nipkow@4935
   408
Goal "list_all P xs = (!x. x mem xs --> P(x))";
nipkow@3040
   409
by (induct_tac "xs" 1);
nipkow@4686
   410
by (ALLGOALS Asm_simp_tac);
paulson@2891
   411
by (Blast_tac 1);
clasohm@923
   412
qed "list_all_mem_conv";
clasohm@923
   413
clasohm@923
   414
nipkow@2608
   415
(** filter **)
clasohm@923
   416
nipkow@3467
   417
section "filter";
nipkow@3467
   418
nipkow@4935
   419
Goal "filter P (xs@ys) = filter P xs @ filter P ys";
paulson@3457
   420
by (induct_tac "xs" 1);
nipkow@4686
   421
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   422
qed "filter_append";
nipkow@2608
   423
Addsimps [filter_append];
nipkow@2608
   424
nipkow@4935
   425
Goal "filter (%x. True) xs = xs";
nipkow@4605
   426
by (induct_tac "xs" 1);
nipkow@4605
   427
by (ALLGOALS Asm_simp_tac);
nipkow@4605
   428
qed "filter_True";
nipkow@4605
   429
Addsimps [filter_True];
nipkow@4605
   430
nipkow@4935
   431
Goal "filter (%x. False) xs = []";
nipkow@4605
   432
by (induct_tac "xs" 1);
nipkow@4605
   433
by (ALLGOALS Asm_simp_tac);
nipkow@4605
   434
qed "filter_False";
nipkow@4605
   435
Addsimps [filter_False];
nipkow@4605
   436
nipkow@4935
   437
Goal "length (filter P xs) <= length xs";
paulson@3457
   438
by (induct_tac "xs" 1);
nipkow@4686
   439
by (ALLGOALS Asm_simp_tac);
nipkow@4605
   440
qed "length_filter";
paulson@3383
   441
nipkow@2608
   442
nipkow@2608
   443
(** concat **)
nipkow@2608
   444
nipkow@3467
   445
section "concat";
nipkow@3467
   446
nipkow@4935
   447
Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
nipkow@3040
   448
by (induct_tac "xs" 1);
clasohm@1264
   449
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   450
qed"concat_append";
nipkow@2608
   451
Addsimps [concat_append];
nipkow@2512
   452
nipkow@4935
   453
Goal "(concat xss = []) = (!xs:set xss. xs=[])";
wenzelm@4423
   454
by (induct_tac "xss" 1);
wenzelm@4423
   455
by (ALLGOALS Asm_simp_tac);
nipkow@3896
   456
qed "concat_eq_Nil_conv";
nipkow@3896
   457
AddIffs [concat_eq_Nil_conv];
nipkow@3896
   458
nipkow@4935
   459
Goal "([] = concat xss) = (!xs:set xss. xs=[])";
wenzelm@4423
   460
by (induct_tac "xss" 1);
wenzelm@4423
   461
by (ALLGOALS Asm_simp_tac);
nipkow@3896
   462
qed "Nil_eq_concat_conv";
nipkow@3896
   463
AddIffs [Nil_eq_concat_conv];
nipkow@3896
   464
nipkow@4935
   465
Goal  "set(concat xs) = Union(set `` set xs)";
nipkow@3467
   466
by (induct_tac "xs" 1);
nipkow@3467
   467
by (ALLGOALS Asm_simp_tac);
paulson@3647
   468
qed"set_concat";
paulson@3647
   469
Addsimps [set_concat];
nipkow@3467
   470
nipkow@4935
   471
Goal "map f (concat xs) = concat (map (map f) xs)"; 
nipkow@3467
   472
by (induct_tac "xs" 1);
nipkow@3467
   473
by (ALLGOALS Asm_simp_tac);
nipkow@3467
   474
qed "map_concat";
nipkow@3467
   475
nipkow@4935
   476
Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
nipkow@3467
   477
by (induct_tac "xs" 1);
nipkow@3467
   478
by (ALLGOALS Asm_simp_tac);
nipkow@3467
   479
qed"filter_concat"; 
nipkow@3467
   480
nipkow@4935
   481
Goal "rev(concat xs) = concat (map rev (rev xs))";
nipkow@3467
   482
by (induct_tac "xs" 1);
nipkow@2512
   483
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   484
qed "rev_concat";
clasohm@923
   485
clasohm@923
   486
(** nth **)
clasohm@923
   487
nipkow@3467
   488
section "nth";
nipkow@3467
   489
nipkow@4935
   490
Goal
nipkow@4502
   491
  "!xs. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
paulson@3457
   492
by (nat_ind_tac "n" 1);
paulson@3457
   493
 by (Asm_simp_tac 1);
paulson@3457
   494
 by (rtac allI 1);
paulson@3457
   495
 by (exhaust_tac "xs" 1);
paulson@3457
   496
  by (ALLGOALS Asm_simp_tac);
nipkow@2608
   497
qed_spec_mp "nth_append";
nipkow@2608
   498
nipkow@4935
   499
Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
nipkow@3040
   500
by (induct_tac "xs" 1);
nipkow@1301
   501
(* case [] *)
nipkow@1301
   502
by (Asm_full_simp_tac 1);
nipkow@1301
   503
(* case x#xl *)
nipkow@1301
   504
by (rtac allI 1);
nipkow@1301
   505
by (nat_ind_tac "n" 1);
nipkow@1301
   506
by (ALLGOALS Asm_full_simp_tac);
nipkow@1485
   507
qed_spec_mp "nth_map";
nipkow@1301
   508
Addsimps [nth_map];
nipkow@1301
   509
nipkow@4935
   510
Goal "!n. n < length xs --> list_all P xs --> P(xs!n)";
nipkow@3040
   511
by (induct_tac "xs" 1);
nipkow@1301
   512
(* case [] *)
nipkow@1301
   513
by (Simp_tac 1);
nipkow@1301
   514
(* case x#xl *)
nipkow@1301
   515
by (rtac allI 1);
nipkow@1301
   516
by (nat_ind_tac "n" 1);
nipkow@1301
   517
by (ALLGOALS Asm_full_simp_tac);
nipkow@1485
   518
qed_spec_mp "list_all_nth";
nipkow@1301
   519
nipkow@4935
   520
Goal "!n. n < length xs --> xs!n mem xs";
nipkow@3040
   521
by (induct_tac "xs" 1);
nipkow@1301
   522
(* case [] *)
nipkow@1301
   523
by (Simp_tac 1);
nipkow@1301
   524
(* case x#xl *)
nipkow@1301
   525
by (rtac allI 1);
nipkow@1301
   526
by (nat_ind_tac "n" 1);
nipkow@1301
   527
(* case 0 *)
nipkow@1301
   528
by (Asm_full_simp_tac 1);
nipkow@1301
   529
(* case Suc x *)
nipkow@4686
   530
by (Asm_full_simp_tac 1);
nipkow@1485
   531
qed_spec_mp "nth_mem";
nipkow@1301
   532
Addsimps [nth_mem];
nipkow@1301
   533
nipkow@5077
   534
(** list update **)
nipkow@5077
   535
nipkow@5077
   536
section "list update";
nipkow@5077
   537
nipkow@5077
   538
Goal "!i. length(xs[i:=x]) = length xs";
nipkow@5077
   539
by (induct_tac "xs" 1);
nipkow@5077
   540
by (Simp_tac 1);
nipkow@5077
   541
by (asm_full_simp_tac (simpset() addsplits [split_nat_case]) 1);
nipkow@5077
   542
qed_spec_mp "length_list_update";
nipkow@5077
   543
Addsimps [length_list_update];
nipkow@5077
   544
nipkow@5077
   545
nipkow@3896
   546
(** last & butlast **)
nipkow@1327
   547
nipkow@4935
   548
Goal "last(xs@[x]) = x";
wenzelm@4423
   549
by (induct_tac "xs" 1);
nipkow@4686
   550
by (ALLGOALS Asm_simp_tac);
nipkow@3896
   551
qed "last_snoc";
nipkow@3896
   552
Addsimps [last_snoc];
nipkow@3896
   553
nipkow@4935
   554
Goal "butlast(xs@[x]) = xs";
wenzelm@4423
   555
by (induct_tac "xs" 1);
nipkow@4686
   556
by (ALLGOALS Asm_simp_tac);
nipkow@3896
   557
qed "butlast_snoc";
nipkow@3896
   558
Addsimps [butlast_snoc];
nipkow@3896
   559
nipkow@4935
   560
Goal "length(butlast xs) = length xs - 1";
nipkow@4935
   561
by (res_inst_tac [("xs","xs")] rev_induct 1);
nipkow@4686
   562
by (ALLGOALS Asm_simp_tac);
nipkow@4643
   563
qed "length_butlast";
nipkow@4643
   564
Addsimps [length_butlast];
nipkow@4643
   565
nipkow@4935
   566
Goal
nipkow@3896
   567
  "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
wenzelm@4423
   568
by (induct_tac "xs" 1);
nipkow@4686
   569
by (ALLGOALS Asm_simp_tac);
nipkow@3896
   570
qed_spec_mp "butlast_append";
nipkow@3896
   571
nipkow@4935
   572
Goal "x:set(butlast xs) --> x:set xs";
wenzelm@4423
   573
by (induct_tac "xs" 1);
nipkow@4686
   574
by (ALLGOALS Asm_simp_tac);
nipkow@3896
   575
qed_spec_mp "in_set_butlastD";
nipkow@3896
   576
nipkow@5043
   577
Goal "x:set(butlast xs) ==> x:set(butlast(xs@ys))";
nipkow@4686
   578
by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
wenzelm@4423
   579
by (blast_tac (claset() addDs [in_set_butlastD]) 1);
nipkow@3896
   580
qed "in_set_butlast_appendI1";
nipkow@3896
   581
nipkow@5043
   582
Goal "x:set(butlast ys) ==> x:set(butlast(xs@ys))";
nipkow@4686
   583
by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
wenzelm@4423
   584
by (Clarify_tac 1);
wenzelm@4423
   585
by (Full_simp_tac 1);
nipkow@3896
   586
qed "in_set_butlast_appendI2";
nipkow@3902
   587
nipkow@2608
   588
(** take  & drop **)
nipkow@2608
   589
section "take & drop";
nipkow@1327
   590
nipkow@4935
   591
Goal "take 0 xs = []";
nipkow@3040
   592
by (induct_tac "xs" 1);
nipkow@1419
   593
by (ALLGOALS Asm_simp_tac);
nipkow@1327
   594
qed "take_0";
nipkow@1327
   595
nipkow@4935
   596
Goal "drop 0 xs = xs";
nipkow@3040
   597
by (induct_tac "xs" 1);
nipkow@2608
   598
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   599
qed "drop_0";
nipkow@2608
   600
nipkow@4935
   601
Goal "take (Suc n) (x#xs) = x # take n xs";
paulson@1552
   602
by (Simp_tac 1);
nipkow@1419
   603
qed "take_Suc_Cons";
nipkow@1327
   604
nipkow@4935
   605
Goal "drop (Suc n) (x#xs) = drop n xs";
nipkow@2608
   606
by (Simp_tac 1);
nipkow@2608
   607
qed "drop_Suc_Cons";
nipkow@2608
   608
nipkow@2608
   609
Delsimps [take_Cons,drop_Cons];
nipkow@2608
   610
Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
nipkow@2608
   611
nipkow@4935
   612
Goal "!xs. length(take n xs) = min (length xs) n";
paulson@3457
   613
by (nat_ind_tac "n" 1);
paulson@3457
   614
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   615
by (rtac allI 1);
paulson@3457
   616
by (exhaust_tac "xs" 1);
paulson@3457
   617
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   618
qed_spec_mp "length_take";
nipkow@2608
   619
Addsimps [length_take];
clasohm@923
   620
nipkow@4935
   621
Goal "!xs. length(drop n xs) = (length xs - n)";
paulson@3457
   622
by (nat_ind_tac "n" 1);
paulson@3457
   623
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   624
by (rtac allI 1);
paulson@3457
   625
by (exhaust_tac "xs" 1);
paulson@3457
   626
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   627
qed_spec_mp "length_drop";
nipkow@2608
   628
Addsimps [length_drop];
nipkow@2608
   629
nipkow@4935
   630
Goal "!xs. length xs <= n --> take n xs = xs";
paulson@3457
   631
by (nat_ind_tac "n" 1);
paulson@3457
   632
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   633
by (rtac allI 1);
paulson@3457
   634
by (exhaust_tac "xs" 1);
paulson@3457
   635
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   636
qed_spec_mp "take_all";
clasohm@923
   637
nipkow@4935
   638
Goal "!xs. length xs <= n --> drop n xs = []";
paulson@3457
   639
by (nat_ind_tac "n" 1);
paulson@3457
   640
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   641
by (rtac allI 1);
paulson@3457
   642
by (exhaust_tac "xs" 1);
paulson@3457
   643
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   644
qed_spec_mp "drop_all";
nipkow@2608
   645
nipkow@4935
   646
Goal 
nipkow@2608
   647
  "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
paulson@3457
   648
by (nat_ind_tac "n" 1);
paulson@3457
   649
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   650
by (rtac allI 1);
paulson@3457
   651
by (exhaust_tac "xs" 1);
paulson@3457
   652
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   653
qed_spec_mp "take_append";
nipkow@2608
   654
Addsimps [take_append];
nipkow@2608
   655
nipkow@4935
   656
Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
paulson@3457
   657
by (nat_ind_tac "n" 1);
paulson@3457
   658
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   659
by (rtac allI 1);
paulson@3457
   660
by (exhaust_tac "xs" 1);
paulson@3457
   661
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   662
qed_spec_mp "drop_append";
nipkow@2608
   663
Addsimps [drop_append];
nipkow@2608
   664
nipkow@4935
   665
Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
paulson@3457
   666
by (nat_ind_tac "m" 1);
paulson@3457
   667
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   668
by (rtac allI 1);
paulson@3457
   669
by (exhaust_tac "xs" 1);
paulson@3457
   670
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   671
by (rtac allI 1);
paulson@3457
   672
by (exhaust_tac "n" 1);
paulson@3457
   673
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   674
qed_spec_mp "take_take";
nipkow@2608
   675
nipkow@4935
   676
Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
paulson@3457
   677
by (nat_ind_tac "m" 1);
paulson@3457
   678
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   679
by (rtac allI 1);
paulson@3457
   680
by (exhaust_tac "xs" 1);
paulson@3457
   681
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   682
qed_spec_mp "drop_drop";
clasohm@923
   683
nipkow@4935
   684
Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
paulson@3457
   685
by (nat_ind_tac "m" 1);
paulson@3457
   686
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   687
by (rtac allI 1);
paulson@3457
   688
by (exhaust_tac "xs" 1);
paulson@3457
   689
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   690
qed_spec_mp "take_drop";
nipkow@2608
   691
nipkow@4935
   692
Goal "!xs. take n (map f xs) = map f (take n xs)"; 
paulson@3457
   693
by (nat_ind_tac "n" 1);
paulson@3457
   694
by (ALLGOALS Asm_simp_tac);
paulson@3457
   695
by (rtac allI 1);
paulson@3457
   696
by (exhaust_tac "xs" 1);
paulson@3457
   697
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   698
qed_spec_mp "take_map"; 
nipkow@2608
   699
nipkow@4935
   700
Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
paulson@3457
   701
by (nat_ind_tac "n" 1);
paulson@3457
   702
by (ALLGOALS Asm_simp_tac);
paulson@3457
   703
by (rtac allI 1);
paulson@3457
   704
by (exhaust_tac "xs" 1);
paulson@3457
   705
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   706
qed_spec_mp "drop_map";
nipkow@2608
   707
nipkow@4935
   708
Goal "!n i. i < n --> (take n xs)!i = xs!i";
paulson@3457
   709
by (induct_tac "xs" 1);
paulson@3457
   710
 by (ALLGOALS Asm_simp_tac);
paulson@3708
   711
by (Clarify_tac 1);
paulson@3457
   712
by (exhaust_tac "n" 1);
paulson@3457
   713
 by (Blast_tac 1);
paulson@3457
   714
by (exhaust_tac "i" 1);
paulson@3457
   715
by (ALLGOALS Asm_full_simp_tac);
nipkow@2608
   716
qed_spec_mp "nth_take";
nipkow@2608
   717
Addsimps [nth_take];
clasohm@923
   718
nipkow@4935
   719
Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
paulson@3457
   720
by (nat_ind_tac "n" 1);
paulson@3457
   721
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   722
by (rtac allI 1);
paulson@3457
   723
by (exhaust_tac "xs" 1);
paulson@3457
   724
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   725
qed_spec_mp "nth_drop";
nipkow@2608
   726
Addsimps [nth_drop];
nipkow@2608
   727
nipkow@2608
   728
(** takeWhile & dropWhile **)
nipkow@2608
   729
nipkow@3467
   730
section "takeWhile & dropWhile";
nipkow@3467
   731
nipkow@4935
   732
Goal "takeWhile P xs @ dropWhile P xs = xs";
nipkow@3586
   733
by (induct_tac "xs" 1);
nipkow@4686
   734
by (ALLGOALS Asm_full_simp_tac);
nipkow@3586
   735
qed "takeWhile_dropWhile_id";
nipkow@3586
   736
Addsimps [takeWhile_dropWhile_id];
nipkow@3586
   737
nipkow@4935
   738
Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
paulson@3457
   739
by (induct_tac "xs" 1);
nipkow@4686
   740
by (ALLGOALS Asm_full_simp_tac);
paulson@3457
   741
by (Blast_tac 1);
nipkow@2608
   742
bind_thm("takeWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   743
Addsimps [takeWhile_append1];
clasohm@923
   744
nipkow@4935
   745
Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
paulson@3457
   746
by (induct_tac "xs" 1);
nipkow@4686
   747
by (ALLGOALS Asm_full_simp_tac);
nipkow@2608
   748
bind_thm("takeWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   749
Addsimps [takeWhile_append2];
lcp@1169
   750
nipkow@4935
   751
Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
paulson@3457
   752
by (induct_tac "xs" 1);
nipkow@4686
   753
by (ALLGOALS Asm_full_simp_tac);
paulson@3457
   754
by (Blast_tac 1);
nipkow@2608
   755
bind_thm("dropWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   756
Addsimps [dropWhile_append1];
nipkow@2608
   757
nipkow@4935
   758
Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
paulson@3457
   759
by (induct_tac "xs" 1);
nipkow@4686
   760
by (ALLGOALS Asm_full_simp_tac);
nipkow@2608
   761
bind_thm("dropWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   762
Addsimps [dropWhile_append2];
nipkow@2608
   763
nipkow@4935
   764
Goal "x:set(takeWhile P xs) --> x:set xs & P x";
paulson@3457
   765
by (induct_tac "xs" 1);
nipkow@4686
   766
by (ALLGOALS Asm_full_simp_tac);
paulson@3647
   767
qed_spec_mp"set_take_whileD";
nipkow@2608
   768
oheimb@4132
   769
qed_goal "zip_Nil_Nil"   thy "zip []     []     = []" (K [Simp_tac 1]);
oheimb@4132
   770
qed_goal "zip_Cons_Cons" thy "zip (x#xs) (y#ys) = (x,y)#zip xs ys" 
oheimb@4132
   771
						      (K [Simp_tac 1]);
nipkow@4605
   772
nipkow@4605
   773
(** nodups & remdups **)
nipkow@4605
   774
section "nodups & remdups";
nipkow@4605
   775
nipkow@4935
   776
Goal "set(remdups xs) = set xs";
nipkow@4605
   777
by (induct_tac "xs" 1);
nipkow@4605
   778
 by (Simp_tac 1);
nipkow@4686
   779
by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
nipkow@4605
   780
qed "set_remdups";
nipkow@4605
   781
Addsimps [set_remdups];
nipkow@4605
   782
nipkow@4935
   783
Goal "nodups(remdups xs)";
nipkow@4605
   784
by (induct_tac "xs" 1);
nipkow@4686
   785
by (ALLGOALS Asm_full_simp_tac);
nipkow@4605
   786
qed "nodups_remdups";
nipkow@4605
   787
nipkow@4935
   788
Goal "nodups xs --> nodups (filter P xs)";
nipkow@4605
   789
by (induct_tac "xs" 1);
nipkow@4686
   790
by (ALLGOALS Asm_full_simp_tac);
nipkow@4605
   791
qed_spec_mp "nodups_filter";
nipkow@4605
   792
nipkow@3589
   793
(** replicate **)
nipkow@3589
   794
section "replicate";
nipkow@3589
   795
nipkow@4935
   796
Goal "set(replicate (Suc n) x) = {x}";
wenzelm@4423
   797
by (induct_tac "n" 1);
wenzelm@4423
   798
by (ALLGOALS Asm_full_simp_tac);
nipkow@3589
   799
val lemma = result();
nipkow@3589
   800
nipkow@5043
   801
Goal "n ~= 0 ==> set(replicate n x) = {x}";
wenzelm@4423
   802
by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
nipkow@3589
   803
qed "set_replicate";
nipkow@3589
   804
Addsimps [set_replicate];