src/HOLCF/Tools/Domain/domain_theorems.ML
author haftmann
Wed May 05 18:25:34 2010 +0200 (2010-05-05)
changeset 36692 54b64d4ad524
parent 36610 bafd82950e24
child 36837 4d1dd57103b9
permissions -rw-r--r--
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
     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 val pat_rews = #pat_rews result;
   149 
   150 (* ----- theorems concerning the isomorphism -------------------------------- *)
   151 
   152 val pg = pg' thy;
   153 
   154 val retraction_strict = @{thm retraction_strict};
   155 val abs_strict = ax_rep_iso RS (allI RS retraction_strict);
   156 val rep_strict = ax_abs_iso RS (allI RS retraction_strict);
   157 val iso_rews = map Drule.export_without_context [ax_abs_iso, ax_rep_iso, abs_strict, rep_strict];
   158 
   159 (* ----- theorems concerning one induction step ----------------------------- *)
   160 
   161 local
   162   fun dc_take dn = %%:(dn^"_take");
   163   val dnames = map (fst o fst) eqs;
   164   val deflation_thms = Domain_Take_Proofs.get_deflation_thms thy;
   165 
   166   fun copy_of_dtyp tab r dt =
   167       if Datatype_Aux.is_rec_type dt then copy tab r dt else ID
   168   and copy tab r (Datatype_Aux.DtRec i) = r i
   169     | copy tab r (Datatype_Aux.DtTFree a) = ID
   170     | copy tab r (Datatype_Aux.DtType (c, ds)) =
   171       case Symtab.lookup tab c of
   172         SOME f => list_ccomb (%%:f, map (copy_of_dtyp tab r) ds)
   173       | NONE => (warning ("copy_of_dtyp: unknown type constructor " ^ c); ID);
   174 
   175   fun one_take_app (con, args) =
   176     let
   177       fun mk_take n = dc_take (List.nth (dnames, n)) $ %:"n";
   178       fun one_rhs arg =
   179           if Datatype_Aux.is_rec_type (dtyp_of arg)
   180           then copy_of_dtyp map_tab
   181                  mk_take (dtyp_of arg) ` (%# arg)
   182           else (%# arg);
   183       val lhs = (dc_take dname $ (%%:"Suc" $ %:"n"))`(con_app con args);
   184       val rhs = con_app2 con one_rhs args;
   185       val goal = mk_trp (lhs === rhs);
   186       val rules =
   187           [ax_abs_iso] @ @{thms take_con_rules}
   188           @ take_Suc_thms @ deflation_thms @ deflation_take_thms;
   189       val tacs = [simp_tac (HOL_basic_ss addsimps rules) 1];
   190     in pg con_appls goal (K tacs) end;
   191   val take_apps = map one_take_app cons;
   192 in
   193   val take_rews = ax_take_0 :: ax_take_strict :: take_apps;
   194 end;
   195 
   196 val case_ns =
   197     "bottom" :: map (fn (b,_,_) => Binding.name_of b) spec;
   198 
   199 fun qualified name = Binding.qualified true name dbind;
   200 val simp = Simplifier.simp_add;
   201 val fixrec_simp = Fixrec.fixrec_simp_add;
   202 
   203 in
   204   thy
   205   |> PureThy.add_thmss [
   206      ((qualified "iso_rews"  , iso_rews    ), [simp]),
   207      ((qualified "nchotomy"  , [nchotomy]  ), []),
   208      ((qualified "exhaust"   , [exhaust]   ),
   209       [Rule_Cases.case_names case_ns, Induct.cases_type dname]),
   210      ((qualified "when_rews" , when_rews   ), [simp]),
   211      ((qualified "compacts"  , compacts    ), [simp]),
   212      ((qualified "con_rews"  , con_rews    ), [simp, fixrec_simp]),
   213      ((qualified "sel_rews"  , sel_rews    ), [simp]),
   214      ((qualified "dis_rews"  , dis_rews    ), [simp]),
   215      ((qualified "pat_rews"  , pat_rews    ), [simp]),
   216      ((qualified "dist_les"  , dist_les    ), [simp]),
   217      ((qualified "dist_eqs"  , dist_eqs    ), [simp]),
   218      ((qualified "inverts"   , inverts     ), [simp]),
   219      ((qualified "injects"   , injects     ), [simp]),
   220      ((qualified "take_rews" , take_rews   ), [simp]),
   221      ((qualified "match_rews", mat_rews    ), [simp, fixrec_simp])]
   222   |> snd
   223   |> pair (iso_rews @ when_rews @ con_rews @ sel_rews @ dis_rews @
   224       pat_rews @ dist_les @ dist_eqs)
   225 end; (* let *)
   226 
   227 (******************************************************************************)
   228 (****************************** induction rules *******************************)
   229 (******************************************************************************)
   230 
   231 fun prove_induction
   232     (comp_dbind : binding, eqs : eq list)
   233     (take_rews : thm list)
   234     (take_info : Domain_Take_Proofs.take_induct_info)
   235     (thy : theory) =
   236 let
   237   val comp_dname = Sign.full_name thy comp_dbind;
   238   val dnames = map (fst o fst) eqs;
   239   val conss  = map  snd        eqs;
   240   fun dc_take dn = %%:(dn^"_take");
   241   val x_name = idx_name dnames "x";
   242   val P_name = idx_name dnames "P";
   243   val pg = pg' thy;
   244 
   245   local
   246     fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
   247     fun gts s dn = PureThy.get_thms thy (dn ^ "." ^ s);
   248   in
   249     val axs_rep_iso = map (ga "rep_iso") dnames;
   250     val axs_abs_iso = map (ga "abs_iso") dnames;
   251     val exhausts = map (ga  "exhaust" ) dnames;
   252     val con_rews  = maps (gts "con_rews" ) dnames;
   253   end;
   254 
   255   val {take_consts, ...} = take_info;
   256   val {take_0_thms, take_Suc_thms, chain_take_thms, ...} = take_info;
   257   val {lub_take_thms, finite_defs, reach_thms, ...} = take_info;
   258   val {take_induct_thms, ...} = take_info;
   259 
   260   fun one_con p (con, args) =
   261     let
   262       val P_names = map P_name (1 upto (length dnames));
   263       val vns = Name.variant_list P_names (map vname args);
   264       val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
   265       fun ind_hyp arg = %:(P_name (1 + rec_of arg)) $ bound_arg args arg;
   266       val t1 = mk_trp (%:p $ con_app2 con (bound_arg args) args);
   267       val t2 = lift ind_hyp (filter is_rec args, t1);
   268       val t3 = lift_defined (bound_arg vns) (nonlazy_vns, t2);
   269     in Library.foldr mk_All (vns, t3) end;
   270 
   271   fun one_eq ((p, cons), concl) =
   272     mk_trp (%:p $ UU) ===> Logic.list_implies (map (one_con p) cons, concl);
   273 
   274   fun ind_term concf = Library.foldr one_eq
   275     (mapn (fn n => fn x => (P_name n, x)) 1 conss,
   276      mk_trp (foldr1 mk_conj (mapn concf 1 dnames)));
   277   val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
   278   fun quant_tac ctxt i = EVERY
   279     (mapn (fn n => fn _ => res_inst_tac ctxt [(("x", 0), x_name n)] spec i) 1 dnames);
   280 
   281   fun ind_prems_tac prems = EVERY
   282     (maps (fn cons =>
   283       (resolve_tac prems 1 ::
   284         maps (fn (_,args) => 
   285           resolve_tac prems 1 ::
   286           map (K(atac 1)) (nonlazy args) @
   287           map (K(atac 1)) (filter is_rec args))
   288         cons))
   289       conss);
   290   local 
   291     (* check whether every/exists constructor of the n-th part of the equation:
   292        it has a possibly indirectly recursive argument that isn't/is possibly 
   293        indirectly lazy *)
   294     fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
   295           is_rec arg andalso not (member (op =) ns (rec_of arg)) andalso
   296           ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
   297             rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
   298               (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
   299           ) o snd) cons;
   300     fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
   301     fun warn (n,cons) =
   302       if all_rec_to [] false (n,cons)
   303       then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
   304       else false;
   305     fun lazy_rec_to ns = rec_to exists I  lazy_rec_to ns;
   306 
   307   in
   308     val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   309     val is_emptys = map warn n__eqs;
   310     val is_finite = #is_finite take_info;
   311     val _ = if is_finite
   312             then message ("Proving finiteness rule for domain "^comp_dname^" ...")
   313             else ();
   314   end;
   315   val _ = trace " Proving finite_ind...";
   316   val finite_ind =
   317     let
   318       fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
   319       val goal = ind_term concf;
   320 
   321       fun tacf {prems, context} =
   322         let
   323           val tacs1 = [
   324             quant_tac context 1,
   325             simp_tac HOL_ss 1,
   326             InductTacs.induct_tac context [[SOME "n"]] 1,
   327             simp_tac (take_ss addsimps prems) 1,
   328             TRY (safe_tac HOL_cs)];
   329           fun arg_tac arg =
   330                         (* FIXME! case_UU_tac *)
   331             case_UU_tac context (prems @ con_rews) 1
   332               (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
   333           fun con_tacs (con, args) = 
   334             asm_simp_tac take_ss 1 ::
   335             map arg_tac (filter is_nonlazy_rec args) @
   336             [resolve_tac prems 1] @
   337             map (K (atac 1)) (nonlazy args) @
   338             map (K (etac spec 1)) (filter is_rec args);
   339           fun cases_tacs (cons, exhaust) =
   340             res_inst_tac context [(("y", 0), "x")] exhaust 1 ::
   341             asm_simp_tac (take_ss addsimps prems) 1 ::
   342             maps con_tacs cons;
   343         in
   344           tacs1 @ maps cases_tacs (conss ~~ exhausts)
   345         end;
   346     in pg'' thy [] goal tacf end;
   347 
   348 (* ----- theorems concerning finiteness and induction ----------------------- *)
   349 
   350   val global_ctxt = ProofContext.init_global thy;
   351 
   352   val _ = trace " Proving ind...";
   353   val ind =
   354     if is_finite
   355     then (* finite case *)
   356       let
   357         fun concf n dn = %:(P_name n) $ %:(x_name n);
   358         fun tacf {prems, context} =
   359           let
   360             fun finite_tacs (take_induct, fin_ind) = [
   361                 rtac take_induct 1,
   362                 rtac fin_ind 1,
   363                 ind_prems_tac prems];
   364           in
   365             TRY (safe_tac HOL_cs) ::
   366             maps finite_tacs (take_induct_thms ~~ atomize global_ctxt finite_ind)
   367           end;
   368       in pg'' thy [] (ind_term concf) tacf end
   369 
   370     else (* infinite case *)
   371       let
   372         val goal =
   373           let
   374             fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
   375             fun concf n dn = %:(P_name n) $ %:(x_name n);
   376           in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
   377         val cont_rules =
   378             @{thms cont_id cont_const cont2cont_Rep_CFun
   379                    cont2cont_fst cont2cont_snd};
   380         val subgoal =
   381           let
   382             val Ts = map (Type o fst) eqs;
   383             val P_names = Datatype_Prop.indexify_names (map (K "P") dnames);
   384             val x_names = Datatype_Prop.indexify_names (map (K "x") dnames);
   385             val P_types = map (fn T => T --> HOLogic.boolT) Ts;
   386             val Ps = map Free (P_names ~~ P_types);
   387             val xs = map Free (x_names ~~ Ts);
   388             val n = Free ("n", HOLogic.natT);
   389             val goals =
   390                 map (fn ((P,t),x) => P $ HOLCF_Library.mk_capply (t $ n, x))
   391                   (Ps ~~ take_consts ~~ xs);
   392           in
   393             HOLogic.mk_Trueprop
   394             (HOLogic.mk_all ("n", HOLogic.natT, foldr1 HOLogic.mk_conj goals))
   395           end;
   396         fun tacf {prems, context} =
   397           let
   398             val subtac =
   399                 EVERY [rtac allI 1, rtac finite_ind 1, ind_prems_tac prems];
   400             val subthm = Goal.prove context [] [] subgoal (K subtac);
   401           in
   402             map (fn ax_reach => rtac (ax_reach RS subst) 1) reach_thms @ [
   403             cut_facts_tac (subthm :: take (length dnames) prems) 1,
   404             REPEAT (rtac @{thm conjI} 1 ORELSE
   405                     EVERY [etac @{thm admD [OF _ ch2ch_Rep_CFunL]} 1,
   406                            resolve_tac chain_take_thms 1,
   407                            asm_simp_tac HOL_basic_ss 1])
   408             ]
   409           end;
   410       in pg'' thy [] goal tacf end;
   411 
   412 val case_ns =
   413   let
   414     val adms =
   415         if is_finite then [] else
   416         if length dnames = 1 then ["adm"] else
   417         map (fn s => "adm_" ^ Long_Name.base_name s) dnames;
   418     val bottoms =
   419         if length dnames = 1 then ["bottom"] else
   420         map (fn s => "bottom_" ^ Long_Name.base_name s) dnames;
   421     fun one_eq bot (_,cons) =
   422           bot :: map (fn (c,_) => Long_Name.base_name c) cons;
   423   in adms @ flat (map2 one_eq bottoms eqs) end;
   424 
   425 val inducts = Project_Rule.projections (ProofContext.init_global thy) ind;
   426 fun ind_rule (dname, rule) =
   427     ((Binding.empty, [rule]),
   428      [Rule_Cases.case_names case_ns, Induct.induct_type dname]);
   429 
   430 in
   431   thy
   432   |> snd o PureThy.add_thmss [
   433      ((Binding.qualified true "finite_induct" comp_dbind, [finite_ind]), []),
   434      ((Binding.qualified true "induct"        comp_dbind, [ind]       ), [])]
   435   |> (snd o PureThy.add_thmss (map ind_rule (dnames ~~ inducts)))
   436 end; (* prove_induction *)
   437 
   438 (******************************************************************************)
   439 (************************ bisimulation and coinduction ************************)
   440 (******************************************************************************)
   441 
   442 fun prove_coinduction
   443     (comp_dbind : binding, eqs : eq list)
   444     (take_lemmas : thm list)
   445     (thy : theory) : theory =
   446 let
   447 
   448 val dnames = map (fst o fst) eqs;
   449 val comp_dname = Sign.full_name thy comp_dbind;
   450 fun dc_take dn = %%:(dn^"_take");
   451 val x_name = idx_name dnames "x"; 
   452 val n_eqs = length eqs;
   453 
   454 val take_rews =
   455     maps (fn dn => PureThy.get_thms thy (dn ^ ".take_rews")) dnames;
   456 
   457 (* ----- define bisimulation predicate -------------------------------------- *)
   458 
   459 local
   460   open HOLCF_Library
   461   val dtypes  = map (Type o fst) eqs;
   462   val relprod = mk_tupleT (map (fn tp => tp --> tp --> boolT) dtypes);
   463   val bisim_bind = Binding.suffix_name "_bisim" comp_dbind;
   464   val bisim_type = relprod --> boolT;
   465 in
   466   val (bisim_const, thy) =
   467       Sign.declare_const ((bisim_bind, bisim_type), NoSyn) thy;
   468 end;
   469 
   470 local
   471 
   472   fun legacy_infer_term thy t =
   473       singleton (Syntax.check_terms (ProofContext.init_global thy)) (intern_term thy t);
   474   fun legacy_infer_prop thy t = legacy_infer_term thy (TypeInfer.constrain propT t);
   475   fun infer_props thy = map (apsnd (legacy_infer_prop thy));
   476   fun add_defs_i x = PureThy.add_defs false (map Thm.no_attributes x);
   477   fun add_defs_infer defs thy = add_defs_i (infer_props thy defs) thy;
   478 
   479   fun one_con (con, args) =
   480     let
   481       val nonrec_args = filter_out is_rec args;
   482       val    rec_args = filter is_rec args;
   483       val    recs_cnt = length rec_args;
   484       val allargs     = nonrec_args @ rec_args
   485                         @ map (upd_vname (fn s=> s^"'")) rec_args;
   486       val allvns      = map vname allargs;
   487       fun vname_arg s arg = if is_rec arg then vname arg^s else vname arg;
   488       val vns1        = map (vname_arg "" ) args;
   489       val vns2        = map (vname_arg "'") args;
   490       val allargs_cnt = length nonrec_args + 2*recs_cnt;
   491       val rec_idxs    = (recs_cnt-1) downto 0;
   492       val nonlazy_idxs = map snd (filter_out (fn (arg,_) => is_lazy arg)
   493                                              (allargs~~((allargs_cnt-1) downto 0)));
   494       fun rel_app i ra = proj (Bound(allargs_cnt+2)) eqs (rec_of ra) $ 
   495                               Bound (2*recs_cnt-i) $ Bound (recs_cnt-i);
   496       val capps =
   497           List.foldr
   498             mk_conj
   499             (mk_conj(
   500              Bound(allargs_cnt+1)===list_ccomb(%%:con,map (bound_arg allvns) vns1),
   501              Bound(allargs_cnt+0)===list_ccomb(%%:con,map (bound_arg allvns) vns2)))
   502             (mapn rel_app 1 rec_args);
   503     in
   504       List.foldr
   505         mk_ex
   506         (Library.foldr mk_conj
   507                        (map (defined o Bound) nonlazy_idxs,capps)) allvns
   508     end;
   509   fun one_comp n (_,cons) =
   510       mk_all (x_name(n+1),
   511       mk_all (x_name(n+1)^"'",
   512       mk_imp (proj (Bound 2) eqs n $ Bound 1 $ Bound 0,
   513       foldr1 mk_disj (mk_conj(Bound 1 === UU,Bound 0 === UU)
   514                       ::map one_con cons))));
   515   val bisim_eqn =
   516       %%:(comp_dname^"_bisim") ==
   517          mk_lam("R", foldr1 mk_conj (mapn one_comp 0 eqs));
   518 
   519 in
   520   val (ax_bisim_def, thy) =
   521       yield_singleton add_defs_infer
   522         (Binding.qualified true "bisim_def" comp_dbind, bisim_eqn) thy;
   523 end; (* local *)
   524 
   525 (* ----- theorem concerning coinduction ------------------------------------- *)
   526 
   527 local
   528   val pg = pg' thy;
   529   val xs = mapn (fn n => K (x_name n)) 1 dnames;
   530   fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
   531   val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
   532   val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
   533   val _ = trace " Proving coind_lemma...";
   534   val coind_lemma =
   535     let
   536       fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
   537       fun mk_eqn n dn =
   538         (dc_take dn $ %:"n" ` bnd_arg n 0) ===
   539         (dc_take dn $ %:"n" ` bnd_arg n 1);
   540       fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
   541       val goal =
   542         mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
   543           Library.foldr mk_all2 (xs,
   544             Library.foldr mk_imp (mapn mk_prj 0 dnames,
   545               foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
   546       fun x_tacs ctxt n x = [
   547         rotate_tac (n+1) 1,
   548         etac all2E 1,
   549         eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
   550         TRY (safe_tac HOL_cs),
   551         REPEAT (CHANGED (asm_simp_tac take_ss 1))];
   552       fun tacs ctxt = [
   553         rtac impI 1,
   554         InductTacs.induct_tac ctxt [[SOME "n"]] 1,
   555         simp_tac take_ss 1,
   556         safe_tac HOL_cs] @
   557         flat (mapn (x_tacs ctxt) 0 xs);
   558     in pg [ax_bisim_def] goal tacs end;
   559 in
   560   val _ = trace " Proving coind...";
   561   val coind = 
   562     let
   563       fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
   564       fun mk_eqn x = %:x === %:(x^"'");
   565       val goal =
   566         mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
   567           Logic.list_implies (mapn mk_prj 0 xs,
   568             mk_trp (foldr1 mk_conj (map mk_eqn xs)));
   569       val tacs =
   570         TRY (safe_tac HOL_cs) ::
   571         maps (fn take_lemma => [
   572           rtac take_lemma 1,
   573           cut_facts_tac [coind_lemma] 1,
   574           fast_tac HOL_cs 1])
   575         take_lemmas;
   576     in pg [] goal (K tacs) end;
   577 end; (* local *)
   578 
   579 in thy |> snd o PureThy.add_thmss
   580     [((Binding.qualified true "coinduct" comp_dbind, [coind]), [])]
   581 end; (* let *)
   582 
   583 fun comp_theorems
   584     (comp_dbind : binding, eqs : eq list)
   585     (take_info : Domain_Take_Proofs.take_induct_info)
   586     (thy : theory) =
   587 let
   588 val map_tab = Domain_Take_Proofs.get_map_tab thy;
   589 
   590 val dnames = map (fst o fst) eqs;
   591 val comp_dname = Sign.full_name thy comp_dbind;
   592 
   593 (* ----- getting the composite axiom and definitions ------------------------ *)
   594 
   595 (* Test for indirect recursion *)
   596 local
   597   fun indirect_arg arg =
   598       rec_of arg = ~1 andalso Datatype_Aux.is_rec_type (dtyp_of arg);
   599   fun indirect_con (_, args) = exists indirect_arg args;
   600   fun indirect_eq (_, cons) = exists indirect_con cons;
   601 in
   602   val is_indirect = exists indirect_eq eqs;
   603   val _ =
   604       if is_indirect
   605       then message "Indirect recursion detected, skipping proofs of (co)induction rules"
   606       else message ("Proving induction properties of domain "^comp_dname^" ...");
   607 end;
   608 
   609 (* theorems about take *)
   610 
   611 val take_lemmas = #take_lemma_thms take_info;
   612 
   613 val take_rews =
   614     maps (fn dn => PureThy.get_thms thy (dn ^ ".take_rews")) dnames;
   615 
   616 (* prove induction rules, unless definition is indirect recursive *)
   617 val thy =
   618     if is_indirect then thy else
   619     prove_induction (comp_dbind, eqs) take_rews take_info thy;
   620 
   621 val thy =
   622     if is_indirect then thy else
   623     prove_coinduction (comp_dbind, eqs) take_lemmas thy;
   624 
   625 in
   626   (take_rews, thy)
   627 end; (* let *)
   628 end; (* struct *)