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