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