author | blanchet |
Thu, 16 Dec 2010 22:45:02 +0100 | |
changeset 41220 | 4d11b0de7dd8 |
parent 35679 | da87ffdcf7ea |
child 41254 | 78c3e472bb35 |
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 |
32738 | 12 |
val long_names: bool Unsynchronized.ref |
13 |
val short_names: bool Unsynchronized.ref |
|
14 |
val unique_names: bool Unsynchronized.ref |
|
30215
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 |
5012 | 22 |
type T |
33096 | 23 |
val empty: string -> T |
24 |
val kind_of: T -> string |
|
33164
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
25 |
val the_entry: T -> string -> |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
26 |
{concealed: bool, group: serial option, theory_name: string, pos: Position.T, id: serial} |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
27 |
val is_concealed: T -> string -> bool |
16137 | 28 |
val intern: T -> xstring -> string |
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
29 |
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
|
30 |
T -> string -> xstring |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
31 |
val extern: T -> string -> xstring |
16137 | 32 |
val hide: bool -> string -> T -> T |
5012 | 33 |
val merge: T * T -> T |
16137 | 34 |
type naming |
28965 | 35 |
val default_naming: naming |
33164
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
36 |
val conceal: naming -> naming |
33724 | 37 |
val get_group: naming -> serial option |
38 |
val set_group: serial option -> naming -> naming |
|
33164
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
39 |
val set_theory_name: string -> naming -> naming |
33724 | 40 |
val new_group: naming -> naming |
41 |
val reset_group: naming -> naming |
|
16137 | 42 |
val add_path: string -> naming -> naming |
30418
b5044aca0729
add_path: discontinued special meaning of "//", "/", "..";
wenzelm
parents:
30412
diff
changeset
|
43 |
val root_path: naming -> naming |
b5044aca0729
add_path: discontinued special meaning of "//", "/", "..";
wenzelm
parents:
30412
diff
changeset
|
44 |
val parent_path: naming -> naming |
30469 | 45 |
val mandatory_path: string -> naming -> naming |
35200
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
wenzelm
parents:
33724
diff
changeset
|
46 |
val qualified_path: bool -> binding -> naming -> naming |
33281
223ef9bc399a
let naming transform binding beforehand -- covering only the "conceal" flag for now;
wenzelm
parents:
33164
diff
changeset
|
47 |
val transform_binding: naming -> binding -> binding |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
48 |
val full_name: naming -> binding -> string |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
49 |
val declare: bool -> naming -> binding -> T -> string * T |
35679
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
50 |
val alias: naming -> binding -> string -> T -> T |
24361 | 51 |
type 'a table = T * 'a Symtab.table |
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
52 |
val define: bool -> naming -> binding * 'a -> 'a table -> string * 'a table |
33096 | 53 |
val empty_table: string -> 'a table |
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
54 |
val merge_tables: 'a table * 'a table -> 'a table |
33097
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
33096
diff
changeset
|
55 |
val join_tables: (string -> 'a * 'a -> 'a) (*Symtab.SAME*) -> |
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
33096
diff
changeset
|
56 |
'a table * 'a table -> 'a table |
16848 | 57 |
val dest_table: 'a table -> (string * 'a) list |
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
58 |
val extern_table: 'a table -> (xstring * 'a) list |
5012 | 59 |
end; |
60 |
||
33095
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents:
33091
diff
changeset
|
61 |
structure Name_Space: NAME_SPACE = |
5012 | 62 |
struct |
63 |
||
30412
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
64 |
|
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
65 |
(** name spaces **) |
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
66 |
|
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
67 |
(* hidden entries *) |
5012 | 68 |
|
16137 | 69 |
fun hidden name = "??." ^ name; |
29338 | 70 |
val is_hidden = String.isPrefix "??."; |
16137 | 71 |
|
5012 | 72 |
|
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
73 |
(* datatype entry *) |
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
74 |
|
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
75 |
type entry = |
35679
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
76 |
{concealed: bool, |
33164
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
77 |
group: serial option, |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
78 |
theory_name: string, |
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
79 |
pos: Position.T, |
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
80 |
id: serial}; |
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
81 |
|
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
82 |
fun str_of_entry def (name, {pos, id, ...}: entry) = |
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
83 |
let |
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
84 |
val occurrence = (if def then Markup.defN else Markup.refN, string_of_int id); |
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
85 |
val props = occurrence :: Position.properties_of pos; |
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
86 |
in Markup.markup (Markup.properties props (Markup.entity name)) name end; |
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
87 |
|
33096 | 88 |
fun err_dup kind entry1 entry2 = |
89 |
error ("Duplicate " ^ kind ^ " declaration " ^ |
|
90 |
quote (str_of_entry true entry1) ^ " vs. " ^ quote (str_of_entry true entry2)); |
|
91 |
||
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
92 |
|
5012 | 93 |
(* datatype T *) |
94 |
||
95 |
datatype T = |
|
33095
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents:
33091
diff
changeset
|
96 |
Name_Space of |
33096 | 97 |
{kind: string, |
98 |
internals: (string list * string list) Symtab.table, (*visible, hidden*) |
|
35679
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
99 |
entries: (xstring list * entry) Symtab.table}; (*externals, entry*) |
33096 | 100 |
|
101 |
fun make_name_space (kind, internals, entries) = |
|
102 |
Name_Space {kind = kind, internals = internals, entries = entries}; |
|
103 |
||
104 |
fun map_name_space f (Name_Space {kind = kind, internals = internals, entries = entries}) = |
|
105 |
make_name_space (f (kind, internals, entries)); |
|
106 |
||
107 |
fun map_internals f xname = map_name_space (fn (kind, internals, entries) => |
|
108 |
(kind, Symtab.map_default (xname, ([], [])) f internals, entries)); |
|
109 |
||
5012 | 110 |
|
33096 | 111 |
fun empty kind = make_name_space (kind, Symtab.empty, Symtab.empty); |
112 |
||
113 |
fun kind_of (Name_Space {kind, ...}) = kind; |
|
5012 | 114 |
|
33096 | 115 |
fun the_entry (Name_Space {kind, entries, ...}) name = |
116 |
(case Symtab.lookup entries name of |
|
117 |
NONE => error ("Unknown " ^ kind ^ " " ^ quote name) |
|
35679
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
118 |
| SOME (_, entry) => entry); |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
119 |
|
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
120 |
fun is_concealed space name = #concealed (the_entry space name); |
33096 | 121 |
|
122 |
||
123 |
(* name accesses *) |
|
124 |
||
125 |
fun lookup (Name_Space {internals, ...}) xname = |
|
126 |
(case Symtab.lookup internals xname of |
|
16137 | 127 |
NONE => (xname, true) |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
128 |
| SOME ([], []) => (xname, true) |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
129 |
| SOME ([name], _) => (name, true) |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
130 |
| SOME (name :: _, _) => (name, false) |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
131 |
| SOME ([], name' :: _) => (hidden name', true)); |
8728 | 132 |
|
33096 | 133 |
fun get_accesses (Name_Space {entries, ...}) name = |
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
134 |
(case Symtab.lookup entries name of |
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
135 |
NONE => [name] |
35679
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
136 |
| SOME (externals, _) => externals); |
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
137 |
|
33096 | 138 |
fun valid_accesses (Name_Space {internals, ...}) name = |
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
139 |
Symtab.fold (fn (xname, (names, _)) => |
33096 | 140 |
if not (null names) andalso hd names = name then cons xname else I) internals []; |
8728 | 141 |
|
142 |
||
16137 | 143 |
(* intern and extern *) |
144 |
||
145 |
fun intern space xname = #1 (lookup space xname); |
|
146 |
||
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
147 |
fun extern_flags {long_names, short_names, unique_names} space name = |
16137 | 148 |
let |
30277 | 149 |
fun valid require_unique xname = |
150 |
let val (name', is_unique) = lookup space xname |
|
151 |
in name = name' andalso (not require_unique orelse is_unique) end; |
|
8728 | 152 |
|
26440 | 153 |
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
|
154 |
| ext (nm :: nms) = if valid unique_names nm then nm else ext nms; |
16137 | 155 |
in |
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
156 |
if long_names then name |
30359
3f9b3ff851ca
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
157 |
else if short_names then Long_Name.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
|
158 |
else ext (get_accesses space name) |
16137 | 159 |
end; |
160 |
||
32738 | 161 |
val long_names = Unsynchronized.ref false; |
162 |
val short_names = Unsynchronized.ref false; |
|
163 |
val unique_names = Unsynchronized.ref true; |
|
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
164 |
|
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
165 |
fun extern space name = |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
166 |
extern_flags |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
167 |
{long_names = ! long_names, |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
168 |
short_names = ! short_names, |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
169 |
unique_names = ! unique_names} space name; |
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
170 |
|
5012 | 171 |
|
33096 | 172 |
(* modify internals *) |
16137 | 173 |
|
33096 | 174 |
val del_name = map_internals o apfst o remove (op =); |
175 |
fun del_name_extra name = |
|
176 |
map_internals (apfst (fn [] => [] | x :: xs => x :: remove (op =) name xs)); |
|
177 |
val add_name = map_internals o apfst o update (op =); |
|
178 |
val add_name' = map_internals o apsnd o update (op =); |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
179 |
|
8728 | 180 |
|
181 |
(* hide *) |
|
5012 | 182 |
|
16137 | 183 |
fun hide fully name space = |
30359
3f9b3ff851ca
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
184 |
if not (Long_Name.is_qualified name) then |
8728 | 185 |
error ("Attempt to hide global name " ^ quote name) |
186 |
else if is_hidden name then |
|
187 |
error ("Attempt to hide hidden name " ^ quote name) |
|
16137 | 188 |
else |
189 |
let val names = valid_accesses space name in |
|
190 |
space |
|
191 |
|> add_name' name name |
|
30359
3f9b3ff851ca
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
192 |
|> fold (del_name name) |
33049
c38f02fdf35d
curried inter as canonical list operation (beware of argument order)
haftmann
parents:
33038
diff
changeset
|
193 |
(if fully then names else inter (op =) [Long_Name.base_name name] names) |
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
|
194 |
|> fold (del_name_extra name) (get_accesses space name) |
16137 | 195 |
end; |
5012 | 196 |
|
197 |
||
16137 | 198 |
(* merge *) |
5012 | 199 |
|
33096 | 200 |
fun merge |
201 |
(Name_Space {kind = kind1, internals = internals1, entries = entries1}, |
|
202 |
Name_Space {kind = kind2, internals = internals2, entries = entries2}) = |
|
25072
03f57b516e12
store external accesses within name space (as produced by naming policy);
wenzelm
parents:
24361
diff
changeset
|
203 |
let |
33096 | 204 |
val kind' = |
205 |
if kind1 = kind2 then kind1 |
|
206 |
else error ("Attempt to merge different kinds of name spaces " ^ |
|
207 |
quote kind1 ^ " vs. " ^ quote kind2); |
|
208 |
val internals' = (internals1, internals2) |> Symtab.join |
|
30465 | 209 |
(K (fn ((names1, names1'), (names2, names2')) => |
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
210 |
if pointer_eq (names1, names2) andalso pointer_eq (names1', names2') |
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
211 |
then raise Symtab.SAME |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
212 |
else (Library.merge (op =) (names1, names2), Library.merge (op =) (names1', names2')))); |
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
213 |
val entries' = (entries1, entries2) |> Symtab.join |
35679
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
214 |
(fn name => fn ((_, entry1), (_, entry2)) => |
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
215 |
if #id entry1 = #id entry2 then raise Symtab.SAME |
33096 | 216 |
else err_dup kind' (name, entry1) (name, entry2)); |
217 |
in make_name_space (kind', internals', entries') end; |
|
5012 | 218 |
|
16137 | 219 |
|
26440 | 220 |
|
16137 | 221 |
(** naming contexts **) |
222 |
||
223 |
(* datatype naming *) |
|
224 |
||
33164
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
225 |
datatype naming = Naming of |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
226 |
{conceal: bool, |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
227 |
group: serial option, |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
228 |
theory_name: string, |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
229 |
path: (string * bool) list}; |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
230 |
|
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
231 |
fun make_naming (conceal, group, theory_name, path) = |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
232 |
Naming {conceal = conceal, group = group, theory_name = theory_name, path = path}; |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
233 |
|
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
234 |
fun map_naming f (Naming {conceal, group, theory_name, path}) = |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
235 |
make_naming (f (conceal, group, theory_name, path)); |
16137 | 236 |
|
33164
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
237 |
fun map_path f = map_naming (fn (conceal, group, theory_name, path) => |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
238 |
(conceal, group, theory_name, f path)); |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
239 |
|
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
240 |
|
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
241 |
val default_naming = make_naming (false, NONE, "", []); |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
242 |
|
33164
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
243 |
val conceal = map_naming (fn (_, group, theory_name, path) => |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
244 |
(true, group, theory_name, path)); |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
245 |
|
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
246 |
fun set_theory_name theory_name = map_naming (fn (conceal, group, _, path) => |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
247 |
(conceal, group, theory_name, path)); |
16137 | 248 |
|
33724 | 249 |
|
250 |
fun get_group (Naming {group, ...}) = group; |
|
251 |
||
252 |
fun set_group group = map_naming (fn (conceal, _, theory_name, path) => |
|
253 |
(conceal, group, theory_name, path)); |
|
254 |
||
255 |
fun new_group naming = set_group (SOME (serial ())) naming; |
|
256 |
val reset_group = set_group NONE; |
|
257 |
||
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
258 |
fun add_path elems = map_path (fn path => path @ [(elems, false)]); |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
259 |
val root_path = map_path (fn _ => []); |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
260 |
val parent_path = map_path (perhaps (try (#1 o split_last))); |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
261 |
fun mandatory_path elems = map_path (fn path => path @ [(elems, true)]); |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
262 |
|
35200
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
wenzelm
parents:
33724
diff
changeset
|
263 |
fun qualified_path mandatory binding = map_path (fn path => |
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
wenzelm
parents:
33724
diff
changeset
|
264 |
path @ #2 (Binding.dest (Binding.qualified mandatory "" binding))); |
aaddb2b526d6
more systematic treatment of qualified names derived from binding;
wenzelm
parents:
33724
diff
changeset
|
265 |
|
28860 | 266 |
|
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
267 |
(* full name *) |
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
268 |
|
33281
223ef9bc399a
let naming transform binding beforehand -- covering only the "conceal" flag for now;
wenzelm
parents:
33164
diff
changeset
|
269 |
fun transform_binding (Naming {conceal = true, ...}) = Binding.conceal |
223ef9bc399a
let naming transform binding beforehand -- covering only the "conceal" flag for now;
wenzelm
parents:
33164
diff
changeset
|
270 |
| transform_binding _ = I; |
223ef9bc399a
let naming transform binding beforehand -- covering only the "conceal" flag for now;
wenzelm
parents:
33164
diff
changeset
|
271 |
|
30465 | 272 |
fun err_bad binding = error ("Bad name binding " ^ quote (Binding.str_of binding)); |
28860 | 273 |
|
33281
223ef9bc399a
let naming transform binding beforehand -- covering only the "conceal" flag for now;
wenzelm
parents:
33164
diff
changeset
|
274 |
fun name_spec (naming as Naming {path, ...}) raw_binding = |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
275 |
let |
33281
223ef9bc399a
let naming transform binding beforehand -- covering only the "conceal" flag for now;
wenzelm
parents:
33164
diff
changeset
|
276 |
val binding = transform_binding naming raw_binding; |
223ef9bc399a
let naming transform binding beforehand -- covering only the "conceal" flag for now;
wenzelm
parents:
33164
diff
changeset
|
277 |
val (concealed, prefix, name) = Binding.dest binding; |
30438
c2d49315b93b
eliminated qualified_names naming policy: qualified names are only permitted via explicit Binding.qualify/qualified_name etc. (NB: user-level outer syntax should never do this);
wenzelm
parents:
30418
diff
changeset
|
278 |
val _ = Long_Name.is_qualified name andalso err_bad binding; |
30412
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
279 |
|
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
280 |
val spec1 = maps (fn (a, b) => map (rpair b) (Long_Name.explode a)) (path @ prefix); |
30438
c2d49315b93b
eliminated qualified_names naming policy: qualified names are only permitted via explicit Binding.qualify/qualified_name etc. (NB: user-level outer syntax should never do this);
wenzelm
parents:
30418
diff
changeset
|
281 |
val spec2 = if name = "" then [] else [(name, true)]; |
30412
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
282 |
val spec = spec1 @ spec2; |
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
283 |
val _ = |
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
284 |
exists (fn (a, _) => a = "" orelse a = "??" orelse exists_string (fn s => s = "\"") a) spec |
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
285 |
andalso err_bad binding; |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
286 |
in (concealed, if null spec2 then [] else spec) end; |
30412
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
287 |
|
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
288 |
fun full_name naming = |
33164
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
289 |
name_spec naming #> #2 #> map #1 #> Long_Name.implode; |
30412
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
290 |
|
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
291 |
|
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
292 |
(* accesses *) |
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
293 |
|
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
294 |
fun mandatory xs = map_filter (fn (x, true) => SOME x | _ => NONE) xs; |
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
295 |
|
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
296 |
fun mandatory_prefixes xs = mandatory xs :: mandatory_prefixes1 xs |
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
297 |
and mandatory_prefixes1 [] = [] |
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
298 |
| mandatory_prefixes1 ((x, true) :: xs) = map (cons x) (mandatory_prefixes1 xs) |
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
299 |
| mandatory_prefixes1 ((x, false) :: xs) = map (cons x) (mandatory_prefixes xs); |
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
300 |
|
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
301 |
fun mandatory_suffixes xs = map rev (mandatory_prefixes (rev xs)); |
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
302 |
|
30522 | 303 |
fun accesses naming binding = |
30412
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
304 |
let |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
305 |
val spec = #2 (name_spec naming binding); |
30412
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
306 |
val sfxs = mandatory_suffixes spec; |
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
307 |
val pfxs = mandatory_prefixes spec; |
30522 | 308 |
in pairself (map Long_Name.implode) (sfxs @ pfxs, sfxs) end; |
30412
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
309 |
|
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
310 |
|
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
311 |
(* declaration *) |
28860 | 312 |
|
35679
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
313 |
fun new_entry strict (name, (externals, entry)) = |
33096 | 314 |
map_name_space (fn (kind, internals, entries) => |
315 |
let |
|
316 |
val entries' = |
|
35679
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
317 |
(if strict then Symtab.update_new else Symtab.update) (name, (externals, entry)) entries |
33096 | 318 |
handle Symtab.DUP dup => |
35679
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
319 |
err_dup kind (dup, #2 (the (Symtab.lookup entries dup))) (name, entry); |
33096 | 320 |
in (kind, internals, entries') end); |
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
321 |
|
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
322 |
fun declare strict naming binding space = |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
323 |
let |
33164
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
324 |
val Naming {group, theory_name, ...} = naming; |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
325 |
val (concealed, spec) = name_spec naming binding; |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
326 |
val (accs, accs') = accesses naming binding; |
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
327 |
|
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
328 |
val name = Long_Name.implode (map fst spec); |
30412
7f5b0a020ccd
just one naming policy based on binding content -- eliminated odd "object-oriented" style;
wenzelm
parents:
30359
diff
changeset
|
329 |
val _ = name = "" andalso err_bad binding; |
33157
56f836b9414f
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm
parents:
33097
diff
changeset
|
330 |
|
33096 | 331 |
val entry = |
35679
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
332 |
{concealed = concealed, |
33164
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
333 |
group = group, |
b8fd9b6bba7c
Name_Space.naming: maintain group and theory_name as well;
wenzelm
parents:
33157
diff
changeset
|
334 |
theory_name = theory_name, |
33097
9d501e11084a
maintain position of formal entities via name space;
wenzelm
parents:
33096
diff
changeset
|
335 |
pos = Position.default (Binding.pos_of binding), |
33096 | 336 |
id = serial ()}; |
35679
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
337 |
val space' = space |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
338 |
|> fold (add_name name) accs |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
339 |
|> new_entry strict (name, (accs', entry)); |
30233
6eb726e43ed1
eliminated internal stamp equality, replaced by bare-metal pointer_eq;
wenzelm
parents:
30222
diff
changeset
|
340 |
in (name, space') end; |
28860 | 341 |
|
16137 | 342 |
|
35679
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
343 |
(* alias *) |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
344 |
|
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
345 |
fun alias naming binding name space = |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
346 |
let |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
347 |
val (accs, accs') = accesses naming binding; |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
348 |
val space' = space |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
349 |
|> fold (add_name name) accs |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
350 |
|> map_name_space (fn (kind, internals, entries) => |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
351 |
let |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
352 |
val _ = Symtab.defined entries name orelse |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
353 |
error ("Undefined " ^ kind ^ " " ^ quote name); |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
354 |
val entries' = entries |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
355 |
|> Symtab.map_entry name (fn (externals, entry) => |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
356 |
(Library.merge (op =) (externals, accs'), entry)) |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
357 |
in (kind, internals, entries') end); |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
358 |
in space' end; |
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
359 |
|
da87ffdcf7ea
added Name_Space.alias -- additional accesses for an existing entry;
wenzelm
parents:
35432
diff
changeset
|
360 |
|
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
361 |
|
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
362 |
(** name spaces coupled with symbol tables **) |
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
363 |
|
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
364 |
type 'a table = T * 'a Symtab.table; |
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
365 |
|
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
366 |
fun define strict naming (binding, x) (space, tab) = |
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
367 |
let val (name, space') = declare strict naming binding space |
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
368 |
in (name, (space', Symtab.update (name, x) tab)) end; |
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
369 |
|
33096 | 370 |
fun empty_table kind = (empty kind, Symtab.empty); |
28860 | 371 |
|
33091
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
372 |
fun merge_tables ((space1, tab1), (space2, tab2)) = |
d23e75d4f7da
maintain abstract entry, with position, identity etc.;
wenzelm
parents:
33049
diff
changeset
|
373 |
(merge (space1, space2), Symtab.merge (K true) (tab1, tab2)); |
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
374 |
|
28991 | 375 |
fun join_tables f ((space1, tab1), (space2, tab2)) = |
376 |
(merge (space1, space2), Symtab.join f (tab1, tab2)); |
|
377 |
||
16848 | 378 |
fun ext_table (space, tab) = |
379 |
Symtab.fold (fn (name, x) => cons ((name, extern space name), x)) tab [] |
|
380 |
|> Library.sort_wrt (#2 o #1); |
|
381 |
||
382 |
fun dest_table tab = map (apfst #1) (ext_table tab); |
|
383 |
fun extern_table tab = map (apfst #2) (ext_table tab); |
|
16341
e573e5167eda
added type NameSpace.table with basic operations;
wenzelm
parents:
16262
diff
changeset
|
384 |
|
5012 | 385 |
end; |
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
386 |
|
33095
bbd52d2f8696
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents:
33091
diff
changeset
|
387 |
structure Basic_Name_Space: BASIC_NAME_SPACE = Name_Space; |
32738 | 388 |
open Basic_Name_Space; |
30215
47cce3d47e62
moved name space externalization flags back to name_space.ML;
wenzelm
parents:
30213
diff
changeset
|
389 |