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
|
3833
|
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 dest: T -> string list
|
|
21 |
val empty: T
|
3786
|
22 |
val extend: string list * T -> T
|
3761
|
23 |
val merge: T * T -> T
|
|
24 |
val lookup: T -> string -> string
|
|
25 |
val prune: T -> string -> string
|
|
26 |
end;
|
|
27 |
|
3833
|
28 |
structure NameSpace(*: NAME_SPACE FIXME *) =
|
3761
|
29 |
struct
|
|
30 |
|
|
31 |
|
|
32 |
(** long identifiers **)
|
|
33 |
|
3833
|
34 |
val separator = ".";
|
3761
|
35 |
|
3833
|
36 |
val unpack = space_explode separator;
|
3761
|
37 |
val pack = space_implode separator;
|
|
38 |
|
3786
|
39 |
val base = last_elem o unpack;
|
3833
|
40 |
fun qualified name = length (unpack name) > 1;
|
3761
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
(** name spaces **)
|
|
45 |
|
|
46 |
(* utils *)
|
|
47 |
|
|
48 |
fun prefixes1 [] = []
|
|
49 |
| prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
|
|
50 |
|
|
51 |
fun suffixes1 xs = map rev (prefixes1 (rev xs));
|
|
52 |
|
|
53 |
|
|
54 |
(* datatype T *)
|
|
55 |
|
|
56 |
datatype T =
|
|
57 |
NameSpace of string list list * string Symtab.table;
|
|
58 |
|
|
59 |
fun entries_of (NameSpace (entries, _)) = entries;
|
|
60 |
fun tab_of (NameSpace (_, tab)) = tab;
|
|
61 |
|
|
62 |
fun make entries =
|
|
63 |
let
|
3833
|
64 |
fun accesses [] = [] (* FIXME !? *)
|
|
65 |
| accesses entry =
|
|
66 |
let
|
|
67 |
val p = pack entry;
|
|
68 |
val (q, b) = split_last entry;
|
|
69 |
val sfxs = suffixes1 entry;
|
|
70 |
val pfxs = map (fn x => x @ [b]) (prefixes1 q);
|
|
71 |
in
|
|
72 |
map (rpair p o pack) (sfxs @ pfxs)
|
|
73 |
end;
|
3803
|
74 |
val mapping = filter_out (op =)
|
|
75 |
(gen_distinct eq_fst (flat (map accesses entries)));
|
3761
|
76 |
in
|
|
77 |
NameSpace (entries, Symtab.make mapping)
|
|
78 |
end;
|
|
79 |
|
|
80 |
fun dest space = rev (map pack (entries_of space));
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
(* empty, extend, merge operations *)
|
|
85 |
|
|
86 |
val empty = make [];
|
|
87 |
|
3786
|
88 |
fun extend (entries, space) =
|
|
89 |
make (map unpack (rev entries) @ entries_of space);
|
3761
|
90 |
|
3803
|
91 |
fun merge (space1, space2) = (*2nd overrides 1st*)
|
|
92 |
make (merge_lists (entries_of space2) (entries_of space1));
|
3761
|
93 |
|
|
94 |
|
|
95 |
(* lookup / prune names *)
|
|
96 |
|
|
97 |
fun lookup space name =
|
|
98 |
if_none (Symtab.lookup (tab_of space, name)) name;
|
|
99 |
|
|
100 |
fun prune space name =
|
3786
|
101 |
if not (qualified name) then name
|
|
102 |
else
|
|
103 |
let
|
3833
|
104 |
fun try [] = "??" ^ separator ^ name (*hidden name*)
|
3786
|
105 |
| try (nm :: nms) =
|
|
106 |
if lookup space nm = name then nm
|
|
107 |
else try nms;
|
|
108 |
in try (map pack (suffixes1 (unpack name))) end;
|
3761
|
109 |
|
|
110 |
|
|
111 |
end;
|