src/Pure/General/name_space.ML
author wenzelm
Sun Jun 25 23:46:03 2000 +0200 (2000-06-25)
changeset 9120 04a46ceace35
parent 8806 a202293db3f6
child 11829 f252646080fc
permissions -rw-r--r--
export hidden: string -> string;
wenzelm@6118
     1
(*  Title:      Pure/General/name_space.ML
wenzelm@5012
     2
    ID:         $Id$
wenzelm@5012
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8806
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@5012
     5
wenzelm@5012
     6
Hierarchically structured name spaces.
wenzelm@5012
     7
wenzelm@5012
     8
More general than ML-like nested structures, but also slightly more
wenzelm@5012
     9
ad-hoc.  Does not support absolute addressing.  Unknown names are
wenzelm@5012
    10
implicitely considered to be declared outermost.
wenzelm@5012
    11
*)
wenzelm@5012
    12
wenzelm@5012
    13
signature NAME_SPACE =
wenzelm@5012
    14
sig
wenzelm@5012
    15
  val separator: string                 (*single char!*)
wenzelm@9120
    16
  val hidden: string -> string
wenzelm@5012
    17
  val append: string -> string -> string
wenzelm@5012
    18
  val unpack: string -> string list
wenzelm@5012
    19
  val pack: string list -> string
wenzelm@5012
    20
  val base: string -> string
wenzelm@8728
    21
  val is_qualified: string -> bool
wenzelm@5012
    22
  val accesses: string -> string list
wenzelm@5012
    23
  type T
wenzelm@5012
    24
  val empty: T
wenzelm@5012
    25
  val extend: T * string list -> T
wenzelm@5012
    26
  val merge: T * T -> T
wenzelm@8728
    27
  val hide: T * string list -> T
wenzelm@5012
    28
  val intern: T -> string -> string
wenzelm@5012
    29
  val extern: T -> string -> string
wenzelm@5175
    30
  val long_names: bool ref
wenzelm@5175
    31
  val cond_extern: T -> string -> string
wenzelm@6845
    32
  val cond_extern_table: T -> 'a Symtab.table -> (string * 'a) list
wenzelm@5012
    33
  val dest: T -> (string * string list) list
wenzelm@5012
    34
end;
wenzelm@5012
    35
wenzelm@5012
    36
structure NameSpace: NAME_SPACE =
wenzelm@5012
    37
struct
wenzelm@5012
    38
wenzelm@5012
    39
wenzelm@5012
    40
(** utils **)
wenzelm@5012
    41
wenzelm@5012
    42
fun prefixes1 [] = []
wenzelm@5012
    43
  | prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
wenzelm@5012
    44
wenzelm@5012
    45
fun suffixes1 xs = map rev (prefixes1 (rev xs));
wenzelm@5012
    46
wenzelm@5012
    47
wenzelm@5012
    48
wenzelm@5012
    49
(** long identifiers **)
wenzelm@5012
    50
wenzelm@5012
    51
val separator = ".";
wenzelm@8728
    52
fun hidden name = "??." ^ name;
wenzelm@8728
    53
fun is_hidden name = (case explode name of "?" :: "?" :: "." :: _ => true | _ => false);
wenzelm@5012
    54
wenzelm@5012
    55
fun append name1 name2 = name1 ^ separator ^ name2;
wenzelm@5012
    56
wenzelm@5012
    57
val unpack = space_explode separator;
wenzelm@5012
    58
val pack = space_implode separator;
wenzelm@5012
    59
wenzelm@5012
    60
val base = last_elem o unpack;
wenzelm@8728
    61
fun is_qualified name = length (unpack name) > 1;
wenzelm@5012
    62
wenzelm@5012
    63
fun accesses name =
wenzelm@5012
    64
  let
wenzelm@5012
    65
    val uname = unpack name;
wenzelm@5012
    66
    val (q, b) = split_last uname;
wenzelm@5012
    67
    val sfxs = suffixes1 uname;
wenzelm@5012
    68
    val pfxs = map (fn x => x @ [b]) (prefixes1 q);
wenzelm@5012
    69
  in map pack (sfxs @ pfxs) end;
wenzelm@5012
    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 =
