src/FOLP/hypsubst.ML
author hoelzl
Thu, 09 Jun 2011 14:24:34 +0200
changeset 43342 2929f96d3ae7
parent 42799 4e33894aec6d
child 56245 84fc7dfa3cd4
permissions -rw-r--r--
merged
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37744
3daaf23b9ab4 tuned titles
haftmann
parents: 36692
diff changeset
     1
(*  Title:      FOLP/hypsubst.ML
1459
d12da312eff4 expanded tabs
clasohm
parents: 1022
diff changeset
     2
    Author:     Martin D Coen, Cambridge University Computer Laboratory
1022
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
     3
    Copyright   1995  University of Cambridge
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
     4
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
     5
Original version of Provers/hypsubst.  Cannot use new version because it
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
     6
relies on the new simplifier!
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
     7
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
     8
Martin Coen's tactic for substitution in the hypotheses
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
     9
*)
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
signature HYPSUBST_DATA =
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    12
  sig
1459
d12da312eff4 expanded tabs
clasohm
parents: 1022
diff changeset
    13
  val dest_eq   : term -> term*term
d12da312eff4 expanded tabs
clasohm
parents: 1022
diff changeset
    14
  val imp_intr  : thm           (* (P ==> Q) ==> P-->Q *)
d12da312eff4 expanded tabs
clasohm
parents: 1022
diff changeset
    15
  val rev_mp    : thm           (* [| P;  P-->Q |] ==> Q *)
d12da312eff4 expanded tabs
clasohm
parents: 1022
diff changeset
    16
  val subst     : thm           (* [| a=b;  P(a) |] ==> P(b) *)
d12da312eff4 expanded tabs
clasohm
parents: 1022
diff changeset
    17
  val sym       : thm           (* a=b ==> b=a *)
1022
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    18
  end;
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    19
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    20
signature HYPSUBST =
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    21
  sig
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    22
  val bound_hyp_subst_tac : int -> tactic
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    23
  val hyp_subst_tac       : int -> tactic
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    24
    (*exported purely for debugging purposes*)
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    25
  val eq_var              : bool -> term -> int * thm
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    26
  val inspect_pair        : bool -> term * term -> thm
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    27
  end;
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    28
42799
4e33894aec6d modernized functor names;
wenzelm
parents: 42125
diff changeset
    29
functor Hypsubst(Data: HYPSUBST_DATA): HYPSUBST =
1022
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    30
struct
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    31
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    32
local open Data in
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    33
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    34
exception EQ_VAR;
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    35
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    36
(*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
    37
  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
    38
  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
    39
  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
    40
    but how could we check for this?*)
42125
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    41
fun inspect_pair bnd (t, u) =
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    42
  (case (Envir.eta_contract t, Envir.eta_contract u) of
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    43
    (Bound i, _) =>
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    44
      if loose_bvar1 (u, i) then raise Match
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    45
      else sym         (*eliminates t*)
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    46
   | (_, Bound i) =>
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    47
      if loose_bvar (t, i) then raise Match
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    48
      else asm_rl      (*eliminates u*)
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    49
   | (Free _, _) =>
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    50
      if bnd orelse Logic.occs (t, u) then raise Match
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    51
      else sym          (*eliminates t*)
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    52
   | (_, Free _) =>
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    53
      if bnd orelse Logic.occs(u,t) then raise Match
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    54
      else asm_rl       (*eliminates u*)
a8cbb9371154 more direct loose_bvar1;
wenzelm
parents: 37744
diff changeset
    55
   | _ => raise Match);
1022
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    56
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    57
(*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
    58
   assumption.  Returns the number of intervening assumptions, paried with
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    59
   the rule asm_rl (resp. sym). *)
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    60
fun eq_var bnd =
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    61
  let fun eq_var_aux k (Const("all",_) $ Abs(_,_,t)) = eq_var_aux k t
32449
696d64ed85da eliminated hard tabs;
wenzelm
parents: 26830
diff changeset
    62
        | eq_var_aux k (Const("==>",_) $ A $ B) =
1459
d12da312eff4 expanded tabs
clasohm
parents: 1022
diff changeset
    63
              ((k, inspect_pair bnd (dest_eq A))
d12da312eff4 expanded tabs
clasohm
parents: 1022
diff changeset
    64
                      (*Exception Match comes from inspect_pair or dest_eq*)
d12da312eff4 expanded tabs
clasohm
parents: 1022
diff changeset
    65
               handle Match => eq_var_aux (k+1) B)
d12da312eff4 expanded tabs
clasohm
parents: 1022
diff changeset
    66
        | eq_var_aux k _ = raise EQ_VAR
1022
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    67
  in  eq_var_aux 0  end;
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    68
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    69
(*Select a suitable equality assumption and substitute throughout the subgoal
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    70
  Replaces only Bound variables if bnd is true*)
3537
79ac9b475621 Removal of the tactical STATE
paulson
parents: 2728
diff changeset
    71
fun gen_hyp_subst_tac bnd = SUBGOAL(fn (Bi,i) =>
79ac9b475621 Removal of the tactical STATE
paulson
parents: 2728
diff changeset
    72
      let val n = length(Logic.strip_assums_hyp Bi) - 1
1459
d12da312eff4 expanded tabs
clasohm
parents: 1022
diff changeset
    73
          val (k,symopt) = eq_var bnd Bi
32449
696d64ed85da eliminated hard tabs;
wenzelm
parents: 26830
diff changeset
    74
      in
3537
79ac9b475621 Removal of the tactical STATE
paulson
parents: 2728
diff changeset
    75
         DETERM
79ac9b475621 Removal of the tactical STATE
paulson
parents: 2728
diff changeset
    76
           (EVERY [REPEAT_DETERM_N k (etac rev_mp i),
32449
696d64ed85da eliminated hard tabs;
wenzelm
parents: 26830
diff changeset
    77
                   etac revcut_rl i,
696d64ed85da eliminated hard tabs;
wenzelm
parents: 26830
diff changeset
    78
                   REPEAT_DETERM_N (n-k) (etac rev_mp i),
696d64ed85da eliminated hard tabs;
wenzelm
parents: 26830
diff changeset
    79
                   etac (symopt RS subst) i,
696d64ed85da eliminated hard tabs;
wenzelm
parents: 26830
diff changeset
    80
                   REPEAT_DETERM_N n (rtac imp_intr i)])
1022
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    81
      end
3537
79ac9b475621 Removal of the tactical STATE
paulson
parents: 2728
diff changeset
    82
      handle THM _ => no_tac | EQ_VAR => no_tac);
1022
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    83
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    84
(*Substitutes for Free or Bound variables*)
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    85
val hyp_subst_tac = gen_hyp_subst_tac false;
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    86
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    87
(*Substitutes for Bound variables only -- this is always safe*)
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    88
val bound_hyp_subst_tac = gen_hyp_subst_tac true;
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    89
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
end;
c4921e635bf7 Local version of (original) hypsubst: needs no simplifier
lcp
parents:
diff changeset
    92