src/Pure/General/name_space.ML
author skalberg
Thu Mar 03 12:43:01 2005 +0100 (2005-03-03)
changeset 15570 8d8c70b41bab
parent 15531 08c8dad8e399
child 15574 b1d1b5bfc464
permissions -rw-r--r--
Move towards standard functions.
wenzelm@6118
     1
(*  Title:      Pure/General/name_space.ML
wenzelm@5012
     2
    ID:         $Id$
wenzelm@5012
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5012
     4
wenzelm@5012
     5
Hierarchically structured name spaces.
wenzelm@5012
     6
wenzelm@5012
     7
More general than ML-like nested structures, but also slightly more
wenzelm@5012
     8
ad-hoc.  Does not support absolute addressing.  Unknown names are
wenzelm@5012
     9
implicitely considered to be declared outermost.
wenzelm@5012
    10
*)
wenzelm@5012
    11
wenzelm@5012
    12
signature NAME_SPACE =
wenzelm@5012
    13
sig
wenzelm@12588
    14
  val separator: string                 (*single char*)
wenzelm@9120
    15
  val hidden: string -> string
wenzelm@5012
    16
  val append: string -> string -> string
wenzelm@5012
    17
  val unpack: string -> string list
wenzelm@5012
    18
  val pack: string list -> string
wenzelm@5012
    19
  val base: string -> string
wenzelm@11829
    20
  val map_base: (string -> string) -> string -> string
wenzelm@8728
    21
  val is_qualified: string -> bool
wenzelm@5012
    22
  val accesses: string -> string list
wenzelm@13332
    23
  val accesses': string -> string list
wenzelm@5012
    24
  type T
wenzelm@5012
    25
  val empty: T
wenzelm@5012
    26
  val extend: T * string list -> T
wenzelm@5012
    27
  val merge: T * T -> T
wenzelm@12588
    28
  val hide: bool -> T * string list -> T
wenzelm@5012
    29
  val intern: T -> string -> string
wenzelm@5012
    30
  val extern: T -> string -> string
wenzelm@5175
    31
  val long_names: bool ref
berghofe@14382
    32
  val short_names: bool ref
schirmer@15016
    33
  val unique_names: bool ref
wenzelm@5175
    34
  val cond_extern: T -> string -> string
wenzelm@6845
    35
  val cond_extern_table: T -> 'a Symtab.table -> (string * 'a) list
wenzelm@5012
    36
  val dest: T -> (string * string list) list
wenzelm@5012
    37
end;
wenzelm@5012
    38
wenzelm@5012
    39
structure NameSpace: NAME_SPACE =
wenzelm@5012
    40
struct
wenzelm@5012
    41
wenzelm@5012
    42
(** long identifiers **)
wenzelm@5012
    43
wenzelm@5012
    44
val separator = ".";
wenzelm@8728
    45
fun hidden name = "??." ^ name;
wenzelm@8728
    46
fun is_hidden name = (case explode name of "?" :: "?" :: "." :: _ => true | _ => false);
wenzelm@5012
    47
wenzelm@5012
    48
fun append name1 name2 = name1 ^ separator ^ name2;
wenzelm@5012
    49
wenzelm@5012
    50
val unpack = space_explode separator;
wenzelm@5012
    51
val pack = space_implode separator;
wenzelm@5012
    52
skalberg@15570
    53
val base = List.last o unpack;
wenzelm@11829
    54
wenzelm@11829
    55
fun map_base f name =
wenzelm@11829
    56
  let val uname = unpack name
wenzelm@11829
    57
  in pack (map_nth_elem (length uname - 1) f uname) end;
wenzelm@11829
    58
wenzelm@8728
    59
fun is_qualified name = length (unpack name) > 1;
wenzelm@5012
    60
wenzelm@5012
    61
fun accesses name =
wenzelm@5012
    62
  let
wenzelm@5012
    63
    val uname = unpack name;
wenzelm@5012
    64
    val (q, b) = split_last uname;
wenzelm@12251
    65
    val sfxs = Library.suffixes1 uname;
wenzelm@12251
    66
    val pfxs = map (fn x => x @ [b]) (Library.prefixes1 q);
wenzelm@5012
    67
  in map pack (sfxs @ pfxs) end;
wenzelm@5012
    68
wenzelm@13332
    69
val accesses' = map pack o Library.suffixes1 o unpack;
wenzelm@13332
    70
wenzelm@5012
    71
wenzelm@5012
    72
wenzelm@5012
    73
(** name spaces **)
wenzelm@5012
    74
wenzelm@8728
    75
(* basic operations *)
wenzelm@8728
    76
wenzelm@8728
    77
fun map_space f xname tab =
skalberg@15570
    78
  Symtab.update ((xname, f (getOpt (Symtab.lookup (tab, xname), ([], [])))), tab);
wenzelm@8728
    79
wenzelm@8728
    80
fun change f xname (name, tab) =
wenzelm@8728
    81
  map_space (fn (names, names') => (f names name, names')) xname tab;
wenzelm@8728
    82
wenzelm@8728
    83
