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