src/Pure/Isar/attrib.ML
author wenzelm
Sat Mar 08 21:31:12 2014 +0100 (2014-03-08)
changeset 55998 f5f9fad3321c
parent 55997 9dc5ce83202c
child 56025 d74fed45fa8b
permissions -rw-r--r--
keep current context visibility for PIDE markup and completion (in contrast to 8e3e004f1c31): Attrib.check_src of 9dc5ce83202c should intern/report attributes once, which happens for local_theory in the (visible) auxiliary context;
     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 is_empty_binding: binding -> bool
    13   val print_attributes: Proof.context -> unit
    14   val check_name_generic: Context.generic -> xstring * Position.T -> string
    15   val check_src_generic: Context.generic -> src -> src
    16   val check_name: Proof.context -> xstring * Position.T -> string
    17   val check_src: Proof.context -> src -> src
    18   val pretty_attribs: Proof.context -> src list -> Pretty.T list
    19   val attribute: Proof.context -> src -> attribute
    20   val attribute_global: theory -> src -> attribute
    21   val attribute_cmd: Proof.context -> src -> attribute
    22   val attribute_cmd_global: theory -> src -> attribute
    23   val map_specs: ('a list -> 'att list) ->
    24     (('c * 'a list) * 'b) list -> (('c * 'att list) * 'b) list
    25   val map_facts: ('a list -> 'att list) ->
    26     (('c * 'a list) * ('d * 'a list) list) list ->
    27     (('c * 'att list) * ('d * 'att list) list) list
    28   val map_facts_refs: ('a list -> 'att list) -> ('b -> 'fact) ->
    29     (('c * 'a list) * ('b * 'a list) list) list ->
    30     (('c * 'att list) * ('fact * 'att list) list) list
    31   val global_notes: string -> (binding * (thm list * src list) list) list ->
    32     theory -> (string * thm list) list * theory
    33   val local_notes: string -> (binding * (thm list * src list) list) list ->
    34     Proof.context -> (string * thm list) list * Proof.context
    35   val generic_notes: string -> (binding * (thm list * src list) list) list ->
    36     Context.generic -> (string * thm list) list * Context.generic
    37   val eval_thms: Proof.context -> (Facts.ref * src list) list -> thm list
    38   val setup: Binding.binding -> attribute context_parser -> string -> theory -> theory
    39   val attribute_setup: bstring * Position.T -> Symbol_Pos.source -> string -> theory -> theory
    40   val internal: (morphism -> attribute) -> src
    41   val add_del: attribute -> attribute -> attribute context_parser
    42   val thm_sel: Facts.interval list parser
    43   val thm: thm context_parser
    44   val thms: thm list context_parser
    45   val multi_thm: thm list context_parser
    46   val partial_evaluation: Proof.context ->
    47     (binding * (thm list * Args.src list) list) list ->
    48     (binding * (thm list * Args.src list) list) list
    49   val print_options: Proof.context -> unit
    50   val config_bool: Binding.binding ->
    51     (Context.generic -> bool) -> bool Config.T * (theory -> theory)
    52   val config_int: Binding.binding ->
    53     (Context.generic -> int) -> int Config.T * (theory -> theory)
    54   val config_real: Binding.binding ->
    55     (Context.generic -> real) -> real Config.T * (theory -> theory)
    56   val config_string: Binding.binding ->
    57     (Context.generic -> string) -> string Config.T * (theory -> theory)
    58   val setup_config_bool: Binding.binding -> (Context.generic -> bool) -> bool Config.T
    59   val setup_config_int: Binding.binding -> (Context.generic -> int) -> int Config.T
    60   val setup_config_real: Binding.binding -> (Context.generic -> real) -> real Config.T
    61   val setup_config_string: Binding.binding -> (Context.generic -> string) -> string Config.T
    62   val option_bool: string -> bool Config.T * (theory -> theory)
    63   val option_int: string -> int Config.T * (theory -> theory)
    64   val option_real: string -> real Config.T * (theory -> theory)
    65   val option_string: string -> string Config.T * (theory -> theory)
    66   val setup_option_bool: string -> bool Config.T
    67   val setup_option_int: string -> int Config.T
    68   val setup_option_real: string -> real Config.T
    69   val setup_option_string: string -> string Config.T
    70 end;
    71 
    72 structure Attrib: ATTRIB =
    73 struct
    74 
    75 (* source and bindings *)
    76 
    77 type src = Args.src;
    78 
    79 type binding = binding * src list;
    80 
    81 val empty_binding: binding = (Binding.empty, []);
    82 fun is_empty_binding ((b, srcs): binding) = Binding.is_empty b andalso null srcs;
    83 
    84 
    85 
    86 (** named attributes **)
    87 
    88 (* theory data *)
    89 
    90 structure Attributes = Theory_Data
    91 (
    92   type T = ((src -> attribute) * string) Name_Space.table;
    93   val empty : T = Name_Space.empty_table "attribute";
    94   val extend = I;
    95   fun merge data : T = Name_Space.merge_tables data;
    96 );
    97 
    98 val get_attributes = Attributes.get o Context.theory_of;
    99 
   100 fun print_attributes ctxt =
   101   let
   102     val attribs = get_attributes (Context.Proof ctxt);
   103     fun prt_attr (name, (_, "")) = Pretty.mark_str name
   104       | prt_attr (name, (_, comment)) =
   105           Pretty.block
   106             (Pretty.mark_str name :: Pretty.str ":" :: Pretty.brk 2 :: Pretty.text comment);
   107   in
   108     [Pretty.big_list "attributes:" (map prt_attr (Name_Space.extern_table ctxt attribs))]
   109     |> Pretty.chunks |> Pretty.writeln
   110   end;
   111 
   112 fun add_attribute name att comment thy = thy
   113   |> Attributes.map (Name_Space.define (Context.Theory thy) true (name, (att, comment)) #> snd);
   114 
   115 
   116 (* check *)
   117 
   118 fun check_name_generic context = #1 o Name_Space.check context (get_attributes context);
   119 
   120 fun check_src_generic context src =
   121   let
   122     val ((xname, toks), pos) = Args.dest_src src;
   123     val name = check_name_generic context (xname, pos);
   124   in Args.src ((name, toks), pos) end;
   125 
   126 val check_name = check_name_generic o Context.Proof;
   127 val check_src = check_src_generic o Context.Proof;
   128 
   129 
   130 (* pretty printing *)
   131 
   132 fun extern ctxt = Name_Space.extern ctxt (#1 (get_attributes (Context.Proof ctxt)));
   133 
   134 fun pretty_attribs _ [] = []
   135   | pretty_attribs ctxt srcs =
   136       [Pretty.enum "," "[" "]" (map (Args.pretty_src ctxt o Args.map_name (extern ctxt)) srcs)];
   137 
   138 
   139 (* get attributes *)
   140 
   141 fun attribute_generic context =
   142   let val (_, tab) = get_attributes context in
   143     fn src =>
   144       let val ((name, _), pos) = Args.dest_src src in
   145         (case Symtab.lookup tab name of
   146           NONE => error ("Undefined attribute: " ^ quote name ^ Position.here pos)
   147         | SOME (att, _) => att src)
   148       end
   149   end;
   150 
   151 val attribute = attribute_generic o Context.Proof;
   152 val attribute_global = attribute_generic o Context.Theory;
   153 
   154 fun attribute_cmd ctxt = attribute ctxt o check_src ctxt;
   155 fun attribute_cmd_global thy = attribute_global thy o check_src_generic (Context.Theory thy);
   156 
   157 
   158 (* attributed declarations *)
   159 
   160 fun map_specs f = map (apfst (apsnd f));
   161 
   162 fun map_facts f = map (apfst (apsnd f) o apsnd (map (apsnd f)));
   163 fun map_facts_refs f g = map_facts f #> map (apsnd (map (apfst g)));
   164 
   165 
   166 (* fact expressions *)
   167 
   168 fun global_notes kind facts thy = thy |>
   169   Global_Theory.note_thmss kind (map_facts (map (attribute_global thy)) facts);
   170 
   171 fun local_notes kind facts ctxt = ctxt |>
   172   Proof_Context.note_thmss kind (map_facts (map (attribute ctxt)) facts);
   173 
   174 fun generic_notes kind facts context = context |>
   175   Context.mapping_result (global_notes kind facts) (local_notes kind facts);
   176 
   177 fun eval_thms ctxt srcs = ctxt
   178   |> Proof_Context.note_thmss ""
   179     (map_facts_refs (map (attribute_cmd ctxt)) (Proof_Context.get_fact ctxt)
   180       [((Binding.empty, []), srcs)])
   181   |> fst |> maps snd;
   182 
   183 
   184 (* attribute setup *)
   185 
   186 fun syntax scan = Args.syntax "attribute" scan;
   187 
   188 fun setup name scan =
   189   add_attribute name
   190     (fn src => fn (ctxt, th) => let val (a, ctxt') = syntax scan src ctxt in a (ctxt', th) end);
   191 
   192 fun attribute_setup name source cmt =
   193   Context.theory_map (ML_Context.expression (#pos source)
   194     "val (name, scan, comment): binding * attribute context_parser * string"
   195     "Context.map_theory (Attrib.setup name scan comment)"
   196     (ML_Lex.read Position.none ("(" ^ ML_Syntax.make_binding name ^ ", ") @
   197       ML_Lex.read_source source @
   198       ML_Lex.read Position.none (", " ^ ML_Syntax.print_string cmt ^ ")")));
   199 
   200 
   201 (* internal attribute *)
   202 
   203 fun internal att = Args.src (("Pure.attribute", [Token.mk_attribute att]), Position.none);
   204 
   205 val _ = Theory.setup
   206  (setup (Binding.name "attribute") (Scan.lift Args.internal_attribute >> Morphism.form)
   207     "internal attribute");
   208 
   209 
   210 (* add/del syntax *)
   211 
   212 fun add_del add del = Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add);
   213 
   214 
   215 
   216 (** parsing attributed theorems **)
   217 
   218 val thm_sel = Parse.$$$ "(" |-- Parse.list1
   219  (Parse.nat --| Parse.minus -- Parse.nat >> Facts.FromTo ||
   220   Parse.nat --| Parse.minus >> Facts.From ||
   221   Parse.nat >> Facts.Single) --| Parse.$$$ ")";
   222 
   223 local
   224 
   225 val fact_name = Args.internal_fact >> K "<fact>" || Args.name;
   226 
   227 fun gen_thm pick = Scan.depend (fn context =>
   228   let
   229     val get = Context.cases (Global_Theory.get_fact context) Proof_Context.get_fact context;
   230     val get_fact = get o Facts.Fact;
   231     fun get_named pos name = get (Facts.Named ((name, pos), NONE));
   232   in
   233     Parse.$$$ "[" |-- Args.attribs (check_name_generic context) --| Parse.$$$ "]" >> (fn srcs =>
   234       let
   235         val atts = map (attribute_generic context) srcs;
   236         val (th', context') = fold (uncurry o Thm.apply_attribute) atts (Drule.dummy_thm, context);
   237       in (context', pick ("", Position.none) [th']) end)
   238     ||
   239     (Scan.ahead Args.alt_name -- Args.named_fact get_fact
   240       >> (fn (s, fact) => ("", Facts.Fact s, fact)) ||
   241      Scan.ahead (Parse.position fact_name) :|-- (fn (name, pos) =>
   242       Args.named_fact (get_named pos) -- Scan.option thm_sel
   243         >> (fn (fact, sel) => (name, Facts.Named ((name, pos), sel), fact))))
   244     -- Args.opt_attribs (check_name_generic context) >> (fn ((name, thmref, fact), srcs) =>
   245       let
   246         val ths = Facts.select thmref fact;
   247         val atts = map (attribute_generic context) srcs;
   248         val (ths', context') =
   249           fold_map (curry (fold (uncurry o Thm.apply_attribute) atts)) ths context;
   250       in (context', pick (name, Facts.pos_of_ref thmref) ths') end)
   251   end);
   252 
   253 in
   254 
   255 val thm = gen_thm Facts.the_single;
   256 val multi_thm = gen_thm (K I);
   257 val thms = Scan.repeat multi_thm >> flat;
   258 
   259 end;
   260 
   261 
   262 
   263 (** partial evaluation -- observing rule/declaration/mixed attributes **)
   264 
   265 local
   266 
   267 fun apply_att src (context, th) =
   268   let
   269     val src1 = Args.init_assignable src;
   270     val result = attribute_generic context src1 (context, th);
   271     val src2 = Args.closure src1;
   272   in (src2, result) end;
   273 
   274 fun err msg src =
   275   let val ((name, _), pos) = Args.dest_src src
   276   in error (msg ^ " " ^ quote name ^ Position.here pos) end;
   277 
   278 fun eval src ((th, dyn), (decls, context)) =
   279   (case (apply_att src (context, th), dyn) of
   280     ((_, (NONE, SOME th')), NONE) => ((th', NONE), (decls, context))
   281   | ((_, (NONE, SOME _)), SOME _) => err "Mixed dynamic attribute followed by static rule" src
   282   | ((src', (SOME context', NONE)), NONE) =>
   283       let
   284         val decls' =
   285           (case decls of
   286             [] => [(th, [src'])]
   287           | (th2, srcs2) :: rest =>
   288               if Thm.eq_thm_strict (th, th2)
   289               then ((th2, src' :: srcs2) :: rest)
   290               else (th, [src']) :: (th2, srcs2) :: rest);
   291       in ((th, NONE), (decls', context')) end
   292   | ((src', (opt_context', opt_th')), _) =>
   293       let
   294         val context' = the_default context opt_context';
   295         val th' = the_default th opt_th';
   296         val dyn' =
   297           (case dyn of
   298             NONE => SOME (th, [src'])
   299           | SOME (dyn_th, srcs) => SOME (dyn_th, src' :: srcs));
   300       in ((th', dyn'), (decls, context')) end);
   301 
   302 in
   303 
   304 fun partial_evaluation ctxt facts =
   305   (facts, Context.Proof ctxt) |->
   306     fold_map (fn ((b, more_atts), fact) => fn context =>
   307       let
   308         val (fact', (decls, context')) =
   309           (fact, ([], context)) |-> fold_map (fn (ths, atts) => fn res1 =>
   310             (ths, res1) |-> fold_map (fn th => fn res2 =>
   311               let
   312                 val ((th', dyn'), res3) = fold eval (atts @ more_atts) ((th, NONE), res2);
   313                 val th_atts' =
   314                   (case dyn' of
   315                     NONE => (th', [])
   316                   | SOME (dyn_th', atts') => (dyn_th', rev atts'));
   317               in (th_atts', res3) end))
   318           |>> flat;
   319         val decls' = rev (map (apsnd rev) decls);
   320         val facts' =
   321           if eq_list (eq_fst Thm.eq_thm_strict) (decls', fact') then
   322             [((b, []), map2 (fn (th, atts1) => fn (_, atts2) => (th, atts1 @ atts2)) decls' fact')]
   323           else if null decls' then [((b, []), fact')]
   324           else [(empty_binding, decls'), ((b, []), fact')];
   325       in (facts', context') end)
   326   |> fst |> flat |> map (apsnd (map (apfst single)))
   327   |> filter_out (fn (b, fact) => is_empty_binding b andalso forall (null o #2) fact);
   328 
   329 end;
   330 
   331 
   332 
   333 (** configuration options **)
   334 
   335 (* naming *)
   336 
   337 structure Configs = Theory_Data
   338 (
   339   type T = Config.raw Symtab.table;
   340   val empty = Symtab.empty;
   341   val extend = I;
   342   fun merge data = Symtab.merge (K true) data;
   343 );
   344 
   345 fun print_options ctxt =
   346   let
   347     fun prt (name, config) =
   348       let val value = Config.get ctxt config in
   349         Pretty.block [Pretty.mark_str name, Pretty.str (": " ^ Config.print_type value ^ " ="),
   350           Pretty.brk 1, Pretty.str (Config.print_value value)]
   351       end;
   352     val space = #1 (get_attributes (Context.Proof ctxt));
   353     val configs = Name_Space.extern_table ctxt (space, Configs.get (Proof_Context.theory_of ctxt));
   354   in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
   355 
   356 
   357 (* concrete syntax *)
   358 
   359 local
   360 
   361 val equals = Parse.$$$ "=";
   362 
   363 fun scan_value (Config.Bool _) =
   364       equals -- Args.$$$ "false" >> K (Config.Bool false) ||
   365       equals -- Args.$$$ "true" >> K (Config.Bool true) ||
   366       Scan.succeed (Config.Bool true)
   367   | scan_value (Config.Int _) = equals |-- Parse.int >> Config.Int
   368   | scan_value (Config.Real _) = equals |-- Parse.real >> Config.Real
   369   | scan_value (Config.String _) = equals |-- Args.name >> Config.String;
   370 
   371 fun scan_config thy config =
   372   let val config_type = Config.get_global thy config
   373   in scan_value config_type >> (K o Thm.declaration_attribute o K o Config.put_generic config) end;
   374 
   375 fun register binding config thy =
   376   let val name = Sign.full_name thy binding in
   377     thy
   378     |> setup binding (Scan.lift (scan_config thy config) >> Morphism.form) "configuration option"
   379     |> Configs.map (Symtab.update (name, config))
   380   end;
   381 
   382 fun declare make coerce binding default =
   383   let
   384     val name = Binding.name_of binding;
   385     val config_value = Config.declare name (make o default);
   386     val config = coerce config_value;
   387   in (config, register binding config_value) end;
   388 
   389 in
   390 
   391 fun register_config config = register (Binding.name (Config.name_of config)) config;
   392 
   393 val config_bool = declare Config.Bool Config.bool;
   394 val config_int = declare Config.Int Config.int;
   395 val config_real = declare Config.Real Config.real;
   396 val config_string = declare Config.String Config.string;
   397 
   398 end;
   399 
   400 
   401 (* implicit setup *)
   402 
   403 local
   404 
   405 fun setup_config declare_config binding default =
   406   let
   407     val (config, setup) = declare_config binding default;
   408     val _ = Theory.setup setup;
   409   in config end;
   410 
   411 in
   412 
   413 val setup_config_bool = setup_config config_bool;
   414 val setup_config_int = setup_config config_int;
   415 val setup_config_string = setup_config config_string;
   416 val setup_config_real = setup_config config_real;
   417 
   418 end;
   419 
   420 
   421 (* system options *)
   422 
   423 local
   424 
   425 fun declare_option coerce name =
   426   let
   427     val config = Config.declare_option name;
   428   in (coerce config, register_config config) end;
   429 
   430 fun setup_option coerce name =
   431   let
   432     val config = Config.declare_option name;
   433     val _ = Theory.setup (register_config config);
   434   in coerce config end;
   435 
   436 in
   437 
   438 val option_bool = declare_option Config.bool;
   439 val option_int = declare_option Config.int;
   440 val option_real = declare_option Config.real;
   441 val option_string = declare_option Config.string;
   442 
   443 val setup_option_bool = setup_option Config.bool;
   444 val setup_option_int = setup_option Config.int;
   445 val setup_option_real = setup_option Config.real;
   446 val setup_option_string = setup_option Config.string;
   447 
   448 end;
   449 
   450 
   451 (* theory setup *)
   452 
   453 val _ = Theory.setup
   454  (register_config quick_and_dirty_raw #>
   455   register_config Ast.trace_raw #>
   456   register_config Ast.stats_raw #>
   457   register_config Printer.show_brackets_raw #>
   458   register_config Printer.show_sorts_raw #>
   459   register_config Printer.show_types_raw #>
   460   register_config Printer.show_markup_raw #>
   461   register_config Printer.show_structs_raw #>
   462   register_config Printer.show_question_marks_raw #>
   463   register_config Syntax.ambiguity_warning_raw #>
   464   register_config Syntax.ambiguity_limit_raw #>
   465   register_config Syntax_Trans.eta_contract_raw #>
   466   register_config Name_Space.names_long_raw #>
   467   register_config Name_Space.names_short_raw #>
   468   register_config Name_Space.names_unique_raw #>
   469   register_config ML_Context.trace_raw #>
   470   register_config Proof_Context.show_abbrevs_raw #>
   471   register_config Goal_Display.goals_limit_raw #>
   472   register_config Goal_Display.show_main_goal_raw #>
   473   register_config Goal_Display.show_consts_raw #>
   474   register_config Display.show_hyps_raw #>
   475   register_config Display.show_tags_raw #>
   476   register_config Pattern.unify_trace_failure_raw #>
   477   register_config Unify.trace_bound_raw #>
   478   register_config Unify.search_bound_raw #>
   479   register_config Unify.trace_simp_raw #>
   480   register_config Unify.trace_types_raw #>
   481   register_config Raw_Simplifier.simp_depth_limit_raw #>
   482   register_config Raw_Simplifier.simp_trace_depth_limit_raw #>
   483   register_config Raw_Simplifier.simp_debug_raw #>
   484   register_config Raw_Simplifier.simp_trace_raw);
   485 
   486 end;