src/HOLCF/Tools/Domain/domain_theorems.ML
author huffman
Fri Feb 26 09:13:29 2010 -0800 (2010-02-26)
changeset 35451 a726a033b313
parent 35448 f9f73f0475eb
child 35452 cf8c5a751a9a
permissions -rw-r--r--
don't bother returning con_defs
     1 (*  Title:      HOLCF/Tools/Domain/domain_theorems.ML
     2     Author:     David von Oheimb
     3     Author:     Brian Huffman
     4 
     5 Proof generator for domain command.
     6 *)
     7 
     8 val HOLCF_ss = @{simpset};
     9 
    10 signature DOMAIN_THEOREMS =
    11 sig
    12   val theorems:
    13     Domain_Library.eq * Domain_Library.eq list
    14     -> typ * (binding * (bool * binding option * typ) list * mixfix) list
    15     -> theory -> thm list * theory;
    16 
    17   val comp_theorems: bstring * Domain_Library.eq list -> theory -> thm list * theory;
    18   val quiet_mode: bool Unsynchronized.ref;
    19   val trace_domain: bool Unsynchronized.ref;
    20 end;
    21 
    22 structure Domain_Theorems :> DOMAIN_THEOREMS =
    23 struct
    24 
    25 val quiet_mode = Unsynchronized.ref false;
    26 val trace_domain = Unsynchronized.ref false;
    27 
    28 fun message s = if !quiet_mode then () else writeln s;
    29 fun trace s = if !trace_domain then tracing s else ();
    30 
    31 val adm_impl_admw = @{thm adm_impl_admw};
    32 val adm_all = @{thm adm_all};
    33 val adm_conj = @{thm adm_conj};
    34 val adm_subst = @{thm adm_subst};
    35 val antisym_less_inverse = @{thm below_antisym_inverse};
    36 val beta_cfun = @{thm beta_cfun};
    37 val cfun_arg_cong = @{thm cfun_arg_cong};
    38 val ch2ch_fst = @{thm ch2ch_fst};
    39 val ch2ch_snd = @{thm ch2ch_snd};
    40 val ch2ch_Rep_CFunL = @{thm ch2ch_Rep_CFunL};
    41 val ch2ch_Rep_CFunR = @{thm ch2ch_Rep_CFunR};
    42 val chain_iterate = @{thm chain_iterate};
    43 val compact_ONE = @{thm compact_ONE};
    44 val compact_sinl = @{thm compact_sinl};
    45 val compact_sinr = @{thm compact_sinr};
    46 val compact_spair = @{thm compact_spair};
    47 val compact_up = @{thm compact_up};
    48 val contlub_cfun_arg = @{thm contlub_cfun_arg};
    49 val contlub_cfun_fun = @{thm contlub_cfun_fun};
    50 val contlub_fst = @{thm contlub_fst};
    51 val contlub_snd = @{thm contlub_snd};
    52 val contlubE = @{thm contlubE};
    53 val cont_const = @{thm cont_const};
    54 val cont_id = @{thm cont_id};
    55 val cont2cont_fst = @{thm cont2cont_fst};
    56 val cont2cont_snd = @{thm cont2cont_snd};
    57 val cont2cont_Rep_CFun = @{thm cont2cont_Rep_CFun};
    58 val fix_def2 = @{thm fix_def2};
    59 val injection_eq = @{thm injection_eq};
    60 val injection_less = @{thm injection_below};
    61 val lub_equal = @{thm lub_equal};
    62 val monofun_cfun_arg = @{thm monofun_cfun_arg};
    63 val retraction_strict = @{thm retraction_strict};
    64 val spair_eq = @{thm spair_eq};
    65 val spair_less = @{thm spair_below};
    66 val sscase1 = @{thm sscase1};
    67 val ssplit1 = @{thm ssplit1};
    68 val strictify1 = @{thm strictify1};
    69 val wfix_ind = @{thm wfix_ind};
    70 
    71 val iso_intro       = @{thm iso.intro};
    72 val iso_abs_iso     = @{thm iso.abs_iso};
    73 val iso_rep_iso     = @{thm iso.rep_iso};
    74 val iso_abs_strict  = @{thm iso.abs_strict};
    75 val iso_rep_strict  = @{thm iso.rep_strict};
    76 val iso_abs_defin'  = @{thm iso.abs_defin'};
    77 val iso_rep_defin'  = @{thm iso.rep_defin'};
    78 val iso_abs_defined = @{thm iso.abs_defined};
    79 val iso_rep_defined = @{thm iso.rep_defined};
    80 val iso_compact_abs = @{thm iso.compact_abs};
    81 val iso_compact_rep = @{thm iso.compact_rep};
    82 val iso_iso_swap    = @{thm iso.iso_swap};
    83 
    84 val exh_start = @{thm exh_start};
    85 val ex_defined_iffs = @{thms ex_defined_iffs};
    86 val exh_casedist0 = @{thm exh_casedist0};
    87 val exh_casedists = @{thms exh_casedists};
    88 
    89 open Domain_Library;
    90 infixr 0 ===>;
    91 infixr 0 ==>;
    92 infix 0 == ; 
    93 infix 1 ===;
    94 infix 1 ~= ;
    95 infix 1 <<;
    96 infix 1 ~<<;
    97 infix 9 `   ;
    98 infix 9 `% ;
    99 infix 9 `%%;
   100 infixr 9 oo;
   101 
   102 (* ----- general proof facilities ------------------------------------------- *)
   103 
   104 fun legacy_infer_term thy t =
   105   let val ctxt = ProofContext.set_mode ProofContext.mode_schematic (ProofContext.init thy)
   106   in singleton (Syntax.check_terms ctxt) (Sign.intern_term thy t) end;
   107 
   108 fun pg'' thy defs t tacs =
   109   let
   110     val t' = legacy_infer_term thy t;
   111     val asms = Logic.strip_imp_prems t';
   112     val prop = Logic.strip_imp_concl t';
   113     fun tac {prems, context} =
   114       rewrite_goals_tac defs THEN
   115       EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
   116   in Goal.prove_global thy [] asms prop tac end;
   117 
   118 fun pg' thy defs t tacsf =
   119   let
   120     fun tacs {prems, context} =
   121       if null prems then tacsf context
   122       else cut_facts_tac prems 1 :: tacsf context;
   123   in pg'' thy defs t tacs end;
   124 
   125 (* FIXME!!!!!!!!! *)
   126 (* We should NEVER re-parse variable names as strings! *)
   127 (* The names can conflict with existing constants or other syntax! *)
   128 fun case_UU_tac ctxt rews i v =
   129   InductTacs.case_tac ctxt (v^"=UU") i THEN
   130   asm_simp_tac (HOLCF_ss addsimps rews) i;
   131 
   132 val chain_tac =
   133   REPEAT_DETERM o resolve_tac 
   134     [chain_iterate, ch2ch_Rep_CFunR, ch2ch_Rep_CFunL, ch2ch_fst, ch2ch_snd];
   135 
   136 (* ----- general proofs ----------------------------------------------------- *)
   137 
   138 val all2E = @{lemma "!x y . P x y ==> (P x y ==> R) ==> R" by simp}
   139 
   140 val dist_eqI = @{lemma "!!x::'a::po. ~ x << y ==> x ~= y" by (blast dest!: below_antisym_inverse)}
   141 
   142 fun theorems
   143     (((dname, _), cons) : eq, eqs : eq list)
   144     (dom_eqn : typ * (binding * (bool * binding option * typ) list * mixfix) list)
   145     (thy : theory) =
   146 let
   147 
   148 val _ = message ("Proving isomorphism properties of domain "^dname^" ...");
   149 val map_tab = Domain_Isomorphism.get_map_tab thy;
   150 
   151 
   152 (* ----- getting the axioms and definitions --------------------------------- *)
   153 
   154 local
   155   fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
   156 in
   157   val ax_abs_iso  = ga "abs_iso"  dname;
   158   val ax_rep_iso  = ga "rep_iso"  dname;
   159   val ax_when_def = ga "when_def" dname;
   160   fun get_def mk_name (con, _, _) = ga (mk_name con^"_def") dname;
   161   val axs_dis_def = map (get_def dis_name) cons;
   162   val axs_mat_def = map (get_def mat_name) cons;
   163   val axs_pat_def = map (get_def pat_name) cons;
   164 (*
   165   val axs_sel_def =
   166     let
   167       fun def_of_sel sel = ga (sel^"_def") dname;
   168       fun def_of_arg arg = Option.map def_of_sel (sel_of arg);
   169       fun defs_of_con (_, _, args) = map_filter def_of_arg args;
   170     in
   171       maps defs_of_con cons
   172     end;
   173 *)
   174   val ax_copy_def = ga "copy_def" dname;
   175 end; (* local *)
   176 
   177 (* ----- define constructors ------------------------------------------------ *)
   178 
   179 val lhsT = fst dom_eqn;
   180 
   181 val rhsT =
   182   let
   183     fun mk_arg_typ (lazy, sel, T) = if lazy then mk_uT T else T;
   184     fun mk_con_typ (bind, args, mx) =
   185         if null args then oneT else foldr1 mk_sprodT (map mk_arg_typ args);
   186     fun mk_eq_typ (_, cons) = foldr1 mk_ssumT (map mk_con_typ cons);
   187   in
   188     mk_eq_typ dom_eqn
   189   end;
   190 
   191 val rep_const = Const(dname^"_rep", lhsT ->> rhsT);
   192 
   193 val abs_const = Const(dname^"_abs", rhsT ->> lhsT);
   194 
   195 val (result, thy) =
   196   Domain_Constructors.add_domain_constructors
   197     (Long_Name.base_name dname) dom_eqn
   198     (rep_const, abs_const) (ax_rep_iso, ax_abs_iso) thy;
   199 
   200 val con_appls = #con_betas result;
   201 val con_compacts = #con_compacts result;
   202 val sel_rews = #sel_rews result;
   203 
   204 (* ----- theorems concerning the isomorphism -------------------------------- *)
   205 
   206 val pg = pg' thy;
   207 
   208 val dc_abs  = %%:(dname^"_abs");
   209 val dc_rep  = %%:(dname^"_rep");
   210 val dc_copy = %%:(dname^"_copy");
   211 val x_name = "x";
   212 
   213 val iso_locale = iso_intro OF [ax_abs_iso, ax_rep_iso];
   214 val abs_strict = ax_rep_iso RS (allI RS retraction_strict);
   215 val rep_strict = ax_abs_iso RS (allI RS retraction_strict);
   216 val abs_defin' = iso_locale RS iso_abs_defin';
   217 val rep_defin' = iso_locale RS iso_rep_defin';
   218 val iso_rews = map Drule.export_without_context [ax_abs_iso, ax_rep_iso, abs_strict, rep_strict];
   219 
   220 (* ----- generating beta reduction rules from definitions-------------------- *)
   221 
   222 val _ = trace " Proving beta reduction rules...";
   223 
   224 local
   225   fun arglist (Const _ $ Abs (s, _, t)) =
   226     let
   227       val (vars,body) = arglist t;
   228     in (s :: vars, body) end
   229     | arglist t = ([], t);
   230   fun bind_fun vars t = Library.foldr mk_All (vars, t);
   231   fun bound_vars 0 = []
   232     | bound_vars i = Bound (i-1) :: bound_vars (i - 1);
   233 in
   234   fun appl_of_def def =
   235     let
   236       val (_ $ con $ lam) = concl_of def;
   237       val (vars, rhs) = arglist lam;
   238       val lhs = list_ccomb (con, bound_vars (length vars));
   239       val appl = bind_fun vars (lhs == rhs);
   240       val cs = ContProc.cont_thms lam;
   241       val betas = map (fn c => mk_meta_eq (c RS beta_cfun)) cs;
   242     in pg (def::betas) appl (K [rtac reflexive_thm 1]) end;
   243 end;
   244 
   245 val _ = trace "Proving when_appl...";
   246 val when_appl = appl_of_def ax_when_def;
   247 
   248 local
   249   fun arg2typ n arg =
   250     let val t = TVar (("'a", n), pcpoS)
   251     in (n + 1, if is_lazy arg then mk_uT t else t) end;
   252 
   253   fun args2typ n [] = (n, oneT)
   254     | args2typ n [arg] = arg2typ n arg
   255     | args2typ n (arg::args) =
   256     let
   257       val (n1, t1) = arg2typ n arg;
   258       val (n2, t2) = args2typ n1 args
   259     in (n2, mk_sprodT (t1, t2)) end;
   260 
   261   fun cons2typ n [] = (n,oneT)
   262     | cons2typ n [con] = args2typ n (third con)
   263     | cons2typ n (con::cons) =
   264     let
   265       val (n1, t1) = args2typ n (third con);
   266       val (n2, t2) = cons2typ n1 cons
   267     in (n2, mk_ssumT (t1, t2)) end;
   268 in
   269   fun cons2ctyp cons = ctyp_of thy (snd (cons2typ 1 cons));
   270 end;
   271 
   272 local
   273   val iso_swap = iso_locale RS iso_iso_swap;
   274   fun one_con (con, _, args) =
   275     let
   276       val vns = Name.variant_list ["x"] (map vname args);
   277       val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
   278       val eqn = %:x_name === con_app2 con %: vns;
   279       val conj = foldr1 mk_conj (eqn :: map (defined o %:) nonlazy_vns);
   280     in Library.foldr mk_ex (vns, conj) end;
   281 
   282   val conj_assoc = @{thm conj_assoc};
   283   val exh = foldr1 mk_disj ((%:x_name === UU) :: map one_con cons);
   284   val thm1 = instantiate' [SOME (cons2ctyp cons)] [] exh_start;
   285   val thm2 = rewrite_rule (map mk_meta_eq ex_defined_iffs) thm1;
   286   val thm3 = rewrite_rule [mk_meta_eq @{thm conj_assoc}] thm2;
   287 
   288   (* first 3 rules replace "x = UU \/ P" with "rep$x = UU \/ P" *)
   289   val tacs = [
   290     rtac disjE 1,
   291     etac (rep_defin' RS disjI1) 2,
   292     etac disjI2 2,
   293     rewrite_goals_tac [mk_meta_eq iso_swap],
   294     rtac thm3 1];
   295 in
   296   val _ = trace " Proving exhaust...";
   297   val exhaust = pg con_appls (mk_trp exh) (K tacs);
   298   val _ = trace " Proving casedist...";
   299   val casedist =
   300     Drule.export_without_context (rewrite_rule exh_casedists (exhaust RS exh_casedist0));
   301 end;
   302 
   303 local 
   304   fun bind_fun t = Library.foldr mk_All (when_funs cons, t);
   305   fun bound_fun i _ = Bound (length cons - i);
   306   val when_app = list_ccomb (%%:(dname^"_when"), mapn bound_fun 1 cons);
   307 in
   308   val _ = trace " Proving when_strict...";
   309   val when_strict =
   310     let
   311       val axs = [when_appl, mk_meta_eq rep_strict];
   312       val goal = bind_fun (mk_trp (strict when_app));
   313       val tacs = [resolve_tac [sscase1, ssplit1, strictify1] 1];
   314     in pg axs goal (K tacs) end;
   315 
   316   val _ = trace " Proving when_apps...";
   317   val when_apps =
   318     let
   319       fun one_when n (con, _, args) =
   320         let
   321           val axs = when_appl :: con_appls;
   322           val goal = bind_fun (lift_defined %: (nonlazy args, 
   323                 mk_trp (when_app`(con_app con args) ===
   324                        list_ccomb (bound_fun n 0, map %# args))));
   325           val tacs = [asm_simp_tac (HOLCF_ss addsimps [ax_abs_iso]) 1];
   326         in pg axs goal (K tacs) end;
   327     in mapn one_when 1 cons end;
   328 end;
   329 val when_rews = when_strict :: when_apps;
   330 
   331 (* ----- theorems concerning the constructors, discriminators and selectors - *)
   332 
   333 local
   334   fun dis_strict (con, _, _) =
   335     let
   336       val goal = mk_trp (strict (%%:(dis_name con)));
   337     in pg axs_dis_def goal (K [rtac when_strict 1]) end;
   338 
   339   fun dis_app c (con, _, args) =
   340     let
   341       val lhs = %%:(dis_name c) ` con_app con args;
   342       val rhs = if con = c then TT else FF;
   343       val goal = lift_defined %: (nonlazy args, mk_trp (lhs === rhs));
   344       val tacs = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   345     in pg axs_dis_def goal (K tacs) end;
   346 
   347   val _ = trace " Proving dis_apps...";
   348   val dis_apps = maps (fn (c,_,_) => map (dis_app c) cons) cons;
   349 
   350   fun dis_defin (con, _, args) =
   351     let
   352       val goal = defined (%:x_name) ==> defined (%%:(dis_name con) `% x_name);
   353       val tacs =
   354         [rtac casedist 1,
   355          contr_tac 1,
   356          DETERM_UNTIL_SOLVED (CHANGED
   357           (asm_simp_tac (HOLCF_ss addsimps dis_apps) 1))];
   358     in pg [] goal (K tacs) end;
   359 
   360   val _ = trace " Proving dis_stricts...";
   361   val dis_stricts = map dis_strict cons;
   362   val _ = trace " Proving dis_defins...";
   363   val dis_defins = map dis_defin cons;
   364 in
   365   val dis_rews = dis_stricts @ dis_defins @ dis_apps;
   366 end;
   367 
   368 local
   369   fun mat_strict (con, _, _) =
   370     let
   371       val goal = mk_trp (%%:(mat_name con) ` UU ` %:"rhs" === UU);
   372       val tacs = [asm_simp_tac (HOLCF_ss addsimps [when_strict]) 1];
   373     in pg axs_mat_def goal (K tacs) end;
   374 
   375   val _ = trace " Proving mat_stricts...";
   376   val mat_stricts = map mat_strict cons;
   377 
   378   fun one_mat c (con, _, args) =
   379     let
   380       val lhs = %%:(mat_name c) ` con_app con args ` %:"rhs";
   381       val rhs =
   382         if con = c
   383         then list_ccomb (%:"rhs", map %# args)
   384         else mk_fail;
   385       val goal = lift_defined %: (nonlazy args, mk_trp (lhs === rhs));
   386       val tacs = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   387     in pg axs_mat_def goal (K tacs) end;
   388 
   389   val _ = trace " Proving mat_apps...";
   390   val mat_apps =
   391     maps (fn (c,_,_) => map (one_mat c) cons) cons;
   392 in
   393   val mat_rews = mat_stricts @ mat_apps;
   394 end;
   395 
   396 local
   397   fun ps args = mapn (fn n => fn _ => %:("pat" ^ string_of_int n)) 1 args;
   398 
   399   fun pat_lhs (con,_,args) = mk_branch (list_comb (%%:(pat_name con), ps args));
   400 
   401   fun pat_rhs (con,_,[]) = mk_return ((%:"rhs") ` HOLogic.unit)
   402     | pat_rhs (con,_,args) =
   403         (mk_branch (mk_ctuple_pat (ps args)))
   404           `(%:"rhs")`(mk_ctuple (map %# args));
   405 
   406   fun pat_strict c =
   407     let
   408       val axs = @{thm branch_def} :: axs_pat_def;
   409       val goal = mk_trp (strict (pat_lhs c ` (%:"rhs")));
   410       val tacs = [simp_tac (HOLCF_ss addsimps [when_strict]) 1];
   411     in pg axs goal (K tacs) end;
   412 
   413   fun pat_app c (con, _, args) =
   414     let
   415       val axs = @{thm branch_def} :: axs_pat_def;
   416       val lhs = (pat_lhs c)`(%:"rhs")`(con_app con args);
   417       val rhs = if con = first c then pat_rhs c else mk_fail;
   418       val goal = lift_defined %: (nonlazy args, mk_trp (lhs === rhs));
   419       val tacs = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   420     in pg axs goal (K tacs) end;
   421 
   422   val _ = trace " Proving pat_stricts...";
   423   val pat_stricts = map pat_strict cons;
   424   val _ = trace " Proving pat_apps...";
   425   val pat_apps = maps (fn c => map (pat_app c) cons) cons;
   426 in
   427   val pat_rews = pat_stricts @ pat_apps;
   428 end;
   429 
   430 local
   431   fun con_strict (con, _, args) = 
   432     let
   433       val rules = abs_strict :: @{thms con_strict_rules};
   434       fun one_strict vn =
   435         let
   436           fun f arg = if vname arg = vn then UU else %# arg;
   437           val goal = mk_trp (con_app2 con f args === UU);
   438           val tacs = [simp_tac (HOL_basic_ss addsimps rules) 1];
   439         in pg con_appls goal (K tacs) end;
   440     in map one_strict (nonlazy args) end;
   441 
   442   fun con_defin (con, _, args) =
   443     let
   444       fun iff_disj (t, []) = HOLogic.mk_not t
   445         | iff_disj (t, ts) = t === foldr1 HOLogic.mk_disj ts;
   446       val lhs = con_app con args === UU;
   447       val rhss = map (fn x => %:x === UU) (nonlazy args);
   448       val goal = mk_trp (iff_disj (lhs, rhss));
   449       val rule1 = iso_locale RS @{thm iso.abs_defined_iff};
   450       val rules = rule1 :: @{thms con_defined_iff_rules};
   451       val tacs = [simp_tac (HOL_ss addsimps rules) 1];
   452     in pg con_appls goal (K tacs) end;
   453 in
   454   val _ = trace " Proving con_stricts...";
   455   val con_stricts = maps con_strict cons;
   456   val _ = trace " Proving con_defins...";
   457   val con_defins = map con_defin cons;
   458   val con_rews = con_stricts @ con_defins;
   459 end;
   460 
   461 val _ = trace " Proving dist_les...";
   462 val dist_les =
   463   let
   464     fun dist (con1, args1) (con2, args2) =
   465       let
   466         fun iff_disj (t, []) = HOLogic.mk_not t
   467           | iff_disj (t, ts) = t === foldr1 HOLogic.mk_disj ts;
   468         val lhs = con_app con1 args1 << con_app con2 args2;
   469         val rhss = map (fn x => %:x === UU) (nonlazy args1);
   470         val goal = mk_trp (iff_disj (lhs, rhss));
   471         val rule1 = iso_locale RS @{thm iso.abs_below};
   472         val rules = rule1 :: @{thms con_below_iff_rules};
   473         val tacs = [simp_tac (HOL_ss addsimps rules) 1];
   474       in pg con_appls goal (K tacs) end;
   475 
   476     fun distinct (con1, _, args1) (con2, _, args2) =
   477         let
   478           val arg1 = (con1, args1);
   479           val arg2 =
   480             (con2, ListPair.map (fn (arg,vn) => upd_vname (K vn) arg)
   481               (args2, Name.variant_list (map vname args1) (map vname args2)));
   482         in [dist arg1 arg2, dist arg2 arg1] end;
   483     fun distincts []      = []
   484       | distincts (c::cs) = maps (distinct c) cs @ distincts cs;
   485   in distincts cons end;
   486 
   487 val _ = trace " Proving dist_eqs...";
   488 val dist_eqs =
   489   let
   490     fun dist (con1, args1) (con2, args2) =
   491       let
   492         fun iff_disj (t, [], us) = HOLogic.mk_not t
   493           | iff_disj (t, ts, []) = HOLogic.mk_not t
   494           | iff_disj (t, ts, us) =
   495             let
   496               val disj1 = foldr1 HOLogic.mk_disj ts;
   497               val disj2 = foldr1 HOLogic.mk_disj us;
   498             in t === HOLogic.mk_conj (disj1, disj2) end;
   499         val lhs = con_app con1 args1 === con_app con2 args2;
   500         val rhss1 = map (fn x => %:x === UU) (nonlazy args1);
   501         val rhss2 = map (fn x => %:x === UU) (nonlazy args2);
   502         val goal = mk_trp (iff_disj (lhs, rhss1, rhss2));
   503         val rule1 = iso_locale RS @{thm iso.abs_eq};
   504         val rules = rule1 :: @{thms con_eq_iff_rules};
   505         val tacs = [simp_tac (HOL_ss addsimps rules) 1];
   506       in pg con_appls goal (K tacs) end;
   507 
   508     fun distinct (con1, _, args1) (con2, _, args2) =
   509         let
   510           val arg1 = (con1, args1);
   511           val arg2 =
   512             (con2, ListPair.map (fn (arg,vn) => upd_vname (K vn) arg)
   513               (args2, Name.variant_list (map vname args1) (map vname args2)));
   514         in [dist arg1 arg2, dist arg2 arg1] end;
   515     fun distincts []      = []
   516       | distincts (c::cs) = maps (distinct c) cs @ distincts cs;
   517   in distincts cons end;
   518 
   519 local 
   520   fun pgterm rel con args =
   521     let
   522       fun append s = upd_vname (fn v => v^s);
   523       val (largs, rargs) = (args, map (append "'") args);
   524       val concl =
   525         foldr1 mk_conj (ListPair.map rel (map %# largs, map %# rargs));
   526       val prem = rel (con_app con largs, con_app con rargs);
   527       val sargs = case largs of [_] => [] | _ => nonlazy args;
   528       val prop = lift_defined %: (sargs, mk_trp (prem === concl));
   529     in pg con_appls prop end;
   530   val cons' = filter (fn (_, _, args) => args<>[]) cons;
   531 in
   532   val _ = trace " Proving inverts...";
   533   val inverts =
   534     let
   535       val abs_less = ax_abs_iso RS (allI RS injection_less);
   536       val tacs =
   537         [asm_full_simp_tac (HOLCF_ss addsimps [abs_less, spair_less]) 1];
   538     in map (fn (con, _, args) => pgterm (op <<) con args (K tacs)) cons' end;
   539 
   540   val _ = trace " Proving injects...";
   541   val injects =
   542     let
   543       val abs_eq = ax_abs_iso RS (allI RS injection_eq);
   544       val tacs = [asm_full_simp_tac (HOLCF_ss addsimps [abs_eq, spair_eq]) 1];
   545     in map (fn (con, _, args) => pgterm (op ===) con args (K tacs)) cons' end;
   546 end;
   547 
   548 (* ----- theorems concerning one induction step ----------------------------- *)
   549 
   550 val copy_strict =
   551   let
   552     val _ = trace " Proving copy_strict...";
   553     val goal = mk_trp (strict (dc_copy `% "f"));
   554     val rules = [abs_strict, rep_strict] @ @{thms domain_map_stricts};
   555     val tacs = [asm_simp_tac (HOLCF_ss addsimps rules) 1];
   556   in
   557     SOME (pg [ax_copy_def] goal (K tacs))
   558     handle
   559       THM (s, _, _) => (trace s; NONE)
   560     | ERROR s => (trace s; NONE)
   561   end;
   562 
   563 local
   564   fun copy_app (con, _, args) =
   565     let
   566       val lhs = dc_copy`%"f"`(con_app con args);
   567       fun one_rhs arg =
   568           if Datatype_Aux.is_rec_type (dtyp_of arg)
   569           then Domain_Axioms.copy_of_dtyp map_tab
   570                  (proj (%:"f") eqs) (dtyp_of arg) ` (%# arg)
   571           else (%# arg);
   572       val rhs = con_app2 con one_rhs args;
   573       fun is_rec arg = Datatype_Aux.is_rec_type (dtyp_of arg);
   574       fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
   575       fun nonlazy_rec args = map vname (filter is_nonlazy_rec args);
   576       val goal = lift_defined %: (nonlazy_rec args, mk_trp (lhs === rhs));
   577       val args' = filter_out (fn a => is_rec a orelse is_lazy a) args;
   578       val stricts = abs_strict :: rep_strict :: @{thms domain_map_stricts};
   579                         (* FIXME! case_UU_tac *)
   580       fun tacs1 ctxt = map (case_UU_tac ctxt stricts 1 o vname) args';
   581       val rules = [ax_abs_iso] @ @{thms domain_map_simps};
   582       val tacs2 = [asm_simp_tac (HOLCF_ss addsimps rules) 1];
   583     in pg (ax_copy_def::con_appls) goal (fn ctxt => (tacs1 ctxt @ tacs2)) end;
   584 in
   585   val _ = trace " Proving copy_apps...";
   586   val copy_apps = map copy_app cons;
   587 end;
   588 
   589 local
   590   fun one_strict (con, _, args) = 
   591     let
   592       val goal = mk_trp (dc_copy`UU`(con_app con args) === UU);
   593       val rews = the_list copy_strict @ copy_apps @ con_rews;
   594                         (* FIXME! case_UU_tac *)
   595       fun tacs ctxt = map (case_UU_tac ctxt rews 1) (nonlazy args) @
   596         [asm_simp_tac (HOLCF_ss addsimps rews) 1];
   597     in
   598       SOME (pg [] goal tacs)
   599       handle
   600         THM (s, _, _) => (trace s; NONE)
   601       | ERROR s => (trace s; NONE)
   602     end;
   603 
   604   fun has_nonlazy_rec (_, _, args) = exists is_nonlazy_rec args;
   605 in
   606   val _ = trace " Proving copy_stricts...";
   607   val copy_stricts = map_filter one_strict (filter has_nonlazy_rec cons);
   608 end;
   609 
   610 val copy_rews = the_list copy_strict @ copy_apps @ copy_stricts;
   611 
   612 in
   613   thy
   614     |> Sign.add_path (Long_Name.base_name dname)
   615     |> snd o PureThy.add_thmss [
   616         ((Binding.name "iso_rews"  , iso_rews    ), [Simplifier.simp_add]),
   617         ((Binding.name "exhaust"   , [exhaust]   ), []),
   618         ((Binding.name "casedist"  , [casedist]  ), [Induct.cases_type dname]),
   619         ((Binding.name "when_rews" , when_rews   ), [Simplifier.simp_add]),
   620         ((Binding.name "compacts"  , con_compacts), [Simplifier.simp_add]),
   621         ((Binding.name "con_rews"  , con_rews    ),
   622          [Simplifier.simp_add, Fixrec.fixrec_simp_add]),
   623         ((Binding.name "sel_rews"  , sel_rews    ), [Simplifier.simp_add]),
   624         ((Binding.name "dis_rews"  , dis_rews    ), [Simplifier.simp_add]),
   625         ((Binding.name "pat_rews"  , pat_rews    ), [Simplifier.simp_add]),
   626         ((Binding.name "dist_les"  , dist_les    ), [Simplifier.simp_add]),
   627         ((Binding.name "dist_eqs"  , dist_eqs    ), [Simplifier.simp_add]),
   628         ((Binding.name "inverts"   , inverts     ), [Simplifier.simp_add]),
   629         ((Binding.name "injects"   , injects     ), [Simplifier.simp_add]),
   630         ((Binding.name "copy_rews" , copy_rews   ), [Simplifier.simp_add]),
   631         ((Binding.name "match_rews", mat_rews    ),
   632          [Simplifier.simp_add, Fixrec.fixrec_simp_add])]
   633     |> Sign.parent_path
   634     |> pair (iso_rews @ when_rews @ con_rews @ sel_rews @ dis_rews @
   635         pat_rews @ dist_les @ dist_eqs @ copy_rews)
   636 end; (* let *)
   637 
   638 fun comp_theorems (comp_dnam, eqs: eq list) thy =
   639 let
   640 val global_ctxt = ProofContext.init thy;
   641 val map_tab = Domain_Isomorphism.get_map_tab thy;
   642 
   643 val dnames = map (fst o fst) eqs;
   644 val conss  = map  snd        eqs;
   645 val comp_dname = Sign.full_bname thy comp_dnam;
   646 
   647 val _ = message ("Proving induction properties of domain "^comp_dname^" ...");
   648 val pg = pg' thy;
   649 
   650 (* ----- getting the composite axiom and definitions ------------------------ *)
   651 
   652 local
   653   fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
   654 in
   655   val axs_reach      = map (ga "reach"     ) dnames;
   656   val axs_take_def   = map (ga "take_def"  ) dnames;
   657   val axs_finite_def = map (ga "finite_def") dnames;
   658   val ax_copy2_def   =      ga "copy_def"  comp_dnam;
   659 (* TEMPORARILY DISABLED
   660   val ax_bisim_def   =      ga "bisim_def" comp_dnam;
   661 TEMPORARILY DISABLED *)
   662 end;
   663 
   664 local
   665   fun gt  s dn = PureThy.get_thm  thy (dn ^ "." ^ s);
   666   fun gts s dn = PureThy.get_thms thy (dn ^ "." ^ s);
   667 in
   668   val cases = map (gt  "casedist" ) dnames;
   669   val con_rews  = maps (gts "con_rews" ) dnames;
   670   val copy_rews = maps (gts "copy_rews") dnames;
   671 end;
   672 
   673 fun dc_take dn = %%:(dn^"_take");
   674 val x_name = idx_name dnames "x"; 
   675 val P_name = idx_name dnames "P";
   676 val n_eqs = length eqs;
   677 
   678 (* ----- theorems concerning finite approximation and finite induction ------ *)
   679 
   680 local
   681   val iterate_Cprod_ss = global_simpset_of @{theory Fix};
   682   val copy_con_rews  = copy_rews @ con_rews;
   683   val copy_take_defs =
   684     (if n_eqs = 1 then [] else [ax_copy2_def]) @ axs_take_def;
   685   val _ = trace " Proving take_stricts...";
   686   fun one_take_strict ((dn, args), _) =
   687     let
   688       val goal = mk_trp (strict (dc_take dn $ %:"n"));
   689       val rules = [
   690         @{thm monofun_fst [THEN monofunE]},
   691         @{thm monofun_snd [THEN monofunE]}];
   692       val tacs = [
   693         rtac @{thm UU_I} 1,
   694         rtac @{thm below_eq_trans} 1,
   695         resolve_tac axs_reach 2,
   696         rtac @{thm monofun_cfun_fun} 1,
   697         REPEAT (resolve_tac rules 1),
   698         rtac @{thm iterate_below_fix} 1];
   699     in pg axs_take_def goal (K tacs) end;
   700   val take_stricts = map one_take_strict eqs;
   701   fun take_0 n dn =
   702     let
   703       val goal = mk_trp ((dc_take dn $ @{term "0::nat"}) `% x_name n === UU);
   704     in pg axs_take_def goal (K [simp_tac iterate_Cprod_ss 1]) end;
   705   val take_0s = mapn take_0 1 dnames;
   706   val _ = trace " Proving take_apps...";
   707   fun one_take_app dn (con, _, args) =
   708     let
   709       fun mk_take n = dc_take (List.nth (dnames, n)) $ %:"n";
   710       fun one_rhs arg =
   711           if Datatype_Aux.is_rec_type (dtyp_of arg)
   712           then Domain_Axioms.copy_of_dtyp map_tab
   713                  mk_take (dtyp_of arg) ` (%# arg)
   714           else (%# arg);
   715       val lhs = (dc_take dn $ (%%:"Suc" $ %:"n"))`(con_app con args);
   716       val rhs = con_app2 con one_rhs args;
   717       fun is_rec arg = Datatype_Aux.is_rec_type (dtyp_of arg);
   718       fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
   719       fun nonlazy_rec args = map vname (filter is_nonlazy_rec args);
   720       val goal = lift_defined %: (nonlazy_rec args, mk_trp (lhs === rhs));
   721       val tacs = [asm_simp_tac (HOLCF_ss addsimps copy_con_rews) 1];
   722     in pg copy_take_defs goal (K tacs) end;
   723   fun one_take_apps ((dn, _), cons) = map (one_take_app dn) cons;
   724   val take_apps = maps one_take_apps eqs;
   725 in
   726   val take_rews = map Drule.export_without_context
   727     (take_stricts @ take_0s @ take_apps);
   728 end; (* local *)
   729 
   730 local
   731   fun one_con p (con, _, args) =
   732     let
   733       val P_names = map P_name (1 upto (length dnames));
   734       val vns = Name.variant_list P_names (map vname args);
   735       val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
   736       fun ind_hyp arg = %:(P_name (1 + rec_of arg)) $ bound_arg args arg;
   737       val t1 = mk_trp (%:p $ con_app2 con (bound_arg args) args);
   738       val t2 = lift ind_hyp (filter is_rec args, t1);
   739       val t3 = lift_defined (bound_arg vns) (nonlazy_vns, t2);
   740     in Library.foldr mk_All (vns, t3) end;
   741 
   742   fun one_eq ((p, cons), concl) =
   743     mk_trp (%:p $ UU) ===> Logic.list_implies (map (one_con p) cons, concl);
   744 
   745   fun ind_term concf = Library.foldr one_eq
   746     (mapn (fn n => fn x => (P_name n, x)) 1 conss,
   747      mk_trp (foldr1 mk_conj (mapn concf 1 dnames)));
   748   val take_ss = HOL_ss addsimps take_rews;
   749   fun quant_tac ctxt i = EVERY
   750     (mapn (fn n => fn _ => res_inst_tac ctxt [(("x", 0), x_name n)] spec i) 1 dnames);
   751 
   752   fun ind_prems_tac prems = EVERY
   753     (maps (fn cons =>
   754       (resolve_tac prems 1 ::
   755         maps (fn (_,_,args) => 
   756           resolve_tac prems 1 ::
   757           map (K(atac 1)) (nonlazy args) @
   758           map (K(atac 1)) (filter is_rec args))
   759         cons))
   760       conss);
   761   local 
   762     (* check whether every/exists constructor of the n-th part of the equation:
   763        it has a possibly indirectly recursive argument that isn't/is possibly 
   764        indirectly lazy *)
   765     fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
   766           is_rec arg andalso not(rec_of arg mem ns) andalso
   767           ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
   768             rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
   769               (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
   770           ) o third) cons;
   771     fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
   772     fun warn (n,cons) =
   773       if all_rec_to [] false (n,cons)
   774       then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
   775       else false;
   776     fun lazy_rec_to ns = rec_to exists I  lazy_rec_to ns;
   777 
   778   in
   779     val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   780     val is_emptys = map warn n__eqs;
   781     val is_finite = forall (not o lazy_rec_to [] false) n__eqs;
   782   end;
   783 in (* local *)
   784   val _ = trace " Proving finite_ind...";
   785   val finite_ind =
   786     let
   787       fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
   788       val goal = ind_term concf;
   789 
   790       fun tacf {prems, context} =
   791         let
   792           val tacs1 = [
   793             quant_tac context 1,
   794             simp_tac HOL_ss 1,
   795             InductTacs.induct_tac context [[SOME "n"]] 1,
   796             simp_tac (take_ss addsimps prems) 1,
   797             TRY (safe_tac HOL_cs)];
   798           fun arg_tac arg =
   799                         (* FIXME! case_UU_tac *)
   800             case_UU_tac context (prems @ con_rews) 1
   801               (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
   802           fun con_tacs (con, _, args) = 
   803             asm_simp_tac take_ss 1 ::
   804             map arg_tac (filter is_nonlazy_rec args) @
   805             [resolve_tac prems 1] @
   806             map (K (atac 1)) (nonlazy args) @
   807             map (K (etac spec 1)) (filter is_rec args);
   808           fun cases_tacs (cons, cases) =
   809             res_inst_tac context [(("x", 0), "x")] cases 1 ::
   810             asm_simp_tac (take_ss addsimps prems) 1 ::
   811             maps con_tacs cons;
   812         in
   813           tacs1 @ maps cases_tacs (conss ~~ cases)
   814         end;
   815     in pg'' thy [] goal tacf
   816        handle ERROR _ => (warning "Proof of finite_ind failed."; TrueI)
   817     end;
   818 
   819   val _ = trace " Proving take_lemmas...";
   820   val take_lemmas =
   821     let
   822       fun take_lemma n (dn, ax_reach) =
   823         let
   824           val lhs = dc_take dn $ Bound 0 `%(x_name n);
   825           val rhs = dc_take dn $ Bound 0 `%(x_name n^"'");
   826           val concl = mk_trp (%:(x_name n) === %:(x_name n^"'"));
   827           val goal = mk_All ("n", mk_trp (lhs === rhs)) ===> concl;
   828           val rules = [contlub_fst RS contlubE RS ssubst,
   829                        contlub_snd RS contlubE RS ssubst];
   830           fun tacf {prems, context} = [
   831             res_inst_tac context [(("t", 0), x_name n    )] (ax_reach RS subst) 1,
   832             res_inst_tac context [(("t", 0), x_name n^"'")] (ax_reach RS subst) 1,
   833             stac fix_def2 1,
   834             REPEAT (CHANGED
   835               (resolve_tac rules 1 THEN chain_tac 1)),
   836             stac contlub_cfun_fun 1,
   837             stac contlub_cfun_fun 2,
   838             rtac lub_equal 3,
   839             chain_tac 1,
   840             rtac allI 1,
   841             resolve_tac prems 1];
   842         in pg'' thy axs_take_def goal tacf end;
   843     in mapn take_lemma 1 (dnames ~~ axs_reach) end;
   844 
   845 (* ----- theorems concerning finiteness and induction ----------------------- *)
   846 
   847   val _ = trace " Proving finites, ind...";
   848   val (finites, ind) =
   849   (
   850     if is_finite
   851     then (* finite case *)
   852       let 
   853         fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %:"x" === %:"x");
   854         fun dname_lemma dn =
   855           let
   856             val prem1 = mk_trp (defined (%:"x"));
   857             val disj1 = mk_all ("n", dc_take dn $ Bound 0 ` %:"x" === UU);
   858             val prem2 = mk_trp (mk_disj (disj1, take_enough dn));
   859             val concl = mk_trp (take_enough dn);
   860             val goal = prem1 ===> prem2 ===> concl;
   861             val tacs = [
   862               etac disjE 1,
   863               etac notE 1,
   864               resolve_tac take_lemmas 1,
   865               asm_simp_tac take_ss 1,
   866               atac 1];
   867           in pg [] goal (K tacs) end;
   868         val _ = trace " Proving finite_lemmas1a";
   869         val finite_lemmas1a = map dname_lemma dnames;
   870  
   871         val _ = trace " Proving finite_lemma1b";
   872         val finite_lemma1b =
   873           let
   874             fun mk_eqn n ((dn, args), _) =
   875               let
   876                 val disj1 = dc_take dn $ Bound 1 ` Bound 0 === UU;
   877                 val disj2 = dc_take dn $ Bound 1 ` Bound 0 === Bound 0;
   878               in
   879                 mk_constrainall
   880                   (x_name n, Type (dn,args), mk_disj (disj1, disj2))
   881               end;
   882             val goal =
   883               mk_trp (mk_all ("n", foldr1 mk_conj (mapn mk_eqn 1 eqs)));
   884             fun arg_tacs ctxt vn = [
   885               eres_inst_tac ctxt [(("x", 0), vn)] all_dupE 1,
   886               etac disjE 1,
   887               asm_simp_tac (HOL_ss addsimps con_rews) 1,
   888               asm_simp_tac take_ss 1];
   889             fun con_tacs ctxt (con, _, args) =
   890               asm_simp_tac take_ss 1 ::
   891               maps (arg_tacs ctxt) (nonlazy_rec args);
   892             fun foo_tacs ctxt n (cons, cases) =
   893               simp_tac take_ss 1 ::
   894               rtac allI 1 ::
   895               res_inst_tac ctxt [(("x", 0), x_name n)] cases 1 ::
   896               asm_simp_tac take_ss 1 ::
   897               maps (con_tacs ctxt) cons;
   898             fun tacs ctxt =
   899               rtac allI 1 ::
   900               InductTacs.induct_tac ctxt [[SOME "n"]] 1 ::
   901               simp_tac take_ss 1 ::
   902               TRY (safe_tac (empty_cs addSEs [conjE] addSIs [conjI])) ::
   903               flat (mapn (foo_tacs ctxt) 1 (conss ~~ cases));
   904           in pg [] goal tacs end;
   905 
   906         fun one_finite (dn, l1b) =
   907           let
   908             val goal = mk_trp (%%:(dn^"_finite") $ %:"x");
   909             fun tacs ctxt = [
   910                         (* FIXME! case_UU_tac *)
   911               case_UU_tac ctxt take_rews 1 "x",
   912               eresolve_tac finite_lemmas1a 1,
   913               step_tac HOL_cs 1,
   914               step_tac HOL_cs 1,
   915               cut_facts_tac [l1b] 1,
   916               fast_tac HOL_cs 1];
   917           in pg axs_finite_def goal tacs end;
   918 
   919         val _ = trace " Proving finites";
   920         val finites = map one_finite (dnames ~~ atomize global_ctxt finite_lemma1b);
   921         val _ = trace " Proving ind";
   922         val ind =
   923           let
   924             fun concf n dn = %:(P_name n) $ %:(x_name n);
   925             fun tacf {prems, context} =
   926               let
   927                 fun finite_tacs (finite, fin_ind) = [
   928                   rtac(rewrite_rule axs_finite_def finite RS exE)1,
   929                   etac subst 1,
   930                   rtac fin_ind 1,
   931                   ind_prems_tac prems];
   932               in
   933                 TRY (safe_tac HOL_cs) ::
   934                 maps finite_tacs (finites ~~ atomize global_ctxt finite_ind)
   935               end;
   936           in pg'' thy [] (ind_term concf) tacf end;
   937       in (finites, ind) end (* let *)
   938 
   939     else (* infinite case *)
   940       let
   941         fun one_finite n dn =
   942           read_instantiate global_ctxt [(("P", 0), dn ^ "_finite " ^ x_name n)] excluded_middle;
   943         val finites = mapn one_finite 1 dnames;
   944 
   945         val goal =
   946           let
   947             fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
   948             fun concf n dn = %:(P_name n) $ %:(x_name n);
   949           in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
   950         val cont_rules =
   951             [cont_id, cont_const, cont2cont_Rep_CFun,
   952              cont2cont_fst, cont2cont_snd];
   953         fun tacf {prems, context} =
   954           map (fn ax_reach => rtac (ax_reach RS subst) 1) axs_reach @ [
   955           quant_tac context 1,
   956           rtac (adm_impl_admw RS wfix_ind) 1,
   957           REPEAT_DETERM (rtac adm_all 1),
   958           REPEAT_DETERM (
   959             TRY (rtac adm_conj 1) THEN 
   960             rtac adm_subst 1 THEN 
   961             REPEAT (resolve_tac cont_rules 1) THEN
   962             resolve_tac prems 1),
   963           strip_tac 1,
   964           rtac (rewrite_rule axs_take_def finite_ind) 1,
   965           ind_prems_tac prems];
   966         val ind = (pg'' thy [] goal tacf
   967           handle ERROR _ =>
   968             (warning "Cannot prove infinite induction rule"; TrueI));
   969       in (finites, ind) end
   970   )
   971       handle THM _ =>
   972              (warning "Induction proofs failed (THM raised)."; ([], TrueI))
   973            | ERROR _ =>
   974              (warning "Cannot prove induction rule"; ([], TrueI));
   975 
   976 
   977 end; (* local *)
   978 
   979 (* ----- theorem concerning coinduction ------------------------------------- *)
   980 
   981 (* COINDUCTION TEMPORARILY DISABLED
   982 local
   983   val xs = mapn (fn n => K (x_name n)) 1 dnames;
   984   fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
   985   val take_ss = HOL_ss addsimps take_rews;
   986   val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
   987   val _ = trace " Proving coind_lemma...";
   988   val coind_lemma =
   989     let
   990       fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
   991       fun mk_eqn n dn =
   992         (dc_take dn $ %:"n" ` bnd_arg n 0) ===
   993         (dc_take dn $ %:"n" ` bnd_arg n 1);
   994       fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
   995       val goal =
   996         mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
   997           Library.foldr mk_all2 (xs,
   998             Library.foldr mk_imp (mapn mk_prj 0 dnames,
   999               foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
  1000       fun x_tacs ctxt n x = [
  1001         rotate_tac (n+1) 1,
  1002         etac all2E 1,
  1003         eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
  1004         TRY (safe_tac HOL_cs),
  1005         REPEAT (CHANGED (asm_simp_tac take_ss 1))];
  1006       fun tacs ctxt = [
  1007         rtac impI 1,
  1008         InductTacs.induct_tac ctxt [[SOME "n"]] 1,
  1009         simp_tac take_ss 1,
  1010         safe_tac HOL_cs] @
  1011         flat (mapn (x_tacs ctxt) 0 xs);
  1012     in pg [ax_bisim_def] goal tacs end;
  1013 in
  1014   val _ = trace " Proving coind...";
  1015   val coind = 
  1016     let
  1017       fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
  1018       fun mk_eqn x = %:x === %:(x^"'");
  1019       val goal =
  1020         mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
  1021           Logic.list_implies (mapn mk_prj 0 xs,
  1022             mk_trp (foldr1 mk_conj (map mk_eqn xs)));
  1023       val tacs =
  1024         TRY (safe_tac HOL_cs) ::
  1025         maps (fn take_lemma => [
  1026           rtac take_lemma 1,
  1027           cut_facts_tac [coind_lemma] 1,
  1028           fast_tac HOL_cs 1])
  1029         take_lemmas;
  1030     in pg [] goal (K tacs) end;
  1031 end; (* local *)
  1032 COINDUCTION TEMPORARILY DISABLED *)
  1033 
  1034 val inducts = Project_Rule.projections (ProofContext.init thy) ind;
  1035 fun ind_rule (dname, rule) = ((Binding.empty, [rule]), [Induct.induct_type dname]);
  1036 val induct_failed = (Thm.prop_of ind = Thm.prop_of TrueI);
  1037 
  1038 in thy |> Sign.add_path comp_dnam
  1039        |> snd o PureThy.add_thmss [
  1040            ((Binding.name "take_rews"  , take_rews   ), [Simplifier.simp_add]),
  1041            ((Binding.name "take_lemmas", take_lemmas ), []),
  1042            ((Binding.name "finites"    , finites     ), []),
  1043            ((Binding.name "finite_ind" , [finite_ind]), []),
  1044            ((Binding.name "ind"        , [ind]       ), [])(*,
  1045            ((Binding.name "coind"      , [coind]     ), [])*)]
  1046        |> (if induct_failed then I
  1047            else snd o PureThy.add_thmss (map ind_rule (dnames ~~ inducts)))
  1048        |> Sign.parent_path |> pair take_rews
  1049 end; (* let *)
  1050 end; (* struct *)