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