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