src/Pure/Isar/rule_insts.ML
author wenzelm
Sun Mar 01 23:36:12 2009 +0100 (2009-03-01)
changeset 30190 479806475f3c
parent 29606 fedb8be05f24
child 30510 4120fc59dd85
permissions -rw-r--r--
use long names for old-style fold combinators;
wenzelm@20336
     1
(*  Title:      Pure/Isar/rule_insts.ML
wenzelm@20336
     2
    Author:     Makarius
wenzelm@20336
     3
wenzelm@20336
     4
Rule instantiations -- operations within a rule/subgoal context.
wenzelm@20336
     5
*)
wenzelm@20336
     6
wenzelm@27245
     7
signature BASIC_RULE_INSTS =
wenzelm@20336
     8
sig
wenzelm@27236
     9
  val read_instantiate: Proof.context -> (indexname * string) list -> thm -> thm
wenzelm@27245
    10
  val instantiate_tac: Proof.context -> (indexname * string) list -> tactic
wenzelm@27120
    11
  val res_inst_tac: Proof.context -> (indexname * string) list -> thm -> int -> tactic
wenzelm@27120
    12
  val eres_inst_tac: Proof.context -> (indexname * string) list -> thm -> int -> tactic
wenzelm@27245
    13
  val cut_inst_tac: Proof.context -> (indexname * string) list -> thm -> int -> tactic
wenzelm@27245
    14
  val forw_inst_tac: Proof.context -> (indexname * string) list -> thm -> int -> tactic
wenzelm@27245
    15
  val dres_inst_tac: Proof.context -> (indexname * string) list -> thm -> int -> tactic
wenzelm@27245
    16
  val thin_tac: Proof.context -> string -> int -> tactic
wenzelm@27219
    17
  val subgoal_tac: Proof.context -> string -> int -> tactic
wenzelm@27219
    18
  val subgoals_tac: Proof.context -> string list -> int -> tactic
wenzelm@27245
    19
end;
wenzelm@27245
    20
wenzelm@27245
    21
signature RULE_INSTS =
wenzelm@27245
    22
sig
wenzelm@27245
    23
  include BASIC_RULE_INSTS
wenzelm@27245
    24
  val make_elim_preserve: thm -> thm
wenzelm@20336
    25
end;
wenzelm@20336
    26
wenzelm@20336
    27
structure RuleInsts: RULE_INSTS =
wenzelm@20336
    28
struct
wenzelm@20336
    29
wenzelm@27809
    30
structure T = OuterLex;
wenzelm@27809
    31
structure P = OuterParse;
wenzelm@27809
    32
wenzelm@20336
    33
wenzelm@20343
    34
(** reading instantiations **)
wenzelm@20336
    35
wenzelm@20336
    36
local
wenzelm@20336
    37
wenzelm@20343
    38
fun is_tvar (x, _) = String.isPrefix "'" x;
wenzelm@20336
    39
wenzelm@22681
    40
fun error_var msg xi = error (msg ^ Term.string_of_vname xi);
wenzelm@20336
    41
wenzelm@20343
    42
fun the_sort tvars xi = the (AList.lookup (op =) tvars xi)
wenzelm@20336
    43
  handle Option.Option => error_var "No such type variable in theorem: " xi;
wenzelm@20336
    44
wenzelm@20343
    45
fun the_type vars xi = the (AList.lookup (op =) vars xi)
wenzelm@20336
    46
  handle Option.Option => error_var "No such variable in theorem: " xi;
wenzelm@20336
    47
wenzelm@20343
    48
fun unify_vartypes thy vars (xi, u) (unifier, maxidx) =
wenzelm@20336
    49
  let
wenzelm@20343
    50
    val T = the_type vars xi;
wenzelm@20336
    51
    val U = Term.fastype_of u;
wenzelm@20343
    52
    val maxidx' = Term.maxidx_term u (Int.max (#2 xi, maxidx));
wenzelm@20336
    53
  in
