src/Pure/Tools/rule_insts.ML
author wenzelm
Thu, 19 Mar 2015 17:25:57 +0100
changeset 59754 696d87036f04
parent 59623 920889b0788e
child 59755 f8d164ab0dc1
permissions -rw-r--r--
misc tuning;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53707
d1c6bff9ff58 moved module into plain Isabelle/ML user space;
wenzelm
parents: 53171
diff changeset
     1
(*  Title:      Pure/Tools/rule_insts.ML
20336
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
53708
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
     4
Rule instantiations -- operations within implicit rule / subgoal context.
20336
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
27120
b21eec437295 added (e)res_inst_tac;
wenzelm
parents: 26463
diff changeset
     9
  val res_inst_tac: Proof.context -> (indexname * string) list -> thm -> int -> tactic
b21eec437295 added (e)res_inst_tac;
wenzelm
parents: 26463
diff changeset
    10
  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
    11
  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
    12
  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
    13
  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
    14
  val thin_tac: Proof.context -> string -> 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
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
    16
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
    17
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
    18
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
    19
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
    20
  include BASIC_RULE_INSTS
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
    21
  val where_rule: Proof.context -> (indexname * string) list ->
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
    22
    (binding * string option * mixfix) list -> thm -> thm
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
    23
  val of_rule: Proof.context -> string option list * string option list ->
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
    24
    (binding * string option * mixfix) list -> thm -> thm
55151
f331472f1027 tuned signature;
wenzelm
parents: 55143
diff changeset
    25
  val read_instantiate: Proof.context -> (indexname * string) list -> string list -> thm -> thm
f331472f1027 tuned signature;
wenzelm
parents: 55143
diff changeset
    26
  val instantiate_tac: Proof.context -> (indexname * string) list -> string list -> tactic
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58027
diff changeset
    27
  val make_elim_preserve: Proof.context -> thm -> thm
53708
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
    28
  val method: (Proof.context -> (indexname * string) list -> thm -> int -> tactic) ->
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
    29
    (Proof.context -> thm list -> int -> tactic) -> (Proof.context -> Proof.method) context_parser
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    30
end;
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    31
42806
4b660cdab9b7 modernized structure Rule_Insts;
wenzelm
parents: 42360
diff changeset
    32
structure Rule_Insts: RULE_INSTS =
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    33
struct
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
(** reading instantiations **)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    36
59754
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
    37
val partition_insts = List.partition (fn ((x, _), _) => String.isPrefix "'" x);
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
    38
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    39
local
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    40
45611
wenzelm
parents: 44241
diff changeset
    41
fun error_var msg xi = error (msg ^ quote (Term.string_of_vname xi));
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    42
45611
wenzelm
parents: 44241
diff changeset
    43
fun the_sort tvars (xi: indexname) =
wenzelm
parents: 44241
diff changeset
    44
  (case AList.lookup (op =) tvars xi of
wenzelm
parents: 44241
diff changeset
    45
    SOME S => S
wenzelm
parents: 44241
diff changeset
    46
  | NONE => error_var "No such type variable in theorem: " xi);
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    47
45611
wenzelm
parents: 44241
diff changeset
    48
fun the_type vars (xi: indexname) =
wenzelm
parents: 44241
diff changeset
    49
  (case AList.lookup (op =) vars xi of
wenzelm
parents: 44241
diff changeset
    50
    SOME T => T
wenzelm
parents: 44241
diff changeset
    51
  | NONE => error_var "No such variable in theorem: " xi);
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    52
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    53
fun instantiate inst =
31977
e03059ae2d82 renamed structure TermSubst to Term_Subst;
wenzelm
parents: 31945
diff changeset
    54
  Term_Subst.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
    55
  Envir.beta_norm;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    56
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    57
fun make_instT f v =
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    58
  let
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    59
    val T = TVar v;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    60
    val T' = f T;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    61
  in if T = T' then NONE else SOME (T, T') end;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    62
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    63
fun make_inst f v =
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    64
  let
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    65
    val t = Var v;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    66
    val t' = f t;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    67
  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
    68
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    69
in
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    70
55156
wenzelm
parents: 55151
diff changeset
    71
fun read_termTs ctxt ss Ts =
25329
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    72
  let
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    73
    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
    74
    val ts = map2 parse Ts ss;
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    75
    val ts' =
