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