45 val mandatory_path: string -> naming -> naming |
45 val mandatory_path: string -> naming -> naming |
46 val qualified_path: bool -> binding -> naming -> naming |
46 val qualified_path: bool -> binding -> naming -> naming |
47 val transform_binding: naming -> binding -> binding |
47 val transform_binding: naming -> binding -> binding |
48 val full_name: naming -> binding -> string |
48 val full_name: naming -> binding -> string |
49 val declare: bool -> naming -> binding -> T -> string * T |
49 val declare: bool -> naming -> binding -> T -> string * T |
|
50 val alias: naming -> binding -> string -> T -> T |
50 type 'a table = T * 'a Symtab.table |
51 type 'a table = T * 'a Symtab.table |
51 val define: bool -> naming -> binding * 'a -> 'a table -> string * 'a table |
52 val define: bool -> naming -> binding * 'a -> 'a table -> string * 'a table |
52 val empty_table: string -> 'a table |
53 val empty_table: string -> 'a table |
53 val merge_tables: 'a table * 'a table -> 'a table |
54 val merge_tables: 'a table * 'a table -> 'a table |
54 val join_tables: (string -> 'a * 'a -> 'a) (*Symtab.SAME*) -> |
55 val join_tables: (string -> 'a * 'a -> 'a) (*Symtab.SAME*) -> |
94 |
94 |
95 datatype T = |
95 datatype T = |
96 Name_Space of |
96 Name_Space of |
97 {kind: string, |
97 {kind: string, |
98 internals: (string list * string list) Symtab.table, (*visible, hidden*) |
98 internals: (string list * string list) Symtab.table, (*visible, hidden*) |
99 entries: entry Symtab.table}; |
99 entries: (xstring list * entry) Symtab.table}; (*externals, entry*) |
100 |
100 |
101 fun make_name_space (kind, internals, entries) = |
101 fun make_name_space (kind, internals, entries) = |
102 Name_Space {kind = kind, internals = internals, entries = entries}; |
102 Name_Space {kind = kind, internals = internals, entries = entries}; |
103 |
103 |
104 fun map_name_space f (Name_Space {kind = kind, internals = internals, entries = entries}) = |
104 fun map_name_space f (Name_Space {kind = kind, internals = internals, entries = entries}) = |
113 fun kind_of (Name_Space {kind, ...}) = kind; |
113 fun kind_of (Name_Space {kind, ...}) = kind; |
114 |
114 |
115 fun the_entry (Name_Space {kind, entries, ...}) name = |
115 fun the_entry (Name_Space {kind, entries, ...}) name = |
116 (case Symtab.lookup entries name of |
116 (case Symtab.lookup entries name of |
117 NONE => error ("Unknown " ^ kind ^ " " ^ quote name) |
117 NONE => error ("Unknown " ^ kind ^ " " ^ quote name) |
118 | SOME {concealed, group, theory_name, pos, id, ...} => |
118 | SOME (_, entry) => entry); |
119 {concealed = concealed, group = group, theory_name = theory_name, pos = pos, id = id}); |
|
120 |
119 |
121 fun is_concealed space name = #concealed (the_entry space name); |
120 fun is_concealed space name = #concealed (the_entry space name); |
122 |
121 |
123 |
122 |
124 (* name accesses *) |
123 (* name accesses *) |
132 | SOME ([], name' :: _) => (hidden name', true)); |
131 | SOME ([], name' :: _) => (hidden name', true)); |
133 |
132 |
134 fun get_accesses (Name_Space {entries, ...}) name = |
133 fun get_accesses (Name_Space {entries, ...}) name = |
135 (case Symtab.lookup entries name of |
134 (case Symtab.lookup entries name of |
136 NONE => [name] |
135 NONE => [name] |
137 | SOME {externals, ...} => externals); |
136 | SOME (externals, _) => externals); |
138 |
137 |
139 fun valid_accesses (Name_Space {internals, ...}) name = |
138 fun valid_accesses (Name_Space {internals, ...}) name = |
140 Symtab.fold (fn (xname, (names, _)) => |
139 Symtab.fold (fn (xname, (names, _)) => |
141 if not (null names) andalso hd names = name then cons xname else I) internals []; |
140 if not (null names) andalso hd names = name then cons xname else I) internals []; |
142 |
141 |
210 (K (fn ((names1, names1'), (names2, names2')) => |
209 (K (fn ((names1, names1'), (names2, names2')) => |
211 if pointer_eq (names1, names2) andalso pointer_eq (names1', names2') |
210 if pointer_eq (names1, names2) andalso pointer_eq (names1', names2') |
212 then raise Symtab.SAME |
211 then raise Symtab.SAME |
213 else (Library.merge (op =) (names1, names2), Library.merge (op =) (names1', names2')))); |
212 else (Library.merge (op =) (names1, names2), Library.merge (op =) (names1', names2')))); |
214 val entries' = (entries1, entries2) |> Symtab.join |
213 val entries' = (entries1, entries2) |> Symtab.join |
215 (fn name => fn (entry1, entry2) => |
214 (fn name => fn ((_, entry1), (_, entry2)) => |
216 if #id entry1 = #id entry2 then raise Symtab.SAME |
215 if #id entry1 = #id entry2 then raise Symtab.SAME |
217 else err_dup kind' (name, entry1) (name, entry2)); |
216 else err_dup kind' (name, entry1) (name, entry2)); |
218 in make_name_space (kind', internals', entries') end; |
217 in make_name_space (kind', internals', entries') end; |
219 |
218 |
220 |
219 |
309 in pairself (map Long_Name.implode) (sfxs @ pfxs, sfxs) end; |
308 in pairself (map Long_Name.implode) (sfxs @ pfxs, sfxs) end; |
310 |
309 |
311 |
310 |
312 (* declaration *) |
311 (* declaration *) |
313 |
312 |
314 fun new_entry strict entry = |
313 fun new_entry strict (name, (externals, entry)) = |
315 map_name_space (fn (kind, internals, entries) => |
314 map_name_space (fn (kind, internals, entries) => |
316 let |
315 let |
317 val entries' = |
316 val entries' = |
318 (if strict then Symtab.update_new else Symtab.update) entry entries |
317 (if strict then Symtab.update_new else Symtab.update) (name, (externals, entry)) entries |
319 handle Symtab.DUP dup => |
318 handle Symtab.DUP dup => |
320 err_dup kind (dup, the (Symtab.lookup entries dup)) entry; |
319 err_dup kind (dup, #2 (the (Symtab.lookup entries dup))) (name, entry); |
321 in (kind, internals, entries') end); |
320 in (kind, internals, entries') end); |
322 |
321 |
323 fun declare strict naming binding space = |
322 fun declare strict naming binding space = |
324 let |
323 let |
325 val Naming {group, theory_name, ...} = naming; |
324 val Naming {group, theory_name, ...} = naming; |
328 |
327 |
329 val name = Long_Name.implode (map fst spec); |
328 val name = Long_Name.implode (map fst spec); |
330 val _ = name = "" andalso err_bad binding; |
329 val _ = name = "" andalso err_bad binding; |
331 |
330 |
332 val entry = |
331 val entry = |
333 {externals = accs', |
332 {concealed = concealed, |
334 concealed = concealed, |
|
335 group = group, |
333 group = group, |
336 theory_name = theory_name, |
334 theory_name = theory_name, |
337 pos = Position.default (Binding.pos_of binding), |
335 pos = Position.default (Binding.pos_of binding), |
338 id = serial ()}; |
336 id = serial ()}; |
339 val space' = space |> fold (add_name name) accs |> new_entry strict (name, entry); |
337 val space' = space |
|
338 |> fold (add_name name) accs |
|
339 |> new_entry strict (name, (accs', entry)); |
340 in (name, space') end; |
340 in (name, space') end; |
|
341 |
|
342 |
|
343 (* alias *) |
|
344 |
|
345 fun alias naming binding name space = |
|
346 let |
|
347 val (accs, accs') = accesses naming binding; |
|
348 val space' = space |
|
349 |> fold (add_name name) accs |
|
350 |> map_name_space (fn (kind, internals, entries) => |
|
351 let |
|
352 val _ = Symtab.defined entries name orelse |
|
353 error ("Undefined " ^ kind ^ " " ^ quote name); |
|
354 val entries' = entries |
|
355 |> Symtab.map_entry name (fn (externals, entry) => |
|
356 (Library.merge (op =) (externals, accs'), entry)) |
|
357 in (kind, internals, entries') end); |
|
358 in space' end; |
341 |
359 |
342 |
360 |
343 |
361 |
344 (** name spaces coupled with symbol tables **) |
362 (** name spaces coupled with symbol tables **) |
345 |
363 |