src/Pure/Isar/attrib.ML
author wenzelm
Sun Jul 08 19:51:58 2007 +0200 (2007-07-08)
changeset 23655 d2d1138e0ddc
parent 23577 c5b93c69afd3
child 23937 66e1f24d655d
permissions -rw-r--r--
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
     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 : T = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUP dup =>
    58     error ("Attempt to merge different versions of attribute " ^ quote dup);
    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.DUP dup => error ("Duplicate declaration of attributes " ^ quote dup);
   132   in AttributesData.map add thy end;
   133 
   134 
   135 
   136 (** attribute parsers **)
   137 
   138 (* tags *)
   139 
   140 fun tag x = Scan.lift (Args.name -- Args.name) x;
   141 
   142 
   143 (* theorems *)
   144 
   145 local
   146 
   147 val get_thms = Context.cases PureThy.get_thms ProofContext.get_thms;
   148 
   149 val fact_name = Args.internal_fact >> K "<fact>" || Args.name;
   150 
   151 fun gen_thm pick = Scan.depend (fn st =>
   152  (Scan.ahead Args.alt_name -- Args.named_fact (get_thms st o Fact)
   153     >> (fn (s, fact) => ("", Fact s, fact)) ||
   154   Scan.ahead fact_name -- Args.named_fact (get_thms st o Name) -- Args.thm_sel
   155     >> (fn ((name, fact), sel) => (name, NameSelection (name, sel), fact)) ||
   156   Scan.ahead fact_name -- Args.named_fact (get_thms st o Name)
   157     >> (fn (name, fact) => (name, Name name, fact))) --
   158   Args.opt_attribs (intern (Context.theory_of st))
   159   >> (fn ((name, thmref, fact), srcs) =>
   160     let
   161       val ths = PureThy.select_thm thmref fact;
   162       val atts = map (attribute_i (Context.theory_of st)) srcs;
   163       val (st', ths') = foldl_map (Library.apply atts) (st, ths);
   164     in (st', pick name ths') end));
   165 
   166 in
   167 
   168 val thm = gen_thm PureThy.single_thm;
   169 val multi_thm = gen_thm (K I);
   170 val thms = Scan.repeat multi_thm >> flat;
   171 
   172 end;
   173 
   174 
   175 
   176 (** attribute syntax **)
   177 
   178 fun syntax scan src (st, th) =
   179   let val (f, st') = Args.syntax "attribute" scan src st
   180   in f (st', th) end;
   181 
   182 fun no_args x = syntax (Scan.succeed x);
   183 
   184 fun add_del_args add del x = syntax
   185   (Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add)) x;
   186 
   187 
   188 
   189 (** basic attributes **)
   190 
   191 (* tags *)
   192 
   193 fun tagged x = syntax (tag >> PureThy.tag) x;
   194 fun untagged x = syntax (Scan.lift Args.name >> PureThy.untag) x;
   195 
   196 fun kind x = syntax (Scan.lift Args.name >> PureThy.kind) x;
   197 
   198 
   199 (* rule composition *)
   200 
   201 val COMP_att =
   202   syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
   203     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B))));
   204 
   205 val THEN_att =
   206   syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
   207     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B))));
   208 
   209 val OF_att =
   210   syntax (thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A)));
   211 
   212 
   213 (* rename_abs *)
   214 
   215 fun rename_abs src = syntax
   216   (Scan.lift (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars'))) src;
   217 
   218 
   219 (* unfold / fold definitions *)
   220 
   221 fun unfolded_syntax rule =
   222   syntax (thms >>
   223     (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths)));
   224 
   225 val unfolded = unfolded_syntax LocalDefs.unfold;
   226 val folded = unfolded_syntax LocalDefs.fold;
   227 
   228 
   229 (* rule cases *)
   230 
   231 fun consumes x = syntax (Scan.lift (Scan.optional Args.nat 1) >> RuleCases.consumes) x;
   232 fun case_names x = syntax (Scan.lift (Scan.repeat1 Args.name) >> RuleCases.case_names) x;
   233 fun case_conclusion x =
   234   syntax (Scan.lift (Args.name -- Scan.repeat Args.name) >> RuleCases.case_conclusion) x;
   235 fun params x = syntax (Args.and_list1 (Scan.lift (Scan.repeat Args.name)) >> RuleCases.params) x;
   236 
   237 
   238 (* rule format *)
   239 
   240 fun rule_format_att x = syntax (Args.mode "no_asm"
   241   >> (fn true => ObjectLogic.rule_format_no_asm | false => ObjectLogic.rule_format)) x;
   242 
   243 fun elim_format x = no_args (Thm.rule_attribute (K Tactic.make_elim)) x;
   244 
   245 
   246 (* misc rules *)
   247 
   248 fun standard x = no_args (Thm.rule_attribute (K Drule.standard)) x;
   249 
   250 fun no_vars x = no_args (Thm.rule_attribute (fn ctxt => fn th =>
   251   let val ((_, [th']), _) = Variable.import_thms true [th] (Context.proof_of ctxt)
   252   in th' end)) x;
   253 
   254 fun eta_long x =
   255   no_args (Thm.rule_attribute (K (Conv.fconv_rule Drule.eta_long_conversion))) x;
   256 
   257 
   258 (* internal attribute *)
   259 
   260 fun internal att = Args.src (("Pure.attribute", [Args.mk_attribute att]), Position.none);
   261 
   262 fun internal_att x =
   263   syntax (Scan.lift Args.internal_attribute >> Morphism.form) x;
   264 
   265 
   266 (* theory setup *)
   267 
   268 val _ = Context.add_setup
   269  (add_attributes
   270    [("tagged", tagged, "tagged theorem"),
   271     ("untagged", untagged, "untagged theorem"),
   272     ("kind", kind, "theorem kind"),
   273     ("COMP", COMP_att, "direct composition with rules (no lifting)"),
   274     ("THEN", THEN_att, "resolution with rule"),
   275     ("OF", OF_att, "rule applied to facts"),
   276     ("rename_abs", rename_abs, "rename bound variables in abstractions"),
   277     ("unfolded", unfolded, "unfolded definitions"),
   278     ("folded", folded, "folded definitions"),
   279     ("standard", standard, "result put into standard form"),
   280     ("elim_format", elim_format, "destruct rule turned into elimination rule format"),
   281     ("no_vars", no_vars, "frozen schematic vars"),
   282     ("eta_long", eta_long, "put theorem into eta long beta normal form"),
   283     ("consumes", consumes, "number of consumed facts"),
   284     ("case_names", case_names, "named rule cases"),
   285     ("case_conclusion", case_conclusion, "named conclusion of rule cases"),
   286     ("params", params, "named rule parameters"),
   287     ("atomize", no_args ObjectLogic.declare_atomize, "declaration of atomize rule"),
   288     ("rulify", no_args ObjectLogic.declare_rulify, "declaration of rulify rule"),
   289     ("rule_format", rule_format_att, "result put into standard rule format"),
   290     ("defn", add_del_args LocalDefs.defn_add LocalDefs.defn_del,
   291       "declaration of definitional transformations"),
   292     ("attribute", internal_att, "internal attribute")]);
   293 
   294 end;
   295 
   296 structure BasicAttrib: BASIC_ATTRIB = Attrib;
   297 open BasicAttrib;