src/Provers/hypsubst.ML
author wenzelm
Sat Oct 17 00:52:37 2009 +0200 (2009-10-17)
changeset 32957 675c0c7e6a37
parent 30515 bca05b17b618
child 35021 c839a4c670c6
permissions -rw-r--r--
explicitly qualify Drule.standard;
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)";
wenzelm@23908
    31
by (blast_hyp_subst_tac 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 *)
krauss@27572
    46
  val prop_subst       : thm               (* PROP P t ==> PROP prop (x = t ==> PROP P x) *)
wenzelm@21221
    47
end;
lcp@1011
    48
clasohm@0
    49
signature HYPSUBST =
wenzelm@21221
    50
sig
krauss@27572
    51
  val single_hyp_subst_tac   : int -> int -> tactic
krauss@27572
    52
  val single_hyp_meta_subst_tac : int -> int -> tactic
lcp@1011
    53
  val bound_hyp_subst_tac    : int -> tactic
lcp@1011
    54
  val hyp_subst_tac          : int -> tactic
wenzelm@23908
    55
  val blast_hyp_subst_tac    : bool -> int -> tactic
haftmann@20945
    56
  val stac                   : thm -> int -> tactic
wenzelm@18708
    57
  val hypsubst_setup         : theory -> theory
wenzelm@21221
    58
end;
paulson@2722
    59
wenzelm@9532
    60
functor HypsubstFun(Data: HYPSUBST_DATA): HYPSUBST =
clasohm@0
    61
struct
clasohm@0
    62
clasohm@0
    63
exception EQ_VAR;
clasohm@0
    64
wenzelm@27734
    65
val meta_subst = @{lemma "PROP P t \<Longrightarrow> PROP prop (x \<equiv> t \<Longrightarrow> PROP P x)"
krauss@27572
    66
  by (unfold prop_def)}
krauss@27572
    67
krauss@27572
    68
(** Simple version: Just subtitute one hypothesis, specified by index k **)
krauss@27572
    69
fun gen_single_hyp_subst_tac subst_rule k = CSUBGOAL (fn (csubg, i) =>
krauss@27572
    70
 let 
krauss@27572
    71
   val pat = fold_rev (Logic.all o Free) (Logic.strip_params (term_of csubg)) (Term.dummy_pattern propT)
krauss@27572
    72
             |> cterm_of (theory_of_cterm csubg)
krauss@27572
    73
krauss@27572
    74
   val rule =
krauss@27572
    75
       Thm.lift_rule pat subst_rule (* lift just over parameters *)
krauss@27572
    76
       |> Conv.fconv_rule (MetaSimplifier.rewrite true [@{thm prop_def}])
krauss@27572
    77
 in
krauss@27572
    78
   rotate_tac k i
krauss@27572
    79
   THEN Thm.compose_no_flatten false (rule, 1) i
krauss@27572
    80
   THEN rotate_tac (~k) i
krauss@27572
    81
 end)
krauss@27572
    82
krauss@27572
    83
val single_hyp_meta_subst_tac = gen_single_hyp_subst_tac meta_subst
krauss@27572
    84
val single_hyp_subst_tac = gen_single_hyp_subst_tac Data.prop_subst
krauss@27572
    85
wenzelm@17896
    86
fun loose (i,t) = member (op =) (add_loose_bnos (t, i, [])) 0;
clasohm@0
    87
wenzelm@16979
    88
(*Simplifier turns Bound variables to special Free variables:
wenzelm@16979
    89
  change it back (any Bound variable will do)*)
lcp@1011
    90
fun contract t =
berghofe@26833
    91
  (case Envir.eta_contract t of
wenzelm@20074
    92
    Free (a, T) => if Name.is_bound a then Bound 0 else Free (a, T)
wenzelm@16979
    93
  | t' => t');
lcp@1011
    94
wenzelm@21221
    95
val has_vars = Term.exists_subterm Term.is_Var;
wenzelm@21221
    96
val has_tvars = Term.exists_type (Term.exists_subtype Term.is_TVar);
lcp@1011
    97
lcp@1011
    98
(*If novars then we forbid Vars in the equality.
wenzelm@16979
    99
  If bnd then we only look for Bound variables to eliminate.
lcp@1011
   100
  When can we safely delete the equality?
lcp@1011
   101
    Not if it equates two constants; consider 0=1.
lcp@1011
   102
    Not if it resembles x=t[x], since substitution does not eliminate x.
paulson@4299
   103
    Not if it resembles ?x=0; consider ?x=0 ==> ?x=1 or even ?x=0 ==> P
wenzelm@9532
   104
    Not if it involves a variable free in the premises,
lcp@1011
   105
        but we can't check for this -- hence bnd and bound_hyp_subst_tac
lcp@1011
   106
  Prefer to eliminate Bound variables if possible.
lcp@1011
   107
  Result:  true = use as is,  false = reorient first *)
