src/HOL/List.ML
author nipkow
Mon May 18 17:31:49 1998 +0200 (1998-05-18)
changeset 4935 1694e2daef8f
parent 4911 6195e4468c54
child 5043 3fdc881e8ff9
permissions -rw-r--r--
Cleaned up and simplified etc.
snoc_induct/exhaust -> rev_induct_exhaust.
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@4935
    31
Goalw lists.defs "!!A B. 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@4935
    40
Goal "!!l. 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@4935
    91
Goal "!!xs. 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@4935
   212
Goal "!!xs. 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@4935
   222
Goal "!!xs. 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 "set(map f xs) = f``(set xs)";
nipkow@4605
   385
by (induct_tac "xs" 1);
nipkow@4605
   386
by (ALLGOALS Asm_simp_tac);
nipkow@4605
   387
qed "set_map";
nipkow@4605
   388
Addsimps [set_map];
nipkow@4605
   389
nipkow@4935
   390
Goal "(x : set(filter P xs)) = (x : set xs & P x)";
nipkow@4605
   391
by (induct_tac "xs" 1);
nipkow@4686
   392
by (ALLGOALS Asm_simp_tac);
nipkow@4605
   393
by(Blast_tac 1);
nipkow@4605
   394
qed "in_set_filter";
nipkow@4605
   395
Addsimps [in_set_filter];
nipkow@4605
   396
paulson@1812
   397
clasohm@923
   398
(** list_all **)
clasohm@923
   399
nipkow@3467
   400
section "list_all";
nipkow@3467
   401
nipkow@4935
   402
Goal "list_all (%x. True) xs = True";
nipkow@3040
   403
by (induct_tac "xs" 1);
clasohm@1264
   404
by (ALLGOALS Asm_simp_tac);
clasohm@923
   405
qed "list_all_True";
nipkow@2512
   406
Addsimps [list_all_True];
clasohm@923
   407
nipkow@4935
   408
Goal "list_all p (xs@ys) = (list_all p xs & list_all p ys)";
nipkow@3040
   409
by (induct_tac "xs" 1);
clasohm@1264
   410
by (ALLGOALS Asm_simp_tac);
nipkow@2512
   411
qed "list_all_append";
nipkow@2512
   412
Addsimps [list_all_append];
clasohm@923
   413
nipkow@4935
   414
Goal "list_all P xs = (!x. x mem xs --> P(x))";
nipkow@3040
   415
by (induct_tac "xs" 1);
nipkow@4686
   416
by (ALLGOALS Asm_simp_tac);
paulson@2891
   417
by (Blast_tac 1);
clasohm@923
   418
qed "list_all_mem_conv";
clasohm@923
   419
clasohm@923
   420
nipkow@2608
   421
(** filter **)
clasohm@923
   422
nipkow@3467
   423
section "filter";
nipkow@3467
   424
nipkow@4935
   425
Goal "filter P (xs@ys) = filter P xs @ filter P ys";
paulson@3457
   426
by (induct_tac "xs" 1);
nipkow@4686
   427
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   428
qed "filter_append";
nipkow@2608
   429
Addsimps [filter_append];
nipkow@2608
   430
nipkow@4935
   431
Goal "filter (%x. True) xs = xs";
nipkow@4605
   432
by (induct_tac "xs" 1);
nipkow@4605
   433
by (ALLGOALS Asm_simp_tac);
nipkow@4605
   434
qed "filter_True";
nipkow@4605
   435
Addsimps [filter_True];
nipkow@4605
   436
nipkow@4935
   437
Goal "filter (%x. False) xs = []";
nipkow@4605
   438
by (induct_tac "xs" 1);
nipkow@4605
   439
by (ALLGOALS Asm_simp_tac);
nipkow@4605
   440
qed "filter_False";
nipkow@4605
   441
Addsimps [filter_False];
nipkow@4605
   442
nipkow@4935
   443
Goal "length (filter P xs) <= length xs";
paulson@3457
   444
by (induct_tac "xs" 1);
nipkow@4686
   445
by (ALLGOALS Asm_simp_tac);
nipkow@4605
   446
qed "length_filter";
paulson@3383
   447
nipkow@2608
   448
nipkow@2608
   449
(** concat **)
nipkow@2608
   450
nipkow@3467
   451
section "concat";
nipkow@3467
   452
nipkow@4935
   453
Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
nipkow@3040
   454
by (induct_tac "xs" 1);
clasohm@1264
   455
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   456
qed"concat_append";
nipkow@2608
   457
Addsimps [concat_append];
nipkow@2512
   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 "concat_eq_Nil_conv";
