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