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