src/Pure/General/name_space.ML
author wenzelm
Wed Jun 10 11:51:58 1998 +0200 (1998-06-10)
changeset 5012 086b055c4d73
child 5175 2dbef0104bcf
permissions -rw-r--r--
moved name_space.ML to General/name_space.ML;
wenzelm@5012
     1
(*  Title:      Pure/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@5012
    14
  val separator: string                 (*single char!*)
wenzelm@5012
    15
  val append: string -> string -> string
wenzelm@5012
    16
  val unpack: string -> string list
wenzelm@5012
    17
  val pack: string list -> string
wenzelm@5012
    18
  val base: string -> string
wenzelm@5012
    19
  val qualified: string -> bool
wenzelm@5012
    20
  val accesses: string -> string list
wenzelm@5012
    21
  type T
wenzelm@5012
    22
  val empty: T
wenzelm@5012
    23
  val extend: T * string list -> T
wenzelm@5012
    24
  val merge: T * T -> T
wenzelm@5012
    25
  val intern: T -> string -> string
wenzelm@5012
    26
  val extern: T -> string -> string
wenzelm@5012
    27
  val dest: T -> (string * string list) list
wenzelm@5012
    28
end;
wenzelm@5012
    29
wenzelm@5012
    30
structure NameSpace: NAME_SPACE =
wenzelm@5012
    31
struct
wenzelm@5012
    32
wenzelm@5012
    33
wenzelm@5012
    34
(** utils **)
wenzelm@5012
    35
wenzelm@5012
    36
fun prefixes1 [] = []
wenzelm@5012
    37
  | prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
wenzelm@5012
    38
wenzelm@5012
    39
fun suffixes1 xs = map rev (prefixes1 (rev xs));
wenzelm@5012
    40
wenzelm@5012
    41
wenzelm@5012
    42
wenzelm@5012
    43
(** long identifiers **)
wenzelm@5012
    44
wenzelm@5012
    45
val separator = ".";
wenzelm@5012
    46
wenzelm@5012
    47
fun append name1 name2 = name1 ^ separator ^ name2;
wenzelm@5012
    48
wenzelm@5012
    49
val unpack = space_explode separator;
wenzelm@5012
    50
val pack = space_implode separator;
wenzelm@5012
    51
wenzelm@5012
    52
val base = last_elem o unpack;
wenzelm@5012
    53
fun qualified name = length (unpack name) > 1;
wenzelm@5012
    54
wenzelm@5012
    55
fun accesses name =
wenzelm@5012
    56
  let
wenzelm@5012
    57
    val uname = unpack name;
wenzelm@5012
    58
    val (q, b) = split_last uname;
wenzelm@5012
    59
    val sfxs = suffixes1 uname;
wenzelm@5012
    60
    val pfxs = map (fn x => x @ [b]) (prefixes1 q);
wenzelm@5012
    61
  in map pack (sfxs @ pfxs) end;
wenzelm@5012
    62
wenzelm@5012
    63
wenzelm@5012
    64
wenzelm@5012
    65
(** name spaces **)
wenzelm@5012
    66
wenzelm@5012
    67
(* datatype T *)
wenzelm@5012
    68
wenzelm@5012
    69
datatype T =
wenzelm@5012
    70
  NameSpace of string Symtab.table;
wenzelm@5012
    71
wenzelm@5012
    72
val empty = NameSpace Symtab.empty;
wenzelm@5012
    73
wenzelm@5012
    74
wenzelm@5012
    75
(* extend, merge operations *)
wenzelm@5012
    76
wenzelm@5012
    77
fun add (tab, entry) = Symtab.update (entry, tab);
wenzelm@5012
    78
wenzelm@5012
    79
fun extend (NameSpace tab, names) =
wenzelm@5012
    80
  NameSpace (foldl add (tab, flat (map (fn name => map (rpair name) (accesses name)) names)));
wenzelm@5012
    81
wenzelm@5012
    82
fun merge (NameSpace tab1, NameSpace tab2) =    (*2nd overrides 1st*)
wenzelm@5012
    83
  NameSpace (foldl add (tab1, Symtab.dest tab2));
wenzelm@5012
    84
wenzelm@5012
    85
wenzelm@5012
    86
(* intern / extern names *)
wenzelm@5012
    87
wenzelm@5012
    88
fun intern (NameSpace tab) name =
wenzelm@5012
    89
  if_none (Symtab.lookup (tab, name)) name;
wenzelm@5012
    90
wenzelm@5012
    91
fun extern space name =
wenzelm@5012
    92
  let
wenzelm@5012
    93
    fun try [] = "??" ^ separator ^ name      (*hidden name*)
wenzelm@5012
    94
      | try (nm :: nms) =
wenzelm@5012
    95
          if intern space nm = name then nm
wenzelm@5012
    96
          else try nms;
wenzelm@5012
    97
  in try (map pack (suffixes1 (unpack name))) end;
wenzelm@5012
    98
wenzelm@5012
    99
wenzelm@5012
   100
(* dest *)
wenzelm@5012
   101
wenzelm@5012
   102
fun dest (NameSpace tab) =
wenzelm@5012
   103
  map (apsnd (sort (int_ord o pairself size)))
wenzelm@5012
   104
    (Symtab.dest (Symtab.make_multi (map swap (Symtab.dest tab))));
wenzelm@5012
   105
wenzelm@5012
   106
wenzelm@5012
   107
end;