src/Pure/Isar/attrib.ML
author wenzelm
Wed Nov 16 21:16:36 2011 +0100 (2011-11-16)
changeset 45526 20a82863d5ef
parent 45493 12453fd8dcff
child 45527 d2b3d16b673a
permissions -rw-r--r--
clarified Attrib.partial_evaluation;
     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 map_specs: ('a list -> 'att list) ->
    20     (('c * 'a list) * 'b) list -> (('c * 'att list) * 'b) list
    21   val map_facts: ('a list -> 'att list) ->
    22     (('c * 'a list) * ('d * 'a list) list) list ->
    23     (('c * 'att list) * ('d * 'att list) list) list
    24   val map_facts_refs: ('a list -> 'att list) -> ('b -> 'fact) ->
    25     (('c * 'a list) * ('b * 'a list) list) list ->
    26     (('c * 'att list) * ('fact * 'att list) list) list
    27   val eval_thms: Proof.context -> (Facts.ref * src list) list -> thm list
    28   val setup: Binding.binding -> attribute context_parser -> string -> theory -> theory
    29   val attribute_setup: bstring * Position.T -> Symbol_Pos.text * Position.T -> string ->
    30     theory -> theory
    31   val add_del: attribute -> attribute -> attribute context_parser
    32   val thm_sel: Facts.interval list parser
    33   val thm: thm context_parser
    34   val thms: thm list context_parser
    35   val multi_thm: thm list context_parser
    36   val partial_evaluation: Proof.context ->
    37     (binding * (thm list * Args.src list) list) list ->
    38     (binding * (thm list * Args.src list) list) list
    39   val internal: (morphism -> attribute) -> src
    40   val print_configs: Proof.context -> unit
    41   val config_bool: Binding.binding ->
    42     (Context.generic -> bool) -> bool Config.T * (theory -> theory)
    43   val config_int: Binding.binding ->
    44     (Context.generic -> int) -> int Config.T * (theory -> theory)
    45   val config_real: Binding.binding ->
    46     (Context.generic -> real) -> real Config.T * (theory -> theory)
    47   val config_string: Binding.binding ->
    48     (Context.generic -> string) -> string Config.T * (theory -> theory)
    49   val setup_config_bool: Binding.binding -> (Context.generic -> bool) -> bool Config.T
    50   val setup_config_int: Binding.binding -> (Context.generic -> int) -> int Config.T
    51   val setup_config_string: Binding.binding -> (Context.generic -> string) -> string Config.T
    52   val setup_config_real: Binding.binding -> (Context.generic -> real) -> real Config.T
    53 end;
    54 
    55 structure Attrib: ATTRIB =
    56 struct
    57 
    58 (* source and bindings *)
    59 
    60 type src = Args.src;
    61 
    62 type binding = binding * src list;
    63 val empty_binding: binding = (Binding.empty, []);
    64 
    65 
    66 
    67 (** named attributes **)
    68 
    69 (* theory data *)
    70 
    71 structure Attributes = Theory_Data
    72 (
    73   type T = ((src -> attribute) * string) Name_Space.table;
    74   val empty : T = Name_Space.empty_table "attribute";
    75   val extend = I;
    76   fun merge data : T = Name_Space.merge_tables data;
    77 );
    78 
    79 fun print_attributes thy =
    80   let
    81     val ctxt = Proof_Context.init_global thy;
    82     val attribs = Attributes.get thy;
    83     fun prt_attr (name, (_, "")) = Pretty.str name
    84       | prt_attr (name, (_, comment)) =
    85           Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
    86   in
    87     [Pretty.big_list "attributes:" (map prt_attr (Name_Space.extern_table ctxt attribs))]
    88     |> Pretty.chunks |> Pretty.writeln
    89   end;
    90 
    91 fun add_attribute name att comment thy = thy
    92   |> Attributes.map
    93     (Name_Space.define (Proof_Context.init_global thy) true (Sign.naming_of thy)
    94       (name, (att, comment)) #> snd);
    95 
    96 
    97 (* name space *)
    98 
    99 val intern = Name_Space.intern o #1 o Attributes.get;
   100 val intern_src = Args.map_name o intern;
   101 
   102 fun extern ctxt = Name_Space.extern ctxt (#1 (Attributes.get (Proof_Context.theory_of ctxt)));
   103 
   104 
   105 (* pretty printing *)
   106 
   107 fun pretty_attribs _ [] = []
   108   | pretty_attribs ctxt srcs =
   109       [Pretty.enum "," "[" "]" (map (Args.pretty_src ctxt o Args.map_name (extern ctxt)) srcs)];
   110 
   111 
   112 (* get attributes *)
   113 
   114 val defined = Symtab.defined o #2 o Attributes.get;
   115 
   116 fun attribute_i thy =
   117   let
   118     val (space, tab) = Attributes.get thy;
   119     fun attr src =
   120       let val ((name, _), pos) = Args.dest_src src in
   121         (case Symtab.lookup tab name of
   122           NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
   123         | SOME (att, _) => (Position.report pos (Name_Space.markup space name); att src))
   124       end;
   125   in attr end;
   126 
   127 fun attribute thy = attribute_i thy o intern_src thy;
   128 
   129 
   130 (* attributed declarations *)
   131 
   132 fun map_specs f = map (apfst (apsnd f));
   133 
   134 fun map_facts f = map (apfst (apsnd f) o apsnd (map (apsnd f)));
   135 fun map_facts_refs f g = map_facts f #> map (apsnd (map (apfst g)));
   136 
   137 
   138 (* fact expressions *)
   139 
   140 fun eval_thms ctxt srcs = ctxt
   141   |> Proof_Context.note_thmss ""
   142     (map_facts_refs (map (attribute (Proof_Context.theory_of ctxt))) (Proof_Context.get_fact ctxt)
   143       [((Binding.empty, []), srcs)])
   144   |> fst |> maps snd;
   145 
   146 
   147 (* attribute setup *)
   148 
   149 fun syntax scan = Args.syntax "attribute" scan;
   150 
   151 fun setup name scan =
   152   add_attribute name
   153     (fn src => fn (ctxt, th) => let val (a, ctxt') = syntax scan src ctxt in a (ctxt', th) end);
   154 
   155 fun attribute_setup name (txt, pos) cmt =
   156   Context.theory_map (ML_Context.expression pos
   157     "val (name, scan, comment): binding * attribute context_parser * string"
   158     "Context.map_theory (Attrib.setup name scan comment)"
   159     (ML_Lex.read Position.none ("(" ^ ML_Syntax.make_binding name ^ ", ") @
   160       ML_Lex.read pos txt @
   161       ML_Lex.read Position.none (", " ^ 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 = Parse.$$$ "(" |-- Parse.list1
   173  (Parse.nat --| Parse.minus -- Parse.nat >> Facts.FromTo ||
   174   Parse.nat --| Parse.minus >> Facts.From ||
   175   Parse.nat >> Facts.Single) --| Parse.$$$ ")";
   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 (Global_Theory.get_fact context) Proof_Context.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     Parse.$$$ "[" |-- Args.attribs (intern thy) --| Parse.$$$ "]" >> (fn srcs =>
   189       let
   190         val atts = map (attribute_i thy) srcs;
   191         val (context', th') =
   192           Library.apply (map Thm.apply_attribute 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 (Parse.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') =
   205           Library.foldl_map (Library.apply (map Thm.apply_attribute atts)) (context, ths);
   206       in (context', pick name ths') end)
   207   end);
   208 
   209 in
   210 
   211 val thm = gen_thm Facts.the_single;
   212 val multi_thm = gen_thm (K I);
   213 val thms = Scan.repeat multi_thm >> flat;
   214 
   215 end;
   216 
   217 
   218 
   219 (** partial evaluation **)
   220 
   221 local
   222 
   223 val strict_thm_eq = op = o pairself Thm.rep_thm;
   224 
   225 fun apply_att src (context, th) =
   226   let
   227     val src1 = Args.assignable src;
   228     val result = attribute_i (Context.theory_of context) src1 (context, th);
   229     val src2 = Args.closure src1;
   230   in (src2, result) end;
   231 
   232 fun eval src (th, (decls, context)) =
   233   (case apply_att src (context, th) of
   234     (_, (NONE, SOME th')) => (th', (decls, context))
   235   | (src', (opt_context', opt_th')) =>
   236       let
   237         val context' = the_default context opt_context';
   238         val th' = the_default th opt_th';
   239         val decls' =
   240           (case decls of
   241             [] => [(th, [src'])]
   242           | (th2, srcs2) :: rest =>
   243               if strict_thm_eq (th, th2)
   244               then ((th2, src' :: srcs2) :: rest)
   245               else (th, [src']) :: (th2, srcs2) :: rest);
   246       in (th', (decls', context')) end);
   247 
   248 in
   249 
   250 fun partial_evaluation ctxt facts =
   251   let
   252     val (facts', (decls, _)) =
   253       (facts, ([], Context.Proof ctxt)) |-> fold_map (fn ((b, more_atts), fact) => fn res =>
   254         let
   255           val (ths', res') =
   256             (fact, res) |-> fold_map (fn (ths, atts) =>
   257               fold_map (curry (fold eval (atts @ more_atts))) ths)
   258             |>> flat;
   259         in (((b, []), [(ths', [])]), res') end);
   260     val decl_fact = (empty_binding, rev (map (fn (th, atts) => ([th], rev atts)) decls));
   261   in decl_fact :: facts' end;
   262 
   263 end;
   264 
   265 
   266 
   267 (** basic attributes **)
   268 
   269 (* internal *)
   270 
   271 fun internal att = Args.src (("Pure.attribute", [Token.mk_attribute att]), Position.none);
   272 
   273 
   274 (* rule composition *)
   275 
   276 val COMP_att =
   277   Scan.lift (Scan.optional (Args.bracks Parse.nat) 1) -- thm
   278     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B)));
   279 
   280 val THEN_att =
   281   Scan.lift (Scan.optional (Args.bracks Parse.nat) 1) -- thm
   282     >> (fn (i, B) => Thm.rule_attribute (fn _ => fn A => A RSN (i, B)));
   283 
   284 val OF_att =
   285   thms >> (fn Bs => Thm.rule_attribute (fn _ => fn A => Bs MRS A));
   286 
   287 
   288 (* rename_abs *)
   289 
   290 val rename_abs =
   291   Scan.repeat (Args.maybe Args.name)
   292   >> (fn args => Thm.rule_attribute (K (Drule.rename_bvars' args)));
   293 
   294 
   295 (* unfold / fold definitions *)
   296 
   297 fun unfolded_syntax rule =
   298   thms >> (fn ths => Thm.rule_attribute (fn context => rule (Context.proof_of context) ths));
   299 
   300 val unfolded = unfolded_syntax Local_Defs.unfold;
   301 val folded = unfolded_syntax Local_Defs.fold;
   302 
   303 
   304 (* rule format *)
   305 
   306 val rule_format = Args.mode "no_asm"
   307   >> (fn true => Object_Logic.rule_format_no_asm | false => Object_Logic.rule_format);
   308 
   309 val elim_format = Thm.rule_attribute (K Tactic.make_elim);
   310 
   311 
   312 (* case names *)
   313 
   314 val case_names =
   315   Scan.repeat1 (Args.name --
   316     Scan.optional (Parse.$$$ "[" |-- Scan.repeat1 (Args.maybe Args.name) --| Parse.$$$ "]") []) >>
   317   (fn cs =>
   318     Rule_Cases.cases_hyp_names (map fst cs)
   319       (map (map (the_default Rule_Cases.case_hypsN) o snd) cs));
   320 
   321 
   322 (* misc rules *)
   323 
   324 val no_vars = Thm.rule_attribute (fn context => fn th =>
   325   let
   326     val ctxt = Variable.set_body false (Context.proof_of context);
   327     val ((_, [th']), _) = Variable.import true [th] ctxt;
   328   in th' end);
   329 
   330 val eta_long =
   331   Thm.rule_attribute (K (Conv.fconv_rule Drule.eta_long_conversion));
   332 
   333 val rotated = Scan.optional Parse.int 1 >> (fn n => Thm.rule_attribute (K (rotate_prems n)));
   334 
   335 
   336 (* theory setup *)
   337 
   338 val _ = Context.>> (Context.map_theory
   339  (setup (Binding.name "attribute") (Scan.lift Args.internal_attribute >> Morphism.form)
   340     "internal attribute" #>
   341   setup (Binding.name "tagged") (Scan.lift (Args.name -- Args.name) >> Thm.tag) "tagged theorem" #>
   342   setup (Binding.name "untagged") (Scan.lift Args.name >> Thm.untag) "untagged theorem" #>
   343   setup (Binding.name "kind") (Scan.lift Args.name >> Thm.kind) "theorem kind" #>
   344   setup (Binding.name "COMP") COMP_att "direct composition with rules (no lifting)" #>
   345   setup (Binding.name "THEN") THEN_att "resolution with rule" #>
   346   setup (Binding.name "OF") OF_att "rule applied to facts" #>
   347   setup (Binding.name "rename_abs") (Scan.lift rename_abs)
   348     "rename bound variables in abstractions" #>
   349   setup (Binding.name "unfolded") unfolded "unfolded definitions" #>
   350   setup (Binding.name "folded") folded "folded definitions" #>
   351   setup (Binding.name "consumes") (Scan.lift (Scan.optional Parse.nat 1) >> Rule_Cases.consumes)
   352     "number of consumed facts" #>
   353   setup (Binding.name "constraints") (Scan.lift Parse.nat >> Rule_Cases.constraints)
   354     "number of equality constraints" #>
   355   setup (Binding.name "case_names") (Scan.lift case_names) "named rule cases" #>
   356   setup (Binding.name "case_conclusion")
   357     (Scan.lift (Args.name -- Scan.repeat Args.name) >> Rule_Cases.case_conclusion)
   358     "named conclusion of rule cases" #>
   359   setup (Binding.name "params")
   360     (Scan.lift (Parse.and_list1 (Scan.repeat Args.name)) >> Rule_Cases.params)
   361     "named rule parameters" #>
   362   setup (Binding.name "standard") (Scan.succeed (Thm.rule_attribute (K Drule.export_without_context)))
   363     "result put into standard form (legacy)" #>
   364   setup (Binding.name "rule_format") rule_format "result put into canonical rule format" #>
   365   setup (Binding.name "elim_format") (Scan.succeed elim_format)
   366     "destruct rule turned into elimination rule format" #>
   367   setup (Binding.name "no_vars") (Scan.succeed no_vars) "frozen schematic vars" #>
   368   setup (Binding.name "eta_long") (Scan.succeed eta_long)
   369     "put theorem into eta long beta normal form" #>
   370   setup (Binding.name "atomize") (Scan.succeed Object_Logic.declare_atomize)
   371     "declaration of atomize rule" #>
   372   setup (Binding.name "rulify") (Scan.succeed Object_Logic.declare_rulify)
   373     "declaration of rulify rule" #>
   374   setup (Binding.name "rotated") (Scan.lift rotated) "rotated theorem premises" #>
   375   setup (Binding.name "defn") (add_del Local_Defs.defn_add Local_Defs.defn_del)
   376     "declaration of definitional transformations" #>
   377   setup (Binding.name "abs_def") (Scan.succeed (Thm.rule_attribute (K Drule.abs_def)))
   378     "abstract over free variables of a definition"));
   379 
   380 
   381 
   382 (** configuration options **)
   383 
   384 (* naming *)
   385 
   386 structure Configs = Theory_Data
   387 (
   388   type T = Config.raw Symtab.table;
   389   val empty = Symtab.empty;
   390   val extend = I;
   391   fun merge data = Symtab.merge (K true) data;
   392 );
   393 
   394 fun print_configs ctxt =
   395   let
   396     val thy = Proof_Context.theory_of ctxt;
   397     fun prt (name, config) =
   398       let val value = Config.get ctxt config in
   399         Pretty.block [Pretty.str (name ^ ": " ^ Config.print_type value ^ " ="), Pretty.brk 1,
   400           Pretty.str (Config.print_value value)]
   401       end;
   402     val configs = Name_Space.extern_table ctxt (#1 (Attributes.get thy), Configs.get thy);
   403   in Pretty.writeln (Pretty.big_list "configuration options" (map prt configs)) end;
   404 
   405 
   406 (* concrete syntax *)
   407 
   408 local
   409 
   410 val equals = Parse.$$$ "=";
   411 
   412 fun scan_value (Config.Bool _) =
   413       equals -- Args.$$$ "false" >> K (Config.Bool false) ||
   414       equals -- Args.$$$ "true" >> K (Config.Bool true) ||
   415       Scan.succeed (Config.Bool true)
   416   | scan_value (Config.Int _) = equals |-- Parse.int >> Config.Int
   417   | scan_value (Config.Real _) = equals |-- Parse.real >> Config.Real
   418   | scan_value (Config.String _) = equals |-- Args.name >> Config.String;
   419 
   420 fun scan_config thy config =
   421   let val config_type = Config.get_global thy config
   422   in scan_value config_type >> (K o Thm.declaration_attribute o K o Config.put_generic config) end;
   423 
   424 fun register binding config thy =
   425   let val name = Sign.full_name thy binding in
   426     thy
   427     |> setup binding (Scan.lift (scan_config thy config) >> Morphism.form) "configuration option"
   428     |> Configs.map (Symtab.update (name, config))
   429   end;
   430 
   431 fun declare make coerce binding default =
   432   let
   433     val name = Binding.name_of binding;
   434     val config_value = Config.declare_generic {global = false} name (make o default);
   435     val config = coerce config_value;
   436   in (config, register binding config_value) end;
   437 
   438 in
   439 
   440 val config_bool = declare Config.Bool Config.bool;
   441 val config_int = declare Config.Int Config.int;
   442 val config_real = declare Config.Real Config.real;
   443 val config_string = declare Config.String Config.string;
   444 
   445 fun register_config config = register (Binding.name (Config.name_of config)) config;
   446 
   447 end;
   448 
   449 
   450 (* implicit setup *)
   451 
   452 local
   453 
   454 fun setup_config declare_config binding default =
   455   let
   456     val (config, setup) = declare_config binding default;
   457     val _ = Context.>> (Context.map_theory setup);
   458   in config end;
   459 
   460 in
   461 
   462 val setup_config_bool = setup_config config_bool;
   463 val setup_config_int = setup_config config_int;
   464 val setup_config_string = setup_config config_string;
   465 val setup_config_real = setup_config config_real;
   466 
   467 end;
   468 
   469 
   470 (* theory setup *)
   471 
   472 val _ = Context.>> (Context.map_theory
   473  (register_config Ast.trace_raw #>
   474   register_config Ast.stats_raw #>
   475   register_config Syntax.positions_raw #>
   476   register_config Printer.show_brackets_raw #>
   477   register_config Printer.show_sorts_raw #>
   478   register_config Printer.show_types_raw #>
   479   register_config Printer.show_structs_raw #>
   480   register_config Printer.show_question_marks_raw #>
   481   register_config Syntax.ambiguity_level_raw #>
   482   register_config Syntax.ambiguity_warnings_raw #>
   483   register_config Syntax_Trans.eta_contract_raw #>
   484   register_config Name_Space.names_long_raw #>
   485   register_config Name_Space.names_short_raw #>
   486   register_config Name_Space.names_unique_raw #>
   487   register_config ML_Context.trace_raw #>
   488   register_config Proof_Context.show_abbrevs_raw #>
   489   register_config Goal_Display.goals_limit_raw #>
   490   register_config Goal_Display.show_main_goal_raw #>
   491   register_config Goal_Display.show_consts_raw #>
   492   register_config Display.show_hyps_raw #>
   493   register_config Display.show_tags_raw #>
   494   register_config Unify.trace_bound_raw #>
   495   register_config Unify.search_bound_raw #>
   496   register_config Unify.trace_simp_raw #>
   497   register_config Unify.trace_types_raw #>
   498   register_config Raw_Simplifier.simp_depth_limit_raw #>
   499   register_config Raw_Simplifier.simp_trace_depth_limit_raw #>
   500   register_config Raw_Simplifier.simp_debug_raw #>
   501   register_config Raw_Simplifier.simp_trace_raw));
   502 
   503 end;