src/HOLCF/domain/theorems.ML
changeset 1274 ea0668a1c0ba
child 1461 6bcb44e4d6e5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOLCF/domain/theorems.ML	Fri Oct 06 17:25:24 1995 +0100
     1.3 @@ -0,0 +1,596 @@
     1.4 +(* theorems.ML
     1.5 +   ID:         $Id$
     1.6 +   Author : David von Oheimb
     1.7 +   Created: 06-Jun-95
     1.8 +   Updated: 08-Jun-95 first proof from cterms
     1.9 +   Updated: 26-Jun-95 proofs for exhaustion thms
    1.10 +   Updated: 27-Jun-95 proofs for discriminators, constructors and selectors
    1.11 +   Updated: 06-Jul-95 proofs for distinctness, invertibility and injectivity
    1.12 +   Updated: 17-Jul-95 proofs for induction rules
    1.13 +   Updated: 19-Jul-95 proof for co-induction rule
    1.14 +   Updated: 28-Aug-95 definedness theorems for selectors (completion)
    1.15 +   Updated: 05-Sep-95 simultaneous domain equations (main part)
    1.16 +   Updated: 11-Sep-95 simultaneous domain equations (coding finished)
    1.17 +   Updated: 13-Sep-95 simultaneous domain equations (debugging)
    1.18 +   Copyright 1995 TU Muenchen
    1.19 +*)
    1.20 +
    1.21 +
    1.22 +structure Domain_Theorems = struct
    1.23 +
    1.24 +local
    1.25 +
    1.26 +open Domain_Library;
    1.27 +infixr 0 ===>;infixr 0 ==>;infix 0 == ; 
    1.28 +infix 1 ===; infix 1 ~= ; infix 1 <<; infix 1 ~<<;
    1.29 +infix 9 `   ; infix 9 `% ; infix 9 `%%; infixr 9 oo;
    1.30 +
    1.31 +(* ----- general proof facilities ------------------------------------------------- *)
    1.32 +
    1.33 +fun inferT sg pre_tm = #2(Sign.infer_types sg (K None)(K None)[]true([pre_tm],propT));
    1.34 +
    1.35 +(*
    1.36 +infix 0 y;
    1.37 +val b=0;
    1.38 +fun _ y t = by t;
    1.39 +fun  g  defs t = let val sg = sign_of thy;
    1.40 +		     val ct = Thm.cterm_of sg (inferT sg t);
    1.41 +		 in goalw_cterm defs ct end;
    1.42 +*)
    1.43 +
    1.44 +fun pg'' thy defs t = let val sg = sign_of thy;
    1.45 +		          val ct = Thm.cterm_of sg (inferT sg t);
    1.46 +		      in prove_goalw_cterm defs ct end;
    1.47 +fun pg'  thy defs t tacsf=pg'' thy defs t (fn []   => tacsf 
    1.48 +					    | prems=> (cut_facts_tac prems 1)::tacsf);
    1.49 +
    1.50 +fun REPEAT_DETERM_UNTIL p tac = 
    1.51 +let fun drep st = if p st then Sequence.single st
    1.52 +			  else (case Sequence.pull(tapply(tac,st)) of
    1.53 +		                  None        => Sequence.null
    1.54 +				| Some(st',_) => drep st')
    1.55 +in Tactic drep end;
    1.56 +val UNTIL_SOLVED = REPEAT_DETERM_UNTIL (has_fewer_prems 1);
    1.57 +
    1.58 +local val trueI2 = prove_goal HOL.thy "f~=x ==> True" (fn prems => [rtac TrueI 1]) in
    1.59 +val kill_neq_tac = dtac trueI2 end;
    1.60 +fun case_UU_tac rews i v =	res_inst_tac [("Q",v^"=UU")] classical2 i THEN
    1.61 +				asm_simp_tac (HOLCF_ss addsimps rews) i;
    1.62 +
    1.63 +val chain_tac = REPEAT_DETERM o resolve_tac 
    1.64 +		[is_chain_iterate, ch2ch_fappR, ch2ch_fappL];
    1.65 +
    1.66 +(* ----- general proofs ----------------------------------------------------------- *)
    1.67 +
    1.68 +val swap3 = prove_goal HOL.thy "[| Q ==> P; ~P |] ==> ~Q" (fn prems => [
    1.69 +                                cut_facts_tac prems 1,
    1.70 +                                etac swap 1,
    1.71 +                                dtac notnotD 1,
    1.72 +				etac (hd prems) 1]);
    1.73 +
    1.74 +val dist_eqI = prove_goal Porder0.thy "~ x << y ==> x ~= y" (fn prems => [
    1.75 +				cut_facts_tac prems 1,
    1.76 +				etac swap 1,
    1.77 +				dtac notnotD 1,
    1.78 +				asm_simp_tac HOLCF_ss 1]);
    1.79 +val cfst_strict  = prove_goal Cprod3.thy "cfst`UU = UU" (fn _ => [
    1.80 +				(simp_tac (HOLCF_ss addsimps [inst_cprod_pcpo2]) 1)]);
    1.81 +val csnd_strict  = prove_goal Cprod3.thy "csnd`UU = UU" (fn _ => [
    1.82 +			(simp_tac (HOLCF_ss addsimps [inst_cprod_pcpo2]) 1)]);
    1.83 +
    1.84 +in
    1.85 +
    1.86 +
    1.87 +fun theorems thy (((dname,_),cons) : eq, eqs :eq list) =
    1.88 +let
    1.89 +
    1.90 +val dummy = writeln ("Proving isomorphism properties of domain "^dname^"...");
    1.91 +val pg = pg' thy;
    1.92 +
    1.93 +(* ----- getting the axioms and definitions --------------------------------------- *)
    1.94 +
    1.95 +local val ga = get_axiom thy in
    1.96 +val ax_abs_iso    = ga (dname^"_abs_iso"   );
    1.97 +val ax_rep_iso    = ga (dname^"_rep_iso"   );
    1.98 +val ax_when_def   = ga (dname^"_when_def"  );
    1.99 +val axs_con_def   = map (fn (con,_) => ga (extern_name con ^"_def")) cons;
   1.100 +val axs_dis_def   = map (fn (con,_) => ga (   dis_name con ^"_def")) cons;
   1.101 +val axs_sel_def   = flat(map (fn (_,args) => 
   1.102 +		    map (fn     arg => ga (sel_of arg      ^"_def")) args) cons);
   1.103 +val ax_copy_def   = ga (dname^"_copy_def"  );
   1.104 +end; (* local *)
   1.105 +
   1.106 +(* ----- theorems concerning the isomorphism -------------------------------------- *)
   1.107 +
   1.108 +val dc_abs  = %%(dname^"_abs");
   1.109 +val dc_rep  = %%(dname^"_rep");
   1.110 +val dc_copy = %%(dname^"_copy");
   1.111 +val x_name = "x";
   1.112 +
   1.113 +val (rep_strict, abs_strict) = let 
   1.114 +	       val r = ax_rep_iso RS (ax_abs_iso RS (allI  RSN(2,allI RS iso_strict)))
   1.115 +	       in (r RS conjunct1, r RS conjunct2) end;
   1.116 +val abs_defin' = pg [] ((dc_abs`%x_name === UU) ==> (%x_name === UU)) [
   1.117 +				res_inst_tac [("t",x_name)] (ax_abs_iso RS subst) 1,
   1.118 +				etac ssubst 1,
   1.119 +				rtac rep_strict 1];
   1.120 +val rep_defin' = pg [] ((dc_rep`%x_name === UU) ==> (%x_name === UU)) [
   1.121 +				res_inst_tac [("t",x_name)] (ax_rep_iso RS subst) 1,
   1.122 +				etac ssubst 1,
   1.123 +				rtac abs_strict 1];
   1.124 +val iso_rews = [ax_abs_iso,ax_rep_iso,abs_strict,rep_strict];
   1.125 +
   1.126 +local 
   1.127 +val iso_swap = pg [] (dc_rep`%"x" === %"y" ==> %"x" === dc_abs`%"y") [
   1.128 +				dres_inst_tac [("f",dname^"_abs")] cfun_arg_cong 1,
   1.129 +				etac (ax_rep_iso RS subst) 1];
   1.130 +fun exh foldr1 cn quant foldr2 var = let
   1.131 +  fun one_con (con,args) = let val vns = map vname args in
   1.132 +    foldr quant (vns, foldr2 ((%x_name === con_app2 con (var vns) vns)::
   1.133 +			      map (defined o (var vns)) (nonlazy args))) end
   1.134 +  in foldr1 ((cn(%x_name===UU))::map one_con cons) end;
   1.135 +in
   1.136 +val cases = let 
   1.137 +	    fun common_tac thm = rtac thm 1 THEN contr_tac 1;
   1.138 +	    fun unit_tac true = common_tac liftE1
   1.139 +	    |   unit_tac _    = all_tac;
   1.140 +	    fun prod_tac []          = common_tac oneE
   1.141 +	    |   prod_tac [arg]       = unit_tac (is_lazy arg)
   1.142 +	    |   prod_tac (arg::args) = 
   1.143 +				common_tac sprodE THEN
   1.144 +				kill_neq_tac 1 THEN
   1.145 +				unit_tac (is_lazy arg) THEN
   1.146 +				prod_tac args;
   1.147 +	    fun sum_one_tac p = SELECT_GOAL(EVERY[
   1.148 +				rtac p 1,
   1.149 +				rewrite_goals_tac axs_con_def,
   1.150 +				dtac iso_swap 1,
   1.151 +				simp_tac HOLCF_ss 1,
   1.152 +				UNTIL_SOLVED(fast_tac HOL_cs 1)]) 1;
   1.153 +	    fun sum_tac [(_,args)]       [p]        = 
   1.154 +				prod_tac args THEN sum_one_tac p
   1.155 +	    |   sum_tac ((_,args)::cons') (p::prems) = DETERM(
   1.156 +				common_tac ssumE THEN
   1.157 +				kill_neq_tac 1 THEN kill_neq_tac 2 THEN
   1.158 +				prod_tac args THEN sum_one_tac p) THEN
   1.159 +				sum_tac cons' prems
   1.160 +	    |   sum_tac _ _ = Imposs "theorems:sum_tac";
   1.161 +	  in pg'' thy [] (exh (fn l => foldr (op ===>) (l,mk_trp(%"P")))
   1.162 +			      (fn T => T ==> %"P") mk_All
   1.163 +			      (fn l => foldr (op ===>) (map mk_trp l,mk_trp(%"P")))
   1.164 +			      bound_arg)
   1.165 +			     (fn prems => [
   1.166 +				cut_facts_tac [excluded_middle] 1,
   1.167 +				etac disjE 1,
   1.168 +				rtac (hd prems) 2,
   1.169 +				etac rep_defin' 2,
   1.170 +				if is_one_con_one_arg (not o is_lazy) cons
   1.171 +				then rtac (hd (tl prems)) 1 THEN atac 2 THEN
   1.172 +				     rewrite_goals_tac axs_con_def THEN
   1.173 +				     simp_tac (HOLCF_ss addsimps [ax_rep_iso]) 1
   1.174 +				else sum_tac cons (tl prems)])end;
   1.175 +val exhaust = pg [] (mk_trp(exh (foldr' mk_disj) Id mk_ex (foldr' mk_conj) (K %))) [
   1.176 +				rtac cases 1,
   1.177 +				UNTIL_SOLVED(fast_tac HOL_cs 1)];
   1.178 +end;
   1.179 +
   1.180 +local 
   1.181 +val when_app = foldl (op `) (%%(dname^"_when"), map % (when_funs cons));
   1.182 +val when_appl = pg [ax_when_def] (mk_trp(when_app`%x_name===when_body cons 
   1.183 +		(fn (_,n) => %(nth_elem(n-1,when_funs cons)))`(dc_rep`%x_name))) [
   1.184 +				simp_tac HOLCF_ss 1];
   1.185 +in
   1.186 +val when_strict = pg [] ((if is_one_con_one_arg (K true) cons 
   1.187 +	then fn t => mk_trp(strict(%"f")) ===> t else Id)(mk_trp(strict when_app))) [
   1.188 +				simp_tac(HOLCF_ss addsimps [when_appl,rep_strict]) 1];
   1.189 +val when_apps = let fun one_when n (con,args) = pg axs_con_def
   1.190 +		(lift_defined % (nonlazy args, mk_trp(when_app`(con_app con args) ===
   1.191 +		 mk_cfapp(%(nth_elem(n,when_funs cons)),map %# args))))[
   1.192 +			asm_simp_tac (HOLCF_ss addsimps [when_appl,ax_abs_iso]) 1];
   1.193 +		in mapn one_when 0 cons end;
   1.194 +end;
   1.195 +val when_rews = when_strict::when_apps;
   1.196 +
   1.197 +(* ----- theorems concerning the constructors, discriminators and selectors ------- *)
   1.198 +
   1.199 +val dis_stricts = map (fn (con,_) => pg axs_dis_def (mk_trp(
   1.200 +			(if is_one_con_one_arg (K true) cons then mk_not else Id)
   1.201 +		         (strict(%%(dis_name con))))) [
   1.202 +		simp_tac (HOLCF_ss addsimps (if is_one_con_one_arg (K true) cons 
   1.203 +					then [ax_when_def] else when_rews)) 1]) cons;
   1.204 +val dis_apps = let fun one_dis c (con,args)= pg (axs_dis_def)
   1.205 +		   (lift_defined % (nonlazy args, (*(if is_one_con_one_arg is_lazy cons
   1.206 +			then curry (lift_defined %#) args else Id)
   1.207 +#################*)
   1.208 +			(mk_trp((%%(dis_name c))`(con_app con args) ===
   1.209 +			      %%(if con=c then "TT" else "FF"))))) [
   1.210 +				asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
   1.211 +	in flat(map (fn (c,_) => map (one_dis c) cons) cons) end;
   1.212 +val dis_defins = map (fn (con,args) => pg [] (defined(%x_name)==> 
   1.213 +		      defined(%%(dis_name con)`%x_name)) [
   1.214 +				rtac cases 1,
   1.215 +				contr_tac 1,
   1.216 +				UNTIL_SOLVED (CHANGED(asm_simp_tac 
   1.217 +				              (HOLCF_ss addsimps dis_apps) 1))]) cons;
   1.218 +val dis_rews = dis_stricts @ dis_defins @ dis_apps;
   1.219 +
   1.220 +val con_stricts = flat(map (fn (con,args) => map (fn vn =>
   1.221 +			pg (axs_con_def) 
   1.222 +			   (mk_trp(con_app2 con (fn arg => if vname arg = vn 
   1.223 +					then UU else %# arg) args === UU))[
   1.224 +				asm_simp_tac (HOLCF_ss addsimps [abs_strict]) 1]
   1.225 +			) (nonlazy args)) cons);
   1.226 +val con_defins = map (fn (con,args) => pg []
   1.227 +			(lift_defined % (nonlazy args,
   1.228 +				mk_trp(defined(con_app con args)))) ([
   1.229 +				rtac swap3 1] @ (if is_one_con_one_arg (K true) cons 
   1.230 +				then [
   1.231 +				  if is_lazy (hd args) then rtac defined_up 2
   1.232 +						       else atac 2,
   1.233 +				  rtac abs_defin' 1,	
   1.234 +				  asm_full_simp_tac (HOLCF_ss addsimps axs_con_def) 1]
   1.235 +				else [
   1.236 +				  eres_inst_tac [("f",dis_name con)] cfun_arg_cong 1,
   1.237 +				  asm_simp_tac (HOLCF_ss addsimps dis_rews) 1])))cons;
   1.238 +val con_rews = con_stricts @ con_defins;
   1.239 +
   1.240 +val sel_stricts = let fun one_sel sel = pg axs_sel_def (mk_trp(strict(%%sel))) [
   1.241 +				simp_tac (HOLCF_ss addsimps when_rews) 1];
   1.242 +in flat(map (fn (_,args) => map (fn arg => one_sel (sel_of arg)) args) cons) end;
   1.243 +val sel_apps = let fun one_sel c n sel = map (fn (con,args) => 
   1.244 +		let val nlas = nonlazy args;
   1.245 +		    val vns  = map vname args;
   1.246 +		in pg axs_sel_def (lift_defined %
   1.247 +		   (filter (fn v => con=c andalso (v<>nth_elem(n,vns))) nlas,
   1.248 +   mk_trp((%%sel)`(con_app con args) === (if con=c then %(nth_elem(n,vns)) else UU))))
   1.249 +			    ( (if con=c then [] 
   1.250 +			       else map(case_UU_tac(when_rews@con_stricts)1) nlas)
   1.251 +			     @(if con=c andalso ((nth_elem(n,vns)) mem nlas)
   1.252 +					 then[case_UU_tac (when_rews @ con_stricts) 1 
   1.253 +							  (nth_elem(n,vns))] else [])
   1.254 +			     @ [asm_simp_tac(HOLCF_ss addsimps when_rews)1])end) cons;
   1.255 +in flat(map  (fn (c,args) => 
   1.256 +	flat(mapn (fn n => fn arg => one_sel c n (sel_of arg)) 0 args)) cons) end;
   1.257 +val sel_defins = if length cons = 1 then map (fn arg => pg [] (defined(%x_name) ==> 
   1.258 +			defined(%%(sel_of arg)`%x_name)) [
   1.259 +				rtac cases 1,
   1.260 +				contr_tac 1,
   1.261 +				UNTIL_SOLVED (CHANGED(asm_simp_tac 
   1.262 +				              (HOLCF_ss addsimps sel_apps) 1))]) 
   1.263 +		 (filter_out is_lazy (snd(hd cons))) else [];
   1.264 +val sel_rews = sel_stricts @ sel_defins @ sel_apps;
   1.265 +
   1.266 +val distincts_le = let
   1.267 +    fun dist (con1, args1) (con2, args2) = pg []
   1.268 +	      (lift_defined % ((nonlazy args1),
   1.269 +			     (mk_trp (con_app con1 args1 ~<< con_app con2 args2))))([
   1.270 +			rtac swap3 1,
   1.271 +			eres_inst_tac [("fo5",dis_name con1)] monofun_cfun_arg 1]
   1.272 +		      @ map (case_UU_tac (con_stricts @ dis_rews) 1) (nonlazy args2)
   1.273 +		      @[asm_simp_tac (HOLCF_ss addsimps dis_rews) 1]);
   1.274 +    fun distinct (con1,args1) (con2,args2) =
   1.275 +	let val arg1 = (con1, args1);
   1.276 +	    val arg2 = (con2, (map (fn (arg,vn) => upd_vname (K vn) arg)
   1.277 +			      (args2~~variantlist(map vname args2,map vname args1))));
   1.278 +	in [dist arg1 arg2, dist arg2 arg1] end;
   1.279 +    fun distincts []      = []
   1.280 +    |   distincts (c::cs) = (map (distinct c) cs) :: distincts cs;
   1.281 +in distincts cons end;
   1.282 +val dists_le = flat (flat distincts_le);
   1.283 +val dists_eq = let
   1.284 +    fun distinct (_,args1) ((_,args2),leqs) = let
   1.285 +	val (le1,le2) = (hd leqs, hd(tl leqs));
   1.286 +	val (eq1,eq2) = (le1 RS dist_eqI, le2 RS dist_eqI) in
   1.287 +	if nonlazy args1 = [] then [eq1, eq1 RS not_sym] else
   1.288 +	if nonlazy args2 = [] then [eq2, eq2 RS not_sym] else
   1.289 +					[eq1, eq2] end;
   1.290 +    fun distincts []      = []
   1.291 +    |   distincts ((c,leqs)::cs) = flat(map (distinct c) ((map fst cs)~~leqs)) @
   1.292 +				   distincts cs;
   1.293 +    in distincts (cons~~distincts_le) end;
   1.294 +
   1.295 +local 
   1.296 +  fun pgterm rel con args = let
   1.297 +		fun append s = upd_vname(fn v => v^s);
   1.298 +		val (largs,rargs) = (args, map (append "'") args);
   1.299 +		in pg [] (mk_trp (rel(con_app con largs,con_app con rargs)) ===>
   1.300 +		      lift_defined % ((nonlazy largs),lift_defined % ((nonlazy rargs),
   1.301 +			    mk_trp (foldr' mk_conj 
   1.302 +				(map rel (map %# largs ~~ map %# rargs)))))) end;
   1.303 +  val cons' = filter (fn (_,args) => args<>[]) cons;
   1.304 +in
   1.305 +val inverts = map (fn (con,args) => 
   1.306 +		pgterm (op <<) con args (flat(map (fn arg => [
   1.307 +				TRY(rtac conjI 1),
   1.308 +				dres_inst_tac [("fo5",sel_of arg)] monofun_cfun_arg 1,
   1.309 +				asm_full_simp_tac (HOLCF_ss addsimps sel_apps) 1]
   1.310 +			     			      ) args))) cons';
   1.311 +val injects = map (fn ((con,args),inv_thm) => 
   1.312 +			   pgterm (op ===) con args [
   1.313 +				etac (antisym_less_inverse RS conjE) 1,
   1.314 +				dtac inv_thm 1, REPEAT(atac 1),
   1.315 +				dtac inv_thm 1, REPEAT(atac 1),
   1.316 +				TRY(safe_tac HOL_cs),
   1.317 +				REPEAT(rtac antisym_less 1 ORELSE atac 1)] )
   1.318 +		  (cons'~~inverts);
   1.319 +end;
   1.320 +
   1.321 +(* ----- theorems concerning one induction step ----------------------------------- *)
   1.322 +
   1.323 +val copy_strict = pg [ax_copy_def] ((if is_one_con_one_arg (K true) cons then fn t =>
   1.324 +	 mk_trp(strict(cproj (%"f") eqs (rec_of (hd(snd(hd cons)))))) ===> t
   1.325 +	else Id) (mk_trp(strict(dc_copy`%"f")))) [
   1.326 +				asm_simp_tac(HOLCF_ss addsimps [abs_strict,rep_strict,
   1.327 +							cfst_strict,csnd_strict]) 1];
   1.328 +val copy_apps = map (fn (con,args) => pg (ax_copy_def::axs_con_def)
   1.329 +		    (lift_defined %# (filter is_nonlazy_rec args,
   1.330 +			mk_trp(dc_copy`%"f"`(con_app con args) ===
   1.331 +			   (con_app2 con (app_rec_arg (cproj (%"f") eqs)) args))))
   1.332 +				 (map (case_UU_tac [ax_abs_iso] 1 o vname)
   1.333 +				   (filter(fn a=>not(is_rec a orelse is_lazy a))args)@
   1.334 +				 [asm_simp_tac (HOLCF_ss addsimps [ax_abs_iso]) 1])
   1.335 +		)cons;
   1.336 +val copy_stricts = map(fn(con,args)=>pg[](mk_trp(dc_copy`UU`(con_app con args) ===UU))
   1.337 +	     (let val rews = cfst_strict::csnd_strict::copy_strict::copy_apps@con_rews
   1.338 +			 in map (case_UU_tac rews 1) (nonlazy args) @ [
   1.339 +			     asm_simp_tac (HOLCF_ss addsimps rews) 1] end))
   1.340 +		   (filter (fn (_,args)=>exists is_nonlazy_rec args) cons);
   1.341 +val copy_rews = copy_strict::copy_apps @ copy_stricts;
   1.342 +
   1.343 +in     (iso_rews, exhaust, cases, when_rews,
   1.344 +	con_rews, sel_rews, dis_rews, dists_eq, dists_le, inverts, injects,
   1.345 +	copy_rews)
   1.346 +end; (* let *)
   1.347 +
   1.348 +
   1.349 +fun comp_theorems thy (comp_dname, eqs: eq list, casess, con_rews, copy_rews) =
   1.350 +let
   1.351 +
   1.352 +val dummy = writeln ("Proving induction properties of domain "^comp_dname^"...");
   1.353 +val pg = pg' thy;
   1.354 +
   1.355 +val dnames = map (fst o fst) eqs;
   1.356 +val conss  = map  snd        eqs;
   1.357 +
   1.358 +(* ----- getting the composite axiom and definitions ------------------------------ *)
   1.359 +
   1.360 +local val ga = get_axiom thy in
   1.361 +val axs_reach      = map (fn dn => ga (dn ^  "_reach"   )) dnames;
   1.362 +val axs_take_def   = map (fn dn => ga (dn ^  "_take_def")) dnames;
   1.363 +val axs_finite_def = map (fn dn => ga (dn ^"_finite_def")) dnames;
   1.364 +val ax_copy2_def   = ga (comp_dname^ "_copy_def");
   1.365 +val ax_bisim_def   = ga (comp_dname^"_bisim_def");
   1.366 +end; (* local *)
   1.367 +
   1.368 +(* ----- theorems concerning finiteness and induction ----------------------------- *)
   1.369 +
   1.370 +fun dc_take dn = %%(dn^"_take");
   1.371 +val x_name = idx_name dnames "x"; 
   1.372 +val P_name = idx_name dnames "P";
   1.373 +
   1.374 +local
   1.375 +  val iterate_ss = simpset_of "Fix";	
   1.376 +  val iterate_Cprod_strict_ss = iterate_ss addsimps [cfst_strict, csnd_strict];
   1.377 +  val iterate_Cprod_ss = iterate_ss addsimps [cfst2,csnd2,csplit2];
   1.378 +  val copy_con_rews  = copy_rews @ con_rews;
   1.379 +  val copy_take_defs = (if length dnames=1 then [] else [ax_copy2_def]) @axs_take_def;
   1.380 +  val take_stricts = pg copy_take_defs (mk_trp(foldr' mk_conj (map (fn ((dn,args),_)=>
   1.381 +		  (dc_take dn $ %"n")`UU === mk_constrain(Type(dn,args),UU)) eqs)))([
   1.382 +				nat_ind_tac "n" 1,
   1.383 +				simp_tac iterate_ss 1,
   1.384 +				simp_tac iterate_Cprod_strict_ss 1,
   1.385 +				asm_simp_tac iterate_Cprod_ss 1,
   1.386 +				TRY(safe_tac HOL_cs)] @
   1.387 +			map(K(asm_simp_tac (HOL_ss addsimps copy_rews)1))dnames);
   1.388 +  val take_stricts' = rewrite_rule copy_take_defs take_stricts;
   1.389 +  val take_0s = mapn (fn n => fn dn => pg axs_take_def(mk_trp((dc_take dn $ %%"0")
   1.390 +								`%x_name n === UU))[
   1.391 +				simp_tac iterate_Cprod_strict_ss 1]) 1 dnames;
   1.392 +  val take_apps = pg copy_take_defs (mk_trp(foldr' mk_conj 
   1.393 +	    (flat(map (fn ((dn,_),cons) => map (fn (con,args) => foldr mk_all 
   1.394 +		(map vname args,(dc_take dn $ (%%"Suc" $ %"n"))`(con_app con args) ===
   1.395 +  		 con_app2 con (app_rec_arg (fn n=>dc_take (nth_elem(n,dnames))$ %"n"))
   1.396 +			      args)) cons) eqs)))) ([
   1.397 +				nat_ind_tac "n" 1,
   1.398 +				simp_tac iterate_Cprod_strict_ss 1,
   1.399 +				simp_tac (HOLCF_ss addsimps copy_con_rews) 1,
   1.400 +				TRY(safe_tac HOL_cs)] @
   1.401 +			(flat(map (fn ((dn,_),cons) => map (fn (con,args) => EVERY (
   1.402 +				asm_full_simp_tac iterate_Cprod_ss 1::
   1.403 +				map (case_UU_tac (take_stricts'::copy_con_rews) 1)
   1.404 +				    (nonlazy args) @[
   1.405 +				asm_full_simp_tac (HOLCF_ss addsimps copy_rews) 1])
   1.406 +		 	) cons) eqs)));
   1.407 +in
   1.408 +val take_rews = atomize take_stricts @ take_0s @ atomize take_apps;
   1.409 +end; (* local *)
   1.410 +
   1.411 +val take_lemmas = mapn (fn n => fn(dn,ax_reach) => pg'' thy axs_take_def (mk_All("n",
   1.412 +		mk_trp(dc_take dn $ Bound 0 `%(x_name n) === 
   1.413 +		       dc_take dn $ Bound 0 `%(x_name n^"'")))
   1.414 +	   ===> mk_trp(%(x_name n) === %(x_name n^"'"))) (fn prems => [
   1.415 +				res_inst_tac[("t",x_name n    )](ax_reach RS subst) 1,
   1.416 +				res_inst_tac[("t",x_name n^"'")](ax_reach RS subst) 1,
   1.417 +				rtac (fix_def2 RS ssubst) 1,
   1.418 +				REPEAT(CHANGED(rtac (contlub_cfun_arg RS ssubst) 1
   1.419 +					       THEN chain_tac 1)),
   1.420 +				rtac (contlub_cfun_fun RS ssubst) 1,
   1.421 +				rtac (contlub_cfun_fun RS ssubst) 2,
   1.422 +				rtac lub_equal 3,
   1.423 +				chain_tac 1,
   1.424 +				rtac allI 1,
   1.425 +				resolve_tac prems 1])) 1 (dnames~~axs_reach);
   1.426 +
   1.427 +local
   1.428 +  fun one_con p (con,args) = foldr mk_All (map vname args,
   1.429 +	lift_defined (bound_arg (map vname args)) (nonlazy args,
   1.430 +	lift (fn arg => %(P_name (1+rec_of arg)) $ bound_arg args arg)
   1.431 +	     (filter is_rec args,mk_trp(%p $ con_app2 con (bound_arg args) args))));
   1.432 +  fun one_eq ((p,cons),concl) = (mk_trp(%p $ UU) ===> 
   1.433 +			   foldr (op ===>) (map (one_con p) cons,concl));
   1.434 +  fun ind_term concf = foldr one_eq (mapn (fn n => fn x => (P_name n, x)) 1 conss,
   1.435 +	mk_trp(foldr' mk_conj (mapn (fn n => concf (P_name n,x_name n)) 1 dnames)));
   1.436 +  val take_ss = HOL_ss addsimps take_rews;
   1.437 +  fun ind_tacs tacsf thms1 thms2 prems = TRY(safe_tac HOL_cs)::
   1.438 +				flat (mapn (fn n => fn (thm1,thm2) => 
   1.439 +				  tacsf (n,prems) (thm1,thm2) @ 
   1.440 +				  flat (map (fn cons =>
   1.441 +				    (resolve_tac prems 1 ::
   1.442 +				     flat (map (fn (_,args) => 
   1.443 +				       resolve_tac prems 1::
   1.444 +				       map (K(atac 1)) (nonlazy args) @
   1.445 +				       map (K(atac 1)) (filter is_rec args))
   1.446 +				     cons)))
   1.447 +				   conss))
   1.448 +				0 (thms1~~thms2));
   1.449 +  local 
   1.450 +    fun all_rec_to ns lazy_rec (n,cons) = forall (exists (fn arg => 
   1.451 +		  is_rec arg andalso not(rec_of arg mem ns) andalso
   1.452 +		  ((rec_of arg =  n andalso not(lazy_rec orelse is_lazy arg)) orelse 
   1.453 +		    rec_of arg <> n andalso all_rec_to (rec_of arg::ns) 
   1.454 +		      (lazy_rec orelse is_lazy arg) (n, (nth_elem(rec_of arg,conss))))
   1.455 +		  ) o snd) cons;
   1.456 +    fun warn (n,cons) = if all_rec_to [] false (n,cons) then (writeln 
   1.457 +			   ("WARNING: domain "^nth_elem(n,dnames)^" is empty!"); true)
   1.458 +			else false;
   1.459 +    fun lazy_rec_to ns lazy_rec (n,cons) = exists (exists (fn arg => 
   1.460 +		  is_rec arg andalso not(rec_of arg mem ns) andalso
   1.461 +		  ((rec_of arg =  n andalso (lazy_rec orelse is_lazy arg)) orelse 
   1.462 +		    rec_of arg <> n andalso lazy_rec_to (rec_of arg::ns)
   1.463 +		     (lazy_rec orelse is_lazy arg) (n, (nth_elem(rec_of arg,conss))))
   1.464 +		 ) o snd) cons;
   1.465 +  in val is_emptys = map warn (mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs);
   1.466 +     val is_finite = forall (not o lazy_rec_to [] false) 
   1.467 +			    (mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs)
   1.468 +  end;
   1.469 +in
   1.470 +val finite_ind = pg'' thy [] (ind_term (fn (P,x) => fn dn => 
   1.471 +			  mk_all(x,%P $ (dc_take dn $ %"n" `Bound 0)))) (fn prems=> [
   1.472 +				nat_ind_tac "n" 1,
   1.473 +				simp_tac (take_ss addsimps prems) 1,
   1.474 +				TRY(safe_tac HOL_cs)]
   1.475 +				@ flat(mapn (fn n => fn (cons,cases) => [
   1.476 +				 res_inst_tac [("x",x_name n)] cases 1,
   1.477 +				 asm_simp_tac (take_ss addsimps prems) 1]
   1.478 +				 @ flat(map (fn (con,args) => 
   1.479 +				  asm_simp_tac take_ss 1 ::
   1.480 +				  map (fn arg =>
   1.481 +				   case_UU_tac (prems@con_rews) 1 (
   1.482 +				   nth_elem(rec_of arg,dnames)^"_take n1`"^vname arg))
   1.483 +				  (filter is_nonlazy_rec args) @ [
   1.484 +				  resolve_tac prems 1] @
   1.485 +				  map (K (atac 1))      (nonlazy args) @
   1.486 +				  map (K (etac spec 1)) (filter is_rec args)) 
   1.487 +				 cons))
   1.488 +				1 (conss~~casess)));
   1.489 +
   1.490 +val (finites,ind) = if is_finite then
   1.491 +let 
   1.492 +  fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %"x" === %"x");
   1.493 +  val finite_lemmas1a = map (fn dn => pg [] (mk_trp(defined (%"x")) ===> 
   1.494 +	mk_trp(mk_disj(mk_all("n",dc_take dn $ Bound 0 ` %"x" === UU),
   1.495 +	take_enough dn)) ===> mk_trp(take_enough dn)) [
   1.496 +				etac disjE 1,
   1.497 +				etac notE 1,
   1.498 +				resolve_tac take_lemmas 1,
   1.499 +				asm_simp_tac take_ss 1,
   1.500 +				atac 1]) dnames;
   1.501 +  val finite_lemma1b = pg [] (mk_trp (mk_all("n",foldr' mk_conj (mapn 
   1.502 +	(fn n => fn ((dn,args),_) => mk_constrainall(x_name n,Type(dn,args),
   1.503 +	 mk_disj(dc_take dn $ Bound 1 ` Bound 0 === UU,
   1.504 +		 dc_take dn $ Bound 1 ` Bound 0 === Bound 0))) 1 eqs)))) ([
   1.505 +				rtac allI 1,
   1.506 +				nat_ind_tac "n" 1,
   1.507 +				simp_tac take_ss 1,
   1.508 +				TRY(safe_tac(empty_cs addSEs[conjE] addSIs[conjI]))] @
   1.509 +				flat(mapn (fn n => fn (cons,cases) => [
   1.510 +				  simp_tac take_ss 1,
   1.511 +				  rtac allI 1,
   1.512 +				  res_inst_tac [("x",x_name n)] cases 1,
   1.513 +				  asm_simp_tac take_ss 1] @ 
   1.514 +				  flat(map (fn (con,args) => 
   1.515 +				    asm_simp_tac take_ss 1 ::
   1.516 +				    flat(map (fn arg => [
   1.517 +				      eres_inst_tac [("x",vname arg)] all_dupE 1,
   1.518 +				      etac disjE 1,
   1.519 +				      asm_simp_tac (HOL_ss addsimps con_rews) 1,
   1.520 +				      asm_simp_tac take_ss 1])
   1.521 +				    (filter is_nonlazy_rec args)))
   1.522 +				  cons))
   1.523 +				1 (conss~~casess))) handle ERROR => raise ERROR;
   1.524 +  val all_finite=map (fn(dn,l1b)=>pg axs_finite_def (mk_trp(%%(dn^"_finite") $ %"x"))[
   1.525 +				case_UU_tac take_rews 1 "x",
   1.526 +				eresolve_tac finite_lemmas1a 1,
   1.527 +				step_tac HOL_cs 1,
   1.528 +				step_tac HOL_cs 1,
   1.529 +				cut_facts_tac [l1b] 1,
   1.530 +				fast_tac HOL_cs 1]) (dnames~~atomize finite_lemma1b);
   1.531 +in
   1.532 +(all_finite,
   1.533 + pg'' thy [] (ind_term (fn (P,x) => fn dn => %P $ %x))
   1.534 +			       (ind_tacs (fn _ => fn (all_fin,finite_ind) => [
   1.535 +				rtac (rewrite_rule axs_finite_def all_fin RS exE) 1,
   1.536 +				etac subst 1,
   1.537 +				rtac finite_ind 1]) all_finite (atomize finite_ind))
   1.538 +) end (* let *) else
   1.539 +(mapn (fn n => fn dn => read_instantiate_sg (sign_of thy) 
   1.540 +	  	    [("P",dn^"_finite "^x_name n)] excluded_middle) 1 dnames,
   1.541 + pg'' thy [] (foldr (op ===>) (mapn (fn n =>K(mk_trp(%%"adm" $ %(P_name n))))1
   1.542 +				       dnames,ind_term (fn(P,x)=>fn dn=> %P $ %x)))
   1.543 +			       (ind_tacs (fn (n,prems) => fn (ax_reach,finite_ind) =>[
   1.544 +				rtac (ax_reach RS subst) 1,
   1.545 +				res_inst_tac [("x",x_name n)] spec 1,
   1.546 +				rtac wfix_ind 1,
   1.547 +				rtac adm_impl_admw 1,
   1.548 +				resolve_tac adm_thms 1,
   1.549 +				rtac adm_subst 1,
   1.550 +				cont_tacR 1,
   1.551 +				resolve_tac prems 1,
   1.552 +				strip_tac 1,
   1.553 +			        rtac(rewrite_rule axs_take_def finite_ind) 1])
   1.554 +				 axs_reach (atomize finite_ind))
   1.555 +)
   1.556 +end; (* local *)
   1.557 +
   1.558 +local
   1.559 +  val xs = mapn (fn n => K (x_name n)) 1 dnames;
   1.560 +  fun bnd_arg n i = Bound(2*(length dnames - n)-i-1);
   1.561 +  val take_ss = HOL_ss addsimps take_rews;
   1.562 +  val sproj   = bin_branchr (fn s => "fst("^s^")") (fn s => "snd("^s^")");
   1.563 +  val coind_lemma = pg [ax_bisim_def] (mk_trp(mk_imp(%%(comp_dname^"_bisim") $ %"R",
   1.564 +		foldr (fn (x,t)=> mk_all(x,mk_all(x^"'",t))) (xs,
   1.565 +		  foldr mk_imp (mapn (fn n => K(proj (%"R") dnames n $ 
   1.566 +				      bnd_arg n 0 $ bnd_arg n 1)) 0 dnames,
   1.567 +		    foldr' mk_conj (mapn (fn n => fn dn => 
   1.568 +				(dc_take dn $ %"n" `bnd_arg n 0 === 
   1.569 +				(dc_take dn $ %"n" `bnd_arg n 1))) 0 dnames)))))) ([
   1.570 +				rtac impI 1,
   1.571 +				nat_ind_tac "n" 1,
   1.572 +				simp_tac take_ss 1,
   1.573 +				safe_tac HOL_cs] @
   1.574 +				flat(mapn (fn n => fn x => [
   1.575 +				  etac allE 1, etac allE 1, 
   1.576 +				  eres_inst_tac [("P1",sproj "R" dnames n^
   1.577 +						  " "^x^" "^x^"'")](mp RS disjE) 1,
   1.578 +				  TRY(safe_tac HOL_cs),
   1.579 +				  REPEAT(CHANGED(asm_simp_tac take_ss 1))]) 
   1.580 +				0 xs));
   1.581 +in
   1.582 +val coind = pg [] (mk_trp(%%(comp_dname^"_bisim") $ %"R") ===>
   1.583 +		foldr (op ===>) (mapn (fn n => fn x => 
   1.584 +			mk_trp(proj (%"R") dnames n $ %x $ %(x^"'"))) 0 xs,
   1.585 +			mk_trp(foldr' mk_conj (map (fn x => %x === %(x^"'")) xs)))) ([
   1.586 +				TRY(safe_tac HOL_cs)] @
   1.587 +				flat(map (fn take_lemma => [
   1.588 +				  rtac take_lemma 1,
   1.589 +				  cut_facts_tac [coind_lemma] 1,
   1.590 +				  fast_tac HOL_cs 1])
   1.591 +				take_lemmas));
   1.592 +end; (* local *)
   1.593 +
   1.594 +
   1.595 +in (take_rews, take_lemmas, finites, finite_ind, ind, coind)
   1.596 +
   1.597 +end; (* let *)
   1.598 +end; (* local *)
   1.599 +end; (* struct *)