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