src/Pure/Isar/attrib.ML
author wenzelm
Sat May 15 23:16:32 2010 +0200 (2010-05-15)
changeset 36950 75b8f26f2f07
parent 36787 f60e4dd6d76f
child 36959 f5417836dbea
permissions -rw-r--r--
refer directly to structure Keyword and Parse;
eliminated old-style structure aliases K and P;
     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 structure T = OuterLex;
    51 
    52 
    53 (* source and bindings *)
    54 
    55 type src = Args.src;
    56 
    57 type binding = binding * src list;
    58 val empty_binding: binding = (Binding.empty, []);
    59 
    60 
    61 
    62 (** named attributes **)
    63 
    64 (* theory data *)
    65 
    66 structure Attributes = Theory_Data
    67 (
    68   type T = ((src -> attribute) * string) Name_Space.table;
    69   val empty : T = Name_Space.empty_table "attribute";
    70   val extend = I;
    71   fun merge data : T = Name_Space.merge_tables data;
    72 );
    73 
    74 fun print_attributes thy =
    75   let
    76     val attribs = Attributes.get thy;
    77     fun prt_attr (name, (_, comment)) = Pretty.block
    78       [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
    79   in
    80     [Pretty.big_list "attributes:" (map prt_attr (Name_Space.extern_table attribs))]
    81     |> Pretty.chunks |> Pretty.writeln
    82   end;
    83 
    84 fun add_attribute name att comment thy = thy
    85   |> Attributes.map (#2 o Name_Space.define true (Sign.naming_of thy) (name, (att, comment)));
    86 
    87 
    88 (* name space *)
    89 
    90 val intern = Name_Space.intern o #1 o Attributes.get;
    91 val intern_src = Args.map_name o intern;
    92 
    93 val extern = Name_Space.extern o #1 o Attributes.get o ProofContext.theory_of;
    94 
    95 
    96 (* pretty printing *)
    97 
    98 fun pretty_attribs _ [] = []
    99   | pretty_attribs ctxt srcs =
   100       [Pretty.enclose "[" "]"
   101         (Pretty.commas (map (Args.pretty_src ctxt o Args.map_name (extern ctxt)) srcs))];
   102 
   103 
   104 (* get attributes *)
   105 
   106 val defined = Symtab.defined o #2 o Attributes.get;
   107 
   108 fun attribute_i thy =
   109   let
   110     val attrs = #2 (Attributes.get thy);
   111     fun attr src =
   112       let val ((name, _), pos) = Args.dest_src src in
   113         (case Symtab.lookup attrs name of
   114           NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
   115         | SOME (att, _) => (Position.report (Markup.attribute name) pos; att src))
   116       end;
   117   in attr end;
   118 
   119 fun attribute thy = attribute_i thy o intern_src thy;
   120 
   121 fun eval_thms ctxt args = ProofContext.note_thmss ""
   122     [(Thm.empty_binding, args |> map (fn (a, atts) =>
   123         (ProofContext.get_fact ctxt a, map (attribute (ProofContext.theory_of ctxt)) atts)))] ctxt
   124   |> fst |> maps snd;
   125 
   126 
   127 (* attributed declarations *)
   128 
   129 fun map_specs f = map (apfst (apsnd (map f)));
   130 
   131 fun map_facts f = map (apfst (apsnd (map f)) o apsnd (map (apsnd (map f))));
   132 fun map_facts_refs f g = map_facts f #> map (apsnd (map (apfst g)));
   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_Syntax.make_binding name ^ ", " ^ txt ^ ", " ^ ML_Syntax.print_string cmt ^ ")"));
   160 
   161 
   162 (* add/del syntax *)
   163 
   164 fun add_del add del = Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add);
   165 
   166 
   167 
   168 (** parsing attributed theorems **)
   169 
   170 val thm_sel = Parse.$$$ "(" |-- Parse.list1
   171  (Parse.nat --| Parse.minus -- Parse.nat >> Facts.FromTo ||
   172   Parse.nat --| Parse.minus >> Facts.From ||
   173   Parse.nat >> Facts.Single) --| Parse.$$$ ")";
   174 
   175 local
   176 
   177 val fact_name = Args.internal_fact >> K "<fact>" || Args.name;
   178 
   179 fun gen_thm pick = Scan.depend (fn context =>
   180   let
   181     val thy = Context.theory_of context;
   182     val get = Context.cases (PureThy.get_fact context) ProofContext.get_fact context;
   183     val get_fact = get o Facts.Fact;
   184     fun get_named pos name = get (Facts.Named ((name, pos), NONE));
   185   in
   186     Parse.$$$ "[" |-- Args.attribs (intern thy) --| Parse.$$$ "]" >> (fn srcs =>
   187       let
   188         val atts = map (attribute_i thy) srcs;
   189         val (context', th') = Library.apply atts (context, Drule.dummy_thm);
   190       in (context', pick "" [th']) end)
   191     ||
   192     (Scan.ahead Args.alt_name -- Args.named_fact get_fact
   193       >> (fn (s, fact) => ("", Facts.Fact s, fact)) ||
   194      Scan.ahead (Parse.position fact_name) :|-- (fn (name, pos) =>
   195       Args.named_fact (get_named pos) -- Scan.option thm_sel
   196         >> (fn (fact, sel) => (name, Facts.Named ((name, pos), sel), fact))))
   197     -- Args.opt_attribs (intern thy) >> (fn ((name, thmref, fact), srcs) =>
   198       let
   199         val ths = Facts.select thmref fact;
   200         val atts = map (attribute_i thy) srcs;
   201         val (context', ths') = Library.foldl_map (Library.apply atts) (context, ths);
   202       in (context', pick name ths') end)
   203   end);
   204 
   205 in
   206 
   207 val thm = gen_thm Facts.the_single;
   208 val multi_thm = gen_thm (K I);
   209 val thms = Scan.repeat multi_thm >> flat;
   210 
   211 end;
   212 
   213 
   214 
   215 (** basic attributes **)
   216 
   217 (* internal *)
   218 
   219 fun internal att = Args.src (("Pure.attribute", [T.mk_attribute att]), Position.none);
   220 
   221 
   222 (* rule composition *)
   223 
   224 val COMP_att =
   225   Scan.lift (Scan.optional (Args.bracks Parse.nat) 1) -- thm
   226     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B)));
   227 
   228 val THEN_att =
   229   Scan.lift (Scan.optional (Args.bracks Parse.nat) 1) -- thm
   230     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B)));
   231 
   232 val OF_att =
   233   thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A));
   234 
   235 
   236 (* rename_abs *)
   237 
   238 fun rename_abs x = (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars')) x;
   239 
   240 
   241 (* unfold / fold definitions *)
   242 
   243 fun unfolded_syntax rule =
   244   thms >> (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths));
   245 
   246 val unfolded = unfolded_syntax Local_Defs.unfold;
   247 val folded = unfolded_syntax Local_Defs.fold;
   248 
   249 
   250 (* rule format *)
   251 
   252 val rule_format = Args.mode "no_asm"
   253   >> (fn true => Object_Logic.rule_format_no_asm | false => Object_Logic.rule_format);
   254 
   255 val elim_format = Thm.rule_attribute (K Tactic.make_elim);
   256 
   257 
   258 (* misc rules *)
   259 
   260 val no_vars = Thm.rule_attribute (fn context => fn th =>
   261   let
   262     val ctxt = Variable.set_body false (Context.proof_of context);
   263     val ((_, [th']), _) = Variable.import true [th] ctxt;
   264   in th' end);
   265 
   266 val eta_long =
   267   Thm.rule_attribute (K (Conv.fconv_rule Drule.eta_long_conversion));
   268 
   269 val rotated = Scan.optional Parse.int 1 >> (fn n => Thm.rule_attribute (K (rotate_prems n)));
   270 
   271 
   272 (* theory setup *)
   273 
   274 val _ = Context.>> (Context.map_theory
   275  (setup (Binding.name "attribute") (Scan.lift Args.internal_attribute >> Morphism.form)
   276     "internal attribute" #>
   277   setup (Binding.name "tagged") (Scan.lift (Args.name -- Args.name) >> Thm.tag) "tagged theorem" #>
   278   setup (Binding.name "untagged") (Scan.lift Args.name >> Thm.untag) "untagged theorem" #>
   279   setup (Binding.name "kind") (Scan.lift Args.name >> Thm.kind) "theorem kind" #>
   280   setup (Binding.name "COMP") COMP_att "direct composition with rules (no lifting)" #>
   281   setup (Binding.name "THEN") THEN_att "resolution with rule" #>
   282   setup (Binding.name "OF") OF_att "rule applied to facts" #>
   283   setup (Binding.name "rename_abs") (Scan.lift rename_abs)
   284     "rename bound variables in abstractions" #>
   285   setup (Binding.name "unfolded") unfolded "unfolded definitions" #>
   286   setup (Binding.name "folded") folded "folded definitions" #>
   287   setup (Binding.name "consumes") (Scan.lift (Scan.optional Parse.nat 1) >> Rule_Cases.consumes)
   288     "number of consumed facts" #>
   289   setup (Binding.name "constraints") (Scan.lift Parse.nat >> Rule_Cases.constraints)
   290     "number of equality constraints" #>
   291   setup (Binding.name "case_names") (Scan.lift (Scan.repeat1 Args.name) >> Rule_Cases.case_names)
   292     "named rule cases" #>
   293   setup (Binding.name "case_conclusion")
   294     (Scan.lift (Args.name -- Scan.repeat Args.name) >> Rule_Cases.case_conclusion)
   295     "named conclusion of rule cases" #>
   296   setup (Binding.name "params")
   297     (Scan.lift (Parse.and_list1 (Scan.repeat Args.name)) >> Rule_Cases.params)
   298     "named rule parameters" #>
   299   setup (Binding.name "standard") (Scan.succeed (Thm.rule_attribute (K Drule.export_without_context)))
   300     "result put into standard form (legacy)" #>
   301   setup (Binding.name "rule_format") rule_format "result put into canonical rule format" #>
   302   setup (Binding.name "elim_format") (Scan.succeed elim_format)
   303     "destruct rule turned into elimination rule format" #>
   304   setup (Binding.name "no_vars") (Scan.succeed no_vars) "frozen schematic vars" #>
   305   setup (Binding.name "eta_long") (Scan.succeed eta_long)
   306     "put theorem into eta long beta normal form" #>
   307   setup (Binding.name "atomize") (Scan.succeed Object_Logic.declare_atomize)
   308     "declaration of atomize rule" #>
   309   setup (Binding.name "rulify") (Scan.succeed Object_Logic.declare_rulify)
   310     "declaration of rulify rule" #>
   311   setup (Binding.name "rotated") (Scan.lift rotated) "rotated theorem premises" #>
   312   setup (Binding.name "defn") (add_del Local_Defs.defn_add Local_Defs.defn_del)
   313     "declaration of definitional transformations" #>
   314   setup (Binding.name "abs_def") (Scan.succeed (Thm.rule_attribute (K Drule.abs_def)))
   315     "abstract over free variables of a definition"));
   316 
   317 
   318 
   319 (** configuration options **)
   320 
   321 (* naming *)
   322 
   323 structure Configs = Theory_Data
   324 (
   325   type T = Config.value Config.T Symtab.table;
   326   val empty = Symtab.empty;
   327   val extend = I;
   328   fun merge data = Symtab.merge (K true) data;
   329 );
   330 
   331 fun print_configs ctxt =
   332   let
   333     val thy = ProofContext.theory_of ctxt;
   334     fun prt (name, config) =
   335       let val value = Config.get ctxt config in
   336         Pretty.block [Pretty.str (name ^ ": " ^ Config.print_type value ^ " ="), Pretty.brk 1,
   337           Pretty.str (Config.print_value value)]
   338       end;
   339     val configs = Name_Space.extern_table (#1 (Attributes.get thy), Configs.get thy);
   340   in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
   341 
   342 
   343 (* concrete syntax *)
   344 
   345 local
   346 
   347 val equals = Parse.$$$ "=";
   348 
   349 fun scan_value (Config.Bool _) =
   350       equals -- Args.$$$ "false" >> K (Config.Bool false) ||
   351       equals -- Args.$$$ "true" >> K (Config.Bool true) ||
   352       Scan.succeed (Config.Bool true)
   353   | scan_value (Config.Int _) = equals |-- Parse.int >> Config.Int
   354   | scan_value (Config.String _) = equals |-- Args.name >> Config.String;
   355 
   356 fun scan_config thy config =
   357   let val config_type = Config.get_global thy config
   358   in scan_value config_type >> (K o Thm.declaration_attribute o K o Config.put_generic config) end;
   359 
   360 in
   361 
   362 fun register_config config thy =
   363   let
   364     val bname = Config.name_of config;
   365     val name = Sign.full_bname thy bname;
   366   in
   367     thy
   368     |> setup (Binding.name bname) (Scan.lift (scan_config thy config) >> Morphism.form)
   369       "configuration option"
   370     |> Configs.map (Symtab.update (name, config))
   371   end;
   372 
   373 fun declare_config make coerce global name default =
   374   let
   375     val config_value = Config.declare global name (make o default);
   376     val config = coerce config_value;
   377   in (config, register_config config_value) end;
   378 
   379 val config_bool   = declare_config Config.Bool Config.bool false;
   380 val config_int    = declare_config Config.Int Config.int false;
   381 val config_string = declare_config Config.String Config.string false;
   382 
   383 val config_bool_global   = declare_config Config.Bool Config.bool true;
   384 val config_int_global    = declare_config Config.Int Config.int true;
   385 val config_string_global = declare_config Config.String Config.string true;
   386 
   387 end;
   388 
   389 
   390 (* theory setup *)
   391 
   392 val _ = Context.>> (Context.map_theory
   393  (register_config Unify.trace_bound_value #>
   394   register_config Unify.search_bound_value #>
   395   register_config Unify.trace_simp_value #>
   396   register_config Unify.trace_types_value #>
   397   register_config MetaSimplifier.simp_depth_limit_value #>
   398   register_config MetaSimplifier.debug_simp_value #>
   399   register_config MetaSimplifier.trace_simp_value));
   400 
   401 end;