src/Pure/Isar/rule_insts.ML
author wenzelm
Wed, 28 Dec 2011 13:00:51 +0100
changeset 46003 c0fe5e8e4864
parent 45613 70e5b43535cd
child 46461 7524f3ac737c
permissions -rw-r--r--
print case syntax depending on "show_cases" configuration option;
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
42806
4b660cdab9b7 modernized structure Rule_Insts;
wenzelm
parents: 42360
diff changeset
    29
structure Rule_Insts: RULE_INSTS =
20336
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
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    32
(** reading instantiations **)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    33
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    34
local
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    35
45611
wenzelm
parents: 44241
diff changeset
    36
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
    37
45611
wenzelm
parents: 44241
diff changeset
    38
fun the_sort tvars (xi: indexname) =
wenzelm
parents: 44241
diff changeset
    39
  (case AList.lookup (op =) tvars xi of
wenzelm
parents: 44241
diff changeset
    40
    SOME S => S
wenzelm
parents: 44241
diff changeset
    41
  | NONE => error_var "No such type variable in theorem: " 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_type vars (xi: indexname) =
wenzelm
parents: 44241
diff changeset
    44
  (case AList.lookup (op =) vars xi of
wenzelm
parents: 44241
diff changeset
    45
    SOME T => T
wenzelm
parents: 44241
diff changeset
    46
  | NONE => error_var "No such variable in theorem: " xi);
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    47
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    48
fun instantiate inst =
31977
e03059ae2d82 renamed structure TermSubst to Term_Subst;
wenzelm
parents: 31945
diff changeset
    49
  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
    50
  Envir.beta_norm;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    51
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    52
fun make_instT f v =
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    53
  let
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    54
    val T = TVar v;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    55
    val T' = f T;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    56
  in if T = T' then NONE else SOME (T, T') end;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    57
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    58
fun make_inst f v =
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    59
  let
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    60
    val t = Var v;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    61
    val t' = f t;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    62
  in if t aconv t' then NONE else SOME (t, t') end;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    63
27282
432a5baa7546 private add_used (from drule.ML);
wenzelm
parents: 27245
diff changeset
    64
val add_used =
432a5baa7546 private add_used (from drule.ML);
wenzelm
parents: 27245
diff changeset
    65
  (Thm.fold_terms o fold_types o fold_atyps)
432a5baa7546 private add_used (from drule.ML);
wenzelm
parents: 27245
diff changeset
    66
    (fn TFree (a, _) => insert (op =) a
432a5baa7546 private add_used (from drule.ML);
wenzelm
parents: 27245
diff changeset
    67
      | TVar ((a, _), _) => insert (op =) a
432a5baa7546 private add_used (from drule.ML);
wenzelm
parents: 27245
diff changeset
    68
      | _ => I);
432a5baa7546 private add_used (from drule.ML);
wenzelm
parents: 27245
diff changeset
    69
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    70
in
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    71
25333
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
    72
fun read_termTs ctxt schematic ss Ts =
25329
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    73
  let
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    74
    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
    75
    val ts = map2 parse Ts ss;
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    76
    val ts' =
39288
f1ae2493d93f eliminated aliases of Type.constraint;
wenzelm
parents: 37145
diff changeset
    77
      map2 (Type.constraint o Type_Infer.paramify_vars) Ts ts
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 39288
diff changeset
    78
      |> Syntax.check_terms ((schematic ? Proof_Context.set_mode Proof_Context.mode_schematic) ctxt)
