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