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