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