fun change' f xname (name', tab) =
wenzelm@8728
    84
  map_space (fn (names, names') => (names, f names' name')) xname tab;
wenzelm@8728
    85
wenzelm@8728
    86
fun del names nm = if nm mem_string names then Library.gen_rem (op =) (names, nm) else names;
wenzelm@8728
    87
fun add names nm = nm :: del names nm;
wenzelm@8728
    88
wenzelm@8728
    89
wenzelm@5012
    90
(* datatype T *)
wenzelm@5012
    91
wenzelm@5012
    92
datatype T =
wenzelm@8728
    93
  NameSpace of (string list * string list) Symtab.table;
wenzelm@5012
    94
wenzelm@5012
    95
val empty = NameSpace Symtab.empty;
wenzelm@5012
    96
wenzelm@5012
    97
wenzelm@8728
    98
(* extend *)            (*later entries preferred*)
wenzelm@8728
    99
wenzelm@8728
   100
fun extend_aux (name, tab) =
wenzelm@8728
   101
  if is_hidden name then
wenzelm@8728
   102
    error ("Attempt to declare hidden name " ^ quote name)
skalberg@15570
   103
  else Library.foldl (fn (tb, xname) => change add xname (name, tb)) (tab, accesses name);
wenzelm@5012
   104
skalberg@15570
   105
fun extend (NameSpace tab, names) = NameSpace (Library.foldr extend_aux (names, tab));
wenzelm@8728
   106
wenzelm@8728
   107
wenzelm@8728
   108
(* merge *)             (*1st preferred over 2nd*)
wenzelm@8728
   109
berghofe@14198
   110
fun merge_aux (tab, (xname, (names1, names1'))) =
berghofe@14198
   111
  map_space (fn (names2, names2') =>
berghofe@14198
   112
    (merge_lists' names2 names1, merge_lists' names2' names1')) xname tab;
wenzelm@5012
   113
wenzelm@8728
   114
fun merge (NameSpace tab1, NameSpace tab2) =
wenzelm@8728
   115
  NameSpace (Symtab.foldl merge_aux (tab2, tab1));
wenzelm@8728
   116
wenzelm@8728
   117
wenzelm@8728
   118
(* hide *)
wenzelm@5012
   119
wenzelm@12588
   120
fun hide_aux fully (name, tab) =
wenzelm@8728
   121
  if not (is_qualified name) then
wenzelm@8728
   122
    error ("Attempt to hide global name " ^ quote name)
wenzelm@8728
   123
  else if is_hidden name then
wenzelm@8728
   124
    error ("Attempt to hide hidden name " ^ quote name)
wenzelm@8728
   125
  else (change' add name (name,
skalberg@15570
   126
    Library.foldl (fn (tb, xname) => change del xname (name, tb))
wenzelm@12588
   127
      (tab, if fully then accesses name else [base name])));
wenzelm@8728
   128
skalberg@15570
   129
fun hide fully (NameSpace tab, names) = NameSpace (Library.foldr (hide_aux fully) (names, tab));
wenzelm@5012
   130
wenzelm@5012
   131
wenzelm@5012
   132
(* intern / extern names *)
wenzelm@5012
   133
wenzelm@8728
   134
fun lookup (NameSpace tab) xname =
wenzelm@8728
   135
  (case Symtab.lookup (tab, xname) of
skalberg@15531
   136
    NONE => (xname, true)
skalberg@15531
   137
  | SOME ([name], _) => (name, true)
skalberg@15531
   138
  | SOME (name :: _, _) => (name, false)
skalberg@15531
   139
  | SOME ([], []) => (xname, true)
skalberg@15531
   140
  | SOME ([], name' :: _) => (hidden name', true));
wenzelm@8728
   141
wenzelm@8728
   142
fun intern spc xname = #1 (lookup spc xname);
wenzelm@5012
   143
schirmer@15016
   144
fun unique_extern mkunique space name =
wenzelm@5012
   145
  let
wenzelm@8728
   146
    fun try [] = hidden name
wenzelm@5012
   147
      | try (nm :: nms) =
wenzelm@8728
   148
          let val (full_nm, uniq) = lookup space nm
schirmer@15016
   149
          in if full_nm = name andalso (uniq orelse (not mkunique)) then nm else try nms end
wenzelm@13332
   150
  in try (accesses' name) end;
wenzelm@5012
   151
schirmer@15016
   152
(*output unique names by default*)
schirmer@15016
   153
val unique_names = ref true;
schirmer@15016
   154
wenzelm@5175
   155
(*prune names on output by default*)
wenzelm@5175
   156
val long_names = ref false;
wenzelm@5175
   157
berghofe@14382
   158
(*output only base name*)
berghofe@14382
   159
val short_names = ref false;
berghofe@14382
   160
schirmer@15016
   161
fun extern space name = unique_extern (!unique_names) space name; 
schirmer@15016
   162
berghofe@14382
   163
fun cond_extern space =
berghofe@14382
   164
  if ! long_names then I
berghofe@14382
   165
  else if ! short_names then base else extern space;
wenzelm@5175
   166
wenzelm@6845
   167
fun cond_extern_table space tab =
wenzelm@6845
   168
  Library.sort_wrt #1 (map (apfst (cond_extern space)) (Symtab.dest tab));
wenzelm@6845
   169
wenzelm@5012
   170
wenzelm@5012
   171
(* dest *)
wenzelm@5012
   172
skalberg@15531
   173
fun dest_entry (xname, ([], _)) = NONE
skalberg@15531
   174
  | dest_entry (xname, (name :: _, _)) = SOME (name, xname);
wenzelm@8728
   175
wenzelm@5012
   176
fun dest (NameSpace tab) =
wenzelm@5012
   177
  map (apsnd (sort (int_ord o pairself size)))
skalberg@15570
   178
    (Symtab.dest (Symtab.make_multi (List.mapPartial dest_entry (Symtab.dest tab))));
wenzelm@5012
   179
wenzelm@5012
   180
wenzelm@5012
   181
end;
wenzelm@5175
   182
wenzelm@5175
   183
wenzelm@5175
   184
val long_names = NameSpace.long_names;