25329
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    79
      |> Variable.polymorphic ctxt;
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    80
    val Ts' = map Term.fastype_of ts';
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    81
    val tyenv = fold Type.raw_match (Ts ~~ Ts') Vartab.empty;
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    82
  in (ts', map (apsnd snd) (Vartab.dest tyenv)) end;
63e8de11c8e9 attribute where/of: proper Syntax.parse/check;
wenzelm
parents: 22692
diff changeset
    83
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    84
fun read_insts ctxt mixed_insts (tvars, vars) =
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    85
  let
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 39288
diff changeset
    86
    val thy = Proof_Context.theory_of ctxt;
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    87
    val cert = Thm.cterm_of thy;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    88
    val certT = Thm.ctyp_of thy;
20336
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
    val (type_insts, term_insts) = List.partition (String.isPrefix "'" o fst o fst) mixed_insts;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    91
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    92
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
    93
    (* type instantiations *)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    94
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
    95
    fun readT (xi, s) =
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    96
      let
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
    97
        val S = the_sort tvars xi;
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
    98
        val T = Syntax.read_typ ctxt s;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
    99
      in
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   100
        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
   101
        else error_var "Incompatible sort for typ instantiation of " xi
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   102
      end;
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   103
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   104
    val instT1 = Term_Subst.instantiateT (map readT type_insts);
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   105
    val vars1 = map (apsnd instT1) vars;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   106
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   107
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   108
    (* term instantiations *)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   109
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   110
    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
   111
    val Ts = map (the_type vars1) xs;
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   112
    val (ts, inferred) = read_termTs ctxt false ss Ts;
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   113
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   114
    val instT2 = Term.typ_subst_TVars inferred;
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   115
    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
   116
    val inst2 = instantiate (xs ~~ ts);
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   117
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   118
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   119
    (* result *)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   120
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   121
    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
   122
    val inst_vars = map_filter (make_inst inst2) vars2;
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   123
  in
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   124
    (map (pairself certT) inst_tvars, map (pairself cert) inst_vars)
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   125
  end;
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   126
27236
80356567e7ad added read_instantiate;
wenzelm
parents: 27219
diff changeset
   127
fun read_instantiate_mixed ctxt mixed_insts thm =
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   128
  let
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   129
    val ctxt' = ctxt
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   130
      |> Variable.declare_thm thm
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   131
      |> fold (fn a => Variable.declare_names (Logic.mk_type (TFree (a, dummyS)))) (add_used thm []);  (* FIXME !? *)
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22681
diff changeset
   132
    val tvars = Thm.fold_terms Term.add_tvars thm [];
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22681
diff changeset
   133
    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
   134
    val insts = read_insts ctxt' mixed_insts (tvars, vars);
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   135
  in
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   136
    Drule.instantiate_normalize insts thm
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   137
    |> Rule_Cases.save thm
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   138
  end;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   139
27236
80356567e7ad added read_instantiate;
wenzelm
parents: 27219
diff changeset
   140
fun read_instantiate_mixed' ctxt (args, concl_args) thm =
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   141
  let
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   142
    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
   143
      | 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
   144
      | 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
   145
      | zip_vars [] _ = error "More instantiations than variables in theorem";
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   146
    val insts =
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   147
      zip_vars (rev (Term.add_vars (Thm.full_prop_of thm) [])) args @
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   148
      zip_vars (rev (Term.add_vars (Thm.concl_of thm) [])) concl_args;
27236
80356567e7ad added read_instantiate;
wenzelm
parents: 27219
diff changeset
   149
  in read_instantiate_mixed ctxt insts thm end;
80356567e7ad added read_instantiate;
wenzelm
parents: 27219
diff changeset
   150
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
   151
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
   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
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
(* 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
   155
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   156
fun read_instantiate ctxt =
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   157
  read_instantiate_mixed (ctxt |> Proof_Context.set_mode Proof_Context.mode_schematic);  (* FIXME !? *)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   158
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
   159
fun instantiate_tac ctxt args = PRIMITIVE (read_instantiate ctxt args);
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
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
   167
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
   168
  (Attrib.setup (Binding.name "where")
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   169
    (Scan.lift (Parse.and_list (Args.var -- (Args.$$$ "=" |-- Args.name_source))) >>
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   170
      (fn args =>
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   171
        Thm.rule_attribute (fn context => read_instantiate_mixed (Context.proof_of context) args)))
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
   172
    "named instantiation of theorem"));
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   173
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   174
20343
e093a54bf25e reworked read_instantiate -- separate read_insts;
wenzelm
parents: 20336
diff changeset
   175
(* of: positional instantiation (terms only) *)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   176
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   177
local
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   178
45613
70e5b43535cd simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents: 45611
diff changeset
   179
val inst = Args.maybe Args.name_source;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   180
val concl = Args.$$$ "concl" -- Args.colon;
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   181
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   182
val insts =
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   183
  Scan.repeat (Scan.unless concl inst) --
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   184
  Scan.optional (concl |-- Scan.repeat inst) [];
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   185
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   186
in
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   187
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
   188
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
   189
  (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
   190
    (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
   191
      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
   192
    "positional instantiation of theorem"));
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   193
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   194
end;
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   195
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
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
   198
(** tactics **)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   199
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
   200
(* 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
   201
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
(* FIXME cleanup this mess!!! *)
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   203
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   204
fun bires_inst_tac bires_flag ctxt insts thm =
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   205
  let
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 39288
diff changeset
   206
    val thy = Proof_Context.theory_of ctxt;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   207
    (* Separate type and term insts *)
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 31977
diff changeset
   208
    fun has_type_var ((x, _), _) =
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 31977
diff changeset
   209
      (case Symbol.explode x of "'" :: _ => true | _ => false);
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 32784
diff changeset
   210
    val Tinsts = filter has_type_var insts;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   211
    val tinsts = filter_out has_type_var insts;
25333
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   212
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   213
    (* Tactic *)
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   214
    fun tac i st =
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   215
      let
25333
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   216
        val (_, _, Bi, _) = Thm.dest_state (st, i);
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   217
        val params = Logic.strip_params Bi;  (*params of subgoal i as string typ pairs*)
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   218
        val params = rev (Term.rename_wrt_term Bi params)
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   219
          (*as they are printed: bound variables with*)
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   220
          (*the same name are renamed during printing*)
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   221
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   222
        val (param_names, ctxt') = ctxt
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   223
          |> Variable.declare_thm thm
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   224
          |> Thm.fold_terms Variable.declare_constraints st
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 39288
diff changeset
   225
          |> Proof_Context.add_fixes (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) params);
25333
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   226
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   227
        (* Process type insts: Tinsts_env *)
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   228
        fun absent xi = error
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   229
              ("No such variable in theorem: " ^ Term.string_of_vname xi);
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   230
        val (rtypes, rsorts) = Drule.types_sorts thm;
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   231
        fun readT (xi, s) =
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   232
            let val S = case rsorts xi of SOME S => S | NONE => absent xi;
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   233
                val T = Syntax.read_typ ctxt' s;
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   234
                val U = TVar (xi, S);
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   235
            in if Sign.typ_instance thy (T, U) then (U, T)
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   236
               else error ("Instantiation of " ^ Term.string_of_vname xi ^ " fails")
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   237
            end;
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   238
        val Tinsts_env = map readT Tinsts;
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   239
        (* Preprocess rule: extract vars and their types, apply Tinsts *)
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   240
        fun get_typ xi =
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   241
          (case rtypes xi of
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   242
               SOME T => typ_subst_atomic Tinsts_env T
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   243
             | NONE => absent xi);
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   244
        val (xis, ss) = Library.split_list tinsts;
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   245
        val Ts = map get_typ xis;
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   246
0c509c33cfb7 Syntax.read_typ;
wenzelm
parents: 25329
diff changeset
   247
        val (ts, envT) = read_termTs ctxt' true ss Ts;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   248
        val envT' = map (fn (ixn, T) =>
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   249
          (TVar (ixn, the (rsorts ixn)), T)) envT @ Tinsts_env;
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   250
        val cenv =
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   251
          map
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   252
            (fn (xi, t) =>
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   253
              pairself (Thm.cterm_of thy) (Var (xi, fastype_of t), t))
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   254
            (distinct
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   255
              (fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2)
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   256
              (xis ~~ ts));
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   257
        (* Lift and instantiate rule *)
44058
ae85c5d64913 misc tuning -- eliminated old-fashioned rep_thm;
wenzelm
parents: 43333
diff changeset
   258
        val maxidx = Thm.maxidx_of st;
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   259
        val paramTs = map #2 params
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   260
        and inc = maxidx+1
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   261
        fun liftvar (Var ((a,j), T)) =
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   262
              Var((a, j+inc), paramTs ---> Logic.incr_tvar inc T)
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   263
          | liftvar t = raise TERM("Variable expected", [t]);
44241
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 44058
diff changeset
   264
        fun liftterm t =
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 44058
diff changeset
   265
          fold_rev absfree (param_names ~~ paramTs) (Logic.incr_indexes (paramTs, inc) t);
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 44058
diff changeset
   266
        fun liftpair (cv, ct) = (cterm_fun liftvar cv, cterm_fun liftterm ct);
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   267
        val lifttvar = pairself (ctyp_of thy o Logic.incr_tvar inc);
43333
2bdec7f430d3 renamed Drule.instantiate to Drule.instantiate_normalize to emphasize its meaning as opposed to plain Thm.instantiate;
wenzelm
parents: 42806
diff changeset
   268
        val rule = Drule.instantiate_normalize
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   269
              (map lifttvar envT', map liftpair cenv)
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   270
              (Thm.lift_rule (Thm.cprem_of st i) thm)
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   271
      in
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   272
        if i > nprems_of st then no_tac st
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   273
        else st |>
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   274
          compose_tac (bires_flag, rule, nprems_of thm) i
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   275
      end
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   276
           handle TERM (msg,_)   => (warning msg; no_tac st)
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   277
                | THM  (msg,_,_) => (warning msg; no_tac st);
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   278
  in tac end;
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
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   286
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
   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 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
   289
    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
   290
    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
   291
    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
   292
      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
   293
        (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
   294
  in
31945
d5f186aa0bed structure Thm: less pervasive names;
wenzelm
parents: 30763
diff changeset
   295
    (case Seq.list_of (Thm.bicompose false (false, rl, Thm.nprems_of rl) 1 revcut_rl') 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
   296
      [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
   297
    | _ => 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
   298
  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
   299
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
(*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
   301
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
   302
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   303
(*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
   304
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
   305
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   306
(*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
   307
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
   308
817d34377170 added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents: 27236
diff changeset
   309
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
(* 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
   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
(*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
   313
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
   314
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
(*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
   316
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
   317
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
   318
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
(** 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
   322
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
(* 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
   324
30545
8209a7196389 export method parser;
wenzelm
parents: 30528
diff changeset
   325
fun method inst_tac tac =
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   326
  Args.goal_spec --
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   327
  Scan.optional (Scan.lift
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 33368
diff changeset
   328
    (Parse.and_list1 (Args.var -- (Args.$$$ "=" |-- Parse.!!! Args.name_source)) --|
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 33368
diff changeset
   329
      Args.$$$ "in")) [] --
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   330
  Attrib.thms >>
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   331
  (fn ((quant, insts), thms) => fn ctxt => METHOD (fn facts =>
30551
24e156db414c method parser: pass proper context;
wenzelm
parents: 30545
diff changeset
   332
    if null insts then quant (Method.insert_tac facts THEN' tac ctxt thms)
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   333
    else
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   334
      (case thms of
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   335
        [thm] => quant (Method.insert_tac facts THEN' inst_tac ctxt insts thm)
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   336
      | _ => error "Cannot have instantiations with multiple rules")));
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   337
30551
24e156db414c method parser: pass proper context;
wenzelm
parents: 30545
diff changeset
   338
val res_inst_meth = method res_inst_tac (K Tactic.resolve_tac);
24e156db414c method parser: pass proper context;
wenzelm
parents: 30545
diff changeset
   339
val eres_inst_meth = method eres_inst_tac (K Tactic.eresolve_tac);
24e156db414c method parser: pass proper context;
wenzelm
parents: 30545
diff changeset
   340
val cut_inst_meth = method cut_inst_tac (K Tactic.cut_rules_tac);
24e156db414c method parser: pass proper context;
wenzelm
parents: 30545
diff changeset
   341
val dres_inst_meth = method dres_inst_tac (K Tactic.dresolve_tac);
24e156db414c method parser: pass proper context;
wenzelm
parents: 30545
diff changeset
   342
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
   343
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   344
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   345
(* setup *)
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   346
26463
9283b4185fdf Context.>> : operate on Context.generic;
wenzelm
parents: 26435
diff changeset
   347
val _ = Context.>> (Context.map_theory
30515
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   348
 (Method.setup (Binding.name "rule_tac") res_inst_meth "apply rule (dynamic instantiation)" #>
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   349
  Method.setup (Binding.name "erule_tac") eres_inst_meth
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   350
    "apply rule in elimination manner (dynamic instantiation)" #>
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   351
  Method.setup (Binding.name "drule_tac") dres_inst_meth
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   352
    "apply rule in destruct manner (dynamic instantiation)" #>
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   353
  Method.setup (Binding.name "frule_tac") forw_inst_meth
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   354
    "apply rule in forward manner (dynamic instantiation)" #>
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   355
  Method.setup (Binding.name "cut_tac") cut_inst_meth "cut rule (dynamic instantiation)" #>
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   356
  Method.setup (Binding.name "subgoal_tac")
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   357
    (Args.goal_spec -- Scan.lift (Scan.repeat1 Args.name_source) >>
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   358
      (fn (quant, props) => fn ctxt => SIMPLE_METHOD'' quant (subgoals_tac ctxt props)))
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   359
    "insert subgoal (dynamic instantiation)" #>
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   360
  Method.setup (Binding.name "thin_tac")
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   361
    (Args.goal_spec -- Scan.lift Args.name_source >>
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   362
      (fn (quant, prop) => fn ctxt => SIMPLE_METHOD'' quant (thin_tac ctxt prop)))
bca05b17b618 simplified method setup;
wenzelm
parents: 30510
diff changeset
   363
      "remove premise (dynamic instantiation)"));
20336
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   364
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   365
end;
aac494583949 Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff changeset
   366
42806
4b660cdab9b7 modernized structure Rule_Insts;
wenzelm
parents: 42360
diff changeset
   367
structure Basic_Rule_Insts: BASIC_RULE_INSTS = Rule_Insts;
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 33368
diff changeset
   368
open Basic_Rule_Insts;