src/Provers/hypsubst.ML
author wenzelm
Thu Jan 19 21:22:08 2006 +0100 (2006-01-19 ago)
changeset 18708 4b3dadb4fe33
parent 17896 66902148c436
child 20074 b4d0b545df01
permissions -rw-r--r--
setup: theory -> theory;
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 =
clasohm@0
    35
  sig
paulson@4466
    36
  val dest_Trueprop    : term -> term
wenzelm@9532
    37
  val dest_eq          : term -> term*term*typ
wenzelm@9532
    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 *)
paulson@4466
    46
  end;
clasohm@0
    47
lcp@1011
    48
clasohm@0
    49
signature HYPSUBST =
clasohm@0
    50
  sig
lcp@1011
    51
  val bound_hyp_subst_tac    : int -> tactic
lcp@1011
    52
  val hyp_subst_tac          : int -> tactic
paulson@4466
    53
  val blast_hyp_subst_tac    : bool ref -> int -> tactic
clasohm@0
    54
    (*exported purely for debugging purposes*)
lcp@1011
    55
  val gen_hyp_subst_tac      : bool -> int -> tactic
lcp@1011
    56
  val vars_gen_hyp_subst_tac : bool -> int -> tactic
lcp@1011
    57
  val eq_var                 : bool -> bool -> term -> int * bool
paulson@4179
    58
  val inspect_pair           : bool -> bool -> term * term * typ -> bool
paulson@15415
    59
  val mk_eqs                 : bool -> thm -> thm list
wenzelm@9532
    60
  val stac		     : thm -> int -> tactic
wenzelm@18708
    61
  val hypsubst_setup         : theory -> theory
clasohm@0
    62
  end;
clasohm@0
    63
paulson@2722
    64
paulson@2722
    65
wenzelm@9532
    66
functor HypsubstFun(Data: HYPSUBST_DATA): HYPSUBST =
clasohm@0
    67
struct
clasohm@0
    68
clasohm@0
    69
exception EQ_VAR;
clasohm@0
    70
wenzelm@17896
    71
fun loose (i,t) = member (op =) (add_loose_bnos (t, i, [])) 0;
clasohm@0
    72
wenzelm@16979
    73
(*Simplifier turns Bound variables to special Free variables:
wenzelm@16979
    74
  change it back (any Bound variable will do)*)
lcp@1011
    75
fun contract t =
wenzelm@16979
    76
  (case Pattern.eta_contract_atom t of
wenzelm@16979
    77
    Free (a, T) => if Term.is_bound a then Bound 0 else Free (a, T)
wenzelm@16979
    78
  | t' => t');
lcp@1011
    79
lcp@1011
    80
fun has_vars t = maxidx_of_term t <> ~1;
lcp@1011
    81
lcp@1011
    82
(*If novars then we forbid Vars in the equality.
wenzelm@16979
    83
  If bnd then we only look for Bound variables to eliminate.
lcp@1011
    84
  When can we safely delete the equality?
lcp@1011
    85
    Not if it equates two constants; consider 0=1.
lcp@1011
    86
    Not if it resembles x=t[x], since substitution does not eliminate x.
paulson@4299
    87
    Not if it resembles ?x=0; consider ?x=0 ==> ?x=1 or even ?x=0 ==> P
wenzelm@9532
    88
    Not if it involves a variable free in the premises,
lcp@1011
    89
        but we can't check for this -- hence bnd and bound_hyp_subst_tac
lcp@1011
    90
  Prefer to eliminate Bound variables if possible.
lcp@1011
    91
  Result:  true = use as is,  false = reorient first *)
paulson@4179
    92
fun inspect_pair bnd novars (t,u,T) =
wenzelm@9532
    93
  if novars andalso maxidx_of_typ T <> ~1
paulson@4179
    94
  then raise Match   (*variables in the type!*)
paulson@4179
    95
  else
lcp@1011
    96
  case (contract t, contract u) of
wenzelm@9532
    97
       (Bound i, _) => if loose(i,u) orelse novars andalso has_vars u
wenzelm@9532
    98
                       then raise Match
wenzelm@9532
    99
                       else true                (*eliminates t*)
wenzelm@9532
   100
     | (_, Bound i) => if loose(i,t) orelse novars andalso has_vars t
wenzelm@9532
   101
                       then raise Match
wenzelm@9532
   102
                       else false               (*eliminates u*)
wenzelm@9532
   103
     | (Free _, _) =>  if bnd orelse Logic.occs(t,u) orelse
wenzelm@9532
   104
                          novars andalso has_vars u
wenzelm@9532
   105
                       then raise Match
wenzelm@9532
   106
                       else true                (*eliminates t*)
wenzelm@9532
   107
     | (_, Free _) =>  if bnd orelse Logic.occs(u,t) orelse
wenzelm@9532
   108
                          novars andalso has_vars t
wenzelm@9532
   109
                       then raise Match
wenzelm@9532
   110
                       else false               (*eliminates u*)
clasohm@0
   111
     | _ => raise Match;
clasohm@0
   112
lcp@680
   113
