src/Provers/hypsubst.ML
author wenzelm
Fri May 24 17:00:46 2013 +0200 (2013-05-24)
changeset 52131 366fa32ee2a3
parent 51798 ad3a241def73
child 56245 84fc7dfa3cd4
permissions -rw-r--r--
tuned signature;
wenzelm@9532
     1
(*  Title:      Provers/hypsubst.ML
wenzelm@9532
     2
    Authors:    Martin D Coen, Tobias Nipkow and Lawrence C Paulson
lcp@1011
     3
    Copyright   1995  University of Cambridge
lcp@1011
     4
rafal@48107
     5
Basic equational reasoning: hyp_subst_tac and methods "hypsubst", "simplesubst".
wenzelm@9628
     6
wenzelm@9628
     7
Tactic to substitute using (at least) the assumption x=t in the rest
wenzelm@9628
     8
of the subgoal, and to delete (at least) that assumption.  Original
wenzelm@9628
     9
version due to Martin Coen.
lcp@1011
    10
lcp@1011
    11
This version uses the simplifier, and requires it to be already present.
lcp@1011
    12
lcp@1011
    13
Test data:
clasohm@0
    14
wenzelm@9532
    15
Goal "!!x.[| Q(x,y,z); y=x; a=x; z=y; P(y) |] ==> P(z)";
wenzelm@9532
    16
Goal "!!x.[| Q(x,y,z); z=f(x); x=z |] ==> P(z)";
wenzelm@9532
    17
Goal "!!y. [| ?x=y; P(?x) |] ==> y = a";
wenzelm@9532
    18
Goal "!!z. [| ?x=y; P(?x) |] ==> y = a";
lcp@1011
    19
paulson@15415
    20
Goal "!!x a. [| x = f(b); g(a) = b |] ==> P(x)";
paulson@15415
    21
paulson@15415
    22
by (bound_hyp_subst_tac 1);
lcp@1011
    23
by (hyp_subst_tac 1);
lcp@1011
    24
lcp@1011
    25
Here hyp_subst_tac goes wrong; harder still to prove P(f(f(a))) & P(f(a))
wenzelm@9532
    26
Goal "P(a) --> (EX y. a=y --> P(f(a)))";
paulson@4466
    27
wenzelm@9532
    28
Goal "!!x. [| Q(x,h1); P(a,h2); R(x,y,h3); R(y,z,h4); x=f(y); \
paulson@4466
    29
\                 P(x,h5); P(y,h6); K(x,h7) |] ==> Q(x,c)";
wenzelm@23908
    30
by (blast_hyp_subst_tac true 1);
clasohm@0
    31
*)
clasohm@0
    32
clasohm@0
    33
signature HYPSUBST_DATA =
wenzelm@21221
    34
sig
paulson@4466
    35
  val dest_Trueprop    : term -> term
wenzelm@21221
    36
  val dest_eq          : term -> term * term
haftmann@20974
    37
  val dest_imp         : term -> term * term
wenzelm@9532
    38
  val eq_reflection    : thm               (* a=b ==> a==b *)
wenzelm@9532
    39
  val rev_eq_reflection: thm               (* a==b ==> a=b *)
wenzelm@9532
    40
  val imp_intr         : thm               (* (P ==> Q) ==> P-->Q *)
wenzelm@9532
    41
  val rev_mp           : thm               (* [| P;  P-->Q |] ==> Q *)
wenzelm@9532
    42
  val subst            : thm               (* [| a=b;  P(a) |] ==> P(b) *)
wenzelm@9532
    43
  val sym              : thm               (* a=b ==> b=a *)
oheimb@4223
    44
  val thin_refl        : thm               (* [|x=x; PROP W|] ==> PROP W *)
wenzelm@21221
    45
end;
lcp@1011
    46
clasohm@0
    47
signature HYPSUBST =
wenzelm@21221
    48
sig
wenzelm@51798
    49
  val bound_hyp_subst_tac    : Proof.context -> int -> tactic
wenzelm@51798
    50
  val hyp_subst_tac          : Proof.context -> int -> tactic
wenzelm@23908
    51
  val blast_hyp_subst_tac    : bool -> int -> tactic
