src/Pure/Isar/attrib.ML
author wenzelm
Sun Nov 01 15:24:45 2009 +0100 (2009-11-01)
changeset 33368 b1cf34f1855c
parent 33159 369da293bbd4
child 33522 737589bb9bb8
permissions -rw-r--r--
modernized structure Rule_Cases;
     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 = TheoryDataFun
    69 (
    70   type T = ((src -> attribute) * string) Name_Space.table;
    71   val empty : T = Name_Space.empty_table "attribute";
    72   val copy = I;
    73   val extend = I;
    74   fun merge _ tables : T = Name_Space.merge_tables tables;
    75 );
    76 
    77 fun print_attributes thy =
    78   let
    79     val attribs = Attributes.get thy;
    80     fun prt_attr (name, (_, comment)) = Pretty.block
    81       [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
    82   in
    83     [Pretty.big_list "attributes:" (map prt_attr (Name_Space.extern_table attribs))]
    84     |> Pretty.chunks |> Pretty.writeln
    85   end;
    86 
    87 fun add_attribute name att comment thy = thy
    88   |> Attributes.map (#2 o Name_Space.define true (Sign.naming_of thy) (name, (att, comment)));
    89 
    90 
    91 (* name space *)
    92 
    93 val intern = Name_Space.intern o #1 o Attributes.get;
    94 val intern_src = Args.map_name o intern;
    95 
    96 val extern = Name_Space.extern o #1 o Attributes.get o ProofContext.theory_of;
    97 
    98 
    99 (* pretty printing *)
   100 
   101 fun pretty_attribs _ [] = []
   102   | pretty_attribs ctxt srcs =
   103       [Pretty.enclose "[" "]"
   104         (Pretty.commas (map (Args.pretty_src ctxt o Args.map_name (extern ctxt)) srcs))];
   105 
   106 
   107 (* get attributes *)
   108 
   109 val defined = Symtab.defined o #2 o Attributes.get;
   110 
   111 fun attribute_i thy =
   112   let
   113     val attrs = #2 (Attributes.get thy);
   114     fun attr src =
   115       let val ((name, _), pos) = Args.dest_src src in
   116         (case Symtab.lookup attrs name of
   117           NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
   118         | SOME (att, _) => (Position.report (Markup.attribute name) pos; att src))
   119       end;
   120   in attr end;
   121 
   122 fun attribute thy = attribute_i thy o intern_src thy;
   123 
   124 fun eval_thms ctxt args = ProofContext.note_thmss Thm.generatedK
   125     [(Thm.empty_binding, args |> map (fn (a, atts) =>
   126         (ProofContext.get_fact ctxt a, map (attribute (ProofContext.theory_of ctxt)) atts)))] ctxt
   127   |> fst |> maps snd;
   128 
   129 
   130 (* attributed declarations *)
   131 
   132 fun map_specs f = map (apfst (apsnd (map f)));
   133 
   134 fun map_facts f = map (apfst (apsnd (map f)) o apsnd (map (apsnd (map f))));
   135 fun map_facts_refs f g = map_facts f #> map (apsnd (map (apfst g)));
   136 
   137 
   138 (* crude_closure *)
   139 
   140 (*Produce closure without knowing facts in advance! The following
   141   works reasonably well for attribute parsers that do not peek at the
   142   thm structure.*)
   143 
   144 fun crude_closure ctxt src =
   145  (try (fn () => attribute_i (ProofContext.theory_of ctxt) src
   146     (Context.Proof ctxt, Drule.asm_rl)) ();
   147   Args.closure src);
   148 
   149 
   150 (* attribute setup *)
   151 
   152 fun syntax scan = Args.syntax "attribute" scan;
   153 
   154 fun setup name scan =
   155   add_attribute name
   156     (fn src => fn (ctxt, th) => let val (a, ctxt') = syntax scan src ctxt in a (ctxt', th) end);
   157 
   158 fun attribute_setup name (txt, pos) cmt =
   159   Context.theory_map (ML_Context.expression pos
   160     "val (name, scan, comment): binding * attribute context_parser * string"
   161     "Context.map_theory (Attrib.setup name scan comment)"
   162     ("(" ^ ML_Syntax.make_binding name ^ ", " ^ txt ^ ", " ^ ML_Syntax.print_string cmt ^ ")"));
   163 
   164 
   165 (* add/del syntax *)
   166 
   167 fun add_del add del = Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add);
   168 
   169 
   170 
   171 (** parsing attributed theorems **)
   172 
   173 val thm_sel = P.$$$ "(" |-- P.list1
   174  (P.nat --| P.minus -- P.nat >> Facts.FromTo ||
   175   P.nat --| P.minus >> Facts.From ||
   176   P.nat >> Facts.Single) --| P.$$$ ")";
   177 
   178 local
   179 
   180 val fact_name = Args.internal_fact >> K "<fact>" || Args.name;
   181 
   182 fun gen_thm pick = Scan.depend (fn context =>
   183   let
   184     val thy = Context.theory_of context;
   185     val get = Context.cases (PureThy.get_fact context) ProofContext.get_fact context;
   186     val get_fact = get o Facts.Fact;
   187     fun get_named pos name = get (Facts.Named ((name, pos), NONE));
   188   in
   189     P.$$$ "[" |-- Args.attribs (intern thy) --| P.$$$ "]" >> (fn srcs =>
   190       let
   191         val atts = map (attribute_i thy) srcs;
   192         val (context', th') = Library.apply atts (context, Drule.dummy_thm);
   193       in (context', pick "" [th']) end)
   194     ||
   195     (Scan.ahead Args.alt_name -- Args.named_fact get_fact
   196       >> (fn (s, fact) => ("", Facts.Fact s, fact)) ||
   197      Scan.ahead (P.position fact_name) :|-- (fn (name, pos) =>
   198       Args.named_fact (get_named pos) -- Scan.option thm_sel
   199         >> (fn (fact, sel) => (name, Facts.Named ((name, pos), sel), fact))))
   200     -- Args.opt_attribs (intern thy) >> (fn ((name, thmref, fact), srcs) =>
   201       let
   202         val ths = Facts.select thmref fact;
   203         val atts = map (attribute_i thy) srcs;
   204         val (context', ths') = Library.foldl_map (Library.apply atts) (context, ths);
   205       in (context', pick name ths') end)
   206   end);
   207 
   208 in
   209 
   210 val thm = gen_thm Facts.the_single;
   211 val multi_thm = gen_thm (K I);
   212 val thms = Scan.repeat multi_thm >> flat;
   213 
   214 end;
   215 
   216 
   217 
   218 (** basic attributes **)
   219 
   220 (* internal *)
   221 
   222 fun internal att = Args.src (("Pure.attribute", [T.mk_attribute att]), Position.none);
   223 
   224 
   225 (* rule composition *)
   226 
   227 val COMP_att =
   228   Scan.lift (Scan.optional (Args.bracks P.nat) 1) -- thm
   229     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B)));
   230 
   231 val THEN_att =
   232   Scan.lift (Scan.optional (Args.bracks P.nat) 1) -- thm
   233     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B)));
   234 
   235 val OF_att =
   236   thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A));
   237 
   238 
   239 (* rename_abs *)
   240 
   241 fun rename_abs x = (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars')) x;
   242 
   243 
   244 (* unfold / fold definitions *)
   245 
   246 fun unfolded_syntax rule =
   247   thms >> (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths));
   248 
   249 val unfolded = unfolded_syntax LocalDefs.unfold;
   250 val folded = unfolded_syntax LocalDefs.fold;
   251 
   252 
   253 (* rule format *)
   254 
   255 val rule_format = Args.mode "no_asm"
   256   >> (fn true => ObjectLogic.rule_format_no_asm | false => ObjectLogic.rule_format);
   257 
   258 val elim_format = Thm.rule_attribute (K Tactic.make_elim);
   259 
   260 
   261 (* misc rules *)
   262 
   263 val no_vars = Thm.rule_attribute (fn context => fn th =>
   264   let
   265     val ctxt = Variable.set_body false (Context.proof_of context);
   266     val ((_, [th']), _) = Variable.import true [th] ctxt;
   267   in th' end);
   268 
   269 val eta_long =
   270   Thm.rule_attribute (K (Conv.fconv_rule Drule.eta_long_conversion));
   271 
   272 val rotated = Scan.optional P.int 1 >> (fn n => Thm.rule_attribute (K (rotate_prems n)));
   273 
   274 
   275 (* theory setup *)
   276 
   277 val _ = Context.>> (Context.map_theory
   278  (setup (Binding.name "attribute") (Scan.lift Args.internal_attribute >> Morphism.form)
   279     "internal attribute" #>
   280   setup (Binding.name "tagged") (Scan.lift (Args.name -- Args.name) >> Thm.tag) "tagged theorem" #>
   281   setup (Binding.name "untagged") (Scan.lift Args.name >> Thm.untag) "untagged theorem" #>
   282   setup (Binding.name "kind") (Scan.lift Args.name >> Thm.kind) "theorem kind" #>
   283   setup (Binding.name "COMP") COMP_att "direct composition with rules (no lifting)" #>
   284   setup (Binding.name "THEN") THEN_att "resolution with rule" #>
   285   setup (Binding.name "OF") OF_att "rule applied to facts" #>
   286   setup (Binding.name "rename_abs") (Scan.lift rename_abs)
   287     "rename bound variables in abstractions" #>
   288   setup (Binding.name "unfolded") unfolded "unfolded definitions" #>
   289   setup (Binding.name "folded") folded "folded definitions" #>
   290   setup (Binding.name "consumes") (Scan.lift (Scan.optional P.nat 1) >> Rule_Cases.consumes)
   291     "number of consumed facts" #>
   292   setup (Binding.name "case_names") (Scan.lift (Scan.repeat1 Args.name) >> Rule_Cases.case_names)
   293     "named rule cases" #>
   294   setup (Binding.name "case_conclusion")
   295     (Scan.lift (Args.name -- Scan.repeat Args.name) >> Rule_Cases.case_conclusion)
   296     "named conclusion of rule cases" #>
   297   setup (Binding.name "params")
   298     (Scan.lift (P.and_list1 (Scan.repeat Args.name)) >> Rule_Cases.params)
   299     "named rule parameters" #>
   300   setup (Binding.name "standard") (Scan.succeed (Thm.rule_attribute (K Drule.standard)))
   301     "result put into standard form (legacy)" #>
   302   setup (Binding.name "rule_format") rule_format "result put into canonical rule format" #>
   303   setup (Binding.name "elim_format") (Scan.succeed elim_format)
   304     "destruct rule turned into elimination rule format" #>
   305   setup (Binding.name "no_vars") (Scan.succeed no_vars) "frozen schematic vars" #>
   306   setup (Binding.name "eta_long") (Scan.succeed eta_long)
   307     "put theorem into eta long beta normal form" #>
   308   setup (Binding.name "atomize") (Scan.succeed ObjectLogic.declare_atomize)
   309     "declaration of atomize rule" #>
   310   setup (Binding.name "rulify") (Scan.succeed ObjectLogic.declare_rulify)
   311     "declaration of rulify rule" #>
   312   setup (Binding.name "rotated") (Scan.lift rotated) "rotated theorem premises" #>
   313   setup (Binding.name "defn") (add_del LocalDefs.defn_add LocalDefs.defn_del)
   314     "declaration of definitional transformations" #>
   315   setup (Binding.name "abs_def") (Scan.succeed (Thm.rule_attribute (K Drule.abs_def)))
   316     "abstract over free variables of a definition"));
   317 
   318 
   319 
   320 (** configuration options **)
   321 
   322 (* naming *)
   323 
   324 structure Configs = TheoryDataFun
   325 (
   326   type T = Config.value Config.T Symtab.table;
   327   val empty = Symtab.empty;
   328   val copy = I;
   329   val extend = I;
   330   fun merge _ = Symtab.merge (K true);
   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;