src/Pure/Isar/attrib.ML
author wenzelm
Tue Mar 03 14:07:43 2009 +0100 (2009-03-03)
changeset 30211 556d1810cdad
parent 30190 479806475f3c
child 30242 aea5d7fa7ef5
permissions -rw-r--r--
Thm.binding;
     1 (*  Title:      Pure/Isar/attrib.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Symbolic representation of attributes -- with name and syntax.
     5 *)
     6 
     7 signature ATTRIB =
     8 sig
     9   type src = Args.src
    10   type binding = binding * src list
    11   val empty_binding: binding
    12   val print_attributes: theory -> unit
    13   val intern: theory -> xstring -> string
    14   val intern_src: theory -> src -> src
    15   val pretty_attribs: Proof.context -> src list -> Pretty.T list
    16   val defined: theory -> string -> bool
    17   val attribute: theory -> src -> attribute
    18   val attribute_i: theory -> src -> attribute
    19   val eval_thms: Proof.context -> (Facts.ref * src list) list -> thm list
    20   val map_specs: ('a -> 'att) ->
    21     (('c * 'a list) * 'd) list -> (('c * 'att list) * 'd) list
    22   val map_facts: ('a -> 'att) ->
    23     (('c * 'a list) * ('d * 'a list) list) list ->
    24     (('c * 'att list) * ('d * 'att list) list) list
    25   val crude_closure: Proof.context -> src -> src
    26   val add_attributes: (bstring * (src -> attribute) * string) list -> theory -> theory
    27   val syntax: (Context.generic * Args.T list ->
    28     attribute * (Context.generic * Args.T list)) -> src -> attribute
    29   val no_args: attribute -> src -> attribute
    30   val add_del_args: attribute -> attribute -> src -> attribute
    31   val thm_sel: Args.T list -> Facts.interval list * Args.T list
    32   val thm: Context.generic * Args.T list -> thm * (Context.generic * Args.T list)
    33   val thms: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
    34   val multi_thm: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
    35   val print_configs: Proof.context -> unit
    36   val internal: (morphism -> attribute) -> src
    37   val register_config: Config.value Config.T -> theory -> theory
    38   val config_bool: bstring -> bool -> bool Config.T * (theory -> theory)
    39   val config_int: bstring -> int -> int Config.T * (theory -> theory)
    40   val config_string: bstring -> string -> string Config.T * (theory -> theory)
    41   val config_bool_global: bstring -> bool -> bool Config.T * (theory -> theory)
    42   val config_int_global: bstring -> int -> int Config.T * (theory -> theory)
    43   val config_string_global: bstring -> string -> string Config.T * (theory -> theory)
    44 end;
    45 
    46 structure Attrib: ATTRIB =
    47 struct
    48 
    49 structure T = OuterLex;
    50 structure P = OuterParse;
    51 
    52 
    53 (* source and bindings *)
    54 
    55 type src = Args.src;
    56 
    57 type binding = binding * src list;
    58 val empty_binding: binding = (Binding.empty, []);
    59 
    60 
    61 
    62 (** named attributes **)
    63 
    64 (* theory data *)
    65 
    66 structure Attributes = TheoryDataFun
    67 (
    68   type T = (((src -> attribute) * string) * stamp) NameSpace.table;
    69   val empty = NameSpace.empty_table;
    70   val copy = I;
    71   val extend = I;
    72   fun merge _ tables : T = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUP dup =>
    73     error ("Attempt to merge different versions of attribute " ^ quote dup);
    74 );
    75 
    76 fun print_attributes thy =
    77   let
    78     val attribs = Attributes.get thy;
    79     fun prt_attr (name, ((_, comment), _)) = Pretty.block
    80       [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
    81   in
    82     [Pretty.big_list "attributes:" (map prt_attr (NameSpace.extern_table attribs))]
    83     |> Pretty.chunks |> Pretty.writeln
    84   end;
    85 
    86 
    87 (* name space *)
    88 
    89 val intern = NameSpace.intern o #1 o Attributes.get;
    90 val intern_src = Args.map_name o intern;
    91 
    92 val extern = NameSpace.extern o #1 o Attributes.get o ProofContext.theory_of;
    93 
    94 
    95 (* pretty printing *)
    96 
    97 fun pretty_attribs _ [] = []
    98   | pretty_attribs ctxt srcs =
    99       [Pretty.enclose "[" "]"
   100         (Pretty.commas (map (Args.pretty_src ctxt o Args.map_name (extern ctxt)) srcs))];
   101 
   102 
   103 (* get attributes *)
   104 
   105 val defined = Symtab.defined o #2 o Attributes.get;
   106 
   107 fun attribute_i thy =
   108   let
   109     val attrs = #2 (Attributes.get thy);
   110     fun attr src =
   111       let val ((name, _), pos) = Args.dest_src src in
   112         (case Symtab.lookup attrs name of
   113           NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
   114         | SOME ((att, _), _) => (Position.report (Markup.attribute name) pos; att src))
   115       end;
   116   in attr end;
   117 
   118 fun attribute thy = attribute_i thy o intern_src thy;
   119 
   120 fun eval_thms ctxt args = ProofContext.note_thmss Thm.theoremK
   121     [(Thm.empty_binding, map (apsnd (map (attribute (ProofContext.theory_of ctxt)))) args)] ctxt
   122   |> fst |> maps snd;
   123 
   124 
   125 (* attributed declarations *)
   126 
   127 fun map_specs f = map (apfst (apsnd (map f)));
   128 fun map_facts f = map (apfst (apsnd (map f)) o apsnd (map (apsnd (map f))));
   129 
   130 
   131 (* crude_closure *)
   132 
   133 (*Produce closure without knowing facts in advance! The following
   134   works reasonably well for attribute parsers that do not peek at the
   135   thm structure.*)
   136 
   137 fun crude_closure ctxt src =
   138  (try (fn () => attribute_i (ProofContext.theory_of ctxt) src
   139     (Context.Proof ctxt, Drule.asm_rl)) ();
   140   Args.closure src);
   141 
   142 
   143 (* add_attributes *)
   144 
   145 fun add_attributes raw_attrs thy =
   146   let
   147     val new_attrs =
   148       raw_attrs |> map (fn (name, att, comment) => (Binding.name name, ((att, comment), stamp ())));
   149     fun add attrs = fold (snd oo NameSpace.bind (Sign.naming_of thy)) new_attrs attrs
   150       handle Symtab.DUP dup => error ("Duplicate declaration of attributes " ^ quote dup);
   151   in Attributes.map add thy end;
   152 
   153 
   154 (* attribute syntax *)
   155 
   156 fun syntax scan src (st, th) =
   157   let val (f: attribute, st') = Args.syntax "attribute" scan src st
   158   in f (st', th) end;
   159 
   160 fun no_args x = syntax (Scan.succeed x);
   161 
   162 fun add_del_args add del = syntax
   163   (Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add));
   164 
   165 
   166 
   167 (** parsing attributed theorems **)
   168 
   169 val thm_sel = P.$$$ "(" |-- P.list1
   170  (P.nat --| P.minus -- P.nat >> Facts.FromTo ||
   171   P.nat --| P.minus >> Facts.From ||
   172   P.nat >> Facts.Single) --| P.$$$ ")";
   173 
   174 local
   175 
   176 val fact_name = Args.internal_fact >> K "<fact>" || Args.name;
   177 
   178 fun gen_thm pick = Scan.depend (fn context =>
   179   let
   180     val thy = Context.theory_of context;
   181     val get = Context.cases (PureThy.get_fact context) ProofContext.get_fact context;
   182     val get_fact = get o Facts.Fact;
   183     fun get_named pos name = get (Facts.Named ((name, pos), NONE));
   184   in
   185     P.$$$ "[" |-- Args.attribs (intern thy) --| P.$$$ "]" >> (fn srcs =>
   186       let
   187         val atts = map (attribute_i thy) srcs;
   188         val (context', th') = Library.apply atts (context, Drule.dummy_thm);
   189       in (context', pick "" [th']) end)
   190     ||
   191     (Scan.ahead Args.alt_name -- Args.named_fact get_fact
   192       >> (fn (s, fact) => ("", Facts.Fact s, fact)) ||
   193      Scan.ahead (P.position fact_name) :|-- (fn (name, pos) =>
   194       Args.named_fact (get_named pos) -- Scan.option thm_sel
   195         >> (fn (fact, sel) => (name, Facts.Named ((name, pos), sel), fact))))
   196     -- Args.opt_attribs (intern thy) >> (fn ((name, thmref, fact), srcs) =>
   197       let
   198         val ths = Facts.select thmref fact;
   199         val atts = map (attribute_i thy) srcs;
   200         val (context', ths') = Library.foldl_map (Library.apply atts) (context, ths);
   201       in (context', pick name ths') end)
   202   end);
   203 
   204 in
   205 
   206 val thm = gen_thm Facts.the_single;
   207 val multi_thm = gen_thm (K I);
   208 val thms = Scan.repeat multi_thm >> flat;
   209 
   210 end;
   211 
   212 
   213 
   214 (** basic attributes **)
   215 
   216 (* internal *)
   217 
   218 fun internal att = Args.src (("Pure.attribute", [T.mk_attribute att]), Position.none);
   219 
   220 val internal_att =
   221   syntax (Scan.lift Args.internal_attribute >> Morphism.form);
   222 
   223 
   224 (* tags *)
   225 
   226 val tagged = syntax (Scan.lift (Args.name -- Args.name) >> Thm.tag);
   227 val untagged = syntax (Scan.lift Args.name >> Thm.untag);
   228 
   229 val kind = syntax (Scan.lift Args.name >> Thm.kind);
   230 
   231 
   232 (* rule composition *)
   233 
   234 val COMP_att =
   235   syntax (Scan.lift (Scan.optional (Args.bracks P.nat) 1) -- thm
   236     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B))));
   237 
   238 val THEN_att =
   239   syntax (Scan.lift (Scan.optional (Args.bracks P.nat) 1) -- thm
   240     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B))));
   241 
   242 val OF_att =
   243   syntax (thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A)));
   244 
   245 
   246 (* rename_abs *)
   247 
   248 val rename_abs = syntax
   249   (Scan.lift (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars')));
   250 
   251 
   252 (* unfold / fold definitions *)
   253 
   254 fun unfolded_syntax rule =
   255   syntax (thms >>
   256     (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths)));
   257 
   258 val unfolded = unfolded_syntax LocalDefs.unfold;
   259 val folded = unfolded_syntax LocalDefs.fold;
   260 
   261 
   262 (* rule cases *)
   263 
   264 val consumes = syntax (Scan.lift (Scan.optional P.nat 1) >> RuleCases.consumes);
   265 val case_names = syntax (Scan.lift (Scan.repeat1 Args.name) >> RuleCases.case_names);
   266 val case_conclusion =
   267   syntax (Scan.lift (Args.name -- Scan.repeat Args.name) >> RuleCases.case_conclusion);
   268 val params = syntax (Scan.lift (P.and_list1 (Scan.repeat Args.name)) >> RuleCases.params);
   269 
   270 
   271 (* rule format *)
   272 
   273 val rule_format = syntax (Args.mode "no_asm"
   274   >> (fn true => ObjectLogic.rule_format_no_asm | false => ObjectLogic.rule_format));
   275 
   276 val elim_format = no_args (Thm.rule_attribute (K Tactic.make_elim));
   277 
   278 
   279 (* misc rules *)
   280 
   281 val standard = no_args (Thm.rule_attribute (K Drule.standard));
   282 
   283 val no_vars = no_args (Thm.rule_attribute (fn context => fn th =>
   284   let
   285     val ctxt = Variable.set_body false (Context.proof_of context);
   286     val ((_, [th']), _) = Variable.import_thms true [th] ctxt;
   287   in th' end));
   288 
   289 val eta_long =
   290   no_args (Thm.rule_attribute (K (Conv.fconv_rule Drule.eta_long_conversion)));
   291 
   292 val rotated = syntax
   293   (Scan.lift (Scan.optional P.int 1) >> (fn n => Thm.rule_attribute (K (rotate_prems n))));
   294 
   295 val abs_def = no_args (Thm.rule_attribute (K Drule.abs_def));
   296 
   297 
   298 (* theory setup *)
   299 
   300 val _ = Context.>> (Context.map_theory
   301  (add_attributes
   302    [("attribute", internal_att, "internal attribute"),
   303     ("tagged", tagged, "tagged theorem"),
   304     ("untagged", untagged, "untagged theorem"),
   305     ("kind", kind, "theorem kind"),
   306     ("COMP", COMP_att, "direct composition with rules (no lifting)"),
   307     ("THEN", THEN_att, "resolution with rule"),
   308     ("OF", OF_att, "rule applied to facts"),
   309     ("rename_abs", rename_abs, "rename bound variables in abstractions"),
   310     ("unfolded", unfolded, "unfolded definitions"),
   311     ("folded", folded, "folded definitions"),
   312     ("standard", standard, "result put into standard form"),
   313     ("elim_format", elim_format, "destruct rule turned into elimination rule format"),
   314     ("no_vars", no_vars, "frozen schematic vars"),
   315     ("eta_long", eta_long, "put theorem into eta long beta normal form"),
   316     ("consumes", consumes, "number of consumed facts"),
   317     ("case_names", case_names, "named rule cases"),
   318     ("case_conclusion", case_conclusion, "named conclusion of rule cases"),
   319     ("params", params, "named rule parameters"),
   320     ("atomize", no_args ObjectLogic.declare_atomize, "declaration of atomize rule"),
   321     ("rulify", no_args ObjectLogic.declare_rulify, "declaration of rulify rule"),
   322     ("rule_format", rule_format, "result put into standard rule format"),
   323     ("rotated", rotated, "rotated theorem premises"),
   324     ("defn", add_del_args LocalDefs.defn_add LocalDefs.defn_del,
   325       "declaration of definitional transformations"),
   326     ("abs_def", abs_def, "abstract over free variables of a definition")]));
   327 
   328 
   329 
   330 (** configuration options **)
   331 
   332 (* naming *)
   333 
   334 structure Configs = TheoryDataFun
   335 (
   336   type T = Config.value Config.T Symtab.table;
   337   val empty = Symtab.empty;
   338   val copy = I;
   339   val extend = I;
   340   fun merge _ = Symtab.merge (K true);
   341 );
   342 
   343 fun print_configs ctxt =
   344   let
   345     val thy = ProofContext.theory_of ctxt;
   346     fun prt (name, config) =
   347       let val value = Config.get ctxt config in
   348         Pretty.block [Pretty.str (name ^ ": " ^ Config.print_type value ^ " ="), Pretty.brk 1,
   349           Pretty.str (Config.print_value value)]
   350       end;
   351     val configs = NameSpace.extern_table (#1 (Attributes.get thy), Configs.get thy);
   352   in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
   353 
   354 
   355 (* concrete syntax *)
   356 
   357 local
   358 
   359 val equals = P.$$$ "=";
   360 
   361 fun scan_value (Config.Bool _) =
   362       equals -- Args.$$$ "false" >> K (Config.Bool false) ||
   363       equals -- Args.$$$ "true" >> K (Config.Bool true) ||
   364       Scan.succeed (Config.Bool true)
   365   | scan_value (Config.Int _) = equals |-- P.int >> Config.Int
   366   | scan_value (Config.String _) = equals |-- Args.name >> Config.String;
   367 
   368 fun scan_config thy config =
   369   let val config_type = Config.get_thy thy config
   370   in scan_value config_type >> (K o Thm.declaration_attribute o K o Config.put_generic config) end;
   371 
   372 in
   373 
   374 fun register_config config thy =
   375   let
   376     val bname = Config.name_of config;
   377     val name = Sign.full_bname thy bname;
   378   in
   379     thy
   380     |> add_attributes [(bname, syntax (Scan.lift (scan_config thy config) >> Morphism.form),
   381       "configuration option")]
   382     |> Configs.map (Symtab.update (name, config))
   383   end;
   384 
   385 fun declare_config make coerce global name default =
   386   let
   387     val config_value = Config.declare global name (make default);
   388     val config = coerce config_value;
   389   in (config, register_config config_value) end;
   390 
   391 val config_bool   = declare_config Config.Bool Config.bool false;
   392 val config_int    = declare_config Config.Int Config.int false;
   393 val config_string = declare_config Config.String Config.string false;
   394 
   395 val config_bool_global   = declare_config Config.Bool Config.bool true;
   396 val config_int_global    = declare_config Config.Int Config.int true;
   397 val config_string_global = declare_config Config.String Config.string true;
   398 
   399 end;
   400 
   401 
   402 (* theory setup *)
   403 
   404 val _ = Context.>> (Context.map_theory
   405  (register_config Unify.trace_bound_value #>
   406   register_config Unify.search_bound_value #>
   407   register_config Unify.trace_simp_value #>
   408   register_config Unify.trace_types_value #>
   409   register_config MetaSimplifier.simp_depth_limit_value));
   410 
   411 end;