3761
|
1 |
(* Title: Pure/name_space.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
Hierarchically structured name spaces.
|
|
6 |
|
3786
|
7 |
More general than ML-like nested structures, but also slightly more
|
|
8 |
ad-hoc. Does not support absolute addressing. Unknown names are
|
|
9 |
implicitely considered to be declared outermost.
|
3761
|
10 |
*)
|
|
11 |
|
|
12 |
signature NAME_SPACE =
|
|
13 |
sig
|
4490
|
14 |
val separator: string (*single char*)
|
3761
|
15 |
val unpack: string -> string list
|
|
16 |
val pack: string list -> string
|
3786
|
17 |
val base: string -> string
|
3761
|
18 |
val qualified: string -> bool
|
|
19 |
type T
|
|
20 |
val empty: T
|
4490
|
21 |
val extend: T * string list -> T
|
3761
|
22 |
val merge: T * T -> T
|
3993
|
23 |
val intern: T -> string -> string
|
|
24 |
val extern: T -> string -> string
|
4497
|
25 |
val dest: T -> (string * string list) list
|
3761
|
26 |
end;
|
|
27 |
|
3876
|
28 |
structure NameSpace: NAME_SPACE =
|
3761
|
29 |
struct
|
|
30 |
|
|
31 |
|
4490
|
32 |
(** utils **)
|
|
33 |
|
|
34 |
fun prefixes1 [] = []
|
|
35 |
| prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
|
|
36 |
|
|
37 |
fun suffixes1 xs = map rev (prefixes1 (rev xs));
|
|
38 |
|
|
39 |
|
|
40 |
|
3761
|
41 |
(** long identifiers **)
|
|
42 |
|
3833
|
43 |
val separator = ".";
|
3761
|
44 |
|
3833
|
45 |
val unpack = space_explode separator;
|
3761
|
46 |
val pack = space_implode separator;
|
|
47 |
|
3786
|
48 |
val base = last_elem o unpack;
|
3833
|
49 |
fun qualified name = length (unpack name) > 1;
|
3761
|
50 |
|
4490
|
51 |
fun accesses name =
|
|
52 |
let
|
|
53 |
val uname = unpack name;
|
|
54 |
val (q, b) = split_last uname;
|
|
55 |
val sfxs = suffixes1 uname;
|
|
56 |
val pfxs = map (fn x => x @ [b]) (prefixes1 q);
|
|
57 |
in
|
|
58 |
map (rpair name o pack) (sfxs @ pfxs)
|
|
59 |
end;
|
|
60 |
|
3761
|
61 |
|
|
62 |
|
|
63 |
(** name spaces **)
|
|
64 |
|
|
65 |
(* datatype T *)
|
|
66 |
|
|
67 |
datatype T =
|
4490
|
68 |
NameSpace of string Symtab.table;
|
3761
|
69 |
|
4490
|
70 |
val empty = NameSpace Symtab.empty;
|
3761
|
71 |
|
|
72 |
|
4490
|
73 |
(* extend, merge operations *)
|
3761
|
74 |
|
4490
|
75 |
fun add (tab, entry) = Symtab.update (entry, tab);
|
3761
|
76 |
|
4490
|
77 |
fun extend (NameSpace tab, names) =
|
|
78 |
NameSpace (foldl add (tab, flat (map accesses names)));
|
3761
|
79 |
|
4490
|
80 |
fun merge (NameSpace tab1, NameSpace tab2) = (*2nd overrides 1st*)
|
|
81 |
NameSpace (foldl add (tab1, Symtab.dest tab2));
|
3761
|
82 |
|
|
83 |
|
3993
|
84 |
(* intern / extern names *)
|
|
85 |
|
4490
|
86 |
fun intern (NameSpace tab) name =
|
|
87 |
if_none (Symtab.lookup (tab, name)) name;
|
3761
|
88 |
|
3993
|
89 |
fun extern space name =
|
3972
|
90 |
let
|
|
91 |
fun try [] = "??" ^ separator ^ name (*hidden name*)
|
|
92 |
| try (nm :: nms) =
|
3993
|
93 |
if intern space nm = name then nm
|
3972
|
94 |
else try nms;
|
|
95 |
in try (map pack (suffixes1 (unpack name))) end;
|
3761
|
96 |
|
|
97 |
|
4497
|
98 |
(* dest *)
|
|
99 |
|
|
100 |
fun dest (NameSpace tab) =
|
|
101 |
map (apsnd (sort (int_ord o pairself size)))
|
|
102 |
(Symtab.dest (Symtab.make_multi (map swap (Symtab.dest tab))));
|
|
103 |
|
|
104 |
|
3761
|
105 |
end;
|