src/HOLCF/Tools/Domain/domain_theorems.ML
author huffman
Wed Feb 24 14:20:07 2010 -0800 (2010-02-24)
changeset 35443 2e0f9516947e
parent 35288 aa7da51ae1ef
child 35444 73f645fdd4ff
permissions -rw-r--r--
change domain package's treatment of variable names in theorems to be like datatype package
     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 val adm_impl_admw = @{thm adm_impl_admw};
    28 val adm_all = @{thm adm_all};
    29 val adm_conj = @{thm adm_conj};
    30 val adm_subst = @{thm adm_subst};
    31 val antisym_less_inverse = @{thm below_antisym_inverse};
    32 val beta_cfun = @{thm beta_cfun};
    33 val cfun_arg_cong = @{thm cfun_arg_cong};
    34 val ch2ch_fst = @{thm ch2ch_fst};
    35 val ch2ch_snd = @{thm ch2ch_snd};
    36 val ch2ch_Rep_CFunL = @{thm ch2ch_Rep_CFunL};
    37 val ch2ch_Rep_CFunR = @{thm ch2ch_Rep_CFunR};
    38 val chain_iterate = @{thm chain_iterate};
    39 val compact_ONE = @{thm compact_ONE};
    40 val compact_sinl = @{thm compact_sinl};
    41 val compact_sinr = @{thm compact_sinr};
    42 val compact_spair = @{thm compact_spair};
    43 val compact_up = @{thm compact_up};
    44 val contlub_cfun_arg = @{thm contlub_cfun_arg};
    45 val contlub_cfun_fun = @{thm contlub_cfun_fun};
    46 val contlub_fst = @{thm contlub_fst};
    47 val contlub_snd = @{thm contlub_snd};
    48 val contlubE = @{thm contlubE};
    49 val cont_const = @{thm cont_const};
    50 val cont_id = @{thm cont_id};
    51 val cont2cont_fst = @{thm cont2cont_fst};
    52 val cont2cont_snd = @{thm cont2cont_snd};
    53 val cont2cont_Rep_CFun = @{thm cont2cont_Rep_CFun};
    54 val fix_def2 = @{thm fix_def2};
    55 val injection_eq = @{thm injection_eq};
    56 val injection_less = @{thm injection_below};
    57 val lub_equal = @{thm lub_equal};
    58 val monofun_cfun_arg = @{thm monofun_cfun_arg};
    59 val retraction_strict = @{thm retraction_strict};
    60 val spair_eq = @{thm spair_eq};
    61 val spair_less = @{thm spair_below};
    62 val sscase1 = @{thm sscase1};
    63 val ssplit1 = @{thm ssplit1};
    64 val strictify1 = @{thm strictify1};
    65 val wfix_ind = @{thm wfix_ind};
    66 
    67 val iso_intro       = @{thm iso.intro};
    68 val iso_abs_iso     = @{thm iso.abs_iso};
    69 val iso_rep_iso     = @{thm iso.rep_iso};
    70 val iso_abs_strict  = @{thm iso.abs_strict};
    71 val iso_rep_strict  = @{thm iso.rep_strict};
    72 val iso_abs_defin'  = @{thm iso.abs_defin'};
    73 val iso_rep_defin'  = @{thm iso.rep_defin'};
    74 val iso_abs_defined = @{thm iso.abs_defined};
    75 val iso_rep_defined = @{thm iso.rep_defined};
    76 val iso_compact_abs = @{thm iso.compact_abs};
    77 val iso_compact_rep = @{thm iso.compact_rep};
    78 val iso_iso_swap    = @{thm iso.iso_swap};
    79 
    80 val exh_start = @{thm exh_start};
    81 val ex_defined_iffs = @{thms ex_defined_iffs};
    82 val exh_casedist0 = @{thm exh_casedist0};
    83 val exh_casedists = @{thms exh_casedists};
    84 
    85 open Domain_Library;
    86 infixr 0 ===>;
    87 infixr 0 ==>;
    88 infix 0 == ; 
    89 infix 1 ===;
    90 infix 1 ~= ;
    91 infix 1 <<;
    92 infix 1 ~<<;
    93 infix 9 `   ;
    94 infix 9 `% ;
    95 infix 9 `%%;
    96 infixr 9 oo;
    97 
    98 (* ----- general proof facilities ------------------------------------------- *)
    99 
   100 fun legacy_infer_term thy t =
   101   let val ctxt = ProofContext.set_mode ProofContext.mode_schematic (ProofContext.init thy)
   102   in singleton (Syntax.check_terms ctxt) (Sign.intern_term thy t) end;
   103 
   104 fun pg'' thy defs t tacs =
   105   let
   106     val t' = legacy_infer_term thy t;
   107     val asms = Logic.strip_imp_prems t';
   108     val prop = Logic.strip_imp_concl t';
   109     fun tac {prems, context} =
   110       rewrite_goals_tac defs THEN
   111       EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
   112   in Goal.prove_global thy [] asms prop tac end;
   113 
   114 fun pg' thy defs t tacsf =
   115   let
   116     fun tacs {prems, context} =
   117       if null prems then tacsf context
   118       else cut_facts_tac prems 1 :: tacsf context;
   119   in pg'' thy defs t tacs end;
   120 
   121 (* FIXME!!!!!!!!! *)
   122 (* We should NEVER re-parse variable names as strings! *)
   123 (* The names can conflict with existing constants or other syntax! *)
   124 fun case_UU_tac ctxt rews i v =
   125   InductTacs.case_tac ctxt (v^"=UU") i THEN
   126   asm_simp_tac (HOLCF_ss addsimps rews) i;
   127 
   128 val chain_tac =
   129   REPEAT_DETERM o resolve_tac 
   130     [chain_iterate, ch2ch_Rep_CFunR, ch2ch_Rep_CFunL, ch2ch_fst, ch2ch_snd];
   131 
   132 (* ----- general proofs ----------------------------------------------------- *)
   133 
   134 val all2E = @{lemma "!x y . P x y ==> (P x y ==> R) ==> R" by simp}
   135 
   136 val dist_eqI = @{lemma "!!x::'a::po. ~ x << y ==> x ~= y" by (blast dest!: below_antisym_inverse)}
   137 
   138 fun theorems (((dname, _), cons) : eq, eqs : eq list) thy =
   139 let
   140 
   141 val _ = message ("Proving isomorphism properties of domain "^dname^" ...");
   142 val pg = pg' thy;
   143 val map_tab = Domain_Isomorphism.get_map_tab thy;
   144 
   145 
   146 (* ----- getting the axioms and definitions --------------------------------- *)
   147 
   148 local
   149   fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
   150 in
   151   val ax_abs_iso  = ga "abs_iso"  dname;
   152   val ax_rep_iso  = ga "rep_iso"  dname;
   153   val ax_when_def = ga "when_def" dname;
   154   fun get_def mk_name (con, _, _) = ga (mk_name con^"_def") dname;
   155   val axs_con_def = map (get_def extern_name) cons;
   156   val axs_dis_def = map (get_def dis_name) cons;
   157   val axs_mat_def = map (get_def mat_name) cons;
   158   val axs_pat_def = map (get_def pat_name) cons;
   159   val axs_sel_def =
   160     let
   161       fun def_of_sel sel = ga (sel^"_def") dname;
   162       fun def_of_arg arg = Option.map def_of_sel (sel_of arg);
   163       fun defs_of_con (_, _, args) = map_filter def_of_arg args;
   164     in
   165       maps defs_of_con cons
   166     end;
   167   val ax_copy_def = ga "copy_def" dname;
   168 end; (* local *)
   169 
   170 (* ----- theorems concerning the isomorphism -------------------------------- *)
   171 
   172 val dc_abs  = %%:(dname^"_abs");
   173 val dc_rep  = %%:(dname^"_rep");
   174 val dc_copy = %%:(dname^"_copy");
   175 val x_name = "x";
   176 
   177 val iso_locale = iso_intro OF [ax_abs_iso, ax_rep_iso];
   178 val abs_strict = ax_rep_iso RS (allI RS retraction_strict);
   179 val rep_strict = ax_abs_iso RS (allI RS retraction_strict);
   180 val abs_defin' = iso_locale RS iso_abs_defin';
   181 val rep_defin' = iso_locale RS iso_rep_defin';
   182 val iso_rews = map Drule.export_without_context [ax_abs_iso, ax_rep_iso, abs_strict, rep_strict];
   183 
   184 (* ----- generating beta reduction rules from definitions-------------------- *)
   185 
   186 val _ = trace " Proving beta reduction rules...";
   187 
   188 local
   189   fun arglist (Const _ $ Abs (s, _, t)) =
   190     let
   191       val (vars,body) = arglist t;
   192     in (s :: vars, body) end
   193     | arglist t = ([], t);
   194   fun bind_fun vars t = Library.foldr mk_All (vars, t);
   195   fun bound_vars 0 = []
   196     | bound_vars i = Bound (i-1) :: bound_vars (i - 1);
   197 in
   198   fun appl_of_def def =
   199     let
   200       val (_ $ con $ lam) = concl_of def;
   201       val (vars, rhs) = arglist lam;
   202       val lhs = list_ccomb (con, bound_vars (length vars));
   203       val appl = bind_fun vars (lhs == rhs);
   204       val cs = ContProc.cont_thms lam;
   205       val betas = map (fn c => mk_meta_eq (c RS beta_cfun)) cs;
   206     in pg (def::betas) appl (K [rtac reflexive_thm 1]) end;
   207 end;
   208 
   209 val _ = trace "Proving when_appl...";
   210 val when_appl = appl_of_def ax_when_def;
   211 val _ = trace "Proving con_appls...";
   212 val con_appls = map appl_of_def axs_con_def;
   213 
   214 local
   215   fun arg2typ n arg =
   216     let val t = TVar (("'a", n), pcpoS)
   217     in (n + 1, if is_lazy arg then mk_uT t else t) end;
   218 
   219   fun args2typ n [] = (n, oneT)
   220     | args2typ n [arg] = arg2typ n arg
   221     | args2typ n (arg::args) =
   222     let
   223       val (n1, t1) = arg2typ n arg;
   224       val (n2, t2) = args2typ n1 args
   225     in (n2, mk_sprodT (t1, t2)) end;
   226 
   227   fun cons2typ n [] = (n,oneT)
   228     | cons2typ n [con] = args2typ n (third con)
   229     | cons2typ n (con::cons) =
   230     let
   231       val (n1, t1) = args2typ n (third con);
   232       val (n2, t2) = cons2typ n1 cons
   233     in (n2, mk_ssumT (t1, t2)) end;
   234 in
   235   fun cons2ctyp cons = ctyp_of thy (snd (cons2typ 1 cons));
   236 end;
   237 
   238 local
   239   val iso_swap = iso_locale RS iso_iso_swap;
   240   fun one_con (con, _, args) =
   241     let
   242       val vns = Name.variant_list ["x"] (map vname args);
   243       val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
   244       val eqn = %:x_name === con_app2 con %: vns;
   245       val conj = foldr1 mk_conj (eqn :: map (defined o %:) nonlazy_vns);
   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.export_without_context (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 = first 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                         (* FIXME! case_UU_tac *)
   467         then [case_UU_tac ctxt (when_rews @ con_stricts) 1 vnn]
   468         else [];
   469       val tacs2 = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   470     in pg axs_sel_def goal (fn ctxt => (tacs1 ctxt @ tacs2)) end;
   471 
   472   fun sel_app_diff c n sel (con, args) =
   473     let
   474       val nlas = nonlazy args;
   475       val goal = mk_trp (%%:sel ` con_app con args === UU);
   476                         (* FIXME! case_UU_tac *)
   477       fun tacs1 ctxt = map (case_UU_tac ctxt (when_rews @ con_stricts) 1) nlas;
   478       val tacs2 = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   479     in pg axs_sel_def goal (fn ctxt => (tacs1 ctxt @ tacs2)) end;
   480 
   481   fun sel_app c n sel (con, _, args) =
   482     if con = c
   483     then sel_app_same c n sel (con, args)
   484     else sel_app_diff c n sel (con, args);
   485 
   486   fun one_sel c n sel = map (sel_app c n sel) cons;
   487   fun one_sel' c n arg = Option.map (one_sel c n) (sel_of arg);
   488   fun one_con (c, _, args) =
   489     flat (map_filter I (mapn (one_sel' c) 0 args));
   490 in
   491   val _ = trace " Proving sel_apps...";
   492   val sel_apps = maps one_con cons;
   493 end;
   494 
   495 local
   496   fun sel_defin sel =
   497     let
   498       val goal = defined (%:x_name) ==> defined (%%:sel`%x_name);
   499       val tacs = [
   500         rtac casedist 1,
   501         contr_tac 1,
   502         DETERM_UNTIL_SOLVED (CHANGED
   503           (asm_simp_tac (HOLCF_ss addsimps sel_apps) 1))];
   504     in pg [] goal (K tacs) end;
   505 in
   506   val _ = trace " Proving sel_defins...";
   507   val sel_defins =
   508     if length cons = 1
   509     then map_filter (fn arg => Option.map sel_defin (sel_of arg))
   510                  (filter_out is_lazy (third (hd cons)))
   511     else [];
   512 end;
   513 
   514 val sel_rews = sel_stricts @ sel_defins @ sel_apps;
   515 
   516 val _ = trace " Proving dist_les...";
   517 val dist_les =
   518   let
   519     fun dist (con1, args1) (con2, args2) =
   520       let
   521         fun iff_disj (t, []) = HOLogic.mk_not t
   522           | iff_disj (t, ts) = t === foldr1 HOLogic.mk_disj ts;
   523         val lhs = con_app con1 args1 << con_app con2 args2;
   524         val rhss = map (fn x => %:x === UU) (nonlazy args1);
   525         val goal = mk_trp (iff_disj (lhs, rhss));
   526         val rule1 = iso_locale RS @{thm iso.abs_below};
   527         val rules = rule1 :: @{thms con_below_iff_rules};
   528         val tacs = [simp_tac (HOL_ss addsimps rules) 1];
   529       in pg con_appls goal (K tacs) end;
   530 
   531     fun distinct (con1, _, args1) (con2, _, args2) =
   532         let
   533           val arg1 = (con1, args1);
   534           val arg2 =
   535             (con2, ListPair.map (fn (arg,vn) => upd_vname (K vn) arg)
   536               (args2, Name.variant_list (map vname args1) (map vname args2)));
   537         in [dist arg1 arg2, dist arg2 arg1] end;
   538     fun distincts []      = []
   539       | distincts (c::cs) = maps (distinct c) cs @ distincts cs;
   540   in distincts cons end;
   541 
   542 val _ = trace " Proving dist_eqs...";
   543 val dist_eqs =
   544   let
   545     fun dist (con1, args1) (con2, args2) =
   546       let
   547         fun iff_disj (t, [], us) = HOLogic.mk_not t
   548           | iff_disj (t, ts, []) = HOLogic.mk_not t
   549           | iff_disj (t, ts, us) =
   550             let
   551               val disj1 = foldr1 HOLogic.mk_disj ts;
   552               val disj2 = foldr1 HOLogic.mk_disj us;
   553             in t === HOLogic.mk_conj (disj1, disj2) end;
   554         val lhs = con_app con1 args1 === con_app con2 args2;
   555         val rhss1 = map (fn x => %:x === UU) (nonlazy args1);
   556         val rhss2 = map (fn x => %:x === UU) (nonlazy args2);
   557         val goal = mk_trp (iff_disj (lhs, rhss1, rhss2));
   558         val rule1 = iso_locale RS @{thm iso.abs_eq};
   559         val rules = rule1 :: @{thms con_eq_iff_rules};
   560         val tacs = [simp_tac (HOL_ss addsimps rules) 1];
   561       in pg con_appls goal (K tacs) end;
   562 
   563     fun distinct (con1, _, args1) (con2, _, args2) =
   564         let
   565           val arg1 = (con1, args1);
   566           val arg2 =
   567             (con2, ListPair.map (fn (arg,vn) => upd_vname (K vn) arg)
   568               (args2, Name.variant_list (map vname args1) (map vname args2)));
   569         in [dist arg1 arg2, dist arg2 arg1] end;
   570     fun distincts []      = []
   571       | distincts (c::cs) = maps (distinct c) cs @ distincts cs;
   572   in distincts cons end;
   573 
   574 local 
   575   fun pgterm rel con args =
   576     let
   577       fun append s = upd_vname (fn v => v^s);
   578       val (largs, rargs) = (args, map (append "'") args);
   579       val concl =
   580         foldr1 mk_conj (ListPair.map rel (map %# largs, map %# rargs));
   581       val prem = rel (con_app con largs, con_app con rargs);
   582       val sargs = case largs of [_] => [] | _ => nonlazy args;
   583       val prop = lift_defined %: (sargs, mk_trp (prem === concl));
   584     in pg con_appls prop end;
   585   val cons' = filter (fn (_, _, args) => args<>[]) cons;
   586 in
   587   val _ = trace " Proving inverts...";
   588   val inverts =
   589     let
   590       val abs_less = ax_abs_iso RS (allI RS injection_less);
   591       val tacs =
   592         [asm_full_simp_tac (HOLCF_ss addsimps [abs_less, spair_less]) 1];
   593     in map (fn (con, _, args) => pgterm (op <<) con args (K tacs)) cons' end;
   594 
   595   val _ = trace " Proving injects...";
   596   val injects =
   597     let
   598       val abs_eq = ax_abs_iso RS (allI RS injection_eq);
   599       val tacs = [asm_full_simp_tac (HOLCF_ss addsimps [abs_eq, spair_eq]) 1];
   600     in map (fn (con, _, args) => pgterm (op ===) con args (K tacs)) cons' end;
   601 end;
   602 
   603 (* ----- theorems concerning one induction step ----------------------------- *)
   604 
   605 val copy_strict =
   606   let
   607     val _ = trace " Proving copy_strict...";
   608     val goal = mk_trp (strict (dc_copy `% "f"));
   609     val rules = [abs_strict, rep_strict] @ @{thms domain_map_stricts};
   610     val tacs = [asm_simp_tac (HOLCF_ss addsimps rules) 1];
   611   in
   612     SOME (pg [ax_copy_def] goal (K tacs))
   613     handle
   614       THM (s, _, _) => (trace s; NONE)
   615     | ERROR s => (trace s; NONE)
   616   end;
   617 
   618 local
   619   fun copy_app (con, _, args) =
   620     let
   621       val lhs = dc_copy`%"f"`(con_app con args);
   622       fun one_rhs arg =
   623           if Datatype_Aux.is_rec_type (dtyp_of arg)
   624           then Domain_Axioms.copy_of_dtyp map_tab
   625                  (proj (%:"f") eqs) (dtyp_of arg) ` (%# arg)
   626           else (%# arg);
   627       val rhs = con_app2 con one_rhs args;
   628       fun is_rec arg = Datatype_Aux.is_rec_type (dtyp_of arg);
   629       fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
   630       fun nonlazy_rec args = map vname (filter is_nonlazy_rec args);
   631       val goal = lift_defined %: (nonlazy_rec args, mk_trp (lhs === rhs));
   632       val args' = filter_out (fn a => is_rec a orelse is_lazy a) args;
   633       val stricts = abs_strict :: rep_strict :: @{thms domain_map_stricts};
   634                         (* FIXME! case_UU_tac *)
   635       fun tacs1 ctxt = map (case_UU_tac ctxt stricts 1 o vname) args';
   636       val rules = [ax_abs_iso] @ @{thms domain_map_simps};
   637       val tacs2 = [asm_simp_tac (HOLCF_ss addsimps rules) 1];
   638     in pg (ax_copy_def::con_appls) goal (fn ctxt => (tacs1 ctxt @ tacs2)) end;
   639 in
   640   val _ = trace " Proving copy_apps...";
   641   val copy_apps = map copy_app cons;
   642 end;
   643 
   644 local
   645   fun one_strict (con, _, args) = 
   646     let
   647       val goal = mk_trp (dc_copy`UU`(con_app con args) === UU);
   648       val rews = the_list copy_strict @ copy_apps @ con_rews;
   649                         (* FIXME! case_UU_tac *)
   650       fun tacs ctxt = map (case_UU_tac ctxt rews 1) (nonlazy args) @
   651         [asm_simp_tac (HOLCF_ss addsimps rews) 1];
   652     in
   653       SOME (pg [] goal tacs)
   654       handle
   655         THM (s, _, _) => (trace s; NONE)
   656       | ERROR s => (trace s; NONE)
   657     end;
   658 
   659   fun has_nonlazy_rec (_, _, args) = exists is_nonlazy_rec args;
   660 in
   661   val _ = trace " Proving copy_stricts...";
   662   val copy_stricts = map_filter one_strict (filter has_nonlazy_rec cons);
   663 end;
   664 
   665 val copy_rews = the_list copy_strict @ copy_apps @ copy_stricts;
   666 
   667 in
   668   thy
   669     |> Sign.add_path (Long_Name.base_name dname)
   670     |> snd o PureThy.add_thmss [
   671         ((Binding.name "iso_rews"  , iso_rews    ), [Simplifier.simp_add]),
   672         ((Binding.name "exhaust"   , [exhaust]   ), []),
   673         ((Binding.name "casedist"  , [casedist]  ), [Induct.cases_type dname]),
   674         ((Binding.name "when_rews" , when_rews   ), [Simplifier.simp_add]),
   675         ((Binding.name "compacts"  , con_compacts), [Simplifier.simp_add]),
   676         ((Binding.name "con_rews"  , con_rews    ),
   677          [Simplifier.simp_add, Fixrec.fixrec_simp_add]),
   678         ((Binding.name "sel_rews"  , sel_rews    ), [Simplifier.simp_add]),
   679         ((Binding.name "dis_rews"  , dis_rews    ), [Simplifier.simp_add]),
   680         ((Binding.name "pat_rews"  , pat_rews    ), [Simplifier.simp_add]),
   681         ((Binding.name "dist_les"  , dist_les    ), [Simplifier.simp_add]),
   682         ((Binding.name "dist_eqs"  , dist_eqs    ), [Simplifier.simp_add]),
   683         ((Binding.name "inverts"   , inverts     ), [Simplifier.simp_add]),
   684         ((Binding.name "injects"   , injects     ), [Simplifier.simp_add]),
   685         ((Binding.name "copy_rews" , copy_rews   ), [Simplifier.simp_add]),
   686         ((Binding.name "match_rews", mat_rews    ),
   687          [Simplifier.simp_add, Fixrec.fixrec_simp_add])]
   688     |> Sign.parent_path
   689     |> pair (iso_rews @ when_rews @ con_rews @ sel_rews @ dis_rews @
   690         pat_rews @ dist_les @ dist_eqs @ copy_rews)
   691 end; (* let *)
   692 
   693 fun comp_theorems (comp_dnam, eqs: eq list) thy =
   694 let
   695 val global_ctxt = ProofContext.init thy;
   696 val map_tab = Domain_Isomorphism.get_map_tab thy;
   697 
   698 val dnames = map (fst o fst) eqs;
   699 val conss  = map  snd        eqs;
   700 val comp_dname = Sign.full_bname thy comp_dnam;
   701 
   702 val _ = message ("Proving induction properties of domain "^comp_dname^" ...");
   703 val pg = pg' thy;
   704 
   705 (* ----- getting the composite axiom and definitions ------------------------ *)
   706 
   707 local
   708   fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
   709 in
   710   val axs_reach      = map (ga "reach"     ) dnames;
   711   val axs_take_def   = map (ga "take_def"  ) dnames;
   712   val axs_finite_def = map (ga "finite_def") dnames;
   713   val ax_copy2_def   =      ga "copy_def"  comp_dnam;
   714   val ax_bisim_def   =      ga "bisim_def" comp_dnam;
   715 end;
   716 
   717 local
   718   fun gt  s dn = PureThy.get_thm  thy (dn ^ "." ^ s);
   719   fun gts s dn = PureThy.get_thms thy (dn ^ "." ^ s);
   720 in
   721   val cases = map (gt  "casedist" ) dnames;
   722   val con_rews  = maps (gts "con_rews" ) dnames;
   723   val copy_rews = maps (gts "copy_rews") dnames;
   724 end;
   725 
   726 fun dc_take dn = %%:(dn^"_take");
   727 val x_name = idx_name dnames "x"; 
   728 val P_name = idx_name dnames "P";
   729 val n_eqs = length eqs;
   730 
   731 (* ----- theorems concerning finite approximation and finite induction ------ *)
   732 
   733 local
   734   val iterate_Cprod_ss = global_simpset_of @{theory Fix};
   735   val copy_con_rews  = copy_rews @ con_rews;
   736   val copy_take_defs =
   737     (if n_eqs = 1 then [] else [ax_copy2_def]) @ axs_take_def;
   738   val _ = trace " Proving take_stricts...";
   739   fun one_take_strict ((dn, args), _) =
   740     let
   741       val goal = mk_trp (strict (dc_take dn $ %:"n"));
   742       val rules = [
   743         @{thm monofun_fst [THEN monofunE]},
   744         @{thm monofun_snd [THEN monofunE]}];
   745       val tacs = [
   746         rtac @{thm UU_I} 1,
   747         rtac @{thm below_eq_trans} 1,
   748         resolve_tac axs_reach 2,
   749         rtac @{thm monofun_cfun_fun} 1,
   750         REPEAT (resolve_tac rules 1),
   751         rtac @{thm iterate_below_fix} 1];
   752     in pg axs_take_def goal (K tacs) end;
   753   val take_stricts = map one_take_strict eqs;
   754   fun take_0 n dn =
   755     let
   756       val goal = mk_trp ((dc_take dn $ @{term "0::nat"}) `% x_name n === UU);
   757     in pg axs_take_def goal (K [simp_tac iterate_Cprod_ss 1]) end;
   758   val take_0s = mapn take_0 1 dnames;
   759   val _ = trace " Proving take_apps...";
   760   fun one_take_app dn (con, _, args) =
   761     let
   762       fun mk_take n = dc_take (List.nth (dnames, n)) $ %:"n";
   763       fun one_rhs arg =
   764           if Datatype_Aux.is_rec_type (dtyp_of arg)
   765           then Domain_Axioms.copy_of_dtyp map_tab
   766                  mk_take (dtyp_of arg) ` (%# arg)
   767           else (%# arg);
   768       val lhs = (dc_take dn $ (%%:"Suc" $ %:"n"))`(con_app con args);
   769       val rhs = con_app2 con one_rhs args;
   770       fun is_rec arg = Datatype_Aux.is_rec_type (dtyp_of arg);
   771       fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
   772       fun nonlazy_rec args = map vname (filter is_nonlazy_rec args);
   773       val goal = lift_defined %: (nonlazy_rec args, mk_trp (lhs === rhs));
   774       val tacs = [asm_simp_tac (HOLCF_ss addsimps copy_con_rews) 1];
   775     in pg copy_take_defs goal (K tacs) end;
   776   fun one_take_apps ((dn, _), cons) = map (one_take_app dn) cons;
   777   val take_apps = maps one_take_apps eqs;
   778 in
   779   val take_rews = map Drule.export_without_context
   780     (take_stricts @ take_0s @ take_apps);
   781 end; (* local *)
   782 
   783 local
   784   fun one_con p (con, _, args) =
   785     let
   786       val P_names = map P_name (1 upto (length dnames));
   787       val vns = Name.variant_list P_names (map vname args);
   788       val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
   789       fun ind_hyp arg = %:(P_name (1 + rec_of arg)) $ bound_arg args arg;
   790       val t1 = mk_trp (%:p $ con_app2 con (bound_arg args) args);
   791       val t2 = lift ind_hyp (filter is_rec args, t1);
   792       val t3 = lift_defined (bound_arg vns) (nonlazy_vns, t2);
   793     in Library.foldr mk_All (vns, t3) end;
   794 
   795   fun one_eq ((p, cons), concl) =
   796     mk_trp (%:p $ UU) ===> Logic.list_implies (map (one_con p) cons, concl);
   797 
   798   fun ind_term concf = Library.foldr one_eq
   799     (mapn (fn n => fn x => (P_name n, x)) 1 conss,
   800      mk_trp (foldr1 mk_conj (mapn concf 1 dnames)));
   801   val take_ss = HOL_ss addsimps take_rews;
   802   fun quant_tac ctxt i = EVERY
   803     (mapn (fn n => fn _ => res_inst_tac ctxt [(("x", 0), x_name n)] spec i) 1 dnames);
   804 
   805   fun ind_prems_tac prems = EVERY
   806     (maps (fn cons =>
   807       (resolve_tac prems 1 ::
   808         maps (fn (_,_,args) => 
   809           resolve_tac prems 1 ::
   810           map (K(atac 1)) (nonlazy args) @
   811           map (K(atac 1)) (filter is_rec args))
   812         cons))
   813       conss);
   814   local 
   815     (* check whether every/exists constructor of the n-th part of the equation:
   816        it has a possibly indirectly recursive argument that isn't/is possibly 
   817        indirectly lazy *)
   818     fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
   819           is_rec arg andalso not(rec_of arg mem ns) andalso
   820           ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
   821             rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
   822               (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
   823           ) o third) cons;
   824     fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
   825     fun warn (n,cons) =
   826       if all_rec_to [] false (n,cons)
   827       then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
   828       else false;
   829     fun lazy_rec_to ns = rec_to exists I  lazy_rec_to ns;
   830 
   831   in
   832     val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   833     val is_emptys = map warn n__eqs;
   834     val is_finite = forall (not o lazy_rec_to [] false) n__eqs;
   835   end;
   836 in (* local *)
   837   val _ = trace " Proving finite_ind...";
   838   val finite_ind =
   839     let
   840       fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
   841       val goal = ind_term concf;
   842 
   843       fun tacf {prems, context} =
   844         let
   845           val tacs1 = [
   846             quant_tac context 1,
   847             simp_tac HOL_ss 1,
   848             InductTacs.induct_tac context [[SOME "n"]] 1,
   849             simp_tac (take_ss addsimps prems) 1,
   850             TRY (safe_tac HOL_cs)];
   851           fun arg_tac arg =
   852                         (* FIXME! case_UU_tac *)
   853             case_UU_tac context (prems @ con_rews) 1
   854               (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
   855           fun con_tacs (con, _, args) = 
   856             asm_simp_tac take_ss 1 ::
   857             map arg_tac (filter is_nonlazy_rec args) @
   858             [resolve_tac prems 1] @
   859             map (K (atac 1)) (nonlazy args) @
   860             map (K (etac spec 1)) (filter is_rec args);
   861           fun cases_tacs (cons, cases) =
   862             res_inst_tac context [(("x", 0), "x")] cases 1 ::
   863             asm_simp_tac (take_ss addsimps prems) 1 ::
   864             maps con_tacs cons;
   865         in
   866           tacs1 @ maps cases_tacs (conss ~~ cases)
   867         end;
   868     in pg'' thy [] goal tacf
   869        handle ERROR _ => (warning "Proof of finite_ind failed."; TrueI)
   870     end;
   871 
   872   val _ = trace " Proving take_lemmas...";
   873   val take_lemmas =
   874     let
   875       fun take_lemma n (dn, ax_reach) =
   876         let
   877           val lhs = dc_take dn $ Bound 0 `%(x_name n);
   878           val rhs = dc_take dn $ Bound 0 `%(x_name n^"'");
   879           val concl = mk_trp (%:(x_name n) === %:(x_name n^"'"));
   880           val goal = mk_All ("n", mk_trp (lhs === rhs)) ===> concl;
   881           val rules = [contlub_fst RS contlubE RS ssubst,
   882                        contlub_snd RS contlubE RS ssubst];
   883           fun tacf {prems, context} = [
   884             res_inst_tac context [(("t", 0), x_name n    )] (ax_reach RS subst) 1,
   885             res_inst_tac context [(("t", 0), x_name n^"'")] (ax_reach RS subst) 1,
   886             stac fix_def2 1,
   887             REPEAT (CHANGED
   888               (resolve_tac rules 1 THEN chain_tac 1)),
   889             stac contlub_cfun_fun 1,
   890             stac contlub_cfun_fun 2,
   891             rtac lub_equal 3,
   892             chain_tac 1,
   893             rtac allI 1,
   894             resolve_tac prems 1];
   895         in pg'' thy axs_take_def goal tacf end;
   896     in mapn take_lemma 1 (dnames ~~ axs_reach) end;
   897 
   898 (* ----- theorems concerning finiteness and induction ----------------------- *)
   899 
   900   val _ = trace " Proving finites, ind...";
   901   val (finites, ind) =
   902   (
   903     if is_finite
   904     then (* finite case *)
   905       let 
   906         fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %:"x" === %:"x");
   907         fun dname_lemma dn =
   908           let
   909             val prem1 = mk_trp (defined (%:"x"));
   910             val disj1 = mk_all ("n", dc_take dn $ Bound 0 ` %:"x" === UU);
   911             val prem2 = mk_trp (mk_disj (disj1, take_enough dn));
   912             val concl = mk_trp (take_enough dn);
   913             val goal = prem1 ===> prem2 ===> concl;
   914             val tacs = [
   915               etac disjE 1,
   916               etac notE 1,
   917               resolve_tac take_lemmas 1,
   918               asm_simp_tac take_ss 1,
   919               atac 1];
   920           in pg [] goal (K tacs) end;
   921         val _ = trace " Proving finite_lemmas1a";
   922         val finite_lemmas1a = map dname_lemma dnames;
   923  
   924         val _ = trace " Proving finite_lemma1b";
   925         val finite_lemma1b =
   926           let
   927             fun mk_eqn n ((dn, args), _) =
   928               let
   929                 val disj1 = dc_take dn $ Bound 1 ` Bound 0 === UU;
   930                 val disj2 = dc_take dn $ Bound 1 ` Bound 0 === Bound 0;
   931               in
   932                 mk_constrainall
   933                   (x_name n, Type (dn,args), mk_disj (disj1, disj2))
   934               end;
   935             val goal =
   936               mk_trp (mk_all ("n", foldr1 mk_conj (mapn mk_eqn 1 eqs)));
   937             fun arg_tacs ctxt vn = [
   938               eres_inst_tac ctxt [(("x", 0), vn)] all_dupE 1,
   939               etac disjE 1,
   940               asm_simp_tac (HOL_ss addsimps con_rews) 1,
   941               asm_simp_tac take_ss 1];
   942             fun con_tacs ctxt (con, _, args) =
   943               asm_simp_tac take_ss 1 ::
   944               maps (arg_tacs ctxt) (nonlazy_rec args);
   945             fun foo_tacs ctxt n (cons, cases) =
   946               simp_tac take_ss 1 ::
   947               rtac allI 1 ::
   948               res_inst_tac ctxt [(("x", 0), x_name n)] cases 1 ::
   949               asm_simp_tac take_ss 1 ::
   950               maps (con_tacs ctxt) cons;
   951             fun tacs ctxt =
   952               rtac allI 1 ::
   953               InductTacs.induct_tac ctxt [[SOME "n"]] 1 ::
   954               simp_tac take_ss 1 ::
   955               TRY (safe_tac (empty_cs addSEs [conjE] addSIs [conjI])) ::
   956               flat (mapn (foo_tacs ctxt) 1 (conss ~~ cases));
   957           in pg [] goal tacs end;
   958 
   959         fun one_finite (dn, l1b) =
   960           let
   961             val goal = mk_trp (%%:(dn^"_finite") $ %:"x");
   962             fun tacs ctxt = [
   963                         (* FIXME! case_UU_tac *)
   964               case_UU_tac ctxt take_rews 1 "x",
   965               eresolve_tac finite_lemmas1a 1,
   966               step_tac HOL_cs 1,
   967               step_tac HOL_cs 1,
   968               cut_facts_tac [l1b] 1,
   969               fast_tac HOL_cs 1];
   970           in pg axs_finite_def goal tacs end;
   971 
   972         val _ = trace " Proving finites";
   973         val finites = map one_finite (dnames ~~ atomize global_ctxt finite_lemma1b);
   974         val _ = trace " Proving ind";
   975         val ind =
   976           let
   977             fun concf n dn = %:(P_name n) $ %:(x_name n);
   978             fun tacf {prems, context} =
   979               let
   980                 fun finite_tacs (finite, fin_ind) = [
   981                   rtac(rewrite_rule axs_finite_def finite RS exE)1,
   982                   etac subst 1,
   983                   rtac fin_ind 1,
   984                   ind_prems_tac prems];
   985               in
   986                 TRY (safe_tac HOL_cs) ::
   987                 maps finite_tacs (finites ~~ atomize global_ctxt finite_ind)
   988               end;
   989           in pg'' thy [] (ind_term concf) tacf end;
   990       in (finites, ind) end (* let *)
   991 
   992     else (* infinite case *)
   993       let
   994         fun one_finite n dn =
   995           read_instantiate global_ctxt [(("P", 0), dn ^ "_finite " ^ x_name n)] excluded_middle;
   996         val finites = mapn one_finite 1 dnames;
   997 
   998         val goal =
   999           let
  1000             fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
  1001             fun concf n dn = %:(P_name n) $ %:(x_name n);
  1002           in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
  1003         val cont_rules =
  1004             [cont_id, cont_const, cont2cont_Rep_CFun,
  1005              cont2cont_fst, cont2cont_snd];
  1006         fun tacf {prems, context} =
  1007           map (fn ax_reach => rtac (ax_reach RS subst) 1) axs_reach @ [
  1008           quant_tac context 1,
  1009           rtac (adm_impl_admw RS wfix_ind) 1,
  1010           REPEAT_DETERM (rtac adm_all 1),
  1011           REPEAT_DETERM (
  1012             TRY (rtac adm_conj 1) THEN 
  1013             rtac adm_subst 1 THEN 
  1014             REPEAT (resolve_tac cont_rules 1) THEN
  1015             resolve_tac prems 1),
  1016           strip_tac 1,
  1017           rtac (rewrite_rule axs_take_def finite_ind) 1,
  1018           ind_prems_tac prems];
  1019         val ind = (pg'' thy [] goal tacf
  1020           handle ERROR _ =>
  1021             (warning "Cannot prove infinite induction rule"; TrueI));
  1022       in (finites, ind) end
  1023   )
  1024       handle THM _ =>
  1025              (warning "Induction proofs failed (THM raised)."; ([], TrueI))
  1026            | ERROR _ =>
  1027              (warning "Cannot prove induction rule"; ([], TrueI));
  1028 
  1029 
  1030 end; (* local *)
  1031 
  1032 (* ----- theorem concerning coinduction ------------------------------------- *)
  1033 
  1034 local
  1035   val xs = mapn (fn n => K (x_name n)) 1 dnames;
  1036   fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
  1037   val take_ss = HOL_ss addsimps take_rews;
  1038   val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
  1039   val _ = trace " Proving coind_lemma...";
  1040   val coind_lemma =
  1041     let
  1042       fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
  1043       fun mk_eqn n dn =
  1044         (dc_take dn $ %:"n" ` bnd_arg n 0) ===
  1045         (dc_take dn $ %:"n" ` bnd_arg n 1);
  1046       fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
  1047       val goal =
  1048         mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
  1049           Library.foldr mk_all2 (xs,
  1050             Library.foldr mk_imp (mapn mk_prj 0 dnames,
  1051               foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
  1052       fun x_tacs ctxt n x = [
  1053         rotate_tac (n+1) 1,
  1054         etac all2E 1,
  1055         eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
  1056         TRY (safe_tac HOL_cs),
  1057         REPEAT (CHANGED (asm_simp_tac take_ss 1))];
  1058       fun tacs ctxt = [
  1059         rtac impI 1,
  1060         InductTacs.induct_tac ctxt [[SOME "n"]] 1,
  1061         simp_tac take_ss 1,
  1062         safe_tac HOL_cs] @
  1063         flat (mapn (x_tacs ctxt) 0 xs);
  1064     in pg [ax_bisim_def] goal tacs end;
  1065 in
  1066   val _ = trace " Proving coind...";
  1067   val coind = 
  1068     let
  1069       fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
  1070       fun mk_eqn x = %:x === %:(x^"'");
  1071       val goal =
  1072         mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
  1073           Logic.list_implies (mapn mk_prj 0 xs,
  1074             mk_trp (foldr1 mk_conj (map mk_eqn xs)));
  1075       val tacs =
  1076         TRY (safe_tac HOL_cs) ::
  1077         maps (fn take_lemma => [
  1078           rtac take_lemma 1,
  1079           cut_facts_tac [coind_lemma] 1,
  1080           fast_tac HOL_cs 1])
  1081         take_lemmas;
  1082     in pg [] goal (K tacs) end;
  1083 end; (* local *)
  1084 
  1085 val inducts = Project_Rule.projections (ProofContext.init thy) ind;
  1086 fun ind_rule (dname, rule) = ((Binding.empty, [rule]), [Induct.induct_type dname]);
  1087 val induct_failed = (Thm.prop_of ind = Thm.prop_of TrueI);
  1088 
  1089 in thy |> Sign.add_path comp_dnam
  1090        |> snd o PureThy.add_thmss [
  1091            ((Binding.name "take_rews"  , take_rews   ), [Simplifier.simp_add]),
  1092            ((Binding.name "take_lemmas", take_lemmas ), []),
  1093            ((Binding.name "finites"    , finites     ), []),
  1094            ((Binding.name "finite_ind" , [finite_ind]), []),
  1095            ((Binding.name "ind"        , [ind]       ), []),
  1096            ((Binding.name "coind"      , [coind]     ), [])]
  1097        |> (if induct_failed then I
  1098            else snd o PureThy.add_thmss (map ind_rule (dnames ~~ inducts)))
  1099        |> Sign.parent_path |> pair take_rews
  1100 end; (* let *)
  1101 end; (* struct *)