wenzelm@8728
    78
  Symtab.update ((xname, f (if_none (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)
wenzelm@8728
   103
  else foldl (fn (tb, xname) => change add xname (name, tb)) (tab, accesses name);
wenzelm@5012
   104
wenzelm@8728
   105
fun extend (NameSpace tab, names) = NameSpace (foldr extend_aux (names, tab));
wenzelm@8728
   106
wenzelm@8728
   107
wenzelm@8728
   108
(* merge *)             (*1st preferred over 2nd*)
wenzelm@8728
   109
wenzelm@8728
   110
fun merge_aux (tab, (xname, (names, names'))) =
wenzelm@8728
   111
  foldr (change add xname) (names, foldr (change' add xname) (names', tab));
wenzelm@5012
   112
wenzelm@8728
   113
fun merge (NameSpace tab1, NameSpace tab2) =
wenzelm@8728
   114
  NameSpace (Symtab.foldl merge_aux (tab2, tab1));
wenzelm@8728
   115
wenzelm@8728
   116
wenzelm@8728
   117
(* hide *)
wenzelm@5012
   118
wenzelm@8728
   119
fun hide_aux (name, tab) =
wenzelm@8728
   120
  if not (is_qualified name) then
wenzelm@8728
   121
    error ("Attempt to hide global name " ^ quote name)
wenzelm@8728
   122
  else if is_hidden name then
wenzelm@8728
   123
    error ("Attempt to hide hidden name " ^ quote name)
wenzelm@8728
   124
  else (change' add name (name,
wenzelm@8728
   125
    foldl (fn (tb, xname) => change del xname (name, tb)) (tab, accesses name)));
wenzelm@8728
   126
wenzelm@8728
   127
fun hide (NameSpace tab, names) = NameSpace (foldr hide_aux (names, tab));
wenzelm@5012
   128
wenzelm@5012
   129
wenzelm@5012
   130
(* intern / extern names *)
wenzelm@5012
   131
wenzelm@8728
   132
fun lookup (NameSpace tab) xname =
wenzelm@8728
   133
  (case Symtab.lookup (tab, xname) of
wenzelm@8728
   134
    None => (xname, true)
wenzelm@8728
   135
  | Some ([name], _) => (name, true)
wenzelm@8728
   136
  | Some (name :: _, _) => (name, false)
wenzelm@8728
   137
  | Some ([], []) => (xname, true)
wenzelm@8728
   138
  | Some ([], name' :: _) => (hidden name', true));
wenzelm@8728
   139
wenzelm@8728
   140
fun intern spc xname = #1 (lookup spc xname);
wenzelm@5012
   141
wenzelm@5012
   142
fun extern space name =
wenzelm@5012
   143
  let
wenzelm@8728
   144
    fun try [] = hidden name
wenzelm@5012
   145
      | try (nm :: nms) =
wenzelm@8728
   146
          let val (full_nm, uniq) = lookup space nm
wenzelm@8728
   147
          in if full_nm = name andalso uniq then nm else try nms end
wenzelm@5012
   148
  in try (map pack (suffixes1 (unpack name))) end;
wenzelm@5012
   149
wenzelm@5175
   150
(*prune names on output by default*)
wenzelm@5175
   151
val long_names = ref false;
wenzelm@5175
   152
wenzelm@5175
   153
fun cond_extern space = if ! long_names then I else extern space;
wenzelm@5175
   154
wenzelm@6845
   155
fun cond_extern_table space tab =
wenzelm@6845
   156
  Library.sort_wrt #1 (map (apfst (cond_extern space)) (Symtab.dest tab));
wenzelm@6845
   157
wenzelm@5012
   158
wenzelm@5012
   159
(* dest *)
wenzelm@5012
   160
wenzelm@8728
   161
fun dest_entry (xname, ([], _)) = None
wenzelm@8728
   162
  | dest_entry (xname, (name :: _, _)) = Some (name, xname);
wenzelm@8728
   163
wenzelm@5012
   164
fun dest (NameSpace tab) =
wenzelm@5012
   165
  map (apsnd (sort (int_ord o pairself size)))
wenzelm@8728
   166
    (Symtab.dest (Symtab.make_multi (mapfilter dest_entry (Symtab.dest tab))));
wenzelm@5012
   167
wenzelm@5012
   168
wenzelm@5012
   169
end;
wenzelm@5175
   170
wenzelm@5175
   171
wenzelm@5175
   172
val long_names = NameSpace.long_names;