wenzelm@20336
    54
    Sign.typ_unify thy (T, U) (unifier, maxidx')
wenzelm@20336
    55
      handle Type.TUNIFY => error_var "Incompatible type for instantiation of " xi
wenzelm@20336
    56
  end;
wenzelm@20336
    57
wenzelm@20343
    58
fun instantiate inst =
wenzelm@20509
    59
  TermSubst.instantiate ([], map (fn (xi, t) => ((xi, Term.fastype_of t), t)) inst) #>
wenzelm@20343
    60
  Envir.beta_norm;
wenzelm@20336
    61
wenzelm@20343
    62
fun make_instT f v =
wenzelm@20336
    63
  let
wenzelm@20343
    64
    val T = TVar v;
wenzelm@20343
    65
    val T' = f T;
wenzelm@20343
    66
  in if T = T' then NONE else SOME (T, T') end;
wenzelm@20343
    67
wenzelm@20343
    68
fun make_inst f v =
wenzelm@20343
    69
  let
wenzelm@20343
    70
    val t = Var v;
wenzelm@20343
    71
    val t' = f t;
wenzelm@20343
    72
  in if t aconv t' then NONE else SOME (t, t') end;
wenzelm@20336
    73
wenzelm@27282
    74
val add_used =
wenzelm@27282
    75
  (Thm.fold_terms o fold_types o fold_atyps)
wenzelm@27282
    76
    (fn TFree (a, _) => insert (op =) a
wenzelm@27282
    77
      | TVar ((a, _), _) => insert (op =) a
wenzelm@27282
    78
      | _ => I);
wenzelm@27282
    79
wenzelm@20336
    80
in
wenzelm@20336
    81
wenzelm@25333
    82
fun read_termTs ctxt schematic ss Ts =
wenzelm@25329
    83
  let
wenzelm@25329
    84
    fun parse T = if T = propT then Syntax.parse_prop ctxt else Syntax.parse_term ctxt;
wenzelm@25329
    85
    val ts = map2 parse Ts ss;
wenzelm@25329
    86
    val ts' =
wenzelm@25329
    87
      map2 (TypeInfer.constrain o TypeInfer.paramify_vars) Ts ts
wenzelm@25333
    88
      |> Syntax.check_terms ((schematic ? ProofContext.set_mode ProofContext.mode_schematic) ctxt)
wenzelm@25329
    89
      |> Variable.polymorphic ctxt;
wenzelm@25329
    90
    val Ts' = map Term.fastype_of ts';
