src/HOLCF/domain/theorems.ML
author oheimb
Wed Dec 18 15:16:13 1996 +0100 (1996-12-18)
changeset 2445 51993fea433f
parent 2276 3eb9a113029e
child 2446 c2a9bf6c0948
permissions -rw-r--r--
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
     1 (*  Title:      HOLCF/domain/theorems.ML
     2     ID:         $Id$
     3     Author : David von Oheimb
     4     Copyright 1995, 1996 TU Muenchen
     5 
     6 proof generator for domain section
     7 *)
     8 
     9 structure Domain_Theorems = struct
    10 
    11 local
    12 
    13 open Domain_Library;
    14 infixr 0 ===>;infixr 0 ==>;infix 0 == ; 
    15 infix 1 ===; infix 1 ~= ; infix 1 <<; infix 1 ~<<;
    16 infix 9 `   ; infix 9 `% ; infix 9 `%%; infixr 9 oo;
    17 
    18 (* ----- general proof facilities ------------------------------------------- *)
    19 
    20 fun inferT sg pre_tm = #2 (Sign.infer_types sg (K None) (K None) [] true 
    21                            ([pre_tm],propT));
    22 
    23 fun pg'' thy defs t = let val sg = sign_of thy;
    24                           val ct = Thm.cterm_of sg (inferT sg t);
    25                       in prove_goalw_cterm defs ct end;
    26 fun pg'  thy defs t tacsf=pg'' thy defs t (fn []   => tacsf 
    27                                 | prems=> (cut_facts_tac prems 1)::tacsf);
    28 
    29 fun REPEAT_DETERM_UNTIL p tac = 
    30 let fun drep st = if p st then Sequence.single st
    31                           else (case Sequence.pull(tac st) of
    32                                   None        => Sequence.null
    33                                 | Some(st',_) => drep st')
    34 in drep end;
    35 val UNTIL_SOLVED = REPEAT_DETERM_UNTIL (has_fewer_prems 1);
    36 
    37 local val trueI2 = prove_goal HOL.thy"f~=x ==> True"(fn _ => [rtac TrueI 1]) in
    38 val kill_neq_tac = dtac trueI2 end;
    39 fun case_UU_tac rews i v =      case_tac (v^"=UU") i THEN
    40                                 asm_simp_tac (HOLCF_ss addsimps rews) i;
    41 
    42 val chain_tac = REPEAT_DETERM o resolve_tac 
    43                 [is_chain_iterate, ch2ch_fappR, ch2ch_fappL];
    44 
    45 (* ----- general proofs ----------------------------------------------------- *)
    46 
    47 val all2E = prove_goal HOL.thy "[| !x y . P x y; P x y ==> R |] ==> R"
    48  (fn prems =>[
    49                                 resolve_tac prems 1,
    50                                 cut_facts_tac prems 1,
    51                                 fast_tac HOL_cs 1]);
    52 
    53 val dist_eqI = prove_goal Porder.thy "~ x << y ==> x ~= y" (fn prems => [
    54                                 rtac rev_contrapos 1,
    55                                 etac (antisym_less_inverse RS conjunct1) 1,
    56                                 resolve_tac prems 1]);
    57 
    58 in
    59 
    60 
    61 fun theorems thy (((dname,_),cons) : eq, eqs :eq list) =
    62 let
    63 
    64 val dummy = writeln ("Proving isomorphism properties of domain "^dname^"...");
    65 val pg = pg' thy;
    66 (*
    67 infixr 0 y;
    68 val b = 0;
    69 fun _ y t = by t;
    70 fun  g  defs t = let val sg = sign_of thy;
    71                      val ct = Thm.cterm_of sg (inferT sg t);
    72                  in goalw_cterm defs ct end;
    73 *)
    74 
    75 
    76 (* ----- getting the axioms and definitions --------------------------------- *)
    77 
    78 local val ga = get_axiom thy in
    79 val ax_abs_iso    = ga (dname^"_abs_iso"   );
    80 val ax_rep_iso    = ga (dname^"_rep_iso"   );
    81 val ax_when_def   = ga (dname^"_when_def"  );
    82 val axs_con_def   = map (fn (con,_) => ga (extern_name con ^"_def")) cons;
    83 val axs_dis_def   = map (fn (con,_) => ga (   dis_name con ^"_def")) cons;
    84 val axs_sel_def   = flat(map (fn (_,args) => 
    85                     map (fn     arg => ga (sel_of arg      ^"_def")) args)cons);
    86 val ax_copy_def   = ga (dname^"_copy_def"  );
    87 end; (* local *)
    88 
    89 (* ----- theorems concerning the isomorphism -------------------------------- *)
    90 
    91 val dc_abs  = %%(dname^"_abs");
    92 val dc_rep  = %%(dname^"_rep");
    93 val dc_copy = %%(dname^"_copy");
    94 val x_name = "x";
    95 
    96 val (rep_strict, abs_strict) = let 
    97          val r = ax_rep_iso RS (ax_abs_iso RS (allI  RSN(2,allI RS iso_strict)))
    98                in (r RS conjunct1, r RS conjunct2) end;
    99 val abs_defin' = pg [] ((dc_abs`%x_name === UU) ==> (%x_name === UU)) [
   100                            res_inst_tac [("t",x_name)] (ax_abs_iso RS subst) 1,
   101                                 etac ssubst 1, rtac rep_strict 1];
   102 val rep_defin' = pg [] ((dc_rep`%x_name === UU) ==> (%x_name === UU)) [
   103                            res_inst_tac [("t",x_name)] (ax_rep_iso RS subst) 1,
   104                                 etac ssubst 1, rtac abs_strict 1];
   105 val iso_rews = [ax_abs_iso,ax_rep_iso,abs_strict,rep_strict];
   106 
   107 local 
   108 val iso_swap = pg [] (dc_rep`%"x" === %"y" ==> %"x" === dc_abs`%"y") [
   109                             dres_inst_tac [("f",dname^"_abs")] cfun_arg_cong 1,
   110                             etac (ax_rep_iso RS subst) 1];
   111 fun exh foldr1 cn quant foldr2 var = let
   112   fun one_con (con,args) = let val vns = map vname args in
   113     foldr quant (vns, foldr2 ((%x_name === con_app2 con (var vns) vns)::
   114                               map (defined o (var vns)) (nonlazy args))) end
   115   in foldr1 ((cn(%x_name===UU))::map one_con cons) end;
   116 in
   117 val cases = let 
   118             fun common_tac thm = rtac thm 1 THEN contr_tac 1;
   119             fun unit_tac true = common_tac upE1
   120             |   unit_tac _    = all_tac;
   121             fun prod_tac []          = common_tac oneE
   122             |   prod_tac [arg]       = unit_tac (is_lazy arg)
   123             |   prod_tac (arg::args) = 
   124                                 common_tac sprodE THEN
   125                                 kill_neq_tac 1 THEN
   126                                 unit_tac (is_lazy arg) THEN
   127                                 prod_tac args;
   128             fun sum_rest_tac p = SELECT_GOAL(EVERY[
   129                                 rtac p 1,
   130                                 rewrite_goals_tac axs_con_def,
   131                                 dtac iso_swap 1,
   132                                 simp_tac HOLCF_ss 1,
   133                                 UNTIL_SOLVED(fast_tac HOL_cs 1)]) 1;
   134             fun sum_tac [(_,args)]       [p]        = 
   135                                 prod_tac args THEN sum_rest_tac p
   136             |   sum_tac ((_,args)::cons') (p::prems) = DETERM(
   137                                 common_tac ssumE THEN
   138                                 kill_neq_tac 1 THEN kill_neq_tac 2 THEN
   139                                 prod_tac args THEN sum_rest_tac p) THEN
   140                                 sum_tac cons' prems
   141             |   sum_tac _ _ = Imposs "theorems:sum_tac";
   142           in pg'' thy [] (exh (fn l => foldr (op ===>) (l,mk_trp(%"P")))
   143                               (fn T => T ==> %"P") mk_All
   144                               (fn l => foldr (op ===>) (map mk_trp l,
   145                                                             mk_trp(%"P")))
   146                               bound_arg)
   147                              (fn prems => [
   148                                 cut_facts_tac [excluded_middle] 1,
   149                                 etac disjE 1,
   150                                 rtac (hd prems) 2,
   151                                 etac rep_defin' 2,
   152                                 if length cons = 1 andalso 
   153                                    length (snd(hd cons)) = 1 andalso 
   154                                    not(is_lazy(hd(snd(hd cons))))
   155                                 then rtac (hd (tl prems)) 1 THEN atac 2 THEN
   156                                      rewrite_goals_tac axs_con_def THEN
   157                                      simp_tac (HOLCF_ss addsimps [ax_rep_iso]) 1
   158                                 else sum_tac cons (tl prems)])end;
   159 val exhaust= pg[](mk_trp(exh (foldr' mk_disj) Id mk_ex (foldr' mk_conj) (K %)))[
   160                                 rtac cases 1,
   161                                 UNTIL_SOLVED(fast_tac HOL_cs 1)];
   162 end;
   163 
   164 local 
   165   fun bind_fun t = foldr mk_All (when_funs cons,t);
   166   fun bound_fun i _ = Bound (length cons - i);
   167   val when_app  = foldl (op `) (%%(dname^"_when"), mapn bound_fun 1 cons);
   168   val when_appl = pg [ax_when_def] (bind_fun(mk_trp(when_app`%x_name ===
   169              when_body cons (fn (m,n)=> bound_fun (n-m) 0)`(dc_rep`%x_name))))[
   170                                 simp_tac HOLCF_ss 1];
   171 in
   172 val when_strict = pg [] (bind_fun(mk_trp(strict when_app))) [
   173                         simp_tac(HOLCF_ss addsimps [when_appl,rep_strict]) 1];
   174 val when_apps = let fun one_when n (con,args) = pg axs_con_def 
   175                 (bind_fun (lift_defined % (nonlazy args, 
   176                 mk_trp(when_app`(con_app con args) ===
   177                        mk_cfapp(bound_fun n 0,map %# args)))))[
   178                 asm_simp_tac (HOLCF_ss addsimps [when_appl,ax_abs_iso]) 1];
   179         in mapn one_when 1 cons end;
   180 end;
   181 val when_rews = when_strict::when_apps;
   182 
   183 (* ----- theorems concerning the constructors, discriminators and selectors - *)
   184 
   185 val dis_rews = let
   186   val dis_stricts = map (fn (con,_) => pg axs_dis_def (mk_trp(
   187                              strict(%%(dis_name con)))) [
   188                                 simp_tac (HOLCF_ss addsimps when_rews) 1]) cons;
   189   val dis_apps = let fun one_dis c (con,args)= pg axs_dis_def
   190                    (lift_defined % (nonlazy args,
   191                         (mk_trp((%%(dis_name c))`(con_app con args) ===
   192                               %%(if con=c then "TT" else "FF"))))) [
   193                                 asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   194         in flat(map (fn (c,_) => map (one_dis c) cons) cons) end;
   195   val dis_defins = map (fn (con,args) => pg [] (defined(%x_name) ==> 
   196                       defined(%%(dis_name con)`%x_name)) [
   197                                 rtac cases 1,
   198                                 contr_tac 1,
   199                                 UNTIL_SOLVED (CHANGED(asm_simp_tac 
   200                                         (HOLCF_ss addsimps dis_apps) 1))]) cons;
   201 in dis_stricts @ dis_defins @ dis_apps end;
   202 
   203 val con_stricts = flat(map (fn (con,args) => map (fn vn =>
   204                         pg (axs_con_def) 
   205                            (mk_trp(con_app2 con (fn arg => if vname arg = vn 
   206                                         then UU else %# arg) args === UU))[
   207                                 asm_simp_tac (HOLCF_ss addsimps [abs_strict]) 1]
   208                         ) (nonlazy args)) cons);
   209 val con_defins = map (fn (con,args) => pg []
   210                         (lift_defined % (nonlazy args,
   211                                 mk_trp(defined(con_app con args)))) ([
   212                           rtac rev_contrapos 1, 
   213                           eres_inst_tac [("f",dis_name con)] cfun_arg_cong 1,
   214                           asm_simp_tac (HOLCF_ss addsimps dis_rews) 1] )) cons;
   215 val con_rews = con_stricts @ con_defins;
   216 
   217 val sel_stricts = let fun one_sel sel = pg axs_sel_def (mk_trp(strict(%%sel))) [
   218                                 simp_tac (HOLCF_ss addsimps when_rews) 1];
   219 in flat(map (fn (_,args) =>map (fn arg => one_sel (sel_of arg)) args) cons) end;
   220 val sel_apps = let fun one_sel c n sel = map (fn (con,args) => 
   221                 let val nlas = nonlazy args;
   222                     val vns  = map vname args;
   223                 in pg axs_sel_def (lift_defined %
   224                    (filter (fn v => con=c andalso (v<>nth_elem(n,vns))) nlas,
   225                                 mk_trp((%%sel)`(con_app con args) === 
   226                                 (if con=c then %(nth_elem(n,vns)) else UU))))
   227                             ( (if con=c then [] 
   228                        else map(case_UU_tac(when_rews@con_stricts)1) nlas)
   229                      @(if con=c andalso ((nth_elem(n,vns)) mem nlas)
   230                                  then[case_UU_tac (when_rews @ con_stricts) 1 
   231                                                   (nth_elem(n,vns))] else [])
   232                      @ [asm_simp_tac(HOLCF_ss addsimps when_rews)1])end) cons;
   233 in flat(map  (fn (c,args) => 
   234      flat(mapn (fn n => fn arg => one_sel c n (sel_of arg)) 0 args)) cons) end;
   235 val sel_defins = if length cons=1 then map (fn arg => pg [](defined(%x_name)==> 
   236                         defined(%%(sel_of arg)`%x_name)) [
   237                                 rtac cases 1,
   238                                 contr_tac 1,
   239                                 UNTIL_SOLVED (CHANGED(asm_simp_tac 
   240                                              (HOLCF_ss addsimps sel_apps) 1))]) 
   241                  (filter_out is_lazy (snd(hd cons))) else [];
   242 val sel_rews = sel_stricts @ sel_defins @ sel_apps;
   243 
   244 val distincts_le = let
   245     fun dist (con1, args1) (con2, args2) = pg []
   246               (lift_defined % ((nonlazy args1),
   247                         (mk_trp (con_app con1 args1 ~<< con_app con2 args2))))([
   248                         rtac rev_contrapos 1,
   249                         eres_inst_tac[("fo",dis_name con1)] monofun_cfun_arg 1]
   250                       @map(case_UU_tac (con_stricts @ dis_rews)1)(nonlazy args2)
   251                       @[asm_simp_tac (HOLCF_ss addsimps dis_rews) 1]);
   252     fun distinct (con1,args1) (con2,args2) =
   253         let val arg1 = (con1, args1)
   254             val arg2 = (con2,
   255 			ListPair.map (fn (arg,vn) => upd_vname (K vn) arg)
   256                         (args2, variantlist(map vname args2,map vname args1)))
   257         in [dist arg1 arg2, dist arg2 arg1] end;
   258     fun distincts []      = []
   259     |   distincts (c::cs) = (map (distinct c) cs) :: distincts cs;
   260 in distincts cons end;
   261 val dists_le = flat (flat distincts_le);
   262 val dists_eq = let
   263     fun distinct (_,args1) ((_,args2),leqs) = let
   264         val (le1,le2) = (hd leqs, hd(tl leqs));
   265         val (eq1,eq2) = (le1 RS dist_eqI, le2 RS dist_eqI) in
   266         if nonlazy args1 = [] then [eq1, eq1 RS not_sym] else
   267         if nonlazy args2 = [] then [eq2, eq2 RS not_sym] else
   268                                         [eq1, eq2] end;
   269     fun distincts []      = []
   270     |   distincts ((c,leqs)::cs) = List_.concat
   271 	            (ListPair.map (distinct c) ((map #1 cs),leqs)) @
   272 		    distincts cs;
   273     in distincts (cons~~distincts_le) end;
   274 
   275 local 
   276   fun pgterm rel con args = let
   277                 fun append s = upd_vname(fn v => v^s);
   278                 val (largs,rargs) = (args, map (append "'") args);
   279                 in pg [] (mk_trp (rel(con_app con largs,con_app con rargs)) ===>
   280                       lift_defined % ((nonlazy largs),lift_defined % ((nonlazy rargs),
   281                             mk_trp (foldr' mk_conj 
   282                                 (ListPair.map rel
   283 				 (map %# largs, map %# rargs)))))) end;
   284   val cons' = filter (fn (_,args) => args<>[]) cons;
   285 in
   286 val inverts = map (fn (con,args) => 
   287                 pgterm (op <<) con args (flat(map (fn arg => [
   288                                 TRY(rtac conjI 1),
   289                                 dres_inst_tac [("fo",sel_of arg)] monofun_cfun_arg 1,
   290                                 asm_full_simp_tac (HOLCF_ss addsimps sel_apps) 1]
   291                                                       ) args))) cons';
   292 val injects = map (fn ((con,args),inv_thm) => 
   293                            pgterm (op ===) con args [
   294                                 etac (antisym_less_inverse RS conjE) 1,
   295                                 dtac inv_thm 1, REPEAT(atac 1),
   296                                 dtac inv_thm 1, REPEAT(atac 1),
   297                                 TRY(safe_tac HOL_cs),
   298                                 REPEAT(rtac antisym_less 1 ORELSE atac 1)] )
   299                   (cons'~~inverts);
   300 end;
   301 
   302 (* ----- theorems concerning one induction step ----------------------------- *)
   303 
   304 val copy_strict = pg[ax_copy_def](mk_trp(strict(dc_copy`%"f"))) [
   305                    asm_simp_tac(HOLCF_ss addsimps [abs_strict, when_strict,
   306                                                    cfst_strict,csnd_strict]) 1];
   307 val copy_apps = map (fn (con,args) => pg [ax_copy_def]
   308                     (lift_defined % (nonlazy_rec args,
   309                         mk_trp(dc_copy`%"f"`(con_app con args) ===
   310                 (con_app2 con (app_rec_arg (cproj (%"f") (length eqs))) args))))
   311                         (map (case_UU_tac (abs_strict::when_strict::con_stricts)
   312                                  1 o vname)
   313                          (filter (fn a => not (is_rec a orelse is_lazy a)) args)
   314                         @[asm_simp_tac (HOLCF_ss addsimps when_apps) 1,
   315                           simp_tac (HOLCF_ss addsimps axs_con_def) 1]))cons;
   316 val copy_stricts = map (fn (con,args) => pg [] (mk_trp(dc_copy`UU`
   317                                         (con_app con args) ===UU))
   318      (let val rews = cfst_strict::csnd_strict::copy_strict::copy_apps@con_rews
   319                          in map (case_UU_tac rews 1) (nonlazy args) @ [
   320                              asm_simp_tac (HOLCF_ss addsimps rews) 1] end))
   321                         (filter (fn (_,args)=>exists is_nonlazy_rec args) cons);
   322 val copy_rews = copy_strict::copy_apps @ copy_stricts;
   323 
   324 in     (iso_rews, exhaust, cases, when_rews,
   325         con_rews, sel_rews, dis_rews, dists_le, dists_eq, inverts, injects,
   326         copy_rews)
   327 end; (* let *)
   328 
   329 
   330 fun comp_theorems thy (comp_dname, eqs: eq list, casess, con_rews, copy_rews) =
   331 let
   332 
   333 val dummy = writeln("Proving induction properties of domain "^comp_dname^"...");
   334 val pg = pg' thy;
   335 
   336 val dnames = map (fst o fst) eqs;
   337 val conss  = map  snd        eqs;
   338 
   339 (* ----- getting the composite axiom and definitions ------------------------ *)
   340 
   341 local val ga = get_axiom thy in
   342 val axs_reach      = map (fn dn => ga (dn ^  "_reach"   )) dnames;
   343 val axs_take_def   = map (fn dn => ga (dn ^  "_take_def")) dnames;
   344 val axs_finite_def = map (fn dn => ga (dn ^"_finite_def")) dnames;
   345 val ax_copy2_def   = ga (comp_dname^ "_copy_def");
   346 val ax_bisim_def   = ga (comp_dname^"_bisim_def");
   347 end; (* local *)
   348 
   349 fun dc_take dn = %%(dn^"_take");
   350 val x_name = idx_name dnames "x"; 
   351 val P_name = idx_name dnames "P";
   352 val n_eqs = length eqs;
   353 
   354 (* ----- theorems concerning finite approximation and finite induction ------ *)
   355 
   356 local
   357   val iterate_Cprod_ss = simpset_of "Fix"
   358                          addsimps [cfst_strict, csnd_strict]addsimps Cprod_rews;
   359   val copy_con_rews  = copy_rews @ con_rews;
   360   val copy_take_defs =(if n_eqs = 1 then [] else [ax_copy2_def]) @ axs_take_def;
   361   val take_stricts=pg copy_take_defs(mk_trp(foldr' mk_conj(map(fn((dn,args),_)=>
   362             strict(dc_take dn $ %"n")) eqs)))([
   363                         nat_ind_tac "n" 1,
   364                         simp_tac iterate_Cprod_ss 1,
   365                         asm_simp_tac (iterate_Cprod_ss addsimps copy_rews)1]);
   366   val take_stricts' = rewrite_rule copy_take_defs take_stricts;
   367   val take_0s = mapn(fn n=> fn dn => pg axs_take_def(mk_trp((dc_take dn $ %%"0")
   368                                                         `%x_name n === UU))[
   369                                 simp_tac iterate_Cprod_ss 1]) 1 dnames;
   370   val c_UU_tac = case_UU_tac (take_stricts'::copy_con_rews) 1;
   371   val take_apps = pg copy_take_defs (mk_trp(foldr' mk_conj 
   372             (flat(map (fn ((dn,_),cons) => map (fn (con,args) => foldr mk_all 
   373         (map vname args,(dc_take dn $ (%%"Suc" $ %"n"))`(con_app con args) ===
   374          con_app2 con (app_rec_arg (fn n=>dc_take (nth_elem(n,dnames))$ %"n"))
   375                               args)) cons) eqs)))) ([
   376                                 simp_tac iterate_Cprod_ss 1,
   377                                 nat_ind_tac "n" 1,
   378                             simp_tac(iterate_Cprod_ss addsimps copy_con_rews) 1,
   379                                 asm_full_simp_tac (HOLCF_ss addsimps 
   380                                       (filter (has_fewer_prems 1) copy_rews)) 1,
   381                                 TRY(safe_tac HOL_cs)] @
   382                         (flat(map (fn ((dn,_),cons) => map (fn (con,args) => 
   383                                 if nonlazy_rec args = [] then all_tac else
   384                                 EVERY(map c_UU_tac (nonlazy_rec args)) THEN
   385                                 asm_full_simp_tac (HOLCF_ss addsimps copy_rews)1
   386                                                            ) cons) eqs)));
   387 in
   388 val take_rews = atomize take_stricts @ take_0s @ atomize take_apps;
   389 end; (* local *)
   390 
   391 local
   392   fun one_con p (con,args) = foldr mk_All (map vname args,
   393         lift_defined (bound_arg (map vname args)) (nonlazy args,
   394         lift (fn arg => %(P_name (1+rec_of arg)) $ bound_arg args arg)
   395          (filter is_rec args,mk_trp(%p $ con_app2 con (bound_arg args) args))));
   396   fun one_eq ((p,cons),concl) = (mk_trp(%p $ UU) ===> 
   397                            foldr (op ===>) (map (one_con p) cons,concl));
   398   fun ind_term concf = foldr one_eq (mapn (fn n => fn x => (P_name n, x))1conss,
   399                         mk_trp(foldr' mk_conj (mapn concf 1 dnames)));
   400   val take_ss = HOL_ss addsimps take_rews;
   401   fun quant_tac i = EVERY(mapn(fn n=> fn _=> res_inst_tac[("x",x_name n)]spec i)
   402                                1 dnames);
   403   fun ind_prems_tac prems = EVERY(flat (map (fn cons => (
   404                                      resolve_tac prems 1 ::
   405                                      flat (map (fn (_,args) => 
   406                                        resolve_tac prems 1 ::
   407                                        map (K(atac 1)) (nonlazy args) @
   408                                        map (K(atac 1)) (filter is_rec args))
   409                                      cons))) conss));
   410   local 
   411     (* check whether every/exists constructor of the n-th part of the equation:
   412        it has a possibly indirectly recursive argument that isn't/is possibly 
   413        indirectly lazy *)
   414     fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
   415           is_rec arg andalso not(rec_of arg mem ns) andalso
   416           ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
   417             rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
   418               (lazy_rec orelse is_lazy arg) (n, (nth_elem(rec_of arg,conss))))
   419           ) o snd) cons;
   420     fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
   421     fun warn (n,cons)  = if all_rec_to [] false (n,cons) then (writeln 
   422         ("WARNING: domain "^nth_elem(n,dnames)^" is empty!"); true) else false;
   423     fun lazy_rec_to ns = rec_to exists Id  lazy_rec_to ns;
   424 
   425   in val n__eqs     = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   426      val is_emptys = map warn n__eqs;
   427      val is_finite = forall (not o lazy_rec_to [] false) n__eqs;
   428   end;
   429 in (* local *)
   430 val finite_ind = pg'' thy [] (ind_term (fn n => fn dn => %(P_name n)$
   431                              (dc_take dn $ %"n" `%(x_name n)))) (fn prems => [
   432                                 quant_tac 1,
   433                                 simp_tac HOL_ss 1,
   434                                 nat_ind_tac "n" 1,
   435                                 simp_tac (take_ss addsimps prems) 1,
   436                                 TRY(safe_tac HOL_cs)]
   437                                 @ flat(map (fn (cons,cases) => [
   438                                  res_inst_tac [("x","x")] cases 1,
   439                                  asm_simp_tac (take_ss addsimps prems) 1]
   440                                  @ flat(map (fn (con,args) => 
   441                                   asm_simp_tac take_ss 1 ::
   442                                   map (fn arg =>
   443                                    case_UU_tac (prems@con_rews) 1 (
   444                            nth_elem(rec_of arg,dnames)^"_take n1`"^vname arg))
   445                                   (filter is_nonlazy_rec args) @ [
   446                                   resolve_tac prems 1] @
   447                                   map (K (atac 1))      (nonlazy args) @
   448                                   map (K (etac spec 1)) (filter is_rec args)) 
   449                                  cons))
   450                                 (conss~~casess)));
   451 
   452 val take_lemmas =mapn(fn n=> fn(dn,ax_reach)=> pg'' thy axs_take_def(mk_All("n",
   453                 mk_trp(dc_take dn $ Bound 0 `%(x_name n) === 
   454                        dc_take dn $ Bound 0 `%(x_name n^"'")))
   455            ===> mk_trp(%(x_name n) === %(x_name n^"'"))) (fn prems => [
   456                         res_inst_tac[("t",x_name n    )](ax_reach RS subst) 1,
   457                         res_inst_tac[("t",x_name n^"'")](ax_reach RS subst) 1,
   458                                 stac fix_def2 1,
   459                                 REPEAT(CHANGED(rtac(contlub_cfun_arg RS ssubst)1
   460                                                THEN chain_tac 1)),
   461                                 stac contlub_cfun_fun 1,
   462                                 stac contlub_cfun_fun 2,
   463                                 rtac lub_equal 3,
   464                                 chain_tac 1,
   465                                 rtac allI 1,
   466                                 resolve_tac prems 1])) 1 (dnames~~axs_reach);
   467 
   468 (* ----- theorems concerning finiteness and induction ----------------------- *)
   469 
   470 val (finites,ind) = if is_finite then
   471   let 
   472     fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %"x" === %"x");
   473     val finite_lemmas1a = map (fn dn => pg [] (mk_trp(defined (%"x")) ===> 
   474         mk_trp(mk_disj(mk_all("n",dc_take dn $ Bound 0 ` %"x" === UU),
   475         take_enough dn)) ===> mk_trp(take_enough dn)) [
   476                                 etac disjE 1,
   477                                 etac notE 1,
   478                                 resolve_tac take_lemmas 1,
   479                                 asm_simp_tac take_ss 1,
   480                                 atac 1]) dnames;
   481     val finite_lemma1b = pg [] (mk_trp (mk_all("n",foldr' mk_conj (mapn 
   482         (fn n => fn ((dn,args),_) => mk_constrainall(x_name n,Type(dn,args),
   483          mk_disj(dc_take dn $ Bound 1 ` Bound 0 === UU,
   484                  dc_take dn $ Bound 1 ` Bound 0 === Bound 0))) 1 eqs)))) ([
   485                                 rtac allI 1,
   486                                 nat_ind_tac "n" 1,
   487                                 simp_tac take_ss 1,
   488                         TRY(safe_tac(empty_cs addSEs[conjE] addSIs[conjI]))] @
   489                                 flat(mapn (fn n => fn (cons,cases) => [
   490                                   simp_tac take_ss 1,
   491                                   rtac allI 1,
   492                                   res_inst_tac [("x",x_name n)] cases 1,
   493                                   asm_simp_tac take_ss 1] @ 
   494                                   flat(map (fn (con,args) => 
   495                                     asm_simp_tac take_ss 1 ::
   496                                     flat(map (fn vn => [
   497                                       eres_inst_tac [("x",vn)] all_dupE 1,
   498                                       etac disjE 1,
   499                                       asm_simp_tac (HOL_ss addsimps con_rews) 1,
   500                                       asm_simp_tac take_ss 1])
   501                                     (nonlazy_rec args)))
   502                                   cons))
   503                                 1 (conss~~casess)));
   504     val finites = map (fn (dn,l1b) => pg axs_finite_def (mk_trp(
   505                                                 %%(dn^"_finite") $ %"x"))[
   506                                 case_UU_tac take_rews 1 "x",
   507                                 eresolve_tac finite_lemmas1a 1,
   508                                 step_tac HOL_cs 1,
   509                                 step_tac HOL_cs 1,
   510                                 cut_facts_tac [l1b] 1,
   511                         fast_tac HOL_cs 1]) (dnames~~atomize finite_lemma1b);
   512   in
   513   (finites,
   514    pg'' thy[](ind_term (fn n => fn dn => %(P_name n) $ %(x_name n)))(fn prems =>
   515                                 TRY(safe_tac HOL_cs) ::
   516                          flat (map (fn (finite,fin_ind) => [
   517                                rtac(rewrite_rule axs_finite_def finite RS exE)1,
   518                                 etac subst 1,
   519                                 rtac fin_ind 1,
   520                                 ind_prems_tac prems]) 
   521                                    (finites~~(atomize finite_ind)) ))
   522 ) end (* let *) else
   523   (mapn (fn n => fn dn => read_instantiate_sg (sign_of thy) 
   524                     [("P",dn^"_finite "^x_name n)] excluded_middle) 1 dnames,
   525    pg'' thy [] (foldr (op ===>) (mapn (fn n => K(mk_trp(%%"adm" $ %(P_name n))))
   526                1 dnames, ind_term (fn n => fn dn => %(P_name n) $ %(x_name n))))
   527                    (fn prems => map (fn ax_reach => rtac (ax_reach RS subst) 1) 
   528                                     axs_reach @ [
   529                                 quant_tac 1,
   530                                 rtac (adm_impl_admw RS wfix_ind) 1,
   531                                 REPEAT_DETERM(rtac adm_all2 1),
   532                                 REPEAT_DETERM(TRY(rtac adm_conj 1) THEN 
   533                                                   rtac adm_subst 1 THEN 
   534                                         cont_tacR 1 THEN resolve_tac prems 1),
   535                                 strip_tac 1,
   536                                 rtac (rewrite_rule axs_take_def finite_ind) 1,
   537                                 ind_prems_tac prems])
   538 )
   539 end; (* local *)
   540 
   541 (* ----- theorem concerning coinduction ------------------------------------- *)
   542 
   543 local
   544   val xs = mapn (fn n => K (x_name n)) 1 dnames;
   545   fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
   546   val take_ss = HOL_ss addsimps take_rews;
   547   val sproj   = prj (fn s => "fst("^s^")") (fn s => "snd("^s^")");
   548   val coind_lemma=pg[ax_bisim_def](mk_trp(mk_imp(%%(comp_dname^"_bisim") $ %"R",
   549                 foldr (fn (x,t)=> mk_all(x,mk_all(x^"'",t))) (xs,
   550                   foldr mk_imp (mapn (fn n => K(proj (%"R") n_eqs n $ 
   551                                       bnd_arg n 0 $ bnd_arg n 1)) 0 dnames,
   552                     foldr' mk_conj (mapn (fn n => fn dn => 
   553                                 (dc_take dn $ %"n" `bnd_arg n 0 === 
   554                                 (dc_take dn $ %"n" `bnd_arg n 1)))0 dnames))))))
   555                              ([ rtac impI 1,
   556                                 nat_ind_tac "n" 1,
   557                                 simp_tac take_ss 1,
   558                                 safe_tac HOL_cs] @
   559                                 flat(mapn (fn n => fn x => [
   560                                   rotate_tac (n+1) 1,
   561                                   etac all2E 1,
   562                                   eres_inst_tac [("P1", sproj "R" n_eqs n^
   563                                         " "^x^" "^x^"'")](mp RS disjE) 1,
   564                                   TRY(safe_tac HOL_cs),
   565                                   REPEAT(CHANGED(asm_simp_tac take_ss 1))]) 
   566                                 0 xs));
   567 in
   568 val coind = pg [] (mk_trp(%%(comp_dname^"_bisim") $ %"R") ===>
   569                 foldr (op ===>) (mapn (fn n => fn x => 
   570                   mk_trp(proj (%"R") n_eqs n $ %x $ %(x^"'"))) 0 xs,
   571                   mk_trp(foldr' mk_conj (map (fn x => %x === %(x^"'")) xs)))) ([
   572                                 TRY(safe_tac HOL_cs)] @
   573                                 flat(map (fn take_lemma => [
   574                                   rtac take_lemma 1,
   575                                   cut_facts_tac [coind_lemma] 1,
   576                                   fast_tac HOL_cs 1])
   577                                 take_lemmas));
   578 end; (* local *)
   579 
   580 
   581 in (take_rews, take_lemmas, finites, finite_ind, ind, coind)
   582 
   583 end; (* let *)
   584 end; (* local *)
   585 end; (* struct *)