src/FOLP/simp.ML
changeset 0 a5a9c433f639
child 231 cb6a24451544
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/FOLP/simp.ML	Thu Sep 16 12:20:38 1993 +0200
     1.3 @@ -0,0 +1,611 @@
     1.4 +(*  Title:      FOLP/simp
     1.5 +    ID:         $Id$
     1.6 +    Author:     Tobias Nipkow
     1.7 +    Copyright   1993  University of Cambridge
     1.8 +
     1.9 +FOLP version of...
    1.10 +
    1.11 +Generic simplifier, suitable for most logics.  (from Provers)
    1.12 +
    1.13 +This version allows instantiation of Vars in the subgoal, since the proof
    1.14 +term must change.
    1.15 +*)
    1.16 +
    1.17 +signature SIMP_DATA =
    1.18 +sig
    1.19 +  val case_splits  : (thm * string) list
    1.20 +  val dest_red     : term -> term * term * term
    1.21 +  val mk_rew_rules : thm -> thm list
    1.22 +  val norm_thms    : (thm*thm) list (* [(?x>>norm(?x), norm(?x)>>?x), ...] *)
    1.23 +  val red1         : thm        (*  ?P>>?Q  ==>  ?P  ==>  ?Q  *)
    1.24 +  val red2         : thm        (*  ?P>>?Q  ==>  ?Q  ==>  ?P  *)
    1.25 +  val refl_thms    : thm list
    1.26 +  val subst_thms   : thm list   (* [ ?a>>?b ==> ?P(?a) ==> ?P(?b), ...] *)
    1.27 +  val trans_thms   : thm list
    1.28 +end;
    1.29 +
    1.30 +
    1.31 +infix 4 addrews addcongs delrews delcongs setauto;
    1.32 +
    1.33 +signature SIMP =
    1.34 +sig
    1.35 +  type simpset
    1.36 +  val empty_ss  : simpset
    1.37 +  val addcongs  : simpset * thm list -> simpset
    1.38 +  val addrews   : simpset * thm list -> simpset
    1.39 +  val delcongs  : simpset * thm list -> simpset
    1.40 +  val delrews   : simpset * thm list -> simpset
    1.41 +  val dest_ss   : simpset -> thm list * thm list
    1.42 +  val print_ss  : simpset -> unit
    1.43 +  val setauto   : simpset * (int -> tactic) -> simpset
    1.44 +  val ASM_SIMP_CASE_TAC : simpset -> int -> tactic
    1.45 +  val ASM_SIMP_TAC      : simpset -> int -> tactic
    1.46 +  val CASE_TAC          : simpset -> int -> tactic
    1.47 +  val SIMP_CASE2_TAC    : simpset -> int -> tactic
    1.48 +  val SIMP_THM          : simpset -> thm -> thm
    1.49 +  val SIMP_TAC          : simpset -> int -> tactic
    1.50 +  val SIMP_CASE_TAC     : simpset -> int -> tactic
    1.51 +  val mk_congs          : theory -> string list -> thm list
    1.52 +  val mk_typed_congs    : theory -> (string * string) list -> thm list
    1.53 +(* temporarily disabled:
    1.54 +  val extract_free_congs        : unit -> thm list
    1.55 +*)
    1.56 +  val tracing   : bool ref
    1.57 +end;
    1.58 +
    1.59 +functor SimpFun (Simp_data: SIMP_DATA) : SIMP = 
    1.60 +struct
    1.61 +
    1.62 +local open Simp_data Logic in
    1.63 +
    1.64 +(*For taking apart reductions into left, right hand sides*)
    1.65 +val lhs_of = #2 o dest_red;
    1.66 +val rhs_of = #3 o dest_red;
    1.67 +
    1.68 +(*** Indexing and filtering of theorems ***)
    1.69 +
    1.70 +fun eq_brl ((b1,th1),(b2,th2)) = b1=b2 andalso eq_thm(th1,th2);
    1.71 +
    1.72 +(*insert a thm in a discrimination net by its lhs*)
    1.73 +fun lhs_insert_thm (th,net) =
    1.74 +    Net.insert_term((lhs_of (concl_of th), (false,th)), net, eq_brl)
    1.75 +    handle  Net.INSERT => net;
    1.76 +
    1.77 +(*match subgoal i against possible theorems in the net.
    1.78 +  Similar to match_from_nat_tac, but the net does not contain numbers;
    1.79 +  rewrite rules are not ordered.*)
    1.80 +fun net_tac net =
    1.81 +  SUBGOAL(fn (prem,i) => 
    1.82 +	  resolve_tac (Net.unify_term net (strip_assums_concl prem)) i);
    1.83 +
    1.84 +(*match subgoal i against possible theorems indexed by lhs in the net*)
    1.85 +fun lhs_net_tac net =
    1.86 +  SUBGOAL(fn (prem,i) => 
    1.87 +	  biresolve_tac (Net.unify_term net
    1.88 +		       (lhs_of (strip_assums_concl prem))) i);
    1.89 +
    1.90 +fun nth_subgoal i thm = nth_elem(i-1,prems_of thm);
    1.91 +
    1.92 +fun goal_concl i thm = strip_assums_concl(nth_subgoal i thm);
    1.93 +
    1.94 +fun lhs_of_eq i thm = lhs_of(goal_concl i thm)
    1.95 +and rhs_of_eq i thm = rhs_of(goal_concl i thm);
    1.96 +
    1.97 +fun var_lhs(thm,i) =
    1.98 +let fun var(Var _) = true
    1.99 +      | var(Abs(_,_,t)) = var t
   1.100 +      | var(f$_) = var f
   1.101 +      | var _ = false;
   1.102 +in var(lhs_of_eq i thm) end;
   1.103 +
   1.104 +fun contains_op opns =
   1.105 +    let fun contains(Const(s,_)) = s mem opns |
   1.106 +            contains(s$t) = contains s orelse contains t |
   1.107 +            contains(Abs(_,_,t)) = contains t |
   1.108 +            contains _ = false;
   1.109 +    in contains end;
   1.110 +
   1.111 +fun may_match(match_ops,i) = contains_op match_ops o lhs_of_eq i;
   1.112 +
   1.113 +val (normI_thms,normE_thms) = split_list norm_thms;
   1.114 +
   1.115 +(*Get the norm constants from norm_thms*)
   1.116 +val norms =
   1.117 +  let fun norm thm = 
   1.118 +      case lhs_of(concl_of thm) of
   1.119 +	  Const(n,_)$_ => n
   1.120 +	| _ => (prths normE_thms; error"No constant in lhs of a norm_thm")
   1.121 +  in map norm normE_thms end;
   1.122 +
   1.123 +fun lhs_is_NORM(thm,i) = case lhs_of_eq i thm of
   1.124 +	Const(s,_)$_ => s mem norms | _ => false;
   1.125 +
   1.126 +val refl_tac = resolve_tac refl_thms;
   1.127 +
   1.128 +fun find_res thms thm =
   1.129 +    let fun find [] = (prths thms; error"Check Simp_Data")
   1.130 +          | find(th::thms) = thm RS th handle _ => find thms
   1.131 +    in find thms end;
   1.132 +
   1.133 +val mk_trans = find_res trans_thms;
   1.134 +
   1.135 +fun mk_trans2 thm =
   1.136 +let fun mk[] = error"Check transitivity"
   1.137 +      | mk(t::ts) = (thm RSN (2,t))  handle _  => mk ts
   1.138 +in mk trans_thms end;
   1.139 +
   1.140 +(*Applies tactic and returns the first resulting state, FAILS if none!*)
   1.141 +fun one_result(tac,thm) = case Sequence.pull(tapply(tac,thm)) of
   1.142 +	Some(thm',_) => thm'
   1.143 +      | None => raise THM("Simplifier: could not continue", 0, [thm]);
   1.144 +
   1.145 +fun res1(thm,thms,i) = one_result(resolve_tac thms i,thm);
   1.146 +
   1.147 +
   1.148 +(**** Adding "NORM" tags ****)
   1.149 +
   1.150 +(*get name of the constant from conclusion of a congruence rule*)
   1.151 +fun cong_const cong = 
   1.152 +    case head_of (lhs_of (concl_of cong)) of
   1.153 +	Const(c,_) => c
   1.154 +      | _ => ""			(*a placeholder distinct from const names*);
   1.155 +
   1.156 +(*true if the term is an atomic proposition (no ==> signs) *)
   1.157 +val atomic = null o strip_assums_hyp;
   1.158 +
   1.159 +(*ccs contains the names of the constants possessing congruence rules*)
   1.160 +fun add_hidden_vars ccs =
   1.161 +  let fun add_hvars(tm,hvars) = case tm of
   1.162 +	      Abs(_,_,body) => add_term_vars(body,hvars)
   1.163 +	    | _$_ => let val (f,args) = strip_comb tm 
   1.164 +		     in case f of
   1.165 +			    Const(c,T) => 
   1.166 +				if c mem ccs
   1.167 +				then foldr add_hvars (args,hvars)
   1.168 +				else add_term_vars(tm,hvars)
   1.169 +			  | _ => add_term_vars(tm,hvars)
   1.170 +		     end
   1.171 +	    | _ => hvars;
   1.172 +  in add_hvars end;
   1.173 +
   1.174 +fun add_new_asm_vars new_asms =
   1.175 +    let fun itf((tm,at),vars) =
   1.176 +		if at then vars else add_term_vars(tm,vars)
   1.177 +	fun add_list(tm,al,vars) = let val (_,tml) = strip_comb tm
   1.178 +		in if length(tml)=length(al)
   1.179 +		   then foldr itf (tml~~al,vars)
   1.180 +		   else vars
   1.181 +		end
   1.182 +	fun add_vars (tm,vars) = case tm of
   1.183 +		  Abs (_,_,body) => add_vars(body,vars)
   1.184 +		| r$s => (case head_of tm of
   1.185 +			  Const(c,T) => (case assoc(new_asms,c) of
   1.186 +				  None => add_vars(r,add_vars(s,vars))
   1.187 +				| Some(al) => add_list(tm,al,vars))
   1.188 +			| _ => add_vars(r,add_vars(s,vars)))
   1.189 +		| _ => vars
   1.190 +    in add_vars end;
   1.191 +
   1.192 +
   1.193 +fun add_norms(congs,ccs,new_asms) thm =
   1.194 +let val thm' = mk_trans2 thm;
   1.195 +(* thm': [?z -> l; Prems; r -> ?t] ==> ?z -> ?t *)
   1.196 +    val nops = nprems_of thm'
   1.197 +    val lhs = rhs_of_eq 1 thm'
   1.198 +    val rhs = lhs_of_eq nops thm'
   1.199 +    val asms = tl(rev(tl(prems_of thm')))
   1.200 +    val hvars = foldr (add_hidden_vars ccs) (lhs::rhs::asms,[])
   1.201 +    val hvars = add_new_asm_vars new_asms (rhs,hvars)
   1.202 +    fun it_asms (asm,hvars) =
   1.203 +	if atomic asm then add_new_asm_vars new_asms (asm,hvars)
   1.204 +	else add_term_frees(asm,hvars)
   1.205 +    val hvars = foldr it_asms (asms,hvars)
   1.206 +    val hvs = map (#1 o dest_Var) hvars
   1.207 +    val refl1_tac = refl_tac 1
   1.208 +    val add_norm_tac = DEPTH_FIRST (has_fewer_prems nops)
   1.209 +	      (STATE(fn thm =>
   1.210 +		case head_of(rhs_of_eq 1 thm) of
   1.211 +		  Var(ixn,_) => if ixn mem hvs then refl1_tac
   1.212 +				else resolve_tac normI_thms 1 ORELSE refl1_tac
   1.213 +		| Const _ => resolve_tac normI_thms 1 ORELSE
   1.214 +			     resolve_tac congs 1 ORELSE refl1_tac
   1.215 +		| Free _ => resolve_tac congs 1 ORELSE refl1_tac
   1.216 +		| _ => refl1_tac))
   1.217 +    val Some(thm'',_) = Sequence.pull(tapply(add_norm_tac,thm'))
   1.218 +in thm'' end;
   1.219 +
   1.220 +fun add_norm_tags congs =
   1.221 +    let val ccs = map cong_const congs
   1.222 +	val new_asms = filter (exists not o #2)
   1.223 +		(ccs ~~ (map (map atomic o prems_of) congs));
   1.224 +    in add_norms(congs,ccs,new_asms) end;
   1.225 +
   1.226 +fun normed_rews congs =
   1.227 +  let val add_norms = add_norm_tags congs;
   1.228 +  in fn thm => map (varifyT o add_norms o mk_trans) (mk_rew_rules(freezeT thm))
   1.229 +  end;
   1.230 +
   1.231 +fun NORM norm_lhs_tac = EVERY'[resolve_tac [red2], norm_lhs_tac, refl_tac];
   1.232 +
   1.233 +val trans_norms = map mk_trans normE_thms;
   1.234 +
   1.235 +
   1.236 +(* SIMPSET *)
   1.237 +
   1.238 +datatype simpset =
   1.239 +	SS of {auto_tac: int -> tactic,
   1.240 +	       congs: thm list,
   1.241 +	       cong_net: thm Net.net,
   1.242 +	       mk_simps: thm -> thm list,
   1.243 +	       simps: (thm * thm list) list,
   1.244 +	       simp_net: thm Net.net}
   1.245 +
   1.246 +val empty_ss = SS{auto_tac= K no_tac, congs=[], cong_net=Net.empty,
   1.247 +		  mk_simps=normed_rews[], simps=[], simp_net=Net.empty};
   1.248 +
   1.249 +(** Insertion of congruences and rewrites **)
   1.250 +
   1.251 +(*insert a thm in a thm net*)
   1.252 +fun insert_thm_warn (th,net) = 
   1.253 +  Net.insert_term((concl_of th, th), net, eq_thm)
   1.254 +  handle Net.INSERT => 
   1.255 +    (writeln"\nDuplicate rewrite or congruence rule:"; print_thm th;
   1.256 +     net);
   1.257 +
   1.258 +val insert_thms = foldr insert_thm_warn;
   1.259 +
   1.260 +fun addrew(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net}, thm) =
   1.261 +let val thms = mk_simps thm
   1.262 +in SS{auto_tac=auto_tac,congs=congs, cong_net=cong_net, mk_simps=mk_simps,
   1.263 +      simps = (thm,thms)::simps, simp_net = insert_thms(thms,simp_net)}
   1.264 +end;
   1.265 +
   1.266 +val op addrews = foldl addrew;
   1.267 +
   1.268 +fun op addcongs(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net}, thms) =
   1.269 +let val congs' = thms @ congs;
   1.270 +in SS{auto_tac=auto_tac, congs= congs',
   1.271 +      cong_net= insert_thms (map mk_trans thms,cong_net),
   1.272 +      mk_simps= normed_rews congs', simps=simps, simp_net=simp_net}
   1.273 +end;
   1.274 +
   1.275 +(** Deletion of congruences and rewrites **)
   1.276 +
   1.277 +(*delete a thm from a thm net*)
   1.278 +fun delete_thm_warn (th,net) = 
   1.279 +  Net.delete_term((concl_of th, th), net, eq_thm)
   1.280 +  handle Net.DELETE => 
   1.281 +    (writeln"\nNo such rewrite or congruence rule:";  print_thm th;
   1.282 +     net);
   1.283 +
   1.284 +val delete_thms = foldr delete_thm_warn;
   1.285 +
   1.286 +fun op delcongs(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net}, thms) =
   1.287 +let val congs' = foldl (gen_rem eq_thm) (congs,thms)
   1.288 +in SS{auto_tac=auto_tac, congs= congs',
   1.289 +      cong_net= delete_thms(map mk_trans thms,cong_net),
   1.290 +      mk_simps= normed_rews congs', simps=simps, simp_net=simp_net}
   1.291 +end;
   1.292 +
   1.293 +fun delrew(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net}, thm) =
   1.294 +let fun find((p as (th,ths))::ps',ps) =
   1.295 +	  if eq_thm(thm,th) then (ths,ps@ps') else find(ps',p::ps)
   1.296 +      | find([],simps') = (writeln"\nNo such rewrite or congruence rule:";
   1.297 +			   print_thm thm;
   1.298 +			   ([],simps'))
   1.299 +    val (thms,simps') = find(simps,[])
   1.300 +in SS{auto_tac=auto_tac, congs=congs, cong_net=cong_net, mk_simps=mk_simps,
   1.301 +      simps = simps', simp_net = delete_thms(thms,simp_net)}
   1.302 +end;
   1.303 +
   1.304 +val op delrews = foldl delrew;
   1.305 +
   1.306 +
   1.307 +fun op setauto(SS{congs,cong_net,mk_simps,simps,simp_net,...}, auto_tac) =
   1.308 +    SS{auto_tac=auto_tac, congs=congs, cong_net=cong_net, mk_simps=mk_simps,
   1.309 +       simps=simps, simp_net=simp_net};
   1.310 +
   1.311 +
   1.312 +(** Inspection of a simpset **)
   1.313 +
   1.314 +fun dest_ss(SS{congs,simps,...}) = (congs, map #1 simps);
   1.315 +
   1.316 +fun print_ss(SS{congs,simps,...}) =
   1.317 +	(writeln"Congruences:"; prths congs;
   1.318 +	 writeln"Rewrite Rules:"; prths (map #1 simps); ());
   1.319 +
   1.320 +
   1.321 +(* Rewriting with conditionals *)
   1.322 +
   1.323 +val (case_thms,case_consts) = split_list case_splits;
   1.324 +val case_rews = map mk_trans case_thms;
   1.325 +
   1.326 +fun if_rewritable ifc i thm =
   1.327 +    let val tm = goal_concl i thm
   1.328 +	fun nobound(Abs(_,_,tm),j,k) = nobound(tm,j,k+1)
   1.329 +	  | nobound(s$t,j,k) = nobound(s,j,k) andalso nobound(t,j,k)
   1.330 +	  | nobound(Bound n,j,k) = n < k orelse k+j <= n
   1.331 +	  | nobound(_) = true;
   1.332 +	fun check_args(al,j) = forall (fn t => nobound(t,j,0)) al
   1.333 +	fun find_if(Abs(_,_,tm),j) = find_if(tm,j+1)
   1.334 +	  | find_if(tm as s$t,j) = let val (f,al) = strip_comb tm in
   1.335 +		case f of Const(c,_) =>	if c=ifc then check_args(al,j)
   1.336 +			else find_if(s,j) orelse find_if(t,j)
   1.337 +		| _ => find_if(s,j) orelse find_if(t,j) end
   1.338 +	  | find_if(_) = false;
   1.339 +    in find_if(tm,0) end;
   1.340 +
   1.341 +fun IF1_TAC cong_tac i =
   1.342 +    let fun seq_try (ifth::ifths,ifc::ifcs) thm = tapply(
   1.343 +		COND (if_rewritable ifc i) (DETERM(resolve_tac[ifth]i))
   1.344 +			(Tactic(seq_try(ifths,ifcs))), thm)
   1.345 +	      | seq_try([],_) thm = tapply(no_tac,thm)
   1.346 +	and try_rew thm = tapply(Tactic(seq_try(case_rews,case_consts))
   1.347 +				 ORELSE Tactic one_subt, thm)
   1.348 +	and one_subt thm =
   1.349 +		let val test = has_fewer_prems (nprems_of thm + 1)
   1.350 +		    fun loop thm = tapply(COND test no_tac
   1.351 +			((Tactic try_rew THEN DEPTH_FIRST test (refl_tac i))
   1.352 +			 ORELSE (refl_tac i THEN Tactic loop)), thm)
   1.353 +		in tapply(cong_tac THEN Tactic loop, thm) end
   1.354 +    in COND (may_match(case_consts,i)) (Tactic try_rew) no_tac end;
   1.355 +
   1.356 +fun CASE_TAC (SS{cong_net,...}) i =
   1.357 +let val cong_tac = net_tac cong_net i
   1.358 +in NORM (IF1_TAC cong_tac) i end;
   1.359 +
   1.360 +(* Rewriting Automaton *)
   1.361 +
   1.362 +datatype cntrl = STOP | MK_EQ | ASMS of int | SIMP_LHS | REW | REFL | TRUE
   1.363 +	       | PROVE | POP_CS | POP_ARTR | IF;
   1.364 +(*
   1.365 +fun pr_cntrl c = case c of STOP => prs("STOP") | MK_EQ => prs("MK_EQ") |
   1.366 +ASMS i => print_int i | POP_ARTR => prs("POP_ARTR") |
   1.367 +SIMP_LHS => prs("SIMP_LHS") | REW => prs("REW") | REFL => prs("REFL") |
   1.368 +TRUE => prs("TRUE") | PROVE => prs("PROVE") | POP_CS => prs("POP_CS") | IF
   1.369 +=> prs("IF");
   1.370 +*)
   1.371 +fun simp_refl([],_,ss) = ss
   1.372 +  | simp_refl(a'::ns,a,ss) = if a'=a then simp_refl(ns,a,SIMP_LHS::REFL::ss)
   1.373 +	else simp_refl(ns,a,ASMS(a)::SIMP_LHS::REFL::POP_ARTR::ss);
   1.374 +
   1.375 +(** Tracing **)
   1.376 +
   1.377 +val tracing = ref false;
   1.378 +
   1.379 +(*Replace parameters by Free variables in P*)
   1.380 +fun variants_abs ([],P) = P
   1.381 +  | variants_abs ((a,T)::aTs, P) =
   1.382 +      variants_abs (aTs, #2 (variant_abs(a,T,P)));
   1.383 +
   1.384 +(*Select subgoal i from proof state; substitute parameters, for printing*)
   1.385 +fun prepare_goal i st =
   1.386 +    let val subgi = nth_subgoal i st
   1.387 +	val params = rev(strip_params subgi)
   1.388 +    in variants_abs (params, strip_assums_concl subgi) end;
   1.389 +
   1.390 +(*print lhs of conclusion of subgoal i*)
   1.391 +fun pr_goal_lhs i st =
   1.392 +    writeln (Sign.string_of_term (#sign(rep_thm st)) 
   1.393 +	     (lhs_of (prepare_goal i st)));
   1.394 +
   1.395 +(*print conclusion of subgoal i*)
   1.396 +fun pr_goal_concl i st =
   1.397 +    writeln (Sign.string_of_term (#sign(rep_thm st)) (prepare_goal i st)) 
   1.398 +
   1.399 +(*print subgoals i to j (inclusive)*)
   1.400 +fun pr_goals (i,j) st =
   1.401 +    if i>j then ()
   1.402 +    else (pr_goal_concl i st;  pr_goals (i+1,j) st);
   1.403 +
   1.404 +(*Print rewrite for tracing; i=subgoal#, n=number of new subgoals,
   1.405 +  thm=old state, thm'=new state *)
   1.406 +fun pr_rew (i,n,thm,thm',not_asms) =
   1.407 +    if !tracing
   1.408 +    then (if not_asms then () else writeln"Assumption used in";
   1.409 +          pr_goal_lhs i thm; writeln"->"; pr_goal_lhs (i+n) thm';
   1.410 +	  if n>0 then (writeln"Conditions:"; pr_goals (i, i+n-1) thm')
   1.411 +          else ();
   1.412 +          writeln"" )
   1.413 +    else ();
   1.414 +
   1.415 +(* Skip the first n hyps of a goal, and return the rest in generalized form *)
   1.416 +fun strip_varify(Const("==>", _) $ H $ B, n, vs) =
   1.417 +	if n=0 then subst_bounds(vs,H)::strip_varify(B,0,vs)
   1.418 +	else strip_varify(B,n-1,vs)
   1.419 +  | strip_varify(Const("all",_)$Abs(_,T,t), n, vs) =
   1.420 +	strip_varify(t,n,Var(("?",length vs),T)::vs)
   1.421 +  | strip_varify  _  = [];
   1.422 +
   1.423 +fun execute(ss,if_fl,auto_tac,cong_tac,net,i,thm) = let
   1.424 +
   1.425 +fun simp_lhs(thm,ss,anet,ats,cs) =
   1.426 +    if var_lhs(thm,i) then (ss,thm,anet,ats,cs) else
   1.427 +    if lhs_is_NORM(thm,i) then (ss, res1(thm,trans_norms,i), anet,ats,cs)
   1.428 +    else case Sequence.pull(tapply(cong_tac i,thm)) of
   1.429 +	    Some(thm',_) =>
   1.430 +		    let val ps = prems_of thm and ps' = prems_of thm';
   1.431 +			val n = length(ps')-length(ps);
   1.432 +			val a = length(strip_assums_hyp(nth_elem(i-1,ps)))
   1.433 +			val l = map (fn p => length(strip_assums_hyp(p)))
   1.434 +				    (take(n,drop(i-1,ps')));
   1.435 +		    in (simp_refl(rev(l),a,REW::ss),thm',anet,ats,cs) end
   1.436 +	  | None => (REW::ss,thm,anet,ats,cs);
   1.437 +
   1.438 +(*NB: the "Adding rewrites:" trace will look strange because assumptions
   1.439 +      are represented by rules, generalized over their parameters*)
   1.440 +fun add_asms(ss,thm,a,anet,ats,cs) =
   1.441 +    let val As = strip_varify(nth_subgoal i thm, a, []);
   1.442 +	val thms = map (trivial o Sign.cterm_of(#sign(rep_thm(thm))))As;
   1.443 +	val new_rws = flat(map mk_rew_rules thms);
   1.444 +	val rwrls = map mk_trans (flat(map mk_rew_rules thms));
   1.445 +	val anet' = foldr lhs_insert_thm (rwrls,anet)
   1.446 +    in  if !tracing andalso not(null new_rws)
   1.447 +	then (writeln"Adding rewrites:";  prths new_rws;  ())
   1.448 +	else ();
   1.449 +	(ss,thm,anet',anet::ats,cs) 
   1.450 +    end;
   1.451 +
   1.452 +fun rew(seq,thm,ss,anet,ats,cs, more) = case Sequence.pull seq of
   1.453 +      Some(thm',seq') =>
   1.454 +	    let val n = (nprems_of thm') - (nprems_of thm)
   1.455 +	    in pr_rew(i,n,thm,thm',more);
   1.456 +	       if n=0 then (SIMP_LHS::ss, thm', anet, ats, cs)
   1.457 +	       else ((replicate n PROVE) @ (POP_CS::SIMP_LHS::ss),
   1.458 +		     thm', anet, ats, (ss,thm,anet,ats,seq',more)::cs)
   1.459 +	    end
   1.460 +    | None => if more
   1.461 +	    then rew(tapply(lhs_net_tac anet i THEN assume_tac i,thm),
   1.462 +		     thm,ss,anet,ats,cs,false)
   1.463 +	    else (ss,thm,anet,ats,cs);
   1.464 +
   1.465 +fun try_true(thm,ss,anet,ats,cs) =
   1.466 +    case Sequence.pull(tapply(auto_tac i,thm)) of
   1.467 +      Some(thm',_) => (ss,thm',anet,ats,cs)
   1.468 +    | None => let val (ss0,thm0,anet0,ats0,seq,more)::cs0 = cs
   1.469 +	      in if !tracing
   1.470 +		 then (writeln"*** Failed to prove precondition. Normal form:";
   1.471 +		       pr_goal_concl i thm;  writeln"")
   1.472 +		 else ();
   1.473 +		 rew(seq,thm0,ss0,anet0,ats0,cs0,more)
   1.474 +	      end;
   1.475 +
   1.476 +fun if_exp(thm,ss,anet,ats,cs) =
   1.477 +	case Sequence.pull(tapply(IF1_TAC (cong_tac i) i,thm)) of
   1.478 +		Some(thm',_) => (SIMP_LHS::IF::ss,thm',anet,ats,cs)
   1.479 +	      | None => (ss,thm,anet,ats,cs);
   1.480 +
   1.481 +fun step(s::ss, thm, anet, ats, cs) = case s of
   1.482 +	  MK_EQ => (ss, res1(thm,[red2],i), anet, ats, cs)
   1.483 +	| ASMS(a) => add_asms(ss,thm,a,anet,ats,cs)
   1.484 +	| SIMP_LHS => simp_lhs(thm,ss,anet,ats,cs)
   1.485 +	| REW => rew(tapply(net_tac net i,thm),thm,ss,anet,ats,cs,true)
   1.486 +	| REFL => (ss, res1(thm,refl_thms,i), anet, ats, cs)
   1.487 +	| TRUE => try_true(res1(thm,refl_thms,i),ss,anet,ats,cs)
   1.488 +	| PROVE => (if if_fl then MK_EQ::SIMP_LHS::IF::TRUE::ss
   1.489 +		    else MK_EQ::SIMP_LHS::TRUE::ss, thm, anet, ats, cs)
   1.490 +	| POP_ARTR => (ss,thm,hd ats,tl ats,cs)
   1.491 +	| POP_CS => (ss,thm,anet,ats,tl cs)
   1.492 +	| IF => if_exp(thm,ss,anet,ats,cs);
   1.493 +
   1.494 +fun exec(state as (s::ss, thm, _, _, _)) =
   1.495 +	if s=STOP then thm else exec(step(state));
   1.496 +
   1.497 +in exec(ss, thm, Net.empty, [], []) end;
   1.498 +
   1.499 +
   1.500 +fun EXEC_TAC(ss,fl) (SS{auto_tac,cong_net,simp_net,...}) =
   1.501 +let val cong_tac = net_tac cong_net
   1.502 +in fn i => Tactic(fn thm =>
   1.503 +	if i <= 0 orelse nprems_of thm < i then Sequence.null
   1.504 +	else Sequence.single(execute(ss,fl,auto_tac,cong_tac,simp_net,i,thm)))
   1.505 +	   THEN TRY(auto_tac i)
   1.506 +end;
   1.507 +
   1.508 +val SIMP_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,REFL,STOP],false);
   1.509 +val SIMP_CASE_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,IF,REFL,STOP],false);
   1.510 +
   1.511 +val ASM_SIMP_TAC = EXEC_TAC([ASMS(0),MK_EQ,SIMP_LHS,REFL,STOP],false);
   1.512 +val ASM_SIMP_CASE_TAC = EXEC_TAC([ASMS(0),MK_EQ,SIMP_LHS,IF,REFL,STOP],false);
   1.513 +
   1.514 +val SIMP_CASE2_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,IF,REFL,STOP],true);
   1.515 +
   1.516 +fun REWRITE (ss,fl) (SS{auto_tac,cong_net,simp_net,...}) =
   1.517 +let val cong_tac = net_tac cong_net
   1.518 +in fn thm => let val state = thm RSN (2,red1)
   1.519 +	     in execute(ss,fl,auto_tac,cong_tac,simp_net,1,state) end
   1.520 +end;
   1.521 +
   1.522 +val SIMP_THM = REWRITE ([ASMS(0),SIMP_LHS,IF,REFL,STOP],false);
   1.523 +
   1.524 +
   1.525 +(* Compute Congruence rules for individual constants using the substition
   1.526 +   rules *)
   1.527 +
   1.528 +val subst_thms = map standard subst_thms;
   1.529 +
   1.530 +
   1.531 +fun exp_app(0,t) = t
   1.532 +  | exp_app(i,t) = exp_app(i-1,t $ Bound (i-1));
   1.533 +
   1.534 +fun exp_abs(Type("fun",[T1,T2]),t,i) =
   1.535 +	Abs("x"^string_of_int i,T1,exp_abs(T2,t,i+1))
   1.536 +  | exp_abs(T,t,i) = exp_app(i,t);
   1.537 +
   1.538 +fun eta_Var(ixn,T) = exp_abs(T,Var(ixn,T),0);
   1.539 +
   1.540 +
   1.541 +fun Pinst(f,fT,(eq,eqT),k,i,T,yik,Ts) =
   1.542 +let fun xn_list(x,n) =
   1.543 +	let val ixs = map (fn i => (x^(radixstring(26,"a",i)),0)) (0 upto n);
   1.544 +	in map eta_Var (ixs ~~ (take(n+1,Ts))) end
   1.545 +    val lhs = list_comb(f,xn_list("X",k-1))
   1.546 +    val rhs = list_comb(f,xn_list("X",i-1) @ [Bound 0] @ yik)
   1.547 +in Abs("", T, Const(eq,[fT,fT]--->eqT) $ lhs $ rhs) end;
   1.548 +
   1.549 +fun find_subst tsig T =
   1.550 +let fun find (thm::thms) =
   1.551 +	let val (Const(_,cT), va, vb) =	dest_red(hd(prems_of thm));
   1.552 +	    val [P] = add_term_vars(concl_of thm,[]) \\ [va,vb]
   1.553 +	    val eqT::_ = binder_types cT
   1.554 +        in if Type.typ_instance(tsig,T,eqT) then Some(thm,va,vb,P)
   1.555 +	   else find thms
   1.556 +	end
   1.557 +      | find [] = None
   1.558 +in find subst_thms end;
   1.559 +
   1.560 +fun mk_cong sg (f,aTs,rT) (refl,eq) =
   1.561 +let val tsig = #tsig(Sign.rep_sg sg);
   1.562 +    val k = length aTs;
   1.563 +    fun ri((subst,va as Var(_,Ta),vb as Var(_,Tb),P),i,si,T,yik) =
   1.564 +	let val ca = Sign.cterm_of sg va
   1.565 +	    and cx = Sign.cterm_of sg (eta_Var(("X"^si,0),T))
   1.566 +	    val cb = Sign.cterm_of sg vb
   1.567 +	    and cy = Sign.cterm_of sg (eta_Var(("Y"^si,0),T))
   1.568 +	    val cP = Sign.cterm_of sg P
   1.569 +	    and cp = Sign.cterm_of sg (Pinst(f,rT,eq,k,i,T,yik,aTs))
   1.570 +	in cterm_instantiate [(ca,cx),(cb,cy),(cP,cp)] subst end;
   1.571 +    fun mk(c,T::Ts,i,yik) =
   1.572 +	let val si = radixstring(26,"a",i)
   1.573 +	in case find_subst tsig T of
   1.574 +	     None => mk(c,Ts,i-1,eta_Var(("X"^si,0),T)::yik)
   1.575 +	   | Some s => let val c' = c RSN (2,ri(s,i,si,T,yik))
   1.576 +		       in mk(c',Ts,i-1,eta_Var(("Y"^si,0),T)::yik) end
   1.577 +	end
   1.578 +      | mk(c,[],_,_) = c;
   1.579 +in mk(refl,rev aTs,k-1,[]) end;
   1.580 +
   1.581 +fun mk_cong_type sg (f,T) =
   1.582 +let val (aTs,rT) = strip_type T;
   1.583 +    val tsig = #tsig(Sign.rep_sg sg);
   1.584 +    fun find_refl(r::rs) =
   1.585 +	let val (Const(eq,eqT),_,_) = dest_red(concl_of r)
   1.586 +	in if Type.typ_instance(tsig, rT, hd(binder_types eqT))
   1.587 +	   then Some(r,(eq,body_type eqT)) else find_refl rs
   1.588 +	end
   1.589 +      | find_refl([]) = None;
   1.590 +in case find_refl refl_thms of
   1.591 +     None => []  |  Some(refl) => [mk_cong sg (f,aTs,rT) refl]
   1.592 +end;
   1.593 +
   1.594 +fun mk_cong_thy thy f =
   1.595 +let val sg = sign_of thy;
   1.596 +    val T = case Sign.Symtab.lookup(#const_tab(Sign.rep_sg sg),f) of
   1.597 +		None => error(f^" not declared") | Some(T) => T;
   1.598 +    val T' = incr_tvar 9 T;
   1.599 +in mk_cong_type sg (Const(f,T'),T') end;
   1.600 +
   1.601 +fun mk_congs thy = flat o map (mk_cong_thy thy);
   1.602 +
   1.603 +fun mk_typed_congs thy =
   1.604 +let val sg = sign_of thy;
   1.605 +    val S0 = Type.defaultS(#tsig(Sign.rep_sg sg))
   1.606 +    fun readfT(f,s) =
   1.607 +	let val T = incr_tvar 9 (Sign.read_typ(sg,K(Some(S0))) s);
   1.608 +	    val t = case Sign.Symtab.lookup(#const_tab(Sign.rep_sg sg),f) of
   1.609 +		      Some(_) => Const(f,T) | None => Free(f,T)
   1.610 +	in (t,T) end
   1.611 +in flat o map (mk_cong_type sg o readfT) end;
   1.612 +
   1.613 +end (* local *)
   1.614 +end (* SIMP *);