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