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