src/HOLCF/Tools/Domain/domain_theorems.ML
author huffman
Tue May 11 12:38:07 2010 -0700 (2010-05-11)
changeset 36837 4d1dd57103b9
parent 36692 54b64d4ad524
child 37082 a1fb7947dc2b
permissions -rw-r--r--
simplify code for emptiness check
     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 = [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     fun rec_to ns lazy_rec (n,cons) = forall (exists (fn arg => 
   292           is_rec arg andalso not (member (op =) ns (rec_of arg)) andalso
   293           ((rec_of arg =  n andalso not (lazy_rec orelse is_lazy arg)) orelse 
   294             rec_of arg <> n andalso rec_to (rec_of arg::ns) 
   295               (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
   296           ) o snd) cons;
   297     fun warn (n,cons) =
   298       if rec_to [] false (n,cons)
   299       then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
   300       else false;
   301 
   302   in
   303     val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   304     val is_emptys = map warn n__eqs;
   305     val is_finite = #is_finite take_info;
   306     val _ = if is_finite
   307             then message ("Proving finiteness rule for domain "^comp_dname^" ...")
   308             else ();
   309   end;
   310   val _ = trace " Proving finite_ind...";
   311   val finite_ind =
   312     let
   313       fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
   314       val goal = ind_term concf;
   315 
   316       fun tacf {prems, context} =
   317         let
   318           val tacs1 = [
   319             quant_tac context 1,
   320             simp_tac HOL_ss 1,
   321             InductTacs.induct_tac context [[SOME "n"]] 1,
   322             simp_tac (take_ss addsimps prems) 1,
   323             TRY (safe_tac HOL_cs)];
   324           fun arg_tac arg =
   325                         (* FIXME! case_UU_tac *)
   326             case_UU_tac context (prems @ con_rews) 1
   327               (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
   328           fun con_tacs (con, args) = 
   329             asm_simp_tac take_ss 1 ::
   330             map arg_tac (filter is_nonlazy_rec args) @
   331             [resolve_tac prems 1] @
   332             map (K (atac 1)) (nonlazy args) @
   333             map (K (etac spec 1)) (filter is_rec args);
   334           fun cases_tacs (cons, exhaust) =
   335             res_inst_tac context [(("y", 0), "x")] exhaust 1 ::
   336             asm_simp_tac (take_ss addsimps prems) 1 ::
   337             maps con_tacs cons;
   338         in
   339           tacs1 @ maps cases_tacs (conss ~~ exhausts)
   340         end;
   341     in pg'' thy [] goal tacf end;
   342 
   343 (* ----- theorems concerning finiteness and induction ----------------------- *)
   344 
   345   val global_ctxt = ProofContext.init_global thy;
   346 
   347   val _ = trace " Proving ind...";
   348   val ind =
   349     if is_finite
   350     then (* finite case *)
   351       let
   352         fun concf n dn = %:(P_name n) $ %:(x_name n);
   353         fun tacf {prems, context} =
   354           let
   355             fun finite_tacs (take_induct, fin_ind) = [
   356                 rtac take_induct 1,
   357                 rtac fin_ind 1,
   358                 ind_prems_tac prems];
   359           in
   360             TRY (safe_tac HOL_cs) ::
   361             maps finite_tacs (take_induct_thms ~~ atomize global_ctxt finite_ind)
   362           end;
   363       in pg'' thy [] (ind_term concf) tacf end
   364 
   365     else (* infinite case *)
   366       let
   367         val goal =
   368           let
   369             fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
   370             fun concf n dn = %:(P_name n) $ %:(x_name n);
   371           in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
   372         val cont_rules =
   373             @{thms cont_id cont_const cont2cont_Rep_CFun
   374                    cont2cont_fst cont2cont_snd};
   375         val subgoal =
   376           let
   377             val Ts = map (Type o fst) eqs;
   378             val P_names = Datatype_Prop.indexify_names (map (K "P") dnames);
   379             val x_names = Datatype_Prop.indexify_names (map (K "x") dnames);
   380             val P_types = map (fn T => T --> HOLogic.boolT) Ts;
   381             val Ps = map Free (P_names ~~ P_types);
   382             val xs = map Free (x_names ~~ Ts);
   383             val n = Free ("n", HOLogic.natT);
   384             val goals =
   385                 map (fn ((P,t),x) => P $ HOLCF_Library.mk_capply (t $ n, x))
   386                   (Ps ~~ take_consts ~~ xs);
   387           in
   388             HOLogic.mk_Trueprop
   389             (HOLogic.mk_all ("n", HOLogic.natT, foldr1 HOLogic.mk_conj goals))
   390           end;
   391         fun tacf {prems, context} =
   392           let
   393             val subtac =
   394                 EVERY [rtac allI 1, rtac finite_ind 1, ind_prems_tac prems];
   395             val subthm = Goal.prove context [] [] subgoal (K subtac);
   396           in
   397             map (fn ax_reach => rtac (ax_reach RS subst) 1) reach_thms @ [
   398             cut_facts_tac (subthm :: take (length dnames) prems) 1,
   399             REPEAT (rtac @{thm conjI} 1 ORELSE
   400                     EVERY [etac @{thm admD [OF _ ch2ch_Rep_CFunL]} 1,
   401                            resolve_tac chain_take_thms 1,
   402                            asm_simp_tac HOL_basic_ss 1])
   403             ]
   404           end;
   405       in pg'' thy [] goal tacf end;
   406 
   407 val case_ns =
   408   let
   409     val adms =
   410         if is_finite then [] else
   411         if length dnames = 1 then ["adm"] else
   412         map (fn s => "adm_" ^ Long_Name.base_name s) dnames;
   413     val bottoms =
   414         if length dnames = 1 then ["bottom"] else
   415         map (fn s => "bottom_" ^ Long_Name.base_name s) dnames;
   416     fun one_eq bot (_,cons) =
   417           bot :: map (fn (c,_) => Long_Name.base_name c) cons;
   418   in adms @ flat (map2 one_eq bottoms eqs) end;
   419 
   420 val inducts = Project_Rule.projections (ProofContext.init_global thy) ind;
   421 fun ind_rule (dname, rule) =
   422     ((Binding.empty, [rule]),
   423      [Rule_Cases.case_names case_ns, Induct.induct_type dname]);
   424 
   425 in
   426   thy
   427   |> snd o PureThy.add_thmss [
   428      ((Binding.qualified true "finite_induct" comp_dbind, [finite_ind]), []),
   429      ((Binding.qualified true "induct"        comp_dbind, [ind]       ), [])]
   430   |> (snd o PureThy.add_thmss (map ind_rule (dnames ~~ inducts)))
   431 end; (* prove_induction *)
   432 
   433 (******************************************************************************)
   434 (************************ bisimulation and coinduction ************************)
   435 (******************************************************************************)
   436 
   437 fun prove_coinduction
   438     (comp_dbind : binding, eqs : eq list)
   439     (take_lemmas : thm list)
   440     (thy : theory) : theory =
   441 let
   442 
   443 val dnames = map (fst o fst) eqs;
   444 val comp_dname = Sign.full_name thy comp_dbind;
   445 fun dc_take dn = %%:(dn^"_take");
   446 val x_name = idx_name dnames "x"; 
   447 val n_eqs = length eqs;
   448 
   449 val take_rews =
   450     maps (fn dn => PureThy.get_thms thy (dn ^ ".take_rews")) dnames;
   451 
   452 (* ----- define bisimulation predicate -------------------------------------- *)
   453 
   454 local
   455   open HOLCF_Library
   456   val dtypes  = map (Type o fst) eqs;
   457   val relprod = mk_tupleT (map (fn tp => tp --> tp --> boolT) dtypes);
   458   val bisim_bind = Binding.suffix_name "_bisim" comp_dbind;
   459   val bisim_type = relprod --> boolT;
   460 in
   461   val (bisim_const, thy) =
   462       Sign.declare_const ((bisim_bind, bisim_type), NoSyn) thy;
   463 end;
   464 
   465 local
   466 
   467   fun legacy_infer_term thy t =
   468       singleton (Syntax.check_terms (ProofContext.init_global thy)) (intern_term thy t);
   469   fun legacy_infer_prop thy t = legacy_infer_term thy (TypeInfer.constrain propT t);
   470   fun infer_props thy = map (apsnd (legacy_infer_prop thy));
   471   fun add_defs_i x = PureThy.add_defs false (map Thm.no_attributes x);
   472   fun add_defs_infer defs thy = add_defs_i (infer_props thy defs) thy;
   473 
   474   fun one_con (con, args) =
   475     let
   476       val nonrec_args = filter_out is_rec args;
   477       val    rec_args = filter is_rec args;
   478       val    recs_cnt = length rec_args;
   479       val allargs     = nonrec_args @ rec_args
   480                         @ map (upd_vname (fn s=> s^"'")) rec_args;
   481       val allvns      = map vname allargs;
   482       fun vname_arg s arg = if is_rec arg then vname arg^s else vname arg;
   483       val vns1        = map (vname_arg "" ) args;
   484       val vns2        = map (vname_arg "'") args;
   485       val allargs_cnt = length nonrec_args + 2*recs_cnt;
   486       val rec_idxs    = (recs_cnt-1) downto 0;
   487       val nonlazy_idxs = map snd (filter_out (fn (arg,_) => is_lazy arg)
   488                                              (allargs~~((allargs_cnt-1) downto 0)));
   489       fun rel_app i ra = proj (Bound(allargs_cnt+2)) eqs (rec_of ra) $ 
   490                               Bound (2*recs_cnt-i) $ Bound (recs_cnt-i);
   491       val capps =
   492           List.foldr
   493             mk_conj
   494             (mk_conj(
   495              Bound(allargs_cnt+1)===list_ccomb(%%:con,map (bound_arg allvns) vns1),
   496              Bound(allargs_cnt+0)===list_ccomb(%%:con,map (bound_arg allvns) vns2)))
   497             (mapn rel_app 1 rec_args);
   498     in
   499       List.foldr
   500         mk_ex
   501         (Library.foldr mk_conj
   502                        (map (defined o Bound) nonlazy_idxs,capps)) allvns
   503     end;
   504   fun one_comp n (_,cons) =
   505       mk_all (x_name(n+1),
   506       mk_all (x_name(n+1)^"'",
   507       mk_imp (proj (Bound 2) eqs n $ Bound 1 $ Bound 0,
   508       foldr1 mk_disj (mk_conj(Bound 1 === UU,Bound 0 === UU)
   509                       ::map one_con cons))));
   510   val bisim_eqn =
   511       %%:(comp_dname^"_bisim") ==
   512          mk_lam("R", foldr1 mk_conj (mapn one_comp 0 eqs));
   513 
   514 in
   515   val (ax_bisim_def, thy) =
   516       yield_singleton add_defs_infer
   517         (Binding.qualified true "bisim_def" comp_dbind, bisim_eqn) thy;
   518 end; (* local *)
   519 
   520 (* ----- theorem concerning coinduction ------------------------------------- *)
   521 
   522 local
   523   val pg = pg' thy;
   524   val xs = mapn (fn n => K (x_name n)) 1 dnames;
   525   fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
   526   val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
   527   val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
   528   val _ = trace " Proving coind_lemma...";
   529   val coind_lemma =
   530     let
   531       fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
   532       fun mk_eqn n dn =
   533         (dc_take dn $ %:"n" ` bnd_arg n 0) ===
   534         (dc_take dn $ %:"n" ` bnd_arg n 1);
   535       fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
   536       val goal =
   537         mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
   538           Library.foldr mk_all2 (xs,
   539             Library.foldr mk_imp (mapn mk_prj 0 dnames,
   540               foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
   541       fun x_tacs ctxt n x = [
   542         rotate_tac (n+1) 1,
   543         etac all2E 1,
   544         eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
   545         TRY (safe_tac HOL_cs),
   546         REPEAT (CHANGED (asm_simp_tac take_ss 1))];
   547       fun tacs ctxt = [
   548         rtac impI 1,
   549         InductTacs.induct_tac ctxt [[SOME "n"]] 1,
   550         simp_tac take_ss 1,
   551         safe_tac HOL_cs] @
   552         flat (mapn (x_tacs ctxt) 0 xs);
   553     in pg [ax_bisim_def] goal tacs end;
   554 in
   555   val _ = trace " Proving coind...";
   556   val coind = 
   557     let
   558       fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
   559       fun mk_eqn x = %:x === %:(x^"'");
   560       val goal =
   561         mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
   562           Logic.list_implies (mapn mk_prj 0 xs,
   563             mk_trp (foldr1 mk_conj (map mk_eqn xs)));
   564       val tacs =
   565         TRY (safe_tac HOL_cs) ::
   566         maps (fn take_lemma => [
   567           rtac take_lemma 1,
   568           cut_facts_tac [coind_lemma] 1,
   569           fast_tac HOL_cs 1])
   570         take_lemmas;
   571     in pg [] goal (K tacs) end;
   572 end; (* local *)
   573 
   574 in thy |> snd o PureThy.add_thmss
   575     [((Binding.qualified true "coinduct" comp_dbind, [coind]), [])]
   576 end; (* let *)
   577 
   578 fun comp_theorems
   579     (comp_dbind : binding, eqs : eq list)
   580     (take_info : Domain_Take_Proofs.take_induct_info)
   581     (thy : theory) =
   582 let
   583 val map_tab = Domain_Take_Proofs.get_map_tab thy;
   584 
   585 val dnames = map (fst o fst) eqs;
   586 val comp_dname = Sign.full_name thy comp_dbind;
   587 
   588 (* ----- getting the composite axiom and definitions ------------------------ *)
   589 
   590 (* Test for indirect recursion *)
   591 local
   592   fun indirect_arg arg =
   593       rec_of arg = ~1 andalso Datatype_Aux.is_rec_type (dtyp_of arg);
   594   fun indirect_con (_, args) = exists indirect_arg args;
   595   fun indirect_eq (_, cons) = exists indirect_con cons;
   596 in
   597   val is_indirect = exists indirect_eq eqs;
   598   val _ =
   599       if is_indirect
   600       then message "Indirect recursion detected, skipping proofs of (co)induction rules"
   601       else message ("Proving induction properties of domain "^comp_dname^" ...");
   602 end;
   603 
   604 (* theorems about take *)
   605 
   606 val take_lemmas = #take_lemma_thms take_info;
   607 
   608 val take_rews =
   609     maps (fn dn => PureThy.get_thms thy (dn ^ ".take_rews")) dnames;
   610 
   611 (* prove induction rules, unless definition is indirect recursive *)
   612 val thy =
   613     if is_indirect then thy else
   614     prove_induction (comp_dbind, eqs) take_rews take_info thy;
   615 
   616 val thy =
   617     if is_indirect then thy else
   618     prove_coinduction (comp_dbind, eqs) take_lemmas thy;
   619 
   620 in
   621   (take_rews, thy)
   622 end; (* let *)
   623 end; (* struct *)