src/HOL/List.ML
author nipkow
Tue Aug 05 16:21:45 1997 +0200 (1997-08-05)
changeset 3589 244daa75f890
parent 3586 2ee1ed79c802
child 3647 a64c8fbcd98f
permissions -rw-r--r--
Added function `replicate' and lemmas map_cong and set_replicate.
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@3011
     9
goal thy "!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@3011
    16
goal thy "(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
clasohm@923
    22
paulson@3468
    23
(** "lists": the list-forming operator over sets **)
paulson@3342
    24
paulson@3342
    25
goalw thy lists.defs "!!A B. A<=B ==> lists A <= lists B";
paulson@3342
    26
by (rtac lfp_mono 1);
paulson@3342
    27
by (REPEAT (ares_tac basic_monos 1));
paulson@3342
    28
qed "lists_mono";
paulson@3196
    29
paulson@3468
    30
val listsE = lists.mk_cases list.simps  "x#l : lists A";
paulson@3468
    31
AddSEs [listsE];
paulson@3468
    32
AddSIs lists.intrs;
paulson@3468
    33
paulson@3468
    34
goal thy "!!l. l: lists A ==> l: lists B --> l: lists (A Int B)";
paulson@3468
    35
by (etac lists.induct 1);
paulson@3468
    36
by (ALLGOALS Blast_tac);
paulson@3468
    37
qed_spec_mp "lists_IntI";
paulson@3468
    38
paulson@3468
    39
goal thy "lists (A Int B) = lists A Int lists B";
paulson@3468
    40
br (mono_Int RS equalityI) 1;
paulson@3468
    41
by (simp_tac (!simpset addsimps [mono_def, lists_mono]) 1);
paulson@3468
    42
by (blast_tac (!claset addSIs [lists_IntI]) 1);
paulson@3468
    43
qed "lists_Int_eq";
paulson@3468
    44
Addsimps [lists_Int_eq];
paulson@3468
    45
paulson@3196
    46
nipkow@2608
    47
(** list_case **)
nipkow@2608
    48
nipkow@3011
    49
goal thy
nipkow@2608
    50
 "P(list_case a f xs) = ((xs=[] --> P(a)) & \
paulson@2891
    51
\                        (!y ys. xs=y#ys --> P(f y ys)))";
nipkow@3040
    52
by (induct_tac "xs" 1);
nipkow@2608
    53
by (ALLGOALS Asm_simp_tac);
paulson@2891
    54
by (Blast_tac 1);
nipkow@2608
    55
qed "expand_list_case";
nipkow@2608
    56
nipkow@3011
    57
val prems = goal thy "[| P([]); !!x xs. P(x#xs) |] ==> P(xs)";
paulson@3457
    58
by (induct_tac "xs" 1);
paulson@3457
    59
by (REPEAT(resolve_tac prems 1));
nipkow@2608
    60
qed "list_cases";
nipkow@2608
    61
nipkow@3011
    62
goal thy  "(xs=[] --> P([])) & (!y ys. xs=y#ys --> P(y#ys)) --> P(xs)";
nipkow@3040
    63
by (induct_tac "xs" 1);
paulson@2891
    64
by (Blast_tac 1);
paulson@2891
    65
by (Blast_tac 1);
nipkow@2608
    66
bind_thm("list_eq_cases",
nipkow@2608
    67
  impI RSN (2,allI RSN (2,allI RSN (2,impI RS (conjI RS (result() RS mp))))));
nipkow@2608
    68
nipkow@2608
    69
clasohm@923
    70
(** @ - append **)
clasohm@923
    71
nipkow@3467
    72
section "@ - append";
nipkow@3467
    73
nipkow@3011
    74
goal thy "(xs@ys)@zs = xs@(ys@zs)";
nipkow@3040
    75
by (induct_tac "xs" 1);
clasohm@1264
    76
by (ALLGOALS Asm_simp_tac);
clasohm@923
    77
qed "append_assoc";
nipkow@2512
    78
Addsimps [append_assoc];
clasohm@923
    79
nipkow@3011
    80
goal thy "xs @ [] = xs";
nipkow@3040
    81
by (induct_tac "xs" 1);
clasohm@1264
    82
by (ALLGOALS Asm_simp_tac);
clasohm@923
    83
qed "append_Nil2";
nipkow@2512
    84
Addsimps [append_Nil2];
clasohm@923
    85
nipkow@3011
    86
goal thy "(xs@ys = []) = (xs=[] & ys=[])";
nipkow@3040
    87
by (induct_tac "xs" 1);
clasohm@1264
    88
by (ALLGOALS Asm_simp_tac);
nipkow@2608
    89
qed "append_is_Nil_conv";
nipkow@2608
    90
AddIffs [append_is_Nil_conv];
nipkow@2608
    91
nipkow@3011
    92
goal thy "([] = xs@ys) = (xs=[] & ys=[])";
nipkow@3040
    93
by (induct_tac "xs" 1);
nipkow@2608
    94
by (ALLGOALS Asm_simp_tac);
paulson@3457
    95
by (Blast_tac 1);
nipkow@2608
    96
qed "Nil_is_append_conv";
nipkow@2608
    97
AddIffs [Nil_is_append_conv];
clasohm@923
    98
nipkow@3574
    99
goal thy "(xs @ ys = xs) = (ys=[])";
nipkow@3574
   100
by (induct_tac "xs" 1);
nipkow@3574
   101
by (ALLGOALS Asm_simp_tac);
nipkow@3574
   102
qed "append_self_conv";
nipkow@3574
   103
nipkow@3574
   104
goal thy "(xs = xs @ ys) = (ys=[])";
nipkow@3574
   105
by (induct_tac "xs" 1);
nipkow@3574
   106
by (ALLGOALS Asm_simp_tac);
nipkow@3574
   107
by (Blast_tac 1);
nipkow@3574
   108
qed "self_append_conv";
nipkow@3574
   109
AddIffs [append_self_conv,self_append_conv];
nipkow@3574
   110
nipkow@3011
   111
goal thy "(xs @ ys = xs @ zs) = (ys=zs)";
nipkow@3040
   112
by (induct_tac "xs" 1);
clasohm@1264
   113
by (ALLGOALS Asm_simp_tac);
clasohm@923
   114
qed "same_append_eq";
nipkow@2608
   115
AddIffs [same_append_eq];
nipkow@2608
   116
nipkow@3011
   117
goal thy "!ys. (xs @ [x] = ys @ [y]) = (xs = ys & x = y)"; 
paulson@3457
   118
by (induct_tac "xs" 1);
paulson@3457
   119
 by (rtac allI 1);
paulson@3457
   120
 by (induct_tac "ys" 1);
paulson@3457
   121
  by (ALLGOALS Asm_simp_tac);
paulson@3457
   122
by (rtac allI 1);
paulson@3457
   123
by (induct_tac "ys" 1);
paulson@3457
   124
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   125
qed_spec_mp "append1_eq_conv";
nipkow@2608
   126
AddIffs [append1_eq_conv];
nipkow@2608
   127
nipkow@3571
   128
goal thy "!ys zs. (ys @ xs = zs @ xs) = (ys=zs)";
nipkow@3571
   129
by (induct_tac "xs" 1);
nipkow@3571
   130
by (Simp_tac 1);
nipkow@3571
   131
by (strip_tac 1);
nipkow@3571
   132
by (subgoal_tac "((ys @ [a]) @ list = (zs @ [a]) @ list) = (ys=zs)" 1);
nipkow@3571
   133
by (Asm_full_simp_tac 1);
nipkow@3571
   134
by (Blast_tac 1);
nipkow@3571
   135
qed_spec_mp "append_same_eq";
nipkow@3571
   136
AddIffs [append_same_eq];
nipkow@3571
   137
nipkow@3011
   138
goal thy "xs ~= [] --> hd xs # tl xs = xs";
paulson@3457
   139
by (induct_tac "xs" 1);
paulson@3457
   140
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   141
qed_spec_mp "hd_Cons_tl";
nipkow@2608
   142
Addsimps [hd_Cons_tl];
clasohm@923
   143
nipkow@3011
   144
goal thy "hd(xs@ys) = (if xs=[] then hd ys else hd xs)";
nipkow@3040
   145
by (induct_tac "xs" 1);
nipkow@1327
   146
by (ALLGOALS Asm_simp_tac);
nipkow@1327
   147
qed "hd_append";
clasohm@923
   148
nipkow@3571
   149
goal thy "!!xs. xs ~= [] ==> hd(xs @ ys) = hd xs";
nipkow@3571
   150
by (asm_simp_tac (!simpset addsimps [hd_append]
nipkow@3571
   151
                           setloop (split_tac [expand_list_case])) 1);
nipkow@3571
   152
qed "hd_append2";
nipkow@3571
   153
Addsimps [hd_append2];
nipkow@3571
   154
nipkow@3011
   155
goal thy "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)";
paulson@3457
   156
by (simp_tac (!simpset setloop(split_tac[expand_list_case])) 1);
nipkow@2608
   157
qed "tl_append";
nipkow@2608
   158
nipkow@3571
   159
goal thy "!!xs. xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys";
nipkow@3571
   160
by (asm_simp_tac (!simpset addsimps [tl_append]
nipkow@3571
   161
                           setloop (split_tac [expand_list_case])) 1);
nipkow@3571
   162
qed "tl_append2";
nipkow@3571
   163
Addsimps [tl_append2];
nipkow@3571
   164
nipkow@2608
   165
(** map **)
nipkow@2608
   166
nipkow@3467
   167
section "map";
nipkow@3467
   168
nipkow@3011
   169
goal thy
nipkow@3465
   170
  "(!x. x : set xs --> f x = g x) --> map f xs = map g xs";
paulson@3457
   171
by (induct_tac "xs" 1);
paulson@3457
   172
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   173
bind_thm("map_ext", impI RS (allI RS (result() RS mp)));
nipkow@2608
   174
nipkow@3011
   175
goal thy "map (%x.x) = (%xs.xs)";
nipkow@2608
   176
by (rtac ext 1);
nipkow@3040
   177
by (induct_tac "xs" 1);
nipkow@2608
   178
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   179
qed "map_ident";
nipkow@2608
   180
Addsimps[map_ident];
nipkow@2608
   181
nipkow@3011
   182
goal thy "map f (xs@ys) = map f xs @ map f ys";
nipkow@3040
   183
by (induct_tac "xs" 1);
nipkow@2608
   184
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   185
qed "map_append";
nipkow@2608
   186
Addsimps[map_append];
nipkow@2608
   187
nipkow@3011
   188
goalw thy [o_def] "map (f o g) xs = map f (map g xs)";
nipkow@3040
   189
by (induct_tac "xs" 1);
nipkow@2608
   190
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   191
qed "map_compose";
nipkow@2608
   192
Addsimps[map_compose];
nipkow@2608
   193
nipkow@3011
   194
goal thy "rev(map f xs) = map f (rev xs)";
nipkow@3040
   195
by (induct_tac "xs" 1);
nipkow@2608
   196
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   197
qed "rev_map";
nipkow@2608
   198
nipkow@3589
   199
(* a congruence rule for map: *)
nipkow@3589
   200
goal thy
nipkow@3589
   201
 "(xs=ys) --> (!x. x : set ys --> f x = g x) --> map f xs = map g ys";
nipkow@3589
   202
by(rtac impI 1);
nipkow@3589
   203
by(hyp_subst_tac 1);
nipkow@3589
   204
by(induct_tac "ys" 1);
nipkow@3589
   205
by(ALLGOALS Asm_simp_tac);
nipkow@3589
   206
val lemma = result();
nipkow@3589
   207
bind_thm("map_cong",impI RSN (2,allI RSN (2,lemma RS mp RS mp)));
nipkow@3589
   208
lcp@1169
   209
(** rev **)
lcp@1169
   210
nipkow@3467
   211
section "rev";
nipkow@3467
   212
nipkow@3011
   213
goal thy "rev(xs@ys) = rev(ys) @ rev(xs)";
nipkow@3040
   214
by (induct_tac "xs" 1);
nipkow@2512
   215
by (ALLGOALS Asm_simp_tac);
lcp@1169
   216
qed "rev_append";
nipkow@2512
   217
Addsimps[rev_append];
lcp@1169
   218
nipkow@3011
   219
goal thy "rev(rev l) = l";
nipkow@3040
   220
by (induct_tac "l" 1);
nipkow@2512
   221
by (ALLGOALS Asm_simp_tac);
lcp@1169
   222
qed "rev_rev_ident";
nipkow@2512
   223
Addsimps[rev_rev_ident];
lcp@1169
   224
nipkow@2608
   225
clasohm@923
   226
(** mem **)
clasohm@923
   227
nipkow@3467
   228
section "mem";
nipkow@3467
   229
nipkow@3011
   230
goal thy "x mem (xs@ys) = (x mem xs | x mem ys)";
nipkow@3040
   231
by (induct_tac "xs" 1);
clasohm@1264
   232
by (ALLGOALS (asm_simp_tac (!simpset setloop (split_tac [expand_if]))));
clasohm@923
   233
qed "mem_append";
nipkow@2512
   234
Addsimps[mem_append];
clasohm@923
   235
nipkow@3011
   236
goal thy "x mem [x:xs.P(x)] = (x mem xs & P(x))";
nipkow@3040
   237
by (induct_tac "xs" 1);
clasohm@1264
   238
by (ALLGOALS (asm_simp_tac (!simpset setloop (split_tac [expand_if]))));
clasohm@923
   239
qed "mem_filter";
nipkow@2512
   240
Addsimps[mem_filter];
clasohm@923
   241
nipkow@3465
   242
(** set **)
paulson@1812
   243
nipkow@3467
   244
section "set";
nipkow@3467
   245
nipkow@3465
   246
goal thy "set (xs@ys) = (set xs Un set ys)";
nipkow@3040
   247
by (induct_tac "xs" 1);
paulson@1812
   248
by (ALLGOALS Asm_simp_tac);
paulson@1908
   249
qed "set_of_list_append";
nipkow@2512
   250
Addsimps[set_of_list_append];
paulson@1812
   251
nipkow@3465
   252
goal thy "(x mem xs) = (x: set xs)";
nipkow@3040
   253
by (induct_tac "xs" 1);
paulson@1812
   254
by (ALLGOALS (asm_simp_tac (!simpset setloop (split_tac [expand_if]))));
paulson@2891
   255
by (Blast_tac 1);
paulson@1908
   256
qed "set_of_list_mem_eq";
paulson@1812
   257
nipkow@3465
   258
goal thy "set l <= set (x#l)";
paulson@1936
   259
by (Simp_tac 1);
paulson@2891
   260
by (Blast_tac 1);
paulson@1936
   261
qed "set_of_list_subset_Cons";
paulson@1936
   262
nipkow@3465
   263
goal thy "(set xs = {}) = (xs = [])";
paulson@3457
   264
by (induct_tac "xs" 1);
paulson@3457
   265
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   266
qed "set_of_list_empty";
nipkow@2608
   267
Addsimps [set_of_list_empty];
nipkow@2608
   268
nipkow@3465
   269
goal thy "set(rev xs) = set(xs)";
paulson@3457
   270
by (induct_tac "xs" 1);
paulson@3457
   271
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   272
qed "set_of_list_rev";
nipkow@2608
   273
Addsimps [set_of_list_rev];
nipkow@2608
   274
nipkow@3465
   275
goal thy "set(map f xs) = f``(set xs)";
paulson@3457
   276
by (induct_tac "xs" 1);
paulson@3457
   277
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   278
qed "set_of_list_map";
nipkow@2608
   279
Addsimps [set_of_list_map];
nipkow@2608
   280
paulson@1812
   281
clasohm@923
   282
(** list_all **)
clasohm@923
   283
nipkow@3467
   284
section "list_all";
nipkow@3467
   285
nipkow@3011
   286
goal thy "list_all (%x.True) xs = True";
nipkow@3040
   287
by (induct_tac "xs" 1);
clasohm@1264
   288
by (ALLGOALS Asm_simp_tac);
clasohm@923
   289
qed "list_all_True";
nipkow@2512
   290
Addsimps [list_all_True];
clasohm@923
   291
nipkow@3011
   292
goal thy "list_all p (xs@ys) = (list_all p xs & list_all p ys)";
nipkow@3040
   293
by (induct_tac "xs" 1);
clasohm@1264
   294
by (ALLGOALS Asm_simp_tac);
nipkow@2512
   295
qed "list_all_append";
nipkow@2512
   296
Addsimps [list_all_append];
clasohm@923
   297
nipkow@3011
   298
goal thy "list_all P xs = (!x. x mem xs --> P(x))";
nipkow@3040
   299
by (induct_tac "xs" 1);
clasohm@1264
   300
by (ALLGOALS (asm_simp_tac (!simpset setloop (split_tac [expand_if]))));
paulson@2891
   301
by (Blast_tac 1);
clasohm@923
   302
qed "list_all_mem_conv";
clasohm@923
   303
clasohm@923
   304
nipkow@2608
   305
(** filter **)
clasohm@923
   306
nipkow@3467
   307
section "filter";
nipkow@3467
   308
paulson@3383
   309
goal thy "filter P (xs@ys) = filter P xs @ filter P ys";
paulson@3457
   310
by (induct_tac "xs" 1);
paulson@3457
   311
 by (ALLGOALS (asm_simp_tac (!simpset setloop (split_tac [expand_if]))));
nipkow@2608
   312
qed "filter_append";
nipkow@2608
   313
Addsimps [filter_append];
nipkow@2608
   314
paulson@3383
   315
goal thy "size (filter P xs) <= size xs";
paulson@3457
   316
by (induct_tac "xs" 1);
paulson@3457
   317
 by (ALLGOALS (asm_simp_tac (!simpset setloop (split_tac [expand_if]))));
paulson@3383
   318
qed "filter_size";
paulson@3383
   319
nipkow@2608
   320
nipkow@2608
   321
(** concat **)
nipkow@2608
   322
nipkow@3467
   323
section "concat";
nipkow@3467
   324
nipkow@3011
   325
goal thy  "concat(xs@ys) = concat(xs)@concat(ys)";
nipkow@3040
   326
by (induct_tac "xs" 1);
clasohm@1264
   327
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   328
qed"concat_append";
nipkow@2608
   329
Addsimps [concat_append];
nipkow@2512
   330
nipkow@3467
   331
goal thy  "set(concat xs) = Union(set `` set xs)";
nipkow@3467
   332
by (induct_tac "xs" 1);
nipkow@3467
   333
by (ALLGOALS Asm_simp_tac);
nipkow@3467
   334
qed"set_of_list_concat";
nipkow@3467
   335
Addsimps [set_of_list_concat];
nipkow@3467
   336
nipkow@3467
   337
goal thy "map f (concat xs) = concat (map (map f) xs)"; 
nipkow@3467
   338
by (induct_tac "xs" 1);
nipkow@3467
   339
by (ALLGOALS Asm_simp_tac);
nipkow@3467
   340
qed "map_concat";
nipkow@3467
   341
nipkow@3467
   342
goal thy "filter p (concat xs) = concat (map (filter p) xs)"; 
nipkow@3467
   343
by (induct_tac "xs" 1);
nipkow@3467
   344
by (ALLGOALS Asm_simp_tac);
nipkow@3467
   345
qed"filter_concat"; 
nipkow@3467
   346
nipkow@3467
   347
goal thy "rev(concat xs) = concat (map rev (rev xs))";
nipkow@3467
   348
by (induct_tac "xs" 1);
nipkow@2512
   349
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   350
qed "rev_concat";
clasohm@923
   351
nipkow@962
   352
(** length **)
nipkow@962
   353
nipkow@3467
   354
section "length";
nipkow@3467
   355
nipkow@3011
   356
goal thy "length(xs@ys) = length(xs)+length(ys)";
nipkow@3040
   357
by (induct_tac "xs" 1);
clasohm@1264
   358
by (ALLGOALS Asm_simp_tac);
nipkow@962
   359
qed"length_append";
nipkow@1301
   360
Addsimps [length_append];
nipkow@1301
   361
nipkow@3011
   362
goal thy "length (map f l) = length l";
nipkow@3040
   363
by (induct_tac "l" 1);
nipkow@1301
   364
by (ALLGOALS Simp_tac);
nipkow@1301
   365
qed "length_map";
nipkow@1301
   366
Addsimps [length_map];
nipkow@962
   367
nipkow@3011
   368
goal thy "length(rev xs) = length(xs)";
nipkow@3040
   369
by (induct_tac "xs" 1);
nipkow@1301
   370
by (ALLGOALS Asm_simp_tac);
lcp@1169
   371
qed "length_rev";
nipkow@1301
   372
Addsimps [length_rev];
lcp@1169
   373
nipkow@3011
   374
goal thy "(length xs = 0) = (xs = [])";
paulson@3457
   375
by (induct_tac "xs" 1);
paulson@3457
   376
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   377
qed "length_0_conv";
nipkow@2608
   378
AddIffs [length_0_conv];
nipkow@2608
   379
nipkow@3011
   380
goal thy "(0 < length xs) = (xs ~= [])";
paulson@3457
   381
by (induct_tac "xs" 1);
paulson@3457
   382
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   383
qed "length_greater_0_conv";
nipkow@2608
   384
AddIffs [length_greater_0_conv];
nipkow@2608
   385
nipkow@2608
   386
clasohm@923
   387
(** nth **)
clasohm@923
   388
nipkow@3467
   389
section "nth";
nipkow@3467
   390
nipkow@3011
   391
goal thy
nipkow@2608
   392
  "!xs. nth n (xs@ys) = \
nipkow@2608
   393
\          (if n < length xs then nth n xs else nth (n - length xs) ys)";
paulson@3457
   394
by (nat_ind_tac "n" 1);
paulson@3457
   395
 by (Asm_simp_tac 1);
paulson@3457
   396
 by (rtac allI 1);
paulson@3457
   397
 by (exhaust_tac "xs" 1);
paulson@3457
   398
  by (ALLGOALS Asm_simp_tac);
paulson@3457
   399
by (rtac allI 1);
paulson@3457
   400
by (exhaust_tac "xs" 1);
paulson@3457
   401
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   402
qed_spec_mp "nth_append";
nipkow@2608
   403
nipkow@3011
   404
goal thy "!n. n < length xs --> nth n (map f xs) = f (nth n xs)";
nipkow@3040
   405
by (induct_tac "xs" 1);
nipkow@1301
   406
(* case [] *)
nipkow@1301
   407
by (Asm_full_simp_tac 1);
nipkow@1301
   408
(* case x#xl *)
nipkow@1301
   409
by (rtac allI 1);
nipkow@1301
   410
by (nat_ind_tac "n" 1);
nipkow@1301
   411
by (ALLGOALS Asm_full_simp_tac);
nipkow@1485
   412
qed_spec_mp "nth_map";
nipkow@1301
   413
Addsimps [nth_map];
nipkow@1301
   414
nipkow@3011
   415
goal thy "!n. n < length xs --> list_all P xs --> P(nth n xs)";
nipkow@3040
   416
by (induct_tac "xs" 1);
nipkow@1301
   417
(* case [] *)
nipkow@1301
   418
by (Simp_tac 1);
nipkow@1301
   419
(* case x#xl *)
nipkow@1301
   420
by (rtac allI 1);
nipkow@1301
   421
by (nat_ind_tac "n" 1);
nipkow@1301
   422
by (ALLGOALS Asm_full_simp_tac);
nipkow@1485
   423
qed_spec_mp "list_all_nth";
nipkow@1301
   424
nipkow@3011
   425
goal thy "!n. n < length xs --> (nth n xs) mem xs";
nipkow@3040
   426
by (induct_tac "xs" 1);
nipkow@1301
   427
(* case [] *)
nipkow@1301
   428
by (Simp_tac 1);
nipkow@1301
   429
(* case x#xl *)
nipkow@1301
   430
by (rtac allI 1);
nipkow@1301
   431
by (nat_ind_tac "n" 1);
nipkow@1301
   432
(* case 0 *)
nipkow@1301
   433
by (Asm_full_simp_tac 1);
nipkow@1301
   434
(* case Suc x *)
nipkow@1301
   435
by (asm_full_simp_tac (!simpset setloop (split_tac [expand_if])) 1);
nipkow@1485
   436
qed_spec_mp "nth_mem";
nipkow@1301
   437
Addsimps [nth_mem];
nipkow@1301
   438
nipkow@1327
   439
nipkow@2608
   440
(** take  & drop **)
nipkow@2608
   441
section "take & drop";
nipkow@1327
   442
nipkow@1419
   443
goal thy "take 0 xs = []";
nipkow@3040
   444
by (induct_tac "xs" 1);
nipkow@1419
   445
by (ALLGOALS Asm_simp_tac);
nipkow@1327
   446
qed "take_0";
nipkow@1327
   447
nipkow@2608
   448
goal thy "drop 0 xs = xs";
nipkow@3040
   449
by (induct_tac "xs" 1);
nipkow@2608
   450
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   451
qed "drop_0";
nipkow@2608
   452
nipkow@1419
   453
goal thy "take (Suc n) (x#xs) = x # take n xs";
paulson@1552
   454
by (Simp_tac 1);
nipkow@1419
   455
qed "take_Suc_Cons";
nipkow@1327
   456
nipkow@2608
   457
goal thy "drop (Suc n) (x#xs) = drop n xs";
nipkow@2608
   458
by (Simp_tac 1);
nipkow@2608
   459
qed "drop_Suc_Cons";
nipkow@2608
   460
nipkow@2608
   461
Delsimps [take_Cons,drop_Cons];
nipkow@2608
   462
Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
nipkow@2608
   463
nipkow@3011
   464
goal thy "!xs. length(take n xs) = min (length xs) n";
paulson@3457
   465
by (nat_ind_tac "n" 1);
paulson@3457
   466
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   467
by (rtac allI 1);
paulson@3457
   468
by (exhaust_tac "xs" 1);
paulson@3457
   469
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   470
qed_spec_mp "length_take";
nipkow@2608
   471
Addsimps [length_take];
clasohm@923
   472
nipkow@3011
   473
goal thy "!xs. length(drop n xs) = (length xs - n)";
paulson@3457
   474
by (nat_ind_tac "n" 1);
paulson@3457
   475
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   476
by (rtac allI 1);
paulson@3457
   477
by (exhaust_tac "xs" 1);
paulson@3457
   478
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   479
qed_spec_mp "length_drop";
nipkow@2608
   480
Addsimps [length_drop];
nipkow@2608
   481
nipkow@3011
   482
goal thy "!xs. length xs <= n --> take n xs = xs";
paulson@3457
   483
by (nat_ind_tac "n" 1);
paulson@3457
   484
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   485
by (rtac allI 1);
paulson@3457
   486
by (exhaust_tac "xs" 1);
paulson@3457
   487
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   488
qed_spec_mp "take_all";
clasohm@923
   489
nipkow@3011
   490
goal thy "!xs. length xs <= n --> drop n xs = []";
paulson@3457
   491
by (nat_ind_tac "n" 1);
paulson@3457
   492
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   493
by (rtac allI 1);
paulson@3457
   494
by (exhaust_tac "xs" 1);
paulson@3457
   495
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   496
qed_spec_mp "drop_all";
nipkow@2608
   497
nipkow@3011
   498
goal thy 
nipkow@2608
   499
  "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
paulson@3457
   500
by (nat_ind_tac "n" 1);
paulson@3457
   501
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   502
by (rtac allI 1);
paulson@3457
   503
by (exhaust_tac "xs" 1);
paulson@3457
   504
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   505
qed_spec_mp "take_append";
nipkow@2608
   506
Addsimps [take_append];
nipkow@2608
   507
nipkow@3011
   508
goal thy "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
paulson@3457
   509
by (nat_ind_tac "n" 1);
paulson@3457
   510
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   511
by (rtac allI 1);
paulson@3457
   512
by (exhaust_tac "xs" 1);
paulson@3457
   513
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   514
qed_spec_mp "drop_append";
nipkow@2608
   515
Addsimps [drop_append];
nipkow@2608
   516
nipkow@3011
   517
goal thy "!xs n. take n (take m xs) = take (min n m) xs"; 
paulson@3457
   518
by (nat_ind_tac "m" 1);
paulson@3457
   519
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   520
by (rtac allI 1);
paulson@3457
   521
by (exhaust_tac "xs" 1);
paulson@3457
   522
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   523
by (rtac allI 1);
paulson@3457
   524
by (exhaust_tac "n" 1);
paulson@3457
   525
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   526
qed_spec_mp "take_take";
nipkow@2608
   527
nipkow@3011
   528
goal thy "!xs. drop n (drop m xs) = drop (n + m) xs"; 
paulson@3457
   529
by (nat_ind_tac "m" 1);
paulson@3457
   530
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   531
by (rtac allI 1);
paulson@3457
   532
by (exhaust_tac "xs" 1);
paulson@3457
   533
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   534
qed_spec_mp "drop_drop";
clasohm@923
   535
nipkow@3011
   536
goal thy "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
paulson@3457
   537
by (nat_ind_tac "m" 1);
paulson@3457
   538
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   539
by (rtac allI 1);
paulson@3457
   540
by (exhaust_tac "xs" 1);
paulson@3457
   541
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   542
qed_spec_mp "take_drop";
nipkow@2608
   543
nipkow@3011
   544
goal thy "!xs. take n (map f xs) = map f (take n xs)"; 
paulson@3457
   545
by (nat_ind_tac "n" 1);
paulson@3457
   546
by (ALLGOALS Asm_simp_tac);
paulson@3457
   547
by (rtac allI 1);
paulson@3457
   548
by (exhaust_tac "xs" 1);
paulson@3457
   549
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   550
qed_spec_mp "take_map"; 
nipkow@2608
   551
nipkow@3011
   552
goal thy "!xs. drop n (map f xs) = map f (drop n xs)"; 
paulson@3457
   553
by (nat_ind_tac "n" 1);
paulson@3457
   554
by (ALLGOALS Asm_simp_tac);
paulson@3457
   555
by (rtac allI 1);
paulson@3457
   556
by (exhaust_tac "xs" 1);
paulson@3457
   557
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   558
qed_spec_mp "drop_map";
nipkow@2608
   559
nipkow@3283
   560
goal thy "!n i. i < n --> nth i (take n xs) = nth i xs";
paulson@3457
   561
by (induct_tac "xs" 1);
paulson@3457
   562
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   563
by (strip_tac 1);
paulson@3457
   564
by (exhaust_tac "n" 1);
paulson@3457
   565
 by (Blast_tac 1);
paulson@3457
   566
by (exhaust_tac "i" 1);
paulson@3457
   567
by (ALLGOALS Asm_full_simp_tac);
nipkow@2608
   568
qed_spec_mp "nth_take";
nipkow@2608
   569
Addsimps [nth_take];
clasohm@923
   570
nipkow@3585
   571
goal thy  "!xs i. n + i <= length xs --> nth i (drop n xs) = nth (n + i) xs";
paulson@3457
   572
by (nat_ind_tac "n" 1);
paulson@3457
   573
 by (ALLGOALS Asm_simp_tac);
paulson@3457
   574
by (rtac allI 1);
paulson@3457
   575
by (exhaust_tac "xs" 1);
paulson@3457
   576
 by (ALLGOALS Asm_simp_tac);
nipkow@2608
   577
qed_spec_mp "nth_drop";
nipkow@2608
   578
Addsimps [nth_drop];
nipkow@2608
   579
nipkow@2608
   580
(** takeWhile & dropWhile **)
nipkow@2608
   581
nipkow@3467
   582
section "takeWhile & dropWhile";
nipkow@3467
   583
nipkow@3586
   584
goal thy "takeWhile P xs @ dropWhile P xs = xs";
nipkow@3586
   585
by (induct_tac "xs" 1);
nipkow@3586
   586
 by (Simp_tac 1);
nipkow@3586
   587
by (asm_full_simp_tac (!simpset setloop (split_tac[expand_if])) 1);
nipkow@3586
   588
qed "takeWhile_dropWhile_id";
nipkow@3586
   589
Addsimps [takeWhile_dropWhile_id];
nipkow@3586
   590
nipkow@3586
   591
goal thy  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
paulson@3457
   592
by (induct_tac "xs" 1);
paulson@3457
   593
 by (Simp_tac 1);
paulson@3457
   594
by (asm_full_simp_tac (!simpset setloop (split_tac[expand_if])) 1);
paulson@3457
   595
by (Blast_tac 1);
nipkow@2608
   596
bind_thm("takeWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   597
Addsimps [takeWhile_append1];
clasohm@923
   598
nipkow@3011
   599
goal thy
nipkow@3465
   600
  "(!x:set xs.P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
paulson@3457
   601
by (induct_tac "xs" 1);
paulson@3457
   602
 by (Simp_tac 1);
paulson@3457
   603
by (asm_full_simp_tac (!simpset setloop (split_tac[expand_if])) 1);
nipkow@2608
   604
bind_thm("takeWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   605
Addsimps [takeWhile_append2];
lcp@1169
   606
nipkow@3011
   607
goal thy
nipkow@3465
   608
  "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
paulson@3457
   609
by (induct_tac "xs" 1);
paulson@3457
   610
 by (Simp_tac 1);
paulson@3457
   611
by (asm_full_simp_tac (!simpset setloop (split_tac[expand_if])) 1);
paulson@3457
   612
by (Blast_tac 1);
nipkow@2608
   613
bind_thm("dropWhile_append1", conjI RS (result() RS mp));
nipkow@2608
   614
Addsimps [dropWhile_append1];
nipkow@2608
   615
nipkow@3011
   616
goal thy
nipkow@3465
   617
  "(!x:set xs.P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
paulson@3457
   618
by (induct_tac "xs" 1);
paulson@3457
   619
 by (Simp_tac 1);
paulson@3457
   620
by (asm_full_simp_tac (!simpset setloop (split_tac[expand_if])) 1);
nipkow@2608
   621
bind_thm("dropWhile_append2", ballI RS (result() RS mp));
nipkow@2608
   622
Addsimps [dropWhile_append2];
nipkow@2608
   623
nipkow@3465
   624
goal thy "x:set(takeWhile P xs) --> x:set xs & P x";
paulson@3457
   625
by (induct_tac "xs" 1);
paulson@3457
   626
 by (Simp_tac 1);
paulson@3457
   627
by (asm_full_simp_tac (!simpset setloop (split_tac[expand_if])) 1);
nipkow@2608
   628
qed_spec_mp"set_of_list_take_whileD";
nipkow@2608
   629
nipkow@3589
   630
(** replicate **)
nipkow@3589
   631
section "replicate";
nipkow@3589
   632
nipkow@3589
   633
goal thy "set(replicate (Suc n) x) = {x}";
nipkow@3589
   634
by(induct_tac "n" 1);
nipkow@3589
   635
by(ALLGOALS Asm_full_simp_tac);
nipkow@3589
   636
val lemma = result();
nipkow@3589
   637
nipkow@3589
   638
goal thy "!!n. n ~= 0 ==> set(replicate n x) = {x}";
nipkow@3589
   639
by(fast_tac (!claset addSDs [not0_implies_Suc] addSIs [lemma]) 1);
nipkow@3589
   640
qed "set_replicate";
nipkow@3589
   641
Addsimps [set_replicate];