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