author | wenzelm |
Thu, 27 Apr 2006 15:06:40 +0200 | |
changeset 19484 | 89484a62184a |
parent 19367 | 6af9ee48b563 |
child 20530 | 448594cbd82b |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/name_space.ML |
5012 | 2 |
ID: $Id$ |
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
16137 | 5 |
Generic name spaces with declared and hidden entries. Unknown names |
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
6 |
are considered global; no support for absolute addressing. |
16137 | 7 |
*) |
8 |
||
9 |
type bstring = string; (*names to be bound / internalized*) |
|
10 |
type xstring = string; (*externalized names / to be printed*) |
|
5012 | 11 |
|
16137 | 12 |
signature BASIC_NAME_SPACE = |
13 |
sig |
|
14 |
val long_names: bool ref |
|
15 |
val short_names: bool ref |
|
16 |
val unique_names: bool ref |
|
17 |
end; |
|
5012 | 18 |
|
19 |
signature NAME_SPACE = |
|
20 |
sig |
|
16137 | 21 |
include BASIC_NAME_SPACE |
9120 | 22 |
val hidden: string -> string |
16137 | 23 |
val separator: string (*single char*) |
24 |
val is_qualified: string -> bool |
|
25 |
val pack: string list -> string |
|
5012 | 26 |
val unpack: string -> string list |
16137 | 27 |
val append: string -> string -> string |
28 |
val qualified: string -> string -> string |
|
5012 | 29 |
val base: string -> string |
16137 | 30 |
val drop_base: string -> string |
11829 | 31 |
val map_base: (string -> string) -> string -> string |
16137 | 32 |
val suffixes_prefixes: 'a list -> 'a list list |
19015 | 33 |
val suffixes_prefixes_split: int -> int -> 'a list -> 'a list list |
5012 | 34 |
val accesses: string -> string list |
13332 | 35 |
val accesses': string -> string list |
5012 | 36 |
type T |
37 |
val empty: T |
|
16137 | 38 |
val valid_accesses: T -> string -> xstring list |
39 |
val intern: T -> xstring -> string |
|
40 |
val extern: T -> string -> xstring |
|
41 |
val hide: bool -> string -> T -> T |
|
5012 | 42 |
val merge: T * T -> T |
16137 | 43 |
type naming |
44 |
val path_of: naming -> string |
|
45 |
val full: naming -> bstring -> string |
|
46 |
val declare: naming -> string -> T -> T |
|
47 |
val default_naming: naming |
|
48 |
val add_path: string -> naming -> naming |
|
19015 | 49 |
val no_base_names: naming -> naming |
16137 | 50 |
val qualified_names: naming -> naming |
19055 | 51 |
val sticky_prefix: string -> naming -> naming |
16137 | 52 |
val set_policy: (string -> bstring -> string) * (string list -> string list list) -> |
53 |
naming -> naming |
|
17756 | 54 |
type 'a table (*= T * 'a Symtab.table*) |
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
55 |
val empty_table: 'a table |
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
56 |
val extend_table: naming -> 'a table * (bstring * 'a) list -> 'a table |
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
57 |
val merge_tables: ('a * 'a -> bool) -> 'a table * 'a table -> 'a table |
16848 | 58 |
val dest_table: 'a table -> (string * 'a) list |
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
59 |
val extern_table: 'a table -> (xstring * 'a) list |
5012 | 60 |
end; |
61 |
||
62 |
structure NameSpace: NAME_SPACE = |
|
63 |
struct |
|
64 |
||
65 |
(** long identifiers **) |
|
66 |
||
16137 | 67 |
fun hidden name = "??." ^ name; |
68 |
val is_hidden = String.isPrefix "??." |
|
69 |
||
5012 | 70 |
val separator = "."; |
19305 | 71 |
val is_qualified = exists_string (fn s => s = separator); |
5012 | 72 |
|
16137 | 73 |
val pack = space_implode separator; |
5012 | 74 |
val unpack = space_explode separator; |
75 |
||
16137 | 76 |
fun append name1 "" = name1 |
77 |
| append "" name2 = name2 |
|
78 |
| append name1 name2 = name1 ^ separator ^ name2; |
|
79 |
||
80 |
fun qualified path name = |
|
81 |
if path = "" orelse name = "" then name |
|
82 |
else path ^ separator ^ name; |
|
11829 | 83 |
|
16137 | 84 |
fun base "" = "" |
85 |
| base name = List.last (unpack name); |
|
11829 | 86 |
|
16137 | 87 |
fun drop_base "" = "" |
88 |
| drop_base name = pack (#1 (split_last (unpack name))); |
|
5012 | 89 |
|
16137 | 90 |
fun map_base _ "" = "" |
91 |
| map_base f name = |
|
92 |
let val names = unpack name |
|
18011
685d95c793ff
cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents:
17756
diff
changeset
|
93 |
in pack (nth_map (length names - 1) f names) end; |
16137 | 94 |
|
19015 | 95 |
|
96 |
(* accesses *) |
|
97 |
||
98 |
infixr 6 @@; |
|
99 |
fun ([] @@ yss) = [] |
|
100 |
| ((xs :: xss) @@ yss) = map (fn ys => xs @ ys) yss @ (xss @@ yss); |
|
101 |
||
102 |
fun suffixes_prefixes list = |
|
5012 | 103 |
let |
19015 | 104 |
val (xs, ws) = chop (length list - 1) list; |
105 |
val sfxs = suffixes xs @@ [ws]; |
|
106 |
val pfxs = prefixes1 xs @@ [ws]; |
|
107 |
in sfxs @ pfxs end; |
|
108 |
||
109 |
fun suffixes_prefixes_split i k list = |
|
110 |
let |
|
111 |
val (((xs, ys), zs), ws) = list |> chop i ||>> chop k ||>> chop (length list - (i + k + 1)); |
|
112 |
val sfxs = |
|
113 |
[ys] @@ suffixes zs @@ [ws] @ |
|
114 |
suffixes1 xs @@ [ys @ zs @ ws]; |
|
115 |
val pfxs = |
|
116 |
prefixes1 xs @@ [ys @ ws] @ |
|
117 |
[xs @ ys] @@ prefixes1 zs @@ [ws]; |
|
16137 | 118 |
in sfxs @ pfxs end; |
5012 | 119 |
|
16137 | 120 |
val accesses = map pack o suffixes_prefixes o unpack; |
13332 | 121 |
val accesses' = map pack o Library.suffixes1 o unpack; |
122 |
||
5012 | 123 |
|
124 |
||
125 |
(** name spaces **) |
|
126 |
||
127 |
(* datatype T *) |
|
128 |
||
129 |
datatype T = |
|
19030
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
130 |
NameSpace of ((string list * string list) * stamp) Symtab.table; |
5012 | 131 |
|
132 |
val empty = NameSpace Symtab.empty; |
|
133 |
||
16137 | 134 |
fun lookup (NameSpace tab) xname = |
17412 | 135 |
(case Symtab.lookup tab xname of |
16137 | 136 |
NONE => (xname, true) |
19030
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
137 |
| SOME (([], []), _) => (xname, true) |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
138 |
| SOME (([name], _), _) => (name, true) |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
139 |
| SOME ((name :: _, _), _) => (name, false) |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
140 |
| SOME (([], name' :: _), _) => (hidden name', true)); |
8728 | 141 |
|
19030
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
142 |
fun valid_accesses (NameSpace tab) name = Symtab.fold (fn (xname, ((names, _), _)) => |
16444 | 143 |
if not (null names) andalso hd names = name then cons xname else I) tab []; |
8728 | 144 |
|
145 |
||
16137 | 146 |
(* intern and extern *) |
147 |
||
148 |
fun intern space xname = #1 (lookup space xname); |
|
149 |
||
150 |
val long_names = ref false; |
|
151 |
val short_names = ref false; |
|
152 |
val unique_names = ref true; |
|
153 |
||
154 |
fun extern space name = |
|
155 |
let |
|
156 |
fun valid unique xname = |
|
157 |
let val (name', uniq) = lookup space xname |
|
158 |
in name = name' andalso (uniq orelse (not unique)) end; |
|
8728 | 159 |
|
16137 | 160 |
fun ex [] = if valid false name then name else hidden name |
161 |
| ex (nm :: nms) = if valid (! unique_names) nm then nm else ex nms; |
|
162 |
in |
|
163 |
if ! long_names then name |
|
164 |
else if ! short_names then base name |
|
165 |
else ex (accesses' name) |
|
166 |
end; |
|
167 |
||
5012 | 168 |
|
16137 | 169 |
(* basic operations *) |
170 |
||
171 |
fun map_space f xname (NameSpace tab) = |
|
19030
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
172 |
let val entry' = |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
173 |
(case Symtab.lookup tab xname of |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
174 |
NONE => f ([], []) |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
175 |
| SOME (entry, _) => f entry) |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
176 |
in NameSpace (Symtab.update (xname, (entry', stamp ())) tab) end; |
16137 | 177 |
|
178 |
fun del (name: string) = remove (op =) name; |
|
179 |
fun add name names = name :: del name names; |
|
180 |
||
181 |
val del_name = map_space o apfst o del; |
|
182 |
val add_name = map_space o apfst o add; |
|
183 |
val add_name' = map_space o apsnd o add; |
|
8728 | 184 |
|
185 |
||
186 |
(* hide *) |
|
5012 | 187 |
|
16137 | 188 |
fun hide fully name space = |
8728 | 189 |
if not (is_qualified name) then |
190 |
error ("Attempt to hide global name " ^ quote name) |
|
191 |
else if is_hidden name then |
|
192 |
error ("Attempt to hide hidden name " ^ quote name) |
|
16137 | 193 |
else |
194 |
let val names = valid_accesses space name in |
|
195 |
space |
|
196 |
|> add_name' name name |
|
197 |
|> fold (del_name name) (if fully then names else names inter_string [base name]) |
|
198 |
end; |
|
5012 | 199 |
|
200 |
||
16137 | 201 |
(* merge *) |
5012 | 202 |
|
19030
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
203 |
fun merge (NameSpace tab1, NameSpace tab2) = |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
204 |
NameSpace ((tab1, tab2) |> Symtab.join |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
205 |
(K (fn (((names1, names1'), stamp1), ((names2, names2'), stamp2)) => |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
206 |
if stamp1 = stamp2 then raise Symtab.SAME |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
207 |
else |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
208 |
((Library.merge (op =) (names2, names1), |
78d43fe9ac65
low-level tuning of merge: maintain identity of accesses;
wenzelm
parents:
19015
diff
changeset
|
209 |
Library.merge (op =) (names2', names1')), stamp ())))); |
6845 | 210 |
|
5012 | 211 |
|
16137 | 212 |
|
213 |
(** naming contexts **) |
|
214 |
||
215 |
(* datatype naming *) |
|
216 |
||
217 |
datatype naming = Naming of |
|
218 |
string * ((string -> string -> string) * (string list -> string list list)); |
|
219 |
||
220 |
fun path_of (Naming (path, _)) = path; |
|
221 |
||
222 |
||
223 |
(* declarations *) |
|
224 |
||
225 |
fun full (Naming (path, (qualify, _))) = qualify path; |
|
226 |
||
227 |
fun declare (Naming (_, (_, accs))) name space = |
|
228 |
if is_hidden name then |
|
229 |
error ("Attempt to declare hidden name " ^ quote name) |
|
230 |
else |
|
231 |
let val names = unpack name in |
|
19305 | 232 |
conditional (null names orelse exists (fn s => s = "") names) (fn () => |
16137 | 233 |
error ("Bad name declaration " ^ quote name)); |
234 |
fold (add_name name) (map pack (accs names)) space |
|
235 |
end; |
|
236 |
||
237 |
||
238 |
(* manipulate namings *) |
|
239 |
||
240 |
fun reject_qualified name = |
|
241 |
if is_qualified name then |
|
242 |
error ("Attempt to declare qualified name " ^ quote name) |
|
243 |
else name; |
|
244 |
||
245 |
val default_naming = |
|
246 |
Naming ("", (fn path => qualified path o reject_qualified, suffixes_prefixes)); |
|
247 |
||
248 |
fun add_path elems (Naming (path, policy)) = |
|
249 |
if elems = "//" then Naming ("", (qualified, #2 policy)) |
|
250 |
else if elems = "/" then Naming ("", policy) |
|
251 |
else if elems = ".." then Naming (drop_base path, policy) |
|
252 |
else Naming (append path elems, policy); |
|
253 |
||
254 |
fun no_base_names (Naming (path, (qualify, accs))) = |
|
255 |
Naming (path, (qualify, filter_out (fn [_] => true | _ => false) o accs)); |
|
256 |
||
19015 | 257 |
fun qualified_names (Naming (path, (_, accs))) = Naming (path, (qualified, accs)); |
258 |
||
19055 | 259 |
fun sticky_prefix prfx (Naming (path, (qualify, _))) = |
260 |
Naming (append path prfx, |
|
261 |
(qualify, suffixes_prefixes_split (length (unpack path)) (length (unpack prfx)))); |
|
19015 | 262 |
|
16137 | 263 |
fun set_policy policy (Naming (path, _)) = Naming (path, policy); |
264 |
||
265 |
||
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
266 |
|
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
267 |
(** name spaces coupled with symbol tables **) |
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
268 |
|
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
269 |
type 'a table = T * 'a Symtab.table; |
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
270 |
|
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
271 |
val empty_table = (empty, Symtab.empty); |
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
272 |
|
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
273 |
fun extend_table naming ((space, tab), bentries) = |
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
274 |
let val entries = map (apfst (full naming)) bentries |
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
275 |
in (fold (declare naming o #1) entries space, Symtab.extend (tab, entries)) end; |
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
276 |
|
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
277 |
fun merge_tables eq ((space1, tab1), (space2, tab2)) = |
19367 | 278 |
(merge (space1, space2), Symtab.merge eq (tab1, tab2)); |
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
279 |
|
16848 | 280 |
fun ext_table (space, tab) = |
281 |
Symtab.fold (fn (name, x) => cons ((name, extern space name), x)) tab [] |
|
282 |
|> Library.sort_wrt (#2 o #1); |
|
283 |
||
284 |
fun dest_table tab = map (apfst #1) (ext_table tab); |
|
285 |
fun extern_table tab = map (apfst #2) (ext_table tab); |
|
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
286 |
|
5012 | 287 |
end; |
5175 | 288 |
|
16137 | 289 |
structure BasicNameSpace: BASIC_NAME_SPACE = NameSpace; |
290 |
open BasicNameSpace; |