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