src/Pure/Isar/attrib.ML
author wenzelm
Sun May 30 21:34:19 2010 +0200 (2010-05-30)
changeset 37198 3af985b10550
parent 36959 f5417836dbea
child 38329 16bb1e60204b
permissions -rw-r--r--
replaced ML_Lex.read_antiq by more concise ML_Lex.read, which includes full read/report with explicit position information;
ML_Context.eval/expression expect explicit ML_Lex source, which allows surrounding further text without loosing position information;
fall back on ML_Context.eval_text if there is no position or no surrounding text;
proper Args.name_source_position for method "tactic" and "raw_tactic";
tuned;
     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 -> (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.enclose "[" "]"
    98         (Pretty.commas (map (Args.pretty_src ctxt o Args.map_name (extern ctxt)) srcs))];
    99 
   100 
   101 (* get attributes *)
   102 
   103 val defined = Symtab.defined o #2 o Attributes.get;
   104 
   105 fun attribute_i thy =
   106   let
   107     val attrs = #2 (Attributes.get thy);
   108     fun attr src =
   109       let val ((name, _), pos) = Args.dest_src src in
   110         (case Symtab.lookup attrs name of
   111           NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
   112         | SOME (att, _) => (Position.report (Markup.attribute name) pos; att src))
   113       end;
   114   in attr end;
   115 
   116 fun attribute thy = attribute_i thy o intern_src thy;
   117 
   118 fun eval_thms ctxt args = ProofContext.note_thmss ""
   119     [(Thm.empty_binding, args |> map (fn (a, atts) =>
   120         (ProofContext.get_fact ctxt a, map (attribute (ProofContext.theory_of ctxt)) atts)))] ctxt
   121   |> fst |> maps snd;
   122 
   123 
   124 (* attributed declarations *)
   125 
   126 fun map_specs f = map (apfst (apsnd (map f)));
   127 
   128 fun map_facts f = map (apfst (apsnd (map f)) o apsnd (map (apsnd (map f))));
   129 fun map_facts_refs f g = map_facts f #> map (apsnd (map (apfst g)));
   130 
   131 
   132 (* crude_closure *)
   133 
   134 (*Produce closure without knowing facts in advance! The following
   135   works reasonably well for attribute parsers that do not peek at the
   136   thm structure.*)
   137 
   138 fun crude_closure ctxt src =
   139  (try (fn () => attribute_i (ProofContext.theory_of ctxt) src
   140     (Context.Proof ctxt, Drule.asm_rl)) ();
   141   Args.closure src);
   142 
   143 
   144 (* attribute setup *)
   145 
   146 fun syntax scan = Args.syntax "attribute" scan;
   147 
   148 fun setup name scan =
   149   add_attribute name
   150     (fn src => fn (ctxt, th) => let val (a, ctxt') = syntax scan src ctxt in a (ctxt', th) end);
   151 
   152 fun attribute_setup name (txt, pos) cmt =
   153   Context.theory_map (ML_Context.expression pos
   154     "val (name, scan, comment): binding * attribute context_parser * string"
   155     "Context.map_theory (Attrib.setup name scan comment)"
   156     (ML_Lex.read Position.none ("(" ^ ML_Syntax.make_binding name ^ ", ") @
   157       ML_Lex.read pos txt @
   158       ML_Lex.read Position.none (", " ^ ML_Syntax.print_string cmt ^ ")")));
   159 
   160 
   161 (* add/del syntax *)
   162 
   163 fun add_del add del = Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add);
   164 
   165 
   166 
   167 (** parsing attributed theorems **)
   168 
   169 val thm_sel = Parse.$$$ "(" |-- Parse.list1
   170  (Parse.nat --| Parse.minus -- Parse.nat >> Facts.FromTo ||
   171   Parse.nat --| Parse.minus >> Facts.From ||
   172   Parse.nat >> Facts.Single) --| Parse.$$$ ")";
   173 
   174 local
   175 
   176 val fact_name = Args.internal_fact >> K "<fact>" || Args.name;
   177 
   178 fun gen_thm pick = Scan.depend (fn context =>
   179   let
   180     val thy = Context.theory_of context;
   181     val get = Context.cases (PureThy.get_fact context) ProofContext.get_fact context;
   182     val get_fact = get o Facts.Fact;
   183     fun get_named pos name = get (Facts.Named ((name, pos), NONE));
   184   in
   185     Parse.$$$ "[" |-- Args.attribs (intern thy) --| Parse.$$$ "]" >> (fn srcs =>
   186       let
   187         val atts = map (attribute_i thy) srcs;
   188         val (context', th') = Library.apply atts (context, Drule.dummy_thm);
   189       in (context', pick "" [th']) end)
   190     ||
   191     (Scan.ahead Args.alt_name -- Args.named_fact get_fact
   192       >> (fn (s, fact) => ("", Facts.Fact s, fact)) ||
   193      Scan.ahead (Parse.position fact_name) :|-- (fn (name, pos) =>
   194       Args.named_fact (get_named pos) -- Scan.option thm_sel
   195         >> (fn (fact, sel) => (name, Facts.Named ((name, pos), sel), fact))))
   196     -- Args.opt_attribs (intern thy) >> (fn ((name, thmref, fact), srcs) =>
   197       let
   198         val ths = Facts.select thmref fact;
   199         val atts = map (attribute_i thy) srcs;
   200         val (context', ths') = Library.foldl_map (Library.apply atts) (context, ths);
   201       in (context', pick name ths') end)
   202   end);
   203 
   204 in
   205 
   206 val thm = gen_thm Facts.the_single;
   207 val multi_thm = gen_thm (K I);
   208 val thms = Scan.repeat multi_thm >> flat;
   209 
   210 end;
   211 
   212 
   213 
   214 (** basic attributes **)
   215 
   216 (* internal *)
   217 
   218 fun internal att = Args.src (("Pure.attribute", [Token.mk_attribute att]), Position.none);
   219 
   220 
   221 (* rule composition *)
   222 
   223 val COMP_att =
   224   Scan.lift (Scan.optional (Args.bracks Parse.nat) 1) -- thm
   225     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B)));
   226 
   227 val THEN_att =
   228   Scan.lift (Scan.optional (Args.bracks Parse.nat) 1) -- thm
   229     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B)));
   230 
   231 val OF_att =
   232   thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A));
   233 
   234 
   235 (* rename_abs *)
   236 
   237 fun rename_abs x = (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars')) x;
   238 
   239 
   240 (* unfold / fold definitions *)
   241 
   242 fun unfolded_syntax rule =
   243   thms >> (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths));
   244 
   245 val unfolded = unfolded_syntax Local_Defs.unfold;
   246 val folded = unfolded_syntax Local_Defs.fold;
   247 
   248 
   249 (* rule format *)
   250 
   251 val rule_format = Args.mode "no_asm"
   252   >> (fn true => Object_Logic.rule_format_no_asm | false => Object_Logic.rule_format);
   253 
   254 val elim_format = Thm.rule_attribute (K Tactic.make_elim);
   255 
   256 
   257 (* misc rules *)
   258 
   259 val no_vars = Thm.rule_attribute (fn context => fn th =>
   260   let
   261     val ctxt = Variable.set_body false (Context.proof_of context);
   262     val ((_, [th']), _) = Variable.import true [th] ctxt;
   263   in th' end);
   264 
   265 val eta_long =
   266   Thm.rule_attribute (K (Conv.fconv_rule Drule.eta_long_conversion));
   267 
   268 val rotated = Scan.optional Parse.int 1 >> (fn n => Thm.rule_attribute (K (rotate_prems n)));
   269 
   270 
   271 (* theory setup *)
   272 
   273 val _ = Context.>> (Context.map_theory
   274  (setup (Binding.name "attribute") (Scan.lift Args.internal_attribute >> Morphism.form)
   275     "internal attribute" #>
   276   setup (Binding.name "tagged") (Scan.lift (Args.name -- Args.name) >> Thm.tag) "tagged theorem" #>
   277   setup (Binding.name "untagged") (Scan.lift Args.name >> Thm.untag) "untagged theorem" #>
   278   setup (Binding.name "kind") (Scan.lift Args.name >> Thm.kind) "theorem kind" #>
   279   setup (Binding.name "COMP") COMP_att "direct composition with rules (no lifting)" #>
   280   setup (Binding.name "THEN") THEN_att "resolution with rule" #>
   281   setup (Binding.name "OF") OF_att "rule applied to facts" #>
   282   setup (Binding.name "rename_abs") (Scan.lift rename_abs)
   283     "rename bound variables in abstractions" #>
   284   setup (Binding.name "unfolded") unfolded "unfolded definitions" #>
   285   setup (Binding.name "folded") folded "folded definitions" #>
   286   setup (Binding.name "consumes") (Scan.lift (Scan.optional Parse.nat 1) >> Rule_Cases.consumes)
   287     "number of consumed facts" #>
   288   setup (Binding.name "constraints") (Scan.lift Parse.nat >> Rule_Cases.constraints)
   289     "number of equality constraints" #>
   290   setup (Binding.name "case_names") (Scan.lift (Scan.repeat1 Args.name) >> Rule_Cases.case_names)
   291     "named rule cases" #>
   292   setup (Binding.name "case_conclusion")
   293     (Scan.lift (Args.name -- Scan.repeat Args.name) >> Rule_Cases.case_conclusion)
   294     "named conclusion of rule cases" #>
   295   setup (Binding.name "params")
   296     (Scan.lift (Parse.and_list1 (Scan.repeat Args.name)) >> Rule_Cases.params)
   297     "named rule parameters" #>
   298   setup (Binding.name "standard") (Scan.succeed (Thm.rule_attribute (K Drule.export_without_context)))
   299     "result put into standard form (legacy)" #>
   300   setup (Binding.name "rule_format") rule_format "result put into canonical rule format" #>
   301   setup (Binding.name "elim_format") (Scan.succeed elim_format)
   302     "destruct rule turned into elimination rule format" #>
   303   setup (Binding.name "no_vars") (Scan.succeed no_vars) "frozen schematic vars" #>
   304   setup (Binding.name "eta_long") (Scan.succeed eta_long)
   305     "put theorem into eta long beta normal form" #>
   306   setup (Binding.name "atomize") (Scan.succeed Object_Logic.declare_atomize)
   307     "declaration of atomize rule" #>
   308   setup (Binding.name "rulify") (Scan.succeed Object_Logic.declare_rulify)
   309     "declaration of rulify rule" #>
   310   setup (Binding.name "rotated") (Scan.lift rotated) "rotated theorem premises" #>
   311   setup (Binding.name "defn") (add_del Local_Defs.defn_add Local_Defs.defn_del)
   312     "declaration of definitional transformations" #>
   313   setup (Binding.name "abs_def") (Scan.succeed (Thm.rule_attribute (K Drule.abs_def)))
   314     "abstract over free variables of a definition"));
   315 
   316 
   317 
   318 (** configuration options **)
   319 
   320 (* naming *)
   321 
   322 structure Configs = Theory_Data
   323 (
   324   type T = Config.value Config.T Symtab.table;
   325   val empty = Symtab.empty;
   326   val extend = I;
   327   fun merge data = Symtab.merge (K true) data;
   328 );
   329 
   330 fun print_configs ctxt =
   331   let
   332     val thy = ProofContext.theory_of ctxt;
   333     fun prt (name, config) =
   334       let val value = Config.get ctxt config in
   335         Pretty.block [Pretty.str (name ^ ": " ^ Config.print_type value ^ " ="), Pretty.brk 1,
   336           Pretty.str (Config.print_value value)]
   337       end;
   338     val configs = Name_Space.extern_table (#1 (Attributes.get thy), Configs.get thy);
   339   in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
   340 
   341 
   342 (* concrete syntax *)
   343 
   344 local
   345 
   346 val equals = Parse.$$$ "=";
   347 
   348 fun scan_value (Config.Bool _) =
   349       equals -- Args.$$$ "false" >> K (Config.Bool false) ||
   350       equals -- Args.$$$ "true" >> K (Config.Bool true) ||
   351       Scan.succeed (Config.Bool true)
   352   | scan_value (Config.Int _) = equals |-- Parse.int >> Config.Int
   353   | scan_value (Config.String _) = equals |-- Args.name >> Config.String;
   354 
   355 fun scan_config thy config =
   356   let val config_type = Config.get_global thy config
   357   in scan_value config_type >> (K o Thm.declaration_attribute o K o Config.put_generic config) end;
   358 
   359 in
   360 
   361 fun register_config config thy =
   362   let
   363     val bname = Config.name_of config;
   364     val name = Sign.full_bname thy bname;
   365   in
   366     thy
   367     |> setup (Binding.name bname) (Scan.lift (scan_config thy config) >> Morphism.form)
   368       "configuration option"
   369     |> Configs.map (Symtab.update (name, config))
   370   end;
   371 
   372 fun declare_config make coerce global name default =
   373   let
   374     val config_value = Config.declare global name (make o default);
   375     val config = coerce config_value;
   376   in (config, register_config config_value) end;
   377 
   378 val config_bool   = declare_config Config.Bool Config.bool false;
   379 val config_int    = declare_config Config.Int Config.int false;
   380 val config_string = declare_config Config.String Config.string false;
   381 
   382 val config_bool_global   = declare_config Config.Bool Config.bool true;
   383 val config_int_global    = declare_config Config.Int Config.int true;
   384 val config_string_global = declare_config Config.String Config.string true;
   385 
   386 end;
   387 
   388 
   389 (* theory setup *)
   390 
   391 val _ = Context.>> (Context.map_theory
   392  (register_config Unify.trace_bound_value #>
   393   register_config Unify.search_bound_value #>
   394   register_config Unify.trace_simp_value #>
   395   register_config Unify.trace_types_value #>
   396   register_config MetaSimplifier.simp_depth_limit_value #>
   397   register_config MetaSimplifier.debug_simp_value #>
   398   register_config MetaSimplifier.trace_simp_value));
   399 
   400 end;