src/Pure/Isar/attrib.ML
author wenzelm
Tue Sep 25 17:06:18 2007 +0200 (2007-09-25)
changeset 24713 8b3b6d09ef40
parent 24238 ae70f95e31de
child 24723 2110df1e157d
permissions -rw-r--r--
tuned functor application;
     1 (*  Title:      Pure/Isar/attrib.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Symbolic representation of attributes -- with name and syntax.
     6 *)
     7 
     8 signature BASIC_ATTRIB =
     9 sig
    10   val print_attributes: theory -> unit
    11 end;
    12 
    13 signature ATTRIB =
    14 sig
    15   include BASIC_ATTRIB
    16   type src
    17   val intern: theory -> xstring -> string
    18   val intern_src: theory -> src -> src
    19   val pretty_attribs: Proof.context -> src list -> Pretty.T list
    20   val attribute: theory -> src -> attribute
    21   val attribute_i: theory -> src -> attribute
    22   val map_specs: ('a -> 'att) ->
    23     (('c * 'a list) * 'd) list -> (('c * 'att list) * 'd) list
    24   val map_facts: ('a -> 'att) ->
    25     (('c * 'a list) * ('d * 'a list) list) list ->
    26     (('c * 'att list) * ('d * 'att list) list) list
    27   val crude_closure: Proof.context -> src -> src
    28   val add_attributes: (bstring * (src -> attribute) * string) list -> theory -> theory
    29   val print_configs: Proof.context -> unit
    30   val register_config: Config.value Config.T -> theory -> theory
    31   val config_bool: bstring -> bool -> bool Config.T * (theory -> theory)
    32   val config_int: bstring -> int -> int Config.T * (theory -> theory)
    33   val config_string: bstring -> string -> string Config.T * (theory -> theory)
    34   val config_bool_global: bstring -> bool -> bool Config.T * (theory -> theory)
    35   val config_int_global: bstring -> int -> int Config.T * (theory -> theory)
    36   val config_string_global: bstring -> string -> string Config.T * (theory -> theory)
    37   val thm: Context.generic * Args.T list -> thm * (Context.generic * Args.T list)
    38   val thms: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
    39   val multi_thm: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
    40   val syntax: (Context.generic * Args.T list ->
    41     attribute * (Context.generic * Args.T list)) -> src -> attribute
    42   val no_args: attribute -> src -> attribute
    43   val add_del_args: attribute -> attribute -> src -> attribute
    44   val internal: (morphism -> attribute) -> src
    45 end;
    46 
    47 structure Attrib: ATTRIB =
    48 struct
    49 
    50 type src = Args.src;
    51 
    52 
    53 (** named attributes **)
    54 
    55 (* theory data *)
    56 
    57 structure Attributes = TheoryDataFun
    58 (
    59   type T = (((src -> attribute) * string) * stamp) NameSpace.table;
    60   val empty = NameSpace.empty_table;
    61   val copy = I;
    62   val extend = I;
    63   fun merge _ tables : T = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUP dup =>
    64     error ("Attempt to merge different versions of attribute " ^ quote dup);
    65 );
    66 
    67 fun print_attributes thy =
    68   let
    69     val attribs = Attributes.get thy;
    70     fun prt_attr (name, ((_, comment), _)) = Pretty.block
    71       [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
    72   in
    73     [Pretty.big_list "attributes:" (map prt_attr (NameSpace.extern_table attribs))]
    74     |> Pretty.chunks |> Pretty.writeln
    75   end;
    76 
    77 
    78 (* name space *)
    79 
    80 val intern = NameSpace.intern o #1 o Attributes.get;
    81 val intern_src = Args.map_name o intern;
    82 
    83 val extern = NameSpace.extern o #1 o Attributes.get o ProofContext.theory_of;
    84 
    85 
    86 (* pretty printing *)
    87 
    88 fun pretty_attribs _ [] = []
    89   | pretty_attribs ctxt srcs =
    90       [Pretty.enclose "[" "]"
    91         (Pretty.commas (map (Args.pretty_src ctxt o Args.map_name (extern ctxt)) srcs))];
    92 
    93 
    94 (* get attributes *)
    95 
    96 fun attribute_i thy =
    97   let
    98     val attrs = #2 (Attributes.get thy);
    99     fun attr src =
   100       let val ((name, _), pos) = Args.dest_src src in
   101         (case Symtab.lookup attrs name of
   102           NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
   103         | SOME ((att, _), _) => att src)
   104       end;
   105   in attr end;
   106 
   107 fun attribute thy = attribute_i thy o intern_src thy;
   108 
   109 
   110 (* attributed declarations *)
   111 
   112 fun map_specs f = map (apfst (apsnd (map f)));
   113 fun map_facts f = map (apfst (apsnd (map f)) o apsnd (map (apsnd (map f))));
   114 
   115 
   116 (* crude_closure *)
   117 
   118 (*Produce closure without knowing facts in advance! The following
   119   works reasonably well for attribute parsers that do not peek at the
   120   thm structure.*)
   121 
   122 fun crude_closure ctxt src =
   123  (try (fn () => attribute_i (ProofContext.theory_of ctxt) src
   124     (Context.Proof ctxt, Drule.asm_rl)) ();
   125   Args.closure src);
   126 
   127 
   128 (* add_attributes *)
   129 
   130 fun add_attributes raw_attrs thy =
   131   let
   132     val new_attrs =
   133       raw_attrs |> map (fn (name, att, comment) => (name, ((att, comment), stamp ())));
   134     fun add attrs = NameSpace.extend_table (Sign.naming_of thy) new_attrs attrs
   135       handle Symtab.DUP dup => error ("Duplicate declaration of attributes " ^ quote dup);
   136   in Attributes.map add thy end;
   137 
   138 
   139 
   140 (** attribute parsers **)
   141 
   142 (* tags *)
   143 
   144 fun tag x = Scan.lift (Args.name -- Args.name) x;
   145 
   146 
   147 (* theorems *)
   148 
   149 local
   150 
   151 val get_thms = Context.cases PureThy.get_thms ProofContext.get_thms;
   152 
   153 val fact_name = Args.internal_fact >> K "<fact>" || Args.name;
   154 
   155 fun gen_thm pick = Scan.depend (fn st =>
   156   Args.$$$ "[" |-- Args.attribs (intern (Context.theory_of st)) --| Args.$$$ "]"
   157     >> (fn srcs =>
   158       let
   159         val atts = map (attribute_i (Context.theory_of st)) srcs;
   160         val (st', th') = Library.apply atts (st, Drule.dummy_thm);
   161       in (st', pick "" [th']) end) ||
   162   (Scan.ahead Args.alt_name -- Args.named_fact (get_thms st o Fact)
   163     >> (fn (s, fact) => ("", Fact s, fact)) ||
   164   Scan.ahead fact_name -- Args.named_fact (get_thms st o Name) -- Args.thm_sel
   165     >> (fn ((name, fact), sel) => (name, NameSelection (name, sel), fact)) ||
   166   Scan.ahead fact_name -- Args.named_fact (get_thms st o Name)
   167     >> (fn (name, fact) => (name, Name name, fact)))
   168   -- Args.opt_attribs (intern (Context.theory_of st))
   169   >> (fn ((name, thmref, fact), srcs) =>
   170     let
   171       val ths = PureThy.select_thm thmref fact;
   172       val atts = map (attribute_i (Context.theory_of st)) srcs;
   173       val (st', ths') = foldl_map (Library.apply atts) (st, ths);
   174     in (st', pick name ths') end));
   175 
   176 in
   177 
   178 val thm = gen_thm PureThy.single_thm;
   179 val multi_thm = gen_thm (K I);
   180 val thms = Scan.repeat multi_thm >> flat;
   181 
   182 end;
   183 
   184 
   185 
   186 (** attribute syntax **)
   187 
   188 fun syntax scan src (st, th) =
   189   let val (f, st') = Args.syntax "attribute" scan src st
   190   in f (st', th) end;
   191 
   192 fun no_args x = syntax (Scan.succeed x);
   193 
   194 fun add_del_args add del x = syntax
   195   (Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add)) x;
   196 
   197 
   198 
   199 (** configuration options **)
   200 
   201 (* naming *)
   202 
   203 structure Configs = TheoryDataFun
   204 (
   205   type T = Config.value Config.T Symtab.table;
   206   val empty = Symtab.empty;
   207   val copy = I;
   208   val extend = I;
   209   fun merge _ = Symtab.merge (K true);
   210 );
   211 
   212 fun print_configs ctxt =
   213   let
   214     val thy = ProofContext.theory_of ctxt;
   215     fun prt (name, config) =
   216       let val value = Config.get ctxt config in
   217         Pretty.block [Pretty.str (name ^ ": " ^ Config.print_type value ^ " ="), Pretty.brk 1,
   218           Pretty.str (Config.print_value value)]
   219       end;
   220     val configs = NameSpace.extern_table (#1 (Attributes.get thy), Configs.get thy);
   221   in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
   222 
   223 
   224 (* concrete syntax *)
   225 
   226 local
   227 
   228 val equals = Args.$$$ "=";
   229 
   230 fun scan_value (Config.Bool _) =
   231       equals -- Args.$$$ "false" >> K (Config.Bool false) ||
   232       equals -- Args.$$$ "true" >> K (Config.Bool true) ||
   233       Scan.succeed (Config.Bool true)
   234   | scan_value (Config.Int _) = equals |-- Args.int >> Config.Int
   235   | scan_value (Config.String _) = equals |-- Args.name >> Config.String;
   236 
   237 fun scan_config thy config =
   238   let val config_type = Config.get_thy thy config
   239   in scan_value config_type >> (K o Thm.declaration_attribute o K o Config.put_generic config) end;
   240 
   241 in
   242 
   243 fun register_config config thy =
   244   let
   245     val bname = Config.name_of config;
   246     val name = Sign.full_name thy bname;
   247   in
   248     thy
   249     |> add_attributes [(bname, syntax (Scan.lift (scan_config thy config) >> Morphism.form),
   250       "configuration option")]
   251     |> Configs.map (Symtab.update (name, config))
   252   end;
   253 
   254 fun declare_config make coerce global name default =
   255   let
   256     val config_value = Config.declare global name (make default);
   257     val config = coerce config_value;
   258   in (config, register_config config_value) end;
   259 
   260 val config_bool   = declare_config Config.Bool Config.bool false;
   261 val config_int    = declare_config Config.Int Config.int false;
   262 val config_string = declare_config Config.String Config.string false;
   263 
   264 val config_bool_global   = declare_config Config.Bool Config.bool true;
   265 val config_int_global    = declare_config Config.Int Config.int true;
   266 val config_string_global = declare_config Config.String Config.string true;
   267 
   268 end;
   269 
   270 
   271 
   272 (** basic attributes **)
   273 
   274 (* tags *)
   275 
   276 fun tagged x = syntax (tag >> PureThy.tag) x;
   277 fun untagged x = syntax (Scan.lift Args.name >> PureThy.untag) x;
   278 
   279 fun kind x = syntax (Scan.lift Args.name >> PureThy.kind) x;
   280 
   281 
   282 (* rule composition *)
   283 
   284 val COMP_att =
   285   syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
   286     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B))));
   287 
   288 val THEN_att =
   289   syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
   290     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B))));
   291 
   292 val OF_att =
   293   syntax (thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A)));
   294 
   295 
   296 (* rename_abs *)
   297 
   298 fun rename_abs src = syntax
   299   (Scan.lift (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars'))) src;
   300 
   301 
   302 (* unfold / fold definitions *)
   303 
   304 fun unfolded_syntax rule =
   305   syntax (thms >>
   306     (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths)));
   307 
   308 val unfolded = unfolded_syntax LocalDefs.unfold;
   309 val folded = unfolded_syntax LocalDefs.fold;
   310 
   311 
   312 (* rule cases *)
   313 
   314 fun consumes x = syntax (Scan.lift (Scan.optional Args.nat 1) >> RuleCases.consumes) x;
   315 fun case_names x = syntax (Scan.lift (Scan.repeat1 Args.name) >> RuleCases.case_names) x;
   316 fun case_conclusion x =
   317   syntax (Scan.lift (Args.name -- Scan.repeat Args.name) >> RuleCases.case_conclusion) x;
   318 fun params x = syntax (Args.and_list1 (Scan.lift (Scan.repeat Args.name)) >> RuleCases.params) x;
   319 
   320 
   321 (* rule format *)
   322 
   323 fun rule_format_att x = syntax (Args.mode "no_asm"
   324   >> (fn true => ObjectLogic.rule_format_no_asm | false => ObjectLogic.rule_format)) x;
   325 
   326 fun elim_format x = no_args (Thm.rule_attribute (K Tactic.make_elim)) x;
   327 
   328 
   329 (* misc rules *)
   330 
   331 fun standard x = no_args (Thm.rule_attribute (K Drule.standard)) x;
   332 
   333 fun no_vars x = no_args (Thm.rule_attribute (fn ctxt => fn th =>
   334   let val ((_, [th']), _) = Variable.import_thms true [th] (Context.proof_of ctxt)
   335   in th' end)) x;
   336 
   337 fun eta_long x =
   338   no_args (Thm.rule_attribute (K (Conv.fconv_rule Drule.eta_long_conversion))) x;
   339 
   340 
   341 (* internal attribute *)
   342 
   343 fun internal att = Args.src (("Pure.attribute", [Args.mk_attribute att]), Position.none);
   344 
   345 fun internal_att x =
   346   syntax (Scan.lift Args.internal_attribute >> Morphism.form) x;
   347 
   348 
   349 (* attribute rotated *)
   350 
   351 fun rotated_att x = 
   352   syntax (Scan.lift (Scan.optional Args.int 1) >> 
   353                     (fn n => Thm.rule_attribute (fn _ => rotate_prems n))) x
   354 
   355 
   356 (* theory setup *)
   357 
   358 val _ = Context.add_setup
   359  (register_config Unify.trace_bound_value #>
   360   register_config Unify.search_bound_value #>
   361   register_config Unify.trace_simp_value #>
   362   register_config Unify.trace_types_value #>
   363   register_config MetaSimplifier.simp_depth_limit_value #>
   364   add_attributes
   365    [("tagged", tagged, "tagged theorem"),
   366     ("untagged", untagged, "untagged theorem"),
   367     ("kind", kind, "theorem kind"),
   368     ("COMP", COMP_att, "direct composition with rules (no lifting)"),
   369     ("THEN", THEN_att, "resolution with rule"),
   370     ("OF", OF_att, "rule applied to facts"),
   371     ("rename_abs", rename_abs, "rename bound variables in abstractions"),
   372     ("unfolded", unfolded, "unfolded definitions"),
   373     ("folded", folded, "folded definitions"),
   374     ("standard", standard, "result put into standard form"),
   375     ("elim_format", elim_format, "destruct rule turned into elimination rule format"),
   376     ("no_vars", no_vars, "frozen schematic vars"),
   377     ("eta_long", eta_long, "put theorem into eta long beta normal form"),
   378     ("consumes", consumes, "number of consumed facts"),
   379     ("case_names", case_names, "named rule cases"),
   380     ("case_conclusion", case_conclusion, "named conclusion of rule cases"),
   381     ("params", params, "named rule parameters"),
   382     ("atomize", no_args ObjectLogic.declare_atomize, "declaration of atomize rule"),
   383     ("rulify", no_args ObjectLogic.declare_rulify, "declaration of rulify rule"),
   384     ("rule_format", rule_format_att, "result put into standard rule format"),
   385     ("rotated", rotated_att, "rotated theorem premises"),
   386     ("defn", add_del_args LocalDefs.defn_add LocalDefs.defn_del,
   387       "declaration of definitional transformations"),
   388     ("attribute", internal_att, "internal attribute")]);
   389 
   390 end;
   391 
   392 structure BasicAttrib: BASIC_ATTRIB = Attrib;
   393 open BasicAttrib;