src/Pure/Isar/attrib.ML
author wenzelm
Mon May 17 15:11:25 2010 +0200 (2010-05-17)
changeset 36959 f5417836dbea
parent 36950 75b8f26f2f07
child 37198 3af985b10550
permissions -rw-r--r--
renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
eliminated slightly odd alias structure T;
     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_Syntax.make_binding name ^ ", " ^ txt ^ ", " ^ ML_Syntax.print_string cmt ^ ")"));
   157 
   158 
   159 (* add/del syntax *)
   160 
   161 fun add_del add del = Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add);
   162 
   163 
   164 
   165 (** parsing attributed theorems **)
   166 
   167 val thm_sel = Parse.$$$ "(" |-- Parse.list1
   168  (Parse.nat --| Parse.minus -- Parse.nat >> Facts.FromTo ||
   169   Parse.nat --| Parse.minus >> Facts.From ||
   170   Parse.nat >> Facts.Single) --| Parse.$$$ ")";
   171 
   172 local
   173 
   174 val fact_name = Args.internal_fact >> K "<fact>" || Args.name;
   175 
   176 fun gen_thm pick = Scan.depend (fn context =>
   177   let
   178     val thy = Context.theory_of context;
   179     val get = Context.cases (PureThy.get_fact context) ProofContext.get_fact context;
   180     val get_fact = get o Facts.Fact;
   181     fun get_named pos name = get (Facts.Named ((name, pos), NONE));
   182   in
   183     Parse.$$$ "[" |-- Args.attribs (intern thy) --| Parse.$$$ "]" >> (fn srcs =>
   184       let
   185         val atts = map (attribute_i thy) srcs;
   186         val (context', th') = Library.apply atts (context, Drule.dummy_thm);
   187       in (context', pick "" [th']) end)
   188     ||
   189     (Scan.ahead Args.alt_name -- Args.named_fact get_fact
   190       >> (fn (s, fact) => ("", Facts.Fact s, fact)) ||
   191      Scan.ahead (Parse.position fact_name) :|-- (fn (name, pos) =>
   192       Args.named_fact (get_named pos) -- Scan.option thm_sel
   193         >> (fn (fact, sel) => (name, Facts.Named ((name, pos), sel), fact))))
   194     -- Args.opt_attribs (intern thy) >> (fn ((name, thmref, fact), srcs) =>
   195       let
   196         val ths = Facts.select thmref fact;
   197         val atts = map (attribute_i thy) srcs;
   198         val (context', ths') = Library.foldl_map (Library.apply atts) (context, ths);
   199       in (context', pick name ths') end)
   200   end);
   201 
   202 in
   203 
   204 val thm = gen_thm Facts.the_single;
   205 val multi_thm = gen_thm (K I);
   206 val thms = Scan.repeat multi_thm >> flat;
   207 
   208 end;
   209 
   210 
   211 
   212 (** basic attributes **)
   213 
   214 (* internal *)
   215 
   216 fun internal att = Args.src (("Pure.attribute", [Token.mk_attribute att]), Position.none);
   217 
   218 
   219 (* rule composition *)
   220 
   221 val COMP_att =
   222   Scan.lift (Scan.optional (Args.bracks Parse.nat) 1) -- thm
   223     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B)));
   224 
   225 val THEN_att =
   226   Scan.lift (Scan.optional (Args.bracks Parse.nat) 1) -- thm
   227     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B)));
   228 
   229 val OF_att =
   230   thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A));
   231 
   232 
   233 (* rename_abs *)
   234 
   235 fun rename_abs x = (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars')) x;
   236 
   237 
   238 (* unfold / fold definitions *)
   239 
   240 fun unfolded_syntax rule =
   241   thms >> (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths));
   242 
   243 val unfolded = unfolded_syntax Local_Defs.unfold;
   244 val folded = unfolded_syntax Local_Defs.fold;
   245 
   246 
   247 (* rule format *)
   248 
   249 val rule_format = Args.mode "no_asm"
   250   >> (fn true => Object_Logic.rule_format_no_asm | false => Object_Logic.rule_format);
   251 
   252 val elim_format = Thm.rule_attribute (K Tactic.make_elim);
   253 
   254 
   255 (* misc rules *)
   256 
   257 val no_vars = Thm.rule_attribute (fn context => fn th =>
   258   let
   259     val ctxt = Variable.set_body false (Context.proof_of context);
   260     val ((_, [th']), _) = Variable.import true [th] ctxt;
   261   in th' end);
   262 
   263 val eta_long =
   264   Thm.rule_attribute (K (Conv.fconv_rule Drule.eta_long_conversion));
   265 
   266 val rotated = Scan.optional Parse.int 1 >> (fn n => Thm.rule_attribute (K (rotate_prems n)));
   267 
   268 
   269 (* theory setup *)
   270 
   271 val _ = Context.>> (Context.map_theory
   272  (setup (Binding.name "attribute") (Scan.lift Args.internal_attribute >> Morphism.form)
   273     "internal attribute" #>
   274   setup (Binding.name "tagged") (Scan.lift (Args.name -- Args.name) >> Thm.tag) "tagged theorem" #>
   275   setup (Binding.name "untagged") (Scan.lift Args.name >> Thm.untag) "untagged theorem" #>
   276   setup (Binding.name "kind") (Scan.lift Args.name >> Thm.kind) "theorem kind" #>
   277   setup (Binding.name "COMP") COMP_att "direct composition with rules (no lifting)" #>
   278   setup (Binding.name "THEN") THEN_att "resolution with rule" #>
   279   setup (Binding.name "OF") OF_att "rule applied to facts" #>
   280   setup (Binding.name "rename_abs") (Scan.lift rename_abs)
   281     "rename bound variables in abstractions" #>
   282   setup (Binding.name "unfolded") unfolded "unfolded definitions" #>
   283   setup (Binding.name "folded") folded "folded definitions" #>
   284   setup (Binding.name "consumes") (Scan.lift (Scan.optional Parse.nat 1) >> Rule_Cases.consumes)
   285     "number of consumed facts" #>
   286   setup (Binding.name "constraints") (Scan.lift Parse.nat >> Rule_Cases.constraints)
   287     "number of equality constraints" #>
   288   setup (Binding.name "case_names") (Scan.lift (Scan.repeat1 Args.name) >> Rule_Cases.case_names)
   289     "named rule cases" #>
   290   setup (Binding.name "case_conclusion")
   291     (Scan.lift (Args.name -- Scan.repeat Args.name) >> Rule_Cases.case_conclusion)
   292     "named conclusion of rule cases" #>
   293   setup (Binding.name "params")
   294     (Scan.lift (Parse.and_list1 (Scan.repeat Args.name)) >> Rule_Cases.params)
   295     "named rule parameters" #>
   296   setup (Binding.name "standard") (Scan.succeed (Thm.rule_attribute (K Drule.export_without_context)))
   297     "result put into standard form (legacy)" #>
   298   setup (Binding.name "rule_format") rule_format "result put into canonical rule format" #>
   299   setup (Binding.name "elim_format") (Scan.succeed elim_format)
   300     "destruct rule turned into elimination rule format" #>
   301   setup (Binding.name "no_vars") (Scan.succeed no_vars) "frozen schematic vars" #>
   302   setup (Binding.name "eta_long") (Scan.succeed eta_long)
   303     "put theorem into eta long beta normal form" #>
   304   setup (Binding.name "atomize") (Scan.succeed Object_Logic.declare_atomize)
   305     "declaration of atomize rule" #>
   306   setup (Binding.name "rulify") (Scan.succeed Object_Logic.declare_rulify)
   307     "declaration of rulify rule" #>
   308   setup (Binding.name "rotated") (Scan.lift rotated) "rotated theorem premises" #>
   309   setup (Binding.name "defn") (add_del Local_Defs.defn_add Local_Defs.defn_del)
   310     "declaration of definitional transformations" #>
   311   setup (Binding.name "abs_def") (Scan.succeed (Thm.rule_attribute (K Drule.abs_def)))
   312     "abstract over free variables of a definition"));
   313 
   314 
   315 
   316 (** configuration options **)
   317 
   318 (* naming *)
   319 
   320 structure Configs = Theory_Data
   321 (
   322   type T = Config.value Config.T Symtab.table;
   323   val empty = Symtab.empty;
   324   val extend = I;
   325   fun merge data = Symtab.merge (K true) data;
   326 );
   327 
   328 fun print_configs ctxt =
   329   let
   330     val thy = ProofContext.theory_of ctxt;
   331     fun prt (name, config) =
   332       let val value = Config.get ctxt config in
   333         Pretty.block [Pretty.str (name ^ ": " ^ Config.print_type value ^ " ="), Pretty.brk 1,
   334           Pretty.str (Config.print_value value)]
   335       end;
   336     val configs = Name_Space.extern_table (#1 (Attributes.get thy), Configs.get thy);
   337   in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
   338 
   339 
   340 (* concrete syntax *)
   341 
   342 local
   343 
   344 val equals = Parse.$$$ "=";
   345 
   346 fun scan_value (Config.Bool _) =
   347       equals -- Args.$$$ "false" >> K (Config.Bool false) ||
   348       equals -- Args.$$$ "true" >> K (Config.Bool true) ||
   349       Scan.succeed (Config.Bool true)
   350   | scan_value (Config.Int _) = equals |-- Parse.int >> Config.Int
   351   | scan_value (Config.String _) = equals |-- Args.name >> Config.String;
   352 
   353 fun scan_config thy config =
   354   let val config_type = Config.get_global thy config
   355   in scan_value config_type >> (K o Thm.declaration_attribute o K o Config.put_generic config) end;
   356 
   357 in
   358 
   359 fun register_config config thy =
   360   let
   361     val bname = Config.name_of config;
   362     val name = Sign.full_bname thy bname;
   363   in
   364     thy
   365     |> setup (Binding.name bname) (Scan.lift (scan_config thy config) >> Morphism.form)
   366       "configuration option"
   367     |> Configs.map (Symtab.update (name, config))
   368   end;
   369 
   370 fun declare_config make coerce global name default =
   371   let
   372     val config_value = Config.declare global name (make o default);
   373     val config = coerce config_value;
   374   in (config, register_config config_value) end;
   375 
   376 val config_bool   = declare_config Config.Bool Config.bool false;
   377 val config_int    = declare_config Config.Int Config.int false;
   378 val config_string = declare_config Config.String Config.string false;
   379 
   380 val config_bool_global   = declare_config Config.Bool Config.bool true;
   381 val config_int_global    = declare_config Config.Int Config.int true;
   382 val config_string_global = declare_config Config.String Config.string true;
   383 
   384 end;
   385 
   386 
   387 (* theory setup *)
   388 
   389 val _ = Context.>> (Context.map_theory
   390  (register_config Unify.trace_bound_value #>
   391   register_config Unify.search_bound_value #>
   392   register_config Unify.trace_simp_value #>
   393   register_config Unify.trace_types_value #>
   394   register_config MetaSimplifier.simp_depth_limit_value #>
   395   register_config MetaSimplifier.debug_simp_value #>
   396   register_config MetaSimplifier.trace_simp_value));
   397 
   398 end;