src/Provers/hypsubst.ML
author wenzelm
Thu Dec 07 00:42:04 2006 +0100 (2006-12-07)
changeset 21687 f689f729afab
parent 21588 cd0dc678a205
child 23908 edca7f581c09
permissions -rw-r--r--
reorganized structure Goal vs. Tactic;
wenzelm@9532
     1
(*  Title:      Provers/hypsubst.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@9532
     3
    Authors:    Martin D Coen, Tobias Nipkow and Lawrence C Paulson
lcp@1011
     4
    Copyright   1995  University of Cambridge
lcp@1011
     5
wenzelm@15662
     6
Basic equational reasoning: hyp_subst_tac and methods "hypsubst", "subst".
wenzelm@9628
     7
wenzelm@9628
     8
Tactic to substitute using (at least) the assumption x=t in the rest
wenzelm@9628
     9
of the subgoal, and to delete (at least) that assumption.  Original
wenzelm@9628
    10
version due to Martin Coen.
lcp@1011
    11
lcp@1011
    12
This version uses the simplifier, and requires it to be already present.
lcp@1011
    13
lcp@1011
    14
Test data:
clasohm@0
    15
wenzelm@9532
    16
Goal "!!x.[| Q(x,y,z); y=x; a=x; z=y; P(y) |] ==> P(z)";
wenzelm@9532
    17
Goal "!!x.[| Q(x,y,z); z=f(x); x=z |] ==> P(z)";
wenzelm@9532
    18
Goal "!!y. [| ?x=y; P(?x) |] ==> y = a";
wenzelm@9532
    19
Goal "!!z. [| ?x=y; P(?x) |] ==> y = a";
lcp@1011
    20
paulson@15415
    21
Goal "!!x a. [| x = f(b); g(a) = b |] ==> P(x)";
paulson@15415
    22
paulson@15415
    23
by (bound_hyp_subst_tac 1);
lcp@1011
    24
by (hyp_subst_tac 1);
lcp@1011
    25
lcp@1011
    26
Here hyp_subst_tac goes wrong; harder still to prove P(f(f(a))) & P(f(a))
wenzelm@9532
    27
Goal "P(a) --> (EX y. a=y --> P(f(a)))";
paulson@4466
    28
wenzelm@9532
    29
Goal "!!x. [| Q(x,h1); P(a,h2); R(x,y,h3); R(y,z,h4); x=f(y); \
paulson@4466
    30
\                 P(x,h5); P(y,h6); K(x,h7) |] ==> Q(x,c)";
paulson@4466
    31
by (blast_hyp_subst_tac (ref true) 1);
clasohm@0
    32
*)
clasohm@0
    33
clasohm@0
    34
signature HYPSUBST_DATA =
wenzelm@21221
    35
sig
paulson@4466
    36
  val dest_Trueprop    : term -> term
wenzelm@21221
    37
  val dest_eq          : term -> term * term
haftmann@20974
    38
  val dest_imp         : term -> term * term
wenzelm@9532
    39
  val eq_reflection    : thm               (* a=b ==> a==b *)
wenzelm@9532
    40
  val rev_eq_reflection: thm               (* a==b ==> a=b *)
wenzelm@9532
    41
  val imp_intr         : thm               (* (P ==> Q) ==> P-->Q *)
wenzelm@9532
    42
  val rev_mp           : thm               (* [| P;  P-->Q |] ==> Q *)
wenzelm@9532
    43
  val subst            : thm               (* [| a=b;  P(a) |] ==> P(b) *)
wenzelm@9532
    44
  val sym              : thm               (* a=b ==> b=a *)
oheimb@4223
    45
  val thin_refl        : thm               (* [|x=x; PROP W|] ==> PROP W *)
wenzelm@21221
    46
end;
lcp@1011
    47
clasohm@0
    48
signature HYPSUBST =
wenzelm@21221
    49
sig
lcp@1011
    50
  val bound_hyp_subst_tac    : int -> tactic
lcp@1011
    51
  val hyp_subst_tac          : int -> tactic
paulson@4466
    52
  val blast_hyp_subst_tac    : bool ref -> int -> tactic
haftmann@20945
    53
  val stac                   : thm -> int -> tactic
wenzelm@18708
    54
  val hypsubst_setup         : theory -> theory
wenzelm@21221
    55
end;
paulson@2722
    56
wenzelm@9532
    57
