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