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