(*Locates a substitutable variable on the left (resp. right) of an equality
lcp@1011
   114
   assumption.  Returns the number of intervening assumptions. *)
lcp@1011
   115
fun eq_var bnd novars =
lcp@680
   116
  let fun eq_var_aux k (Const("all",_) $ Abs(_,_,t)) = eq_var_aux k t
wenzelm@9532
   117
        | eq_var_aux k (Const("==>",_) $ A $ B) =
wenzelm@9532
   118
              ((k, inspect_pair bnd novars
wenzelm@9532
   119
                    (Data.dest_eq (Data.dest_Trueprop A)))
wenzelm@9532
   120
                      (*Exception comes from inspect_pair or dest_eq*)
wenzelm@9532
   121
               handle _ => eq_var_aux (k+1) B)
wenzelm@9532
   122
        | eq_var_aux k _ = raise EQ_VAR
lcp@680
   123
  in  eq_var_aux 0  end;
clasohm@0
   124
lcp@1011
   125
(*For the simpset.  Adds ALL suitable equalities, even if not first!
lcp@1011
   126
  No vars are allowed here, as simpsets are built from meta-assumptions*)
paulson@15415
   127
fun mk_eqs bnd th =
paulson@15415
   128
    [ if inspect_pair bnd false (Data.dest_eq
wenzelm@9532
   129
                                   (Data.dest_Trueprop (#prop (rep_thm th))))
lcp@1011
   130
      then th RS Data.eq_reflection
wenzelm@9532
   131
      else symmetric(th RS Data.eq_reflection) (*reorient*) ]
paulson@4466
   132
    handle _ => [];  (*Exception comes from inspect_pair or dest_eq*)
lcp@1011
   133
wenzelm@17896
   134
local
lcp@1011
   135
in
lcp@1011
   136
paulson@15415
   137
  (*Select a suitable equality assumption; substitute throughout the subgoal
paulson@15415
   138
    If bnd is true, then it replaces Bound variables only. *)
berghofe@13604
   139
  fun gen_hyp_subst_tac bnd =
wenzelm@17896
   140
    let fun tac i st = SUBGOAL (fn (Bi, _) =>
wenzelm@17896
   141
      let
wenzelm@17896
   142
        val (k, _) = eq_var bnd true Bi
wenzelm@17896
   143
        val hyp_subst_ss = Simplifier.theory_context (Thm.theory_of_thm st) empty_ss
wenzelm@17896
   144
          setmksimps (mk_eqs bnd)
berghofe@13604
   145
      in EVERY [rotate_tac k i, asm_lr_simp_tac hyp_subst_ss i,
berghofe@13604
   146
        etac thin_rl i, rotate_tac (~k) i]
wenzelm@17896
   147
      end handle THM _ => no_tac | EQ_VAR => no_tac) i st
berghofe@13604
   148
    in REPEAT_DETERM1 o tac end;
lcp@1011
   149
lcp@1011
   150
end;
lcp@1011
   151
paulson@4466
   152
val ssubst = standard (Data.sym RS Data.subst);
paulson@4466
   153
paulson@4466
   154
val imp_intr_tac = rtac Data.imp_intr;
lcp@1011
   155
lcp@1011
   156
(*Old version of the tactic above -- slower but the only way
lcp@1011
   157
  to handle equalities containing Vars.*)
paulson@3537
   158
fun vars_gen_hyp_subst_tac bnd = SUBGOAL(fn (Bi,i) =>
paulson@3537
   159
      let val n = length(Logic.strip_assums_hyp Bi) - 1
wenzelm@9532
   160
          val (k,symopt) = eq_var bnd false Bi
wenzelm@9532
   161
      in
wenzelm@9532
   162
         DETERM
paulson@4466
   163
           (EVERY [REPEAT_DETERM_N k (etac Data.rev_mp i),
wenzelm@9532
   164
                   rotate_tac 1 i,
wenzelm@9532
   165
                   REPEAT_DETERM_N (n-k) (etac Data.rev_mp i),
wenzelm@9532
   166
                   etac (if symopt then ssubst else Data.subst) i,
wenzelm@9532
   167
                   REPEAT_DETERM_N n (imp_intr_tac i THEN rotate_tac ~1 i)])
clasohm@0
   168
      end
paulson@3537
   169
      handle THM _ => no_tac | EQ_VAR => no_tac);
clasohm@0
   170
clasohm@0
   171
(*Substitutes for Free or Bound variables*)
paulson@4466
   172
val hyp_subst_tac = FIRST' [ematch_tac [Data.thin_refl],
oheimb@4223
   173
        gen_hyp_subst_tac false, vars_gen_hyp_subst_tac false];
clasohm@0
   174
clasohm@0
   175
(*Substitutes for Bound variables only -- this is always safe*)
wenzelm@9532
   176
val bound_hyp_subst_tac =
lcp@1011
   177
    gen_hyp_subst_tac true ORELSE' vars_gen_hyp_subst_tac true;
clasohm@0
   178
paulson@4466
   179
wenzelm@9532
   180
(** Version for Blast_tac.  Hyps that are affected by the substitution are
paulson@4466
   181
    moved to the front.  Defect: even trivial changes are noticed, such as
paulson@4466
   182
    substitutions in the arguments of a function Var. **)
paulson@4466
   183
paulson@4466
   184
(*final re-reversal of the changed assumptions*)
paulson@4466
   185
fun reverse_n_tac 0 i = all_tac
paulson@4466
   186
  | reverse_n_tac 1 i = rotate_tac ~1 i
wenzelm@9532
   187
  | reverse_n_tac n i =
paulson@4466
   188
      REPEAT_DETERM_N n (rotate_tac ~1 i THEN etac Data.rev_mp i) THEN
paulson@4466
   189
      REPEAT_DETERM_N n (imp_intr_tac i THEN rotate_tac ~1 i);
paulson@4466
   190
paulson@4466
   191
(*Use imp_intr, comparing the old hyps with the new ones as they come out.*)
wenzelm@9532
   192
fun all_imp_intr_tac hyps i =
paulson@4466
   193
  let fun imptac (r, [])    st = reverse_n_tac r i st
wenzelm@9532
   194
        | imptac (r, hyp::hyps) st =
wenzelm@9532
   195
           let val (hyp',_) = List.nth (prems_of st, i-1) |>
wenzelm@9532
   196
                              Logic.strip_assums_concl    |>
wenzelm@9532
   197
                              Data.dest_Trueprop          |> Data.dest_imp
wenzelm@9532
   198
               val (r',tac) = if Pattern.aeconv (hyp,hyp')
wenzelm@9532
   199
                              then (r, imp_intr_tac i THEN rotate_tac ~1 i)
wenzelm@9532
   200
                              else (*leave affected hyps at end*)
wenzelm@9532
   201
                                   (r+1, imp_intr_tac i)
wenzelm@9532
   202
           in
wenzelm@9532
   203
               case Seq.pull(tac st) of
skalberg@15531
   204
                   NONE       => Seq.single(st)
skalberg@15531
   205
                 | SOME(st',_) => imptac (r',hyps) st'
wenzelm@9532
   206
           end handle _ => error "?? in blast_hyp_subst_tac"
paulson@4466
   207
  in  imptac (0, rev hyps)  end;
paulson@4466
   208
paulson@4466
   209
paulson@4466
   210
fun blast_hyp_subst_tac trace = SUBGOAL(fn (Bi,i) =>
paulson@4466
   211
      let val (k,symopt) = eq_var false false Bi
wenzelm@9532
   212
          val hyps0 = map Data.dest_Trueprop (Logic.strip_assums_hyp Bi)
paulson@4466
   213
          (*omit selected equality, returning other hyps*)
wenzelm@9532
   214
          val hyps = List.take(hyps0, k) @ List.drop(hyps0, k+1)
wenzelm@9532
   215
          val n = length hyps
wenzelm@9532
   216
      in
wenzelm@12262
   217
         if !trace then tracing "Substituting an equality" else ();
wenzelm@9532
   218
         DETERM
paulson@4466
   219
           (EVERY [REPEAT_DETERM_N k (etac Data.rev_mp i),
wenzelm@9532
   220
                   rotate_tac 1 i,
wenzelm@9532
   221
                   REPEAT_DETERM_N (n-k) (etac Data.rev_mp i),
wenzelm@9532
   222
                   etac (if symopt then ssubst else Data.subst) i,
wenzelm@9532
   223
                   all_imp_intr_tac hyps i])
paulson@4466
   224
      end
paulson@4466
   225
      handle THM _ => no_tac | EQ_VAR => no_tac);
paulson@4466
   226
wenzelm@9532
   227
wenzelm@9532
   228
(*apply an equality or definition ONCE;
wenzelm@9532
   229
  fails unless the substitution has an effect*)
wenzelm@9532
   230
fun stac th =
wenzelm@9532
   231
  let val th' = th RS Data.rev_eq_reflection handle THM _ => th
wenzelm@9532
   232
  in CHANGED_GOAL (rtac (th' RS ssubst)) end;
wenzelm@9532
   233
wenzelm@9532
   234
wenzelm@9628
   235
(* proof methods *)
wenzelm@9532
   236
wenzelm@9705
   237
val subst_meth = Method.thm_args (Method.SIMPLE_METHOD' HEADGOAL o stac);
wenzelm@10821
   238
val hyp_subst_meth =
wenzelm@10821
   239
  Method.no_args (Method.SIMPLE_METHOD' HEADGOAL (CHANGED_PROP o hyp_subst_tac));
wenzelm@9532
   240
wenzelm@9628
   241
wenzelm@9628
   242
(* theory setup *)
wenzelm@9628
   243
wenzelm@9532
   244
val hypsubst_setup =
wenzelm@18708
   245
  Method.add_methods
wenzelm@18708
   246
    [("hypsubst", hyp_subst_meth, "substitution using an assumption (improper)"),
wenzelm@18708
   247
     ("simplesubst", subst_meth, "simple substitution")];
wenzelm@9532
   248
clasohm@0
   249
end;