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