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