nipkow@3896
   463
AddIffs [concat_eq_Nil_conv];
nipkow@3896
   464
nipkow@4935
   465
Goal "([] = concat xss) = (!xs:set xss. xs=[])";
wenzelm@4423
   466
by (induct_tac "xss" 1);
wenzelm@4423
   467
by (ALLGOALS Asm_simp_tac);
nipkow@3896
   468
qed "Nil_eq_concat_conv";
nipkow@3896
   469
AddIffs [Nil_eq_concat_conv];
nipkow@3896
   470
nipkow@4935
   471
Goal  "set(concat xs) = Union(set `` set xs)";
nipkow@3467
   472
by (induct_tac "xs" 1);
nipkow@3467
   473
by (ALLGOALS Asm_simp_tac);
paulson@3647
   474
qed"set_concat";
paulson@3647
   475
Addsimps [set_concat];
nipkow@3467
   476
nipkow@4935
   477
Goal "map f (concat xs) = concat (map (map f) xs)"; 
nipkow@3467
   478
by (induct_tac "xs" 1);
nipkow@3467
   479
by (ALLGOALS Asm_simp_tac);
nipkow@3467
   480
qed "map_concat";
nipkow@3467
   481
nipkow@4935
   482
Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
nipkow@3467
   483
by (induct_tac "xs" 1);
nipkow@3467
   484
by (ALLGOALS Asm_simp_tac);
nipkow@3467
   485
qed"filter_concat"; 
nipkow@3467
   486
nipkow@4935
   487
Goal "rev(concat xs) = concat (map rev (rev xs))";
nipkow@3467
   488
by (induct_tac "xs" 1);
nipkow@2512
   489
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   490
qed "rev_concat";
clasohm@923
   491
clasohm@923
   492
(** nth **)
clasohm@923
   493
nipkow@3467
   494
section "nth";
nipkow@3467
   495
nipkow@4935
   496
Goal
nipkow@4502
   497
  "!xs. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
paulson@3457
   498
by (nat_ind_tac "n" 1);
paulson@3457
   499
 by (Asm_simp_tac 1);
paulson@3457
   500
 by (rtac allI 1);
paulson@3457
   501
 by (exhaust_tac "xs" 1);
paulson@3457
   502
  by (ALLGOALS Asm_simp_tac);
nipkow@2608
   503
qed_spec_mp "nth_append";
nipkow@2608
   504
nipkow@4935
   505
Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
nipkow@3040
   506
by (induct_tac "xs" 1);
nipkow@1301
   507
(* case [] *)
nipkow@1301
   508
by (Asm_full_simp_tac 1);
nipkow@1301
   509