wenzelm@21221
   108
fun inspect_pair bnd novars (t, u) =
wenzelm@21221
   109
  if novars andalso (has_tvars t orelse has_tvars u)
paulson@4179
   110
  then raise Match   (*variables in the type!*)
paulson@4179
   111
  else
lcp@1011
   112
  case (contract t, contract u) of
wenzelm@9532
   113
       (Bound i, _) => if loose(i,u) orelse novars andalso has_vars u
wenzelm@9532
   114
                       then raise Match
wenzelm@9532
   115
                       else true                (*eliminates t*)
wenzelm@9532
   116
     | (_, Bound i) => if loose(i,t) orelse novars andalso has_vars t
wenzelm@9532
   117
                       then raise Match
wenzelm@9532
   118
                       else false               (*eliminates u*)
wenzelm@9532
   119
     | (Free _, _) =>  if bnd orelse Logic.occs(t,u) orelse
wenzelm@9532
   120
                          novars andalso has_vars u
wenzelm@9532
   121
                       then raise Match
wenzelm@9532
   122
                       else true                (*eliminates t*)
wenzelm@9532
   123
     | (_, Free _) =>  if bnd orelse Logic.occs(u,t) orelse
wenzelm@9532
   124
                          novars andalso has_vars t
wenzelm@9532
   125
                       then raise Match
wenzelm@9532
   126
                       else false               (*eliminates u*)
clasohm@0
   127
     | _ => raise Match;
clasohm@0
   128
lcp@680
   129
(*Locates a substitutable variable on the left (resp. right) of an equality
lcp@1011
   130
   assumption.  Returns the number of intervening assumptions. *)
lcp@1011
   131
fun eq_var bnd novars =
lcp@680
   132
  let fun eq_var_aux k (Const("all",_) $ Abs(_,_,t)) = eq_var_aux k t
wenzelm@9532
   133
        | eq_var_aux k (Const("==>",_) $ A $ B) =
wenzelm@9532
   134
              ((k, inspect_pair bnd novars
wenzelm@9532
   135
                    (Data.dest_eq (Data.dest_Trueprop A)))
wenzelm@21227
   136
               handle TERM _ => eq_var_aux (k+1) B
wenzelm@21227
   137
                 | Match => eq_var_aux (k+1) B)
wenzelm@9532
   138
        | eq_var_aux k _ = raise EQ_VAR
lcp@680
   139
  in  eq_var_aux 0  end;
clasohm@0
   140
lcp@1011
   141
(*For the simpset.  Adds ALL suitable equalities, even if not first!
lcp@1011
   142
  No vars are allowed here, as simpsets are built from meta-assumptions*)
paulson@15415
   143
