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