1 (* Title: Pure/Isar/rule_insts.ML
4 Rule instantiations -- operations within a rule/subgoal context.
7 signature BASIC_RULE_INSTS =
9 val read_instantiate: Proof.context -> (indexname * string) list -> thm -> thm
10 val instantiate_tac: Proof.context -> (indexname * string) list -> tactic
11 val res_inst_tac: Proof.context -> (indexname * string) list -> thm -> int -> tactic
12 val eres_inst_tac: Proof.context -> (indexname * string) list -> thm -> int -> tactic
13 val cut_inst_tac: Proof.context -> (indexname * string) list -> thm -> int -> tactic
14 val forw_inst_tac: Proof.context -> (indexname * string) list -> thm -> int -> tactic
15 val dres_inst_tac: Proof.context -> (indexname * string) list -> thm -> int -> tactic
16 val thin_tac: Proof.context -> string -> int -> tactic
17 val subgoal_tac: Proof.context -> string -> int -> tactic
18 val subgoals_tac: Proof.context -> string list -> int -> tactic
21 signature RULE_INSTS =
23 include BASIC_RULE_INSTS
24 val make_elim_preserve: thm -> thm
27 structure RuleInsts: RULE_INSTS =
30 structure T = OuterLex;
31 structure P = OuterParse;
34 (** reading instantiations **)
38 fun is_tvar (x, _) = String.isPrefix "'" x;
40 fun error_var msg xi = error (msg ^ Term.string_of_vname xi);
42 fun the_sort tvars xi = the (AList.lookup (op =) tvars xi)
43 handle Option.Option => error_var "No such type variable in theorem: " xi;
45 fun the_type vars xi = the (AList.lookup (op =) vars xi)
46 handle Option.Option => error_var "No such variable in theorem: " xi;
48 fun unify_vartypes thy vars (xi, u) (unifier, maxidx) =
50 val T = the_type vars xi;
51 val U = Term.fastype_of u;
52 val maxidx' = Term.maxidx_term u (Int.max (#2 xi, maxidx));
54 Sign.typ_unify thy (T, U) (unifier, maxidx')
55 handle Type.TUNIFY => error_var "Incompatible type for instantiation of " xi
58 fun instantiate inst =
59 TermSubst.instantiate ([], map (fn (xi, t) => ((xi, Term.fastype_of t), t)) inst) #>
66 in if T = T' then NONE else SOME (T, T') end;
72 in if t aconv t' then NONE else SOME (t, t') end;
75 (Thm.fold_terms o fold_types o fold_atyps)
76 (fn TFree (a, _) => insert (op =) a
77 | TVar ((a, _), _) => insert (op =) a
82 fun read_termTs ctxt schematic ss Ts =
84 fun parse T = if T = propT then Syntax.parse_prop ctxt else Syntax.parse_term ctxt;
85 val ts = map2 parse Ts ss;
87 map2 (TypeInfer.constrain o TypeInfer.paramify_vars) Ts ts
88 |> Syntax.check_terms ((schematic ? ProofContext.set_mode ProofContext.mode_schematic) ctxt)
89 |> Variable.polymorphic ctxt;
90 val Ts' = map Term.fastype_of ts';
91 val tyenv = fold Type.raw_match (Ts ~~ Ts') Vartab.empty;
92 in (ts', map (apsnd snd) (Vartab.dest tyenv)) end;
94 fun read_insts ctxt mixed_insts (tvars, vars) =
96 val thy = ProofContext.theory_of ctxt;
97 val cert = Thm.cterm_of thy;
98 val certT = Thm.ctyp_of thy;
100 val (type_insts, term_insts) = List.partition (is_tvar o fst) mixed_insts;
101 val internal_insts = term_insts |> map_filter
102 (fn (xi, T.Term t) => SOME (xi, t)
103 | (_, T.Text _) => NONE
104 | (xi, _) => error_var "Term argument expected for " xi);
105 val external_insts = term_insts |> map_filter
106 (fn (xi, T.Text s) => SOME (xi, s) | _ => NONE);
109 (* mixed type instantiations *)
111 fun readT (xi, arg) =
113 val S = the_sort tvars xi;
116 T.Text s => Syntax.read_typ ctxt s
118 | _ => error_var "Type argument expected for " xi);
120 if Sign.of_sort thy (T, S) then ((xi, S), T)
121 else error_var "Incompatible sort for typ instantiation of " xi
124 val type_insts1 = map readT type_insts;
125 val instT1 = TermSubst.instantiateT type_insts1;
126 val vars1 = map (apsnd instT1) vars;
129 (* internal term instantiations *)
131 val instT2 = Envir.norm_type
132 (#1 (fold (unify_vartypes thy vars1) internal_insts (Vartab.empty, 0)));
133 val vars2 = map (apsnd instT2) vars1;
134 val internal_insts2 = map (apsnd (map_types instT2)) internal_insts;
135 val inst2 = instantiate internal_insts2;
138 (* external term instantiations *)
140 val (xs, strs) = split_list external_insts;
141 val Ts = map (the_type vars2) xs;
142 val (ts, inferred) = read_termTs ctxt false strs Ts;
144 val instT3 = Term.typ_subst_TVars inferred;
145 val vars3 = map (apsnd instT3) vars2;
146 val internal_insts3 = map (apsnd (map_types instT3)) internal_insts2;
147 val external_insts3 = xs ~~ ts;
148 val inst3 = instantiate external_insts3;
153 val type_insts3 = map (fn ((a, _), T) => (a, instT3 (instT2 T))) type_insts1;
154 val term_insts3 = internal_insts3 @ external_insts3;
156 val inst_tvars = map_filter (make_instT (instT3 o instT2 o instT1)) tvars;
157 val inst_vars = map_filter (make_inst (inst3 o inst2)) vars3;
159 ((type_insts3, term_insts3),
160 (map (pairself certT) inst_tvars, map (pairself cert) inst_vars))
163 fun read_instantiate_mixed ctxt mixed_insts thm =
165 val ctxt' = ctxt |> Variable.declare_thm thm
166 |> fold (fn a => Variable.declare_names (Logic.mk_type (TFree (a, dummyS)))) (add_used thm []); (* FIXME tmp *)
167 val tvars = Thm.fold_terms Term.add_tvars thm [];
168 val vars = Thm.fold_terms Term.add_vars thm [];
169 val ((type_insts, term_insts), insts) = read_insts ctxt' (map snd mixed_insts) (tvars, vars);
171 val _ = (*assign internalized values*)
172 mixed_insts |> List.app (fn (arg, (xi, _)) =>
174 T.assign (SOME (T.Typ (the (AList.lookup (op =) type_insts xi)))) arg
176 T.assign (SOME (T.Term (the (AList.lookup (op =) term_insts xi)))) arg);
178 Drule.instantiate insts thm |> RuleCases.save thm
181 fun read_instantiate_mixed' ctxt (args, concl_args) thm =
183 fun zip_vars _ [] = []
184 | zip_vars (_ :: xs) ((_, NONE) :: rest) = zip_vars xs rest
185 | zip_vars ((x, _) :: xs) ((arg, SOME t) :: rest) = (arg, (x, t)) :: zip_vars xs rest
186 | zip_vars [] _ = error "More instantiations than variables in theorem";
188 zip_vars (rev (Term.add_vars (Thm.full_prop_of thm) [])) args @
189 zip_vars (rev (Term.add_vars (Thm.concl_of thm) [])) concl_args;
190 in read_instantiate_mixed ctxt insts thm end;
195 (* instantiation of rule or goal state *)
197 fun read_instantiate ctxt args thm =
198 read_instantiate_mixed (ctxt |> ProofContext.set_mode ProofContext.mode_schematic) (* FIXME !? *)
199 (map (fn (x, y) => (T.eof, (x, T.Text y))) args) thm;
201 fun instantiate_tac ctxt args = PRIMITIVE (read_instantiate ctxt args);
207 (* where: named instantiation *)
212 Args.internal_typ >> T.Typ ||
213 Args.internal_term >> T.Term ||
214 Args.name_source >> T.Text;
216 val inst = Args.var -- (Args.$$$ "=" |-- Scan.ahead P.not_eof -- value)
217 >> (fn (xi, (a, v)) => (a, (xi, v)));
221 val where_att = Attrib.syntax (Scan.lift (P.and_list inst) >> (fn args =>
222 Thm.rule_attribute (fn context => read_instantiate_mixed (Context.proof_of context) args)));
227 (* of: positional instantiation (terms only) *)
232 Args.internal_term >> T.Term ||
233 Args.name_source >> T.Text;
235 val inst = Scan.ahead P.not_eof -- Args.maybe value;
236 val concl = Args.$$$ "concl" -- Args.colon;
239 Scan.repeat (Scan.unless concl inst) --
240 Scan.optional (concl |-- Scan.repeat inst) [];
244 val of_att = Attrib.syntax (Scan.lift insts >> (fn args =>
245 Thm.rule_attribute (fn context => read_instantiate_mixed' (Context.proof_of context) args)));
252 val _ = Context.>> (Context.map_theory
253 (Attrib.add_attributes
254 [("where", where_att, "named instantiation of theorem"),
255 ("of", of_att, "positional instantiation of theorem")]));
261 (* resolution after lifting and instantation; may refer to parameters of the subgoal *)
263 (* FIXME cleanup this mess!!! *)
265 fun bires_inst_tac bires_flag ctxt insts thm =
267 val thy = ProofContext.theory_of ctxt;
268 (* Separate type and term insts *)
269 fun has_type_var ((x, _), _) = (case Symbol.explode x of
270 "'"::cs => true | cs => false);
271 val Tinsts = List.filter has_type_var insts;
272 val tinsts = filter_out has_type_var insts;
277 val (_, _, Bi, _) = Thm.dest_state (st, i);
278 val params = Logic.strip_params Bi; (*params of subgoal i as string typ pairs*)
279 val params = rev (Term.rename_wrt_term Bi params)
280 (*as they are printed: bound variables with*)
281 (*the same name are renamed during printing*)
283 val (param_names, ctxt') = ctxt
284 |> Variable.declare_thm thm
285 |> Thm.fold_terms Variable.declare_constraints st
286 |> ProofContext.add_fixes_i (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) params);
288 (* Process type insts: Tinsts_env *)
289 fun absent xi = error
290 ("No such variable in theorem: " ^ Term.string_of_vname xi);
291 val (rtypes, rsorts) = Drule.types_sorts thm;
293 let val S = case rsorts xi of SOME S => S | NONE => absent xi;
294 val T = Syntax.read_typ ctxt' s;
295 val U = TVar (xi, S);
296 in if Sign.typ_instance thy (T, U) then (U, T)
297 else error ("Instantiation of " ^ Term.string_of_vname xi ^ " fails")
299 val Tinsts_env = map readT Tinsts;
300 (* Preprocess rule: extract vars and their types, apply Tinsts *)
303 SOME T => typ_subst_atomic Tinsts_env T
304 | NONE => absent xi);
305 val (xis, ss) = Library.split_list tinsts;
306 val Ts = map get_typ xis;
308 val (ts, envT) = read_termTs ctxt' true ss Ts;
309 val envT' = map (fn (ixn, T) =>
310 (TVar (ixn, the (rsorts ixn)), T)) envT @ Tinsts_env;
314 pairself (Thm.cterm_of thy) (Var (xi, fastype_of t), t))
316 (fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2)
318 (* Lift and instantiate rule *)
319 val {maxidx, ...} = rep_thm st;
320 val paramTs = map #2 params
322 fun liftvar (Var ((a,j), T)) =
323 Var((a, j+inc), paramTs ---> Logic.incr_tvar inc T)
324 | liftvar t = raise TERM("Variable expected", [t]);
325 fun liftterm t = list_abs_free
326 (param_names ~~ paramTs, Logic.incr_indexes(paramTs,inc) t)
327 fun liftpair (cv,ct) =
328 (cterm_fun liftvar cv, cterm_fun liftterm ct)
329 val lifttvar = pairself (ctyp_of thy o Logic.incr_tvar inc);
330 val rule = Drule.instantiate
331 (map lifttvar envT', map liftpair cenv)
332 (Thm.lift_rule (Thm.cprem_of st i) thm)
334 if i > nprems_of st then no_tac st
336 compose_tac (bires_flag, rule, nprems_of thm) i
338 handle TERM (msg,_) => (warning msg; no_tac st)
339 | THM (msg,_,_) => (warning msg; no_tac st);
342 val res_inst_tac = bires_inst_tac false;
343 val eres_inst_tac = bires_inst_tac true;
346 (* forward resolution *)
348 fun make_elim_preserve rl =
350 val cert = Thm.cterm_of (Thm.theory_of_thm rl);
351 val maxidx = Thm.maxidx_of rl;
352 fun cvar xi = cert (Var (xi, propT));
354 instantiate ([], [(cvar ("V", 0), cvar ("V", maxidx + 1)),
355 (cvar ("W", 0), cvar ("W", maxidx + 1))]) Drule.revcut_rl;
357 (case Seq.list_of (bicompose false (false, rl, Thm.nprems_of rl) 1 revcut_rl') of
359 | _ => raise THM ("make_elim_preserve", 1, [rl]))
362 (*instantiate and cut -- for atomic fact*)
363 fun cut_inst_tac ctxt insts rule = res_inst_tac ctxt insts (make_elim_preserve rule);
365 (*forward tactic applies a rule to an assumption without deleting it*)
366 fun forw_inst_tac ctxt insts rule = cut_inst_tac ctxt insts rule THEN' assume_tac;
368 (*dresolve tactic applies a rule to replace an assumption*)
369 fun dres_inst_tac ctxt insts rule = eres_inst_tac ctxt insts (make_elim_preserve rule);
372 (* derived tactics *)
374 (*deletion of an assumption*)
375 fun thin_tac ctxt s = eres_inst_tac ctxt [(("V", 0), s)] Drule.thin_rl;
377 (*Introduce the given proposition as lemma and subgoal*)
378 fun subgoal_tac ctxt A = DETERM o res_inst_tac ctxt [(("psi", 0), A)] cut_rl;
379 fun subgoals_tac ctxt As = EVERY' (map (subgoal_tac ctxt) As);
385 (* rule_tac etc. -- refer to dynamic goal state! *)
389 fun gen_inst _ tac _ (quant, ([], thms)) =
390 METHOD (fn facts => quant (Method.insert_tac facts THEN' tac thms))
391 | gen_inst inst_tac _ ctxt (quant, (insts, [thm])) =
393 quant (Method.insert_tac facts THEN' inst_tac ctxt insts thm))
394 | gen_inst _ _ _ _ = error "Cannot have instantiations with multiple rules";
398 val res_inst_meth = gen_inst res_inst_tac Tactic.resolve_tac;
399 val eres_inst_meth = gen_inst eres_inst_tac Tactic.eresolve_tac;
400 val cut_inst_meth = gen_inst cut_inst_tac Tactic.cut_rules_tac;
401 val dres_inst_meth = gen_inst dres_inst_tac Tactic.dresolve_tac;
402 val forw_inst_meth = gen_inst forw_inst_tac Tactic.forward_tac;
410 Scan.optional (Scan.lift
411 (P.and_list1 (Args.name -- (Args.$$$ "=" |-- P.!!! Args.name_source)) --| Args.$$$ "in")) []
414 fun inst_args f src ctxt =
415 f ctxt (fst (Method.syntax (Args.goal_spec HEADGOAL -- insts) src ctxt));
418 Scan.optional (Scan.lift
419 (P.and_list1 (Args.var -- (Args.$$$ "=" |-- P.!!! Args.name_source)) --| Args.$$$ "in")) []
422 fun inst_args_var f src ctxt =
423 f ctxt (fst (Method.syntax (Args.goal_spec HEADGOAL -- insts_var) src ctxt));
428 val _ = Context.>> (Context.map_theory
430 [("rule_tac", inst_args_var res_inst_meth,
431 "apply rule (dynamic instantiation)"),
432 ("erule_tac", inst_args_var eres_inst_meth,
433 "apply rule in elimination manner (dynamic instantiation)"),
434 ("drule_tac", inst_args_var dres_inst_meth,
435 "apply rule in destruct manner (dynamic instantiation)"),
436 ("frule_tac", inst_args_var forw_inst_meth,
437 "apply rule in forward manner (dynamic instantiation)"),
438 ("cut_tac", inst_args_var cut_inst_meth,
439 "cut rule (dynamic instantiation)"),
440 ("subgoal_tac", Method.goal_args_ctxt (Scan.repeat1 Args.name_source) subgoals_tac,
441 "insert subgoal (dynamic instantiation)"),
442 ("thin_tac", Method.goal_args_ctxt Args.name_source thin_tac,
443 "remove premise (dynamic instantiation)")]));
447 structure BasicRuleInsts: BASIC_RULE_INSTS = RuleInsts;