src/Pure/Isar/rule_insts.ML
author wenzelm
Wed Sep 30 22:20:58 2009 +0200 (2009-09-30)
changeset 32784 1a5dde5079ac
parent 31977 e03059ae2d82
child 33317 b4534348b8fd
permissions -rw-r--r--
eliminated redundant bindings;
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@30545
    19
  val method: (Proof.context -> (indexname * string) list -> thm -> int -> tactic) ->
wenzelm@30551
    20
    (Proof.context -> thm list -> int -> tactic) -> (Proof.context -> Proof.method) context_parser
wenzelm@27245
    21
end;
wenzelm@27245
    22
wenzelm@27245
    23
signature RULE_INSTS =
wenzelm@27245
    24
sig
wenzelm@27245
    25
  include BASIC_RULE_INSTS
wenzelm@27245
    26
  val make_elim_preserve: thm -> thm
wenzelm@20336
    27
end;
wenzelm@20336
    28
wenzelm@20336
    29
structure RuleInsts: RULE_INSTS =
wenzelm@20336
    30
struct
wenzelm@20336
    31
wenzelm@27809
    32
structure T = OuterLex;
wenzelm@27809
    33
structure P = OuterParse;
wenzelm@27809
    34
wenzelm@20336
    35
wenzelm@20343
    36
(** reading instantiations **)
wenzelm@20336
    37
wenzelm@20336
    38
local
wenzelm@20336
    39
wenzelm@20343
    40
fun is_tvar (x, _) = String.isPrefix "'" x;
wenzelm@20336
    41
wenzelm@22681
    42
fun error_var msg xi = error (msg ^ Term.string_of_vname xi);
wenzelm@20336
    43
wenzelm@20343
    44
fun the_sort tvars xi = the (AList.lookup (op =) tvars xi)
wenzelm@20336
    45
  handle Option.Option => error_var "No such type variable in theorem: " xi;
wenzelm@20336
    46
wenzelm@20343
    47
fun the_type vars xi = the (AList.lookup (op =) vars xi)
wenzelm@20336
    48
  handle Option.Option => error_var "No such variable in theorem: " xi;
wenzelm@20336
    49
wenzelm@20343
    50
fun unify_vartypes thy vars (xi, u) (unifier, maxidx) =
wenzelm@20336
    51
  let
wenzelm@20343
    52
    val T = the_type vars xi;
wenzelm@20336
    53
    val U = Term.fastype_of u;
wenzelm@20343
    54
    val maxidx' = Term.maxidx_term u (Int.max (#2 xi, maxidx));
wenzelm@20336
    55
  in
wenzelm@20336
    56
    Sign.typ_unify thy (T, U) (unifier, maxidx')
wenzelm@20336
    57
      handle Type.TUNIFY => error_var "Incompatible type for instantiation of " xi
wenzelm@20336
    58
  end;
wenzelm@20336
    59
wenzelm@20343
    60
fun instantiate inst =
wenzelm@31977
    61
  Term_Subst.instantiate ([], map (fn (xi, t) => ((xi, Term.fastype_of t), t)) inst) #>
wenzelm@20343
    62
  Envir.beta_norm;
wenzelm@20336
    63
wenzelm@20343
    64
fun make_instT f v =
wenzelm@20336
    65
  let
wenzelm@20343
    66
    val T = TVar v;
wenzelm@20343
    67
    val T' = f T;
wenzelm@20343
    68
  in if T = T' then NONE else SOME (T, T') end;
wenzelm@20343
    69
wenzelm@20343
    70
fun make_inst f v =
wenzelm@20343
    71
  let
wenzelm@20343
    72
    val t = Var v;
wenzelm@20343
    73
    val t' = f t;
wenzelm@20343
    74
  in if t aconv t' then NONE else SOME (t, t') end;
wenzelm@20336
    75
wenzelm@27282
    76
val add_used =
wenzelm@27282
    77
  (Thm.fold_terms o fold_types o fold_atyps)
