src/HOLCF/domain/theorems.ML
author huffman
Tue Jun 14 03:35:15 2005 +0200 (2005-06-14)
changeset 16385 a9dec1969348
parent 16321 ef32a42f4079
child 16394 495dbcd4f4c9
permissions -rw-r--r--
up_eq and up_less in default simpset now
     1 (*  Title:      HOLCF/domain/theorems.ML
     2     ID:         $Id$
     3     Author:     David von Oheimb
     4                 New proofs/tactics by Brian Huffman
     5 
     6 Proof generator for domain section.
     7 *)
     8 
     9 
    10 structure Domain_Theorems = struct
    11 
    12 local
    13 
    14 open Domain_Library;
    15 infixr 0 ===>;infixr 0 ==>;infix 0 == ; 
    16 infix 1 ===; infix 1 ~= ; infix 1 <<; infix 1 ~<<;
    17 infix 9 `   ; infix 9 `% ; infix 9 `%%; infixr 9 oo;
    18 
    19 (* ----- general proof facilities ------------------------------------------- *)
    20 
    21 fun inferT sg pre_tm =
    22   #1 (Sign.infer_types (Sign.pp sg) sg (K NONE) (K NONE) [] true ([pre_tm],propT));
    23 
    24 fun pg'' thy defs t = let val sg = sign_of thy;
    25                           val ct = Thm.cterm_of sg (inferT sg t);
    26                       in prove_goalw_cterm defs ct end;
    27 fun pg'  thy defs t tacsf=pg'' thy defs t (fn []   => tacsf 
    28                                 | prems=> (cut_facts_tac prems 1)::tacsf);
    29 
    30 fun case_UU_tac rews i v =      case_tac (v^"=UU") i THEN
    31                                 asm_simp_tac (HOLCF_ss addsimps rews) i;
    32 
    33 val chain_tac = REPEAT_DETERM o resolve_tac 
    34                 [chain_iterate, ch2ch_Rep_CFunR, ch2ch_Rep_CFunL];
    35 
    36 (* ----- general proofs ----------------------------------------------------- *)
    37 
    38 val all2E = prove_goal HOL.thy "[| !x y . P x y; P x y ==> R |] ==> R"
    39  (fn prems =>[
    40                                 resolve_tac prems 1,
    41                                 cut_facts_tac prems 1,
    42                                 fast_tac HOL_cs 1]);
    43 
    44 val dist_eqI = prove_goal Porder.thy "!!x::'a::po. ~ x << y ==> x ~= y" 
    45              (fn prems => [
    46                (blast_tac (claset() addDs [antisym_less_inverse]) 1)]);
    47 (*
    48 infixr 0 y;
    49 val b = 0;
    50 fun _ y t = by t;
    51 fun g defs t = let val sg = sign_of thy;
    52                      val ct = Thm.cterm_of sg (inferT sg t);
    53                  in goalw_cterm defs ct end;
    54 *)
    55 
    56 in
    57 
    58 fun theorems (((dname,_),cons) : eq, eqs : eq list) thy =
    59 let
    60 
    61 val dummy = writeln ("Proving isomorphism properties of domain "^dname^" ...");
    62 val pg = pg' thy;
    63 
    64 (* ----- getting the axioms and definitions --------------------------------- *)
    65 
    66 local fun ga s dn = get_thm thy (dn ^ "." ^ s, NONE) in
    67 val ax_abs_iso    = ga "abs_iso"  dname;
    68 val ax_rep_iso    = ga "rep_iso"  dname;
    69 val ax_when_def   = ga "when_def" dname;
    70 val axs_con_def   = map (fn (con,_) => ga (extern_name con^"_def") dname) cons;
    71 val axs_dis_def   = map (fn (con,_) => ga (   dis_name con^"_def") dname) cons;
    72 val axs_mat_def   = map (fn (con,_) => ga (   mat_name con^"_def") dname) cons;
    73 val axs_sel_def   = List.concat(map (fn (_,args) => 
    74                     map (fn     arg => ga (sel_of arg     ^"_def") dname) args)
    75 									  cons);
    76 val ax_copy_def   = ga "copy_def" dname;
    77 end; (* local *)
    78 
    79 (* ----- theorems concerning the isomorphism -------------------------------- *)
    80 
    81 val dc_abs  = %%:(dname^"_abs");
    82 val dc_rep  = %%:(dname^"_rep");
    83 val dc_copy = %%:(dname^"_copy");
    84 val x_name = "x";
    85 
    86 val iso_locale = iso_intro OF [ax_abs_iso, ax_rep_iso];
    87 val abs_strict = ax_rep_iso RS (allI RS retraction_strict);
    88 val rep_strict = ax_abs_iso RS (allI RS retraction_strict);
    89 val abs_defin' = iso_locale RS iso_abs_defin';
    90 val rep_defin' = iso_locale RS iso_rep_defin';
    91 val iso_rews = map standard [ax_abs_iso,ax_rep_iso,abs_strict,rep_strict];
    92 
    93 (* ----- generating beta reduction rules from definitions-------------------- *)
    94 
    95 local
    96   fun k NONE = cont_const | k (SOME x) = x;
    97   
    98   fun ap NONE NONE = NONE
    99   |   ap x    y    = SOME (standard (cont2cont_Rep_CFun OF [k x, k y]));
   100 
   101   fun var 0 = [SOME cont_id]
   102   |   var n = NONE :: var (n-1);
   103 
   104   fun zip []      []      = []
   105   |   zip []      (y::ys) = (ap NONE y   ) :: zip [] ys
   106   |   zip (x::xs) []      = (ap x    NONE) :: zip xs []
   107   |   zip (x::xs) (y::ys) = (ap x    y   ) :: zip xs ys
   108 
   109   fun lam [] = ([], cont_const)
   110   |   lam (x::ys) = let val x' = k x
   111                         val Lx = x' RS cont2cont_LAM
   112                     in  (map (fn y => SOME (k y RS Lx)) ys, x')
   113                     end;
   114 
   115   fun term_conts (Bound n) = (var n, [])
   116   |   term_conts (Const _) = ([],[])
   117   |   term_conts (Const _ $ Abs (_,_,t)) = let
   118           val (cs,ls) = term_conts t
   119           val (cs',l) = lam cs
   120           in  (cs',l::ls)
   121           end
   122   |   term_conts (Const _ $ f $ t)
   123          = (zip (fst (term_conts f)) (fst (term_conts t)), [])
   124   |   term_conts t = let val dummy = prin t in ([],[]) end;
   125 
   126   fun arglist (Const _ $ Abs (s,_,t)) = let
   127         val (vars,body) = arglist t
   128         in  (s :: vars, body) end
   129   |   arglist t = ([],t);
   130   fun bind_fun vars t = Library.foldr mk_All (vars,t);
   131   fun bound_vars 0 = [] | bound_vars i = (Bound (i-1) :: bound_vars (i-1));
   132 in
   133   fun appl_of_def def = let
   134         val (_ $ con $ lam) = concl_of def
   135         val (vars, rhs) = arglist lam
   136         val lhs = Library.foldl (op `) (con, bound_vars (length vars));
   137         val appl = bind_fun vars (lhs == rhs)
   138         val ([],cs) = term_conts lam
   139         val betas = map (fn c => mk_meta_eq (c RS beta_cfun)) cs
   140         in pg (def::betas) appl [rtac reflexive_thm 1] end;
   141 end;
   142 
   143 val when_appl = appl_of_def ax_when_def;
   144 val con_appls = map appl_of_def axs_con_def;
   145 
   146 local
   147   fun arg2typ n arg = let val t = TVar (("'a",n),["Pcpo.pcpo"])
   148                       in (n+1, if is_lazy arg then mk_uT t else t) end;
   149   fun args2typ n [] = (n,oneT)
   150   |   args2typ n [arg] = arg2typ n arg
   151   |   args2typ n (arg::args) = let val (n1,t1) = arg2typ n arg;
   152                                    val (n2,t2) = args2typ n1 args
   153 			       in  (n2, mk_sprodT (t1, t2)) end;
   154   fun cons2typ n [] = (n,oneT)
   155   |   cons2typ n [con] = args2typ n (snd con)
   156   |   cons2typ n (con::cons) = let val (n1,t1) = args2typ n (snd con);
   157                                    val (n2,t2) = cons2typ n1 cons
   158 			       in  (n2, mk_ssumT (t1, t2)) end;
   159 in
   160   fun cons2ctyp cons = ctyp_of (sign_of thy) (snd (cons2typ 1 cons));
   161 end;
   162 
   163 local 
   164   val iso_swap = iso_locale RS iso_iso_swap;
   165   fun one_con (con,args) = let val vns = map vname args in
   166     Library.foldr mk_ex (vns, foldr' mk_conj ((%:x_name === con_app2 con %: vns)::
   167                               map (defined o %:) (nonlazy args))) end;
   168   val exh = foldr' mk_disj ((%:x_name===UU)::map one_con cons);
   169   val my_ctyp = cons2ctyp cons;
   170   val thm1 = instantiate' [SOME my_ctyp] [] exh_start;
   171   val thm2 = rewrite_rule (map mk_meta_eq ex_defined_iffs) thm1;
   172   val thm3 = rewrite_rule [mk_meta_eq conj_assoc] thm2;
   173 in
   174 val exhaust = pg con_appls (mk_trp exh)[
   175 (* first 3 rules replace "x = UU \/ P" with "rep$x = UU \/ P" *)
   176 			rtac disjE 1,
   177 			etac (rep_defin' RS disjI1) 2,
   178 			etac disjI2 2,
   179 			rewrite_goals_tac [mk_meta_eq iso_swap],
   180 			rtac thm3 1];
   181 val casedist = standard (rewrite_rule exh_casedists (exhaust RS exh_casedist0));
   182 end;
   183 
   184 local 
   185   fun bind_fun t = Library.foldr mk_All (when_funs cons,t);
   186   fun bound_fun i _ = Bound (length cons - i);
   187   val when_app  = Library.foldl (op `) (%%:(dname^"_when"), mapn bound_fun 1 cons);
   188 in
   189 val when_strict = pg [when_appl, mk_meta_eq rep_strict]
   190 			(bind_fun(mk_trp(strict when_app)))
   191 			[resolve_tac [sscase1,ssplit1,strictify1] 1];
   192 val when_apps = let fun one_when n (con,args) = pg (when_appl :: con_appls)
   193                 (bind_fun (lift_defined %: (nonlazy args, 
   194                 mk_trp(when_app`(con_app con args) ===
   195                        mk_cRep_CFun(bound_fun n 0,map %# args)))))[
   196                 asm_simp_tac (HOLCF_ss addsimps [ax_abs_iso]) 1];
   197         in mapn one_when 1 cons end;
   198 end;
   199 val when_rews = when_strict::when_apps;
   200 
   201 (* ----- theorems concerning the constructors, discriminators and selectors - *)
   202 
   203 val dis_rews = let
   204   val dis_stricts = map (fn (con,_) => pg axs_dis_def (mk_trp(
   205                              strict(%%:(dis_name con)))) [
   206                                 rtac when_strict 1]) cons;
   207   val dis_apps = let fun one_dis c (con,args)= pg axs_dis_def
   208                    (lift_defined %: (nonlazy args,
   209                         (mk_trp((%%:(dis_name c))`(con_app con args) ===
   210                               %%:(if con=c then "TT" else "FF"))))) [
   211                                 asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   212         in List.concat(map (fn (c,_) => map (one_dis c) cons) cons) end;
   213   val dis_defins = map (fn (con,args) => pg [] (defined(%:x_name) ==> 
   214                       defined(%%:(dis_name con)`%x_name)) [
   215                                 rtac casedist 1,
   216                                 contr_tac 1,
   217                                 DETERM_UNTIL_SOLVED (CHANGED(asm_simp_tac 
   218                                         (HOLCF_ss addsimps dis_apps) 1))]) cons;
   219 in dis_stricts @ dis_defins @ dis_apps end;
   220 
   221 val mat_rews = let
   222   val mat_stricts = map (fn (con,_) => pg axs_mat_def (mk_trp(
   223                              strict(%%:(mat_name con)))) [
   224                                 rtac when_strict 1]) cons;
   225   val mat_apps = let fun one_mat c (con,args)= pg axs_mat_def
   226                    (lift_defined %: (nonlazy args,
   227                         (mk_trp((%%:(mat_name c))`(con_app con args) ===
   228                               (if con=c then (%%:"return")`(mk_ctuple (map %# args)) else %%:"fail"))))) [
   229                                 asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   230         in List.concat(map (fn (c,_) => map (one_mat c) cons) cons) end;
   231 in mat_stricts @ mat_apps end;
   232 
   233 val con_stricts = List.concat(map (fn (con,args) => map (fn vn =>
   234                         pg con_appls
   235                            (mk_trp(con_app2 con (fn arg => if vname arg = vn 
   236                                         then UU else %# arg) args === UU))[
   237                                 asm_simp_tac (HOLCF_ss addsimps [abs_strict]) 1]
   238                         ) (nonlazy args)) cons);
   239 val con_defins = map (fn (con,args) => pg []
   240                         (lift_defined %: (nonlazy args,
   241                                 mk_trp(defined(con_app con args)))) ([
   242                           rtac rev_contrapos 1, 
   243                           eres_inst_tac [("f",dis_name con)] cfun_arg_cong 1,
   244                           asm_simp_tac (HOLCF_ss addsimps dis_rews) 1] )) cons;
   245 val con_rews = con_stricts @ con_defins;
   246 
   247 val sel_stricts = let fun one_sel sel = pg axs_sel_def (mk_trp(strict(%%:sel))) [
   248                                 simp_tac (HOLCF_ss addsimps when_rews) 1];
   249 in List.concat(map (fn (_,args) =>map (fn arg => one_sel (sel_of arg)) args) cons) end;
   250 val sel_apps = let fun one_sel c n sel = map (fn (con,args) => 
   251                 let val nlas = nonlazy args;
   252                     val vns  = map vname args;
   253                 in pg axs_sel_def (lift_defined %:
   254                    (List.filter (fn v => con=c andalso (v<>List.nth(vns,n))) nlas,
   255                                 mk_trp((%%:sel)`(con_app con args) === 
   256                                 (if con=c then %:(List.nth(vns,n)) else UU))))
   257                             ( (if con=c then [] 
   258                        else map(case_UU_tac(when_rews@con_stricts)1) nlas)
   259                      @(if con=c andalso ((List.nth(vns,n)) mem nlas)
   260                                  then[case_UU_tac (when_rews @ con_stricts) 1 
   261                                                   (List.nth(vns,n))] else [])
   262                      @ [asm_simp_tac(HOLCF_ss addsimps when_rews)1])end) cons;
   263 in List.concat(map  (fn (c,args) => 
   264      List.concat(mapn (fn n => fn arg => one_sel c n (sel_of arg)) 0 args)) cons) end;
   265 val sel_defins = if length cons=1 then map (fn arg => pg [](defined(%:x_name)==> 
   266                         defined(%%:(sel_of arg)`%x_name)) [
   267                                 rtac casedist 1,
   268                                 contr_tac 1,
   269                                 DETERM_UNTIL_SOLVED (CHANGED(asm_simp_tac 
   270                                              (HOLCF_ss addsimps sel_apps) 1))]) 
   271                  (filter_out is_lazy (snd(hd cons))) else [];
   272 val sel_rews = sel_stricts @ sel_defins @ sel_apps;
   273 
   274 val distincts_le = let
   275     fun dist (con1, args1) (con2, args2) = pg []
   276               (lift_defined %: ((nonlazy args1),
   277                         (mk_trp (con_app con1 args1 ~<< con_app con2 args2))))([
   278                         rtac rev_contrapos 1,
   279                         eres_inst_tac[("f",dis_name con1)] monofun_cfun_arg 1]
   280                       @map(case_UU_tac (con_stricts @ dis_rews)1)(nonlazy args2)
   281                       @[asm_simp_tac (HOLCF_ss addsimps dis_rews) 1]);
   282     fun distinct (con1,args1) (con2,args2) =
   283         let val arg1 = (con1, args1)
   284             val arg2 = (con2,
   285 			ListPair.map (fn (arg,vn) => upd_vname (K vn) arg)
   286                         (args2, variantlist(map vname args2,map vname args1)))
   287         in [dist arg1 arg2, dist arg2 arg1] end;
   288     fun distincts []      = []
   289     |   distincts (c::cs) = (map (distinct c) cs) :: distincts cs;
   290 in distincts cons end;
   291 val dist_les = List.concat (List.concat distincts_le);
   292 val dist_eqs = let
   293     fun distinct (_,args1) ((_,args2),leqs) = let
   294         val (le1,le2) = (hd leqs, hd(tl leqs));
   295         val (eq1,eq2) = (le1 RS dist_eqI, le2 RS dist_eqI) in
   296         if nonlazy args1 = [] then [eq1, eq1 RS not_sym] else
   297         if nonlazy args2 = [] then [eq2, eq2 RS not_sym] else
   298                                         [eq1, eq2] end;
   299     open BasisLibrary (*restore original List*)
   300     fun distincts []      = []
   301     |   distincts ((c,leqs)::cs) = List.concat
   302 	            (ListPair.map (distinct c) ((map #1 cs),leqs)) @
   303 		    distincts cs;
   304     in map standard (distincts (cons~~distincts_le)) end;
   305 
   306 local 
   307   fun pgterm rel con args =
   308     let
   309       fun append s = upd_vname(fn v => v^s);
   310       val (largs,rargs) = (args, map (append "'") args);
   311       val concl = mk_trp (foldr' mk_conj (ListPair.map rel (map %# largs, map %# rargs)));
   312       val prem = mk_trp (rel(con_app con largs,con_app con rargs));
   313       val prop = prem ===> lift_defined %: (nonlazy largs, concl);
   314     in pg con_appls prop end;
   315   val cons' = List.filter (fn (_,args) => args<>[]) cons;
   316 in
   317 val inverts =
   318   let
   319     val abs_less = ax_abs_iso RS (allI RS injection_less) RS iffD1;
   320     val tacs = [
   321       dtac abs_less 1,
   322       REPEAT (dresolve_tac [sinl_less RS iffD1, sinr_less RS iffD1] 1),
   323       asm_full_simp_tac (HOLCF_ss addsimps [spair_less]) 1];
   324   in map (fn (con,args) => pgterm (op <<) con args tacs) cons' end;
   325 val injects =
   326   let
   327     val abs_eq = ax_abs_iso RS (allI RS injection_eq) RS iffD1;
   328     val tacs = [
   329       dtac abs_eq 1,
   330       REPEAT (dresolve_tac [sinl_inject, sinr_inject] 1),
   331       asm_full_simp_tac (HOLCF_ss addsimps [spair_eq]) 1];
   332   in map (fn (con,args) => pgterm (op ===) con args tacs) cons' end;
   333 end;
   334 
   335 (* ----- theorems concerning one induction step ----------------------------- *)
   336 
   337 val copy_strict = pg[ax_copy_def](mk_trp(strict(dc_copy`%"f"))) [
   338                    asm_simp_tac(HOLCF_ss addsimps [abs_strict, when_strict]) 1];
   339 val copy_apps = map (fn (con,args) => pg [ax_copy_def]
   340                     (lift_defined %: (nonlazy_rec args,
   341                         mk_trp(dc_copy`%"f"`(con_app con args) ===
   342                 (con_app2 con (app_rec_arg (cproj (%:"f") eqs)) args))))
   343                         (map (case_UU_tac (abs_strict::when_strict::con_stricts)
   344                                  1 o vname)
   345                          (List.filter (fn a => not (is_rec a orelse is_lazy a)) args)
   346                         @[asm_simp_tac (HOLCF_ss addsimps when_apps) 1,
   347                           simp_tac (HOLCF_ss addsimps con_appls) 1]))cons;
   348 val copy_stricts = map (fn (con,args) => pg [] (mk_trp(dc_copy`UU`
   349                                         (con_app con args) ===UU))
   350      (let val rews = copy_strict::copy_apps@con_rews
   351                          in map (case_UU_tac rews 1) (nonlazy args) @ [
   352                              asm_simp_tac (HOLCF_ss addsimps rews) 1] end))
   353                         (List.filter (fn (_,args)=>exists is_nonlazy_rec args) cons);
   354 val copy_rews = copy_strict::copy_apps @ copy_stricts;
   355 in thy |> Theory.add_path (Sign.base_name dname)
   356        |> (#1 o (PureThy.add_thmss (map Thm.no_attributes [
   357 		("iso_rews" , iso_rews  ),
   358 		("exhaust"  , [exhaust] ),
   359 		("casedist" , [casedist]),
   360 		("when_rews", when_rews ),
   361 		("con_rews", con_rews),
   362 		("sel_rews", sel_rews),
   363 		("dis_rews", dis_rews),
   364 		("match_rews", mat_rews),
   365 		("dist_les", dist_les),
   366 		("dist_eqs", dist_eqs),
   367 		("inverts" , inverts ),
   368 		("injects" , injects ),
   369 		("copy_rews", copy_rews)])))
   370        |> Theory.parent_path |> rpair (iso_rews @ when_rews @ con_rews @ sel_rews @ dis_rews @
   371                  dist_les @ dist_eqs @ copy_rews)
   372 end; (* let *)
   373 
   374 fun comp_theorems (comp_dnam, eqs: eq list) thy =
   375 let
   376 val dnames = map (fst o fst) eqs;
   377 val conss  = map  snd        eqs;
   378 val comp_dname = Sign.full_name (sign_of thy) comp_dnam;
   379 
   380 val d = writeln("Proving induction properties of domain "^comp_dname^" ...");
   381 val pg = pg' thy;
   382 
   383 (* ----- getting the composite axiom and definitions ------------------------ *)
   384 
   385 local fun ga s dn = get_thm thy (dn ^ "." ^ s, NONE) in
   386 val axs_reach      = map (ga "reach"     ) dnames;
   387 val axs_take_def   = map (ga "take_def"  ) dnames;
   388 val axs_finite_def = map (ga "finite_def") dnames;
   389 val ax_copy2_def   =      ga "copy_def"  comp_dnam;
   390 val ax_bisim_def   =      ga "bisim_def" comp_dnam;
   391 end; (* local *)
   392 
   393 local fun gt  s dn = get_thm  thy (dn ^ "." ^ s, NONE);
   394       fun gts s dn = get_thms thy (dn ^ "." ^ s, NONE) in
   395 val cases     =       map (gt  "casedist" ) dnames;
   396 val con_rews  = List.concat (map (gts "con_rews" ) dnames);
   397 val copy_rews = List.concat (map (gts "copy_rews") dnames);
   398 end; (* local *)
   399 
   400 fun dc_take dn = %%:(dn^"_take");
   401 val x_name = idx_name dnames "x"; 
   402 val P_name = idx_name dnames "P";
   403 val n_eqs = length eqs;
   404 
   405 (* ----- theorems concerning finite approximation and finite induction ------ *)
   406 
   407 local
   408   val iterate_Cprod_ss = simpset_of Fix.thy;
   409   val copy_con_rews  = copy_rews @ con_rews;
   410   val copy_take_defs =(if n_eqs = 1 then [] else [ax_copy2_def]) @ axs_take_def;
   411   val take_stricts=pg copy_take_defs(mk_trp(foldr' mk_conj(map(fn((dn,args),_)=>
   412             strict(dc_take dn $ %:"n")) eqs))) ([
   413                         induct_tac "n" 1,
   414                          simp_tac iterate_Cprod_ss 1,
   415                         asm_simp_tac (iterate_Cprod_ss addsimps copy_rews)1]);
   416   val take_stricts' = rewrite_rule copy_take_defs take_stricts;
   417   val take_0s = mapn(fn n=> fn dn => pg axs_take_def(mk_trp((dc_take dn $ %%:"0")
   418                                                         `%x_name n === UU))[
   419                                 simp_tac iterate_Cprod_ss 1]) 1 dnames;
   420   val c_UU_tac = case_UU_tac (take_stricts'::copy_con_rews) 1;
   421   val take_apps = pg copy_take_defs (mk_trp(foldr' mk_conj 
   422             (List.concat(map (fn ((dn,_),cons) => map (fn (con,args) => Library.foldr mk_all 
   423         (map vname args,(dc_take dn $ (%%:"Suc" $ %:"n"))`(con_app con args) ===
   424          con_app2 con (app_rec_arg (fn n=>dc_take (List.nth(dnames,n))$ %:"n"))
   425                               args)) cons) eqs)))) ([
   426                                 simp_tac iterate_Cprod_ss 1,
   427                                 induct_tac "n" 1,
   428                             simp_tac(iterate_Cprod_ss addsimps copy_con_rews) 1,
   429                                 asm_full_simp_tac (HOLCF_ss addsimps 
   430                                       (List.filter (has_fewer_prems 1) copy_rews)) 1,
   431                                 TRY(safe_tac HOL_cs)] @
   432                         (List.concat(map (fn ((dn,_),cons) => map (fn (con,args) => 
   433                                 if nonlazy_rec args = [] then all_tac else
   434                                 EVERY(map c_UU_tac (nonlazy_rec args)) THEN
   435                                 asm_full_simp_tac (HOLCF_ss addsimps copy_rews)1
   436                                                            ) cons) eqs)));
   437 in
   438 val take_rews = map standard (atomize take_stricts @ take_0s @ atomize take_apps);
   439 end; (* local *)
   440 
   441 local
   442   fun one_con p (con,args) = Library.foldr mk_All (map vname args,
   443         lift_defined (bound_arg (map vname args)) (nonlazy args,
   444         lift (fn arg => %:(P_name (1+rec_of arg)) $ bound_arg args arg)
   445          (List.filter is_rec args,mk_trp(%:p $ con_app2 con (bound_arg args) args))));
   446   fun one_eq ((p,cons),concl) = (mk_trp(%:p $ UU) ===> 
   447                            Library.foldr (op ===>) (map (one_con p) cons,concl));
   448   fun ind_term concf = Library.foldr one_eq (mapn (fn n => fn x => (P_name n, x))1conss,
   449                         mk_trp(foldr' mk_conj (mapn concf 1 dnames)));
   450   val take_ss = HOL_ss addsimps take_rews;
   451   fun quant_tac i = EVERY(mapn(fn n=> fn _=> res_inst_tac[("x",x_name n)]spec i)
   452                                1 dnames);
   453   fun ind_prems_tac prems = EVERY(List.concat (map (fn cons => (
   454                                      resolve_tac prems 1 ::
   455                                      List.concat (map (fn (_,args) => 
   456                                        resolve_tac prems 1 ::
   457                                        map (K(atac 1)) (nonlazy args) @
   458                                        map (K(atac 1)) (List.filter is_rec args))
   459                                      cons))) conss));
   460   local 
   461     (* check whether every/exists constructor of the n-th part of the equation:
   462        it has a possibly indirectly recursive argument that isn't/is possibly 
   463        indirectly lazy *)
   464     fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
   465           is_rec arg andalso not(rec_of arg mem ns) andalso
   466           ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
   467             rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
   468               (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
   469           ) o snd) cons;
   470     fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
   471     fun warn (n,cons)  = if all_rec_to [] false (n,cons) then (warning
   472         ("domain "^List.nth(dnames,n)^" is empty!"); true) else false;
   473     fun lazy_rec_to ns = rec_to exists Id  lazy_rec_to ns;
   474 
   475   in val n__eqs     = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   476      val is_emptys = map warn n__eqs;
   477      val is_finite = forall (not o lazy_rec_to [] false) n__eqs;
   478   end;
   479 in (* local *)
   480 val finite_ind = pg'' thy [] (ind_term (fn n => fn dn => %:(P_name n)$
   481                              (dc_take dn $ %:"n" `%(x_name n)))) (fn prems => [
   482                                 quant_tac 1,
   483                                 simp_tac HOL_ss 1,
   484                                 induct_tac "n" 1,
   485                                 simp_tac (take_ss addsimps prems) 1,
   486                                 TRY(safe_tac HOL_cs)]
   487                                 @ List.concat(map (fn (cons,cases) => [
   488                                  res_inst_tac [("x","x")] cases 1,
   489                                  asm_simp_tac (take_ss addsimps prems) 1]
   490                                  @ List.concat(map (fn (con,args) => 
   491                                   asm_simp_tac take_ss 1 ::
   492                                   map (fn arg =>
   493                                    case_UU_tac (prems@con_rews) 1 (
   494                            List.nth(dnames,rec_of arg)^"_take n$"^vname arg))
   495                                   (List.filter is_nonlazy_rec args) @ [
   496                                   resolve_tac prems 1] @
   497                                   map (K (atac 1))      (nonlazy args) @
   498                                   map (K (etac spec 1)) (List.filter is_rec args)) 
   499                                  cons))
   500                                 (conss~~cases)));
   501 
   502 val take_lemmas =mapn(fn n=> fn(dn,ax_reach)=> pg'' thy axs_take_def(mk_All("n",
   503                 mk_trp(dc_take dn $ Bound 0 `%(x_name n) === 
   504                        dc_take dn $ Bound 0 `%(x_name n^"'")))
   505            ===> mk_trp(%:(x_name n) === %:(x_name n^"'"))) (fn prems => [
   506                         res_inst_tac[("t",x_name n    )](ax_reach RS subst) 1,
   507                         res_inst_tac[("t",x_name n^"'")](ax_reach RS subst) 1,
   508                                 stac fix_def2 1,
   509                                 REPEAT(CHANGED(rtac(contlub_cfun_arg RS ssubst)1
   510                                                THEN chain_tac 1)),
   511                                 stac contlub_cfun_fun 1,
   512                                 stac contlub_cfun_fun 2,
   513                                 rtac lub_equal 3,
   514                                 chain_tac 1,
   515                                 rtac allI 1,
   516                                 resolve_tac prems 1])) 1 (dnames~~axs_reach);
   517 
   518 (* ----- theorems concerning finiteness and induction ----------------------- *)
   519 
   520 val (finites,ind) = if is_finite then
   521   let 
   522     fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %:"x" === %:"x");
   523     val finite_lemmas1a = map (fn dn => pg [] (mk_trp(defined (%:"x")) ===> 
   524         mk_trp(mk_disj(mk_all("n",dc_take dn $ Bound 0 ` %:"x" === UU),
   525         take_enough dn)) ===> mk_trp(take_enough dn)) [
   526                                 etac disjE 1,
   527                                 etac notE 1,
   528                                 resolve_tac take_lemmas 1,
   529                                 asm_simp_tac take_ss 1,
   530                                 atac 1]) dnames;
   531     val finite_lemma1b = pg [] (mk_trp (mk_all("n",foldr' mk_conj (mapn 
   532         (fn n => fn ((dn,args),_) => mk_constrainall(x_name n,Type(dn,args),
   533          mk_disj(dc_take dn $ Bound 1 ` Bound 0 === UU,
   534                  dc_take dn $ Bound 1 ` Bound 0 === Bound 0))) 1 eqs)))) ([
   535                                 rtac allI 1,
   536                                 induct_tac "n" 1,
   537                                 simp_tac take_ss 1,
   538                         TRY(safe_tac(empty_cs addSEs[conjE] addSIs[conjI]))] @
   539                                 List.concat(mapn (fn n => fn (cons,cases) => [
   540                                   simp_tac take_ss 1,
   541                                   rtac allI 1,
   542                                   res_inst_tac [("x",x_name n)] cases 1,
   543                                   asm_simp_tac take_ss 1] @ 
   544                                   List.concat(map (fn (con,args) => 
   545                                     asm_simp_tac take_ss 1 ::
   546                                     List.concat(map (fn vn => [
   547                                       eres_inst_tac [("x",vn)] all_dupE 1,
   548                                       etac disjE 1,
   549                                       asm_simp_tac (HOL_ss addsimps con_rews) 1,
   550                                       asm_simp_tac take_ss 1])
   551                                     (nonlazy_rec args)))
   552                                   cons))
   553                                 1 (conss~~cases)));
   554     val finites = map (fn (dn,l1b) => pg axs_finite_def (mk_trp(
   555                                                 %%:(dn^"_finite") $ %:"x"))[
   556                                 case_UU_tac take_rews 1 "x",
   557                                 eresolve_tac finite_lemmas1a 1,
   558                                 step_tac HOL_cs 1,
   559                                 step_tac HOL_cs 1,
   560                                 cut_facts_tac [l1b] 1,
   561                         fast_tac HOL_cs 1]) (dnames~~atomize finite_lemma1b);
   562   in
   563   (finites,
   564    pg'' thy[](ind_term (fn n => fn dn => %:(P_name n) $ %:(x_name n)))(fn prems =>
   565                                 TRY(safe_tac HOL_cs) ::
   566                          List.concat (map (fn (finite,fin_ind) => [
   567                                rtac(rewrite_rule axs_finite_def finite RS exE)1,
   568                                 etac subst 1,
   569                                 rtac fin_ind 1,
   570                                 ind_prems_tac prems]) 
   571                                    (finites~~(atomize finite_ind)) ))
   572 ) end (* let *) else
   573   (mapn (fn n => fn dn => read_instantiate_sg (sign_of thy) 
   574                     [("P",dn^"_finite "^x_name n)] excluded_middle) 1 dnames,
   575    pg'' thy [] (Library.foldr (op ===>) (mapn (fn n => K(mk_trp(%%:"adm" $ %:(P_name n))))
   576                1 dnames, ind_term (fn n => fn dn => %:(P_name n) $ %:(x_name n))))
   577                    (fn prems => map (fn ax_reach => rtac (ax_reach RS subst) 1) 
   578                                     axs_reach @ [
   579                                 quant_tac 1,
   580                                 rtac (adm_impl_admw RS wfix_ind) 1,
   581                                  REPEAT_DETERM(rtac adm_all2 1),
   582                                  REPEAT_DETERM(TRY(rtac adm_conj 1) THEN 
   583                                                    rtac adm_subst 1 THEN 
   584                                         cont_tacR 1 THEN resolve_tac prems 1),
   585                                 strip_tac 1,
   586                                 rtac (rewrite_rule axs_take_def finite_ind) 1,
   587                                 ind_prems_tac prems])
   588   handle ERROR => (warning "Cannot prove infinite induction rule"; refl))
   589 end; (* local *)
   590 
   591 (* ----- theorem concerning coinduction ------------------------------------- *)
   592 
   593 local
   594   val xs = mapn (fn n => K (x_name n)) 1 dnames;
   595   fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
   596   val take_ss = HOL_ss addsimps take_rews;
   597   val sproj   = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
   598   val coind_lemma=pg[ax_bisim_def](mk_trp(mk_imp(%%:(comp_dname^"_bisim") $ %:"R",
   599                 Library.foldr (fn (x,t)=> mk_all(x,mk_all(x^"'",t))) (xs,
   600                   Library.foldr mk_imp (mapn (fn n => K(proj (%:"R") eqs n $ 
   601                                       bnd_arg n 0 $ bnd_arg n 1)) 0 dnames,
   602                     foldr' mk_conj (mapn (fn n => fn dn => 
   603                                 (dc_take dn $ %:"n" `bnd_arg n 0 === 
   604                                 (dc_take dn $ %:"n" `bnd_arg n 1)))0 dnames))))))
   605                              ([ rtac impI 1,
   606                                 induct_tac "n" 1,
   607                                 simp_tac take_ss 1,
   608                                 safe_tac HOL_cs] @
   609                                 List.concat(mapn (fn n => fn x => [
   610                                   rotate_tac (n+1) 1,
   611                                   etac all2E 1,
   612                                   eres_inst_tac [("P1", sproj "R" eqs n^
   613                                         " "^x^" "^x^"'")](mp RS disjE) 1,
   614                                   TRY(safe_tac HOL_cs),
   615                                   REPEAT(CHANGED(asm_simp_tac take_ss 1))]) 
   616                                 0 xs));
   617 in
   618 val coind = pg [] (mk_trp(%%:(comp_dname^"_bisim") $ %:"R") ===>
   619                 Library.foldr (op ===>) (mapn (fn n => fn x => 
   620                   mk_trp(proj (%:"R") eqs n $ %:x $ %:(x^"'"))) 0 xs,
   621                   mk_trp(foldr' mk_conj (map (fn x => %:x === %:(x^"'")) xs)))) ([
   622                                 TRY(safe_tac HOL_cs)] @
   623                                 List.concat(map (fn take_lemma => [
   624                                   rtac take_lemma 1,
   625                                   cut_facts_tac [coind_lemma] 1,
   626                                   fast_tac HOL_cs 1])
   627                                 take_lemmas));
   628 end; (* local *)
   629 
   630 in thy |> Theory.add_path comp_dnam
   631        |> (#1 o (PureThy.add_thmss (map Thm.no_attributes [
   632 		("take_rews"  , take_rews  ),
   633 		("take_lemmas", take_lemmas),
   634 		("finites"    , finites    ),
   635 		("finite_ind", [finite_ind]),
   636 		("ind"       , [ind       ]),
   637 		("coind"     , [coind     ])])))
   638        |> Theory.parent_path |> rpair take_rews
   639 end; (* let *)
   640 end; (* local *)
   641 end; (* struct *)