src/Provers/simp.ML
author clasohm
Thu Sep 16 12:20:38 1993 +0200 (1993-09-16 ago)
changeset 0 a5a9c433f639
child 1 c6a6e3db5353
permissions -rw-r--r--
Initial revision
     1 (*  Title:      Provers/simp
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     Copyright   1993  University of Cambridge
     5 
     6 Generic simplifier, suitable for most logics.  The only known exception is
     7 Constructive Type Theory.  The reflexivity axiom must be unconditional,
     8 namely a=a not a:A ==> a=a:A.  Used typedsimp.ML otherwise.  
     9 *)
    10 
    11 signature SIMP_DATA =
    12 sig
    13   val dest_red     : term -> term * term * term
    14   val mk_rew_rules : thm -> thm list
    15   val norm_thms    : (thm*thm) list (* [(?x>>norm(?x), norm(?x)>>?x), ...] *)
    16   val red1         : thm        (*  ?P>>?Q  ==>  ?P  ==>  ?Q  *)
    17   val red2         : thm        (*  ?P>>?Q  ==>  ?Q  ==>  ?P  *)
    18   val refl_thms    : thm list
    19   val subst_thms   : thm list   (* [ ?a>>?b ==> ?P(?a) ==> ?P(?b), ...] *)
    20   val trans_thms   : thm list
    21 end;
    22 
    23 
    24 infix 4 addrews addcongs addsplits delrews delcongs setauto;
    25 
    26 signature SIMP =
    27 sig
    28   type simpset
    29   val empty_ss  : simpset
    30   val addcongs  : simpset * thm list -> simpset
    31   val addrews   : simpset * thm list -> simpset
    32   val addsplits : simpset * thm list -> simpset
    33   val delcongs  : simpset * thm list -> simpset
    34   val delrews   : simpset * thm list -> simpset
    35   val dest_ss   : simpset -> thm list * thm list
    36   val print_ss  : simpset -> unit
    37   val setauto   : simpset * (thm list -> int -> tactic) -> simpset
    38   val ASM_SIMP_TAC      : simpset -> int -> tactic
    39   val SPLIT_TAC          : simpset -> int -> tactic
    40   val SIMP_SPLIT2_TAC    : simpset -> int -> tactic
    41   val SIMP_THM          : simpset -> thm -> thm
    42   val SIMP_TAC          : simpset -> int -> tactic
    43   val mk_congs          : theory -> string list -> thm list
    44   val mk_typed_congs    : theory -> (string * string) list -> thm list
    45 (* temporarily disabled:
    46   val extract_free_congs        : unit -> thm list
    47 *)
    48   val tracing   : bool ref
    49 end;
    50 
    51 functor SimpFun (Simp_data: SIMP_DATA) : SIMP = 
    52 struct
    53 
    54 local open Simp_data Logic in
    55 
    56 (*For taking apart reductions into left, right hand sides*)
    57 val lhs_of = #2 o dest_red;
    58 val rhs_of = #3 o dest_red;
    59 
    60 (*** Indexing and filtering of theorems ***)
    61 
    62 fun eq_brl ((b1,th1),(b2,th2)) = b1=b2 andalso eq_thm(th1,th2);
    63 
    64 (*insert a thm in a discrimination net by its lhs*)
    65 fun lhs_insert_thm (th,net) =
    66     Net.insert_term((lhs_of (concl_of th), (false,th)), net, eq_brl)
    67     handle  Net.INSERT => net;
    68 
    69 (*match subgoal i against possible theorems in the net.
    70   Similar to match_from_nat_tac, but the net does not contain numbers;
    71   rewrite rules are not ordered.*)
    72 fun net_tac net =
    73   SUBGOAL(fn (prem,i) => 
    74 	  match_tac (Net.match_term net (strip_assums_concl prem)) i);
    75 
    76 (*match subgoal i against possible theorems indexed by lhs in the net*)
    77 fun lhs_net_tac net =
    78   SUBGOAL(fn (prem,i) => 
    79 	  bimatch_tac (Net.match_term net
    80 		       (lhs_of (strip_assums_concl prem))) i);
    81 
    82 fun nth_subgoal i thm = nth_elem(i-1,prems_of thm);
    83 
    84 fun goal_concl i thm = strip_assums_concl(nth_subgoal i thm);
    85 
    86 fun lhs_of_eq i thm = lhs_of(goal_concl i thm)
    87 and rhs_of_eq i thm = rhs_of(goal_concl i thm);
    88 
    89 fun var_lhs(thm,i) =
    90 let fun var(Var _) = true
    91       | var(Abs(_,_,t)) = var t
    92       | var(f$_) = var f
    93       | var _ = false;
    94 in var(lhs_of_eq i thm) end;
    95 
    96 fun contains_op opns =
    97     let fun contains(Const(s,_)) = s mem opns |
    98             contains(s$t) = contains s orelse contains t |
    99             contains(Abs(_,_,t)) = contains t |
   100             contains _ = false;
   101     in contains end;
   102 
   103 fun may_match(match_ops,i) = contains_op match_ops o lhs_of_eq i;
   104 
   105 val (normI_thms,normE_thms) = split_list norm_thms;
   106 
   107 (*Get the norm constants from norm_thms*)
   108 val norms =
   109   let fun norm thm = 
   110       case lhs_of(concl_of thm) of
   111 	  Const(n,_)$_ => n
   112 	| _ => (prths normE_thms; error"No constant in lhs of a norm_thm")
   113   in map norm normE_thms end;
   114 
   115 fun lhs_is_NORM(thm,i) = case lhs_of_eq i thm of
   116 	Const(s,_)$_ => s mem norms | _ => false;
   117 
   118 val refl_tac = resolve_tac refl_thms;
   119 
   120 fun find_res thms thm =
   121     let fun find [] = (prths thms; error"Check Simp_Data")
   122           | find(th::thms) = thm RS th handle _ => find thms
   123     in find thms end;
   124 
   125 val mk_trans = find_res trans_thms;
   126 
   127 fun mk_trans2 thm =
   128 let fun mk[] = error"Check transitivity"
   129       | mk(t::ts) = (thm RSN (2,t))  handle _  => mk ts
   130 in mk trans_thms end;
   131 
   132 (*Applies tactic and returns the first resulting state, FAILS if none!*)
   133 fun one_result(tac,thm) = case Sequence.pull(tapply(tac,thm)) of
   134 	Some(thm',_) => thm'
   135       | None => raise THM("Simplifier: could not continue", 0, [thm]);
   136 
   137 fun res1(thm,thms,i) = one_result(resolve_tac thms i,thm);
   138 
   139 
   140 (**** Adding "NORM" tags ****)
   141 
   142 (*get name of the constant from conclusion of a congruence rule*)
   143 fun cong_const cong = 
   144     case head_of (lhs_of (concl_of cong)) of
   145 	Const(c,_) => c
   146       | _ => ""			(*a placeholder distinct from const names*);
   147 
   148 (*true if the term is an atomic proposition (no ==> signs) *)
   149 val atomic = null o strip_assums_hyp;
   150 
   151 (*ccs contains the names of the constants possessing congruence rules*)
   152 fun add_hidden_vars ccs =
   153   let fun add_hvars(tm,hvars) = case tm of
   154 	      Abs(_,_,body) => add_term_vars(body,hvars)
   155 	    | _$_ => let val (f,args) = strip_comb tm 
   156 		     in case f of
   157 			    Const(c,T) => 
   158 				if c mem ccs
   159 				then foldr add_hvars (args,hvars)
   160 				else add_term_vars(tm,hvars)
   161 			  | _ => add_term_vars(tm,hvars)
   162 		     end
   163 	    | _ => hvars;
   164   in add_hvars end;
   165 
   166 fun add_new_asm_vars new_asms =
   167     let fun itf((tm,at),vars) =
   168 		if at then vars else add_term_vars(tm,vars)
   169 	fun add_list(tm,al,vars) = let val (_,tml) = strip_comb tm
   170 		in if length(tml)=length(al)
   171 		   then foldr itf (tml~~al,vars)
   172 		   else vars
   173 		end
   174 	fun add_vars (tm,vars) = case tm of
   175 		  Abs (_,_,body) => add_vars(body,vars)
   176 		| r$s => (case head_of tm of
   177 			  Const(c,T) => (case assoc(new_asms,c) of
   178 				  None => add_vars(r,add_vars(s,vars))
   179 				| Some(al) => add_list(tm,al,vars))
   180 			| _ => add_vars(r,add_vars(s,vars)))
   181 		| _ => vars
   182     in add_vars end;
   183 
   184 
   185 fun add_norms(congs,ccs,new_asms) thm =
   186 let val thm' = mk_trans2 thm;
   187 (* thm': [?z -> l; Prems; r -> ?t] ==> ?z -> ?t *)
   188     val nops = nprems_of thm'
   189     val lhs = rhs_of_eq 1 thm'
   190     val rhs = lhs_of_eq nops thm'
   191     val asms = tl(rev(tl(prems_of thm')))
   192     val hvars = foldr (add_hidden_vars ccs) (lhs::rhs::asms,[])
   193     val hvars = add_new_asm_vars new_asms (rhs,hvars)
   194     fun it_asms (asm,hvars) =
   195 	if atomic asm then add_new_asm_vars new_asms (asm,hvars)
   196 	else add_term_frees(asm,hvars)
   197     val hvars = foldr it_asms (asms,hvars)
   198     val hvs = map (#1 o dest_Var) hvars
   199     val refl1_tac = refl_tac 1
   200     val add_norm_tac = DEPTH_FIRST (has_fewer_prems nops)
   201 	      (STATE(fn thm =>
   202 		case head_of(rhs_of_eq 1 thm) of
   203 		  Var(ixn,_) => if ixn mem hvs then refl1_tac
   204 				else resolve_tac normI_thms 1 ORELSE refl1_tac
   205 		| Const _ => resolve_tac normI_thms 1 ORELSE
   206 			     resolve_tac congs 1 ORELSE refl1_tac
   207 		| Free _ => resolve_tac congs 1 ORELSE refl1_tac
   208 		| _ => refl1_tac))
   209     val Some(thm'',_) = Sequence.pull(tapply(add_norm_tac,thm'))
   210 in thm'' end;
   211 
   212 fun add_norm_tags congs =
   213     let val ccs = map cong_const congs
   214 	val new_asms = filter (exists not o #2)
   215 		(ccs ~~ (map (map atomic o prems_of) congs));
   216     in add_norms(congs,ccs,new_asms) end;
   217 
   218 fun normed_rews congs =
   219   let val add_norms = add_norm_tags congs;
   220   in fn thm => map (varifyT o add_norms o mk_trans) (mk_rew_rules(freezeT thm))
   221   end;
   222 
   223 fun NORM norm_lhs_tac = EVERY'[resolve_tac [red2], norm_lhs_tac, refl_tac];
   224 
   225 val trans_norms = map mk_trans normE_thms;
   226 
   227 
   228 (* SIMPSET *)
   229 
   230 datatype simpset =
   231 	SS of {auto_tac: thm list -> int -> tactic,
   232 	       congs: thm list,
   233 	       cong_net: thm Net.net,
   234 	       mk_simps: thm -> thm list,
   235 	       simps: (thm * thm list) list,
   236 	       simp_net: thm Net.net,
   237                splits: thm list,
   238                split_consts: string list}
   239 
   240 val empty_ss = SS{auto_tac= K (K no_tac), congs=[], cong_net=Net.empty,
   241 		  mk_simps=normed_rews[], simps=[], simp_net=Net.empty,
   242                   splits=[], split_consts=[]};
   243 
   244 (** Insertion of congruences, rewrites and case splits **)
   245 
   246 (*insert a thm in a thm net*)
   247 fun insert_thm_warn (th,net) = 
   248   Net.insert_term((concl_of th, th), net, eq_thm)
   249   handle Net.INSERT => 
   250     (writeln"\nDuplicate rewrite or congruence rule:"; print_thm th;
   251      net);
   252 
   253 val insert_thms = foldr insert_thm_warn;
   254 
   255 fun addrew(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net,
   256               splits,split_consts}, thm) =
   257 let val thms = mk_simps thm
   258 in SS{auto_tac=auto_tac,congs=congs, cong_net=cong_net, mk_simps=mk_simps,
   259       simps = (thm,thms)::simps, simp_net = insert_thms(thms,simp_net),
   260       splits=splits,split_consts=split_consts}
   261 end;
   262 
   263 val op addrews = foldl addrew;
   264 
   265 fun op addcongs(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net,
   266                    splits,split_consts}, thms) =
   267 let val congs' = thms @ congs;
   268 in SS{auto_tac=auto_tac, congs= congs',
   269       cong_net= insert_thms (map mk_trans thms,cong_net),
   270       mk_simps= normed_rews congs', simps=simps, simp_net=simp_net,
   271       splits=splits,split_consts=split_consts}
   272 end;
   273 
   274 fun split_err() = error("split rule not of the form ?P(c(...)) = ...");
   275 
   276 fun split_const(_ $ t) =
   277        (case head_of t of Const(a,_) => a | _ => split_err())
   278   | split_const _ = split_err();
   279 
   280 fun addsplit(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net,
   281                 splits,split_consts}, thm) =
   282 let val a = split_const(lhs_of(concl_of thm))
   283 in SS{auto_tac=auto_tac,congs=congs,cong_net=cong_net,
   284       mk_simps=mk_simps,simps=simps,simp_net=simp_net,
   285       splits=splits@[mk_trans thm],split_consts=split_consts@[a]} end;
   286 
   287 val op addsplits = foldl addsplit;
   288 
   289 (** Deletion of congruences and rewrites **)
   290 
   291 (*delete a thm from a thm net*)
   292 fun delete_thm_warn (th,net) = 
   293   Net.delete_term((concl_of th, th), net, eq_thm)
   294   handle Net.DELETE => 
   295     (writeln"\nNo such rewrite or congruence rule:";  print_thm th;
   296      net);
   297 
   298 val delete_thms = foldr delete_thm_warn;
   299 
   300 fun op delcongs(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net,
   301                    splits,split_consts}, thms) =
   302 let val congs' = foldl (gen_rem eq_thm) (congs,thms)
   303 in SS{auto_tac=auto_tac, congs= congs',
   304       cong_net= delete_thms(map mk_trans thms,cong_net),
   305       mk_simps= normed_rews congs', simps=simps, simp_net=simp_net,
   306       splits=splits,split_consts=split_consts}
   307 end;
   308 
   309 fun delrew(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net,
   310               splits,split_consts}, thm) =
   311 let fun find((p as (th,ths))::ps',ps) =
   312 	  if eq_thm(thm,th) then (ths,ps@ps') else find(ps',p::ps)
   313       | find([],simps') = (writeln"\nNo such rewrite or congruence rule:";
   314 			   print_thm thm;
   315 			   ([],simps'))
   316     val (thms,simps') = find(simps,[])
   317 in SS{auto_tac=auto_tac, congs=congs, cong_net=cong_net, mk_simps=mk_simps,
   318       simps = simps', simp_net = delete_thms(thms,simp_net),
   319       splits=splits,split_consts=split_consts}
   320 end;
   321 
   322 val op delrews = foldl delrew;
   323 
   324 
   325 fun op setauto(SS{congs,cong_net,mk_simps,simps,simp_net,
   326                   splits,split_consts,...}, auto_tac) =
   327     SS{auto_tac=auto_tac, congs=congs, cong_net=cong_net, mk_simps=mk_simps,
   328        simps=simps, simp_net=simp_net,splits=splits,split_consts=split_consts};
   329 
   330 
   331 (** Inspection of a simpset **)
   332 
   333 fun dest_ss(SS{congs,simps,...}) = (congs, map #1 simps);
   334 
   335 fun print_ss(SS{congs,simps,splits,...}) =
   336 	(writeln"Congruences:"; prths congs;
   337          writeln"Case Splits"; prths splits;
   338 	 writeln"Rewrite Rules:"; prths (map #1 simps); ());
   339 
   340 
   341 (* Rewriting with case splits *)
   342 
   343 fun splittable a i thm =
   344     let val tm = goal_concl i thm
   345 	fun nobound(Abs(_,_,tm),j,k) = nobound(tm,j,k+1)
   346 	  | nobound(s$t,j,k) = nobound(s,j,k) andalso nobound(t,j,k)
   347 	  | nobound(Bound n,j,k) = n < k orelse k+j <= n
   348 	  | nobound(_) = true;
   349 	fun check_args(al,j) = forall (fn t => nobound(t,j,0)) al
   350 	fun find_if(Abs(_,_,tm),j) = find_if(tm,j+1)
   351 	  | find_if(tm as s$t,j) = let val (f,al) = strip_comb tm in
   352 		case f of Const(c,_) =>	if c=a then check_args(al,j)
   353 			else find_if(s,j) orelse find_if(t,j)
   354 		| _ => find_if(s,j) orelse find_if(t,j) end
   355 	  | find_if(_) = false;
   356     in find_if(tm,0) end;
   357 
   358 fun split_tac (cong_tac,splits,split_consts) i =
   359     let fun seq_try (split::splits,a::bs) thm = tapply(
   360 		COND (splittable a i) (DETERM(resolve_tac[split]i))
   361 			(Tactic(seq_try(splits,bs))), thm)
   362 	      | seq_try([],_) thm = tapply(no_tac,thm)
   363 	and try_rew thm = tapply(Tactic(seq_try(splits,split_consts))
   364 				 ORELSE Tactic one_subt, thm)
   365 	and one_subt thm =
   366 		let val test = has_fewer_prems (nprems_of thm + 1)
   367 		    fun loop thm = tapply(COND test no_tac
   368 			((Tactic try_rew THEN DEPTH_FIRST test (refl_tac i))
   369 			 ORELSE (refl_tac i THEN Tactic loop)), thm)
   370 		in tapply(cong_tac THEN Tactic loop, thm) end
   371     in if null splits then no_tac
   372        else COND (may_match(split_consts,i)) (Tactic try_rew) no_tac
   373     end;
   374 
   375 fun SPLIT_TAC (SS{cong_net,splits,split_consts,...}) i =
   376 let val cong_tac = net_tac cong_net i
   377 in NORM (split_tac (cong_tac,splits,split_consts)) i end;
   378 
   379 (* Rewriting Automaton *)
   380 
   381 datatype cntrl = STOP | MK_EQ | ASMS of int | SIMP_LHS | REW | REFL | TRUE
   382 	       | PROVE | POP_CS | POP_ARTR | SPLIT;
   383 (*
   384 fun pr_cntrl c = case c of STOP => prs("STOP") | MK_EQ => prs("MK_EQ") |
   385 ASMS i => print_int i | POP_ARTR => prs("POP_ARTR") |
   386 SIMP_LHS => prs("SIMP_LHS") | REW => prs("REW") | REFL => prs("REFL") |
   387 TRUE => prs("TRUE") | PROVE => prs("PROVE") | POP_CS => prs("POP_CS") | SPLIT
   388 => prs("SPLIT");
   389 *)
   390 fun simp_refl([],_,ss) = ss
   391   | simp_refl(a'::ns,a,ss) = if a'=a then simp_refl(ns,a,SIMP_LHS::REFL::ss)
   392 	else simp_refl(ns,a,ASMS(a)::SIMP_LHS::REFL::POP_ARTR::ss);
   393 
   394 (** Tracing **)
   395 
   396 val tracing = ref false;
   397 
   398 (*Replace parameters by Free variables in P*)
   399 fun variants_abs ([],P) = P
   400   | variants_abs ((a,T)::aTs, P) =
   401       variants_abs (aTs, #2 (variant_abs(a,T,P)));
   402 
   403 (*Select subgoal i from proof state; substitute parameters, for printing*)
   404 fun prepare_goal i st =
   405     let val subgi = nth_subgoal i st
   406 	val params = rev(strip_params subgi)
   407     in variants_abs (params, strip_assums_concl subgi) end;
   408 
   409 (*print lhs of conclusion of subgoal i*)
   410 fun pr_goal_lhs i st =
   411     writeln (Sign.string_of_term (#sign(rep_thm st)) 
   412 	     (lhs_of (prepare_goal i st)));
   413 
   414 (*print conclusion of subgoal i*)
   415 fun pr_goal_concl i st =
   416     writeln (Sign.string_of_term (#sign(rep_thm st)) (prepare_goal i st)) 
   417 
   418 (*print subgoals i to j (inclusive)*)
   419 fun pr_goals (i,j) st =
   420     if i>j then ()
   421     else (pr_goal_concl i st;  pr_goals (i+1,j) st);
   422 
   423 (*Print rewrite for tracing; i=subgoal#, n=number of new subgoals,
   424   thm=old state, thm'=new state *)
   425 fun pr_rew (i,n,thm,thm',not_asms) =
   426     if !tracing
   427     then (if not_asms then () else writeln"Assumption used in";
   428           pr_goal_lhs i thm; writeln"->"; pr_goal_lhs (i+n) thm';
   429 	  if n>0 then (writeln"Conditions:"; pr_goals (i, i+n-1) thm')
   430           else ();
   431           writeln"" )
   432     else ();
   433 
   434 (* Skip the first n hyps of a goal, and return the rest in generalized form *)
   435 fun strip_varify(Const("==>", _) $ H $ B, n, vs) =
   436 	if n=0 then subst_bounds(vs,H)::strip_varify(B,0,vs)
   437 	else strip_varify(B,n-1,vs)
   438   | strip_varify(Const("all",_)$Abs(_,T,t), n, vs) =
   439 	strip_varify(t,n,Var(("?",length vs),T)::vs)
   440   | strip_varify  _  = [];
   441 
   442 fun execute(ss,if_fl,auto_tac,cong_tac,splits,split_consts,net,i) thm = let
   443 
   444 fun simp_lhs(thm,ss,anet,ats,cs) =
   445     if var_lhs(thm,i) then (ss,thm,anet,ats,cs) else
   446     if lhs_is_NORM(thm,i) then (ss, res1(thm,trans_norms,i), anet,ats,cs)
   447     else case Sequence.pull(tapply(cong_tac i,thm)) of
   448 	    Some(thm',_) =>
   449 		    let val ps = prems_of thm and ps' = prems_of thm';
   450 			val n = length(ps')-length(ps);
   451 			val a = length(strip_assums_hyp(nth_elem(i-1,ps)))
   452 			val l = map (fn p => length(strip_assums_hyp(p)))
   453 				    (take(n,drop(i-1,ps')));
   454 		    in (simp_refl(rev(l),a,REW::ss),thm',anet,ats,cs) end
   455 	  | None => (REW::ss,thm,anet,ats,cs);
   456 
   457 (*NB: the "Adding rewrites:" trace will look strange because assumptions
   458       are represented by rules, generalized over their parameters*)
   459 fun add_asms(ss,thm,a,anet,ats,cs) =
   460     let val As = strip_varify(nth_subgoal i thm, a, []);
   461 	val thms = map (trivial o Sign.cterm_of(#sign(rep_thm(thm))))As;
   462 	val new_rws = flat(map mk_rew_rules thms);
   463 	val rwrls = map mk_trans (flat(map mk_rew_rules thms));
   464 	val anet' = foldr lhs_insert_thm (rwrls,anet)
   465     in  if !tracing andalso not(null new_rws)
   466 	then (writeln"Adding rewrites:";  prths new_rws;  ())
   467 	else ();
   468 	(ss,thm,anet',anet::ats,cs) 
   469     end;
   470 
   471 fun rew(seq,thm,ss,anet,ats,cs, more) = case Sequence.pull seq of
   472       Some(thm',seq') =>
   473 	    let val n = (nprems_of thm') - (nprems_of thm)
   474 	    in pr_rew(i,n,thm,thm',more);
   475 	       if n=0 then (SIMP_LHS::ss, thm', anet, ats, cs)
   476 	       else ((replicate n PROVE) @ (POP_CS::SIMP_LHS::ss),
   477 		     thm', anet, ats, (ss,thm,anet,ats,seq',more)::cs)
   478 	    end
   479     | None => if more
   480 	    then rew(tapply(lhs_net_tac anet i THEN assume_tac i,thm),
   481 		     thm,ss,anet,ats,cs,false)
   482 	    else (ss,thm,anet,ats,cs);
   483 
   484 fun try_true(thm,ss,anet,ats,cs) =
   485     case Sequence.pull(tapply(auto_tac i,thm)) of
   486       Some(thm',_) => (ss,thm',anet,ats,cs)
   487     | None => let val (ss0,thm0,anet0,ats0,seq,more)::cs0 = cs
   488 	      in if !tracing
   489 		 then (writeln"*** Failed to prove precondition. Normal form:";
   490 		       pr_goal_concl i thm;  writeln"")
   491 		 else ();
   492 		 rew(seq,thm0,ss0,anet0,ats0,cs0,more)
   493 	      end;
   494 
   495 fun split(thm,ss,anet,ats,cs) =
   496 	case Sequence.pull(tapply(split_tac
   497                                   (cong_tac i,splits,split_consts) i,thm)) of
   498 		Some(thm',_) => (SIMP_LHS::SPLIT::ss,thm',anet,ats,cs)
   499 	      | None => (ss,thm,anet,ats,cs);
   500 
   501 fun step(s::ss, thm, anet, ats, cs) = case s of
   502 	  MK_EQ => (ss, res1(thm,[red2],i), anet, ats, cs)
   503 	| ASMS(a) => add_asms(ss,thm,a,anet,ats,cs)
   504 	| SIMP_LHS => simp_lhs(thm,ss,anet,ats,cs)
   505 	| REW => rew(tapply(net_tac net i,thm),thm,ss,anet,ats,cs,true)
   506 	| REFL => (ss, res1(thm,refl_thms,i), anet, ats, cs)
   507 	| TRUE => try_true(res1(thm,refl_thms,i),ss,anet,ats,cs)
   508 	| PROVE => (if if_fl then MK_EQ::SIMP_LHS::SPLIT::TRUE::ss
   509 		    else MK_EQ::SIMP_LHS::TRUE::ss, thm, anet, ats, cs)
   510 	| POP_ARTR => (ss,thm,hd ats,tl ats,cs)
   511 	| POP_CS => (ss,thm,anet,ats,tl cs)
   512 	| SPLIT => split(thm,ss,anet,ats,cs);
   513 
   514 fun exec(state as (s::ss, thm, _, _, _)) =
   515 	if s=STOP then thm else exec(step(state));
   516 
   517 in exec(ss, thm, Net.empty, [], []) end;
   518 
   519 
   520 (*ss = list of commands (not simpset!); 
   521   fl = even use case splits to solve conditional rewrite rules;
   522   addhyps = add hyps to simpset*)
   523 fun EXEC_TAC (ss,fl,addhyps) simpset = METAHYPS 
   524  (fn hyps => 
   525      case (if addhyps then simpset addrews hyps else simpset) of
   526          (SS{auto_tac,cong_net,simp_net,splits,split_consts,...}) =>
   527 	     PRIMITIVE(execute(ss,fl,auto_tac hyps,
   528 			       net_tac cong_net,splits,split_consts,
   529                                simp_net, 1))
   530 	     THEN TRY(auto_tac hyps 1));
   531 
   532 val SIMP_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,SPLIT,REFL,STOP],false,false);
   533 
   534 val ASM_SIMP_TAC = 
   535     EXEC_TAC([ASMS(0),MK_EQ,SIMP_LHS,SPLIT,REFL,STOP],false,true);
   536 
   537 val SIMP_SPLIT2_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,SPLIT,REFL,STOP],true,false);
   538 
   539 fun REWRITE (ss,fl) (SS{auto_tac,cong_net,simp_net,splits,split_consts,...}) =
   540 let val cong_tac = net_tac cong_net
   541 in fn thm =>
   542    let val state = thm RSN (2,red1)
   543    in execute(ss,fl,auto_tac[],cong_tac,splits,split_consts,simp_net,1)state
   544    end
   545 end;
   546 
   547 val SIMP_THM = REWRITE ([ASMS(0),SIMP_LHS,SPLIT,REFL,STOP],false);
   548 
   549 
   550 (* Compute Congruence rules for individual constants using the substition
   551    rules *)
   552 
   553 val subst_thms = map standard subst_thms;
   554 
   555 
   556 fun exp_app(0,t) = t
   557   | exp_app(i,t) = exp_app(i-1,t $ Bound (i-1));
   558 
   559 fun exp_abs(Type("fun",[T1,T2]),t,i) =
   560 	Abs("x"^string_of_int i,T1,exp_abs(T2,t,i+1))
   561   | exp_abs(T,t,i) = exp_app(i,t);
   562 
   563 fun eta_Var(ixn,T) = exp_abs(T,Var(ixn,T),0);
   564 
   565 
   566 fun Pinst(f,fT,(eq,eqT),k,i,T,yik,Ts) =
   567 let fun xn_list(x,n) =
   568 	let val ixs = map (fn i => (x^(radixstring(26,"a",i)),0)) (0 upto n);
   569 	in map eta_Var (ixs ~~ (take(n+1,Ts))) end
   570     val lhs = list_comb(f,xn_list("X",k-1))
   571     val rhs = list_comb(f,xn_list("X",i-1) @ [Bound 0] @ yik)
   572 in Abs("", T, Const(eq,[fT,fT]--->eqT) $ lhs $ rhs) end;
   573 
   574 fun find_subst tsig T =
   575 let fun find (thm::thms) =
   576 	let val (Const(_,cT), va, vb) =	dest_red(hd(prems_of thm));
   577 	    val [P] = term_vars(concl_of thm) \\ [va,vb]
   578 	    val eqT::_ = binder_types cT
   579         in if Type.typ_instance(tsig,T,eqT) then Some(thm,va,vb,P)
   580 	   else find thms
   581 	end
   582       | find [] = None
   583 in find subst_thms end;
   584 
   585 fun mk_cong sg (f,aTs,rT) (refl,eq) =
   586 let val tsig = #tsig(Sign.rep_sg sg);
   587     val k = length aTs;
   588     fun ri((subst,va as Var(_,Ta),vb as Var(_,Tb),P),i,si,T,yik) =
   589 	let val ca = Sign.cterm_of sg va
   590 	    and cx = Sign.cterm_of sg (eta_Var(("X"^si,0),T))
   591 	    val cb = Sign.cterm_of sg vb
   592 	    and cy = Sign.cterm_of sg (eta_Var(("Y"^si,0),T))
   593 	    val cP = Sign.cterm_of sg P
   594 	    and cp = Sign.cterm_of sg (Pinst(f,rT,eq,k,i,T,yik,aTs))
   595 	in cterm_instantiate [(ca,cx),(cb,cy),(cP,cp)] subst end;
   596     fun mk(c,T::Ts,i,yik) =
   597 	let val si = radixstring(26,"a",i)
   598 	in case find_subst tsig T of
   599 	     None => mk(c,Ts,i-1,eta_Var(("X"^si,0),T)::yik)
   600 	   | Some s => let val c' = c RSN (2,ri(s,i,si,T,yik))
   601 		       in mk(c',Ts,i-1,eta_Var(("Y"^si,0),T)::yik) end
   602 	end
   603       | mk(c,[],_,_) = c;
   604 in mk(refl,rev aTs,k-1,[]) end;
   605 
   606 fun mk_cong_type sg (f,T) =
   607 let val (aTs,rT) = strip_type T;
   608     val tsig = #tsig(Sign.rep_sg sg);
   609     fun find_refl(r::rs) =
   610 	let val (Const(eq,eqT),_,_) = dest_red(concl_of r)
   611 	in if Type.typ_instance(tsig, rT, hd(binder_types eqT))
   612 	   then Some(r,(eq,body_type eqT)) else find_refl rs
   613 	end
   614       | find_refl([]) = None;
   615 in case find_refl refl_thms of
   616      None => []  |  Some(refl) => [mk_cong sg (f,aTs,rT) refl]
   617 end;
   618 
   619 fun mk_cong_thy thy f =
   620 let val sg = sign_of thy;
   621     val T = case Sign.Symtab.lookup(#const_tab(Sign.rep_sg sg),f) of
   622 		None => error(f^" not declared") | Some(T) => T;
   623     val T' = incr_tvar 9 T;
   624 in mk_cong_type sg (Const(f,T'),T') end;
   625 
   626 fun mk_congs thy = filter_out is_fact o flat o map (mk_cong_thy thy);
   627 
   628 fun mk_typed_congs thy =
   629 let val sg = sign_of thy;
   630     val S0 = Type.defaultS(#tsig(Sign.rep_sg sg))
   631     fun readfT(f,s) =
   632 	let val T = incr_tvar 9 (Sign.read_typ(sg,K(Some(S0))) s);
   633 	    val t = case Sign.Symtab.lookup(#const_tab(Sign.rep_sg sg),f) of
   634 		      Some(_) => Const(f,T) | None => Free(f,T)
   635 	in (t,T) end
   636 in flat o map (mk_cong_type sg o readfT) end;
   637 
   638 (* This code is fishy, esp the "let val T' = ..." 
   639 fun extract_free_congs() =
   640 let val {prop,sign,...} = rep_thm(topthm());
   641     val frees = add_term_frees(prop,[]);
   642     fun filter(Free(a,T as Type("fun",_))) =
   643 	  let val T' = incr_tvar 9 (Type.varifyT T)
   644 	  in [(Free(a,T),T)] end
   645       | filter _ = []
   646 in flat(map (mk_cong_type sign) (flat (map filter frees))) end;
   647 *)
   648 
   649 end (* local *)
   650 end (* SIMP *);