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