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