wenzelm@25329
    91
    val tyenv = fold Type.raw_match (Ts ~~ Ts') Vartab.empty;
wenzelm@25329
    92
  in (ts', map (apsnd snd) (Vartab.dest tyenv)) end;
wenzelm@25329
    93
wenzelm@20343
    94
fun read_insts ctxt mixed_insts (tvars, vars) =
wenzelm@20336
    95
  let
wenzelm@20343
    96
    val thy = ProofContext.theory_of ctxt;
wenzelm@20343
    97
    val cert = Thm.cterm_of thy;
wenzelm@20343
    98
    val certT = Thm.ctyp_of thy;
wenzelm@20336
    99
wenzelm@20343
   100
    val (type_insts, term_insts) = List.partition (is_tvar o fst) mixed_insts;
wenzelm@20336
   101
    val internal_insts = term_insts |> map_filter
wenzelm@27809
   102
      (fn (xi, T.Term t) => SOME (xi, t)
wenzelm@27809
   103
        | (_, T.Text _) => NONE
wenzelm@20343
   104
        | (xi, _) => error_var "Term argument expected for " xi);
wenzelm@20336
   105
    val external_insts = term_insts |> map_filter
wenzelm@27809
   106
      (fn (xi, T.Text s) => SOME (xi, s) | _ => NONE);
wenzelm@20336
   107
wenzelm@20336
   108
wenzelm@20343
   109
    (* mixed type instantiations *)
wenzelm@20336
   110
wenzelm@20336
   111
    fun readT (xi, arg) =
wenzelm@20336
   112
      let
wenzelm@20343
   113
        val S = the_sort tvars xi;
wenzelm@20336
   114
        val T =
wenzelm@20336
   115
          (case arg of
wenzelm@27809
   116
            T.Text s => Syntax.read_typ ctxt s
wenzelm@27809
   117
          | T.Typ T => T
wenzelm@20336
   118
          | _ => error_var "Type argument expected for " xi);
wenzelm@20336
   119
      in
wenzelm@20343
   120
        if Sign.of_sort thy (T, S) then ((xi, S), T)
wenzelm@20336
   121
        else error_var "Incompatible sort for typ instantiation of " xi
wenzelm@20336
   122
      end;
wenzelm@20336
   123
wenzelm@20343
   124
    val type_insts1 = map readT type_insts;
wenzelm@20509
   125
    val instT1 = TermSubst.instantiateT type_insts1;
wenzelm@20343
   126
    val vars1 = map (apsnd instT1) vars;
wenzelm@20336
   127
wenzelm@20336
   128
wenzelm@20336
   129
    (* internal term instantiations *)
wenzelm@20336
   130
wenzelm@20343
   131
    val instT2 = Envir.norm_type
wenzelm@20343
   132
      (#1 (fold (unify_vartypes thy vars1) internal_insts (Vartab.empty, 0)));
wenzelm@20343
   133
    val vars2 = map (apsnd instT2) vars1;
wenzelm@20548
   134
    val internal_insts2 = map (apsnd (map_types instT2)) internal_insts;
wenzelm@20343
   135
    val inst2 = instantiate internal_insts2;
wenzelm@20336
   136
wenzelm@20336
   137
wenzelm@20336
   138
    (* external term instantiations *)
wenzelm@20336
   139
wenzelm@20343
   140
    val (xs, strs) = split_list external_insts;
wenzelm@20343
   141
    val Ts = map (the_type vars2) xs;
wenzelm@25354
   142
    val (ts, inferred) = read_termTs ctxt false strs Ts;
wenzelm@20336
   143
wenzelm@20343
   144
    val instT3 = Term.typ_subst_TVars inferred;
wenzelm@20343
   145
    val vars3 = map (apsnd instT3) vars2;
wenzelm@20548
   146
    val internal_insts3 = map (apsnd (map_types instT3)) internal_insts2;
wenzelm@20343
   147
    val external_insts3 = xs ~~ ts;
wenzelm@20343
   148
    val inst3 = instantiate external_insts3;
wenzelm@20336
   149
wenzelm@20336
   150
wenzelm@20343
   151
    (* results *)
wenzelm@20343
   152
wenzelm@20343
   153
    val type_insts3 = map (fn ((a, _), T) => (a, instT3 (instT2 T))) type_insts1;
wenzelm@20343
   154
    val term_insts3 = internal_insts3 @ external_insts3;
wenzelm@20336
   155
wenzelm@20343
   156
    val inst_tvars = map_filter (make_instT (instT3 o instT2 o instT1)) tvars;
wenzelm@20343
   157
    val inst_vars = map_filter (make_inst (inst3 o inst2)) vars3;
wenzelm@20343
   158
  in
wenzelm@20343
   159
    ((type_insts3, term_insts3),
wenzelm@20343
   160
      (map (pairself certT) inst_tvars, map (pairself cert) inst_vars))
wenzelm@20343
   161
  end;
wenzelm@20343
   162
wenzelm@27236
   163
fun read_instantiate_mixed ctxt mixed_insts thm =
wenzelm@20343
   164
  let
wenzelm@20487
   165
    val ctxt' = ctxt |> Variable.declare_thm thm
wenzelm@27282
   166
      |> fold (fn a => Variable.declare_names (Logic.mk_type (TFree (a, dummyS)))) (add_used thm []);  (* FIXME tmp *)
wenzelm@22692
   167
    val tvars = Thm.fold_terms Term.add_tvars thm [];
wenzelm@22692
   168
    val vars = Thm.fold_terms Term.add_vars thm [];
wenzelm@20343
   169
    val ((type_insts, term_insts), insts) = read_insts ctxt' (map snd mixed_insts) (tvars, vars);
wenzelm@20343
   170
wenzelm@20343
   171
    val _ = (*assign internalized values*)
wenzelm@20336
   172
      mixed_insts |> List.app (fn (arg, (xi, _)) =>
wenzelm@20336
   173
        if is_tvar xi then
wenzelm@27809
   174
          T.assign (SOME (T.Typ (the (AList.lookup (op =) type_insts xi)))) arg
wenzelm@20336
   175
        else
wenzelm@27809
   176
          T.assign (SOME (T.Term (the (AList.lookup (op =) term_insts xi)))) arg);
wenzelm@20343
   177
  in
wenzelm@20343
   178
    Drule.instantiate insts thm |> RuleCases.save thm
wenzelm@20343
   179
  end;
wenzelm@20336
   180
wenzelm@27236
   181
fun read_instantiate_mixed' ctxt (args, concl_args) thm =
wenzelm@20343
   182
  let
wenzelm@20343
   183
    fun zip_vars _ [] = []
wenzelm@20343
   184
      | zip_vars (_ :: xs) ((_, NONE) :: rest) = zip_vars xs rest
wenzelm@20343
   185
      | zip_vars ((x, _) :: xs) ((arg, SOME t) :: rest) = (arg, (x, t)) :: zip_vars xs rest
wenzelm@20343
   186
      | zip_vars [] _ = error "More instantiations than variables in theorem";
wenzelm@20343
   187
    val insts =
wenzelm@20343
   188
      zip_vars (rev (Term.add_vars (Thm.full_prop_of thm) [])) args @
wenzelm@20343
   189
      zip_vars (rev (Term.add_vars (Thm.concl_of thm) [])) concl_args;
wenzelm@27236
   190
  in read_instantiate_mixed ctxt insts thm end;
wenzelm@27236
   191
wenzelm@27245
   192
end;
wenzelm@27245
   193
wenzelm@27245
   194
wenzelm@27245
   195
(* instantiation of rule or goal state *)
wenzelm@27245
   196
wenzelm@27236
   197
fun read_instantiate ctxt args thm =
wenzelm@27236
   198
  read_instantiate_mixed (ctxt |> ProofContext.set_mode ProofContext.mode_schematic)  (* FIXME !? *)
wenzelm@27809
   199
    (map (fn (x, y) => (T.eof, (x, T.Text y))) args) thm;
wenzelm@20336
   200
wenzelm@27245
   201
fun instantiate_tac ctxt args = PRIMITIVE (read_instantiate ctxt args);
wenzelm@20336
   202
wenzelm@20336
   203
wenzelm@20343
   204
wenzelm@20343
   205
(** attributes **)
wenzelm@20343
   206
wenzelm@20336
   207
(* where: named instantiation *)
wenzelm@20336
   208
wenzelm@20336
   209
local
wenzelm@20336
   210
wenzelm@20336
   211
val value =
wenzelm@27809
   212
  Args.internal_typ >> T.Typ ||
wenzelm@27809
   213
  Args.internal_term >> T.Term ||
wenzelm@27882
   214
  Args.name_source >> T.Text;
wenzelm@20336
   215
wenzelm@27809
   216
val inst = Args.var -- (Args.$$$ "=" |-- Scan.ahead P.not_eof -- value)
wenzelm@20336
   217
  >> (fn (xi, (a, v)) => (a, (xi, v)));
wenzelm@20336
   218
wenzelm@20336
   219
in
wenzelm@20336
   220
wenzelm@27809
   221
val where_att = Attrib.syntax (Scan.lift (P.and_list inst) >> (fn args =>
wenzelm@27236
   222
  Thm.rule_attribute (fn context => read_instantiate_mixed (Context.proof_of context) args)));
wenzelm@20336
   223
wenzelm@20336
   224
end;
wenzelm@20336
   225
wenzelm@20336
   226
wenzelm@20343
   227
(* of: positional instantiation (terms only) *)
wenzelm@20336
   228
wenzelm@20336
   229
local
wenzelm@20336
   230
wenzelm@20336
   231
val value =
wenzelm@27809
   232
  Args.internal_term >> T.Term ||
wenzelm@27882
   233
  Args.name_source >> T.Text;
wenzelm@20336
   234
wenzelm@27809
   235
val inst = Scan.ahead P.not_eof -- Args.maybe value;
wenzelm@20336
   236
val concl = Args.$$$ "concl" -- Args.colon;
wenzelm@20336
   237
wenzelm@20336
   238
val insts =
wenzelm@20336
   239
  Scan.repeat (Scan.unless concl inst) --
wenzelm@20336
   240
  Scan.optional (concl |-- Scan.repeat inst) [];
wenzelm@20336
   241
wenzelm@20336
   242
in
wenzelm@20336
   243
wenzelm@20343
   244
val of_att = Attrib.syntax (Scan.lift insts >> (fn args =>
wenzelm@27236
   245
  Thm.rule_attribute (fn context => read_instantiate_mixed' (Context.proof_of context) args)));
wenzelm@20336
   246
wenzelm@20336
   247
end;
wenzelm@20336
   248
wenzelm@20336
   249
wenzelm@20336
   250
(* setup *)
wenzelm@20336
   251
wenzelm@26463
   252
val _ = Context.>> (Context.map_theory
wenzelm@26463
   253
  (Attrib.add_attributes
wenzelm@26463
   254
   [("where", where_att, "named instantiation of theorem"),
wenzelm@26463
   255
    ("of", of_att, "positional instantiation of theorem")]));
wenzelm@20336
   256
wenzelm@20336
   257
wenzelm@20336
   258
wenzelm@27245
   259
(** tactics **)
wenzelm@20336
   260
wenzelm@27245
   261
(* resolution after lifting and instantation; may refer to parameters of the subgoal *)
wenzelm@27245
   262
wenzelm@27245
   263
(* FIXME cleanup this mess!!! *)
wenzelm@20336
   264
wenzelm@20336
   265
fun bires_inst_tac bires_flag ctxt insts thm =
wenzelm@20336
   266
  let
wenzelm@20336
   267
    val thy = ProofContext.theory_of ctxt;
wenzelm@20336
   268
    (* Separate type and term insts *)
wenzelm@20336
   269
    fun has_type_var ((x, _), _) = (case Symbol.explode x of
wenzelm@20336
   270
          "'"::cs => true | cs => false);
wenzelm@20336
   271
    val Tinsts = List.filter has_type_var insts;
wenzelm@20336
   272
    val tinsts = filter_out has_type_var insts;
wenzelm@25333
   273
wenzelm@20336
   274
    (* Tactic *)
wenzelm@20336
   275
    fun tac i st =
wenzelm@20336
   276
      let
wenzelm@25333
   277
        val (_, _, Bi, _) = Thm.dest_state (st, i);
wenzelm@25333
   278
        val params = Logic.strip_params Bi;  (*params of subgoal i as string typ pairs*)
wenzelm@25333
   279
        val params = rev (Term.rename_wrt_term Bi params)
wenzelm@25333
   280
          (*as they are printed: bound variables with*)
wenzelm@25333
   281
          (*the same name are renamed during printing*)
wenzelm@25333
   282
wenzelm@25333
   283
        val (param_names, ctxt') = ctxt
wenzelm@25333
   284
          |> Variable.declare_thm thm
wenzelm@25333
   285
          |> Thm.fold_terms Variable.declare_constraints st
haftmann@28965
   286
          |> ProofContext.add_fixes_i (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) params);
wenzelm@25333
   287
wenzelm@25333
   288
        (* Process type insts: Tinsts_env *)
wenzelm@25333
   289
        fun absent xi = error
wenzelm@25333
   290
              ("No such variable in theorem: " ^ Term.string_of_vname xi);
wenzelm@25333
   291
        val (rtypes, rsorts) = Drule.types_sorts thm;
wenzelm@25333
   292
        fun readT (xi, s) =
wenzelm@25333
   293
            let val S = case rsorts xi of SOME S => S | NONE => absent xi;
wenzelm@25333
   294
                val T = Syntax.read_typ ctxt' s;
wenzelm@25333
   295
                val U = TVar (xi, S);
wenzelm@25333
   296
            in if Sign.typ_instance thy (T, U) then (U, T)
wenzelm@25333
   297
               else error ("Instantiation of " ^ Term.string_of_vname xi ^ " fails")
wenzelm@25333
   298
            end;
wenzelm@25333
   299
        val Tinsts_env = map readT Tinsts;
wenzelm@25333
   300
        (* Preprocess rule: extract vars and their types, apply Tinsts *)
wenzelm@25333
   301
        fun get_typ xi =
wenzelm@25333
   302
          (case rtypes xi of
wenzelm@25333
   303
               SOME T => typ_subst_atomic Tinsts_env T
wenzelm@25333
   304
             | NONE => absent xi);
wenzelm@25333
   305
        val (xis, ss) = Library.split_list tinsts;
wenzelm@25333
   306
        val Ts = map get_typ xis;
wenzelm@25333
   307
wenzelm@25333
   308
        val (ts, envT) = read_termTs ctxt' true ss Ts;
wenzelm@20336
   309
        val envT' = map (fn (ixn, T) =>
wenzelm@20336
   310
          (TVar (ixn, the (rsorts ixn)), T)) envT @ Tinsts_env;
wenzelm@20336
   311
        val cenv =
wenzelm@20336
   312
          map
wenzelm@20336
   313
            (fn (xi, t) =>
wenzelm@20336
   314
              pairself (Thm.cterm_of thy) (Var (xi, fastype_of t), t))
wenzelm@20336
   315
            (distinct
wenzelm@20336
   316
              (fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2)
wenzelm@20336
   317
              (xis ~~ ts));
wenzelm@20336
   318
        (* Lift and instantiate rule *)
wenzelm@20336
   319
        val {maxidx, ...} = rep_thm st;
wenzelm@20336
   320
        val paramTs = map #2 params
wenzelm@20336
   321
        and inc = maxidx+1
wenzelm@20336
   322
        fun liftvar (Var ((a,j), T)) =
wenzelm@20336
   323
              Var((a, j+inc), paramTs ---> Logic.incr_tvar inc T)
wenzelm@20336
   324
          | liftvar t = raise TERM("Variable expected", [t]);
wenzelm@20336
   325
        fun liftterm t = list_abs_free
wenzelm@25333
   326
              (param_names ~~ paramTs, Logic.incr_indexes(paramTs,inc) t)
wenzelm@20336
   327
        fun liftpair (cv,ct) =
wenzelm@20336
   328
              (cterm_fun liftvar cv, cterm_fun liftterm ct)
wenzelm@20336
   329
        val lifttvar = pairself (ctyp_of thy o Logic.incr_tvar inc);
wenzelm@20336
   330
        val rule = Drule.instantiate
wenzelm@20336
   331
              (map lifttvar envT', map liftpair cenv)
wenzelm@20336
   332
              (Thm.lift_rule (Thm.cprem_of st i) thm)
wenzelm@20336
   333
      in
wenzelm@20336
   334
        if i > nprems_of st then no_tac st
wenzelm@20336
   335
        else st |>
wenzelm@20336
   336
          compose_tac (bires_flag, rule, nprems_of thm) i
wenzelm@20336
   337
      end
wenzelm@20336
   338
           handle TERM (msg,_)   => (warning msg; no_tac st)
wenzelm@20336
   339
                | THM  (msg,_,_) => (warning msg; no_tac st);
wenzelm@20336
   340
  in tac end;
wenzelm@20336
   341
wenzelm@27120
   342
val res_inst_tac = bires_inst_tac false;
wenzelm@27120
   343
val eres_inst_tac = bires_inst_tac true;
wenzelm@27120
   344
wenzelm@27120
   345
wenzelm@27245
   346
(* forward resolution *)
wenzelm@27245
   347
wenzelm@27245
   348
fun make_elim_preserve rl =
wenzelm@27245
   349
  let
wenzelm@27245
   350
    val cert = Thm.cterm_of (Thm.theory_of_thm rl);
wenzelm@27245
   351
    val maxidx = Thm.maxidx_of rl;
wenzelm@27245
   352
    fun cvar xi = cert (Var (xi, propT));
wenzelm@27245
   353
    val revcut_rl' =
wenzelm@27245
   354
      instantiate ([], [(cvar ("V", 0), cvar ("V", maxidx + 1)),
wenzelm@27245
   355
        (cvar ("W", 0), cvar ("W", maxidx + 1))]) Drule.revcut_rl;
wenzelm@27245
   356
  in
wenzelm@27245
   357
    (case Seq.list_of (bicompose false (false, rl, Thm.nprems_of rl) 1 revcut_rl') of
wenzelm@27245
   358
      [th] => th
wenzelm@27245
   359
    | _ => raise THM ("make_elim_preserve", 1, [rl]))
wenzelm@27245
   360
  end;
wenzelm@27245
   361
wenzelm@27245
   362
(*instantiate and cut -- for atomic fact*)
wenzelm@27245
   363
fun cut_inst_tac ctxt insts rule = res_inst_tac ctxt insts (make_elim_preserve rule);
wenzelm@27245
   364
wenzelm@27245
   365
(*forward tactic applies a rule to an assumption without deleting it*)
wenzelm@27245
   366
fun forw_inst_tac ctxt insts rule = cut_inst_tac ctxt insts rule THEN' assume_tac;
wenzelm@27245
   367
wenzelm@27245
   368
(*dresolve tactic applies a rule to replace an assumption*)
wenzelm@27245
   369
fun dres_inst_tac ctxt insts rule = eres_inst_tac ctxt insts (make_elim_preserve rule);
wenzelm@27245
   370
wenzelm@27245
   371
wenzelm@27245
   372
(* derived tactics *)
wenzelm@27245
   373
wenzelm@27245
   374
(*deletion of an assumption*)
wenzelm@27245
   375
fun thin_tac ctxt s = eres_inst_tac ctxt [(("V", 0), s)] Drule.thin_rl;
wenzelm@27245
   376
wenzelm@27245
   377
(*Introduce the given proposition as lemma and subgoal*)
wenzelm@27245
   378
fun subgoal_tac ctxt A = DETERM o res_inst_tac ctxt [(("psi", 0), A)] cut_rl;
wenzelm@27245
   379
fun subgoals_tac ctxt As = EVERY' (map (subgoal_tac ctxt) As);
wenzelm@27245
   380
wenzelm@27245
   381
wenzelm@27245
   382
wenzelm@27245
   383
(** methods **)
wenzelm@27245
   384
wenzelm@27245
   385
(* rule_tac etc. -- refer to dynamic goal state! *)
wenzelm@27245
   386
wenzelm@20336
   387
local
wenzelm@20336
   388
wenzelm@20336
   389
fun gen_inst _ tac _ (quant, ([], thms)) =
wenzelm@20336
   390
      Method.METHOD (fn facts => quant (Method.insert_tac facts THEN' tac thms))
wenzelm@20336
   391
  | gen_inst inst_tac _ ctxt (quant, (insts, [thm])) =
wenzelm@20336
   392
      Method.METHOD (fn facts =>
wenzelm@20336
   393
        quant (Method.insert_tac facts THEN' inst_tac ctxt insts thm))
wenzelm@20336
   394
  | gen_inst _ _ _ _ = error "Cannot have instantiations with multiple rules";
wenzelm@20336
   395
wenzelm@20336
   396
in
wenzelm@20336
   397
wenzelm@27120
   398
val res_inst_meth = gen_inst res_inst_tac Tactic.resolve_tac;
wenzelm@27120
   399
val eres_inst_meth = gen_inst eres_inst_tac Tactic.eresolve_tac;
wenzelm@27245
   400
val cut_inst_meth = gen_inst cut_inst_tac Tactic.cut_rules_tac;
wenzelm@27245
   401
val dres_inst_meth = gen_inst dres_inst_tac Tactic.dresolve_tac;
wenzelm@27245
   402
val forw_inst_meth = gen_inst forw_inst_tac Tactic.forward_tac;
wenzelm@20336
   403
wenzelm@27245
   404
end;
wenzelm@20336
   405
wenzelm@20336
   406
wenzelm@20336
   407
(* method syntax *)
wenzelm@20336
   408
wenzelm@20336
   409
val insts =
wenzelm@27882
   410
  Scan.optional (Scan.lift
wenzelm@27882
   411
    (P.and_list1 (Args.name -- (Args.$$$ "=" |-- P.!!! Args.name_source)) --| Args.$$$ "in")) []
wenzelm@27809
   412
    -- Attrib.thms;
wenzelm@20336
   413
wenzelm@20336
   414
fun inst_args f src ctxt =
haftmann@21879
   415
  f ctxt (fst (Method.syntax (Args.goal_spec HEADGOAL -- insts) src ctxt));
wenzelm@20336
   416
wenzelm@20336
   417
val insts_var =
wenzelm@27882
   418
  Scan.optional (Scan.lift
wenzelm@27882
   419
    (P.and_list1 (Args.var -- (Args.$$$ "=" |-- P.!!! Args.name_source)) --| Args.$$$ "in")) []
wenzelm@27809
   420
    -- Attrib.thms;
wenzelm@20336
   421
wenzelm@20336
   422
fun inst_args_var f src ctxt =
haftmann@21879
   423
  f ctxt (fst (Method.syntax (Args.goal_spec HEADGOAL -- insts_var) src ctxt));
wenzelm@20336
   424
wenzelm@20336
   425
wenzelm@20336
   426
(* setup *)
wenzelm@20336
   427
wenzelm@26463
   428
val _ = Context.>> (Context.map_theory
wenzelm@26463
   429
  (Method.add_methods
wenzelm@26463
   430
   [("rule_tac", inst_args_var res_inst_meth,
wenzelm@26463
   431
      "apply rule (dynamic instantiation)"),
wenzelm@26463
   432
    ("erule_tac", inst_args_var eres_inst_meth,
wenzelm@26463
   433
      "apply rule in elimination manner (dynamic instantiation)"),
wenzelm@26463
   434
    ("drule_tac", inst_args_var dres_inst_meth,
wenzelm@26463
   435
      "apply rule in destruct manner (dynamic instantiation)"),
wenzelm@26463
   436
    ("frule_tac", inst_args_var forw_inst_meth,
wenzelm@26463
   437
      "apply rule in forward manner (dynamic instantiation)"),
wenzelm@26463
   438
    ("cut_tac", inst_args_var cut_inst_meth,
wenzelm@26463
   439
      "cut rule (dynamic instantiation)"),
wenzelm@27882
   440
    ("subgoal_tac", Method.goal_args_ctxt (Scan.repeat1 Args.name_source) subgoals_tac,
wenzelm@26463
   441
      "insert subgoal (dynamic instantiation)"),
wenzelm@27882
   442
    ("thin_tac", Method.goal_args_ctxt Args.name_source thin_tac,
wenzelm@26463
   443
      "remove premise (dynamic instantiation)")]));
wenzelm@20336
   444
wenzelm@20336
   445
end;
wenzelm@20336
   446
wenzelm@27245
   447
structure BasicRuleInsts: BASIC_RULE_INSTS = RuleInsts;
wenzelm@27245
   448
open BasicRuleInsts;