src/FOLP/hypsubst.ML
author lcp
Fri Apr 07 10:12:01 1995 +0200 (1995-04-07)
changeset 1022 c4921e635bf7
child 1459 d12da312eff4
permissions -rw-r--r--
Local version of (original) hypsubst: needs no simplifier
lcp@1022
     1
(*  Title: 	FOLP/hypsubst
lcp@1022
     2
    ID:         $Id$
lcp@1022
     3
    Author: 	Martin D Coen, Cambridge University Computer Laboratory
lcp@1022
     4
    Copyright   1995  University of Cambridge
lcp@1022
     5
lcp@1022
     6
Original version of Provers/hypsubst.  Cannot use new version because it
lcp@1022
     7
relies on the new simplifier!
lcp@1022
     8
lcp@1022
     9
Martin Coen's tactic for substitution in the hypotheses
lcp@1022
    10
*)
lcp@1022
    11
lcp@1022
    12
signature HYPSUBST_DATA =
lcp@1022
    13
  sig
lcp@1022
    14
  val dest_eq	: term -> term*term
lcp@1022
    15
  val imp_intr	: thm		(* (P ==> Q) ==> P-->Q *)
lcp@1022
    16
  val rev_mp	: thm		(* [| P;  P-->Q |] ==> Q *)
lcp@1022
    17
  val subst	: thm		(* [| a=b;  P(a) |] ==> P(b) *)
lcp@1022
    18
  val sym	: thm		(* a=b ==> b=a *)
lcp@1022
    19
  end;
lcp@1022
    20
lcp@1022
    21
signature HYPSUBST =
lcp@1022
    22
  sig
lcp@1022
    23
  val bound_hyp_subst_tac : int -> tactic
lcp@1022
    24
  val hyp_subst_tac       : int -> tactic
lcp@1022
    25
    (*exported purely for debugging purposes*)
lcp@1022
    26
  val eq_var              : bool -> term -> int * thm
lcp@1022
    27
  val inspect_pair        : bool -> term * term -> thm
lcp@1022
    28
  end;
lcp@1022
    29
lcp@1022
    30
functor HypsubstFun(Data: HYPSUBST_DATA): HYPSUBST = 
lcp@1022
    31
struct
lcp@1022
    32
lcp@1022
    33
local open Data in
lcp@1022
    34
lcp@1022
    35
exception EQ_VAR;
lcp@1022
    36
lcp@1022
    37
fun loose (i,t) = 0 mem add_loose_bnos(t,i,[]);
lcp@1022
    38
lcp@1022
    39
(*It's not safe to substitute for a constant; consider 0=1.
lcp@1022
    40
  It's not safe to substitute for x=t[x] since x is not eliminated.
lcp@1022
    41
  It's not safe to substitute for a Var; what if it appears in other goals?
lcp@1022
    42
  It's not safe to substitute for a variable free in the premises,
lcp@1022
    43
    but how could we check for this?*)
lcp@1022
    44
fun inspect_pair bnd (t,u) =
lcp@1022
    45
  case (Pattern.eta_contract t, Pattern.eta_contract u) of
lcp@1022
    46
       (Bound i, _) => if loose(i,u) then raise Match 
lcp@1022
    47
		       else sym		(*eliminates t*)
lcp@1022
    48
     | (_, Bound i) => if loose(i,t) then raise Match 
lcp@1022
    49
		       else asm_rl	(*eliminates u*)
lcp@1022
    50
     | (Free _, _) => if bnd orelse Logic.occs(t,u) then raise Match 
lcp@1022
    51
		      else sym		(*eliminates t*)
lcp@1022
    52
     | (_, Free _) => if bnd orelse Logic.occs(u,t) then raise Match 
lcp@1022
    53
		      else asm_rl	(*eliminates u*)
lcp@1022
    54
     | _ => raise Match;
lcp@1022
    55
lcp@1022
    56
(*Locates a substitutable variable on the left (resp. right) of an equality
lcp@1022
    57
   assumption.  Returns the number of intervening assumptions, paried with
lcp@1022
    58
   the rule asm_rl (resp. sym). *)
lcp@1022
    59
fun eq_var bnd =
lcp@1022
    60
  let fun eq_var_aux k (Const("all",_) $ Abs(_,_,t)) = eq_var_aux k t
lcp@1022
    61
	| eq_var_aux k (Const("==>",_) $ A $ B) = 
lcp@1022
    62
	      ((k, inspect_pair bnd (dest_eq A))
lcp@1022
    63
		      (*Exception Match comes from inspect_pair or dest_eq*)
lcp@1022
    64
	       handle Match => eq_var_aux (k+1) B)
lcp@1022
    65
	| eq_var_aux k _ = raise EQ_VAR
lcp@1022
    66
  in  eq_var_aux 0  end;
lcp@1022
    67
lcp@1022
    68
(*Select a suitable equality assumption and substitute throughout the subgoal
lcp@1022
    69
  Replaces only Bound variables if bnd is true*)
lcp@1022
    70
fun gen_hyp_subst_tac bnd i = DETERM (STATE(fn state =>
lcp@1022
    71
      let val (_,_,Bi,_) = dest_state(state,i)
lcp@1022
    72
	  val n = length(Logic.strip_assums_hyp Bi) - 1
lcp@1022
    73
	  val (k,symopt) = eq_var bnd Bi
lcp@1022
    74
      in 
lcp@1022
    75
	 EVERY [REPEAT_DETERM_N k (etac rev_mp i),
lcp@1022
    76
		etac revcut_rl i,
lcp@1022
    77
		REPEAT_DETERM_N (n-k) (etac rev_mp i),
lcp@1022
    78
		etac (symopt RS subst) i,
lcp@1022
    79
		REPEAT_DETERM_N n (rtac imp_intr i)]
lcp@1022
    80
      end
lcp@1022
    81
      handle THM _ => no_tac | EQ_VAR => no_tac));
lcp@1022
    82
lcp@1022
    83
(*Substitutes for Free or Bound variables*)
lcp@1022
    84
val hyp_subst_tac = gen_hyp_subst_tac false;
lcp@1022
    85
lcp@1022
    86
(*Substitutes for Bound variables only -- this is always safe*)
lcp@1022
    87
val bound_hyp_subst_tac = gen_hyp_subst_tac true;
lcp@1022
    88
lcp@1022
    89
end
lcp@1022
    90
end;
lcp@1022
    91