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