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