1 (* Title: Pure/Isar/attrib.ML
3 Author: Markus Wenzel, TU Muenchen
5 Symbolic representation of attributes -- with name and syntax.
8 signature BASIC_ATTRIB =
10 val print_attributes: theory -> unit
17 exception ATTRIB_FAIL of (string * Position.T) * exn
18 val intern: theory -> xstring -> string
19 val intern_src: theory -> src -> src
20 val pretty_attribs: Proof.context -> src list -> Pretty.T list
21 val attribute: theory -> src -> attribute
22 val attribute_i: theory -> src -> attribute
23 val map_specs: ('a -> 'att) ->
24 (('c * 'a list) * 'd) list -> (('c * 'att list) * 'd) list
25 val map_facts: ('a -> 'att) ->
26 (('c * 'a list) * ('d * 'a list) list) list ->
27 (('c * 'att list) * ('d * 'att list) list) list
28 val crude_closure: Proof.context -> src -> src
29 val add_attributes: (bstring * (src -> attribute) * string) list -> theory -> theory
30 val thm: Context.generic * Args.T list -> thm * (Context.generic * Args.T list)
31 val thms: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
32 val multi_thm: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
33 val syntax: (Context.generic * Args.T list ->
34 attribute * (Context.generic * Args.T list)) -> src -> attribute
35 val no_args: attribute -> src -> attribute
36 val add_del_args: attribute -> attribute -> src -> attribute
37 val internal: (morphism -> attribute) -> src
40 structure Attrib: ATTRIB =
47 (** named attributes **)
51 structure AttributesData = TheoryDataFun
53 type T = (((src -> attribute) * string) * stamp) NameSpace.table;
54 val empty = NameSpace.empty_table;
57 fun merge _ tables = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUPS dups =>
58 error ("Attempt to merge different versions of attribute(s) " ^ commas_quote dups);
61 fun print_attributes thy =
63 val attribs = AttributesData.get thy;
64 fun prt_attr (name, ((_, comment), _)) = Pretty.block
65 [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
67 [Pretty.big_list "attributes:" (map prt_attr (NameSpace.extern_table attribs))]
68 |> Pretty.chunks |> Pretty.writeln
74 val intern = NameSpace.intern o #1 o AttributesData.get;
75 val intern_src = Args.map_name o intern;
77 val extern = NameSpace.extern o #1 o AttributesData.get o ProofContext.theory_of;
82 fun pretty_attribs _ [] = []
83 | pretty_attribs ctxt srcs =
84 [Pretty.enclose "[" "]"
85 (Pretty.commas (map (Args.pretty_src ctxt o Args.map_name (extern ctxt)) srcs))];
90 exception ATTRIB_FAIL of (string * Position.T) * exn;
94 val attrs = #2 (AttributesData.get thy);
96 let val ((name, _), pos) = Args.dest_src src in
97 (case Symtab.lookup attrs name of
98 NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
99 | SOME ((att, _), _) => transform_failure (curry ATTRIB_FAIL (name, pos)) (att src))
103 fun attribute thy = attribute_i thy o intern_src thy;
106 (* attributed declarations *)
108 fun map_specs f = map (apfst (apsnd (map f)));
109 fun map_facts f = map (apfst (apsnd (map f)) o apsnd (map (apsnd (map f))));
114 (*Produce closure without knowing facts in advance! The following
115 works reasonably well for attribute parsers that do not peek at the
118 fun crude_closure ctxt src =
119 (try (fn () => attribute_i (ProofContext.theory_of ctxt) src
120 (Context.Proof ctxt, Drule.asm_rl)) ();
126 fun add_attributes raw_attrs thy =
129 raw_attrs |> map (fn (name, att, comment) => (name, ((att, comment), stamp ())));
130 fun add attrs = NameSpace.extend_table (Sign.naming_of thy) (attrs, new_attrs)
131 handle Symtab.DUPS dups =>
132 error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
133 in AttributesData.map add thy end;
137 (** attribute parsers **)
141 fun tag x = Scan.lift (Args.name -- Scan.repeat Args.name) x;
148 val get_thms = Context.cases PureThy.get_thms ProofContext.get_thms;
150 val fact_name = Args.internal_fact >> K "<fact>" || Args.name;
152 fun gen_thm pick = Scan.depend (fn st =>
153 (Scan.ahead Args.alt_name -- Args.named_fact (get_thms st o Fact)
154 >> (fn (s, fact) => ("", Fact s, fact)) ||
155 Scan.ahead fact_name -- Args.named_fact (get_thms st o Name) -- Args.thm_sel
156 >> (fn ((name, fact), sel) => (name, NameSelection (name, sel), fact)) ||
157 Scan.ahead fact_name -- Args.named_fact (get_thms st o Name)
158 >> (fn (name, fact) => (name, Name name, fact))) --
159 Args.opt_attribs (intern (Context.theory_of st))
160 >> (fn ((name, thmref, fact), srcs) =>
162 val ths = PureThy.select_thm thmref fact;
163 val atts = map (attribute_i (Context.theory_of st)) srcs;
164 val (st', ths') = foldl_map (Library.apply atts) (st, ths);
165 in (st', pick name ths') end));
169 val thm = gen_thm PureThy.single_thm;
170 val multi_thm = gen_thm (K I);
171 val thms = Scan.repeat multi_thm >> flat;
177 (** attribute syntax **)
179 fun syntax scan src (st, th) =
180 let val (f, st') = Args.syntax "attribute" scan src st
183 fun no_args x = syntax (Scan.succeed x);
185 fun add_del_args add del x = syntax
186 (Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add)) x;
190 (** basic attributes **)
194 fun tagged x = syntax (tag >> PureThy.tag) x;
195 fun untagged x = syntax (Scan.lift Args.name >> PureThy.untag) x;
197 fun kind x = syntax (Scan.lift Args.name >> PureThy.kind) x;
200 (* rule composition *)
203 syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
204 >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B))));
207 syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
208 >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B))));
211 syntax (thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A)));
216 fun rename_abs src = syntax
217 (Scan.lift (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars'))) src;
220 (* unfold / fold definitions *)
222 fun unfolded_syntax rule =
224 (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths)));
226 val unfolded = unfolded_syntax LocalDefs.unfold;
227 val folded = unfolded_syntax LocalDefs.fold;
232 fun consumes x = syntax (Scan.lift (Scan.optional Args.nat 1) >> RuleCases.consumes) x;
233 fun case_names x = syntax (Scan.lift (Scan.repeat1 Args.name) >> RuleCases.case_names) x;
234 fun case_conclusion x =
235 syntax (Scan.lift (Args.name -- Scan.repeat Args.name) >> RuleCases.case_conclusion) x;
236 fun params x = syntax (Args.and_list1 (Scan.lift (Scan.repeat Args.name)) >> RuleCases.params) x;
241 fun rule_format_att x = syntax (Args.mode "no_asm"
242 >> (fn true => ObjectLogic.rule_format_no_asm | false => ObjectLogic.rule_format)) x;
244 fun elim_format x = no_args (Thm.rule_attribute (K Tactic.make_elim)) x;
249 fun standard x = no_args (Thm.rule_attribute (K Drule.standard)) x;
251 fun no_vars x = no_args (Thm.rule_attribute (fn ctxt => fn th =>
252 let val ((_, [th']), _) = Variable.import_thms true [th] (Context.proof_of ctxt)
256 no_args (Thm.rule_attribute (K (Conv.fconv_rule Drule.eta_long_conversion))) x;
259 (* internal attribute *)
261 fun internal att = Args.src (("Pure.attribute", [Args.mk_attribute att]), Position.none);
264 syntax (Scan.lift Args.internal_attribute >> Morphism.form) x;
269 val _ = Context.add_setup
271 [("tagged", tagged, "tagged theorem"),
272 ("untagged", untagged, "untagged theorem"),
273 ("kind", kind, "theorem kind"),
274 ("COMP", COMP_att, "direct composition with rules (no lifting)"),
275 ("THEN", THEN_att, "resolution with rule"),
276 ("OF", OF_att, "rule applied to facts"),
277 ("rename_abs", rename_abs, "rename bound variables in abstractions"),
278 ("unfolded", unfolded, "unfolded definitions"),
279 ("folded", folded, "folded definitions"),
280 ("standard", standard, "result put into standard form"),
281 ("elim_format", elim_format, "destruct rule turned into elimination rule format"),
282 ("no_vars", no_vars, "frozen schematic vars"),
283 ("eta_long", eta_long, "put theorem into eta long beta normal form"),
284 ("consumes", consumes, "number of consumed facts"),
285 ("case_names", case_names, "named rule cases"),
286 ("case_conclusion", case_conclusion, "named conclusion of rule cases"),
287 ("params", params, "named rule parameters"),
288 ("atomize", no_args ObjectLogic.declare_atomize, "declaration of atomize rule"),
289 ("rulify", no_args ObjectLogic.declare_rulify, "declaration of rulify rule"),
290 ("rule_format", rule_format_att, "result put into standard rule format"),
291 ("defn", add_del_args LocalDefs.defn_add LocalDefs.defn_del,
292 "declaration of definitional transformations"),
293 ("attribute", internal_att, "internal attribute")]);
297 structure BasicAttrib: BASIC_ATTRIB = Attrib;