src/Pure/attribute.ML
author wenzelm
Fri, 03 Apr 1998 14:35:39 +0200
changeset 4780 f4ff003bc7ee
child 4790 5adb93457e39
permissions -rw-r--r--
Theorem tags and attributes.
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
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    21
  val apply: ('a * tthm) * 'a attribute list -> ('a * tthm)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    22
  val pretty_tthm: tthm -> Pretty.T
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    23
  val tag: tag -> 'a attribute
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    24
  val simple: string -> 'a attribute
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    25
  val lemma: tag
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    26
  val assumption: tag
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    27
  val tag_lemma: 'a attribute
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    28
  val tag_assumption: 'a attribute
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    29
  val setup: theory -> theory
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    30
  val global_attr: theory -> xstring -> string list -> theory attribute
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    31
  val local_attr: theory -> xstring -> string list -> object Symtab.table attribute
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    32
  val add_attrs: (bstring * ((string list -> theory attribute) *
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    33
      (string list -> object Symtab.table attribute))) list -> theory -> theory
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    34
end;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    35
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    36
structure Attribute: ATTRIBUTE =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    37
struct
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    38
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    39
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    40
(** tags and attributes **)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    41
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    42
type tag = string * string list;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    43
type tthm = thm * tag list;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    44
type 'a attribute = 'a * tthm -> 'a * tthm;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    45
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    46
fun thm_of (thm, _) = thm;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    47
fun tthm_of thm = (thm, []);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    48
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    49
fun apply x_attrs = foldl (op |>) x_attrs;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    50
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    51
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    52
(* display tagged theorems *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    53
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    54
fun pretty_tag (name, []) = Pretty.str name
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    55
  | pretty_tag (name, args) = Pretty.block
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    56
      [Pretty.str name, Pretty.list "(" ")" (map Pretty.str args)];
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    57
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    58
val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    59
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    60
fun pretty_tthm (thm, []) = Pretty.quote (Display.pretty_thm thm)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    61
  | pretty_tthm (thm, tags) = Pretty.block
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    62
      [Pretty.quote (Display.pretty_thm thm), Pretty.brk 1, pretty_tags tags];
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    63
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    64
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    65
(* basic attributes *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    66
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    67
fun tag name_args (x, (thm, tags)) = (x, (thm, name_args ins tags));
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    68
fun simple name = tag (name, []);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    69
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    70
val lemma = ("lemma", []);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    71
val assumption = ("assumption", []);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    72
fun tag_lemma x = tag lemma x;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    73
fun tag_assumption x = tag assumption x;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    74
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
(** theory data **)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    78
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    79
(* data kind 'attributes' *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    80
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    81
val attributesK = "Pure/attributes";
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    82
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    83
exception Attributes of
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    84
  {space: NameSpace.T,
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    85
   attrs:
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    86
    ((string list -> theory attribute) *
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    87
     (string list -> object Symtab.table attribute)) Symtab.table};
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    88
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    89
fun print_attributes thy = Display.print_data thy attributesK;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    90
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    91
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    92
(* setup *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    93
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    94
local
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    95
  val empty = Attributes {space = NameSpace.empty, attrs = Symtab.empty};
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    96
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    97
  fun prep_ext (x as Attributes _) = x;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    98
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
    99
  fun merge (Attributes {space = space1, attrs = attrs1},
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   100
      Attributes {space = space2, attrs = attrs2}) =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   101
    Attributes {
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   102
      space = NameSpace.merge (space1, space2),
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   103
      attrs = Symtab.merge (K true) (attrs1, attrs2)};
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   104
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   105
  fun print _ (Attributes {space, attrs}) =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   106
   (Pretty.writeln (Display.pretty_name_space ("attribute name space", space));
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   107
    Pretty.writeln (Pretty.strs ("attributes:" :: map fst (Symtab.dest attrs))));
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   108
in
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   109
  val setup = Theory.init_data [(attributesK, (empty, prep_ext, merge, print))];
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   110
end;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   111
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   112
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   113
(* get data record *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   114
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   115
fun get_attributes_sg sg =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   116
  (case Sign.get_data sg attributesK of
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   117
    Attributes x => x
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   118
  | _ => sys_error "get_attributes_sg");
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   119
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   120
val get_attributes = get_attributes_sg o Theory.sign_of;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   121
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   122
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   123
(* get global / local attributes *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   124
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   125
fun attrs which thy raw_name args =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   126
  let
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   127
    val {space, attrs} = get_attributes thy;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   128
    val name = NameSpace.intern space raw_name;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   129
  in
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   130
    (case Symtab.lookup (attrs, name) of
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   131
      None => error ("Unknown attribute: " ^ quote name)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   132
    | Some p => which p args)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   133
  end;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   134
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   135
val global_attr = attrs fst;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   136
val local_attr = attrs snd;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   137
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   138
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   139
(* add_attrs *)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   140
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   141
fun add_attrs raw_attrs thy =
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   142
  let
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   143
    val full = Sign.full_name (Theory.sign_of thy);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   144
    val new_attrs = map (apfst full) raw_attrs;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   145
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   146
    val {space, attrs} = get_attributes thy;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   147
    val space' = NameSpace.extend (space, map fst new_attrs);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   148
    val attrs' = Symtab.extend (attrs, new_attrs)
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   149
      handle Symtab.DUPS dups =>
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   150
        error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   151
  in
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   152
    Theory.put_data (attributesK, Attributes {space = space', attrs = attrs'}) thy
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   153
  end;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   154
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   155
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   156
end;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   157
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   158
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   159
structure BasicAttribute: BASIC_ATTRIBUTE = Attribute;
f4ff003bc7ee Theorem tags and attributes.
wenzelm
parents:
diff changeset
   160
open BasicAttribute;