src/Pure/Isar/attrib.ML
author wenzelm
Mon Nov 16 11:03:35 1998 +0100 (1998-11-16)
changeset 5879 18b8f048d93a
parent 5823 ee7c198a2154
child 5894 71667e5c2ff8
permissions -rw-r--r--
several args parsers;
realistic syntax;
attributes: transfer, RS, APP, where, standard, elimify;
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@5823
    18
  val global_attribute: theory -> Args.src -> theory attribute
wenzelm@5823
    19
  val local_attribute: theory -> Args.src -> Proof.context attribute
wenzelm@5823
    20
  val add_attributes: (bstring * ((Args.src -> theory attribute) *
wenzelm@5823
    21
      (Args.src -> Proof.context attribute)) * string) list -> theory -> theory
wenzelm@5879
    22
  val global_thm: theory * Args.T list -> tthm * (theory * Args.T list)
wenzelm@5879
    23
  val global_thms: theory * Args.T list -> tthm list * (theory * Args.T list)
wenzelm@5879
    24
  val global_thmss: theory * Args.T list -> tthm list * (theory * Args.T list)
wenzelm@5879
    25
  val local_thm: Proof.context * Args.T list -> tthm * (Proof.context * Args.T list)
wenzelm@5879
    26
  val local_thms: Proof.context * Args.T list -> tthm list * (Proof.context * Args.T list)
wenzelm@5879
    27
  val local_thmss: Proof.context * Args.T list -> tthm list * (Proof.context * Args.T list)
