src/Pure/attribute.ML
author wenzelm
Sat, 04 Apr 1998 14:27:11 +0200
changeset 4797 d66477d29598
parent 4792 8e3c2dddb9c8
child 4850 050481f41e28
permissions -rw-r--r--
tuned fail;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4780
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/attribute.ML
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
     4
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
     5
Theorem tags and attributes.
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
     6
*)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
     7
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
     8
signature BASIC_ATTRIBUTE =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
     9
sig
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    10
  type tag
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    11
  type tthm
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    12
  type 'a attribute
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    13
  val print_attributes: theory -> unit
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    14
end;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    15
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    16
signature ATTRIBUTE =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    17
sig
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    18
  include BASIC_ATTRIBUTE
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    19
  val thm_of: tthm -> thm
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    20
  val tthm_of: thm -> tthm
4790
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    21
  val fail: string -> string -> 'a
4780
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    22
  val apply: ('a * tthm) * 'a attribute list -> ('a * tthm)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    23
  val pretty_tthm: tthm -> Pretty.T
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    24
  val tag: tag -> 'a attribute
4790
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    25
  val untag: tag -> 'a attribute
4780
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    26
  val lemma: tag
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    27
  val assumption: tag
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    28
  val tag_lemma: 'a attribute
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    29
  val tag_assumption: 'a attribute
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    30
  val setup: theory -> theory
4790
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    31
  val global_attr: theory -> (xstring * string list) -> theory attribute
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    32
  val local_attr: theory -> (xstring * string list) -> local_theory attribute
4780
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    33
  val add_attrs: (bstring * ((string list -> theory attribute) *
4790
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    34
      (string list -> local_theory attribute))) list -> theory -> theory
4780
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    35
end;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    36
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    37
structure Attribute: ATTRIBUTE =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    38
struct
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    39
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    40
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    41
(** tags and attributes **)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    42
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    43
type tag = string * string list;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    44
type tthm = thm * tag list;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    45
type 'a attribute = 'a * tthm -> 'a * tthm;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    46
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    47
fun thm_of (thm, _) = thm;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    48
fun tthm_of thm = (thm, []);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    49
4790
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    50
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    51
(* apply attributes *)
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    52
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    53
exception FAIL of string * string;
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    54
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    55
fun fail name msg = raise FAIL (name, msg);
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    56
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    57
fun warn_failed (name, msg) =
4797
d66477d29598 tuned fail;
wenzelm
parents: 4792
diff changeset
    58
  warning ("Failed invocation of " ^ quote name ^ " attribute: " ^ msg);
4790
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    59
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    60
fun apply (x, []) = x
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    61
  | apply (x, f :: fs) = apply (f x handle FAIL info => (warn_failed info; x), fs);
4780
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    62
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    63
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    64
(* display tagged theorems *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    65
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    66
fun pretty_tag (name, []) = Pretty.str name
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    67
  | pretty_tag (name, args) = Pretty.block
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    68
      [Pretty.str name, Pretty.list "(" ")" (map Pretty.str args)];
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    69
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    70
val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    71
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    72
fun pretty_tthm (thm, []) = Pretty.quote (Display.pretty_thm thm)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    73
  | pretty_tthm (thm, tags) = Pretty.block
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    74
      [Pretty.quote (Display.pretty_thm thm), Pretty.brk 1, pretty_tags tags];
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    75
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    76
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    77
(* basic attributes *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    78
4790
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    79
fun tag tg (x, (thm, tags)) = (x, (thm, tg ins tags));
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    80
fun untag tg (x, (thm, tags)) = (x, (thm, tags \ tg));
4780
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    81
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    82
val lemma = ("lemma", []);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    83
val assumption = ("assumption", []);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    84
fun tag_lemma x = tag lemma x;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    85
fun tag_assumption x = tag assumption x;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    86
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    87
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    88
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    89
(** theory data **)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    90
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    91
(* data kind 'attributes' *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    92
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    93
val attributesK = "Pure/attributes";
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    94
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    95
exception Attributes of
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    96
  {space: NameSpace.T,
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    97
   attrs:
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    98
    ((string list -> theory attribute) *
4790
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
    99
     (string list -> local_theory attribute)) Symtab.table};
4780
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   100
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   101
fun print_attributes thy = Display.print_data thy attributesK;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   102
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   103
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   104
(* setup *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   105
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   106
local
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   107
  val empty = Attributes {space = NameSpace.empty, attrs = Symtab.empty};
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   108
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   109
  fun prep_ext (x as Attributes _) = x;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   110
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   111
  fun merge (Attributes {space = space1, attrs = attrs1},
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   112
      Attributes {space = space2, attrs = attrs2}) =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   113
    Attributes {
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   114
      space = NameSpace.merge (space1, space2),
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   115
      attrs = Symtab.merge (K true) (attrs1, attrs2)};
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   116
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   117
  fun print _ (Attributes {space, attrs}) =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   118
   (Pretty.writeln (Display.pretty_name_space ("attribute name space", space));
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   119
    Pretty.writeln (Pretty.strs ("attributes:" :: map fst (Symtab.dest attrs))));
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   120
in
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   121
  val setup = Theory.init_data [(attributesK, (empty, prep_ext, merge, print))];
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   122
end;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   123
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   124
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   125
(* get data record *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   126
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   127
fun get_attributes_sg sg =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   128
  (case Sign.get_data sg attributesK of
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   129
    Attributes x => x
4792
8e3c2dddb9c8 type_error;
wenzelm
parents: 4790
diff changeset
   130
  | _ => type_error attributesK);
4780
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   131
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   132
val get_attributes = get_attributes_sg o Theory.sign_of;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   133
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   134
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   135
(* get global / local attributes *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   136
4790
5adb93457e39 removed simple;
wenzelm
parents: 4780
diff changeset
   137
fun attrs which thy (raw_name, args) =
4780
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   138
  let
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   139
    val {space, attrs} = get_attributes thy;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   140
    val name = NameSpace.intern space raw_name;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   141
  in
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   142
    (case Symtab.lookup (attrs, name) of
4797
d66477d29598 tuned fail;
wenzelm
parents: 4792
diff changeset
   143
      None => raise FAIL (name, "unknown attribute")
4780
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   144
    | Some p => which p args)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   145
  end;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   146
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   147
val global_attr = attrs fst;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   148
val local_attr = attrs snd;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   149
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   150
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   151
(* add_attrs *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   152
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   153
fun add_attrs raw_attrs thy =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   154
  let
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   155
    val full = Sign.full_name (Theory.sign_of thy);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   156
    val new_attrs = map (apfst full) raw_attrs;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   157
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   158
    val {space, attrs} = get_attributes thy;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   159
    val space' = NameSpace.extend (space, map fst new_attrs);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   160
    val attrs' = Symtab.extend (attrs, new_attrs)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   161
      handle Symtab.DUPS dups =>
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   162
        error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   163
  in
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   164
    Theory.put_data (attributesK, Attributes {space = space', attrs = attrs'}) thy
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   165
  end;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   166
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   167
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   168
end;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   169
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   170
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   171
structure BasicAttribute: BASIC_ATTRIBUTE = Attribute;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   172
open BasicAttribute;