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