src/HOL/List.ML
author oheimb
Wed Sep 09 17:19:26 1998 +0200 (1998-09-09)
changeset 5443 e2459d18ff47
parent 5427 26c9a7c0b36b
child 5448 40a09282ba14
permissions -rw-r--r--
changed constants mem and list_all to mere translations
added filter_is_subset
     1 (*  Title:      HOL/List
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     Copyright   1994 TU Muenchen
     5 
     6 List lemmas
     7 *)
     8 
     9 Goal "!x. xs ~= x#xs";
    10 by (induct_tac "xs" 1);
    11 by Auto_tac;
    12 qed_spec_mp "not_Cons_self";
    13 bind_thm("not_Cons_self2",not_Cons_self RS not_sym);
    14 Addsimps [not_Cons_self,not_Cons_self2];
    15 
    16 Goal "(xs ~= []) = (? y ys. xs = y#ys)";
    17 by (induct_tac "xs" 1);
    18 by Auto_tac;
    19 qed "neq_Nil_conv";
    20 
    21 (* Induction over the length of a list: *)
    22 val [prem] = Goal
    23   "(!!xs. (!ys. length ys < length xs --> P ys) ==> P xs) ==> P(xs)";
    24 by (rtac measure_induct 1 THEN etac prem 1);
    25 qed "length_induct";
    26 
    27 
    28 (** "lists": the list-forming operator over sets **)
    29 
    30 Goalw lists.defs "A<=B ==> lists A <= lists B";
    31 by (rtac lfp_mono 1);
    32 by (REPEAT (ares_tac basic_monos 1));
    33 qed "lists_mono";
    34 
    35 val listsE = lists.mk_cases list.simps  "x#l : lists A";
    36 AddSEs [listsE];
    37 AddSIs lists.intrs;
    38 
    39 Goal "l: lists A ==> l: lists B --> l: lists (A Int B)";
    40 by (etac lists.induct 1);
    41 by (ALLGOALS Blast_tac);
    42 qed_spec_mp "lists_IntI";
    43 
    44 Goal "lists (A Int B) = lists A Int lists B";
    45 by (rtac (mono_Int RS equalityI) 1);
    46 by (simp_tac (simpset() addsimps [mono_def, lists_mono]) 1);
    47 by (blast_tac (claset() addSIs [lists_IntI]) 1);
    48 qed "lists_Int_eq";
    49 Addsimps [lists_Int_eq];
    50 
    51 
    52 (**  Case analysis **)
    53 section "Case analysis";
    54 
    55 val prems = Goal "[| P([]); !!x xs. P(x#xs) |] ==> P(xs)";
    56 by (induct_tac "xs" 1);
    57 by (REPEAT(resolve_tac prems 1));
    58 qed "list_cases";
    59 
    60 Goal "(xs=[] --> P([])) & (!y ys. xs=y#ys --> P(y#ys)) --> P(xs)";
    61 by (induct_tac "xs" 1);
    62 by (Blast_tac 1);
    63 by (Blast_tac 1);
    64 bind_thm("list_eq_cases",
    65   impI RSN (2,allI RSN (2,allI RSN (2,impI RS (conjI RS (result() RS mp))))));
    66 
    67 (** length **)
    68 (* needs to come before "@" because of thm append_eq_append_conv *)
    69 
    70 section "length";
    71 
    72 Goal "length(xs@ys) = length(xs)+length(ys)";
    73 by (induct_tac "xs" 1);
    74 by Auto_tac;
    75 qed"length_append";
    76 Addsimps [length_append];
    77 
    78 Goal "length (map f xs) = length xs";
    79 by (induct_tac "xs" 1);
    80 by Auto_tac;
    81 qed "length_map";
    82 Addsimps [length_map];
    83 
    84 Goal "length(rev xs) = length(xs)";
    85 by (induct_tac "xs" 1);
    86 by Auto_tac;
    87 qed "length_rev";
    88 Addsimps [length_rev];
    89 
    90 Goal "xs ~= [] ==> length(tl xs) = (length xs) - 1";
    91 by (exhaust_tac "xs" 1);
    92 by Auto_tac;
    93 qed "length_tl";
    94 Addsimps [length_tl];
    95 
    96 Goal "(length xs = 0) = (xs = [])";
    97 by (induct_tac "xs" 1);
    98 by Auto_tac;
    99 qed "length_0_conv";
   100 AddIffs [length_0_conv];
   101 
   102 Goal "(0 = length xs) = (xs = [])";
   103 by (induct_tac "xs" 1);
   104 by Auto_tac;
   105 qed "zero_length_conv";
   106 AddIffs [zero_length_conv];
   107 
   108 Goal "(0 < length xs) = (xs ~= [])";
   109 by (induct_tac "xs" 1);
   110 by Auto_tac;
   111 qed "length_greater_0_conv";
   112 AddIffs [length_greater_0_conv];
   113 
   114 Goal "(length xs = Suc n) = (? y ys. xs = y#ys & length ys = n)";
   115 by (induct_tac "xs" 1);
   116 by (Auto_tac);
   117 qed "length_Suc_conv";
   118 AddIffs [length_Suc_conv];
   119 
   120 (** @ - append **)
   121 
   122 section "@ - append";
   123 
   124 Goal "(xs@ys)@zs = xs@(ys@zs)";
   125 by (induct_tac "xs" 1);
   126 by Auto_tac;
   127 qed "append_assoc";
   128 Addsimps [append_assoc];
   129 
   130 Goal "xs @ [] = xs";
   131 by (induct_tac "xs" 1);
   132 by Auto_tac;
   133 qed "append_Nil2";
   134 Addsimps [append_Nil2];
   135 
   136 Goal "(xs@ys = []) = (xs=[] & ys=[])";
   137 by (induct_tac "xs" 1);
   138 by Auto_tac;
   139 qed "append_is_Nil_conv";
   140 AddIffs [append_is_Nil_conv];
   141 
   142 Goal "([] = xs@ys) = (xs=[] & ys=[])";
   143 by (induct_tac "xs" 1);
   144 by Auto_tac;
   145 qed "Nil_is_append_conv";
   146 AddIffs [Nil_is_append_conv];
   147 
   148 Goal "(xs @ ys = xs) = (ys=[])";
   149 by (induct_tac "xs" 1);
   150 by Auto_tac;
   151 qed "append_self_conv";
   152 
   153 Goal "(xs = xs @ ys) = (ys=[])";
   154 by (induct_tac "xs" 1);
   155 by Auto_tac;
   156 qed "self_append_conv";
   157 AddIffs [append_self_conv,self_append_conv];
   158 
   159 Goal "!ys. length xs = length ys | length us = length vs \
   160 \              --> (xs@us = ys@vs) = (xs=ys & us=vs)";
   161 by (induct_tac "xs" 1);
   162  by (rtac allI 1);
   163  by (exhaust_tac "ys" 1);
   164   by (Asm_simp_tac 1);
   165  by (fast_tac (claset() addIs [less_add_Suc2] addss simpset()
   166                       addEs [less_not_refl2 RSN (2,rev_notE)]) 1);
   167 by (rtac allI 1);
   168 by (exhaust_tac "ys" 1);
   169 by (fast_tac (claset() addIs [less_add_Suc2] 
   170 		       addss (simpset() delsimps [length_Suc_conv])
   171                        addEs [(less_not_refl3) RSN (2,rev_notE)]) 1);
   172 by (Asm_simp_tac 1);
   173 qed_spec_mp "append_eq_append_conv";
   174 Addsimps [append_eq_append_conv];
   175 
   176 Goal "(xs @ ys = xs @ zs) = (ys=zs)";
   177 by (Simp_tac 1);
   178 qed "same_append_eq";
   179 
   180 Goal "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)"; 
   181 by (Simp_tac 1);
   182 qed "append1_eq_conv";
   183 
   184 Goal "(ys @ xs = zs @ xs) = (ys=zs)";
   185 by (Simp_tac 1);
   186 qed "append_same_eq";
   187 
   188 AddSIs
   189  [same_append_eq RS iffD2, append1_eq_conv RS iffD2, append_same_eq RS iffD2];
   190 AddSDs
   191  [same_append_eq RS iffD1, append1_eq_conv RS iffD1, append_same_eq RS iffD1];
   192 
   193 Goal "(xs @ ys = ys) = (xs=[])";
   194 by (cut_inst_tac [("zs","[]")] append_same_eq 1);
   195 by Auto_tac;
   196 qed "append_self_conv2";
   197 
   198 Goal "(ys = xs @ ys) = (xs=[])";
   199 by (simp_tac (simpset() addsimps
   200      [simplify (simpset()) (read_instantiate[("ys","[]")]append_same_eq)]) 1);
   201 by (Blast_tac 1);
   202 qed "self_append_conv2";
   203 AddIffs [append_self_conv2,self_append_conv2];
   204 
   205 Goal "xs ~= [] --> hd xs # tl xs = xs";
   206 by (induct_tac "xs" 1);
   207 by Auto_tac;
   208 qed_spec_mp "hd_Cons_tl";
   209 Addsimps [hd_Cons_tl];
   210 
   211 Goal "hd(xs@ys) = (if xs=[] then hd ys else hd xs)";
   212 by (induct_tac "xs" 1);
   213 by Auto_tac;
   214 qed "hd_append";
   215 
   216 Goal "xs ~= [] ==> hd(xs @ ys) = hd xs";
   217 by (asm_simp_tac (simpset() addsimps [hd_append]
   218                            addsplits [list.split]) 1);
   219 qed "hd_append2";
   220 Addsimps [hd_append2];
   221 
   222 Goal "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)";
   223 by (simp_tac (simpset() addsplits [list.split]) 1);
   224 qed "tl_append";
   225 
   226 Goal "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys";
   227 by (asm_simp_tac (simpset() addsimps [tl_append]
   228                            addsplits [list.split]) 1);
   229 qed "tl_append2";
   230 Addsimps [tl_append2];
   231 
   232 (* trivial rules for solving @-equations automatically *)
   233 
   234 Goal "xs = ys ==> xs = [] @ ys";
   235 by (Asm_simp_tac 1);
   236 qed "eq_Nil_appendI";
   237 
   238 Goal "[| x#xs1 = ys; xs = xs1 @ zs |] ==> x#xs = ys@zs";
   239 by (dtac sym 1);
   240 by (Asm_simp_tac 1);
   241 qed "Cons_eq_appendI";
   242 
   243 Goal "[| xs@xs1 = zs; ys = xs1 @ us |] ==> xs@ys = zs@us";
   244 by (dtac sym 1);
   245 by (Asm_simp_tac 1);
   246 qed "append_eq_appendI";
   247 
   248 
   249 (***
   250 Simplification procedure for all list equalities.
   251 Currently only tries to rearranges @ to see if
   252 - both lists end in a singleton list,
   253 - or both lists end in the same list.
   254 ***)
   255 local
   256 
   257 val list_eq_pattern =
   258   read_cterm (sign_of List.thy) ("(xs::'a list) = ys",HOLogic.boolT);
   259 
   260 fun last (cons as Const("List.list.op #",_) $ _ $ xs) =
   261       (case xs of Const("List.list.[]",_) => cons | _ => last xs)
   262   | last (Const("List.op @",_) $ _ $ ys) = last ys
   263   | last t = t;
   264 
   265 fun list1 (Const("List.list.op #",_) $ _ $ Const("List.list.[]",_)) = true
   266   | list1 _ = false;
   267 
   268 fun butlast ((cons as Const("List.list.op #",_) $ x) $ xs) =
   269       (case xs of Const("List.list.[]",_) => xs | _ => cons $ butlast xs)
   270   | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
   271   | butlast xs = Const("List.list.[]",fastype_of xs);
   272 
   273 val rearr_tac =
   274   simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons]);
   275 
   276 fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
   277   let
   278     val lastl = last lhs and lastr = last rhs
   279     fun rearr conv =
   280       let val lhs1 = butlast lhs and rhs1 = butlast rhs
   281           val Type(_,listT::_) = eqT
   282           val appT = [listT,listT] ---> listT
   283           val app = Const("List.op @",appT)
   284           val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
   285           val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2)))
   286           val thm = prove_goalw_cterm [] ct (K [rearr_tac 1])
   287             handle ERROR =>
   288             error("The error(s) above occurred while trying to prove " ^
   289                   string_of_cterm ct)
   290       in Some((conv RS (thm RS trans)) RS eq_reflection) end
   291 
   292   in if list1 lastl andalso list1 lastr
   293      then rearr append1_eq_conv
   294      else
   295      if lastl aconv lastr
   296      then rearr append_same_eq
   297      else None
   298   end;
   299 in
   300 val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq;
   301 end;
   302 
   303 Addsimprocs [list_eq_simproc];
   304 
   305 
   306 (** map **)
   307 
   308 section "map";
   309 
   310 Goal "(!x. x : set xs --> f x = g x) --> map f xs = map g xs";
   311 by (induct_tac "xs" 1);
   312 by Auto_tac;
   313 bind_thm("map_ext", impI RS (allI RS (result() RS mp)));
   314 
   315 Goal "map (%x. x) = (%xs. xs)";
   316 by (rtac ext 1);
   317 by (induct_tac "xs" 1);
   318 by Auto_tac;
   319 qed "map_ident";
   320 Addsimps[map_ident];
   321 
   322 Goal "map f (xs@ys) = map f xs @ map f ys";
   323 by (induct_tac "xs" 1);
   324 by Auto_tac;
   325 qed "map_append";
   326 Addsimps[map_append];
   327 
   328 Goalw [o_def] "map (f o g) xs = map f (map g xs)";
   329 by (induct_tac "xs" 1);
   330 by Auto_tac;
   331 qed "map_compose";
   332 Addsimps[map_compose];
   333 
   334 Goal "rev(map f xs) = map f (rev xs)";
   335 by (induct_tac "xs" 1);
   336 by Auto_tac;
   337 qed "rev_map";
   338 
   339 (* a congruence rule for map: *)
   340 Goal "(xs=ys) --> (!x. x : set ys --> f x = g x) --> map f xs = map g ys";
   341 by (rtac impI 1);
   342 by (hyp_subst_tac 1);
   343 by (induct_tac "ys" 1);
   344 by Auto_tac;
   345 val lemma = result();
   346 bind_thm("map_cong",impI RSN (2,allI RSN (2,lemma RS mp RS mp)));
   347 
   348 Goal "(map f xs = []) = (xs = [])";
   349 by (induct_tac "xs" 1);
   350 by Auto_tac;
   351 qed "map_is_Nil_conv";
   352 AddIffs [map_is_Nil_conv];
   353 
   354 Goal "([] = map f xs) = (xs = [])";
   355 by (induct_tac "xs" 1);
   356 by Auto_tac;
   357 qed "Nil_is_map_conv";
   358 AddIffs [Nil_is_map_conv];
   359 
   360 
   361 (** rev **)
   362 
   363 section "rev";
   364 
   365 Goal "rev(xs@ys) = rev(ys) @ rev(xs)";
   366 by (induct_tac "xs" 1);
   367 by Auto_tac;
   368 qed "rev_append";
   369 Addsimps[rev_append];
   370 
   371 Goal "rev(rev l) = l";
   372 by (induct_tac "l" 1);
   373 by Auto_tac;
   374 qed "rev_rev_ident";
   375 Addsimps[rev_rev_ident];
   376 
   377 Goal "(rev xs = []) = (xs = [])";
   378 by (induct_tac "xs" 1);
   379 by Auto_tac;
   380 qed "rev_is_Nil_conv";
   381 AddIffs [rev_is_Nil_conv];
   382 
   383 Goal "([] = rev xs) = (xs = [])";
   384 by (induct_tac "xs" 1);
   385 by Auto_tac;
   386 qed "Nil_is_rev_conv";
   387 AddIffs [Nil_is_rev_conv];
   388 
   389 val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
   390 by (stac (rev_rev_ident RS sym) 1);
   391 br(read_instantiate [("P","%xs. ?P(rev xs)")]list.induct)1;
   392 by (ALLGOALS Simp_tac);
   393 by (resolve_tac prems 1);
   394 by (eresolve_tac prems 1);
   395 qed "rev_induct";
   396 
   397 fun rev_induct_tac xs = res_inst_tac [("xs",xs)] rev_induct;
   398 
   399 Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
   400 by (res_inst_tac [("xs","xs")] rev_induct 1);
   401 by Auto_tac;
   402 bind_thm ("rev_exhaust",
   403   impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp)))));
   404 
   405 
   406 (** set **)
   407 
   408 section "set";
   409 
   410 qed_goal "finite_set" thy "finite (set xs)" 
   411 	(K [induct_tac "xs" 1, Auto_tac]);
   412 Addsimps[finite_set];
   413 AddSIs[finite_set];
   414 
   415 Goal "set (xs@ys) = (set xs Un set ys)";
   416 by (induct_tac "xs" 1);
   417 by Auto_tac;
   418 qed "set_append";
   419 Addsimps[set_append];
   420 
   421 Goal "set l <= set (x#l)";
   422 by Auto_tac;
   423 qed "set_subset_Cons";
   424 
   425 Goal "(set xs = {}) = (xs = [])";
   426 by (induct_tac "xs" 1);
   427 by Auto_tac;
   428 qed "set_empty";
   429 Addsimps [set_empty];
   430 
   431 Goal "set(rev xs) = set(xs)";
   432 by (induct_tac "xs" 1);
   433 by Auto_tac;
   434 qed "set_rev";
   435 Addsimps [set_rev];
   436 
   437 Goal "set(map f xs) = f``(set xs)";
   438 by (induct_tac "xs" 1);
   439 by Auto_tac;
   440 qed "set_map";
   441 Addsimps [set_map];
   442 
   443 Goal "(x : set (filter P xs)) = (x : set xs & P x)";
   444 by (induct_tac "xs" 1);
   445 by Auto_tac;
   446 qed "in_set_filter";
   447 Addsimps [in_set_filter];
   448 
   449 Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)";
   450 by (induct_tac "xs" 1);
   451  by (Simp_tac 1);
   452 by (Asm_simp_tac 1);
   453 by (rtac iffI 1);
   454 by (blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1);
   455 by (REPEAT(etac exE 1));
   456 by (exhaust_tac "ys" 1);
   457 by Auto_tac;
   458 qed "in_set_conv_decomp";
   459 
   460 (* eliminate `lists' in favour of `set' *)
   461 
   462 Goal "(xs : lists A) = (!x : set xs. x : A)";
   463 by (induct_tac "xs" 1);
   464 by Auto_tac;
   465 qed "in_lists_conv_set";
   466 
   467 bind_thm("in_listsD",in_lists_conv_set RS iffD1);
   468 AddSDs [in_listsD];
   469 bind_thm("in_listsI",in_lists_conv_set RS iffD2);
   470 AddSIs [in_listsI];
   471 
   472 (** list_all **)
   473 
   474 section "list_all";
   475 
   476 Goal "list_all P (xs@ys) = (list_all P xs & list_all P ys)";
   477 by (induct_tac "xs" 1);
   478 by Auto_tac;
   479 qed "list_all_append";
   480 Addsimps [list_all_append];
   481 
   482 Goal "list_all P xs = (!x. x mem xs --> P x)";
   483 by (induct_tac "xs" 1);
   484 by Auto_tac;
   485 qed "list_all_conv";
   486 
   487 
   488 (** filter **)
   489 
   490 section "filter";
   491 
   492 Goal "filter P (xs@ys) = filter P xs @ filter P ys";
   493 by (induct_tac "xs" 1);
   494 by Auto_tac;
   495 qed "filter_append";
   496 Addsimps [filter_append];
   497 
   498 Goal "filter (%x. True) xs = xs";
   499 by (induct_tac "xs" 1);
   500 by Auto_tac;
   501 qed "filter_True";
   502 Addsimps [filter_True];
   503 
   504 Goal "filter (%x. False) xs = []";
   505 by (induct_tac "xs" 1);
   506 by Auto_tac;
   507 qed "filter_False";
   508 Addsimps [filter_False];
   509 
   510 Goal "length (filter P xs) <= length xs";
   511 by (induct_tac "xs" 1);
   512 by Auto_tac;
   513 qed "length_filter";
   514 Addsimps[length_filter];
   515 
   516 Goal "set (filter P xs) <= set xs";
   517 by Auto_tac;
   518 qed "filter_is_subset";
   519 Addsimps [filter_is_subset];
   520 
   521 
   522 section "concat";
   523 
   524 Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
   525 by (induct_tac "xs" 1);
   526 by Auto_tac;
   527 qed"concat_append";
   528 Addsimps [concat_append];
   529 
   530 Goal "(concat xss = []) = (!xs:set xss. xs=[])";
   531 by (induct_tac "xss" 1);
   532 by Auto_tac;
   533 qed "concat_eq_Nil_conv";
   534 AddIffs [concat_eq_Nil_conv];
   535 
   536 Goal "([] = concat xss) = (!xs:set xss. xs=[])";
   537 by (induct_tac "xss" 1);
   538 by Auto_tac;
   539 qed "Nil_eq_concat_conv";
   540 AddIffs [Nil_eq_concat_conv];
   541 
   542 Goal  "set(concat xs) = Union(set `` set xs)";
   543 by (induct_tac "xs" 1);
   544 by Auto_tac;
   545 qed"set_concat";
   546 Addsimps [set_concat];
   547 
   548 Goal "map f (concat xs) = concat (map (map f) xs)"; 
   549 by (induct_tac "xs" 1);
   550 by Auto_tac;
   551 qed "map_concat";
   552 
   553 Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
   554 by (induct_tac "xs" 1);
   555 by Auto_tac;
   556 qed"filter_concat"; 
   557 
   558 Goal "rev(concat xs) = concat (map rev (rev xs))";
   559 by (induct_tac "xs" 1);
   560 by Auto_tac;
   561 qed "rev_concat";
   562 
   563 (** nth **)
   564 
   565 section "nth";
   566 
   567 Goal "!xs. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
   568 by (induct_tac "n" 1);
   569  by (Asm_simp_tac 1);
   570  by (rtac allI 1);
   571  by (exhaust_tac "xs" 1);
   572   by Auto_tac;
   573 qed_spec_mp "nth_append";
   574 
   575 Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
   576 by (induct_tac "xs" 1);
   577 (* case [] *)
   578 by (Asm_full_simp_tac 1);
   579 (* case x#xl *)
   580 by (rtac allI 1);
   581 by (induct_tac "n" 1);
   582 by Auto_tac;
   583 qed_spec_mp "nth_map";
   584 Addsimps [nth_map];
   585 
   586 Goal "!n. n < length xs --> list_all P xs --> P(xs!n)";
   587 by (induct_tac "xs" 1);
   588 (* case [] *)
   589 by (Simp_tac 1);
   590 (* case x#xl *)
   591 by (rtac allI 1);
   592 by (induct_tac "n" 1);
   593 by Auto_tac;
   594 qed_spec_mp "list_all_nth";
   595 
   596 Goal "!n. n < length xs --> xs!n mem xs";
   597 by (induct_tac "xs" 1);
   598 (* case [] *)
   599 by (Simp_tac 1);
   600 (* case x#xl *)
   601 by (rtac allI 1);
   602 by (induct_tac "n" 1);
   603 (* case 0 *)
   604 by (Asm_full_simp_tac 1);
   605 (* case Suc x *)
   606 by (Asm_full_simp_tac 1);
   607 qed_spec_mp "nth_mem";
   608 Addsimps [nth_mem];
   609 
   610 (** list update **)
   611 
   612 section "list update";
   613 
   614 Goal "!i. length(xs[i:=x]) = length xs";
   615 by (induct_tac "xs" 1);
   616 by (Simp_tac 1);
   617 by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
   618 qed_spec_mp "length_list_update";
   619 Addsimps [length_list_update];
   620 
   621 
   622 (** last & butlast **)
   623 
   624 Goal "last(xs@[x]) = x";
   625 by (induct_tac "xs" 1);
   626 by Auto_tac;
   627 qed "last_snoc";
   628 Addsimps [last_snoc];
   629 
   630 Goal "butlast(xs@[x]) = xs";
   631 by (induct_tac "xs" 1);
   632 by Auto_tac;
   633 qed "butlast_snoc";
   634 Addsimps [butlast_snoc];
   635 
   636 Goal "length(butlast xs) = length xs - 1";
   637 by (res_inst_tac [("xs","xs")] rev_induct 1);
   638 by Auto_tac;
   639 qed "length_butlast";
   640 Addsimps [length_butlast];
   641 
   642 Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
   643 by (induct_tac "xs" 1);
   644 by Auto_tac;
   645 qed_spec_mp "butlast_append";
   646 
   647 Goal "x:set(butlast xs) --> x:set xs";
   648 by (induct_tac "xs" 1);
   649 by Auto_tac;
   650 qed_spec_mp "in_set_butlastD";
   651 
   652 Goal "x:set(butlast xs) ==> x:set(butlast(xs@ys))";
   653 by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
   654 by (blast_tac (claset() addDs [in_set_butlastD]) 1);
   655 qed "in_set_butlast_appendI1";
   656 
   657 Goal "x:set(butlast ys) ==> x:set(butlast(xs@ys))";
   658 by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
   659 by (Clarify_tac 1);
   660 by (Full_simp_tac 1);
   661 qed "in_set_butlast_appendI2";
   662 
   663 (** take  & drop **)
   664 section "take & drop";
   665 
   666 Goal "take 0 xs = []";
   667 by (induct_tac "xs" 1);
   668 by Auto_tac;
   669 qed "take_0";
   670 
   671 Goal "drop 0 xs = xs";
   672 by (induct_tac "xs" 1);
   673 by Auto_tac;
   674 qed "drop_0";
   675 
   676 Goal "take (Suc n) (x#xs) = x # take n xs";
   677 by (Simp_tac 1);
   678 qed "take_Suc_Cons";
   679 
   680 Goal "drop (Suc n) (x#xs) = drop n xs";
   681 by (Simp_tac 1);
   682 qed "drop_Suc_Cons";
   683 
   684 Delsimps [take_Cons,drop_Cons];
   685 Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
   686 
   687 Goal "!xs. length(take n xs) = min (length xs) n";
   688 by (induct_tac "n" 1);
   689  by Auto_tac;
   690 by (exhaust_tac "xs" 1);
   691  by Auto_tac;
   692 qed_spec_mp "length_take";
   693 Addsimps [length_take];
   694 
   695 Goal "!xs. length(drop n xs) = (length xs - n)";
   696 by (induct_tac "n" 1);
   697  by Auto_tac;
   698 by (exhaust_tac "xs" 1);
   699  by Auto_tac;
   700 qed_spec_mp "length_drop";
   701 Addsimps [length_drop];
   702 
   703 Goal "!xs. length xs <= n --> take n xs = xs";
   704 by (induct_tac "n" 1);
   705  by Auto_tac;
   706 by (exhaust_tac "xs" 1);
   707  by Auto_tac;
   708 qed_spec_mp "take_all";
   709 
   710 Goal "!xs. length xs <= n --> drop n xs = []";
   711 by (induct_tac "n" 1);
   712  by Auto_tac;
   713 by (exhaust_tac "xs" 1);
   714  by Auto_tac;
   715 qed_spec_mp "drop_all";
   716 
   717 Goal "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
   718 by (induct_tac "n" 1);
   719  by Auto_tac;
   720 by (exhaust_tac "xs" 1);
   721  by Auto_tac;
   722 qed_spec_mp "take_append";
   723 Addsimps [take_append];
   724 
   725 Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
   726 by (induct_tac "n" 1);
   727  by Auto_tac;
   728 by (exhaust_tac "xs" 1);
   729  by Auto_tac;
   730 qed_spec_mp "drop_append";
   731 Addsimps [drop_append];
   732 
   733 Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
   734 by (induct_tac "m" 1);
   735  by Auto_tac;
   736 by (exhaust_tac "xs" 1);
   737  by Auto_tac;
   738 by (exhaust_tac "na" 1);
   739  by Auto_tac;
   740 qed_spec_mp "take_take";
   741 
   742 Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
   743 by (induct_tac "m" 1);
   744  by Auto_tac;
   745 by (exhaust_tac "xs" 1);
   746  by Auto_tac;
   747 qed_spec_mp "drop_drop";
   748 
   749 Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
   750 by (induct_tac "m" 1);
   751  by Auto_tac;
   752 by (exhaust_tac "xs" 1);
   753  by Auto_tac;
   754 qed_spec_mp "take_drop";
   755 
   756 Goal "!xs. take n (map f xs) = map f (take n xs)"; 
   757 by (induct_tac "n" 1);
   758  by Auto_tac;
   759 by (exhaust_tac "xs" 1);
   760  by Auto_tac;
   761 qed_spec_mp "take_map"; 
   762 
   763 Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
   764 by (induct_tac "n" 1);
   765  by Auto_tac;
   766 by (exhaust_tac "xs" 1);
   767  by Auto_tac;
   768 qed_spec_mp "drop_map";
   769 
   770 Goal "!n i. i < n --> (take n xs)!i = xs!i";
   771 by (induct_tac "xs" 1);
   772  by Auto_tac;
   773 by (exhaust_tac "n" 1);
   774  by (Blast_tac 1);
   775 by (exhaust_tac "i" 1);
   776  by Auto_tac;
   777 qed_spec_mp "nth_take";
   778 Addsimps [nth_take];
   779 
   780 Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
   781 by (induct_tac "n" 1);
   782  by Auto_tac;
   783 by (exhaust_tac "xs" 1);
   784  by Auto_tac;
   785 qed_spec_mp "nth_drop";
   786 Addsimps [nth_drop];
   787 
   788 (** takeWhile & dropWhile **)
   789 
   790 section "takeWhile & dropWhile";
   791 
   792 Goal "takeWhile P xs @ dropWhile P xs = xs";
   793 by (induct_tac "xs" 1);
   794 by Auto_tac;
   795 qed "takeWhile_dropWhile_id";
   796 Addsimps [takeWhile_dropWhile_id];
   797 
   798 Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
   799 by (induct_tac "xs" 1);
   800 by Auto_tac;
   801 bind_thm("takeWhile_append1", conjI RS (result() RS mp));
   802 Addsimps [takeWhile_append1];
   803 
   804 Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
   805 by (induct_tac "xs" 1);
   806 by Auto_tac;
   807 bind_thm("takeWhile_append2", ballI RS (result() RS mp));
   808 Addsimps [takeWhile_append2];
   809 
   810 Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
   811 by (induct_tac "xs" 1);
   812 by Auto_tac;
   813 bind_thm("dropWhile_append1", conjI RS (result() RS mp));
   814 Addsimps [dropWhile_append1];
   815 
   816 Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
   817 by (induct_tac "xs" 1);
   818 by Auto_tac;
   819 bind_thm("dropWhile_append2", ballI RS (result() RS mp));
   820 Addsimps [dropWhile_append2];
   821 
   822 Goal "x:set(takeWhile P xs) --> x:set xs & P x";
   823 by (induct_tac "xs" 1);
   824 by Auto_tac;
   825 qed_spec_mp"set_take_whileD";
   826 
   827 qed_goal "zip_Nil_Nil"   thy "zip []     []     = []" (K [Simp_tac 1]);
   828 qed_goal "zip_Cons_Cons" thy "zip (x#xs) (y#ys) = (x,y)#zip xs ys" 
   829 						      (K [Simp_tac 1]);
   830 
   831 
   832 (** foldl **)
   833 section "foldl";
   834 
   835 Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys";
   836 by (induct_tac "xs" 1);
   837 by Auto_tac;
   838 qed_spec_mp "foldl_append";
   839 Addsimps [foldl_append];
   840 
   841 (* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
   842    because it requires an additional transitivity step
   843 *)
   844 Goal "!n::nat. m <= n --> m <= foldl op+ n ns";
   845 by (induct_tac "ns" 1);
   846  by (Simp_tac 1);
   847 by (Asm_full_simp_tac 1);
   848 by (blast_tac (claset() addIs [trans_le_add1]) 1);
   849 qed_spec_mp "start_le_sum";
   850 
   851 Goal "n : set ns ==> n <= foldl op+ 0 ns";
   852 by (auto_tac (claset() addIs [start_le_sum],
   853              simpset() addsimps [in_set_conv_decomp]));
   854 qed "elem_le_sum";
   855 
   856 Goal "!m. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))";
   857 by (induct_tac "ns" 1);
   858 by Auto_tac;
   859 qed_spec_mp "sum_eq_0_conv";
   860 AddIffs [sum_eq_0_conv];
   861 
   862 (** upto **)
   863 
   864 (* Does not terminate! *)
   865 Goal "[i..j(] = (if i<j then i#[Suc i..j(] else [])";
   866 by(induct_tac "j" 1);
   867 by Auto_tac;
   868 by(REPEAT(trans_tac 1));
   869 qed "upt_rec";
   870 
   871 Goal "j<=i ==> [i..j(] = []";
   872 by(stac upt_rec 1);
   873 by(asm_simp_tac (simpset() addSolver cut_trans_tac) 1);
   874 qed "upt_conv_Nil";
   875 Addsimps [upt_conv_Nil];
   876 
   877 Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]";
   878 by (Asm_simp_tac 1);
   879 qed "upt_Suc";
   880 
   881 Goal "i<j ==> [i..j(] = i#[Suc i..j(]";
   882 br trans 1;
   883 by(stac upt_rec 1);
   884 br refl 2;
   885 by (Asm_simp_tac 1);
   886 qed "upt_conv_Cons";
   887 
   888 Goal "length [i..j(] = j-i";
   889 by(induct_tac "j" 1);
   890  by (Simp_tac 1);
   891 by(asm_simp_tac (simpset() addsimps [Suc_diff_le] addSolver cut_trans_tac) 1);
   892 qed "length_upt";
   893 Addsimps [length_upt];
   894 
   895 Goal "i+k < j --> [i..j(] ! k = i+k";
   896 by(induct_tac "j" 1);
   897  by(Simp_tac 1);
   898 by(asm_simp_tac (simpset() addsimps ([nth_append,less_diff_conv]@add_ac)
   899                            addSolver cut_trans_tac) 1);
   900 br conjI 1;
   901  by(Clarify_tac 1);
   902  bd add_lessD1 1;
   903  by(trans_tac 1);
   904 by(Clarify_tac 1);
   905 br conjI 1;
   906  by(Clarify_tac 1);
   907  by(subgoal_tac "n=i+k" 1);
   908   by(Asm_full_simp_tac 1);
   909  by(trans_tac 1);
   910 by(Clarify_tac 1);
   911 by(subgoal_tac "n=i+k" 1);
   912  by(Asm_full_simp_tac 1);
   913 by(trans_tac 1);
   914 qed_spec_mp "nth_upt";
   915 Addsimps [nth_upt];
   916 
   917 
   918 (** nodups & remdups **)
   919 section "nodups & remdups";
   920 
   921 Goal "set(remdups xs) = set xs";
   922 by (induct_tac "xs" 1);
   923  by (Simp_tac 1);
   924 by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
   925 qed "set_remdups";
   926 Addsimps [set_remdups];
   927 
   928 Goal "nodups(remdups xs)";
   929 by (induct_tac "xs" 1);
   930 by Auto_tac;
   931 qed "nodups_remdups";
   932 
   933 Goal "nodups xs --> nodups (filter P xs)";
   934 by (induct_tac "xs" 1);
   935 by Auto_tac;
   936 qed_spec_mp "nodups_filter";
   937 
   938 (** replicate **)
   939 section "replicate";
   940 
   941 Goal "set(replicate (Suc n) x) = {x}";
   942 by (induct_tac "n" 1);
   943 by Auto_tac;
   944 val lemma = result();
   945 
   946 Goal "n ~= 0 ==> set(replicate n x) = {x}";
   947 by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
   948 qed "set_replicate";
   949 Addsimps [set_replicate];
   950 
   951 
   952 (*** Lexcicographic orderings on lists ***)
   953 section"Lexcicographic orderings on lists";
   954 
   955 Goal "wf r ==> wf(lexn r n)";
   956 by (induct_tac "n" 1);
   957 by (Simp_tac 1);
   958 by (Simp_tac 1);
   959 by (rtac wf_subset 1);
   960 by (rtac Int_lower1 2);
   961 by (rtac wf_prod_fun_image 1);
   962 by (rtac injI 2);
   963 by (Auto_tac);
   964 qed "wf_lexn";
   965 
   966 Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n";
   967 by (induct_tac "n" 1);
   968 by (Auto_tac);
   969 qed_spec_mp "lexn_length";
   970 
   971 Goalw [lex_def] "wf r ==> wf(lex r)";
   972 by (rtac wf_UN 1);
   973 by (blast_tac (claset() addIs [wf_lexn]) 1);
   974 by (Clarify_tac 1);
   975 by (rename_tac "m n" 1);
   976 by (subgoal_tac "m ~= n" 1);
   977  by (Blast_tac 2);
   978 by (blast_tac (claset() addDs [lexn_length,not_sym]) 1);
   979 qed "wf_lex";
   980 AddSIs [wf_lex];
   981 
   982 Goal
   983  "lexn r n = \
   984 \ {(xs,ys). length xs = n & length ys = n & \
   985 \           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
   986 by (induct_tac "n" 1);
   987  by (Simp_tac 1);
   988  by (Blast_tac 1);
   989 by (asm_full_simp_tac (simpset() delsimps [length_Suc_conv] 
   990 				addsimps [lex_prod_def]) 1);
   991 by (auto_tac (claset(), simpset() delsimps [length_Suc_conv]));
   992   by (Blast_tac 1);
   993  by (rename_tac "a xys x xs' y ys'" 1);
   994  by (res_inst_tac [("x","a#xys")] exI 1);
   995  by (Simp_tac 1);
   996 by (exhaust_tac "xys" 1);
   997  by (ALLGOALS (asm_full_simp_tac (simpset() delsimps [length_Suc_conv])));
   998 by (Blast_tac 1);
   999 qed "lexn_conv";
  1000 
  1001 Goalw [lex_def]
  1002  "lex r = \
  1003 \ {(xs,ys). length xs = length ys & \
  1004 \           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
  1005 by (force_tac (claset(), simpset() delsimps [length_Suc_conv] addsimps [lexn_conv]) 1);
  1006 qed "lex_conv";
  1007 
  1008 Goalw [lexico_def] "wf r ==> wf(lexico r)";
  1009 by (Blast_tac 1);
  1010 qed "wf_lexico";
  1011 AddSIs [wf_lexico];
  1012 
  1013 Goalw
  1014  [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def]
  1015 "lexico r = {(xs,ys). length xs < length ys | \
  1016 \                     length xs = length ys & (xs,ys) : lex r}";
  1017 by (Simp_tac 1);
  1018 qed "lexico_conv";
  1019 
  1020 Goal "([],ys) ~: lex r";
  1021 by (simp_tac (simpset() addsimps [lex_conv]) 1);
  1022 qed "Nil_notin_lex";
  1023 
  1024 Goal "(xs,[]) ~: lex r";
  1025 by (simp_tac (simpset() addsimps [lex_conv]) 1);
  1026 qed "Nil2_notin_lex";
  1027 
  1028 AddIffs [Nil_notin_lex,Nil2_notin_lex];
  1029 
  1030 Goal "((x#xs,y#ys) : lex r) = \
  1031 \     ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)";
  1032 by (simp_tac (simpset() addsimps [lex_conv]) 1);
  1033 by (rtac iffI 1);
  1034  by (blast_tac (claset() addIs [Cons_eq_appendI]) 2);
  1035 by (REPEAT(eresolve_tac [conjE, exE] 1));
  1036 by (exhaust_tac "xys" 1);
  1037 by (Asm_full_simp_tac 1);
  1038 by (Asm_full_simp_tac 1);
  1039 by (Blast_tac 1);
  1040 qed "Cons_in_lex";
  1041 AddIffs [Cons_in_lex];