src/HOLCF/Tools/Domain/domain_theorems.ML
author huffman
Fri Feb 26 08:37:03 2010 -0800 (2010-02-26)
changeset 35448 f9f73f0475eb
parent 35446 b719dad322fa
child 35451 a726a033b313
permissions -rw-r--r--
move proof of compactness rules into domain_constructors.ML
     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 axs_con_def = #con_defs 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 val _ = trace "Proving con_appls...";
   248 val con_appls = map appl_of_def axs_con_def;
   249 
   250 local
   251   fun arg2typ n arg =
   252     let val t = TVar (("'a", n), pcpoS)
   253     in (n + 1, if is_lazy arg then mk_uT t else t) end;
   254 
   255   fun args2typ n [] = (n, oneT)
   256     | args2typ n [arg] = arg2typ n arg
   257     | args2typ n (arg::args) =
   258     let
   259       val (n1, t1) = arg2typ n arg;
   260       val (n2, t2) = args2typ n1 args
   261     in (n2, mk_sprodT (t1, t2)) end;
   262 
   263   fun cons2typ n [] = (n,oneT)
   264     | cons2typ n [con] = args2typ n (third con)
   265     | cons2typ n (con::cons) =
   266     let
   267       val (n1, t1) = args2typ n (third con);
   268       val (n2, t2) = cons2typ n1 cons
   269     in (n2, mk_ssumT (t1, t2)) end;
   270 in
   271   fun cons2ctyp cons = ctyp_of thy (snd (cons2typ 1 cons));
   272 end;
   273 
   274 local
   275   val iso_swap = iso_locale RS iso_iso_swap;
   276   fun one_con (con, _, args) =
   277     let
   278       val vns = Name.variant_list ["x"] (map vname args);
   279       val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
   280       val eqn = %:x_name === con_app2 con %: vns;
   281       val conj = foldr1 mk_conj (eqn :: map (defined o %:) nonlazy_vns);
   282     in Library.foldr mk_ex (vns, conj) end;
   283 
   284   val conj_assoc = @{thm conj_assoc};
   285   val exh = foldr1 mk_disj ((%:x_name === UU) :: map one_con cons);
   286   val thm1 = instantiate' [SOME (cons2ctyp cons)] [] exh_start;
   287   val thm2 = rewrite_rule (map mk_meta_eq ex_defined_iffs) thm1;
   288   val thm3 = rewrite_rule [mk_meta_eq @{thm conj_assoc}] thm2;
   289 
   290   (* first 3 rules replace "x = UU \/ P" with "rep$x = UU \/ P" *)
   291   val tacs = [
   292     rtac disjE 1,
   293     etac (rep_defin' RS disjI1) 2,
   294     etac disjI2 2,
   295     rewrite_goals_tac [mk_meta_eq iso_swap],
   296     rtac thm3 1];
   297 in
   298   val _ = trace " Proving exhaust...";
   299   val exhaust = pg con_appls (mk_trp exh) (K tacs);
   300   val _ = trace " Proving casedist...";
   301   val casedist =
   302     Drule.export_without_context (rewrite_rule exh_casedists (exhaust RS exh_casedist0));
   303 end;
   304 
   305 local 
   306   fun bind_fun t = Library.foldr mk_All (when_funs cons, t);
   307   fun bound_fun i _ = Bound (length cons - i);
   308   val when_app = list_ccomb (%%:(dname^"_when"), mapn bound_fun 1 cons);
   309 in
   310   val _ = trace " Proving when_strict...";
   311   val when_strict =
   312     let
   313       val axs = [when_appl, mk_meta_eq rep_strict];
   314       val goal = bind_fun (mk_trp (strict when_app));
   315       val tacs = [resolve_tac [sscase1, ssplit1, strictify1] 1];
   316     in pg axs goal (K tacs) end;
   317 
   318   val _ = trace " Proving when_apps...";
   319   val when_apps =
   320     let
   321       fun one_when n (con, _, args) =
   322         let
   323           val axs = when_appl :: con_appls;
   324           val goal = bind_fun (lift_defined %: (nonlazy args, 
   325                 mk_trp (when_app`(con_app con args) ===
   326                        list_ccomb (bound_fun n 0, map %# args))));
   327           val tacs = [asm_simp_tac (HOLCF_ss addsimps [ax_abs_iso]) 1];
   328         in pg axs goal (K tacs) end;
   329     in mapn one_when 1 cons end;
   330 end;
   331 val when_rews = when_strict :: when_apps;
   332 
   333 (* ----- theorems concerning the constructors, discriminators and selectors - *)
   334 
   335 local
   336   fun dis_strict (con, _, _) =
   337     let
   338       val goal = mk_trp (strict (%%:(dis_name con)));
   339     in pg axs_dis_def goal (K [rtac when_strict 1]) end;
   340 
   341   fun dis_app c (con, _, args) =
   342     let
   343       val lhs = %%:(dis_name c) ` con_app con args;
   344       val rhs = if con = c then TT else FF;
   345       val goal = lift_defined %: (nonlazy args, mk_trp (lhs === rhs));
   346       val tacs = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   347     in pg axs_dis_def goal (K tacs) end;
   348 
   349   val _ = trace " Proving dis_apps...";
   350   val dis_apps = maps (fn (c,_,_) => map (dis_app c) cons) cons;
   351 
   352   fun dis_defin (con, _, args) =
   353     let
   354       val goal = defined (%:x_name) ==> defined (%%:(dis_name con) `% x_name);
   355       val tacs =
   356         [rtac casedist 1,
   357          contr_tac 1,
   358          DETERM_UNTIL_SOLVED (CHANGED
   359           (asm_simp_tac (HOLCF_ss addsimps dis_apps) 1))];
   360     in pg [] goal (K tacs) end;
   361 
   362   val _ = trace " Proving dis_stricts...";
   363   val dis_stricts = map dis_strict cons;
   364   val _ = trace " Proving dis_defins...";
   365   val dis_defins = map dis_defin cons;
   366 in
   367   val dis_rews = dis_stricts @ dis_defins @ dis_apps;
   368 end;
   369 
   370 local
   371   fun mat_strict (con, _, _) =
   372     let
   373       val goal = mk_trp (%%:(mat_name con) ` UU ` %:"rhs" === UU);
   374       val tacs = [asm_simp_tac (HOLCF_ss addsimps [when_strict]) 1];
   375     in pg axs_mat_def goal (K tacs) end;
   376 
   377   val _ = trace " Proving mat_stricts...";
   378   val mat_stricts = map mat_strict cons;
   379 
   380   fun one_mat c (con, _, args) =
   381     let
   382       val lhs = %%:(mat_name c) ` con_app con args ` %:"rhs";
   383       val rhs =
   384         if con = c
   385         then list_ccomb (%:"rhs", map %# args)
   386         else mk_fail;
   387       val goal = lift_defined %: (nonlazy args, mk_trp (lhs === rhs));
   388       val tacs = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   389     in pg axs_mat_def goal (K tacs) end;
   390 
   391   val _ = trace " Proving mat_apps...";
   392   val mat_apps =
   393     maps (fn (c,_,_) => map (one_mat c) cons) cons;
   394 in
   395   val mat_rews = mat_stricts @ mat_apps;
   396 end;
   397 
   398 local
   399   fun ps args = mapn (fn n => fn _ => %:("pat" ^ string_of_int n)) 1 args;
   400 
   401   fun pat_lhs (con,_,args) = mk_branch (list_comb (%%:(pat_name con), ps args));
   402 
   403   fun pat_rhs (con,_,[]) = mk_return ((%:"rhs") ` HOLogic.unit)
   404     | pat_rhs (con,_,args) =
   405         (mk_branch (mk_ctuple_pat (ps args)))
   406           `(%:"rhs")`(mk_ctuple (map %# args));
   407 
   408   fun pat_strict c =
   409     let
   410       val axs = @{thm branch_def} :: axs_pat_def;
   411       val goal = mk_trp (strict (pat_lhs c ` (%:"rhs")));
   412       val tacs = [simp_tac (HOLCF_ss addsimps [when_strict]) 1];
   413     in pg axs goal (K tacs) end;
   414 
   415   fun pat_app c (con, _, args) =
   416     let
   417       val axs = @{thm branch_def} :: axs_pat_def;
   418       val lhs = (pat_lhs c)`(%:"rhs")`(con_app con args);
   419       val rhs = if con = first c then pat_rhs c else mk_fail;
   420       val goal = lift_defined %: (nonlazy args, mk_trp (lhs === rhs));
   421       val tacs = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   422     in pg axs goal (K tacs) end;
   423 
   424   val _ = trace " Proving pat_stricts...";
   425   val pat_stricts = map pat_strict cons;
   426   val _ = trace " Proving pat_apps...";
   427   val pat_apps = maps (fn c => map (pat_app c) cons) cons;
   428 in
   429   val pat_rews = pat_stricts @ pat_apps;
   430 end;
   431 
   432 local
   433   fun con_strict (con, _, args) = 
   434     let
   435       val rules = abs_strict :: @{thms con_strict_rules};
   436       fun one_strict vn =
   437         let
   438           fun f arg = if vname arg = vn then UU else %# arg;
   439           val goal = mk_trp (con_app2 con f args === UU);
   440           val tacs = [simp_tac (HOL_basic_ss addsimps rules) 1];
   441         in pg con_appls goal (K tacs) end;
   442     in map one_strict (nonlazy args) end;
   443 
   444   fun con_defin (con, _, args) =
   445     let
   446       fun iff_disj (t, []) = HOLogic.mk_not t
   447         | iff_disj (t, ts) = t === foldr1 HOLogic.mk_disj ts;
   448       val lhs = con_app con args === UU;
   449       val rhss = map (fn x => %:x === UU) (nonlazy args);
   450       val goal = mk_trp (iff_disj (lhs, rhss));
   451       val rule1 = iso_locale RS @{thm iso.abs_defined_iff};
   452       val rules = rule1 :: @{thms con_defined_iff_rules};
   453       val tacs = [simp_tac (HOL_ss addsimps rules) 1];
   454     in pg con_appls goal (K tacs) end;
   455 in
   456   val _ = trace " Proving con_stricts...";
   457   val con_stricts = maps con_strict cons;
   458   val _ = trace " Proving con_defins...";
   459   val con_defins = map con_defin cons;
   460   val con_rews = con_stricts @ con_defins;
   461 end;
   462 
   463 val _ = trace " Proving dist_les...";
   464 val dist_les =
   465   let
   466     fun dist (con1, args1) (con2, args2) =
   467       let
   468         fun iff_disj (t, []) = HOLogic.mk_not t
   469           | iff_disj (t, ts) = t === foldr1 HOLogic.mk_disj ts;
   470         val lhs = con_app con1 args1 << con_app con2 args2;
   471         val rhss = map (fn x => %:x === UU) (nonlazy args1);
   472         val goal = mk_trp (iff_disj (lhs, rhss));
   473         val rule1 = iso_locale RS @{thm iso.abs_below};
   474         val rules = rule1 :: @{thms con_below_iff_rules};
   475         val tacs = [simp_tac (HOL_ss addsimps rules) 1];
   476       in pg con_appls goal (K tacs) end;
   477 
   478     fun distinct (con1, _, args1) (con2, _, args2) =
   479         let
   480           val arg1 = (con1, args1);
   481           val arg2 =
   482             (con2, ListPair.map (fn (arg,vn) => upd_vname (K vn) arg)
   483               (args2, Name.variant_list (map vname args1) (map vname args2)));
   484         in [dist arg1 arg2, dist arg2 arg1] end;
   485     fun distincts []      = []
   486       | distincts (c::cs) = maps (distinct c) cs @ distincts cs;
   487   in distincts cons end;
   488 
   489 val _ = trace " Proving dist_eqs...";
   490 val dist_eqs =
   491   let
   492     fun dist (con1, args1) (con2, args2) =
   493       let
   494         fun iff_disj (t, [], us) = HOLogic.mk_not t
   495           | iff_disj (t, ts, []) = HOLogic.mk_not t
   496           | iff_disj (t, ts, us) =
   497             let
   498               val disj1 = foldr1 HOLogic.mk_disj ts;
   499               val disj2 = foldr1 HOLogic.mk_disj us;
   500             in t === HOLogic.mk_conj (disj1, disj2) end;
   501         val lhs = con_app con1 args1 === con_app con2 args2;
   502         val rhss1 = map (fn x => %:x === UU) (nonlazy args1);
   503         val rhss2 = map (fn x => %:x === UU) (nonlazy args2);
   504         val goal = mk_trp (iff_disj (lhs, rhss1, rhss2));
   505         val rule1 = iso_locale RS @{thm iso.abs_eq};
   506         val rules = rule1 :: @{thms con_eq_iff_rules};
   507         val tacs = [simp_tac (HOL_ss addsimps rules) 1];
   508       in pg con_appls goal (K tacs) end;
   509 
   510     fun distinct (con1, _, args1) (con2, _, args2) =
   511         let
   512           val arg1 = (con1, args1);
   513           val arg2 =
   514             (con2, ListPair.map (fn (arg,vn) => upd_vname (K vn) arg)
   515               (args2, Name.variant_list (map vname args1) (map vname args2)));
   516         in [dist arg1 arg2, dist arg2 arg1] end;
   517     fun distincts []      = []
   518       | distincts (c::cs) = maps (distinct c) cs @ distincts cs;
   519   in distincts cons end;
   520 
   521 local 
   522   fun pgterm rel con args =
   523     let
   524       fun append s = upd_vname (fn v => v^s);
   525       val (largs, rargs) = (args, map (append "'") args);
   526       val concl =
   527         foldr1 mk_conj (ListPair.map rel (map %# largs, map %# rargs));
   528       val prem = rel (con_app con largs, con_app con rargs);
   529       val sargs = case largs of [_] => [] | _ => nonlazy args;
   530       val prop = lift_defined %: (sargs, mk_trp (prem === concl));
   531     in pg con_appls prop end;
   532   val cons' = filter (fn (_, _, args) => args<>[]) cons;
   533 in
   534   val _ = trace " Proving inverts...";
   535   val inverts =
   536     let
   537       val abs_less = ax_abs_iso RS (allI RS injection_less);
   538       val tacs =
   539         [asm_full_simp_tac (HOLCF_ss addsimps [abs_less, spair_less]) 1];
   540     in map (fn (con, _, args) => pgterm (op <<) con args (K tacs)) cons' end;
   541 
   542   val _ = trace " Proving injects...";
   543   val injects =
   544     let
   545       val abs_eq = ax_abs_iso RS (allI RS injection_eq);
   546       val tacs = [asm_full_simp_tac (HOLCF_ss addsimps [abs_eq, spair_eq]) 1];
   547     in map (fn (con, _, args) => pgterm (op ===) con args (K tacs)) cons' end;
   548 end;
   549 
   550 (* ----- theorems concerning one induction step ----------------------------- *)
   551 
   552 val copy_strict =
   553   let
   554     val _ = trace " Proving copy_strict...";
   555     val goal = mk_trp (strict (dc_copy `% "f"));
   556     val rules = [abs_strict, rep_strict] @ @{thms domain_map_stricts};
   557     val tacs = [asm_simp_tac (HOLCF_ss addsimps rules) 1];
   558   in
   559     SOME (pg [ax_copy_def] goal (K tacs))
   560     handle
   561       THM (s, _, _) => (trace s; NONE)
   562     | ERROR s => (trace s; NONE)
   563   end;
   564 
   565 local
   566   fun copy_app (con, _, args) =
   567     let
   568       val lhs = dc_copy`%"f"`(con_app con args);
   569       fun one_rhs arg =
   570           if Datatype_Aux.is_rec_type (dtyp_of arg)
   571           then Domain_Axioms.copy_of_dtyp map_tab
   572                  (proj (%:"f") eqs) (dtyp_of arg) ` (%# arg)
   573           else (%# arg);
   574       val rhs = con_app2 con one_rhs args;
   575       fun is_rec arg = Datatype_Aux.is_rec_type (dtyp_of arg);
   576       fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
   577       fun nonlazy_rec args = map vname (filter is_nonlazy_rec args);
   578       val goal = lift_defined %: (nonlazy_rec args, mk_trp (lhs === rhs));
   579       val args' = filter_out (fn a => is_rec a orelse is_lazy a) args;
   580       val stricts = abs_strict :: rep_strict :: @{thms domain_map_stricts};
   581                         (* FIXME! case_UU_tac *)
   582       fun tacs1 ctxt = map (case_UU_tac ctxt stricts 1 o vname) args';
   583       val rules = [ax_abs_iso] @ @{thms domain_map_simps};
   584       val tacs2 = [asm_simp_tac (HOLCF_ss addsimps rules) 1];
   585     in pg (ax_copy_def::con_appls) goal (fn ctxt => (tacs1 ctxt @ tacs2)) end;
   586 in
   587   val _ = trace " Proving copy_apps...";
   588   val copy_apps = map copy_app cons;
   589 end;
   590 
   591 local
   592   fun one_strict (con, _, args) = 
   593     let
   594       val goal = mk_trp (dc_copy`UU`(con_app con args) === UU);
   595       val rews = the_list copy_strict @ copy_apps @ con_rews;
   596                         (* FIXME! case_UU_tac *)
   597       fun tacs ctxt = map (case_UU_tac ctxt rews 1) (nonlazy args) @
   598         [asm_simp_tac (HOLCF_ss addsimps rews) 1];
   599     in
   600       SOME (pg [] goal tacs)
   601       handle
   602         THM (s, _, _) => (trace s; NONE)
   603       | ERROR s => (trace s; NONE)
   604     end;
   605 
   606   fun has_nonlazy_rec (_, _, args) = exists is_nonlazy_rec args;
   607 in
   608   val _ = trace " Proving copy_stricts...";
   609   val copy_stricts = map_filter one_strict (filter has_nonlazy_rec cons);
   610 end;
   611 
   612 val copy_rews = the_list copy_strict @ copy_apps @ copy_stricts;
   613 
   614 in
   615   thy
   616     |> Sign.add_path (Long_Name.base_name dname)
   617     |> snd o PureThy.add_thmss [
   618         ((Binding.name "iso_rews"  , iso_rews    ), [Simplifier.simp_add]),
   619         ((Binding.name "exhaust"   , [exhaust]   ), []),
   620         ((Binding.name "casedist"  , [casedist]  ), [Induct.cases_type dname]),
   621         ((Binding.name "when_rews" , when_rews   ), [Simplifier.simp_add]),
   622         ((Binding.name "compacts"  , con_compacts), [Simplifier.simp_add]),
   623         ((Binding.name "con_rews"  , con_rews    ),
   624          [Simplifier.simp_add, Fixrec.fixrec_simp_add]),
   625         ((Binding.name "sel_rews"  , sel_rews    ), [Simplifier.simp_add]),
   626         ((Binding.name "dis_rews"  , dis_rews    ), [Simplifier.simp_add]),
   627         ((Binding.name "pat_rews"  , pat_rews    ), [Simplifier.simp_add]),
   628         ((Binding.name "dist_les"  , dist_les    ), [Simplifier.simp_add]),
   629         ((Binding.name "dist_eqs"  , dist_eqs    ), [Simplifier.simp_add]),
   630         ((Binding.name "inverts"   , inverts     ), [Simplifier.simp_add]),
   631         ((Binding.name "injects"   , injects     ), [Simplifier.simp_add]),
   632         ((Binding.name "copy_rews" , copy_rews   ), [Simplifier.simp_add]),
   633         ((Binding.name "match_rews", mat_rews    ),
   634          [Simplifier.simp_add, Fixrec.fixrec_simp_add])]
   635     |> Sign.parent_path
   636     |> pair (iso_rews @ when_rews @ con_rews @ sel_rews @ dis_rews @
   637         pat_rews @ dist_les @ dist_eqs @ copy_rews)
   638 end; (* let *)
   639 
   640 fun comp_theorems (comp_dnam, eqs: eq list) thy =
   641 let
   642 val global_ctxt = ProofContext.init thy;
   643 val map_tab = Domain_Isomorphism.get_map_tab thy;
   644 
   645 val dnames = map (fst o fst) eqs;
   646 val conss  = map  snd        eqs;
   647 val comp_dname = Sign.full_bname thy comp_dnam;
   648 
   649 val _ = message ("Proving induction properties of domain "^comp_dname^" ...");
   650 val pg = pg' thy;
   651 
   652 (* ----- getting the composite axiom and definitions ------------------------ *)
   653 
   654 local
   655   fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
   656 in
   657   val axs_reach      = map (ga "reach"     ) dnames;
   658   val axs_take_def   = map (ga "take_def"  ) dnames;
   659   val axs_finite_def = map (ga "finite_def") dnames;
   660   val ax_copy2_def   =      ga "copy_def"  comp_dnam;
   661 (* TEMPORARILY DISABLED
   662   val ax_bisim_def   =      ga "bisim_def" comp_dnam;
   663 TEMPORARILY DISABLED *)
   664 end;
   665 
   666 local
   667   fun gt  s dn = PureThy.get_thm  thy (dn ^ "." ^ s);
   668   fun gts s dn = PureThy.get_thms thy (dn ^ "." ^ s);
   669 in
   670   val cases = map (gt  "casedist" ) dnames;
   671   val con_rews  = maps (gts "con_rews" ) dnames;
   672   val copy_rews = maps (gts "copy_rews") dnames;
   673 end;
   674 
   675 fun dc_take dn = %%:(dn^"_take");
   676 val x_name = idx_name dnames "x"; 
   677 val P_name = idx_name dnames "P";
   678 val n_eqs = length eqs;
   679 
   680 (* ----- theorems concerning finite approximation and finite induction ------ *)
   681 
   682 local
   683   val iterate_Cprod_ss = global_simpset_of @{theory Fix};
   684   val copy_con_rews  = copy_rews @ con_rews;
   685   val copy_take_defs =
   686     (if n_eqs = 1 then [] else [ax_copy2_def]) @ axs_take_def;
   687   val _ = trace " Proving take_stricts...";
   688   fun one_take_strict ((dn, args), _) =
   689     let
   690       val goal = mk_trp (strict (dc_take dn $ %:"n"));
   691       val rules = [
   692         @{thm monofun_fst [THEN monofunE]},
   693         @{thm monofun_snd [THEN monofunE]}];
   694       val tacs = [
   695         rtac @{thm UU_I} 1,
   696         rtac @{thm below_eq_trans} 1,
   697         resolve_tac axs_reach 2,
   698         rtac @{thm monofun_cfun_fun} 1,
   699         REPEAT (resolve_tac rules 1),
   700         rtac @{thm iterate_below_fix} 1];
   701     in pg axs_take_def goal (K tacs) end;
   702   val take_stricts = map one_take_strict eqs;
   703   fun take_0 n dn =
   704     let
   705       val goal = mk_trp ((dc_take dn $ @{term "0::nat"}) `% x_name n === UU);
   706     in pg axs_take_def goal (K [simp_tac iterate_Cprod_ss 1]) end;
   707   val take_0s = mapn take_0 1 dnames;
   708   val _ = trace " Proving take_apps...";
   709   fun one_take_app dn (con, _, args) =
   710     let
   711       fun mk_take n = dc_take (List.nth (dnames, n)) $ %:"n";
   712       fun one_rhs arg =
   713           if Datatype_Aux.is_rec_type (dtyp_of arg)
   714           then Domain_Axioms.copy_of_dtyp map_tab
   715                  mk_take (dtyp_of arg) ` (%# arg)
   716           else (%# arg);
   717       val lhs = (dc_take dn $ (%%:"Suc" $ %:"n"))`(con_app con args);
   718       val rhs = con_app2 con one_rhs args;
   719       fun is_rec arg = Datatype_Aux.is_rec_type (dtyp_of arg);
   720       fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
   721       fun nonlazy_rec args = map vname (filter is_nonlazy_rec args);
   722       val goal = lift_defined %: (nonlazy_rec args, mk_trp (lhs === rhs));
   723       val tacs = [asm_simp_tac (HOLCF_ss addsimps copy_con_rews) 1];
   724     in pg copy_take_defs goal (K tacs) end;
   725   fun one_take_apps ((dn, _), cons) = map (one_take_app dn) cons;
   726   val take_apps = maps one_take_apps eqs;
   727 in
   728   val take_rews = map Drule.export_without_context
   729     (take_stricts @ take_0s @ take_apps);
   730 end; (* local *)
   731 
   732 local
   733   fun one_con p (con, _, args) =
   734     let
   735       val P_names = map P_name (1 upto (length dnames));
   736       val vns = Name.variant_list P_names (map vname args);
   737       val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
   738       fun ind_hyp arg = %:(P_name (1 + rec_of arg)) $ bound_arg args arg;
   739       val t1 = mk_trp (%:p $ con_app2 con (bound_arg args) args);
   740       val t2 = lift ind_hyp (filter is_rec args, t1);
   741       val t3 = lift_defined (bound_arg vns) (nonlazy_vns, t2);
   742     in Library.foldr mk_All (vns, t3) end;
   743 
   744   fun one_eq ((p, cons), concl) =
   745     mk_trp (%:p $ UU) ===> Logic.list_implies (map (one_con p) cons, concl);
   746 
   747   fun ind_term concf = Library.foldr one_eq
   748     (mapn (fn n => fn x => (P_name n, x)) 1 conss,
   749      mk_trp (foldr1 mk_conj (mapn concf 1 dnames)));
   750   val take_ss = HOL_ss addsimps take_rews;
   751   fun quant_tac ctxt i = EVERY
   752     (mapn (fn n => fn _ => res_inst_tac ctxt [(("x", 0), x_name n)] spec i) 1 dnames);
   753 
   754   fun ind_prems_tac prems = EVERY
   755     (maps (fn cons =>
   756       (resolve_tac prems 1 ::
   757         maps (fn (_,_,args) => 
   758           resolve_tac prems 1 ::
   759           map (K(atac 1)) (nonlazy args) @
   760           map (K(atac 1)) (filter is_rec args))
   761         cons))
   762       conss);
   763   local 
   764     (* check whether every/exists constructor of the n-th part of the equation:
   765        it has a possibly indirectly recursive argument that isn't/is possibly 
   766        indirectly lazy *)
   767     fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
   768           is_rec arg andalso not(rec_of arg mem ns) andalso
   769           ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
   770             rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
   771               (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
   772           ) o third) cons;
   773     fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
   774     fun warn (n,cons) =
   775       if all_rec_to [] false (n,cons)
   776       then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
   777       else false;
   778     fun lazy_rec_to ns = rec_to exists I  lazy_rec_to ns;
   779 
   780   in
   781     val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   782     val is_emptys = map warn n__eqs;
   783     val is_finite = forall (not o lazy_rec_to [] false) n__eqs;
   784   end;
   785 in (* local *)
   786   val _ = trace " Proving finite_ind...";
   787   val finite_ind =
   788     let
   789       fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
   790       val goal = ind_term concf;
   791 
   792       fun tacf {prems, context} =
   793         let
   794           val tacs1 = [
   795             quant_tac context 1,
   796             simp_tac HOL_ss 1,
   797             InductTacs.induct_tac context [[SOME "n"]] 1,
   798             simp_tac (take_ss addsimps prems) 1,
   799             TRY (safe_tac HOL_cs)];
   800           fun arg_tac arg =
   801                         (* FIXME! case_UU_tac *)
   802             case_UU_tac context (prems @ con_rews) 1
   803               (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
   804           fun con_tacs (con, _, args) = 
   805             asm_simp_tac take_ss 1 ::
   806             map arg_tac (filter is_nonlazy_rec args) @
   807             [resolve_tac prems 1] @
   808             map (K (atac 1)) (nonlazy args) @
   809             map (K (etac spec 1)) (filter is_rec args);
   810           fun cases_tacs (cons, cases) =
   811             res_inst_tac context [(("x", 0), "x")] cases 1 ::
   812             asm_simp_tac (take_ss addsimps prems) 1 ::
   813             maps con_tacs cons;
   814         in
   815           tacs1 @ maps cases_tacs (conss ~~ cases)
   816         end;
   817     in pg'' thy [] goal tacf
   818        handle ERROR _ => (warning "Proof of finite_ind failed."; TrueI)
   819     end;
   820 
   821   val _ = trace " Proving take_lemmas...";
   822   val take_lemmas =
   823     let
   824       fun take_lemma n (dn, ax_reach) =
   825         let
   826           val lhs = dc_take dn $ Bound 0 `%(x_name n);
   827           val rhs = dc_take dn $ Bound 0 `%(x_name n^"'");
   828           val concl = mk_trp (%:(x_name n) === %:(x_name n^"'"));
   829           val goal = mk_All ("n", mk_trp (lhs === rhs)) ===> concl;
   830           val rules = [contlub_fst RS contlubE RS ssubst,
   831                        contlub_snd RS contlubE RS ssubst];
   832           fun tacf {prems, context} = [
   833             res_inst_tac context [(("t", 0), x_name n    )] (ax_reach RS subst) 1,
   834             res_inst_tac context [(("t", 0), x_name n^"'")] (ax_reach RS subst) 1,
   835             stac fix_def2 1,
   836             REPEAT (CHANGED
   837               (resolve_tac rules 1 THEN chain_tac 1)),
   838             stac contlub_cfun_fun 1,
   839             stac contlub_cfun_fun 2,
   840             rtac lub_equal 3,
   841             chain_tac 1,
   842             rtac allI 1,
   843             resolve_tac prems 1];
   844         in pg'' thy axs_take_def goal tacf end;
   845     in mapn take_lemma 1 (dnames ~~ axs_reach) end;
   846 
   847 (* ----- theorems concerning finiteness and induction ----------------------- *)
   848 
   849   val _ = trace " Proving finites, ind...";
   850   val (finites, ind) =
   851   (
   852     if is_finite
   853     then (* finite case *)
   854       let 
   855         fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %:"x" === %:"x");
   856         fun dname_lemma dn =
   857           let
   858             val prem1 = mk_trp (defined (%:"x"));
   859             val disj1 = mk_all ("n", dc_take dn $ Bound 0 ` %:"x" === UU);
   860             val prem2 = mk_trp (mk_disj (disj1, take_enough dn));
   861             val concl = mk_trp (take_enough dn);
   862             val goal = prem1 ===> prem2 ===> concl;
   863             val tacs = [
   864               etac disjE 1,
   865               etac notE 1,
   866               resolve_tac take_lemmas 1,
   867               asm_simp_tac take_ss 1,
   868               atac 1];
   869           in pg [] goal (K tacs) end;
   870         val _ = trace " Proving finite_lemmas1a";
   871         val finite_lemmas1a = map dname_lemma dnames;
   872  
   873         val _ = trace " Proving finite_lemma1b";
   874         val finite_lemma1b =
   875           let
   876             fun mk_eqn n ((dn, args), _) =
   877               let
   878                 val disj1 = dc_take dn $ Bound 1 ` Bound 0 === UU;
   879                 val disj2 = dc_take dn $ Bound 1 ` Bound 0 === Bound 0;
   880               in
   881                 mk_constrainall
   882                   (x_name n, Type (dn,args), mk_disj (disj1, disj2))
   883               end;
   884             val goal =
   885               mk_trp (mk_all ("n", foldr1 mk_conj (mapn mk_eqn 1 eqs)));
   886             fun arg_tacs ctxt vn = [
   887               eres_inst_tac ctxt [(("x", 0), vn)] all_dupE 1,
   888               etac disjE 1,
   889               asm_simp_tac (HOL_ss addsimps con_rews) 1,
   890               asm_simp_tac take_ss 1];
   891             fun con_tacs ctxt (con, _, args) =
   892               asm_simp_tac take_ss 1 ::
   893               maps (arg_tacs ctxt) (nonlazy_rec args);
   894             fun foo_tacs ctxt n (cons, cases) =
   895               simp_tac take_ss 1 ::
   896               rtac allI 1 ::
   897               res_inst_tac ctxt [(("x", 0), x_name n)] cases 1 ::
   898               asm_simp_tac take_ss 1 ::
   899               maps (con_tacs ctxt) cons;
   900             fun tacs ctxt =
   901               rtac allI 1 ::
   902               InductTacs.induct_tac ctxt [[SOME "n"]] 1 ::
   903               simp_tac take_ss 1 ::
   904               TRY (safe_tac (empty_cs addSEs [conjE] addSIs [conjI])) ::
   905               flat (mapn (foo_tacs ctxt) 1 (conss ~~ cases));
   906           in pg [] goal tacs end;
   907 
   908         fun one_finite (dn, l1b) =
   909           let
   910             val goal = mk_trp (%%:(dn^"_finite") $ %:"x");
   911             fun tacs ctxt = [
   912                         (* FIXME! case_UU_tac *)
   913               case_UU_tac ctxt take_rews 1 "x",
   914               eresolve_tac finite_lemmas1a 1,
   915               step_tac HOL_cs 1,
   916               step_tac HOL_cs 1,
   917               cut_facts_tac [l1b] 1,
   918               fast_tac HOL_cs 1];
   919           in pg axs_finite_def goal tacs end;
   920 
   921         val _ = trace " Proving finites";
   922         val finites = map one_finite (dnames ~~ atomize global_ctxt finite_lemma1b);
   923         val _ = trace " Proving ind";
   924         val ind =
   925           let
   926             fun concf n dn = %:(P_name n) $ %:(x_name n);
   927             fun tacf {prems, context} =
   928               let
   929                 fun finite_tacs (finite, fin_ind) = [
   930                   rtac(rewrite_rule axs_finite_def finite RS exE)1,
   931                   etac subst 1,
   932                   rtac fin_ind 1,
   933                   ind_prems_tac prems];
   934               in
   935                 TRY (safe_tac HOL_cs) ::
   936                 maps finite_tacs (finites ~~ atomize global_ctxt finite_ind)
   937               end;
   938           in pg'' thy [] (ind_term concf) tacf end;
   939       in (finites, ind) end (* let *)
   940 
   941     else (* infinite case *)
   942       let
   943         fun one_finite n dn =
   944           read_instantiate global_ctxt [(("P", 0), dn ^ "_finite " ^ x_name n)] excluded_middle;
   945         val finites = mapn one_finite 1 dnames;
   946 
   947         val goal =
   948           let
   949             fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
   950             fun concf n dn = %:(P_name n) $ %:(x_name n);
   951           in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
   952         val cont_rules =
   953             [cont_id, cont_const, cont2cont_Rep_CFun,
   954              cont2cont_fst, cont2cont_snd];
   955         fun tacf {prems, context} =
   956           map (fn ax_reach => rtac (ax_reach RS subst) 1) axs_reach @ [
   957           quant_tac context 1,
   958           rtac (adm_impl_admw RS wfix_ind) 1,
   959           REPEAT_DETERM (rtac adm_all 1),
   960           REPEAT_DETERM (
   961             TRY (rtac adm_conj 1) THEN 
   962             rtac adm_subst 1 THEN 
   963             REPEAT (resolve_tac cont_rules 1) THEN
   964             resolve_tac prems 1),
   965           strip_tac 1,
   966           rtac (rewrite_rule axs_take_def finite_ind) 1,
   967           ind_prems_tac prems];
   968         val ind = (pg'' thy [] goal tacf
   969           handle ERROR _ =>
   970             (warning "Cannot prove infinite induction rule"; TrueI));
   971       in (finites, ind) end
   972   )
   973       handle THM _ =>
   974              (warning "Induction proofs failed (THM raised)."; ([], TrueI))
   975            | ERROR _ =>
   976              (warning "Cannot prove induction rule"; ([], TrueI));
   977 
   978 
   979 end; (* local *)
   980 
   981 (* ----- theorem concerning coinduction ------------------------------------- *)
   982 
   983 (* COINDUCTION TEMPORARILY DISABLED
   984 local
   985   val xs = mapn (fn n => K (x_name n)) 1 dnames;
   986   fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
   987   val take_ss = HOL_ss addsimps take_rews;
   988   val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
   989   val _ = trace " Proving coind_lemma...";
   990   val coind_lemma =
   991     let
   992       fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
   993       fun mk_eqn n dn =
   994         (dc_take dn $ %:"n" ` bnd_arg n 0) ===
   995         (dc_take dn $ %:"n" ` bnd_arg n 1);
   996       fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
   997       val goal =
   998         mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
   999           Library.foldr mk_all2 (xs,
  1000             Library.foldr mk_imp (mapn mk_prj 0 dnames,
  1001               foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
  1002       fun x_tacs ctxt n x = [
  1003         rotate_tac (n+1) 1,
  1004         etac all2E 1,
  1005         eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
  1006         TRY (safe_tac HOL_cs),
  1007         REPEAT (CHANGED (asm_simp_tac take_ss 1))];
  1008       fun tacs ctxt = [
  1009         rtac impI 1,
  1010         InductTacs.induct_tac ctxt [[SOME "n"]] 1,
  1011         simp_tac take_ss 1,
  1012         safe_tac HOL_cs] @
  1013         flat (mapn (x_tacs ctxt) 0 xs);
  1014     in pg [ax_bisim_def] goal tacs end;
  1015 in
  1016   val _ = trace " Proving coind...";
  1017   val coind = 
  1018     let
  1019       fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
  1020       fun mk_eqn x = %:x === %:(x^"'");
  1021       val goal =
  1022         mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
  1023           Logic.list_implies (mapn mk_prj 0 xs,
  1024             mk_trp (foldr1 mk_conj (map mk_eqn xs)));
  1025       val tacs =
  1026         TRY (safe_tac HOL_cs) ::
  1027         maps (fn take_lemma => [
  1028           rtac take_lemma 1,
  1029           cut_facts_tac [coind_lemma] 1,
  1030           fast_tac HOL_cs 1])
  1031         take_lemmas;
  1032     in pg [] goal (K tacs) end;
  1033 end; (* local *)
  1034 COINDUCTION TEMPORARILY DISABLED *)
  1035 
  1036 val inducts = Project_Rule.projections (ProofContext.init thy) ind;
  1037 fun ind_rule (dname, rule) = ((Binding.empty, [rule]), [Induct.induct_type dname]);
  1038 val induct_failed = (Thm.prop_of ind = Thm.prop_of TrueI);
  1039 
  1040 in thy |> Sign.add_path comp_dnam
  1041        |> snd o PureThy.add_thmss [
  1042            ((Binding.name "take_rews"  , take_rews   ), [Simplifier.simp_add]),
  1043            ((Binding.name "take_lemmas", take_lemmas ), []),
  1044            ((Binding.name "finites"    , finites     ), []),
  1045            ((Binding.name "finite_ind" , [finite_ind]), []),
  1046            ((Binding.name "ind"        , [ind]       ), [])(*,
  1047            ((Binding.name "coind"      , [coind]     ), [])*)]
  1048        |> (if induct_failed then I
  1049            else snd o PureThy.add_thmss (map ind_rule (dnames ~~ inducts)))
  1050        |> Sign.parent_path |> pair take_rews
  1051 end; (* let *)
  1052 end; (* struct *)