src/HOLCF/Tools/Domain/domain_theorems.ML
author haftmann
Fri Dec 04 12:17:43 2009 +0100 (2009-12-04)
changeset 33971 9c7fa7f76950
parent 33810 38375b16ffd9
child 34974 18b41bba42b5
permissions -rw-r--r--
modernized structure Datatype_Aux
     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: Domain_Library.eq * Domain_Library.eq list -> theory -> thm list * theory;
    13   val comp_theorems: bstring * Domain_Library.eq list -> theory -> thm list * theory;
    14   val quiet_mode: bool Unsynchronized.ref;
    15   val trace_domain: bool Unsynchronized.ref;
    16 end;
    17 
    18 structure Domain_Theorems :> DOMAIN_THEOREMS =
    19 struct
    20 
    21 val quiet_mode = Unsynchronized.ref false;
    22 val trace_domain = Unsynchronized.ref false;
    23 
    24 fun message s = if !quiet_mode then () else writeln s;
    25 fun trace s = if !trace_domain then tracing s else ();
    26 
    27 local
    28 
    29 val adm_impl_admw = @{thm adm_impl_admw};
    30 val adm_all = @{thm adm_all};
    31 val adm_conj = @{thm adm_conj};
    32 val adm_subst = @{thm adm_subst};
    33 val antisym_less_inverse = @{thm below_antisym_inverse};
    34 val beta_cfun = @{thm beta_cfun};
    35 val cfun_arg_cong = @{thm cfun_arg_cong};
    36 val ch2ch_fst = @{thm ch2ch_fst};
    37 val ch2ch_snd = @{thm ch2ch_snd};
    38 val ch2ch_Rep_CFunL = @{thm ch2ch_Rep_CFunL};
    39 val ch2ch_Rep_CFunR = @{thm ch2ch_Rep_CFunR};
    40 val chain_iterate = @{thm chain_iterate};
    41 val compact_ONE = @{thm compact_ONE};
    42 val compact_sinl = @{thm compact_sinl};
    43 val compact_sinr = @{thm compact_sinr};
    44 val compact_spair = @{thm compact_spair};
    45 val compact_up = @{thm compact_up};
    46 val contlub_cfun_arg = @{thm contlub_cfun_arg};
    47 val contlub_cfun_fun = @{thm contlub_cfun_fun};
    48 val contlub_fst = @{thm contlub_fst};
    49 val contlub_snd = @{thm contlub_snd};
    50 val contlubE = @{thm contlubE};
    51 val cont_const = @{thm cont_const};
    52 val cont_id = @{thm cont_id};
    53 val cont2cont_fst = @{thm cont2cont_fst};
    54 val cont2cont_snd = @{thm cont2cont_snd};
    55 val cont2cont_Rep_CFun = @{thm cont2cont_Rep_CFun};
    56 val fix_def2 = @{thm fix_def2};
    57 val injection_eq = @{thm injection_eq};
    58 val injection_less = @{thm injection_below};
    59 val lub_equal = @{thm lub_equal};
    60 val monofun_cfun_arg = @{thm monofun_cfun_arg};
    61 val retraction_strict = @{thm retraction_strict};
    62 val spair_eq = @{thm spair_eq};
    63 val spair_less = @{thm spair_below};
    64 val sscase1 = @{thm sscase1};
    65 val ssplit1 = @{thm ssplit1};
    66 val strictify1 = @{thm strictify1};
    67 val wfix_ind = @{thm wfix_ind};
    68 
    69 val iso_intro       = @{thm iso.intro};
    70 val iso_abs_iso     = @{thm iso.abs_iso};
    71 val iso_rep_iso     = @{thm iso.rep_iso};
    72 val iso_abs_strict  = @{thm iso.abs_strict};
    73 val iso_rep_strict  = @{thm iso.rep_strict};
    74 val iso_abs_defin'  = @{thm iso.abs_defin'};
    75 val iso_rep_defin'  = @{thm iso.rep_defin'};
    76 val iso_abs_defined = @{thm iso.abs_defined};
    77 val iso_rep_defined = @{thm iso.rep_defined};
    78 val iso_compact_abs = @{thm iso.compact_abs};
    79 val iso_compact_rep = @{thm iso.compact_rep};
    80 val iso_iso_swap    = @{thm iso.iso_swap};
    81 
    82 val exh_start = @{thm exh_start};
    83 val ex_defined_iffs = @{thms ex_defined_iffs};
    84 val exh_casedist0 = @{thm exh_casedist0};
    85 val exh_casedists = @{thms exh_casedists};
    86 
    87 open Domain_Library;
    88 infixr 0 ===>;
    89 infixr 0 ==>;
    90 infix 0 == ; 
    91 infix 1 ===;
    92 infix 1 ~= ;
    93 infix 1 <<;
    94 infix 1 ~<<;
    95 infix 9 `   ;
    96 infix 9 `% ;
    97 infix 9 `%%;
    98 infixr 9 oo;
    99 
   100 (* ----- general proof facilities ------------------------------------------- *)
   101 
   102 fun legacy_infer_term thy t =
   103   let val ctxt = ProofContext.set_mode ProofContext.mode_schematic (ProofContext.init thy)
   104   in singleton (Syntax.check_terms ctxt) (Sign.intern_term thy t) end;
   105 
   106 fun pg'' thy defs t tacs =
   107   let
   108     val t' = legacy_infer_term thy t;
   109     val asms = Logic.strip_imp_prems t';
   110     val prop = Logic.strip_imp_concl t';
   111     fun tac {prems, context} =
   112       rewrite_goals_tac defs THEN
   113       EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
   114   in Goal.prove_global thy [] asms prop tac end;
   115 
   116 fun pg' thy defs t tacsf =
   117   let
   118     fun tacs {prems, context} =
   119       if null prems then tacsf context
   120       else cut_facts_tac prems 1 :: tacsf context;
   121   in pg'' thy defs t tacs end;
   122 
   123 fun case_UU_tac ctxt rews i v =
   124   InductTacs.case_tac ctxt (v^"=UU") i THEN
   125   asm_simp_tac (HOLCF_ss addsimps rews) i;
   126 
   127 val chain_tac =
   128   REPEAT_DETERM o resolve_tac 
   129     [chain_iterate, ch2ch_Rep_CFunR, ch2ch_Rep_CFunL, ch2ch_fst, ch2ch_snd];
   130 
   131 (* ----- general proofs ----------------------------------------------------- *)
   132 
   133 val all2E = @{lemma "!x y . P x y ==> (P x y ==> R) ==> R" by simp}
   134 
   135 val dist_eqI = @{lemma "!!x::'a::po. ~ x << y ==> x ~= y" by (blast dest!: below_antisym_inverse)}
   136 
   137 in
   138 
   139 fun theorems (((dname, _), cons) : eq, eqs : eq list) thy =
   140 let
   141 
   142 val _ = message ("Proving isomorphism properties of domain "^dname^" ...");
   143 val pg = pg' thy;
   144 val map_tab = Domain_Isomorphism.get_map_tab thy;
   145 
   146 
   147 (* ----- getting the axioms and definitions --------------------------------- *)
   148 
   149 local
   150   fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
   151 in
   152   val ax_abs_iso  = ga "abs_iso"  dname;
   153   val ax_rep_iso  = ga "rep_iso"  dname;
   154   val ax_when_def = ga "when_def" dname;
   155   fun get_def mk_name (con,_) = ga (mk_name con^"_def") dname;
   156   val axs_con_def = map (get_def extern_name) cons;
   157   val axs_dis_def = map (get_def dis_name) cons;
   158   val axs_mat_def = map (get_def mat_name) cons;
   159   val axs_pat_def = map (get_def pat_name) cons;
   160   val axs_sel_def =
   161     let
   162       fun def_of_sel sel = ga (sel^"_def") dname;
   163       fun def_of_arg arg = Option.map def_of_sel (sel_of arg);
   164       fun defs_of_con (_, args) = map_filter def_of_arg args;
   165     in
   166       maps defs_of_con cons
   167     end;
   168   val ax_copy_def = ga "copy_def" dname;
   169 end; (* local *)
   170 
   171 (* ----- theorems concerning the isomorphism -------------------------------- *)
   172 
   173 val dc_abs  = %%:(dname^"_abs");
   174 val dc_rep  = %%:(dname^"_rep");
   175 val dc_copy = %%:(dname^"_copy");
   176 val x_name = "x";
   177 
   178 val iso_locale = iso_intro OF [ax_abs_iso, ax_rep_iso];
   179 val abs_strict = ax_rep_iso RS (allI RS retraction_strict);
   180 val rep_strict = ax_abs_iso RS (allI RS retraction_strict);
   181 val abs_defin' = iso_locale RS iso_abs_defin';
   182 val rep_defin' = iso_locale RS iso_rep_defin';
   183 val iso_rews = map Drule.standard [ax_abs_iso,ax_rep_iso,abs_strict,rep_strict];
   184 
   185 (* ----- generating beta reduction rules from definitions-------------------- *)
   186 
   187 val _ = trace " Proving beta reduction rules...";
   188 
   189 local
   190   fun arglist (Const _ $ Abs (s, _, t)) =
   191     let
   192       val (vars,body) = arglist t;
   193     in (s :: vars, body) end
   194     | arglist t = ([], t);
   195   fun bind_fun vars t = Library.foldr mk_All (vars, t);
   196   fun bound_vars 0 = []
   197     | bound_vars i = Bound (i-1) :: bound_vars (i - 1);
   198 in
   199   fun appl_of_def def =
   200     let
   201       val (_ $ con $ lam) = concl_of def;
   202       val (vars, rhs) = arglist lam;
   203       val lhs = list_ccomb (con, bound_vars (length vars));
   204       val appl = bind_fun vars (lhs == rhs);
   205       val cs = ContProc.cont_thms lam;
   206       val betas = map (fn c => mk_meta_eq (c RS beta_cfun)) cs;
   207     in pg (def::betas) appl (K [rtac reflexive_thm 1]) end;
   208 end;
   209 
   210 val _ = trace "Proving when_appl...";
   211 val when_appl = appl_of_def ax_when_def;
   212 val _ = trace "Proving con_appls...";
   213 val con_appls = map appl_of_def axs_con_def;
   214 
   215 local
   216   fun arg2typ n arg =
   217     let val t = TVar (("'a", n), pcpoS)
   218     in (n + 1, if is_lazy arg then mk_uT t else t) end;
   219 
   220   fun args2typ n [] = (n, oneT)
   221     | args2typ n [arg] = arg2typ n arg
   222     | args2typ n (arg::args) =
   223     let
   224       val (n1, t1) = arg2typ n arg;
   225       val (n2, t2) = args2typ n1 args
   226     in (n2, mk_sprodT (t1, t2)) end;
   227 
   228   fun cons2typ n [] = (n,oneT)
   229     | cons2typ n [con] = args2typ n (snd con)
   230     | cons2typ n (con::cons) =
   231     let
   232       val (n1, t1) = args2typ n (snd con);
   233       val (n2, t2) = cons2typ n1 cons
   234     in (n2, mk_ssumT (t1, t2)) end;
   235 in
   236   fun cons2ctyp cons = ctyp_of thy (snd (cons2typ 1 cons));
   237 end;
   238 
   239 local 
   240   val iso_swap = iso_locale RS iso_iso_swap;
   241   fun one_con (con, args) =
   242     let
   243       val vns = map vname args;
   244       val eqn = %:x_name === con_app2 con %: vns;
   245       val conj = foldr1 mk_conj (eqn :: map (defined o %:) (nonlazy args));
   246     in Library.foldr mk_ex (vns, conj) end;
   247 
   248   val conj_assoc = @{thm conj_assoc};
   249   val exh = foldr1 mk_disj ((%:x_name === UU) :: map one_con cons);
   250   val thm1 = instantiate' [SOME (cons2ctyp cons)] [] exh_start;
   251   val thm2 = rewrite_rule (map mk_meta_eq ex_defined_iffs) thm1;
   252   val thm3 = rewrite_rule [mk_meta_eq @{thm conj_assoc}] thm2;
   253 
   254   (* first 3 rules replace "x = UU \/ P" with "rep$x = UU \/ P" *)
   255   val tacs = [
   256     rtac disjE 1,
   257     etac (rep_defin' RS disjI1) 2,
   258     etac disjI2 2,
   259     rewrite_goals_tac [mk_meta_eq iso_swap],
   260     rtac thm3 1];
   261 in
   262   val _ = trace " Proving exhaust...";
   263   val exhaust = pg con_appls (mk_trp exh) (K tacs);
   264   val _ = trace " Proving casedist...";
   265   val casedist =
   266     Drule.standard (rewrite_rule exh_casedists (exhaust RS exh_casedist0));
   267 end;
   268 
   269 local 
   270   fun bind_fun t = Library.foldr mk_All (when_funs cons, t);
   271   fun bound_fun i _ = Bound (length cons - i);
   272   val when_app = list_ccomb (%%:(dname^"_when"), mapn bound_fun 1 cons);
   273 in
   274   val _ = trace " Proving when_strict...";
   275   val when_strict =
   276     let
   277       val axs = [when_appl, mk_meta_eq rep_strict];
   278       val goal = bind_fun (mk_trp (strict when_app));
   279       val tacs = [resolve_tac [sscase1, ssplit1, strictify1] 1];
   280     in pg axs goal (K tacs) end;
   281 
   282   val _ = trace " Proving when_apps...";
   283   val when_apps =
   284     let
   285       fun one_when n (con,args) =
   286         let
   287           val axs = when_appl :: con_appls;
   288           val goal = bind_fun (lift_defined %: (nonlazy args, 
   289                 mk_trp (when_app`(con_app con args) ===
   290                        list_ccomb (bound_fun n 0, map %# args))));
   291           val tacs = [asm_simp_tac (HOLCF_ss addsimps [ax_abs_iso]) 1];
   292         in pg axs goal (K tacs) end;
   293     in mapn one_when 1 cons end;
   294 end;
   295 val when_rews = when_strict :: when_apps;
   296 
   297 (* ----- theorems concerning the constructors, discriminators and selectors - *)
   298 
   299 local
   300   fun dis_strict (con, _) =
   301     let
   302       val goal = mk_trp (strict (%%:(dis_name con)));
   303     in pg axs_dis_def goal (K [rtac when_strict 1]) end;
   304 
   305   fun dis_app c (con, args) =
   306     let
   307       val lhs = %%:(dis_name c) ` con_app con args;
   308       val rhs = if con = c then TT else FF;
   309       val goal = lift_defined %: (nonlazy args, mk_trp (lhs === rhs));
   310       val tacs = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   311     in pg axs_dis_def goal (K tacs) end;
   312 
   313   val _ = trace " Proving dis_apps...";
   314   val dis_apps = maps (fn (c,_) => map (dis_app c) cons) cons;
   315 
   316   fun dis_defin (con, args) =
   317     let
   318       val goal = defined (%:x_name) ==> defined (%%:(dis_name con) `% x_name);
   319       val tacs =
   320         [rtac casedist 1,
   321          contr_tac 1,
   322          DETERM_UNTIL_SOLVED (CHANGED
   323           (asm_simp_tac (HOLCF_ss addsimps dis_apps) 1))];
   324     in pg [] goal (K tacs) end;
   325 
   326   val _ = trace " Proving dis_stricts...";
   327   val dis_stricts = map dis_strict cons;
   328   val _ = trace " Proving dis_defins...";
   329   val dis_defins = map dis_defin cons;
   330 in
   331   val dis_rews = dis_stricts @ dis_defins @ dis_apps;
   332 end;
   333 
   334 local
   335   fun mat_strict (con, _) =
   336     let
   337       val goal = mk_trp (%%:(mat_name con) ` UU ` %:"rhs" === UU);
   338       val tacs = [asm_simp_tac (HOLCF_ss addsimps [when_strict]) 1];
   339     in pg axs_mat_def goal (K tacs) end;
   340 
   341   val _ = trace " Proving mat_stricts...";
   342   val mat_stricts = map mat_strict cons;
   343 
   344   fun one_mat c (con, args) =
   345     let
   346       val lhs = %%:(mat_name c) ` con_app con args ` %:"rhs";
   347       val rhs =
   348         if con = c
   349         then list_ccomb (%:"rhs", map %# args)
   350         else mk_fail;
   351       val goal = lift_defined %: (nonlazy args, mk_trp (lhs === rhs));
   352       val tacs = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   353     in pg axs_mat_def goal (K tacs) end;
   354 
   355   val _ = trace " Proving mat_apps...";
   356   val mat_apps =
   357     maps (fn (c,_) => map (one_mat c) cons) cons;
   358 in
   359   val mat_rews = mat_stricts @ mat_apps;
   360 end;
   361 
   362 local
   363   fun ps args = mapn (fn n => fn _ => %:("pat" ^ string_of_int n)) 1 args;
   364 
   365   fun pat_lhs (con,args) = mk_branch (list_comb (%%:(pat_name con), ps args));
   366 
   367   fun pat_rhs (con,[]) = mk_return ((%:"rhs") ` HOLogic.unit)
   368     | pat_rhs (con,args) =
   369         (mk_branch (mk_ctuple_pat (ps args)))
   370           `(%:"rhs")`(mk_ctuple (map %# args));
   371 
   372   fun pat_strict c =
   373     let
   374       val axs = @{thm branch_def} :: axs_pat_def;
   375       val goal = mk_trp (strict (pat_lhs c ` (%:"rhs")));
   376       val tacs = [simp_tac (HOLCF_ss addsimps [when_strict]) 1];
   377     in pg axs goal (K tacs) end;
   378 
   379   fun pat_app c (con, args) =
   380     let
   381       val axs = @{thm branch_def} :: axs_pat_def;
   382       val lhs = (pat_lhs c)`(%:"rhs")`(con_app con args);
   383       val rhs = if con = fst c then pat_rhs c else mk_fail;
   384       val goal = lift_defined %: (nonlazy args, mk_trp (lhs === rhs));
   385       val tacs = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   386     in pg axs goal (K tacs) end;
   387 
   388   val _ = trace " Proving pat_stricts...";
   389   val pat_stricts = map pat_strict cons;
   390   val _ = trace " Proving pat_apps...";
   391   val pat_apps = maps (fn c => map (pat_app c) cons) cons;
   392 in
   393   val pat_rews = pat_stricts @ pat_apps;
   394 end;
   395 
   396 local
   397   fun con_strict (con, args) = 
   398     let
   399       val rules = abs_strict :: @{thms con_strict_rules};
   400       fun one_strict vn =
   401         let
   402           fun f arg = if vname arg = vn then UU else %# arg;
   403           val goal = mk_trp (con_app2 con f args === UU);
   404           val tacs = [simp_tac (HOL_basic_ss addsimps rules) 1];
   405         in pg con_appls goal (K tacs) end;
   406     in map one_strict (nonlazy args) end;
   407 
   408   fun con_defin (con, args) =
   409     let
   410       fun iff_disj (t, []) = HOLogic.mk_not t
   411         | iff_disj (t, ts) = t === foldr1 HOLogic.mk_disj ts;
   412       val lhs = con_app con args === UU;
   413       val rhss = map (fn x => %:x === UU) (nonlazy args);
   414       val goal = mk_trp (iff_disj (lhs, rhss));
   415       val rule1 = iso_locale RS @{thm iso.abs_defined_iff};
   416       val rules = rule1 :: @{thms con_defined_iff_rules};
   417       val tacs = [simp_tac (HOL_ss addsimps rules) 1];
   418     in pg con_appls goal (K tacs) end;
   419 in
   420   val _ = trace " Proving con_stricts...";
   421   val con_stricts = maps con_strict cons;
   422   val _ = trace " Proving con_defins...";
   423   val con_defins = map con_defin cons;
   424   val con_rews = con_stricts @ con_defins;
   425 end;
   426 
   427 local
   428   val rules =
   429     [compact_sinl, compact_sinr, compact_spair, compact_up, compact_ONE];
   430   fun con_compact (con, args) =
   431     let
   432       val concl = mk_trp (mk_compact (con_app con args));
   433       val goal = lift (fn x => mk_compact (%#x)) (args, concl);
   434       val tacs = [
   435         rtac (iso_locale RS iso_compact_abs) 1,
   436         REPEAT (resolve_tac rules 1 ORELSE atac 1)];
   437     in pg con_appls goal (K tacs) end;
   438 in
   439   val _ = trace " Proving con_compacts...";
   440   val con_compacts = map con_compact cons;
   441 end;
   442 
   443 local
   444   fun one_sel sel =
   445     pg axs_sel_def (mk_trp (strict (%%:sel)))
   446       (K [simp_tac (HOLCF_ss addsimps when_rews) 1]);
   447 
   448   fun sel_strict (_, args) =
   449     map_filter (Option.map one_sel o sel_of) args;
   450 in
   451   val _ = trace " Proving sel_stricts...";
   452   val sel_stricts = maps sel_strict cons;
   453 end;
   454 
   455 local
   456   fun sel_app_same c n sel (con, args) =
   457     let
   458       val nlas = nonlazy args;
   459       val vns = map vname args;
   460       val vnn = List.nth (vns, n);
   461       val nlas' = filter (fn v => v <> vnn) nlas;
   462       val lhs = (%%:sel)`(con_app con args);
   463       val goal = lift_defined %: (nlas', mk_trp (lhs === %:vnn));
   464       fun tacs1 ctxt =
   465         if vnn mem nlas
   466         then [case_UU_tac ctxt (when_rews @ con_stricts) 1 vnn]
   467         else [];
   468       val tacs2 = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   469     in pg axs_sel_def goal (fn ctxt => (tacs1 ctxt @ tacs2)) end;
   470 
   471   fun sel_app_diff c n sel (con, args) =
   472     let
   473       val nlas = nonlazy args;
   474       val goal = mk_trp (%%:sel ` con_app con args === UU);
   475       fun tacs1 ctxt = map (case_UU_tac ctxt (when_rews @ con_stricts) 1) nlas;
   476       val tacs2 = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   477     in pg axs_sel_def goal (fn ctxt => (tacs1 ctxt @ tacs2)) end;
   478 
   479   fun sel_app c n sel (con, args) =
   480     if con = c
   481     then sel_app_same c n sel (con, args)
   482     else sel_app_diff c n sel (con, args);
   483 
   484   fun one_sel c n sel = map (sel_app c n sel) cons;
   485   fun one_sel' c n arg = Option.map (one_sel c n) (sel_of arg);
   486   fun one_con (c, args) =
   487     flat (map_filter I (mapn (one_sel' c) 0 args));
   488 in
   489   val _ = trace " Proving sel_apps...";
   490   val sel_apps = maps one_con cons;
   491 end;
   492 
   493 local
   494   fun sel_defin sel =
   495     let
   496       val goal = defined (%:x_name) ==> defined (%%:sel`%x_name);
   497       val tacs = [
   498         rtac casedist 1,
   499         contr_tac 1,
   500         DETERM_UNTIL_SOLVED (CHANGED
   501           (asm_simp_tac (HOLCF_ss addsimps sel_apps) 1))];
   502     in pg [] goal (K tacs) end;
   503 in
   504   val _ = trace " Proving sel_defins...";
   505   val sel_defins =
   506     if length cons = 1
   507     then map_filter (fn arg => Option.map sel_defin (sel_of arg))
   508                  (filter_out is_lazy (snd (hd cons)))
   509     else [];
   510 end;
   511 
   512 val sel_rews = sel_stricts @ sel_defins @ sel_apps;
   513 
   514 val _ = trace " Proving dist_les...";
   515 val distincts_le =
   516   let
   517     fun dist (con1, args1) (con2, args2) =
   518       let
   519         val goal = lift_defined %: (nonlazy args1,
   520                         mk_trp (con_app con1 args1 ~<< con_app con2 args2));
   521         fun tacs ctxt = [
   522           rtac @{thm rev_contrapos} 1,
   523           eres_inst_tac ctxt [(("f", 0), dis_name con1)] monofun_cfun_arg 1]
   524           @ map (case_UU_tac ctxt (con_stricts @ dis_rews) 1) (nonlazy args2)
   525           @ [asm_simp_tac (HOLCF_ss addsimps dis_rews) 1];
   526       in pg [] goal tacs end;
   527 
   528     fun distinct (con1, args1) (con2, args2) =
   529         let
   530           val arg1 = (con1, args1);
   531           val arg2 =
   532             (con2, ListPair.map (fn (arg,vn) => upd_vname (K vn) arg)
   533               (args2, Name.variant_list (map vname args1) (map vname args2)));
   534         in [dist arg1 arg2, dist arg2 arg1] end;
   535     fun distincts []      = []
   536       | distincts (c::cs) = (map (distinct c) cs) :: distincts cs;
   537   in distincts cons end;
   538 val dist_les = flat (flat distincts_le);
   539 
   540 val _ = trace " Proving dist_eqs...";
   541 val dist_eqs =
   542   let
   543     fun distinct (_,args1) ((_,args2), leqs) =
   544       let
   545         val (le1,le2) = (hd leqs, hd(tl leqs));
   546         val (eq1,eq2) = (le1 RS dist_eqI, le2 RS dist_eqI)
   547       in
   548         if nonlazy args1 = [] then [eq1, eq1 RS not_sym] else
   549         if nonlazy args2 = [] then [eq2, eq2 RS not_sym] else
   550           [eq1, eq2]
   551       end;
   552     fun distincts []      = []
   553       | distincts ((c,leqs)::cs) =
   554         flat
   555           (ListPair.map (distinct c) ((map #1 cs),leqs)) @
   556         distincts cs;
   557   in map Drule.standard (distincts (cons ~~ distincts_le)) end;
   558 
   559 local 
   560   fun pgterm rel con args =
   561     let
   562       fun append s = upd_vname (fn v => v^s);
   563       val (largs, rargs) = (args, map (append "'") args);
   564       val concl =
   565         foldr1 mk_conj (ListPair.map rel (map %# largs, map %# rargs));
   566       val prem = rel (con_app con largs, con_app con rargs);
   567       val sargs = case largs of [_] => [] | _ => nonlazy args;
   568       val prop = lift_defined %: (sargs, mk_trp (prem === concl));
   569     in pg con_appls prop end;
   570   val cons' = filter (fn (_,args) => args<>[]) cons;
   571 in
   572   val _ = trace " Proving inverts...";
   573   val inverts =
   574     let
   575       val abs_less = ax_abs_iso RS (allI RS injection_less);
   576       val tacs =
   577         [asm_full_simp_tac (HOLCF_ss addsimps [abs_less, spair_less]) 1];
   578     in map (fn (con, args) => pgterm (op <<) con args (K tacs)) cons' end;
   579 
   580   val _ = trace " Proving injects...";
   581   val injects =
   582     let
   583       val abs_eq = ax_abs_iso RS (allI RS injection_eq);
   584       val tacs = [asm_full_simp_tac (HOLCF_ss addsimps [abs_eq, spair_eq]) 1];
   585     in map (fn (con, args) => pgterm (op ===) con args (K tacs)) cons' end;
   586 end;
   587 
   588 (* ----- theorems concerning one induction step ----------------------------- *)
   589 
   590 val copy_strict =
   591   let
   592     val _ = trace " Proving copy_strict...";
   593     val goal = mk_trp (strict (dc_copy `% "f"));
   594     val rules = [abs_strict, rep_strict] @ @{thms domain_map_stricts};
   595     val tacs = [asm_simp_tac (HOLCF_ss addsimps rules) 1];
   596   in pg [ax_copy_def] goal (K tacs) end;
   597 
   598 local
   599   fun copy_app (con, args) =
   600     let
   601       val lhs = dc_copy`%"f"`(con_app con args);
   602       fun one_rhs arg =
   603           if Datatype_Aux.is_rec_type (dtyp_of arg)
   604           then Domain_Axioms.copy_of_dtyp map_tab
   605                  (proj (%:"f") eqs) (dtyp_of arg) ` (%# arg)
   606           else (%# arg);
   607       val rhs = con_app2 con one_rhs args;
   608       val goal = lift_defined %: (nonlazy_rec args, mk_trp (lhs === rhs));
   609       val args' = filter_out (fn a => is_rec a orelse is_lazy a) args;
   610       val stricts = abs_strict :: rep_strict :: @{thms domain_map_stricts};
   611       fun tacs1 ctxt = map (case_UU_tac ctxt stricts 1 o vname) args';
   612       val rules = [ax_abs_iso] @ @{thms domain_map_simps};
   613       val tacs2 = [asm_simp_tac (HOLCF_ss addsimps rules) 1];
   614     in pg (ax_copy_def::con_appls) goal (fn ctxt => (tacs1 ctxt @ tacs2)) end;
   615 in
   616   val _ = trace " Proving copy_apps...";
   617   val copy_apps = map copy_app cons;
   618 end;
   619 
   620 local
   621   fun one_strict (con, args) = 
   622     let
   623       val goal = mk_trp (dc_copy`UU`(con_app con args) === UU);
   624       val rews = copy_strict :: copy_apps @ con_rews;
   625       fun tacs ctxt = map (case_UU_tac ctxt rews 1) (nonlazy args) @
   626         [asm_simp_tac (HOLCF_ss addsimps rews) 1];
   627     in pg [] goal tacs end;
   628 
   629   fun has_nonlazy_rec (_, args) = exists is_nonlazy_rec args;
   630 in
   631   val _ = trace " Proving copy_stricts...";
   632   val copy_stricts = map one_strict (filter has_nonlazy_rec cons);
   633 end;
   634 
   635 val copy_rews = copy_strict :: copy_apps @ copy_stricts;
   636 
   637 in
   638   thy
   639     |> Sign.add_path (Long_Name.base_name dname)
   640     |> snd o PureThy.add_thmss [
   641         ((Binding.name "iso_rews"  , iso_rews    ), [Simplifier.simp_add]),
   642         ((Binding.name "exhaust"   , [exhaust]   ), []),
   643         ((Binding.name "casedist"  , [casedist]  ), [Induct.cases_type dname]),
   644         ((Binding.name "when_rews" , when_rews   ), [Simplifier.simp_add]),
   645         ((Binding.name "compacts"  , con_compacts), [Simplifier.simp_add]),
   646         ((Binding.name "con_rews"  , con_rews    ),
   647          [Simplifier.simp_add, Fixrec.fixrec_simp_add]),
   648         ((Binding.name "sel_rews"  , sel_rews    ), [Simplifier.simp_add]),
   649         ((Binding.name "dis_rews"  , dis_rews    ), [Simplifier.simp_add]),
   650         ((Binding.name "pat_rews"  , pat_rews    ), [Simplifier.simp_add]),
   651         ((Binding.name "dist_les"  , dist_les    ), [Simplifier.simp_add]),
   652         ((Binding.name "dist_eqs"  , dist_eqs    ), [Simplifier.simp_add]),
   653         ((Binding.name "inverts"   , inverts     ), [Simplifier.simp_add]),
   654         ((Binding.name "injects"   , injects     ), [Simplifier.simp_add]),
   655         ((Binding.name "copy_rews" , copy_rews   ), [Simplifier.simp_add]),
   656         ((Binding.name "match_rews", mat_rews    ),
   657          [Simplifier.simp_add, Fixrec.fixrec_simp_add])]
   658     |> Sign.parent_path
   659     |> pair (iso_rews @ when_rews @ con_rews @ sel_rews @ dis_rews @
   660         pat_rews @ dist_les @ dist_eqs @ copy_rews)
   661 end; (* let *)
   662 
   663 fun comp_theorems (comp_dnam, eqs: eq list) thy =
   664 let
   665 val global_ctxt = ProofContext.init thy;
   666 val map_tab = Domain_Isomorphism.get_map_tab thy;
   667 
   668 val dnames = map (fst o fst) eqs;
   669 val conss  = map  snd        eqs;
   670 val comp_dname = Sign.full_bname thy comp_dnam;
   671 
   672 val _ = message ("Proving induction properties of domain "^comp_dname^" ...");
   673 val pg = pg' thy;
   674 
   675 (* ----- getting the composite axiom and definitions ------------------------ *)
   676 
   677 local
   678   fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
   679 in
   680   val axs_reach      = map (ga "reach"     ) dnames;
   681   val axs_take_def   = map (ga "take_def"  ) dnames;
   682   val axs_finite_def = map (ga "finite_def") dnames;
   683   val ax_copy2_def   =      ga "copy_def"  comp_dnam;
   684   val ax_bisim_def   =      ga "bisim_def" comp_dnam;
   685 end;
   686 
   687 local
   688   fun gt  s dn = PureThy.get_thm  thy (dn ^ "." ^ s);
   689   fun gts s dn = PureThy.get_thms thy (dn ^ "." ^ s);
   690 in
   691   val cases = map (gt  "casedist" ) dnames;
   692   val con_rews  = maps (gts "con_rews" ) dnames;
   693   val copy_rews = maps (gts "copy_rews") dnames;
   694 end;
   695 
   696 fun dc_take dn = %%:(dn^"_take");
   697 val x_name = idx_name dnames "x"; 
   698 val P_name = idx_name dnames "P";
   699 val n_eqs = length eqs;
   700 
   701 (* ----- theorems concerning finite approximation and finite induction ------ *)
   702 
   703 local
   704   val iterate_Cprod_ss = global_simpset_of @{theory Fix};
   705   val copy_con_rews  = copy_rews @ con_rews;
   706   val copy_take_defs =
   707     (if n_eqs = 1 then [] else [ax_copy2_def]) @ axs_take_def;
   708   val _ = trace " Proving take_stricts...";
   709   val take_stricts =
   710     let
   711       fun one_eq ((dn, args), _) = strict (dc_take dn $ %:"n");
   712       val goal = mk_trp (foldr1 mk_conj (map one_eq eqs));
   713       fun tacs ctxt = [
   714         InductTacs.induct_tac ctxt [[SOME "n"]] 1,
   715         simp_tac iterate_Cprod_ss 1,
   716         asm_simp_tac (iterate_Cprod_ss addsimps copy_rews) 1];
   717     in pg copy_take_defs goal tacs end;
   718 
   719   val take_stricts' = rewrite_rule copy_take_defs take_stricts;
   720   fun take_0 n dn =
   721     let
   722       val goal = mk_trp ((dc_take dn $ %%:"HOL.zero") `% x_name n === UU);
   723     in pg axs_take_def goal (K [simp_tac iterate_Cprod_ss 1]) end;
   724   val take_0s = mapn take_0 1 dnames;
   725   fun c_UU_tac ctxt = case_UU_tac ctxt (take_stricts'::copy_con_rews) 1;
   726   val _ = trace " Proving take_apps...";
   727   val take_apps =
   728     let
   729       fun mk_eqn dn (con, args) =
   730         let
   731           fun mk_take n = dc_take (List.nth (dnames, n)) $ %:"n";
   732           fun one_rhs arg =
   733               if Datatype_Aux.is_rec_type (dtyp_of arg)
   734               then Domain_Axioms.copy_of_dtyp map_tab
   735                      mk_take (dtyp_of arg) ` (%# arg)
   736               else (%# arg);
   737           val lhs = (dc_take dn $ (%%:"Suc" $ %:"n"))`(con_app con args);
   738           val rhs = con_app2 con one_rhs args;
   739         in Library.foldr mk_all (map vname args, lhs === rhs) end;
   740       fun mk_eqns ((dn, _), cons) = map (mk_eqn dn) cons;
   741       val goal = mk_trp (foldr1 mk_conj (maps mk_eqns eqs));
   742       val simps = filter (has_fewer_prems 1) copy_rews;
   743       fun con_tac ctxt (con, args) =
   744         if nonlazy_rec args = []
   745         then all_tac
   746         else EVERY (map (c_UU_tac ctxt) (nonlazy_rec args)) THEN
   747           asm_full_simp_tac (HOLCF_ss addsimps copy_rews) 1;
   748       fun eq_tacs ctxt ((dn, _), cons) = map (con_tac ctxt) cons;
   749       fun tacs ctxt =
   750         simp_tac iterate_Cprod_ss 1 ::
   751         InductTacs.induct_tac ctxt [[SOME "n"]] 1 ::
   752         simp_tac (iterate_Cprod_ss addsimps copy_con_rews) 1 ::
   753         asm_full_simp_tac (HOLCF_ss addsimps simps) 1 ::
   754         TRY (safe_tac HOL_cs) ::
   755         maps (eq_tacs ctxt) eqs;
   756     in pg copy_take_defs goal tacs end;
   757 in
   758   val take_rews = map Drule.standard
   759     (atomize global_ctxt take_stricts @ take_0s @ atomize global_ctxt take_apps);
   760 end; (* local *)
   761 
   762 local
   763   fun one_con p (con,args) =
   764     let
   765       fun ind_hyp arg = %:(P_name (1 + rec_of arg)) $ bound_arg args arg;
   766       val t1 = mk_trp (%:p $ con_app2 con (bound_arg args) args);
   767       val t2 = lift ind_hyp (filter is_rec args, t1);
   768       val t3 = lift_defined (bound_arg (map vname args)) (nonlazy args, t2);
   769     in Library.foldr mk_All (map vname args, t3) end;
   770 
   771   fun one_eq ((p, cons), concl) =
   772     mk_trp (%:p $ UU) ===> Logic.list_implies (map (one_con p) cons, concl);
   773 
   774   fun ind_term concf = Library.foldr one_eq
   775     (mapn (fn n => fn x => (P_name n, x)) 1 conss,
   776      mk_trp (foldr1 mk_conj (mapn concf 1 dnames)));
   777   val take_ss = HOL_ss addsimps take_rews;
   778   fun quant_tac ctxt i = EVERY
   779     (mapn (fn n => fn _ => res_inst_tac ctxt [(("x", 0), x_name n)] spec i) 1 dnames);
   780 
   781   fun ind_prems_tac prems = EVERY
   782     (maps (fn cons =>
   783       (resolve_tac prems 1 ::
   784         maps (fn (_,args) => 
   785           resolve_tac prems 1 ::
   786           map (K(atac 1)) (nonlazy args) @
   787           map (K(atac 1)) (filter is_rec args))
   788         cons))
   789       conss);
   790   local 
   791     (* check whether every/exists constructor of the n-th part of the equation:
   792        it has a possibly indirectly recursive argument that isn't/is possibly 
   793        indirectly lazy *)
   794     fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
   795           is_rec arg andalso not(rec_of arg mem ns) andalso
   796           ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
   797             rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
   798               (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
   799           ) o snd) cons;
   800     fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
   801     fun warn (n,cons) =
   802       if all_rec_to [] false (n,cons)
   803       then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
   804       else false;
   805     fun lazy_rec_to ns = rec_to exists I  lazy_rec_to ns;
   806 
   807   in
   808     val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   809     val is_emptys = map warn n__eqs;
   810     val is_finite = forall (not o lazy_rec_to [] false) n__eqs;
   811   end;
   812 in (* local *)
   813   val _ = trace " Proving finite_ind...";
   814   val finite_ind =
   815     let
   816       fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
   817       val goal = ind_term concf;
   818 
   819       fun tacf {prems, context} =
   820         let
   821           val tacs1 = [
   822             quant_tac context 1,
   823             simp_tac HOL_ss 1,
   824             InductTacs.induct_tac context [[SOME "n"]] 1,
   825             simp_tac (take_ss addsimps prems) 1,
   826             TRY (safe_tac HOL_cs)];
   827           fun arg_tac arg =
   828             case_UU_tac context (prems @ con_rews) 1
   829               (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
   830           fun con_tacs (con, args) = 
   831             asm_simp_tac take_ss 1 ::
   832             map arg_tac (filter is_nonlazy_rec args) @
   833             [resolve_tac prems 1] @
   834             map (K (atac 1)) (nonlazy args) @
   835             map (K (etac spec 1)) (filter is_rec args);
   836           fun cases_tacs (cons, cases) =
   837             res_inst_tac context [(("x", 0), "x")] cases 1 ::
   838             asm_simp_tac (take_ss addsimps prems) 1 ::
   839             maps con_tacs cons;
   840         in
   841           tacs1 @ maps cases_tacs (conss ~~ cases)
   842         end;
   843     in pg'' thy [] goal tacf
   844        handle ERROR _ => (warning "Proof of finite_ind failed."; TrueI)
   845     end;
   846 
   847   val _ = trace " Proving take_lemmas...";
   848   val take_lemmas =
   849     let
   850       fun take_lemma n (dn, ax_reach) =
   851         let
   852           val lhs = dc_take dn $ Bound 0 `%(x_name n);
   853           val rhs = dc_take dn $ Bound 0 `%(x_name n^"'");
   854           val concl = mk_trp (%:(x_name n) === %:(x_name n^"'"));
   855           val goal = mk_All ("n", mk_trp (lhs === rhs)) ===> concl;
   856           val rules = [contlub_fst RS contlubE RS ssubst,
   857                        contlub_snd RS contlubE RS ssubst];
   858           fun tacf {prems, context} = [
   859             res_inst_tac context [(("t", 0), x_name n    )] (ax_reach RS subst) 1,
   860             res_inst_tac context [(("t", 0), x_name n^"'")] (ax_reach RS subst) 1,
   861             stac fix_def2 1,
   862             REPEAT (CHANGED
   863               (resolve_tac rules 1 THEN chain_tac 1)),
   864             stac contlub_cfun_fun 1,
   865             stac contlub_cfun_fun 2,
   866             rtac lub_equal 3,
   867             chain_tac 1,
   868             rtac allI 1,
   869             resolve_tac prems 1];
   870         in pg'' thy axs_take_def goal tacf end;
   871     in mapn take_lemma 1 (dnames ~~ axs_reach) end;
   872 
   873 (* ----- theorems concerning finiteness and induction ----------------------- *)
   874 
   875   val _ = trace " Proving finites, ind...";
   876   val (finites, ind) =
   877   (
   878     if is_finite
   879     then (* finite case *)
   880       let 
   881         fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %:"x" === %:"x");
   882         fun dname_lemma dn =
   883           let
   884             val prem1 = mk_trp (defined (%:"x"));
   885             val disj1 = mk_all ("n", dc_take dn $ Bound 0 ` %:"x" === UU);
   886             val prem2 = mk_trp (mk_disj (disj1, take_enough dn));
   887             val concl = mk_trp (take_enough dn);
   888             val goal = prem1 ===> prem2 ===> concl;
   889             val tacs = [
   890               etac disjE 1,
   891               etac notE 1,
   892               resolve_tac take_lemmas 1,
   893               asm_simp_tac take_ss 1,
   894               atac 1];
   895           in pg [] goal (K tacs) end;
   896         val _ = trace " Proving finite_lemmas1a";
   897         val finite_lemmas1a = map dname_lemma dnames;
   898  
   899         val _ = trace " Proving finite_lemma1b";
   900         val finite_lemma1b =
   901           let
   902             fun mk_eqn n ((dn, args), _) =
   903               let
   904                 val disj1 = dc_take dn $ Bound 1 ` Bound 0 === UU;
   905                 val disj2 = dc_take dn $ Bound 1 ` Bound 0 === Bound 0;
   906               in
   907                 mk_constrainall
   908                   (x_name n, Type (dn,args), mk_disj (disj1, disj2))
   909               end;
   910             val goal =
   911               mk_trp (mk_all ("n", foldr1 mk_conj (mapn mk_eqn 1 eqs)));
   912             fun arg_tacs ctxt vn = [
   913               eres_inst_tac ctxt [(("x", 0), vn)] all_dupE 1,
   914               etac disjE 1,
   915               asm_simp_tac (HOL_ss addsimps con_rews) 1,
   916               asm_simp_tac take_ss 1];
   917             fun con_tacs ctxt (con, args) =
   918               asm_simp_tac take_ss 1 ::
   919               maps (arg_tacs ctxt) (nonlazy_rec args);
   920             fun foo_tacs ctxt n (cons, cases) =
   921               simp_tac take_ss 1 ::
   922               rtac allI 1 ::
   923               res_inst_tac ctxt [(("x", 0), x_name n)] cases 1 ::
   924               asm_simp_tac take_ss 1 ::
   925               maps (con_tacs ctxt) cons;
   926             fun tacs ctxt =
   927               rtac allI 1 ::
   928               InductTacs.induct_tac ctxt [[SOME "n"]] 1 ::
   929               simp_tac take_ss 1 ::
   930               TRY (safe_tac (empty_cs addSEs [conjE] addSIs [conjI])) ::
   931               flat (mapn (foo_tacs ctxt) 1 (conss ~~ cases));
   932           in pg [] goal tacs end;
   933 
   934         fun one_finite (dn, l1b) =
   935           let
   936             val goal = mk_trp (%%:(dn^"_finite") $ %:"x");
   937             fun tacs ctxt = [
   938               case_UU_tac ctxt take_rews 1 "x",
   939               eresolve_tac finite_lemmas1a 1,
   940               step_tac HOL_cs 1,
   941               step_tac HOL_cs 1,
   942               cut_facts_tac [l1b] 1,
   943               fast_tac HOL_cs 1];
   944           in pg axs_finite_def goal tacs end;
   945 
   946         val _ = trace " Proving finites";
   947         val finites = map one_finite (dnames ~~ atomize global_ctxt finite_lemma1b);
   948         val _ = trace " Proving ind";
   949         val ind =
   950           let
   951             fun concf n dn = %:(P_name n) $ %:(x_name n);
   952             fun tacf {prems, context} =
   953               let
   954                 fun finite_tacs (finite, fin_ind) = [
   955                   rtac(rewrite_rule axs_finite_def finite RS exE)1,
   956                   etac subst 1,
   957                   rtac fin_ind 1,
   958                   ind_prems_tac prems];
   959               in
   960                 TRY (safe_tac HOL_cs) ::
   961                 maps finite_tacs (finites ~~ atomize global_ctxt finite_ind)
   962               end;
   963           in pg'' thy [] (ind_term concf) tacf end;
   964       in (finites, ind) end (* let *)
   965 
   966     else (* infinite case *)
   967       let
   968         fun one_finite n dn =
   969           read_instantiate global_ctxt [(("P", 0), dn ^ "_finite " ^ x_name n)] excluded_middle;
   970         val finites = mapn one_finite 1 dnames;
   971 
   972         val goal =
   973           let
   974             fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
   975             fun concf n dn = %:(P_name n) $ %:(x_name n);
   976           in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
   977         val cont_rules =
   978             [cont_id, cont_const, cont2cont_Rep_CFun,
   979              cont2cont_fst, cont2cont_snd];
   980         fun tacf {prems, context} =
   981           map (fn ax_reach => rtac (ax_reach RS subst) 1) axs_reach @ [
   982           quant_tac context 1,
   983           rtac (adm_impl_admw RS wfix_ind) 1,
   984           REPEAT_DETERM (rtac adm_all 1),
   985           REPEAT_DETERM (
   986             TRY (rtac adm_conj 1) THEN 
   987             rtac adm_subst 1 THEN 
   988             REPEAT (resolve_tac cont_rules 1) THEN
   989             resolve_tac prems 1),
   990           strip_tac 1,
   991           rtac (rewrite_rule axs_take_def finite_ind) 1,
   992           ind_prems_tac prems];
   993         val ind = (pg'' thy [] goal tacf
   994           handle ERROR _ =>
   995             (warning "Cannot prove infinite induction rule"; TrueI));
   996       in (finites, ind) end
   997   )
   998       handle THM _ =>
   999              (warning "Induction proofs failed (THM raised)."; ([], TrueI))
  1000            | ERROR _ =>
  1001              (warning "Cannot prove induction rule"; ([], TrueI));
  1002 
  1003 
  1004 end; (* local *)
  1005 
  1006 (* ----- theorem concerning coinduction ------------------------------------- *)
  1007 
  1008 local
  1009   val xs = mapn (fn n => K (x_name n)) 1 dnames;
  1010   fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
  1011   val take_ss = HOL_ss addsimps take_rews;
  1012   val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
  1013   val _ = trace " Proving coind_lemma...";
  1014   val coind_lemma =
  1015     let
  1016       fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
  1017       fun mk_eqn n dn =
  1018         (dc_take dn $ %:"n" ` bnd_arg n 0) ===
  1019         (dc_take dn $ %:"n" ` bnd_arg n 1);
  1020       fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
  1021       val goal =
  1022         mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
  1023           Library.foldr mk_all2 (xs,
  1024             Library.foldr mk_imp (mapn mk_prj 0 dnames,
  1025               foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
  1026       fun x_tacs ctxt n x = [
  1027         rotate_tac (n+1) 1,
  1028         etac all2E 1,
  1029         eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
  1030         TRY (safe_tac HOL_cs),
  1031         REPEAT (CHANGED (asm_simp_tac take_ss 1))];
  1032       fun tacs ctxt = [
  1033         rtac impI 1,
  1034         InductTacs.induct_tac ctxt [[SOME "n"]] 1,
  1035         simp_tac take_ss 1,
  1036         safe_tac HOL_cs] @
  1037         flat (mapn (x_tacs ctxt) 0 xs);
  1038     in pg [ax_bisim_def] goal tacs end;
  1039 in
  1040   val _ = trace " Proving coind...";
  1041   val coind = 
  1042     let
  1043       fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
  1044       fun mk_eqn x = %:x === %:(x^"'");
  1045       val goal =
  1046         mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
  1047           Logic.list_implies (mapn mk_prj 0 xs,
  1048             mk_trp (foldr1 mk_conj (map mk_eqn xs)));
  1049       val tacs =
  1050         TRY (safe_tac HOL_cs) ::
  1051         maps (fn take_lemma => [
  1052           rtac take_lemma 1,
  1053           cut_facts_tac [coind_lemma] 1,
  1054           fast_tac HOL_cs 1])
  1055         take_lemmas;
  1056     in pg [] goal (K tacs) end;
  1057 end; (* local *)
  1058 
  1059 val inducts = Project_Rule.projections (ProofContext.init thy) ind;
  1060 fun ind_rule (dname, rule) = ((Binding.empty, [rule]), [Induct.induct_type dname]);
  1061 val induct_failed = (Thm.prop_of ind = Thm.prop_of TrueI);
  1062 
  1063 in thy |> Sign.add_path comp_dnam
  1064        |> snd o PureThy.add_thmss [
  1065            ((Binding.name "take_rews"  , take_rews   ), [Simplifier.simp_add]),
  1066            ((Binding.name "take_lemmas", take_lemmas ), []),
  1067            ((Binding.name "finites"    , finites     ), []),
  1068            ((Binding.name "finite_ind" , [finite_ind]), []),
  1069            ((Binding.name "ind"        , [ind]       ), []),
  1070            ((Binding.name "coind"      , [coind]     ), [])]
  1071        |> (if induct_failed then I
  1072            else snd o PureThy.add_thmss (map ind_rule (dnames ~~ inducts)))
  1073        |> Sign.parent_path |> pair take_rews
  1074 end; (* let *)
  1075 end; (* local *)
  1076 end; (* struct *)