haftmann@20945
    52
  val stac                   : thm -> int -> tactic
wenzelm@18708
    53
  val hypsubst_setup         : theory -> theory
wenzelm@21221
    54
end;
paulson@2722
    55
wenzelm@42799
    56
functor Hypsubst(Data: HYPSUBST_DATA): HYPSUBST =
clasohm@0
    57
struct
clasohm@0
    58
clasohm@0
    59
exception EQ_VAR;
clasohm@0
    60
wenzelm@16979
    61
(*Simplifier turns Bound variables to special Free variables:
wenzelm@16979
    62
  change it back (any Bound variable will do)*)
lcp@1011
    63
fun contract t =
berghofe@26833
    64
  (case Envir.eta_contract t of
wenzelm@20074
    65
    Free (a, T) => if Name.is_bound a then Bound 0 else Free (a, T)
wenzelm@16979
    66
  | t' => t');
lcp@1011
    67
wenzelm@21221
    68
val has_vars = Term.exists_subterm Term.is_Var;
wenzelm@21221
    69
val has_tvars = Term.exists_type (Term.exists_subtype Term.is_TVar);
lcp@1011
    70
lcp@1011
    71
(*If novars then we forbid Vars in the equality.
wenzelm@16979
    72
  If bnd then we only look for Bound variables to eliminate.
lcp@1011
    73
  When can we safely delete the equality?
lcp@1011
    74
    Not if it equates two constants; consider 0=1.
lcp@1011
    75
    Not if it resembles x=t[x], since substitution does not eliminate x.
paulson@4299
    76
    Not if it resembles ?x=0; consider ?x=0 ==> ?x=1 or even ?x=0 ==> P
wenzelm@9532
    77
    Not if it involves a variable free in the premises,
lcp@1011
    78
        but we can't check for this -- hence bnd and bound_hyp_subst_tac
lcp@1011
    79
  Prefer to eliminate Bound variables if possible.
lcp@1011
    80
  Result:  true = use as is,  false = reorient first *)
wenzelm@21221
    81
fun inspect_pair bnd novars (t, u) =
wenzelm@21221
    82
  if novars andalso (has_tvars t orelse has_tvars u)
paulson@4179
    83
  then raise Match   (*variables in the type!*)
paulson@4179
    84
  else
