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.
|
|
10 |
|
|
11 |
TODO:
|
|
12 |
- absolute paths?
|
3761
|
13 |
*)
|
|
14 |
|
|
15 |
signature NAME_SPACE =
|
|
16 |
sig
|
3769
|
17 |
val separator: string (*single char!*)
|
3761
|
18 |
val unpack: string -> string list
|
|
19 |
val pack: string list -> string
|
3786
|
20 |
val base: string -> string
|
3761
|
21 |
val qualified: string -> bool
|
|
22 |
type T
|
|
23 |
val dest: T -> string list
|
|
24 |
val empty: T
|
3786
|
25 |
val extend: string list * T -> T
|
3761
|
26 |
val merge: T * T -> T
|
|
27 |
val lookup: T -> string -> string
|
|
28 |
val prune: T -> string -> string
|
|
29 |
end;
|
|
30 |
|
|
31 |
structure NameSpace: NAME_SPACE =
|
|
32 |
struct
|
|
33 |
|
|
34 |
|
|
35 |
(** long identifiers **)
|
|
36 |
|
|
37 |
val separator = "'";
|
|
38 |
|
|
39 |
fun unpack_aux name =
|
|
40 |
let
|
|
41 |
(*handle trailing separators gracefully*)
|
|
42 |
val (chars, seps) = take_suffix (equal separator) name;
|
|
43 |
fun expl chs =
|
|
44 |
(case take_prefix (not_equal separator) chs of
|
|
45 |
(cs, []) => [implode (cs @ seps)]
|
|
46 |
| (cs, _ :: cs') => implode cs :: expl cs');
|
|
47 |
in expl chars end;
|
|
48 |
|
|
49 |
val unpack = unpack_aux o explode;
|
|
50 |
val pack = space_implode separator;
|
|
51 |
|
3786
|
52 |
val base = last_elem o unpack;
|
|
53 |
|
3761
|
54 |
fun qualified name =
|
|
55 |
let val chs = explode name in
|
|
56 |
exists (equal separator) chs andalso (length (unpack_aux chs) > 1)
|
|
57 |
end;
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
(** name spaces **)
|
|
62 |
|
|
63 |
(* utils *)
|
|
64 |
|
|
65 |
fun prefixes1 [] = []
|
|
66 |
| prefixes1 (x :: xs) = map (cons x) ([] :: prefixes1 xs);
|
|
67 |
|
|
68 |
fun suffixes1 xs = map rev (prefixes1 (rev xs));
|
|
69 |
|
|
70 |
|
|
71 |
(* datatype T *)
|
|
72 |
|
|
73 |
datatype T =
|
|
74 |
NameSpace of string list list * string Symtab.table;
|
|
75 |
|
|
76 |
fun entries_of (NameSpace (entries, _)) = entries;
|
|
77 |
fun tab_of (NameSpace (_, tab)) = tab;
|
|
78 |
|
|
79 |
fun make entries =
|
|
80 |
let
|
|
81 |
fun accesses entry =
|
|
82 |
let val packed = pack entry in
|
|
83 |
map (rpair packed o pack) (suffixes1 entry)
|
|
84 |
end;
|
|
85 |
val mapping = gen_distinct eq_fst (flat (map accesses entries));
|
|
86 |
in
|
|
87 |
NameSpace (entries, Symtab.make mapping)
|
|
88 |
end;
|
|
89 |
|
|
90 |
fun dest space = rev (map pack (entries_of space));
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
(* empty, extend, merge operations *)
|
|
95 |
|
|
96 |
val empty = make [];
|
|
97 |
|
3786
|
98 |
fun extend (entries, space) =
|
|
99 |
make (map unpack (rev entries) @ entries_of space);
|
3761
|
100 |
|
|
101 |
fun merge (space1, space2) =
|
|
102 |
make (merge_lists (entries_of space1) (entries_of space2));
|
|
103 |
|
|
104 |
|
|
105 |
(* lookup / prune names *)
|
|
106 |
|
|
107 |
fun lookup space name =
|
|
108 |
if_none (Symtab.lookup (tab_of space, name)) name;
|
|
109 |
|
|
110 |
fun prune space name =
|
3786
|
111 |
if not (qualified name) then name
|
|
112 |
else
|
|
113 |
let
|
|
114 |
fun try [] = name
|
|
115 |
| try (nm :: nms) =
|
|
116 |
if lookup space nm = name then nm
|
|
117 |
else try nms;
|
|
118 |
in try (map pack (suffixes1 (unpack name))) end;
|
3761
|
119 |
|
|
120 |
|
|
121 |
end;
|