src/Pure/Isar/attrib.ML
author wenzelm
Mon Nov 09 15:32:20 1998 +0100 (1998-11-09)
changeset 5823 ee7c198a2154
child 5879 18b8f048d93a
permissions -rw-r--r--
Symbolic theorem attributes.
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@5823
    11
end;
wenzelm@5823
    12
wenzelm@5823
    13
signature ATTRIB =
wenzelm@5823
    14
sig
wenzelm@5823
    15
  include BASIC_ATTRIB
wenzelm@5823
    16
  val global_attribute: theory -> Args.src -> theory attribute
wenzelm@5823
    17
  val local_attribute: theory -> Args.src -> Proof.context attribute
wenzelm@5823
    18
  val add_attributes: (bstring * ((Args.src -> theory attribute) *
wenzelm@5823
    19
      (Args.src -> Proof.context attribute)) * string) list -> theory -> theory
wenzelm@5823
    20
  val syntax: (Args.T list -> 'a * Args.T list) -> ('a -> 'b) -> Args.src -> 'b
wenzelm@5823
    21
  val no_args: 'a attribute -> Args.src -> 'a attribute
wenzelm@5823
    22
  val thm_args: ('a -> string list -> tthm list)
wenzelm@5823
    23
    -> (tthm list -> 'a attribute) -> Args.src -> 'a attribute
wenzelm@5823
    24
  val global_thm_args: (tthm list -> theory attribute) -> Args.src -> theory attribute
wenzelm@5823
    25
  val local_thm_args: (tthm list -> Proof.context attribute)
wenzelm@5823
    26
    -> Args.src -> Proof.context attribute
wenzelm@5823
    27
  val setup: (theory -> theory) list
wenzelm@5823
    28
end;
wenzelm@5823
    29
wenzelm@5823
    30
structure Attrib: ATTRIB =
wenzelm@5823
    31
struct
wenzelm@5823
    32
wenzelm@5823
    33
wenzelm@5823
    34
(** attributes theory data **)
wenzelm@5823
    35
wenzelm@5823
    36
(* data kind 'Isar/attributes' *)
wenzelm@5823
    37
wenzelm@5823
    38
structure AttributesDataArgs =
wenzelm@5823
    39
struct
wenzelm@5823
    40
  val name = "Isar/attributes";
wenzelm@5823
    41
  type T =
wenzelm@5823
    42
    {space: NameSpace.T,
wenzelm@5823
    43
     attrs:
wenzelm@5823
    44
       ((((Args.src -> theory attribute) * (Args.src -> Proof.context attribute))
wenzelm@5823
    45
         * string) * stamp) Symtab.table};
wenzelm@5823
    46
wenzelm@5823
    47
  val empty = {space = NameSpace.empty, attrs = Symtab.empty};
wenzelm@5823
    48
  val prep_ext = I;
wenzelm@5823
    49
wenzelm@5823
    50
  fun merge ({space = space1, attrs = attrs1}, {space = space2, attrs = attrs2}) =
wenzelm@5823
    51
    {space = NameSpace.merge (space1, space2),
wenzelm@5823
    52
      attrs = Symtab.merge eq_snd (attrs1, attrs2) handle Symtab.DUPS dups =>
wenzelm@5823
    53
        error ("Attempt to merge different versions of attributes " ^ commas_quote dups)};
wenzelm@5823
    54
wenzelm@5823
    55
  fun print _ ({space, attrs}) =
wenzelm@5823
    56
    let
wenzelm@5823
    57
      fun prt_attr (name, ((_, comment), _)) = Pretty.block
wenzelm@5823
    58
        [Pretty.str (NameSpace.cond_extern space name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@5823
    59
    in
wenzelm@5823
    60
      Pretty.writeln (Display.pretty_name_space ("attribute name space", space));
wenzelm@5823
    61
      Pretty.writeln (Pretty.big_list "attributes:" (map prt_attr (Symtab.dest attrs)))
wenzelm@5823
    62
    end;
wenzelm@5823
    63
end;
wenzelm@5823
    64
wenzelm@5823
    65
structure AttributesData = TheoryDataFun(AttributesDataArgs);
wenzelm@5823
    66
val print_attributes = AttributesData.print;
wenzelm@5823
    67
wenzelm@5823
    68
wenzelm@5823
    69
(* get global / local attributes *)
wenzelm@5823
    70
wenzelm@5823
    71
fun gen_attribute which thy =
wenzelm@5823
    72
  let
wenzelm@5823
    73
    val {space, attrs} = AttributesData.get thy;
wenzelm@5823
    74
wenzelm@5823
    75
    fun attr ((raw_name, args), pos) =
wenzelm@5823
    76
      let val name = NameSpace.intern space raw_name in
wenzelm@5823
    77
        (case Symtab.lookup (attrs, name) of
wenzelm@5823
    78
          None => error ("Unknown attribute: " ^ quote name ^ Position.str_of pos)
wenzelm@5823
    79
        | Some ((p, _), _) => which p ((name, args), pos))
wenzelm@5823
    80
      end;
wenzelm@5823
    81
  in attr end;
wenzelm@5823
    82
wenzelm@5823
    83
val global_attribute = gen_attribute fst;
wenzelm@5823
    84
val local_attribute = gen_attribute snd;
wenzelm@5823
    85
wenzelm@5823
    86
wenzelm@5823
    87
(* add_attributes *)
wenzelm@5823
    88
wenzelm@5823
    89
fun add_attributes raw_attrs thy =
wenzelm@5823
    90
  let
wenzelm@5823
    91
    val full = Sign.full_name (Theory.sign_of thy);
wenzelm@5823
    92
    val new_attrs =
wenzelm@5823
    93
      map (fn (name, (f, g), comment) => (full name, (((f, g), comment), stamp ()))) raw_attrs;
wenzelm@5823
    94
wenzelm@5823
    95
    val {space, attrs} = AttributesData.get thy;
wenzelm@5823
    96
    val space' = NameSpace.extend (space, map fst new_attrs);
wenzelm@5823
    97
    val attrs' = Symtab.extend (attrs, new_attrs) handle Symtab.DUPS dups =>
wenzelm@5823
    98
      error ("Duplicate declaration of attributes(s) " ^ commas_quote dups);
wenzelm@5823
    99
  in thy |> AttributesData.put {space = space', attrs = attrs'} end;
wenzelm@5823
   100
wenzelm@5823
   101
wenzelm@5823
   102
(* attribute syntax *)
wenzelm@5823
   103
wenzelm@5823
   104
val attributeN = "attribute";
wenzelm@5823
   105
fun syntax scan = Args.syntax attributeN scan;
wenzelm@5823
   106
wenzelm@5823
   107
fun no_args x = syntax (Scan.succeed x) I;
wenzelm@5823
   108
wenzelm@5823
   109
fun thm_args get f = syntax (Scan.repeat Args.name)
wenzelm@5823
   110
  (fn names => fn (x, ths) => f (get x names) (x, ths));
wenzelm@5823
   111
wenzelm@5823
   112
fun global_thm_args f = thm_args PureThy.get_tthmss f;
wenzelm@5823
   113
fun local_thm_args f = thm_args ProofContext.get_tthmss f;
wenzelm@5823
   114
wenzelm@5823
   115
wenzelm@5823
   116
wenzelm@5823
   117
(** Pure attributes **)
wenzelm@5823
   118
wenzelm@5823
   119
(* tags *)
wenzelm@5823
   120
wenzelm@5823
   121
fun gen_tag x = syntax (Args.name -- Scan.repeat Args.name) Attribute.tag x;
wenzelm@5823
   122
fun gen_untag x = syntax (Args.name -- Scan.repeat Args.name) Attribute.untag x;
wenzelm@5823
   123
fun gen_lemma x = no_args Attribute.tag_lemma x;
wenzelm@5823
   124
fun gen_assumption x = no_args Attribute.tag_assumption x;
wenzelm@5823
   125
wenzelm@5823
   126
wenzelm@5823
   127
(* resolution *)
wenzelm@5823
   128
wenzelm@5823
   129
fun gen_RS get = syntax Args.name
wenzelm@5823
   130
  (fn name => fn (x, (thm, tags)) => (x, (thm RS (Attribute.thm_of (get x name)), tags)));
wenzelm@5823
   131
wenzelm@5823
   132
fun RS_global x = gen_RS PureThy.get_tthm x;
wenzelm@5823
   133
fun RS_local x = gen_RS ProofContext.get_tthm x;
wenzelm@5823
   134
wenzelm@5823
   135
wenzelm@5823
   136
(* pure_attributes *)
wenzelm@5823
   137
wenzelm@5823
   138
val pure_attributes =
wenzelm@5823
   139
 [("tag", (gen_tag, gen_tag), "tag theorem"),
wenzelm@5823
   140
  ("untag", (gen_untag, gen_untag), "untag theorem"),
wenzelm@5823
   141
  ("lemma", (gen_lemma, gen_lemma), "tag as lemma"),
wenzelm@5823
   142
  ("assumption", (gen_assumption, gen_assumption), "tag as assumption"),
wenzelm@5823
   143
  ("RS", (RS_global, RS_local), "resolve with rule")];
wenzelm@5823
   144
wenzelm@5823
   145
wenzelm@5823
   146
wenzelm@5823
   147
(** theory setup **)
wenzelm@5823
   148
wenzelm@5823
   149
val setup = [AttributesData.init, add_attributes pure_attributes];
wenzelm@5823
   150
wenzelm@5823
   151
wenzelm@5823
   152
end;
wenzelm@5823
   153
wenzelm@5823
   154
wenzelm@5823
   155
structure BasicAttrib: BASIC_ATTRIB = Attrib;
wenzelm@5823
   156
open BasicAttrib;