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