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