wenzelm@27282
    78
    (fn TFree (a, _) => insert (op =) a
wenzelm@27282
    79
      | TVar ((a, _), _) => insert (op =) a
wenzelm@27282
    80
      | _ => I);
wenzelm@27282
    81
wenzelm@20336
    82
in
wenzelm@20336
    83
wenzelm@25333
    84
fun read_termTs ctxt schematic ss Ts =
wenzelm@25329
    85
  let
wenzelm@25329
    86
    fun parse T = if T = propT then Syntax.parse_prop ctxt else Syntax.parse_term ctxt;
wenzelm@25329
    87
    val ts = map2 parse Ts ss;
wenzelm@25329
    88
    val ts' =
wenzelm@25329
    89
      map2 (TypeInfer.constrain o TypeInfer.paramify_vars) Ts ts
wenzelm@25333
    90
      |> Syntax.check_terms ((schematic ? ProofContext.set_mode ProofContext.mode_schematic) ctxt)
wenzelm@25329
    91
      |> Variable.polymorphic ctxt;
wenzelm@25329
    92
    val Ts' = map Term.fastype_of ts';
wenzelm@25329
    93
    val tyenv = fold Type.raw_match (Ts ~~ Ts') Vartab.empty;
wenzelm@25329
    94
  in (ts', map (apsnd snd) (Vartab.dest tyenv)) end;
wenzelm@25329
    95
wenzelm@20343
    96
fun read_insts ctxt mixed_insts (tvars, vars) =
wenzelm@20336
    97
  let
wenzelm@20343
    98
    val thy = ProofContext.theory_of ctxt;
wenzelm@20343
    99
    val cert = Thm.cterm_of thy;
wenzelm@20343
   100
    val certT = Thm.ctyp_of thy;
wenzelm@20336
   101
wenzelm@20343
   102
    val (type_insts, term_insts) = List.partition (is_tvar o fst) mixed_insts;
wenzelm@20336
   103
    val internal_insts = term_insts |> map_filter
wenzelm@27809
   104
      (fn (xi, T.Term t) => SOME (xi, t)
wenzelm@27809
   105
        | (_, T.Text _) => NONE
wenzelm@20343
   106
        | (xi, _) => error_var "Term argument expected for " xi);
wenzelm@20336
   107
    val external_insts = term_insts |> map_filter
wenzelm@27809
   108
      (fn (xi, T.Text s) => SOME (xi, s) | _ => NONE);
wenzelm@20336
   109
wenzelm@20336
   110
wenzelm@20343
   111
    (* mixed type instantiations *)
wenzelm@20336
   112
wenzelm@20336
   113
    fun readT (xi, arg) =
wenzelm@20336
   114
      let
wenzelm@20343
   115
        val S = the_sort tvars xi;
wenzelm@20336
   116
        val T =
wenzelm@20336
   117
          (case arg of
wenzelm@27809
   118
            T.Text s => Syntax.read_typ ctxt s
wenzelm@27809
   119
          | T.Typ T => T
wenzelm@20336
   120
          | _ => error_var "Type argument expected for " xi);
wenzelm@20336
   121
      in
wenzelm@20343
   122
        if Sign.of_sort thy (T, S) then ((xi, S), T)
wenzelm@20336
   123
        else error_var "Incompatible sort for typ instantiation of " xi
wenzelm@20336
   124
      end;
wenzelm@20336
   125
wenzelm@20343
   126
    val type_insts1 = map readT type_insts;
wenzelm@31977
   127
    val instT1 = Term_Subst.instantiateT type_insts1;
wenzelm@20343
   128
    val vars1 = map (apsnd instT1) vars;
wenzelm@20336
   129
wenzelm@20336
   130
wenzelm@20336
   131
    (* internal term instantiations *)
wenzelm@20336
   132
wenzelm@20343
   133
    val instT2 = Envir.norm_type
wenzelm@20343
   134
      (#1 (fold (unify_vartypes thy vars1) internal_insts (Vartab.empty, 0)));
wenzelm@20343
   135
    val vars2 = map (apsnd instT2) vars1;
wenzelm@20548
   136
    val internal_insts2 = map (apsnd (map_types instT2)) internal_insts;
wenzelm@20343
   137
    val inst2 = instantiate internal_insts2;
wenzelm@20336
   138
wenzelm@20336
   139
wenzelm@20336
   140
    (* external term instantiations *)
wenzelm@20336
   141
wenzelm@20343
   142
    val (xs, strs) = split_list external_insts;
wenzelm@20343
   143
    val Ts = map (the_type vars2) xs;
wenzelm@25354
   144
    val (ts, inferred) = read_termTs ctxt false strs Ts;
wenzelm@20336
   145
wenzelm@20343
   146
    val instT3 = Term.typ_subst_TVars inferred;
wenzelm@20343
   147
    val vars3 = map (apsnd instT3) vars2;
wenzelm@20548
   148
    val internal_insts3 = map (apsnd (map_types instT3)) internal_insts2;
wenzelm@20343
   149
    val external_insts3 = xs ~~ ts;
wenzelm@20343
   150
    val inst3 = instantiate external_insts3;
wenzelm@20336
   151
wenzelm@20336
   152
wenzelm@20343
   153
    (* results *)
wenzelm@20343
   154
wenzelm@20343
   155
    val type_insts3 = map (fn ((a, _), T) => (a, instT3 (instT2 T))) type_insts1;
wenzelm@20343
   156
    val term_insts3 = internal_insts3 @ external_insts3;
wenzelm@20336
   157
wenzelm@20343
   158
    val inst_tvars = map_filter (make_instT (instT3 o instT2 o instT1)) tvars;
wenzelm@20343
   159
    val inst_vars = map_filter (make_inst (inst3 o inst2)) vars3;
wenzelm@20343
   160
  in
wenzelm@20343
   161
    ((type_insts3, term_insts3),
wenzelm@20343
   162
      (map (pairself certT) inst_tvars, map (pairself cert) inst_vars))
wenzelm@20343
   163
  end;
wenzelm@20343
   164
wenzelm@27236
   165
fun read_instantiate_mixed ctxt mixed_insts thm =
wenzelm@20343
   166
  let
wenzelm@20487
   167
    val ctxt' = ctxt |> Variable.declare_thm thm
wenzelm@27282
   168
      |> fold (fn a => Variable.declare_names (Logic.mk_type (TFree (a, dummyS)))) (add_used thm []);  (* FIXME tmp *)
wenzelm@22692
   169
    val tvars = Thm.fold_terms Term.add_tvars thm [];
wenzelm@22692
   170
    val vars = Thm.fold_terms Term.add_vars thm [];
wenzelm@20343
   171
    val ((type_insts, term_insts), insts) = read_insts ctxt' (map snd mixed_insts) (tvars, vars);
wenzelm@20343
   172
wenzelm@20343
   173
    val _ = (*assign internalized values*)
wenzelm@20336
   174
      mixed_insts |> List.app (fn (arg, (xi, _)) =>
wenzelm@20336
   175
        if is_tvar xi then
wenzelm@27809
   176
          T.assign (SOME (T.Typ (the (AList.lookup (op =) type_insts xi)))) arg
wenzelm@20336
   177
        else
wenzelm@27809
   178
          T.assign (SOME (T.Term (the (AList.lookup (op =) term_insts xi)))) arg);
wenzelm@20343
   179
  in
wenzelm@20343
   180
    Drule.instantiate insts thm |> RuleCases.save thm
wenzelm@20343
   181
  end;
wenzelm@20336
   182
wenzelm@27236
   183
fun read_instantiate_mixed' ctxt (args, concl_args) thm =
wenzelm@20343
   184
  let
wenzelm@20343
   185
    fun zip_vars _ [] = []
wenzelm@20343
   186
      | zip_vars (_ :: xs) ((_, NONE) :: rest) = zip_vars xs rest
wenzelm@20343
   187
      | zip_vars ((x, _) :: xs) ((arg, SOME t) :: rest) = (arg, (x, t)) :: zip_vars xs rest
wenzelm@20343
   188
      | zip_vars [] _ = error "More instantiations than variables in theorem";
wenzelm@20343
   189
    val insts =
wenzelm@20343
   190
      zip_vars (rev (Term.add_vars (Thm.full_prop_of thm) [])) args @
wenzelm@20343
   191
      zip_vars (rev (Term.add_vars (Thm.concl_of thm) [])) concl_args;
wenzelm@27236
   192
  in read_instantiate_mixed ctxt insts thm end;
wenzelm@27236
   193
wenzelm@27245
   194
end;
wenzelm@27245
   195
wenzelm@27245
   196
wenzelm@27245
   197
(* instantiation of rule or goal state *)
wenzelm@27245
   198
wenzelm@27236
   199
fun read_instantiate ctxt args thm =
wenzelm@27236
   200
  read_instantiate_mixed (ctxt |> ProofContext.set_mode ProofContext.mode_schematic)  (* FIXME !? *)
wenzelm@27809
   201
    (map (fn (x, y) => (T.eof, (x, T.Text y))) args) thm;
wenzelm@20336
   202
wenzelm@27245
   203
fun instantiate_tac ctxt args = PRIMITIVE (read_instantiate ctxt args);
wenzelm@20336
   204
wenzelm@20336
   205
wenzelm@20343
   206
wenzelm@20343
   207
(** attributes **)
wenzelm@20343
   208
wenzelm@20336
   209
(* where: named instantiation *)
wenzelm@20336
   210
wenzelm@20336
   211
local
wenzelm@20336
   212
wenzelm@20336
   213
val value =
wenzelm@27809
   214
  Args.internal_typ >> T.Typ ||
wenzelm@27809
   215
  Args.internal_term >> T.Term ||
wenzelm@27882
   216
  Args.name_source >> T.Text;
wenzelm@20336
   217
wenzelm@27809
   218
val inst = Args.var -- (Args.$$$ "=" |-- Scan.ahead P.not_eof -- value)
wenzelm@20336
   219
  >> (fn (xi, (a, v)) => (a, (xi, v)));
wenzelm@20336
   220
wenzelm@20336
   221
in
wenzelm@20336
   222
wenzelm@30722
   223
val _ = Context.>> (Context.map_theory
wenzelm@30722
   224
  (Attrib.setup (Binding.name "where")
wenzelm@30722
   225
    (Scan.lift (P.and_list inst) >> (fn args =>
wenzelm@30722
   226
      Thm.rule_attribute (fn context => read_instantiate_mixed (Context.proof_of context) args)))
wenzelm@30722
   227
    "named instantiation of theorem"));
wenzelm@20336
   228
wenzelm@20336
   229
end;
wenzelm@20336
   230
wenzelm@20336
   231
wenzelm@20343
   232
(* of: positional instantiation (terms only) *)
wenzelm@20336
   233
wenzelm@20336
   234
local
wenzelm@20336
   235
wenzelm@20336
   236
val value =
wenzelm@27809
   237
  Args.internal_term >> T.Term ||
wenzelm@27882
   238
  Args.name_source >> T.Text;
wenzelm@20336
   239
wenzelm@27809
   240
val inst = Scan.ahead P.not_eof -- Args.maybe value;
wenzelm@20336
   241
val concl = Args.$$$ "concl" -- Args.colon;
wenzelm@20336
   242
wenzelm@20336
   243
val insts =
wenzelm@20336
   244
  Scan.repeat (Scan.unless concl inst) --
wenzelm@20336
   245
  Scan.optional (concl |-- Scan.repeat inst) [];
wenzelm@20336
   246
wenzelm@20336
   247
in
wenzelm@20336
   248
wenzelm@30722
   249
val _ = Context.>> (Context.map_theory
wenzelm@30722
   250
  (Attrib.setup (Binding.name "of")
wenzelm@30722
   251
    (Scan.lift insts >> (fn args =>
wenzelm@30722
   252
      Thm.rule_attribute (fn context => read_instantiate_mixed' (Context.proof_of context) args)))
wenzelm@30722
   253
    "positional instantiation of theorem"));
wenzelm@20336
   254
wenzelm@20336
   255
end;
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@32784
   269
    fun has_type_var ((x, _), _) =
wenzelm@32784
   270
      (case Symbol.explode x of "'" :: _ => true | _ => 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
wenzelm@30763
   286
          |> ProofContext.add_fixes (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@31945
   357
    (case Seq.list_of (Thm.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@30545
   387
fun method inst_tac tac =
wenzelm@30515
   388
  Args.goal_spec --
wenzelm@30515
   389
  Scan.optional (Scan.lift
wenzelm@30515
   390
    (P.and_list1 (Args.var -- (Args.$$$ "=" |-- P.!!! Args.name_source)) --| Args.$$$ "in")) [] --
wenzelm@30515
   391
  Attrib.thms >>
wenzelm@30515
   392
  (fn ((quant, insts), thms) => fn ctxt => METHOD (fn facts =>
wenzelm@30551
   393
    if null insts then quant (Method.insert_tac facts THEN' tac ctxt thms)
wenzelm@30515
   394
    else
wenzelm@30515
   395
      (case thms of
wenzelm@30515
   396
        [thm] => quant (Method.insert_tac facts THEN' inst_tac ctxt insts thm)
wenzelm@30515
   397
      | _ => error "Cannot have instantiations with multiple rules")));
wenzelm@20336
   398
wenzelm@30551
   399
val res_inst_meth = method res_inst_tac (K Tactic.resolve_tac);
wenzelm@30551
   400
val eres_inst_meth = method eres_inst_tac (K Tactic.eresolve_tac);
wenzelm@30551
   401
val cut_inst_meth = method cut_inst_tac (K Tactic.cut_rules_tac);
wenzelm@30551
   402
val dres_inst_meth = method dres_inst_tac (K Tactic.dresolve_tac);
wenzelm@30551
   403
val forw_inst_meth = method forw_inst_tac (K Tactic.forward_tac);
wenzelm@20336
   404
wenzelm@20336
   405
wenzelm@20336
   406
(* setup *)
wenzelm@20336
   407
wenzelm@26463
   408
val _ = Context.>> (Context.map_theory
wenzelm@30515
   409
 (Method.setup (Binding.name "rule_tac") res_inst_meth "apply rule (dynamic instantiation)" #>
wenzelm@30515
   410
  Method.setup (Binding.name "erule_tac") eres_inst_meth
wenzelm@30515
   411
    "apply rule in elimination manner (dynamic instantiation)" #>
wenzelm@30515
   412
  Method.setup (Binding.name "drule_tac") dres_inst_meth
wenzelm@30515
   413
    "apply rule in destruct manner (dynamic instantiation)" #>
wenzelm@30515
   414
  Method.setup (Binding.name "frule_tac") forw_inst_meth
wenzelm@30515
   415
    "apply rule in forward manner (dynamic instantiation)" #>
wenzelm@30515
   416
  Method.setup (Binding.name "cut_tac") cut_inst_meth "cut rule (dynamic instantiation)" #>
wenzelm@30515
   417
  Method.setup (Binding.name "subgoal_tac")
wenzelm@30515
   418
    (Args.goal_spec -- Scan.lift (Scan.repeat1 Args.name_source) >>
wenzelm@30515
   419
      (fn (quant, props) => fn ctxt => SIMPLE_METHOD'' quant (subgoals_tac ctxt props)))
wenzelm@30515
   420
    "insert subgoal (dynamic instantiation)" #>
wenzelm@30515
   421
  Method.setup (Binding.name "thin_tac")
wenzelm@30515
   422
    (Args.goal_spec -- Scan.lift Args.name_source >>
wenzelm@30515
   423
      (fn (quant, prop) => fn ctxt => SIMPLE_METHOD'' quant (thin_tac ctxt prop)))
wenzelm@30515
   424
      "remove premise (dynamic instantiation)"));
wenzelm@20336
   425
wenzelm@20336
   426
end;
wenzelm@20336
   427
wenzelm@27245
   428
structure BasicRuleInsts: BASIC_RULE_INSTS = RuleInsts;
wenzelm@27245
   429
open BasicRuleInsts;