src/HOLCF/Tools/Domain/domain_theorems.ML
author huffman
Sat Feb 27 18:09:11 2010 -0800 (2010-02-27)
changeset 35462 f5461b02d754
parent 35461 34360a1e3537
child 35464 2ae3362ba8ee
permissions -rw-r--r--
move definition of match combinators to domain_constructors.ML
     1 (*  Title:      HOLCF/Tools/Domain/domain_theorems.ML
     2     Author:     David von Oheimb
     3     Author:     Brian Huffman
     4 
     5 Proof generator for domain command.
     6 *)
     7 
     8 val HOLCF_ss = @{simpset};
     9 
    10 signature DOMAIN_THEOREMS =
    11 sig
    12   val theorems:
    13     Domain_Library.eq * Domain_Library.eq list
    14     -> typ * (binding * (bool * binding option * typ) list * mixfix) list
    15     -> theory -> thm list * theory;
    16 
    17   val comp_theorems: bstring * Domain_Library.eq list -> theory -> thm list * theory;
    18   val quiet_mode: bool Unsynchronized.ref;
    19   val trace_domain: bool Unsynchronized.ref;
    20 end;
    21 
    22 structure Domain_Theorems :> DOMAIN_THEOREMS =
    23 struct
    24 
    25 val quiet_mode = Unsynchronized.ref false;
    26 val trace_domain = Unsynchronized.ref false;
    27 
    28 fun message s = if !quiet_mode then () else writeln s;
    29 fun trace s = if !trace_domain then tracing s else ();
    30 
    31 val adm_impl_admw = @{thm adm_impl_admw};
    32 val adm_all = @{thm adm_all};
    33 val adm_conj = @{thm adm_conj};
    34 val adm_subst = @{thm adm_subst};
    35 val antisym_less_inverse = @{thm below_antisym_inverse};
    36 val beta_cfun = @{thm beta_cfun};
    37 val cfun_arg_cong = @{thm cfun_arg_cong};
    38 val ch2ch_fst = @{thm ch2ch_fst};
    39 val ch2ch_snd = @{thm ch2ch_snd};
    40 val ch2ch_Rep_CFunL = @{thm ch2ch_Rep_CFunL};
    41 val ch2ch_Rep_CFunR = @{thm ch2ch_Rep_CFunR};
    42 val chain_iterate = @{thm chain_iterate};
    43 val compact_ONE = @{thm compact_ONE};
    44 val compact_sinl = @{thm compact_sinl};
    45 val compact_sinr = @{thm compact_sinr};
    46 val compact_spair = @{thm compact_spair};
    47 val compact_up = @{thm compact_up};
    48 val contlub_cfun_arg = @{thm contlub_cfun_arg};
    49 val contlub_cfun_fun = @{thm contlub_cfun_fun};
    50 val contlub_fst = @{thm contlub_fst};
    51 val contlub_snd = @{thm contlub_snd};
    52 val contlubE = @{thm contlubE};
    53 val cont_const = @{thm cont_const};
    54 val cont_id = @{thm cont_id};
    55 val cont2cont_fst = @{thm cont2cont_fst};
    56 val cont2cont_snd = @{thm cont2cont_snd};
    57 val cont2cont_Rep_CFun = @{thm cont2cont_Rep_CFun};
    58 val fix_def2 = @{thm fix_def2};
    59 val injection_eq = @{thm injection_eq};
    60 val injection_less = @{thm injection_below};
    61 val lub_equal = @{thm lub_equal};
    62 val monofun_cfun_arg = @{thm monofun_cfun_arg};
    63 val retraction_strict = @{thm retraction_strict};
    64 val spair_eq = @{thm spair_eq};
    65 val spair_less = @{thm spair_below};
    66 val sscase1 = @{thm sscase1};
    67 val ssplit1 = @{thm ssplit1};
    68 val strictify1 = @{thm strictify1};
    69 val wfix_ind = @{thm wfix_ind};
    70 
    71 val iso_intro       = @{thm iso.intro};
    72 val iso_abs_iso     = @{thm iso.abs_iso};
    73 val iso_rep_iso     = @{thm iso.rep_iso};
    74 val iso_abs_strict  = @{thm iso.abs_strict};
    75 val iso_rep_strict  = @{thm iso.rep_strict};
    76 val iso_abs_defined = @{thm iso.abs_defined};
    77 val iso_rep_defined = @{thm iso.rep_defined};
    78 val iso_compact_abs = @{thm iso.compact_abs};
    79 val iso_compact_rep = @{thm iso.compact_rep};
    80 val iso_iso_swap    = @{thm iso.iso_swap};
    81 
    82 val exh_start = @{thm exh_start};
    83 val ex_defined_iffs = @{thms ex_defined_iffs};
    84 val exh_casedist0 = @{thm exh_casedist0};
    85 val exh_casedists = @{thms exh_casedists};
    86 
    87 open Domain_Library;
    88 infixr 0 ===>;
    89 infixr 0 ==>;
    90 infix 0 == ; 
    91 infix 1 ===;
    92 infix 1 ~= ;
    93 infix 1 <<;
    94 infix 1 ~<<;
    95 infix 9 `   ;
    96 infix 9 `% ;
    97 infix 9 `%%;
    98 infixr 9 oo;
    99 
   100 (* ----- general proof facilities ------------------------------------------- *)
   101 
   102 fun legacy_infer_term thy t =
   103   let val ctxt = ProofContext.set_mode ProofContext.mode_schematic (ProofContext.init thy)
   104   in singleton (Syntax.check_terms ctxt) (Sign.intern_term thy t) end;
   105 
   106 fun pg'' thy defs t tacs =
   107   let
   108     val t' = legacy_infer_term thy t;
   109     val asms = Logic.strip_imp_prems t';
   110     val prop = Logic.strip_imp_concl t';
   111     fun tac {prems, context} =
   112       rewrite_goals_tac defs THEN
   113       EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
   114   in Goal.prove_global thy [] asms prop tac end;
   115 
   116 fun pg' thy defs t tacsf =
   117   let
   118     fun tacs {prems, context} =
   119       if null prems then tacsf context
   120       else cut_facts_tac prems 1 :: tacsf context;
   121   in pg'' thy defs t tacs end;
   122 
   123 (* FIXME!!!!!!!!! *)
   124 (* We should NEVER re-parse variable names as strings! *)
   125 (* The names can conflict with existing constants or other syntax! *)
   126 fun case_UU_tac ctxt rews i v =
   127   InductTacs.case_tac ctxt (v^"=UU") i THEN
   128   asm_simp_tac (HOLCF_ss addsimps rews) i;
   129 
   130 val chain_tac =
   131   REPEAT_DETERM o resolve_tac 
   132     [chain_iterate, ch2ch_Rep_CFunR, ch2ch_Rep_CFunL, ch2ch_fst, ch2ch_snd];
   133 
   134 (* ----- general proofs ----------------------------------------------------- *)
   135 
   136 val all2E = @{lemma "!x y . P x y ==> (P x y ==> R) ==> R" by simp}
   137 
   138 val dist_eqI = @{lemma "!!x::'a::po. ~ x << y ==> x ~= y" by (blast dest!: below_antisym_inverse)}
   139 
   140 fun theorems
   141     (((dname, _), cons) : eq, eqs : eq list)
   142     (dom_eqn : typ * (binding * (bool * binding option * typ) list * mixfix) list)
   143     (thy : theory) =
   144 let
   145 
   146 val _ = message ("Proving isomorphism properties of domain "^dname^" ...");
   147 val map_tab = Domain_Isomorphism.get_map_tab thy;
   148 
   149 
   150 (* ----- getting the axioms and definitions --------------------------------- *)
   151 
   152 local
   153   fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
   154 in
   155   val ax_abs_iso  = ga "abs_iso"  dname;
   156   val ax_rep_iso  = ga "rep_iso"  dname;
   157   val ax_when_def = ga "when_def" dname;
   158   fun get_def mk_name (con, _, _) = ga (mk_name con^"_def") dname;
   159   val axs_pat_def = map (get_def pat_name) cons;
   160   val ax_copy_def = ga "copy_def" dname;
   161 end; (* local *)
   162 
   163 (* ----- define constructors ------------------------------------------------ *)
   164 
   165 val lhsT = fst dom_eqn;
   166 
   167 val rhsT =
   168   let
   169     fun mk_arg_typ (lazy, sel, T) = if lazy then mk_uT T else T;
   170     fun mk_con_typ (bind, args, mx) =
   171         if null args then oneT else foldr1 mk_sprodT (map mk_arg_typ args);
   172     fun mk_eq_typ (_, cons) = foldr1 mk_ssumT (map mk_con_typ cons);
   173   in
   174     mk_eq_typ dom_eqn
   175   end;
   176 
   177 val rep_const = Const(dname^"_rep", lhsT ->> rhsT);
   178 
   179 val abs_const = Const(dname^"_abs", rhsT ->> lhsT);
   180 
   181 val (result, thy) =
   182   Domain_Constructors.add_domain_constructors
   183     (Long_Name.base_name dname) dom_eqn
   184     (rep_const, abs_const) (ax_rep_iso, ax_abs_iso) ax_when_def thy;
   185 
   186 val con_appls = #con_betas result;
   187 val {exhaust, casedist, ...} = result;
   188 val {con_compacts, con_rews, inverts, injects, dist_les, dist_eqs, ...} = result;
   189 val {sel_rews, ...} = result;
   190 val when_rews = #cases result;
   191 val when_strict = hd when_rews;
   192 val dis_rews = #dis_rews result;
   193 val axs_mat_def = #match_rews result;
   194 
   195 (* ----- theorems concerning the isomorphism -------------------------------- *)
   196 
   197 val pg = pg' thy;
   198 
   199 val dc_abs  = %%:(dname^"_abs");
   200 val dc_rep  = %%:(dname^"_rep");
   201 val dc_copy = %%:(dname^"_copy");
   202 val x_name = "x";
   203 
   204 val iso_locale = iso_intro OF [ax_abs_iso, ax_rep_iso];
   205 val abs_strict = ax_rep_iso RS (allI RS retraction_strict);
   206 val rep_strict = ax_abs_iso RS (allI RS retraction_strict);
   207 val iso_rews = map Drule.export_without_context [ax_abs_iso, ax_rep_iso, abs_strict, rep_strict];
   208 
   209 (* ----- theorems concerning the constructors, discriminators and selectors - *)
   210 
   211 local
   212   fun mat_strict (con, _, _) =
   213     let
   214       val goal = mk_trp (%%:(mat_name con) ` UU ` %:"rhs" === UU);
   215       val tacs = [asm_simp_tac (HOLCF_ss addsimps [when_strict]) 1];
   216     in pg axs_mat_def goal (K tacs) end;
   217 
   218   val _ = trace " Proving mat_stricts...";
   219   val mat_stricts = map mat_strict cons;
   220 
   221   fun one_mat c (con, _, args) =
   222     let
   223       val lhs = %%:(mat_name c) ` con_app con args ` %:"rhs";
   224       val rhs =
   225         if con = c
   226         then list_ccomb (%:"rhs", map %# args)
   227         else mk_fail;
   228       val goal = lift_defined %: (nonlazy args, mk_trp (lhs === rhs));
   229       val tacs = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   230     in pg axs_mat_def goal (K tacs) end;
   231 
   232   val _ = trace " Proving mat_apps...";
   233   val mat_apps =
   234     maps (fn (c,_,_) => map (one_mat c) cons) cons;
   235 in
   236   val mat_rews = mat_stricts @ mat_apps;
   237 end;
   238 
   239 local
   240   fun ps args = mapn (fn n => fn _ => %:("pat" ^ string_of_int n)) 1 args;
   241 
   242   fun pat_lhs (con,_,args) = mk_branch (list_comb (%%:(pat_name con), ps args));
   243 
   244   fun pat_rhs (con,_,[]) = mk_return ((%:"rhs") ` HOLogic.unit)
   245     | pat_rhs (con,_,args) =
   246         (mk_branch (mk_ctuple_pat (ps args)))
   247           `(%:"rhs")`(mk_ctuple (map %# args));
   248 
   249   fun pat_strict c =
   250     let
   251       val axs = @{thm branch_def} :: axs_pat_def;
   252       val goal = mk_trp (strict (pat_lhs c ` (%:"rhs")));
   253       val tacs = [simp_tac (HOLCF_ss addsimps [when_strict]) 1];
   254     in pg axs goal (K tacs) end;
   255 
   256   fun pat_app c (con, _, args) =
   257     let
   258       val axs = @{thm branch_def} :: axs_pat_def;
   259       val lhs = (pat_lhs c)`(%:"rhs")`(con_app con args);
   260       val rhs = if con = first c then pat_rhs c else mk_fail;
   261       val goal = lift_defined %: (nonlazy args, mk_trp (lhs === rhs));
   262       val tacs = [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   263     in pg axs goal (K tacs) end;
   264 
   265   val _ = trace " Proving pat_stricts...";
   266   val pat_stricts = map pat_strict cons;
   267   val _ = trace " Proving pat_apps...";
   268   val pat_apps = maps (fn c => map (pat_app c) cons) cons;
   269 in
   270   val pat_rews = pat_stricts @ pat_apps;
   271 end;
   272 
   273 (* ----- theorems concerning one induction step ----------------------------- *)
   274 
   275 val copy_strict =
   276   let
   277     val _ = trace " Proving copy_strict...";
   278     val goal = mk_trp (strict (dc_copy `% "f"));
   279     val rules = [abs_strict, rep_strict] @ @{thms domain_map_stricts};
   280     val tacs = [asm_simp_tac (HOLCF_ss addsimps rules) 1];
   281   in
   282     SOME (pg [ax_copy_def] goal (K tacs))
   283     handle
   284       THM (s, _, _) => (trace s; NONE)
   285     | ERROR s => (trace s; NONE)
   286   end;
   287 
   288 local
   289   fun copy_app (con, _, args) =
   290     let
   291       val lhs = dc_copy`%"f"`(con_app con args);
   292       fun one_rhs arg =
   293           if Datatype_Aux.is_rec_type (dtyp_of arg)
   294           then Domain_Axioms.copy_of_dtyp map_tab
   295                  (proj (%:"f") eqs) (dtyp_of arg) ` (%# arg)
   296           else (%# arg);
   297       val rhs = con_app2 con one_rhs args;
   298       fun is_rec arg = Datatype_Aux.is_rec_type (dtyp_of arg);
   299       fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
   300       fun nonlazy_rec args = map vname (filter is_nonlazy_rec args);
   301       val goal = lift_defined %: (nonlazy_rec args, mk_trp (lhs === rhs));
   302       val args' = filter_out (fn a => is_rec a orelse is_lazy a) args;
   303       val stricts = abs_strict :: rep_strict :: @{thms domain_map_stricts};
   304                         (* FIXME! case_UU_tac *)
   305       fun tacs1 ctxt = map (case_UU_tac ctxt stricts 1 o vname) args';
   306       val rules = [ax_abs_iso] @ @{thms domain_map_simps};
   307       val tacs2 = [asm_simp_tac (HOLCF_ss addsimps rules) 1];
   308     in pg (ax_copy_def::con_appls) goal (fn ctxt => (tacs1 ctxt @ tacs2)) end;
   309 in
   310   val _ = trace " Proving copy_apps...";
   311   val copy_apps = map copy_app cons;
   312 end;
   313 
   314 local
   315   fun one_strict (con, _, args) = 
   316     let
   317       val goal = mk_trp (dc_copy`UU`(con_app con args) === UU);
   318       val rews = the_list copy_strict @ copy_apps @ con_rews;
   319                         (* FIXME! case_UU_tac *)
   320       fun tacs ctxt = map (case_UU_tac ctxt rews 1) (nonlazy args) @
   321         [asm_simp_tac (HOLCF_ss addsimps rews) 1];
   322     in
   323       SOME (pg [] goal tacs)
   324       handle
   325         THM (s, _, _) => (trace s; NONE)
   326       | ERROR s => (trace s; NONE)
   327     end;
   328 
   329   fun has_nonlazy_rec (_, _, args) = exists is_nonlazy_rec args;
   330 in
   331   val _ = trace " Proving copy_stricts...";
   332   val copy_stricts = map_filter one_strict (filter has_nonlazy_rec cons);
   333 end;
   334 
   335 val copy_rews = the_list copy_strict @ copy_apps @ copy_stricts;
   336 
   337 in
   338   thy
   339     |> Sign.add_path (Long_Name.base_name dname)
   340     |> snd o PureThy.add_thmss [
   341         ((Binding.name "iso_rews"  , iso_rews    ), [Simplifier.simp_add]),
   342         ((Binding.name "exhaust"   , [exhaust]   ), []),
   343         ((Binding.name "casedist"  , [casedist]  ), [Induct.cases_type dname]),
   344         ((Binding.name "when_rews" , when_rews   ), [Simplifier.simp_add]),
   345         ((Binding.name "compacts"  , con_compacts), [Simplifier.simp_add]),
   346         ((Binding.name "con_rews"  , con_rews    ),
   347          [Simplifier.simp_add, Fixrec.fixrec_simp_add]),
   348         ((Binding.name "sel_rews"  , sel_rews    ), [Simplifier.simp_add]),
   349         ((Binding.name "dis_rews"  , dis_rews    ), [Simplifier.simp_add]),
   350         ((Binding.name "pat_rews"  , pat_rews    ), [Simplifier.simp_add]),
   351         ((Binding.name "dist_les"  , dist_les    ), [Simplifier.simp_add]),
   352         ((Binding.name "dist_eqs"  , dist_eqs    ), [Simplifier.simp_add]),
   353         ((Binding.name "inverts"   , inverts     ), [Simplifier.simp_add]),
   354         ((Binding.name "injects"   , injects     ), [Simplifier.simp_add]),
   355         ((Binding.name "copy_rews" , copy_rews   ), [Simplifier.simp_add]),
   356         ((Binding.name "match_rews", mat_rews    ),
   357          [Simplifier.simp_add, Fixrec.fixrec_simp_add])]
   358     |> Sign.parent_path
   359     |> pair (iso_rews @ when_rews @ con_rews @ sel_rews @ dis_rews @
   360         pat_rews @ dist_les @ dist_eqs @ copy_rews)
   361 end; (* let *)
   362 
   363 fun comp_theorems (comp_dnam, eqs: eq list) thy =
   364 let
   365 val global_ctxt = ProofContext.init thy;
   366 val map_tab = Domain_Isomorphism.get_map_tab thy;
   367 
   368 val dnames = map (fst o fst) eqs;
   369 val conss  = map  snd        eqs;
   370 val comp_dname = Sign.full_bname thy comp_dnam;
   371 
   372 val _ = message ("Proving induction properties of domain "^comp_dname^" ...");
   373 val pg = pg' thy;
   374 
   375 (* ----- getting the composite axiom and definitions ------------------------ *)
   376 
   377 local
   378   fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
   379 in
   380   val axs_reach      = map (ga "reach"     ) dnames;
   381   val axs_take_def   = map (ga "take_def"  ) dnames;
   382   val axs_finite_def = map (ga "finite_def") dnames;
   383   val ax_copy2_def   =      ga "copy_def"  comp_dnam;
   384 (* TEMPORARILY DISABLED
   385   val ax_bisim_def   =      ga "bisim_def" comp_dnam;
   386 TEMPORARILY DISABLED *)
   387 end;
   388 
   389 local
   390   fun gt  s dn = PureThy.get_thm  thy (dn ^ "." ^ s);
   391   fun gts s dn = PureThy.get_thms thy (dn ^ "." ^ s);
   392 in
   393   val cases = map (gt  "casedist" ) dnames;
   394   val con_rews  = maps (gts "con_rews" ) dnames;
   395   val copy_rews = maps (gts "copy_rews") dnames;
   396 end;
   397 
   398 fun dc_take dn = %%:(dn^"_take");
   399 val x_name = idx_name dnames "x"; 
   400 val P_name = idx_name dnames "P";
   401 val n_eqs = length eqs;
   402 
   403 (* ----- theorems concerning finite approximation and finite induction ------ *)
   404 
   405 local
   406   val iterate_Cprod_ss = global_simpset_of @{theory Fix};
   407   val copy_con_rews  = copy_rews @ con_rews;
   408   val copy_take_defs =
   409     (if n_eqs = 1 then [] else [ax_copy2_def]) @ axs_take_def;
   410   val _ = trace " Proving take_stricts...";
   411   fun one_take_strict ((dn, args), _) =
   412     let
   413       val goal = mk_trp (strict (dc_take dn $ %:"n"));
   414       val rules = [
   415         @{thm monofun_fst [THEN monofunE]},
   416         @{thm monofun_snd [THEN monofunE]}];
   417       val tacs = [
   418         rtac @{thm UU_I} 1,
   419         rtac @{thm below_eq_trans} 1,
   420         resolve_tac axs_reach 2,
   421         rtac @{thm monofun_cfun_fun} 1,
   422         REPEAT (resolve_tac rules 1),
   423         rtac @{thm iterate_below_fix} 1];
   424     in pg axs_take_def goal (K tacs) end;
   425   val take_stricts = map one_take_strict eqs;
   426   fun take_0 n dn =
   427     let
   428       val goal = mk_trp ((dc_take dn $ @{term "0::nat"}) `% x_name n === UU);
   429     in pg axs_take_def goal (K [simp_tac iterate_Cprod_ss 1]) end;
   430   val take_0s = mapn take_0 1 dnames;
   431   val _ = trace " Proving take_apps...";
   432   fun one_take_app dn (con, _, args) =
   433     let
   434       fun mk_take n = dc_take (List.nth (dnames, n)) $ %:"n";
   435       fun one_rhs arg =
   436           if Datatype_Aux.is_rec_type (dtyp_of arg)
   437           then Domain_Axioms.copy_of_dtyp map_tab
   438                  mk_take (dtyp_of arg) ` (%# arg)
   439           else (%# arg);
   440       val lhs = (dc_take dn $ (%%:"Suc" $ %:"n"))`(con_app con args);
   441       val rhs = con_app2 con one_rhs args;
   442       fun is_rec arg = Datatype_Aux.is_rec_type (dtyp_of arg);
   443       fun is_nonlazy_rec arg = is_rec arg andalso not (is_lazy arg);
   444       fun nonlazy_rec args = map vname (filter is_nonlazy_rec args);
   445       val goal = lift_defined %: (nonlazy_rec args, mk_trp (lhs === rhs));
   446       val tacs = [asm_simp_tac (HOLCF_ss addsimps copy_con_rews) 1];
   447     in pg copy_take_defs goal (K tacs) end;
   448   fun one_take_apps ((dn, _), cons) = map (one_take_app dn) cons;
   449   val take_apps = maps one_take_apps eqs;
   450 in
   451   val take_rews = map Drule.export_without_context
   452     (take_stricts @ take_0s @ take_apps);
   453 end; (* local *)
   454 
   455 local
   456   fun one_con p (con, _, args) =
   457     let
   458       val P_names = map P_name (1 upto (length dnames));
   459       val vns = Name.variant_list P_names (map vname args);
   460       val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
   461       fun ind_hyp arg = %:(P_name (1 + rec_of arg)) $ bound_arg args arg;
   462       val t1 = mk_trp (%:p $ con_app2 con (bound_arg args) args);
   463       val t2 = lift ind_hyp (filter is_rec args, t1);
   464       val t3 = lift_defined (bound_arg vns) (nonlazy_vns, t2);
   465     in Library.foldr mk_All (vns, t3) end;
   466 
   467   fun one_eq ((p, cons), concl) =
   468     mk_trp (%:p $ UU) ===> Logic.list_implies (map (one_con p) cons, concl);
   469 
   470   fun ind_term concf = Library.foldr one_eq
   471     (mapn (fn n => fn x => (P_name n, x)) 1 conss,
   472      mk_trp (foldr1 mk_conj (mapn concf 1 dnames)));
   473   val take_ss = HOL_ss addsimps take_rews;
   474   fun quant_tac ctxt i = EVERY
   475     (mapn (fn n => fn _ => res_inst_tac ctxt [(("x", 0), x_name n)] spec i) 1 dnames);
   476 
   477   fun ind_prems_tac prems = EVERY
   478     (maps (fn cons =>
   479       (resolve_tac prems 1 ::
   480         maps (fn (_,_,args) => 
   481           resolve_tac prems 1 ::
   482           map (K(atac 1)) (nonlazy args) @
   483           map (K(atac 1)) (filter is_rec args))
   484         cons))
   485       conss);
   486   local 
   487     (* check whether every/exists constructor of the n-th part of the equation:
   488        it has a possibly indirectly recursive argument that isn't/is possibly 
   489        indirectly lazy *)
   490     fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
   491           is_rec arg andalso not(rec_of arg mem ns) andalso
   492           ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
   493             rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
   494               (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
   495           ) o third) cons;
   496     fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
   497     fun warn (n,cons) =
   498       if all_rec_to [] false (n,cons)
   499       then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
   500       else false;
   501     fun lazy_rec_to ns = rec_to exists I  lazy_rec_to ns;
   502 
   503   in
   504     val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   505     val is_emptys = map warn n__eqs;
   506     val is_finite = forall (not o lazy_rec_to [] false) n__eqs;
   507   end;
   508 in (* local *)
   509   val _ = trace " Proving finite_ind...";
   510   val finite_ind =
   511     let
   512       fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
   513       val goal = ind_term concf;
   514 
   515       fun tacf {prems, context} =
   516         let
   517           val tacs1 = [
   518             quant_tac context 1,
   519             simp_tac HOL_ss 1,
   520             InductTacs.induct_tac context [[SOME "n"]] 1,
   521             simp_tac (take_ss addsimps prems) 1,
   522             TRY (safe_tac HOL_cs)];
   523           fun arg_tac arg =
   524                         (* FIXME! case_UU_tac *)
   525             case_UU_tac context (prems @ con_rews) 1
   526               (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
   527           fun con_tacs (con, _, args) = 
   528             asm_simp_tac take_ss 1 ::
   529             map arg_tac (filter is_nonlazy_rec args) @
   530             [resolve_tac prems 1] @
   531             map (K (atac 1)) (nonlazy args) @
   532             map (K (etac spec 1)) (filter is_rec args);
   533           fun cases_tacs (cons, cases) =
   534             res_inst_tac context [(("x", 0), "x")] cases 1 ::
   535             asm_simp_tac (take_ss addsimps prems) 1 ::
   536             maps con_tacs cons;
   537         in
   538           tacs1 @ maps cases_tacs (conss ~~ cases)
   539         end;
   540     in pg'' thy [] goal tacf
   541        handle ERROR _ => (warning "Proof of finite_ind failed."; TrueI)
   542     end;
   543 
   544   val _ = trace " Proving take_lemmas...";
   545   val take_lemmas =
   546     let
   547       fun take_lemma n (dn, ax_reach) =
   548         let
   549           val lhs = dc_take dn $ Bound 0 `%(x_name n);
   550           val rhs = dc_take dn $ Bound 0 `%(x_name n^"'");
   551           val concl = mk_trp (%:(x_name n) === %:(x_name n^"'"));
   552           val goal = mk_All ("n", mk_trp (lhs === rhs)) ===> concl;
   553           val rules = [contlub_fst RS contlubE RS ssubst,
   554                        contlub_snd RS contlubE RS ssubst];
   555           fun tacf {prems, context} = [
   556             res_inst_tac context [(("t", 0), x_name n    )] (ax_reach RS subst) 1,
   557             res_inst_tac context [(("t", 0), x_name n^"'")] (ax_reach RS subst) 1,
   558             stac fix_def2 1,
   559             REPEAT (CHANGED
   560               (resolve_tac rules 1 THEN chain_tac 1)),
   561             stac contlub_cfun_fun 1,
   562             stac contlub_cfun_fun 2,
   563             rtac lub_equal 3,
   564             chain_tac 1,
   565             rtac allI 1,
   566             resolve_tac prems 1];
   567         in pg'' thy axs_take_def goal tacf end;
   568     in mapn take_lemma 1 (dnames ~~ axs_reach) end;
   569 
   570 (* ----- theorems concerning finiteness and induction ----------------------- *)
   571 
   572   val _ = trace " Proving finites, ind...";
   573   val (finites, ind) =
   574   (
   575     if is_finite
   576     then (* finite case *)
   577       let 
   578         fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %:"x" === %:"x");
   579         fun dname_lemma dn =
   580           let
   581             val prem1 = mk_trp (defined (%:"x"));
   582             val disj1 = mk_all ("n", dc_take dn $ Bound 0 ` %:"x" === UU);
   583             val prem2 = mk_trp (mk_disj (disj1, take_enough dn));
   584             val concl = mk_trp (take_enough dn);
   585             val goal = prem1 ===> prem2 ===> concl;
   586             val tacs = [
   587               etac disjE 1,
   588               etac notE 1,
   589               resolve_tac take_lemmas 1,
   590               asm_simp_tac take_ss 1,
   591               atac 1];
   592           in pg [] goal (K tacs) end;
   593         val _ = trace " Proving finite_lemmas1a";
   594         val finite_lemmas1a = map dname_lemma dnames;
   595  
   596         val _ = trace " Proving finite_lemma1b";
   597         val finite_lemma1b =
   598           let
   599             fun mk_eqn n ((dn, args), _) =
   600               let
   601                 val disj1 = dc_take dn $ Bound 1 ` Bound 0 === UU;
   602                 val disj2 = dc_take dn $ Bound 1 ` Bound 0 === Bound 0;
   603               in
   604                 mk_constrainall
   605                   (x_name n, Type (dn,args), mk_disj (disj1, disj2))
   606               end;
   607             val goal =
   608               mk_trp (mk_all ("n", foldr1 mk_conj (mapn mk_eqn 1 eqs)));
   609             fun arg_tacs ctxt vn = [
   610               eres_inst_tac ctxt [(("x", 0), vn)] all_dupE 1,
   611               etac disjE 1,
   612               asm_simp_tac (HOL_ss addsimps con_rews) 1,
   613               asm_simp_tac take_ss 1];
   614             fun con_tacs ctxt (con, _, args) =
   615               asm_simp_tac take_ss 1 ::
   616               maps (arg_tacs ctxt) (nonlazy_rec args);
   617             fun foo_tacs ctxt n (cons, cases) =
   618               simp_tac take_ss 1 ::
   619               rtac allI 1 ::
   620               res_inst_tac ctxt [(("x", 0), x_name n)] cases 1 ::
   621               asm_simp_tac take_ss 1 ::
   622               maps (con_tacs ctxt) cons;
   623             fun tacs ctxt =
   624               rtac allI 1 ::
   625               InductTacs.induct_tac ctxt [[SOME "n"]] 1 ::
   626               simp_tac take_ss 1 ::
   627               TRY (safe_tac (empty_cs addSEs [conjE] addSIs [conjI])) ::
   628               flat (mapn (foo_tacs ctxt) 1 (conss ~~ cases));
   629           in pg [] goal tacs end;
   630 
   631         fun one_finite (dn, l1b) =
   632           let
   633             val goal = mk_trp (%%:(dn^"_finite") $ %:"x");
   634             fun tacs ctxt = [
   635                         (* FIXME! case_UU_tac *)
   636               case_UU_tac ctxt take_rews 1 "x",
   637               eresolve_tac finite_lemmas1a 1,
   638               step_tac HOL_cs 1,
   639               step_tac HOL_cs 1,
   640               cut_facts_tac [l1b] 1,
   641               fast_tac HOL_cs 1];
   642           in pg axs_finite_def goal tacs end;
   643 
   644         val _ = trace " Proving finites";
   645         val finites = map one_finite (dnames ~~ atomize global_ctxt finite_lemma1b);
   646         val _ = trace " Proving ind";
   647         val ind =
   648           let
   649             fun concf n dn = %:(P_name n) $ %:(x_name n);
   650             fun tacf {prems, context} =
   651               let
   652                 fun finite_tacs (finite, fin_ind) = [
   653                   rtac(rewrite_rule axs_finite_def finite RS exE)1,
   654                   etac subst 1,
   655                   rtac fin_ind 1,
   656                   ind_prems_tac prems];
   657               in
   658                 TRY (safe_tac HOL_cs) ::
   659                 maps finite_tacs (finites ~~ atomize global_ctxt finite_ind)
   660               end;
   661           in pg'' thy [] (ind_term concf) tacf end;
   662       in (finites, ind) end (* let *)
   663 
   664     else (* infinite case *)
   665       let
   666         fun one_finite n dn =
   667           read_instantiate global_ctxt [(("P", 0), dn ^ "_finite " ^ x_name n)] excluded_middle;
   668         val finites = mapn one_finite 1 dnames;
   669 
   670         val goal =
   671           let
   672             fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
   673             fun concf n dn = %:(P_name n) $ %:(x_name n);
   674           in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
   675         val cont_rules =
   676             [cont_id, cont_const, cont2cont_Rep_CFun,
   677              cont2cont_fst, cont2cont_snd];
   678         fun tacf {prems, context} =
   679           map (fn ax_reach => rtac (ax_reach RS subst) 1) axs_reach @ [
   680           quant_tac context 1,
   681           rtac (adm_impl_admw RS wfix_ind) 1,
   682           REPEAT_DETERM (rtac adm_all 1),
   683           REPEAT_DETERM (
   684             TRY (rtac adm_conj 1) THEN 
   685             rtac adm_subst 1 THEN 
   686             REPEAT (resolve_tac cont_rules 1) THEN
   687             resolve_tac prems 1),
   688           strip_tac 1,
   689           rtac (rewrite_rule axs_take_def finite_ind) 1,
   690           ind_prems_tac prems];
   691         val ind = (pg'' thy [] goal tacf
   692           handle ERROR _ =>
   693             (warning "Cannot prove infinite induction rule"; TrueI));
   694       in (finites, ind) end
   695   )
   696       handle THM _ =>
   697              (warning "Induction proofs failed (THM raised)."; ([], TrueI))
   698            | ERROR _ =>
   699              (warning "Cannot prove induction rule"; ([], TrueI));
   700 
   701 
   702 end; (* local *)
   703 
   704 (* ----- theorem concerning coinduction ------------------------------------- *)
   705 
   706 (* COINDUCTION TEMPORARILY DISABLED
   707 local
   708   val xs = mapn (fn n => K (x_name n)) 1 dnames;
   709   fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
   710   val take_ss = HOL_ss addsimps take_rews;
   711   val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
   712   val _ = trace " Proving coind_lemma...";
   713   val coind_lemma =
   714     let
   715       fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
   716       fun mk_eqn n dn =
   717         (dc_take dn $ %:"n" ` bnd_arg n 0) ===
   718         (dc_take dn $ %:"n" ` bnd_arg n 1);
   719       fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
   720       val goal =
   721         mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
   722           Library.foldr mk_all2 (xs,
   723             Library.foldr mk_imp (mapn mk_prj 0 dnames,
   724               foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
   725       fun x_tacs ctxt n x = [
   726         rotate_tac (n+1) 1,
   727         etac all2E 1,
   728         eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
   729         TRY (safe_tac HOL_cs),
   730         REPEAT (CHANGED (asm_simp_tac take_ss 1))];
   731       fun tacs ctxt = [
   732         rtac impI 1,
   733         InductTacs.induct_tac ctxt [[SOME "n"]] 1,
   734         simp_tac take_ss 1,
   735         safe_tac HOL_cs] @
   736         flat (mapn (x_tacs ctxt) 0 xs);
   737     in pg [ax_bisim_def] goal tacs end;
   738 in
   739   val _ = trace " Proving coind...";
   740   val coind = 
   741     let
   742       fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
   743       fun mk_eqn x = %:x === %:(x^"'");
   744       val goal =
   745         mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
   746           Logic.list_implies (mapn mk_prj 0 xs,
   747             mk_trp (foldr1 mk_conj (map mk_eqn xs)));
   748       val tacs =
   749         TRY (safe_tac HOL_cs) ::
   750         maps (fn take_lemma => [
   751           rtac take_lemma 1,
   752           cut_facts_tac [coind_lemma] 1,
   753           fast_tac HOL_cs 1])
   754         take_lemmas;
   755     in pg [] goal (K tacs) end;
   756 end; (* local *)
   757 COINDUCTION TEMPORARILY DISABLED *)
   758 
   759 val inducts = Project_Rule.projections (ProofContext.init thy) ind;
   760 fun ind_rule (dname, rule) = ((Binding.empty, [rule]), [Induct.induct_type dname]);
   761 val induct_failed = (Thm.prop_of ind = Thm.prop_of TrueI);
   762 
   763 in thy |> Sign.add_path comp_dnam
   764        |> snd o PureThy.add_thmss [
   765            ((Binding.name "take_rews"  , take_rews   ), [Simplifier.simp_add]),
   766            ((Binding.name "take_lemmas", take_lemmas ), []),
   767            ((Binding.name "finites"    , finites     ), []),
   768            ((Binding.name "finite_ind" , [finite_ind]), []),
   769            ((Binding.name "ind"        , [ind]       ), [])(*,
   770            ((Binding.name "coind"      , [coind]     ), [])*)]
   771        |> (if induct_failed then I
   772            else snd o PureThy.add_thmss (map ind_rule (dnames ~~ inducts)))
   773        |> Sign.parent_path |> pair take_rews
   774 end; (* let *)
   775 end; (* struct *)