src/HOLCF/Tools/Domain/domain_theorems.ML
author huffman
Thu Oct 14 13:46:27 2010 -0700 (2010-10-14)
changeset 40017 575d3aa1f3c5
parent 40016 2eff1cbc1ccb
child 40018 bf85fef3cce4
permissions -rw-r--r--
include iso_info as part of constr_info type
     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 comp_theorems :
    13       binding * Domain_Library.eq list ->
    14       (binding * (binding * (bool * binding option * typ) list * mixfix) list) list ->
    15       Domain_Take_Proofs.take_induct_info ->
    16       Domain_Constructors.constr_info list ->
    17       theory -> thm list * theory
    18 
    19   val quiet_mode: bool Unsynchronized.ref;
    20   val trace_domain: bool Unsynchronized.ref;
    21 end;
    22 
    23 structure Domain_Theorems :> DOMAIN_THEOREMS =
    24 struct
    25 
    26 val quiet_mode = Unsynchronized.ref false;
    27 val trace_domain = Unsynchronized.ref false;
    28 
    29 fun message s = if !quiet_mode then () else writeln s;
    30 fun trace s = if !trace_domain then tracing s else ();
    31 
    32 open Domain_Library;
    33 infixr 0 ===>;
    34 infixr 0 ==>;
    35 infix 0 == ; 
    36 infix 1 ===;
    37 infix 1 ~= ;
    38 infix 1 <<;
    39 infix 1 ~<<;
    40 infix 9 `   ;
    41 infix 9 `% ;
    42 infix 9 `%%;
    43 infixr 9 oo;
    44 
    45 (* ----- general proof facilities ------------------------------------------- *)
    46 
    47 local
    48 
    49 fun map_typ f g (Type (c, Ts)) = Type (g c, map (map_typ f g) Ts)
    50   | map_typ f _ (TFree (x, S)) = TFree (x, map f S)
    51   | map_typ f _ (TVar (xi, S)) = TVar (xi, map f S);
    52 
    53 fun map_term f g h (Const (c, T)) = Const (h c, map_typ f g T)
    54   | map_term f g _ (Free (x, T)) = Free (x, map_typ f g T)
    55   | map_term f g _ (Var (xi, T)) = Var (xi, map_typ f g T)
    56   | map_term _ _ _ (t as Bound _) = t
    57   | map_term f g h (Abs (x, T, t)) = Abs (x, map_typ f g T, map_term f g h t)
    58   | map_term f g h (t $ u) = map_term f g h t $ map_term f g h u;
    59 
    60 in
    61 
    62 fun intern_term thy =
    63   map_term (Sign.intern_class thy) (Sign.intern_type thy) (Sign.intern_const thy);
    64 
    65 end;
    66 
    67 fun legacy_infer_term thy t =
    68   let val ctxt = ProofContext.set_mode ProofContext.mode_schematic (ProofContext.init_global thy)
    69   in singleton (Syntax.check_terms ctxt) (intern_term thy t) end;
    70 
    71 fun pg'' thy defs t tacs =
    72   let
    73     val t' = legacy_infer_term thy t;
    74     val asms = Logic.strip_imp_prems t';
    75     val prop = Logic.strip_imp_concl t';
    76     fun tac {prems, context} =
    77       rewrite_goals_tac defs THEN
    78       EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
    79   in Goal.prove_global thy [] asms prop tac end;
    80 
    81 fun pg' thy defs t tacsf =
    82   let
    83     fun tacs {prems, context} =
    84       if null prems then tacsf context
    85       else cut_facts_tac prems 1 :: tacsf context;
    86   in pg'' thy defs t tacs end;
    87 
    88 (* FIXME!!!!!!!!! *)
    89 (* We should NEVER re-parse variable names as strings! *)
    90 (* The names can conflict with existing constants or other syntax! *)
    91 fun case_UU_tac ctxt rews i v =
    92   InductTacs.case_tac ctxt (v^"=UU") i THEN
    93   asm_simp_tac (HOLCF_ss addsimps rews) i;
    94 
    95 (******************************************************************************)
    96 (***************************** proofs about take ******************************)
    97 (******************************************************************************)
    98 
    99 fun take_theorems
   100     (specs : (binding * (binding * (bool * binding option * typ) list * mixfix) list) list)
   101     (take_info : Domain_Take_Proofs.take_induct_info)
   102     (constr_infos : Domain_Constructors.constr_info list)
   103     (thy : theory) : thm list list * theory =
   104 let
   105   open HOLCF_Library;
   106 
   107   val {take_consts, take_Suc_thms, deflation_take_thms, ...} = take_info;
   108   val deflation_thms = Domain_Take_Proofs.get_deflation_thms thy;
   109 
   110   val n = Free ("n", @{typ nat});
   111   val n' = @{const Suc} $ n;
   112 
   113   local
   114     val newTs = map (#absT o #iso_info) constr_infos;
   115     val subs = newTs ~~ map (fn t => t $ n) take_consts;
   116     fun is_ID (Const (c, _)) = (c = @{const_name ID})
   117       | is_ID _              = false;
   118   in
   119     fun map_of_arg v T =
   120       let val m = Domain_Take_Proofs.map_of_typ thy subs T;
   121       in if is_ID m then v else mk_capply (m, v) end;
   122   end
   123 
   124   fun prove_take_apps
   125       (((dbind, spec), take_const), constr_info) thy =
   126     let
   127       val {iso_info, con_consts, con_betas, ...} = constr_info;
   128       val {abs_inverse, ...} = iso_info;
   129       fun prove_take_app (con_const : term) (bind, args, mx) =
   130         let
   131           val Ts = map (fn (_, _, T) => T) args;
   132           val ns = Name.variant_list ["n"] (Datatype_Prop.make_tnames Ts);
   133           val vs = map Free (ns ~~ Ts);
   134           val lhs = mk_capply (take_const $ n', list_ccomb (con_const, vs));
   135           val rhs = list_ccomb (con_const, map2 map_of_arg vs Ts);
   136           val goal = mk_trp (mk_eq (lhs, rhs));
   137           val rules =
   138               [abs_inverse] @ con_betas @ @{thms take_con_rules}
   139               @ take_Suc_thms @ deflation_thms @ deflation_take_thms;
   140           val tac = simp_tac (HOL_basic_ss addsimps rules) 1;
   141         in
   142           Goal.prove_global thy [] [] goal (K tac)
   143         end;
   144       val take_apps = map2 prove_take_app con_consts spec;
   145     in
   146       yield_singleton Global_Theory.add_thmss
   147         ((Binding.qualified true "take_rews" dbind, take_apps),
   148         [Simplifier.simp_add]) thy
   149     end;
   150 in
   151   fold_map prove_take_apps
   152     (specs ~~ take_consts ~~ constr_infos) thy
   153 end;
   154 
   155 (* ----- general proofs ----------------------------------------------------- *)
   156 
   157 val all2E = @{lemma "!x y . P x y ==> (P x y ==> R) ==> R" by simp}
   158 
   159 (******************************************************************************)
   160 (****************************** induction rules *******************************)
   161 (******************************************************************************)
   162 
   163 fun prove_induction
   164     (comp_dbind : binding, eqs : eq list)
   165     (take_rews : thm list)
   166     (take_info : Domain_Take_Proofs.take_induct_info)
   167     (thy : theory) =
   168 let
   169   val comp_dname = Sign.full_name thy comp_dbind;
   170   val dnames = map (fst o fst) eqs;
   171   val conss  = map  snd        eqs;
   172   fun dc_take dn = %%:(dn^"_take");
   173   val x_name = idx_name dnames "x";
   174   val P_name = idx_name dnames "P";
   175   val pg = pg' thy;
   176 
   177   local
   178     fun ga s dn = Global_Theory.get_thm thy (dn ^ "." ^ s);
   179     fun gts s dn = Global_Theory.get_thms thy (dn ^ "." ^ s);
   180   in
   181     val axs_rep_iso = map (ga "rep_iso") dnames;
   182     val axs_abs_iso = map (ga "abs_iso") dnames;
   183     val exhausts = map (ga  "exhaust" ) dnames;
   184     val con_rews  = maps (gts "con_rews" ) dnames;
   185   end;
   186 
   187   val {take_consts, ...} = take_info;
   188   val {take_0_thms, take_Suc_thms, chain_take_thms, ...} = take_info;
   189   val {lub_take_thms, finite_defs, reach_thms, ...} = take_info;
   190   val {take_induct_thms, ...} = take_info;
   191 
   192   fun one_con p (con, args) =
   193     let
   194       val P_names = map P_name (1 upto (length dnames));
   195       val vns = Name.variant_list P_names (map vname args);
   196       val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
   197       fun ind_hyp arg = %:(P_name (1 + rec_of arg)) $ bound_arg args arg;
   198       val t1 = mk_trp (%:p $ con_app2 con (bound_arg args) args);
   199       val t2 = lift ind_hyp (filter is_rec args, t1);
   200       val t3 = lift_defined (bound_arg vns) (nonlazy_vns, t2);
   201     in Library.foldr mk_All (vns, t3) end;
   202 
   203   fun one_eq ((p, cons), concl) =
   204     mk_trp (%:p $ UU) ===> Logic.list_implies (map (one_con p) cons, concl);
   205 
   206   fun ind_term concf = Library.foldr one_eq
   207     (mapn (fn n => fn x => (P_name n, x)) 1 conss,
   208      mk_trp (foldr1 mk_conj (mapn concf 1 dnames)));
   209   val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
   210   fun quant_tac ctxt i = EVERY
   211     (mapn (fn n => fn _ => res_inst_tac ctxt [(("x", 0), x_name n)] spec i) 1 dnames);
   212 
   213   fun ind_prems_tac prems = EVERY
   214     (maps (fn cons =>
   215       (resolve_tac prems 1 ::
   216         maps (fn (_,args) => 
   217           resolve_tac prems 1 ::
   218           map (K(atac 1)) (nonlazy args) @
   219           map (K(atac 1)) (filter is_rec args))
   220         cons))
   221       conss);
   222   local
   223     fun rec_to ns lazy_rec (n,cons) = forall (exists (fn arg => 
   224           is_rec arg andalso not (member (op =) ns (rec_of arg)) andalso
   225           ((rec_of arg =  n andalso not (lazy_rec orelse is_lazy arg)) orelse 
   226             rec_of arg <> n andalso rec_to (rec_of arg::ns) 
   227               (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
   228           ) o snd) cons;
   229     fun warn (n,cons) =
   230       if rec_to [] false (n,cons)
   231       then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
   232       else false;
   233 
   234   in
   235     val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   236     val is_emptys = map warn n__eqs;
   237     val is_finite = #is_finite take_info;
   238     val _ = if is_finite
   239             then message ("Proving finiteness rule for domain "^comp_dname^" ...")
   240             else ();
   241   end;
   242   val _ = trace " Proving finite_ind...";
   243   val finite_ind =
   244     let
   245       fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
   246       val goal = ind_term concf;
   247 
   248       fun tacf {prems, context} =
   249         let
   250           val tacs1 = [
   251             quant_tac context 1,
   252             simp_tac HOL_ss 1,
   253             InductTacs.induct_tac context [[SOME "n"]] 1,
   254             simp_tac (take_ss addsimps prems) 1,
   255             TRY (safe_tac HOL_cs)];
   256           fun arg_tac arg =
   257                         (* FIXME! case_UU_tac *)
   258             case_UU_tac context (prems @ con_rews) 1
   259               (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
   260           fun con_tacs (con, args) = 
   261             asm_simp_tac take_ss 1 ::
   262             map arg_tac (filter is_nonlazy_rec args) @
   263             [resolve_tac prems 1] @
   264             map (K (atac 1)) (nonlazy args) @
   265             map (K (etac spec 1)) (filter is_rec args);
   266           fun cases_tacs (cons, exhaust) =
   267             res_inst_tac context [(("y", 0), "x")] exhaust 1 ::
   268             asm_simp_tac (take_ss addsimps prems) 1 ::
   269             maps con_tacs cons;
   270         in
   271           tacs1 @ maps cases_tacs (conss ~~ exhausts)
   272         end;
   273     in pg'' thy [] goal tacf end;
   274 
   275 (* ----- theorems concerning finiteness and induction ----------------------- *)
   276 
   277   val global_ctxt = ProofContext.init_global thy;
   278 
   279   val _ = trace " Proving ind...";
   280   val ind =
   281     if is_finite
   282     then (* finite case *)
   283       let
   284         fun concf n dn = %:(P_name n) $ %:(x_name n);
   285         fun tacf {prems, context} =
   286           let
   287             fun finite_tacs (take_induct, fin_ind) = [
   288                 rtac take_induct 1,
   289                 rtac fin_ind 1,
   290                 ind_prems_tac prems];
   291           in
   292             TRY (safe_tac HOL_cs) ::
   293             maps finite_tacs (take_induct_thms ~~ atomize global_ctxt finite_ind)
   294           end;
   295       in pg'' thy [] (ind_term concf) tacf end
   296 
   297     else (* infinite case *)
   298       let
   299         val goal =
   300           let
   301             fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
   302             fun concf n dn = %:(P_name n) $ %:(x_name n);
   303           in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
   304         val cont_rules =
   305             @{thms cont_id cont_const cont2cont_Rep_CFun
   306                    cont2cont_fst cont2cont_snd};
   307         val subgoal =
   308           let
   309             val Ts = map (Type o fst) eqs;
   310             val P_names = Datatype_Prop.indexify_names (map (K "P") dnames);
   311             val x_names = Datatype_Prop.indexify_names (map (K "x") dnames);
   312             val P_types = map (fn T => T --> HOLogic.boolT) Ts;
   313             val Ps = map Free (P_names ~~ P_types);
   314             val xs = map Free (x_names ~~ Ts);
   315             val n = Free ("n", HOLogic.natT);
   316             val goals =
   317                 map (fn ((P,t),x) => P $ HOLCF_Library.mk_capply (t $ n, x))
   318                   (Ps ~~ take_consts ~~ xs);
   319           in
   320             HOLogic.mk_Trueprop
   321             (HOLogic.mk_all ("n", HOLogic.natT, foldr1 HOLogic.mk_conj goals))
   322           end;
   323         fun tacf {prems, context} =
   324           let
   325             val subtac =
   326                 EVERY [rtac allI 1, rtac finite_ind 1, ind_prems_tac prems];
   327             val subthm = Goal.prove context [] [] subgoal (K subtac);
   328           in
   329             map (fn ax_reach => rtac (ax_reach RS subst) 1) reach_thms @ [
   330             cut_facts_tac (subthm :: take (length dnames) prems) 1,
   331             REPEAT (rtac @{thm conjI} 1 ORELSE
   332                     EVERY [etac @{thm admD [OF _ ch2ch_Rep_CFunL]} 1,
   333                            resolve_tac chain_take_thms 1,
   334                            asm_simp_tac HOL_basic_ss 1])
   335             ]
   336           end;
   337       in pg'' thy [] goal tacf end;
   338 
   339 val case_ns =
   340   let
   341     val adms =
   342         if is_finite then [] else
   343         if length dnames = 1 then ["adm"] else
   344         map (fn s => "adm_" ^ Long_Name.base_name s) dnames;
   345     val bottoms =
   346         if length dnames = 1 then ["bottom"] else
   347         map (fn s => "bottom_" ^ Long_Name.base_name s) dnames;
   348     fun one_eq bot (_,cons) =
   349           bot :: map (fn (c,_) => Long_Name.base_name c) cons;
   350   in adms @ flat (map2 one_eq bottoms eqs) end;
   351 
   352 val inducts = Project_Rule.projections (ProofContext.init_global thy) ind;
   353 fun ind_rule (dname, rule) =
   354     ((Binding.empty, [rule]),
   355      [Rule_Cases.case_names case_ns, Induct.induct_type dname]);
   356 
   357 in
   358   thy
   359   |> snd o Global_Theory.add_thmss [
   360      ((Binding.qualified true "finite_induct" comp_dbind, [finite_ind]), []),
   361      ((Binding.qualified true "induct"        comp_dbind, [ind]       ), [])]
   362   |> (snd o Global_Theory.add_thmss (map ind_rule (dnames ~~ inducts)))
   363 end; (* prove_induction *)
   364 
   365 (******************************************************************************)
   366 (************************ bisimulation and coinduction ************************)
   367 (******************************************************************************)
   368 
   369 fun prove_coinduction
   370     (comp_dbind : binding, eqs : eq list)
   371     (take_rews : thm list)
   372     (take_lemmas : thm list)
   373     (thy : theory) : theory =
   374 let
   375 
   376 val dnames = map (fst o fst) eqs;
   377 val comp_dname = Sign.full_name thy comp_dbind;
   378 fun dc_take dn = %%:(dn^"_take");
   379 val x_name = idx_name dnames "x"; 
   380 val n_eqs = length eqs;
   381 
   382 (* ----- define bisimulation predicate -------------------------------------- *)
   383 
   384 local
   385   open HOLCF_Library
   386   val dtypes  = map (Type o fst) eqs;
   387   val relprod = mk_tupleT (map (fn tp => tp --> tp --> boolT) dtypes);
   388   val bisim_bind = Binding.suffix_name "_bisim" comp_dbind;
   389   val bisim_type = relprod --> boolT;
   390 in
   391   val (bisim_const, thy) =
   392       Sign.declare_const ((bisim_bind, bisim_type), NoSyn) thy;
   393 end;
   394 
   395 local
   396 
   397   fun legacy_infer_term thy t =
   398       singleton (Syntax.check_terms (ProofContext.init_global thy)) (intern_term thy t);
   399   fun legacy_infer_prop thy t = legacy_infer_term thy (Type.constraint propT t);
   400   fun infer_props thy = map (apsnd (legacy_infer_prop thy));
   401   fun add_defs_i x = Global_Theory.add_defs false (map Thm.no_attributes x);
   402   fun add_defs_infer defs thy = add_defs_i (infer_props thy defs) thy;
   403 
   404   fun one_con (con, args) =
   405     let
   406       val nonrec_args = filter_out is_rec args;
   407       val    rec_args = filter is_rec args;
   408       val    recs_cnt = length rec_args;
   409       val allargs     = nonrec_args @ rec_args
   410                         @ map (upd_vname (fn s=> s^"'")) rec_args;
   411       val allvns      = map vname allargs;
   412       fun vname_arg s arg = if is_rec arg then vname arg^s else vname arg;
   413       val vns1        = map (vname_arg "" ) args;
   414       val vns2        = map (vname_arg "'") args;
   415       val allargs_cnt = length nonrec_args + 2*recs_cnt;
   416       val rec_idxs    = (recs_cnt-1) downto 0;
   417       val nonlazy_idxs = map snd (filter_out (fn (arg,_) => is_lazy arg)
   418                                              (allargs~~((allargs_cnt-1) downto 0)));
   419       fun rel_app i ra = proj (Bound(allargs_cnt+2)) eqs (rec_of ra) $ 
   420                               Bound (2*recs_cnt-i) $ Bound (recs_cnt-i);
   421       val capps =
   422           List.foldr
   423             mk_conj
   424             (mk_conj(
   425              Bound(allargs_cnt+1)===list_ccomb(%%:con,map (bound_arg allvns) vns1),
   426              Bound(allargs_cnt+0)===list_ccomb(%%:con,map (bound_arg allvns) vns2)))
   427             (mapn rel_app 1 rec_args);
   428     in
   429       List.foldr
   430         mk_ex
   431         (Library.foldr mk_conj
   432                        (map (defined o Bound) nonlazy_idxs,capps)) allvns
   433     end;
   434   fun one_comp n (_,cons) =
   435       mk_all (x_name(n+1),
   436       mk_all (x_name(n+1)^"'",
   437       mk_imp (proj (Bound 2) eqs n $ Bound 1 $ Bound 0,
   438       foldr1 mk_disj (mk_conj(Bound 1 === UU,Bound 0 === UU)
   439                       ::map one_con cons))));
   440   val bisim_eqn =
   441       %%:(comp_dname^"_bisim") ==
   442          mk_lam("R", foldr1 mk_conj (mapn one_comp 0 eqs));
   443 
   444 in
   445   val (ax_bisim_def, thy) =
   446       yield_singleton add_defs_infer
   447         (Binding.qualified true "bisim_def" comp_dbind, bisim_eqn) thy;
   448 end; (* local *)
   449 
   450 (* ----- theorem concerning coinduction ------------------------------------- *)
   451 
   452 local
   453   val pg = pg' thy;
   454   val xs = mapn (fn n => K (x_name n)) 1 dnames;
   455   fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
   456   val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
   457   val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
   458   val _ = trace " Proving coind_lemma...";
   459   val coind_lemma =
   460     let
   461       fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
   462       fun mk_eqn n dn =
   463         (dc_take dn $ %:"n" ` bnd_arg n 0) ===
   464         (dc_take dn $ %:"n" ` bnd_arg n 1);
   465       fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
   466       val goal =
   467         mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
   468           Library.foldr mk_all2 (xs,
   469             Library.foldr mk_imp (mapn mk_prj 0 dnames,
   470               foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
   471       fun x_tacs ctxt n x = [
   472         rotate_tac (n+1) 1,
   473         etac all2E 1,
   474         eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
   475         TRY (safe_tac HOL_cs),
   476         REPEAT (CHANGED (asm_simp_tac take_ss 1))];
   477       fun tacs ctxt = [
   478         rtac impI 1,
   479         InductTacs.induct_tac ctxt [[SOME "n"]] 1,
   480         simp_tac take_ss 1,
   481         safe_tac HOL_cs] @
   482         flat (mapn (x_tacs ctxt) 0 xs);
   483     in pg [ax_bisim_def] goal tacs end;
   484 in
   485   val _ = trace " Proving coind...";
   486   val coind = 
   487     let
   488       fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
   489       fun mk_eqn x = %:x === %:(x^"'");
   490       val goal =
   491         mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
   492           Logic.list_implies (mapn mk_prj 0 xs,
   493             mk_trp (foldr1 mk_conj (map mk_eqn xs)));
   494       val tacs =
   495         TRY (safe_tac HOL_cs) ::
   496         maps (fn take_lemma => [
   497           rtac take_lemma 1,
   498           cut_facts_tac [coind_lemma] 1,
   499           fast_tac HOL_cs 1])
   500         take_lemmas;
   501     in pg [] goal (K tacs) end;
   502 end; (* local *)
   503 
   504 in thy |> snd o Global_Theory.add_thmss
   505     [((Binding.qualified true "coinduct" comp_dbind, [coind]), [])]
   506 end; (* let *)
   507 
   508 fun comp_theorems
   509     (comp_dbind : binding, eqs : eq list)
   510     (specs : (binding * (binding * (bool * binding option * typ) list * mixfix) list) list)
   511     (take_info : Domain_Take_Proofs.take_induct_info)
   512     (constr_infos : Domain_Constructors.constr_info list)
   513     (thy : theory) =
   514 let
   515 val map_tab = Domain_Take_Proofs.get_map_tab thy;
   516 
   517 val dnames = map (fst o fst) eqs;
   518 val comp_dname = Sign.full_name thy comp_dbind;
   519 
   520 (* ----- getting the composite axiom and definitions ------------------------ *)
   521 
   522 (* Test for indirect recursion *)
   523 local
   524   fun indirect_arg arg =
   525       rec_of arg = ~1 andalso Datatype_Aux.is_rec_type (dtyp_of arg);
   526   fun indirect_con (_, args) = exists indirect_arg args;
   527   fun indirect_eq (_, cons) = exists indirect_con cons;
   528 in
   529   val is_indirect = exists indirect_eq eqs;
   530   val _ =
   531       if is_indirect
   532       then message "Indirect recursion detected, skipping proofs of (co)induction rules"
   533       else message ("Proving induction properties of domain "^comp_dname^" ...");
   534 end;
   535 
   536 (* theorems about take *)
   537 
   538 val (take_rewss, thy) =
   539     take_theorems specs take_info constr_infos thy;
   540 
   541 val {take_lemma_thms, take_0_thms, take_strict_thms, ...} = take_info;
   542 
   543 val take_rews = take_0_thms @ take_strict_thms @ flat take_rewss;
   544 
   545 (* prove induction rules, unless definition is indirect recursive *)
   546 val thy =
   547     if is_indirect then thy else
   548     prove_induction (comp_dbind, eqs) take_rews take_info thy;
   549 
   550 val thy =
   551     if is_indirect then thy else
   552     prove_coinduction (comp_dbind, eqs) take_rews take_lemma_thms thy;
   553 
   554 in
   555   (take_rews, thy)
   556 end; (* let *)
   557 end; (* struct *)