author | wenzelm |
Fri, 20 Mar 2015 22:18:40 +0100 | |
changeset 59771 | c6e60787ffe2 |
parent 59770 | dbd41a84ebd3 |
child 59772 | e6f0c361ac73 |
permissions | -rw-r--r-- |
53707 | 1 |
(* Title: Pure/Tools/rule_insts.ML |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
3 |
|
53708 | 4 |
Rule instantiations -- operations within implicit rule / subgoal context. |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
5 |
*) |
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
6 |
|
59763 | 7 |
signature RULE_INSTS = |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
8 |
sig |
59763 | 9 |
val where_rule: Proof.context -> |
10 |
((indexname * Position.T) * string) list -> |
|
11 |
(binding * string option * mixfix) list -> thm -> thm |
|
12 |
val of_rule: Proof.context -> string option list * string option list -> |
|
13 |
(binding * string option * mixfix) list -> thm -> thm |
|
14 |
val read_instantiate: Proof.context -> |
|
15 |
((indexname * Position.T) * string) list -> string list -> thm -> thm |
|
59755 | 16 |
val res_inst_tac: Proof.context -> |
17 |
((indexname * Position.T) * string) list -> thm -> int -> tactic |
|
18 |
val eres_inst_tac: Proof.context -> |
|
19 |
((indexname * Position.T) * string) list -> thm -> int -> tactic |
|
20 |
val cut_inst_tac: Proof.context -> |
|
21 |
((indexname * Position.T) * string) list -> thm -> int -> tactic |
|
22 |
val forw_inst_tac: Proof.context -> |
|
23 |
((indexname * Position.T) * string) list -> thm -> int -> tactic |
|
24 |
val dres_inst_tac: Proof.context -> |
|
25 |
((indexname * Position.T) * 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
|
26 |
val thin_tac: Proof.context -> string -> int -> tactic |
27219 | 27 |
val subgoal_tac: Proof.context -> string -> int -> tactic |
58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58027
diff
changeset
|
28 |
val make_elim_preserve: Proof.context -> thm -> thm |
59755 | 29 |
val method: |
30 |
(Proof.context -> ((indexname * Position.T) * string) list -> thm -> int -> tactic) -> |
|
53708 | 31 |
(Proof.context -> thm list -> int -> tactic) -> (Proof.context -> Proof.method) context_parser |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
32 |
end; |
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
33 |
|
42806 | 34 |
structure Rule_Insts: RULE_INSTS = |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
35 |
struct |
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
36 |
|
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
37 |
(** read instantiations **) |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
38 |
|
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
39 |
local |
59755 | 40 |
|
41 |
fun error_var msg (xi, pos) = |
|
42 |
error (msg ^ quote (Term.string_of_vname xi) ^ Position.here pos); |
|
59754 | 43 |
|
59755 | 44 |
fun the_sort tvars (xi, pos) : sort = |
45611 | 45 |
(case AList.lookup (op =) tvars xi of |
46 |
SOME S => S |
|
59755 | 47 |
| NONE => error_var "No such type variable in theorem: " (xi, pos)); |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
48 |
|
59755 | 49 |
fun the_type vars (xi, pos) : typ = |
45611 | 50 |
(case AList.lookup (op =) vars xi of |
51 |
SOME T => T |
|
59755 | 52 |
| NONE => error_var "No such variable in theorem: " (xi, pos)); |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
53 |
|
59759 | 54 |
fun readT ctxt tvars ((xi, pos), s) = |
55 |
let |
|
56 |
val S = the_sort tvars (xi, pos); |
|
57 |
val T = Syntax.read_typ ctxt s; |
|
58 |
in |
|
59 |
if Sign.of_sort (Proof_Context.theory_of ctxt) (T, S) then ((xi, S), T) |
|
59768 | 60 |
else error_var "Bad sort for instantiation of type variable: " (xi, pos) |
59759 | 61 |
end; |
62 |
||
55156 | 63 |
fun read_termTs ctxt ss Ts = |
25329 | 64 |
let |
65 |
fun parse T = if T = propT then Syntax.parse_prop ctxt else Syntax.parse_term ctxt; |
|
66 |
val ts = map2 parse Ts ss; |
|
67 |
val ts' = |
|
39288 | 68 |
map2 (Type.constraint o Type_Infer.paramify_vars) Ts ts |
55156 | 69 |
|> Syntax.check_terms ctxt |
25329 | 70 |
|> Variable.polymorphic ctxt; |
71 |
val Ts' = map Term.fastype_of ts'; |
|
72 |
val tyenv = fold Type.raw_match (Ts ~~ Ts') Vartab.empty; |
|
59759 | 73 |
val tyenv' = Vartab.fold (fn (xi, (S, T)) => cons ((xi, S), T)) tyenv []; |
74 |
in (ts', tyenv') end; |
|
25329 | 75 |
|
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
76 |
fun instantiate inst = |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
77 |
Term_Subst.instantiate ([], map (fn (xi, t) => ((xi, Term.fastype_of t), t)) inst) #> |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
78 |
Envir.beta_norm; |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
79 |
|
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
80 |
fun make_instT f v = |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
81 |
let |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
82 |
val T = TVar v; |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
83 |
val T' = f T; |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
84 |
in if T = T' then NONE else SOME (v, T') end; |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
85 |
|
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
86 |
fun make_inst f v = |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
87 |
let |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
88 |
val t = Var v; |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
89 |
val t' = f t; |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
90 |
in if t aconv t' then NONE else SOME (v, t') end; |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
91 |
|
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
92 |
in |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
93 |
|
59769 | 94 |
fun read_insts ctxt mixed_insts thm = |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
95 |
let |
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
96 |
val (type_insts, term_insts) = |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
97 |
List.partition (fn (((x, _), _), _) => String.isPrefix "'" x) mixed_insts; |
59768 | 98 |
|
59769 | 99 |
val tvars = Thm.fold_terms Term.add_tvars thm []; |
100 |
val vars = Thm.fold_terms Term.add_vars thm []; |
|
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
101 |
|
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
102 |
(*explicit type instantiations*) |
59759 | 103 |
val instT1 = Term_Subst.instantiateT (map (readT ctxt tvars) type_insts); |
20343
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
104 |
val vars1 = map (apsnd instT1) vars; |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
105 |
|
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
106 |
(*term instantiations*) |
45613
70e5b43535cd
simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset
|
107 |
val (xs, ss) = split_list term_insts; |
70e5b43535cd
simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset
|
108 |
val Ts = map (the_type vars1) xs; |
55156 | 109 |
val (ts, inferred) = read_termTs ctxt ss Ts; |
45613
70e5b43535cd
simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset
|
110 |
|
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
111 |
(*implicit type instantiations*) |
59759 | 112 |
val instT2 = Term_Subst.instantiateT inferred; |
20343
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
113 |
val vars2 = map (apsnd instT2) vars1; |
59755 | 114 |
val inst2 = instantiate (map #1 xs ~~ ts); |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
115 |
|
45613
70e5b43535cd
simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset
|
116 |
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
|
117 |
val inst_vars = map_filter (make_inst inst2) vars2; |
59769 | 118 |
in (inst_tvars, inst_vars) end; |
20343
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
119 |
|
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
120 |
end; |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
121 |
|
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
122 |
|
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
123 |
|
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
124 |
(** forward rules **) |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
125 |
|
55143
04448228381d
explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset
|
126 |
fun where_rule ctxt mixed_insts fixes thm = |
20343
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
127 |
let |
45613
70e5b43535cd
simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset
|
128 |
val ctxt' = ctxt |
55143
04448228381d
explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset
|
129 |
|> Proof_Context.read_vars fixes |-> Proof_Context.add_fixes |> #2 |
58027
dc58ab4d9f44
discontinued odd "temporary" workaround from 2006 (6ac7a4fc32a0), which has no measurable relevance;
wenzelm
parents:
55156
diff
changeset
|
130 |
|> Variable.declare_thm thm; |
59769 | 131 |
val (inst_tvars, inst_vars) = read_insts ctxt' mixed_insts thm; |
20343
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
132 |
in |
59769 | 133 |
thm |
134 |
|> Drule.instantiate_normalize |
|
135 |
(map (apply2 (Thm.ctyp_of ctxt) o apfst TVar) inst_tvars, |
|
136 |
map (apply2 (Thm.cterm_of ctxt) o apfst Var) inst_vars) |
|
55143
04448228381d
explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset
|
137 |
|> singleton (Proof_Context.export ctxt' ctxt) |
45613
70e5b43535cd
simplified read_instantiate -- no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset
|
138 |
|> Rule_Cases.save thm |
20343
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
139 |
end; |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
140 |
|
55143
04448228381d
explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset
|
141 |
fun of_rule ctxt (args, concl_args) fixes thm = |
20343
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
142 |
let |
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
143 |
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
|
144 |
| zip_vars (_ :: xs) (NONE :: rest) = zip_vars xs rest |
59755 | 145 |
| zip_vars ((x, _) :: xs) (SOME t :: rest) = ((x, Position.none), t) :: zip_vars xs rest |
20343
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
146 |
| zip_vars [] _ = error "More instantiations than variables in theorem"; |
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
147 |
val insts = |
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
148 |
zip_vars (rev (Term.add_vars (Thm.full_prop_of thm) [])) args @ |
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
149 |
zip_vars (rev (Term.add_vars (Thm.concl_of thm) [])) concl_args; |
55143
04448228381d
explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset
|
150 |
in where_rule ctxt insts fixes thm end; |
27236 | 151 |
|
55143
04448228381d
explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset
|
152 |
fun read_instantiate ctxt insts xs = |
04448228381d
explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset
|
153 |
where_rule ctxt insts (map (fn x => (Binding.name x, NONE, NoSyn)) xs); |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
154 |
|
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
155 |
|
20343
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
156 |
|
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
157 |
(** attributes **) |
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
158 |
|
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
159 |
(* where: named instantiation *) |
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
160 |
|
53171 | 161 |
val _ = Theory.setup |
53708 | 162 |
(Attrib.setup @{binding "where"} |
55143
04448228381d
explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset
|
163 |
(Scan.lift |
59755 | 164 |
(Parse.and_list (Parse.position Args.var -- (Args.$$$ "=" |-- Args.name_inner_syntax)) |
165 |
-- Parse.for_fixes) >> (fn (insts, fixes) => |
|
166 |
Thm.rule_attribute (fn context => where_rule (Context.proof_of context) insts fixes))) |
|
53171 | 167 |
"named instantiation of theorem"); |
20336
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 |
|
20343
e093a54bf25e
reworked read_instantiate -- separate read_insts;
wenzelm
parents:
20336
diff
changeset
|
170 |
(* of: positional instantiation (terms only) *) |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
171 |
|
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
172 |
local |
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
173 |
|
55111 | 174 |
val inst = Args.maybe Args.name_inner_syntax; |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
175 |
val concl = Args.$$$ "concl" -- Args.colon; |
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 |
val insts = |
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
178 |
Scan.repeat (Scan.unless concl inst) -- |
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
179 |
Scan.optional (concl |-- Scan.repeat inst) []; |
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
180 |
|
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
181 |
in |
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
182 |
|
53171 | 183 |
val _ = Theory.setup |
53708 | 184 |
(Attrib.setup @{binding "of"} |
55143
04448228381d
explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset
|
185 |
(Scan.lift (insts -- Parse.for_fixes) >> (fn (args, fixes) => |
04448228381d
explicit eigen-context for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset
|
186 |
Thm.rule_attribute (fn context => of_rule (Context.proof_of context) args fixes))) |
53171 | 187 |
"positional instantiation of theorem"); |
20336
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 |
end; |
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
190 |
|
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
191 |
|
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
192 |
|
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
|
193 |
(** tactics **) |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
194 |
|
59754 | 195 |
(* resolution after lifting and instantiation; may refer to parameters of the subgoal *) |
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
196 |
|
59754 | 197 |
fun bires_inst_tac bires_flag ctxt mixed_insts thm i st = CSUBGOAL (fn (cgoal, _) => |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
198 |
let |
59759 | 199 |
(* goal context *) |
200 |
||
59754 | 201 |
val goal = Thm.term_of cgoal; |
202 |
val params = |
|
203 |
Logic.strip_params goal |
|
204 |
(*as they are printed: bound variables with the same name are renamed*) |
|
205 |
|> Term.rename_wrt_term goal |
|
206 |
|> rev; |
|
207 |
val (param_names, ctxt') = ctxt |
|
208 |
|> Variable.declare_thm thm |
|
209 |
|> Thm.fold_terms Variable.declare_constraints st |
|
210 |
|> Proof_Context.add_fixes (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) params); |
|
211 |
||
212 |
||
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
213 |
(* lift and instantiate rule *) |
59754 | 214 |
|
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
215 |
val (inst_tvars, inst_vars) = |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
216 |
read_insts (Proof_Context.set_mode Proof_Context.mode_schematic ctxt') |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
217 |
mixed_insts thm; |
59754 | 218 |
|
219 |
val maxidx = Thm.maxidx_of st; |
|
220 |
val paramTs = map #2 params; |
|
221 |
val inc = maxidx + 1; |
|
222 |
||
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
223 |
fun lift_var ((a, j), T) = |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
224 |
Var ((a, j + inc), paramTs ---> Logic.incr_tvar inc T); |
59754 | 225 |
fun lift_term t = |
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
226 |
fold_rev absfree (param_names ~~ paramTs) |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
227 |
(Logic.incr_indexes (paramTs, inc) t); |
59754 | 228 |
|
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
229 |
val inst_tvars' = inst_tvars |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
230 |
|> map (apply2 (Thm.ctyp_of ctxt' o Logic.incr_tvar inc) o apfst TVar); |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
231 |
val inst_vars' = inst_vars |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
232 |
|> map (fn (v, t) => apply2 (Thm.cterm_of ctxt') (lift_var v, lift_term t)); |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
233 |
|
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
234 |
val thm' = |
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
235 |
Drule.instantiate_normalize (inst_tvars', inst_vars') |
59754 | 236 |
(Thm.lift_rule cgoal thm); |
237 |
in |
|
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset
|
238 |
compose_tac ctxt' (bires_flag, thm', Thm.nprems_of thm) i |
59754 | 239 |
end) i st; |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
240 |
|
27120 | 241 |
val res_inst_tac = bires_inst_tac false; |
242 |
val eres_inst_tac = bires_inst_tac true; |
|
243 |
||
244 |
||
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
|
245 |
(* 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
|
246 |
|
58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58027
diff
changeset
|
247 |
fun make_elim_preserve ctxt rl = |
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
248 |
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
|
249 |
val maxidx = Thm.maxidx_of rl; |
59623 | 250 |
fun cvar xi = Thm.cterm_of ctxt (Var (xi, propT)); |
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
251 |
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
|
252 |
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
|
253 |
(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
|
254 |
in |
52223
5bb6ae8acb87
tuned signature -- more explicit flags for low-level Thm.bicompose;
wenzelm
parents:
46476
diff
changeset
|
255 |
(case Seq.list_of |
58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58027
diff
changeset
|
256 |
(Thm.bicompose (SOME ctxt) {flatten = true, match = false, incremented = false} |
52223
5bb6ae8acb87
tuned signature -- more explicit flags for low-level Thm.bicompose;
wenzelm
parents:
46476
diff
changeset
|
257 |
(false, rl, Thm.nprems_of rl) 1 revcut_rl') |
5bb6ae8acb87
tuned signature -- more explicit flags for low-level Thm.bicompose;
wenzelm
parents:
46476
diff
changeset
|
258 |
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
|
259 |
[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
|
260 |
| _ => 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
|
261 |
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
|
262 |
|
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
263 |
(*instantiate and cut -- for atomic fact*) |
58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58027
diff
changeset
|
264 |
fun cut_inst_tac ctxt insts rule = res_inst_tac ctxt insts (make_elim_preserve ctxt rule); |
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
265 |
|
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
266 |
(*forward tactic applies a rule to an assumption without deleting it*) |
58963
26bf09b95dda
proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents:
58956
diff
changeset
|
267 |
fun forw_inst_tac ctxt insts rule = cut_inst_tac ctxt insts rule THEN' assume_tac ctxt; |
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
268 |
|
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
269 |
(*dresolve tactic applies a rule to replace an assumption*) |
58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58027
diff
changeset
|
270 |
fun dres_inst_tac ctxt insts rule = eres_inst_tac ctxt insts (make_elim_preserve ctxt rule); |
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
271 |
|
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
272 |
|
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
273 |
(* 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
|
274 |
|
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
275 |
(*deletion of an assumption*) |
59755 | 276 |
fun thin_tac ctxt s = |
277 |
eres_inst_tac ctxt [((("V", 0), Position.none), s)] Drule.thin_rl; |
|
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
278 |
|
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
279 |
(*Introduce the given proposition as lemma and subgoal*) |
59755 | 280 |
fun subgoal_tac ctxt A = |
281 |
DETERM o res_inst_tac ctxt [((("psi", 0), Position.none), A)] cut_rl; |
|
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
282 |
|
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
283 |
|
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 |
|
53708 | 285 |
(* method wrapper *) |
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset
|
286 |
|
30545 | 287 |
fun method inst_tac tac = |
30515 | 288 |
Args.goal_spec -- |
289 |
Scan.optional (Scan.lift |
|
59755 | 290 |
(Parse.and_list1 (Parse.position Args.var -- (Args.$$$ "=" |-- Parse.!!! Args.name_inner_syntax)) |
291 |
--| Args.$$$ "in")) [] -- |
|
30515 | 292 |
Attrib.thms >> |
293 |
(fn ((quant, insts), thms) => fn ctxt => METHOD (fn facts => |
|
30551 | 294 |
if null insts then quant (Method.insert_tac facts THEN' tac ctxt thms) |
30515 | 295 |
else |
296 |
(case thms of |
|
297 |
[thm] => quant (Method.insert_tac facts THEN' inst_tac ctxt insts thm) |
|
298 |
| _ => error "Cannot have instantiations with multiple rules"))); |
|
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
299 |
|
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
300 |
|
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
301 |
(* setup *) |
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
302 |
|
53708 | 303 |
(*warning: rule_tac etc. refer to dynamic subgoal context!*) |
304 |
||
53171 | 305 |
val _ = Theory.setup |
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59058
diff
changeset
|
306 |
(Method.setup @{binding rule_tac} (method res_inst_tac resolve_tac) |
53708 | 307 |
"apply rule (dynamic instantiation)" #> |
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59058
diff
changeset
|
308 |
Method.setup @{binding erule_tac} (method eres_inst_tac eresolve_tac) |
30515 | 309 |
"apply rule in elimination manner (dynamic instantiation)" #> |
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59058
diff
changeset
|
310 |
Method.setup @{binding drule_tac} (method dres_inst_tac dresolve_tac) |
30515 | 311 |
"apply rule in destruct manner (dynamic instantiation)" #> |
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59058
diff
changeset
|
312 |
Method.setup @{binding frule_tac} (method forw_inst_tac forward_tac) |
30515 | 313 |
"apply rule in forward manner (dynamic instantiation)" #> |
53708 | 314 |
Method.setup @{binding cut_tac} (method cut_inst_tac (K cut_rules_tac)) |
315 |
"cut rule (dynamic instantiation)" #> |
|
316 |
Method.setup @{binding subgoal_tac} |
|
55111 | 317 |
(Args.goal_spec -- Scan.lift (Scan.repeat1 Args.name_inner_syntax) >> |
46461 | 318 |
(fn (quant, props) => fn ctxt => |
319 |
SIMPLE_METHOD'' quant (EVERY' (map (subgoal_tac ctxt) props)))) |
|
30515 | 320 |
"insert subgoal (dynamic instantiation)" #> |
53708 | 321 |
Method.setup @{binding thin_tac} |
55111 | 322 |
(Args.goal_spec -- Scan.lift Args.name_inner_syntax >> |
30515 | 323 |
(fn (quant, prop) => fn ctxt => SIMPLE_METHOD'' quant (thin_tac ctxt prop))) |
53171 | 324 |
"remove premise (dynamic instantiation)"); |
20336
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
325 |
|
aac494583949
Rule instantiations -- operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset
|
326 |
end; |