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