src/Pure/Isar/attrib.ML
author wenzelm
Tue Jan 10 19:33:35 2006 +0100 (2006-01-10)
changeset 18636 cb068cfdcac8
parent 18418 bf448d999b7e
child 18678 dd0c569fa43d
permissions -rw-r--r--
added rule, declaration;
support generic attributes: theory, context, generic, common, generic_attribute(_i);
added generic syntax;
basic attributes now generic;
tuned;
     1 (*  Title:      Pure/Isar/attrib.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Symbolic theorem attributes.
     6 *)
     7 
     8 signature BASIC_ATTRIB =
     9 sig
    10   val print_attributes: theory -> unit
    11   val Attribute: bstring ->
    12     (Args.src -> theory attribute) * (Args.src -> ProofContext.context attribute) ->
    13     string -> unit
    14 end;
    15 
    16 signature ATTRIB =
    17 sig
    18   include BASIC_ATTRIB
    19   val rule: ('a -> thm -> thm) -> 'a attribute
    20   val declaration: (thm -> 'a -> 'a) -> 'a attribute
    21   type src
    22   val theory: Context.generic attribute -> theory attribute
    23   val context: Context.generic attribute -> ProofContext.context attribute
    24   val generic: theory attribute * ProofContext.context attribute -> Context.generic attribute
    25   val common: (src -> Context.generic attribute) ->
    26     (src -> theory attribute) * (src -> ProofContext.context attribute)
    27   exception ATTRIB_FAIL of (string * Position.T) * exn
    28   val intern: theory -> xstring -> string
    29   val intern_src: theory -> src -> src
    30   val global_attribute_i: theory -> src -> theory attribute
    31   val global_attribute: theory -> src -> theory attribute
    32   val local_attribute_i: theory -> src -> ProofContext.context attribute
    33   val local_attribute: theory -> src -> ProofContext.context attribute
    34   val context_attribute_i: ProofContext.context -> Args.src -> ProofContext.context attribute
    35   val context_attribute: ProofContext.context -> Args.src -> ProofContext.context attribute
    36   val undef_global_attribute: theory attribute
    37   val undef_local_attribute: ProofContext.context attribute
    38   val generic_attribute_i: theory -> src -> Context.generic attribute
    39   val generic_attribute: theory -> src -> Context.generic attribute
    40   val map_specs: ('a -> 'b attribute) ->
    41     (('c * 'a list) * 'd) list -> (('c * 'b attribute list) * 'd) list
    42   val map_facts: ('a -> 'b attribute) ->
    43     (('c * 'a list) * ('d * 'a list) list) list ->
    44     (('c * 'b attribute list) * ('d * 'b attribute list) list) list
    45   val crude_closure: ProofContext.context -> src -> src
    46   val add_attributes: (bstring * ((src -> theory attribute) *
    47       (src -> ProofContext.context attribute)) * string) list -> theory -> theory
    48   val global_thm: theory * Args.T list -> thm * (theory * Args.T list)
    49   val global_thms: theory * Args.T list -> thm list * (theory * Args.T list)
    50   val global_thmss: theory * Args.T list -> thm list * (theory * Args.T list)
    51   val local_thm: ProofContext.context * Args.T list ->
    52     thm * (ProofContext.context * Args.T list)
    53   val local_thms: ProofContext.context * Args.T list ->
    54     thm list * (ProofContext.context * Args.T list)
    55   val local_thmss: ProofContext.context * Args.T list ->
    56     thm list * (ProofContext.context * Args.T list)
    57   val thm: Context.generic * Args.T list -> thm * (Context.generic * Args.T list)
    58   val thms: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
    59   val thmss: Context.generic * Args.T list -> thm list * (Context.generic * Args.T list)
    60   val syntax: ('a * Args.T list -> 'a attribute * ('a * Args.T list)) -> src -> 'a attribute
    61   val no_args: 'a attribute -> src -> 'a attribute
    62   val add_del_args: 'a attribute -> 'a attribute -> src -> 'a attribute
    63   val attribute: Context.generic attribute -> src
    64 end;
    65 
    66 structure Attrib: ATTRIB =
    67 struct
    68 
    69 val rule = Drule.rule_attribute;
    70 val declaration = Drule.declaration_attribute;
    71 
    72 type src = Args.src;
    73 
    74 
    75 
    76 (** generic attributes **)
    77 
    78 fun generic (global_att, local_att) =
    79    fn (Context.Theory thy, th) => apfst Context.Theory (global_att (thy, th))
    80     | (Context.Proof ctxt, th) => apfst Context.Proof (local_att (ctxt, th));
    81 
    82 fun theory att (thy, th) = apfst Context.the_theory (att (Context.Theory thy, th));
    83 fun context att (ctxt, th) = apfst Context.the_proof (att (Context.Proof ctxt, th));
    84 
    85 fun common att = (theory o att, context o att);
    86 
    87 
    88 
    89 (** attributes theory data **)
    90 
    91 (* datatype attributes *)
    92 
    93 structure AttributesData = TheoryDataFun
    94 (struct
    95   val name = "Isar/attributes";
    96   type T =
    97     ((((src -> theory attribute) * (src -> ProofContext.context attribute))
    98         * string) * stamp) NameSpace.table;
    99 
   100   val empty = NameSpace.empty_table;
   101   val copy = I;
   102   val extend = I;
   103 
   104   fun merge _ tables = NameSpace.merge_tables (eq_snd (op =)) tables handle Symtab.DUPS dups =>
   105     error ("Attempt to merge different versions of attribute(s) " ^ commas_quote dups);
   106 
   107   fun print _ attribs =
   108     let
   109       fun prt_attr (name, ((_, comment), _)) = Pretty.block
   110         [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   111     in
   112       [Pretty.big_list "attributes:" (map prt_attr (NameSpace.extern_table attribs))]
   113       |> Pretty.chunks |> Pretty.writeln
   114     end;
   115 end);
   116 
   117 val _ = Context.add_setup [AttributesData.init];
   118 val print_attributes = AttributesData.print;
   119 
   120 
   121 (* interning *)
   122 
   123 val intern = NameSpace.intern o #1 o AttributesData.get;
   124 val intern_src = Args.map_name o intern;
   125 
   126 
   127 (* get global / local attributes *)
   128 
   129 exception ATTRIB_FAIL of (string * Position.T) * exn;
   130 
   131 fun gen_attribute which thy =
   132   let
   133     val attrs = #2 (AttributesData.get thy);
   134     fun attr src =
   135       let val ((name, _), pos) = Args.dest_src src in
   136         (case Symtab.lookup attrs name of
   137           NONE => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
   138         | SOME ((p, _), _) => transform_failure (curry ATTRIB_FAIL (name, pos)) (which p src))
   139       end;
   140   in attr end;
   141 
   142 val global_attribute_i = gen_attribute fst;
   143 fun global_attribute thy = global_attribute_i thy o intern_src thy;
   144 
   145 val local_attribute_i = gen_attribute snd;
   146 fun local_attribute thy = local_attribute_i thy o intern_src thy;
   147 
   148 val context_attribute_i = local_attribute_i o ProofContext.theory_of;
   149 val context_attribute = local_attribute o ProofContext.theory_of;
   150 
   151 val undef_global_attribute: theory attribute =
   152   fn _ => error "attribute undefined in theory context";
   153 
   154 val undef_local_attribute: ProofContext.context attribute =
   155   fn _ => error "attribute undefined in proof context";
   156 
   157 fun generic_attribute thy src = generic (global_attribute thy src, local_attribute thy src);
   158 fun generic_attribute_i thy src = generic (global_attribute_i thy src, local_attribute_i thy src);
   159 
   160 
   161 (* attributed declarations *)
   162 
   163 fun map_specs f = map (apfst (apsnd (map f)));
   164 fun map_facts f = map (apfst (apsnd (map f)) o apsnd (map (apsnd (map f))));
   165 
   166 
   167 (* crude_closure *)
   168 
   169 (*Produce closure without knowing facts in advance! The following
   170   should work reasonably well for attribute parsers that do not peek
   171   at the thm structure.*)
   172 
   173 fun crude_closure ctxt src =
   174  (try (transform_error (fn () => context_attribute_i ctxt src (ctxt, Drule.asm_rl))) ();
   175   Args.closure src);
   176 
   177 
   178 (* add_attributes *)
   179 
   180 fun add_attributes raw_attrs thy =
   181   let
   182     val new_attrs = raw_attrs |> map (fn (name, (f, g), comment) =>
   183       (name, (((f, g), comment), stamp ())));
   184     fun add attrs = NameSpace.extend_table (Sign.naming_of thy) (attrs, new_attrs)
   185       handle Symtab.DUPS dups =>
   186         error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
   187   in AttributesData.map add thy end;
   188 
   189 (*implicit version*)
   190 fun Attribute name att cmt = Context.>> (add_attributes [(name, att, cmt)]);
   191 
   192 
   193 
   194 (** attribute parsers **)
   195 
   196 (* tags *)
   197 
   198 fun tag x = Scan.lift (Args.name -- Scan.repeat Args.name) x;
   199 
   200 
   201 (* theorems *)
   202 
   203 local
   204 
   205 fun gen_thm theory_of attrib get pick = Scan.depend (fn st =>
   206  (Scan.ahead Args.alt_name -- Args.named_fact (get st o Fact)
   207     >> (fn (s, fact) => ("", Fact s, fact)) ||
   208   Scan.ahead Args.name -- Args.named_fact (get st o Name) -- Args.thm_sel
   209     >> (fn ((name, fact), sel) => (name, NameSelection (name, sel), fact)) ||
   210   Scan.ahead Args.name -- Args.named_fact (get st o Name)
   211     >> (fn (name, fact) => (name, Name name, fact))) --
   212   Args.opt_attribs (intern (theory_of st))
   213   >> (fn ((name, thmref, fact), srcs) =>
   214     let
   215       val ths = PureThy.select_thm thmref fact;
   216       val atts = map (attrib (theory_of st)) srcs;
   217       val (st', ths') = Thm.applys_attributes atts (st, ths);
   218     in (st', pick name ths') end));
   219 
   220 val get_thms = Context.cases PureThy.get_thms ProofContext.get_thms;
   221 
   222 in
   223 
   224 val global_thm = gen_thm I global_attribute_i PureThy.get_thms PureThy.single_thm;
   225 val global_thms = gen_thm I global_attribute_i PureThy.get_thms (K I);
   226 val global_thmss = Scan.repeat global_thms >> List.concat;
   227 
   228 val local_thm =
   229   gen_thm ProofContext.theory_of local_attribute_i ProofContext.get_thms PureThy.single_thm;
   230 val local_thms =
   231   gen_thm ProofContext.theory_of local_attribute_i ProofContext.get_thms (K I);
   232 val local_thmss = Scan.repeat local_thms >> List.concat;
   233 
   234 val thm = gen_thm Context.theory_of generic_attribute_i get_thms PureThy.single_thm;
   235 val thms = gen_thm Context.theory_of generic_attribute_i get_thms (K I);
   236 val thmss = Scan.repeat thms >> List.concat;
   237 
   238 end;
   239 
   240 
   241 
   242 (** attribute syntax **)
   243 
   244 fun syntax scan src (st, th) =
   245   let val (st', f) = Args.syntax "attribute" scan src st
   246   in f (st', th) end;
   247 
   248 fun no_args x = syntax (Scan.succeed x);
   249 
   250 fun add_del_args add del x = syntax
   251   (Scan.lift (Args.add >> K add || Args.del >> K del || Scan.succeed add)) x;
   252 
   253 
   254 
   255 (** basic attributes **)
   256 
   257 (* tags *)
   258 
   259 fun tagged x = syntax (tag >> Drule.tag) x;
   260 fun untagged x = syntax (Scan.lift Args.name >> Drule.untag) x;
   261 
   262 
   263 (* rule composition *)
   264 
   265 val COMP_att =
   266   syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
   267     >> (fn (i, B) => Drule.rule_attribute (fn _ => fn A => Drule.compose_single (A, i, B))));
   268 
   269 val THEN_att =
   270   syntax (Scan.lift (Scan.optional (Args.bracks Args.nat) 1) -- thm
   271     >> (fn (i, B) => Drule.rule_attribute (fn _ => fn A => A RSN (i, B))));
   272 
   273 val OF_att =
   274   syntax (thmss >> (fn Bs => Drule.rule_attribute (fn _ => fn A => Bs MRS A)));
   275 
   276 
   277 (* read_instantiate: named instantiation of type and term variables *)
   278 
   279 local
   280 
   281 fun is_tvar (x, _) = (case Symbol.explode x of "'" :: _ => true | _ => false);
   282 
   283 fun error_var msg xi = error (msg ^ Syntax.string_of_vname xi);
   284 
   285 fun the_sort sorts xi = the (sorts xi)
   286   handle Option.Option => error_var "No such type variable in theorem: " xi;
   287 
   288 fun the_type types xi = the (types xi)
   289   handle Option.Option => error_var "No such variable in theorem: " xi;
   290 
   291 fun unify_types thy types ((unifier, maxidx), (xi, u)) =
   292   let
   293     val T = the_type types xi;
   294     val U = Term.fastype_of u;
   295     val maxidx' = Int.max (maxidx, Int.max (#2 xi, Term.maxidx_of_term u));
   296   in
   297     Sign.typ_unify thy (T, U) (unifier, maxidx')
   298       handle Type.TUNIFY => error_var "Incompatible type for instantiation of " xi
   299   end;
   300 
   301 fun typ_subst env = apsnd (Term.typ_subst_TVars env);
   302 fun subst env = apsnd (Term.subst_TVars env);
   303 
   304 fun instantiate thy envT env thm =
   305   let
   306     val (_, sorts) = Drule.types_sorts thm;
   307     fun prepT (a, T) = (Thm.ctyp_of thy (TVar (a, the_sort sorts a)), Thm.ctyp_of thy T);
   308     fun prep (xi, t) = pairself (Thm.cterm_of thy) (Var (xi, Term.fastype_of t), t);
   309   in
   310     Drule.instantiate (map prepT (distinct envT),
   311       map prep (gen_distinct (fn ((xi, t), (yj, u)) => xi = yj andalso t aconv u) env)) thm
   312   end;
   313 
   314 in
   315 
   316 fun read_instantiate mixed_insts (generic, thm) =
   317   let
   318     val thy = Context.theory_of generic;
   319     val ctxt = Context.proof_of generic;
   320 
   321     val (type_insts, term_insts) = List.partition (is_tvar o fst) (map snd mixed_insts);
   322     val internal_insts = term_insts |> List.mapPartial
   323       (fn (xi, Args.Term t) => SOME (xi, t)
   324       | (_, Args.Name _) => NONE
   325       | (xi, _) => error_var "Term argument expected for " xi);
   326     val external_insts = term_insts |> List.mapPartial
   327       (fn (xi, Args.Name s) => SOME (xi, s) | _ => NONE);
   328 
   329 
   330     (* type instantiations *)
   331 
   332     val sorts = #2 (Drule.types_sorts thm);
   333 
   334     fun readT (xi, arg) =
   335       let
   336         val S = the_sort sorts xi;
   337         val T =
   338           (case arg of
   339             Args.Name s => ProofContext.read_typ ctxt s
   340           | Args.Typ T => T
   341           | _ => error_var "Type argument expected for " xi);
   342       in
   343         if Sign.of_sort thy (T, S) then (xi, T)
   344         else error_var "Incompatible sort for typ instantiation of " xi
   345       end;
   346 
   347     val type_insts' = map readT type_insts;
   348     val thm' = instantiate thy type_insts' [] thm;
   349 
   350 
   351     (* internal term instantiations *)
   352 
   353     val types' = #1 (Drule.types_sorts thm');
   354     val unifier = map (apsnd snd) (Vartab.dest (#1
   355       (Library.foldl (unify_types thy types') ((Vartab.empty, 0), internal_insts))));
   356 
   357     val type_insts'' = map (typ_subst unifier) type_insts';
   358     val internal_insts'' = map (subst unifier) internal_insts;
   359     val thm'' = instantiate thy unifier internal_insts'' thm';
   360 
   361 
   362     (* external term instantiations *)
   363 
   364     val types'' = #1 (Drule.types_sorts thm'');
   365 
   366     val (xs, ss) = split_list external_insts;
   367     val Ts = map (the_type types'') xs;
   368     val (ts, inferred) = ProofContext.read_termTs ctxt (K false)
   369         (K NONE) (K NONE) (Drule.add_used thm'' []) (ss ~~ Ts);
   370 
   371     val type_insts''' = map (typ_subst inferred) type_insts'';
   372     val internal_insts''' = map (subst inferred) internal_insts'';
   373 
   374     val external_insts''' = xs ~~ ts;
   375     val term_insts''' = internal_insts''' @ external_insts''';
   376     val thm''' = instantiate thy inferred external_insts''' thm'';
   377 
   378 
   379     (* assign internalized values *)
   380 
   381     val _ =
   382       mixed_insts |> List.app (fn (arg, (xi, _)) =>
   383         if is_tvar xi then
   384           Args.assign (SOME (Args.Typ (the (AList.lookup (op =) type_insts''' xi)))) arg
   385         else
   386           Args.assign (SOME (Args.Term (the (AList.lookup (op =) term_insts''' xi)))) arg);
   387 
   388   in (generic, thm''' |> RuleCases.save thm) end;
   389 
   390 end;
   391 
   392 
   393 (* where: named instantiation *)
   394 
   395 local
   396 
   397 val value =
   398   Args.internal_typ >> Args.Typ ||
   399   Args.internal_term >> Args.Term ||
   400   Args.name >> Args.Name;
   401 
   402 val inst = Args.var -- (Args.$$$ "=" |-- Args.ahead -- value)
   403   >> (fn (xi, (a, v)) => (a, (xi, v)));
   404 
   405 in
   406 
   407 val where_att = syntax (Args.and_list (Scan.lift inst) >> read_instantiate);
   408 
   409 end;
   410 
   411 
   412 (* of: positional instantiation (term arguments only) *)
   413 
   414 local
   415 
   416 fun read_instantiate' (args, concl_args) (generic, thm) =
   417   let
   418     fun zip_vars _ [] = []
   419       | zip_vars (_ :: xs) ((_, NONE) :: rest) = zip_vars xs rest
   420       | zip_vars ((x, _) :: xs) ((a, SOME t) :: rest) = (a, (x, t)) :: zip_vars xs rest
   421       | zip_vars [] _ = error "More instantiations than variables in theorem";
   422     val insts =
   423       zip_vars (Drule.vars_of_terms [Thm.prop_of thm]) args @
   424       zip_vars (Drule.vars_of_terms [Thm.concl_of thm]) concl_args;
   425   in read_instantiate insts (generic, thm) end;
   426 
   427 val value =
   428   Args.internal_term >> Args.Term ||
   429   Args.name >> Args.Name;
   430 
   431 val inst = Args.ahead -- Args.maybe value;
   432 val concl = Args.$$$ "concl" -- Args.colon;
   433 
   434 val insts =
   435   Scan.repeat (Scan.unless concl inst) --
   436   Scan.optional (concl |-- Scan.repeat inst) [];
   437 
   438 in
   439 
   440 val of_att = syntax (Scan.lift insts >> read_instantiate');
   441 
   442 end;
   443 
   444 
   445 (* rename_abs *)
   446 
   447 fun rename_abs src = syntax
   448   (Scan.lift (Scan.repeat (Args.maybe Args.name) >> (apsnd o Drule.rename_bvars'))) src;
   449 
   450 
   451 (* unfold / fold definitions *)
   452 
   453 val unfolded = syntax (thmss >> (fn defs => Drule.rule_attribute (K (Tactic.rewrite_rule defs))));
   454 val folded = syntax (thmss >> (fn defs => Drule.rule_attribute (K (Tactic.fold_rule defs))));
   455 
   456 
   457 (* rule cases *)
   458 
   459 fun consumes x = syntax (Scan.lift (Scan.optional Args.nat 1) >> RuleCases.consumes) x;
   460 fun case_names x = syntax (Scan.lift (Scan.repeat1 Args.name) >> RuleCases.case_names) x;
   461 fun case_conclusion x =
   462   syntax (Scan.lift (Args.name -- Scan.repeat Args.name) >> RuleCases.case_conclusion) x;
   463 fun params x = syntax (Args.and_list1 (Scan.lift (Scan.repeat Args.name)) >> RuleCases.params) x;
   464 
   465 
   466 (* rule_format *)
   467 
   468 fun rule_format_att x = syntax (Args.mode "no_asm"
   469   >> (fn true => ObjectLogic.rule_format_no_asm | false => ObjectLogic.rule_format)) x;
   470 
   471 
   472 (* misc rules *)
   473 
   474 fun standard x = no_args (Drule.rule_attribute (K Drule.standard)) x;
   475 fun elim_format x = no_args (Drule.rule_attribute (K Tactic.make_elim)) x;
   476 fun no_vars x = no_args (Drule.rule_attribute (K (#1 o Drule.freeze_thaw))) x;
   477 fun eta_long x = no_args (Drule.rule_attribute (K (Drule.fconv_rule Drule.eta_long_conversion))) x;
   478 
   479 
   480 (* internal attribute *)
   481 
   482 fun attribute att = Args.src (("Pure.attribute", [Args.mk_attribute att]), Position.none);
   483 
   484 fun internal_att x = syntax (Scan.lift Args.internal_attribute) x;
   485 
   486 
   487 (* theory setup *)
   488 
   489 val _ = Context.add_setup
   490  [add_attributes
   491    [("tagged", common tagged, "tagged theorem"),
   492     ("untagged", common untagged, "untagged theorem"),
   493     ("COMP", common COMP_att, "direct composition with rules (no lifting)"),
   494     ("THEN", common THEN_att, "resolution with rule"),
   495     ("OF", common OF_att, "rule applied to facts"),
   496     ("where", common where_att, "named instantiation of theorem"),
   497     ("of", common of_att, "rule applied to terms"),
   498     ("rename_abs", common rename_abs, "rename bound variables in abstractions"),
   499     ("unfolded", common unfolded, "unfolded definitions"),
   500     ("folded", common folded, "folded definitions"),
   501     ("standard", common standard, "result put into standard form"),
   502     ("elim_format", common elim_format, "destruct rule turned into elimination rule format"),
   503     ("no_vars", common no_vars, "frozen schematic vars"),
   504     ("eta_long", common eta_long, "put theorem into eta long beta normal form"),
   505     ("consumes", common consumes, "number of consumed facts"),
   506     ("case_names", common case_names, "named rule cases"),
   507     ("case_conclusion", common case_conclusion, "named conclusion of rule cases"),
   508     ("params", common params, "named rule parameters"),
   509     ("atomize", (no_args ObjectLogic.declare_atomize, no_args undef_local_attribute),
   510       "declaration of atomize rule"),
   511     ("rulify", (no_args ObjectLogic.declare_rulify, no_args undef_local_attribute),
   512       "declaration of rulify rule"),
   513     ("rule_format", common rule_format_att, "result put into standard rule format"),
   514     ("attribute", common internal_att, "internal attribute")]];
   515 
   516 end;
   517 
   518 structure BasicAttrib: BASIC_ATTRIB = Attrib;
   519 open BasicAttrib;