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