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