src/Pure/Isar/attrib.ML
author haftmann
Thu May 24 08:37:37 2007 +0200 (2007-05-24)
changeset 23086 12320f6e2523
parent 22900 f8a7c10e1bd0
child 23577 c5b93c69afd3
permissions -rw-r--r--
tuned Pure/General/name_space.ML
     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   exception ATTRIB_FAIL of (string * Position.T) * exn
    18   val intern: theory -> xstring -> string
    19   val intern_src: theory -> src -> src
    20   val pretty_attribs: Proof.context -> src list -> Pretty.T list
    21   val attribute: theory -> src -> attribute
    22   val attribute_i: theory -> src -> attribute
    23   val map_specs: ('a -> 'att) ->
    24     (('c * 'a list) * 'd) list -> (('c * 'att list) * 'd) list
    25   val map_facts: ('a -> 'att) ->
    26     (('c * 'a list) * ('d * 'a list) list) list ->
    27     (('c * 'att list) * ('d * 'att list) list) list
    28   val crude_closure: Proof.context -> src -> src
    29   val add_attributes: (bstring * (src -> attribute) * string) list -> theory -> theory
    30   val thm: Context.generic * Args.T list -> thm * (Context.generic * Args.T list)
    31   val thms: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
    32   val multi_thm: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
    33   val syntax: (Context.generic * Args.T list ->
    34     attribute * (Context.generic * Args.T list)) -> src -> attribute
    35   val no_args: attribute -> src -> attribute
    36   val add_del_args: attribute -> attribute -> src -> attribute
    37   val internal: (morphism -> attribute) -> src
    38 end;
    39 
    40 structure Attrib: ATTRIB =
    41 struct
    42 
    43 type src = Args.src;
    44 
    45 
    46 
    47 (** named attributes **)
    48 
    49 (* theory data *)
    50 
    51 structure AttributesData = TheoryDataFun
    52 (
    53   type T = (((src -> attribute) * string) * stamp) NameSpace.table;
    54   val empty = NameSpace.empty_table;
    55   val copy = I;
    56   val extend = I;
    57   fun merge _ tables = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUPS dups =>
    58     error ("Attempt to merge different versions of attribute(s) " ^ commas_quote dups);
    59 );
    60 
    61 fun print_attributes thy =
    62   let
    63     val attribs = AttributesData.get thy;
    64     fun prt_attr (name, ((_, comment), _)) = Pretty.block
    65       [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
    66   in
    67     [Pretty.big_list "attributes:" (map prt_attr (NameSpace.extern_table attribs))]
    68     |> Pretty.chunks |> Pretty.writeln
    69   end;
    70 
    71 
    72 (* name space *)
    73 
    74 val intern = NameSpace.intern o #1 o AttributesData.get;
    75 val intern_src = Args.map_name o intern;
    76 
    77 val extern = NameSpace.extern o #1 o AttributesData.get o ProofContext.theory_of;
    78 
    79 
    80 (* pretty printing *)
    81 
    82 fun pretty_attribs _ [] = []
    83   | pretty_attribs ctxt srcs =
    84       [Pretty.enclose "[" "]"
    85         (Pretty.commas (map (Args.pretty_src ctxt o Args.map_name (extern ctxt)) srcs))];
    86 
    87 
    88 (* get attributes *)
    89 
    90 exception ATTRIB_FAIL of (string * Position.T) * exn;
    91 
    92 fun attribute_i thy =
    93   let
    94     val attrs = #2 (AttributesData.get thy);
    95     fun attr src =
    96       let val ((name, _), pos) = Args.dest_src src in
    97         (case Symtab.lookup attrs name of
    98           NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
    99         | SOME ((att, _), _) => transform_failure (curry ATTRIB_FAIL (name, pos)) (att src))
   100       end;
   101   in attr end;
   102 
   103 fun attribute thy = attribute_i thy o intern_src thy;
   104 
   105 
   106 (* attributed declarations *)
   107 
   108 fun map_specs f = map (apfst (apsnd (map f)));
   109 fun map_facts f = map (apfst (apsnd (map f)) o apsnd (map (apsnd (map f))));
   110 
   111 
   112 (* crude_closure *)
   113 
   114 (*Produce closure without knowing facts in advance! The following
   115   works reasonably well for attribute parsers that do not peek at the
   116   thm structure.*)
   117 
   118 fun crude_closure ctxt src =
   119  (try (fn () => attribute_i (ProofContext.theory_of ctxt) src
   120     (Context.Proof ctxt, Drule.asm_rl)) ();
   121   Args.closure src);
   122 
   123 
   124 (* add_attributes *)
   125 
   126 fun add_attributes raw_attrs thy =
   127   let
   128     val new_attrs =
   129       raw_attrs |> map (fn (name, att, comment) => (name, ((att, comment), stamp ())));
   130     fun add attrs = NameSpace.extend_table (Sign.naming_of thy) new_attrs attrs
   131       handle Symtab.DUPS dups =>
   132         error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
   133   in AttributesData.map add thy end;
   134 
   135 
   136 
   137 (** attribute parsers **)
   138 
   139 (* tags *)
   140 
   141 fun tag x = Scan.lift (Args.name -- Scan.repeat Args.name) x;
   142 
   143 
   144 (* theorems *)
   145 
   146 local
   147 
   148 val get_thms = Context.cases PureThy.get_thms ProofContext.get_thms;
   149 
   150 val fact_name = Args.internal_fact >> K "<fact>" || Args.name;
   151 
   152 fun gen_thm pick = Scan.depend (fn st =>
   153  (Scan.ahead Args.alt_name -- Args.named_fact (get_thms st o Fact)
   154     >> (fn (s, fact) => ("", Fact s, fact)) ||
   155   Scan.ahead fact_name -- Args.named_fact (get_thms st o Name) -- Args.thm_sel
   156     >> (fn ((name, fact), sel) => (name, NameSelection (name, sel), fact)) ||
   157   Scan.ahead fact_name -- Args.named_fact (get_thms st o Name)
   158     >> (fn (name, fact) => (name, Name name, fact))) --
   159   Args.opt_attribs (intern (Context.theory_of st))
   160   >> (fn ((name, thmref, fact), srcs) =>
   161     let
   162       val ths = PureThy.select_thm thmref fact;
   163       val atts = map (attribute_i (Context.theory_of st)) srcs;
   164       val (st', ths') = foldl_map (Library.apply atts) (st, ths);
   165     in (st', pick name ths') end));
   166 
   167 in
   168 
   169 val thm = gen_thm PureThy.single_thm;
   170 val multi_thm = gen_thm (K I);
   171 val thms = Scan.repeat multi_thm >> flat;
   172 
   173 end;
   174 
   175 
   176 
   177 (** attribute syntax **)
   178 
   179 fun syntax scan src (st, th) =
   180   let val (f, st') = Args.syntax "attribute" scan src st
   181   in f (st', th) end;
   182 
   183 fun no_args x = syntax (Scan.succeed x);
   184 
   185 fun add_del_args add del x = syntax
   186   (Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add)) x;
   187 
   188 
   189 
   190 (** basic attributes **)
   191 
   192 (* tags *)
   193 
   194 fun tagged x = syntax (tag >> PureThy.tag) x;
   195 fun untagged x = syntax (Scan.lift Args.name >> PureThy.untag) x;
   196 
   197 fun kind x = syntax (Scan.lift Args.name >> PureThy.kind) x;
   198 
   199 
   200 (* rule composition *)
   201 
   202 val COMP_att =
   203   syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
   204     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B))));
   205 
   206 val THEN_att =
   207   syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
   208     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B))));
   209 
   210 val OF_att =
   211   syntax (thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A)));
   212 
   213 
   214 (* rename_abs *)
   215 
   216 fun rename_abs src = syntax
   217   (Scan.lift (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars'))) src;
   218 
   219 
   220 (* unfold / fold definitions *)
   221 
   222 fun unfolded_syntax rule =
   223   syntax (thms >>
   224     (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths)));
   225 
   226 val unfolded = unfolded_syntax LocalDefs.unfold;
   227 val folded = unfolded_syntax LocalDefs.fold;
   228 
   229 
   230 (* rule cases *)
   231 
   232 fun consumes x = syntax (Scan.lift (Scan.optional Args.nat 1) >> RuleCases.consumes) x;
   233 fun case_names x = syntax (Scan.lift (Scan.repeat1 Args.name) >> RuleCases.case_names) x;
   234 fun case_conclusion x =
   235   syntax (Scan.lift (Args.name -- Scan.repeat Args.name) >> RuleCases.case_conclusion) x;
   236 fun params x = syntax (Args.and_list1 (Scan.lift (Scan.repeat Args.name)) >> RuleCases.params) x;
   237 
   238 
   239 (* rule format *)
   240 
   241 fun rule_format_att x = syntax (Args.mode "no_asm"
   242   >> (fn true => ObjectLogic.rule_format_no_asm | false => ObjectLogic.rule_format)) x;
   243 
   244 fun elim_format x = no_args (Thm.rule_attribute (K Tactic.make_elim)) x;
   245 
   246 
   247 (* misc rules *)
   248 
   249 fun standard x = no_args (Thm.rule_attribute (K Drule.standard)) x;
   250 
   251 fun no_vars x = no_args (Thm.rule_attribute (fn ctxt => fn th =>
   252   let val ((_, [th']), _) = Variable.import_thms true [th] (Context.proof_of ctxt)
   253   in th' end)) x;
   254 
   255 fun eta_long x =
   256   no_args (Thm.rule_attribute (K (Conv.fconv_rule Drule.eta_long_conversion))) x;
   257 
   258 
   259 (* internal attribute *)
   260 
   261 fun internal att = Args.src (("Pure.attribute", [Args.mk_attribute att]), Position.none);
   262 
   263 fun internal_att x =
   264   syntax (Scan.lift Args.internal_attribute >> Morphism.form) x;
   265 
   266 
   267 (* theory setup *)
   268 
   269 val _ = Context.add_setup
   270  (add_attributes
   271    [("tagged", tagged, "tagged theorem"),
   272     ("untagged", untagged, "untagged theorem"),
   273     ("kind", kind, "theorem kind"),
   274     ("COMP", COMP_att, "direct composition with rules (no lifting)"),
   275     ("THEN", THEN_att, "resolution with rule"),
   276     ("OF", OF_att, "rule applied to facts"),
   277     ("rename_abs", rename_abs, "rename bound variables in abstractions"),
   278     ("unfolded", unfolded, "unfolded definitions"),
   279     ("folded", folded, "folded definitions"),
   280     ("standard", standard, "result put into standard form"),
   281     ("elim_format", elim_format, "destruct rule turned into elimination rule format"),
   282     ("no_vars", no_vars, "frozen schematic vars"),
   283     ("eta_long", eta_long, "put theorem into eta long beta normal form"),
   284     ("consumes", consumes, "number of consumed facts"),
   285     ("case_names", case_names, "named rule cases"),
   286     ("case_conclusion", case_conclusion, "named conclusion of rule cases"),
   287     ("params", params, "named rule parameters"),
   288     ("atomize", no_args ObjectLogic.declare_atomize, "declaration of atomize rule"),
   289     ("rulify", no_args ObjectLogic.declare_rulify, "declaration of rulify rule"),
   290     ("rule_format", rule_format_att, "result put into standard rule format"),
   291     ("defn", add_del_args LocalDefs.defn_add LocalDefs.defn_del,
   292       "declaration of definitional transformations"),
   293     ("attribute", internal_att, "internal attribute")]);
   294 
   295 end;
   296 
   297 structure BasicAttrib: BASIC_ATTRIB = Attrib;
   298 open BasicAttrib;