wenzelm@5879
    28
  val syntax: ('a * Args.T list -> 'a attribute * ('a * Args.T list)) -> Args.src -> 'a attribute
wenzelm@5823
    29
  val no_args: 'a attribute -> Args.src -> 'a attribute
wenzelm@5823
    30
  val setup: (theory -> theory) list
wenzelm@5823
    31
end;
wenzelm@5823
    32
wenzelm@5823
    33
structure Attrib: ATTRIB =
wenzelm@5823
    34
struct
wenzelm@5823
    35
wenzelm@5823
    36
wenzelm@5823
    37
(** attributes theory data **)
wenzelm@5823
    38
wenzelm@5823
    39
(* data kind 'Isar/attributes' *)
wenzelm@5823
    40
wenzelm@5823
    41
structure AttributesDataArgs =
wenzelm@5823
    42
struct
wenzelm@5823
    43
  val name = "Isar/attributes";
wenzelm@5823
    44
  type T =
wenzelm@5823
    45
    {space: NameSpace.T,
wenzelm@5823
    46
     attrs:
wenzelm@5823
    47
       ((((Args.src -> theory attribute) * (Args.src -> Proof.context attribute))
wenzelm@5823
    48
         * string) * stamp) Symtab.table};
wenzelm@5823
    49
wenzelm@5823
    50
  val empty = {space = NameSpace.empty, attrs = Symtab.empty};
wenzelm@5823
    51
  val prep_ext = I;
wenzelm@5823
    52
wenzelm@5823
    53
  fun merge ({space = space1, attrs = attrs1}, {space = space2, attrs = attrs2}) =
wenzelm@5823
    54
    {space = NameSpace.merge (space1, space2),
wenzelm@5823
    55
      attrs = Symtab.merge eq_snd (attrs1, attrs2) handle Symtab.DUPS dups =>
wenzelm@5823
    56
        error ("Attempt to merge different versions of attributes " ^ commas_quote dups)};
wenzelm@5823
    57
wenzelm@5823
    58
  fun print _ ({space, attrs}) =
wenzelm@5823
    59
    let
wenzelm@5823
    60
      fun prt_attr (name, ((_, comment), _)) = Pretty.block
wenzelm@5823
    61
        [Pretty.str (NameSpace.cond_extern space name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@5823
    62
    in
wenzelm@5823
    63
      Pretty.writeln (Display.pretty_name_space ("attribute name space", space));
wenzelm@5823
    64
      Pretty.writeln (Pretty.big_list "attributes:" (map prt_attr (Symtab.dest attrs)))
wenzelm@5823
    65
    end;
wenzelm@5823
    66
end;
wenzelm@5823
    67
wenzelm@5823
    68
structure AttributesData = TheoryDataFun(AttributesDataArgs);
wenzelm@5823
    69
val print_attributes = AttributesData.print;
wenzelm@5823
    70
wenzelm@5823
    71
wenzelm@5823
    72
(* get global / local attributes *)
wenzelm@5823
    73
wenzelm@5823
    74
fun gen_attribute which thy =
wenzelm@5823
    75
  let
wenzelm@5823
    76
    val {space, attrs} = AttributesData.get thy;
wenzelm@5823
    77
wenzelm@5879
    78
    fun attr src =
wenzelm@5879
    79
      let
wenzelm@5879
    80
        val ((raw_name, _), pos) = Args.dest_src src;
wenzelm@5879
    81
        val name = NameSpace.intern space raw_name;
wenzelm@5879
    82
      in
wenzelm@5823
    83
        (case Symtab.lookup (attrs, name) of
wenzelm@5823
    84
          None => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
wenzelm@5879
    85
        | Some ((p, _), _) => which p src)
wenzelm@5823
    86
      end;
wenzelm@5823
    87
  in attr end;
wenzelm@5823
    88
wenzelm@5823
    89
val global_attribute = gen_attribute fst;
wenzelm@5823
    90
val local_attribute = gen_attribute snd;
wenzelm@5879
    91
val local_attribute' = local_attribute o ProofContext.theory_of;
wenzelm@5823
    92
wenzelm@5823
    93
wenzelm@5823
    94
(* add_attributes *)
wenzelm@5823
    95
wenzelm@5823
    96
fun add_attributes raw_attrs thy =
wenzelm@5823
    97
  let
wenzelm@5823
    98
    val full = Sign.full_name (Theory.sign_of thy);
wenzelm@5823
    99
    val new_attrs =
wenzelm@5823
   100
      map (fn (name, (f, g), comment) => (full name, (((f, g), comment), stamp ()))) raw_attrs;
wenzelm@5823
   101
wenzelm@5823
   102
    val {space, attrs} = AttributesData.get thy;
wenzelm@5823
   103
    val space' = NameSpace.extend (space, map fst new_attrs);
wenzelm@5823
   104
    val attrs' = Symtab.extend (attrs, new_attrs) handle Symtab.DUPS dups =>
wenzelm@5823
   105
      error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
wenzelm@5823
   106
  in thy |> AttributesData.put {space = space', attrs = attrs'} end;
wenzelm@5823
   107
wenzelm@5879
   108
(*implicit version*)
wenzelm@5879
   109
fun Attribute name att cmt = Context.>> (add_attributes [(name, att, cmt)]);
wenzelm@5823
   110
wenzelm@5879
   111
wenzelm@5879
   112
wenzelm@5879
   113
(** attribute parsers **)
wenzelm@5879
   114
wenzelm@5879
   115
(* tags *)
wenzelm@5823
   116
wenzelm@5879
   117
fun tag x = Scan.lift (Args.name -- Scan.repeat Args.name) x;
wenzelm@5879
   118
wenzelm@5879
   119
wenzelm@5879
   120
(* theorems *)
wenzelm@5879
   121
wenzelm@5879
   122
fun gen_thm get attrib app =
wenzelm@5879
   123
  Scan.depend (fn st => Args.name -- Args.opt_attribs >>
wenzelm@5879
   124
    (fn (name, srcs) => app ((st, get st name), map (attrib st) srcs)));
wenzelm@5823
   125
wenzelm@5879
   126
val global_thm = gen_thm PureThy.get_tthm global_attribute Attribute.apply;
wenzelm@5879
   127
val global_thms = gen_thm PureThy.get_tthms global_attribute Attribute.applys;
wenzelm@5879
   128
val global_thmss = Scan.repeat global_thms >> flat;
wenzelm@5879
   129
wenzelm@5879
   130
val local_thm = gen_thm ProofContext.get_tthm local_attribute' Attribute.apply;
wenzelm@5879
   131
val local_thms = gen_thm ProofContext.get_tthms local_attribute' Attribute.applys;
wenzelm@5879
   132
val local_thmss = Scan.repeat local_thms >> flat;
wenzelm@5879
   133
wenzelm@5823
   134
wenzelm@5879
   135
wenzelm@5879
   136
(** attribute syntax **)
wenzelm@5823
   137
wenzelm@5879
   138
fun syntax scan src (st, th) =
wenzelm@5879
   139
  let val (st', f) = Args.syntax "attribute" scan st src
wenzelm@5879
   140
  in f (st', th) end;
wenzelm@5879
   141
wenzelm@5879
   142
fun no_args x = syntax (Scan.succeed x);
wenzelm@5823
   143
wenzelm@5823
   144
wenzelm@5823
   145
wenzelm@5823
   146
(** Pure attributes **)
wenzelm@5823
   147
wenzelm@5823
   148
(* tags *)
wenzelm@5823
   149
wenzelm@5879
   150
fun gen_tag x = syntax (tag >> Attribute.tag) x;
wenzelm@5879
   151
fun gen_untag x = syntax (tag >> Attribute.untag) x;
wenzelm@5823
   152
fun gen_lemma x = no_args Attribute.tag_lemma x;
wenzelm@5823
   153
fun gen_assumption x = no_args Attribute.tag_assumption x;
wenzelm@5823
   154
wenzelm@5823
   155
wenzelm@5879
   156
(* transfer *)
wenzelm@5879
   157
wenzelm@5879
   158
fun gen_transfer theory_of = no_args (Attribute.rule (fn st => Thm.transfer (theory_of st)));
wenzelm@5879
   159
wenzelm@5879
   160
val global_transfer = gen_transfer I;
wenzelm@5879
   161
val local_transfer = gen_transfer ProofContext.theory_of;
wenzelm@5879
   162
wenzelm@5879
   163
wenzelm@5879
   164
(* RS *)
wenzelm@5879
   165
wenzelm@5879
   166
fun resolve (i, B) (x, A) =
wenzelm@5879
   167
  (x, Attribute.tthm_of (Attribute.thm_of A RSN (i, Attribute.thm_of B)));
wenzelm@5879
   168
wenzelm@5879
   169
fun gen_RS thm = syntax (Scan.lift (Scan.optional Args.nat 1) -- thm >> resolve);
wenzelm@5879
   170
val global_RS = gen_RS global_thm;
wenzelm@5879
   171
val local_RS = gen_RS local_thm;
wenzelm@5879
   172
wenzelm@5879
   173
wenzelm@5879
   174
(* APP *)
wenzelm@5879
   175
wenzelm@5879
   176
fun apply Bs (x, A) =
wenzelm@5879
   177
  (x, Attribute.tthm_of (Attribute.thms_of Bs MRS Attribute.thm_of A));
wenzelm@5879
   178
wenzelm@5879
   179
val global_APP = syntax (global_thmss >> apply);
wenzelm@5879
   180
val local_APP = syntax (local_thmss >> apply);
wenzelm@5879
   181
wenzelm@5879
   182
wenzelm@5879
   183
(* instantiations *)
wenzelm@5879
   184
wenzelm@5879
   185
(* FIXME assumes non var hyps *)  (* FIXME move (see also drule.ML) *)
wenzelm@5879
   186
val add_vars = foldl_aterms (fn (vs, Var v) => v ins vs | (vs, _) => vs);
wenzelm@5879
   187
fun vars_of thm = (add_vars ([], #prop (Thm.rep_thm thm)));
wenzelm@5823
   188
wenzelm@5879
   189
fun read_instantiate context_of raw_insts x thm =
wenzelm@5879
   190
  let
wenzelm@5879
   191
    val ctxt = context_of x;
wenzelm@5879
   192
    val sign = ProofContext.sign_of ctxt;
wenzelm@5879
   193
wenzelm@5879
   194
    val vars = vars_of thm;
wenzelm@5879
   195
    fun get_typ xi =
wenzelm@5879
   196
      (case assoc (vars, xi) of
wenzelm@5879
   197
        Some T => T
wenzelm@5879
   198
      | None => error ("No such variable in theorem: " ^ Syntax.string_of_vname xi));
wenzelm@5879
   199
wenzelm@5879
   200
    val (xs, ss) = Library.split_list raw_insts;
wenzelm@5879
   201
    val Ts = map get_typ xs;
wenzelm@5879
   202
wenzelm@5879
   203
    (* FIXME really declare_thm (!!!!??????) *)
wenzelm@5879
   204
    val (ts, envT) = ProofContext.read_termTs (ctxt |> ProofContext.declare_thm thm) (ss ~~ Ts);
wenzelm@5823
   205
wenzelm@5879
   206
    val cenvT = map (apsnd (Thm.ctyp_of sign)) envT;
wenzelm@5879
   207
    val cenv = map2 (fn (xi, t) => pairself (Thm.cterm_of sign) (Var (xi, fastype_of t), t)) (xs, ts);
wenzelm@5879
   208
  in Thm.instantiate (cenvT, cenv) thm end;
wenzelm@5879
   209
wenzelm@5879
   210
wenzelm@5879
   211
val insts = Scan.lift (Args.enum1 "and" (Args.var --| Args.$$$ "=" -- Args.name));
wenzelm@5879
   212
fun gen_where context_of = syntax (insts >> (Attribute.rule o read_instantiate context_of));
wenzelm@5823
   213
wenzelm@5879
   214
val global_where = gen_where ProofContext.init;
wenzelm@5879
   215
val local_where = gen_where I;
wenzelm@5879
   216
wenzelm@5879
   217
wenzelm@5879
   218
(* misc rules *)
wenzelm@5879
   219
wenzelm@5879
   220
fun standard x = no_args (Attribute.rule (K Drule.standard)) x;
wenzelm@5879
   221
fun elimify x = no_args (Attribute.rule (K Tactic.make_elim)) x;
wenzelm@5879
   222
wenzelm@5879
   223
wenzelm@5879
   224
wenzelm@5879
   225
(** theory setup **)
wenzelm@5823
   226
wenzelm@5823
   227
(* pure_attributes *)
wenzelm@5823
   228
wenzelm@5823
   229
val pure_attributes =
wenzelm@5823
   230
 [("tag", (gen_tag, gen_tag), "tag theorem"),
wenzelm@5823
   231
  ("untag", (gen_untag, gen_untag), "untag theorem"),
wenzelm@5823
   232
  ("lemma", (gen_lemma, gen_lemma), "tag as lemma"),
wenzelm@5823
   233
  ("assumption", (gen_assumption, gen_assumption), "tag as assumption"),
wenzelm@5879
   234
  ("RS", (global_RS, local_RS), "resolve with rule"),
wenzelm@5879
   235
  ("APP", (global_APP, local_APP), "resolve rule with"),
wenzelm@5879
   236
  ("where", (global_where, local_where), "instantiate theorem (named vars)"),
wenzelm@5879
   237
  ("standard", (standard, standard), "put theorem into standard form"),
wenzelm@5879
   238
  ("elimify", (elimify, elimify), "turn destruct rule into elimination rule"),
wenzelm@5879
   239
  ("transfer", (global_transfer, local_transfer), "transfer theorem to this theory")];
wenzelm@5823
   240
wenzelm@5823
   241
wenzelm@5879
   242
(* setup *)
wenzelm@5823
   243
wenzelm@5823
   244
val setup = [AttributesData.init, add_attributes pure_attributes];
wenzelm@5823
   245
wenzelm@5823
   246
wenzelm@5823
   247
end;
wenzelm@5823
   248
wenzelm@5823
   249
wenzelm@5823
   250
structure BasicAttrib: BASIC_ATTRIB = Attrib;
wenzelm@5823
   251
open BasicAttrib;