author | wenzelm |
Fri, 17 Mar 2000 16:28:59 +0100 | |
changeset 8496 | 7e4a466b18d5 |
parent 8368 | bdc3ee0d8cb6 |
child 8633 | 427ead639d8a |
permissions | -rw-r--r-- |
5823 | 1 |
(* Title: Pure/Isar/attrib.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
5 |
Symbolic theorem attributes. |
|
6 |
*) |
|
7 |
||
8 |
signature BASIC_ATTRIB = |
|
9 |
sig |
|
10 |
val print_attributes: theory -> unit |
|
5879 | 11 |
val Attribute: bstring -> (Args.src -> theory attribute) * (Args.src -> Proof.context attribute) |
12 |
-> string -> unit |
|
5823 | 13 |
end; |
14 |
||
15 |
signature ATTRIB = |
|
16 |
sig |
|
17 |
include BASIC_ATTRIB |
|
7611 | 18 |
val help_attributes: theory option -> unit |
5912 | 19 |
exception ATTRIB_FAIL of (string * Position.T) * exn |
5823 | 20 |
val global_attribute: theory -> Args.src -> theory attribute |
21 |
val local_attribute: theory -> Args.src -> Proof.context attribute |
|
5912 | 22 |
val local_attribute': Proof.context -> Args.src -> Proof.context attribute |
7673
b8e7fa177d62
added undef_global_attribute, undef_local_attribute;
wenzelm
parents:
7668
diff
changeset
|
23 |
val undef_global_attribute: theory attribute |
b8e7fa177d62
added undef_global_attribute, undef_local_attribute;
wenzelm
parents:
7668
diff
changeset
|
24 |
val undef_local_attribute: Proof.context attribute |
5823 | 25 |
val add_attributes: (bstring * ((Args.src -> theory attribute) * |
26 |
(Args.src -> Proof.context attribute)) * string) list -> theory -> theory |
|
6091 | 27 |
val global_thm: theory * Args.T list -> thm * (theory * Args.T list) |
28 |
val global_thms: theory * Args.T list -> thm list * (theory * Args.T list) |
|
29 |
val global_thmss: theory * Args.T list -> thm list * (theory * Args.T list) |
|
30 |
val local_thm: Proof.context * Args.T list -> thm * (Proof.context * Args.T list) |
|
31 |
val local_thms: Proof.context * Args.T list -> thm list * (Proof.context * Args.T list) |
|
32 |
val local_thmss: Proof.context * Args.T list -> thm list * (Proof.context * Args.T list) |
|
5879 | 33 |
val syntax: ('a * Args.T list -> 'a attribute * ('a * Args.T list)) -> Args.src -> 'a attribute |
5823 | 34 |
val no_args: 'a attribute -> Args.src -> 'a attribute |
35 |
val setup: (theory -> theory) list |
|
36 |
end; |
|
37 |
||
38 |
structure Attrib: ATTRIB = |
|
39 |
struct |
|
40 |
||
41 |
||
42 |
(** attributes theory data **) |
|
43 |
||
44 |
(* data kind 'Isar/attributes' *) |
|
45 |
||
46 |
structure AttributesDataArgs = |
|
47 |
struct |
|
48 |
val name = "Isar/attributes"; |
|
49 |
type T = |
|
50 |
{space: NameSpace.T, |
|
51 |
attrs: |
|
52 |
((((Args.src -> theory attribute) * (Args.src -> Proof.context attribute)) |
|
53 |
* string) * stamp) Symtab.table}; |
|
54 |
||
55 |
val empty = {space = NameSpace.empty, attrs = Symtab.empty}; |
|
6546 | 56 |
val copy = I; |
5823 | 57 |
val prep_ext = I; |
58 |
||
59 |
fun merge ({space = space1, attrs = attrs1}, {space = space2, attrs = attrs2}) = |
|
60 |
{space = NameSpace.merge (space1, space2), |
|
61 |
attrs = Symtab.merge eq_snd (attrs1, attrs2) handle Symtab.DUPS dups => |
|
62 |
error ("Attempt to merge different versions of attributes " ^ commas_quote dups)}; |
|
63 |
||
7367 | 64 |
fun print_atts verbose ({space, attrs}) = |
5823 | 65 |
let |
66 |
fun prt_attr (name, ((_, comment), _)) = Pretty.block |
|
6846 | 67 |
[Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; |
5823 | 68 |
in |
7367 | 69 |
if not verbose then () |
70 |
else Pretty.writeln (Display.pretty_name_space ("attribute name space", space)); |
|
6846 | 71 |
Pretty.writeln (Pretty.big_list "attributes:" |
72 |
(map prt_attr (NameSpace.cond_extern_table space attrs))) |
|
5823 | 73 |
end; |
7367 | 74 |
|
75 |
fun print _ = print_atts true; |
|
5823 | 76 |
end; |
77 |
||
78 |
structure AttributesData = TheoryDataFun(AttributesDataArgs); |
|
79 |
val print_attributes = AttributesData.print; |
|
7611 | 80 |
|
81 |
fun help_attributes None = writeln "attributes: (unkown theory context)" |
|
82 |
| help_attributes (Some thy) = AttributesDataArgs.print_atts false (AttributesData.get thy); |
|
5823 | 83 |
|
84 |
||
85 |
(* get global / local attributes *) |
|
86 |
||
5912 | 87 |
exception ATTRIB_FAIL of (string * Position.T) * exn; |
88 |
||
5823 | 89 |
fun gen_attribute which thy = |
90 |
let |
|
91 |
val {space, attrs} = AttributesData.get thy; |
|
92 |
||
5879 | 93 |
fun attr src = |
94 |
let |
|
95 |
val ((raw_name, _), pos) = Args.dest_src src; |
|
96 |
val name = NameSpace.intern space raw_name; |
|
97 |
in |
|
5823 | 98 |
(case Symtab.lookup (attrs, name) of |
99 |
None => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos) |
|
5912 | 100 |
| Some ((p, _), _) => transform_failure (curry ATTRIB_FAIL (name, pos)) (which p src)) |
5823 | 101 |
end; |
102 |
in attr end; |
|
103 |
||
104 |
val global_attribute = gen_attribute fst; |
|
105 |
val local_attribute = gen_attribute snd; |
|
5879 | 106 |
val local_attribute' = local_attribute o ProofContext.theory_of; |
5823 | 107 |
|
7673
b8e7fa177d62
added undef_global_attribute, undef_local_attribute;
wenzelm
parents:
7668
diff
changeset
|
108 |
val undef_global_attribute: theory attribute = |
b8e7fa177d62
added undef_global_attribute, undef_local_attribute;
wenzelm
parents:
7668
diff
changeset
|
109 |
fn _ => error "attribute undefined in theory context"; |
b8e7fa177d62
added undef_global_attribute, undef_local_attribute;
wenzelm
parents:
7668
diff
changeset
|
110 |
|
b8e7fa177d62
added undef_global_attribute, undef_local_attribute;
wenzelm
parents:
7668
diff
changeset
|
111 |
val undef_local_attribute: Proof.context attribute = |
b8e7fa177d62
added undef_global_attribute, undef_local_attribute;
wenzelm
parents:
7668
diff
changeset
|
112 |
fn _ => error "attribute undefined in proof context"; |
b8e7fa177d62
added undef_global_attribute, undef_local_attribute;
wenzelm
parents:
7668
diff
changeset
|
113 |
|
5823 | 114 |
|
115 |
(* add_attributes *) |
|
116 |
||
117 |
fun add_attributes raw_attrs thy = |
|
118 |
let |
|
119 |
val full = Sign.full_name (Theory.sign_of thy); |
|
120 |
val new_attrs = |
|
121 |
map (fn (name, (f, g), comment) => (full name, (((f, g), comment), stamp ()))) raw_attrs; |
|
122 |
||
123 |
val {space, attrs} = AttributesData.get thy; |
|
124 |
val space' = NameSpace.extend (space, map fst new_attrs); |
|
125 |
val attrs' = Symtab.extend (attrs, new_attrs) handle Symtab.DUPS dups => |
|
126 |
error ("Duplicate declaration of attributes(s) " ^ commas_quote dups); |
|
127 |
in thy |> AttributesData.put {space = space', attrs = attrs'} end; |
|
128 |
||
5879 | 129 |
(*implicit version*) |
130 |
fun Attribute name att cmt = Context.>> (add_attributes [(name, att, cmt)]); |
|
5823 | 131 |
|
5879 | 132 |
|
133 |
||
134 |
(** attribute parsers **) |
|
135 |
||
136 |
(* tags *) |
|
5823 | 137 |
|
5879 | 138 |
fun tag x = Scan.lift (Args.name -- Scan.repeat Args.name) x; |
139 |
||
140 |
||
141 |
(* theorems *) |
|
142 |
||
143 |
fun gen_thm get attrib app = |
|
144 |
Scan.depend (fn st => Args.name -- Args.opt_attribs >> |
|
145 |
(fn (name, srcs) => app ((st, get st name), map (attrib st) srcs))); |
|
5823 | 146 |
|
6091 | 147 |
val global_thm = gen_thm PureThy.get_thm global_attribute Thm.apply_attributes; |
148 |
val global_thms = gen_thm PureThy.get_thms global_attribute Thm.applys_attributes; |
|
5879 | 149 |
val global_thmss = Scan.repeat global_thms >> flat; |
150 |
||
6091 | 151 |
val local_thm = gen_thm ProofContext.get_thm local_attribute' Thm.apply_attributes; |
152 |
val local_thms = gen_thm ProofContext.get_thms local_attribute' Thm.applys_attributes; |
|
5879 | 153 |
val local_thmss = Scan.repeat local_thms >> flat; |
154 |
||
5823 | 155 |
|
5879 | 156 |
|
157 |
(** attribute syntax **) |
|
5823 | 158 |
|
5879 | 159 |
fun syntax scan src (st, th) = |
8282 | 160 |
let val (st', f) = Args.syntax "attribute" scan src st |
5879 | 161 |
in f (st', th) end; |
162 |
||
163 |
fun no_args x = syntax (Scan.succeed x); |
|
5823 | 164 |
|
165 |
||
166 |
||
167 |
(** Pure attributes **) |
|
168 |
||
169 |
(* tags *) |
|
170 |
||
6091 | 171 |
fun gen_tag x = syntax (tag >> Drule.tag) x; |
8496 | 172 |
fun gen_untag x = syntax (Scan.lift Args.name >> Drule.untag) x; |
5823 | 173 |
|
174 |
||
5879 | 175 |
(* transfer *) |
176 |
||
6091 | 177 |
fun gen_transfer theory_of = no_args (Drule.rule_attribute (fn st => Thm.transfer (theory_of st))); |
5879 | 178 |
|
179 |
val global_transfer = gen_transfer I; |
|
180 |
val local_transfer = gen_transfer ProofContext.theory_of; |
|
181 |
||
182 |
||
6772 | 183 |
(* COMP *) |
184 |
||
6948 | 185 |
fun comp (i, B) (x, A) = (x, Drule.compose_single (A, i, B)); |
6772 | 186 |
|
6948 | 187 |
fun gen_COMP thm = syntax (Scan.lift (Scan.optional Args.nat 1) -- thm >> comp); |
6772 | 188 |
val global_COMP = gen_COMP global_thm; |
189 |
val local_COMP = gen_COMP local_thm; |
|
190 |
||
191 |
||
5879 | 192 |
(* RS *) |
193 |
||
6091 | 194 |
fun resolve (i, B) (x, A) = (x, A RSN (i, B)); |
5879 | 195 |
|
196 |
fun gen_RS thm = syntax (Scan.lift (Scan.optional Args.nat 1) -- thm >> resolve); |
|
197 |
val global_RS = gen_RS global_thm; |
|
198 |
val local_RS = gen_RS local_thm; |
|
199 |
||
200 |
||
201 |
(* APP *) |
|
202 |
||
6091 | 203 |
fun apply Bs (x, A) = (x, Bs MRS A); |
5879 | 204 |
|
205 |
val global_APP = syntax (global_thmss >> apply); |
|
206 |
val local_APP = syntax (local_thmss >> apply); |
|
207 |
||
208 |
||
5912 | 209 |
(* where: named instantiations *) |
5879 | 210 |
|
5912 | 211 |
fun read_instantiate context_of insts x thm = |
5879 | 212 |
let |
213 |
val ctxt = context_of x; |
|
214 |
val sign = ProofContext.sign_of ctxt; |
|
215 |
||
5912 | 216 |
val vars = Drule.vars_of thm; |
5879 | 217 |
fun get_typ xi = |
218 |
(case assoc (vars, xi) of |
|
219 |
Some T => T |
|
220 |
| None => error ("No such variable in theorem: " ^ Syntax.string_of_vname xi)); |
|
221 |
||
5912 | 222 |
val (xs, ss) = Library.split_list insts; |
5879 | 223 |
val Ts = map get_typ xs; |
224 |
||
7668 | 225 |
val (ts, envT) = ProofContext.read_termTs ctxt (ss ~~ Ts); |
5879 | 226 |
val cenvT = map (apsnd (Thm.ctyp_of sign)) envT; |
5912 | 227 |
val cenv = |
228 |
map (fn (xi, t) => pairself (Thm.cterm_of sign) (Var (xi, fastype_of t), t)) |
|
229 |
(gen_distinct (fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2) (xs ~~ ts)); |
|
8164 | 230 |
in Drule.instantiate (cenvT, cenv) thm end; |
5879 | 231 |
|
6448 | 232 |
fun insts x = Args.and_list (Scan.lift (Args.var --| Args.$$$ "=" -- Args.name)) x; |
5879 | 233 |
|
6091 | 234 |
fun gen_where context_of = syntax (insts >> (Drule.rule_attribute o read_instantiate context_of)); |
5823 | 235 |
|
5879 | 236 |
val global_where = gen_where ProofContext.init; |
237 |
val local_where = gen_where I; |
|
238 |
||
239 |
||
5912 | 240 |
(* with: positional instantiations *) |
241 |
||
242 |
fun read_instantiate' context_of (args, concl_args) x thm = |
|
243 |
let |
|
244 |
fun zip_vars _ [] = [] |
|
245 |
| zip_vars (_ :: xs) (None :: opt_ts) = zip_vars xs opt_ts |
|
246 |
| zip_vars ((x, _) :: xs) (Some t :: opt_ts) = (x, t) :: zip_vars xs opt_ts |
|
247 |
| zip_vars [] _ = error "More instantiations than variables in theorem"; |
|
248 |
val insts = |
|
249 |
zip_vars (Drule.vars_of_terms [#prop (Thm.rep_thm thm)]) args @ |
|
250 |
zip_vars (Drule.vars_of_terms [Thm.concl_of thm]) concl_args; |
|
251 |
in read_instantiate context_of insts x thm end; |
|
252 |
||
253 |
val concl = Args.$$$ "concl" -- Args.$$$ ":"; |
|
254 |
val inst_arg = Scan.unless concl (Args.$$$ "_" >> K None || Args.name >> Some); |
|
255 |
val inst_args = Scan.repeat inst_arg; |
|
256 |
fun insts' x = Scan.lift (inst_args -- Scan.optional (concl |-- Args.!!! inst_args) []) x; |
|
257 |
||
6091 | 258 |
fun gen_with context_of = syntax (insts' >> (Drule.rule_attribute o read_instantiate' context_of)); |
5912 | 259 |
|
260 |
val global_with = gen_with ProofContext.init; |
|
261 |
val local_with = gen_with I; |
|
262 |
||
263 |
||
7598 | 264 |
(* unfold / fold definitions *) |
265 |
||
266 |
fun gen_rewrite rew defs (x, thm) = (x, rew defs thm); |
|
267 |
||
268 |
val global_unfold = syntax (global_thmss >> gen_rewrite Tactic.rewrite_rule); |
|
269 |
val local_unfold = syntax (local_thmss >> gen_rewrite Tactic.rewrite_rule); |
|
270 |
val global_fold = syntax (global_thmss >> gen_rewrite Tactic.fold_rule); |
|
271 |
val local_fold = syntax (local_thmss >> gen_rewrite Tactic.fold_rule); |
|
272 |
||
273 |
||
8368 | 274 |
(* rule cases *) |
275 |
||
276 |
fun case_names x = syntax (Scan.lift (Scan.repeat1 Args.name) >> RuleCases.case_names) x; |
|
277 |
fun params x = syntax (Args.and_list1 (Scan.lift (Scan.repeat Args.name)) >> RuleCases.params) x; |
|
278 |
||
279 |
||
5879 | 280 |
(* misc rules *) |
281 |
||
6091 | 282 |
fun standard x = no_args (Drule.rule_attribute (K Drule.standard)) x; |
283 |
fun elimify x = no_args (Drule.rule_attribute (K Tactic.make_elim)) x; |
|
5879 | 284 |
|
6933 | 285 |
fun global_export x = no_args (Drule.rule_attribute (Proof.export_thm o ProofContext.init)) x; |
286 |
fun local_export x = no_args (Drule.rule_attribute Proof.export_thm) x; |
|
287 |
||
5879 | 288 |
|
289 |
||
290 |
(** theory setup **) |
|
5823 | 291 |
|
292 |
(* pure_attributes *) |
|
293 |
||
294 |
val pure_attributes = |
|
295 |
[("tag", (gen_tag, gen_tag), "tag theorem"), |
|
296 |
("untag", (gen_untag, gen_untag), "untag theorem"), |
|
6772 | 297 |
("COMP", (global_COMP, local_COMP), "compose rules (no lifting)"), |
5879 | 298 |
("RS", (global_RS, local_RS), "resolve with rule"), |
6874 | 299 |
("OF", (global_APP, local_APP), "resolve with rule -- apply rule to rules"), |
5912 | 300 |
("where", (global_where, local_where), "named instantiation of theorem"), |
6874 | 301 |
("of", (global_with, local_with), "positional instantiation of theorem -- apply rule to terms"), |
7598 | 302 |
("unfold", (global_unfold, local_unfold), "unfold definitions"), |
303 |
("fold", (global_fold, local_fold), "fold definitions"), |
|
5879 | 304 |
("standard", (standard, standard), "put theorem into standard form"), |
305 |
("elimify", (elimify, elimify), "turn destruct rule into elimination rule"), |
|
8368 | 306 |
("case_names", (case_names, case_names), "name rule cases"), |
307 |
("params", (params, params), "name rule parameters"), |
|
6933 | 308 |
("transfer", (global_transfer, local_transfer), "transfer theorem to this theory"), |
309 |
("export", (global_export, local_export), "export theorem from context")]; |
|
5823 | 310 |
|
311 |
||
5879 | 312 |
(* setup *) |
5823 | 313 |
|
314 |
val setup = [AttributesData.init, add_attributes pure_attributes]; |
|
315 |
||
316 |
||
317 |
end; |
|
318 |
||
319 |
||
320 |
structure BasicAttrib: BASIC_ATTRIB = Attrib; |
|
321 |
open BasicAttrib; |