src/HOLCF/domain/theorems.ML
author wenzelm
Tue Feb 07 19:56:56 2006 +0100 (2006-02-07)
changeset 18972 2905d1805e1e
parent 18728 6790126ab5f6
child 19549 a8ed346f8635
permissions -rw-r--r--
adapted Sign.infer_types;
     1 (*  Title:      HOLCF/domain/theorems.ML
     2     ID:         $Id$
     3     Author:     David von Oheimb
     4                 New proofs/tactics by Brian Huffman
     5 
     6 Proof generator for domain section.
     7 *)
     8 
     9 val HOLCF_ss = simpset();
    10 
    11 structure Domain_Theorems = struct
    12 
    13 local
    14 
    15 open Domain_Library;
    16 infixr 0 ===>;infixr 0 ==>;infix 0 == ; 
    17 infix 1 ===; infix 1 ~= ; infix 1 <<; infix 1 ~<<;
    18 infix 9 `   ; infix 9 `% ; infix 9 `%%; infixr 9 oo;
    19 
    20 (* ----- general proof facilities ------------------------------------------- *)
    21 
    22 (* FIXME better avoid this low-level stuff *)
    23 fun inferT sg pre_tm =
    24   #1 (Sign.infer_types (Sign.pp sg) sg (Sign.consts_of sg) (K NONE) (K NONE) [] true ([pre_tm],propT));
    25 
    26 fun pg'' thy defs t tacs =
    27   let val t' = inferT thy t in
    28     standard (Goal.prove thy [] (Logic.strip_imp_prems t') (Logic.strip_imp_concl t')
    29       (fn prems => rewrite_goals_tac defs THEN EVERY (tacs (map (rewrite_rule defs) prems))))
    30   end;
    31 
    32 fun pg'  thy defs t tacsf=pg'' thy defs t (fn []   => tacsf 
    33                                 | prems=> (cut_facts_tac prems 1)::tacsf);
    34 
    35 fun case_UU_tac rews i v =      case_tac (v^"=UU") i THEN
    36                                 asm_simp_tac (HOLCF_ss addsimps rews) i;
    37 
    38 val chain_tac = REPEAT_DETERM o resolve_tac 
    39                 [chain_iterate, ch2ch_Rep_CFunR, ch2ch_Rep_CFunL];
    40 
    41 (* ----- general proofs ----------------------------------------------------- *)
    42 
    43 val all2E = prove_goal HOL.thy "[| !x y . P x y; P x y ==> R |] ==> R"
    44  (fn prems =>[
    45                                 resolve_tac prems 1,
    46                                 cut_facts_tac prems 1,
    47                                 fast_tac HOL_cs 1]);
    48 
    49 val dist_eqI = prove_goal (the_context ()) "!!x::'a::po. ~ x << y ==> x ~= y" 
    50              (fn prems => [
    51                (blast_tac (claset() addDs [antisym_less_inverse]) 1)]);
    52 (*
    53 infixr 0 y;
    54 val b = 0;
    55 fun _ y t = by t;
    56 fun g defs t = let val sg = sign_of thy;
    57                      val ct = Thm.cterm_of sg (inferT sg t);
    58                  in goalw_cterm defs ct end;
    59 *)
    60 
    61 in
    62 
    63 fun theorems (((dname,_),cons) : eq, eqs : eq list) thy =
    64 let
    65 
    66 val dummy = writeln ("Proving isomorphism properties of domain "^dname^" ...");
    67 val pg = pg' thy;
    68 
    69 (* ----- getting the axioms and definitions --------------------------------- *)
    70 
    71 local fun ga s dn = get_thm thy (Name (dn ^ "." ^ s)) in
    72 val ax_abs_iso    = ga "abs_iso"  dname;
    73 val ax_rep_iso    = ga "rep_iso"  dname;
    74 val ax_when_def   = ga "when_def" dname;
    75 val axs_con_def   = map (fn (con,_) => ga (extern_name con^"_def") dname) cons;
    76 val axs_dis_def   = map (fn (con,_) => ga (   dis_name con^"_def") dname) cons;
    77 val axs_mat_def   = map (fn (con,_) => ga (   mat_name con^"_def") dname) cons;
    78 val axs_pat_def   = map (fn (con,_) => ga (   pat_name con^"_def") dname) cons;
    79 val axs_sel_def   = List.concat(map (fn (_,args) => List.mapPartial (fn arg =>
    80                  Option.map (fn sel => ga (sel^"_def") dname) (sel_of arg)) args)
    81 									  cons);
    82 val ax_copy_def   = ga "copy_def" dname;
    83 end; (* local *)
    84 
    85 (* ----- theorems concerning the isomorphism -------------------------------- *)
    86 
    87 val dc_abs  = %%:(dname^"_abs");
    88 val dc_rep  = %%:(dname^"_rep");
    89 val dc_copy = %%:(dname^"_copy");
    90 val x_name = "x";
    91 
    92 val iso_locale = iso_intro OF [ax_abs_iso, ax_rep_iso];
    93 val abs_strict = ax_rep_iso RS (allI RS retraction_strict);
    94 val rep_strict = ax_abs_iso RS (allI RS retraction_strict);
    95 val abs_defin' = iso_locale RS iso_abs_defin';
    96 val rep_defin' = iso_locale RS iso_rep_defin';
    97 val iso_rews = map standard [ax_abs_iso,ax_rep_iso,abs_strict,rep_strict];
    98 
    99 (* ----- generating beta reduction rules from definitions-------------------- *)
   100 
   101 local
   102   fun arglist (Const _ $ Abs (s,_,t)) = let
   103         val (vars,body) = arglist t
   104         in  (s :: vars, body) end
   105   |   arglist t = ([],t);
   106   fun bind_fun vars t = Library.foldr mk_All (vars,t);
   107   fun bound_vars 0 = [] | bound_vars i = (Bound (i-1) :: bound_vars (i-1));
   108 in
   109   fun appl_of_def def = let
   110         val (_ $ con $ lam) = concl_of def;
   111         val (vars, rhs) = arglist lam;
   112         val lhs = list_ccomb (con, bound_vars (length vars));
   113         val appl = bind_fun vars (lhs == rhs);
   114         val cs = ContProc.cont_thms lam;
   115         val betas = map (fn c => mk_meta_eq (c RS beta_cfun)) cs;
   116         in pg (def::betas) appl [rtac reflexive_thm 1] end;
   117 end;
   118 
   119 val when_appl = appl_of_def ax_when_def;
   120 val con_appls = map appl_of_def axs_con_def;
   121 
   122 local
   123   fun arg2typ n arg = let val t = TVar (("'a",n),pcpoS)
   124                       in (n+1, if is_lazy arg then mk_uT t else t) end;
   125   fun args2typ n [] = (n,oneT)
   126   |   args2typ n [arg] = arg2typ n arg
   127   |   args2typ n (arg::args) = let val (n1,t1) = arg2typ n arg;
   128                                    val (n2,t2) = args2typ n1 args
   129 			       in  (n2, mk_sprodT (t1, t2)) end;
   130   fun cons2typ n [] = (n,oneT)
   131   |   cons2typ n [con] = args2typ n (snd con)
   132   |   cons2typ n (con::cons) = let val (n1,t1) = args2typ n (snd con);
   133                                    val (n2,t2) = cons2typ n1 cons
   134 			       in  (n2, mk_ssumT (t1, t2)) end;
   135 in
   136   fun cons2ctyp cons = ctyp_of (sign_of thy) (snd (cons2typ 1 cons));
   137 end;
   138 
   139 local 
   140   val iso_swap = iso_locale RS iso_iso_swap;
   141   fun one_con (con,args) = let val vns = map vname args in
   142     Library.foldr mk_ex (vns, foldr1 mk_conj ((%:x_name === con_app2 con %: vns)::
   143                               map (defined o %:) (nonlazy args))) end;
   144   val exh = foldr1 mk_disj ((%:x_name===UU)::map one_con cons);
   145   val my_ctyp = cons2ctyp cons;
   146   val thm1 = instantiate' [SOME my_ctyp] [] exh_start;
   147   val thm2 = rewrite_rule (map mk_meta_eq ex_defined_iffs) thm1;
   148   val thm3 = rewrite_rule [mk_meta_eq conj_assoc] thm2;
   149 in
   150 val exhaust = pg con_appls (mk_trp exh)[
   151 (* first 3 rules replace "x = UU \/ P" with "rep$x = UU \/ P" *)
   152 			rtac disjE 1,
   153 			etac (rep_defin' RS disjI1) 2,
   154 			etac disjI2 2,
   155 			rewrite_goals_tac [mk_meta_eq iso_swap],
   156 			rtac thm3 1];
   157 val casedist = standard (rewrite_rule exh_casedists (exhaust RS exh_casedist0));
   158 end;
   159 
   160 local 
   161   fun bind_fun t = Library.foldr mk_All (when_funs cons,t);
   162   fun bound_fun i _ = Bound (length cons - i);
   163   val when_app  = list_ccomb (%%:(dname^"_when"), mapn bound_fun 1 cons);
   164 in
   165 val when_strict = pg [when_appl, mk_meta_eq rep_strict]
   166 			(bind_fun(mk_trp(strict when_app)))
   167 			[resolve_tac [sscase1,ssplit1,strictify1] 1];
   168 val when_apps = let fun one_when n (con,args) = pg (when_appl :: con_appls)
   169                 (bind_fun (lift_defined %: (nonlazy args, 
   170                 mk_trp(when_app`(con_app con args) ===
   171                        list_ccomb(bound_fun n 0,map %# args)))))[
   172                 asm_simp_tac (HOLCF_ss addsimps [ax_abs_iso]) 1];
   173         in mapn one_when 1 cons end;
   174 end;
   175 val when_rews = when_strict::when_apps;
   176 
   177 (* ----- theorems concerning the constructors, discriminators and selectors - *)
   178 
   179 val dis_rews = let
   180   val dis_stricts = map (fn (con,_) => pg axs_dis_def (mk_trp(
   181                              strict(%%:(dis_name con)))) [
   182                                 rtac when_strict 1]) cons;
   183   val dis_apps = let fun one_dis c (con,args)= pg axs_dis_def
   184                    (lift_defined %: (nonlazy args,
   185                         (mk_trp((%%:(dis_name c))`(con_app con args) ===
   186                               %%:(if con=c then TT_N else FF_N))))) [
   187                                 asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   188         in List.concat(map (fn (c,_) => map (one_dis c) cons) cons) end;
   189   val dis_defins = map (fn (con,args) => pg [] (defined(%:x_name) ==> 
   190                       defined(%%:(dis_name con)`%x_name)) [
   191                                 rtac casedist 1,
   192                                 contr_tac 1,
   193                                 DETERM_UNTIL_SOLVED (CHANGED(asm_simp_tac 
   194                                         (HOLCF_ss addsimps dis_apps) 1))]) cons;
   195 in dis_stricts @ dis_defins @ dis_apps end;
   196 
   197 val mat_rews = let
   198   val mat_stricts = map (fn (con,_) => pg axs_mat_def (mk_trp(
   199                              strict(%%:(mat_name con)))) [
   200                                 rtac when_strict 1]) cons;
   201   val mat_apps = let fun one_mat c (con,args)= pg axs_mat_def
   202                    (lift_defined %: (nonlazy args,
   203                         (mk_trp((%%:(mat_name c))`(con_app con args) ===
   204                               (if con=c
   205                                   then %%:returnN`(mk_ctuple (map %# args))
   206                                   else %%:failN)))))
   207                    [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   208         in List.concat(map (fn (c,_) => map (one_mat c) cons) cons) end;
   209 in mat_stricts @ mat_apps end;
   210 
   211 val pat_rews = let
   212   fun ps args = mapn (fn n => fn _ => %:("pat" ^ string_of_int n)) 1 args;
   213   fun pat_lhs (con,args) = %%:branchN $ list_comb (%%:(pat_name con), ps args);
   214   fun pat_rhs (con,[]) = %%:returnN ` ((%:"rhs") ` HOLogic.unit)
   215   |   pat_rhs (con,args) =
   216         (%%:branchN $ foldr1 cpair_pat (ps args))`(%:"rhs")`(mk_ctuple (map %# args));
   217   val pat_stricts = map (fn (con,args) => pg (branch_def::axs_pat_def)
   218                       (mk_trp(strict(pat_lhs (con,args)`(%:"rhs"))))
   219                       [simp_tac (HOLCF_ss addsimps [when_strict]) 1]) cons;
   220   val pat_apps = let fun one_pat c (con,args) = pg (branch_def::axs_pat_def)
   221                    (lift_defined %: (nonlazy args,
   222                         (mk_trp((pat_lhs c)`(%:"rhs")`(con_app con args) ===
   223                               (if con = fst c then pat_rhs c else %%:failN)))))
   224                    [asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   225         in List.concat (map (fn c => map (one_pat c) cons) cons) end;
   226 in pat_stricts @ pat_apps end;
   227 
   228 val con_stricts = List.concat(map (fn (con,args) => map (fn vn =>
   229                         pg con_appls
   230                            (mk_trp(con_app2 con (fn arg => if vname arg = vn 
   231                                         then UU else %# arg) args === UU))[
   232                                 asm_simp_tac (HOLCF_ss addsimps [abs_strict]) 1]
   233                         ) (nonlazy args)) cons);
   234 val con_defins = map (fn (con,args) => pg []
   235                         (lift_defined %: (nonlazy args,
   236                                 mk_trp(defined(con_app con args)))) ([
   237                           rtac rev_contrapos 1, 
   238                           eres_inst_tac [("f",dis_name con)] cfun_arg_cong 1,
   239                           asm_simp_tac (HOLCF_ss addsimps dis_rews) 1] )) cons;
   240 val con_rews = con_stricts @ con_defins;
   241 
   242 val con_compacts =
   243   let
   244     val rules = [compact_sinl, compact_sinr, compact_spair, compact_up, compact_ONE];
   245     fun one_compact (con,args) = pg con_appls
   246       (lift (fn x => %%:compactN $ %#x) (args, mk_trp(%%:compactN $ (con_app con args))))
   247       [rtac (iso_locale RS iso_compact_abs) 1, REPEAT (resolve_tac rules 1 ORELSE atac 1)];
   248   in map one_compact cons end;
   249 
   250 val sel_stricts = let fun one_sel sel = pg axs_sel_def (mk_trp(strict(%%:sel))) [
   251                                 simp_tac (HOLCF_ss addsimps when_rews) 1];
   252 in List.concat(map (fn (_,args) => List.mapPartial (fn arg => Option.map one_sel (sel_of arg)) args) cons) end;
   253 val sel_apps = let fun one_sel c n sel = map (fn (con,args) => 
   254                 let val nlas = nonlazy args;
   255                     val vns  = map vname args;
   256                 in pg axs_sel_def (lift_defined %:
   257                    (List.filter (fn v => con=c andalso (v<>List.nth(vns,n))) nlas,
   258                                 mk_trp((%%:sel)`(con_app con args) === 
   259                                 (if con=c then %:(List.nth(vns,n)) else UU))))
   260                             ( (if con=c then [] 
   261                        else map(case_UU_tac(when_rews@con_stricts)1) nlas)
   262                      @(if con=c andalso ((List.nth(vns,n)) mem nlas)
   263                                  then[case_UU_tac (when_rews @ con_stricts) 1 
   264                                                   (List.nth(vns,n))] else [])
   265                      @ [asm_simp_tac(HOLCF_ss addsimps when_rews)1])end) cons;
   266 in List.concat(map  (fn (c,args) => 
   267      List.concat(List.mapPartial I (mapn (fn n => fn arg => Option.map (one_sel c n) (sel_of arg)) 0 args))) cons) end;
   268 val sel_defins = if length cons=1 then List.mapPartial (fn arg => Option.map (fn sel => pg [](defined(%:x_name)==> 
   269                         defined(%%:sel`%x_name)) [
   270                                 rtac casedist 1,
   271                                 contr_tac 1,
   272                                 DETERM_UNTIL_SOLVED (CHANGED(asm_simp_tac 
   273                                              (HOLCF_ss addsimps sel_apps) 1))])(sel_of arg)) 
   274                  (filter_out is_lazy (snd(hd cons))) else [];
   275 val sel_rews = sel_stricts @ sel_defins @ sel_apps;
   276 
   277 val distincts_le = let
   278     fun dist (con1, args1) (con2, args2) = pg []
   279               (lift_defined %: ((nonlazy args1),
   280                         (mk_trp (con_app con1 args1 ~<< con_app con2 args2))))([
   281                         rtac rev_contrapos 1,
   282                         eres_inst_tac[("f",dis_name con1)] monofun_cfun_arg 1]
   283                       @map(case_UU_tac (con_stricts @ dis_rews)1)(nonlazy args2)
   284                       @[asm_simp_tac (HOLCF_ss addsimps dis_rews) 1]);
   285     fun distinct (con1,args1) (con2,args2) =
   286         let val arg1 = (con1, args1)
   287             val arg2 = (con2,
   288 			ListPair.map (fn (arg,vn) => upd_vname (K vn) arg)
   289                         (args2, variantlist(map vname args2,map vname args1)))
   290         in [dist arg1 arg2, dist arg2 arg1] end;
   291     fun distincts []      = []
   292     |   distincts (c::cs) = (map (distinct c) cs) :: distincts cs;
   293 in distincts cons end;
   294 val dist_les = List.concat (List.concat distincts_le);
   295 val dist_eqs = let
   296     fun distinct (_,args1) ((_,args2),leqs) = let
   297         val (le1,le2) = (hd leqs, hd(tl leqs));
   298         val (eq1,eq2) = (le1 RS dist_eqI, le2 RS dist_eqI) in
   299         if nonlazy args1 = [] then [eq1, eq1 RS not_sym] else
   300         if nonlazy args2 = [] then [eq2, eq2 RS not_sym] else
   301                                         [eq1, eq2] end;
   302     fun distincts []      = []
   303     |   distincts ((c,leqs)::cs) = List.concat
   304 	            (ListPair.map (distinct c) ((map #1 cs),leqs)) @
   305 		    distincts cs;
   306     in map standard (distincts (cons~~distincts_le)) end;
   307 
   308 local 
   309   fun pgterm rel con args =
   310     let
   311       fun append s = upd_vname(fn v => v^s);
   312       val (largs,rargs) = (args, map (append "'") args);
   313       val concl = mk_trp (foldr1 mk_conj (ListPair.map rel (map %# largs, map %# rargs)));
   314       val prem = mk_trp (rel(con_app con largs,con_app con rargs));
   315       val prop = prem ===> lift_defined %: (nonlazy largs, concl);
   316     in pg con_appls prop end;
   317   val cons' = List.filter (fn (_,args) => args<>[]) cons;
   318 in
   319 val inverts =
   320   let
   321     val abs_less = ax_abs_iso RS (allI RS injection_less) RS iffD1;
   322     val tacs = [
   323       dtac abs_less 1,
   324       REPEAT (dresolve_tac [sinl_less RS iffD1, sinr_less RS iffD1] 1),
   325       asm_full_simp_tac (HOLCF_ss addsimps [spair_less]) 1];
   326   in map (fn (con,args) => pgterm (op <<) con args tacs) cons' end;
   327 val injects =
   328   let
   329     val abs_eq = ax_abs_iso RS (allI RS injection_eq) RS iffD1;
   330     val tacs = [
   331       dtac abs_eq 1,
   332       REPEAT (dresolve_tac [sinl_inject, sinr_inject] 1),
   333       asm_full_simp_tac (HOLCF_ss addsimps [spair_eq]) 1];
   334   in map (fn (con,args) => pgterm (op ===) con args tacs) cons' end;
   335 end;
   336 
   337 (* ----- theorems concerning one induction step ----------------------------- *)
   338 
   339 val copy_strict = pg[ax_copy_def](mk_trp(strict(dc_copy`%"f"))) [
   340                    asm_simp_tac(HOLCF_ss addsimps [abs_strict, when_strict]) 1];
   341 val copy_apps = map (fn (con,args) => pg [ax_copy_def]
   342                     (lift_defined %: (nonlazy_rec args,
   343                         mk_trp(dc_copy`%"f"`(con_app con args) ===
   344                 (con_app2 con (app_rec_arg (cproj (%:"f") eqs)) args))))
   345                         (map (case_UU_tac (abs_strict::when_strict::con_stricts)
   346                                  1 o vname)
   347                          (List.filter (fn a => not (is_rec a orelse is_lazy a)) args)
   348                         @[asm_simp_tac (HOLCF_ss addsimps when_apps) 1]))cons;
   349 val copy_stricts = map (fn (con,args) => pg [] (mk_trp(dc_copy`UU`
   350                                         (con_app con args) ===UU))
   351      (let val rews = copy_strict::copy_apps@con_rews
   352                          in map (case_UU_tac rews 1) (nonlazy args) @ [
   353                              asm_simp_tac (HOLCF_ss addsimps rews) 1] end))
   354                         (List.filter (fn (_,args)=>exists is_nonlazy_rec args) cons);
   355 val copy_rews = copy_strict::copy_apps @ copy_stricts;
   356 in thy |> Theory.add_path (Sign.base_name dname)
   357        |> (snd o (PureThy.add_thmss (map Thm.no_attributes [
   358 		("iso_rews" , iso_rews  ),
   359 		("exhaust"  , [exhaust] ),
   360 		("casedist" , [casedist]),
   361 		("when_rews", when_rews ),
   362 		("compacts", con_compacts),
   363 		("con_rews", con_rews),
   364 		("sel_rews", sel_rews),
   365 		("dis_rews", dis_rews),
   366 		("pat_rews", pat_rews),
   367 		("dist_les", dist_les),
   368 		("dist_eqs", dist_eqs),
   369 		("inverts" , inverts ),
   370 		("injects" , injects ),
   371 		("copy_rews", copy_rews)])))
   372        |> (snd o PureThy.add_thmss [(("match_rews", mat_rews), [Simplifier.simp_add])])
   373        |> Theory.parent_path |> rpair (iso_rews @ when_rews @ con_rews @ sel_rews @ dis_rews @
   374                  pat_rews @ dist_les @ dist_eqs @ copy_rews)
   375 end; (* let *)
   376 
   377 fun comp_theorems (comp_dnam, eqs: eq list) thy =
   378 let
   379 val dnames = map (fst o fst) eqs;
   380 val conss  = map  snd        eqs;
   381 val comp_dname = Sign.full_name (sign_of thy) comp_dnam;
   382 
   383 val d = writeln("Proving induction properties of domain "^comp_dname^" ...");
   384 val pg = pg' thy;
   385 
   386 (* ----- getting the composite axiom and definitions ------------------------ *)
   387 
   388 local fun ga s dn = get_thm thy (Name (dn ^ "." ^ s)) in
   389 val axs_reach      = map (ga "reach"     ) dnames;
   390 val axs_take_def   = map (ga "take_def"  ) dnames;
   391 val axs_finite_def = map (ga "finite_def") dnames;
   392 val ax_copy2_def   =      ga "copy_def"  comp_dnam;
   393 val ax_bisim_def   =      ga "bisim_def" comp_dnam;
   394 end; (* local *)
   395 
   396 local fun gt  s dn = get_thm  thy (Name (dn ^ "." ^ s));
   397       fun gts s dn = get_thms thy (Name (dn ^ "." ^ s)) in
   398 val cases     =       map (gt  "casedist" ) dnames;
   399 val con_rews  = List.concat (map (gts "con_rews" ) dnames);
   400 val copy_rews = List.concat (map (gts "copy_rews") dnames);
   401 end; (* local *)
   402 
   403 fun dc_take dn = %%:(dn^"_take");
   404 val x_name = idx_name dnames "x"; 
   405 val P_name = idx_name dnames "P";
   406 val n_eqs = length eqs;
   407 
   408 (* ----- theorems concerning finite approximation and finite induction ------ *)
   409 
   410 local
   411   val iterate_Cprod_ss = simpset_of Fix.thy;
   412   val copy_con_rews  = copy_rews @ con_rews;
   413   val copy_take_defs = (if n_eqs = 1 then [] else [ax_copy2_def]) @ axs_take_def;
   414   val take_stricts=pg copy_take_defs(mk_trp(foldr1 mk_conj(map(fn((dn,args),_)=>
   415             strict(dc_take dn $ %:"n")) eqs))) ([
   416                         induct_tac "n" 1,
   417                         simp_tac iterate_Cprod_ss 1,
   418                         asm_simp_tac (iterate_Cprod_ss addsimps copy_rews)1]);
   419   val take_stricts' = rewrite_rule copy_take_defs take_stricts;
   420   val take_0s = mapn(fn n=> fn dn => pg axs_take_def(mk_trp((dc_take dn $ %%:"0")
   421                                                         `%x_name n === UU))[
   422                                 simp_tac iterate_Cprod_ss 1]) 1 dnames;
   423   val c_UU_tac = case_UU_tac (take_stricts'::copy_con_rews) 1;
   424   val take_apps = pg copy_take_defs (mk_trp(foldr1 mk_conj 
   425             (List.concat(map (fn ((dn,_),cons) => map (fn (con,args) => Library.foldr mk_all 
   426         (map vname args,(dc_take dn $ (%%:"Suc" $ %:"n"))`(con_app con args) ===
   427          con_app2 con (app_rec_arg (fn n=>dc_take (List.nth(dnames,n))$ %:"n"))
   428                               args)) cons) eqs)))) ([
   429                                 simp_tac iterate_Cprod_ss 1,
   430                                 induct_tac "n" 1,
   431                             simp_tac(iterate_Cprod_ss addsimps copy_con_rews) 1,
   432                                 asm_full_simp_tac (HOLCF_ss addsimps 
   433                                       (List.filter (has_fewer_prems 1) copy_rews)) 1,
   434                                 TRY(safe_tac HOL_cs)] @
   435                         (List.concat(map (fn ((dn,_),cons) => map (fn (con,args) => 
   436                                 if nonlazy_rec args = [] then all_tac else
   437                                 EVERY(map c_UU_tac (nonlazy_rec args)) THEN
   438                                 asm_full_simp_tac (HOLCF_ss addsimps copy_rews)1
   439                                                            ) cons) eqs)));
   440 in
   441 val take_rews = map standard (atomize take_stricts @ take_0s @ atomize take_apps);
   442 end; (* local *)
   443 
   444 local
   445   fun one_con p (con,args) = Library.foldr mk_All (map vname args,
   446         lift_defined (bound_arg (map vname args)) (nonlazy args,
   447         lift (fn arg => %:(P_name (1+rec_of arg)) $ bound_arg args arg)
   448          (List.filter is_rec args,mk_trp(%:p $ con_app2 con (bound_arg args) args))));
   449   fun one_eq ((p,cons),concl) = (mk_trp(%:p $ UU) ===> 
   450                            Library.foldr (op ===>) (map (one_con p) cons,concl));
   451   fun ind_term concf = Library.foldr one_eq (mapn (fn n => fn x => (P_name n, x))1conss,
   452                         mk_trp(foldr1 mk_conj (mapn concf 1 dnames)));
   453   val take_ss = HOL_ss addsimps take_rews;
   454   fun quant_tac i = EVERY(mapn(fn n=> fn _=> res_inst_tac[("x",x_name n)]spec i)
   455                                1 dnames);
   456   fun ind_prems_tac prems = EVERY(List.concat (map (fn cons => (
   457                                      resolve_tac prems 1 ::
   458                                      List.concat (map (fn (_,args) => 
   459                                        resolve_tac prems 1 ::
   460                                        map (K(atac 1)) (nonlazy args) @
   461                                        map (K(atac 1)) (List.filter is_rec args))
   462                                      cons))) conss));
   463   local 
   464     (* check whether every/exists constructor of the n-th part of the equation:
   465        it has a possibly indirectly recursive argument that isn't/is possibly 
   466        indirectly lazy *)
   467     fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
   468           is_rec arg andalso not(rec_of arg mem ns) andalso
   469           ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
   470             rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
   471               (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
   472           ) o snd) cons;
   473     fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
   474     fun warn (n,cons)  = if all_rec_to [] false (n,cons) then (warning
   475         ("domain "^List.nth(dnames,n)^" is empty!"); true) else false;
   476     fun lazy_rec_to ns = rec_to exists I  lazy_rec_to ns;
   477 
   478   in val n__eqs     = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   479      val is_emptys = map warn n__eqs;
   480      val is_finite = forall (not o lazy_rec_to [] false) n__eqs;
   481   end;
   482 in (* local *)
   483 val finite_ind = pg'' thy [] (ind_term (fn n => fn dn => %:(P_name n)$
   484                              (dc_take dn $ %:"n" `%(x_name n)))) (fn prems => [
   485                                 quant_tac 1,
   486                                 simp_tac HOL_ss 1,
   487                                 induct_tac "n" 1,
   488                                 simp_tac (take_ss addsimps prems) 1,
   489                                 TRY(safe_tac HOL_cs)]
   490                                 @ List.concat(map (fn (cons,cases) => [
   491                                  res_inst_tac [("x","x")] cases 1,
   492                                  asm_simp_tac (take_ss addsimps prems) 1]
   493                                  @ List.concat(map (fn (con,args) => 
   494                                   asm_simp_tac take_ss 1 ::
   495                                   map (fn arg =>
   496                                    case_UU_tac (prems@con_rews) 1 (
   497                            List.nth(dnames,rec_of arg)^"_take n$"^vname arg))
   498                                   (List.filter is_nonlazy_rec args) @ [
   499                                   resolve_tac prems 1] @
   500                                   map (K (atac 1))      (nonlazy args) @
   501                                   map (K (etac spec 1)) (List.filter is_rec args)) 
   502                                  cons))
   503                                 (conss~~cases)));
   504 
   505 val take_lemmas =mapn(fn n=> fn(dn,ax_reach)=> pg'' thy axs_take_def(mk_All("n",
   506                 mk_trp(dc_take dn $ Bound 0 `%(x_name n) === 
   507                        dc_take dn $ Bound 0 `%(x_name n^"'")))
   508            ===> mk_trp(%:(x_name n) === %:(x_name n^"'"))) (fn prems => [
   509                         res_inst_tac[("t",x_name n    )](ax_reach RS subst) 1,
   510                         res_inst_tac[("t",x_name n^"'")](ax_reach RS subst) 1,
   511                                 stac fix_def2 1,
   512                                 REPEAT(CHANGED(rtac(contlub_cfun_arg RS ssubst)1
   513                                                THEN chain_tac 1)),
   514                                 stac contlub_cfun_fun 1,
   515                                 stac contlub_cfun_fun 2,
   516                                 rtac lub_equal 3,
   517                                 chain_tac 1,
   518                                 rtac allI 1,
   519                                 resolve_tac prems 1])) 1 (dnames~~axs_reach);
   520 
   521 (* ----- theorems concerning finiteness and induction ----------------------- *)
   522 
   523 val (finites,ind) = if is_finite then
   524   let 
   525     fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %:"x" === %:"x");
   526     val finite_lemmas1a = map (fn dn => pg [] (mk_trp(defined (%:"x")) ===> 
   527         mk_trp(mk_disj(mk_all("n",dc_take dn $ Bound 0 ` %:"x" === UU),
   528         take_enough dn)) ===> mk_trp(take_enough dn)) [
   529                                 etac disjE 1,
   530                                 etac notE 1,
   531                                 resolve_tac take_lemmas 1,
   532                                 asm_simp_tac take_ss 1,
   533                                 atac 1]) dnames;
   534     val finite_lemma1b = pg [] (mk_trp (mk_all("n",foldr1 mk_conj (mapn 
   535         (fn n => fn ((dn,args),_) => mk_constrainall(x_name n,Type(dn,args),
   536          mk_disj(dc_take dn $ Bound 1 ` Bound 0 === UU,
   537                  dc_take dn $ Bound 1 ` Bound 0 === Bound 0))) 1 eqs)))) ([
   538                                 rtac allI 1,
   539                                 induct_tac "n" 1,
   540                                 simp_tac take_ss 1,
   541                         TRY(safe_tac(empty_cs addSEs[conjE] addSIs[conjI]))] @
   542                                 List.concat(mapn (fn n => fn (cons,cases) => [
   543                                   simp_tac take_ss 1,
   544                                   rtac allI 1,
   545                                   res_inst_tac [("x",x_name n)] cases 1,
   546                                   asm_simp_tac take_ss 1] @ 
   547                                   List.concat(map (fn (con,args) => 
   548                                     asm_simp_tac take_ss 1 ::
   549                                     List.concat(map (fn vn => [
   550                                       eres_inst_tac [("x",vn)] all_dupE 1,
   551                                       etac disjE 1,
   552                                       asm_simp_tac (HOL_ss addsimps con_rews) 1,
   553                                       asm_simp_tac take_ss 1])
   554                                     (nonlazy_rec args)))
   555                                   cons))
   556                                 1 (conss~~cases)));
   557     val finites = map (fn (dn,l1b) => pg axs_finite_def (mk_trp(
   558                                                 %%:(dn^"_finite") $ %:"x"))[
   559                                 case_UU_tac take_rews 1 "x",
   560                                 eresolve_tac finite_lemmas1a 1,
   561                                 step_tac HOL_cs 1,
   562                                 step_tac HOL_cs 1,
   563                                 cut_facts_tac [l1b] 1,
   564                         fast_tac HOL_cs 1]) (dnames~~atomize finite_lemma1b);
   565   in
   566   (finites,
   567    pg'' thy[](ind_term (fn n => fn dn => %:(P_name n) $ %:(x_name n)))(fn prems =>
   568                                 TRY(safe_tac HOL_cs) ::
   569                          List.concat (map (fn (finite,fin_ind) => [
   570                                rtac(rewrite_rule axs_finite_def finite RS exE)1,
   571                                 etac subst 1,
   572                                 rtac fin_ind 1,
   573                                 ind_prems_tac prems]) 
   574                                    (finites~~(atomize finite_ind)) ))
   575 ) end (* let *) else
   576   (mapn (fn n => fn dn => read_instantiate_sg (sign_of thy) 
   577                     [("P",dn^"_finite "^x_name n)] excluded_middle) 1 dnames,
   578    pg'' thy [] (Library.foldr (op ===>) (mapn (fn n => K(mk_trp(%%:admN $ %:(P_name n))))
   579                1 dnames, ind_term (fn n => fn dn => %:(P_name n) $ %:(x_name n))))
   580                    (fn prems => map (fn ax_reach => rtac (ax_reach RS subst) 1) 
   581                                     axs_reach @ [
   582                                 quant_tac 1,
   583                                 rtac (adm_impl_admw RS wfix_ind) 1,
   584                                  REPEAT_DETERM(rtac adm_all2 1),
   585                                  REPEAT_DETERM(TRY(rtac adm_conj 1) THEN 
   586                                                    rtac adm_subst 1 THEN 
   587                                         cont_tacR 1 THEN resolve_tac prems 1),
   588                                 strip_tac 1,
   589                                 rtac (rewrite_rule axs_take_def finite_ind) 1,
   590                                 ind_prems_tac prems])
   591   handle ERROR _ => (warning "Cannot prove infinite induction rule"; refl))
   592 end; (* local *)
   593 
   594 (* ----- theorem concerning coinduction ------------------------------------- *)
   595 
   596 local
   597   val xs = mapn (fn n => K (x_name n)) 1 dnames;
   598   fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
   599   val take_ss = HOL_ss addsimps take_rews;
   600   val sproj   = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
   601   val coind_lemma=pg[ax_bisim_def](mk_trp(mk_imp(%%:(comp_dname^"_bisim") $ %:"R",
   602                 Library.foldr (fn (x,t)=> mk_all(x,mk_all(x^"'",t))) (xs,
   603                   Library.foldr mk_imp (mapn (fn n => K(proj (%:"R") eqs n $ 
   604                                       bnd_arg n 0 $ bnd_arg n 1)) 0 dnames,
   605                     foldr1 mk_conj (mapn (fn n => fn dn => 
   606                                 (dc_take dn $ %:"n" `bnd_arg n 0 === 
   607                                 (dc_take dn $ %:"n" `bnd_arg n 1)))0 dnames))))))
   608                              ([ rtac impI 1,
   609                                 induct_tac "n" 1,
   610                                 simp_tac take_ss 1,
   611                                 safe_tac HOL_cs] @
   612                                 List.concat(mapn (fn n => fn x => [
   613                                   rotate_tac (n+1) 1,
   614                                   etac all2E 1,
   615                                   eres_inst_tac [("P1", sproj "R" eqs n^
   616                                         " "^x^" "^x^"'")](mp RS disjE) 1,
   617                                   TRY(safe_tac HOL_cs),
   618                                   REPEAT(CHANGED(asm_simp_tac take_ss 1))]) 
   619                                 0 xs));
   620 in
   621 val coind = pg [] (mk_trp(%%:(comp_dname^"_bisim") $ %:"R") ===>
   622                 Library.foldr (op ===>) (mapn (fn n => fn x => 
   623                   mk_trp(proj (%:"R") eqs n $ %:x $ %:(x^"'"))) 0 xs,
   624                   mk_trp(foldr1 mk_conj (map (fn x => %:x === %:(x^"'")) xs)))) ([
   625                                 TRY(safe_tac HOL_cs)] @
   626                                 List.concat(map (fn take_lemma => [
   627                                   rtac take_lemma 1,
   628                                   cut_facts_tac [coind_lemma] 1,
   629                                   fast_tac HOL_cs 1])
   630                                 take_lemmas));
   631 end; (* local *)
   632 
   633 in thy |> Theory.add_path comp_dnam
   634        |> (snd o (PureThy.add_thmss (map Thm.no_attributes [
   635 		("take_rews"  , take_rews  ),
   636 		("take_lemmas", take_lemmas),
   637 		("finites"    , finites    ),
   638 		("finite_ind", [finite_ind]),
   639 		("ind"       , [ind       ]),
   640 		("coind"     , [coind     ])])))
   641        |> Theory.parent_path |> rpair take_rews
   642 end; (* let *)
   643 end; (* local *)
   644 end; (* struct *)