src/Pure/Isar/attrib.ML
author wenzelm
Sat Sep 25 13:06:59 1999 +0200 (1999-09-25 ago)
changeset 7598 af320257c902
parent 7367 a79d4683fadf
child 7611 5b5aba10c8f6
permissions -rw-r--r--
unfold / fold defs;
     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 -> (Args.src -> theory attribute) * (Args.src -> Proof.context attribute)
    12     -> string -> unit
    13 end;
    14 
    15 signature ATTRIB =
    16 sig
    17   include BASIC_ATTRIB
    18   val help_attributes: theory -> unit
    19   exception ATTRIB_FAIL of (string * Position.T) * exn
    20   val global_attribute: theory -> Args.src -> theory attribute
    21   val local_attribute: theory -> Args.src -> Proof.context attribute
    22   val local_attribute': Proof.context -> Args.src -> Proof.context attribute
    23   val add_attributes: (bstring * ((Args.src -> theory attribute) *
    24       (Args.src -> Proof.context attribute)) * string) list -> theory -> theory
    25   val global_thm: theory * Args.T list -> thm * (theory * Args.T list)
    26   val global_thms: theory * Args.T list -> thm list * (theory * Args.T list)
    27   val global_thmss: theory * Args.T list -> thm list * (theory * Args.T list)
    28   val local_thm: Proof.context * Args.T list -> thm * (Proof.context * Args.T list)
    29   val local_thms: Proof.context * Args.T list -> thm list * (Proof.context * Args.T list)
    30   val local_thmss: Proof.context * Args.T list -> thm list * (Proof.context * Args.T list)
    31   val syntax: ('a * Args.T list -> 'a attribute * ('a * Args.T list)) -> Args.src -> 'a attribute
    32   val no_args: 'a attribute -> Args.src -> 'a attribute
    33   val setup: (theory -> theory) list
    34 end;
    35 
    36 structure Attrib: ATTRIB =
    37 struct
    38 
    39 
    40 (** attributes theory data **)
    41 
    42 (* data kind 'Isar/attributes' *)
    43 
    44 structure AttributesDataArgs =
    45 struct
    46   val name = "Isar/attributes";
    47   type T =
    48     {space: NameSpace.T,
    49      attrs:
    50        ((((Args.src -> theory attribute) * (Args.src -> Proof.context attribute))
    51          * string) * stamp) Symtab.table};
    52 
    53   val empty = {space = NameSpace.empty, attrs = Symtab.empty};
    54   val copy = I;
    55   val prep_ext = I;
    56 
    57   fun merge ({space = space1, attrs = attrs1}, {space = space2, attrs = attrs2}) =
    58     {space = NameSpace.merge (space1, space2),
    59       attrs = Symtab.merge eq_snd (attrs1, attrs2) handle Symtab.DUPS dups =>
    60         error ("Attempt to merge different versions of attributes " ^ commas_quote dups)};
    61 
    62   fun print_atts verbose ({space, attrs}) =
    63     let
    64       fun prt_attr (name, ((_, comment), _)) = Pretty.block
    65         [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
    66     in
    67       if not verbose then ()
    68       else Pretty.writeln (Display.pretty_name_space ("attribute name space", space));
    69       Pretty.writeln (Pretty.big_list "attributes:"
    70         (map prt_attr (NameSpace.cond_extern_table space attrs)))
    71     end;
    72 
    73    fun print _ = print_atts true;
    74 end;
    75 
    76 structure AttributesData = TheoryDataFun(AttributesDataArgs);
    77 val print_attributes = AttributesData.print;
    78 val help_attributes = AttributesDataArgs.print_atts false o AttributesData.get;
    79 
    80 
    81 (* get global / local attributes *)
    82 
    83 exception ATTRIB_FAIL of (string * Position.T) * exn;
    84 
    85 fun gen_attribute which thy =
    86   let
    87     val {space, attrs} = AttributesData.get thy;
    88 
    89     fun attr src =
    90       let
    91         val ((raw_name, _), pos) = Args.dest_src src;
    92         val name = NameSpace.intern space raw_name;
    93       in
    94         (case Symtab.lookup (attrs, name) of
    95           None => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
    96         | Some ((p, _), _) => transform_failure (curry ATTRIB_FAIL (name, pos)) (which p src))
    97       end;
    98   in attr end;
    99 
   100 val global_attribute = gen_attribute fst;
   101 val local_attribute = gen_attribute snd;
   102 val local_attribute' = local_attribute o ProofContext.theory_of;
   103 
   104 
   105 (* add_attributes *)
   106 
   107 fun add_attributes raw_attrs thy =
   108   let
   109     val full = Sign.full_name (Theory.sign_of thy);
   110     val new_attrs =
   111       map (fn (name, (f, g), comment) => (full name, (((f, g), comment), stamp ()))) raw_attrs;
   112 
   113     val {space, attrs} = AttributesData.get thy;
   114     val space' = NameSpace.extend (space, map fst new_attrs);
   115     val attrs' = Symtab.extend (attrs, new_attrs) handle Symtab.DUPS dups =>
   116       error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
   117   in thy |> AttributesData.put {space = space', attrs = attrs'} end;
   118 
   119 (*implicit version*)
   120 fun Attribute name att cmt = Context.>> (add_attributes [(name, att, cmt)]);
   121 
   122 
   123 
   124 (** attribute parsers **)
   125 
   126 (* tags *)
   127 
   128 fun tag x = Scan.lift (Args.name -- Scan.repeat Args.name) x;
   129 
   130 
   131 (* theorems *)
   132 
   133 fun gen_thm get attrib app =
   134   Scan.depend (fn st => Args.name -- Args.opt_attribs >>
   135     (fn (name, srcs) => app ((st, get st name), map (attrib st) srcs)));
   136 
   137 val global_thm = gen_thm PureThy.get_thm global_attribute Thm.apply_attributes;
   138 val global_thms = gen_thm PureThy.get_thms global_attribute Thm.applys_attributes;
   139 val global_thmss = Scan.repeat global_thms >> flat;
   140 
   141 val local_thm = gen_thm ProofContext.get_thm local_attribute' Thm.apply_attributes;
   142 val local_thms = gen_thm ProofContext.get_thms local_attribute' Thm.applys_attributes;
   143 val local_thmss = Scan.repeat local_thms >> flat;
   144 
   145 
   146 
   147 (** attribute syntax **)
   148 
   149 fun syntax scan src (st, th) =
   150   let val (st', f) = Args.syntax "attribute" scan st src
   151   in f (st', th) end;
   152 
   153 fun no_args x = syntax (Scan.succeed x);
   154 
   155 
   156 
   157 (** Pure attributes **)
   158 
   159 (* tags *)
   160 
   161 fun gen_tag x = syntax (tag >> Drule.tag) x;
   162 fun gen_untag x = syntax (tag >> Drule.untag) x;
   163 
   164 
   165 (* transfer *)
   166 
   167 fun gen_transfer theory_of = no_args (Drule.rule_attribute (fn st => Thm.transfer (theory_of st)));
   168 
   169 val global_transfer = gen_transfer I;
   170 val local_transfer = gen_transfer ProofContext.theory_of;
   171 
   172 
   173 (* COMP *)
   174 
   175 fun comp (i, B) (x, A) = (x, Drule.compose_single (A, i, B));
   176 
   177 fun gen_COMP thm = syntax (Scan.lift (Scan.optional Args.nat 1) -- thm >> comp);
   178 val global_COMP = gen_COMP global_thm;
   179 val local_COMP = gen_COMP local_thm;
   180 
   181 
   182 (* RS *)
   183 
   184 fun resolve (i, B) (x, A) = (x, A RSN (i, B));
   185 
   186 fun gen_RS thm = syntax (Scan.lift (Scan.optional Args.nat 1) -- thm >> resolve);
   187 val global_RS = gen_RS global_thm;
   188 val local_RS = gen_RS local_thm;
   189 
   190 
   191 (* APP *)
   192 
   193 fun apply Bs (x, A) = (x, Bs MRS A);
   194 
   195 val global_APP = syntax (global_thmss >> apply);
   196 val local_APP = syntax (local_thmss >> apply);
   197 
   198 
   199 (* where: named instantiations *)
   200 
   201 fun read_instantiate context_of insts x thm =
   202   let
   203     val ctxt = context_of x;
   204     val sign = ProofContext.sign_of ctxt;
   205 
   206     val vars = Drule.vars_of thm;
   207     fun get_typ xi =
   208       (case assoc (vars, xi) of
   209         Some T => T
   210       | None => error ("No such variable in theorem: " ^ Syntax.string_of_vname xi));
   211 
   212     val (xs, ss) = Library.split_list insts;
   213     val Ts = map get_typ xs;
   214 
   215     val (ts, envT) = ProofContext.read_termTs (ctxt |> ProofContext.declare_thm thm) (ss ~~ Ts);
   216     val cenvT = map (apsnd (Thm.ctyp_of sign)) envT;
   217     val cenv =
   218       map (fn (xi, t) => pairself (Thm.cterm_of sign) (Var (xi, fastype_of t), t))
   219         (gen_distinct (fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2) (xs ~~ ts));
   220   in Thm.instantiate (cenvT, cenv) thm end;
   221 
   222 fun insts x = Args.and_list (Scan.lift (Args.var --| Args.$$$ "=" -- Args.name)) x;
   223 
   224 fun gen_where context_of = syntax (insts >> (Drule.rule_attribute o read_instantiate context_of));
   225 
   226 val global_where = gen_where ProofContext.init;
   227 val local_where = gen_where I;
   228 
   229 
   230 (* with: positional instantiations *)
   231 
   232 fun read_instantiate' context_of (args, concl_args) x thm =
   233   let
   234     fun zip_vars _ [] = []
   235       | zip_vars (_ :: xs) (None :: opt_ts) = zip_vars xs opt_ts
   236       | zip_vars ((x, _) :: xs) (Some t :: opt_ts) = (x, t) :: zip_vars xs opt_ts
   237       | zip_vars [] _ = error "More instantiations than variables in theorem";
   238     val insts =
   239       zip_vars (Drule.vars_of_terms [#prop (Thm.rep_thm thm)]) args @
   240       zip_vars (Drule.vars_of_terms [Thm.concl_of thm]) concl_args;
   241   in read_instantiate context_of insts x thm end;
   242 
   243 val concl = Args.$$$ "concl" -- Args.$$$ ":";
   244 val inst_arg = Scan.unless concl (Args.$$$ "_" >> K None || Args.name >> Some);
   245 val inst_args = Scan.repeat inst_arg;
   246 fun insts' x = Scan.lift (inst_args -- Scan.optional (concl |-- Args.!!! inst_args) []) x;
   247 
   248 fun gen_with context_of = syntax (insts' >> (Drule.rule_attribute o read_instantiate' context_of));
   249 
   250 val global_with = gen_with ProofContext.init;
   251 val local_with = gen_with I;
   252 
   253 
   254 (* unfold / fold definitions *)
   255 
   256 fun gen_rewrite rew defs (x, thm) = (x, rew defs thm);
   257 
   258 val global_unfold = syntax (global_thmss >> gen_rewrite Tactic.rewrite_rule);
   259 val local_unfold = syntax (local_thmss >> gen_rewrite Tactic.rewrite_rule);
   260 val global_fold = syntax (global_thmss >> gen_rewrite Tactic.fold_rule);
   261 val local_fold = syntax (local_thmss >> gen_rewrite Tactic.fold_rule);
   262 
   263 
   264 (* misc rules *)
   265 
   266 fun standard x = no_args (Drule.rule_attribute (K Drule.standard)) x;
   267 fun elimify x = no_args (Drule.rule_attribute (K Tactic.make_elim)) x;
   268 
   269 fun global_export x = no_args (Drule.rule_attribute (Proof.export_thm o ProofContext.init)) x;
   270 fun local_export x = no_args (Drule.rule_attribute Proof.export_thm) x;
   271 
   272 
   273 
   274 (** theory setup **)
   275 
   276 (* pure_attributes *)
   277 
   278 val pure_attributes =
   279  [("tag", (gen_tag, gen_tag), "tag theorem"),
   280   ("untag", (gen_untag, gen_untag), "untag theorem"),
   281   ("COMP", (global_COMP, local_COMP), "compose rules (no lifting)"),
   282   ("RS", (global_RS, local_RS), "resolve with rule"),
   283   ("OF", (global_APP, local_APP), "resolve with rule -- apply rule to rules"),
   284   ("where", (global_where, local_where), "named instantiation of theorem"),
   285   ("of", (global_with, local_with), "positional instantiation of theorem -- apply rule to terms"),
   286   ("unfold", (global_unfold, local_unfold), "unfold definitions"),
   287   ("fold", (global_fold, local_fold), "fold definitions"),
   288   ("standard", (standard, standard), "put theorem into standard form"),
   289   ("elimify", (elimify, elimify), "turn destruct rule into elimination rule"),
   290   ("transfer", (global_transfer, local_transfer), "transfer theorem to this theory"),
   291   ("export", (global_export, local_export), "export theorem from context")];
   292 
   293 
   294 (* setup *)
   295 
   296 val setup = [AttributesData.init, add_attributes pure_attributes];
   297 
   298 
   299 end;
   300 
   301 
   302 structure BasicAttrib: BASIC_ATTRIB = Attrib;
   303 open BasicAttrib;