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