src/HOLCF/Tools/Domain/domain_theorems.ML
author huffman
Mon Mar 08 13:58:00 2010 -0800 (2010-03-08)
changeset 35661 ede27eb8e94b
parent 35660 8169419cd824
child 35662 44d7aafdddb9
permissions -rw-r--r--
don't generate rule foo.finites for non-flat domains; use take_induct rule to prove induction rule
     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     -> Domain_Take_Proofs.iso_info
    16     -> theory -> thm list * theory;
    17 
    18   val comp_theorems :
    19       bstring * Domain_Library.eq list ->
    20       Domain_Take_Proofs.take_induct_info ->
    21       theory -> thm list * theory
    22 
    23   val quiet_mode: bool Unsynchronized.ref;
    24   val trace_domain: bool Unsynchronized.ref;
    25 end;
    26 
    27 structure Domain_Theorems :> DOMAIN_THEOREMS =
    28 struct
    29 
    30 val quiet_mode = Unsynchronized.ref false;
    31 val trace_domain = Unsynchronized.ref false;
    32 
    33 fun message s = if !quiet_mode then () else writeln s;
    34 fun trace s = if !trace_domain then tracing s else ();
    35 
    36 open Domain_Library;
    37 infixr 0 ===>;
    38 infixr 0 ==>;
    39 infix 0 == ; 
    40 infix 1 ===;
    41 infix 1 ~= ;
    42 infix 1 <<;
    43 infix 1 ~<<;
    44 infix 9 `   ;
    45 infix 9 `% ;
    46 infix 9 `%%;
    47 infixr 9 oo;
    48 
    49 (* ----- general proof facilities ------------------------------------------- *)
    50 
    51 fun legacy_infer_term thy t =
    52   let val ctxt = ProofContext.set_mode ProofContext.mode_schematic (ProofContext.init thy)
    53   in singleton (Syntax.check_terms ctxt) (Sign.intern_term thy t) end;
    54 
    55 fun pg'' thy defs t tacs =
    56   let
    57     val t' = legacy_infer_term thy t;
    58     val asms = Logic.strip_imp_prems t';
    59     val prop = Logic.strip_imp_concl t';
    60     fun tac {prems, context} =
    61       rewrite_goals_tac defs THEN
    62       EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
    63   in Goal.prove_global thy [] asms prop tac end;
    64 
    65 fun pg' thy defs t tacsf =
    66   let
    67     fun tacs {prems, context} =
    68       if null prems then tacsf context
    69       else cut_facts_tac prems 1 :: tacsf context;
    70   in pg'' thy defs t tacs end;
    71 
    72 (* FIXME!!!!!!!!! *)
    73 (* We should NEVER re-parse variable names as strings! *)
    74 (* The names can conflict with existing constants or other syntax! *)
    75 fun case_UU_tac ctxt rews i v =
    76   InductTacs.case_tac ctxt (v^"=UU") i THEN
    77   asm_simp_tac (HOLCF_ss addsimps rews) i;
    78 
    79 (* ----- general proofs ----------------------------------------------------- *)
    80 
    81 val all2E = @{lemma "!x y . P x y ==> (P x y ==> R) ==> R" by simp}
    82 
    83 fun theorems
    84     (((dname, _), cons) : eq, eqs : eq list)
    85     (dom_eqn : typ * (binding * (bool * binding option * typ) list * mixfix) list)
    86     (iso_info : Domain_Take_Proofs.iso_info)
    87     (thy : theory) =
    88 let
    89 
    90 val _ = message ("Proving isomorphism properties of domain "^dname^" ...");
    91 val map_tab = Domain_Take_Proofs.get_map_tab thy;
    92 
    93 
    94 (* ----- getting the axioms and definitions --------------------------------- *)
    95 
    96 val ax_abs_iso = #abs_inverse iso_info;
    97 val ax_rep_iso = #rep_inverse iso_info;
    98 
    99 val abs_const = #abs_const iso_info;
   100 val rep_const = #rep_const iso_info;
   101 
   102 local
   103   fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
   104 in
   105   val ax_take_0      = ga "take_0" dname;
   106   val ax_take_Suc    = ga "take_Suc" dname;
   107   val ax_take_strict = ga "take_strict" dname;
   108 end; (* local *)
   109 
   110 (* ----- define constructors ------------------------------------------------ *)
   111 
   112 val (result, thy) =
   113   Domain_Constructors.add_domain_constructors
   114     (Long_Name.base_name dname) (snd dom_eqn) iso_info thy;
   115 
   116 val con_appls = #con_betas result;
   117 val {exhaust, casedist, ...} = result;
   118 val {con_compacts, con_rews, inverts, injects, dist_les, dist_eqs, ...} = result;
   119 val {sel_rews, ...} = result;
   120 val when_rews = #cases result;
   121 val when_strict = hd when_rews;
   122 val dis_rews = #dis_rews result;
   123 val mat_rews = #match_rews result;
   124 val pat_rews = #pat_rews result;
   125 
   126 (* ----- theorems concerning the isomorphism -------------------------------- *)
   127 
   128 val pg = pg' thy;
   129 
   130 val retraction_strict = @{thm retraction_strict};
   131 val abs_strict = ax_rep_iso RS (allI RS retraction_strict);
   132 val rep_strict = ax_abs_iso RS (allI RS retraction_strict);
   133 val iso_rews = map Drule.export_without_context [ax_abs_iso, ax_rep_iso, abs_strict, rep_strict];
   134 
   135 (* ----- theorems concerning one induction step ----------------------------- *)
   136 
   137 local
   138   fun dc_take dn = %%:(dn^"_take");
   139   val dnames = map (fst o fst) eqs;
   140   val deflation_thms = Domain_Take_Proofs.get_deflation_thms thy;
   141   fun get_deflation_take dn = PureThy.get_thm thy (dn ^ ".deflation_take");
   142   val axs_deflation_take = map get_deflation_take dnames;
   143 
   144   fun copy_of_dtyp tab r dt =
   145       if Datatype_Aux.is_rec_type dt then copy tab r dt else ID
   146   and copy tab r (Datatype_Aux.DtRec i) = r i
   147     | copy tab r (Datatype_Aux.DtTFree a) = ID
   148     | copy tab r (Datatype_Aux.DtType (c, ds)) =
   149       case Symtab.lookup tab c of
   150         SOME f => list_ccomb (%%:f, map (copy_of_dtyp tab r) ds)
   151       | NONE => (warning ("copy_of_dtyp: unknown type constructor " ^ c); ID);
   152 
   153   fun one_take_app (con, args) =
   154     let
   155       fun mk_take n = dc_take (List.nth (dnames, n)) $ %:"n";
   156       fun one_rhs arg =
   157           if Datatype_Aux.is_rec_type (dtyp_of arg)
   158           then copy_of_dtyp map_tab
   159                  mk_take (dtyp_of arg) ` (%# arg)
   160           else (%# arg);
   161       val lhs = (dc_take dname $ (%%:"Suc" $ %:"n"))`(con_app con args);
   162       val rhs = con_app2 con one_rhs args;
   163       val goal = mk_trp (lhs === rhs);
   164       val rules =
   165           [ax_take_Suc, ax_abs_iso, @{thm cfcomp2}]
   166           @ @{thms take_con_rules ID1 deflation_strict}
   167           @ deflation_thms @ axs_deflation_take;
   168       val tacs = [simp_tac (HOL_basic_ss addsimps rules) 1];
   169     in pg con_appls goal (K tacs) end;
   170   val take_apps = map one_take_app cons;
   171 in
   172   val take_rews = ax_take_0 :: ax_take_strict :: take_apps;
   173 end;
   174 
   175 val case_ns =
   176     "bottom" :: map (fn (b,_,_) => Binding.name_of b) (snd dom_eqn);
   177 
   178 in
   179   thy
   180     |> Sign.add_path (Long_Name.base_name dname)
   181     |> snd o PureThy.add_thmss [
   182         ((Binding.name "iso_rews"  , iso_rews    ), [Simplifier.simp_add]),
   183         ((Binding.name "exhaust"   , [exhaust]   ), []),
   184         ((Binding.name "casedist"  , [casedist]  ),
   185          [Rule_Cases.case_names case_ns, Induct.cases_type dname]),
   186         ((Binding.name "when_rews" , when_rews   ), [Simplifier.simp_add]),
   187         ((Binding.name "compacts"  , con_compacts), [Simplifier.simp_add]),
   188         ((Binding.name "con_rews"  , con_rews    ),
   189          [Simplifier.simp_add, Fixrec.fixrec_simp_add]),
   190         ((Binding.name "sel_rews"  , sel_rews    ), [Simplifier.simp_add]),
   191         ((Binding.name "dis_rews"  , dis_rews    ), [Simplifier.simp_add]),
   192         ((Binding.name "pat_rews"  , pat_rews    ), [Simplifier.simp_add]),
   193         ((Binding.name "dist_les"  , dist_les    ), [Simplifier.simp_add]),
   194         ((Binding.name "dist_eqs"  , dist_eqs    ), [Simplifier.simp_add]),
   195         ((Binding.name "inverts"   , inverts     ), [Simplifier.simp_add]),
   196         ((Binding.name "injects"   , injects     ), [Simplifier.simp_add]),
   197         ((Binding.name "take_rews" , take_rews   ), [Simplifier.simp_add]),
   198         ((Binding.name "match_rews", mat_rews    ),
   199          [Simplifier.simp_add, Fixrec.fixrec_simp_add])]
   200     |> Sign.parent_path
   201     |> pair (iso_rews @ when_rews @ con_rews @ sel_rews @ dis_rews @
   202         pat_rews @ dist_les @ dist_eqs)
   203 end; (* let *)
   204 
   205 (******************************************************************************)
   206 (****************************** induction rules *******************************)
   207 (******************************************************************************)
   208 
   209 fun prove_induction
   210     (comp_dnam, eqs : eq list)
   211     (take_rews : thm list)
   212     (take_info : Domain_Take_Proofs.take_induct_info)
   213     (thy : theory) =
   214 let
   215   val dnames = map (fst o fst) eqs;
   216   val conss  = map  snd        eqs;
   217   fun dc_take dn = %%:(dn^"_take");
   218   val x_name = idx_name dnames "x"; 
   219   val P_name = idx_name dnames "P";
   220   val pg = pg' thy;
   221 
   222   local
   223     fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
   224     fun gts s dn = PureThy.get_thms thy (dn ^ "." ^ s);
   225   in
   226     val axs_rep_iso = map (ga "rep_iso") dnames;
   227     val axs_abs_iso = map (ga "abs_iso") dnames;
   228     val cases = map (ga  "casedist" ) dnames;
   229     val con_rews  = maps (gts "con_rews" ) dnames;
   230   end;
   231 
   232   val {take_0_thms, take_Suc_thms, chain_take_thms, ...} = take_info;
   233   val {lub_take_thms, finite_defs, reach_thms, ...} = take_info;
   234   val {take_induct_thms, ...} = take_info;
   235 
   236   fun one_con p (con, args) =
   237     let
   238       val P_names = map P_name (1 upto (length dnames));
   239       val vns = Name.variant_list P_names (map vname args);
   240       val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
   241       fun ind_hyp arg = %:(P_name (1 + rec_of arg)) $ bound_arg args arg;
   242       val t1 = mk_trp (%:p $ con_app2 con (bound_arg args) args);
   243       val t2 = lift ind_hyp (filter is_rec args, t1);
   244       val t3 = lift_defined (bound_arg vns) (nonlazy_vns, t2);
   245     in Library.foldr mk_All (vns, t3) end;
   246 
   247   fun one_eq ((p, cons), concl) =
   248     mk_trp (%:p $ UU) ===> Logic.list_implies (map (one_con p) cons, concl);
   249 
   250   fun ind_term concf = Library.foldr one_eq
   251     (mapn (fn n => fn x => (P_name n, x)) 1 conss,
   252      mk_trp (foldr1 mk_conj (mapn concf 1 dnames)));
   253   val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
   254   fun quant_tac ctxt i = EVERY
   255     (mapn (fn n => fn _ => res_inst_tac ctxt [(("x", 0), x_name n)] spec i) 1 dnames);
   256 
   257   fun ind_prems_tac prems = EVERY
   258     (maps (fn cons =>
   259       (resolve_tac prems 1 ::
   260         maps (fn (_,args) => 
   261           resolve_tac prems 1 ::
   262           map (K(atac 1)) (nonlazy args) @
   263           map (K(atac 1)) (filter is_rec args))
   264         cons))
   265       conss);
   266   local 
   267     (* check whether every/exists constructor of the n-th part of the equation:
   268        it has a possibly indirectly recursive argument that isn't/is possibly 
   269        indirectly lazy *)
   270     fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
   271           is_rec arg andalso not(rec_of arg mem ns) andalso
   272           ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
   273             rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
   274               (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
   275           ) o snd) cons;
   276     fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
   277     fun warn (n,cons) =
   278       if all_rec_to [] false (n,cons)
   279       then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
   280       else false;
   281     fun lazy_rec_to ns = rec_to exists I  lazy_rec_to ns;
   282 
   283   in
   284     val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   285     val is_emptys = map warn n__eqs;
   286     val is_finite = #is_finite take_info;
   287     val _ = if is_finite
   288             then message ("Proving finiteness rule for domain "^comp_dnam^" ...")
   289             else ();
   290   end;
   291   val _ = trace " Proving finite_ind...";
   292   val finite_ind =
   293     let
   294       fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
   295       val goal = ind_term concf;
   296 
   297       fun tacf {prems, context} =
   298         let
   299           val tacs1 = [
   300             quant_tac context 1,
   301             simp_tac HOL_ss 1,
   302             InductTacs.induct_tac context [[SOME "n"]] 1,
   303             simp_tac (take_ss addsimps prems) 1,
   304             TRY (safe_tac HOL_cs)];
   305           fun arg_tac arg =
   306                         (* FIXME! case_UU_tac *)
   307             case_UU_tac context (prems @ con_rews) 1
   308               (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
   309           fun con_tacs (con, args) = 
   310             asm_simp_tac take_ss 1 ::
   311             map arg_tac (filter is_nonlazy_rec args) @
   312             [resolve_tac prems 1] @
   313             map (K (atac 1)) (nonlazy args) @
   314             map (K (etac spec 1)) (filter is_rec args);
   315           fun cases_tacs (cons, cases) =
   316             res_inst_tac context [(("y", 0), "x")] cases 1 ::
   317             asm_simp_tac (take_ss addsimps prems) 1 ::
   318             maps con_tacs cons;
   319         in
   320           tacs1 @ maps cases_tacs (conss ~~ cases)
   321         end;
   322     in pg'' thy [] goal tacf
   323        handle ERROR _ => (warning "Proof of finite_ind failed."; TrueI)
   324     end;
   325 
   326 (* ----- theorems concerning finiteness and induction ----------------------- *)
   327 
   328   val global_ctxt = ProofContext.init thy;
   329 
   330   val _ = trace " Proving ind...";
   331   val ind =
   332   (
   333     if is_finite
   334     then (* finite case *)
   335       let
   336         fun concf n dn = %:(P_name n) $ %:(x_name n);
   337         fun tacf {prems, context} =
   338           let
   339             fun finite_tacs (take_induct, fin_ind) = [
   340                 rtac take_induct 1,
   341                 rtac fin_ind 1,
   342                 ind_prems_tac prems];
   343           in
   344             TRY (safe_tac HOL_cs) ::
   345             maps finite_tacs (take_induct_thms ~~ atomize global_ctxt finite_ind)
   346           end;
   347       in pg'' thy [] (ind_term concf) tacf end
   348 
   349     else (* infinite case *)
   350       let
   351         val goal =
   352           let
   353             fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
   354             fun concf n dn = %:(P_name n) $ %:(x_name n);
   355           in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
   356         val cont_rules =
   357             @{thms cont_id cont_const cont2cont_Rep_CFun
   358                    cont2cont_fst cont2cont_snd};
   359         val subgoal =
   360           let fun p n dn = %:(P_name n) $ (dc_take dn $ Bound 0 `%(x_name n));
   361           in mk_trp (mk_all ("n", foldr1 mk_conj (mapn p 1 dnames))) end;
   362         val subgoal' = legacy_infer_term thy subgoal;
   363         fun tacf {prems, context} =
   364           let
   365             val subtac =
   366                 EVERY [rtac allI 1, rtac finite_ind 1, ind_prems_tac prems];
   367             val subthm = Goal.prove context [] [] subgoal' (K subtac);
   368           in
   369             map (fn ax_reach => rtac (ax_reach RS subst) 1) reach_thms @ [
   370             cut_facts_tac (subthm :: take (length dnames) prems) 1,
   371             REPEAT (rtac @{thm conjI} 1 ORELSE
   372                     EVERY [etac @{thm admD [OF _ ch2ch_Rep_CFunL]} 1,
   373                            resolve_tac chain_take_thms 1,
   374                            asm_simp_tac HOL_basic_ss 1])
   375             ]
   376           end;
   377         val ind = (pg'' thy [] goal tacf
   378           handle ERROR _ =>
   379             (warning "Cannot prove infinite induction rule"; TrueI)
   380                   );
   381       in ind end
   382   )
   383       handle THM _ =>
   384              (warning "Induction proofs failed (THM raised)."; TrueI)
   385            | ERROR _ =>
   386              (warning "Cannot prove induction rule"; TrueI);
   387 
   388 val case_ns =
   389   let
   390     val bottoms =
   391         if length dnames = 1 then ["bottom"] else
   392         map (fn s => "bottom_" ^ Long_Name.base_name s) dnames;
   393     fun one_eq bot (_,cons) =
   394           bot :: map (fn (c,_) => Long_Name.base_name c) cons;
   395   in flat (map2 one_eq bottoms eqs) end;
   396 
   397 val inducts = Project_Rule.projections (ProofContext.init thy) ind;
   398 fun ind_rule (dname, rule) =
   399     ((Binding.empty, [rule]),
   400      [Rule_Cases.case_names case_ns, Induct.induct_type dname]);
   401 
   402 val induct_failed = (Thm.prop_of ind = Thm.prop_of TrueI);
   403 
   404 in thy |> Sign.add_path comp_dnam
   405        |> snd o PureThy.add_thmss [
   406            ((Binding.name "finite_ind" , [finite_ind]), []),
   407            ((Binding.name "ind"        , [ind]       ), [])]
   408        |> (if induct_failed then I
   409            else snd o PureThy.add_thmss (map ind_rule (dnames ~~ inducts)))
   410        |> Sign.parent_path
   411 end; (* prove_induction *)
   412 
   413 (******************************************************************************)
   414 (************************ bisimulation and coinduction ************************)
   415 (******************************************************************************)
   416 
   417 fun prove_coinduction
   418     (comp_dnam, eqs : eq list)
   419     (take_lemmas : thm list)
   420     (thy : theory) : theory =
   421 let
   422 
   423 val dnames = map (fst o fst) eqs;
   424 val comp_dname = Sign.full_bname thy comp_dnam;
   425 fun dc_take dn = %%:(dn^"_take");
   426 val x_name = idx_name dnames "x"; 
   427 val n_eqs = length eqs;
   428 
   429 val take_rews =
   430     maps (fn dn => PureThy.get_thms thy (dn ^ ".take_rews")) dnames;
   431 
   432 (* ----- define bisimulation predicate -------------------------------------- *)
   433 
   434 local
   435   open HOLCF_Library
   436   val dtypes  = map (Type o fst) eqs;
   437   val relprod = mk_tupleT (map (fn tp => tp --> tp --> boolT) dtypes);
   438   val bisim_bind = Binding.name (comp_dnam ^ "_bisim");
   439   val bisim_type = relprod --> boolT;
   440 in
   441   val (bisim_const, thy) =
   442       Sign.declare_const ((bisim_bind, bisim_type), NoSyn) thy;
   443 end;
   444 
   445 local
   446 
   447   fun legacy_infer_term thy t =
   448       singleton (Syntax.check_terms (ProofContext.init thy)) (Sign.intern_term thy t);
   449   fun legacy_infer_prop thy t = legacy_infer_term thy (TypeInfer.constrain propT t);
   450   fun infer_props thy = map (apsnd (legacy_infer_prop thy));
   451   fun add_defs_i x = PureThy.add_defs false (map Thm.no_attributes x);
   452   fun add_defs_infer defs thy = add_defs_i (infer_props thy defs) thy;
   453 
   454   val comp_dname = Sign.full_bname thy comp_dnam;
   455   val dnames = map (fst o fst) eqs;
   456   val x_name = idx_name dnames "x"; 
   457 
   458   fun one_con (con, args) =
   459     let
   460       val nonrec_args = filter_out is_rec args;
   461       val    rec_args = filter is_rec args;
   462       val    recs_cnt = length rec_args;
   463       val allargs     = nonrec_args @ rec_args
   464                         @ map (upd_vname (fn s=> s^"'")) rec_args;
   465       val allvns      = map vname allargs;
   466       fun vname_arg s arg = if is_rec arg then vname arg^s else vname arg;
   467       val vns1        = map (vname_arg "" ) args;
   468       val vns2        = map (vname_arg "'") args;
   469       val allargs_cnt = length nonrec_args + 2*recs_cnt;
   470       val rec_idxs    = (recs_cnt-1) downto 0;
   471       val nonlazy_idxs = map snd (filter_out (fn (arg,_) => is_lazy arg)
   472                                              (allargs~~((allargs_cnt-1) downto 0)));
   473       fun rel_app i ra = proj (Bound(allargs_cnt+2)) eqs (rec_of ra) $ 
   474                               Bound (2*recs_cnt-i) $ Bound (recs_cnt-i);
   475       val capps =
   476           List.foldr
   477             mk_conj
   478             (mk_conj(
   479              Bound(allargs_cnt+1)===list_ccomb(%%:con,map (bound_arg allvns) vns1),
   480              Bound(allargs_cnt+0)===list_ccomb(%%:con,map (bound_arg allvns) vns2)))
   481             (mapn rel_app 1 rec_args);
   482     in
   483       List.foldr
   484         mk_ex
   485         (Library.foldr mk_conj
   486                        (map (defined o Bound) nonlazy_idxs,capps)) allvns
   487     end;
   488   fun one_comp n (_,cons) =
   489       mk_all (x_name(n+1),
   490       mk_all (x_name(n+1)^"'",
   491       mk_imp (proj (Bound 2) eqs n $ Bound 1 $ Bound 0,
   492       foldr1 mk_disj (mk_conj(Bound 1 === UU,Bound 0 === UU)
   493                       ::map one_con cons))));
   494   val bisim_eqn =
   495       %%:(comp_dname^"_bisim") ==
   496          mk_lam("R", foldr1 mk_conj (mapn one_comp 0 eqs));
   497 
   498 in
   499   val ([ax_bisim_def], thy) =
   500       thy
   501         |> Sign.add_path comp_dnam
   502         |> add_defs_infer [(Binding.name "bisim_def", bisim_eqn)]
   503         ||> Sign.parent_path;
   504 end; (* local *)
   505 
   506 (* ----- theorem concerning coinduction ------------------------------------- *)
   507 
   508 local
   509   val pg = pg' thy;
   510   val xs = mapn (fn n => K (x_name n)) 1 dnames;
   511   fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
   512   val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
   513   val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
   514   val _ = trace " Proving coind_lemma...";
   515   val coind_lemma =
   516     let
   517       fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
   518       fun mk_eqn n dn =
   519         (dc_take dn $ %:"n" ` bnd_arg n 0) ===
   520         (dc_take dn $ %:"n" ` bnd_arg n 1);
   521       fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
   522       val goal =
   523         mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
   524           Library.foldr mk_all2 (xs,
   525             Library.foldr mk_imp (mapn mk_prj 0 dnames,
   526               foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
   527       fun x_tacs ctxt n x = [
   528         rotate_tac (n+1) 1,
   529         etac all2E 1,
   530         eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
   531         TRY (safe_tac HOL_cs),
   532         REPEAT (CHANGED (asm_simp_tac take_ss 1))];
   533       fun tacs ctxt = [
   534         rtac impI 1,
   535         InductTacs.induct_tac ctxt [[SOME "n"]] 1,
   536         simp_tac take_ss 1,
   537         safe_tac HOL_cs] @
   538         flat (mapn (x_tacs ctxt) 0 xs);
   539     in pg [ax_bisim_def] goal tacs end;
   540 in
   541   val _ = trace " Proving coind...";
   542   val coind = 
   543     let
   544       fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
   545       fun mk_eqn x = %:x === %:(x^"'");
   546       val goal =
   547         mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
   548           Logic.list_implies (mapn mk_prj 0 xs,
   549             mk_trp (foldr1 mk_conj (map mk_eqn xs)));
   550       val tacs =
   551         TRY (safe_tac HOL_cs) ::
   552         maps (fn take_lemma => [
   553           rtac take_lemma 1,
   554           cut_facts_tac [coind_lemma] 1,
   555           fast_tac HOL_cs 1])
   556         take_lemmas;
   557     in pg [] goal (K tacs) end;
   558 end; (* local *)
   559 
   560 in thy |> Sign.add_path comp_dnam
   561        |> snd o PureThy.add_thmss [((Binding.name "coind", [coind]), [])]
   562        |> Sign.parent_path
   563 end; (* let *)
   564 
   565 fun comp_theorems
   566     (comp_dnam : string, eqs : eq list)
   567     (take_info : Domain_Take_Proofs.take_induct_info)
   568     (thy : theory) =
   569 let
   570 val map_tab = Domain_Take_Proofs.get_map_tab thy;
   571 
   572 val dnames = map (fst o fst) eqs;
   573 val comp_dname = Sign.full_bname thy comp_dnam;
   574 
   575 (* ----- getting the composite axiom and definitions ------------------------ *)
   576 
   577 (* Test for indirect recursion *)
   578 local
   579   fun indirect_arg arg =
   580       rec_of arg = ~1 andalso Datatype_Aux.is_rec_type (dtyp_of arg);
   581   fun indirect_con (_, args) = exists indirect_arg args;
   582   fun indirect_eq (_, cons) = exists indirect_con cons;
   583 in
   584   val is_indirect = exists indirect_eq eqs;
   585   val _ =
   586       if is_indirect
   587       then message "Indirect recursion detected, skipping proofs of (co)induction rules"
   588       else message ("Proving induction properties of domain "^comp_dname^" ...");
   589 end;
   590 
   591 (* theorems about take *)
   592 
   593 val take_lemmas = #take_lemma_thms take_info;
   594 
   595 val take_rews =
   596     maps (fn dn => PureThy.get_thms thy (dn ^ ".take_rews")) dnames;
   597 
   598 (* prove induction rules, unless definition is indirect recursive *)
   599 val thy =
   600     if is_indirect then thy else
   601     prove_induction (comp_dnam, eqs) take_rews take_info thy;
   602 
   603 val thy =
   604     if is_indirect then thy else
   605     prove_coinduction (comp_dnam, eqs) take_lemmas thy;
   606 
   607 in
   608   (take_rews, thy)
   609 end; (* let *)
   610 end; (* struct *)