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