src/Pure/Isar/attrib.ML
author berghofe
Thu Jan 29 22:27:07 2009 +0100 (2009-01-29)
changeset 29690 c81f8b2967e1
parent 29581 b3b33e0298eb
child 30190 479806475f3c
child 30240 5b25fee0362c
permissions -rw-r--r--
Added abs_def attribute.
     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) * 'd) list -> (('c * 'att list) * 'd) 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 crude_closure: Proof.context -> src -> src
    26   val add_attributes: (bstring * (src -> attribute) * string) list -> theory -> theory
    27   val syntax: (Context.generic * Args.T list ->
    28     attribute * (Context.generic * Args.T list)) -> src -> attribute
    29   val no_args: attribute -> src -> attribute
    30   val add_del_args: attribute -> attribute -> src -> attribute
    31   val thm_sel: Args.T list -> Facts.interval list * Args.T list
    32   val thm: Context.generic * Args.T list -> thm * (Context.generic * Args.T list)
    33   val thms: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
    34   val multi_thm: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
    35   val print_configs: Proof.context -> unit
    36   val internal: (morphism -> attribute) -> src
    37   val register_config: Config.value Config.T -> theory -> theory
    38   val config_bool: bstring -> bool -> bool Config.T * (theory -> theory)
    39   val config_int: bstring -> int -> int Config.T * (theory -> theory)
    40   val config_string: bstring -> string -> string Config.T * (theory -> theory)
    41   val config_bool_global: bstring -> bool -> bool Config.T * (theory -> theory)
    42   val config_int_global: bstring -> int -> int Config.T * (theory -> theory)
    43   val config_string_global: bstring -> string -> string Config.T * (theory -> theory)
    44 end;
    45 
    46 structure Attrib: ATTRIB =
    47 struct
    48 
    49 structure T = OuterLex;
    50 structure P = OuterParse;
    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 = TheoryDataFun
    67 (
    68   type T = (((src -> attribute) * string) * stamp) NameSpace.table;
    69   val empty = NameSpace.empty_table;
    70   val copy = I;
    71   val extend = I;
    72   fun merge _ tables : T = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUP dup =>
    73     error ("Attempt to merge different versions of attribute " ^ quote dup);
    74 );
    75 
    76 fun print_attributes thy =
    77   let
    78     val attribs = Attributes.get thy;
    79     fun prt_attr (name, ((_, comment), _)) = Pretty.block
    80       [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
    81   in
    82     [Pretty.big_list "attributes:" (map prt_attr (NameSpace.extern_table attribs))]
    83     |> Pretty.chunks |> Pretty.writeln
    84   end;
    85 
    86 
    87 (* name space *)
    88 
    89 val intern = NameSpace.intern o #1 o Attributes.get;
    90 val intern_src = Args.map_name o intern;
    91 
    92 val extern = NameSpace.extern o #1 o Attributes.get o ProofContext.theory_of;
    93 
    94 
    95 (* pretty printing *)
    96 
    97 fun pretty_attribs _ [] = []
    98   | pretty_attribs ctxt srcs =
    99       [Pretty.enclose "[" "]"
   100         (Pretty.commas (map (Args.pretty_src ctxt o Args.map_name (extern ctxt)) srcs))];
   101 
   102 
   103 (* get attributes *)
   104 
   105 val defined = Symtab.defined o #2 o Attributes.get;
   106 
   107 fun attribute_i thy =
   108   let
   109     val attrs = #2 (Attributes.get thy);
   110     fun attr src =
   111       let val ((name, _), pos) = Args.dest_src src in
   112         (case Symtab.lookup attrs name of
   113           NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
   114         | SOME ((att, _), _) => (Position.report (Markup.attribute name) pos; att src))
   115       end;
   116   in attr end;
   117 
   118 fun attribute thy = attribute_i thy o intern_src thy;
   119 
   120 fun eval_thms ctxt args = ProofContext.note_thmss Thm.theoremK
   121     [((Binding.empty, []),
   122       map (apsnd (map (attribute (ProofContext.theory_of ctxt)))) args)] ctxt
   123   |> fst |> maps snd;
   124 
   125 
   126 (* attributed declarations *)
   127 
   128 fun map_specs f = map (apfst (apsnd (map f)));
   129 fun map_facts f = map (apfst (apsnd (map f)) o apsnd (map (apsnd (map f))));
   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 (* add_attributes *)
   145 
   146 fun add_attributes raw_attrs thy =
   147   let
   148     val new_attrs =
   149       raw_attrs |> map (fn (name, att, comment) => (Binding.name name, ((att, comment), stamp ())));
   150     fun add attrs = fold (snd oo NameSpace.bind (Sign.naming_of thy)) new_attrs attrs
   151       handle Symtab.DUP dup => error ("Duplicate declaration of attributes " ^ quote dup);
   152   in Attributes.map add thy end;
   153 
   154 
   155 (* attribute syntax *)
   156 
   157 fun syntax scan src (st, th) =
   158   let val (f: attribute, st') = Args.syntax "attribute" scan src st
   159   in f (st', th) end;
   160 
   161 fun no_args x = syntax (Scan.succeed x);
   162 
   163 fun add_del_args add del = syntax
   164   (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 = P.$$$ "(" |-- P.list1
   171  (P.nat --| P.minus -- P.nat >> Facts.FromTo ||
   172   P.nat --| P.minus >> Facts.From ||
   173   P.nat >> Facts.Single) --| P.$$$ ")";
   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     P.$$$ "[" |-- Args.attribs (intern thy) --| P.$$$ "]" >> (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 (P.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') = 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 val internal_att =
   222   syntax (Scan.lift Args.internal_attribute >> Morphism.form);
   223 
   224 
   225 (* tags *)
   226 
   227 val tagged = syntax (Scan.lift (Args.name -- Args.name) >> Thm.tag);
   228 val untagged = syntax (Scan.lift Args.name >> Thm.untag);
   229 
   230 val kind = syntax (Scan.lift Args.name >> Thm.kind);
   231 
   232 
   233 (* rule composition *)
   234 
   235 val COMP_att =
   236   syntax (Scan.lift (Scan.optional (Args.bracks P.nat) 1) -- thm
   237     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B))));
   238 
   239 val THEN_att =
   240   syntax (Scan.lift (Scan.optional (Args.bracks P.nat) 1) -- thm
   241     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B))));
   242 
   243 val OF_att =
   244   syntax (thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A)));
   245 
   246 
   247 (* rename_abs *)
   248 
   249 val rename_abs = syntax
   250   (Scan.lift (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars')));
   251 
   252 
   253 (* unfold / fold definitions *)
   254 
   255 fun unfolded_syntax rule =
   256   syntax (thms >>
   257     (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths)));
   258 
   259 val unfolded = unfolded_syntax LocalDefs.unfold;
   260 val folded = unfolded_syntax LocalDefs.fold;
   261 
   262 
   263 (* rule cases *)
   264 
   265 val consumes = syntax (Scan.lift (Scan.optional P.nat 1) >> RuleCases.consumes);
   266 val case_names = syntax (Scan.lift (Scan.repeat1 Args.name) >> RuleCases.case_names);
   267 val case_conclusion =
   268   syntax (Scan.lift (Args.name -- Scan.repeat Args.name) >> RuleCases.case_conclusion);
   269 val params = syntax (Scan.lift (P.and_list1 (Scan.repeat Args.name)) >> RuleCases.params);
   270 
   271 
   272 (* rule format *)
   273 
   274 val rule_format = syntax (Args.mode "no_asm"
   275   >> (fn true => ObjectLogic.rule_format_no_asm | false => ObjectLogic.rule_format));
   276 
   277 val elim_format = no_args (Thm.rule_attribute (K Tactic.make_elim));
   278 
   279 
   280 (* misc rules *)
   281 
   282 val standard = no_args (Thm.rule_attribute (K Drule.standard));
   283 
   284 val no_vars = no_args (Thm.rule_attribute (fn context => fn th =>
   285   let
   286     val ctxt = Variable.set_body false (Context.proof_of context);
   287     val ((_, [th']), _) = Variable.import_thms true [th] ctxt;
   288   in th' end));
   289 
   290 val eta_long =
   291   no_args (Thm.rule_attribute (K (Conv.fconv_rule Drule.eta_long_conversion)));
   292 
   293 val rotated = syntax
   294   (Scan.lift (Scan.optional P.int 1) >> (fn n => Thm.rule_attribute (K (rotate_prems n))));
   295 
   296 val abs_def = no_args (Thm.rule_attribute (K Drule.abs_def));
   297 
   298 
   299 (* theory setup *)
   300 
   301 val _ = Context.>> (Context.map_theory
   302  (add_attributes
   303    [("attribute", internal_att, "internal attribute"),
   304     ("tagged", tagged, "tagged theorem"),
   305     ("untagged", untagged, "untagged theorem"),
   306     ("kind", kind, "theorem kind"),
   307     ("COMP", COMP_att, "direct composition with rules (no lifting)"),
   308     ("THEN", THEN_att, "resolution with rule"),
   309     ("OF", OF_att, "rule applied to facts"),
   310     ("rename_abs", rename_abs, "rename bound variables in abstractions"),
   311     ("unfolded", unfolded, "unfolded definitions"),
   312     ("folded", folded, "folded definitions"),
   313     ("standard", standard, "result put into standard form"),
   314     ("elim_format", elim_format, "destruct rule turned into elimination rule format"),
   315     ("no_vars", no_vars, "frozen schematic vars"),
   316     ("eta_long", eta_long, "put theorem into eta long beta normal form"),
   317     ("consumes", consumes, "number of consumed facts"),
   318     ("case_names", case_names, "named rule cases"),
   319     ("case_conclusion", case_conclusion, "named conclusion of rule cases"),
   320     ("params", params, "named rule parameters"),
   321     ("atomize", no_args ObjectLogic.declare_atomize, "declaration of atomize rule"),
   322     ("rulify", no_args ObjectLogic.declare_rulify, "declaration of rulify rule"),
   323     ("rule_format", rule_format, "result put into standard rule format"),
   324     ("rotated", rotated, "rotated theorem premises"),
   325     ("defn", add_del_args LocalDefs.defn_add LocalDefs.defn_del,
   326       "declaration of definitional transformations"),
   327     ("abs_def", abs_def, "abstract over free variables of a definition")]));
   328 
   329 
   330 
   331 (** configuration options **)
   332 
   333 (* naming *)
   334 
   335 structure Configs = TheoryDataFun
   336 (
   337   type T = Config.value Config.T Symtab.table;
   338   val empty = Symtab.empty;
   339   val copy = I;
   340   val extend = I;
   341   fun merge _ = Symtab.merge (K true);
   342 );
   343 
   344 fun print_configs ctxt =
   345   let
   346     val thy = ProofContext.theory_of ctxt;
   347     fun prt (name, config) =
   348       let val value = Config.get ctxt config in
   349         Pretty.block [Pretty.str (name ^ ": " ^ Config.print_type value ^ " ="), Pretty.brk 1,
   350           Pretty.str (Config.print_value value)]
   351       end;
   352     val configs = NameSpace.extern_table (#1 (Attributes.get thy), Configs.get thy);
   353   in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
   354 
   355 
   356 (* concrete syntax *)
   357 
   358 local
   359 
   360 val equals = P.$$$ "=";
   361 
   362 fun scan_value (Config.Bool _) =
   363       equals -- Args.$$$ "false" >> K (Config.Bool false) ||
   364       equals -- Args.$$$ "true" >> K (Config.Bool true) ||
   365       Scan.succeed (Config.Bool true)
   366   | scan_value (Config.Int _) = equals |-- P.int >> Config.Int
   367   | scan_value (Config.String _) = equals |-- Args.name >> Config.String;
   368 
   369 fun scan_config thy config =
   370   let val config_type = Config.get_thy thy config
   371   in scan_value config_type >> (K o Thm.declaration_attribute o K o Config.put_generic config) end;
   372 
   373 in
   374 
   375 fun register_config config thy =
   376   let
   377     val bname = Config.name_of config;
   378     val name = Sign.full_bname thy bname;
   379   in
   380     thy
   381     |> add_attributes [(bname, syntax (Scan.lift (scan_config thy config) >> Morphism.form),
   382       "configuration option")]
   383     |> Configs.map (Symtab.update (name, config))
   384   end;
   385 
   386 fun declare_config make coerce global name default =
   387   let
   388     val config_value = Config.declare global name (make default);
   389     val config = coerce config_value;
   390   in (config, register_config config_value) end;
   391 
   392 val config_bool   = declare_config Config.Bool Config.bool false;
   393 val config_int    = declare_config Config.Int Config.int false;
   394 val config_string = declare_config Config.String Config.string false;
   395 
   396 val config_bool_global   = declare_config Config.Bool Config.bool true;
   397 val config_int_global    = declare_config Config.Int Config.int true;
   398 val config_string_global = declare_config Config.String Config.string true;
   399 
   400 end;
   401 
   402 
   403 (* theory setup *)
   404 
   405 val _ = Context.>> (Context.map_theory
   406  (register_config Unify.trace_bound_value #>
   407   register_config Unify.search_bound_value #>
   408   register_config Unify.trace_simp_value #>
   409   register_config Unify.trace_types_value #>
   410   register_config MetaSimplifier.simp_depth_limit_value));
   411 
   412 end;