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