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