author | wenzelm |
Thu, 05 Mar 2009 12:08:00 +0100 | |
changeset 30280 | eb98b49ef835 |
parent 30277 | 4f2b6ccce047 |
child 30359 | 3f9b3ff851ca |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/name_space.ML |
5012 | 2 |
Author: Markus Wenzel, TU Muenchen |
3 |
||
16137 | 4 |
Generic name spaces with declared and hidden entries. Unknown names |
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
5 |
are considered global; no support for absolute addressing. |
16137 | 6 |
*) |
7 |
||
26440 | 8 |
type xstring = string; (*external names*) |
5012 | 9 |
|
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
10 |
signature BASIC_NAME_SPACE = |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
11 |
sig |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
12 |
val long_names: bool ref |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
13 |
val short_names: bool ref |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
14 |
val unique_names: bool ref |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
15 |
end; |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
16 |
|
5012 | 17 |
signature NAME_SPACE = |
18 |
sig |
|
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
19 |
include BASIC_NAME_SPACE |
9120 | 20 |
val hidden: string -> string |
25225 | 21 |
val is_hidden: string -> bool |
16137 | 22 |
val separator: string (*single char*) |
23 |
val is_qualified: string -> bool |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
24 |
val implode: string list -> string |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
25 |
val explode: string -> string list |
16137 | 26 |
val append: string -> string -> string |
27 |
val qualified: string -> string -> string |
|
30280 | 28 |
val base_name: string -> string |
20530
448594cbd82b
renamed NameSpace.drop_base to NameSpace.qualifier;
wenzelm
parents:
19367
diff
changeset
|
29 |
val qualifier: string -> string |
30280 | 30 |
val map_base_name: (string -> string) -> string -> string |
5012 | 31 |
type T |
32 |
val empty: T |
|
16137 | 33 |
val intern: T -> xstring -> string |
34 |
val extern: T -> string -> xstring |
|
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
35 |
val extern_flags: {long_names: bool, short_names: bool, unique_names: bool} -> |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
36 |
T -> string -> xstring |
16137 | 37 |
val hide: bool -> string -> T -> T |
5012 | 38 |
val merge: T * T -> T |
16137 | 39 |
type naming |
28965 | 40 |
val default_naming: naming |
29581 | 41 |
val declare: naming -> binding -> T -> string * T |
42 |
val full_name: naming -> binding -> string |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
43 |
val external_names: naming -> string -> string list |
28965 | 44 |
val path_of: naming -> string |
16137 | 45 |
val add_path: string -> naming -> naming |
19015 | 46 |
val no_base_names: naming -> naming |
16137 | 47 |
val qualified_names: naming -> naming |
19055 | 48 |
val sticky_prefix: string -> naming -> naming |
24361 | 49 |
type 'a table = T * 'a Symtab.table |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
50 |
val bind: naming -> binding * 'a -> 'a table -> string * 'a table (*exception Symtab.DUP*) |
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
51 |
val empty_table: 'a table |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
52 |
val merge_tables: ('a * 'a -> bool) -> 'a table * 'a table -> 'a table (*exception Symtab.DUP*) |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
53 |
val join_tables: (string -> 'a * 'a -> 'a) (*exception Symtab.DUP/Symtab.SAME*) -> |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
54 |
'a table * 'a table -> 'a table (*exception Symtab.DUP*) |
16848 | 55 |
val dest_table: 'a table -> (string * 'a) list |
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
56 |
val extern_table: 'a table -> (xstring * 'a) list |
5012 | 57 |
end; |
58 |
||
59 |
structure NameSpace: NAME_SPACE = |
|
60 |
struct |
|
61 |
||
62 |
(** long identifiers **) |
|
63 |
||
16137 | 64 |
fun hidden name = "??." ^ name; |
29338 | 65 |
val is_hidden = String.isPrefix "??."; |
16137 | 66 |
|
30222
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30215
diff
changeset
|
67 |
val separator = "."; |
4102bbf2af21
moved type bstring from name_space.ML to binding.ML -- it is the primitive concept behind bindings;
wenzelm
parents:
30215
diff
changeset
|
68 |
val is_qualified = exists_string (fn s => s = separator); |
5012 | 69 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
70 |
val implode_name = space_implode separator; |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
71 |
val explode_name = space_explode separator; |
5012 | 72 |
|
16137 | 73 |
fun append name1 "" = name1 |
74 |
| append "" name2 = name2 |
|
75 |
| append name1 name2 = name1 ^ separator ^ name2; |
|
76 |
||
77 |
fun qualified path name = |
|
78 |
if path = "" orelse name = "" then name |
|
79 |
else path ^ separator ^ name; |
|
11829 | 80 |
|
30280 | 81 |
fun base_name "" = "" |
82 |
| base_name name = List.last (explode_name name); |
|
11829 | 83 |
|
20530
448594cbd82b
renamed NameSpace.drop_base to NameSpace.qualifier;
wenzelm
parents:
19367
diff
changeset
|
84 |
fun qualifier "" = "" |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
85 |
| qualifier name = implode_name (#1 (split_last (explode_name name))); |
5012 | 86 |
|
30280 | 87 |
fun map_base_name _ "" = "" |
88 |
| map_base_name f name = |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
89 |
let val names = explode_name name |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
90 |
in implode_name (nth_map (length names - 1) f names) end; |
16137 | 91 |
|
19015 | 92 |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
93 |
(* standard accesses *) |
19015 | 94 |
|
95 |
infixr 6 @@; |
|
96 |
fun ([] @@ yss) = [] |
|
97 |
| ((xs :: xss) @@ yss) = map (fn ys => xs @ ys) yss @ (xss @@ yss); |
|
98 |
||
99 |
fun suffixes_prefixes list = |
|
5012 | 100 |
let |
19015 | 101 |
val (xs, ws) = chop (length list - 1) list; |
102 |
val sfxs = suffixes xs @@ [ws]; |
|
103 |
val pfxs = prefixes1 xs @@ [ws]; |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
104 |
in (sfxs @ pfxs, sfxs) end; |
19015 | 105 |
|
106 |
fun suffixes_prefixes_split i k list = |
|
107 |
let |
|
108 |
val (((xs, ys), zs), ws) = list |> chop i ||>> chop k ||>> chop (length list - (i + k + 1)); |
|
109 |
val sfxs = |
|
110 |
[ys] @@ suffixes zs @@ [ws] @ |
|
111 |
suffixes1 xs @@ [ys @ zs @ ws]; |
|
112 |
val pfxs = |
|
113 |
prefixes1 xs @@ [ys @ ws] @ |
|
114 |
[xs @ ys] @@ prefixes1 zs @@ [ws]; |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
115 |
in (sfxs @ pfxs, sfxs) end; |
13332 | 116 |
|
5012 | 117 |
|
118 |
||
119 |
(** name spaces **) |
|
120 |
||
121 |
(* datatype T *) |
|
122 |
||
123 |
datatype T = |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
124 |
NameSpace of |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
125 |
(string list * string list) Symtab.table * (*internals, hidden internals*) |
30277 | 126 |
xstring list Symtab.table; (*externals*) |
5012 | 127 |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
128 |
val empty = NameSpace (Symtab.empty, Symtab.empty); |
5012 | 129 |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
130 |
fun lookup (NameSpace (tab, _)) xname = |
17412 | 131 |
(case Symtab.lookup tab xname of |
16137 | 132 |
NONE => (xname, true) |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
133 |
| SOME ([], []) => (xname, true) |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
134 |
| SOME ([name], _) => (name, true) |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
135 |
| SOME (name :: _, _) => (name, false) |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
136 |
| SOME ([], name' :: _) => (hidden name', true)); |
8728 | 137 |
|
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
138 |
fun get_accesses (NameSpace (_, xtab)) name = |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
139 |
(case Symtab.lookup xtab name of |
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
140 |
NONE => [name] |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
141 |
| SOME xnames => xnames); |
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
142 |
|
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
143 |
fun put_accesses name xnames (NameSpace (tab, xtab)) = |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
144 |
NameSpace (tab, Symtab.update (name, xnames) xtab); |
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
145 |
|
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
146 |
fun valid_accesses (NameSpace (tab, _)) name = Symtab.fold (fn (xname, (names, _)) => |
16444 | 147 |
if not (null names) andalso hd names = name then cons xname else I) tab []; |
8728 | 148 |
|
149 |
||
16137 | 150 |
(* intern and extern *) |
151 |
||
152 |
fun intern space xname = #1 (lookup space xname); |
|
153 |
||
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
154 |
fun extern_flags {long_names, short_names, unique_names} space name = |
16137 | 155 |
let |
30277 | 156 |
fun valid require_unique xname = |
157 |
let val (name', is_unique) = lookup space xname |
|
158 |
in name = name' andalso (not require_unique orelse is_unique) end; |
|
8728 | 159 |
|
26440 | 160 |
fun ext [] = if valid false name then name else hidden name |
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
161 |
| ext (nm :: nms) = if valid unique_names nm then nm else ext nms; |
16137 | 162 |
in |
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
163 |
if long_names then name |
30280 | 164 |
else if short_names then base_name name |
30213
3951aab916fd
reverted change introduced in a7c164e228e1 -- there cannot be a "bug" in a perfectly normal operation on the internal data representation that merely escaped into public by accident (cf. 0a981c596372);
wenzelm
parents:
29848
diff
changeset
|
165 |
else ext (get_accesses space name) |
16137 | 166 |
end; |
167 |
||
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
168 |
val long_names = ref false; |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
169 |
val short_names = ref false; |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
170 |
val unique_names = ref true; |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
171 |
|
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
172 |
fun extern space name = |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
173 |
extern_flags |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
174 |
{long_names = ! long_names, |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
175 |
short_names = ! short_names, |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
176 |
unique_names = ! unique_names} space name; |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
177 |
|
5012 | 178 |
|
16137 | 179 |
(* basic operations *) |
180 |
||
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
181 |
local |
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
182 |
|
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
183 |
fun map_space f xname (NameSpace (tab, xtab)) = |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
184 |
NameSpace (Symtab.map_default (xname, ([], [])) f tab, xtab); |
16137 | 185 |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
186 |
in |
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
187 |
|
26440 | 188 |
val del_name = map_space o apfst o remove (op =); |
27196
ef2f01da7a12
hide: delete all accesses from extra names -- reduces ambiguity in extern;
wenzelm
parents:
26657
diff
changeset
|
189 |
fun del_name_extra name = map_space (apfst (fn [] => [] | x :: xs => x :: remove (op =) name xs)); |
26440 | 190 |
val add_name = map_space o apfst o update (op =); |
191 |
val add_name' = map_space o apsnd o update (op =); |
|
8728 | 192 |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
193 |
end; |
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
194 |
|
8728 | 195 |
|
196 |
(* hide *) |
|
5012 | 197 |
|
16137 | 198 |
fun hide fully name space = |
8728 | 199 |
if not (is_qualified name) then |
200 |
error ("Attempt to hide global name " ^ quote name) |
|
201 |
else if is_hidden name then |
|
202 |
error ("Attempt to hide hidden name " ^ quote name) |
|
16137 | 203 |
else |
204 |
let val names = valid_accesses space name in |
|
205 |
space |
|
206 |
|> add_name' name name |
|
30280 | 207 |
|> fold (del_name name) (if fully then names else names inter_string [base_name name]) |
30213
3951aab916fd
reverted change introduced in a7c164e228e1 -- there cannot be a "bug" in a perfectly normal operation on the internal data representation that merely escaped into public by accident (cf. 0a981c596372);
wenzelm
parents:
29848
diff
changeset
|
208 |
|> fold (del_name_extra name) (get_accesses space name) |
16137 | 209 |
end; |
5012 | 210 |
|
211 |
||
16137 | 212 |
(* merge *) |
5012 | 213 |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
214 |
fun merge (NameSpace (tab1, xtab1), NameSpace (tab2, xtab2)) = |
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
215 |
let |
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
216 |
val tab' = (tab1, tab2) |> Symtab.join |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
217 |
(K (fn names as ((names1, names1'), (names2, names2')) => |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
218 |
if pointer_eq names then raise Symtab.SAME |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
219 |
else (Library.merge (op =) (names1, names2), Library.merge (op =) (names1', names2')))); |
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
220 |
val xtab' = (xtab1, xtab2) |> Symtab.join |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
221 |
(K (fn xnames => |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
222 |
if pointer_eq xnames then raise Symtab.SAME |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
223 |
else (Library.merge (op =) xnames))); |
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
224 |
in NameSpace (tab', xtab') end; |
5012 | 225 |
|
16137 | 226 |
|
26440 | 227 |
|
16137 | 228 |
(** naming contexts **) |
229 |
||
230 |
(* datatype naming *) |
|
231 |
||
232 |
datatype naming = Naming of |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
233 |
string * (*path*) |
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
234 |
((string -> string -> string) * (*qualify*) |
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
235 |
(string list -> string list list * string list list)); (*accesses*) |
16137 | 236 |
|
237 |
fun path_of (Naming (path, _)) = path; |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
238 |
fun accesses (Naming (_, (_, accs))) = accs; |
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
239 |
|
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
240 |
fun external_names naming = map implode_name o #2 o accesses naming o explode_name; |
16137 | 241 |
|
242 |
||
243 |
(* manipulate namings *) |
|
244 |
||
245 |
fun reject_qualified name = |
|
246 |
if is_qualified name then |
|
247 |
error ("Attempt to declare qualified name " ^ quote name) |
|
248 |
else name; |
|
249 |
||
250 |
val default_naming = |
|
251 |
Naming ("", (fn path => qualified path o reject_qualified, suffixes_prefixes)); |
|
252 |
||
253 |
fun add_path elems (Naming (path, policy)) = |
|
254 |
if elems = "//" then Naming ("", (qualified, #2 policy)) |
|
255 |
else if elems = "/" then Naming ("", policy) |
|
20530
448594cbd82b
renamed NameSpace.drop_base to NameSpace.qualifier;
wenzelm
parents:
19367
diff
changeset
|
256 |
else if elems = ".." then Naming (qualifier path, policy) |
16137 | 257 |
else Naming (append path elems, policy); |
258 |
||
259 |
fun no_base_names (Naming (path, (qualify, accs))) = |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
260 |
Naming (path, (qualify, pairself (filter_out (fn [_] => true | _ => false)) o accs)); |
16137 | 261 |
|
19015 | 262 |
fun qualified_names (Naming (path, (_, accs))) = Naming (path, (qualified, accs)); |
263 |
||
19055 | 264 |
fun sticky_prefix prfx (Naming (path, (qualify, _))) = |
265 |
Naming (append path prfx, |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
266 |
(qualify, suffixes_prefixes_split (length (explode_name path)) (length (explode_name prfx)))); |
19015 | 267 |
|
28860 | 268 |
val apply_prefix = |
269 |
let |
|
270 |
fun mk_prefix (prfx, true) = sticky_prefix prfx |
|
271 |
| mk_prefix (prfx, false) = add_path prfx; |
|
272 |
in fold mk_prefix end; |
|
273 |
||
274 |
||
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
275 |
(* full name *) |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
276 |
|
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
277 |
fun full (Naming (path, (qualify, _))) = qualify path; |
28860 | 278 |
|
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
279 |
fun full_name naming binding = |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
280 |
let |
30277 | 281 |
val (prfx, bname) = Binding.dest binding; |
282 |
val naming' = apply_prefix prfx naming; |
|
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
283 |
in full naming' bname end; |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
284 |
|
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
285 |
|
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
286 |
(* declaration *) |
28860 | 287 |
|
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
288 |
fun declare naming binding space = |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
289 |
let |
30277 | 290 |
val (prfx, bname) = Binding.dest binding; |
291 |
val naming' = apply_prefix prfx naming; |
|
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
292 |
val name = full naming' bname; |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
293 |
val names = explode_name name; |
28860 | 294 |
|
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
295 |
val _ = (null names orelse exists (fn s => s = "" orelse s = "??") names |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
296 |
orelse exists_string (fn s => s = "\"") name) andalso |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
297 |
error ("Bad name declaration " ^ quote (Binding.str_of binding)); |
28991 | 298 |
|
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
299 |
val (accs, accs') = pairself (map implode_name) (accesses naming' names); |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
300 |
val space' = space |> fold (add_name name) accs |> put_accesses name accs'; |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
301 |
in (name, space') end; |
28860 | 302 |
|
16137 | 303 |
|
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
304 |
|
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
305 |
(** name spaces coupled with symbol tables **) |
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
306 |
|
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
307 |
type 'a table = T * 'a Symtab.table; |
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
308 |
|
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
309 |
fun bind naming (binding, x) (space, tab) = |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
310 |
let val (name, space') = declare naming binding space |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
311 |
in (name, (space', Symtab.update_new (name, x) tab)) end; |
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
312 |
|
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
313 |
val empty_table = (empty, Symtab.empty); |
28860 | 314 |
|
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
315 |
fun merge_tables eq ((space1, tab1), (space2, tab2)) = |
19367 | 316 |
(merge (space1, space2), Symtab.merge eq (tab1, tab2)); |
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
317 |
|
28991 | 318 |
fun join_tables f ((space1, tab1), (space2, tab2)) = |
319 |
(merge (space1, space2), Symtab.join f (tab1, tab2)); |
|
320 |
||
16848 | 321 |
fun ext_table (space, tab) = |
322 |
Symtab.fold (fn (name, x) => cons ((name, extern space name), x)) tab [] |
|
323 |
|> Library.sort_wrt (#2 o #1); |
|
324 |
||
325 |
fun dest_table tab = map (apfst #1) (ext_table tab); |
|
326 |
fun extern_table tab = map (apfst #2) (ext_table tab); |
|
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
327 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
328 |
|
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
329 |
(*final declarations of this structure!*) |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
330 |
val implode = implode_name; |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
331 |
val explode = explode_name; |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
20530
diff
changeset
|
332 |
|
5012 | 333 |
end; |
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
334 |
|
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
335 |
structure BasicNameSpace: BASIC_NAME_SPACE = NameSpace; |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
336 |
open BasicNameSpace; |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
337 |