(* case x#xl *)
nipkow@1301
   510
by (rtac allI 1);
nipkow@1301
   511
by (nat_ind_tac "n" 1);
nipkow@1301
   512
by (ALLGOALS Asm_full_simp_tac);
nipkow@1485
   513
qed_spec_mp "nth_map";
nipkow@1301
   514
Addsimps [nth_map];
nipkow@1301
   515
nipkow@4935
   516
Goal "!n. n < length xs --> list_all P xs --> P(xs!n)";
nipkow@3040
   517
by (induct_tac "xs" 1);
nipkow@1301
   518
(* case [] *)
nipkow@1301
   519
by (Simp_tac 1);
nipkow@1301
   520
(* case x#xl *)
nipkow@1301
   521
by (rtac allI 1);
nipkow@1301
   522
by (nat_ind_tac "n" 1);
nipkow@1301
   523
by (ALLGOALS Asm_full_simp_tac);
nipkow@1485
   524
qed_spec_mp "list_all_nth";
nipkow@1301
   525
nipkow@4935
   526
Goal "!n. n < length xs --> xs!n mem xs";
nipkow@3040
   527
by (induct_tac "xs" 1);
nipkow@1301
   528
(* case [] *)
nipkow@1301
   529
by (Simp_tac 1);
nipkow@1301
   530
(* case x#xl *)
nipkow@1301
   531
by (rtac allI 1);
nipkow@1301
   532
by (nat_ind_tac "n" 1);
nipkow@1301
   533
(* case 0 *)
nipkow@1301
   534
by (Asm_full_simp_tac 1);
nipkow@1301
   535
(* case Suc x *)
nipkow@4686
   536
by (Asm_full_simp_tac 1);
nipkow@1485
   537
qed_spec_mp "nth_mem";
nipkow@1301
   538
Addsimps [nth_mem];
nipkow@1301
   539
nipkow@3896
   540
(** last & butlast **)
nipkow@1327
   541
nipkow@4935
   542
Goal "last(xs@[x]) = x";
wenzelm@4423
   543
by (induct_tac "xs" 1);
nipkow@4686
   544
by (ALLGOALS Asm_simp_tac);
nipkow@3896
   545
qed "last_snoc";
nipkow@3896
   546
Addsimps [last_snoc];
nipkow@3896
   547
nipkow@4935
   548
Goal "butlast(xs@[x]) = xs";
wenzelm@4423
   549
by (induct_tac "xs" 1);
nipkow@4686
   550
by (ALLGOALS Asm_simp_tac);
nipkow@3896
   551
qed "butlast_snoc";
nipkow@3896
   552
Addsimps [butlast_snoc];
nipkow@3896
   553
nipkow@4935
   554
Goal "length(butlast xs) = length xs - 1";
nipkow@4935
   555
by (res_inst_tac [("xs","xs")] rev_induct 1);
nipkow@4686
   556
by (ALLGOALS Asm_simp_tac);
nipkow@4643
   557
qed "length_butlast";
nipkow@4643
   558
Addsimps [length_butlast];
nipkow@4643
   559
nipkow@4935
   560
Goal
nipkow@3896
   561
  "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
wenzelm@4423
   562
by (induct_tac "xs" 1);
nipkow@4686
   563
by (ALLGOALS Asm_simp_tac);
nipkow@3896
   564
qed_spec_mp "butlast_append";
nipkow@3896
   565
nipkow@4935
   566
Goal "x:set(butlast xs) --> x:set xs";
wenzelm@4423
   567
by (induct_tac "xs" 1);
nipkow@4686
   568
by (ALLGOALS Asm_simp_tac);
nipkow@3896
   569
qed_spec_mp "in_set_butlastD";
nipkow@3896
   570
nipkow@4935
   571
Goal "!!xs. x:set(butlast xs) ==> x:set(butlast(xs@ys))";
nipkow@4686
   572
by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
wenzelm@4423
   573
by (blast_tac (claset() addDs [in_set_butlastD]) 1);
nipkow@3896
   574
qed "in_set_butlast_appendI1";
nipkow@3896
   575
nipkow@4935
   576
Goal "!!xs. x:set(butlast ys) ==> x:set(butlast(xs@ys))";
nipkow@4686
   577
by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
wenzelm@4423
   578
by (Clarify_tac 1);
wenzelm@4423
   579
by (Full_simp_tac 1);
nipkow@3896
   580
qed "in_set_butlast_appendI2";
nipkow@3902
   581
nipkow@2608
   582
(** take  & drop **)
nipkow@2608
   583
section "take & drop";
nipkow@1327
   584
nipkow@4935
   585
Goal "take 0 xs = []";
nipkow@3040
   586
by (induct_tac "xs" 1);
nipkow@1419
   587
by (ALLGOALS Asm_simp_tac);
nipkow@1327
   588
qed "take_0";
nipkow@1327
   589
nipkow@4935
   590
Goal "drop 0 xs = xs";
nipkow@3040
   591
by (induct_tac "xs" 1);
nipkow@2608
   592
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   593
qed "drop_0";
nipkow@2608
   594
nipkow@4935
   595
Goal "take (Suc n) (x#xs) = x # take n xs";
paulson@1552
   596
by (Simp_tac 1);
nipkow@1419
   597
qed "take_Suc_Cons";
nipkow@1327
   598
nipkow@4935
   599
Goal "drop (Suc n) (x#xs) = drop n xs";
nipkow@2608
   600
by (Simp_tac 1);
nipkow@2608
   601
qed "drop_Suc_Cons";
nipkow@2608
   602
nipkow@2608
   603
Delsimps [take_Cons,drop_Cons];
nipkow@2608
   604
Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
nipkow@2608
   605
nipkow@4935
   606
Goal "!xs. length(take n xs) = min (length xs) n";
paulson@3457
   607
by (nat_ind_tac "n" 1);
paulson@3457
   608
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   609
by (rtac allI 1);
paulson@3457
   610
by (exhaust_tac "xs" 1);
paulson@3457
   611
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   612
qed_spec_mp "length_take";
nipkow@2608
   613
Addsimps [length_take];
clasohm@923
   614
nipkow@4935
   615
Goal "!xs. length(drop n xs) = (length xs - n)";
paulson@3457
   616
by (nat_ind_tac "n" 1);
paulson@3457
   617
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   618
by (rtac allI 1);
paulson@3457
   619
by (exhaust_tac "xs" 1);
paulson@3457
   620
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   621
qed_spec_mp "length_drop";
nipkow@2608
   622
Addsimps [length_drop];
nipkow@2608
   623
nipkow@4935
   624
Goal "!xs. length xs <= n --> take n xs = xs";
paulson@3457
   625
by (nat_ind_tac "n" 1);
paulson@3457
   626
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   627
by (rtac allI 1);
paulson@3457
   628
by (exhaust_tac "xs" 1);
paulson@3457
   629
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   630
qed_spec_mp "take_all";
clasohm@923
   631
nipkow@4935
   632
Goal "!xs. length xs <= n --> drop n xs = []";
paulson@3457
   633
by (nat_ind_tac "n" 1);
paulson@3457
   634
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   635
by (rtac allI 1);
paulson@3457
   636
by (exhaust_tac "xs" 1);
paulson@3457
   637
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   638
qed_spec_mp "drop_all";
nipkow@2608
   639
nipkow@4935
   640
Goal 
nipkow@2608
   641
  "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
paulson@3457
   642
by (nat_ind_tac "n" 1);
paulson@3457
   643
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   644
by (rtac allI 1);
paulson@3457
   645
by (exhaust_tac "xs" 1);
paulson@3457
   646
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   647
qed_spec_mp "take_append";
nipkow@2608
   648
Addsimps [take_append];
nipkow@2608
   649
nipkow@4935
   650
Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
paulson@3457
   651
by (nat_ind_tac "n" 1);
paulson@3457
   652
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   653
by (rtac allI 1);
paulson@3457
   654
by (exhaust_tac "xs" 1);
paulson@3457
   655
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   656
qed_spec_mp "drop_append";
nipkow@2608
   657
Addsimps [drop_append];
nipkow@2608
   658
nipkow@4935
   659
Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
paulson@3457
   660
by (nat_ind_tac "m" 1);
paulson@3457
   661
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   662
by (rtac allI 1);
paulson@3457
   663
by (exhaust_tac "xs" 1);
paulson@3457
   664
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   665
by (rtac allI 1);
paulson@3457
   666
by (exhaust_tac "n" 1);
paulson@3457
   667
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   668
qed_spec_mp "take_take";
nipkow@2608
   669
nipkow@4935
   670
Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
paulson@3457
   671
by (nat_ind_tac "m" 1);
paulson@3457
   672
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   673
by (rtac allI 1);
paulson@3457
   674
by (exhaust_tac "xs" 1);
paulson@3457
   675
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   676
qed_spec_mp "drop_drop";
clasohm@923
   677
nipkow@4935
   678
Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
paulson@3457
   679
by (nat_ind_tac "m" 1);
paulson@3457
   680
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   681
by (rtac allI 1);
paulson@3457
   682
by (exhaust_tac "xs" 1);
paulson@3457
   683
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   684
qed_spec_mp "take_drop";
nipkow@2608
   685
nipkow@4935
   686
Goal "!xs. take n (map f xs) = map f (take n xs)"; 
paulson@3457
   687
by (nat_ind_tac "n" 1);
paulson@3457
   688
by (ALLGOALS Asm_simp_tac);
paulson@3457
   689
by (rtac allI 1);
paulson@3457
   690
by (exhaust_tac "xs" 1);
paulson@3457
   691
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   692
qed_spec_mp "take_map"; 
nipkow@2608
   693
nipkow@4935
   694
Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
paulson@3457
   695
by (nat_ind_tac "n" 1);
paulson@3457
   696
by (ALLGOALS Asm_simp_tac);
paulson@3457
   697
by (rtac allI 1);
paulson@3457
   698
by (exhaust_tac "xs" 1);
paulson@3457
   699
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   700
qed_spec_mp "drop_map";
nipkow@2608
   701
nipkow@4935
   702
Goal "!n i. i < n --> (take n xs)!i = xs!i";
paulson@3457
   703
by (induct_tac "xs" 1);
paulson@3457
   704
 by (ALLGOALS Asm_simp_tac);
paulson@3708
   705
by (Clarify_tac 1);
paulson@3457
   706
by (exhaust_tac "n" 1);
paulson@3457
   707
 by (Blast_tac 1);
paulson@3457
   708
by (exhaust_tac "i" 1);
paulson@3457
   709
by (ALLGOALS Asm_full_simp_tac);
nipkow@2608
   710
qed_spec_mp "nth_take";
nipkow@2608
   711
Addsimps [nth_take];
clasohm@923
   712
nipkow@4935
   713
Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
paulson@3457
   714
by (nat_ind_tac "n" 1);
paulson@3457
   715
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   716
by (rtac allI 1);
paulson@3457
   717
by (exhaust_tac "xs" 1);
paulson@3457
   718
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   719
qed_spec_mp "nth_drop";
nipkow@2608
   720
Addsimps [nth_drop];
nipkow@2608
   721
nipkow@2608
   722
(** takeWhile & dropWhile **)
nipkow@2608
   723
nipkow@3467
   724
section "takeWhile & dropWhile";
nipkow@3467
   725
nipkow@4935
   726
Goal "takeWhile P xs @ dropWhile P xs = xs";
nipkow@3586
   727
by (induct_tac "xs" 1);
nipkow@4686
   728
by (ALLGOALS Asm_full_simp_tac);
nipkow@3586
   729
qed "takeWhile_dropWhile_id";
nipkow@3586
   730
Addsimps [takeWhile_dropWhile_id];
nipkow@3586
   731
nipkow@4935
   732
Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
paulson@3457
   733
by (induct_tac "xs" 1);
nipkow@4686
   734
by (ALLGOALS Asm_full_simp_tac);
paulson@3457
   735
by (Blast_tac 1);
nipkow@2608
   736
bind_thm("takeWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   737
Addsimps [takeWhile_append1];
clasohm@923
   738
nipkow@4935
   739
Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
paulson@3457
   740
by (induct_tac "xs" 1);
nipkow@4686
   741
by (ALLGOALS Asm_full_simp_tac);
nipkow@2608
   742
bind_thm("takeWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   743
Addsimps [takeWhile_append2];
lcp@1169
   744
nipkow@4935
   745
Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
paulson@3457
   746
by (induct_tac "xs" 1);
nipkow@4686
   747
by (ALLGOALS Asm_full_simp_tac);
paulson@3457
   748
by (Blast_tac 1);
nipkow@2608
   749
bind_thm("dropWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   750
Addsimps [dropWhile_append1];
nipkow@2608
   751
nipkow@4935
   752
Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
paulson@3457
   753
by (induct_tac "xs" 1);
nipkow@4686
   754
by (ALLGOALS Asm_full_simp_tac);
nipkow@2608
   755
bind_thm("dropWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   756
Addsimps [dropWhile_append2];
nipkow@2608
   757
nipkow@4935
   758
Goal "x:set(takeWhile P xs) --> x:set xs & P x";
paulson@3457
   759
by (induct_tac "xs" 1);
nipkow@4686
   760
by (ALLGOALS Asm_full_simp_tac);
paulson@3647
   761
qed_spec_mp"set_take_whileD";
nipkow@2608
   762
oheimb@4132
   763
qed_goal "zip_Nil_Nil"   thy "zip []     []     = []" (K [Simp_tac 1]);
oheimb@4132
   764
qed_goal "zip_Cons_Cons" thy "zip (x#xs) (y#ys) = (x,y)#zip xs ys" 
oheimb@4132
   765
						      (K [Simp_tac 1]);
nipkow@4605
   766
nipkow@4605
   767
(** nodups & remdups **)
nipkow@4605
   768
section "nodups & remdups";
nipkow@4605
   769
nipkow@4935
   770
Goal "set(remdups xs) = set xs";
nipkow@4605
   771
by (induct_tac "xs" 1);
nipkow@4605
   772
 by (Simp_tac 1);
nipkow@4686
   773
by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
nipkow@4605
   774
qed "set_remdups";
nipkow@4605
   775
Addsimps [set_remdups];
nipkow@4605
   776
nipkow@4935
   777
Goal "nodups(remdups xs)";
nipkow@4605
   778
by (induct_tac "xs" 1);
nipkow@4686
   779
by (ALLGOALS Asm_full_simp_tac);
nipkow@4605
   780
qed "nodups_remdups";
nipkow@4605
   781
nipkow@4935
   782
Goal "nodups xs --> nodups (filter P xs)";
nipkow@4605
   783
by (induct_tac "xs" 1);
nipkow@4686
   784
by (ALLGOALS Asm_full_simp_tac);
nipkow@4605
   785
qed_spec_mp "nodups_filter";
nipkow@4605
   786
nipkow@3589
   787
(** replicate **)
nipkow@3589
   788
section "replicate";
nipkow@3589
   789
nipkow@4935
   790
Goal "set(replicate (Suc n) x) = {x}";
wenzelm@4423
   791
by (induct_tac "n" 1);
wenzelm@4423
   792
by (ALLGOALS Asm_full_simp_tac);
nipkow@3589
   793
val lemma = result();
nipkow@3589
   794
nipkow@4935
   795
Goal "!!n. n ~= 0 ==> set(replicate n x) = {x}";
wenzelm@4423
   796
by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
nipkow@3589
   797
qed "set_replicate";
nipkow@3589
   798
Addsimps [set_replicate];