functor HypsubstFun(Data: HYPSUBST_DATA): HYPSUBST =
clasohm@0
    58
struct
clasohm@0
    59
clasohm@0
    60
exception EQ_VAR;
clasohm@0
    61
wenzelm@17896
    62
fun loose (i,t) = member (op =) (add_loose_bnos (t, i, [])) 0;
clasohm@0
    63
wenzelm@16979
    64
(*Simplifier turns Bound variables to special Free variables:
wenzelm@16979
    65
  change it back (any Bound variable will do)*)
lcp@1011
    66
fun contract t =
wenzelm@16979
    67
  (case Pattern.eta_contract_atom t of
wenzelm@20074
    68
    Free (a, T) => if Name.is_bound a then Bound 0 else Free (a, T)
wenzelm@16979
    69
  | t' => t');
lcp@1011
    70
wenzelm@21221
    71
val has_vars = Term.exists_subterm Term.is_Var;
wenzelm@21221
    72
val has_tvars = Term.exists_type (Term.exists_subtype Term.is_TVar);
lcp@1011
    73
lcp@1011
    74
(*If novars then we forbid Vars in the equality.
wenzelm@16979
    75
  If bnd then we only look for Bound variables to eliminate.
lcp@1011
    76
  When can we safely delete the equality?
lcp@1011
    77
    Not if it equates two constants; consider 0=1.
lcp@1011
    78
    Not if it resembles x=t[x], since substitution does not eliminate x.
paulson@4299
    79
    Not if it resembles ?x=0; consider ?x=0 ==> ?x=1 or even ?x=0 ==> P
wenzelm@9532
    80
    Not if it involves a variable free in the premises,
lcp@1011
    81
        but we can't check for this -- hence bnd and bound_hyp_subst_tac
lcp@1011
    82
  Prefer to eliminate Bound variables if possible.
lcp@1011
    83
  Result:  true = use as is,  false = reorient first *)
wenzelm@21221
    84
fun inspect_pair bnd novars (t, u) =
wenzelm@21221
    85
  if novars andalso (has_tvars t orelse has_tvars u)
paulson@4179
    86
  then raise Match   (*variables in the type!*)
paulson@4179
    87
  else
lcp@1011
    88
  case (contract t, contract u) of
wenzelm@9532
    89
       (Bound i, _) => if loose(i,u) orelse novars andalso has_vars u
wenzelm@9532
    90
                       then raise Match
wenzelm@9532
    91
                       else true                (*eliminates t*)
wenzelm@9532
    92
     | (_, Bound i) => if loose(i,t) orelse novars andalso has_vars t
wenzelm@9532
    93
                       then raise Match
wenzelm@9532
    94
                       else false               (*eliminates u*)
wenzelm@9532
    95
     | (Free _, _) =>  if bnd orelse Logic.occs(t,u) orelse
wenzelm@9532
    96
                          novars andalso has_vars u
wenzelm@9532
    97
                       then raise Match
wenzelm@9532
    98
                       else true                (*eliminates t*)
wenzelm@9532
    99
     | (_, Free _) =>  if bnd orelse Logic.occs(u,t) orelse
wenzelm@9532
   100
                          novars andalso has_vars t
wenzelm@9532
   101
                       then raise Match
wenzelm@9532
   102
                       else false               (*eliminates u*)
clasohm@0
   103
     | _ => raise Match;
clasohm@0
   104
lcp@680
   105
(*Locates a substitutable variable on the left (resp. right) of an equality
lcp@1011
   106
   assumption.  Returns the number of intervening assumptions. *)
lcp@1011
   107
fun eq_var bnd novars =
lcp@680
   108
  let fun eq_var_aux k (Const("all",_) $ Abs(_,_,t)) = eq_var_aux k t
wenzelm@9532
   109
        | eq_var_aux k (Const("==>",_) $ A $ B) =
wenzelm@9532
   110
              ((k, inspect_pair bnd novars
wenzelm@9532
   111
                    (Data.dest_eq (Data.dest_Trueprop A)))
wenzelm@21227
   112
               handle TERM _ => eq_var_aux (k+1) B
wenzelm@21227
   113
                 | Match => eq_var_aux (k+1) B)
wenzelm@9532
   114
        | eq_var_aux k _ = raise EQ_VAR
lcp@680
   115
  in  eq_var_aux 0  end;
clasohm@0
   116
lcp@1011
   117
