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