src/HOL/Induct/LList.ML
author oheimb
Tue Apr 21 17:23:24 1998 +0200 (1998-04-21)
changeset 4818 90dab9f7d81e
parent 4521 c7f56322a84b
child 4831 dae4d63a1318
permissions -rw-r--r--
split_all_tac is now added to claset() _before_ other safe tactics
     1 (*  Title:      HOL/ex/LList
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1993  University of Cambridge
     5 
     6 SHOULD LListD_Fun_CONS_I, etc., be equations (for rewriting)?
     7 *)
     8 
     9 open LList;
    10 
    11 bind_thm ("UN1_I", UNIV_I RS UN_I);
    12 
    13 (** Simplification **)
    14 
    15 simpset_ref() := simpset() addsplits [expand_split, expand_sum_case];
    16 
    17 
    18 (*This justifies using llist in other recursive type definitions*)
    19 goalw LList.thy llist.defs "!!A B. A<=B ==> llist(A) <= llist(B)";
    20 by (rtac gfp_mono 1);
    21 by (REPEAT (ares_tac basic_monos 1));
    22 qed "llist_mono";
    23 
    24 
    25 goal LList.thy "llist(A) = {Numb(0)} <+> (A <*> llist(A))";
    26 let val rew = rewrite_rule [NIL_def, CONS_def] in  
    27 by (fast_tac (claset() addSIs (map rew llist.intrs)
    28                       addEs [rew llist.elim]) 1)
    29 end;
    30 qed "llist_unfold";
    31 
    32 
    33 (*** Type checking by coinduction, using list_Fun 
    34      THE COINDUCTIVE DEFINITION PACKAGE COULD DO THIS!
    35 ***)
    36 
    37 goalw LList.thy [list_Fun_def]
    38     "!!M. [| M : X;  X <= list_Fun A (X Un llist(A)) |] ==>  M : llist(A)";
    39 by (etac llist.coinduct 1);
    40 by (etac (subsetD RS CollectD) 1);
    41 by (assume_tac 1);
    42 qed "llist_coinduct";
    43 
    44 goalw LList.thy [list_Fun_def, NIL_def] "NIL: list_Fun A X";
    45 by (Fast_tac 1);
    46 qed "list_Fun_NIL_I";
    47 AddIffs [list_Fun_NIL_I];
    48 
    49 goalw LList.thy [list_Fun_def,CONS_def]
    50     "!!M N. [| M: A;  N: X |] ==> CONS M N : list_Fun A X";
    51 by (Fast_tac 1);
    52 qed "list_Fun_CONS_I";
    53 Addsimps [list_Fun_CONS_I];
    54 AddSIs   [list_Fun_CONS_I];
    55 
    56 (*Utilise the "strong" part, i.e. gfp(f)*)
    57 goalw LList.thy (llist.defs @ [list_Fun_def])
    58     "!!M N. M: llist(A) ==> M : list_Fun A (X Un llist(A))";
    59 by (etac (llist.mono RS gfp_fun_UnI2) 1);
    60 qed "list_Fun_llist_I";
    61 
    62 (*** LList_corec satisfies the desired recurion equation ***)
    63 
    64 (*A continuity result?*)
    65 goalw LList.thy [CONS_def] "CONS M (UN x. f(x)) = (UN x. CONS M (f x))";
    66 by (simp_tac (simpset() addsimps [In1_UN1, Scons_UN1_y]) 1);
    67 qed "CONS_UN1";
    68 
    69 (*UNUSED; obsolete?
    70 goal Prod.thy "split p (%x y. UN z. f x y z) = (UN z. split p (%x y. f x y z))";
    71 by (simp_tac (simpset() addsplits [expand_split]) 1);
    72 qed "split_UN1";
    73 
    74 goal Sum.thy "sum_case s f (%y. UN z. g y z) = (UN z. sum_case s f (%y. g y z))";
    75 by (simp_tac (simpset() addsplits [expand_sum_case]) 1);
    76 qed "sum_case2_UN1";
    77 *)
    78 
    79 val prems = goalw LList.thy [CONS_def]
    80     "[| M<=M';  N<=N' |] ==> CONS M N <= CONS M' N'";
    81 by (REPEAT (resolve_tac ([In1_mono,Scons_mono]@prems) 1));
    82 qed "CONS_mono";
    83 
    84 Addsimps [LList_corec_fun_def RS def_nat_rec_0,
    85           LList_corec_fun_def RS def_nat_rec_Suc];
    86 
    87 (** The directions of the equality are proved separately **)
    88 
    89 goalw LList.thy [LList_corec_def]
    90     "LList_corec a f <= sum_case (%u. NIL) \
    91 \                          (split(%z w. CONS z (LList_corec w f))) (f a)";
    92 by (rtac UN_least 1);
    93 by (exhaust_tac "k" 1);
    94 by (ALLGOALS Asm_simp_tac);
    95 by (REPEAT (resolve_tac [allI, impI, subset_refl RS CONS_mono, 
    96 			 UNIV_I RS UN_upper] 1));
    97 qed "LList_corec_subset1";
    98 
    99 goalw LList.thy [LList_corec_def]
   100     "sum_case (%u. NIL) (split(%z w. CONS z (LList_corec w f))) (f a) <= \
   101 \    LList_corec a f";
   102 by (simp_tac (simpset() addsimps [CONS_UN1]) 1);
   103 by Safe_tac;
   104 by (ALLGOALS (res_inst_tac [("a","Suc(?k)")] UN_I));
   105 by (ALLGOALS Asm_simp_tac);
   106 qed "LList_corec_subset2";
   107 
   108 (*the recursion equation for LList_corec -- NOT SUITABLE FOR REWRITING!*)
   109 goal LList.thy
   110     "LList_corec a f = sum_case (%u. NIL) \
   111 \                           (split(%z w. CONS z (LList_corec w f))) (f a)";
   112 by (REPEAT (resolve_tac [equalityI, LList_corec_subset1, 
   113                          LList_corec_subset2] 1));
   114 qed "LList_corec";
   115 
   116 (*definitional version of same*)
   117 val [rew] = goal LList.thy
   118     "[| !!x. h(x) == LList_corec x f |] ==>     \
   119 \    h(a) = sum_case (%u. NIL) (split(%z w. CONS z (h w))) (f a)";
   120 by (rewtac rew);
   121 by (rtac LList_corec 1);
   122 qed "def_LList_corec";
   123 
   124 (*A typical use of co-induction to show membership in the gfp. 
   125   Bisimulation is  range(%x. LList_corec x f) *)
   126 goal LList.thy "LList_corec a f : llist({u. True})";
   127 by (res_inst_tac [("X", "range(%x. LList_corec x ?g)")] llist_coinduct 1);
   128 by (rtac rangeI 1);
   129 by Safe_tac;
   130 by (stac LList_corec 1);
   131 by (Simp_tac 1);
   132 qed "LList_corec_type";
   133 
   134 (*Lemma for the proof of llist_corec*)
   135 goal LList.thy
   136    "LList_corec a (%z. sum_case Inl (split(%v w. Inr((Leaf(v),w)))) (f z)) : \
   137 \   llist(range Leaf)";
   138 by (res_inst_tac [("X", "range(%x. LList_corec x ?g)")] llist_coinduct 1);
   139 by (rtac rangeI 1);
   140 by Safe_tac;
   141 by (stac LList_corec 1);
   142 by (Asm_simp_tac 1);
   143 qed "LList_corec_type2";
   144 
   145 
   146 (**** llist equality as a gfp; the bisimulation principle ****)
   147 
   148 (*This theorem is actually used, unlike the many similar ones in ZF*)
   149 goal LList.thy "LListD(r) = diag({Numb(0)}) <++> (r <**> LListD(r))";
   150 let val rew = rewrite_rule [NIL_def, CONS_def] in  
   151 by (fast_tac (claset() addSIs (map rew LListD.intrs)
   152                       addEs [rew LListD.elim]) 1)
   153 end;
   154 qed "LListD_unfold";
   155 
   156 goal LList.thy "!M N. (M,N) : LListD(diag(A)) --> ntrunc k M = ntrunc k N";
   157 by (res_inst_tac [("n", "k")] less_induct 1);
   158 by (safe_tac ((claset_of Fun.thy) delrules [equalityI]));
   159 by (etac LListD.elim 1);
   160 by (safe_tac (claset_of Prod.thy delrules [equalityI] addSEs [diagE]));
   161 by (res_inst_tac [("n", "n")] natE 1);
   162 by (Asm_simp_tac 1);
   163 by (rename_tac "n'" 1);
   164 by (res_inst_tac [("n", "n'")] natE 1);
   165 by (asm_simp_tac (simpset() addsimps [CONS_def]) 1);
   166 by (asm_simp_tac (simpset() addsimps [CONS_def, less_Suc_eq]) 1);
   167 qed "LListD_implies_ntrunc_equality";
   168 
   169 (*The domain of the LListD relation*)
   170 goalw LList.thy (llist.defs @ [NIL_def, CONS_def])
   171     "fst``LListD(diag(A)) <= llist(A)";
   172 by (rtac gfp_upperbound 1);
   173 (*avoids unfolding LListD on the rhs*)
   174 by (res_inst_tac [("P", "%x. fst``x <= ?B")] (LListD_unfold RS ssubst) 1);
   175 by (Simp_tac 1);
   176 by (Fast_tac 1);
   177 qed "fst_image_LListD";
   178 
   179 (*This inclusion justifies the use of coinduction to show M=N*)
   180 goal LList.thy "LListD(diag(A)) <= diag(llist(A))";
   181 by (rtac subsetI 1);
   182 by (res_inst_tac [("p","x")] PairE 1);
   183 by Safe_tac;
   184 by (rtac diag_eqI 1);
   185 by (rtac (LListD_implies_ntrunc_equality RS spec RS spec RS mp RS 
   186           ntrunc_equality) 1);
   187 by (assume_tac 1);
   188 by (etac (fst_imageI RS (fst_image_LListD RS subsetD)) 1);
   189 qed "LListD_subset_diag";
   190 
   191 
   192 (** Coinduction, using LListD_Fun
   193     THE COINDUCTIVE DEFINITION PACKAGE COULD DO THIS!
   194  **)
   195 
   196 goalw thy [LListD_Fun_def] "!!A B. A<=B ==> LListD_Fun r A <= LListD_Fun r B";
   197 by (REPEAT (ares_tac basic_monos 1));
   198 qed "LListD_Fun_mono";
   199 
   200 goalw LList.thy [LListD_Fun_def]
   201     "!!M. [| M : X;  X <= LListD_Fun r (X Un LListD(r)) |] ==>  M : LListD(r)";
   202 by (etac LListD.coinduct 1);
   203 by (etac (subsetD RS CollectD) 1);
   204 by (assume_tac 1);
   205 qed "LListD_coinduct";
   206 
   207 goalw LList.thy [LListD_Fun_def,NIL_def] "(NIL,NIL) : LListD_Fun r s";
   208 by (Fast_tac 1);
   209 qed "LListD_Fun_NIL_I";
   210 
   211 goalw LList.thy [LListD_Fun_def,CONS_def]
   212  "!!x. [| x:A;  (M,N):s |] ==> (CONS x M, CONS x N) : LListD_Fun (diag A) s";
   213 by (Fast_tac 1);
   214 qed "LListD_Fun_CONS_I";
   215 
   216 (*Utilise the "strong" part, i.e. gfp(f)*)
   217 goalw LList.thy (LListD.defs @ [LListD_Fun_def])
   218     "!!M N. M: LListD(r) ==> M : LListD_Fun r (X Un LListD(r))";
   219 by (etac (LListD.mono RS gfp_fun_UnI2) 1);
   220 qed "LListD_Fun_LListD_I";
   221 
   222 
   223 (*This converse inclusion helps to strengthen LList_equalityI*)
   224 goal LList.thy "diag(llist(A)) <= LListD(diag(A))";
   225 by (rtac subsetI 1);
   226 by (etac LListD_coinduct 1);
   227 by (rtac subsetI 1);
   228 by (etac diagE 1);
   229 by (etac ssubst 1);
   230 by (eresolve_tac [llist.elim] 1);
   231 by (ALLGOALS
   232     (asm_simp_tac (simpset() addsimps [diagI, LListD_Fun_NIL_I,
   233 				       LListD_Fun_CONS_I])));
   234 qed "diag_subset_LListD";
   235 
   236 goal LList.thy "LListD(diag(A)) = diag(llist(A))";
   237 by (REPEAT (resolve_tac [equalityI, LListD_subset_diag, 
   238                          diag_subset_LListD] 1));
   239 qed "LListD_eq_diag";
   240 
   241 goal LList.thy 
   242     "!!M N. M: llist(A) ==> (M,M) : LListD_Fun (diag A) (X Un diag(llist(A)))";
   243 by (rtac (LListD_eq_diag RS subst) 1);
   244 by (rtac LListD_Fun_LListD_I 1);
   245 by (asm_simp_tac (simpset() addsimps [LListD_eq_diag, diagI]) 1);
   246 qed "LListD_Fun_diag_I";
   247 
   248 
   249 (** To show two LLists are equal, exhibit a bisimulation! 
   250       [also admits true equality]
   251    Replace "A" by some particular set, like {x.True}??? *)
   252 goal LList.thy 
   253     "!!r. [| (M,N) : r;  r <= LListD_Fun (diag A) (r Un diag(llist(A))) \
   254 \         |] ==>  M=N";
   255 by (rtac (LListD_subset_diag RS subsetD RS diagE) 1);
   256 by (etac LListD_coinduct 1);
   257 by (asm_simp_tac (simpset() addsimps [LListD_eq_diag]) 1);
   258 by Safe_tac;
   259 qed "LList_equalityI";
   260 
   261 
   262 (*** Finality of llist(A): Uniqueness of functions defined by corecursion ***)
   263 
   264 (*We must remove Pair_eq because it may turn an instance of reflexivity
   265   (h1 b, h2 b) = (h1 ?x17, h2 ?x17) into a conjunction! 
   266   (or strengthen the Solver?) 
   267 *)
   268 Delsimps [Pair_eq];
   269 
   270 (*abstract proof using a bisimulation*)
   271 val [prem1,prem2] = goal LList.thy
   272  "[| !!x. h1(x) = sum_case (%u. NIL) (split(%z w. CONS z (h1 w))) (f x);  \
   273 \    !!x. h2(x) = sum_case (%u. NIL) (split(%z w. CONS z (h2 w))) (f x) |]\
   274 \ ==> h1=h2";
   275 by (rtac ext 1);
   276 (*next step avoids an unknown (and flexflex pair) in simplification*)
   277 by (res_inst_tac [("A", "{u. True}"),
   278                   ("r", "range(%u. (h1(u),h2(u)))")] LList_equalityI 1);
   279 by (rtac rangeI 1);
   280 by Safe_tac;
   281 by (stac prem1 1);
   282 by (stac prem2 1);
   283 by (simp_tac (simpset() addsimps [LListD_Fun_NIL_I,
   284 				  CollectI RS LListD_Fun_CONS_I]) 1);
   285 qed "LList_corec_unique";
   286 
   287 val [prem] = goal LList.thy
   288  "[| !!x. h(x) = sum_case (%u. NIL) (split(%z w. CONS z (h w))) (f x) |] \
   289 \ ==> h = (%x. LList_corec x f)";
   290 by (rtac (LList_corec RS (prem RS LList_corec_unique)) 1);
   291 qed "equals_LList_corec";
   292 
   293 
   294 (** Obsolete LList_corec_unique proof: complete induction, not coinduction **)
   295 
   296 goalw LList.thy [CONS_def] "ntrunc (Suc 0) (CONS M N) = {}";
   297 by (rtac ntrunc_one_In1 1);
   298 qed "ntrunc_one_CONS";
   299 
   300 goalw LList.thy [CONS_def]
   301     "ntrunc (Suc(Suc(k))) (CONS M N) = CONS (ntrunc k M) (ntrunc k N)";
   302 by (Simp_tac 1);
   303 qed "ntrunc_CONS";
   304 
   305 Addsimps [ntrunc_one_CONS, ntrunc_CONS];
   306 
   307 
   308 val [prem1,prem2] = goal LList.thy
   309  "[| !!x. h1(x) = sum_case (%u. NIL) (split(%z w. CONS z (h1 w))) (f x);  \
   310 \    !!x. h2(x) = sum_case (%u. NIL) (split(%z w. CONS z (h2 w))) (f x) |]\
   311 \ ==> h1=h2";
   312 by (rtac (ntrunc_equality RS ext) 1);
   313 by (rename_tac "x k" 1);
   314 by (res_inst_tac [("x", "x")] spec 1);
   315 by (res_inst_tac [("n", "k")] less_induct 1);
   316 by (rename_tac "n" 1);
   317 by (rtac allI 1);
   318 by (rename_tac "y" 1);
   319 by (stac prem1 1);
   320 by (stac prem2 1);
   321 by (simp_tac (simpset() addsplits [expand_sum_case]) 1);
   322 by (strip_tac 1);
   323 by (res_inst_tac [("n", "n")] natE 1);
   324 by (rename_tac "m" 2);
   325 by (res_inst_tac [("n", "m")] natE 2);
   326 by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq])));
   327 result();
   328 
   329 
   330 (*** Lconst -- defined directly using lfp, but equivalent to a LList_corec ***)
   331 
   332 goal LList.thy "mono(CONS(M))";
   333 by (REPEAT (ares_tac [monoI, subset_refl, CONS_mono] 1));
   334 qed "Lconst_fun_mono";
   335 
   336 (* Lconst(M) = CONS M (Lconst M) *)
   337 bind_thm ("Lconst", (Lconst_fun_mono RS (Lconst_def RS def_lfp_Tarski)));
   338 
   339 (*A typical use of co-induction to show membership in the gfp.
   340   The containing set is simply the singleton {Lconst(M)}. *)
   341 goal LList.thy "!!M A. M:A ==> Lconst(M): llist(A)";
   342 by (rtac (singletonI RS llist_coinduct) 1);
   343 by Safe_tac;
   344 by (res_inst_tac [("P", "%u. u: ?A")] (Lconst RS ssubst) 1);
   345 by (REPEAT (ares_tac [list_Fun_CONS_I, singletonI, UnI1] 1));
   346 qed "Lconst_type";
   347 
   348 goal LList.thy "Lconst(M) = LList_corec M (%x. Inr((x,x)))";
   349 by (rtac (equals_LList_corec RS fun_cong) 1);
   350 by (Simp_tac 1);
   351 by (rtac Lconst 1);
   352 qed "Lconst_eq_LList_corec";
   353 
   354 (*Thus we could have used gfp in the definition of Lconst*)
   355 goal LList.thy "gfp(%N. CONS M N) = LList_corec M (%x. Inr((x,x)))";
   356 by (rtac (equals_LList_corec RS fun_cong) 1);
   357 by (Simp_tac 1);
   358 by (rtac (Lconst_fun_mono RS gfp_Tarski) 1);
   359 qed "gfp_Lconst_eq_LList_corec";
   360 
   361 
   362 (*** Isomorphisms ***)
   363 
   364 goal LList.thy "inj(Rep_llist)";
   365 by (rtac inj_inverseI 1);
   366 by (rtac Rep_llist_inverse 1);
   367 qed "inj_Rep_llist";
   368 
   369 goal LList.thy "inj_onto Abs_llist (llist(range Leaf))";
   370 by (rtac inj_onto_inverseI 1);
   371 by (etac Abs_llist_inverse 1);
   372 qed "inj_onto_Abs_llist";
   373 
   374 (** Distinctness of constructors **)
   375 
   376 goalw LList.thy [LNil_def,LCons_def] "~ LCons x xs = LNil";
   377 by (rtac (CONS_not_NIL RS (inj_onto_Abs_llist RS inj_onto_contraD)) 1);
   378 by (REPEAT (resolve_tac (llist.intrs @ [rangeI, Rep_llist]) 1));
   379 qed "LCons_not_LNil";
   380 
   381 bind_thm ("LNil_not_LCons", LCons_not_LNil RS not_sym);
   382 
   383 AddIffs [LCons_not_LNil, LNil_not_LCons];
   384 
   385 
   386 (** llist constructors **)
   387 
   388 goalw LList.thy [LNil_def]
   389     "Rep_llist(LNil) = NIL";
   390 by (rtac (llist.NIL_I RS Abs_llist_inverse) 1);
   391 qed "Rep_llist_LNil";
   392 
   393 goalw LList.thy [LCons_def]
   394     "Rep_llist(LCons x l) = CONS (Leaf x) (Rep_llist l)";
   395 by (REPEAT (resolve_tac [llist.CONS_I RS Abs_llist_inverse,
   396                          rangeI, Rep_llist] 1));
   397 qed "Rep_llist_LCons";
   398 
   399 (** Injectiveness of CONS and LCons **)
   400 
   401 goalw LList.thy [CONS_def] "(CONS M N=CONS M' N') = (M=M' & N=N')";
   402 by (fast_tac (claset() addSEs [Scons_inject]) 1);
   403 qed "CONS_CONS_eq2";
   404 
   405 bind_thm ("CONS_inject", (CONS_CONS_eq RS iffD1 RS conjE));
   406 
   407 
   408 (*For reasoning about abstract llist constructors*)
   409 
   410 AddIs ([Rep_llist]@llist.intrs);
   411 AddSDs [inj_onto_Abs_llist RS inj_ontoD,
   412         inj_Rep_llist RS injD, Leaf_inject];
   413 
   414 goalw LList.thy [LCons_def] "(LCons x xs=LCons y ys) = (x=y & xs=ys)";
   415 by (Fast_tac 1);
   416 qed "LCons_LCons_eq";
   417 
   418 AddIffs [LCons_LCons_eq];
   419 
   420 val [major] = goal LList.thy "CONS M N: llist(A) ==> M: A & N: llist(A)";
   421 by (rtac (major RS llist.elim) 1);
   422 by (etac CONS_neq_NIL 1);
   423 by (Fast_tac 1);
   424 qed "CONS_D2";
   425 
   426 
   427 (****** Reasoning about llist(A) ******)
   428 
   429 Addsimps [List_case_NIL, List_case_CONS];
   430 
   431 (*A special case of list_equality for functions over lazy lists*)
   432 val [Mlist,gMlist,NILcase,CONScase] = goal LList.thy
   433  "[| M: llist(A); g(NIL): llist(A);                             \
   434 \    f(NIL)=g(NIL);                                             \
   435 \    !!x l. [| x:A;  l: llist(A) |] ==>                         \
   436 \           (f(CONS x l),g(CONS x l)) :                         \
   437 \               LListD_Fun (diag A) ((%u.(f(u),g(u)))``llist(A) Un  \
   438 \                                   diag(llist(A)))             \
   439 \ |] ==> f(M) = g(M)";
   440 by (rtac LList_equalityI 1);
   441 by (rtac (Mlist RS imageI) 1);
   442 by (rtac image_subsetI 1);
   443 by (etac llist.elim 1);
   444 by (etac ssubst 1);
   445 by (stac NILcase 1);
   446 by (rtac (gMlist RS LListD_Fun_diag_I) 1);
   447 by (etac ssubst 1);
   448 by (REPEAT (ares_tac [CONScase] 1));
   449 qed "LList_fun_equalityI";
   450 
   451 
   452 (*** The functional "Lmap" ***)
   453 
   454 goal LList.thy "Lmap f NIL = NIL";
   455 by (rtac (Lmap_def RS def_LList_corec RS trans) 1);
   456 by (Simp_tac 1);
   457 qed "Lmap_NIL";
   458 
   459 goal LList.thy "Lmap f (CONS M N) = CONS (f M) (Lmap f N)";
   460 by (rtac (Lmap_def RS def_LList_corec RS trans) 1);
   461 by (Simp_tac 1);
   462 qed "Lmap_CONS";
   463 
   464 Addsimps [Lmap_NIL, Lmap_CONS];
   465 
   466 (*Another type-checking proof by coinduction*)
   467 val [major,minor] = goal LList.thy
   468     "[| M: llist(A);  !!x. x:A ==> f(x):B |] ==> Lmap f M: llist(B)";
   469 by (rtac (major RS imageI RS llist_coinduct) 1);
   470 by Safe_tac;
   471 by (etac llist.elim 1);
   472 by (ALLGOALS Asm_simp_tac);
   473 by (REPEAT (ares_tac [list_Fun_NIL_I, list_Fun_CONS_I, 
   474                       minor, imageI, UnI1] 1));
   475 qed "Lmap_type";
   476 
   477 (*This type checking rule synthesises a sufficiently large set for f*)
   478 val [major] = goal LList.thy  "M: llist(A) ==> Lmap f M: llist(f``A)";
   479 by (rtac (major RS Lmap_type) 1);
   480 by (etac imageI 1);
   481 qed "Lmap_type2";
   482 
   483 (** Two easy results about Lmap **)
   484 
   485 val [prem] = goalw LList.thy [o_def]
   486     "M: llist(A) ==> Lmap (f o g) M = Lmap f (Lmap g M)";
   487 by (rtac (prem RS imageI RS LList_equalityI) 1);
   488 by Safe_tac;
   489 by (etac llist.elim 1);
   490 by (ALLGOALS Asm_simp_tac);
   491 by (REPEAT (ares_tac [LListD_Fun_NIL_I, imageI, UnI1,
   492                       rangeI RS LListD_Fun_CONS_I] 1));
   493 qed "Lmap_compose";
   494 
   495 val [prem] = goal LList.thy "M: llist(A) ==> Lmap (%x. x) M = M";
   496 by (rtac (prem RS imageI RS LList_equalityI) 1);
   497 by Safe_tac;
   498 by (etac llist.elim 1);
   499 by (ALLGOALS Asm_simp_tac);
   500 by (REPEAT (ares_tac [LListD_Fun_NIL_I, imageI RS UnI1,
   501                       rangeI RS LListD_Fun_CONS_I] 1));
   502 qed "Lmap_ident";
   503 
   504 
   505 (*** Lappend -- its two arguments cause some complications! ***)
   506 
   507 goalw LList.thy [Lappend_def] "Lappend NIL NIL = NIL";
   508 by (rtac (LList_corec RS trans) 1);
   509 by (Simp_tac 1);
   510 qed "Lappend_NIL_NIL";
   511 
   512 goalw LList.thy [Lappend_def]
   513     "Lappend NIL (CONS N N') = CONS N (Lappend NIL N')";
   514 by (rtac (LList_corec RS trans) 1);
   515 by (Simp_tac 1);
   516 qed "Lappend_NIL_CONS";
   517 
   518 goalw LList.thy [Lappend_def]
   519     "Lappend (CONS M M') N = CONS M (Lappend M' N)";
   520 by (rtac (LList_corec RS trans) 1);
   521 by (Simp_tac 1);
   522 qed "Lappend_CONS";
   523 
   524 Addsimps [llist.NIL_I, Lappend_NIL_NIL, Lappend_NIL_CONS,
   525           Lappend_CONS, LListD_Fun_CONS_I, range_eqI, image_eqI];
   526 
   527 
   528 goal LList.thy "!!M. M: llist(A) ==> Lappend NIL M = M";
   529 by (etac LList_fun_equalityI 1);
   530 by (ALLGOALS Asm_simp_tac);
   531 qed "Lappend_NIL";
   532 
   533 goal LList.thy "!!M. M: llist(A) ==> Lappend M NIL = M";
   534 by (etac LList_fun_equalityI 1);
   535 by (ALLGOALS Asm_simp_tac);
   536 qed "Lappend_NIL2";
   537 
   538 Addsimps [Lappend_NIL, Lappend_NIL2];
   539 
   540 
   541 (** Alternative type-checking proofs for Lappend **)
   542 
   543 (*weak co-induction: bisimulation and case analysis on both variables*)
   544 goal LList.thy
   545     "!!M N. [| M: llist(A); N: llist(A) |] ==> Lappend M N: llist(A)";
   546 by (res_inst_tac
   547     [("X", "UN u:llist(A). UN v: llist(A). {Lappend u v}")] llist_coinduct 1);
   548 by (Fast_tac 1);
   549 by Safe_tac;
   550 by (eres_inst_tac [("a", "u")] llist.elim 1);
   551 by (eres_inst_tac [("a", "v")] llist.elim 1);
   552 by (ALLGOALS Asm_simp_tac);
   553 by (Blast_tac 1);
   554 qed "Lappend_type";
   555 
   556 (*strong co-induction: bisimulation and case analysis on one variable*)
   557 goal LList.thy
   558     "!!M N. [| M: llist(A); N: llist(A) |] ==> Lappend M N: llist(A)";
   559 by (res_inst_tac [("X", "(%u. Lappend u N)``llist(A)")] llist_coinduct 1);
   560 by (etac imageI 1);
   561 by (rtac image_subsetI 1);
   562 by (eres_inst_tac [("a", "x")] llist.elim 1);
   563 by (asm_simp_tac (simpset() addsimps [list_Fun_llist_I]) 1);
   564 by (Asm_simp_tac 1);
   565 qed "Lappend_type";
   566 
   567 (**** Lazy lists as the type 'a llist -- strongly typed versions of above ****)
   568 
   569 (** llist_case: case analysis for 'a llist **)
   570 
   571 Addsimps ([Abs_llist_inverse, Rep_llist_inverse,
   572            Rep_llist, rangeI, inj_Leaf, inv_f_f] @ llist.intrs);
   573 
   574 goalw LList.thy [llist_case_def,LNil_def]  "llist_case c d LNil = c";
   575 by (Simp_tac 1);
   576 qed "llist_case_LNil";
   577 
   578 goalw LList.thy [llist_case_def,LCons_def]
   579     "llist_case c d (LCons M N) = d M N";
   580 by (Simp_tac 1);
   581 qed "llist_case_LCons";
   582 
   583 (*Elimination is case analysis, not induction.*)
   584 val [prem1,prem2] = goalw LList.thy [NIL_def,CONS_def]
   585     "[| l=LNil ==> P;  !!x l'. l=LCons x l' ==> P \
   586 \    |] ==> P";
   587 by (rtac (Rep_llist RS llist.elim) 1);
   588 by (rtac (inj_Rep_llist RS injD RS prem1) 1);
   589 by (stac Rep_llist_LNil 1);
   590 by (assume_tac 1);
   591 by (etac rangeE 1);
   592 by (rtac (inj_Rep_llist RS injD RS prem2) 1);
   593 by (asm_simp_tac (simpset() delsimps [CONS_CONS_eq] 
   594 		            addsimps [Rep_llist_LCons]) 1);
   595 by (etac (Abs_llist_inverse RS ssubst) 1);
   596 by (rtac refl 1);
   597 qed "llistE";
   598 
   599 (** llist_corec: corecursion for 'a llist **)
   600 
   601 goalw LList.thy [llist_corec_def,LNil_def,LCons_def]
   602     "llist_corec a f = sum_case (%u. LNil) \
   603 \                           (split(%z w. LCons z (llist_corec w f))) (f a)";
   604 by (stac LList_corec 1);
   605 by (res_inst_tac [("s","f(a)")] sumE 1);
   606 by (asm_simp_tac (simpset() addsimps [LList_corec_type2]) 1);
   607 by (res_inst_tac [("p","y")] PairE 1);
   608 by (asm_simp_tac (simpset() addsimps [LList_corec_type2]) 1);
   609 (*FIXME: correct case splits usd to be found automatically:
   610 by (ASM_SIMP_TAC(simpset() addsimps [LList_corec_type2]) 1);*)
   611 qed "llist_corec";
   612 
   613 (*definitional version of same*)
   614 val [rew] = goal LList.thy
   615     "[| !!x. h(x) == llist_corec x f |] ==>     \
   616 \    h(a) = sum_case (%u. LNil) (split(%z w. LCons z (h w))) (f a)";
   617 by (rewtac rew);
   618 by (rtac llist_corec 1);
   619 qed "def_llist_corec";
   620 
   621 (**** Proofs about type 'a llist functions ****)
   622 
   623 (*** Deriving llist_equalityI -- llist equality is a bisimulation ***)
   624 
   625 goalw LList.thy [LListD_Fun_def]
   626     "!!r A. r <= (llist A) Times (llist A) ==> \
   627 \           LListD_Fun (diag A) r <= (llist A) Times (llist A)";
   628 by (stac llist_unfold 1);
   629 by (simp_tac (simpset() addsimps [NIL_def, CONS_def]) 1);
   630 by (Fast_tac 1);
   631 qed "LListD_Fun_subset_Sigma_llist";
   632 
   633 goal LList.thy
   634     "prod_fun Rep_llist Rep_llist `` r <= \
   635 \    (llist(range Leaf)) Times (llist(range Leaf))";
   636 by (fast_tac (claset() delrules [image_subsetI]
   637 		       addIs [Rep_llist]) 1);
   638 qed "subset_Sigma_llist";
   639 
   640 val [prem] = goal LList.thy
   641     "r <= (llist(range Leaf)) Times (llist(range Leaf)) ==> \
   642 \    prod_fun (Rep_llist o Abs_llist) (Rep_llist o Abs_llist) `` r <= r";
   643 by Safe_tac;
   644 by (rtac (prem RS subsetD RS SigmaE2) 1);
   645 by (assume_tac 1);
   646 by (asm_simp_tac (simpset() addsimps [Abs_llist_inverse]) 1);
   647 qed "prod_fun_lemma";
   648 
   649 goal LList.thy
   650     "prod_fun Rep_llist  Rep_llist `` range(%x. (x, x)) = \
   651 \    diag(llist(range Leaf))";
   652 by (rtac equalityI 1);
   653 by (fast_tac (claset() addIs [Rep_llist]) 1);
   654 by (fast_tac (claset() delSWrapper "split_all_tac"
   655 		       addSEs [Abs_llist_inverse RS subst]) 1);
   656 qed "prod_fun_range_eq_diag";
   657 
   658 (*Surprisingly hard to prove.  Used with lfilter*)
   659 goalw thy [llistD_Fun_def, prod_fun_def]
   660     "!!A B. A<=B ==> llistD_Fun A <= llistD_Fun B";
   661 by Auto_tac;
   662 by (rtac image_eqI 1);
   663 by (fast_tac (claset() addss (simpset())) 1);
   664 by (blast_tac (claset() addIs [impOfSubs LListD_Fun_mono]) 1);
   665 qed "llistD_Fun_mono";
   666 
   667 (** To show two llists are equal, exhibit a bisimulation! 
   668       [also admits true equality] **)
   669 val [prem1,prem2] = goalw LList.thy [llistD_Fun_def]
   670     "[| (l1,l2) : r;  r <= llistD_Fun(r Un range(%x.(x,x))) |] ==> l1=l2";
   671 by (rtac (inj_Rep_llist RS injD) 1);
   672 by (res_inst_tac [("r", "prod_fun Rep_llist Rep_llist ``r"),
   673                   ("A", "range(Leaf)")] 
   674         LList_equalityI 1);
   675 by (rtac (prem1 RS prod_fun_imageI) 1);
   676 by (rtac (prem2 RS image_mono RS subset_trans) 1);
   677 by (rtac (image_compose RS subst) 1);
   678 by (rtac (prod_fun_compose RS subst) 1);
   679 by (stac image_Un 1);
   680 by (stac prod_fun_range_eq_diag 1);
   681 by (rtac (LListD_Fun_subset_Sigma_llist RS prod_fun_lemma) 1);
   682 by (rtac (subset_Sigma_llist RS Un_least) 1);
   683 by (rtac diag_subset_Sigma 1);
   684 qed "llist_equalityI";
   685 
   686 (** Rules to prove the 2nd premise of llist_equalityI **)
   687 goalw LList.thy [llistD_Fun_def,LNil_def] "(LNil,LNil) : llistD_Fun(r)";
   688 by (rtac (LListD_Fun_NIL_I RS prod_fun_imageI) 1);
   689 qed "llistD_Fun_LNil_I";
   690 
   691 val [prem] = goalw LList.thy [llistD_Fun_def,LCons_def]
   692     "(l1,l2):r ==> (LCons x l1, LCons x l2) : llistD_Fun(r)";
   693 by (rtac (rangeI RS LListD_Fun_CONS_I RS prod_fun_imageI) 1);
   694 by (rtac (prem RS prod_fun_imageI) 1);
   695 qed "llistD_Fun_LCons_I";
   696 
   697 (*Utilise the "strong" part, i.e. gfp(f)*)
   698 goalw LList.thy [llistD_Fun_def]
   699      "!!l. (l,l) : llistD_Fun(r Un range(%x.(x,x)))";
   700 by (rtac (Rep_llist_inverse RS subst) 1);
   701 by (rtac prod_fun_imageI 1);
   702 by (stac image_Un 1);
   703 by (stac prod_fun_range_eq_diag 1);
   704 by (rtac (Rep_llist RS LListD_Fun_diag_I) 1);
   705 qed "llistD_Fun_range_I";
   706 
   707 (*A special case of list_equality for functions over lazy lists*)
   708 val [prem1,prem2] = goal LList.thy
   709     "[| f(LNil)=g(LNil);                                                \
   710 \       !!x l. (f(LCons x l),g(LCons x l)) :                            \
   711 \              llistD_Fun(range(%u. (f(u),g(u))) Un range(%v. (v,v)))   \
   712 \    |] ==> f(l) = (g(l :: 'a llist) :: 'b llist)";
   713 by (res_inst_tac [("r", "range(%u. (f(u),g(u)))")] llist_equalityI 1);
   714 by (rtac rangeI 1);
   715 by (rtac subsetI 1);
   716 by (etac rangeE 1);
   717 by (etac ssubst 1);
   718 by (res_inst_tac [("l", "u")] llistE 1);
   719 by (etac ssubst 1);
   720 by (stac prem1 1);
   721 by (rtac llistD_Fun_range_I 1);
   722 by (etac ssubst 1);
   723 by (rtac prem2 1);
   724 qed "llist_fun_equalityI";
   725 
   726 (*simpset for llist bisimulations*)
   727 Addsimps [llist_case_LNil, llist_case_LCons, 
   728           llistD_Fun_LNil_I, llistD_Fun_LCons_I];
   729 
   730 
   731 (*** The functional "lmap" ***)
   732 
   733 goal LList.thy "lmap f LNil = LNil";
   734 by (rtac (lmap_def RS def_llist_corec RS trans) 1);
   735 by (Simp_tac 1);
   736 qed "lmap_LNil";
   737 
   738 goal LList.thy "lmap f (LCons M N) = LCons (f M) (lmap f N)";
   739 by (rtac (lmap_def RS def_llist_corec RS trans) 1);
   740 by (Simp_tac 1);
   741 qed "lmap_LCons";
   742 
   743 Addsimps [lmap_LNil, lmap_LCons];
   744 
   745 
   746 (** Two easy results about lmap **)
   747 
   748 goal LList.thy "lmap (f o g) l = lmap f (lmap g l)";
   749 by (res_inst_tac [("l","l")] llist_fun_equalityI 1);
   750 by (ALLGOALS Simp_tac);
   751 qed "lmap_compose";
   752 
   753 goal LList.thy "lmap (%x. x) l = l";
   754 by (res_inst_tac [("l","l")] llist_fun_equalityI 1);
   755 by (ALLGOALS Simp_tac);
   756 qed "lmap_ident";
   757 
   758 
   759 (*** iterates -- llist_fun_equalityI cannot be used! ***)
   760 
   761 goal LList.thy "iterates f x = LCons x (iterates f (f x))";
   762 by (rtac (iterates_def RS def_llist_corec RS trans) 1);
   763 by (Simp_tac 1);
   764 qed "iterates";
   765 
   766 goal LList.thy "lmap f (iterates f x) = iterates f (f x)";
   767 by (res_inst_tac [("r", "range(%u.(lmap f (iterates f u),iterates f (f u)))")] 
   768     llist_equalityI 1);
   769 by (rtac rangeI 1);
   770 by Safe_tac;
   771 by (res_inst_tac [("x1", "f(u)")] (iterates RS ssubst) 1);
   772 by (res_inst_tac [("x1", "u")] (iterates RS ssubst) 1);
   773 by (Simp_tac 1);
   774 qed "lmap_iterates";
   775 
   776 goal LList.thy "iterates f x = LCons x (lmap f (iterates f x))";
   777 by (stac lmap_iterates 1);
   778 by (rtac iterates 1);
   779 qed "iterates_lmap";
   780 
   781 (*** A rather complex proof about iterates -- cf Andy Pitts ***)
   782 
   783 (** Two lemmas about natrec n x (%m.g), which is essentially (g^n)(x) **)
   784 
   785 goal LList.thy
   786     "nat_rec (LCons b l) (%m. lmap(f)) n =      \
   787 \    LCons (nat_rec b (%m. f) n) (nat_rec l (%m. lmap(f)) n)";
   788 by (nat_ind_tac "n" 1);
   789 by (ALLGOALS Asm_simp_tac);
   790 qed "fun_power_lmap";
   791 
   792 goal Nat.thy "nat_rec (g x) (%m. g) n = nat_rec x (%m. g) (Suc n)";
   793 by (nat_ind_tac "n" 1);
   794 by (ALLGOALS Asm_simp_tac);
   795 qed "fun_power_Suc";
   796 
   797 val Pair_cong = read_instantiate_sg (sign_of Prod.thy)
   798  [("f","Pair")] (standard(refl RS cong RS cong));
   799 
   800 (*The bisimulation consists of {(lmap(f)^n (h(u)), lmap(f)^n (iterates(f,u)))}
   801   for all u and all n::nat.*)
   802 val [prem] = goal LList.thy
   803     "(!!x. h(x) = LCons x (lmap f (h x))) ==> h = iterates(f)";
   804 by (rtac ext 1);
   805 by (res_inst_tac [("r", 
   806    "UN u. range(%n. (nat_rec (h u) (%m y. lmap f y) n, \
   807 \                    nat_rec (iterates f u) (%m y. lmap f y) n))")] 
   808     llist_equalityI 1);
   809 by (REPEAT (resolve_tac [UN1_I, range_eqI, Pair_cong, nat_rec_0 RS sym] 1));
   810 by (Clarify_tac 1);
   811 by (stac iterates 1);
   812 by (stac prem 1);
   813 by (stac fun_power_lmap 1);
   814 by (stac fun_power_lmap 1);
   815 by (rtac llistD_Fun_LCons_I 1);
   816 by (rtac (lmap_iterates RS subst) 1);
   817 by (stac fun_power_Suc 1);
   818 by (stac fun_power_Suc 1);
   819 by (rtac (UN1_I RS UnI1) 1);
   820 by (rtac rangeI 1);
   821 qed "iterates_equality";
   822 
   823 
   824 (*** lappend -- its two arguments cause some complications! ***)
   825 
   826 goalw LList.thy [lappend_def] "lappend LNil LNil = LNil";
   827 by (rtac (llist_corec RS trans) 1);
   828 by (Simp_tac 1);
   829 qed "lappend_LNil_LNil";
   830 
   831 goalw LList.thy [lappend_def]
   832     "lappend LNil (LCons l l') = LCons l (lappend LNil l')";
   833 by (rtac (llist_corec RS trans) 1);
   834 by (Simp_tac 1);
   835 qed "lappend_LNil_LCons";
   836 
   837 goalw LList.thy [lappend_def]
   838     "lappend (LCons l l') N = LCons l (lappend l' N)";
   839 by (rtac (llist_corec RS trans) 1);
   840 by (Simp_tac 1);
   841 qed "lappend_LCons";
   842 
   843 Addsimps [lappend_LNil_LNil, lappend_LNil_LCons, lappend_LCons];
   844 
   845 goal LList.thy "lappend LNil l = l";
   846 by (res_inst_tac [("l","l")] llist_fun_equalityI 1);
   847 by (ALLGOALS Simp_tac);
   848 qed "lappend_LNil";
   849 
   850 goal LList.thy "lappend l LNil = l";
   851 by (res_inst_tac [("l","l")] llist_fun_equalityI 1);
   852 by (ALLGOALS Simp_tac);
   853 qed "lappend_LNil2";
   854 
   855 Addsimps [lappend_LNil, lappend_LNil2];
   856 
   857 (*The infinite first argument blocks the second*)
   858 goal LList.thy "lappend (iterates f x) N = iterates f x";
   859 by (res_inst_tac [("r", "range(%u.(lappend (iterates f u) N,iterates f u))")] 
   860     llist_equalityI 1);
   861 by (rtac rangeI 1);
   862 by Safe_tac;
   863 by (stac iterates 1);
   864 by (Simp_tac 1);
   865 qed "lappend_iterates";
   866 
   867 (** Two proofs that lmap distributes over lappend **)
   868 
   869 (*Long proof requiring case analysis on both both arguments*)
   870 goal LList.thy "lmap f (lappend l n) = lappend (lmap f l) (lmap f n)";
   871 by (res_inst_tac 
   872     [("r",  
   873       "UN n. range(%l.(lmap f (lappend l n),lappend (lmap f l) (lmap f n)))")] 
   874     llist_equalityI 1);
   875 by (rtac UN1_I 1);
   876 by (rtac rangeI 1);
   877 by Safe_tac;
   878 by (res_inst_tac [("l", "l")] llistE 1);
   879 by (res_inst_tac [("l", "n")] llistE 1);
   880 by (ALLGOALS Asm_simp_tac);
   881 by (REPEAT_SOME (ares_tac [llistD_Fun_LCons_I, UN1_I RS UnI1, rangeI]));
   882 qed "lmap_lappend_distrib";
   883 
   884 (*Shorter proof of theorem above using llist_equalityI as strong coinduction*)
   885 goal LList.thy "lmap f (lappend l n) = lappend (lmap f l) (lmap f n)";
   886 by (res_inst_tac [("l","l")] llist_fun_equalityI 1);
   887 by (Simp_tac 1);
   888 by (Simp_tac 1);
   889 qed "lmap_lappend_distrib";
   890 
   891 (*Without strong coinduction, three case analyses might be needed*)
   892 goal LList.thy "lappend (lappend l1 l2) l3 = lappend l1 (lappend l2 l3)";
   893 by (res_inst_tac [("l","l1")] llist_fun_equalityI 1);
   894 by (Simp_tac 1);
   895 by (Simp_tac 1);
   896 qed "lappend_assoc";