fun mk_eqs bnd th =
paulson@15415
   144
    [ if inspect_pair bnd false (Data.dest_eq
wenzelm@9532
   145
                                   (Data.dest_Trueprop (#prop (rep_thm th))))
lcp@1011
   146
      then th RS Data.eq_reflection
wenzelm@9532
   147
      else symmetric(th RS Data.eq_reflection) (*reorient*) ]
wenzelm@21227
   148
    handle TERM _ => [] | Match => [];
lcp@1011
   149
wenzelm@17896
   150
local
lcp@1011
   151
in
lcp@1011
   152
paulson@15415
   153
  (*Select a suitable equality assumption; substitute throughout the subgoal
paulson@15415
   154
    If bnd is true, then it replaces Bound variables only. *)
berghofe@13604
   155
  fun gen_hyp_subst_tac bnd =
wenzelm@17896
   156
    let fun tac i st = SUBGOAL (fn (Bi, _) =>
wenzelm@17896
   157
      let
wenzelm@17896
   158
        val (k, _) = eq_var bnd true Bi
wenzelm@17896
   159
        val hyp_subst_ss = Simplifier.theory_context (Thm.theory_of_thm st) empty_ss
wenzelm@17896
   160
          setmksimps (mk_eqs bnd)
berghofe@13604
   161
      in EVERY [rotate_tac k i, asm_lr_simp_tac hyp_subst_ss i,
berghofe@13604
   162
        etac thin_rl i, rotate_tac (~k) i]
wenzelm@17896
   163
      end handle THM _ => no_tac | EQ_VAR => no_tac) i st
berghofe@13604
   164
    in REPEAT_DETERM1 o tac end;
lcp@1011
   165
lcp@1011
   166
end;
lcp@1011
   167
wenzelm@32957
   168
val ssubst = Drule.standard (Data.sym RS Data.subst);
paulson@4466
   169
wenzelm@26992
   170
fun inst_subst_tac b rl = CSUBGOAL (fn (cBi, i) =>
berghofe@26833
   171
  case try (Logic.strip_assums_hyp #> hd #>
wenzelm@26992
   172
      Data.dest_Trueprop #> Data.dest_eq #> pairself contract) (Thm.term_of cBi) of
berghofe@26833
   173
    SOME (t, t') =>
berghofe@26833
   174
      let
wenzelm@26992
   175
        val Bi = Thm.term_of cBi;
berghofe@26833
   176
        val ps = Logic.strip_params Bi;
wenzelm@26992
   177
        val U = Term.fastype_of1 (rev (map snd ps), t);
berghofe@26833
   178
        val Q = Data.dest_Trueprop (Logic.strip_assums_concl Bi);
wenzelm@26992
   179
        val rl' = Thm.lift_rule cBi rl;
wenzelm@26992
   180
        val Var (ixn, T) = Term.head_of (Data.dest_Trueprop
wenzelm@26992
   181
          (Logic.strip_assums_concl (Thm.prop_of rl')));
berghofe@26833
   182
        val (v1, v2) = Data.dest_eq (Data.dest_Trueprop
wenzelm@26992
   183
          (Logic.strip_assums_concl (hd (Thm.prems_of rl'))));
wenzelm@26992
   184
        val (Ts, V) = split_last (Term.binder_types T);
berghofe@26833
   185
        val u = list_abs (ps @ [("x", U)], case (if b then t else t') of
berghofe@26833
   186
            Bound j => subst_bounds (map Bound
berghofe@26833
   187
              ((1 upto j) @ 0 :: (j + 2 upto length ps)), Q)
wenzelm@26992
   188
          | t => Term.abstract_over (t, Term.incr_boundvars 1 Q));
wenzelm@26992
   189
        val thy = Thm.theory_of_thm rl';
wenzelm@26992
   190
        val (instT, _) = Thm.match (pairself (cterm_of thy o Logic.mk_type) (V, U));
wenzelm@26992
   191
      in compose_tac (true, Drule.instantiate (instT,
berghofe@26833
   192
        map (pairself (cterm_of thy))
berghofe@26833
   193
          [(Var (ixn, Ts ---> U --> body_type T), u),
berghofe@26833
   194
           (Var (fst (dest_Var (head_of v1)), Ts ---> U), list_abs (ps, t)),
berghofe@26833
   195
           (Var (fst (dest_Var (head_of v2)), Ts ---> U), list_abs (ps, t'))]) rl',
wenzelm@26992
   196
        nprems_of rl) i
berghofe@26833
   197
      end
wenzelm@26992
   198
  | NONE => no_tac);
berghofe@26833
   199
paulson@4466
   200
val imp_intr_tac = rtac Data.imp_intr;
lcp@1011
   201
berghofe@26833
   202
(* FIXME: "etac Data.rev_mp i" will not behave as expected if goal has *)
berghofe@26833
   203
(* premises containing meta-implications or quantifiers                *)
berghofe@26833
   204
lcp@1011
   205
(*Old version of the tactic above -- slower but the only way
lcp@1011
   206
  to handle equalities containing Vars.*)
paulson@3537
   207
fun vars_gen_hyp_subst_tac bnd = SUBGOAL(fn (Bi,i) =>
paulson@3537
   208
      let val n = length(Logic.strip_assums_hyp Bi) - 1
wenzelm@9532
   209
          val (k,symopt) = eq_var bnd false Bi
wenzelm@9532
   210
      in
wenzelm@9532
   211
         DETERM
paulson@4466
   212
           (EVERY [REPEAT_DETERM_N k (etac Data.rev_mp i),
wenzelm@9532
   213
                   rotate_tac 1 i,
wenzelm@9532
   214
                   REPEAT_DETERM_N (n-k) (etac Data.rev_mp i),
berghofe@26833
   215
                   inst_subst_tac symopt (if symopt then ssubst else Data.subst) i,
wenzelm@9532
   216
                   REPEAT_DETERM_N n (imp_intr_tac i THEN rotate_tac ~1 i)])
clasohm@0
   217
      end
paulson@3537
   218
      handle THM _ => no_tac | EQ_VAR => no_tac);
clasohm@0
   219
clasohm@0
   220
(*Substitutes for Free or Bound variables*)
paulson@4466
   221
val hyp_subst_tac = FIRST' [ematch_tac [Data.thin_refl],
oheimb@4223
   222
        gen_hyp_subst_tac false, vars_gen_hyp_subst_tac false];
clasohm@0
   223
clasohm@0
   224
(*Substitutes for Bound variables only -- this is always safe*)
wenzelm@9532
   225
val bound_hyp_subst_tac =
lcp@1011
   226
    gen_hyp_subst_tac true ORELSE' vars_gen_hyp_subst_tac true;
clasohm@0
   227
paulson@4466
   228
wenzelm@9532
   229
(** Version for Blast_tac.  Hyps that are affected by the substitution are
paulson@4466
   230
    moved to the front.  Defect: even trivial changes are noticed, such as
paulson@4466
   231
    substitutions in the arguments of a function Var. **)
paulson@4466
   232
paulson@4466
   233
(*final re-reversal of the changed assumptions*)
paulson@4466
   234
fun reverse_n_tac 0 i = all_tac
paulson@4466
   235
  | reverse_n_tac 1 i = rotate_tac ~1 i
wenzelm@9532
   236
  | reverse_n_tac n i =
paulson@4466
   237
      REPEAT_DETERM_N n (rotate_tac ~1 i THEN etac Data.rev_mp i) THEN
paulson@4466
   238
      REPEAT_DETERM_N n (imp_intr_tac i THEN rotate_tac ~1 i);
paulson@4466
   239
paulson@4466
   240
(*Use imp_intr, comparing the old hyps with the new ones as they come out.*)
wenzelm@9532
   241
fun all_imp_intr_tac hyps i =
paulson@4466
   242
  let fun imptac (r, [])    st = reverse_n_tac r i st
wenzelm@9532
   243
        | imptac (r, hyp::hyps) st =
wenzelm@9532
   244
           let val (hyp',_) = List.nth (prems_of st, i-1) |>
wenzelm@9532
   245
                              Logic.strip_assums_concl    |>
wenzelm@9532
   246
                              Data.dest_Trueprop          |> Data.dest_imp
wenzelm@9532
   247
               val (r',tac) = if Pattern.aeconv (hyp,hyp')
wenzelm@9532
   248
                              then (r, imp_intr_tac i THEN rotate_tac ~1 i)
wenzelm@9532
   249
                              else (*leave affected hyps at end*)
wenzelm@9532
   250
                                   (r+1, imp_intr_tac i)
wenzelm@9532
   251
           in
wenzelm@9532
   252
               case Seq.pull(tac st) of
skalberg@15531
   253
                   NONE       => Seq.single(st)
skalberg@15531
   254
                 | SOME(st',_) => imptac (r',hyps) st'
wenzelm@21221
   255
           end
paulson@4466
   256
  in  imptac (0, rev hyps)  end;
paulson@4466
   257
paulson@4466
   258
paulson@4466
   259
fun blast_hyp_subst_tac trace = SUBGOAL(fn (Bi,i) =>
paulson@4466
   260
      let val (k,symopt) = eq_var false false Bi
wenzelm@9532
   261
          val hyps0 = map Data.dest_Trueprop (Logic.strip_assums_hyp Bi)
paulson@4466
   262
          (*omit selected equality, returning other hyps*)
wenzelm@9532
   263
          val hyps = List.take(hyps0, k) @ List.drop(hyps0, k+1)
wenzelm@9532
   264
          val n = length hyps
wenzelm@9532
   265
      in
wenzelm@23908
   266
         if trace then tracing "Substituting an equality" else ();
wenzelm@9532
   267
         DETERM
paulson@4466
   268
           (EVERY [REPEAT_DETERM_N k (etac Data.rev_mp i),
wenzelm@9532
   269
                   rotate_tac 1 i,
wenzelm@9532
   270
                   REPEAT_DETERM_N (n-k) (etac Data.rev_mp i),
berghofe@26833
   271
                   inst_subst_tac symopt (if symopt then ssubst else Data.subst) i,
wenzelm@9532
   272
                   all_imp_intr_tac hyps i])
paulson@4466
   273
      end
paulson@4466
   274
      handle THM _ => no_tac | EQ_VAR => no_tac);
paulson@4466
   275
wenzelm@9532
   276
wenzelm@9532
   277
(*apply an equality or definition ONCE;
wenzelm@9532
   278
  fails unless the substitution has an effect*)
wenzelm@9532
   279
fun stac th =
wenzelm@9532
   280
  let val th' = th RS Data.rev_eq_reflection handle THM _ => th
wenzelm@9532
   281
  in CHANGED_GOAL (rtac (th' RS ssubst)) end;
wenzelm@9532
   282
wenzelm@9532
   283
wenzelm@9628
   284
(* theory setup *)
wenzelm@9628
   285
wenzelm@9532
   286
val hypsubst_setup =
wenzelm@30515
   287
  Method.setup @{binding hypsubst}
wenzelm@30515
   288
    (Scan.succeed (K (SIMPLE_METHOD' (CHANGED_PROP o hyp_subst_tac))))
wenzelm@30515
   289
    "substitution using an assumption (improper)" #>
wenzelm@30515
   290
  Method.setup @{binding simplesubst} (Attrib.thm >> (fn th => K (SIMPLE_METHOD' (stac th))))
wenzelm@30515
   291
    "simple substitution";
wenzelm@9532
   292
clasohm@0
   293
end;