39288
f1ae2493d93f eliminated aliases of Type.constraint;
wenzelm
parents: 37145
diff changeset
    76
      map2 (Type.constraint o Type_Infer.paramify_vars) Ts ts
55156
wenzelm
parents: 55151
diff changeset
    77
      |> Syntax.check_terms ctxt
25329
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    78
      |> Variable.polymorphic ctxt;
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    79
    val Ts' = map Term.fastype_of ts';
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    80
    val tyenv = fold Type.raw_match (Ts ~~ Ts') Vartab.empty;
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    81
  in (ts', map (apsnd snd) (Vartab.dest tyenv)) end;
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    82
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    83
fun read_insts ctxt mixed_insts (tvars, vars) =
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    84
  let
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 39288
diff changeset
    85
    val thy = Proof_Context.theory_of ctxt;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    86
59754
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
    87
    val (type_insts, term_insts) = partition_insts mixed_insts;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    88
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    89
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
    90
    (* type instantiations *)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    91
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
    92
    fun readT (xi, s) =
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    93
      let
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    94
        val S = the_sort tvars xi;
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
    95
        val T = Syntax.read_typ ctxt s;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    96
      in
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    97
        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
    98
        else error_var "Incompatible sort for typ instantiation of " xi
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    99
      end;
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   100
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   101
    val instT1 = Term_Subst.instantiateT (map readT type_insts);
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   102
    val vars1 = map (apsnd instT1) vars;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   103
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   104
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   105
    (* term instantiations *)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   106
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   107
    val (xs, ss) = split_list term_insts;
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   108
    val Ts = map (the_type vars1) xs;
55156
wenzelm
parents: 55151
diff changeset
   109
    val (ts, inferred) = read_termTs ctxt ss Ts;
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   110
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   111
    val instT2 = Term.typ_subst_TVars inferred;
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   112
    val vars2 = map (apsnd instT2) vars1;
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   113
    val inst2 = instantiate (xs ~~ ts);
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   114
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   115
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   116
    (* result *)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   117
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   118
    val inst_tvars = map_filter (make_instT (instT2 o instT1)) tvars;
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   119
    val inst_vars = map_filter (make_inst inst2) vars2;
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   120
  in
59623
920889b0788e clarified context;
wenzelm
parents: 59621
diff changeset
   121
    (map (apply2 (Thm.ctyp_of ctxt)) inst_tvars,
920889b0788e clarified context;
wenzelm
parents: 59621
diff changeset
   122
     map (apply2 (Thm.cterm_of ctxt)) inst_vars)
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   123
  end;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   124
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   125
fun where_rule ctxt mixed_insts fixes thm =
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   126
  let
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   127
    val ctxt' = ctxt
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   128
      |> Proof_Context.read_vars fixes |-> Proof_Context.add_fixes |> #2
58027
dc58ab4d9f44 discontinued odd "temporary" workaround from 2006 (6ac7a4fc32a0), which has no measurable relevance;
wenzelm
parents: 55156
diff changeset
   129
      |> Variable.declare_thm thm;
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22681
diff changeset
   130
    val tvars = Thm.fold_terms Term.add_tvars thm [];
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22681
diff changeset
   131
    val vars = Thm.fold_terms Term.add_vars thm [];
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   132
    val insts = read_insts ctxt' mixed_insts (tvars, vars);
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   133
  in
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   134
    Drule.instantiate_normalize insts thm
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   135
    |> singleton (Proof_Context.export ctxt' ctxt)
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   136
    |> Rule_Cases.save thm
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   137
  end;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   138
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   139
fun of_rule ctxt (args, concl_args) fixes thm =
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   140
  let
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   141
    fun zip_vars _ [] = []
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   142
      | zip_vars (_ :: xs) (NONE :: rest) = zip_vars xs rest
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   143
      | zip_vars ((x, _) :: xs) (SOME t :: rest) = (x, t) :: zip_vars xs rest
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   144
      | zip_vars [] _ = error "More instantiations than variables in theorem";
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   145
    val insts =
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   146
      zip_vars (rev (Term.add_vars (Thm.full_prop_of thm) [])) args @
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   147
      zip_vars (rev (Term.add_vars (Thm.concl_of thm) [])) concl_args;
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   148
  in where_rule ctxt insts fixes thm end;
27236
80356567e7ad added read_instantiate;
wenzelm
parents: 27219
diff changeset
   149
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
   150
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
   151
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   152
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   153
(* 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
   154
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   155
fun read_instantiate ctxt insts xs =
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   156
  where_rule ctxt insts (map (fn x => (Binding.name x, NONE, NoSyn)) xs);
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   157
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   158
fun instantiate_tac ctxt insts fixes =
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   159
  PRIMITIVE (read_instantiate ctxt insts fixes);
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   160
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   161
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   162
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   163
(** attributes **)
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   164
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   165
(* where: named instantiation *)
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   166
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 52732
diff changeset
   167
val _ = Theory.setup
53708
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
   168
  (Attrib.setup @{binding "where"}
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   169
    (Scan.lift
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   170
      (Parse.and_list (Args.var -- (Args.$$$ "=" |-- Args.name_inner_syntax)) -- Parse.for_fixes) >>
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   171
      (fn (insts, fixes) =>
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   172
        Thm.rule_attribute (fn context => where_rule (Context.proof_of context) insts fixes)))
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 52732
diff changeset
   173
    "named instantiation of theorem");
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   174
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   175
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   176
(* of: positional instantiation (terms only) *)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   177
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   178
local
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   179
55111
5792f5106c40 tuned signature;
wenzelm
parents: 53708
diff changeset
   180
val inst = Args.maybe Args.name_inner_syntax;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   181
val concl = Args.$$$ "concl" -- Args.colon;
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   182
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   183
val insts =
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   184
  Scan.repeat (Scan.unless concl inst) --
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   185
  Scan.optional (concl |-- Scan.repeat inst) [];
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   186
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   187
in
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   188
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 52732
diff changeset
   189
val _ = Theory.setup
53708
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
   190
  (Attrib.setup @{binding "of"}
55143
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   191
    (Scan.lift (insts -- Parse.for_fixes) >> (fn (args, fixes) =>
04448228381d explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents: 55111
diff changeset
   192
      Thm.rule_attribute (fn context => of_rule (Context.proof_of context) args fixes)))
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 52732
diff changeset
   193
    "positional instantiation of theorem");
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
end;
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   196
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
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
   199
(** tactics **)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   200
59754
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   201
(* resolution after lifting and instantiation; may refer to parameters of the subgoal *)
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
   202
59754
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   203
fun bires_inst_tac bires_flag ctxt mixed_insts thm i st = CSUBGOAL (fn (cgoal, _) =>
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   204
  let
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 39288
diff changeset
   205
    val thy = Proof_Context.theory_of ctxt;
59754
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   206
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   207
    val (Tinsts, tinsts) = partition_insts mixed_insts;
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   208
    val goal = Thm.term_of cgoal;
25333
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   209
59754
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   210
    val params =
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   211
      Logic.strip_params goal
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   212
      (*as they are printed: bound variables with the same name are renamed*)
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   213
      |> Term.rename_wrt_term goal
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   214
      |> rev;
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   215
    val (param_names, ctxt') = ctxt
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   216
      |> Variable.declare_thm thm
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   217
      |> Thm.fold_terms Variable.declare_constraints st
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   218
      |> Proof_Context.add_fixes (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) params);
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   219
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   220
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   221
    (* process type insts: Tinsts_env *)
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   222
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   223
    fun absent xi =
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   224
      error ("No such variable in theorem: " ^ Term.string_of_vname xi);
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   225
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   226
    val (rtypes, rsorts) = Drule.types_sorts thm;
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   227
    fun readT (xi, s) =
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   228
      let
59754
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   229
        val S = (case rsorts xi of SOME S => S | NONE => absent xi);
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   230
        val T = Syntax.read_typ ctxt' s;
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   231
        val U = TVar (xi, S);
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   232
      in
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   233
        if Sign.typ_instance thy (T, U) then (U, T)
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   234
        else error ("Instantiation of " ^ Term.string_of_vname xi ^ " fails")
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   235
      end;
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   236
    val Tinsts_env = map readT Tinsts;
25333
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   237
59754
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   238
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   239
    (* preprocess rule: extract vars and their types, apply Tinsts *)
25333
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   240
59754
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   241
    fun get_typ xi =
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   242
      (case rtypes xi of
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   243
        SOME T => typ_subst_atomic Tinsts_env T
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   244
      | NONE => absent xi);
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   245
    val (xis, ss) = Library.split_list tinsts;
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   246
    val Ts = map get_typ xis;
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   247
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   248
    val (ts, envT) =
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   249
      read_termTs (Proof_Context.set_mode Proof_Context.mode_schematic ctxt') ss Ts;
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   250
    val envT' =
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   251
      map (fn (xi, T) => (TVar (xi, the (rsorts xi)), T)) envT @ Tinsts_env;
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   252
    val cenv =
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   253
      map (fn (xi, t) => apply2 (Thm.cterm_of ctxt') (Var (xi, fastype_of t), t))
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   254
        (distinct
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   255
          (fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2)
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   256
          (xis ~~ ts));
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   257
25333
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   258
59754
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   259
    (* lift and instantiate rule *)
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   260
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   261
    val maxidx = Thm.maxidx_of st;
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   262
    val paramTs = map #2 params;
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   263
    val inc = maxidx + 1;
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   264
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   265
    fun lift_var (Var ((a, j), T)) = Var ((a, j + inc), paramTs ---> Logic.incr_tvar inc T)
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   266
      | lift_var t = raise TERM ("Variable expected", [t]);
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   267
    fun lift_term t =
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   268
      fold_rev absfree (param_names ~~ paramTs) (Logic.incr_indexes (paramTs, inc) t);
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   269
    fun lift_inst (cv, ct) = (cterm_fun lift_var cv, cterm_fun lift_term ct);
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   270
    val lift_tvar = apply2 (Thm.ctyp_of ctxt' o Logic.incr_tvar inc);
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   271
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   272
    val rule =
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   273
      Drule.instantiate_normalize
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   274
        (map lift_tvar envT', map lift_inst cenv)
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   275
        (Thm.lift_rule cgoal thm);
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   276
  in
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   277
    compose_tac ctxt' (bires_flag, rule, Thm.nprems_of thm) i
696d87036f04 misc tuning;
wenzelm
parents: 59623
diff changeset
   278
  end) i st;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   279
27120
b21eec437295 added (e)res_inst_tac;
wenzelm
parents: 26463
diff changeset
   280
val res_inst_tac = bires_inst_tac false;
b21eec437295 added (e)res_inst_tac;
wenzelm
parents: 26463
diff changeset
   281
val eres_inst_tac = bires_inst_tac true;
b21eec437295 added (e)res_inst_tac;
wenzelm
parents: 26463
diff changeset
   282
b21eec437295 added (e)res_inst_tac;
wenzelm
parents: 26463
diff changeset
   283
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
   284
(* 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
   285
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58027
diff changeset
   286
fun make_elim_preserve ctxt rl =
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
   287
  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
   288
    val maxidx = Thm.maxidx_of rl;
59623
920889b0788e clarified context;
wenzelm
parents: 59621
diff changeset
   289
    fun cvar xi = Thm.cterm_of ctxt (Var (xi, propT));
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
   290
    val revcut_rl' =
43333
2bdec7f430d3 renamed Drule.instantiate to Drule.instantiate_normalize to emphasize its meaning as opposed to plain Thm.instantiate;
wenzelm
parents: 42806
diff changeset
   291
      Drule.instantiate_normalize ([], [(cvar ("V", 0), cvar ("V", maxidx + 1)),
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
   292
        (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
   293
  in
52223
5bb6ae8acb87 tuned signature -- more explicit flags for low-level Thm.bicompose;
wenzelm
parents: 46476
diff changeset
   294
    (case Seq.list_of
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58027
diff changeset
   295
      (Thm.bicompose (SOME ctxt) {flatten = true, match = false, incremented = false}
52223
5bb6ae8acb87 tuned signature -- more explicit flags for low-level Thm.bicompose;
wenzelm
parents: 46476
diff changeset
   296
        (false, rl, Thm.nprems_of rl) 1 revcut_rl')
5bb6ae8acb87 tuned signature -- more explicit flags for low-level Thm.bicompose;
wenzelm
parents: 46476
diff changeset
   297
     of
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
   298
      [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
   299
    | _ => 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
   300
  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
   301
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   302
(*instantiate and cut -- for atomic fact*)
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58027
diff changeset
   303
fun cut_inst_tac ctxt insts rule = res_inst_tac ctxt insts (make_elim_preserve ctxt rule);
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
   304
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   305
(*forward tactic applies a rule to an assumption without deleting it*)
58963
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58956
diff changeset
   306
fun forw_inst_tac ctxt insts rule = cut_inst_tac ctxt insts rule THEN' assume_tac ctxt;
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
   307
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   308
(*dresolve tactic applies a rule to replace an assumption*)
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58027
diff changeset
   309
fun dres_inst_tac ctxt insts rule = eres_inst_tac ctxt insts (make_elim_preserve ctxt rule);
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
   310
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   311
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   312
(* 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
   313
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   314
(*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
   315
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
   316
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   317
(*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
   318
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
   319
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   320
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   321
53708
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
   322
(* method wrapper *)
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
   323
30545
8209a7196389 export method parser;
wenzelm
parents: 30528
diff changeset
   324
fun method inst_tac tac =
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   325
  Args.goal_spec --
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   326
  Scan.optional (Scan.lift
55111
5792f5106c40 tuned signature;
wenzelm
parents: 53708
diff changeset
   327
    (Parse.and_list1 (Args.var -- (Args.$$$ "=" |-- Parse.!!! Args.name_inner_syntax)) --|
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 33368
diff changeset
   328
      Args.$$$ "in")) [] --
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   329
  Attrib.thms >>
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   330
  (fn ((quant, insts), thms) => fn ctxt => METHOD (fn facts =>
30551
24e156db414c method parser: pass proper context;
wenzelm
parents: 30545
diff changeset
   331
    if null insts then quant (Method.insert_tac facts THEN' tac ctxt thms)
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   332
    else
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   333
      (case thms of
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   334
        [thm] => quant (Method.insert_tac facts THEN' inst_tac ctxt insts thm)
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   335
      | _ => error "Cannot have instantiations with multiple rules")));
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
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   338
(* setup *)
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   339
53708
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
   340
(*warning: rule_tac etc. refer to dynamic subgoal context!*)
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
   341
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 52732
diff changeset
   342
val _ = Theory.setup
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   343
 (Method.setup @{binding rule_tac} (method res_inst_tac resolve_tac)
53708
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
   344
    "apply rule (dynamic instantiation)" #>
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   345
  Method.setup @{binding erule_tac} (method eres_inst_tac eresolve_tac)
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   346
    "apply rule in elimination manner (dynamic instantiation)" #>
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   347
  Method.setup @{binding drule_tac} (method dres_inst_tac dresolve_tac)
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   348
    "apply rule in destruct manner (dynamic instantiation)" #>
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   349
  Method.setup @{binding frule_tac} (method forw_inst_tac forward_tac)
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   350
    "apply rule in forward manner (dynamic instantiation)" #>
53708
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
   351
  Method.setup @{binding cut_tac} (method cut_inst_tac (K cut_rules_tac))
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
   352
    "cut rule (dynamic instantiation)" #>
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
   353
  Method.setup @{binding subgoal_tac}
55111
5792f5106c40 tuned signature;
wenzelm
parents: 53708
diff changeset
   354
    (Args.goal_spec -- Scan.lift (Scan.repeat1 Args.name_inner_syntax) >>
46461
7524f3ac737c eliminated unused subgoals_tac;
wenzelm
parents: 45613
diff changeset
   355
      (fn (quant, props) => fn ctxt =>
7524f3ac737c eliminated unused subgoals_tac;
wenzelm
parents: 45613
diff changeset
   356
        SIMPLE_METHOD'' quant (EVERY' (map (subgoal_tac ctxt) props))))
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   357
    "insert subgoal (dynamic instantiation)" #>
53708
92aa282841f8 more antiquotations;
wenzelm
parents: 53707
diff changeset
   358
  Method.setup @{binding thin_tac}
55111
5792f5106c40 tuned signature;
wenzelm
parents: 53708
diff changeset
   359
    (Args.goal_spec -- Scan.lift Args.name_inner_syntax >>
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   360
      (fn (quant, prop) => fn ctxt => SIMPLE_METHOD'' quant (thin_tac ctxt prop)))
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 52732
diff changeset
   361
      "remove premise (dynamic instantiation)");
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   362
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   363
end;
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   364
42806
4b660cdab9b7 modernized structure Rule_Insts;
wenzelm
parents: 42360
diff changeset
   365
structure Basic_Rule_Insts: BASIC_RULE_INSTS = Rule_Insts;
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 33368
diff changeset
   366
open Basic_Rule_Insts;