(*For the simpset.  Adds ALL suitable equalities, even if not first!
lcp@1011
   118
  No vars are allowed here, as simpsets are built from meta-assumptions*)
paulson@15415
   119
fun mk_eqs bnd th =
paulson@15415
   120
    [ if inspect_pair bnd false (Data.dest_eq
wenzelm@9532
   121
                                   (Data.dest_Trueprop (#prop (rep_thm th))))
lcp@1011
   122
      then th RS Data.eq_reflection
wenzelm@9532
   123
      else symmetric(th RS Data.eq_reflection) (*reorient*) ]
wenzelm@21227
   124
    handle TERM _ => [] | Match => [];
lcp@1011
   125
wenzelm@17896
   126
local
lcp@1011
   127
in
lcp@1011
   128
paulson@15415
   129
  (*Select a suitable equality assumption; substitute throughout the subgoal
paulson@15415
   130
    If bnd is true, then it replaces Bound variables only. *)
berghofe@13604
   131
  fun gen_hyp_subst_tac bnd =
wenzelm@17896
   132
    let fun tac i st = SUBGOAL (fn (Bi, _) =>
wenzelm@17896
   133
      let
wenzelm@17896
   134
        val (k, _) = eq_var bnd true Bi
wenzelm@17896
   135
        val hyp_subst_ss = Simplifier.theory_context (Thm.theory_of_thm st) empty_ss
wenzelm@17896
   136
          setmksimps (mk_eqs bnd)
berghofe@13604
   137
      in EVERY [rotate_tac k i, asm_lr_simp_tac hyp_subst_ss i,
berghofe@13604
   138
        etac thin_rl i, rotate_tac (~k) i]
wenzelm@17896
   139
      end handle THM _ => no_tac | EQ_VAR => no_tac) i st
berghofe@13604
   140
    in REPEAT_DETERM1 o tac end;
lcp@1011
   141
lcp@1011
   142
end;
lcp@1011
   143
paulson@4466
   144
val ssubst = standard (Data.sym RS Data.subst);
paulson@4466
   145
paulson@4466
   146
val imp_intr_tac = rtac Data.imp_intr;
lcp@1011
   147
lcp@1011
   148
(*Old version of the tactic above -- slower but the only way
lcp@1011
   149
  to handle equalities containing Vars.*)
paulson@3537
   150
fun vars_gen_hyp_subst_tac bnd = SUBGOAL(fn (Bi,i) =>
paulson@3537
   151
      let val n = length(Logic.strip_assums_hyp Bi) - 1
wenzelm@9532
   152
          val (k,symopt) = eq_var bnd false Bi
wenzelm@9532
   153
      in
wenzelm@9532
   154
         DETERM
paulson@4466
   155
           (EVERY [REPEAT_DETERM_N k (etac Data.rev_mp i),
wenzelm@9532
   156
                   rotate_tac 1 i,
wenzelm@9532
   157
                   REPEAT_DETERM_N (n-k) (etac Data.rev_mp i),
wenzelm@9532
   158
                   etac (if symopt then ssubst else Data.subst) i,
wenzelm@9532
   159
                   REPEAT_DETERM_N n (imp_intr_tac i THEN rotate_tac ~1 i)])
clasohm@0
   160
      end
paulson@3537
   161
      handle THM _ => no_tac | EQ_VAR => no_tac);
clasohm@0
   162
clasohm@0
   163
(*Substitutes for Free or Bound variables*)
paulson@4466
   164
val hyp_subst_tac = FIRST' [ematch_tac [Data.thin_refl],
oheimb@4223
   165
        gen_hyp_subst_tac false, vars_gen_hyp_subst_tac false];
clasohm@0
   166
clasohm@0
   167
(*Substitutes for Bound variables only -- this is always safe*)
wenzelm@9532
   168
val bound_hyp_subst_tac =
lcp@1011
   169
    gen_hyp_subst_tac true ORELSE' vars_gen_hyp_subst_tac true;
clasohm@0
   170
paulson@4466
   171
wenzelm@9532
   172
(** Version for Blast_tac.  Hyps that are affected by the substitution are
paulson@4466
   173
    moved to the front.  Defect: even trivial changes are noticed, such as
paulson@4466
   174
    substitutions in the arguments of a function Var. **)
paulson@4466
   175
paulson@4466
   176
(*final re-reversal of the changed assumptions*)
paulson@4466
   177
fun reverse_n_tac 0 i = all_tac
paulson@4466
   178
  | reverse_n_tac 1 i = rotate_tac ~1 i
wenzelm@9532
   179
  | reverse_n_tac n i =
paulson@4466
   180
      REPEAT_DETERM_N n (rotate_tac ~1 i THEN etac Data.rev_mp i) THEN
paulson@4466
   181
      REPEAT_DETERM_N n (imp_intr_tac i THEN rotate_tac ~1 i);
paulson@4466
   182
paulson@4466
   183
(*Use imp_intr, comparing the old hyps with the new ones as they come out.*)
wenzelm@9532
   184
fun all_imp_intr_tac hyps i =
paulson@4466
   185
  let fun imptac (r, [])    st = reverse_n_tac r i st
wenzelm@9532
   186
        | imptac (r, hyp::hyps) st =
wenzelm@9532
   187
           let val (hyp',_) = List.nth (prems_of st, i-1) |>
wenzelm@9532
   188
                              Logic.strip_assums_concl    |>
wenzelm@9532
   189
                              Data.dest_Trueprop          |> Data.dest_imp
wenzelm@9532
   190
               val (r',tac) = if Pattern.aeconv (hyp,hyp')
wenzelm@9532
   191
                              then (r, imp_intr_tac i THEN rotate_tac ~1 i)
wenzelm@9532
   192
                              else (*leave affected hyps at end*)
wenzelm@9532
   193
                                   (r+1, imp_intr_tac i)
wenzelm@9532
   194
           in
wenzelm@9532
   195
               case Seq.pull(tac st) of
skalberg@15531
   196
                   NONE       => Seq.single(st)
skalberg@15531
   197
                 | SOME(st',_) => imptac (r',hyps) st'
wenzelm@21221
   198
           end
paulson@4466
   199
  in  imptac (0, rev hyps)  end;
paulson@4466
   200
paulson@4466
   201
paulson@4466
   202
fun blast_hyp_subst_tac trace = SUBGOAL(fn (Bi,i) =>
paulson@4466
   203
      let val (k,symopt) = eq_var false false Bi
wenzelm@9532
   204
          val hyps0 = map Data.dest_Trueprop (Logic.strip_assums_hyp Bi)
paulson@4466
   205
          (*omit selected equality, returning other hyps*)
wenzelm@9532
   206
          val hyps = List.take(hyps0, k) @ List.drop(hyps0, k+1)
wenzelm@9532
   207
          val n = length hyps
wenzelm@9532
   208
      in
wenzelm@12262
   209
         if !trace then tracing "Substituting an equality" else ();
wenzelm@9532
   210
         DETERM
paulson@4466
   211
           (EVERY [REPEAT_DETERM_N k (etac Data.rev_mp i),
wenzelm@9532
   212
                   rotate_tac 1 i,
wenzelm@9532
   213
                   REPEAT_DETERM_N (n-k) (etac Data.rev_mp i),
wenzelm@9532
   214
                   etac (if symopt then ssubst else Data.subst) i,
wenzelm@9532
   215
                   all_imp_intr_tac hyps i])
paulson@4466
   216
      end
paulson@4466
   217
      handle THM _ => no_tac | EQ_VAR => no_tac);
paulson@4466
   218
wenzelm@9532
   219
wenzelm@9532
   220
(*apply an equality or definition ONCE;
wenzelm@9532
   221
  fails unless the substitution has an effect*)
wenzelm@9532
   222
fun stac th =
wenzelm@9532
   223
  let val th' = th RS Data.rev_eq_reflection handle THM _ => th
wenzelm@9532
   224
  in CHANGED_GOAL (rtac (th' RS ssubst)) end;
wenzelm@9532
   225
wenzelm@9532
   226
wenzelm@9628
   227
(* theory setup *)
wenzelm@9628
   228
wenzelm@9532
   229
val hypsubst_setup =
wenzelm@18708
   230
  Method.add_methods
wenzelm@21588
   231
    [("hypsubst", Method.no_args (Method.SIMPLE_METHOD' (CHANGED_PROP o hyp_subst_tac)),
wenzelm@21588
   232
        "substitution using an assumption (improper)"),
wenzelm@21588
   233
     ("simplesubst", Method.thm_args (Method.SIMPLE_METHOD' o stac), "simple substitution")];
wenzelm@9532
   234
clasohm@0
   235
end;