wenzelm@42082
    85
    (case (contract t, contract u) of
wenzelm@42082
    86
      (Bound i, _) =>
wenzelm@42082
    87
        if loose_bvar1 (u, i) orelse novars andalso has_vars u
wenzelm@42082
    88
        then raise Match
wenzelm@42082
    89
        else true                (*eliminates t*)
wenzelm@42082
    90
    | (_, Bound i) =>
wenzelm@42082
    91
        if loose_bvar1 (t, i) orelse novars andalso has_vars t
wenzelm@42082
    92
        then raise Match
wenzelm@42082
    93
        else false               (*eliminates u*)
wenzelm@42082
    94
    | (t' as Free _, _) =>
wenzelm@42082
    95
        if bnd orelse Logic.occs (t', u) orelse novars andalso has_vars u
wenzelm@42082
    96
        then raise Match
wenzelm@42082
    97
        else true                (*eliminates t*)
wenzelm@42082
    98
    | (_, u' as Free _) =>
wenzelm@42082
    99
        if bnd orelse Logic.occs (u', t) orelse novars andalso has_vars t
wenzelm@42082
   100
        then raise Match
wenzelm@42082
   101
        else false               (*eliminates u*)
wenzelm@42082
   102
    | _ => raise Match);
clasohm@0
   103
lcp@680
   104
(*Locates a substitutable variable on the left (resp. right) of an equality
lcp@1011
   105
   assumption.  Returns the number of intervening assumptions. *)
lcp@1011
   106
fun eq_var bnd novars =
lcp@680
   107
  let fun eq_var_aux k (Const("all",_) $ Abs(_,_,t)) = eq_var_aux k t
wenzelm@9532
   108
        | eq_var_aux k (Const("==>",_) $ A $ B) =
wenzelm@9532
   109
              ((k, inspect_pair bnd novars
wenzelm@9532
   110
                    (Data.dest_eq (Data.dest_Trueprop A)))
wenzelm@21227
   111
               handle TERM _ => eq_var_aux (k+1) B
wenzelm@21227
   112
                 | Match => eq_var_aux (k+1) B)
wenzelm@9532
   113
        | eq_var_aux k _ = raise EQ_VAR
lcp@680
   114
  in  eq_var_aux 0  end;
clasohm@0
   115
lcp@1011
   116
(*For the simpset.  Adds ALL suitable equalities, even if not first!
lcp@1011
   117
  No vars are allowed here, as simpsets are built from meta-assumptions*)
paulson@15415
   118
fun mk_eqs bnd th =
wenzelm@44058
   119
    [ if inspect_pair bnd false (Data.dest_eq (Data.dest_Trueprop (Thm.prop_of th)))
lcp@1011
   120
      then th RS Data.eq_reflection
wenzelm@36945
   121
      else Thm.symmetric(th RS Data.eq_reflection) (*reorient*) ]
wenzelm@21227
   122
    handle TERM _ => [] | Match => [];
lcp@1011
   123
wenzelm@17896
   124
local
lcp@1011
   125
in
lcp@1011
   126
paulson@15415
   127
  (*Select a suitable equality assumption; substitute throughout the subgoal
paulson@15415
   128
    If bnd is true, then it replaces Bound variables only. *)
wenzelm@51798
   129
  fun gen_hyp_subst_tac ctxt bnd =
wenzelm@17896
   130
    let fun tac i st = SUBGOAL (fn (Bi, _) =>
wenzelm@17896
   131
      let
wenzelm@17896
   132
        val (k, _) = eq_var bnd true Bi
wenzelm@51717
   133
        val hyp_subst_ctxt = empty_simpset ctxt |> Simplifier.set_mksimps (K (mk_eqs bnd))
wenzelm@51717
   134
      in EVERY [rotate_tac k i, asm_lr_simp_tac hyp_subst_ctxt i,
berghofe@13604
   135
        etac thin_rl i, rotate_tac (~k) i]
wenzelm@17896
   136
      end handle THM _ => no_tac | EQ_VAR => no_tac) i st
berghofe@13604
   137
    in REPEAT_DETERM1 o tac end;
lcp@1011
   138
lcp@1011
   139
end;
lcp@1011
   140
wenzelm@45659
   141
val ssubst = Drule.zero_var_indexes (Data.sym RS Data.subst);
paulson@4466
   142
wenzelm@26992
   143
fun inst_subst_tac b rl = CSUBGOAL (fn (cBi, i) =>
berghofe@26833
   144
  case try (Logic.strip_assums_hyp #> hd #>
wenzelm@26992
   145
      Data.dest_Trueprop #> Data.dest_eq #> pairself contract) (Thm.term_of cBi) of
berghofe@26833
   146
    SOME (t, t') =>
berghofe@26833
   147
      let
wenzelm@26992
   148
        val Bi = Thm.term_of cBi;
berghofe@26833
   149
        val ps = Logic.strip_params Bi;
wenzelm@26992
   150
        val U = Term.fastype_of1 (rev (map snd ps), t);
berghofe@26833
   151
        val Q = Data.dest_Trueprop (Logic.strip_assums_concl Bi);
wenzelm@26992
   152
        val rl' = Thm.lift_rule cBi rl;
wenzelm@26992
   153
        val Var (ixn, T) = Term.head_of (Data.dest_Trueprop
wenzelm@26992
   154
          (Logic.strip_assums_concl (Thm.prop_of rl')));
berghofe@26833
   155
        val (v1, v2) = Data.dest_eq (Data.dest_Trueprop
wenzelm@26992
   156
          (Logic.strip_assums_concl (hd (Thm.prems_of rl'))));
wenzelm@26992
   157
        val (Ts, V) = split_last (Term.binder_types T);
wenzelm@46219
   158
        val u =
wenzelm@46219
   159
          fold_rev Term.abs (ps @ [("x", U)])
wenzelm@46219
   160
            (case (if b then t else t') of
wenzelm@46219
   161
              Bound j => subst_bounds (map Bound ((1 upto j) @ 0 :: (j + 2 upto length ps)), Q)
wenzelm@46219
   162
            | t => Term.abstract_over (t, Term.incr_boundvars 1 Q));
wenzelm@26992
   163
        val thy = Thm.theory_of_thm rl';
wenzelm@26992
   164
        val (instT, _) = Thm.match (pairself (cterm_of thy o Logic.mk_type) (V, U));
wenzelm@46219
   165
      in
wenzelm@46219
   166
        compose_tac (true, Drule.instantiate_normalize (instT,
wenzelm@46219
   167
          map (pairself (cterm_of thy))
wenzelm@46219
   168
            [(Var (ixn, Ts ---> U --> body_type T), u),
wenzelm@46219
   169
             (Var (fst (dest_Var (head_of v1)), Ts ---> U), fold_rev Term.abs ps t),
wenzelm@46219
   170
             (Var (fst (dest_Var (head_of v2)), Ts ---> U), fold_rev Term.abs ps t')]) rl',
wenzelm@46219
   171
          nprems_of rl) i
berghofe@26833
   172
      end
wenzelm@26992
   173
  | NONE => no_tac);
berghofe@26833
   174
paulson@4466
   175
val imp_intr_tac = rtac Data.imp_intr;
lcp@1011
   176
berghofe@26833
   177
(* FIXME: "etac Data.rev_mp i" will not behave as expected if goal has *)
berghofe@26833
   178
(* premises containing meta-implications or quantifiers                *)
berghofe@26833
   179
lcp@1011
   180
(*Old version of the tactic above -- slower but the only way
lcp@1011
   181
  to handle equalities containing Vars.*)
paulson@3537
   182
fun vars_gen_hyp_subst_tac bnd = SUBGOAL(fn (Bi,i) =>
paulson@3537
   183
      let val n = length(Logic.strip_assums_hyp Bi) - 1
wenzelm@9532
   184
          val (k,symopt) = eq_var bnd false Bi
wenzelm@9532
   185
      in
wenzelm@9532
   186
         DETERM
paulson@4466
   187
           (EVERY [REPEAT_DETERM_N k (etac Data.rev_mp i),
wenzelm@9532
   188
                   rotate_tac 1 i,
wenzelm@9532
   189
                   REPEAT_DETERM_N (n-k) (etac Data.rev_mp i),
berghofe@26833
   190
                   inst_subst_tac symopt (if symopt then ssubst else Data.subst) i,
wenzelm@9532
   191
                   REPEAT_DETERM_N n (imp_intr_tac i THEN rotate_tac ~1 i)])
clasohm@0
   192
      end
paulson@3537
   193
      handle THM _ => no_tac | EQ_VAR => no_tac);
clasohm@0
   194
clasohm@0
   195
(*Substitutes for Free or Bound variables*)
wenzelm@51798
   196
fun hyp_subst_tac ctxt =
wenzelm@51798
   197
  FIRST' [ematch_tac [Data.thin_refl],
wenzelm@51798
   198
    gen_hyp_subst_tac ctxt false, vars_gen_hyp_subst_tac false];
clasohm@0
   199
clasohm@0
   200
(*Substitutes for Bound variables only -- this is always safe*)
wenzelm@51798
   201
fun bound_hyp_subst_tac ctxt =
wenzelm@51798
   202
  gen_hyp_subst_tac ctxt true ORELSE' vars_gen_hyp_subst_tac true;
clasohm@0
   203
paulson@4466
   204
wenzelm@9532
   205
(** Version for Blast_tac.  Hyps that are affected by the substitution are
paulson@4466
   206
    moved to the front.  Defect: even trivial changes are noticed, such as
paulson@4466
   207
    substitutions in the arguments of a function Var. **)
paulson@4466
   208
paulson@4466
   209
(*final re-reversal of the changed assumptions*)
paulson@4466
   210
fun reverse_n_tac 0 i = all_tac
paulson@4466
   211
  | reverse_n_tac 1 i = rotate_tac ~1 i
wenzelm@9532
   212
  | reverse_n_tac n i =
paulson@4466
   213
      REPEAT_DETERM_N n (rotate_tac ~1 i THEN etac Data.rev_mp i) THEN
paulson@4466
   214
      REPEAT_DETERM_N n (imp_intr_tac i THEN rotate_tac ~1 i);
paulson@4466
   215
paulson@4466
   216
(*Use imp_intr, comparing the old hyps with the new ones as they come out.*)
wenzelm@9532
   217
fun all_imp_intr_tac hyps i =
wenzelm@42364
   218
  let
wenzelm@42364
   219
    fun imptac (r, []) st = reverse_n_tac r i st
wenzelm@42364
   220
      | imptac (r, hyp::hyps) st =
wenzelm@42364
   221
          let
wenzelm@42364
   222
            val (hyp', _) =
wenzelm@42366
   223
              term_of (Thm.cprem_of st i)
wenzelm@42364
   224
              |> Logic.strip_assums_concl
wenzelm@42364
   225
              |> Data.dest_Trueprop |> Data.dest_imp;
wenzelm@42364
   226
            val (r', tac) =
wenzelm@52131
   227
              if Envir.aeconv (hyp, hyp')
wenzelm@42364
   228
              then (r, imp_intr_tac i THEN rotate_tac ~1 i)
wenzelm@42364
   229
              else (*leave affected hyps at end*) (r + 1, imp_intr_tac i);
wenzelm@42364
   230
          in
wenzelm@42364
   231
            (case Seq.pull (tac st) of
wenzelm@42364
   232
              NONE => Seq.single st
wenzelm@42364
   233
            | SOME (st', _) => imptac (r', hyps) st')
wenzelm@42364
   234
          end
wenzelm@42364
   235
  in imptac (0, rev hyps) end;
paulson@4466
   236
paulson@4466
   237
paulson@4466
   238
fun blast_hyp_subst_tac trace = SUBGOAL(fn (Bi,i) =>
paulson@4466
   239
      let val (k,symopt) = eq_var false false Bi
wenzelm@9532
   240
          val hyps0 = map Data.dest_Trueprop (Logic.strip_assums_hyp Bi)
paulson@4466
   241
          (*omit selected equality, returning other hyps*)
wenzelm@9532
   242
          val hyps = List.take(hyps0, k) @ List.drop(hyps0, k+1)
wenzelm@9532
   243
          val n = length hyps
wenzelm@9532
   244
      in
wenzelm@23908
   245
         if trace then tracing "Substituting an equality" else ();
wenzelm@9532
   246
         DETERM
paulson@4466
   247
           (EVERY [REPEAT_DETERM_N k (etac Data.rev_mp i),
wenzelm@9532
   248
                   rotate_tac 1 i,
wenzelm@9532
   249
                   REPEAT_DETERM_N (n-k) (etac Data.rev_mp i),
berghofe@26833
   250
                   inst_subst_tac symopt (if symopt then ssubst else Data.subst) i,
wenzelm@9532
   251
                   all_imp_intr_tac hyps i])
paulson@4466
   252
      end
paulson@4466
   253
      handle THM _ => no_tac | EQ_VAR => no_tac);
paulson@4466
   254
wenzelm@9532
   255
wenzelm@9532
   256
(*apply an equality or definition ONCE;
wenzelm@9532
   257
  fails unless the substitution has an effect*)
wenzelm@9532
   258
fun stac th =
wenzelm@9532
   259
  let val th' = th RS Data.rev_eq_reflection handle THM _ => th
wenzelm@9532
   260
  in CHANGED_GOAL (rtac (th' RS ssubst)) end;
wenzelm@9532
   261
wenzelm@9532
   262
wenzelm@9628
   263
(* theory setup *)
wenzelm@9628
   264
wenzelm@9532
   265
val hypsubst_setup =
wenzelm@30515
   266
  Method.setup @{binding hypsubst}
wenzelm@51798
   267
    (Scan.succeed (fn ctxt => SIMPLE_METHOD' (CHANGED_PROP o hyp_subst_tac ctxt)))
wenzelm@30515
   268
    "substitution using an assumption (improper)" #>
wenzelm@30515
   269
  Method.setup @{binding simplesubst} (Attrib.thm >> (fn th => K (SIMPLE_METHOD' (stac th))))
wenzelm@30515
   270
    "simple substitution";
wenzelm@9532
   271
clasohm@0
   272
end;