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