src/Provers/hypsubst.ML
changeset 0 a5a9c433f639
child 231 cb6a24451544
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Provers/hypsubst.ML	Thu Sep 16 12:20:38 1993 +0200
     1.3 @@ -0,0 +1,112 @@
     1.4 +(*  Title: 	Provers/hypsubst
     1.5 +    ID:         $Id$
     1.6 +    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
     1.7 +    Copyright   1993  University of Cambridge
     1.8 +
     1.9 +Martin Coen's tactic for substitution in the hypotheses
    1.10 +*)
    1.11 +
    1.12 +signature HYPSUBST_DATA =
    1.13 +  sig
    1.14 +  val dest_eq: term -> term*term
    1.15 +  val imp_intr: thm	(* (P ==> Q) ==> P-->Q *)
    1.16 +  val rev_cut_eq: thm	(* [| a=b;  a=b ==> R |] ==> R *)
    1.17 +  val rev_mp: thm	(* [| P;  P-->Q |] ==> Q *)
    1.18 +  val subst: thm	(* [| a=b;  P(a) |] ==> P(b) *)
    1.19 +  val sym: thm		(* a=b ==> b=a *)
    1.20 +  end;
    1.21 +
    1.22 +signature HYPSUBST =
    1.23 +  sig
    1.24 +  val bound_hyp_subst_tac : int -> tactic
    1.25 +  val hyp_subst_tac       : int -> tactic
    1.26 +    (*exported purely for debugging purposes*)
    1.27 +  val eq_var              : bool -> term -> term * thm
    1.28 +  val inspect_pair        : bool -> term * term -> term * thm
    1.29 +  val liftvar             : int -> typ list -> term
    1.30 +  end;
    1.31 +
    1.32 +functor HypsubstFun(Data: HYPSUBST_DATA): HYPSUBST = 
    1.33 +struct
    1.34 +
    1.35 +local open Data in
    1.36 +
    1.37 +fun REPEATN 0 tac = all_tac
    1.38 +  | REPEATN n tac = Tactic(fn state =>
    1.39 +                           tapply(tac THEN REPEATN (n-1) tac,  state));
    1.40 +
    1.41 +local
    1.42 +  val T = case #1 (types_sorts rev_cut_eq) ("a",0) of
    1.43 +	      Some T => T
    1.44 +   	    | None   => error"No such variable in rev_cut_eq"
    1.45 +in
    1.46 +  fun liftvar inc paramTs = Var(("a",inc), paramTs ---> incr_tvar inc T);
    1.47 +end;
    1.48 +
    1.49 +exception EQ_VAR;
    1.50 +
    1.51 +fun loose (i,t) = 0 mem add_loose_bnos(t,i,[]);
    1.52 +
    1.53 +(*It's not safe to substitute for a constant; consider 0=1.
    1.54 +  It's not safe to substitute for x=t[x] since x is not eliminated.
    1.55 +  It's not safe to substitute for a variable free in the premises,
    1.56 +    but how could we check for this?*)
    1.57 +fun inspect_pair bnd (t,u) =
    1.58 +  case (Pattern.eta_contract t, Pattern.eta_contract u) of
    1.59 +       (Bound i, _) => if loose(i,u) then raise Match 
    1.60 +		       else (t, asm_rl)
    1.61 +     | (_, Bound i) => if loose(i,t) then raise Match 
    1.62 +		       else (u, sym)
    1.63 +     | (Free _, _) => if bnd orelse Logic.occs(t,u) then raise Match 
    1.64 +		      else (t, asm_rl)
    1.65 +     | (_, Free _) => if bnd orelse Logic.occs(u,t) then raise Match 
    1.66 +		      else (u, sym) 
    1.67 +     | _ => raise Match;
    1.68 +
    1.69 + (* Extracts the name of the variable on the left (resp. right) of an equality
    1.70 +   assumption.  Returns the rule asm_rl (resp. sym). *)
    1.71 +fun eq_var bnd (Const("all",_) $ Abs(_,_,t)) = eq_var bnd t
    1.72 +  | eq_var bnd (Const("==>",_) $ A $ B) = 
    1.73 +	(inspect_pair bnd (dest_eq A) 
    1.74 +	        (*Match comes from inspect_pair or dest_eq*)
    1.75 +	 handle Match => eq_var bnd B)
    1.76 +  | eq_var bnd _ = raise EQ_VAR;
    1.77 +
    1.78 +(*Lift and instantiate a rule wrt the given state and subgoal number *)
    1.79 +fun lift_instpair (state, i, t, rule) =
    1.80 +  let val {maxidx,sign,...} = rep_thm state
    1.81 +      val (_, _, Bi, _) = dest_state(state,i)
    1.82 +      val params = Logic.strip_params Bi
    1.83 +      val var = liftvar (maxidx+1) (map #2 params)
    1.84 +      and u   = Unify.rlist_abs(rev params, t)
    1.85 +      and cterm = Sign.cterm_of sign
    1.86 +  in cterm_instantiate [(cterm var, cterm u)] (lift_rule (state,i) rule)
    1.87 +  end;
    1.88 +
    1.89 +fun eres_instpair_tac t rule i = STATE (fn state => 
    1.90 +	   compose_tac (true, lift_instpair (state, i, t, rule),
    1.91 +			length(prems_of rule)) i);
    1.92 +
    1.93 +val ssubst = sym RS subst;
    1.94 +
    1.95 +(*Select a suitable equality assumption and substitute throughout the subgoal
    1.96 +  Replaces only Bound variables if bnd is true*)
    1.97 +fun gen_hyp_subst_tac bnd i = DETERM (STATE(fn state =>
    1.98 +      let val (_,_,Bi,_) = dest_state(state,i)
    1.99 +	  val n = length(Logic.strip_assums_hyp Bi) - 1
   1.100 +	  val (t,symopt) = eq_var bnd Bi
   1.101 +      in eres_instpair_tac t (symopt RS rev_cut_eq) i  THEN
   1.102 +         REPEATN n (etac rev_mp i) THEN
   1.103 +	 etac ssubst i THEN REPEATN n (rtac imp_intr i)
   1.104 +      end
   1.105 +      handle THM _ => no_tac | EQ_VAR => no_tac));
   1.106 +
   1.107 +(*Substitutes for Free or Bound variables*)
   1.108 +val hyp_subst_tac = gen_hyp_subst_tac false;
   1.109 +
   1.110 +(*Substitutes for Bound variables only -- this is always safe*)
   1.111 +val bound_hyp_subst_tac = gen_hyp_subst_tac true;
   1.112 +
   1.113 +end
   1.114 +end;
   1.115 +